-
Notifications
You must be signed in to change notification settings - Fork 5
/
resolve.go
206 lines (177 loc) · 5.63 KB
/
resolve.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
package raml
import (
"fmt"
"github.com/antlr4-go/antlr/v4"
"github.com/acronis/go-stacktrace"
"github.com/acronis/go-raml/rdt"
)
/*
resolveShapes resolves all unresolved (UnknownShape) shapes in the RAML.
NOTE: Unresolved shapes is a linked list that is populated by the YAML parser. Shape parsing occurs in two places:
1. During the RAML fragment parsing. Shapes that could not be determined during the parsing process
are stored in `unresolvedShapes` as UnknownShape. UnknownShape includes YAML nodes that can be parsed later.
2. During the shape resolution. YAML parser is invoked on YAML nodes that UnknownShape has stored.
This helps to avoid additional traversals of nested shapes since the traverse is already done by YAML parser and it will
generate UnknownShapes and add them to `unresolvedShapes` recursively as they occur.
*/
func (r *RAML) resolveShapes() error {
var st *stacktrace.StackTrace
for r.unresolvedShapes.Len() > 0 {
v := r.unresolvedShapes.Front()
base, ok := v.Value.(*BaseShape)
if !ok {
return fmt.Errorf("invalid unresolved shape: Value is not *BaseShape: %T", v.Value)
}
if err := r.resolveShape(base); err != nil {
se := StacktraceNewWrapped("resolve shape", err, base.Location,
stacktrace.WithPosition(&base.Position),
stacktrace.WithType(StacktraceTypeResolving))
if st == nil {
st = se
} else {
st = st.Append(se)
}
}
r.unresolvedShapes.Remove(v)
}
if st != nil {
return st
}
return nil
}
// resolveDomainExtensions resolves all domain extensions in the RAML.
func (r *RAML) resolveDomainExtensions() error {
var st *stacktrace.StackTrace
for _, de := range r.domainExtensions {
if err := r.resolveDomainExtension(de); err != nil {
se := StacktraceNewWrapped("resolve domain extension", err, de.Location,
stacktrace.WithPosition(&de.Position),
stacktrace.WithType(StacktraceTypeResolving))
if st == nil {
st = se
} else {
st = st.Append(se)
}
continue
}
}
if st != nil {
return st
}
return nil
}
func (r *RAML) resolveDomainExtension(de *DomainExtension) error {
ref, err := r.GetReferencedAnnotationType(de.Name, de.Location)
if err != nil {
return fmt.Errorf("get referenced shape: %w", err)
}
de.DefinedBy = ref
return nil
}
func (r *RAML) resolveMultipleInheritance(base *BaseShape, shape *UnknownShape) (Shape, error) {
inherits := base.Inherits
for _, inherit := range inherits {
if err := r.resolveShape(inherit); err != nil {
return nil, fmt.Errorf("resolve inherit: %w", err)
}
}
// Multiple inheritance validation to be performed in a separate validation stage
s, err := r.MakeConcreteShapeYAML(base, inherits[0].Type, shape.facets)
if err != nil {
return nil, fmt.Errorf("make concrete shape: %w", err)
}
return s, nil
}
func (r *RAML) resolveLink(base *BaseShape, shape *UnknownShape) (Shape, error) {
linkShape := base.Link.Shape
if err := r.resolveShape(linkShape); err != nil {
return nil, fmt.Errorf("resolve link shape: %w", err)
}
s, err := r.MakeConcreteShapeYAML(base, linkShape.Type, shape.facets)
if err != nil {
return nil, fmt.Errorf("make concrete shape: %w", err)
}
return s, nil
}
type CustomErrorListener struct {
*antlr.DefaultErrorListener // Embed default which ensures we fit the interface
Stacktrace *stacktrace.StackTrace
position stacktrace.Position
location string
}
func (c *CustomErrorListener) SyntaxError(
_ antlr.Recognizer,
offendingSymbol interface{},
_, _ int,
msg string,
_ antlr.RecognitionException,
) {
symbolInfoOpt := stacktrace.WithInfo("offendingSymbol", offendingSymbol)
posOpt := stacktrace.WithPosition(
&stacktrace.Position{
Line: c.position.Line,
Column: c.position.Column,
},
)
if c.Stacktrace == nil {
c.Stacktrace = StacktraceNew("antlr error", c.location)
}
c.Stacktrace = c.Stacktrace.Append(StacktraceNew(msg, c.location, posOpt, symbolInfoOpt))
}
// resolveShape resolves an unknown shape in-place.
// NOTE: This function is not thread-safe. Use Clone() to create a copy of the shape before resolving if necessary.
func (r *RAML) resolveShape(base *BaseShape) error {
shape := base.Shape
if shape == nil {
return fmt.Errorf("shape is nil")
}
// Skip already resolved shapes
unknownShape, ok := shape.(*UnknownShape)
if !ok {
return nil
}
if base.Link != nil {
s, err := r.resolveLink(base, unknownShape)
if err != nil {
return StacktraceNewWrapped("resolve link", err, base.Location,
stacktrace.WithPosition(&base.Position))
}
base.SetShape(s)
return nil
}
shapeType := base.Type
if shapeType == TypeComposite {
// Special case for multiple inheritance
s, err := r.resolveMultipleInheritance(base, unknownShape)
if err != nil {
return StacktraceNewWrapped("resolve multiple inheritance", err, base.Location,
stacktrace.WithPosition(&base.Position))
}
base.SetShape(s)
return nil
}
is := antlr.NewInputStream(shapeType)
customListener := &CustomErrorListener{
location: base.Location,
position: base.Position,
}
lexer := rdt.NewrdtLexer(is)
lexer.RemoveErrorListeners()
lexer.AddErrorListener(customListener)
tokens := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
rdtParser := rdt.NewrdtParser(tokens)
rdtParser.RemoveErrorListeners()
rdtParser.AddErrorListener(customListener)
visitor := NewRdtVisitor(r)
tree := rdtParser.Entrypoint()
if customListener.Stacktrace != nil {
return customListener.Stacktrace
}
s, err := visitor.Visit(tree, unknownShape)
if err != nil {
return StacktraceNewWrapped("visit type expression", err, base.Location,
stacktrace.WithPosition(&base.Position))
}
base.SetShape(s)
return nil
}