From 1b0f09955215b94fff1f86b75b2b5336546e0c36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20Kr=C3=BCger?= <45282134+UweKrueger@users.noreply.github.com> Date: Sat, 17 Apr 2021 01:37:17 +0200 Subject: [PATCH] parser: fix handling of `-` inside array literals (#9771) --- vlib/v/fmt/tests/array_init_expected.vv | 19 +++++++++++++++++++ vlib/v/fmt/tests/array_init_input.vv | 19 +++++++++++++++++++ vlib/v/parser/containers.v | 3 +++ vlib/v/parser/parser.v | 1 + vlib/v/parser/pratt.v | 11 +++++++++++ vlib/v/tests/shared_map_test.v | 2 +- 6 files changed, 54 insertions(+), 1 deletion(-) diff --git a/vlib/v/fmt/tests/array_init_expected.vv b/vlib/v/fmt/tests/array_init_expected.vv index e799c688e7..44cf30fe8c 100644 --- a/vlib/v/fmt/tests/array_init_expected.vv +++ b/vlib/v/fmt/tests/array_init_expected.vv @@ -14,3 +14,22 @@ fn wrapping_tests() { 'velit. Sed at mauris et quam ornare tristique.', ] } + +fn array_init_without_commas() { + a := [3, -4, 5, -2] + b := [5 - 2, 7, 2 * 3, -4, -7 * 8, 2 * -4] + c := [4, 6 - 7, 8, 3 * 4] + d := [&a, &b, &c] + e := u16(434) + f := u16(23) + g := u16(4324) + h := [e & f, g, g & g] + k := 323 + l := [k, -k, 5] + x := &e + y := &f + z := &g + q := [f, *y, f * g, e] + ch := chan u16{} + w := [f, 12 <- ch] +} diff --git a/vlib/v/fmt/tests/array_init_input.vv b/vlib/v/fmt/tests/array_init_input.vv index 4c453f8dfb..4b465d7cd8 100644 --- a/vlib/v/fmt/tests/array_init_input.vv +++ b/vlib/v/fmt/tests/array_init_input.vv @@ -10,3 +10,22 @@ fn main() { fn wrapping_tests() { my_arr := ['Lorem ipsum dolor sit amet, consectetur adipiscing', 'elit. Donec varius purus leo, vel maximus diam', 'finibus sed. Etiam eu urna ante. Nunc quis vehicula', 'velit. Sed at mauris et quam ornare tristique.'] } + +fn array_init_without_commas() { + a := [3 -4 5 -2] + b := [5-2,7,2*3,-4,-7*8,2*-4] + c := [4 6 - 7 8 3 * 4] + d := [&a &b &c] + e := u16(434) + f := u16(23) + g := u16(4324) + h := [e&f g g & g] + k := 323 + l := [k -k 5] + x := &e + y := &f + z := &g + q := [f *y f * g e] + ch := chan u16{} + w := [f 12 <-ch] +} diff --git a/vlib/v/parser/containers.v b/vlib/v/parser/containers.v index 870c7d115b..d29f8cc63e 100644 --- a/vlib/v/parser/containers.v +++ b/vlib/v/parser/containers.v @@ -43,6 +43,8 @@ fn (mut p Parser) array_init() ast.ArrayInit { last_pos = p.tok.position() } else { // [1,2,3] or [const]byte + old_inside_array_lit := p.inside_array_lit + p.inside_array_lit = true pre_cmnts = p.eat_comments({}) for i := 0; p.tok.kind !in [.rsbr, .eof]; i++ { exprs << p.expr(0) @@ -52,6 +54,7 @@ fn (mut p Parser) array_init() ast.ArrayInit { } ecmnts.last() << p.eat_comments({}) } + p.inside_array_lit = old_inside_array_lit line_nr := p.tok.line_nr $if tinyc { // NB: do not remove the next line without testing diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 804b2e7398..dace207f2d 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -40,6 +40,7 @@ mut: inside_fn bool // true even with implicit main inside_unsafe_fn bool inside_str_interp bool + inside_array_lit bool or_is_handled bool // ignore `or` in this expression builtin_mod bool // are we in the `builtin` module? mod string // current module name diff --git a/vlib/v/parser/pratt.v b/vlib/v/parser/pratt.v index e076cac2ee..8e78842305 100644 --- a/vlib/v/parser/pratt.v +++ b/vlib/v/parser/pratt.v @@ -19,6 +19,11 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr { if !p.pref.is_fmt { p.eat_comments({}) } + inside_array_lit := p.inside_array_lit + p.inside_array_lit = false + defer { + p.inside_array_lit = inside_array_lit + } // Prefix match p.tok.kind { .key_mut, .key_shared, .key_atomic, .key_static { @@ -322,6 +327,12 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr { } } } + if inside_array_lit { + if p.tok.kind in [.minus, .mul, .amp, .arrow] && p.tok.pos + 1 == p.peek_tok.pos + && p.prev_tok.pos + p.prev_tok.len + 1 != p.peek_tok.pos { + return node + } + } return p.expr_with_left(node, precedence, is_stmt_ident) } diff --git a/vlib/v/tests/shared_map_test.v b/vlib/v/tests/shared_map_test.v index d9b71c39e2..38fd29ac46 100644 --- a/vlib/v/tests/shared_map_test.v +++ b/vlib/v/tests/shared_map_test.v @@ -84,7 +84,7 @@ fn new_map() map[string]f64 { } fn test_shared_array_iteration() { - shared a := [12.75, -0.125, 17] + shared a := [12.75, -0.125, 18.5 - (1.0 + .5)] mut n0 := 0 mut n1 := 0 mut n2 := 0