parser: fix nested array_init syntax
parent
1d78914a8f
commit
60716bba29
|
@ -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)
|
||||
|
|
|
@ -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]'
|
||||
|
|
Loading…
Reference in New Issue