From 163d15e2c412869d58ac5f09c5b883eebd5f6f0b Mon Sep 17 00:00:00 2001 From: PhoenixBound Date: Tue, 2 Jun 2026 21:12:05 -0500 Subject: [PATCH] Add warning for extra symbols in single-expression contexts This is a common mistake that people run into. It normally causes a lot of confusion and pain, because it's not a compile error or anything. In anticipation of making it an error eventually, make it a warning. (I also made a CoilSnake PR so that warnings don't get hidden.) --- src/parser.cpp | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/parser.cpp b/src/parser.cpp index 3616622..c523fd9 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -40,7 +40,27 @@ Program* Parser::Parse() Expression* Parser::ParseExpression() { getsym(); - return expression(); + Expression* e = expression(); + + // Add a warning (error?) for all trailing tokens, because we only wanted to parse + // a single expression + if(sym != finished) { + stringstream ss; + string empty_string{}; + + while(sym != finished) { + ss << "ignoring unexpected symbol '" << last.ToString() << "' after expression"; + Warning(ss.str(), line); + + // Reset error text + ss.str(empty_string); + + // Advance to the next token + getsym(); + } + } + + return e; } @@ -514,4 +534,4 @@ Expression* Parser::primaryexpr() { ss << "symbol '" << last.ToString() << "'"; Error(ss.str(), last.line); return new ErrorExpr(last.line, "unexpected symbol '" + last.ToString() + "'"); -} \ No newline at end of file +}