Moved config parser into dyn/.

This commit is contained in:
Gunnar Beutner 2012-05-31 09:43:46 +02:00
parent d76e4b2a5b
commit 3c92c7b2d7
14 changed files with 116 additions and 118 deletions

View File

@ -3,17 +3,9 @@
pkglib_LTLIBRARIES = \ pkglib_LTLIBRARIES = \
configfile.la configfile.la
BUILD_SOURCES = icinga_parser.h
AM_YFLAGS = -d
configfile_la_SOURCES = \ configfile_la_SOURCES = \
configcontext.cpp \
configcontext.h \
configfilecomponent.cpp \ configfilecomponent.cpp \
configfilecomponent.h \ configfilecomponent.h \
icinga_lexer.ll \
icinga_parser.yy \
i2-configfile.h i2-configfile.h
configfile_la_CPPFLAGS = \ configfile_la_CPPFLAGS = \

View File

@ -1,15 +0,0 @@
#include "i2-configfile.h"
using namespace icinga;
ConfigContext::ConfigContext(istream *input)
{
Input = input;
InitializeScanner();
}
ConfigContext::~ConfigContext(void)
{
DestroyScanner();
}

View File

@ -30,7 +30,6 @@
#include <i2-base.h> #include <i2-base.h>
#include <i2-icinga.h> #include <i2-icinga.h>
#include "configcontext.h"
#include "configfilecomponent.h" #include "configfilecomponent.h"
#endif /* I2CONFIGFILECOMPONENT_H */ #endif /* I2CONFIGFILECOMPONENT_H */

View File

@ -4,7 +4,15 @@
pkglib_LTLIBRARIES = \ pkglib_LTLIBRARIES = \
libdyn.la libdyn.la
BUILT_SOURCES = config_parser.h
AM_YFLAGS = -d
libdyn_la_SOURCES = \ libdyn_la_SOURCES = \
configcontext.cpp \
configcontext.h \
config_lexer.ll
config_parser.yy \
i2-dyn.h \ i2-dyn.h \
dynamicobject.cpp \ dynamicobject.cpp \
dynamicobject.h \ dynamicobject.h \

View File

