Check end brace parse errors

This commit is contained in:
Kampfkarren 2022-07-26 22:14:28 -07:00
parent c302ea18e6
commit aa9f9c371f
2 changed files with 24 additions and 4 deletions

View file

@ -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<Lexeme::Type>(static_cast<unsigned char>('}'))) {
return reportExprError(location, {}, "Expected '}' after interpolated string expression");
return reportExprError(startOfBrace, {}, "Expected '}' after interpolated string expression");
}
expressions.push_back(expression);

View file

@ -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