]> granicus.if.org Git - icinga2/commitdiff
Added config parser.
authorGunnar Beutner <gunnar.beutner@netways.de>
Thu, 31 May 2012 06:45:02 +0000 (08:45 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Thu, 31 May 2012 06:45:02 +0000 (08:45 +0200)
components/configfile/Makefile.am
components/configfile/configcontext.cpp [new file with mode: 0644]
components/configfile/configcontext.h [new file with mode: 0644]
components/configfile/icinga_lexer.ll [new file with mode: 0644]
components/configfile/icinga_parser.yy [new file with mode: 0644]
configure.ac

index 931ae6790258e8ef2e0906da4cb098b459e9bdd3..f1d6783ff9934fbad2d8f8b80c0519a5210299af 100644 (file)
@@ -3,9 +3,17 @@
 pkglib_LTLIBRARIES = \
        configfile.la
 
+BUILD_SOURCES = icinga_parser.h
+
+AM_YFLAGS = -d
+
 configfile_la_SOURCES = \
+       configcontext.cpp \
+       configcontext.h \
        configfilecomponent.cpp \
        configfilecomponent.h \
+       icinga_lexer.ll \
+       icinga_parser.yy \
        i2-configfile.h
 
 configfile_la_CPPFLAGS = \
diff --git a/components/configfile/configcontext.cpp b/components/configfile/configcontext.cpp
new file mode 100644 (file)
index 0000000..1abfa20
--- /dev/null
@@ -0,0 +1,16 @@
+#include <iostream>
+#include "configcontext.h"
+
+using namespace std;
+
+ConfigContext::ConfigContext(istream *input)
+{
+       Input = input;
+       InitializeScanner();
+}
+
+ConfigContext::~ConfigContext(void)
+{
+       DestroyScanner();
+}
+
diff --git a/components/configfile/configcontext.h b/components/configfile/configcontext.h
new file mode 100644 (file)
index 0000000..4d4ada0
--- /dev/null
@@ -0,0 +1,20 @@
+#ifndef CONFIGCONTEXT_H
+#define CONFIGCONTEXT_H
+
+class ConfigContext
+{
+public:
+       ConfigContext(std::istream *input = &std::cin);
+       virtual ~ConfigContext(void);
+
+       std::istream *Input;
+       void *Scanner;
+
+private:
+       void InitializeScanner(void);
+       void DestroyScanner(void);
+};
+
+int icingaparse(ConfigContext *context);
+
+#endif /* CONFIGCONTEXT_H */
diff --git a/components/configfile/icinga_lexer.ll b/components/configfile/icinga_lexer.ll
new file mode 100644 (file)
index 0000000..e15183b
--- /dev/null
@@ -0,0 +1,64 @@
+%{
+#include <iostream>
+#include "configcontext.h"
+#include "icinga.tab.h"
+
+#define YY_EXTRA_TYPE ConfigContext *
+#define YY_USER_ACTION yylloc->first_line = yylineno;
+
+#define YY_INPUT(buf, result, max_size)                \
+do {                                           \
+       yyextra->Input->read(buf, max_size);    \
+       result = yyextra->Input->gcount();      \
+} while (0)
+%}
+
+%option reentrant noyywrap yylineno
+%option prefix="icinga"
+%option bison-bridge bison-locations
+
+%x IN_C_COMMENT
+
+%%
+abstract               return T_ABSTRACT;
+local                  return T_LOCAL;
+object                 return T_OBJECT;
+include                        return T_INCLUDE;
+inherits               return T_INHERITS;
+[a-zA-Z][a-zA-Z0-9]*   return T_IDENTIFIER;
+\"[^\"]+\"             { yytext[yyleng-1] = '\0'; yylval->text = strdup(yytext + 1); return T_STRING; }
+[0-9]+                 { yylval->num = atoi(yytext); return T_NUMBER; }
+\{                     return T_OPEN_BRACE;
+\}                     return T_CLOSE_BRACE;
+\[                     return T_OPEN_BRACKET;
+\]                     return T_CLOSE_BRACKET;
+,                      return T_COMMA;
+=                      return T_EQUAL;
+
+<INITIAL>{
+"/*"                   BEGIN(IN_C_COMMENT);
+}
+
+<IN_C_COMMENT>{
+"*/"                   BEGIN(INITIAL);
+[^*]+                  /* ignore comment */
+"*"                    /* ignore star */
+}
+
+\/\/[^\n]+             /* ignore C++-style comments */
+#[^\n]+                        /* ignore shell-style comments */
+[ \t\n]+               /* ignore whitespace */
+%%
+
+
+void ConfigContext::InitializeScanner(void)
+{
+       yylex_init(&Scanner);
+       yyset_extra(this, Scanner);
+}
+
+void ConfigContext::DestroyScanner(void)
+{
+       yylex_destroy(Scanner);
+}
+
diff --git a/components/configfile/icinga_parser.yy b/components/configfile/icinga_parser.yy
new file mode 100644 (file)
index 0000000..fc6adb1
--- /dev/null
@@ -0,0 +1,105 @@
+%pure-parser
+%name-prefix="icinga"
+
+%locations
+%defines
+%error-verbose
+
+%parse-param { ConfigContext *context }
+%lex-param { void *scanner }
+
+%union {
+       char *text;
+       int num;
+}
+
+%token <text> T_STRING
+%token <num> T_NUMBER
+%token T_IDENTIFIER
+%token T_OPEN_BRACE
+%token T_CLOSE_BRACE
+%token T_OPEN_BRACKET
+%token T_CLOSE_BRACKET
+%token T_EQUAL
+%token T_COMMA
+%token T_ABSTRACT
+%token T_LOCAL
+%token T_OBJECT
+%token T_INCLUDE
+%token T_INHERITS
+
+%{
+#include <iostream>
+#include "configcontext.h"
+
+int icingalex(YYSTYPE *lvalp, YYLTYPE *llocp, void *scanner);
+
+void icingaerror(YYLTYPE *locp, ConfigContext *context, const char *err)
+{
+       std::cout << locp->first_line << ":" << locp->first_column
+           << "-"
+           << locp->last_line << ":" << locp->last_column
+           << ": " << err << std::endl;
+}
+
+#define scanner context->Scanner
+
+%}
+
+%%
+statements: /* empty */
+       | statements statement
+       ;
+
+statement: object | include
+       ;
+
+include: T_INCLUDE T_STRING
+       ;
+
+object: attributes_list object_declaration
+       | object_declaration
+       ;
+
+object_declaration: T_OBJECT T_IDENTIFIER T_STRING inherits_specifier dictionary
+       ;
+
+attributes_list: attributes_list attribute
+       | attribute
+       ;
+
+attribute: T_ABSTRACT
+       | T_LOCAL
+       ;
+
+inherits_list: T_STRING
+       | inherits_list T_COMMA T_STRING
+       ;
+
+inherits_specifier: /* empty */
+       | T_INHERITS inherits_list
+       ;
+
+dictionary: T_OPEN_BRACE nvpairs T_CLOSE_BRACE
+       ;
+
+nvpairs: /* empty */
+       | nvpair
+       | nvpairs T_COMMA nvpair
+       ;
+
+nvpair: T_IDENTIFIER T_EQUAL value
+       ;
+
+value: T_STRING | T_NUMBER | array | dictionary
+       ;
+
+array: T_OPEN_BRACKET arrayitems T_CLOSE_BRACKET
+       ;
+
+arrayitems:
+       /* empty */
+       | value
+       | arrayitems T_COMMA value
+       ;
+%%
index 0bee8497d965bfd1740d55e07e30529d16f6efd5..5577e5f1c61c7ae32903896a1fe09ed94ea6e66a 100644 (file)
@@ -46,6 +46,8 @@ DX_PS_FEATURE(OFF)
 DX_INIT_DOXYGEN([icinga], [Doxyfile], [doc])
 
 AC_PROG_INSTALL
+AM_PROG_LEX
+AC_PROG_YACC
 AC_PROG_LIBTOOL
 AX_CXX_COMPILE_STDCXX_0X
 AX_CXX_GCC_ABI_DEMANGLE