Skip to content

Commit a200a77

Browse files
committed
v3: parse named fixed-array interface params
1 parent 343ea6e commit a200a77

2 files changed

Lines changed: 26 additions & 5 deletions

File tree

vlib/v3/parser/parser.v

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4518,11 +4518,31 @@ fn (mut p Parser) peek_lbr_starts_array_type() bool {
45184518
|| p.s.src[idx] == `\n` || p.s.src[idx] == `\r`) {
45194519
idx++
45204520
}
4521-
if idx >= p.s.src.len {
4522-
return false
4521+
mut depth := 1
4522+
for idx < p.s.src.len {
4523+
c := p.s.src[idx]
4524+
if c == `[` {
4525+
depth++
4526+
} else if c == `]` {
4527+
depth--
4528+
if depth == 0 {
4529+
idx++
4530+
for idx < p.s.src.len && (p.s.src[idx] == ` ` || p.s.src[idx] == `\t`
4531+
|| p.s.src[idx] == `\n` || p.s.src[idx] == `\r`) {
4532+
idx++
4533+
}
4534+
if idx >= p.s.src.len {
4535+
return false
4536+
}
4537+
next := p.s.src[idx]
4538+
return (next >= `a` && next <= `z`) || (next >= `A` && next <= `Z`)
4539+
|| next == `_` || next == `&` || next == `?` || next == `!`
4540+
|| next == `[` || next == `(`
4541+
}
4542+
}
4543+
idx++
45234544
}
4524-
c := p.s.src[idx]
4525-
return c == `]` || (c >= `0` && c <= `9`)
4545+
return false
45264546
}
45274547

45284548
fn (p &Parser) can_start_type_name() bool {

vlib/v3/tests/parser_regression_test.v

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ fn interface_method_param_types(a &flat.FlatAst, iface string, method string) []
3737

3838
fn test_interface_method_generic_type_only_param_is_not_parsed_as_name() {
3939
a := parse_parser_regression_source('interface_generic_param',
40-
'struct Result[T] {}\nstruct Node {}\n\ninterface Sink {\n\tput(Result[int])\n\tappend(values []int)\n\tvisit(node &Node)\n}\n')
40+
'const max_len = 16\nstruct Result[T] {}\nstruct Node {}\n\ninterface Sink {\n\tput(Result[int])\n\tappend(values []int)\n\tvisit(node &Node)\n\tread(buf [max_len]u8)\n}\n')
4141
assert interface_method_param_types(a, 'Sink', 'put') == ['Result[int]']
4242
assert interface_method_param_types(a, 'Sink', 'append') == ['[]int']
4343
assert interface_method_param_types(a, 'Sink', 'visit') == ['&Node']
44+
assert interface_method_param_types(a, 'Sink', 'read') == ['[max_len]u8']
4445
}

0 commit comments

Comments
 (0)