Code

frontend/grammar: Check arithmetic expressions early.
authorSebastian Harl <sh@tokkee.org>
Sun, 1 Mar 2015 17:36:34 +0000 (18:36 +0100)
committerSebastian Harl <sh@tokkee.org>
Sun, 1 Mar 2015 17:36:34 +0000 (18:36 +0100)
Constant expressions are evaluated early. Thus, creating an expression object
may fail if there's a semantics error. Catch this early and report a (generic)
error to avoid propagating a NULL value.

src/frontend/grammar.y

index 323fe4feadba40715c3b43c95d0f5d93441de564..db3df642d3525c9ec729159eb45e4fd4034079bc 100644 (file)
@@ -166,7 +166,7 @@ sdb_fe_yyerrorf(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *fmt, ...);
 %type <m> matcher
        compare_matcher
 
-%type <expr> expression object_expression
+%type <expr> expression arithmetic_expression object_expression
 
 %type <integer> object_type object_type_plural
 %type <integer> iterable
@@ -575,6 +575,31 @@ compare_matcher:
        ;
 
 expression:
+       arithmetic_expression
+               {
+                       if (! $1) {
+                               /* we should have better error messages here
+                                * TODO: maybe let the analyzer handle this instead */
+                               sdb_fe_yyerrorf(&yylloc, scanner,
+                                               YY_("syntax error, invalid arithmetic expression"));
+                               YYABORT;
+                       }
+                       $$ = $1;
+               }
+       |
+       object_expression
+               {
+                       $$ = $1;
+               }
+       |
+       data
+               {
+                       $$ = sdb_store_expr_constvalue(&$1);
+                       sdb_data_free_datum(&$1);
+               }
+       ;
+
+arithmetic_expression:
        '(' expression ')'
                {
                        $$ = $2;
@@ -621,17 +646,6 @@ expression:
                        sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
                        sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
                }
-       |
-       object_expression
-               {
-                       $$ = $1;
-               }
-       |
-       data
-               {
-                       $$ = sdb_store_expr_constvalue(&$1);
-                       sdb_data_free_datum(&$1);
-               }
        ;
 
 object_expression: