From 86b43ea14f722ff0ca49908a7d9423eeeee12882 Mon Sep 17 00:00:00 2001 From: Rangi42 Date: Thu, 31 Jul 2025 15:17:11 -0400 Subject: [PATCH] Fix interpolation after empty string literal --- src/asm/lexer.cpp | 15 ++++++++++++--- test/asm/interpolation-after-string.err | 18 ++++-------------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/asm/lexer.cpp b/src/asm/lexer.cpp index 4ed76496..50d2811a 100644 --- a/src/asm/lexer.cpp +++ b/src/asm/lexer.cpp @@ -1219,7 +1219,9 @@ static std::shared_ptr readInterpolation(size_t depth) { // `lexerState->peekChar()` lets `readInterpolation()` handle its own nested expansions, // increasing `depth` each time. 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(); if (std::shared_ptr str = readInterpolation(depth + 1); str) { beginExpansion(str, *str); @@ -1425,12 +1427,19 @@ static void readString(std::string &str, bool rawString) { if (rawMode) { 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 return; } - // """ begins a multi-line string + // Increment `lexerState->expansionScanDistance` to prevent `shiftChar()` from calling + // `peek()` and to balance its decrement. + ++lexerState->expansionScanDistance; shiftChar(); + // """ begins a multi-line string if (rawMode) { str += '"'; } diff --git a/test/asm/interpolation-after-string.err b/test/asm/interpolation-after-string.err index 6e123fe9..187d35a0 100644 --- a/test/asm/interpolation-after-string.err +++ b/test/asm/interpolation-after-string.err @@ -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): syntax error, unexpected symbol -while expanding symbol "greeting" -error: interpolation-after-string.asm(5): - Unknown character '}' +while expanding symbol "hello" error: interpolation-after-string.asm(8): - Unknown character '{' + Interpolated symbol "goodbye" does not exist error: interpolation-after-string.asm(8): - syntax error, unexpected symbol -error: interpolation-after-string.asm(8): - Unknown character '}' -Assembly aborted with 8 errors! + syntax error, unexpected string +Assembly aborted with 3 errors!