@ -1,5 +1,5 @@
#line 3 "icinga_lexer.cc" #line 3 "config_lexer.cc"
#define YY_INT_ALIGNED short int #define YY_INT_ALIGNED short int
@ -500,10 +500,10 @@ static yyconst flex_int32_t yy_rule_can_match_eol[23] =
#define yymore() yymore_used_but_not_detected #define yymore() yymore_used_but_not_detected
#define YY_MORE_ADJ 0 #define YY_MORE_ADJ 0
#define YY_RESTORE_YY_MORE_OFFSET #define YY_RESTORE_YY_MORE_OFFSET
#line 1 "icinga_lexer.ll" #line 1 "config_lexer.ll"
#line 2 "icinga_lexer.ll" #line 2 "config_lexer.ll"
#include "i2-configfile.h" #include "i2-dyn.h"
#include "icinga_parser.h" #include "config_parser.h"
using namespace icinga; using namespace icinga;
@ -512,12 +512,11 @@ using namespace icinga;
#define YY_INPUT(buf, result, max_size) \ #define YY_INPUT(buf, result, max_size) \
do { \ do { \
yyextra->Input->read(buf, max_size); \ result = yyextra->ReadInput(buf, max_size); \
result = yyextra->Input->gcount(); \
} while (0) } while (0)
#define YY_NO_UNISTD_H 1 #define YY_NO_UNISTD_H 1
#line 521 "icinga_lexer.cc" #line 520 "config_lexer.cc"
#define INITIAL 0 #define INITIAL 0
#define IN_C_COMMENT 1 #define IN_C_COMMENT 1
@ -764,9 +763,9 @@ YY_DECL
register int yy_act; register int yy_act;
struct yyguts_t * yyg = (struct yyguts_t*)yyscanner; struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
#line 23 "icinga_lexer.ll" #line 22 "config_lexer.ll"
#line 770 "icinga_lexer.cc" #line 769 "config_lexer.cc"
yylval = yylval_param; yylval = yylval_param;
@ -863,122 +862,122 @@ do_action: /* This label is used only to access EOF actions. */
case 1: case 1:
YY_RULE_SETUP YY_RULE_SETUP
#line 24 "icinga_lexer.ll" #line 23 "config_lexer.ll"
return T_ABSTRACT; return T_ABSTRACT;
YY_BREAK YY_BREAK
case 2: case 2:
YY_RULE_SETUP YY_RULE_SETUP
#line 25 "icinga_lexer.ll" #line 24 "config_lexer.ll"
return T_LOCAL; return T_LOCAL;
YY_BREAK YY_BREAK
case 3: case 3:
YY_RULE_SETUP YY_RULE_SETUP
#line 26 "icinga_lexer.ll" #line 25 "config_lexer.ll"
return T_OBJECT; return T_OBJECT;
YY_BREAK YY_BREAK
case 4: case 4:
YY_RULE_SETUP YY_RULE_SETUP
#line 27 "icinga_lexer.ll" #line 26 "config_lexer.ll"
return T_INCLUDE; return T_INCLUDE;
YY_BREAK YY_BREAK
case 5: case 5:
YY_RULE_SETUP YY_RULE_SETUP
#line 28 "icinga_lexer.ll" #line 27 "config_lexer.ll"
return T_INHERITS; return T_INHERITS;
YY_BREAK YY_BREAK
case 6: case 6:
YY_RULE_SETUP YY_RULE_SETUP
#line 29 "icinga_lexer.ll" #line 28 "config_lexer.ll"
return T_IDENTIFIER; return T_IDENTIFIER;
YY_BREAK YY_BREAK
case 7: case 7:
/* rule 7 can match eol */ /* rule 7 can match eol */
YY_RULE_SETUP YY_RULE_SETUP
#line 30 "icinga_lexer.ll" #line 29 "config_lexer.ll"
{ yytext[yyleng-1] = '\0'; yylval->text = strdup(yytext + 1); return T_STRING; } { yytext[yyleng-1] = '\0'; yylval->text = strdup(yytext + 1); return T_STRING; }
YY_BREAK YY_BREAK
case 8: case 8:
YY_RULE_SETUP YY_RULE_SETUP
#line 31 "icinga_lexer.ll" #line 30 "config_lexer.ll"
{ yylval->num = atoi(yytext); return T_NUMBER; } { yylval->num = atoi(yytext); return T_NUMBER; }
YY_BREAK YY_BREAK
case 9: case 9:
YY_RULE_SETUP YY_RULE_SETUP
#line 32 "icinga_lexer.ll" #line 31 "config_lexer.ll"
return T_OPEN_BRACE; return T_OPEN_BRACE;
YY_BREAK YY_BREAK
case 10: case 10:
YY_RULE_SETUP YY_RULE_SETUP
#line 33 "icinga_lexer.ll" #line 32 "config_lexer.ll"
return T_CLOSE_BRACE; return T_CLOSE_BRACE;
YY_BREAK YY_BREAK
case 11: case 11:
YY_RULE_SETUP YY_RULE_SETUP
#line 34 "icinga_lexer.ll" #line 33 "config_lexer.ll"
return T_OPEN_BRACKET; return T_OPEN_BRACKET;
YY_BREAK YY_BREAK
case 12: case 12:
YY_RULE_SETUP YY_RULE_SETUP
#line 35 "icinga_lexer.ll" #line 34 "config_lexer.ll"
return T_CLOSE_BRACKET; return T_CLOSE_BRACKET;
YY_BREAK YY_BREAK
case 13: case 13:
YY_RULE_SETUP YY_RULE_SETUP
#line 36 "icinga_lexer.ll" #line 35 "config_lexer.ll"
return T_COMMA; return T_COMMA;
YY_BREAK YY_BREAK
case 14: case 14:
YY_RULE_SETUP YY_RULE_SETUP
#line 37 "icinga_lexer.ll" #line 36 "config_lexer.ll"
return T_EQUAL; return T_EQUAL;
YY_BREAK YY_BREAK
case 15: case 15:
YY_RULE_SETUP YY_RULE_SETUP
#line 40 "icinga_lexer.ll" #line 39 "config_lexer.ll"
BEGIN(IN_C_COMMENT); BEGIN(IN_C_COMMENT);
YY_BREAK YY_BREAK
case 16: case 16:
YY_RULE_SETUP YY_RULE_SETUP
#line 44 "icinga_lexer.ll" #line 43 "config_lexer.ll"
BEGIN(INITIAL); BEGIN(INITIAL);
YY_BREAK YY_BREAK
case 17: case 17:
/* rule 17 can match eol */ /* rule 17 can match eol */
YY_RULE_SETUP YY_RULE_SETUP
#line 45 "icinga_lexer.ll" #line 44 "config_lexer.ll"
/* ignore comment */ /* ignore comment */
YY_BREAK YY_BREAK
case 18: case 18:
YY_RULE_SETUP YY_RULE_SETUP
#line 46 "icinga_lexer.ll" #line 45 "config_lexer.ll"
/* ignore star */ /* ignore star */
YY_BREAK YY_BREAK
case 19: case 19:
YY_RULE_SETUP YY_RULE_SETUP
#line 49 "icinga_lexer.ll" #line 48 "config_lexer.ll"
/* ignore C++-style comments */ /* ignore C++-style comments */
YY_BREAK YY_BREAK
case 20: case 20:
YY_RULE_SETUP YY_RULE_SETUP
#line 50 "icinga_lexer.ll" #line 49 "config_lexer.ll"
/* ignore shell-style comments */ /* ignore shell-style comments */
YY_BREAK YY_BREAK
case 21: case 21:
/* rule 21 can match eol */ /* rule 21 can match eol */
YY_RULE_SETUP YY_RULE_SETUP
#line 51 "icinga_lexer.ll" #line 50 "config_lexer.ll"
/* ignore whitespace */ /* ignore whitespace */
YY_BREAK YY_BREAK
case 22: case 22:
YY_RULE_SETUP YY_RULE_SETUP
#line 52 "icinga_lexer.ll" #line 51 "config_lexer.ll"
ECHO; ECHO;
YY_BREAK YY_BREAK
#line 982 "icinga_lexer.cc" #line 981 "config_lexer.cc"
case YY_STATE_EOF(INITIAL): case YY_STATE_EOF(INITIAL):
case YY_STATE_EOF(IN_C_COMMENT): case YY_STATE_EOF(IN_C_COMMENT):
yyterminate(); yyterminate();
@ -2166,20 +2165,20 @@ void yyfree (void * ptr , yyscan_t yyscanner)
#define YYTABLES_NAME "yytables" #define YYTABLES_NAME "yytables"
#line 52 "icinga_lexer.ll" #line 51 "config_lexer.ll"
void ConfigContext::InitializeScanner(void) void ConfigContext::InitializeScanner(void)
{ {
yylex_init(&Scanner); yylex_init(&m_Scanner);
yyset_extra(this,Scanner); yyset_extra(this,m_Scanner);
} }
void ConfigContext::DestroyScanner(void) void ConfigContext::DestroyScanner(void)
{ {
yylex_destroy(Scanner); yylex_destroy(m_Scanner);
} }

