Remove copy paste (though I'm not sure it's much better), start work on {{ parse failure

This commit is contained in:
Kampfkarren 2022-07-26 19:20:33 -07:00
parent 5a9de6ba0a
commit 6cb9054c07
4 changed files with 39 additions and 36 deletions

View file

@ -218,6 +218,7 @@ private:
Lexeme readQuotedString(); Lexeme readQuotedString();
Lexeme readInterpolatedStringBegin(); Lexeme readInterpolatedStringBegin();
std::optional<Lexeme> readInterpolatedStringSection(Position start, Lexeme::Type formatType);
void readBackslashInString(); void readBackslashInString();

View file

@ -602,35 +602,15 @@ Lexeme Lexer::readQuotedString()
const Lexeme Lexer::nextInterpolatedString() const Lexeme Lexer::nextInterpolatedString()
{ {
// INTERP TODO: This is a copy-paste
Position start = position(); Position start = position();
unsigned int startOffset = offset; unsigned int startOffset = offset;
while (peekch() != '`') std::optional<Lexeme> readSectionOpt = readInterpolatedStringSection(start, Lexeme::InterpStringMid);
if (auto readSection = readSectionOpt)
{ {
switch (peekch()) lexeme = *readSection;
{ return lexeme;
case 0:
case '\r':
case '\n':
lexeme = Lexeme(Location(start, position()), Lexeme::BrokenString);
return lexeme;
case '\\':
readBackslashInString();
break;
case '{':
incrementInterpolatedStringDepth();
lexeme = Lexeme(Location(start, position()), Lexeme::InterpStringMid, &buffer[startOffset], offset - startOffset);
consume();
return lexeme;
default:
consume();
}
} }
consume(); consume();
@ -642,9 +622,20 @@ const Lexeme Lexer::nextInterpolatedString()
Lexeme Lexer::readInterpolatedStringBegin() Lexeme Lexer::readInterpolatedStringBegin()
{ {
Position start = position(); Position start = position();
consume(); consume();
std::optional<Lexeme> readSectionOpt = readInterpolatedStringSection(start, Lexeme::InterpStringBegin);
if (!readSectionOpt)
{
LUAU_ASSERT(!"INTERP TODO: Error if there was no interpolated expression");
}
return *readSectionOpt;
}
std::optional<Lexeme> Lexer::readInterpolatedStringSection(Position start, Lexeme::Type formatType)
{
unsigned int startOffset = offset; unsigned int startOffset = offset;
while (peekch() != '`') while (peekch() != '`')
@ -654,27 +645,26 @@ Lexeme Lexer::readInterpolatedStringBegin()
case 0: case 0:
case '\r': case '\r':
case '\n': case '\n':
return Lexeme(Location(start, position()), Lexeme::BrokenString); return std::optional(Lexeme(Location(start, position()), Lexeme::BrokenString));
case '\\': case '\\':
readBackslashInString(); readBackslashInString();
break; break;
case '{': case '{':
{
incrementInterpolatedStringDepth(); incrementInterpolatedStringDepth();
lexeme = Lexeme(Location(start, position()), Lexeme::InterpStringBegin, &buffer[startOffset], offset - startOffset); auto lexemeOutput = Lexeme(Location(start, position()), Lexeme::InterpStringBegin, &buffer[startOffset], offset - startOffset);
consume(); consume();
return lexeme; return std::optional(lexemeOutput);
}
default: default:
consume(); consume();
} }
} }
consume(); return std::nullopt;
// INTERP TODO: Error if there was no interpolated expression
LUAU_ASSERT(!"INTERP TODO: interpolated string without ending");
} }
Lexeme Lexer::readNumber(const Position& start, unsigned int startOffset) Lexeme Lexer::readNumber(const Position& start, unsigned int startOffset)

View file

@ -1034,6 +1034,21 @@ TEST_CASE_FIXTURE(Fixture, "parse_compound_assignment_error_multiple")
} }
} }
TEST_CASE_FIXTURE(Fixture, "parse_interpolated_string_double_brace")
{
try
{
parse(R"(
_ = `{{oops}}`
)");
FAIL("Expected ParseErrors to be thrown");
}
catch (const ParseErrors& e)
{
CHECK_EQ("INTERP TODO: Message", e.getErrors().front().getMessage());
}
}
TEST_CASE_FIXTURE(Fixture, "parse_nesting_based_end_detection") TEST_CASE_FIXTURE(Fixture, "parse_nesting_based_end_detection")
{ {
try try

View file

@ -18,9 +18,6 @@ assertEq(`The lock combinations are: {table.concat(combo, ", ")}`, "The lock com
assertEq(`true = {true}`, "true = true") assertEq(`true = {true}`, "true = true")
-- -- INTERP TODO: Syntax error
-- -- assert(string.find(`{{ "nested braces!" }}`, "table"))
local name = "Luau" local name = "Luau"
assertEq(`Welcome to { assertEq(`Welcome to {
name name