Code

frontend/grammar: Unified field and attribute value matchers.
[sysdb.git] / src / frontend / grammar.y
index 192671823ac67cc1bc541dd22509cbfa50bccaf4..376c84afdb06bac4965e16c27b2bbb90662fd0ee 100644 (file)
@@ -38,6 +38,8 @@
 #include "utils/error.h"
 #include "utils/llist.h"
 
+#include <assert.h>
+
 #include <stdio.h>
 #include <string.h>
 
@@ -92,7 +94,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
+%token CMP_LT CMP_LE CMP_GE CMP_GT IN
 %token CONCAT
 
 %token START END
@@ -115,6 +117,7 @@ sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
 %left CMP_EQUAL CMP_NEQUAL
 %left CMP_LT CMP_LE CMP_GE CMP_GT
 %nonassoc CMP_REGEX CMP_NREGEX
+%nonassoc IN
 %left CONCAT
 %nonassoc IS
 %left '+' '-'
@@ -430,17 +433,14 @@ matcher:
                }
        ;
 
-/*
- * <object_type>.<object_attr> <cmp> <value>
- *
- * Parse matchers comparing object attributes with a value.
- */
 compare_matcher:
-       '.' IDENTIFIER cmp expression
+       expression cmp expression
                {
-                       $$ = sdb_store_matcher_parse_field_cmp($2, $3, $4);
-                       free($2); $2 = NULL;
-                       sdb_object_deref(SDB_OBJ($4));
+                       sdb_store_matcher_op_cb cb = sdb_store_parse_matcher_op($2);
+                       assert(cb); /* else, the grammar accepts invalid 'cmp' */
+                       $$ = cb($1, $3);
+                       sdb_object_deref(SDB_OBJ($1));
+                       sdb_object_deref(SDB_OBJ($3));
                }
        |
        IDENTIFIER cmp expression
@@ -450,31 +450,23 @@ compare_matcher:
                        sdb_object_deref(SDB_OBJ($3));
                }
        |
-       IDENTIFIER '[' IDENTIFIER ']' cmp expression
+       expression IS NULL_T
                {
-                       $$ = sdb_store_matcher_parse_cmp($1, $3, $5, $6);
-                       free($1); $1 = NULL;
-                       free($3); $3 = NULL;
-                       sdb_object_deref(SDB_OBJ($6));
+                       $$ = sdb_store_isnull_matcher($1);
+                       sdb_object_deref(SDB_OBJ($1));
                }
        |
-       IDENTIFIER '[' IDENTIFIER ']' IS NULL_T
+       expression IS NOT NULL_T
                {
-                       $$ = sdb_store_matcher_parse_cmp($1, $3, "IS", NULL);
-                       free($1); $1 = NULL;
-                       free($3); $3 = NULL;
+                       $$ = sdb_store_isnnull_matcher($1);
+                       sdb_object_deref(SDB_OBJ($1));
                }
        |
-       IDENTIFIER '[' IDENTIFIER ']' IS NOT NULL_T
+       expression IN expression
                {
-                       sdb_store_matcher_t *m;
-                       m = sdb_store_matcher_parse_cmp($1, $3, "IS", NULL);
-                       free($1); $1 = NULL;
-                       free($3); $3 = NULL;
-
-                       /* sdb_store_inv_matcher return NULL if m==NULL */
-                       $$ = sdb_store_inv_matcher(m);
-                       sdb_object_deref(SDB_OBJ(m));
+                       $$ = sdb_store_in_matcher($1, $3);
+                       sdb_object_deref(SDB_OBJ($1));
+                       sdb_object_deref(SDB_OBJ($3));
                }
        ;
 
@@ -533,8 +525,17 @@ expression:
                        $$ = sdb_store_expr_fieldvalue(field);
                }
        |
-       IDENTIFIER '[' IDENTIFIER ']'
+       IDENTIFIER '[' STRING ']'
                {
+                       if (strcasecmp($1, "attribute")) {
+                               char errmsg[strlen($1) + strlen($3) + 32];
+                               snprintf(errmsg, sizeof(errmsg),
+                                               YY_("unknown value %s[%s]"), $1, $3);
+                               sdb_fe_yyerror(&yylloc, scanner, errmsg);
+                               free($1); $1 = NULL;
+                               free($3); $3 = NULL;
+                               YYABORT;
+                       }
                        $$ = sdb_store_expr_attrvalue($3);
                        free($1); $1 = NULL;
                        free($3); $3 = NULL;