View File

@ -1,6 +1,6 @@
%{ %{
#include "i2-configfile.h" #include "i2-dyn.h"
#include "icinga_parser.h" #include "config_parser.h"
using namespace icinga; using namespace icinga;
@ -9,8 +9,7 @@ using namespace icinga;
#define YY_INPUT(buf, result, max_size) \ #define YY_INPUT(buf, result, max_size) \
do { \ do { \
yyextra->Input->read(buf, max_size); \ result = yyextra->ReadInput(buf, max_size); \
result = yyextra->Input->gcount(); \
} while (0) } while (0)
%} %}
@ -54,12 +53,12 @@ inherits return T_INHERITS;
void ConfigContext::InitializeScanner(void) void ConfigContext::InitializeScanner(void)
{ {
yylex_init(&Scanner); yylex_init(&m_Scanner);
yyset_extra(this, Scanner); yyset_extra(this, m_Scanner);
} }
void ConfigContext::DestroyScanner(void) void ConfigContext::DestroyScanner(void)
{ {
yylex_destroy(Scanner); yylex_destroy(m_Scanner);
} }

View File

@ -176,18 +176,22 @@ int yylex(YYSTYPE *lvalp, YYLTYPE *llocp, void *scanner);
void yyerror(YYLTYPE *locp, ConfigContext *context, const char *err) void yyerror(YYLTYPE *locp, ConfigContext *context, const char *err)
{ {
std::cout << locp->first_line << ":" << locp->first_column stringstream message;
message << locp->first_line << ":" << locp->first_column
<< "-" << "-"
<< locp->last_line << ":" << locp->last_column << locp->last_line << ":" << locp->last_column
<< ": " << err << std::endl; << ": " << err << std::endl;
throw runtime_error(message.str());
} }
#define scanner context->Scanner #define scanner (context->GetScanner())
/* Line 343 of yacc.c */ /* Line 343 of yacc.c */
#line 191 "icinga_parser.cc" #line 195 "icinga_parser.cc"
#ifdef short #ifdef short
# undef short # undef short
@ -486,10 +490,10 @@ static const yytype_int8 yyrhs[] =
/* YYRLINE[YYN] -- source line where rule number YYN was defined. */ /* YYRLINE[YYN] -- source line where rule number YYN was defined. */
static const yytype_uint8 yyrline[] = static const yytype_uint8 yyrline[] =
{ {
0, 50, 50, 51, 54, 54, 57, 60, 61, 64, 0, 54, 54, 55, 58, 58, 61, 64, 65, 68,
67, 68, 71, 72, 75, 76, 79, 80, 83, 86, 71, 72, 75, 76, 79, 80, 83, 84, 87, 90,
87, 88, 91, 94, 94, 94, 94, 97, 100, 102, 91, 92, 95, 98, 98, 98, 98, 101, 104, 106,
103 107
}; };
#endif #endif
@ -1494,7 +1498,7 @@ yyreduce:
/* Line 1806 of yacc.c */ /* Line 1806 of yacc.c */
#line 1498 "icinga_parser.cc" #line 1502 "icinga_parser.cc"
default: break; default: break;
} }
/* User semantic actions sometimes alter yychar, and that requires /* User semantic actions sometimes alter yychar, and that requires
@ -1732,6 +1736,6 @@ yyreturn:
/* Line 2067 of yacc.c */ /* Line 2067 of yacc.c */
#line 105 "icinga_parser.yy" #line 109 "icinga_parser.yy"

