-
Notifications
You must be signed in to change notification settings - Fork 1
/
ast.go
172 lines (150 loc) · 4.1 KB
/
ast.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package astquery
import (
"fmt"
"go/ast"
"reflect"
"regexp"
)
type Filter interface {
Filter(node ast.Node) bool
}
// SetFilter matches nodes whose names are in the specified set of names.
type SetFilter struct {
// Names is a set of names that match the filter
Names []string
// Type is the type of AST node to filter for
Type reflect.Type
}
func (f SetFilter) Filter(node ast.Node) bool {
nodeName, exists := GetName(node)
if !exists {
return false
}
matched := false
for _, name := range f.Names {
if name == nodeName {
matched = true
break
}
}
return reflect.TypeOf(node) == f.Type && matched
}
// RegexpFilter matches nodes whose names match a regular expression.
type RegexpFilter struct {
// Pattern is a regular expression matching AST node names
Pattern *regexp.Regexp
// Type is the type of AST node to filter for
Type reflect.Type
}
func (s RegexpFilter) Filter(node ast.Node) bool {
nodeName, exists := GetName(node)
if !exists {
return false
}
return reflect.TypeOf(node) == s.Type && s.Pattern.MatchString(nodeName)
}
// MethodFilter matches method declaration nodes that have the specified receiver type.
type MethodFilter struct {
// ReceiverType is the name of the receiver's type (without the '*' if a pointer).
ReceiverType string
// ExportedOnly is if the filter should select only exported methods.
ExportedOnly bool
}
func (f MethodFilter) Filter(node ast.Node) bool {
switch node := node.(type) {
case *ast.FuncDecl:
recv := node.Recv
if recv == nil || len(recv.List) != 1 {
return false // not a method
}
recvType, _ := typeName(recv.List[0].Type)
if recvType != f.ReceiverType {
return false // receiver doesn't match
}
if f.ExportedOnly && !node.Name.IsExported() {
return false // not exported
}
return true
default:
return false
}
}
// FilterFunc lets you specify a function for custom filtering logic.
type FilterFunc func(node ast.Node) bool
func (f FilterFunc) Filter(node ast.Node) bool { return f(node) }
// Find recursively searches the AST nodes passed as the first argument and returns all
// AST nodes that match the filter. It does not descend into matching nodes for additional
// matching nodes.
func Find(nodes []ast.Node, filter Filter) []ast.Node {
var found []ast.Node
for _, node := range nodes {
found = append(found, find(node, filter)...)
}
return found
}
func find(node ast.Node, filter Filter) []ast.Node {
var found []ast.Node
ast.Walk(visitFunc(func(node ast.Node) bool {
if filter.Filter(node) {
found = append(found, node)
return false
}
return true
}), node)
return found
}
// visitFunc is a wrapper for traversing nodes in the AST
type visitFunc func(node ast.Node) (descend bool)
func (v visitFunc) Visit(node ast.Node) ast.Visitor {
descend := v(node)
if descend {
return v
} else {
return nil
}
}
// GetName gets the name of a node's identifier. For TypeSpecs and FuncDecls, it looks at the .Name field. For
// SelectorExpr's, it looks at the Sel field.
func GetName(n ast.Node) (name string, exists bool) {
var ident_ interface{}
if idt, exists := getStructField(n, "Name"); exists {
ident_ = idt
} else if idt, exists := getStructField(n, "Sel"); exists {
ident_ = idt
}
if ident_ == nil {
return "", false
}
nodeName, isIdent := ident_.(*ast.Ident)
if !isIdent {
return "", false
}
return nodeName.Name, true
}
// getStructField returns the value of v's field with the given name
// if it exists. v must be a struct or a pointer to a struct.
func getStructField(v interface{}, field string) (fieldVal interface{}, exists bool) {
vv := reflect.ValueOf(v)
if !vv.IsValid() {
return nil, false
}
if vv.Kind() == reflect.Ptr {
vv = vv.Elem()
}
fv := vv.FieldByName(field)
if !fv.IsValid() {
return nil, false
}
return fv.Interface(), true
}
// typeName returns the name of the type referenced by typeExpr.
func typeName(typeExpr ast.Expr) (string, error) {
switch typeExpr := typeExpr.(type) {
case *ast.StarExpr:
return typeName(typeExpr.X)
case *ast.Ident:
return typeExpr.Name, nil
default:
return "", fmt.Errorf("expr %+v is not a type expression", typeExpr)
}
}