This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
/
builder.go
679 lines (602 loc) · 14.2 KB
/
builder.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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
package eval
import (
"fmt"
"strings"
"unicode"
"github.com/acorn-io/aml/pkg/ast"
"github.com/acorn-io/aml/pkg/errors"
"github.com/acorn-io/aml/pkg/token"
"github.com/acorn-io/aml/pkg/value"
)
type BuildOption struct {
PositionalArgs []any
Args map[string]any
Profiles []string
}
type BuildOptions []BuildOption
func (b BuildOptions) Merge() (merged BuildOption) {
for _, opt := range b {
merged.PositionalArgs = append(merged.PositionalArgs, opt.PositionalArgs...)
if merged.Args == nil {
merged.Args = map[string]any{}
}
for k, v := range opt.Args {
merged.Args[k] = v
}
merged.Profiles = append(merged.Profiles, opt.Profiles...)
}
return
}
func Build(file *ast.File, opts ...BuildOption) (*File, error) {
s, err := fileToObject(file)
if err != nil {
return nil, err
}
opt := BuildOptions(opts).Merge()
return &File{
PositionalArgs: opt.PositionalArgs,
Args: opt.Args,
Profiles: opt.Profiles,
Body: s,
}, nil
}
func fileToObject(file *ast.File) (*Struct, error) {
fields, err := declsToFields(file.Decls)
if err != nil {
return nil, err
}
return &Struct{
Position: pos(file.Pos()),
Comments: getComments(file),
Fields: fields,
}, err
}
func declsToFields(decls []ast.Decl) (result []Field, err error) {
var (
errs []error
fields []Field
)
for _, decl := range decls {
field, err := declToField(decl)
if err != nil {
errs = append(errs, err)
continue
}
fields = append(fields, field)
}
return fields, errors.Join(errs...)
}
func declToField(decl ast.Decl) (ref Field, err error) {
switch v := decl.(type) {
case *ast.Field:
var result KeyValue
result.Comments = getComments(decl)
result.Optional = v.Constraint == token.OPTION
result.Key, err = labelToKey(v.Label, v.Match != token.NoPos)
if err != nil {
return &result, err
}
result.Pos = pos(decl.Pos())
result.Value, err = exprToExpression(v.Value)
return &result, err
case *ast.EmbedDecl:
var result Embedded
result.Pos = pos(decl.Pos())
result.Comments = getComments(decl)
result.Expression, err = exprToExpression(v.Expr)
return &result, err
case *ast.LetClause:
var result KeyValue
result.Comments = getComments(decl)
result.Local = true
result.Key, err = labelToKey(v.Ident, false)
if err != nil {
return nil, err
}
result.Value, err = exprToExpression(v.Expr)
return &result, err
default:
return nil, NewErrUnknownError(decl)
}
}
func defaultToExpression(comp *ast.DefaultExpr) (Expression, error) {
expr, err := exprToExpression(comp.X)
if err != nil {
return nil, err
}
return &Default{
Comments: getComments(comp),
Expr: expr,
Pos: pos(comp.Default),
}, nil
}
func interpolationToExpression(comp *ast.Interpolation) (*Interpolation, error) {
result := &Interpolation{}
for i := range comp.Elts {
switch {
case i == 0:
lit := *comp.Elts[i].(*ast.BasicLit)
lit.Value = strings.TrimSuffix(lit.Value, "\\(")
result.Parts = append(result.Parts, lit.Value)
case i == len(comp.Elts)-1:
lit := *comp.Elts[i].(*ast.BasicLit)
lit.Value = strings.TrimPrefix(lit.Value, ")")
result.Parts = append(result.Parts, lit.Value)
case i%2 == 0:
lit := *comp.Elts[i].(*ast.BasicLit)
lit.Value = strings.TrimPrefix(lit.Value, ")")
lit.Value = strings.TrimSuffix(lit.Value, "\\(")
result.Parts = append(result.Parts, lit.Value)
case i%2 == 1:
expr, err := exprToExpression(comp.Elts[i])
if err != nil {
return nil, err
}
result.Parts = append(result.Parts, expr)
}
}
return result, nil
}
func elseToExpression(c *ast.Else) (Expression, error) {
if c.If != nil {
return ifToExpression(c.If)
}
return structToExpression(c.Struct)
}
func ifToExpression(c *ast.If) (Expression, error) {
value, err := exprToExpression(c.Struct)
if err != nil {
return nil, err
}
var elseExpr Expression
if c.Else != nil {
elseExpr, err = exprToExpression(c.Else)
if err != nil {
return nil, err
}
}
condition, err := exprToExpression(c.Condition.Condition)
if err != nil {
return nil, err
}
return &If{
Pos: pos(c.If),
Comments: getComments(c),
Condition: condition,
Value: value,
Else: elseExpr,
}, nil
}
func listComprehensionToExpression(c *ast.ListComprehension) (Expression, error) {
value, err := exprToExpression(c.Value)
if err != nil {
return nil, err
}
return forClauseToFor(c.Clause, value, false)
}
func forToExpression(c *ast.For) (Expression, error) {
value, err := exprToExpression(c.Struct)
if err != nil {
return nil, err
}
e, err := forClauseToFor(c.Clause, value, true)
if err != nil {
return nil, err
}
if c.Else != nil {
e.Else, err = exprToExpression(c.Else)
if err != nil {
return nil, err
}
}
e.Merge = true
return e, nil
}
func forClauseToFor(comp *ast.ForClause, expr Expression, merge bool) (*For, error) {
var (
result = &For{
Comments: getComments(comp),
Body: expr,
Merge: merge,
Position: pos(comp.Pos()),
}
err error
)
if comp.Key != nil {
result.Key, err = value.Unquote(comp.Key.Name)
if err != nil {
return nil, value.NewErrPosition(posValue(comp.Key.Pos()), err)
}
}
result.Value, err = value.Unquote(comp.Value.Name)
if err != nil {
return nil, value.NewErrPosition(posValue(comp.Value.Pos()), err)
}
result.Collection, err = exprToExpression(comp.Source)
if err != nil {
return nil, err
}
return result, nil
}
func basicListToValue(lit *ast.BasicLit) (Expression, error) {
switch lit.Kind {
case token.NUMBER:
return Value{
Value: value.Number(lit.Value),
}, nil
case token.STRING:
s, err := value.Unquote(lit.Value)
if err != nil {
return nil, value.NewErrPosition(posValue(lit.Pos()), err)
}
return Value{
Value: value.NewValue(s),
}, nil
case token.TRUE:
return Value{
Value: value.True,
}, nil
case token.FALSE:
return Value{
Value: value.False,
}, nil
case token.NULL:
return Value{
Value: &value.Null{},
}, nil
default:
return nil, fmt.Errorf("unknown literal kind %s, value %s at %s", lit.Kind.String(), lit.Value, lit.Pos())
}
}
func schemaToExpression(s *ast.SchemaLit) (Expression, error) {
decl, err := declToField(s.Decl)
if err != nil {
return nil, err
}
if field, ok := decl.(*KeyValue); ok {
field.Value = &Schema{
Comments: field.Comments,
Expression: field.Value,
}
return &Struct{
Position: pos(s.Pos()),
Comments: getComments(s),
Fields: []Field{field},
}, nil
}
return &Schema{
Comments: getComments(s),
Expression: &Struct{
Position: pos(s.Pos()),
Comments: getComments(s),
Fields: []Field{decl},
},
}, nil
}
func structToExpression(s *ast.StructLit) (*Struct, error) {
fields, err := declsToFields(s.Elts)
if err != nil {
return nil, err
}
return &Struct{
Position: pos(s.Pos()),
Comments: getComments(s),
Fields: fields,
}, err
}
func listToExpression(list *ast.ListLit) (Expression, error) {
exprs, err := exprsToExpressions(list.Elts)
if err != nil {
return nil, err
}
return &Array{
Pos: pos(list.Lbrack),
Comments: getComments(list),
Items: exprs,
}, nil
}
func exprsToExpressions(exprs []ast.Expr) (result []Expression, _ error) {
var errs []error
for _, expr := range exprs {
newExpr, err := exprToExpression(expr)
if err != nil {
errs = append(errs, err)
continue
}
result = append(result, newExpr)
}
return result, errors.Join(errs...)
}
func unaryToExpression(bin *ast.UnaryExpr) (Expression, error) {
left, err := exprToExpression(bin.X)
if err != nil {
return nil, err
}
return &Op{
Comments: getComments(bin),
Unary: true,
Operator: value.Operator(bin.Op.String()),
Left: left,
Pos: pos(bin.OpPos),
}, nil
}
func pos(t token.Pos) value.Position {
return value.Position(t.Position())
}
func posValue(t token.Pos) value.Position {
return value.Position(t.Position())
}
func binaryToExpression(bin *ast.BinaryExpr) (Expression, error) {
left, err := exprToExpression(bin.X)
if err != nil {
return nil, err
}
right, err := exprToExpression(bin.Y)
if err != nil {
return nil, err
}
return &Op{
Comments: getComments(bin),
Operator: value.Operator(bin.Op.String()),
Left: left,
Right: right,
Pos: pos(bin.OpPos),
}, nil
}
func parensToExpression(parens *ast.ParenExpr) (Expression, error) {
expr, err := exprToExpression(parens.X)
return &Parens{
Comments: getComments(parens),
Expr: expr,
}, err
}
func identToExpression(ident *ast.Ident) (Expression, error) {
key, err := value.Unquote(ident.Name)
if err != nil {
return nil, value.NewErrPosition(posValue(ident.Pos()), err)
}
return &Lookup{
Comments: getComments(ident),
Pos: pos(ident.NamePos),
Key: key,
}, nil
}
func selectorToExpression(sel *ast.SelectorExpr) (Expression, error) {
str, key, _, err := labelToExpression(sel.Sel)
if err != nil {
return nil, err
}
if key == nil {
key = Value{
Value: value.NewValue(str),
}
}
selExpr, err := exprToExpression(sel.X)
if err != nil {
return nil, err
}
return &Selector{
Comments: getComments(sel),
Pos: pos(sel.Sel.Pos()),
Base: selExpr,
Key: key,
}, nil
}
func indexToExpression(indexExpr *ast.IndexExpr) (Expression, error) {
base, err := exprToExpression(indexExpr.X)
if err != nil {
return nil, err
}
index, err := exprToExpression(indexExpr.Index)
if err != nil {
return nil, err
}
return &Index{
Comments: getComments(indexExpr),
Pos: pos(indexExpr.Pos()),
Base: base,
Index: index,
}, nil
}
func sliceToExpression(sliceExpr *ast.SliceExpr) (Expression, error) {
base, err := exprToExpression(sliceExpr.X)
if err != nil {
return nil, err
}
low, err := exprToExpression(sliceExpr.Low)
if err != nil {
return nil, err
}
high, err := exprToExpression(sliceExpr.High)
if err != nil {
return nil, err
}
return &Slice{
Comments: getComments(sliceExpr),
Pos: pos(sliceExpr.Lbrack),
Base: base,
Start: low,
End: high,
}, nil
}
func lambdaToExpression(def *ast.Lambda) (Expression, error) {
expr, err := exprToExpression(def.Expr)
if err != nil {
return nil, err
}
lambda := &LambdaDefinition{
Comments: getComments(def),
Pos: pos(def.Lambda),
Body: expr,
}
for _, ident := range def.Idents {
s, err := value.Unquote(ident.Name)
if err != nil {
return nil, err
}
lambda.Vars = append(lambda.Vars, s)
}
return lambda, nil
}
func funcToExpression(def *ast.Func) (Expression, error) {
body, err := structToExpression(def.Body)
if err != nil {
return nil, err
}
returnType, err := exprToExpression(def.ReturnType)
if err != nil {
return nil, err
}
return &FunctionDefinition{
Comments: getComments(def),
Pos: pos(def.Func),
Body: body,
ReturnType: returnType,
}, nil
}
func callToExpression(callExpr *ast.CallExpr) (Expression, error) {
f, err := exprToExpression(callExpr.Fun)
if err != nil {
return nil, err
}
args, err := declsToFields(callExpr.Args)
if err != nil {
return nil, err
}
return &Call{
Comments: getComments(callExpr),
Pos: pos(callExpr.Lparen),
Func: f,
Args: args,
}, nil
}
func exprToExpression(expr ast.Expr) (Expression, error) {
if expr == nil {
return nil, nil
}
switch n := expr.(type) {
case *ast.BasicLit:
return basicListToValue(n)
case *ast.StructLit:
return structToExpression(n)
case *ast.SchemaLit:
return schemaToExpression(n)
case *ast.ListLit:
return listToExpression(n)
case *ast.BinaryExpr:
return binaryToExpression(n)
case *ast.UnaryExpr:
return unaryToExpression(n)
case *ast.ParenExpr:
return parensToExpression(n)
case *ast.Ident:
return identToExpression(n)
case *ast.SelectorExpr:
return selectorToExpression(n)
case *ast.IndexExpr:
return indexToExpression(n)
case *ast.SliceExpr:
return sliceToExpression(n)
case *ast.CallExpr:
return callToExpression(n)
case *ast.If:
return ifToExpression(n)
case *ast.Else:
return elseToExpression(n)
case *ast.For:
return forToExpression(n)
case *ast.ListComprehension:
return listComprehensionToExpression(n)
case *ast.Interpolation:
return interpolationToExpression(n)
case *ast.DefaultExpr:
return defaultToExpression(n)
case *ast.Func:
return funcToExpression(n)
case *ast.Lambda:
return lambdaToExpression(n)
default:
return nil, NewErrUnknownError(n)
}
}
func labelToKey(label ast.Label, match bool) (FieldKey, error) {
str, expr, stringKey, err := labelToExpression(label)
if err != nil {
return FieldKey{}, err
}
if stringKey && !match {
return FieldKey{
Match: &Value{
Value: value.NewValue(".*"),
},
Pos: pos(label.Pos()),
}, nil
} else if match {
if expr == nil {
expr = Value{
Value: value.NewValue(str),
}
}
return FieldKey{
Match: expr,
Pos: pos(label.Pos()),
}, nil
} else if expr != nil {
return FieldKey{
Interpolation: expr,
Pos: pos(label.Pos()),
}, nil
}
return FieldKey{
Key: str,
Pos: pos(label.Pos()),
}, nil
}
func labelToExpression(expr ast.Label) (s string, _ Expression, isStringIdent bool, _ error) {
if expr == nil {
return "", nil, false, nil
}
switch n := expr.(type) {
case *ast.BasicLit:
s, err := value.Unquote(n.Value)
if err != nil {
return "", nil, false, value.NewErrPosition(posValue(n.Pos()), err)
}
return s, nil, false, nil
case *ast.Ident:
s, err := value.Unquote(n.Name)
if err != nil {
return s, nil, false, value.NewErrPosition(posValue(n.Pos()), err)
}
return s, nil, n.Name == "string", nil
case *ast.Interpolation:
i, err := interpolationToExpression(n)
return "", i, false, err
default:
return "", nil, false, NewErrUnknownError(n)
}
}
func getComments(node ast.Node) (result Comments) {
for _, cg := range ast.Comments(node) {
var group []string
if cg != nil {
for _, c := range cg.List {
l := strings.TrimLeftFunc(strings.TrimPrefix(c.Text, "//"), unicode.IsSpace)
group = append(group, l)
}
}
result.Comments = append(result.Comments, group)
}
return
}
type ErrUnknownError struct {
Node ast.Node
}
func (e *ErrUnknownError) Error() string {
return fmt.Sprintf("unknown node %T encountered at %s", e.Node, e.Node.Pos())
}
func NewErrUnknownError(node ast.Node) error {
return &ErrUnknownError{
Node: node,
}
}