-
Notifications
You must be signed in to change notification settings - Fork 0
/
expr.go
151 lines (140 loc) · 2.89 KB
/
expr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package goastutil
import (
"go/ast"
"strings"
"github.com/samber/lo"
)
type ExprType int
func (et ExprType) String() string {
switch et {
case ArrayExprType:
return "ArrayType"
case BasicLitExprType:
return "BasicLit"
case BinaryExprType:
return "BinaryExpr"
case CallExprType:
return "CallExpr"
case ChanExprType:
return "ChanType"
case CompositeLitExprType:
return "CompositeLit"
case EllipsisExprType:
return "Ellipsis"
case FuncLitExprType:
return "FuncLit"
case FuncExprType:
return "FuncType"
case IdentExprType:
return "Ident"
case IndexExprType:
return "IndexExpr"
case IndexListExprType:
return "IndexListExpr"
case InterfaceExprType:
return "InterfaceType"
case KeyValueExprType:
return "KeyValueExpr"
case ParenExprType:
return "ParenExpr"
case SelectorExprType:
return "SelectorExpr"
case SliceExprType:
return "SliceExpr"
case StarExprType:
return "StarExpr"
case TypeAssertExprType:
return "TypeAssertExpr"
case UnaryExprType:
return "UnaryExpr"
default:
return "UnknownExpr"
}
}
const (
ArrayExprType = iota
BasicLitExprType
BinaryExprType
CallExprType
ChanExprType
CompositeLitExprType
EllipsisExprType
FuncLitExprType
FuncExprType
IdentExprType
IndexExprType
IndexListExprType
InterfaceExprType
KeyValueExprType
ParenExprType
SelectorExprType
SliceExprType
StarExprType
StructExprType
TypeAssertExprType
UnaryExprType
)
type Expr interface {
ast.Expr
String() string
ExprType() ExprType
}
type Exprs []Expr
func (e Exprs) String() string {
return strings.Join(lo.Map(e, func(x Expr, _ int) string {
return x.String()
}), ", ")
}
func toExprs(exprs []ast.Expr) Exprs {
return lo.Map(exprs, func(expr ast.Expr, _ int) Expr {
return NewExpr(expr)
})
}
func NewExpr(expr ast.Expr) Expr {
switch expr := expr.(type) {
case *ast.ArrayType:
return NewArrayType(expr)
case *ast.BasicLit:
return NewBasicLit(expr)
case *ast.BinaryExpr:
return NewBinaryExpr(expr)
case *ast.CallExpr:
return NewCallExpr(expr)
case *ast.ChanType:
return NewChanType(expr)
case *ast.CompositeLit:
return NewCompositeLit(expr)
case *ast.Ellipsis:
return NewEllipsis(expr)
case *ast.FuncLit:
return NewFuncLit(expr)
case *ast.FuncType:
return NewFuncType(expr, "", FnExpr)
case *ast.Ident:
return NewIdent(expr)
case *ast.IndexExpr:
return NewIndexExpr(expr)
case *ast.IndexListExpr:
return NewIndexListExpr(expr)
case *ast.InterfaceType:
return NewInterfaceType("", expr)
case *ast.KeyValueExpr:
return NewKeyValueExpr(expr)
case *ast.ParenExpr:
return NewParenExpr(expr)
case *ast.SelectorExpr:
return NewSelectorExpr(expr)
case *ast.SliceExpr:
return NewSliceExpr(expr)
case *ast.StarExpr:
return NewStarExpr(expr)
case *ast.StructType:
return NewStructType("", expr)
case *ast.TypeAssertExpr:
return NewTypeAssertExpr(expr)
case *ast.UnaryExpr:
return NewUnaryExpr(expr)
default:
return nil
}
}