From 23038d327894d12ec0f794b70923f8ebdbbb8c27 Mon Sep 17 00:00:00 2001 From: Max Sagebaum Date: Thu, 8 Jun 2023 10:54:00 +0200 Subject: [PATCH 1/2] Output the correct location when a statement can not be parsed. Currently the start of the current compound statement is provided as a hint. --- regression-tests/pure2-statement-parse-error.cpp2 | 5 +++++ .../test-results/pure2-statement-parse-error.cpp2.output | 3 +++ source/parse.h | 9 +++++++++ 3 files changed, 17 insertions(+) create mode 100644 regression-tests/pure2-statement-parse-error.cpp2 create mode 100644 regression-tests/test-results/pure2-statement-parse-error.cpp2.output diff --git a/regression-tests/pure2-statement-parse-error.cpp2 b/regression-tests/pure2-statement-parse-error.cpp2 new file mode 100644 index 0000000000..3424703dc6 --- /dev/null +++ b/regression-tests/pure2-statement-parse-error.cpp2 @@ -0,0 +1,5 @@ +func: () = { + a : int = 5; + int b = 4; + c: int = 3; +} diff --git a/regression-tests/test-results/pure2-statement-parse-error.cpp2.output b/regression-tests/test-results/pure2-statement-parse-error.cpp2.output new file mode 100644 index 0000000000..1d27c1b4bc --- /dev/null +++ b/regression-tests/test-results/pure2-statement-parse-error.cpp2.output @@ -0,0 +1,3 @@ +pure2-statement-parse-error.cpp2... +pure2-statement-parse-error.cpp2(3,9): error: Could not parse statement (at 'b') + diff --git a/source/parse.h b/source/parse.h index 4ff01cbb09..192bc4cf4b 100644 --- a/source/parse.h +++ b/source/parse.h @@ -3521,6 +3521,10 @@ class parser error( msg.c_str(), include_curr_token, err_pos, fallback ); } + bool has_error() { + return !errors.empty(); + } + //----------------------------------------------------------------------- // Token navigation: Only these functions should access this->token_ @@ -5372,6 +5376,11 @@ class parser // contained statement() may have parameters auto s = statement(true, source_position{}, true); if (!s) { + + // Only add error when no specific one already exist + if(!has_error()) { + error("Could not parse statement", true); + } pos = start_pos; // backtrack return {}; } From 2961a57e18ee7ae0d44be8276d458a726d815f87 Mon Sep 17 00:00:00 2001 From: Herb Sutter Date: Thu, 10 Aug 2023 21:41:52 -0700 Subject: [PATCH 2/2] Tweak error message wording Signed-off-by: Herb Sutter --- source/parse.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/parse.h b/source/parse.h index 192bc4cf4b..1ce84769a4 100644 --- a/source/parse.h +++ b/source/parse.h @@ -5379,7 +5379,7 @@ class parser // Only add error when no specific one already exist if(!has_error()) { - error("Could not parse statement", true); + error("Invalid statement encountered inside a compound-statement", true); } pos = start_pos; // backtrack return {};