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

parser: fix nested array_init syntax

This commit is contained in:
spaceface777 2020-05-28 18:36:57 +02:00 committed by GitHub
parent 1d78914a8f
commit 60716bba29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 1 deletions

View File

@ -24,7 +24,7 @@ fn (mut p Parser) array_init() ast.ArrayInit {
line_nr := p.tok.line_nr
p.next()
// []string
if p.tok.kind in [.name, .amp] && p.tok.line_nr == line_nr {
if p.tok.kind in [.name, .amp, .lsbr] && p.tok.line_nr == line_nr {
elem_type_pos = p.tok.position()
elem_type = p.parse_type()
sym := p.table.get_type_symbol(elem_type)

View File

@ -15,6 +15,21 @@ fn test_array_init() {
'$d, $d.len, $d.cap' == "['aaa', 'bbb'], 2, 3"
}
fn test_nested_array_init() {
mut a := [][]int{}
mut b := [][][]string{cap: 10}
mut c := [][][]string{len: 3, init: [][]string{len: 2}}
// mut c := [][][]string{len: 2, init: [][]string{len: 2, init: []string{len: 2, init: 'hello'}}}
a << [1, 2]
a << [3, 4]
b << [['foo', 'bar'], ['baz']]
b << [['qux']]
assert '$a' == '[[1, 2], [3, 4]]'
assert '$b' == "[[['foo', 'bar'], ['baz']], [['qux']]]"
assert '$c' == '[[[], []], [[], []], [[], []]]'
}
fn test_array_init_with_default() {
a1 := []int{len: 4, init: 2}
assert '$a1' == '[2, 2, 2, 2]'