Fix: add semantic check in grammar
[babeltrace.git] / formats / ctf / metadata / ctf-parser.y
index 912f12123c7fa02f7e19c6378b7a341238914c1f..58ea245b8d30f2b43b17cb26b7a8a72594bd0dc2 100644 (file)
@@ -67,6 +67,8 @@ BT_HIDDEN
 void yyrestart(FILE * in_str, yyscan_t scanner);
 BT_HIDDEN
 int yyget_lineno(yyscan_t yyscanner);
+BT_HIDDEN
+char *yyget_text(yyscan_t yyscanner);
 
 struct gc_string {
        struct bt_list_head gc;
@@ -248,13 +250,14 @@ static struct ctf_node *make_node(struct ctf_scanner *scanner,
                return NULL;
        memset(node, 0, sizeof(*node));
        node->type = type;
+       node->lineno = yyget_lineno(scanner->scanner);
        BT_INIT_LIST_HEAD(&node->tmp_head);
        bt_list_add(&node->gc, &ast->allocated_nodes);
        bt_list_add(&node->siblings, &node->tmp_head);
 
        switch (type) {
        case NODE_ROOT:
-               fprintf(stderr, "[error] %s: trying to create root node\n", __func__);
+               printfn_fatal(node, "trying to create root node");
                break;
 
        case NODE_EVENT:
@@ -334,8 +337,7 @@ static struct ctf_node *make_node(struct ctf_scanner *scanner,
 
        case NODE_UNKNOWN:
        default:
-               fprintf(stderr, "[error] %s: unknown node type %d\n", __func__,
-                       (int) type);
+               printfn_fatal(node, "unknown node type '%d'", (int) type);
                break;
        }
 
@@ -394,8 +396,7 @@ static int reparent_ctf_expression(struct ctf_node *node,
 
        case NODE_UNKNOWN:
        default:
-               fprintf(stderr, "[error] %s: unknown node type %d\n", __func__,
-                       (int) parent->type);
+               printfn_fatal(node, "unknown node type '%d'", (int) parent->type);
                return -EINVAL;
        }
        return 0;
@@ -452,8 +453,7 @@ static int reparent_typedef(struct ctf_node *node, struct ctf_node *parent)
 
        case NODE_UNKNOWN:
        default:
-               fprintf(stderr, "[error] %s: unknown node type %d\n", __func__,
-                       (int) parent->type);
+               printfn_fatal(node, "unknown node type %d", parent->type);
                return -EINVAL;
        }
        return 0;
@@ -510,8 +510,7 @@ static int reparent_typealias(struct ctf_node *node, struct ctf_node *parent)
 
        case NODE_UNKNOWN:
        default:
-               fprintf(stderr, "[error] %s: unknown node type %d\n", __func__,
-                       (int) parent->type);
+               printfn_fatal(node, "unknown node type '%d'", (int) parent->type);
                return -EINVAL;
        }
        return 0;
@@ -552,8 +551,7 @@ static int reparent_type_specifier(struct ctf_node *node,
 
        case NODE_UNKNOWN:
        default:
-               fprintf(stderr, "[error] %s: unknown node type %d\n", __func__,
-                       (int) parent->type);
+               printfn_fatal(node, "unknown node type '%d'", (int) parent->type);
                return -EINVAL;
        }
        return 0;
@@ -619,8 +617,7 @@ static int reparent_type_specifier_list(struct ctf_node *node,
 
        case NODE_UNKNOWN:
        default:
-               fprintf(stderr, "[error] %s: unknown node type %d\n", __func__,
-                       (int) parent->type);
+               printfn_fatal(node, "unknown node type '%d'", (int) parent->type);
                return -EINVAL;
        }
        return 0;
@@ -671,8 +668,7 @@ static int reparent_type_declarator(struct ctf_node *node,
 
        case NODE_UNKNOWN:
        default:
-               fprintf(stderr, "[error] %s: unknown node type %d\n", __func__,
-                       (int) parent->type);
+               printfn_fatal(node, "unknown node type '%d'", (int) parent->type);
                return -EINVAL;
        }
        return 0;
@@ -695,7 +691,7 @@ static int set_parent_node(struct ctf_node *node,
 
        switch (node->type) {
        case NODE_ROOT:
-               fprintf(stderr, "[error] %s: trying to reparent root node\n", __func__);
+               printfn_fatal(node, "trying to reparent root node");
                return -EINVAL;
 
        case NODE_EVENT:
@@ -810,8 +806,7 @@ static int set_parent_node(struct ctf_node *node,
 
        case NODE_UNKNOWN:
        default:
-               fprintf(stderr, "[error] %s: unknown node type %d\n", __func__,
-                       (int) parent->type);
+               printfn_fatal(node, "unknown node type '%d'", (int) parent->type);
                return -EINVAL;
        }
        return 0;
@@ -820,8 +815,9 @@ static int set_parent_node(struct ctf_node *node,
 BT_HIDDEN
 void yyerror(struct ctf_scanner *scanner, const char *str)
 {
-       fprintf(stderr, "error at line %d: %s\n",
-               yyget_lineno(scanner->scanner), str);
+       printfl_error(yyget_lineno(scanner->scanner),
+               "token \"%s\": %s\n",
+               yyget_text(scanner->scanner), str);
 }
  
 BT_HIDDEN
@@ -832,7 +828,7 @@ int yywrap(void)
 
 #define reparent_error(scanner, str)                           \
 do {                                                           \
-       yyerror(scanner, YY_("reparent_error: " str "\n"));     \
+       yyerror(scanner, YY_("reparent_error: " str));  \
        YYERROR;                                                \
 } while (0)
 
@@ -893,7 +889,7 @@ struct ctf_scanner *ctf_scanner_alloc(FILE *input)
 
        ret = yylex_init_extra(scanner, &scanner->scanner);
        if (ret) {
-               fprintf(stderr, "yylex_init error\n");
+               printf_fatal("yylex_init error");
                goto cleanup_scanner;
        }
        /* Start processing new stream */
@@ -916,7 +912,7 @@ struct ctf_scanner *ctf_scanner_alloc(FILE *input)
 cleanup_lexer:
        ret = yylex_destroy(scanner->scanner);
        if (!ret)
-               fprintf(stderr, "yylex_destroy error\n");
+               printf_fatal("yylex_destroy error");
 cleanup_scanner:
        free(scanner);
        return NULL;
@@ -931,7 +927,7 @@ void ctf_scanner_free(struct ctf_scanner *scanner)
        ctf_ast_free(scanner->ast);
        ret = yylex_destroy(scanner->scanner);
        if (ret)
-               fprintf(stderr, "yylex_destroy error\n");
+               printf_error("yylex_destroy error");
        free(scanner);
 }
 
@@ -1229,21 +1225,25 @@ unary_expression:
                postfix_expression
                {       $$ = $1;                                }
        |       PLUS postfix_expression
-               {       $$ = $2;                                }
-       |       MINUS postfix_expression
                {
                        $$ = $2;
-                       if ($$->u.unary_expression.type != UNARY_SIGNED_CONSTANT
-                               && $$->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT)
+                       if ($$->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT
+                               && $$->u.unary_expression.type != UNARY_SIGNED_CONSTANT) {
                                reparent_error(scanner, "expecting numeric constant");
-
+                       }
+               }
+       |       MINUS postfix_expression
+               {
+                       $$ = $2;
                        if ($$->u.unary_expression.type == UNARY_UNSIGNED_CONSTANT) {
                                $$->u.unary_expression.type = UNARY_SIGNED_CONSTANT;
                                $$->u.unary_expression.u.signed_constant =
                                        -($$->u.unary_expression.u.unsigned_constant);
-                       } else {
+                       } else if ($$->u.unary_expression.type == UNARY_UNSIGNED_CONSTANT) {
                                $$->u.unary_expression.u.signed_constant =
                                        -($$->u.unary_expression.u.signed_constant);
+                       } else {
+                               reparent_error(scanner, "expecting numeric constant");
                        }
                }
        ;
This page took 0.026481 seconds and 4 git commands to generate.