-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrdt_visitor.go
180 lines (161 loc) · 6.08 KB
/
rdt_visitor.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
package raml
import (
"fmt"
"github.com/antlr4-go/antlr/v4"
"github.com/acronis/go-raml/rdt"
)
// RdtVisitor defines a struct that implements the visitor
type RdtVisitor struct {
rdt.BaserdtParserVisitor // Embedding the base visitor class
raml *RAML
}
func NewRdtVisitor(rml *RAML) *RdtVisitor {
return &RdtVisitor{raml: rml}
}
func (visitor *RdtVisitor) Visit(tree antlr.ParseTree, target *UnknownShape) (Shape, error) {
// Target is required to isolate anonymous shapes created by Union, Optional and Array syntax.
// This is done to avoid sharing base shape properties between the original type and implicitly created type.
switch t := tree.(type) {
case *rdt.EntrypointContext:
return visitor.VisitEntrypoint(t, target)
case *rdt.ExpressionContext:
return visitor.VisitExpression(t, target)
case *rdt.TypeContext:
return visitor.VisitType(t, target)
case *rdt.PrimitiveContext:
return visitor.VisitPrimitive(t, target)
case *rdt.OptionalContext:
return visitor.VisitOptional(t, target)
case *rdt.ArrayContext:
return visitor.VisitArray(t, target)
case *rdt.UnionContext:
return visitor.VisitUnion(t, target)
case *rdt.GroupContext:
return visitor.VisitGroup(t, target)
case *rdt.ReferenceContext:
return visitor.VisitReference(t, target)
}
return nil, fmt.Errorf("unknown node type %T", tree)
}
func (visitor *RdtVisitor) VisitUnionMembers(node antlr.RuleNode, target *UnknownShape) ([]*BaseShape, error) {
var shapes []*BaseShape
children := node.GetChildren()
// Each union member is paired with a pipe separator so we increment by 2.
// type1 | type2 | type3
// ^ ^ ^ ^ ^
// 0 1 2 3 4
for i := 0; i < len(children); i += 2 {
baseResolved, implicitAnonShape, _ := visitor.raml.MakeNewShape("", "", target.Location, &target.Position)
s, err := visitor.Visit(children[i].(antlr.ParseTree), implicitAnonShape.(*UnknownShape))
if err != nil {
return nil, fmt.Errorf("visit children: %w", err)
}
baseResolved.SetShape(s)
shapes = append(shapes, baseResolved)
}
return shapes, nil
}
func (visitor *RdtVisitor) VisitEntrypoint(ctx *rdt.EntrypointContext, target *UnknownShape) (Shape, error) {
return visitor.Visit(ctx.GetChildren()[0].(antlr.ParseTree), target)
}
func (visitor *RdtVisitor) VisitExpression(ctx *rdt.ExpressionContext, target *UnknownShape) (Shape, error) {
return visitor.Visit(ctx.GetChildren()[0].(antlr.ParseTree), target)
}
func (visitor *RdtVisitor) VisitType(ctx *rdt.TypeContext, target *UnknownShape) (Shape, error) {
return visitor.Visit(ctx.GetChildren()[0].(antlr.ParseTree), target)
}
func (visitor *RdtVisitor) VisitPrimitive(ctx *rdt.PrimitiveContext, target *UnknownShape) (Shape, error) {
s, err := visitor.raml.MakeConcreteShapeYAML(target.Base(), ctx.GetText(), nil)
if err != nil {
return nil, fmt.Errorf("make concrete shape: %w", err)
}
return s, nil
}
func (visitor *RdtVisitor) VisitOptional(ctx *rdt.OptionalContext, target *UnknownShape) (Shape, error) {
// Passed target shape becomes anonymous here because union shape takes its place later.
baseResolved, anonResolvedShape, _ := visitor.raml.MakeNewShape("", "", target.Location, &target.Position)
s, err := visitor.Visit(ctx.GetChildren()[0].(antlr.ParseTree), anonResolvedShape.(*UnknownShape))
if err != nil {
return nil, fmt.Errorf("visit: %w", err)
}
// Replace with resolved shape
baseResolved.SetShape(s)
// Nil shape is also anonymous here and doesn't share the base shape with the target.
baseNil, _, _ := visitor.raml.MakeNewShape("", TypeNil, target.Location, &target.Position)
// We transfer base to new shape
// TODO: Need some kind of conversion interface.
base := target.Base()
base.Type = TypeUnion
return &UnionShape{
BaseShape: base,
UnionFacets: UnionFacets{
AnyOf: []*BaseShape{baseResolved, baseNil},
},
}, nil
}
func (visitor *RdtVisitor) VisitArray(ctx *rdt.ArrayContext, target *UnknownShape) (Shape, error) {
// Passed target shape becomes anonymous here because union shape takes its place later.
baseResolved, anonResolvedShape, _ := visitor.raml.MakeNewShape("", "", target.Location, &target.Position)
s, err := visitor.Visit(ctx.GetChildren()[0].(antlr.ParseTree), anonResolvedShape.(*UnknownShape))
if err != nil {
return nil, fmt.Errorf("visit: %w", err)
}
// Replace with resolved shape
baseResolved.SetShape(s)
// We transfer base to new shape
// TODO: Need some kind of conversion interface.
base := target.Base()
base.Type = TypeArray
return &ArrayShape{
BaseShape: base,
ArrayFacets: ArrayFacets{
Items: baseResolved,
},
}, nil
}
func (visitor *RdtVisitor) VisitUnion(ctx *rdt.UnionContext, target *UnknownShape) (Shape, error) {
ss, err := visitor.VisitUnionMembers(ctx, target)
if err != nil {
return nil, fmt.Errorf("visit children: %w", err)
}
base := target.Base()
base.Type = TypeUnion
return &UnionShape{
BaseShape: base,
UnionFacets: UnionFacets{
AnyOf: ss,
},
}, nil
}
func (visitor *RdtVisitor) VisitGroup(ctx *rdt.GroupContext, target *UnknownShape) (Shape, error) {
// First and last nodes are terminal nodes
// ( expression )
// ^ ^ ^
// 0 1 2
return visitor.Visit(ctx.GetChildren()[1].(antlr.ParseTree), target)
}
func (visitor *RdtVisitor) VisitReference(ctx *rdt.ReferenceContext, target *UnknownShape) (Shape, error) {
shapeType := ctx.GetText()
ref, err := visitor.raml.GetReferencedType(shapeType, target.Location)
if err != nil {
return nil, fmt.Errorf("get referenced shape: %w", err)
}
if ref.ID == target.ID {
return nil, fmt.Errorf("self recursion %s", shapeType)
}
if errResolveShape := visitor.raml.resolveShape(ref); errResolveShape != nil {
return nil, fmt.Errorf("resolve: %w", errResolveShape)
}
s, err := visitor.raml.MakeConcreteShapeYAML(target.Base(), ref.Type, target.facets)
if err != nil {
return nil, fmt.Errorf("make concrete shape: %w", err)
}
// If target.facets is nil (makeNewShapeYAML returned nil instead of empty array) then reference is an alias.
s.Base().TypeLabel = shapeType
if target.facets == nil {
s.Base().Alias = ref
} else {
s.Base().Inherits = append(s.Base().Inherits, ref)
}
return s, nil
}