Skip to content

Commit 754394c

Browse files
authored
Merge pull request #137 from yosefAlsuhaibani/escaped-dollar-sign-at-end-of-string-does-not-parse
Amend `scanner.c` to correctly parse strings of the form `"*\$"`
2 parents fe73604 + b5ce081 commit 754394c

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/scanner.c

+14-2
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,20 @@ static bool scan_string_content(TSLexer *lexer, Stack *stack) {
115115
advance(lexer);
116116
// this dollar sign is escaped, so it must be content.
117117
// we consume it here so we don't enter the dollar sign case above,
118-
// which leaves the possibility that it is an interpolation
119-
if (lexer->lookahead == '$') advance(lexer);
118+
// which leaves the possibility that it is an interpolation
119+
if (lexer->lookahead == '$') {
120+
advance(lexer);
121+
// however this leaves an edgecase where an escaped dollar sign could
122+
// appear at the end of a string (e.g "aa\$") which isn't handled
123+
// correctly; if we were at the end of the string, terminate properly
124+
if (lexer->lookahead == end_char) {
125+
stack_pop(stack);
126+
advance(lexer);
127+
lexer->mark_end(lexer);
128+
lexer->result_symbol = STRING_END;
129+
return true;
130+
}
131+
}
120132
} else if (lexer->lookahead == end_char) {
121133
if (is_triple) {
122134
lexer->mark_end(lexer);

test/corpus/literals.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ More string interpolation
114114
"$1"
115115
"$ $ $"
116116
"\$foo"
117+
"\$"
117118

118119
--------------------------------------------------------------------------------
119120

@@ -130,7 +131,8 @@ More string interpolation
130131
(string_content)
131132
(string_content))
132133
(string_literal
133-
(string_content)))
134+
(string_content))
135+
(string_literal))
134136

135137
================================================================================
136138
Integer literals

0 commit comments

Comments
 (0)