-
Notifications
You must be signed in to change notification settings - Fork 29
/
wit.pegjs
483 lines (396 loc) · 11.9 KB
/
wit.pegjs
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
{{
const ast = require('./wit-ast');
const Node = ast.Node;
function range(loc) {
return {
start: loc.start,
end: loc.end
}
}
}}
start = wit_document
wit_document
= pack:package_item? members:(world_item / interface_item)* c1:_ {
return Node.finalize(ast.Document.create(range(location()), pack, members), c1);
}
package_item "package item"
= c1:_ 'package' c2:_ id:package_id c3:__ {
return Node.finalize(ast.PackageItem.create(range(location()), id), c1, c2, c3);
}
package_id "package id"
= namespace:(namespace:id_item ':' { return namespace; })? name:id_item version:('@' version:version_number { return version; })? c1:__ {
return Node.finalize(ast.PackageIdentifier.create(range(location()), namespace, name, version), c1);
}
version_number "version number"
= [^ \t\n\r]+ {
return Node.finalize(ast.VersionNumber.create(range(location()), text()));
}
world_item "world declaration"
= c1:_ 'world' c2:_ name:id c3:_ '{' members:world_items c4:_ '}' c5:__ {
return Node.finalize(ast.WorldItem.create(range(location()), name, members), c1, c2, c3, c4, c5);
}
world_items
= world_members|.., lineTerminatorSequence|
world_members
= export_item
/ import_item
/ use_item
/ typedef_item
export_item
= c1:_ 'export' c2:_ name:id c3:_ ':' type:extern_type c4:__ {
return Node.finalize(ast.ExportItem.create(range(location()), name, type), c1, c2, c3, c4);
}
import_item
= c1:_ 'import' c2:_ name:id c3:__ {
return Node.finalize(ast.ImportItem.create(range(location()), name), c1, c2, c3);
}
extern_type
= func_type
/ interface_type
interface_item "interface declaration"
= c1:_ 'interface' c2:_ name:id c3:_ '{' members:interface_items c4:_ '}' c5:__ {
return Node.finalize(ast.InterfaceItem.create(range(location()), name, members), c1, c2, c3, c4, c5);
}
interface_type "interface type"
= c1:_ 'interface' c2:_ '{' members:interface_items c3:_ '}' c4:__ {
return Node.finalize(ast.InterfaceType.create(range(location()), members), c1, c2, c3, c4);
}
interface_items
= interface_member|.., lineTerminatorSequence|
interface_member
= typedef_item
/ use_item
/ func_item
typedef_item
= variant_items
/ record_item
/ union_items
/ flags_items
/ enum_items
/ type_item
variant_items "variant"
= c1:_ 'variant' c2:_ name:id c3:_ '{' members:variant_cases c4:_ '}' c5:__ {
return Node.finalize(ast.VariantItem.create(range(location()), name, members), c1, c2, c3, c4, c5);
}
variant_cases "variant cases"
= items:variant_case|0.., ','| sep:','? {
return items;
}
variant_case
= c1:_ name:id c2:_ '(' type:ty_item ')' c3:__ {
return Node.finalize(ast.VariantCase.create(range(location()), name, type), c1, c2, c3);
}
/ c1:_ name:id c2:__ {
return Node.finalize(ast.VariantCase.create(range(location()), name), c1, c2);
}
record_item "record"
= c1:_ 'record' c2:_ name:id c3:_ '{' members:record_fields c4:_ '}' c5:__ {
return Node.finalize(ast.RecordItem.create(range(location()), name, members), c1, c2, c3, c4, c5);
}
record_fields
= items:record_field|0.., ','| sep:','? {
return items;
}
record_field
= name:id_item ':' type:ty_item_ {
return Node.finalize(ast.RecordField.create(range(location()), name, type));
}
union_items "union"
= c1:_ 'union' c2:_ name:id c3:_ '{' members:union_cases c4:_ '}' c5:__ {
return Node.finalize(ast.UnionItem.create(range(location()), name, members), c1, c2, c3, c4, c5);
}
union_cases "union cases"
= items:union_case|0.., ','| sep:','? {
return items;
}
union_case "union case"
= type:ty_item {
return type;
}
flags_items "flags"
= c1:_ 'flags' c2:_ name:id c3:_ '{' members:flags_fields c4:_ '}' c5:__ {
return Node.finalize(ast.FlagsItem.create(range(location()), name, members), c1, c2, c3, c4, c5);
}
flags_fields "flag fields"
= items:flags_field|0.., ','| sep:','? {
return items;
}
flags_field "flag field"
= name:id_item {
return name;
}
enum_items "enums"
= c1:_ 'enum' c2:_ name:id c3:_ '{' members:enum_cases c4:_ '}' c5:__ {
return Node.finalize(ast.EnumItem.create(range(location()), name, members), c1, c2, c3, c4, c5);
}
enum_cases "enum cases"
= items:enum_case|0.., ','| sep:','? {
return items;
}
enum_case "enum case"
= name:id_item {
return name;
}
type_item "type"
= c1:_ 'type' c2:_ name:id c3:_ '=' c4:_ type:ty c5:__ {
return Node.finalize(ast.TypeItem.create(range(location()), name, type), c1, c2, c3, c4, c5);
}
use_item "use"
= c1:_ 'use' c2:_ pack:package_id c3:_ '/' c4:_ importItem:renamed_item c5:__ {
return Node.finalize(ast.UseItem.create(range(location()), pack, importItem), c1, c2, c3, c4, c5);
}
/ c1:_ 'use' c2:_ pack:package_id c3:_ '/' c4:_ namedImports:named_imports c5:__ {
return Node.finalize(ast.UseItem.create(range(location()), pack, namedImports), c1, c2, c3, c4, c5);
}
/ c1:_ 'use' c2:_ pack:package_id c3:_ '/' c4:_ item:id c5:__ {
return Node.finalize(ast.UseItem.create(range(location()), pack, item), c1, c2, c3, c4, c5);
}
/ c1:_ 'use' namedImports:named_imports c2:__ {
return Node.finalize(ast.UseItem.create(range(location()), undefined, namedImports), c1, c2);
}
/ c1:_ 'use' item:id_item_ c2:__ {
return Node.finalize(ast.UseItem.create(range(location()), undefined, item), c1, c2);
}
renamed_item "renamed identifier"
= from:id c1:_ 'as' c2:_ to:id {
return Node.finalize(ast.RenameItem.create(range(location()), from, to), c1, c2);
}
named_imports "named imports"
= item:id_item '.' c1:_ '{' members:use_names_list c2:_ '}' {
return Node.finalize(ast.NamedImports.create(range(location()), item, members), c1, c2);
}
use_names_list "use names"
= items:use_names_item|1..,','| sep:','? {
return items;
}
use_names_item
= from:id_item 'as' to:id_item {
return Node.finalize(ast.RenameItem.create(range(location()), from, to));
}
/ id_item
func_item "function"
= name:id_item ':' signature:func_type {
return Node.finalize(ast.FuncItem.create(range(location()), name, signature));
}
func_type
= c1:_ 'func' c2:_ params:param_list c3:_ result:result_list c4:__ {
return Node.finalize(ast.FuncType.create(range(location()), params, result), c1, c2, c3, c4);
}
/ c1:_ 'func' c2:_ params:param_list c3:__ {
return Node.finalize(ast.FuncType.create(range(location()), params), c1, c2, c3);
}
param_list
= '(' members:named_type_list c1:_ ')' {
return Node.finalize(ast.FuncParams.create(range(location()), members), c1);
}
result_list
= '->' c1:_ '(' members:named_type_list c2:_ ')' {
return Node.finalize(ast.NamedFuncResult.create(range(location()), members), c1, c2);
}
/ '->' c1:_ type:ty {
return Node.finalize(ast.FuncResult.create(range(location()), type), c1);
}
named_type_list
= items:named_type|0..,','| sep:','? {
return items;
}
named_type
= name:id_item ':' type:ty_item {
return Node.finalize(ast.NamedType.create(range(location()), name, type));
}
ty "built in types"
= baseTypes
/ tuple
/ list
/ option
/ result
/ handle
/ id
ty_item "build in type with comment"
= c1:_ type:ty c2:_ {
const result = Node.attachComments(type, c1, c2);
result.range = range(location());
return result;
}
ty_item_ "build in type with comment"
= c1:_ type:ty c2:__ {
const result = Node.attachComments(type, c1, c2);
result.range = range(location());
return result;
}
tuple "tuple type"
= 'tuple' c1:_ '<' members:tupleList '>' {
return Node.finalize(ast.Tuple.create(range(location()), members), c1);
}
tupleList "tuple element list"
= ty_item|.., ','|
/ ty_item
list "element list"
= 'list' c1:_ '<' type:ty_item '>' {
return Node.finalize(ast.List.create(range(location()), type), c1);
}
option "option type"
= 'option' c1:_ '<' type:ty_item '>' {
return Node.finalize(ast.Option.create(range(location()), type), c1);
}
result "result type"
= 'result' c1:_ '<' result:ty_item ',' error:ty_item '>' {
return Node.finalize(ast.ResultType.create(range(location()), result, error), c1);
}
/ 'result' c1:_ '<' result:no_result ',' error:ty_item '>' {
return Node.finalize(ast.ResultType.create(range(location()), result, error), c1);
}
/ 'result' c1:_ '<' error:ty_item '>' {
return Node.finalize(ast.ResultType.create(range(location()), undefined, error), c1);
}
/ 'result' {
return Node.finalize(ast.ResultType.create(range(location()), undefined, undefined), c1);
}
no_result "no result"
= c1:_ '_' c2:_ {
return Node.finalize(ast.NoResultType.create(range(location())), c1, c2);
}
handle "handle type"
= id
/ 'borrow' c1:_ '<' name:id_item '>' {
return Node.finalize(ast.Borrow.create(range(location()), name), c1);
}
id_item
= c1:_ id:id c2:_ {
return Node.finalize(ast.Identifier.create(range(location()), id.value), c1, c2);
}
id_item_
= c1:_ id:id c2:__ {
return Node.finalize(ast.Identifier.create(range(location()), id.value), c1, c2);
}
id "id"
= name:name {
return ast.Identifier.create(range(location()), name);
}
reservedWord "reserved words"
= 'package'
/ 'world'
/ 'import'
/ 'export'
/ 'interface'
/ 'use'
/ 'func'
/ 'variant'
/ 'record'
/ 'union'
/ 'flags'
/ 'enum'
/ 'type'
/ 'tuple'
/ 'list'
/ 'option'
/ 'result'
/ 'borrow'
/ baseTypes
name "name"
= '%'label {
return text();
}
/ !(reservedWord (' ' / comment / lineTerminator)) label {
return text();
}
label "label"
= word|1..,'-'| {
return text();
}
word "word"
= ([a-z]i)([0-9a-z]i)* {
return text();
}
baseTypes "base types"
= u8 / u16 / u32 / u64
/ s8 / s16 / s32 / s64
/ float32 / float64
/ bool
/ char
/ string
u8 "u8"
= 'u8' {
return ast.u8.create(range(location()));
}
u16 "u16"
= 'u16' {
return ast.u16.create(range(location()));
}
u32 "u32"
= 'u32' {
return ast.u32.create(range(location()));
}
u64 "u64"
= 'u64' {
return ast.u64.create(range(location()));
}
s8 "s8"
= 's8' {
return ast.s8.create(range(location()));
}
s16 "s16"
= 's16' {
return ast.s16.create(range(location()));
}
s32 "s32"
= 's32' {
return ast.s32.create(range(location()));
}
s64 "s64"
= 's64' {
return ast.s64.create(range(location()));
}
float32 "float32"
= 'float32' {
return ast.float32.create(range(location()));
}
float64 "float64"
= 'float64' {
return ast.float64.create(range(location()));
}
bool "bool"
= 'bool' {
return ast.bool.create(range(location()));
}
char "char"
= 'char' {
return ast.char.create(range(location()));
}
string "string"
= 'string' {
return ast.string_.create(range(location()));
}
__ "whitespace end of line"
= space:[ \t]* comment:(singleLineComment / multiLineCommentOneLine+)? {
return comment;
}
_ "whitespace with comments"
= items:(comment / s)* {
return ast.Comment.create(range(location()), items);
}
comment "comment"
= multiLineComment
/ singleLineComment
multiLineComment "multi line comment"
= '/*' (!'*/' .)* '*/' {
return ast.MultiLineComment.create(range(location()), text());
}
multiLineCommentOneLine "multi line comment on one line"
= '/*' (!('*/' / lineTerminatorSequence) .)* '*/' {
return ast.MultiLineCommentOneLine.create(range(location()), text());
}
singleLineComment " single line comment"
= '//' (!lineTerminator .)* {
return ast.SingleLineComment.create(range(location()), text());
}
lineTerminator
= [\n\r]
lineTerminatorSequence "end of line"
= '\n'
/ '\r'
/ '\r\n'
s "whitespace"
= [ \t\n\r] {
return text()
}