From c375aef9a3658a7b3577b0534241616f547730e5 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Thu, 4 Sep 2025 20:18:43 +0200 Subject: [PATCH] Updates to grammar --- resources/grammar/grammar.y | 90 +++++++++++++++++++++++-------------- src/compiler/parse_global.c | 6 +-- 2 files changed, 60 insertions(+), 36 deletions(-) diff --git a/resources/grammar/grammar.y b/resources/grammar/grammar.y index d4e488976..0222038aa 100644 --- a/resources/grammar/grammar.y +++ b/resources/grammar/grammar.y @@ -887,6 +887,7 @@ statement | asm_block_stmt | ct_echo_stmt | ct_assert_stmt + | ct_error_stmt | ct_if_stmt | ct_switch_stmt | ct_foreach_stmt @@ -927,14 +928,35 @@ optional_label | empty ; +ct_assert_expr_list + : ',' constant_expr + | ct_assert_expr_list ',' constant_expr + ; + ct_assert_stmt : CT_ASSERT constant_expr ':' constant_expr ';' + | CT_ASSERT constant_expr ':' constant_expr ct_assert_expr_list ';' | CT_ASSERT constant_expr ';' - | CT_ERROR constant_expr ';' + ; + +ct_error_stmt + : CT_ERROR constant_expr ';' + | CT_ERROR constant_expr ct_assert_expr_list ';' ; ct_include_stmt - : CT_INCLUDE string_expr ';' + : CT_INCLUDE constant_expr opt_attributes ';' + ; + +ct_exec_list + : constant_expr + | ct_exec_list ',' constant_expr + ; + +ct_exec_stmt + : CT_EXEC '(' string_expr ')' opt_attributes ';' + | CT_EXEC '(' string_expr ',' '{' ct_exec_list opt_comma '}' ')' opt_attributes ';' + | CT_EXEC '(' string_expr ',' '{' ct_exec_list opt_comma '}' ',' constant_expr ')' opt_attributes ';' ; ct_echo_stmt @@ -1077,7 +1099,7 @@ faults | faults ',' CONST_IDENT opt_attributes ; -fault_declaration +faultdef_declaration : FAULTDEF faults ';' ; @@ -1215,13 +1237,17 @@ opt_generic_parameters define_ident : IDENT opt_attributes '=' path_ident opt_generic_parameters + | IDENT opt_attributes '=' MODULE path_ident opt_generic_parameters | CONST_IDENT opt_attributes '=' path_const opt_generic_parameters | AT_IDENT opt_attributes '=' path_at_ident opt_generic_parameters ; -define_declaration +attrdef_declaration + : ATTRDEF define_attribute ';' + ; + +alias_declaration : ALIAS define_ident ';' - | ATTRDEF define_attribute ';' | ALIAS TYPE_IDENT opt_attributes '=' typedef_type opt_attributes ';' ; @@ -1245,7 +1271,7 @@ interface_declaration_name | TYPE_IDENT ':' interface_parents ; -distinct_declaration +typedef_declaration : TYPEDEF TYPE_IDENT opt_interface_impl opt_attributes '=' opt_inline type ';' ; @@ -1274,46 +1300,44 @@ import_decl ; translation_unit - : top_level_statements + : module top_level_after_module + | top_level_no_module | empty ; -top_level_statements - : top_level - | top_level_statements top_level - ; - -opt_extern - : EXTERN - | empty - ; - -exec_decl - : CT_EXEC '(' expr ')' opt_attributes ';' - | CT_EXEC '(' expr ',' initializer_list ')' opt_attributes ';' - | CT_EXEC '(' expr ',' initializer_list ',' expr ')' opt_attributes ';' - ; - -top_level - : module - | import_decl - | exec_decl - | opt_extern func_definition - | opt_extern const_declaration - | opt_extern global_declaration +top_level_decl + : import_decl + | func_definition + | EXTERN func_definition + | const_declaration + | EXTERN const_declaration + | global_declaration + | EXTERN global_declaration | ct_assert_stmt | ct_echo_stmt | ct_include_stmt + | ct_exec_stmt | struct_declaration - | fault_declaration + | faultdef_declaration | enum_declaration | macro_declaration - | define_declaration + | alias_declaration + | attrdef_declaration | bitstruct_declaration - | distinct_declaration + | typedef_declaration | interface_declaration ; +top_level_after_module + : top_level_decl + | top_level_after_module top_level_decl + ; + +top_level_no_module + : top_level_decl + | top_level_after_module top_level_decl + ; + %% void yyerror(YYLTYPE * yylloc_param , yyscan_t yyscanner, const char *yymsgp) diff --git a/src/compiler/parse_global.c b/src/compiler/parse_global.c index 6d7099fdb..b65a8ee2c 100644 --- a/src/compiler/parse_global.c +++ b/src/compiler/parse_global.c @@ -16,7 +16,7 @@ typedef enum FunctionParse_ static inline Decl *parse_func_definition(ParseContext *c, AstId contracts, FunctionParse parse_kind); static inline bool parse_bitstruct_body(ParseContext *c, Decl *decl); static inline bool parse_enum_param_list(ParseContext *c, Decl*** parameters_ref, ArrayIndex *inline_index); -static Decl *parse_include(ParseContext *c); +static Decl *parse_ct_include(ParseContext *c); static Decl *parse_exec(ParseContext *c); static bool parse_attributes_for_global(ParseContext *c, Decl *decl); INLINE bool parse_decl_initializer(ParseContext *c, Decl *decl); @@ -3139,7 +3139,7 @@ static bool parse_contracts(ParseContext *c, AstId *contracts_ref) return true; } -static Decl *parse_include(ParseContext *c) +static Decl *parse_ct_include(ParseContext *c) { SourceSpan loc = c->span; Decl *decl = decl_new(DECL_CT_INCLUDE, NULL, loc); @@ -3323,7 +3323,7 @@ Decl *parse_top_level_statement(ParseContext *c, ParseContext **context_out) return NULL; case TOKEN_CT_INCLUDE: if (contracts) goto CONTRACT_NOT_ALLOWED; - decl = parse_include(c); + decl = parse_ct_include(c); break; case TOKEN_CT_EXEC: if (contracts) goto CONTRACT_NOT_ALLOWED;