View File

@ -36,13 +36,17 @@ int yylex(YYSTYPE *lvalp, YYLTYPE *llocp, void *scanner);
void yyerror(YYLTYPE *locp, ConfigContext *context, const char *err) void yyerror(YYLTYPE *locp, ConfigContext *context, const char *err)
{ {
std::cout << locp->first_line << ":" << locp->first_column stringstream message;
message << locp->first_line << ":" << locp->first_column
<< "-" << "-"
<< locp->last_line << ":" << locp->last_column << locp->last_line << ":" << locp->last_column
<< ": " << err << std::endl; << ": " << err << endl;
throw runtime_error(message.str());
} }
#define scanner context->Scanner #define scanner (context->GetScanner())
%} %}

26
dyn/configcontext.cpp Normal file
View File

@ -0,0 +1,26 @@
#include "i2-dyn.h"
using namespace icinga;
ConfigContext::ConfigContext(istream *input)
{
m_Input = input;
InitializeScanner();
}
ConfigContext::~ConfigContext(void)
{
DestroyScanner();
}
size_t ConfigContext::ReadInput(char *buffer, size_t max_size)
{
m_Input->read(buffer, max_size);
return m_Input->gcount();
}
void *ConfigContext::GetScanner(void) const
{
return m_Scanner;
}

