-
Notifications
You must be signed in to change notification settings - Fork 42
/
parse.go
851 lines (774 loc) · 23.2 KB
/
parse.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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
package parse
import (
"context"
"fmt"
"path"
"sort"
"strings"
"github.com/anz-bank/sysl/pkg/env"
parser "github.com/anz-bank/sysl/pkg/grammar"
"github.com/anz-bank/sysl/pkg/importer"
"github.com/anz-bank/sysl/pkg/msg"
"github.com/anz-bank/sysl/pkg/sysl"
"github.com/anz-bank/sysl/pkg/syslutil"
"github.com/antlr/antlr4/runtime/Go/antlr"
"github.com/anz-bank/golden-retriever/reader"
"github.com/anz-bank/pkg/mod"
"github.com/imdario/mergo"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"google.golang.org/protobuf/encoding/protojson"
)
// TypeData contains referenced type and actual tuple of referenced type
type TypeData struct {
RefType *sysl.Type
Tuple *sysl.Type
}
// Parser holds assign types and let(variable) definitions in hierarchical order, messages generated while parsing
// and whether a root was found or not
type Parser struct {
AssignTypes map[string]TypeData
LetTypes map[string]TypeData
Messages map[string][]msg.Msg
allowAbsoluteImport bool
}
func NewParser() *Parser {
return &Parser{
AssignTypes: map[string]TypeData{},
LetTypes: map[string]TypeData{},
Messages: map[string][]msg.Msg{},
allowAbsoluteImport: true,
}
}
func parseString(filename string, input antlr.CharStream) (parser.ISysl_fileContext, error) {
errorListener := SyslParserErrorListener{}
lexer := parser.NewThreadSafeSyslLexer(input)
defer parser.DeleteLexerState(lexer)
stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
p := parser.NewThreadSafeSyslParser(stream)
p.GetInterpreter().SetPredictionMode(antlr.PredictionModeSLL)
p.AddErrorListener(antlr.NewDiagnosticErrorListener(true))
p.AddErrorListener(&errorListener)
p.BuildParseTrees = true
tree := p.Sysl_file()
if errorListener.hasErrors {
return nil, syslutil.Exitf(ParseError, fmt.Sprintf("%s has syntax errors\n", filename))
}
return tree, nil
}
// Currently only supports JSON format Proto.
// TODO: Add support for binary and textpb
func importSyslProto(fsinput antlr.CharStream) (*sysl.Module, error) {
file := fsinput.GetText(0, fsinput.Size())
syslFile := &sysl.Module{}
err := protojson.Unmarshal([]byte(file), syslFile)
if err != nil {
return nil, err
}
return syslFile, nil
}
func importForeign(def importDef, input antlr.CharStream) (antlr.CharStream, error) {
logger := logrus.StandardLogger()
fileName, _ := mod.ExtractVersion(def.filename)
file := input.GetText(0, input.Size())
fileType, err := detectFileType(fileName, []byte(file))
if err != nil {
return nil, err
}
switch fileType.Name {
case importer.SYSL.Name:
return input, nil
case importer.OpenAPI3.Name, importer.OpenAPI2.Name:
imp, err := importer.Factory(fileName, false, "", []byte(file), logger)
imp.WithAppName(def.appname).WithPackage(def.pkg)
if err != nil {
return nil, syslutil.Exitf(ParseError, fmt.Sprintf("%s has unknown format", fileName))
}
output, err := imp.Load(file)
if err != nil {
return nil, syslutil.Exitf(ParseError, fmt.Sprintf("%s has unknown format", fileName))
}
return antlr.NewInputStream(output), nil
default:
return nil, syslutil.Exitf(ParseError, fmt.Sprintf("%s has unknown format", fileName))
}
}
func detectFileType(fileName string, file []byte) (importer.Format, error) {
var ParserFormats = []importer.Format{
importer.OpenAPI3,
importer.OpenAPI2,
importer.SYSL,
}
return importer.GuessFileType(fileName, false, file, ParserFormats)
}
func (p *Parser) RestrictToLocalImport() {
// if root is not defined, only relative imports are allowed
p.allowAbsoluteImport = false
}
// ParseString parses a sysl definition in string form.
func (p *Parser) ParseString(content string) (*sysl.Module, error) {
fs := afero.NewMemMapFs()
file, err := fs.Create("temp.sysl")
if err != nil {
return nil, err
}
if _, err := file.Write([]byte(content)); err != nil {
return nil, err
}
return p.ParseFromFs("temp.sysl", fs)
}
// ParseFromFs parses a sysl definition from an afero filesystem
func (p *Parser) ParseFromFs(filename string, fs afero.Fs) (*sysl.Module, error) {
reader, err := NewReader(fs)
if err != nil {
return nil, err
}
return p.Parse(filename, reader)
}
// Parse parses a sysl definition from an retriever interface
func (p *Parser) Parse(resource string, reader reader.Reader) (*sysl.Module, error) { //nolint:funlen
imported := map[string]struct{}{}
listener := NewTreeShapeListener()
listener.lint()
if !strings.HasSuffix(resource, syslExt) {
resource += syslExt
}
source := importDef{filename: resource}
for {
filename := source.filename
logrus.Debugf("Parsing: " + filename)
content, hash, err := reader.ReadHash(context.Background(), filename)
if err != nil {
return nil, syslutil.Exitf(ImportError, fmt.Sprintf("error reading %#v: \n%v\n", filename, err))
}
fsinput := &fsFileStream{antlr.NewInputStream(string(content)), filename}
version := hash.String()
listener.sc = sourceCtxHelper{source.filename, version}
listener.base = path.Dir(filename)
if syslutil.IsRemoteImport(filename) {
listener.base = "/" + listener.base
}
listener.version = version
// Import Sysl Proto
if strings.HasSuffix(filename, ".sysl.pb.json") {
syslProtoImport, err := importSyslProto(fsinput)
if err != nil {
return nil, fmt.Errorf("error parsing %s: %w", filename, err)
}
if syslProtoImport != nil {
// Merge structs recursively
//nolint:govet
if err := mergo.Merge(listener.module, *syslProtoImport); err != nil {
return nil, err
}
}
break
}
input, err := importForeign(source, fsinput)
if err != nil {
return nil, err
}
tree, err := parseString(filename, input)
if err != nil {
return nil, err
}
// FIXME
localListener := NewTreeShapeListener()
localListener.sc = listener.sc
localListener.base = listener.base
localListener.version = listener.version
walker := antlr.NewParseTreeWalker()
walker.Walk(localListener, tree)
walker.Walk(listener, tree)
if len(listener.imports) == 0 {
break
}
duplicateImportCheck := func(list []importDef) {
// assume that a file will have less than 256 imports
set := make(map[string]byte)
for _, value := range list {
if count := set[value.filename]; count == 1 {
logrus.Warnf("Duplicate import: '%s' in file: '%s'\n", value.filename, source.filename)
}
set[value.filename]++
}
}
duplicateImportCheck(localListener.imports)
imported[filename] = struct{}{}
for len(listener.imports) > 0 {
source = listener.imports[0]
listener.imports = listener.imports[1:]
if _, has := imported[source.filename]; !has {
if !p.allowAbsoluteImport && strings.HasPrefix(source.filename, "/") && !syslutil.IsRemoteImport(source.filename) {
return nil, syslutil.Exitf(2,
"error importing: importing outside current directory is only allowed when root is defined")
}
break
}
}
if _, has := imported[source.filename]; has {
break
}
}
listener.lintAppDefs()
listener.lintEndpoint()
p.postProcess(listener.module)
return listener.module, nil
}
// apply attributes from src to dst statement and all of its
// child statements as well (e.g. For / Loop statements).
func applyAttributes(src *sysl.Statement, dst *sysl.Statement) bool {
var stmts []*sysl.Statement
applied := false
switch s := dst.GetStmt().(type) {
case *sysl.Statement_Cond:
stmts = s.Cond.Stmt
case *sysl.Statement_Alt:
for _, c := range s.Alt.Choice {
for _, ss := range c.Stmt {
applied = applyAttributes(src, ss) || applied
}
}
return applied
case *sysl.Statement_Group:
stmts = s.Group.Stmt
case *sysl.Statement_Loop:
stmts = s.Loop.Stmt
case *sysl.Statement_LoopN:
stmts = s.LoopN.Stmt
case *sysl.Statement_Foreach:
stmts = s.Foreach.Stmt
case *sysl.Statement_Call:
if syslutil.IsSameCall(src.GetCall(), s.Call) {
if dst.Attrs == nil {
dst.Attrs = map[string]*sysl.Attribute{}
}
mergeAttrs(src.Attrs, dst.Attrs)
applied = true
}
return applied
case *sysl.Statement_Action:
return applied
case *sysl.Statement_Ret:
return applied
default:
panic("collector: unhandled type")
}
for _, stmt := range stmts {
applied = applyAttributes(src, stmt) || applied
}
return applied
}
func checkCalls(mod *sysl.Module, appname string, epname string, dst *sysl.Statement) bool {
var stmts []*sysl.Statement
switch s := dst.GetStmt().(type) {
case *sysl.Statement_Cond:
stmts = s.Cond.Stmt
case *sysl.Statement_Alt:
for _, c := range s.Alt.Choice {
for _, ss := range c.Stmt {
if !checkCalls(mod, appname, epname, ss) {
return false
}
}
}
return true
case *sysl.Statement_Group:
stmts = s.Group.Stmt
case *sysl.Statement_Loop:
stmts = s.Loop.Stmt
case *sysl.Statement_LoopN:
stmts = s.LoopN.Stmt
case *sysl.Statement_Foreach:
stmts = s.Foreach.Stmt
case *sysl.Statement_Call:
app := syslutil.GetApp(s.Call.Target, mod)
if app == nil {
return false
}
_, valid := app.Endpoints[s.Call.Endpoint]
return valid
case *sysl.Statement_Action:
return true
case *sysl.Statement_Ret:
return true
default:
panic("collector: unhandled type")
}
for _, stmt := range stmts {
if !checkCalls(mod, appname, epname, stmt) {
return false
}
}
return true
}
func collectorPubSubCalls(appName string, app *sysl.Application) {
endpoint := app.Endpoints[`.. * <- *`]
if endpoint == nil {
return
}
for _, collectorStmt := range endpoint.Stmt {
switch x := collectorStmt.Stmt.(type) {
case *sysl.Statement_Action:
modifyEP := app.Endpoints[x.Action.Action]
if modifyEP == nil {
logrus.Errorf("App (%s) calls non-existent endpoint (%s)\n",
appName, x.Action.Action)
continue
}
if modifyEP.Attrs == nil {
modifyEP.Attrs = map[string]*sysl.Attribute{}
}
mergeAttrs(collectorStmt.Attrs, modifyEP.Attrs)
case *sysl.Statement_Call:
applied := false
for callEPName, callEndpoint := range app.Endpoints {
if callEPName == `.. * <- *` {
continue
}
for _, callStmt := range callEndpoint.Stmt {
applied = applyAttributes(collectorStmt, callStmt) || applied
}
}
if !applied {
logrus.Errorf("Unused template (%s <- %s) in app %s\n",
x.Call.Target.Part, x.Call.Endpoint, appName)
}
default:
panic("unhandled type:")
}
}
}
func checkEndpointCalls(mod *sysl.Module) bool {
valid := false
for appName, app := range mod.Apps {
for epname, ep := range app.Endpoints {
for _, stmt := range ep.Stmt {
valid = checkCalls(mod, appName, epname, stmt)
if !valid {
return valid
}
}
}
}
return valid
}
func inferAnonymousType(mod *sysl.Module, appName string, anonCount int, t *sysl.Expr_Transform_, expr *sysl.Expr) int {
// logrus.Printf("found anonymous type\n")
newType := &sysl.Type{
Type: &sysl.Type_Tuple_{
Tuple: &sysl.Type_Tuple{
AttrDefs: map[string]*sysl.Type{},
},
},
}
typeName := fmt.Sprintf("AnonType_%d__", anonCount)
anonCount++
if mod.Apps[appName].Types == nil {
mod.Apps[appName].Types = map[string]*sysl.Type{}
}
mod.Apps[appName].Types[typeName] = newType
attr := newType.GetTuple().AttrDefs
for _, stmt := range t.Transform.Stmt {
if stmt.GetAssign() != nil {
assign := stmt.GetAssign()
aexpr := assign.Expr
syslutil.Assert(aexpr.GetTransform() != nil, "expression should be of type transform")
ftype := aexpr.Type
setof := ftype.GetSet() != nil
if setof {
ftype = ftype.GetSet()
}
syslutil.Assert(ftype.GetTypeRef() != nil, "transform type should be type_ref")
t1 := &sysl.Type{
Type: &sysl.Type_TypeRef{
TypeRef: &sysl.ScopedRef{
Context: &sysl.Scope{
Appname: mod.Apps[appName].Name,
Path: []string{typeName},
},
Ref: ftype.GetTypeRef().Ref,
},
},
}
if setof {
t1 = &sysl.Type{
Type: &sysl.Type_Set{
Set: t1,
},
}
}
attr[assign.Name] = t1
}
}
expr.Type = &sysl.Type{
Type: &sysl.Type_Set{
Set: &sysl.Type{
Type: &sysl.Type_TypeRef{
TypeRef: &sysl.ScopedRef{
Context: &sysl.Scope{
Appname: mod.Apps[appName].Name,
},
Ref: &sysl.Scope{
Appname: mod.Apps[appName].Name,
Path: []string{typeName},
},
},
},
},
},
}
return anonCount
}
// for nested transform's Type
func (p *Parser) inferExprType(mod *sysl.Module,
appName string,
expr *sysl.Expr, top bool,
anonCount int,
viewName string,
scope string,
refType *sysl.Type) (exprType *sysl.Type, anonymousCount int, inferredType *sysl.Type) {
switch t := expr.Expr.(type) {
case *sysl.Expr_Transform_:
newTuple := &sysl.Type{
Type: &sysl.Type_Tuple_{
Tuple: &sysl.Type_Tuple{
AttrDefs: map[string]*sysl.Type{},
},
},
}
attrDefs := newTuple.GetTuple().AttrDefs
for _, stmt := range t.Transform.Stmt {
switch s := stmt.Stmt.(type) {
case *sysl.Expr_Transform_Stmt_Assign_:
varName := s.Assign.GetName()
if _, exists := attrDefs[varName]; exists {
p.Messages[viewName] = append(p.Messages[viewName], *msg.NewMsg(msg.ErrRedefined, []string{scope,
varName}))
} else {
var inferredType *sysl.Type
_, anonCount, inferredType = p.inferExprType(mod, appName, s.Assign.Expr, false, anonCount,
viewName, scope+":"+varName, refType)
attrDefs[varName] = inferredType
p.AssignTypes[viewName] = TypeData{RefType: refType, Tuple: newTuple}
}
case *sysl.Expr_Transform_Stmt_Let:
varName := s.Let.GetName()
if _, exists := p.LetTypes[scope+":"+varName]; exists {
p.Messages[viewName] = append(p.Messages[viewName], *msg.NewMsg(msg.ErrRedefined, []string{scope, varName}))
} else {
var inferredType *sysl.Type
_, anonCount, inferredType = p.inferExprType(mod, appName, s.Let.Expr, false, anonCount,
viewName, scope+":"+varName, s.Let.GetExpr().GetType())
p.LetTypes[scope+":"+varName] = TypeData{RefType: s.Let.GetExpr().GetType(), Tuple: inferredType}
}
}
}
if !top && expr.Type == nil {
anonCount = inferAnonymousType(mod, appName, anonCount, t, expr)
}
return expr.Type, anonCount, newTuple
case *sysl.Expr_Relexpr:
if t.Relexpr.Op == sysl.Expr_RelExpr_RANK {
if !top && expr.Type == nil {
type1, c, _ := p.inferExprType(mod, appName, t.Relexpr.Target, true, anonCount, viewName, scope, refType)
anonCount = c
logrus.Printf(type1.String())
}
}
case *sysl.Expr_Literal:
expr.Type = valueTypeToSysl(t.Literal)
case *sysl.Expr_List_:
expr.Type, _, _ = p.inferExprType(mod, appName, t.List.Expr[0], true, anonCount, viewName, scope, refType)
case *sysl.Expr_Ifelse:
exprTypeIfTrue, _, _ := p.inferExprType(mod, appName, t.Ifelse.GetIfTrue(), true, anonCount,
viewName, scope, refType)
expr.Type = exprTypeIfTrue
if t.Ifelse.GetIfFalse() != nil {
exprTypeIfFalse, _, _ := p.inferExprType(mod, appName, t.Ifelse.GetIfFalse(), true, anonCount,
viewName, scope, refType)
// TODO if exprTypeIfTrue != exprTypeIfFalse, raise an error. Then remove following 3 lines
if exprTypeIfFalse != nil {
expr.Type = exprTypeIfFalse
}
}
case *sysl.Expr_Binexpr:
exprTypeLHS, _, _ := p.inferExprType(mod, appName, t.Binexpr.GetLhs(), true, anonCount, viewName, scope, refType)
expr.Type = exprTypeLHS
exprTypeRHS, _, _ := p.inferExprType(mod, appName, t.Binexpr.GetRhs(), true, anonCount, viewName, scope, refType)
// TODO if exprTypeRHS != exprTypeLHS, raise an error. Then remove following 3 lines
if exprTypeRHS != nil {
expr.Type = exprTypeRHS
}
case *sysl.Expr_Unexpr:
varType, _, _ := p.inferExprType(mod, appName, expr.GetUnexpr().GetArg(), true, anonCount, viewName, scope, refType)
switch t.Unexpr.GetOp() {
case sysl.Expr_UnExpr_NOT, sysl.Expr_UnExpr_INV:
if !syslutil.HasSameType(varType, syslutil.TypeBool()) {
_, typeDetail := syslutil.GetTypeDetail(varType)
p.Messages[viewName] = append(p.Messages[viewName],
*msg.NewMsg(msg.ErrInvalidUnary, []string{viewName, typeDetail}))
}
expr.Type = syslutil.TypeBool()
case sysl.Expr_UnExpr_NEG, sysl.Expr_UnExpr_POS:
if !syslutil.HasSameType(varType, syslutil.TypeInt()) {
_, typeDetail := syslutil.GetTypeDetail(varType)
p.Messages[viewName] = append(p.Messages[viewName],
*msg.NewMsg(msg.ErrInvalidUnary, []string{viewName, typeDetail}))
}
expr.Type = syslutil.TypeInt()
default:
expr.Type = syslutil.TypeNone()
}
default:
// TODO Handle expression
logrus.Debug("[Parse.infer_expr_type] Unhandled type", t)
return syslutil.TypeNone(), anonCount, syslutil.TypeNone()
}
return expr.Type, anonCount, expr.Type
}
func (p *Parser) inferTypes(mod *sysl.Module, appName string) {
for viewName, view := range mod.Apps[appName].Views {
if syslutil.HasPattern(view.Attrs, "abstract") {
continue
}
if view.Expr.GetTransform() == nil {
logrus.Warnf("view %s expression should be of type transform", viewName)
continue
}
p.inferExprType(mod, appName, view.Expr, true, 0, viewName, viewName, view.GetRetType())
}
}
// fixTypeRefScope fixes the local and full type references so that it points to the correct type.
func fixTypeRefScope(mod *sysl.Module, currApp string, ref *sysl.Scope) {
if ref == nil {
return
}
appPath := ref.GetAppname().GetPart()
if len(appPath) > 1 {
// multiple app parts means full type specification
return
}
// type ref must have at least app and type
typePath := ref.GetPath()
if len(typePath) == 0 {
// impossible
return
}
if len(appPath) == 0 && len(typePath) == 1 {
// local type ref
return
}
appName, typeName := appPath[0], typePath[0]
if currApp == appName {
// same app
return
}
if app, exists := mod.Apps[appName]; exists {
if _, exists := app.Types[typeName]; exists {
// full type ref
return
}
}
if app, exists := mod.Apps[currApp]; exists {
if _, exists := app.Types[appName]; exists {
// local type ref using deep ref e.g A.B.C.D
ref.Appname = nil
ref.Path = append([]string{appName}, typePath...)
return
}
}
// TODO: type not found, return an error?
}
func fixParamTypeRef(mod *sysl.Module, app *sysl.Application, appName string) {
for _, ep := range app.GetEndpoints() {
for _, param := range ep.GetParam() {
t := param.GetType()
if ref := t.GetTypeRef(); ref != nil {
fixTypeRefScope(mod, appName, ref.GetRef())
}
}
}
}
func (p *Parser) postProcess(mod *sysl.Module) {
appNames := make([]string, 0, len(mod.Apps))
for a := range mod.Apps {
appNames = append(appNames, a)
}
sort.Strings(appNames)
for _, appName := range appNames {
app := mod.Apps[appName]
fixParamTypeRef(mod, app, appName)
if app.Mixin2 != nil {
for _, src := range app.Mixin2 {
srcApp := syslutil.GetApp(src.Name, mod)
if !syslutil.HasPattern(srcApp.Attrs, "abstract") {
logrus.Warnf("mixin App (%s) should be ~abstract", syslutil.GetAppName(src.Name))
continue
}
if srcApp.Types != nil && app.Types == nil {
app.Types = map[string]*sysl.Type{}
}
if srcApp.Views != nil && app.Views == nil {
app.Views = map[string]*sysl.View{}
}
for k, v := range srcApp.Types {
if _, has := app.Types[k]; !has {
app.Types[k] = v
} else {
logrus.Warnf("Type %s defined in %s and in %s",
k, appName, syslutil.GetAppName(src.Name))
}
}
for k, v := range srcApp.Views {
if _, has := app.Views[k]; !has {
app.Views[k] = v
} else {
logrus.Warnf("View %s defined in %s and in %s",
k, appName, syslutil.GetAppName(src.Name))
}
}
}
}
for typeName, types := range app.Types {
var attrs map[string]*sysl.Type
switch x := types.Type.(type) {
case *sysl.Type_Tuple_:
if len(x.Tuple.AttrDefs) == 0 {
types.Type = nil
continue
}
attrs = x.Tuple.GetAttrDefs()
case *sysl.Type_Relation_:
if len(x.Relation.AttrDefs) == 0 {
types.Type = nil
continue
}
attrs = x.Relation.GetAttrDefs()
}
for fieldname, t := range attrs {
if x := t.GetTypeRef(); x != nil {
refApp := app
refName := x.GetRef().GetPath()[0]
if refName == "string_8" {
continue
}
fixTypeRefScope(mod, appName, x.GetRef())
if refType, has := refApp.Types[refName]; has {
var refAttrs map[string]*sysl.Type
switch refType.Type.(type) {
case *sysl.Type_Tuple_:
refType, _ := refApp.Types[refName].Type.(*sysl.Type_Tuple_)
refAttrs = refType.Tuple.GetAttrDefs()
case *sysl.Type_Relation_:
refType, _ := refApp.Types[refName].Type.(*sysl.Type_Relation_)
refAttrs = refType.Relation.GetAttrDefs()
}
var field string
var has bool
if len(x.GetRef().GetPath()) > 1 {
last := len(x.GetRef().GetPath()) - 1
field = x.GetRef().GetPath()[last]
_, has = refAttrs[field]
} else if len(x.GetRef().GetPath()) == 1 {
field = x.GetRef().GetPath()[0]
_, has = refApp.Types[field]
}
if !has {
logrus.Infof("Field %#v (type %#v) refers to Field %#v (type %#v) in app %#v",
fieldname, typeName, field, refName, appName)
}
} else {
logrus.Infof("Field %#v (type %#v) refers to type %s in app %#v",
fieldname, typeName, refName, appName)
}
}
}
}
p.inferTypes(mod, appName)
collectorPubSubCalls(appName, app)
renestTypes(app)
}
checkEndpointCalls(mod)
}
func renestTypes(app *sysl.Application) {
mode := env.SYSL_DEV_RENEST_FLATTENED_TYPES.Value()
if mode == "off" {
return
}
typeNames := make([]string, 0, len(app.Types))
for typeName := range app.Types {
typeNames = append(typeNames, typeName)
}
sort.Strings(typeNames)
for _, typeName := range typeNames {
path := strings.Split(typeName, ".")
if len(path) > 1 && injectType(app.Types[typeName], app.Types, path) {
if mode == "move" {
delete(app.Types, typeName)
}
}
}
}
func injectType(leaf *sysl.Type, attrs map[string]*sysl.Type, path []string) bool {
if attrs == nil {
return false
}
if len(path) > 1 {
return injectType(leaf, subTuple(attrs, path[0]), path[1:])
}
attrs[path[0]] = leaf
return true
}
func subTuple(attrs map[string]*sysl.Type, elem string) map[string]*sysl.Type {
if attr, has := attrs[elem]; has {
if tuple, is := attr.Type.(*sysl.Type_Tuple_); is {
return tuple.Tuple.AttrDefs
}
}
return nil
}
func valueTypeToSysl(value *sysl.Value) *sysl.Type {
switch value.Value.(type) {
case *sysl.Value_B:
return syslutil.TypeBool()
case *sysl.Value_I:
return syslutil.TypeInt()
case *sysl.Value_D:
return syslutil.TypeFloat()
case *sysl.Value_S:
return syslutil.TypeString()
case *sysl.Value_Decimal:
return syslutil.TypeDecimal()
case *sysl.Value_Null_:
return syslutil.TypeEmpty()
default:
panic(errors.Errorf("valueTypeToSysl: unhandled type: %v", value))
}
}
// return the one and only app defined in the module
func getDefaultAppName(mod *sysl.Module) string {
for app := range mod.Apps {
return app
}
return ""
}
func LoadAndGetDefaultApp(model string, fs afero.Fs, p *Parser) (*sysl.Module, string, error) {
// Model we want to generate code for
mod, err := p.ParseFromFs(model, fs)
if err != nil {
return nil, "", err
}
modelAppName := getDefaultAppName(mod)
return mod, modelAppName, nil
}
func (p *Parser) GetAssigns() map[string]TypeData {
return p.AssignTypes
}
func (p *Parser) GetLets() map[string]TypeData {
return p.LetTypes
}
func (p *Parser) GetMessages() map[string][]msg.Msg {
return p.Messages
}