@@ -16,179 +16,238 @@ type Node interface {
16
16
String () string
17
17
}
18
18
19
+ // Patch replaces the node with a new one.
20
+ // Location information is preserved.
21
+ // Type information is lost.
19
22
func Patch (node * Node , newNode Node ) {
20
23
newNode .SetLocation ((* node ).Location ())
21
24
* node = newNode
22
25
}
23
26
27
+ // base is a base struct for all nodes.
24
28
type base struct {
25
29
loc file.Location
26
30
nodeType reflect.Type
27
31
}
28
32
33
+ // Location returns the location of the node in the source code.
29
34
func (n * base ) Location () file.Location {
30
35
return n .loc
31
36
}
32
37
38
+ // SetLocation sets the location of the node in the source code.
33
39
func (n * base ) SetLocation (loc file.Location ) {
34
40
n .loc = loc
35
41
}
36
42
43
+ // Type returns the type of the node.
37
44
func (n * base ) Type () reflect.Type {
38
45
return n .nodeType
39
46
}
40
47
48
+ // SetType sets the type of the node.
41
49
func (n * base ) SetType (t reflect.Type ) {
42
50
n .nodeType = t
43
51
}
44
52
53
+ // NilNode represents nil.
45
54
type NilNode struct {
46
55
base
47
56
}
48
57
58
+ // IdentifierNode represents an identifier.
49
59
type IdentifierNode struct {
50
60
base
51
- Value string
52
- FieldIndex []int
53
- Method bool // true if method, false if field
54
- MethodIndex int // index of method, set only if Method is true
61
+ Value string // Name of the identifier. Like "foo" in "foo.bar".
62
+ FieldIndex []int // Index of the field in the list of fields.
63
+ Method bool // If true then the identifier is a method call.
64
+ MethodIndex int // Index of the method in the list of methods.
55
65
}
56
66
67
+ // SetFieldIndex sets the field index of the identifier.
57
68
func (n * IdentifierNode ) SetFieldIndex (field []int ) {
58
69
n .FieldIndex = field
59
70
}
60
71
72
+ // SetMethodIndex sets the method index of the identifier.
61
73
func (n * IdentifierNode ) SetMethodIndex (methodIndex int ) {
62
74
n .Method = true
63
75
n .MethodIndex = methodIndex
64
76
}
65
77
78
+ // IntegerNode represents an integer.
66
79
type IntegerNode struct {
67
80
base
68
- Value int
81
+ Value int // Value of the integer.
69
82
}
70
83
84
+ // FloatNode represents a float.
71
85
type FloatNode struct {
72
86
base
73
- Value float64
87
+ Value float64 // Value of the float.
74
88
}
75
89
90
+ // BoolNode represents a boolean.
76
91
type BoolNode struct {
77
92
base
78
- Value bool
93
+ Value bool // Value of the boolean.
79
94
}
80
95
96
+ // StringNode represents a string.
81
97
type StringNode struct {
82
98
base
83
- Value string
99
+ Value string // Value of the string.
84
100
}
85
101
102
+ // ConstantNode represents a constant.
103
+ // Constants are predefined values like nil, true, false, array, map, etc.
104
+ // The parser.Parse will never generate ConstantNode, it is only generated
105
+ // by the optimizer.
86
106
type ConstantNode struct {
87
107
base
88
- Value any
108
+ Value any // Value of the constant.
89
109
}
90
110
111
+ // UnaryNode represents a unary operator.
91
112
type UnaryNode struct {
92
113
base
93
- Operator string
94
- Node Node
114
+ Operator string // Operator of the unary operator. Like "!" in "!foo" or "not" in "not foo".
115
+ Node Node // Node of the unary operator. Like "foo" in "!foo".
95
116
}
96
117
118
+ // BinaryNode represents a binary operator.
97
119
type BinaryNode struct {
98
120
base
99
- Regexp * regexp.Regexp
100
- Operator string
101
- Left Node
102
- Right Node
121
+ Regexp * regexp.Regexp // Regexp of the "matches" operator. Like "f.+" in `foo matches "f.+"`.
122
+ Operator string // Operator of the binary operator. Like "+" in "foo + bar" or "matches" in "foo matches bar".
123
+ Left Node // Left node of the binary operator.
124
+ Right Node // Right node of the binary operator.
103
125
}
104
126
127
+ // ChainNode represents an optional chaining group.
128
+ // A few MemberNode nodes can be chained together,
129
+ // and will be wrapped in a ChainNode. Example:
130
+ // ```
131
+ // foo.bar?.baz?.qux
132
+ // ```
133
+ // The whole chain will be wrapped in a ChainNode.
105
134
type ChainNode struct {
106
135
base
107
- Node Node
136
+ Node Node // Node of the chain.
108
137
}
109
138
139
+ // MemberNode represents a member access.
140
+ // It can be a field access, a method call,
141
+ // or an array element access.
142
+ // Example:
143
+ // ```
144
+ // foo.bar or foo["bar"]
145
+ // foo.bar()
146
+ // array[0]
147
+ // ```
110
148
type MemberNode struct {
111
149
base
112
- Node Node
113
- Property Node
150
+ Node Node // Node of the member access. Like "foo" in "foo.bar".
151
+ Property Node // Property of the member access. For property access it is a StringNode.
114
152
Name string // Name of the filed or method. Used for error reporting.
115
- Optional bool
116
- FieldIndex []int
153
+ Optional bool // If true then the member access is optional. Like "foo?.bar".
154
+ FieldIndex []int // Index sequence of fields. Generated by type checker.
117
155
118
156
// TODO: Combine Method and MethodIndex into a single MethodIndex field of &int type.
119
- Method bool
120
- MethodIndex int
157
+ Method bool // If true then the member access is a method call.
158
+ MethodIndex int // Index of the method in the list of methods. Generated by type checker.
121
159
}
122
160
161
+ // SetFieldIndex sets the field index of the member access.
123
162
func (n * MemberNode ) SetFieldIndex (field []int ) {
124
163
n .FieldIndex = field
125
164
}
126
165
166
+ // SetMethodIndex sets the method index of the member access.
127
167
func (n * MemberNode ) SetMethodIndex (methodIndex int ) {
128
168
n .Method = true
129
169
n .MethodIndex = methodIndex
130
170
}
131
171
172
+ // SliceNode represents access to a slice of an array.
173
+ // Example:
174
+ // ```
175
+ // array[1:4]
176
+ // ```
132
177
type SliceNode struct {
133
178
base
134
- Node Node
135
- From Node
136
- To Node
179
+ Node Node // Node of the slice. Like "array" in "array[1:4]".
180
+ From Node // From an index of the array. Like "1" in "array[1:4]".
181
+ To Node // To an index of the array. Like "4" in "array[1:4]".
137
182
}
138
183
184
+ // CallNode represents a function or a method call.
139
185
type CallNode struct {
140
186
base
141
- Callee Node
142
- Arguments []Node
143
- Typed int
144
- Fast bool
145
- Func * Function
187
+ Callee Node // Node of the call. Like "foo" in "foo()".
188
+ Arguments []Node // Arguments of the call.
189
+ Typed int // If more than 0 then the call is one of the types from vm.FuncTypes.
190
+ Fast bool // If true then the call type is "func(...any) any".
191
+ Func * Function // If not nil then the func is provider via expr.Function.
146
192
}
147
193
194
+ // BuiltinNode represents a builtin function call.
148
195
type BuiltinNode struct {
149
196
base
150
- Name string
151
- Arguments []Node
152
- Throws bool
153
- Map Node
197
+ Name string // Name of the builtin function. Like "len" in "len(foo)".
198
+ Arguments []Node // Arguments of the builtin function.
199
+ Throws bool // If true then accessing a field or array index can throw an error. Used by optimizer.
200
+ Map Node // Used by optimizer to fold filter() and map() builtins.
154
201
}
155
202
203
+ // ClosureNode represents a predicate.
204
+ // Example:
205
+ // ```
206
+ // filter(foo, .bar == 1)
207
+ // ```
208
+ // The predicate is `.bar == 1`.
156
209
type ClosureNode struct {
157
210
base
158
- Node Node
211
+ Node Node // Node of the predicate body.
159
212
}
160
213
214
+ // PointerNode represents a pointer to a current value in predicate.
161
215
type PointerNode struct {
162
216
base
163
- Name string
217
+ Name string // Name of the pointer. Like "index" in "#index".
164
218
}
165
219
220
+ // ConditionalNode represents a ternary operator.
166
221
type ConditionalNode struct {
167
222
base
168
- Cond Node
169
- Exp1 Node
170
- Exp2 Node
223
+ Cond Node // Condition of the ternary operator. Like "foo" in "foo ? bar : baz".
224
+ Exp1 Node // Expression 1 of the ternary operator. Like "bar" in "foo ? bar : baz".
225
+ Exp2 Node // Expression 2 of the ternary operator. Like "baz" in "foo ? bar : baz".
171
226
}
172
227
228
+ // VariableDeclaratorNode represents a variable declaration.
173
229
type VariableDeclaratorNode struct {
174
230
base
175
- Name string
176
- Value Node
177
- Expr Node
231
+ Name string // Name of the variable. Like "foo" in "let foo = 1; foo + 1".
232
+ Value Node // Value of the variable. Like "1" in "let foo = 1; foo + 1".
233
+ Expr Node // Expression of the variable. Like "foo + 1" in "let foo = 1; foo + 1".
178
234
}
179
235
236
+ // ArrayNode represents an array.
180
237
type ArrayNode struct {
181
238
base
182
- Nodes []Node
239
+ Nodes []Node // Nodes of the array.
183
240
}
184
241
242
+ // MapNode represents a map.
185
243
type MapNode struct {
186
244
base
187
- Pairs []Node
245
+ Pairs []Node // PairNode nodes.
188
246
}
189
247
248
+ // PairNode represents a key-value pair of a map.
190
249
type PairNode struct {
191
250
base
192
- Key Node
193
- Value Node
251
+ Key Node // Key of the pair.
252
+ Value Node // Value of the pair.
194
253
}
0 commit comments