From daa2f9002316b72770a5554650fd0af77b5e784e Mon Sep 17 00:00:00 2001 From: yuyi Date: Fri, 28 Oct 2022 23:39:32 +0800 Subject: [PATCH] scanner: fix and resotre string interpolation tests (#16242) --- .../tests/str_interpol_invalid_err.out | 39 ++++++++++++++----- .../checker/tests/str_interpol_invalid_err.vv | 6 +-- vlib/v/scanner/scanner.v | 6 ++- .../string_interpolation_inner_cbr_test.v | 3 -- 4 files changed, 37 insertions(+), 17 deletions(-) diff --git a/vlib/v/checker/tests/str_interpol_invalid_err.out b/vlib/v/checker/tests/str_interpol_invalid_err.out index 49b3fb4147..41c68f0423 100644 --- a/vlib/v/checker/tests/str_interpol_invalid_err.out +++ b/vlib/v/checker/tests/str_interpol_invalid_err.out @@ -4,31 +4,52 @@ vlib/v/checker/tests/str_interpol_invalid_err.vv:8:13: error: illegal format spe 8 | _ = '${[1]:x}' | ^ 9 | _ = '${[1]!:x}' - 10 | //_ = '${Foo{}:x}' + 10 | _ = '${Foo{}:x}' vlib/v/checker/tests/str_interpol_invalid_err.vv:9:14: error: illegal format specifier `x` for type `[1]int` 7 | fn main() { 8 | _ = '${[1]:x}' 9 | _ = '${[1]!:x}' | ^ - 10 | //_ = '${Foo{}:x}' + 10 | _ = '${Foo{}:x}' 11 | _ = '${[1]:f}' +vlib/v/checker/tests/str_interpol_invalid_err.vv:10:15: error: illegal format specifier `x` for type `Foo` + 8 | _ = '${[1]:x}' + 9 | _ = '${[1]!:x}' + 10 | _ = '${Foo{}:x}' + | ^ + 11 | _ = '${[1]:f}' + 12 | _ := '${none:F}' vlib/v/checker/tests/str_interpol_invalid_err.vv:11:13: error: illegal format specifier `f` for type `[]int` 9 | _ = '${[1]!:x}' - 10 | //_ = '${Foo{}:x}' + 10 | _ = '${Foo{}:x}' 11 | _ = '${[1]:f}' | ^ 12 | _ := '${none:F}' - 13 | //_ = '${{"a": "b"}:x}' + 13 | _ = '${{"a": "b"}:x}' vlib/v/checker/tests/str_interpol_invalid_err.vv:12:15: error: illegal format specifier `F` for type `none` - 10 | //_ = '${Foo{}:x}' + 10 | _ = '${Foo{}:x}' 11 | _ = '${[1]:f}' 12 | _ := '${none:F}' | ^ - 13 | //_ = '${{"a": "b"}:x}' - 14 | //_ = '${Alias(Foo{}):x}' + 13 | _ = '${{"a": "b"}:x}' + 14 | _ = '${Alias(Foo{}):x}' +vlib/v/checker/tests/str_interpol_invalid_err.vv:13:20: error: illegal format specifier `x` for type `map[string]string` + 11 | _ = '${[1]:f}' + 12 | _ := '${none:F}' + 13 | _ = '${{"a": "b"}:x}' + | ^ + 14 | _ = '${Alias(Foo{}):x}' + 15 | _ = '${SumType(int(5)):o}' +vlib/v/checker/tests/str_interpol_invalid_err.vv:14:22: error: illegal format specifier `x` for type `Alias` + 12 | _ := '${none:F}' + 13 | _ = '${{"a": "b"}:x}' + 14 | _ = '${Alias(Foo{}):x}' + | ^ + 15 | _ = '${SumType(int(5)):o}' + 16 | } vlib/v/checker/tests/str_interpol_invalid_err.vv:15:25: error: illegal format specifier `o` for type `SumType` - 13 | //_ = '${{"a": "b"}:x}' - 14 | //_ = '${Alias(Foo{}):x}' + 13 | _ = '${{"a": "b"}:x}' + 14 | _ = '${Alias(Foo{}):x}' 15 | _ = '${SumType(int(5)):o}' | ^ 16 | } diff --git a/vlib/v/checker/tests/str_interpol_invalid_err.vv b/vlib/v/checker/tests/str_interpol_invalid_err.vv index 79f74e3108..b938fcfc5d 100644 --- a/vlib/v/checker/tests/str_interpol_invalid_err.vv +++ b/vlib/v/checker/tests/str_interpol_invalid_err.vv @@ -7,10 +7,10 @@ type SumType = Alias | int fn main() { _ = '${[1]:x}' _ = '${[1]!:x}' - //_ = '${Foo{}:x}' + _ = '${Foo{}:x}' _ = '${[1]:f}' _ := '${none:F}' - //_ = '${{"a": "b"}:x}' - //_ = '${Alias(Foo{}):x}' + _ = '${{"a": "b"}:x}' + _ = '${Alias(Foo{}):x}' _ = '${SumType(int(5)):o}' } diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index 0437d5930b..b65afc3d95 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -803,11 +803,13 @@ fn (mut s Scanner) text_scan() token.Token { } `{` { if s.is_inside_string { + prev_char := s.text[s.pos - 1] + next_char := s.text[s.pos + 1] // Handle new `hello {name}` string interpolation - if !s.text[s.pos + 1].is_space() && s.text[s.pos - 1] != `$` { + if !next_char.is_space() && next_char != `}` && prev_char !in [`$`, `{`] { return s.new_token(.str_dollar, '', 1) } - if s.text[s.pos - 1] == `$` { + if prev_char == `$` { // Skip { in `${` in strings continue } else { diff --git a/vlib/v/tests/string_interpolation_inner_cbr_test.v b/vlib/v/tests/string_interpolation_inner_cbr_test.v index 5dd0df090f..a1c03df9f4 100644 --- a/vlib/v/tests/string_interpolation_inner_cbr_test.v +++ b/vlib/v/tests/string_interpolation_inner_cbr_test.v @@ -9,12 +9,9 @@ fn test_string_interpolation_inner_cbr() { println(s1) assert s1 == '22' - /* - XTODO s2 := '${St{}}' println(s2) assert s2 == 'St{}' - */ s3 := '${{ 'a': 1