@willbooster/tree-sitter-zig 16.0.1 → 18.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -7
- package/grammar.js +179 -118
- package/package.json +1 -1
- package/src/node-types.json +250 -26
- package/src/scanner.c +121 -11
- package/tree-sitter-zig.wasm +0 -0
- package/tree-sitter.json +1 -1
package/README.md
CHANGED
|
@@ -18,12 +18,10 @@ which upstream's tree-sitter runtimes reject, and is tested and supported with t
|
|
|
18
18
|
version 23.1.0 or later in the 23.x series. It parses as the parser in upstream's 1.1.2 packages does, with these
|
|
19
19
|
exceptions:
|
|
20
20
|
|
|
21
|
-
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
a field access, a binary expression, and so on. tree-sitter CLI 0.27 generates this fork's reading from the same
|
|
26
|
-
grammar. This generator difference applies only to those `for`-loop range ends.
|
|
21
|
+
- A range outside slice brackets or a `for` operand is a syntax error. A sentinel also requires a range,
|
|
22
|
+
so `a[0:0]` is a syntax error. In `for (0..(n))`, `for (0..&x)`, and `for (0..-n)`, the expression
|
|
23
|
+
after `..` is the right operand of the range. Upstream's packaged parser can use the open range as
|
|
24
|
+
the start of a larger expression in these positions.
|
|
27
25
|
- The names `true`, `false`, `null`, and `undefined` can label blocks in statements and expressions or name parameters.
|
|
28
26
|
- Labeled blocks used as indices or slice bounds retain their `block_label` inside the expression,
|
|
29
27
|
instead of splitting the label colon into an index and a sentinel.
|
|
@@ -34,13 +32,20 @@ exceptions:
|
|
|
34
32
|
- `///` and `//!` produce `doc_comment` and `container_doc_comment` nodes. Their `doc_comment_content` children
|
|
35
33
|
exclude the prefix and include the line ending when present.
|
|
36
34
|
An empty comment at EOF has no content child. Ordinary comments, including `////`, remain `comment` nodes.
|
|
37
|
-
|
|
35
|
+
Declaration documentation starts on its own line in member, parameter, and error-set lists. Ordinary comments can
|
|
36
|
+
also appear inside expressions. Code that handles every comment kind must match all three node types.
|
|
38
37
|
|
|
39
38
|
- Error sets are accepted directly as container field types, and pointer, slice and array element types may be conditional.
|
|
40
39
|
- `async`, `await`, and `usingnamespace` are ordinary identifiers. Removed `usingnamespace` declarations produce syntax
|
|
41
40
|
errors. Assembly clobbers use an expression, including an anonymous struct
|
|
42
41
|
initializer, as in Zig 0.16.
|
|
43
42
|
- Zero-bit integers `u0` and `i0` are builtin types.
|
|
43
|
+
- Structs and unions accept `packed`, `extern`, or the `bitpack` layout modifier from Zig 0.18 development builds.
|
|
44
|
+
Packed and bitpack containers accept an optional parenthesized backing type and one contiguous group of named fields
|
|
45
|
+
without `comptime` or alignment overrides. Packed and bitpack structs accept field defaults. This grammar uses
|
|
46
|
+
the Zig 0.18 keyword set. Use quoted identifiers such as `@"bitpack"` for names that match keywords. An unmodified
|
|
47
|
+
union accepts a parenthesized tag. Enums accept an optional backing type. Enums and opaque declarations have no
|
|
48
|
+
layout modifier, and opaque declarations have no backing type.
|
|
44
49
|
- Quoted identifiers such as `@"hello world"` expose their content directly inside `identifier`, without a nested
|
|
45
50
|
`string` node.
|
|
46
51
|
- Boolean negation is a `unary_expression`. Inferred error unions (`!T`) occur only in function return types;
|
package/grammar.js
CHANGED
|
@@ -33,7 +33,6 @@ const builtinTypes = [
|
|
|
33
33
|
'type',
|
|
34
34
|
'anyerror',
|
|
35
35
|
'anyopaque',
|
|
36
|
-
'anytype',
|
|
37
36
|
'noreturn',
|
|
38
37
|
'isize',
|
|
39
38
|
'usize',
|
|
@@ -54,7 +53,18 @@ const builtinTypes = [
|
|
|
54
53
|
module.exports = grammar({
|
|
55
54
|
name: 'zig',
|
|
56
55
|
|
|
57
|
-
externals: $ => [
|
|
56
|
+
externals: $ => [
|
|
57
|
+
$.doc_comment_content,
|
|
58
|
+
$._empty_doc_comment_content,
|
|
59
|
+
$._error_sentinel,
|
|
60
|
+
$._declaration_name_colon,
|
|
61
|
+
$._union_backing_open,
|
|
62
|
+
$.comment,
|
|
63
|
+
$._string_content_guard,
|
|
64
|
+
$._division,
|
|
65
|
+
$._division_assignment,
|
|
66
|
+
$._declaration_doc_open,
|
|
67
|
+
],
|
|
58
68
|
|
|
59
69
|
conflicts: $ => [
|
|
60
70
|
[$.block_label, $.primary_type_expression],
|
|
@@ -62,17 +72,11 @@ module.exports = grammar({
|
|
|
62
72
|
[$.for_expression],
|
|
63
73
|
[$.while_expression],
|
|
64
74
|
|
|
65
|
-
[$.expression, $._function_prototype],
|
|
66
|
-
[$.expression, $.if_type_expression],
|
|
67
|
-
|
|
68
|
-
[$.comptime_type_expression, $.expression],
|
|
69
75
|
[$.comptime_type_expression, $.parameter],
|
|
70
76
|
],
|
|
71
77
|
|
|
72
78
|
extras: $ => [
|
|
73
79
|
$.comment,
|
|
74
|
-
$.doc_comment,
|
|
75
|
-
$.container_doc_comment,
|
|
76
80
|
/\s/,
|
|
77
81
|
],
|
|
78
82
|
|
|
@@ -82,6 +86,7 @@ module.exports = grammar({
|
|
|
82
86
|
|
|
83
87
|
precedences: $ => [
|
|
84
88
|
[$.container_field, $.type_expression],
|
|
89
|
+
[$.container_field, $.comptime_type_expression],
|
|
85
90
|
[$._variable_declaration_expression_statement, $.assignment_expression],
|
|
86
91
|
[$._variable_declaration_expression_statement, $._single_assignment_expression],
|
|
87
92
|
],
|
|
@@ -95,22 +100,27 @@ module.exports = grammar({
|
|
|
95
100
|
|
|
96
101
|
word: $ => $._identifier,
|
|
97
102
|
|
|
103
|
+
reserved: {
|
|
104
|
+
global: _ => [
|
|
105
|
+
'addrspace', 'align', 'allowzero', 'and', 'anyframe', 'anytype',
|
|
106
|
+
'asm', 'bitpack', 'break', 'callconv', 'catch', 'comptime',
|
|
107
|
+
'const', 'continue', 'defer', 'else', 'enum', 'errdefer',
|
|
108
|
+
'error', 'export', 'extern', 'fn', 'for', 'if',
|
|
109
|
+
'inline', 'linksection', 'noalias', 'noinline', 'nosuspend', 'opaque',
|
|
110
|
+
'or', 'orelse', 'packed', 'pub', 'resume', 'return',
|
|
111
|
+
'struct', 'suspend', 'switch', 'test', 'threadlocal', 'try',
|
|
112
|
+
'union', 'unreachable', 'var', 'volatile', 'while',
|
|
113
|
+
],
|
|
114
|
+
},
|
|
115
|
+
|
|
98
116
|
rules: {
|
|
99
|
-
source_file: $ => optional($._container_members),
|
|
117
|
+
source_file: $ => seq(repeat($.container_doc_comment), optional(choice($._container_members, repeat1($.doc_comment)))),
|
|
100
118
|
|
|
101
|
-
_container_members: $ =>
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
$.variable_declaration,
|
|
107
|
-
$.function_declaration,
|
|
108
|
-
seq($.container_field, ','),
|
|
109
|
-
)),
|
|
110
|
-
optional($.container_field),
|
|
111
|
-
),
|
|
112
|
-
$.container_field,
|
|
113
|
-
),
|
|
119
|
+
_container_members: $ => containerMembers($, $.container_field),
|
|
120
|
+
|
|
121
|
+
_packed_struct_members: $ => packedContainerMembers($, alias($._packed_struct_field, $.container_field)),
|
|
122
|
+
|
|
123
|
+
_packed_union_members: $ => packedContainerMembers($, alias($._packed_union_field, $.container_field)),
|
|
114
124
|
|
|
115
125
|
test_declaration: $ => seq(
|
|
116
126
|
optional('pub'),
|
|
@@ -127,18 +137,15 @@ module.exports = grammar({
|
|
|
127
137
|
|
|
128
138
|
container_field: $ => prec.right(prec.dynamic(1, seq(
|
|
129
139
|
optional('comptime'),
|
|
130
|
-
choice(
|
|
131
|
-
seq(
|
|
132
|
-
field('name', choice($.identifier, $._reserved_identifier, alias($.builtin_type, $.identifier))),
|
|
133
|
-
alias($._declaration_name_colon, ':'),
|
|
134
|
-
field('type', choice($.primary_type_expression, $.if_type_expression, $.comptime_type_expression, $.error_set_declaration)),
|
|
135
|
-
),
|
|
136
|
-
field('name', choice($.primary_type_expression, $.if_type_expression, $.comptime_type_expression, $.error_set_declaration)),
|
|
137
|
-
),
|
|
140
|
+
choice(namedContainerField($), field('name', typeExpression($))),
|
|
138
141
|
optional($.byte_alignment),
|
|
139
142
|
optional(seq('=', $.expression)),
|
|
140
143
|
))),
|
|
141
144
|
|
|
145
|
+
_packed_struct_field: $ => packedField($, optional(seq('=', $.expression))),
|
|
146
|
+
|
|
147
|
+
_packed_union_field: $ => packedField($),
|
|
148
|
+
|
|
142
149
|
variable_declaration: $ => seq(
|
|
143
150
|
optional('pub'),
|
|
144
151
|
optional(choice(
|
|
@@ -181,7 +188,7 @@ module.exports = grammar({
|
|
|
181
188
|
$.identifier,
|
|
182
189
|
optional(seq(
|
|
183
190
|
':',
|
|
184
|
-
field('type',
|
|
191
|
+
field('type', typeExpression($)),
|
|
185
192
|
)),
|
|
186
193
|
optional($.byte_alignment),
|
|
187
194
|
optional($.address_space),
|
|
@@ -212,25 +219,24 @@ module.exports = grammar({
|
|
|
212
219
|
optional($.link_section),
|
|
213
220
|
optional($.calling_convention),
|
|
214
221
|
field('type', choice(
|
|
215
|
-
|
|
216
|
-
$.if_type_expression,
|
|
217
|
-
$.comptime_type_expression,
|
|
222
|
+
typeExpression($),
|
|
218
223
|
alias($._inferred_error_union_type, $.error_union_type),
|
|
219
224
|
)),
|
|
220
225
|
),
|
|
221
226
|
|
|
222
|
-
_inferred_error_union_type: $ => prec.right(-1, seq('!', field('ok',
|
|
227
|
+
_inferred_error_union_type: $ => prec.right(-1, seq('!', field('ok', typeExpression($)))),
|
|
223
228
|
|
|
224
229
|
parameters: $ => seq('(', optionalCommaSep($.parameter), ')'),
|
|
225
230
|
|
|
226
231
|
parameter: $ => choice(
|
|
227
232
|
seq(
|
|
233
|
+
repeat($.doc_comment),
|
|
228
234
|
optional(choice('noalias', 'comptime')),
|
|
229
235
|
optional(seq(
|
|
230
236
|
field('name', choice($.identifier, alias(choice($.builtin_type, $._reserved_identifier), $.identifier))),
|
|
231
237
|
alias($._declaration_name_colon, ':'),
|
|
232
238
|
)),
|
|
233
|
-
field('type', choice(
|
|
239
|
+
field('type', choice(alias('anytype', $.builtin_type), typeExpression($))),
|
|
234
240
|
),
|
|
235
241
|
'...',
|
|
236
242
|
),
|
|
@@ -241,52 +247,46 @@ module.exports = grammar({
|
|
|
241
247
|
'}',
|
|
242
248
|
),
|
|
243
249
|
|
|
244
|
-
|
|
245
|
-
optional(choice('extern', 'packed')),
|
|
246
|
-
'struct',
|
|
247
|
-
optional(seq('(', $.expression, ')')),
|
|
248
|
-
'{',
|
|
249
|
-
optional($._container_members),
|
|
250
|
-
'}',
|
|
251
|
-
),
|
|
250
|
+
_packed_layout: _ => choice('packed', 'bitpack'),
|
|
252
251
|
|
|
253
|
-
|
|
254
|
-
optional(
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
252
|
+
struct_declaration: $ => choice(
|
|
253
|
+
containerBody($, seq($._packed_layout, 'struct', optional(seq('(', $.expression, ')'))), $._packed_struct_members),
|
|
254
|
+
containerBody(
|
|
255
|
+
$,
|
|
256
|
+
seq(optional('extern'), 'struct'),
|
|
257
|
+
$._container_members,
|
|
258
|
+
),
|
|
259
259
|
),
|
|
260
260
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
'
|
|
266
|
-
|
|
267
|
-
'}',
|
|
261
|
+
opaque_declaration: $ => containerBody($, 'opaque', $._container_members),
|
|
262
|
+
|
|
263
|
+
enum_declaration: $ => containerBody(
|
|
264
|
+
$,
|
|
265
|
+
seq('enum', optional(seq('(', $.expression, ')'))),
|
|
266
|
+
$._container_members,
|
|
268
267
|
),
|
|
269
268
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
269
|
+
_union_backing: $ => seq(alias($._union_backing_open, '('), $.expression, ')'),
|
|
270
|
+
|
|
271
|
+
union_declaration: $ => choice(
|
|
272
|
+
containerBody($, seq($._packed_layout, 'union', optional($._union_backing)), $._packed_union_members),
|
|
273
|
+
containerBody(
|
|
274
|
+
$,
|
|
275
275
|
choice(
|
|
276
|
-
seq('
|
|
277
|
-
|
|
276
|
+
seq('extern', 'union'),
|
|
277
|
+
seq('union', optional(choice(
|
|
278
|
+
$._union_backing,
|
|
279
|
+
seq('(', 'enum', optional(seq('(', $.expression, ')')), ')'),
|
|
280
|
+
))),
|
|
278
281
|
),
|
|
279
|
-
|
|
280
|
-
)
|
|
281
|
-
'{',
|
|
282
|
-
optional($._container_members),
|
|
283
|
-
'}',
|
|
282
|
+
$._container_members,
|
|
283
|
+
),
|
|
284
284
|
),
|
|
285
285
|
|
|
286
286
|
error_set_declaration: $ => seq(
|
|
287
287
|
'error',
|
|
288
288
|
'{',
|
|
289
|
-
optionalCommaSep($.identifier),
|
|
289
|
+
optionalCommaSep(seq(repeat($.doc_comment), $.identifier)),
|
|
290
290
|
'}',
|
|
291
291
|
),
|
|
292
292
|
|
|
@@ -363,10 +363,7 @@ module.exports = grammar({
|
|
|
363
363
|
_for_prefix: $ => seq(
|
|
364
364
|
'for',
|
|
365
365
|
'(',
|
|
366
|
-
|
|
367
|
-
$.expression,
|
|
368
|
-
optional(seq('..', $.expression)),
|
|
369
|
-
)),
|
|
366
|
+
optionalCommaSep1(choice($.range_expression, $.expression)),
|
|
370
367
|
')',
|
|
371
368
|
$.payload,
|
|
372
369
|
),
|
|
@@ -422,6 +419,7 @@ module.exports = grammar({
|
|
|
422
419
|
$.break_expression,
|
|
423
420
|
$.try_expression,
|
|
424
421
|
$.catch_expression,
|
|
422
|
+
$.struct_initializer,
|
|
425
423
|
$.type_expression,
|
|
426
424
|
$.block,
|
|
427
425
|
)),
|
|
@@ -441,7 +439,7 @@ module.exports = grammar({
|
|
|
441
439
|
']',
|
|
442
440
|
choice($.string, $.multiline_string),
|
|
443
441
|
'(',
|
|
444
|
-
choice(seq('->',
|
|
442
|
+
choice(seq('->', typeExpression($)), $.identifier),
|
|
445
443
|
')',
|
|
446
444
|
),
|
|
447
445
|
asm_input: $ => seq(':', optionalCommaSep($.asm_input_item), optional($.asm_clobbers)),
|
|
@@ -462,21 +460,16 @@ module.exports = grammar({
|
|
|
462
460
|
optional(seq('else', optional($.payload), $.expression)),
|
|
463
461
|
)),
|
|
464
462
|
|
|
465
|
-
for_expression: $ =>
|
|
466
|
-
optional($.block_label),
|
|
467
|
-
optional('inline'),
|
|
468
|
-
$._for_prefix,
|
|
469
|
-
$.expression,
|
|
470
|
-
optional(seq('else', $.expression)),
|
|
471
|
-
)),
|
|
463
|
+
for_expression: $ => loopExpression($, $._for_prefix, $.expression),
|
|
472
464
|
|
|
473
|
-
while_expression: $ =>
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
465
|
+
while_expression: $ => loopExpression($, $._while_prefix, $.expression, seq(optional($.payload), $.expression)),
|
|
466
|
+
|
|
467
|
+
_for_type_expression: $ => loopExpression($, $._for_prefix, typeExpression($)),
|
|
468
|
+
|
|
469
|
+
_while_type_expression: $ => {
|
|
470
|
+
const type = typeExpression($);
|
|
471
|
+
return loopExpression($, $._while_prefix, type, seq(optional($.payload), type));
|
|
472
|
+
},
|
|
480
473
|
|
|
481
474
|
assignment_expression: $ => choice(
|
|
482
475
|
$._single_assignment_expression,
|
|
@@ -494,8 +487,8 @@ module.exports = grammar({
|
|
|
494
487
|
field('right', $.expression),
|
|
495
488
|
),
|
|
496
489
|
|
|
497
|
-
_assignment_operator:
|
|
498
|
-
'=', '*=', '*%=', '*|=', '/=', '%=',
|
|
490
|
+
_assignment_operator: $ => choice(
|
|
491
|
+
'=', '*=', '*%=', '*|=', alias($._division_assignment, '/='), '%=',
|
|
499
492
|
'+=', '+%=', '+|=', '-=', '-%=', '-|=',
|
|
500
493
|
'<<=', '<<|=', '>>=', '&=', '^=', '|=',
|
|
501
494
|
),
|
|
@@ -530,7 +523,7 @@ module.exports = grammar({
|
|
|
530
523
|
['+|', PREC.ADD],
|
|
531
524
|
['-|', PREC.ADD],
|
|
532
525
|
['*', PREC.MULTIPLY],
|
|
533
|
-
['/', PREC.MULTIPLY],
|
|
526
|
+
[alias($._division, '/'), PREC.MULTIPLY],
|
|
534
527
|
['%', PREC.MULTIPLY],
|
|
535
528
|
['**', PREC.MULTIPLY],
|
|
536
529
|
['*%', PREC.MULTIPLY],
|
|
@@ -601,7 +594,6 @@ module.exports = grammar({
|
|
|
601
594
|
|
|
602
595
|
type_expression: $ => prec.right(choice(
|
|
603
596
|
$.anonymous_struct_initializer,
|
|
604
|
-
$.struct_initializer,
|
|
605
597
|
$.labeled_type_expression,
|
|
606
598
|
$.error_set_declaration,
|
|
607
599
|
$.parenthesized_expression,
|
|
@@ -621,7 +613,6 @@ module.exports = grammar({
|
|
|
621
613
|
$.index_expression,
|
|
622
614
|
$.dereference_expression,
|
|
623
615
|
$.null_coercion_expression,
|
|
624
|
-
$.range_expression,
|
|
625
616
|
$.call_expression,
|
|
626
617
|
prec.right(alias($._function_prototype, $.function_signature)),
|
|
627
618
|
$.identifier,
|
|
@@ -645,13 +636,13 @@ module.exports = grammar({
|
|
|
645
636
|
|
|
646
637
|
nullable_type: $ => prec(1, seq(
|
|
647
638
|
'?',
|
|
648
|
-
|
|
639
|
+
typeExpression($),
|
|
649
640
|
)),
|
|
650
641
|
|
|
651
642
|
anyframe_type: $ => prec(1, seq(
|
|
652
643
|
'anyframe',
|
|
653
644
|
'->',
|
|
654
|
-
|
|
645
|
+
typeExpression($),
|
|
655
646
|
)),
|
|
656
647
|
|
|
657
648
|
slice_type: $ => prec.right(1, seq(
|
|
@@ -668,7 +659,7 @@ module.exports = grammar({
|
|
|
668
659
|
'volatile',
|
|
669
660
|
'allowzero',
|
|
670
661
|
)),
|
|
671
|
-
|
|
662
|
+
typeExpression($),
|
|
672
663
|
)),
|
|
673
664
|
|
|
674
665
|
pointer_type: $ => prec.right(1, seq(
|
|
@@ -694,7 +685,7 @@ module.exports = grammar({
|
|
|
694
685
|
'volatile',
|
|
695
686
|
'allowzero',
|
|
696
687
|
)),
|
|
697
|
-
|
|
688
|
+
typeExpression($),
|
|
698
689
|
)),
|
|
699
690
|
|
|
700
691
|
array_type: $ => prec(1, seq(
|
|
@@ -702,32 +693,37 @@ module.exports = grammar({
|
|
|
702
693
|
$.expression,
|
|
703
694
|
optional(seq(':', $.expression)),
|
|
704
695
|
']',
|
|
705
|
-
|
|
696
|
+
typeExpression($),
|
|
706
697
|
)),
|
|
707
698
|
|
|
708
699
|
error_union_type: $ => prec.right(2, seq(
|
|
709
700
|
field('error', $.type_expression),
|
|
710
701
|
'!',
|
|
711
|
-
field('ok',
|
|
702
|
+
field('ok', typeExpression($)),
|
|
712
703
|
)),
|
|
713
704
|
|
|
714
705
|
field_expression: $ => prec(PREC.MEMBER, seq(
|
|
715
|
-
optional(field('object', $.
|
|
706
|
+
optional(field('object', $.type_expression)),
|
|
716
707
|
'.',
|
|
717
708
|
field('member', $.identifier),
|
|
718
709
|
)),
|
|
719
710
|
|
|
720
711
|
index_expression: $ => prec(PREC.MEMBER, seq(
|
|
721
|
-
field('object', $.
|
|
712
|
+
field('object', $.type_expression),
|
|
722
713
|
'[',
|
|
723
|
-
|
|
724
|
-
|
|
714
|
+
choice(
|
|
715
|
+
field('index', $.expression),
|
|
716
|
+
seq(
|
|
717
|
+
field('index', $.range_expression),
|
|
718
|
+
optional(seq(':', field('sentinel', $.expression))),
|
|
719
|
+
),
|
|
720
|
+
),
|
|
725
721
|
']',
|
|
726
722
|
)),
|
|
727
723
|
|
|
728
|
-
dereference_expression: $ => prec(PREC.MEMBER, seq($.
|
|
724
|
+
dereference_expression: $ => prec(PREC.MEMBER, seq($.type_expression, '.*')),
|
|
729
725
|
|
|
730
|
-
null_coercion_expression: $ => prec(PREC.MEMBER, seq($.
|
|
726
|
+
null_coercion_expression: $ => prec(PREC.MEMBER, seq($.type_expression, '.?')),
|
|
731
727
|
|
|
732
728
|
range_expression: $ => prec.right(PREC.MEMBER, seq(
|
|
733
729
|
field('left', $.expression),
|
|
@@ -736,7 +732,7 @@ module.exports = grammar({
|
|
|
736
732
|
)),
|
|
737
733
|
|
|
738
734
|
call_expression: $ => prec(PREC.MEMBER, seq(
|
|
739
|
-
field('function', $.
|
|
735
|
+
field('function', $.type_expression),
|
|
740
736
|
'(',
|
|
741
737
|
optionalCommaSep($.expression),
|
|
742
738
|
')',
|
|
@@ -744,7 +740,7 @@ module.exports = grammar({
|
|
|
744
740
|
|
|
745
741
|
anonymous_struct_initializer: $ => seq('.', $.initializer_list),
|
|
746
742
|
|
|
747
|
-
struct_initializer: $ => prec(-1, seq($.
|
|
743
|
+
struct_initializer: $ => prec(-1, seq($.type_expression, $.initializer_list)),
|
|
748
744
|
|
|
749
745
|
initializer_list: $ => seq(
|
|
750
746
|
'{',
|
|
@@ -764,12 +760,12 @@ module.exports = grammar({
|
|
|
764
760
|
|
|
765
761
|
labeled_type_expression: $ => seq($.block_label, $.block),
|
|
766
762
|
|
|
767
|
-
comptime_type_expression: $ => seq('comptime',
|
|
763
|
+
comptime_type_expression: $ => seq('comptime', typeExpression($)),
|
|
768
764
|
|
|
769
765
|
if_type_expression: $ => prec.right(seq(
|
|
770
766
|
$._if_prefix,
|
|
771
|
-
|
|
772
|
-
optional(seq('else', optional($.payload),
|
|
767
|
+
typeExpression($),
|
|
768
|
+
optional(seq('else', optional($.payload), typeExpression($))),
|
|
773
769
|
)),
|
|
774
770
|
|
|
775
771
|
parenthesized_expression: $ => seq('(', $.expression, ')'),
|
|
@@ -792,7 +788,7 @@ module.exports = grammar({
|
|
|
792
788
|
_string: $ => seq(
|
|
793
789
|
'"',
|
|
794
790
|
repeat(choice(
|
|
795
|
-
|
|
791
|
+
stringContent($, /[^\\"\n]+/),
|
|
796
792
|
$.escape_sequence,
|
|
797
793
|
)),
|
|
798
794
|
'"',
|
|
@@ -800,7 +796,7 @@ module.exports = grammar({
|
|
|
800
796
|
|
|
801
797
|
multiline_string: $ => prec.right(repeat1(seq(
|
|
802
798
|
'\\\\',
|
|
803
|
-
optional(
|
|
799
|
+
optional(stringContent($, /[^\r\n]+/)),
|
|
804
800
|
))),
|
|
805
801
|
|
|
806
802
|
escape_sequence: _ => token(prec(1, seq(
|
|
@@ -873,14 +869,10 @@ module.exports = grammar({
|
|
|
873
869
|
'false',
|
|
874
870
|
),
|
|
875
871
|
|
|
876
|
-
doc_comment: $ => seq('///', choice($.doc_comment_content, $._empty_doc_comment_content)),
|
|
872
|
+
doc_comment: $ => seq(alias($._declaration_doc_open, '///'), choice($.doc_comment_content, $._empty_doc_comment_content)),
|
|
877
873
|
|
|
878
874
|
container_doc_comment: $ => seq('//!', choice($.doc_comment_content, $._empty_doc_comment_content)),
|
|
879
875
|
|
|
880
|
-
comment: _ => token(seq('//', optional(choice(
|
|
881
|
-
seq(/[^!/\n]/, /.*/),
|
|
882
|
-
seq('//', /.*/),
|
|
883
|
-
)))),
|
|
884
876
|
},
|
|
885
877
|
});
|
|
886
878
|
|
|
@@ -895,3 +887,72 @@ function optionalCommaSep1(rule) {
|
|
|
895
887
|
function commaSep1(rule) {
|
|
896
888
|
return seq(rule, repeat(seq(',', rule)));
|
|
897
889
|
}
|
|
890
|
+
|
|
891
|
+
function containerMembers($, field) {
|
|
892
|
+
const member = seq(repeat($.doc_comment), field);
|
|
893
|
+
return choice(
|
|
894
|
+
seq(
|
|
895
|
+
repeat1(choice(containerDeclaration($), seq(member, ','))),
|
|
896
|
+
optional(member),
|
|
897
|
+
),
|
|
898
|
+
member,
|
|
899
|
+
);
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
function packedContainerMembers($, field) {
|
|
903
|
+
const member = seq(repeat($.doc_comment), field);
|
|
904
|
+
const declaration = containerDeclaration($);
|
|
905
|
+
return choice(
|
|
906
|
+
repeat1(declaration),
|
|
907
|
+
seq(repeat(declaration), commaSep1(member), optional(seq(',', repeat(declaration)))),
|
|
908
|
+
);
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
function containerDeclaration($) {
|
|
912
|
+
return choice(
|
|
913
|
+
$.test_declaration,
|
|
914
|
+
$.comptime_declaration,
|
|
915
|
+
seq(repeat($.doc_comment), choice($.variable_declaration, $.function_declaration)),
|
|
916
|
+
);
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
function containerBody($, header, members) {
|
|
920
|
+
return seq(header, '{', repeat($.container_doc_comment), optional(members), '}');
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
|
|
924
|
+
function packedField($, defaultValue = blank()) {
|
|
925
|
+
return prec.right(prec.dynamic(1, seq(namedContainerField($), defaultValue)));
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
function namedContainerField($) {
|
|
929
|
+
return seq(
|
|
930
|
+
field('name', choice($.identifier, alias(choice($.builtin_type, $._reserved_identifier), $.identifier))),
|
|
931
|
+
alias($._declaration_name_colon, ':'),
|
|
932
|
+
field('type', typeExpression($)),
|
|
933
|
+
);
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
function typeExpression($) {
|
|
937
|
+
return choice(
|
|
938
|
+
$.type_expression,
|
|
939
|
+
$.if_type_expression,
|
|
940
|
+
$.comptime_type_expression,
|
|
941
|
+
alias($._for_type_expression, $.for_expression),
|
|
942
|
+
alias($._while_type_expression, $.while_expression),
|
|
943
|
+
);
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
function loopExpression($, prefix, body, otherwise = body) {
|
|
947
|
+
return prec.right(seq(
|
|
948
|
+
optional($.block_label),
|
|
949
|
+
optional('inline'),
|
|
950
|
+
prefix,
|
|
951
|
+
body,
|
|
952
|
+
optional(seq('else', otherwise)),
|
|
953
|
+
));
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
function stringContent($, pattern) {
|
|
957
|
+
return alias(choice($._string_content_guard, token.immediate(prec(1, pattern))), $.string_content);
|
|
958
|
+
}
|