1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package astutil
16
17 import (
18 "fmt"
19
20 "cuelang.org/go/cue/ast"
21 )
22
23
24
25
26
27
28 type visitor interface {
29 Before(node ast.Node) (w visitor)
30 After(node ast.Node)
31 }
32
33
34
35 func walkExprList(v visitor, list []ast.Expr) {
36 for _, x := range list {
37 walk(v, x)
38 }
39 }
40
41 func walkDeclList(v visitor, list []ast.Decl) {
42 for _, x := range list {
43 walk(v, x)
44 }
45 }
46
47
48
49
50
51
52 func walk(v visitor, node ast.Node) {
53 if v = v.Before(node); v == nil {
54 return
55 }
56
57
58
59 for _, c := range node.Comments() {
60 walk(v, c)
61 }
62
63
64
65
66 switch n := node.(type) {
67
68 case *ast.Comment:
69
70
71 case *ast.CommentGroup:
72 for _, c := range n.List {
73 walk(v, c)
74 }
75
76 case *ast.Attribute:
77
78
79 case *ast.Field:
80 walk(v, n.Label)
81 if n.Value != nil {
82 walk(v, n.Value)
83 }
84 for _, a := range n.Attrs {
85 walk(v, a)
86 }
87
88 case *ast.Func:
89 walkExprList(v, n.Args)
90 walk(v, n.Ret)
91
92 case *ast.StructLit:
93 for _, f := range n.Elts {
94 walk(v, f)
95 }
96
97
98 case *ast.BottomLit, *ast.BadExpr, *ast.Ident, *ast.BasicLit:
99
100
101 case *ast.Interpolation:
102 for _, e := range n.Elts {
103 walk(v, e)
104 }
105
106 case *ast.ListLit:
107 walkExprList(v, n.Elts)
108
109 case *ast.Ellipsis:
110 if n.Type != nil {
111 walk(v, n.Type)
112 }
113
114 case *ast.ParenExpr:
115 walk(v, n.X)
116
117 case *ast.SelectorExpr:
118 walk(v, n.X)
119 walk(v, n.Sel)
120
121 case *ast.IndexExpr:
122 walk(v, n.X)
123 walk(v, n.Index)
124
125 case *ast.SliceExpr:
126 walk(v, n.X)
127 if n.Low != nil {
128 walk(v, n.Low)
129 }
130 if n.High != nil {
131 walk(v, n.High)
132 }
133
134 case *ast.CallExpr:
135 walk(v, n.Fun)
136 walkExprList(v, n.Args)
137
138 case *ast.UnaryExpr:
139 walk(v, n.X)
140
141 case *ast.BinaryExpr:
142 walk(v, n.X)
143 walk(v, n.Y)
144
145
146 case *ast.ImportSpec:
147 if n.Name != nil {
148 walk(v, n.Name)
149 }
150 walk(v, n.Path)
151
152 case *ast.BadDecl:
153
154
155 case *ast.ImportDecl:
156 for _, s := range n.Specs {
157 walk(v, s)
158 }
159
160 case *ast.EmbedDecl:
161 walk(v, n.Expr)
162
163 case *ast.Alias:
164 walk(v, n.Ident)
165 walk(v, n.Expr)
166
167 case *ast.Comprehension:
168 for _, c := range n.Clauses {
169 walk(v, c)
170 }
171 walk(v, n.Value)
172
173
174 case *ast.File:
175 walkDeclList(v, n.Decls)
176
177 case *ast.Package:
178
179
180 case *ast.LetClause:
181 walk(v, n.Ident)
182 walk(v, n.Expr)
183
184 case *ast.ForClause:
185 if n.Key != nil {
186 walk(v, n.Key)
187 }
188 walk(v, n.Value)
189 walk(v, n.Source)
190
191 case *ast.IfClause:
192 walk(v, n.Condition)
193
194 default:
195 panic(fmt.Sprintf("Walk: unexpected node type %T", n))
196 }
197
198 v.After(node)
199 }
200
View as plain text