From de76ac583f171d2f74326043528f1493da3cdf28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20Kr=C3=BCger?= <45282134+UweKrueger@users.noreply.github.com> Date: Fri, 5 Jun 2020 22:37:34 +0200 Subject: [PATCH] parser: fix string interpolation for expressions ending `c`, `r`, `js` --- vlib/v/parser/parser.v | 5 ++++- vlib/v/tests/string_interpolation_test.v | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index f656fd46bb..2ecb5da3f0 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -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 } diff --git a/vlib/v/tests/string_interpolation_test.v b/vlib/v/tests/string_interpolation_test.v index 3b8a257258..7e9d1ca7be 100644 --- a/vlib/v/tests/string_interpolation_test.v +++ b/vlib/v/tests/string_interpolation_test.v @@ -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)