aboutsummaryrefslogtreecommitdiff
path: root/lib/Parser/ParserFunction.cc
diff options
context:
space:
mode:
author_ov4 <ov4@ov4.org>2026-10-22 03:42:49 +0000
committer_ov4 <ov4@ov4.org>2026-10-22 04:05:50 +0000
commitd603a3bab5ee07e3fe8665a7dce8c80cb391ca08 (patch)
tree43d55d6f029959aafdf4700cb6c21c47a441b5da /lib/Parser/ParserFunction.cc
parent0b87ded993d59450ab70ade6e4ce154da65948a3 (diff)
refactor(parser): share callee-name parsing and make operator out-param optional
ParseOperatorName() takes an optional int * so callers that ignore the operator token no longer declare a dummy. The identical identifier-or- OPERATOR prologues of assignment, identifier-expression and CALL parsing move into ParseCalleeName(). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'lib/Parser/ParserFunction.cc')
-rw-r--r--lib/Parser/ParserFunction.cc29
1 files changed, 17 insertions, 12 deletions
diff --git a/lib/Parser/ParserFunction.cc b/lib/Parser/ParserFunction.cc
index f5ebee1..d71b16e 100644
--- a/lib/Parser/ParserFunction.cc
+++ b/lib/Parser/ParserFunction.cc
@@ -141,9 +141,10 @@ std::optional<std::vector<ParamDecl>> Parser::ParsePrototypeArgs(bool AllowByRef
return Params;
}
-std::string Parser::ParseOperatorName(int &Op) {
+std::string Parser::ParseOperatorName(int *OpOut) {
getNextToken(); // OPERATOR
- Op = CurTok;
+ int Op = CurTok;
+ if (OpOut) *OpOut = Op;
if (operatorSpelling(Op).empty()) {
sourceError(Lex.getLine(), "Expected a supported operator after OPERATOR");
getNextToken();
@@ -157,6 +158,17 @@ std::string Parser::ParseOperatorName(int &Op) {
return operatorName(Op);
}
+// A callee is an identifier or OPERATOR <op>; false means a diagnosed failure.
+bool Parser::ParseCalleeName(std::string &Name) {
+ if (CurTok == tok_operator) {
+ Name = ParseOperatorName();
+ return !Name.empty();
+ }
+ Name = Lex.IdentifierStr;
+ getNextToken();
+ return true;
+}
+
std::unique_ptr<FunctionDefAST> Parser::ParseFunction(const std::string &OwnerClass, bool IsPublic, bool IsOverload) {
int Line = Lex.getLine();
getNextToken();
@@ -176,7 +188,7 @@ std::unique_ptr<FunctionDefAST> Parser::ParseFunction(const std::string &OwnerCl
int Operator = 0;
std::string Name;
if (CurTok == tok_operator) {
- Name = ParseOperatorName(Operator);
+ Name = ParseOperatorName(&Operator);
if (Name.empty()) return nullptr;
if (!IsOverload) {
sourceError(Line, "An operator function must be declared OVERLOAD");
@@ -236,7 +248,7 @@ std::unique_ptr<FunctionDefAST> Parser::ParseProcedure(const std::string &OwnerC
bool IsOperator = CurTok == tok_operator;
std::string OperatorName;
if (IsOperator) {
- OperatorName = ParseOperatorName(Operator);
+ OperatorName = ParseOperatorName(&Operator);
if (OperatorName.empty()) return nullptr;
if (Operator != tok_assign || OwnerClass.empty() || !IsOverload) {
sourceError(Line, "Assignment operator must be an OVERLOAD member PROCEDURE");
@@ -376,14 +388,7 @@ std::unique_ptr<StmtAST> Parser::ParseCallStmt() {
}
std::string Callee;
- if (CurTok == tok_operator) {
- int Op;
- Callee = ParseOperatorName(Op);
- if (Callee.empty()) return nullptr;
- } else {
- Callee = Lex.IdentifierStr;
- getNextToken();
- }
+ if (!ParseCalleeName(Callee)) return nullptr;
std::vector<std::string> TemplateArgs;
if (CurTok == '<') {