From c63a274a489e53a9e84e9772839a16a1c2a3b2b8 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Sat, 5 Apr 2014 20:06:28 +0200 Subject: [PATCH] frontend: Accept compare matchers as expressions. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit … replacing the current placeholder. For now, this is specific to '=' (equal) and '=~' (regex) comparison. --- src/frontend/grammar.y | 64 ++++++++++++++++++++++++++++++++++-------- src/frontend/scanner.l | 3 ++ 2 files changed, 56 insertions(+), 11 deletions(-) diff --git a/src/frontend/grammar.y b/src/frontend/grammar.y index 50bb00e..6c52b74 100644 --- a/src/frontend/grammar.y +++ b/src/frontend/grammar.y @@ -32,6 +32,7 @@ #include "frontend/grammar.h" #include "core/store.h" +#include "core/store-private.h" #include "utils/error.h" #include "utils/llist.h" @@ -69,6 +70,8 @@ sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg); sdb_llist_t *list; sdb_conn_node_t *node; + + sdb_store_matcher_t *m; } %start statements @@ -77,6 +80,8 @@ sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg); %token WHERE +%token CMP_EQUAL CMP_REGEX + %token IDENTIFIER STRING %token FETCH LIST LOOKUP @@ -87,6 +92,8 @@ sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg); lookup_statement expression +%type compare_matcher + %% statements: @@ -164,8 +171,7 @@ fetch_statement: conn_fetch_t, conn_fetch_destroy)); CONN_FETCH($$)->name = strdup($2); $$->cmd = CONNECTION_FETCH; - free($2); - $2 = NULL; + free($2); $2 = NULL; } ; @@ -204,23 +210,59 @@ lookup_statement: conn_lookup_t, conn_lookup_destroy)); CONN_LOOKUP($$)->matcher = CONN_MATCHER($4); $$->cmd = CONNECTION_LOOKUP; - free($2); - $2 = NULL; + free($2); $2 = NULL; } ; expression: - STRING + compare_matcher { + sdb_store_matcher_t *m = $1; + if (! m) { + /* TODO: improve error reporting */ + sdb_fe_yyerror(&yylloc, scanner, + YY_("syntax error, invalid expression")); + YYABORT; + } + $$ = SDB_CONN_NODE(sdb_object_create_T(/* name = */ NULL, conn_node_matcher_t)); $$->cmd = CONNECTION_EXPR; - /* XXX: this is just a placeholder for now */ - CONN_MATCHER($$)->matcher = sdb_store_host_matcher($1, - /* name_re = */ NULL, /* service = */ NULL, - /* attr = */ NULL); - free($1); - $1 = NULL; + + if (M(m)->type == MATCHER_HOST) + CONN_MATCHER($$)->matcher = m; + else if (M(m)->type == MATCHER_SERVICE) + CONN_MATCHER($$)->matcher = sdb_store_host_matcher(NULL, + /* name_re = */ NULL, /* service = */ m, + /* attr = */ NULL); + else if (M(m)->type == MATCHER_ATTR) + CONN_MATCHER($$)->matcher = sdb_store_host_matcher(NULL, + /* name_re = */ NULL, /* service = */ NULL, + /* attr = */ m); + } + ; + +/* + * . + * + * Parse matchers comparing object attributes with a value. + */ +compare_matcher: + IDENTIFIER '.' IDENTIFIER CMP_EQUAL STRING + { + $$ = sdb_store_matcher_parse_cmp($1, $3, "=", $5); + /* TODO: simplify memory management in the parser */ + free($1); $1 = NULL; + free($3); $3 = NULL; + free($5); $5 = NULL; + } + | + IDENTIFIER '.' IDENTIFIER CMP_REGEX STRING + { + $$ = sdb_store_matcher_parse_cmp($1, $3, "=~", $5); + free($1); $1 = NULL; + free($3); $3 = NULL; + free($5); $5 = NULL; } ; diff --git a/src/frontend/scanner.l b/src/frontend/scanner.l index 4dcd5e2..1e371f4 100644 --- a/src/frontend/scanner.l +++ b/src/frontend/scanner.l @@ -110,6 +110,9 @@ string ('[^']*') return STRING; } += { return CMP_EQUAL; } +=~ { return CMP_REGEX; } + . { /* XXX: */ return yytext[0]; } %% -- 2.30.2