Code

frontend/parser: Let ‘LIST’ and ‘FETCH’ accept optional filters as well.
[sysdb.git] / src / frontend / grammar.y
index 92bfd1f5c60d2ab24d5e0b6f82b90e7e4ce73c71..a2ae448cc744166875afd8e0f4904b5a186ad5cf 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
@@ -204,11 +206,12 @@ statement:
  * Retrieve detailed information about a single host.
  */
 fetch_statement:
-       FETCH STRING
+       FETCH STRING filter_clause
                {
                        $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
                                                conn_fetch_t, conn_fetch_destroy));
                        CONN_FETCH($$)->name = strdup($2);
+                       CONN_FETCH($$)->filter = CONN_MATCHER($3);
                        $$->cmd = CONNECTION_FETCH;
                        free($2); $2 = NULL;
                }
@@ -220,10 +223,11 @@ fetch_statement:
  * Returns a list of all hosts in the store.
  */
 list_statement:
-       LIST
+       LIST filter_clause
                {
-                       $$ = SDB_CONN_NODE(sdb_object_create_T(/* name = */ NULL,
-                                               sdb_conn_node_t));
+                       $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
+                                               conn_list_t, conn_list_destroy));
+                       CONN_LIST($$)->filter = CONN_MATCHER($2);
                        $$->cmd = CONNECTION_LIST;
                }
        ;
@@ -234,47 +238,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
@@ -414,6 +413,13 @@ expression:
                        sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
                }
        |
+       ':' IDENTIFIER
+               {
+                       int field = sdb_store_parse_field_name($2);
+                       free($2); $2 = NULL;
+                       $$ = sdb_store_expr_fieldvalue(field);
+               }
+       |
        data
                {
                        $$ = sdb_store_expr_constvalue(&$1);