-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapply.go
More file actions
366 lines (298 loc) · 9.01 KB
/
Copy pathapply.go
File metadata and controls
366 lines (298 loc) · 9.01 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"go/ast"
"reflect"
)
// An ApplyFunc is invoked by Apply for each node n, even if n is nil,
// before and/or after the node's children.
//
// The parent, name, and index arguments identify the parent node's field
// containing n. If that field is a slice, index identifies the node's position
// in that slice; index is < 0 otherwise. Roughly speaking, the following
// invariants hold:
//
// parent.name == n if index < 0
// parent.name[index] == n if index >= 0
//
// SetField(parent, name, index, n1) can be used to change that field
// to a different node n1.
//
// Exception: If the parent is a *Package, and Apply is iterating
// through the Files map, name is the filename, and index is -1.
//
// The return value of ApplyFunc controls the syntax tree traversal.
// See Apply for details.
type ApplyFunc func(parent ast.Node, name string, index int, n ast.Node) bool
// Apply traverses a syntax tree recursively, starting with root,
// and calling pre and post for each node as described below. The
// result is the (possibly modified) syntax tree.
//
// If pre is not nil, it is called for each node before its children
// are traversed (pre-order). If the result of calling pre is false,
// no children are traversed, and post is not called for that node.
//
// If post is not nil, it is called for each node after its children
// were traversed (post-order). If the result of calling post is false,
// traversal is terminated and Apply returns immediately.
//
// Only fields that refer to AST nodes are considered children.
// Children are traversed in the order in which they appear in the
// respective node's struct definition.
func Apply(root ast.Node, pre, post ApplyFunc) ast.Node {
defer func() {
if r := recover(); r != nil && r != abort {
panic(r)
}
}()
a := &application{root, pre, post}
a.apply(a, "Node", -1, a.Node)
return a.Node
}
// SetField sets the named field in the parent node to n. If the field
// is a slice, index is the slice index. The named field must exist in
// the parent, n must be assignable to that field, and the field must be
// indexable if index >= 0. In other words, SetField performs the following
// assignment:
//
// parent.name = n if index < 0
// parent.name[index] = n if index >= 0
//
// The parent node may be a pointer to the struct containing the named
// field, or it may be the struct itself.
//
// Exception: If the parent is a Package, n must be a *File and name is
// interpreted as the filename in the Package.Files map.
func SetField(parent ast.Node, name string, index int, n ast.Node) {
// TODO(gri) This doesn't handle the Package.Files map yet.
v := reflect.Indirect(reflect.ValueOf(parent)).FieldByName(name)
if index >= 0 {
v = v.Index(index)
}
v.Set(reflect.ValueOf(n))
}
type application struct {
ast.Node
pre, post ApplyFunc
}
func (a *application) apply(parent ast.Node, name string, index int, n ast.Node) {
if a.pre != nil && !a.pre(parent, name, index, n) {
return
}
// walk children
// (the order of the cases matches the order
// of the corresponding node types in ast.go)
switch n := n.(type) {
case nil:
// nothing to do
// Comments and fields
case *ast.Comment:
// nothing to do
case *ast.CommentGroup:
if n != nil {
for i, x := range n.List {
a.apply(n, "List", i, x)
}
}
case *ast.Field:
a.apply(n, "Doc", -1, n.Doc)
a.applyIdentList(n, "Names", n.Names)
a.apply(n, "Type", -1, n.Type)
a.apply(n, "Tag", -1, n.Tag)
a.apply(n, "Comment", -1, n.Comment)
case *ast.FieldList:
if n != nil {
for i, x := range n.List {
a.apply(n, "List", i, x)
}
}
// Expressions
case *ast.BadExpr, *ast.Ident, *ast.BasicLit:
// nothing to do
case *ast.Ellipsis:
a.apply(n, "Elt", -1, n.Elt)
case *ast.FuncLit:
a.apply(n, "Type", -1, n.Type)
a.apply(n, "Body", -1, n.Body)
case *ast.CompositeLit:
a.apply(n, "Type", -1, n.Type)
a.applyExprList(n, "Elts", n.Elts)
case *ast.ParenExpr:
a.apply(n, "X", -1, n.X)
case *ast.SelectorExpr:
a.apply(n, "X", -1, n.X)
a.apply(n, "Sel", -1, n.Sel)
case *ast.IndexExpr:
a.apply(n, "X", -1, n.X)
a.apply(n, "Index", -1, n.Index)
case *ast.SliceExpr:
a.apply(n, "X", -1, n.X)
a.apply(n, "Low", -1, n.Low)
a.apply(n, "High", -1, n.High)
a.apply(n, "Max", -1, n.Max)
case *ast.TypeAssertExpr:
a.apply(n, "X", -1, n.X)
a.apply(n, "Type", -1, n.Type)
case *ast.CallExpr:
a.apply(n, "Fun", -1, n.Fun)
a.applyExprList(n, "Args", n.Args)
case *ast.StarExpr:
a.apply(n, "X", -1, n.X)
case *ast.UnaryExpr:
a.apply(n, "X", -1, n.X)
case *ast.BinaryExpr:
a.apply(n, "X", -1, n.X)
a.apply(n, "Y", -1, n.Y)
case *ast.KeyValueExpr:
a.apply(n, "Key", -1, n.Key)
a.apply(n, "Value", -1, n.Value)
// Types
case *ast.ArrayType:
a.apply(n, "Len", -1, n.Len)
a.apply(n, "Elt", -1, n.Elt)
case *ast.StructType:
a.apply(n, "Fields", -1, n.Fields)
case *ast.FuncType:
a.apply(n, "Params", -1, n.Params)
a.apply(n, "Results", -1, n.Results)
case *ast.InterfaceType:
a.apply(n, "Methods", -1, n.Methods)
case *ast.MapType:
a.apply(n, "Key", -1, n.Key)
a.apply(n, "Value", -1, n.Value)
case *ast.ChanType:
a.apply(n, "Value", -1, n.Value)
// Statements
case *ast.BadStmt:
// nothing to do
case *ast.DeclStmt:
a.apply(n, "Decl", -1, n.Decl)
case *ast.EmptyStmt:
// nothing to do
case *ast.LabeledStmt:
a.apply(n, "Label", -1, n.Label)
a.apply(n, "Stmt", -1, n.Stmt)
case *ast.ExprStmt:
a.apply(n, "X", -1, n.X)
case *ast.SendStmt:
a.apply(n, "Chan", -1, n.Chan)
a.apply(n, "Value", -1, n.Value)
case *ast.IncDecStmt:
a.apply(n, "X", -1, n.X)
case *ast.AssignStmt:
a.applyExprList(n, "Lhs", n.Lhs)
a.applyExprList(n, "Rhs", n.Rhs)
case *ast.GoStmt:
a.apply(n, "Call", -1, n.Call)
case *ast.DeferStmt:
a.apply(n, "Call", -1, n.Call)
case *ast.ReturnStmt:
a.applyExprList(n, "Results", n.Results)
case *ast.BranchStmt:
a.apply(n, "Label", -1, n.Label)
case *ast.BlockStmt:
a.applyStmtList(n, "List", n.List)
case *ast.IfStmt:
a.apply(n, "Init", -1, n.Init)
a.apply(n, "Cond", -1, n.Cond)
a.apply(n, "Body", -1, n.Body)
a.apply(n, "Else", -1, n.Else)
case *ast.CaseClause:
a.applyExprList(n, "List", n.List)
a.applyStmtList(n, "Body", n.Body)
case *ast.SwitchStmt:
a.apply(n, "Init", -1, n.Init)
a.apply(n, "Tag", -1, n.Tag)
a.apply(n, "Body", -1, n.Body)
case *ast.TypeSwitchStmt:
a.apply(n, "Init", -1, n.Init)
a.apply(n, "Assign", -1, n.Assign)
a.apply(n, "Body", -1, n.Body)
case *ast.CommClause:
a.apply(n, "Comm", -1, n.Comm)
a.applyStmtList(n, "Body", n.Body)
case *ast.SelectStmt:
a.apply(n, "Body", -1, n.Body)
case *ast.ForStmt:
a.apply(n, "Init", -1, n.Init)
a.apply(n, "Cond", -1, n.Cond)
a.apply(n, "Post", -1, n.Post)
a.apply(n, "Body", -1, n.Body)
case *ast.RangeStmt:
a.apply(n, "Key", -1, n.Key)
a.apply(n, "Value", -1, n.Value)
a.apply(n, "X", -1, n.X)
a.apply(n, "Body", -1, n.Body)
// Declarations
case *ast.ImportSpec:
a.apply(n, "Doc", -1, n.Doc)
a.apply(n, "Name", -1, n.Name)
a.apply(n, "Path", -1, n.Path)
a.apply(n, "Comment", -1, n.Comment)
case *ast.ValueSpec:
a.apply(n, "Doc", -1, n.Doc)
a.applyIdentList(n, "Names", n.Names)
a.apply(n, "Type", -1, n.Type)
a.applyExprList(n, "Values", n.Values)
a.apply(n, "Comment", -1, n.Comment)
case *ast.TypeSpec:
a.apply(n, "Doc", -1, n.Doc)
a.apply(n, "Name", -1, n.Name)
a.apply(n, "Type", -1, n.Type)
a.apply(n, "Comment", -1, n.Comment)
case *ast.BadDecl:
// nothing to do
case *ast.GenDecl:
a.apply(n, "Doc", -1, n.Doc)
for i, x := range n.Specs {
a.apply(n, "Specs", i, x)
}
case *ast.FuncDecl:
a.apply(n, "Doc", -1, n.Doc)
a.apply(n, "Recv", -1, n.Recv)
a.apply(n, "Name", -1, n.Name)
a.apply(n, "Type", -1, n.Type)
a.apply(n, "Body", -1, n.Body)
// Files and packages
case *ast.File:
a.apply(n, "Doc", -1, n.Doc)
a.apply(n, "Name", -1, n.Name)
a.applyDeclList(n, "Decls", n.Decls)
// don't walk n.Comments - they have been
// visited already through the individual
// nodes
case *ast.Package:
for name, f := range n.Files {
a.apply(n, name, -1, f)
}
default:
panic(fmt.Sprintf("ast.Apply: unexpected node type %T", n))
}
if a.post != nil && !a.post(parent, name, index, n) {
panic(abort)
}
}
var abort = new(int) // singleton, to signal abortion of Apply
func (a *application) applyIdentList(parent ast.Node, name string, list []*ast.Ident) {
for i, x := range list {
a.apply(parent, name, i, x)
}
}
func (a *application) applyExprList(parent ast.Node, name string, list []ast.Expr) {
for i, x := range list {
a.apply(parent, name, i, x)
}
}
func (a *application) applyStmtList(parent ast.Node, name string, list []ast.Stmt) {
for i, x := range list {
a.apply(parent, name, i, x)
}
}
func (a *application) applyDeclList(parent ast.Node, name string, list []ast.Decl) {
for i, x := range list {
a.apply(parent, name, i, x)
}
}