erick-alcachofa 30d64d9b65
fix(parser): support empty blocks, support nested scoping, and refine loop lookahead
Signed-off-by: erick-alcachofa <erick@artichoke.dev>

This commit addresses several critical issues in the recursive descent
parser, specifically regarding the handling of empty constructs,
statement termination, and AST representation of nested scopes. These
changes bring the implementation in line with the Artichoke EBNF
specification.

* **CodeBlock as Statement:** Added `CodeBlockStmtNode` to the
  `StatementNode` variant. This allows a bare `{}` to be treated as a
  valid statement, enabling manual scoping within functions.
* **Visitor Support:** Updated `toDot.cpp` (Graphviz) and `toString.cpp`
  (Pretty-print) to support the new `CodeBlockStmtNode` during AST
  traversal.

* **Empty Member Lists:** Implemented a pre-loop check for the closing
  brace `}` in `parseStruct` and `parseEnum`. This prevents the parser
  from attempting to parse members in empty declarations (e.g., `struct
  Empty {}`).
* **Diagnostic Accuracy:** Enhanced the member-parsing loop to provide
  better error context. If a member is not followed by a comma or a
  closing brace, the parser now explicitly suggests `',' or '}'` as the
  expected tokens.

* **Nested Scopes:** The parser now correctly identifies a `{` at the
  start of a statement and dispatches to `parseCodeBlock`.
* **Empty Code Blocks:** Added a guard in the block-parsing loop to
  check for `}` immediately after `{`, allowing functions or nested
  scopes to be empty.

* **C-Style For-Loops:** Replaced `match` with `matchAndConsume` for the
  initialization semicolon. This allows the parser to correctly handle
  loops where the initialization is omitted (e.g., `for (; 1; 1)`).

* **Correctness:** Resolves parser hangs or errors when encountering
  empty blocks.
* **Compliance:** Fully supports the EBNF definition of zero-or-more
  members/statements.
* **Visuals:** AST diagrams now accurately reflect nested block
  structures.
2025-12-26 00:28:18 -06:00
..