From fdef2814d0d7e65573d1b666ae9bc561fa35a9f9 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Sun, 26 Jul 2026 12:11:00 +0000 Subject: refactor: extract ParseBlock/ParseExprList, resync-based error recovery Seven copy-pasted statement-block loops collapse into ParseBlock(terminators); five comma-separated expression-list loops collapse into ParseExprList(close, allowEmpty), which also fixes "arr[1 2] <- x" being silently accepted as arr[1,2] on the statement path. Error recovery now lives in one place: syncToStatementStart skips to the next statement-start/block-end token, so a bad statement no longer swallows the first token of the following good one. Drop the dead -1000/-1001 token check in ParsePrototypeArgs. Co-Authored-By: Claude Fable 5 --- lib/Parser/ParserFunction.cc | 34 ++++++---------------------------- 1 file changed, 6 insertions(+), 28 deletions(-) (limited to 'lib/Parser/ParserFunction.cc') diff --git a/lib/Parser/ParserFunction.cc b/lib/Parser/ParserFunction.cc index 06348c4..6ce0b8e 100644 --- a/lib/Parser/ParserFunction.cc +++ b/lib/Parser/ParserFunction.cc @@ -11,8 +11,6 @@ std::optional>> Parser::P getNextToken(); while (CurTok != ')') { - if (CurTok == -1000 || CurTok == -1001) getNextToken(); - bool IsRef = false; if (CurTok == tok_byref) { IsRef = true; @@ -79,12 +77,7 @@ std::unique_ptr Parser::ParseFunction() { auto Proto = std::make_unique(Name, std::move(*Args), RetType); - std::vector> Body; - while (CurTok != tok_endfunction && CurTok != tok_eof) { - auto Stmt = ParseStatement(); - if (Stmt) Body.push_back(std::move(Stmt)); - else if (CurTok != tok_eof && CurTok != tok_endfunction) getNextToken(); - } + std::vector> Body = ParseBlock({tok_endfunction}); if (CurTok != tok_endfunction) { fprintf(stderr, "Error: expected ENDFUNCTION\n"); @@ -109,12 +102,7 @@ std::unique_ptr Parser::ParseProcedure() { if (!Args) return nullptr; auto Proto = std::make_unique(Name, std::move(*Args), "VOID"); - std::vector> Body; - while (CurTok != tok_endprocedure && CurTok != tok_eof) { - auto Stmt = ParseStatement(); - if (Stmt) Body.push_back(std::move(Stmt)); - else if (CurTok != tok_eof && CurTok != tok_endprocedure) getNextToken(); - } + std::vector> Body = ParseBlock({tok_endprocedure}); if (CurTok != tok_endprocedure) { fprintf(stderr, "Error: expected ENDPROCEDURE\n"); @@ -138,21 +126,11 @@ std::unique_ptr Parser::ParseCallStmt() { std::vector> Args; if (CurTok == '(') { getNextToken(); - while (CurTok != ')') { - auto Arg = ParseExpression(); - if (Arg) Args.push_back(std::move(Arg)); - else return nullptr; - - if (CurTok == ')') break; - if (CurTok != ',') { - fprintf(stderr, "Error: Expected ',' or ')' in call\n"); - return nullptr; - } - getNextToken(); - } - getNextToken(); + auto Parsed = ParseExprList(')', true); + if (!Parsed) return nullptr; + Args = std::move(*Parsed); } - + return std::make_unique(Callee, std::move(Args)); } -- cgit v1.3.1-4-g156e