parser: fix handling of `-` inside array literals (#9771)

pull/9777/head
Uwe Krüger 2021-04-17 01:37:17 +02:00 committed by GitHub
parent 0b3d1656f0
commit 1b0f099552
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 54 additions and 1 deletions

View File

@ -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]
}

View File

@ -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]
}

View File

@ -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

View File

@ -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

View File

@ -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)
}

View File

@ -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