compiler: '\$' escape
parent
5eee980499
commit
87216cff63
|
@ -569,6 +569,18 @@ fn (s &Scanner) error(msg string) {
|
||||||
exit(1)
|
exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn (s Scanner) count_symbol_before(p int, sym byte) int {
|
||||||
|
mut count := 0
|
||||||
|
for i:=p; i>=0; i-- {
|
||||||
|
if s.text[i] != sym {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
return count
|
||||||
|
}
|
||||||
|
|
||||||
// println('array out of bounds $idx len=$a.len')
|
// println('array out of bounds $idx len=$a.len')
|
||||||
// This is really bad. It needs a major clean up
|
// This is really bad. It needs a major clean up
|
||||||
fn (s mut Scanner) ident_string() string {
|
fn (s mut Scanner) ident_string() string {
|
||||||
|
@ -601,14 +613,14 @@ fn (s mut Scanner) ident_string() string {
|
||||||
s.error('0 character in a string literal')
|
s.error('0 character in a string literal')
|
||||||
}
|
}
|
||||||
// ${var}
|
// ${var}
|
||||||
if c == `{` && prevc == `$` {
|
if c == `{` && prevc == `$` && s.count_symbol_before(s.pos-2, `\\`) % 2 == 0 {
|
||||||
s.inside_string = true
|
s.inside_string = true
|
||||||
// so that s.pos points to $ at the next step
|
// so that s.pos points to $ at the next step
|
||||||
s.pos -= 2
|
s.pos -= 2
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
// $var
|
// $var
|
||||||
if (c.is_letter() || c == `_`) && prevc == `$` {
|
if (c.is_letter() || c == `_`) && prevc == `$` && s.count_symbol_before(s.pos-2, `\\`) % 2 == 0 {
|
||||||
s.inside_string = true
|
s.inside_string = true
|
||||||
s.dollar_start = true
|
s.dollar_start = true
|
||||||
s.pos -= 2
|
s.pos -= 2
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
|
||||||
|
fn test_excape_dollar_in_string() {
|
||||||
|
i := 42
|
||||||
|
|
||||||
|
assert '($i)' == '(42)'
|
||||||
|
assert '(\$i)'.contains('i') && !'(\$i)'.contains('42')
|
||||||
|
assert !'(\\$i)'.contains('i') && '(\\$i)'.contains('42') && '(\\$i)'.contains('\\')
|
||||||
|
assert '(\\\$i)'.contains('i') && !'(\\\$i)'.contains('42') && '(\\$i)'.contains('\\')
|
||||||
|
assert !'(\\\\$i)'.contains('i') && '(\\\\$i)'.contains('42') && '(\\\\$i)'.contains('\\\\')
|
||||||
|
|
||||||
|
assert '(${i})' == '(42)'
|
||||||
|
assert '(\${i})'.contains('i') && !'(\${i})'.contains('42')
|
||||||
|
assert !'(\\${i})'.contains('i') && '(\\${i})'.contains('42') && '(\\${i})'.contains('\\')
|
||||||
|
assert '(\\\${i})'.contains('i') && !'(\\\${i})'.contains('42') && '(\\${i})'.contains('\\')
|
||||||
|
assert !'(\\\\${i})'.contains('i') && '(\\\\${i})'.contains('42') && '(\\\\${i})'.contains('\\\\')
|
||||||
|
assert i==42
|
||||||
|
}
|
Loading…
Reference in New Issue