-
Notifications
You must be signed in to change notification settings - Fork 2
/
walk.go
124 lines (108 loc) · 2.01 KB
/
walk.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
// Copyright 2017 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package ast
// Walk calls callback cb for every top-level node in description.
// Note: it's not recursive. Use Recursive/PostRecursive helpers for recursive walk.
func (desc *Description) Walk(cb func(Node)) {
for _, n := range desc.Nodes {
cb(n)
}
}
func Recursive(cb func(Node)) func(Node) {
var rec func(Node)
rec = func(n Node) {
cb(n)
n.walk(rec)
}
return rec
}
func PostRecursive(cb func(Node)) func(Node) {
var rec func(Node)
rec = func(n Node) {
n.walk(rec)
cb(n)
}
return rec
}
func (n *NewLine) walk(cb func(Node)) {}
func (n *Comment) walk(cb func(Node)) {}
func (n *Ident) walk(cb func(Node)) {}
func (n *String) walk(cb func(Node)) {}
func (n *Int) walk(cb func(Node)) {}
func (n *Include) walk(cb func(Node)) {
cb(n.File)
}
func (n *Incdir) walk(cb func(Node)) {
cb(n.Dir)
}
func (n *Define) walk(cb func(Node)) {
cb(n.Name)
cb(n.Value)
}
func (n *Resource) walk(cb func(Node)) {
cb(n.Name)
cb(n.Base)
for _, v := range n.Values {
cb(v)
}
}
func (n *TypeDef) walk(cb func(Node)) {
cb(n.Name)
for _, a := range n.Args {
cb(a)
}
if n.Type != nil {
cb(n.Type)
}
if n.Struct != nil {
cb(n.Struct)
}
}
func (n *Call) walk(cb func(Node)) {
cb(n.Name)
for _, f := range n.Args {
cb(f)
}
if n.Ret != nil {
cb(n.Ret)
}
for _, a := range n.Attrs {
cb(a)
}
}
func (n *Struct) walk(cb func(Node)) {
cb(n.Name)
for _, f := range n.Fields {
cb(f)
}
for _, a := range n.Attrs {
cb(a)
}
for _, c := range n.Comments {
cb(c)
}
}
func (n *IntFlags) walk(cb func(Node)) {
cb(n.Name)
for _, v := range n.Values {
cb(v)
}
}
func (n *StrFlags) walk(cb func(Node)) {
cb(n.Name)
for _, v := range n.Values {
cb(v)
}
}
func (n *Type) walk(cb func(Node)) {
for _, t := range n.Args {
cb(t)
}
}
func (n *Field) walk(cb func(Node)) {
cb(n.Name)
cb(n.Type)
for _, c := range n.Comments {
cb(c)
}
}