diff options
| author | _ov4 <ov4@ov4.org> | 2026-10-22 04:05:31 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-10-22 04:05:51 +0000 |
| commit | e1fea0e3b49f3b9b3e8a317ceb1938d3d02bf0a0 (patch) | |
| tree | 70042e0e6a70c18e6c305f7c8855e3b1243d748e /lib/Parser | |
| parent | 0b763257ed1e230767171234971cc8f660d59087 (diff) | |
refactor(parser): share the FUNCTION and PROCEDURE body tail
ParseFunctionBody() scopes the prototype's pack parameters, parses the
body up to ENDFUNCTION/ENDPROCEDURE, reports the same "expected ..."
diagnostic and builds the FunctionDefAST for both declaration forms.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'lib/Parser')
| -rw-r--r-- | lib/Parser/ParserFunction.cc | 29 |
1 files changed, 11 insertions, 18 deletions
diff --git a/lib/Parser/ParserFunction.cc b/lib/Parser/ParserFunction.cc index d71b16e..8f3a117 100644 --- a/lib/Parser/ParserFunction.cc +++ b/lib/Parser/ParserFunction.cc @@ -219,21 +219,7 @@ std::unique_ptr<FunctionDefAST> Parser::ParseFunction(const std::string &OwnerCl auto Proto = std::make_unique<PrototypeAST>(Name, std::move(*Args), RetType, false, OwnerClass, IsPublic, IsOverload); Proto->setOperator(Operator); - auto SavedPacks = std::move(PackNames); - PackNames.clear(); - for (const auto &P : Proto->getParams()) if (P.IsPack) PackNames.insert(P.Name); - std::vector<std::unique_ptr<StmtAST>> Body = ParseBlock({tok_endfunction}); - PackNames = std::move(SavedPacks); - - if (CurTok != tok_endfunction) { - cps::sourceError(Lex.getLine(), "expected ENDFUNCTION"); - return nullptr; - } - getNextToken(); - - auto Def = std::make_unique<FunctionDefAST>(std::move(Proto), std::move(Body)); - Def->setLine(Line); - return Def; + return ParseFunctionBody(std::move(Proto), tok_endfunction, "expected ENDFUNCTION", Line); } std::unique_ptr<FunctionDefAST> Parser::ParseProcedure(const std::string &OwnerClass, bool IsPublic, bool IsOverload) { @@ -275,14 +261,21 @@ std::unique_ptr<FunctionDefAST> Parser::ParseProcedure(const std::string &OwnerC auto Proto = std::make_unique<PrototypeAST>(Name, std::move(*Args), "VOID", false, OwnerClass, IsPublic, IsOverload); Proto->setOperator(Operator); + return ParseFunctionBody(std::move(Proto), tok_endprocedure, "expected ENDPROCEDURE", Line); +} + +// Shared FUNCTION/PROCEDURE tail: the body (with the prototype's pack +// parameters in scope) up to EndToken, which must be present. +std::unique_ptr<FunctionDefAST> Parser::ParseFunctionBody( + std::unique_ptr<PrototypeAST> Proto, int EndToken, const char *EndMessage, int Line) { auto SavedPacks = std::move(PackNames); PackNames.clear(); for (const auto &P : Proto->getParams()) if (P.IsPack) PackNames.insert(P.Name); - std::vector<std::unique_ptr<StmtAST>> Body = ParseBlock({tok_endprocedure}); + std::vector<std::unique_ptr<StmtAST>> Body = ParseBlock({EndToken}); PackNames = std::move(SavedPacks); - if (CurTok != tok_endprocedure) { - cps::sourceError(Lex.getLine(), "expected ENDPROCEDURE"); + if (CurTok != EndToken) { + cps::sourceError(Lex.getLine(), "%s", EndMessage); return nullptr; } getNextToken(); |
