scanner: fix `' "$var", "$another"'` where `r"`, was treated as start of a raw string

pull/8331/head
Delyan Angelov 2021-01-28 15:38:00 +02:00
parent 079fbffaf5
commit 4fcd8d8a98
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 28 additions and 2 deletions

View File

@ -907,3 +907,29 @@ fn test_split_by_whitespace() {
assert '\n xyz \t abc def'.split_by_whitespace() == ['xyz', 'abc', 'def']
assert ''.split_by_whitespace() == []
}
fn test_interpolation_after_quoted_variable_still_works() {
rr := 'abc'
tt := 'xyz'
// Basic interpolation, no internal quotes
yy := 'Replacing $rr with $tt'
assert yy == 'Replacing abc with xyz'
// Interpolation after quoted variable ending with 'r'quote
// that may be mistaken with the start of a raw string,
// ensure that it is not.
ss := 'Replacing "$rr" with "$tt"'
assert ss == 'Replacing "abc" with "xyz"'
zz := "Replacing '$rr' with '$tt'"
assert zz == "Replacing 'abc' with 'xyz'"
// Interpolation after quoted variable ending with 'c'quote
// may be mistaken with the start of a c string, so
// check it is not.
cc := 'abc'
ccc := "Replacing '$cc' with '$tt'"
assert ccc == "Replacing 'abc' with 'xyz'"
cccq := 'Replacing "$cc" with "$tt"'
assert cccq == 'Replacing "abc" with "xyz"'
}

View File

@ -1025,8 +1025,8 @@ fn (s &Scanner) count_symbol_before(p int, sym byte) int {
fn (mut s Scanner) ident_string() string {
q := s.text[s.pos]
is_quote := q == scanner.single_quote || q == scanner.double_quote
is_raw := is_quote && s.pos > 0 && s.text[s.pos - 1] == `r`
is_cstr := is_quote && s.pos > 0 && s.text[s.pos - 1] == `c`
is_raw := is_quote && s.pos > 0 && s.text[s.pos - 1] == `r` && !s.is_inside_string
is_cstr := is_quote && s.pos > 0 && s.text[s.pos - 1] == `c` && !s.is_inside_string
if is_quote {
if s.is_inside_string || s.is_enclosed_inter || s.is_inter_start {
s.inter_quote = q