View File

@ -29,10 +29,13 @@ public:
ConfigContext(istream *input = &cin); ConfigContext(istream *input = &cin);
virtual ~ConfigContext(void); virtual ~ConfigContext(void);
istream *Input; size_t ReadInput(char *buffer, size_t max_bytes);
void *Scanner; void *GetScanner(void) const;
private: private:
istream *m_Input;
void *m_Scanner;
void InitializeScanner(void); void InitializeScanner(void);
void DestroyScanner(void); void DestroyScanner(void);
}; };

View File

@ -35,6 +35,7 @@
# define I2_DYN_API I2_IMPORT # define I2_DYN_API I2_IMPORT
#endif /* I2_DYN_BUILD */ #endif /* I2_DYN_BUILD */
#include "configcontext.h"
#include "dynamicdictionary.h" #include "dynamicdictionary.h"
#include "dynamicobject.h" #include "dynamicobject.h"
#include "objectset.h" #include "objectset.h"

View File

@ -12,6 +12,9 @@ dyntest_CPPFLAGS = \
$(BOOST_CPPFLAGS) \ $(BOOST_CPPFLAGS) \
-I${top_srcdir}/base \ -I${top_srcdir}/base \
-I${top_srcdir}/dyn \ -I${top_srcdir}/dyn \
-I${top_srcdir}/jsonrpc \
-I${top_srcdir}/icinga \
-I${top_srcdir}/components/configfile \
-I${top_srcdir} -I${top_srcdir}
dyntest_LDFLAGS = \ dyntest_LDFLAGS = \
@ -19,4 +22,5 @@ dyntest_LDFLAGS = \
dyntest_LDADD = \ dyntest_LDADD = \
${top_builddir}/base/libbase.la \ ${top_builddir}/base/libbase.la \
${top_builddir}/dyn/libdyn.la ${top_builddir}/dyn/libdyn.la \
${top_builddir}/components/configfile/configfile.la

View File

@ -1,38 +1,12 @@
#include <i2-dyn.h> #include <i2-configfile.h>
using namespace icinga; using namespace icinga;
bool propgetter(string prop, const Object::Ptr& object, string *key)
{
DynamicObject::Ptr dobj = dynamic_pointer_cast<DynamicObject>(object);
return dobj->GetConfig()->GetProperty(prop, key);
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
for (int i = 0; i < 10000; i++) { stringstream config;
stringstream sname; config << "object process \"foo\" {}";
sname << "foo" << i; ConfigContext ctx(&config);
yyparse(&ctx);
DynamicObject::Ptr dobj = make_shared<DynamicObject>();
dobj->GetConfig()->SetProperty("type", "process");
dobj->GetConfig()->SetProperty("name", sname.str());
dobj->Commit();
}
ObjectMap::Ptr byType = make_shared<ObjectMap>(ObjectSet::GetAllObjects(),
bind(&propgetter, "type", _1, _2));
byType->Start();
ObjectMap::Ptr byName = make_shared<ObjectMap>(ObjectSet::GetAllObjects(),
bind(&propgetter, "name", _1, _2));
byName->Start();
ObjectMap::Range processes = byType->GetRange("process");
cout << distance(processes.first, processes.second) << " processes" << endl;
ObjectMap::Range foo55 = byName->GetRange("foo55");
cout << distance(foo55.first, foo55.second) << " foo55s" << endl;
return 0; return 0;
} }