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

parser: fix map init with multi enum keys (fix #15965) (#15991)

This commit is contained in:
yuyi 2022-10-08 23:56:02 +08:00 committed by GitHub
parent 754c387d1b
commit 91e641a422
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 7 deletions

View File

@ -192,6 +192,11 @@ fn (mut p Parser) array_init() ast.ArrayInit {
// parse tokens between braces
fn (mut p Parser) map_init() ast.MapInit {
old_inside_map_init := p.inside_map_init
p.inside_map_init = true
defer {
p.inside_map_init = old_inside_map_init
}
first_pos := p.prev_tok.pos()
mut keys := []ast.Expr{}
mut vals := []ast.Expr{}

View File

@ -387,13 +387,11 @@ pub fn (mut p Parser) expr_with_left(left ast.Expr, precedence int, is_stmt_iden
}
// Infix
for precedence < p.tok.precedence() {
if p.tok.kind == .dot { //&& (p.tok.line_nr == p.prev_tok.line_nr
// TODO fix a bug with prev_tok.last_line
//|| p.prev_tok.pos().last_line == p.tok.line_nr) {
// if p.fileis('vcache.v') {
// p.warn('tok.line_nr = $p.tok.line_nr; prev_tok.line_nr=$p.prev_tok.line_nr;
// prev_tok.last_line=$p.prev_tok.pos().last_line')
//}
if p.tok.kind == .dot {
// no spaces or line break before dot in map_init
if p.inside_map_init && p.tok.pos - p.prev_tok.pos > p.prev_tok.len {
return node
}
node = p.dot_expr(node)
if p.name_error {
return node

View File

@ -59,6 +59,7 @@ mut:
inside_generic_params bool // indicates if parsing between `<` and `>` of a method/function
inside_receiver_param bool // indicates if parsing the receiver parameter inside the first `(` and `)` of a method
inside_struct_field_decl bool
inside_map_init 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

@ -0,0 +1,42 @@
enum Foo {
a
b
}
type NestedAbc = map[string]string | string
enum NestedFoo {
a
b
c
}
fn test_map_init_with_multi_enum_keys() {
mp := {
Foo.a: 'A'
.b: 'B',
}
println(mp)
assert mp[.a] == 'A'
assert mp[.b] == 'B'
}
fn test_nested_map_init_with_multi_enum_keys() {
mp := {
NestedFoo.a: NestedAbc({
'A': 'AA'
})
.b: 'B',
.c: {
'c': 'C'
},
}
println(mp)
assert mp[.a]? == NestedAbc({
'A': 'AA'
})
assert mp[.b]? == NestedAbc('B')
assert mp[.c]? == NestedAbc({
'c': 'C'
})
}