Skip to content

Commit a089e96

Browse files
committed
WIP language updates
1 parent e7ab963 commit a089e96

26 files changed

+1255
-1564
lines changed

ast/definitions.go

Lines changed: 307 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,307 @@
1+
package ast
2+
3+
import "github.com/wapc/widl-go/kinds"
4+
5+
type (
6+
Definition interface {
7+
Node
8+
}
9+
10+
Annotated interface {
11+
Annotation(name string, callback func(annotation *Annotation)) *Annotation
12+
}
13+
14+
AnnotatedNode struct {
15+
Annotations []*Annotation `json:"annotations"`
16+
}
17+
)
18+
19+
func (a *AnnotatedNode) Annotation(name string) *Annotation {
20+
for _, annotation := range a.Annotations {
21+
if annotation.Name.Value == name {
22+
return annotation
23+
}
24+
}
25+
return nil
26+
}
27+
28+
// NamespaceDefinition implements Node, Definition
29+
var _ Definition = (*NamespaceDefinition)(nil)
30+
31+
type NamespaceDefinition struct {
32+
BaseNode
33+
Name *Name `json:" name"`
34+
Description *StringValue `json:"description,omitempty"` // Optional
35+
AnnotatedNode
36+
}
37+
38+
func NewNamespaceDefinition(loc *Location, name *Name, description *StringValue, annotations []*Annotation) *NamespaceDefinition {
39+
return &NamespaceDefinition{
40+
BaseNode: BaseNode{kinds.NamespaceDefinition, loc},
41+
Name: name,
42+
Description: description,
43+
AnnotatedNode: AnnotatedNode{annotations},
44+
}
45+
}
46+
47+
// AliasDefinition implements Node, Definition
48+
var _ Definition = (*AliasDefinition)(nil)
49+
50+
type AliasDefinition struct {
51+
BaseNode
52+
Name *Name `json:"name"`
53+
Description *StringValue `json:"description,omitempty"` // Optional
54+
Type Type `json:"type"`
55+
AnnotatedNode
56+
}
57+
58+
func NewAliasDefinition(loc *Location, name *Name, description *StringValue, t Type, annotations []*Annotation) *AliasDefinition {
59+
return &AliasDefinition{
60+
BaseNode: BaseNode{kinds.AliasDefinition, loc},
61+
Name: name,
62+
Description: description,
63+
Type: t,
64+
AnnotatedNode: AnnotatedNode{annotations},
65+
}
66+
}
67+
68+
// ImportDefinition implements Node, Definition
69+
var _ Definition = (*ImportDefinition)(nil)
70+
71+
type ImportDefinition struct {
72+
BaseNode
73+
Description *StringValue `json:"description,omitempty"` // Optional
74+
All bool `json:"all"`
75+
Names []*ImportName `json:"names"`
76+
From *StringValue `json:"from"`
77+
AnnotatedNode
78+
}
79+
80+
func NewImportDefinition(loc *Location, description *StringValue, all bool, names []*ImportName, from *StringValue, annotations []*Annotation) *ImportDefinition {
81+
return &ImportDefinition{
82+
BaseNode: BaseNode{kinds.ImportDefinition, loc},
83+
Description: description,
84+
All: all,
85+
Names: names,
86+
From: from,
87+
AnnotatedNode: AnnotatedNode{annotations},
88+
}
89+
}
90+
91+
// TypeDefinition implements Node, Definition
92+
var _ Definition = (*TypeDefinition)(nil)
93+
94+
type TypeDefinition struct {
95+
BaseNode
96+
Name *Name `json:"name"`
97+
Description *StringValue `json:"description,omitempty"` // Optional
98+
Interfaces []*Named `json:"interfaces,omitempty"`
99+
AnnotatedNode
100+
Fields []*FieldDefinition `json:"fields"`
101+
}
102+
103+
func NewTypeDefinition(loc *Location, name *Name, description *StringValue, interfaces []*Named, annotations []*Annotation, fields []*FieldDefinition) *TypeDefinition {
104+
return &TypeDefinition{
105+
BaseNode: BaseNode{kinds.TypeDefinition, loc},
106+
Name: name,
107+
Description: description,
108+
Interfaces: interfaces,
109+
AnnotatedNode: AnnotatedNode{annotations},
110+
Fields: fields,
111+
}
112+
}
113+
114+
type ValuedDefinition struct {
115+
BaseNode
116+
Name *Name `json:"name"`
117+
Description *StringValue `json:"description,omitempty"` // Optional
118+
Type Type `json:"type"`
119+
Default Value `json:"default,omitempty"` // Optional
120+
AnnotatedNode
121+
}
122+
123+
// FieldDefinition implements Node, Definition
124+
var _ Definition = (*FieldDefinition)(nil)
125+
126+
type FieldDefinition ValuedDefinition
127+
128+
func NewFieldDefinition(loc *Location, name *Name, description *StringValue, t Type, defaultValue Value, annotations []*Annotation) *FieldDefinition {
129+
return &FieldDefinition{
130+
BaseNode: BaseNode{kinds.FieldDefinition, loc},
131+
Name: name,
132+
Description: description,
133+
Type: t,
134+
Default: defaultValue,
135+
AnnotatedNode: AnnotatedNode{annotations},
136+
}
137+
}
138+
139+
// InterfaceDefinition implements Node, Definition
140+
var _ Definition = (*InterfaceDefinition)(nil)
141+
142+
type InterfaceDefinition struct {
143+
BaseNode
144+
Description *StringValue `json:"description,omitempty"` // Optional
145+
AnnotatedNode
146+
Operations []*OperationDefinition `json:"operations"`
147+
}
148+
149+
func NewInterfaceDefinition(loc *Location, description *StringValue, annotations []*Annotation, operations []*OperationDefinition) *InterfaceDefinition {
150+
return &InterfaceDefinition{
151+
BaseNode: BaseNode{kinds.InterfaceDefinition, loc},
152+
Description: description,
153+
AnnotatedNode: AnnotatedNode{annotations},
154+
Operations: operations,
155+
}
156+
}
157+
158+
// RoleDefinition implements Node, Definition
159+
var _ Definition = (*RoleDefinition)(nil)
160+
161+
type RoleDefinition struct {
162+
BaseNode
163+
Name *Name `json:"name"`
164+
Description *StringValue `json:"description,omitempty"` // Optional
165+
AnnotatedNode
166+
Operations []*OperationDefinition `json:"operations"`
167+
}
168+
169+
func NewRoleDefinition(loc *Location, name *Name, description *StringValue, annotations []*Annotation, operations []*OperationDefinition) *RoleDefinition {
170+
return &RoleDefinition{
171+
BaseNode: BaseNode{kinds.RoleDefinition, loc},
172+
Name: name,
173+
Description: description,
174+
Operations: operations,
175+
AnnotatedNode: AnnotatedNode{annotations},
176+
}
177+
}
178+
179+
// OperationDefinition implements Node, Definition
180+
var _ Definition = (*OperationDefinition)(nil)
181+
182+
type OperationDefinition struct {
183+
BaseNode
184+
Name *Name `json:"name"`
185+
Description *StringValue `json:"description,omitempty"` // Optional
186+
Type Type `json:"type"`
187+
AnnotatedNode
188+
Unary bool `json:"unary"`
189+
Parameters []*ParameterDefinition `json:"parameters"`
190+
}
191+
192+
func NewOperationDefinition(loc *Location, name *Name, description *StringValue, ttype Type, annotations []*Annotation, unary bool, parameters []*ParameterDefinition) *OperationDefinition {
193+
return &OperationDefinition{
194+
BaseNode: BaseNode{kinds.OperationDefinition, loc},
195+
Name: name,
196+
Description: description,
197+
Type: ttype,
198+
AnnotatedNode: AnnotatedNode{annotations},
199+
Unary: unary,
200+
Parameters: parameters,
201+
}
202+
}
203+
204+
func (o *OperationDefinition) IsUnary() bool {
205+
return o.Unary && len(o.Parameters) == 1
206+
}
207+
208+
// ParameterDefinition implements Node, Definition
209+
var _ Definition = (*ParameterDefinition)(nil)
210+
211+
type ParameterDefinition ValuedDefinition
212+
213+
func NewParameterDefinition(loc *Location, name *Name, description *StringValue, t Type, defaultValue Value, annotations []*Annotation) *ParameterDefinition {
214+
return &ParameterDefinition{
215+
BaseNode: BaseNode{kinds.ParameterDefinition, loc},
216+
Name: name,
217+
Description: description,
218+
Type: t,
219+
Default: defaultValue,
220+
AnnotatedNode: AnnotatedNode{annotations},
221+
}
222+
}
223+
224+
// UnionDefinition implements Node, Definition
225+
var _ Definition = (*UnionDefinition)(nil)
226+
227+
type UnionDefinition struct {
228+
BaseNode
229+
Name *Name `json:"name"`
230+
Description *StringValue `json:"description,omitempty"` // Optional
231+
Parameters []*ParameterDefinition `json:"parameters"`
232+
AnnotatedNode
233+
Types []Type `json:"types"`
234+
}
235+
236+
func NewUnionDefinition(loc *Location, name *Name, description *StringValue, annotations []*Annotation, types []Type) *UnionDefinition {
237+
return &UnionDefinition{
238+
BaseNode: BaseNode{kinds.UnionDefinition, loc},
239+
Name: name,
240+
Description: description,
241+
AnnotatedNode: AnnotatedNode{annotations},
242+
Types: types,
243+
}
244+
}
245+
246+
// EnumDefinition implements Node, Definition
247+
var _ Definition = (*EnumDefinition)(nil)
248+
249+
type EnumDefinition struct {
250+
BaseNode
251+
Name *Name `json:"name"`
252+
Description *StringValue `json:"description,omitempty"` // Optional
253+
AnnotatedNode
254+
Values []*EnumValueDefinition `json:"values"`
255+
}
256+
257+
func NewEnumDefinition(loc *Location, name *Name, description *StringValue, annotations []*Annotation, values []*EnumValueDefinition) *EnumDefinition {
258+
return &EnumDefinition{
259+
BaseNode: BaseNode{kinds.EnumDefinition, loc},
260+
Name: name,
261+
Description: description,
262+
AnnotatedNode: AnnotatedNode{annotations},
263+
Values: values,
264+
}
265+
}
266+
267+
// EnumValueDefinition implements Node, Definition
268+
var _ Definition = (*EnumValueDefinition)(nil)
269+
270+
type EnumValueDefinition struct {
271+
BaseNode
272+
Name *Name `json:"name"`
273+
Description *StringValue `json:"description,omitempty"` // Optional
274+
AnnotatedNode
275+
}
276+
277+
func NewEnumValueDefinition(loc *Location, name *Name, description *StringValue, annotations []*Annotation) *EnumValueDefinition {
278+
return &EnumValueDefinition{
279+
BaseNode: BaseNode{kinds.EnumValueDefinition, loc},
280+
Name: name,
281+
Description: description,
282+
AnnotatedNode: AnnotatedNode{annotations},
283+
}
284+
}
285+
286+
// DirectiveDefinition implements Node, Definition
287+
var _ Definition = (*DirectiveDefinition)(nil)
288+
289+
type DirectiveDefinition struct {
290+
BaseNode
291+
Name *Name `json:"name"`
292+
Description *StringValue `json:"description,omitempty"` // Optional
293+
Parameters []*ParameterDefinition `json:"parameters"`
294+
Locations []*Name `json:"locations"`
295+
Requires []*DirectiveRequire `json:"requires,omitempty"` // Optional
296+
}
297+
298+
func NewDirectiveDefinition(loc *Location, name *Name, description *StringValue, parameters []*ParameterDefinition, locations []*Name, requires []*DirectiveRequire) *DirectiveDefinition {
299+
return &DirectiveDefinition{
300+
BaseNode: BaseNode{kinds.DirectiveDefinition, loc},
301+
Name: name,
302+
Description: description,
303+
Parameters: parameters,
304+
Locations: locations,
305+
Requires: requires,
306+
}
307+
}

ast/document.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package ast
2+
3+
import (
4+
"github.com/wapc/widl-go/kinds"
5+
)
6+
7+
// Document implements Node
8+
type Document struct {
9+
BaseNode
10+
Definitions []Node `json:"definitions"`
11+
}
12+
13+
func NewDocument(loc *Location, definitions []Node) *Document {
14+
return &Document{
15+
BaseNode: BaseNode{kinds.Document, loc},
16+
Definitions: definitions,
17+
}
18+
}

ast/location.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package ast
2+
3+
import (
4+
"github.com/wapc/widl-go/source"
5+
)
6+
7+
type Location struct {
8+
Start uint
9+
End uint
10+
Source *source.Source `json:"source,omitempty"`
11+
}
12+
13+
func NewLocation(start, end uint, source *source.Source) *Location {
14+
return &Location{
15+
Start: start,
16+
End: end,
17+
Source: source,
18+
}
19+
}

0 commit comments

Comments
 (0)