From de58de2a9a86e3acf6ba02d34226e901eaeaab9b Mon Sep 17 00:00:00 2001 From: erick-alcachofa Date: Sat, 11 Oct 2025 11:30:27 -0600 Subject: [PATCH] fix(grammar): Correct ambiguities and restructure expression parsing Signed-off-by: erick-alcachofa The grammar contained several structural issues and ambiguities, particularly in expression parsing, operator precedence, and the `export` keyword. This commit restructures significant parts of the grammar to resolve these problems and improve its formal correctness, making it more suitable for parser generation. The most relevant changes include: * **Centralized Export Handling:** Corrects the definition of exports by introducing a top-level `` rule that distinguishes between `` and ``. This removes the repetitive and ambiguous `export?` prefix from multiple individual declarations (`module`, `struct`, `fn`, etc.). * **Unified Postfix Operations:** Integrates scoped access into the suffix operations . This provides an unambiguous and unified definition for these common constructs. * **Updated Identifier Chain Issue:** Several rules in the precedence chain ultimately resolved into starting with an identifier, this caused ambiguitiy and issues for parsing, this was refactored in order to correctly handle the cases. * **Reduced Ambiguity in Statements:** Refactors complex rules like `` and `` into smaller, more explicit sub-rules (``, ``). This eliminates potential parsing conflicts and improves the overall clarity of the grammar. * **Simplified Access Expressions:** Removes the separate `` and `` rules. Their logic has been integrated directly into the more generic and powerful postfix expression system, simplifying the grammar. --- docs/grammar.ebnf | 102 ++++++++++++++++++++++++++++------------------ 1 file changed, 62 insertions(+), 40 deletions(-) diff --git a/docs/grammar.ebnf b/docs/grammar.ebnf index 34feacc..1b881c6 100644 --- a/docs/grammar.ebnf +++ b/docs/grammar.ebnf @@ -11,16 +11,29 @@ /* A program is a sequence of top-level declarations and statements. */ = - ( - | - | - | - | - | )* + * + = + "export" + | + + = + + | + | + | + +non_exportable_declaration = + + | + | + | + | + | + = - "export"? "module" "{" + "module" "{" ( | | @@ -42,7 +55,7 @@ /* Rules for defining functions, structs, enums, and their components. */ = - "export"? "fn" ? "(" ? ")" ( "->" )? + "fn" ? "(" ? ")" ( "->" )? = "this" ("," ( "," )* )? @@ -52,7 +65,7 @@ ":" = - "export"? "struct" ? "{" "}" + "struct" ? "{" "}" = ( "," )* @@ -61,7 +74,7 @@ ":" = - "export"? "enum" ? "{" "}" + "enum" ? "{" "}" = ( "," )* @@ -88,20 +101,23 @@ = ";" | - | | ";" | ";" | ";" | ";" | ";" - | ";" | | | + | + | ";" = - ( ":" )? "=" - | ":" ( "=" )? + + + = + ":" ( "=" )? + | "=" = "let" @@ -112,8 +128,11 @@ ? = - "else" ? - | "else" + "else" + + = + + | ? = "|" "|" @@ -152,7 +171,7 @@ "switch" "(" ")" "{" * ? "}" = - ( | ) ( "(" ")" )? "->" + ( "(" ")" )? "->" = "->" @@ -204,45 +223,41 @@ ( )* = - * + * - = - ( | )* + = + ( | )* /* --- Primary Expressions & Literals --- */ /* The highest-precedence expressions, including literals and grouped expressions. */ - = + = - | - | - | - | - | + | + | ( "{" "}" )? + + = + ( "<" ">" )? + + = + | | | | - | = "(" ")" - = - "::" - - = - ( | ) ".@" ? - = "(" ")" = ( ",")* ? - = - "{" ( | )? ","? "}" + = + ( | )? ","? = ( "," )* @@ -278,15 +293,23 @@ = "!" | "-" | "~" | "&" | "*" = - "[" ? ":" ? "]" - | "[" "]" + "[" | "." + | "::" ( "<" ">" )? | "->" | ".@" ? | ".[" "]" | ".#" | ".*" + = + + | ":" ? "]" + + = + "]" + | ":" ? "]" + /* --- Type System --- */ /* Rules for defining types, type names, and type qualifiers. */ @@ -307,11 +330,10 @@ | "?" ? = - ( "<" ">" )? + ( "::" ( "<" ">" )? )* = - - | "::" + ( "::" identifier )* = ( "," )*