forked from google/go-jsonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
desugarer.go
631 lines (561 loc) · 14.5 KB
/
desugarer.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/*
Copyright 2016 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 jsonnet
import (
"bytes"
"encoding/hex"
"fmt"
"reflect"
"unicode/utf8"
"github.com/google/go-jsonnet/ast"
"github.com/google/go-jsonnet/parser"
)
func makeStr(s string) *ast.LiteralString {
return &ast.LiteralString{
NodeBase: ast.NodeBase{},
Value: s,
Kind: ast.StringDouble,
BlockIndent: "",
}
}
func stringUnescape(loc *ast.LocationRange, s string) (string, error) {
var buf bytes.Buffer
// read one rune at a time
for i := 0; i < len(s); {
r, w := utf8.DecodeRuneInString(s[i:])
i += w
switch r {
case '\\':
if i >= len(s) {
return "", parser.MakeStaticError("Truncated escape sequence in string literal.", *loc)
}
r2, w := utf8.DecodeRuneInString(s[i:])
i += w
switch r2 {
case '"':
buf.WriteRune('"')
case '\'':
buf.WriteRune('\'')
case '\\':
buf.WriteRune('\\')
case '/':
buf.WriteRune('/') // See json.org, \/ is a valid escape.
case 'b':
buf.WriteRune('\b')
case 'f':
buf.WriteRune('\f')
case 'n':
buf.WriteRune('\n')
case 'r':
buf.WriteRune('\r')
case 't':
buf.WriteRune('\t')
case 'u':
if i+4 > len(s) {
return "", parser.MakeStaticError("Truncated unicode escape sequence in string literal.", *loc)
}
codeBytes, err := hex.DecodeString(s[i : i+4])
if err != nil {
return "", parser.MakeStaticError(fmt.Sprintf("Unicode escape sequence was malformed: %s", s[0:4]), *loc)
}
code := int(codeBytes[0])*256 + int(codeBytes[1])
buf.WriteRune(rune(code))
i += 4
default:
return "", parser.MakeStaticError(fmt.Sprintf("Unknown escape sequence in string literal: \\%c", r2), *loc)
}
default:
buf.WriteRune(r)
}
}
return buf.String(), nil
}
func desugarFields(location ast.LocationRange, fields *ast.ObjectFields, objLevel int) error {
// Simplify asserts
for i := range *fields {
field := &(*fields)[i]
if field.Kind != ast.ObjectAssert {
continue
}
msg := field.Expr3
if msg == nil {
msg = buildLiteralString("Object assertion failed.")
}
field.Expr3 = nil
onFailure := &ast.Error{Expr: msg}
assertion := &ast.Conditional{
Cond: field.Expr2,
BranchTrue: &ast.LiteralBoolean{Value: true}, // ignored anyway
BranchFalse: onFailure,
}
field.Expr2 = assertion
}
for i := range *fields {
field := &((*fields)[i])
if field.Method == nil {
continue
}
field.Expr2 = field.Method
field.Method = nil
// Body of the function already desugared through expr2
}
// Remove object-level locals
newFields := []ast.ObjectField{}
for _, field := range *fields {
if field.Kind == ast.ObjectLocal {
continue
}
var binds ast.LocalBinds
for _, local := range *fields {
if local.Kind != ast.ObjectLocal {
continue
}
binds = append(binds, ast.LocalBind{Variable: *local.Id, Body: ast.Clone(local.Expr2)})
}
if len(binds) > 0 {
field.Expr2 = &ast.Local{
NodeBase: ast.NewNodeBaseLoc(*field.Expr2.Loc()),
Binds: binds,
Body: field.Expr2,
}
}
newFields = append(newFields, field)
}
*fields = newFields
// Change all to FIELD_EXPR
for i := range *fields {
field := &(*fields)[i]
switch field.Kind {
case ast.ObjectAssert:
// Nothing to do.
case ast.ObjectFieldID:
field.Expr1 = makeStr(string(*field.Id))
field.Kind = ast.ObjectFieldExpr
case ast.ObjectFieldExpr:
// Nothing to do.
case ast.ObjectFieldStr:
// Just set the flag.
field.Kind = ast.ObjectFieldExpr
case ast.ObjectLocal:
return fmt.Errorf("INTERNAL ERROR: Locals should be removed by now")
}
}
return nil
}
func simpleLambda(body ast.Node, paramName ast.Identifier) ast.Node {
return &ast.Function{
Body: body,
Parameters: ast.Parameters{Required: ast.Identifiers{paramName}},
}
}
func buildAnd(left ast.Node, right ast.Node) ast.Node {
return &ast.Binary{Op: ast.BopAnd, Left: left, Right: right}
}
func desugarForSpec(inside ast.Node, forSpec *ast.ForSpec) (ast.Node, error) {
var body ast.Node
if len(forSpec.Conditions) > 0 {
cond := forSpec.Conditions[0].Expr
for i := 1; i < len(forSpec.Conditions); i++ {
cond = buildAnd(cond, forSpec.Conditions[i].Expr)
}
body = &ast.Conditional{
Cond: cond,
BranchTrue: inside,
BranchFalse: &ast.Array{},
}
} else {
body = inside
}
function := simpleLambda(body, forSpec.VarName)
current := buildStdCall("flatMap", function, forSpec.Expr)
if forSpec.Outer == nil {
return current, nil
}
return desugarForSpec(current, forSpec.Outer)
}
func wrapInArray(inside ast.Node) ast.Node {
return &ast.Array{Elements: ast.Nodes{inside}}
}
func desugarArrayComp(comp *ast.ArrayComp, objLevel int) (ast.Node, error) {
return desugarForSpec(wrapInArray(comp.Body), &comp.Spec)
}
func desugarObjectComp(comp *ast.ObjectComp, objLevel int) (ast.Node, error) {
if objLevel == 0 {
dollar := ast.Identifier("$")
comp.Fields = append(comp.Fields, ast.ObjectFieldLocalNoMethod(&dollar, &ast.Self{}))
}
err := desugarFields(*comp.Loc(), &comp.Fields, objLevel+1)
if err != nil {
return nil, err
}
if len(comp.Fields) != 1 {
panic("Too many fields in object comprehension, it should have been caught during parsing")
}
arrComp := ast.ArrayComp{
Body: buildDesugaredObject(comp.NodeBase, comp.Fields),
Spec: comp.Spec,
}
desugaredArrayComp, err := desugarArrayComp(&arrComp, objLevel)
if err != nil {
return nil, err
}
desugaredComp := buildStdCall("$objectFlatMerge", desugaredArrayComp)
return desugaredComp, nil
}
func buildLiteralString(value string) ast.Node {
return &ast.LiteralString{
Kind: ast.StringDouble,
Value: value,
}
}
func buildSimpleIndex(obj ast.Node, member ast.Identifier) ast.Node {
return &ast.Index{
Target: obj,
Id: &member,
}
}
func buildStdCall(builtinName ast.Identifier, args ...ast.Node) ast.Node {
std := &ast.Var{Id: "std"}
builtin := buildSimpleIndex(std, builtinName)
return &ast.Apply{
Target: builtin,
Arguments: ast.Arguments{Positional: args},
}
}
func buildDesugaredObject(nodeBase ast.NodeBase, fields ast.ObjectFields) *ast.DesugaredObject {
var newFields ast.DesugaredObjectFields
var newAsserts ast.Nodes
for _, field := range fields {
if field.Kind == ast.ObjectAssert {
newAsserts = append(newAsserts, field.Expr2)
} else if field.Kind == ast.ObjectFieldExpr {
newFields = append(newFields, ast.DesugaredObjectField{
Hide: field.Hide,
Name: field.Expr1,
Body: field.Expr2,
PlusSuper: field.SuperSugar,
})
} else {
panic(fmt.Sprintf("INTERNAL ERROR: field should have been desugared: %v", field.Kind))
}
}
return &ast.DesugaredObject{
NodeBase: nodeBase,
Asserts: newAsserts,
Fields: newFields,
}
}
// Desugar Jsonnet expressions to reduce the number of constructs the rest of the implementation
// needs to understand.
//
// Note that despite the name, desugar() is not idempotent. String literals have their escape
// codes translated to low-level characters during desugaring.
//
// Desugaring should happen immediately after parsing, i.e. before static analysis and execution.
// Temporary variables introduced here should be prefixed with $ to ensure they do not clash with
// variables used in user code.
// TODO(sbarzowski) Actually we may want to do some static analysis before desugaring, e.g.
// warning user about dangerous use of constructs that we desugar.
func desugar(astPtr *ast.Node, objLevel int) (err error) {
node := *astPtr
if node == nil {
return
}
switch node := node.(type) {
case *ast.Apply:
desugar(&node.Target, objLevel)
for i := range node.Arguments.Positional {
err = desugar(&node.Arguments.Positional[i], objLevel)
if err != nil {
return
}
}
for i := range node.Arguments.Named {
err = desugar(&node.Arguments.Named[i].Arg, objLevel)
if err != nil {
return
}
}
case *ast.ApplyBrace:
err = desugar(&node.Left, objLevel)
if err != nil {
return
}
err = desugar(&node.Right, objLevel)
if err != nil {
return
}
*astPtr = &ast.Binary{
NodeBase: node.NodeBase,
Left: node.Left,
Op: ast.BopPlus,
Right: node.Right,
}
case *ast.Array:
for i := range node.Elements {
err = desugar(&node.Elements[i], objLevel)
if err != nil {
return
}
}
case *ast.ArrayComp:
comp, err := desugarArrayComp(node, objLevel)
if err != nil {
return err
}
*astPtr = comp
err = desugar(astPtr, objLevel)
if err != nil {
return err
}
case *ast.Assert:
if node.Message == nil {
node.Message = buildLiteralString("Assertion failed")
}
*astPtr = &ast.Conditional{
Cond: node.Cond,
BranchTrue: node.Rest,
BranchFalse: &ast.Error{Expr: node.Message},
}
err = desugar(astPtr, objLevel)
if err != nil {
return err
}
case *ast.Binary:
// some operators get replaced by stdlib functions
if funcname, replaced := desugaredBop[node.Op]; replaced {
if funcname == "notEquals" {
// TODO(sbarzowski) maybe we can handle it in more regular way
// but let's be consistent with the spec
*astPtr = &ast.Unary{
Op: ast.UopNot,
Expr: buildStdCall(desugaredBop[ast.BopManifestEqual], node.Left, node.Right),
}
} else if node.Op == ast.BopIn {
// reversed order of arguments
*astPtr = buildStdCall(funcname, node.Right, node.Left)
} else {
*astPtr = buildStdCall(funcname, node.Left, node.Right)
}
return desugar(astPtr, objLevel)
}
err = desugar(&node.Left, objLevel)
if err != nil {
return
}
err = desugar(&node.Right, objLevel)
if err != nil {
return
}
case *ast.Conditional:
err = desugar(&node.Cond, objLevel)
if err != nil {
return
}
err = desugar(&node.BranchTrue, objLevel)
if err != nil {
return
}
if node.BranchFalse == nil {
node.BranchFalse = &ast.LiteralNull{}
}
err = desugar(&node.BranchFalse, objLevel)
if err != nil {
return
}
case *ast.Dollar:
if objLevel == 0 {
return parser.MakeStaticError("No top-level object found.", *node.Loc())
}
*astPtr = &ast.Var{NodeBase: node.NodeBase, Id: ast.Identifier("$")}
case *ast.Error:
err = desugar(&node.Expr, objLevel)
if err != nil {
return
}
case *ast.Function:
for i := range node.Parameters.Optional {
param := &node.Parameters.Optional[i]
err = desugar(¶m.DefaultArg, objLevel)
if err != nil {
return
}
}
err = desugar(&node.Body, objLevel)
if err != nil {
return
}
case *ast.Import:
// desugar() is allowed to update the pointer to point to something else, but will never do
// this for a LiteralString. We cannot simply do &node.File because the type is
// **ast.LiteralString which is not compatible with *ast.Node.
var file ast.Node = node.File
err = desugar(&file, objLevel)
if err != nil {
return
}
case *ast.ImportStr:
// See comment in ast.Import.
var file ast.Node = node.File
err = desugar(&file, objLevel)
if err != nil {
return
}
case *ast.Index:
err = desugar(&node.Target, objLevel)
if err != nil {
return
}
if node.Id != nil {
if node.Index != nil {
panic(fmt.Sprintf("Node with both Id and Index: %#+v", node))
}
node.Index = makeStr(string(*node.Id))
node.Id = nil
}
err = desugar(&node.Index, objLevel)
if err != nil {
return
}
case *ast.Slice:
if node.BeginIndex == nil {
node.BeginIndex = &ast.LiteralNull{}
}
if node.EndIndex == nil {
node.EndIndex = &ast.LiteralNull{}
}
if node.Step == nil {
node.Step = &ast.LiteralNull{}
}
*astPtr = buildStdCall("slice", node.Target, node.BeginIndex, node.EndIndex, node.Step)
err = desugar(astPtr, objLevel)
if err != nil {
return
}
case *ast.Local:
for i := range node.Binds {
if node.Binds[i].Fun != nil {
node.Binds[i] = ast.LocalBind{
Variable: node.Binds[i].Variable,
Body: node.Binds[i].Fun,
Fun: nil,
}
}
err = desugar(&node.Binds[i].Body, objLevel)
if err != nil {
return
}
}
err = desugar(&node.Body, objLevel)
if err != nil {
return
}
case *ast.LiteralBoolean:
// Nothing to do.
case *ast.LiteralNull:
// Nothing to do.
case *ast.LiteralNumber:
// Nothing to do.
case *ast.LiteralString:
if node.Kind.FullyEscaped() {
unescaped, err := stringUnescape(node.Loc(), node.Value)
if err != nil {
return err
}
node.Value = unescaped
}
node.Kind = ast.StringDouble
node.BlockIndent = ""
case *ast.Object:
// Hidden variable to allow $ binding.
if objLevel == 0 {
dollar := ast.Identifier("$")
node.Fields = append(node.Fields, ast.ObjectFieldLocalNoMethod(&dollar, &ast.Self{}))
}
err = desugarFields(*node.Loc(), &node.Fields, objLevel)
if err != nil {
return
}
*astPtr = buildDesugaredObject(node.NodeBase, node.Fields)
err = desugar(astPtr, objLevel)
if err != nil {
return
}
case *ast.DesugaredObject:
for i := range node.Fields {
field := &((node.Fields)[i])
if field.Name != nil {
err := desugar(&field.Name, objLevel)
if err != nil {
return err
}
}
err := desugar(&field.Body, objLevel+1)
if err != nil {
return err
}
}
for i := range node.Asserts {
assert := &((node.Asserts)[i])
err := desugar(assert, objLevel+1)
if err != nil {
return err
}
}
case *ast.ObjectComp:
comp, err := desugarObjectComp(node, objLevel)
if err != nil {
return err
}
err = desugar(&comp, objLevel)
if err != nil {
return err
}
*astPtr = comp
case *ast.Parens:
*astPtr = node.Inner
err = desugar(astPtr, objLevel)
if err != nil {
return err
}
case *ast.Self:
// Nothing to do.
case *ast.SuperIndex:
if node.Id != nil {
node.Index = &ast.LiteralString{Value: string(*node.Id)}
node.Id = nil
}
case *ast.InSuper:
err := desugar(&node.Index, objLevel)
if err != nil {
return err
}
case *ast.Unary:
err = desugar(&node.Expr, objLevel)
if err != nil {
return
}
case *ast.Var:
// Nothing to do.
default:
panic(fmt.Sprintf("Desugarer does not recognize ast: %s", reflect.TypeOf(node)))
}
return nil
}
func desugarFile(ast *ast.Node) error {
err := desugar(ast, 0)
if err != nil {
return err
}
return nil
}