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

scanner: fix string interpolation string literal with fmt (#17895)

This commit is contained in:
yuyi 2023-04-06 17:11:15 +08:00 committed by GitHub
parent 930bdd935b
commit 31ffbc7a87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 6 deletions

View File

@ -34,6 +34,7 @@ pub mut:
last_nl_pos int = -1 // for calculating column
is_crlf bool // special check when computing columns
is_inside_string bool // set to true in a string, *at the start* of an $var or ${expr}
is_nested_string bool // '${'abc':-12s}'
is_inter_start bool // for hacky string interpolation TODO simplify
is_inter_end bool
is_enclosed_inter bool
@ -268,7 +269,7 @@ fn (mut s Scanner) ident_bin_number() string {
s.error('cannot use `_` consecutively')
}
if !c.is_bin_digit() && c != scanner.num_sep {
if (!c.is_digit() && !c.is_letter()) || s.is_inside_string {
if (!c.is_digit() && !c.is_letter()) || s.is_inside_string || s.is_nested_string {
break
} else if !has_wrong_digit {
has_wrong_digit = true
@ -312,7 +313,7 @@ fn (mut s Scanner) ident_hex_number() string {
s.error('cannot use `_` consecutively')
}
if !c.is_hex_digit() && c != scanner.num_sep {
if !c.is_letter() || s.is_inside_string {
if !c.is_letter() || s.is_inside_string || s.is_nested_string {
break
} else if !has_wrong_digit {
has_wrong_digit = true
@ -352,7 +353,7 @@ fn (mut s Scanner) ident_oct_number() string {
s.error('cannot use `_` consecutively')
}
if !c.is_oct_digit() && c != scanner.num_sep {
if (!c.is_digit() && !c.is_letter()) || s.is_inside_string {
if (!c.is_digit() && !c.is_letter()) || s.is_inside_string || s.is_nested_string {
break
} else if !has_wrong_digit {
has_wrong_digit = true
@ -390,7 +391,7 @@ fn (mut s Scanner) ident_dec_number() string {
s.error('cannot use `_` consecutively')
}
if !c.is_digit() && c != scanner.num_sep {
if !c.is_letter() || c in [`e`, `E`] || s.is_inside_string {
if !c.is_letter() || c in [`e`, `E`] || s.is_inside_string || s.is_nested_string {
break
} else if !has_wrong_digit {
has_wrong_digit = true
@ -415,7 +416,8 @@ fn (mut s Scanner) ident_dec_number() string {
for s.pos < s.text.len {
c := s.text[s.pos]
if !c.is_digit() {
if !c.is_letter() || c in [`e`, `E`] || s.is_inside_string {
if !c.is_letter() || c in [`e`, `E`] || s.is_inside_string
|| s.is_nested_string {
// 5.5.str()
if c == `.` && s.pos + 1 < s.text.len && s.text[s.pos + 1].is_letter() {
call_method = true
@ -461,7 +463,7 @@ fn (mut s Scanner) ident_dec_number() string {
for s.pos < s.text.len {
c := s.text[s.pos]
if !c.is_digit() {
if !c.is_letter() || s.is_inside_string {
if !c.is_letter() || s.is_inside_string || s.is_nested_string {
// 5e5.str()
if c == `.` && s.pos + 1 < s.text.len && s.text[s.pos + 1].is_letter() {
call_method = true
@ -1130,6 +1132,12 @@ fn (s &Scanner) count_symbol_before(p int, sym u8) int {
[direct_array_access]
fn (mut s Scanner) ident_string() string {
// determines if it is a nested string
if s.is_inside_string {
s.is_nested_string = true
} else {
s.is_nested_string = false
}
lspos := token.Pos{
line_nr: s.line_nr
pos: s.pos

View File

@ -0,0 +1,6 @@
fn test_string_interpolation_string_lit_with_fmt() {
println('${'hello':-12s}')
assert '${'hello':-12s}' == 'hello '
println('${'hello':12s}')
assert '${'hello':12s}' == ' hello'
}