1
2
3
4
5 package typeparams
6
7 import (
8 "go/ast"
9 "go/token"
10 )
11
12 func PackIndexExpr(x ast.Expr, lbrack token.Pos, exprs []ast.Expr, rbrack token.Pos) ast.Expr {
13 switch len(exprs) {
14 case 0:
15 panic("internal error: PackIndexExpr with empty expr slice")
16 case 1:
17 return &ast.IndexExpr{
18 X: x,
19 Lbrack: lbrack,
20 Index: exprs[0],
21 Rbrack: rbrack,
22 }
23 default:
24 return &ast.IndexListExpr{
25 X: x,
26 Lbrack: lbrack,
27 Indices: exprs,
28 Rbrack: rbrack,
29 }
30 }
31 }
32
33
34
35
36
37
38
39
40
41
42 type IndexExpr struct {
43 Orig ast.Expr
44 X ast.Expr
45 Lbrack token.Pos
46 Indices []ast.Expr
47 Rbrack token.Pos
48 }
49
50 func (x *IndexExpr) Pos() token.Pos {
51 return x.Orig.Pos()
52 }
53
54 func UnpackIndexExpr(n ast.Node) *IndexExpr {
55 switch e := n.(type) {
56 case *ast.IndexExpr:
57 return &IndexExpr{
58 Orig: e,
59 X: e.X,
60 Lbrack: e.Lbrack,
61 Indices: []ast.Expr{e.Index},
62 Rbrack: e.Rbrack,
63 }
64 case *ast.IndexListExpr:
65 return &IndexExpr{
66 Orig: e,
67 X: e.X,
68 Lbrack: e.Lbrack,
69 Indices: e.Indices,
70 Rbrack: e.Rbrack,
71 }
72 }
73 return nil
74 }
75
View as plain text