/
context.go
446 lines (418 loc) · 11.5 KB
/
context.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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
Copyright 2017 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package parser
import (
"fmt"
"github.com/google/go-jsonnet/ast"
)
var topLevelContext = "$"
const anonymous = "anonymous"
// TODO(sbarzowski) The children functions should definitely be moved to ast
// package or a separate internal astutils package. The only reason I'm not doing it
// right now is that it's a pretty invasive change that deserves a separate PR.
// DirectChildren are children of AST node that are executed in the same context
// and environment as their parent. It supports ASTs before and after desugaring.
//
// They must satisfy the following rules:
// * (no-delayed-evaluation) They are evaluated when their parent is evaluated or never.
// * (no-indirect-evaluation) They cannot be evaluated during evaluation of any non-direct children
// * (same-environment) They must be evaluated in the same environment as their parent
func DirectChildren(node ast.Node) []ast.Node {
switch node := node.(type) {
case *ast.Apply:
return []ast.Node{node.Target}
// TODO(sbarzowski) tailstrict call arguments (once we have tailstrict)
case *ast.ApplyBrace:
return []ast.Node{node.Left, node.Right}
case *ast.Array:
return nil
case *ast.Assert:
if node.Message != nil {
return []ast.Node{node.Cond, node.Message, node.Rest}
}
return []ast.Node{node.Cond, node.Rest}
case *ast.Binary:
return []ast.Node{node.Left, node.Right}
case *ast.Conditional:
if node.BranchFalse != nil {
return []ast.Node{node.Cond, node.BranchTrue, node.BranchFalse}
}
return []ast.Node{node.Cond, node.BranchTrue}
case *ast.Dollar:
return nil
case *ast.Error:
return []ast.Node{node.Expr}
case *ast.Function:
return nil
case *ast.Import, *ast.ImportStr, *ast.ImportBin:
return nil
case *ast.Index:
if node.Id != nil {
return nil // non-desugared dot reference
}
return []ast.Node{node.Target, node.Index}
case *ast.Slice:
var params []ast.Node
if node.Target != nil {
params = append(params, node.Target)
}
if node.BeginIndex != nil {
params = append(params, node.BeginIndex)
}
if node.EndIndex != nil {
params = append(params, node.EndIndex)
}
return params
case *ast.Local:
return nil
case *ast.LiteralBoolean:
return nil
case *ast.LiteralNull:
return nil
case *ast.LiteralNumber:
return nil
case *ast.LiteralString:
return nil
case *ast.Object:
return objectFieldsDirectChildren(node.Fields)
case *ast.DesugaredObject:
return desugaredObjectDirectChildren(node)
case *ast.ArrayComp:
result := []ast.Node{}
spec := &node.Spec
for spec != nil {
result = append(result, spec.Expr)
for _, ifspec := range spec.Conditions {
result = append(result, ifspec.Expr)
}
spec = spec.Outer
}
return result
case *ast.ObjectComp:
result := objectFieldsDirectChildren(node.Fields)
spec := &node.Spec
for spec != nil {
result = append(result, spec.Expr)
for _, ifspec := range spec.Conditions {
result = append(result, ifspec.Expr)
}
spec = spec.Outer
}
return result
case *ast.Parens:
return []ast.Node{node.Inner}
case *ast.Self:
return nil
case *ast.SuperIndex:
if node.Id != nil {
return nil
}
return []ast.Node{node.Index}
case *ast.InSuper:
return []ast.Node{node.Index}
case *ast.Unary:
return []ast.Node{node.Expr}
case *ast.Var:
return nil
}
panic(fmt.Sprintf("directChildren: Unknown node %#v", node))
}
// thunkChildren are children of AST node that are executed in a new context
// and capture environment from parent (thunked).
//
// It supports ASTs before and after desugaring.
//
// TODO(sbarzowski) Make sure it works well with boundary cases like tailstrict arguments,
// make it more precise.
//
// Rules:
// * (same-environment) They must be evaluated in the same environment as their parent
// * (not-direct) If they can be direct children, they should (and cannot be thunked).
func thunkChildren(node ast.Node) []ast.Node {
switch node := node.(type) {
case *ast.Apply:
var nodes []ast.Node
for _, arg := range node.Arguments.Positional {
nodes = append(nodes, arg.Expr)
}
for _, arg := range node.Arguments.Named {
nodes = append(nodes, arg.Arg)
}
return nodes
case *ast.ApplyBrace:
return nil
case *ast.Array:
var nodes []ast.Node
for _, element := range node.Elements {
nodes = append(nodes, element.Expr)
}
return nodes
case *ast.Assert:
return nil
case *ast.Binary:
return nil
case *ast.Conditional:
return nil
case *ast.Dollar:
return nil
case *ast.Error:
return nil
case *ast.Function:
return nil
case *ast.Import, *ast.ImportStr, *ast.ImportBin:
return nil
case *ast.Index:
return nil
case *ast.Slice:
return nil
case *ast.Local:
// TODO(sbarzowski) complicated
return nil
case *ast.LiteralBoolean:
return nil
case *ast.LiteralNull:
return nil
case *ast.LiteralNumber:
return nil
case *ast.LiteralString:
return nil
case *ast.DesugaredObject:
return nil
case *ast.Object:
return nil
case *ast.ArrayComp:
return []ast.Node{node.Body}
case *ast.ObjectComp:
return nil
case *ast.Parens:
return nil
case *ast.Self:
return nil
case *ast.SuperIndex:
return nil
case *ast.InSuper:
return nil
case *ast.Unary:
return nil
case *ast.Var:
return nil
}
panic(fmt.Sprintf("thunkChildren: Unknown node %#v", node))
}
func objectFieldsDirectChildren(fields ast.ObjectFields) ast.Nodes {
result := ast.Nodes{}
for _, field := range fields {
if field.Expr1 != nil {
result = append(result, field.Expr1)
}
}
return result
}
func inObjectFieldsChildren(fields ast.ObjectFields) ast.Nodes {
result := ast.Nodes{}
for _, field := range fields {
if field.Method != nil {
result = append(result, field.Method)
} else {
if field.Expr2 != nil {
result = append(result, field.Expr2)
}
if field.Expr3 != nil {
result = append(result, field.Expr3)
}
}
}
return result
}
func desugaredObjectDirectChildren(obj *ast.DesugaredObject) ast.Nodes {
result := ast.Nodes{}
for _, field := range obj.Fields {
if field.Name == nil {
panic("Name cannot be nil")
}
result = append(result, field.Name)
}
return result
}
func inDesugaredObjectSpecialChildren(obj *ast.DesugaredObject) ast.Nodes {
result := make([]ast.Node, 0, len(obj.Fields)+len(obj.Locals))
for _, field := range obj.Fields {
result = append(result, field.Body)
}
for _, local := range obj.Locals {
result = append(result, local.Body)
}
return result
}
// specialChildren returns children that are neither direct nor thunked,
// e.g. object field body.
// These nodes are evaluated in a different environment from their parent.
//
// It supports ASTs before and after desugaring.
func specialChildren(node ast.Node) []ast.Node {
switch node := node.(type) {
case *ast.Apply:
return nil
case *ast.ApplyBrace:
return nil
case *ast.Array:
return nil
case *ast.Assert:
return nil
case *ast.Binary:
return nil
case *ast.Conditional:
return nil
case *ast.Dollar:
return nil
case *ast.Error:
return nil
case *ast.Function:
children := []ast.Node{node.Body}
for _, child := range node.Parameters {
if child.DefaultArg != nil {
children = append(children, child.DefaultArg)
}
}
return children
case *ast.Import, *ast.ImportStr, *ast.ImportBin:
return nil
case *ast.Index:
return nil
case *ast.Slice:
return nil
case *ast.Local:
children := make([]ast.Node, 1, len(node.Binds)+1)
children[0] = node.Body
for _, bind := range node.Binds {
children = append(children, bind.Body)
}
return children
case *ast.LiteralBoolean:
return nil
case *ast.LiteralNull:
return nil
case *ast.LiteralNumber:
return nil
case *ast.LiteralString:
return nil
case *ast.DesugaredObject:
return inDesugaredObjectSpecialChildren(node)
case *ast.Object:
return inObjectFieldsChildren(node.Fields)
case *ast.ArrayComp:
return []ast.Node{node.Body}
case *ast.ObjectComp:
return inObjectFieldsChildren(node.Fields)
case *ast.Self:
return nil
case *ast.SuperIndex:
return nil
case *ast.InSuper:
return nil
case *ast.Unary:
return nil
case *ast.Var:
return nil
case *ast.Parens:
return nil
}
panic(fmt.Sprintf("specialChildren: Unknown node %#v", node))
}
// Children returns all children of a node. It supports ASTs before and after desugaring.
func Children(node ast.Node) []ast.Node {
var result []ast.Node
result = append(result, DirectChildren(node)...)
result = append(result, thunkChildren(node)...)
result = append(result, specialChildren(node)...)
return result
}
func functionContext(funcName string) *string {
r := "function <" + funcName + ">"
return &r
}
func objectContext(objName string) *string {
r := "object <" + objName + ">"
return &r
}
// addContext adds context to a node and its whole subtree.
//
// context is the surrounding context of a node (e.g. a function it's in)
//
// bind is a name that the node is bound to, i.e. if node is a local bind body
// then bind is its name. For nodes that are not bound to variables `anonymous`
// should be passed. For example:
// local x = 2 + 2; x
// In such case bind for binary node 2 + 2 is "x" and for every other node,
// including its children, its anonymous.
func addContext(node ast.Node, context *string, bind string) {
if node == nil {
return
}
node.SetContext(context)
switch node := node.(type) {
case *ast.Function:
funContext := functionContext(bind)
addContext(node.Body, funContext, anonymous)
for i := range node.Parameters {
if node.Parameters[i].DefaultArg != nil {
// Default arguments have the same context as the function body.
addContext(node.Parameters[i].DefaultArg, funContext, anonymous)
}
}
case *ast.Object:
// TODO(sbarzowski) include fieldname, maybe even chains
outOfObject := DirectChildren(node)
for _, f := range outOfObject {
// This actually is evaluated outside of object
addContext(f, context, anonymous)
}
objContext := objectContext(bind)
inObject := inObjectFieldsChildren(node.Fields)
for _, f := range inObject {
// This actually is evaluated outside of object
addContext(f, objContext, anonymous)
}
case *ast.ObjectComp:
outOfObject := DirectChildren(node)
for _, f := range outOfObject {
// This actually is evaluated outside of object
addContext(f, context, anonymous)
}
objContext := objectContext(bind)
inObject := inObjectFieldsChildren(node.Fields)
for _, f := range inObject {
// This actually is evaluated outside of object
addContext(f, objContext, anonymous)
}
case *ast.Local:
for _, bind := range node.Binds {
namedThunkContext := "thunk <" + string(bind.Variable) + "> from <" + *context + ">"
if bind.Fun != nil {
addContext(bind.Fun, &namedThunkContext, string(bind.Variable))
} else {
addContext(bind.Body, &namedThunkContext, string(bind.Variable))
}
}
addContext(node.Body, context, bind)
default:
for _, child := range DirectChildren(node) {
addContext(child, context, anonymous)
}
// TODO(sbarzowski) avoid "thunk from <thunk from..."
thunkContext := "thunk from <" + *context + ">"
for _, child := range thunkChildren(node) {
addContext(child, &thunkContext, anonymous)
}
}
}