### Node Marker and Creation Resulting Syntax Tree Source: https://github.com/0x2a-42/lelwel/blob/main/README.md Example syntax tree output for the node marker and creation example. Shows foo node with A and a bar node containing B and C. ```text foo ├─ A └─ bar ├─ B └─ C ``` -------------------------------- ### Node Rename Resulting Syntax Tree Source: https://github.com/0x2a-42/lelwel/blob/main/README.md Example syntax tree output for the node rename example. Shows a paren_expr node containing '(' , an int_expr node with Int, and ')'. ```text paren_expr ├─ '(' ├─ int_expr │ └─ Int └─ ')' ``` -------------------------------- ### Unconditional Node Elision Resulting Syntax Tree Source: https://github.com/0x2a-42/lelwel/blob/main/README.md Example syntax tree output for the unconditional node elision example. Shows foo node with A, B, and C directly. ```text foo ├─ A ├─ B └─ C ``` -------------------------------- ### Node Elision Resulting Syntax Tree Source: https://github.com/0x2a-42/lelwel/blob/main/README.md Example syntax tree output for the node elision example. Shows foo node with A, B, and a bar node containing C. ```text foo ├─ A ├─ B └─ bar └─ C ``` -------------------------------- ### Start definition for the grammar start rule Source: https://github.com/0x2a-42/lelwel/blob/main/README.md Specifies the start rule of the grammar. There must be exactly one start definition, and the start rule must not be referenced in a regex. ```antlr start translation_unit; ``` -------------------------------- ### Semantic Actions, Predicates, and Assertions Example (Rust ParserCallbacks) Source: https://github.com/0x2a-42/lelwel/blob/main/README.md Rust implementation of ParserCallbacks for the grammar example. The trait methods predicate_foo_1, predicate_foo_2, action_foo_1, action_foo_2, and assertion_foo_1 are defined. The assertion returns None to indicate success. The '// ...' indicates omitted methods. ```rust impl ParserCallbacks for Parser<'_> { // ... fn predicate_foo_1(&self) -> bool { self.peek(1) == Token::B } fn predicate_foo_2(&self) -> bool { self.context.some_condition } fn action_foo_1(&mut self, diags: &mut Vec) { println!("executed action 1"); } fn action_foo_2(&mut self, diags: &mut Vec) { println!("executed action 2"); } fn assertion_foo_1(&self) -> Option { None } } ``` -------------------------------- ### Node Marker and Creation Example (ANTLR grammar) Source: https://github.com/0x2a-42/lelwel/blob/main/README.md Grammar rule 'foo' with node marker <1 and node creation 1>bar. The marker marks a position where a node can be inserted, and the creation uses the marker index and node name 'bar'. ```antlr foo: A <1 B C 1>bar; ``` -------------------------------- ### Return operator example in ANTLR grammar Source: https://github.com/0x2a-42/lelwel/blob/main/README.md Shows the return operator `&` used in a block rule to only parse a block if the opening brace exists. The operator cannot be used in the start rule and is useful for improving error recovery for block-like rules in concatenations. ```antlr if_stmt: 'if' expr block; block: '{' & stmts* '}'; // only parse a block if the opening brace exists ``` -------------------------------- ### Ordered Choice Example with Commit Operator Source: https://github.com/0x2a-42/lelwel/blob/main/README.md Grammar showing ordered choice with '/' and commit operator '~' to resolve ambiguity between declarations and expressions. Includes token declarations and rules for statements and expressions. ```antlr token Id Num Eq='=' Semi=';' LPar='(' RPar=')'; start stmt; stmt^: decl_stmt / expr_stmt ; decl_stmt: type Id ~ ['=' expr] ';'; expr_stmt: expr ';'; expr: Id | Num | '(' ( type ')' !1 ~ expr / expr ')' ) ; type: Id; ``` -------------------------------- ### Token list definition with symbols Source: https://github.com/0x2a-42/lelwel/blob/main/README.md Defines a list of tokens with optional symbols delimited by single quotes. Token names must start with a capital letter. Symbols starting with '<' and ending with '>' are interpreted as token classes. ```antlr token MyKeyword='my_keyword' Int='' True='true' False='false'; ``` -------------------------------- ### Node Rename Example (ANTLR grammar) Source: https://github.com/0x2a-42/lelwel/blob/main/README.md Grammar rule 'expr' with node rename operators @int_expr and @paren_expr. When the operator is visited, the syntax tree node for the rule is renamed accordingly. ```antlr expr: Int @int_expr | '(' expr ')' @paren_expr ; ``` -------------------------------- ### Semantic Actions, Predicates, and Assertions Example (ANTLR grammar) Source: https://github.com/0x2a-42/lelwel/blob/main/README.md Grammar rule 'foo' using semantic predicates (?1, ?2), actions (#1, #2), and assertions (!1, !2). Predicates and assertions are defined in the ParserCallbacks trait. The predicate_foo_1 checks if the next token is Token::B; predicate_foo_2 uses a context condition. Actions print messages when executed. Assertion returns None to pass. ```antlr token A B C D; foo: ?1 A #1 (?2 B C)* B #2 | A !1 C #2 / A D ; ``` -------------------------------- ### Direct Left Recursion Example with Pratt Parser Source: https://github.com/0x2a-42/lelwel/blob/main/README.md Grammar rule for expressions using direct left recursion. The order of branches defines operator binding power, decreasing from first to last. Demonstrates mixing left and right associative operators in separate branches. ```antlr expr: expr '^' expr | ('-' | '+') expr | expr ('*' | '/') expr | expr ('+' | '-') expr | Num | '(' expr ')' ; ``` -------------------------------- ### Grammar rule definition with regex Source: https://github.com/0x2a-42/lelwel/blob/main/README.md Defines a grammar rule using a regular expression for the right-hand side. Rule names must start with a lower case letter. ```antlr translation_unit: declaration*; ``` -------------------------------- ### Invalid Mixing of Left and Right Associative Operators Source: https://github.com/0x2a-42/lelwel/blob/main/README.md Example showing an error when mixing left and right associative operators in the same branch. The comment marks the line with the error. ```antlr right '='; expr: expr ('+' | '=') expr // ❌ error | Num ; ``` -------------------------------- ### Node Elision Example (ANTLR grammar) Source: https://github.com/0x2a-42/lelwel/blob/main/README.md Grammar rule 'foo' with node elision operator '^' on the second 'bar' in the rule. The elision prevents creation of a syntax tree node for that occurrence. ```antlr foo: A bar bar; bar: B ^ | C ``` -------------------------------- ### Unconditional Node Elision Example (ANTLR grammar) Source: https://github.com/0x2a-42/lelwel/blob/main/README.md Grammar rule 'bar^' with unconditional node elision. The operator directly after the rule name elides the node for all parses of that rule. ```antlr foo: A bar bar; bar^: B | C ``` -------------------------------- ### build.rs for Lelwel grammar compilation Source: https://github.com/0x2a-42/lelwel/blob/main/README.md Place this build.rs in your crate root. It calls lelwel::build with the path to your grammar file (e.g., src/your_grammar.llw). This generates lexer.rs and parser.rs files next to the grammar file. ```rust fn main() { lelwel::build("src/your_grammar.llw"); } ``` -------------------------------- ### Minimal main.rs for printing CST and diagnostics Source: https://github.com/0x2a-42/lelwel/blob/main/README.md Use this main.rs to parse a source file passed as a command-line argument. It prints the concrete syntax tree (CST) and emits diagnostics to stderr using codespan-reporting. Requires the generated lexer and parser modules. ```rust mod lexer; mod parser; use codespan_reporting::files::SimpleFile; use codespan_reporting::term::termcolor::{ColorChoice, StandardStream}; use codespan_reporting::term::{self, Config}; use parser::*; fn main() -> std::io::Result<()> { let args: Vec = std::env::args().collect(); if args.len() != 2 { std::process::exit(1); } let source = std::fs::read_to_string(&args[1])?; let mut diags = vec![]; let cst = Parser::new(&source, &mut diags).parse(&mut diags); println!("{cst}"); let writer = StandardStream::stderr(ColorChoice::Auto); let config = Config::default(); let file = SimpleFile::new(&args[1], &source); for diag in diags.iter() { term::emit_to_write_style(&mut writer.lock(), &config, &file, diag).unwrap(); } Ok(()) } ``` -------------------------------- ### Lelwel syntax tree output Source: https://github.com/0x2a-42/lelwel/blob/main/README.md The syntax tree produced by Lelwel for the incomplete C code. It shows a structured tree without explicit ERROR nodes, parsing the incomplete code into valid constructs. ```text translation_unit [0..33] function_definition [0..33] declaration_specifiers [0..4] type_specifier [0..4] Void "void" [0..4] Whitespace " " [4..5] declarator [5..8] function_declarator [5..8] ident_declarator [5..6] Identifier "f" [5..6] LPar "(" [6..7] RPar ")" [7..8] Whitespace " " [8..9] compound_statement [9..33] LBrace "{" [9..10] Whitespace "\n " [10..13] expression_statement [13..17] call_expr [13..17] ident_expr [13..14] Identifier "g" [13..14] LPar "(" [14..15] argument_expression_list [15..17] int_expr [15..16] IntConst "1" [15..16] Comma "," [16..17] Whitespace "\n " [17..20] declaration [20..31] declaration_specifiers [20..23] type_specifier [20..23] Int "int" [20..23] Whitespace " " [23..24] init_declarator_list [24..31] init_declarator [24..31] declarator [24..25] ident_declarator [24..25] Identifier "x" [24..25] Whitespace " " [25..26] Assign "=" [26..27] Whitespace " " [27..28] initializer [28..31] bin_expr [28..31] int_expr [28..29] IntConst "2" [28..29] Whitespace " " [29..30] Plus "+" [30..31] Whitespace "\n" [31..32] RBrace "}" [32..33] ``` -------------------------------- ### C-style and C++-style comments in Lelwel grammar Source: https://github.com/0x2a-42/lelwel/blob/main/README.md Shows how to write single-line and multi-line comments in a Lelwel grammar file. Use // for single-line comments and /* ... */ for multi-line comments. ```antlr // this is a single-line comment /* this is a multi-line comment */ ``` -------------------------------- ### Grammar for the toy language 'L' Source: https://github.com/0x2a-42/lelwel/blob/main/README.md A complete Lelwel grammar (.llw) for the toy language 'L' from the Resilient LL Parsing Tutorial. It defines tokens, skip rules, and productions for functions, parameters, expressions, and statements. The grammar uses semantic actions like @expr_binary and @expr_call. ```antlr token Fn='fn' Let='let' Return='return' True='true' False='false'; token Arrow='->' LPar='(' RPar=')' Comma=',' Colon=':' LBrace='{' RBrace='}' Semi=';' Asn='=' Plus='+' Minus='-' Star='*' Slash='/'; token Name='' Int=''; token Whitespace; skip Whitespace; start file; file: fn*; fn: 'fn' Name param_list ['->' type_expr] block; param_list: '(' [param (?1 ',' param)* [',']] ')'; param: Name ':' type_expr; type_expr: Name; block: '{' stmt* '}'; stmt^: stmt_expr | stmt_let | stmt_return ; stmt_expr: expr ';'; stmt_let: 'let' Name '=' expr ';'; stmt_return: 'return' [expr] ';'; expr: expr ('*' | '/') expr @expr_binary | expr ('+' | '-') expr @expr_binary | expr arg_list @expr_call | Name @expr_name | '(' expr ')' @expr_paren | expr_literal ^ ; expr_literal: Int | 'true' | 'false'; arg_list: '(' [expr (?1 ',' expr)* [',']] ')'; ``` -------------------------------- ### Cargo.toml dependencies for Lelwel Source: https://github.com/0x2a-42/lelwel/blob/main/README.md Add these dependencies to your Cargo.toml. Requires logos 0.16 and codespan-reporting 0.13 as runtime dependencies, and lelwel 0.10 as a build dependency. ```toml [dependencies] logos = "0.16" codespan-reporting = "0.13" [build-dependencies] lelwel = "0.10" ``` -------------------------------- ### Alternative Node Creation with Missing Index (ANTLR grammar) Source: https://github.com/0x2a-42/lelwel/blob/main/README.md Alternative grammar rule 'assign_expr' using node creation '^' in an optional branch. This is usually less preferable because it will not elide a node if bin_expr is followed by an invalid token not in the follow set. ```antlr assign_expr: bin_expr ('=' assign_expr | ^); ``` -------------------------------- ### Documentation comment before a token definition Source: https://github.com/0x2a-42/lelwel/blob/main/README.md Shows a documentation comment (///) placed before a top-level definition. Documentation comments are shown by the language server on hover. ```antlr /// this is a doc-comment token A B C; ``` -------------------------------- ### Node Creation with Missing Index Not Allowed in Left Recursive Rules (ANTLR grammar) Source: https://github.com/0x2a-42/lelwel/blob/main/README.md Grammar rule 'foo' with left recursive branches. Node creation '>' without an index is not allowed in left recursive rules; both branches are errors. ```antlr foo: foo A > // ❌ error | B > // ❌ error ; ``` -------------------------------- ### Node Creation with Missing Index and Name (ANTLR grammar) Source: https://github.com/0x2a-42/lelwel/blob/main/README.md Grammar rule 'assign_expr^' with unconditional node elision and node creation '>' without index or name. This is useful to create a node for certain parses even when the rule is elided. ```antlr assign_expr^: bin_expr ['=' assign_expr >]; ``` -------------------------------- ### Tree-sitter syntax tree output Source: https://github.com/0x2a-42/lelwel/blob/main/README.md The syntax tree produced by Tree-sitter for the same incomplete C code. It introduces ERROR nodes to represent the malformed parts, showing a less structured recovery. ```text translation_unit [0, 0] - [4, 0] function_definition [0, 0] - [3, 1] type: primitive_type [0, 0] - [0, 4] declarator: function_declarator [0, 5] - [0, 8] declarator: identifier [0, 5] - [0, 6] parameters: parameter_list [0, 6] - [0, 8] ( [0, 6] - [0, 7] ) [0, 7] - [0, 8] body: compound_statement [0, 9] - [3, 1] { [0, 9] - [0, 10] ERROR [1, 2] - [2, 11] identifier [1, 2] - [1, 3] ( [1, 3] - [1, 4] number_literal [1, 4] - [1, 5] , [1, 5] - [1, 6] assignment_expression [2, 2] - [2, 11] left: identifier [2, 2] - [2, 5] ERROR [2, 6] - [2, 7] identifier [2, 6] - [2, 7] operator: = [2, 8] - [2, 9] right: number_literal [2, 10] - [2, 11] ERROR [2, 12] - [2, 13] + [2, 12] - [2, 13] } [3, 0] - [3, 1] ``` -------------------------------- ### Incomplete C source code Source: https://github.com/0x2a-42/lelwel/blob/main/README.md The incomplete C code used for the error resilience comparison. It contains a function call with an unclosed parenthesis and an incomplete expression. ```c void f() { g(1, int x = 2 + } ``` -------------------------------- ### Skip definition for ignored tokens Source: https://github.com/0x2a-42/lelwel/blob/main/README.md Specifies tokens that are ignored by the parser but still appear in the syntax tree. ```antlr skip Whitespace Comment; ``` -------------------------------- ### Right definition for right-associative operators Source: https://github.com/0x2a-42/lelwel/blob/main/README.md Declares tokens that are handled as right-associative operators in left-recursive rules. ```antlr right '^' '='; ``` -------------------------------- ### Node Elision Not Allowed in Left Recursive Branches (ANTLR grammar) Source: https://github.com/0x2a-42/lelwel/blob/main/README.md Grammar rule 'foo' with left recursive branch. The node elision operator '^' is not allowed in left recursive branches; the first branch is an error, the second is allowed. ```antlr foo: foo A ^ // ❌ error | B ^ // ✅ ok ; ```