From: Sebastian Harl Date: Sat, 2 Aug 2014 19:51:13 +0000 (+0200) Subject: frontend/parser: Simplified handling of MATCHING and FILTER clauses. X-Git-Tag: sysdb-0.4.0~45 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=05f8e6e8f6132ef71e51280a75a9ad9aa0c00665;p=sysdb.git frontend/parser: Simplified handling of MATCHING and FILTER clauses. Make LOOKUP MATCHING clauses optional. --- diff --git a/src/frontend/grammar.y b/src/frontend/grammar.y index 08d194d..a444423 100644 --- a/src/frontend/grammar.y +++ b/src/frontend/grammar.y @@ -115,6 +115,8 @@ sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg); fetch_statement list_statement lookup_statement + matching_clause + filter_clause condition %type matcher @@ -234,47 +236,42 @@ list_statement: * Returns detailed information about matching condition. */ lookup_statement: - LOOKUP IDENTIFIER MATCHING condition + LOOKUP IDENTIFIER matching_clause filter_clause { /* TODO: support other types as well */ if (strcasecmp($2, "hosts")) { char errmsg[strlen($2) + 32]; snprintf(errmsg, sizeof(errmsg), - YY_("unknown table %s"), $2); + YY_("unknown data-source %s"), $2); sdb_fe_yyerror(&yylloc, scanner, errmsg); free($2); $2 = NULL; + sdb_object_deref(SDB_OBJ($3)); sdb_object_deref(SDB_OBJ($4)); YYABORT; } $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL, conn_lookup_t, conn_lookup_destroy)); - CONN_LOOKUP($$)->matcher = CONN_MATCHER($4); + CONN_LOOKUP($$)->matcher = CONN_MATCHER($3); + CONN_LOOKUP($$)->filter = CONN_MATCHER($4); $$->cmd = CONNECTION_LOOKUP; free($2); $2 = NULL; } + ; + +matching_clause: + MATCHING condition { $$ = $2; } | - LOOKUP IDENTIFIER MATCHING condition FILTER condition - { - /* TODO: support other types as well */ - if (strcasecmp($2, "hosts")) { - char errmsg[strlen($2) + 32]; - snprintf(errmsg, sizeof(errmsg), - YY_("unknown table %s"), $2); - sdb_fe_yyerror(&yylloc, scanner, errmsg); - free($2); $2 = NULL; - sdb_object_deref(SDB_OBJ($4)); - YYABORT; - } + /* empty */ { $$ = NULL; } - $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL, - conn_lookup_t, conn_lookup_destroy)); - CONN_LOOKUP($$)->matcher = CONN_MATCHER($4); - CONN_LOOKUP($$)->filter = CONN_MATCHER($6); - $$->cmd = CONNECTION_LOOKUP; - free($2); $2 = NULL; - } - ; +filter_clause: + FILTER condition { $$ = $2; } + | + /* empty */ { $$ = NULL; } + +/* + * Basic expressions. + */ condition: matcher diff --git a/t/unit/frontend/parser_test.c b/t/unit/frontend/parser_test.c index dade011..1b5cb24 100644 --- a/t/unit/frontend/parser_test.c +++ b/t/unit/frontend/parser_test.c @@ -58,6 +58,7 @@ START_TEST(test_parse) { "LIST;", -1, 1, CONNECTION_LIST }, { "LIST; INVALID", 5, 1, CONNECTION_LIST }, + { "LOOKUP hosts", -1, 1, CONNECTION_LOOKUP }, { "LOOKUP hosts MATCHING " "host = 'host'", -1, 1, CONNECTION_LOOKUP }, { "LOOKUP hosts MATCHING NOT " @@ -184,9 +185,14 @@ START_TEST(test_parse) { "LIST; INVALID", 8, -1, 0 }, { "/* some incomplete", -1, -1, 0 }, - { "LOOKUP hosts", -1, -1, 0 }, + { "LOOKUP foo", -1, -1, 0 }, { "LOOKUP foo MATCHING " "host = 'host'", -1, -1, 0 }, + { "LOOKUP foo FILTER " + "host = 'host'", -1, -1, 0 }, + { "LOOKUP foo MATCHING " + "host = 'host' FILTER " + "host = 'host'", -1, -1, 0 }, }; size_t i;