@@ -5,83 +5,121 @@ package actionlint
5
5
type ExprNode interface {
6
6
// Token returns the first token of the node. This method is useful to get position of this node.
7
7
Token () * Token
8
+ // Parent returns the parent node of this node.
9
+ Parent () ExprNode
8
10
}
9
11
10
12
// Variable
11
13
12
14
// VariableNode is node for variable access.
13
15
type VariableNode struct {
14
16
// Name is name of the variable
15
- Name string
16
- tok * Token
17
+ Name string
18
+ tok * Token
19
+ parent ExprNode
17
20
}
18
21
19
22
// Token returns the first token of the node. This method is useful to get position of this node.
20
23
func (n * VariableNode ) Token () * Token {
21
24
return n .tok
22
25
}
23
26
27
+ // Parent returns the parent node of this node.
28
+ func (n * VariableNode ) Parent () ExprNode {
29
+ return n .parent
30
+ }
31
+
24
32
// Literals
25
33
26
34
// NullNode is node for null literal.
27
35
type NullNode struct {
28
- tok * Token
36
+ tok * Token
37
+ parent ExprNode
29
38
}
30
39
31
40
// Token returns the first token of the node. This method is useful to get position of this node.
32
41
func (n * NullNode ) Token () * Token {
33
42
return n .tok
34
43
}
35
44
45
+ // Parent returns the parent node of this node.
46
+ func (n * NullNode ) Parent () ExprNode {
47
+ return n .parent
48
+ }
49
+
36
50
// BoolNode is node for boolean literal, true or false.
37
51
type BoolNode struct {
38
52
// Value is value of the boolean literal.
39
- Value bool
40
- tok * Token
53
+ Value bool
54
+ tok * Token
55
+ parent ExprNode
41
56
}
42
57
43
58
// Token returns the first token of the node. This method is useful to get position of this node.
44
59
func (n * BoolNode ) Token () * Token {
45
60
return n .tok
46
61
}
47
62
63
+ // Parent returns the parent node of this node.
64
+ func (n * BoolNode ) Parent () ExprNode {
65
+ return n .parent
66
+ }
67
+
48
68
// IntNode is node for integer literal.
49
69
type IntNode struct {
50
70
// Value is value of the integer literal.
51
- Value int
52
- tok * Token
71
+ Value int
72
+ tok * Token
73
+ parent ExprNode
53
74
}
54
75
55
76
// Token returns the first token of the node. This method is useful to get position of this node.
56
77
func (n * IntNode ) Token () * Token {
57
78
return n .tok
58
79
}
59
80
81
+ // Parent returns the parent node of this node.
82
+ func (n * IntNode ) Parent () ExprNode {
83
+ return n .parent
84
+ }
85
+
60
86
// FloatNode is node for float literal.
61
87
type FloatNode struct {
62
88
// Value is value of the float literal.
63
- Value float64
64
- tok * Token
89
+ Value float64
90
+ tok * Token
91
+ parent ExprNode
65
92
}
66
93
67
94
// Token returns the first token of the node. This method is useful to get position of this node.
68
95
func (n * FloatNode ) Token () * Token {
69
96
return n .tok
70
97
}
71
98
99
+ // Parent returns the parent node of this node.
100
+ func (n * FloatNode ) Parent () ExprNode {
101
+ return n .parent
102
+ }
103
+
72
104
// StringNode is node for string literal.
73
105
type StringNode struct {
74
106
// Value is value of the string literal. Escapes are resolved and quotes at both edges are
75
107
// removed.
76
- Value string
77
- tok * Token
108
+ Value string
109
+ tok * Token
110
+ parent ExprNode
78
111
}
79
112
80
113
// Token returns the first token of the node. This method is useful to get position of this node.
81
114
func (n * StringNode ) Token () * Token {
82
115
return n .tok
83
116
}
84
117
118
+ // Parent returns the parent node of this node.
119
+ func (n * StringNode ) Parent () ExprNode {
120
+ return n .parent
121
+ }
122
+
85
123
// Operators
86
124
87
125
// ObjectDerefNode represents property dereference of object like 'foo.bar'.
@@ -90,52 +128,76 @@ type ObjectDerefNode struct {
90
128
Receiver ExprNode
91
129
// Property is a name of property to access.
92
130
Property string
131
+ parent ExprNode
93
132
}
94
133
95
134
// Token returns the first token of the node. This method is useful to get position of this node.
96
- func (n ObjectDerefNode ) Token () * Token {
135
+ func (n * ObjectDerefNode ) Token () * Token {
97
136
return n .Receiver .Token ()
98
137
}
99
138
139
+ // Parent returns the parent node of this node.
140
+ func (n * ObjectDerefNode ) Parent () ExprNode {
141
+ return n .parent
142
+ }
143
+
100
144
// ArrayDerefNode represents elements dereference of arrays like '*' in 'foo.bar.*.piyo'.
101
145
type ArrayDerefNode struct {
102
146
// Receiver is an expression at receiver of array element dereference.
103
147
Receiver ExprNode
148
+ parent ExprNode
104
149
}
105
150
106
151
// Token returns the first token of the node. This method is useful to get position of this node.
107
- func (n ArrayDerefNode ) Token () * Token {
152
+ func (n * ArrayDerefNode ) Token () * Token {
108
153
return n .Receiver .Token ()
109
154
}
110
155
156
+ // Parent returns the parent node of this node.
157
+ func (n * ArrayDerefNode ) Parent () ExprNode {
158
+ return n .parent
159
+ }
160
+
111
161
// IndexAccessNode is node for index access, which represents dynamic object property access or
112
162
// array index access.
113
163
type IndexAccessNode struct {
114
164
// Operand is an expression at operand of index access, which should be array or object.
115
165
Operand ExprNode
116
166
// Index is an expression at index, which should be integer or string.
117
- Index ExprNode
167
+ Index ExprNode
168
+ parent ExprNode
118
169
}
119
170
120
171
// Token returns the first token of the node. This method is useful to get position of this node.
121
172
func (n * IndexAccessNode ) Token () * Token {
122
173
return n .Operand .Token ()
123
174
}
124
175
176
+ // Parent returns the parent node of this node.
177
+ func (n * IndexAccessNode ) Parent () ExprNode {
178
+ return n .parent
179
+ }
180
+
125
181
// Note: Currently only ! is a logical unary operator
126
182
127
183
// NotOpNode is node for unary ! operator.
128
184
type NotOpNode struct {
129
185
// Operand is an expression at operand of ! operator.
130
186
Operand ExprNode
131
187
tok * Token
188
+ parent ExprNode
132
189
}
133
190
134
191
// Token returns the first token of the node. This method is useful to get position of this node.
135
192
func (n * NotOpNode ) Token () * Token {
136
193
return n .tok
137
194
}
138
195
196
+ // Parent returns the parent node of this node.
197
+ func (n * NotOpNode ) Parent () ExprNode {
198
+ return n .parent
199
+ }
200
+
139
201
// CompareOpNodeKind is a kind of compare operators; ==, !=, <, <=, >, >=.
140
202
type CompareOpNodeKind int
141
203
@@ -187,14 +249,20 @@ type CompareOpNode struct {
187
249
// Left is an expression for left hand side of the binary operator.
188
250
Left ExprNode
189
251
// Right is an expression for right hand side of the binary operator.
190
- Right ExprNode
252
+ Right ExprNode
253
+ parent ExprNode
191
254
}
192
255
193
256
// Token returns the first token of the node. This method is useful to get position of this node.
194
257
func (n * CompareOpNode ) Token () * Token {
195
258
return n .Left .Token ()
196
259
}
197
260
261
+ // Parent returns the parent node of this node.
262
+ func (n * CompareOpNode ) Parent () ExprNode {
263
+ return n .parent
264
+ }
265
+
198
266
// LogicalOpNodeKind is a kind of logical operators; && and ||.
199
267
type LogicalOpNodeKind int
200
268
@@ -225,30 +293,42 @@ type LogicalOpNode struct {
225
293
// Left is an expression for left hand side of the binary operator.
226
294
Left ExprNode
227
295
// Right is an expression for right hand side of the binary operator.
228
- Right ExprNode
296
+ Right ExprNode
297
+ parent ExprNode
229
298
}
230
299
231
300
// Token returns the first token of the node. This method is useful to get position of this node.
232
301
func (n * LogicalOpNode ) Token () * Token {
233
302
return n .Left .Token ()
234
303
}
235
304
305
+ // Parent returns the parent node of this node.
306
+ func (n * LogicalOpNode ) Parent () ExprNode {
307
+ return n .parent
308
+ }
309
+
236
310
// FuncCallNode represents function call in expression.
237
311
// Note that currently only calling builtin functions is supported.
238
312
type FuncCallNode struct {
239
313
// Callee is a name of called function. This is string value because currently only built-in
240
314
// functions can be called.
241
315
Callee string
242
316
// Args is arguments of the function call.
243
- Args []ExprNode
244
- tok * Token
317
+ Args []ExprNode
318
+ tok * Token
319
+ parent ExprNode
245
320
}
246
321
247
322
// Token returns the first token of the node. This method is useful to get position of this node.
248
323
func (n * FuncCallNode ) Token () * Token {
249
324
return n .tok
250
325
}
251
326
327
+ // Parent returns the parent node of this node.
328
+ func (n * FuncCallNode ) Parent () ExprNode {
329
+ return n .parent
330
+ }
331
+
252
332
// VisitExprNodeFunc is a visitor function for VisitExprNode(). The entering argument is set to
253
333
// true when it is called before visiting children. It is set to false when it is called after
254
334
// visiting children. It means that this function is called twice for the same node. The parent
@@ -275,8 +355,8 @@ func visitExprNode(n, p ExprNode, f VisitExprNodeFunc) {
275
355
visitExprNode (n .Left , n , f )
276
356
visitExprNode (n .Right , n , f )
277
357
case * FuncCallNode :
278
- for _ , a := range n .Args {
279
- visitExprNode (a , n , f )
358
+ for i := range n .Args {
359
+ visitExprNode (n . Args [ i ] , n , f )
280
360
}
281
361
}
282
362
f (n , p , false )
@@ -286,3 +366,18 @@ func visitExprNode(n, p ExprNode, f VisitExprNodeFunc) {
286
366
func VisitExprNode (n ExprNode , f VisitExprNodeFunc ) {
287
367
visitExprNode (n , nil , f )
288
368
}
369
+
370
+ // FindParent applies predicate to each parent of this node until predicate returns true.
371
+ // Then it returns result of predicate. If no parent found, returns nil, false.
372
+ func FindParent [T ExprNode ](n ExprNode , predicate func (n ExprNode ) (T , bool )) (T , bool ) {
373
+ parent := n .Parent ()
374
+ for parent != nil {
375
+ t , ok := predicate (parent )
376
+ if ok {
377
+ return t , true
378
+ }
379
+ parent = parent .Parent ()
380
+ }
381
+ var zero T
382
+ return zero , false
383
+ }
0 commit comments