Fix interpolation after empty string literal

This commit is contained in:
Rangi42
2025-07-31 15:17:11 -04:00
parent d1829ed923
commit 86b43ea14f
2 changed files with 16 additions and 17 deletions

View File

@@ -1219,7 +1219,9 @@ static std::shared_ptr<std::string> readInterpolation(size_t depth) {
// `lexerState->peekChar()` lets `readInterpolation()` handle its own nested expansions, // `lexerState->peekChar()` lets `readInterpolation()` handle its own nested expansions,
// increasing `depth` each time. // increasing `depth` each time.
if (lexerState->peekChar() == '{') { if (lexerState->peekChar() == '{') {
++lexerState->expansionScanDistance; // Prevent `shiftChar()` from calling `peek()` // Increment `lexerState->expansionScanDistance` to prevent `shiftChar()` from calling
// `peek()` and to balance its decrement.
++lexerState->expansionScanDistance;
shiftChar(); shiftChar();
if (std::shared_ptr<std::string> str = readInterpolation(depth + 1); str) { if (std::shared_ptr<std::string> str = readInterpolation(depth + 1); str) {
beginExpansion(str, *str); beginExpansion(str, *str);
@@ -1425,12 +1427,19 @@ static void readString(std::string &str, bool rawString) {
if (rawMode) { if (rawMode) {
str += '"'; str += '"';
} }
if (nextChar() != '"') { shiftChar();
// `peek()` would mark the third character here as "painted blue" whether or not it is a
// third quote, which would incorrectly prevent expansions right after an empty string "".
// `lexerState->peekChar()` avoids this, and is okay since expansions are disabled here.
if (lexerState->peekChar() != '"') {
// "" is an empty string, skip the loop // "" is an empty string, skip the loop
return; return;
} }
// """ begins a multi-line string // Increment `lexerState->expansionScanDistance` to prevent `shiftChar()` from calling
// `peek()` and to balance its decrement.
++lexerState->expansionScanDistance;
shiftChar(); shiftChar();
// """ begins a multi-line string
if (rawMode) { if (rawMode) {
str += '"'; str += '"';
} }

View File

@@ -1,18 +1,8 @@
error: interpolation-after-string.asm(2):
Unknown character '{'
error: interpolation-after-string.asm(2):
Unknown character '}'
error: interpolation-after-string.asm(5):
Unknown character '{'
error: interpolation-after-string.asm(5): error: interpolation-after-string.asm(5):
syntax error, unexpected symbol syntax error, unexpected symbol
while expanding symbol "greeting" while expanding symbol "hello"
error: interpolation-after-string.asm(5):
Unknown character '}'
error: interpolation-after-string.asm(8): error: interpolation-after-string.asm(8):
Unknown character '{' Interpolated symbol "goodbye" does not exist
error: interpolation-after-string.asm(8): error: interpolation-after-string.asm(8):
syntax error, unexpected symbol syntax error, unexpected string
error: interpolation-after-string.asm(8): Assembly aborted with 3 errors!
Unknown character '}'
Assembly aborted with 8 errors!