diff options
| author | _ov4 <ov4@ov4.org> | 2026-07-26 12:11:00 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-07-26 12:11:00 +0000 |
| commit | fdef2814d0d7e65573d1b666ae9bc561fa35a9f9 (patch) | |
| tree | 043b9dbe477f85c82bb334d68363bc70196a9d47 /lib/Parser/ParserFunction.cc | |
| parent | 4a25b279d8f6c662fa8152cfe42e038d1ea06901 (diff) | |
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 <noreply@anthropic.com>
Diffstat (limited to 'lib/Parser/ParserFunction.cc')
| -rw-r--r-- | lib/Parser/ParserFunction.cc | 34 |
1 files changed, 6 insertions, 28 deletions
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<std::vector<std::tuple<std::string, std::string, bool>>> 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<StmtAST> Parser::ParseFunction() { auto Proto = std::make_unique<PrototypeAST>(Name, std::move(*Args), RetType); - std::vector<std::unique_ptr<StmtAST>> 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<std::unique_ptr<StmtAST>> Body = ParseBlock({tok_endfunction}); if (CurTok != tok_endfunction) { fprintf(stderr, "Error: expected ENDFUNCTION\n"); @@ -109,12 +102,7 @@ std::unique_ptr<StmtAST> Parser::ParseProcedure() { if (!Args) return nullptr; auto Proto = std::make_unique<PrototypeAST>(Name, std::move(*Args), "VOID"); - std::vector<std::unique_ptr<StmtAST>> 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<std::unique_ptr<StmtAST>> Body = ParseBlock({tok_endprocedure}); if (CurTok != tok_endprocedure) { fprintf(stderr, "Error: expected ENDPROCEDURE\n"); @@ -138,21 +126,11 @@ std::unique_ptr<StmtAST> Parser::ParseCallStmt() { std::vector<std::unique_ptr<ExprAST>> 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<CallStmtAST>(Callee, std::move(Args)); } |
