@hew-lang/tree-sitter-hew 1.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/grammar.js ADDED
@@ -0,0 +1,968 @@
1
+ /**
2
+ * @file Hew grammar for tree-sitter
3
+ * @author Hew Contributors
4
+ * @license Apache-2.0
5
+ */
6
+
7
+ /// <reference types="tree-sitter-cli/dsl" />
8
+ // @ts-check
9
+
10
+ // Precedence levels (from Pratt parser in hew-parser/src/parser.rs)
11
+ const PREC = {
12
+ SEND: 1, // <-
13
+ OR: 3, // || or
14
+ BIT_OR: 4, // |
15
+ AND: 5, // && and
16
+ BIT_AND: 6, // &
17
+ BIT_XOR: 6, // ^
18
+ EQ: 7, // == != =~ !~
19
+ REL: 9, // < <= > >=
20
+ RANGE: 11, // .. ..=
21
+ SHIFT: 12, // << >>
22
+ ADD: 13, // + -
23
+ MUL: 15, // * / %
24
+ UNARY: 17, // ! - ~ await
25
+ POSTFIX: 19, // . () [] ?
26
+ FIELD: 20, // field access
27
+ };
28
+
29
+ export default grammar({
30
+ name: "hew",
31
+
32
+ extras: $ => [/\s/, $.line_comment, $.block_comment],
33
+
34
+ word: $ => $.identifier,
35
+
36
+ conflicts: $ => [
37
+ [$.expression, $.lambda_parameter],
38
+ [$.if_statement, $.expression],
39
+ [$.match_statement, $.expression],
40
+ [$.expression, $.struct_init],
41
+ ],
42
+
43
+ rules: {
44
+ source_file: $ => repeat($._item),
45
+
46
+ // ---- Items ----
47
+
48
+ _item: $ => seq(
49
+ repeat($.attribute),
50
+ choice(
51
+ $.import_declaration,
52
+ $.const_declaration,
53
+ $.struct_declaration,
54
+ $.enum_declaration,
55
+ $.wire_declaration,
56
+ $.trait_declaration,
57
+ $.impl_declaration,
58
+ $.function_declaration,
59
+ $.gen_function_declaration,
60
+ $.async_gen_function_declaration,
61
+ $.async_function_declaration,
62
+ $.extern_block,
63
+ $.actor_declaration,
64
+ $.supervisor_declaration,
65
+ $.type_alias,
66
+ ),
67
+ ),
68
+
69
+ attribute: $ => seq('#', '[', $.identifier, optional(seq('(', optional(sep1(choice($.identifier, $._literal), ',')), ')')), ']'),
70
+
71
+ import_declaration: $ => seq(
72
+ 'import',
73
+ $.module_path,
74
+ optional(seq('::', $._import_spec)),
75
+ ';',
76
+ ),
77
+
78
+ module_path: $ => prec.left(sep1($.identifier, '::')),
79
+
80
+ _import_spec: $ => choice(
81
+ $.identifier,
82
+ seq('{', sep1($.identifier, ','), '}'),
83
+ '*',
84
+ ),
85
+
86
+ const_declaration: $ => seq(
87
+ optional($.visibility),
88
+ 'const',
89
+ field('name', $.identifier),
90
+ ':',
91
+ field('type', $._type),
92
+ '=',
93
+ field('value', $.expression),
94
+ ';',
95
+ ),
96
+
97
+ type_alias: $ => seq(
98
+ 'type',
99
+ field('name', $.identifier),
100
+ optional($.type_parameters),
101
+ '=',
102
+ field('type', $._type),
103
+ ';',
104
+ ),
105
+
106
+ struct_declaration: $ => seq(
107
+ optional($.visibility),
108
+ 'type',
109
+ field('name', $.identifier),
110
+ optional($.type_parameters),
111
+ optional($.where_clause),
112
+ field('body', $.struct_body),
113
+ ),
114
+
115
+ struct_body: $ => seq('{', repeat($.struct_field), '}'),
116
+
117
+ struct_field: $ => seq(
118
+ optional('var'),
119
+ field('name', $.identifier),
120
+ ':',
121
+ field('type', $._type),
122
+ optional(choice(',', ';')),
123
+ ),
124
+
125
+ enum_declaration: $ => seq(
126
+ optional($.visibility),
127
+ 'enum',
128
+ field('name', $.identifier),
129
+ optional($.type_parameters),
130
+ optional($.where_clause),
131
+ field('body', $.enum_body),
132
+ ),
133
+
134
+ enum_body: $ => seq('{', repeat($.variant), '}'),
135
+
136
+ variant: $ => seq(
137
+ field('name', $.identifier),
138
+ optional(choice(
139
+ seq('(', sep1($._type, ','), ')'),
140
+ seq('{', repeat($.struct_field), '}'),
141
+ )),
142
+ optional(choice(',', ';')),
143
+ ),
144
+
145
+ // "type X { ... }" is a struct alias form
146
+ // handled by struct_declaration with choice('struct', 'type')
147
+
148
+ wire_declaration: $ => seq(
149
+ 'wire',
150
+ choice('struct', 'enum'),
151
+ field('name', $.identifier),
152
+ field('body', $.wire_body),
153
+ ),
154
+
155
+ wire_body: $ => seq('{', repeat(choice($.wire_field, $.variant, $.reserved_declaration)), '}'),
156
+
157
+ wire_field: $ => prec.left(seq(
158
+ field('name', $.identifier),
159
+ ':',
160
+ field('type', $.wire_type),
161
+ optional(seq('@', $.integer_literal)),
162
+ repeat($.wire_attribute),
163
+ optional(choice(',', ';')),
164
+ )),
165
+
166
+ wire_type: $ => choice(
167
+ 'u8', 'u16', 'u32', 'u64',
168
+ 'i8', 'i16', 'i32', 'i64',
169
+ 'f32', 'f64',
170
+ 'bool', 'bytes', 'string',
171
+ $.identifier,
172
+ seq('list', '[', $.wire_type, ']'),
173
+ ),
174
+
175
+ reserved_declaration: $ => seq(
176
+ 'reserved', '(', sep1($.integer_literal, ','), ')', ';',
177
+ ),
178
+
179
+ wire_attribute: $ => choice(
180
+ 'optional',
181
+ 'deprecated',
182
+ seq('default', '(', $.expression, ')'),
183
+ seq('reserved', '(', sep1($.integer_literal, ','), ')'),
184
+ ),
185
+
186
+ // ---- Traits & Impls ----
187
+
188
+ trait_declaration: $ => seq(
189
+ 'trait',
190
+ field('name', $.identifier),
191
+ optional($.type_parameters),
192
+ optional(seq(':', $.trait_bounds)),
193
+ optional($.where_clause),
194
+ '{',
195
+ repeat($._trait_item),
196
+ '}',
197
+ ),
198
+
199
+ _trait_item: $ => choice(
200
+ $.trait_function_signature,
201
+ $.associated_type,
202
+ ),
203
+
204
+ trait_function_signature: $ => seq(
205
+ optional('pure'),
206
+ 'fn',
207
+ field('name', $.identifier),
208
+ optional($.type_parameters),
209
+ '(',
210
+ optional($.parameters),
211
+ ')',
212
+ optional($.return_type),
213
+ choice(';', field('body', $.block)),
214
+ ),
215
+
216
+ associated_type: $ => seq(
217
+ 'type',
218
+ field('name', $.identifier),
219
+ optional(seq(':', $.trait_bounds)),
220
+ optional(seq('=', $._type)),
221
+ ';',
222
+ ),
223
+
224
+ impl_declaration: $ => seq(
225
+ 'impl',
226
+ optional($.type_parameters),
227
+ field('trait', $.identifier),
228
+ optional($.type_arguments),
229
+ 'for',
230
+ field('type', $._type),
231
+ optional($.where_clause),
232
+ '{',
233
+ repeat(choice($.function_declaration, $.associated_type_impl)),
234
+ '}',
235
+ ),
236
+
237
+ associated_type_impl: $ => seq('type', $.identifier, '=', $._type, ';'),
238
+
239
+ // ---- Functions ----
240
+
241
+ function_declaration: $ => seq(
242
+ optional($.visibility),
243
+ optional('pure'),
244
+ 'fn',
245
+ field('name', $.identifier),
246
+ optional($.type_parameters),
247
+ '(',
248
+ optional($.parameters),
249
+ ')',
250
+ optional($.return_type),
251
+ optional($.where_clause),
252
+ field('body', $.block),
253
+ ),
254
+
255
+ gen_function_declaration: $ => seq(
256
+ optional('pure'),
257
+ 'gen',
258
+ 'fn',
259
+ field('name', $.identifier),
260
+ optional($.type_parameters),
261
+ '(',
262
+ optional($.parameters),
263
+ ')',
264
+ '->',
265
+ field('yield_type', $._type),
266
+ optional($.where_clause),
267
+ field('body', $.block),
268
+ ),
269
+
270
+ async_function_declaration: $ => seq(
271
+ 'async',
272
+ 'fn',
273
+ field('name', $.identifier),
274
+ optional($.type_parameters),
275
+ '(',
276
+ optional($.parameters),
277
+ ')',
278
+ optional($.return_type),
279
+ optional($.where_clause),
280
+ field('body', $.block),
281
+ ),
282
+
283
+ async_gen_function_declaration: $ => seq(
284
+ 'async',
285
+ 'gen',
286
+ 'fn',
287
+ field('name', $.identifier),
288
+ optional($.type_parameters),
289
+ '(',
290
+ optional($.parameters),
291
+ ')',
292
+ '->',
293
+ field('yield_type', $._type),
294
+ optional($.where_clause),
295
+ field('body', $.block),
296
+ ),
297
+
298
+ parameters: $ => sep1($.parameter, ','),
299
+
300
+ parameter: $ => seq(
301
+ field('name', choice($.identifier, $.self)),
302
+ ':',
303
+ field('type', $._type),
304
+ ),
305
+
306
+ return_type: $ => seq('->', $._type),
307
+
308
+ visibility: $ => seq('pub', optional(seq('(', choice('package', 'super'), ')'))),
309
+
310
+ // ---- Actors ----
311
+
312
+ actor_declaration: $ => seq(
313
+ 'actor',
314
+ field('name', $.identifier),
315
+ optional($.type_parameters),
316
+ optional(seq(':', $.trait_bounds)),
317
+ optional($.where_clause),
318
+ '{',
319
+ optional($.actor_init),
320
+ repeat(choice(
321
+ $.actor_field,
322
+ $.mailbox_declaration,
323
+ $.receive_function,
324
+ $.receive_gen_function,
325
+ $.function_declaration,
326
+ $.gen_function_declaration,
327
+ )),
328
+ '}',
329
+ ),
330
+
331
+ actor_init: $ => seq(
332
+ 'init',
333
+ '(',
334
+ optional($.parameters),
335
+ ')',
336
+ $.block,
337
+ ),
338
+
339
+ mailbox_declaration: $ => seq(
340
+ 'mailbox',
341
+ $.integer_literal,
342
+ optional($.overflow_policy),
343
+ ';',
344
+ ),
345
+
346
+ overflow_policy: $ => seq(
347
+ 'overflow',
348
+ $.overflow_kind,
349
+ ),
350
+
351
+ overflow_kind: $ => choice(
352
+ 'block',
353
+ 'drop_new',
354
+ 'drop_old',
355
+ 'fail',
356
+ seq('coalesce', '(', $.identifier, ')', optional(seq('fallback', $.overflow_kind))),
357
+ ),
358
+
359
+ actor_field: $ => seq(
360
+ choice('let', 'var'),
361
+ field('name', $.identifier),
362
+ ':',
363
+ field('type', $._type),
364
+ optional(seq('=', $.expression)),
365
+ ';',
366
+ ),
367
+
368
+ receive_function: $ => seq(
369
+ optional('pure'),
370
+ 'receive',
371
+ 'fn',
372
+ field('name', $.identifier),
373
+ optional($.type_parameters),
374
+ '(',
375
+ optional($.parameters),
376
+ ')',
377
+ optional($.return_type),
378
+ optional($.where_clause),
379
+ field('body', $.block),
380
+ ),
381
+
382
+ receive_gen_function: $ => seq(
383
+ optional('pure'),
384
+ 'receive',
385
+ 'gen',
386
+ 'fn',
387
+ field('name', $.identifier),
388
+ optional($.type_parameters),
389
+ '(',
390
+ optional($.parameters),
391
+ ')',
392
+ '->',
393
+ field('yield_type', $._type),
394
+ optional($.where_clause),
395
+ field('body', $.block),
396
+ ),
397
+
398
+ // ---- Supervisors ----
399
+
400
+ supervisor_declaration: $ => seq(
401
+ 'supervisor',
402
+ field('name', $.identifier),
403
+ '{',
404
+ repeat(choice($.child_spec, $.supervisor_field)),
405
+ '}',
406
+ ),
407
+
408
+ supervisor_field: $ => prec(5, seq(
409
+ field('name', $.identifier),
410
+ ':',
411
+ field('value', $.expression),
412
+ optional(choice(',', ';')),
413
+ )),
414
+
415
+ child_spec: $ => seq(
416
+ 'child',
417
+ field('name', $.identifier),
418
+ ':',
419
+ field('actor', $.identifier),
420
+ optional($.restart_spec),
421
+ ';',
422
+ ),
423
+
424
+ restart_spec: $ => seq(
425
+ 'restart',
426
+ '(',
427
+ choice('permanent', 'transient', 'temporary'),
428
+ ')',
429
+ optional(seq('budget', '(', $.integer_literal, ',', $.duration_literal, ')')),
430
+ optional(seq('strategy', '(', choice('one_for_one', 'one_for_all', 'rest_for_one'), ')')),
431
+ ),
432
+
433
+ duration_literal: $ => seq($.integer_literal, choice('ns', 'us', 'ms', 's', 'm', 'h')),
434
+
435
+ // ---- Extern ----
436
+
437
+ extern_block: $ => seq(
438
+ 'extern',
439
+ $.string_literal,
440
+ '{',
441
+ repeat($.extern_function),
442
+ '}',
443
+ ),
444
+
445
+ extern_function: $ => seq(
446
+ 'fn',
447
+ field('name', $.identifier),
448
+ '(',
449
+ optional($.parameters),
450
+ ')',
451
+ optional($.return_type),
452
+ optional('...'),
453
+ ';',
454
+ ),
455
+
456
+ // ---- Types ----
457
+
458
+ type_parameters: $ => seq('<', sep1($.type_parameter, ','), '>'),
459
+
460
+ type_parameter: $ => seq(
461
+ $.identifier,
462
+ optional(seq(':', $.trait_bounds)),
463
+ ),
464
+
465
+ type_arguments: $ => seq('<', sep1($._type, ','), '>'),
466
+
467
+ trait_bounds: $ => sep1($.trait_bound, '+'),
468
+
469
+ trait_bound: $ => seq($.identifier, optional($.type_arguments)),
470
+
471
+ where_clause: $ => seq(
472
+ 'where',
473
+ sep1($.where_predicate, ','),
474
+ ),
475
+
476
+ where_predicate: $ => seq($._type, ':', $.trait_bounds),
477
+
478
+ _type: $ => choice(
479
+ $.primitive_type,
480
+ $.identifier,
481
+ $.generic_type,
482
+ $.tuple_type,
483
+ $.array_type,
484
+ $.slice_type,
485
+ $.function_type,
486
+ $.pointer_type,
487
+ $.trait_object_type,
488
+ $.unit_type,
489
+ ),
490
+
491
+ primitive_type: $ => choice(
492
+ 'i8', 'i16', 'i32', 'i64',
493
+ 'u8', 'u16', 'u32', 'u64',
494
+ 'f32', 'f64',
495
+ 'isize', 'usize',
496
+ 'bool', 'char', 'string', 'bytes', 'void', 'duration',
497
+ ),
498
+
499
+ generic_type: $ => prec(1, seq($.identifier, $.type_arguments)),
500
+
501
+ tuple_type: $ => seq('(', sep1($._type, ','), ')'),
502
+
503
+ unit_type: $ => seq('(', ')'),
504
+
505
+ array_type: $ => seq('[', $._type, ';', $.integer_literal, ']'),
506
+
507
+ slice_type: $ => seq('[', $._type, ']'),
508
+
509
+ function_type: $ => prec(1, seq('fn', '(', optional(sep1($._type, ',')), ')', optional($.return_type))),
510
+
511
+ pointer_type: $ => seq('*', optional('var'), $._type),
512
+
513
+ trait_object_type: $ => seq('dyn', choice(
514
+ $.trait_bound,
515
+ seq('(', $.trait_bounds, ')'),
516
+ )),
517
+
518
+ // ---- Statements ----
519
+
520
+ block: $ => seq('{', repeat($._statement), '}'),
521
+
522
+ _statement: $ => choice(
523
+ $.let_statement,
524
+ $.var_statement,
525
+ $.assignment_statement,
526
+ $.defer_statement,
527
+
528
+ $.for_statement,
529
+ $.while_statement,
530
+ $.loop_statement,
531
+ $.break_statement,
532
+ $.continue_statement,
533
+ $.return_statement,
534
+ $.expression_statement,
535
+ $.if_statement,
536
+ $.match_statement,
537
+ $.block_statement,
538
+ ),
539
+
540
+ let_statement: $ => seq(
541
+ 'let',
542
+ field('pattern', $.pattern),
543
+ optional(seq(':', field('type', $._type))),
544
+ '=',
545
+ field('value', $.expression),
546
+ ';',
547
+ ),
548
+
549
+ var_statement: $ => seq(
550
+ 'var',
551
+ field('name', $.identifier),
552
+ optional(seq(':', field('type', $._type))),
553
+ '=',
554
+ field('value', $.expression),
555
+ ';',
556
+ ),
557
+
558
+ assignment_statement: $ => seq(
559
+ field('left', $.expression),
560
+ field('operator', choice('=', '+=', '-=', '*=', '/=', '%=', '&=', '|=', '^=', '<<=', '>>=')),
561
+ field('right', $.expression),
562
+ ';',
563
+ ),
564
+
565
+ match_arm: $ => prec(3, seq(
566
+ field('pattern', $.pattern),
567
+ optional(seq('if', field('guard', $.expression))),
568
+ '=>',
569
+ field('value', choice(
570
+ seq($.block, optional(',')),
571
+ seq($.expression, optional(',')),
572
+ )),
573
+ )),
574
+
575
+
576
+
577
+ break_statement: $ => seq('break', optional($.label), optional($.expression), ';'),
578
+
579
+ continue_statement: $ => seq('continue', optional($.label), ';'),
580
+
581
+ return_statement: $ => seq('return', optional($.expression), ';'),
582
+
583
+ defer_statement: $ => seq('defer', $.expression, ';'),
584
+
585
+ expression_statement: $ => seq($.expression, optional(';')),
586
+
587
+ if_statement: $ => $.if_expression,
588
+ match_statement: $ => $.match_expression,
589
+
590
+ block_statement: $ => $.block,
591
+
592
+ label: $ => /@[a-zA-Z_][a-zA-Z0-9_]*/,
593
+
594
+ // ---- Expressions ----
595
+
596
+ expression: $ => choice(
597
+ $.identifier,
598
+ $.self,
599
+ $._literal,
600
+ $.interpolated_string,
601
+ $.unary_expression,
602
+ $.binary_expression,
603
+ $.call_expression,
604
+ $.method_call_expression,
605
+ $.field_expression,
606
+ $.index_expression,
607
+ $.try_expression,
608
+ $.await_expression,
609
+ $.struct_init,
610
+ $.array_expression,
611
+ $.array_repeat,
612
+ $.tuple_expression,
613
+ $.parenthesized_expression,
614
+ $.if_expression,
615
+ $.match_expression,
616
+ $.lambda,
617
+ $.spawn_expression,
618
+ $.select_expression,
619
+ $.join_expression,
620
+ $.scope_expression,
621
+ $.cooperate_expression,
622
+ $.yield_expression,
623
+ $.path_expression,
624
+ $.unsafe_expression,
625
+ ),
626
+
627
+ unary_expression: $ => prec(PREC.UNARY, seq(
628
+ field('operator', choice('!', '-', '~')),
629
+ field('operand', $.expression),
630
+ )),
631
+
632
+ binary_expression: $ => choice(
633
+ prec.right(PREC.SEND, seq($.expression, '<-', $.expression)),
634
+ prec.left(PREC.OR, seq($.expression, '||', $.expression)),
635
+ prec.left(PREC.BIT_OR, seq($.expression, '|', $.expression)),
636
+ prec.left(PREC.BIT_XOR, seq($.expression, '^', $.expression)),
637
+ prec.left(PREC.AND, seq($.expression, '&&', $.expression)),
638
+ prec.left(PREC.BIT_AND, seq($.expression, '&', $.expression)),
639
+ prec.left(PREC.EQ, seq($.expression, choice('==', '!=', '=~', '!~'), $.expression)),
640
+ prec.left(PREC.REL, seq($.expression, choice('<', '<=', '>', '>='), $.expression)),
641
+ prec.right(PREC.RANGE, seq($.expression, choice('..', '..='), $.expression)),
642
+ prec.left(PREC.SHIFT, seq($.expression, choice('<<', '>>'), $.expression)),
643
+ prec.left(PREC.ADD, seq($.expression, choice('+', '-'), $.expression)),
644
+ prec.left(PREC.MUL, seq($.expression, choice('*', '/', '%'), $.expression)),
645
+ ),
646
+
647
+ call_expression: $ => prec(PREC.POSTFIX, seq(
648
+ field('function', $.expression),
649
+ '(',
650
+ optional(sep1($.call_argument, ',')),
651
+ optional(','),
652
+ ')',
653
+ )),
654
+
655
+ call_argument: $ => choice(
656
+ seq(field('name', $.identifier), ':', field('value', $.expression)),
657
+ $.expression,
658
+ ),
659
+
660
+ method_call_expression: $ => prec(PREC.POSTFIX, seq(
661
+ field('object', $.expression),
662
+ '.',
663
+ field('method', $.identifier),
664
+ '(',
665
+ optional(sep1($.expression, ',')),
666
+ optional(','),
667
+ ')',
668
+ )),
669
+
670
+ field_expression: $ => prec(PREC.FIELD, seq(
671
+ field('object', $.expression),
672
+ '.',
673
+ field('field', choice($.identifier, $.integer_literal)),
674
+ )),
675
+
676
+ index_expression: $ => prec(PREC.POSTFIX, seq(
677
+ field('object', $.expression),
678
+ '[',
679
+ field('index', $.expression),
680
+ ']',
681
+ )),
682
+
683
+ try_expression: $ => prec(PREC.POSTFIX, seq(
684
+ $.expression,
685
+ '?',
686
+ )),
687
+
688
+ await_expression: $ => prec(PREC.UNARY, seq(
689
+ 'await',
690
+ $.expression,
691
+ )),
692
+
693
+ struct_init: $ => prec.dynamic(1, seq(
694
+ field('name', $.identifier),
695
+ '{',
696
+ sep1($.field_initializer, ','),
697
+ optional(','),
698
+ '}',
699
+ )),
700
+
701
+ field_initializer: $ => seq(
702
+ field('name', $.identifier),
703
+ ':',
704
+ field('value', $.expression),
705
+ ),
706
+
707
+ array_expression: $ => seq(
708
+ '[',
709
+ optional(seq(sep1($.expression, ','), optional(','))),
710
+ ']',
711
+ ),
712
+
713
+ array_repeat: $ => seq('[', $.expression, ';', $.expression, ']'),
714
+
715
+ tuple_expression: $ => seq('(', $.expression, ',', optional(sep1($.expression, ',')), ')'),
716
+
717
+ parenthesized_expression: $ => seq('(', $.expression, ')'),
718
+
719
+ if_expression: $ => prec.right(seq(
720
+ 'if',
721
+ field('condition', $.expression),
722
+ field('consequence', $.block),
723
+ optional(field('alternative', $.else_clause)),
724
+ )),
725
+
726
+ else_clause: $ => seq('else', choice($.if_expression, $.block)),
727
+
728
+ match_expression: $ => seq(
729
+ 'match',
730
+ field('value', $.expression),
731
+ '{',
732
+ repeat($.match_arm),
733
+ '}',
734
+ ),
735
+
736
+ lambda: $ => prec(2, seq(
737
+ optional('move'),
738
+ '(',
739
+ optional(sep1($.lambda_parameter, ',')),
740
+ ')',
741
+ optional($.return_type),
742
+ '=>',
743
+ choice($.expression, $.block),
744
+ )),
745
+
746
+ lambda_parameter: $ => seq(
747
+ field('name', $.identifier),
748
+ optional(seq(':', field('type', $._type))),
749
+ ),
750
+
751
+ spawn_expression: $ => seq(
752
+ 'spawn',
753
+ choice(
754
+ $.actor_spawn,
755
+ $.lambda_actor,
756
+ ),
757
+ ),
758
+
759
+ actor_spawn: $ => seq(
760
+ field('actor', $.identifier),
761
+ optional($.type_arguments),
762
+ '(',
763
+ optional(sep1($.call_argument, ',')),
764
+ ')',
765
+ ),
766
+
767
+ lambda_actor: $ => prec(2, seq(
768
+ optional('move'),
769
+ '(',
770
+ optional(sep1($.lambda_parameter, ',')),
771
+ ')',
772
+ optional($.return_type),
773
+ '=>',
774
+ choice($.expression, $.block),
775
+ )),
776
+
777
+ select_expression: $ => seq(
778
+ 'select',
779
+ '{',
780
+ repeat($.select_arm),
781
+ '}',
782
+ ),
783
+
784
+ select_arm: $ => choice(
785
+ seq($.identifier, 'from', $.expression, '=>', $.expression, ','),
786
+ seq('after', $.expression, '=>', $.expression, ','),
787
+ ),
788
+
789
+ join_expression: $ => seq(
790
+ 'join',
791
+ '{',
792
+ sep1($.expression, ','),
793
+ optional(','),
794
+ '}',
795
+ ),
796
+
797
+ loop_statement: $ => prec(10, seq(
798
+ optional(seq($.label, ':')),
799
+ 'loop',
800
+ $.block,
801
+ )),
802
+
803
+ for_statement: $ => prec(10, seq(
804
+ optional(seq($.label, ':')),
805
+ 'for',
806
+ optional('await'),
807
+ field('pattern', $.pattern),
808
+ 'in',
809
+ field('iterable', $.expression),
810
+ field('body', $.block),
811
+ )),
812
+
813
+ while_statement: $ => prec(10, seq(
814
+ optional(seq($.label, ':')),
815
+ 'while',
816
+ field('condition', $.expression),
817
+ field('body', $.block),
818
+ )),
819
+
820
+ scope_expression: $ => seq(
821
+ 'scope',
822
+ optional(seq('|', field('binding', $.identifier), '|')),
823
+ $.block,
824
+ ),
825
+ cooperate_expression: $ => 'cooperate',
826
+ yield_expression: $ => seq('yield', $.expression),
827
+
828
+ unsafe_expression: $ => seq('unsafe', $.block),
829
+
830
+ path_expression: $ => seq($.identifier, '::', $.identifier),
831
+
832
+
833
+
834
+ // ---- Patterns ----
835
+
836
+ pattern: $ => choice(
837
+ '_',
838
+ $.identifier,
839
+ $.path_expression,
840
+ $._literal,
841
+ $.tuple_pattern,
842
+ $.struct_pattern,
843
+ $.constructor_pattern,
844
+ $.or_pattern,
845
+ ),
846
+
847
+ tuple_pattern: $ => seq('(', sep1($.pattern, ','), ')'),
848
+
849
+ struct_pattern: $ => seq(
850
+ field('name', $.identifier),
851
+ '{',
852
+ optional(seq(sep1($.pattern_field, ','), optional(','))),
853
+ '}',
854
+ ),
855
+
856
+ pattern_field: $ => seq(
857
+ field('name', $.identifier),
858
+ optional(seq(':', field('pattern', $.pattern))),
859
+ ),
860
+
861
+ constructor_pattern: $ => seq(
862
+ field('name', choice($.identifier, $.path_expression)),
863
+ '(',
864
+ optional(sep1($.pattern, ',')),
865
+ ')',
866
+ ),
867
+
868
+ or_pattern: $ => prec.left(seq($.pattern, '|', $.pattern)),
869
+
870
+ // ---- Literals ----
871
+
872
+ _literal: $ => choice(
873
+ $.duration_literal,
874
+ $.integer_literal,
875
+ $.float_literal,
876
+ $.string_literal,
877
+ $.raw_string_literal,
878
+ $.regex_literal,
879
+ $.boolean_literal,
880
+ $.none_literal,
881
+ ),
882
+
883
+ integer_literal: $ => token(choice(
884
+ /0[xX][0-9a-fA-F][0-9a-fA-F_]*/,
885
+ /0[bB][01][01_]*/,
886
+ /0[oO][0-7][0-7_]*/,
887
+ /[0-9][0-9_]*/,
888
+ )),
889
+
890
+ float_literal: $ => token(
891
+ /[0-9][0-9_]*\.[0-9][0-9_]*([eE][+-]?[0-9][0-9_]*)?/,
892
+ ),
893
+
894
+ string_literal: $ => seq(
895
+ '"',
896
+ repeat(choice(
897
+ $.string_content,
898
+ $.escape_sequence,
899
+ )),
900
+ '"',
901
+ ),
902
+
903
+ string_content: $ => token.immediate(prec(1, /[^"\\]+/)),
904
+
905
+ escape_sequence: $ => token.immediate(seq(
906
+ '\\',
907
+ choice(
908
+ /[nrt\\"0]/,
909
+ /x[0-9a-fA-F]{2}/,
910
+ ),
911
+ )),
912
+
913
+ interpolated_string: $ => seq(
914
+ $.fstring_start,
915
+ repeat(choice(
916
+ alias($.interpolated_string_content, $.string_content),
917
+ $.escape_sequence,
918
+ $.interpolation,
919
+ )),
920
+ '"',
921
+ ),
922
+
923
+ fstring_start: $ => token(prec(2, /f"/)),
924
+
925
+ interpolated_string_content: $ => token.immediate(prec(1, /[^"\{]+/)),
926
+
927
+ interpolation: $ => seq(
928
+ token.immediate('{'),
929
+ $.expression,
930
+ '}',
931
+ ),
932
+
933
+ raw_string_literal: $ => token(seq(
934
+ 'r"',
935
+ /[^"]*/,
936
+ '"',
937
+ )),
938
+
939
+ regex_literal: $ => token(seq(
940
+ 're"',
941
+ /[^"]*/,
942
+ '"',
943
+ )),
944
+
945
+ boolean_literal: $ => choice('true', 'false'),
946
+
947
+ none_literal: $ => 'None',
948
+
949
+ self: $ => 'self',
950
+
951
+ // ---- Comments ----
952
+
953
+ line_comment: $ => token(seq('//', /[^\n]*/)),
954
+
955
+ block_comment: $ => token(seq('/*', /[^*]*\*+([^/*][^*]*\*+)*/, '/')),
956
+
957
+ // ---- Identifiers ----
958
+
959
+ identifier: $ => /[a-zA-Z_][a-zA-Z0-9_]*/,
960
+ },
961
+ });
962
+
963
+ /**
964
+ * Comma-separated list with at least one element.
965
+ */
966
+ function sep1(rule, sep) {
967
+ return seq(rule, repeat(seq(sep, rule)));
968
+ }