Code

frontend/parser: Simplified handling of MATCHING and FILTER clauses.
authorSebastian Harl <sh@tokkee.org>
Sat, 2 Aug 2014 19:51:13 +0000 (21:51 +0200)
committerSebastian Harl <sh@tokkee.org>
Sat, 2 Aug 2014 19:51:13 +0000 (21:51 +0200)
Make LOOKUP MATCHING clauses optional.

src/frontend/grammar.y
t/unit/frontend/parser_test.c

index 08d194deeac438290b55dd570356e31d6099a449..a444423b409ce6854190b5d9e9ef7812fe702a5a 100644 (file)
@@ -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 <m> matcher
@@ -234,47 +236,42 @@ list_statement:
  * Returns detailed information about <type> 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
index dade011bd31cd2b505382df27fe57903b8f94b6f..1b5cb242e9c94b3bc4c45eb3e4e638f6f5702d44 100644 (file)
@@ -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;