0 issues 1 contributor 4 branches 0 releases
Additions: 40 Deletions: 7 View patch
1 return p.parse_array_type(p.tok.kind)
2 }
3 else {
4- if p.tok.kind == .lpar && !p.inside_sum_type {
5+ if p.tok.kind == .lpar {
6 // multiple return
7 if is_ptr {
8 p.unexpected(prepend_msg: 'parse_type:', got: '`&` before multiple returns')
9
1 return tok
2 }
3
4+// peek token `type Fn = fn () int`
5+fn (p &Parser) is_fn_type_decl() bool {
6+ mut n := 1
7+ mut tok := p.tok
8+ mut prev_tok := p.tok
9+ cur_ln := p.tok.line_nr
10+ for {
11+ tok = p.scanner.peek_token(n)
12+ if tok.kind in [.lpar, .rpar] {
13+ n++
14+ prev_tok = tok
15+ continue
16+ }
17+ if tok.kind == .pipe {
18+ if tok.pos - prev_tok.pos > prev_tok.len {
19+ return false
20+ }
21+ }
22+ if tok.line_nr > cur_ln {
23+ break
24+ }
25+ prev_tok = tok
26+ n++
27+ }
28+ return true
29+}
30+
31 fn (p &Parser) is_array_type() bool {
32 mut i := 1
33 mut tok := p.tok
34 p.check(.assign)
35 mut type_pos := p.tok.pos()
36 mut comments := []ast.Comment{}
37- if p.tok.kind == .key_fn {
38+ if p.tok.kind == .key_fn && p.is_fn_type_decl() {
39 // function type: `type mycallback = fn(string, int)`
40 fn_name := p.prepend_mod(name)
41 fn_type := p.parse_fn_type(fn_name)
42 }
43 }
44 sum_variants << p.parse_sum_type_variants()
45- // type SumType = A | B | c
46+ // type SumType = Aaa | Bbb | Ccc
47 if sum_variants.len > 1 {
48 for variant in sum_variants {
49 variant_sym := p.table.sym(variant.typ)
50
1-Fns
2+Fn
3+Fn
4
1-type Fns = bool | fn (int) int
2+type Fn = fn () int | fn (int) int
3
4 fn main() {
5- f := Fns(fn (a int) int {
6+ f1 := Fn(fn (a int) int {
7 return a
8 })
9- println(typeof(f).name)
10+ println(typeof(f1).name)
11+
12+ f2 := Fn(fn () int {
13+ return 22
14+ })
15+ println(typeof(f2).name)
16 }
17