parser: fix string interpolation for expressions ending `c`, `r`, `js`

pull/5230/head
Uwe Krüger 2020-06-05 22:37:34 +02:00 committed by GitHub
parent 11908410f3
commit de76ac583f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View File

@ -32,6 +32,7 @@ mut:
inside_or_expr bool
inside_for bool
inside_fn bool
inside_str_interp bool
pref &pref.Preferences
builtin_mod bool // are we in the `builtin` module?
mod string // current module name
@ -811,7 +812,7 @@ pub fn (mut p Parser) name_expr() ast.Expr {
}
}
// Raw string (`s := r'hello \n ')
if p.tok.lit in ['r', 'c', 'js'] && p.peek_tok.kind == .string && p.prev_tok.kind != .str_dollar {
if p.tok.lit in ['r', 'c', 'js'] && p.peek_tok.kind == .string && !p.inside_str_interp {
return p.string_expr()
}
mut known_var := false
@ -1106,6 +1107,7 @@ fn (mut p Parser) string_expr() ast.Expr {
mut vals := []string{}
mut efmts := []string{}
// Handle $ interpolation
p.inside_str_interp = true
for p.tok.kind == .string {
vals << p.tok.lit
p.next()
@ -1141,6 +1143,7 @@ fn (mut p Parser) string_expr() ast.Expr {
expr_fmts: efmts
pos: pos
}
p.inside_str_interp = false
return node
}

View File

@ -76,6 +76,15 @@ fn test_string_interpolation_string_prefix() {
assert jsjs == 'jsjs'
}
fn test_interpolation_string_prefix_expr() {
r := 1
c := 2
js := 1
assert '>${3+r}<' == '>4<'
assert '${r == js} $js' == 'true 1'
assert '>${js+c} ${js+r==c}<' == '>3 true<'
}
fn test_inttypes_string_interpolation() {
c := i8(-103)
uc := byte(217)