From f5be339f43d829da9796b14c64221e5d3a0a37bc Mon Sep 17 00:00:00 2001 From: erick-alcachofa Date: Sun, 5 Oct 2025 14:49:50 -0600 Subject: [PATCH] fix(grammar): Simplify and improve EBNF definition Signed-off-by: erick-alcachofa The EBNF grammar definition contained several redundancies, inconsistencies, and minor omissions. This commit refactors the grammar to make it more concise, readable, and robust for parsing. Key changes include: - **Rule Simplification**: Redundant intermediate rules (`fn_params`, `statements`, `assign_expression`) have been removed. Rules like `code_block` and `import_target` are now more concisely expressed using standard EBNF operators (`?`, `*`). - **EOF Enforcement**: The top-level `program` rule now requires an `` token. This is a crucial fix to ensure the parser consumes the entire file and fails on trailing invalid tokens. - **Optional Generics**: Generic parameters (`<... >`) are now correctly marked as optional on `function`, `struct`, and `enum` declarations, which was the original intent. - **Flexible For-Loops**: The update/increment expression (the third part) in a C-style `for` loop is now optional, aligning with behavior in languages like C and C++. - **Primary Expressions**: Primary type expressions failed to parse correctly namespaced elements and types, now it's fixed and improved. --- docs/grammar.ebnf | 51 +++++++++++++++++++---------------------------- 1 file changed, 20 insertions(+), 31 deletions(-) diff --git a/docs/grammar.ebnf b/docs/grammar.ebnf index 134ac36..34feacc 100644 --- a/docs/grammar.ebnf +++ b/docs/grammar.ebnf @@ -7,7 +7,6 @@ ================================================================================ */ - /* --- Program Structure --- */ /* A program is a sequence of top-level declarations and statements. */ @@ -18,6 +17,7 @@ | | | )* + = "export"? "module" "{" @@ -32,8 +32,7 @@ "import" ";" = - - | "::" "*" + ( "::" "*" )? = "using" "=" ";" @@ -43,10 +42,7 @@ /* Rules for defining functions, structs, enums, and their components. */ = - "export"? "fn" "(" ")" ( "->" )? - - = - ? + "export"? "fn" ? "(" ? ")" ( "->" )? = "this" ("," ( "," )* )? @@ -56,7 +52,7 @@ ":" = - "export"? "struct" "{" "}" + "export"? "struct" ? "{" "}" = ( "," )* @@ -65,7 +61,7 @@ ":" = - "export"? "enum" "{" "}" + "export"? "enum" ? "{" "}" = ( "," )* @@ -74,7 +70,7 @@ ( "(" ")" )? = - ( "<" ">" )? + "<" ">" = ( "," )* @@ -87,10 +83,7 @@ /* Rules for code blocks, variable declarations, and control structures. */ = - "{" ? "}" - - = - ( )* + "{" * "}" = ";" @@ -101,7 +94,7 @@ | ";" | ";" | ";" - | + | ";" | | | @@ -135,7 +128,7 @@ ) = - "for" "(" ? ";" ";" ")" + "for" "(" ? ";" ";" ? ")" = @@ -182,17 +175,11 @@ = "return" ? - = - ";" - /* --- Expressions & Operator Precedence --- */ /* The full expression hierarchy, from lowest to highest precedence. */ = - - - = ( ( | ) )? = @@ -227,16 +214,17 @@ /* The highest-precedence expressions, including literals and grouped expressions. */ = - + + | + | + | + | + | | | | | - | | - | - | - | = "(" ")" @@ -245,7 +233,7 @@ "::" = - ( | | ) ".@" ? + ( | ) ".@" ? = "(" ")" @@ -292,9 +280,10 @@ = "[" ? ":" ? "]" | "[" "]" - | ".[" "]" | "." | "->" + | ".@" ? + | ".[" "]" | ".#" | ".*" @@ -302,7 +291,7 @@ /* Rules for defining types, type names, and type qualifiers. */ = - + ? = ( "*" | "[]" ) ? @@ -345,4 +334,4 @@ = [1-9] = E /* Represents an empty terminal string */ - + = /* End Of File */