From aa9f9c371fdbea223d727eed8abc9c64ffd39616 Mon Sep 17 00:00:00 2001 From: Kampfkarren Date: Tue, 26 Jul 2022 22:14:28 -0700 Subject: [PATCH] Check end brace parse errors --- Ast/src/Parser.cpp | 5 +---- tests/Parser.test.cpp | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Ast/src/Parser.cpp b/Ast/src/Parser.cpp index bb5982f8..440be35b 100644 --- a/Ast/src/Parser.cpp +++ b/Ast/src/Parser.cpp @@ -2644,7 +2644,6 @@ AstExpr* Parser::parseInterpString() Location location = currentLexeme.location; - // INTERP TODO: Maybe 1 off? Location startOfBrace = Location(location.end, 1); scratchData.assign(currentLexeme.data, currentLexeme.length); @@ -2676,12 +2675,10 @@ AstExpr* Parser::parseInterpString() AstExpr* expression = parseExpr(); - // expectMatchAndConsume('}', Lexeme(startOfBrace, '{')); - // INTERP CODE REVIEW: I want to use expectMatchAndConsume, but using that // consumes the rest of the string, not the `}` if (lexer.current().type != static_cast(static_cast('}'))) { - return reportExprError(location, {}, "Expected '}' after interpolated string expression"); + return reportExprError(startOfBrace, {}, "Expected '}' after interpolated string expression"); } expressions.push_back(expression); diff --git a/tests/Parser.test.cpp b/tests/Parser.test.cpp index d112d80a..aec1fbe7 100644 --- a/tests/Parser.test.cpp +++ b/tests/Parser.test.cpp @@ -1079,6 +1079,29 @@ TEST_CASE_FIXTURE(Fixture, "parse_interpolated_string_without_format") } } +TEST_CASE_FIXTURE(Fixture, "parse_interpolated_string_without_end_brace") +{ + auto columnOfEndBraceError = [=](const char* code) + { + try + { + parse(code); + FAIL("Expected ParseErrors to be thrown"); + return UINT_MAX; + } + catch (const ParseErrors& e) + { + auto error = e.getErrors().front(); + CHECK_EQ("Expected '}' after interpolated string expression", error.getMessage()); + return error.getLocation().begin.column; + } + }; + + // This makes sure that the error is coming from the brace itself + CHECK_EQ(columnOfEndBraceError("_ = `{a`"), columnOfEndBraceError("_ = `{abcdefg`")); + CHECK_NE(columnOfEndBraceError("_ = `{a`"), columnOfEndBraceError("_ = `{a`")); +} + TEST_CASE_FIXTURE(Fixture, "parse_nesting_based_end_detection") { try