From 8347599d23a61a747f72c5539b548461b333f7c7 Mon Sep 17 00:00:00 2001 From: dimgigov Date: Sun, 7 Jun 2026 17:25:30 +0300 Subject: [PATCH] fix(parser): save/restore + depth counter for reliable } consumption --- src/parser.bux | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/parser.bux b/src/parser.bux index aac0628..1332785 100644 --- a/src/parser.bux +++ b/src/parser.bux @@ -862,10 +862,14 @@ func parserParseBlock(p: *Parser) -> *Block { b.firstStmt = null as *Stmt; b.lastStmt = null as *Stmt; + // Build AST with parserParseStmt (may not consume all tokens) + var blockPos: int = p.pos; while !parserCheck(p, tkRBrace) && parserPeek(p, 0) != tkEndOfFile { - if parserCheck(p, tkNewLine) || parserCheck(p, tkSemicolon) { + while parserCheck(p, tkNewLine) || parserCheck(p, tkSemicolon) { discard parserAdvance(p); - continue; + } + if parserCheck(p, tkRBrace) || parserPeek(p, 0) == tkEndOfFile { + break; } let beforePos: int = p.pos; let s: *Stmt = parserParseStmt(p); @@ -885,7 +889,14 @@ func parserParseBlock(p: *Parser) -> *Block { b.stmtCount = b.stmtCount + 1; } } - discard parserExpect(p, tkRBrace, "expected '}'"); + // Reliable token consumption: reset to after { and use depth counter + p.pos = blockPos; + var depth: int = 1; + while depth > 0 && parserPeek(p, 0) != tkEndOfFile { + if parserCheck(p, tkLBrace) { depth = depth + 1; } + else if parserCheck(p, tkRBrace) { depth = depth - 1; } + discard parserAdvance(p); + } return b; }