Code

frontend/grammar: Access fields by their name rather than '.<name>'.
[sysdb.git] / src / frontend / grammar.y
index 3c813dcddb05d6ff54ac4a1d93fde958f4afab08..fed30d238f613fe92f301bd3a5ae695d26a10d9f 100644 (file)
 #include <stdio.h>
 #include <string.h>
 
+/*
+ * private helper functions
+ */
+
+static sdb_store_matcher_t *
+name_iter_matcher(int m_type, const char *type_name, const char *cmp,
+               sdb_store_expr_t *expr);
+
+/*
+ * public API
+ */
+
 int
 sdb_fe_yylex(YYSTYPE *yylval, YYLTYPE *yylloc, sdb_fe_yyscan_t yyscanner);
 
@@ -94,7 +106,7 @@ sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
 
 %token AND OR IS NOT MATCHING FILTER
 %token CMP_EQUAL CMP_NEQUAL CMP_REGEX CMP_NREGEX
-%token CMP_LT CMP_LE CMP_GE CMP_GT IN
+%token CMP_LT CMP_LE CMP_GE CMP_GT ALL ANY IN
 %token CONCAT
 
 %token START END
@@ -443,28 +455,18 @@ compare_matcher:
                        sdb_object_deref(SDB_OBJ($3));
                }
        |
-       IDENTIFIER cmp expression
+       ANY IDENTIFIER cmp expression
                {
-                       int type = sdb_store_parse_object_type($1);
-                       sdb_store_expr_t *e = sdb_store_expr_fieldvalue(SDB_FIELD_NAME);
-                       sdb_store_matcher_op_cb cb = sdb_store_parse_matcher_op($2);
-                       sdb_store_matcher_t *m;
-                       assert(cb);
-
-                       m = cb(e, $3);
-                       /* TODO: this only works as long as queries
-                        * are limited to hosts */
-                       if (type == SDB_HOST) {
-                               $$ = m;
-                       }
-                       else {
-                               $$ = sdb_store_child_matcher(type, m);
-                               sdb_object_deref(SDB_OBJ(m));
-                       }
-
-                       free($1); $1 = NULL;
-                       sdb_object_deref(SDB_OBJ($3));
-                       sdb_object_deref(SDB_OBJ(e));
+                       $$ = name_iter_matcher(MATCHER_ANY, $2, $3, $4);
+                       free($2); $2 = NULL;
+                       sdb_object_deref(SDB_OBJ($4));
+               }
+       |
+       ALL IDENTIFIER cmp expression
+               {
+                       $$ = name_iter_matcher(MATCHER_ALL, $2, $3, $4);
+                       free($2); $2 = NULL;
+                       sdb_object_deref(SDB_OBJ($4));
                }
        |
        expression IS NULL_T
@@ -535,10 +537,16 @@ expression:
                        sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
                }
        |
-       '.' IDENTIFIER
+       IDENTIFIER
                {
-                       int field = sdb_store_parse_field_name($2);
-                       free($2); $2 = NULL;
+                       int field;
+                       /* TODO: this only works as long as queries
+                        * are limited to hosts */
+                       if (!strcasecmp($1, "host"))
+                               field = SDB_FIELD_NAME;
+                       else
+                               field = sdb_store_parse_field_name($1);
+                       free($1); $1 = NULL;
                        $$ = sdb_store_expr_fieldvalue(field);
                }
        |
@@ -647,5 +655,32 @@ sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg)
        sdb_log(SDB_LOG_ERR, "frontend: parse error: %s", msg);
 } /* sdb_fe_yyerror */
 
+static sdb_store_matcher_t *
+name_iter_matcher(int m_type, const char *type_name, const char *cmp,
+               sdb_store_expr_t *expr)
+{
+       int type = sdb_store_parse_object_type(type_name);
+       sdb_store_matcher_op_cb cb = sdb_store_parse_matcher_op(cmp);
+       sdb_store_expr_t *e;
+       sdb_store_matcher_t *m, *tmp = NULL;
+       assert(cb);
+
+       /* TODO: this only works as long as queries
+        * are limited to hosts */
+       if (type == SDB_HOST) {
+               return NULL;
+       }
+
+       e = sdb_store_expr_fieldvalue(SDB_FIELD_NAME);
+       m = cb(e, expr);
+       if (m_type == MATCHER_ANY)
+               tmp = sdb_store_any_matcher(type, m);
+       else if (m_type == MATCHER_ALL)
+               tmp = sdb_store_all_matcher(type, m);
+       sdb_object_deref(SDB_OBJ(m));
+       sdb_object_deref(SDB_OBJ(e));
+       return tmp;
+} /* name_iter_matcher */
+
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */