From dc645a36aaade7087ade574cefe69ce7bb29753e Mon Sep 17 00:00:00 2001 From: _ov4 Date: Sat, 17 Oct 2026 09:09:03 +0000 Subject: refactor(diag): file-qualified diagnostic locations Co-Authored-By: Claude Opus 5.5 --- lib/Parser/ParserExpression.cc | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'lib/Parser/ParserExpression.cc') diff --git a/lib/Parser/ParserExpression.cc b/lib/Parser/ParserExpression.cc index ee3e193..a4f4fcc 100644 --- a/lib/Parser/ParserExpression.cc +++ b/lib/Parser/ParserExpression.cc @@ -1,3 +1,4 @@ +#include "cps/Diagnostics.h" #include "cps/Parser.h" #include "cps/FunctionAST.h" #include @@ -17,7 +18,7 @@ int Parser::GetTokPrecedence() { bool Parser::ParseIntegerValue(bool Negative, int64_t &Out) { const uint64_t Max = uint64_t(std::numeric_limits::max()); if (Lex.NumVal > Max + (Negative ? 1u : 0u)) { - fprintf(stderr, "error: Integer literal is outside the i64 range at line %d\n", Lex.getLine()); + cps::sourceDiagnostic(Lex.getLine(), "error: Integer literal is outside the i64 range at line %d\n", Lex.getLine()); getNextToken(); return false; } @@ -49,7 +50,7 @@ std::unique_ptr Parser::ParseIdentifierExpr() { auto Args = ParseExprList(')', true); if (!Args) return nullptr; if (CurTok == '[' || CurTok == '.' || CurTok == '^') { - fprintf(stderr, "error: record/array/pointer access on a function result is not supported at line %d\n", + cps::sourceDiagnostic(Lex.getLine(), "error: record/array/pointer access on a function result is not supported at line %d\n", Lex.getLine()); return nullptr; } @@ -74,7 +75,7 @@ std::unique_ptr Parser::ParseIdentifierExpr() { std::unique_ptr Parser::ParseMemberCall( const std::string &Name, std::vector Accesses, int Line) { if (Accesses.empty() || Accesses.back().Kind != AccessKind::Field) { - fprintf(stderr, "error: Expected a method name after '.' at line %d\n", Line); + cps::sourceDiagnostic(Lex.getLine(), "error: Expected a method name after '.' at line %d\n", Line); return nullptr; } std::string Method = Accesses.back().FieldName; @@ -88,7 +89,7 @@ std::unique_ptr Parser::ParseMemberCall( Args = std::move(*Parsed); } if (CurTok == '[' || CurTok == '.' || CurTok == '^') { - fprintf(stderr, "error: record/array/pointer access on a function result is not supported at line %d\n", + cps::sourceDiagnostic(Lex.getLine(), "error: record/array/pointer access on a function result is not supported at line %d\n", Lex.getLine()); return nullptr; } @@ -110,7 +111,7 @@ bool Parser::ParseDesignatorSuffix(std::vector &Out) { } else if (CurTok == '.') { getNextToken(); if (CurTok != tok_identifier && CurTok != tok_new && CurTok != tok_delete) { - fprintf(stderr, "error: Expected field name after '.' at line %d\n", Lex.getLine()); + cps::sourceDiagnostic(Lex.getLine(), "error: Expected field name after '.' at line %d\n", Lex.getLine()); return false; } DesignatorAccess A; @@ -141,7 +142,7 @@ std::unique_ptr Parser::ParseParenExpr() { std::string TypeName = ParseTypeName(); if (TypeName.empty()) return nullptr; if (CurTok != ')') { - fprintf(stderr, "error: Expected ')' after cast type at line %d\n", Lex.getLine()); + cps::sourceDiagnostic(Lex.getLine(), "error: Expected ')' after cast type at line %d\n", Lex.getLine()); return nullptr; } getNextToken(); @@ -152,7 +153,7 @@ std::unique_ptr Parser::ParseParenExpr() { auto V = ParseExpression(); if (!V) return nullptr; if (CurTok != ')') { - fprintf(stderr, "error: expected ')' at line %d\n", Lex.getLine()); + cps::sourceDiagnostic(Lex.getLine(), "error: expected ')' at line %d\n", Lex.getLine()); return nullptr; } getNextToken(); @@ -169,7 +170,7 @@ std::unique_ptr Parser::ParsePrimary() { std::unique_ptr Parser::ParsePrimaryImpl() { switch (CurTok) { case tok_new: - fprintf(stderr, "error: NEW is only allowed on the right of an assignment at line %d\n", Lex.getLine()); + cps::sourceDiagnostic(Lex.getLine(), "error: NEW is only allowed on the right of an assignment at line %d\n", Lex.getLine()); return nullptr; case tok_identifier: return ParseIdentifierExpr(); case tok_number_int: return ParseNumberExpr(); @@ -194,7 +195,7 @@ std::unique_ptr Parser::ParsePrimaryImpl() { } case '(': return ParseParenExpr(); default: - fprintf(stderr, "error: unknown token '%c' (%d) at line %d when expecting an expression\n", + cps::sourceDiagnostic(Lex.getLine(), "error: unknown token '%c' (%d) at line %d when expecting an expression\n", (char)CurTok, CurTok, Lex.getLine()); return nullptr; } @@ -220,14 +221,14 @@ std::unique_ptr Parser::ParseUnaryImpl() { int Line = Lex.getLine(); getNextToken(); if (CurTok != tok_identifier) { - fprintf(stderr, "error: '^' (address-of) requires a variable, array element or record field at line %d\n", + cps::sourceDiagnostic(Lex.getLine(), "error: '^' (address-of) requires a variable, array element or record field at line %d\n", Line); return nullptr; } std::string BaseName = Lex.IdentifierStr; getNextToken(); if (CurTok == '(') { - fprintf(stderr, "error: cannot take the address of a function result at line %d\n", Line); + cps::sourceDiagnostic(Lex.getLine(), "error: cannot take the address of a function result at line %d\n", Line); return nullptr; } std::vector Accesses; @@ -294,7 +295,7 @@ std::optional>> Parser::ParseExprList(char if (CurTok == CloseDelim) break; if (CurTok != ',') { - fprintf(stderr, "error: Expected ',' or '%c' at line %d\n", CloseDelim, Lex.getLine()); + cps::sourceDiagnostic(Lex.getLine(), "error: Expected ',' or '%c' at line %d\n", CloseDelim, Lex.getLine()); return std::nullopt; } getNextToken(); -- cgit v1.3.1-4-g156e