1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

scanner: fix string interpolation with inner curly braces (fix #12242) (#14553)

This commit is contained in:
yuyi 2022-05-30 00:28:23 +08:00 committed by GitHub
parent 79d861ad4f
commit 2c5febe25e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 2 deletions

View File

@ -54,6 +54,7 @@ pub mut:
all_tokens []token.Token // *only* used in comments_mode: .toplevel_comments, contains all tokens
tidx int
eofs int
inter_cbr_count int
pref &pref.Preferences
error_details []string
errors []errors.Error
@ -803,7 +804,11 @@ fn (mut s Scanner) text_scan() token.Token {
`{` {
// Skip { in `${` in strings
if s.is_inside_string {
if s.text[s.pos - 1] == `$` {
continue
} else {
s.inter_cbr_count++
}
}
return s.new_token(.lcbr, '', 1)
}
@ -817,7 +822,7 @@ fn (mut s Scanner) text_scan() token.Token {
`}` {
// s = `hello $name !`
// s = `hello ${name} !`
if s.is_enclosed_inter {
if s.is_enclosed_inter && s.inter_cbr_count == 0 {
if s.pos < s.text.len - 1 {
s.pos++
} else {
@ -832,6 +837,9 @@ fn (mut s Scanner) text_scan() token.Token {
ident_string := s.ident_string()
return s.new_token(.string, ident_string, ident_string.len + 2) // + two quotes
} else {
if s.inter_cbr_count > 0 {
s.inter_cbr_count--
}
return s.new_token(.rcbr, '', 1)
}
}

View File

@ -0,0 +1,21 @@
struct St {}
fn foo() ?int {
return 22
}
fn test_string_interpolation_inner_cbr() {
s1 := '${foo() or { 11 }}'
println(s1)
assert s1 == '22'
s2 := '${St{}}'
println(s2)
assert s2 == 'St{}'
s3 := '${{
'a': 1
}}'
println(s3)
assert s3 == "{'a': 1}"
}