@konomanoasa/tree-sitter-posix-awk 0.7.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,1730 @@
1
+ const PRECEDENCE = {
2
+ assignment: 1,
3
+ conditional: 2,
4
+ logicalOr: 3,
5
+ logicalAnd: 4,
6
+ membership: 5,
7
+ match: 6,
8
+ comparison: 7,
9
+ concatenation: 8,
10
+ additive: 9,
11
+ multiplicative: 10,
12
+ unary: 11,
13
+ exponentiation: 12,
14
+ prefixUpdate: 13,
15
+ postfixUpdate: 14,
16
+ field: 15,
17
+ };
18
+
19
+ const withLineContinuations = ($, ...members) =>
20
+ seq(
21
+ members[0],
22
+ ...members
23
+ .slice(1)
24
+ .flatMap((member) => [repeat($.line_continuation), member]),
25
+ );
26
+
27
+ const optionalNewlineLayout = ($) =>
28
+ seq(
29
+ repeat($.line_continuation),
30
+ optional(seq($.newline_opt, repeat($.line_continuation))),
31
+ );
32
+
33
+ const statementEndLayout = ($) =>
34
+ optional(seq(repeat($.line_continuation), $.newline_opt));
35
+
36
+ const actionBody = ($, statements) =>
37
+ seq(field("body", statements), repeat($.line_continuation));
38
+
39
+ const continuedMember = ($, marker, member) =>
40
+ choice(member, seq(marker, repeat1($.line_continuation), member));
41
+
42
+ const continuedOperatorWith = ($, marker, operator) =>
43
+ continuedMember($, marker, field("operator", operator));
44
+
45
+ const continuedOperator = ($, operator) =>
46
+ continuedOperatorWith($, $._lc_before_operator, operator);
47
+
48
+ const continuedExpressionMember = ($, member) =>
49
+ choice(member, seq($._continued_expression_layout, member));
50
+
51
+ const continuedExpression = ($, name, expression) =>
52
+ continuedExpressionMember($, field(name, expression));
53
+
54
+ const requiredAfterOptionalNewline = (
55
+ $,
56
+ targetGuard,
57
+ present,
58
+ required = present,
59
+ ) =>
60
+ choice(
61
+ seq(repeat($.line_continuation), targetGuard, $.newline_opt, present),
62
+ required,
63
+ );
64
+
65
+ const directFunctionBody = ($) =>
66
+ seq(repeat($.line_continuation), field("body", $.action));
67
+
68
+ const newlineFunctionBody = ($) =>
69
+ seq(
70
+ repeat($.line_continuation),
71
+ $._action_target_guard,
72
+ $.newline_opt,
73
+ repeat($.line_continuation),
74
+ field("body", $.action),
75
+ );
76
+
77
+ const functionBody = ($) =>
78
+ choice(directFunctionBody($), newlineFunctionBody($));
79
+
80
+ const requiredParameter = ($) =>
81
+ requiredAfterOptionalNewline(
82
+ $,
83
+ $._parameter_target_guard,
84
+ continuedExpressionMember($, $.name),
85
+ );
86
+
87
+ const continuedParameter = ($) =>
88
+ seq(continuedMember($, $._lc_before_comma, ","), requiredParameter($));
89
+
90
+ const ereEscapeWithCharacter = ($, character) =>
91
+ seq($._ere_escape_start, $._escape_introducer, character);
92
+
93
+ const ereBracketListAlternatives = ($, followList) => [
94
+ followList,
95
+ prec.dynamic(2, seq(followList, $._ere_bracket_hyphen)),
96
+ ];
97
+
98
+ const ereRangeExpressionWith = ($, startRange) =>
99
+ prec.dynamic(
100
+ 3,
101
+ choice(
102
+ seq(startRange, $.end_range),
103
+ seq(startRange, $._ere_bracket_hyphen),
104
+ ),
105
+ );
106
+
107
+ const ereCompoundOpening = ($, punctuation) =>
108
+ seq(
109
+ $._ere_compound_open_guard,
110
+ token.immediate("["),
111
+ token.immediate(punctuation),
112
+ );
113
+
114
+ const ereCompoundClosing = (guard, punctuation) =>
115
+ seq(guard, token.immediate(punctuation), token.immediate("]"));
116
+
117
+ const ereRequiredPayload = ($, payload, closing) =>
118
+ choice($._ere_lexical_end, seq(payload, choice(closing, $._ere_lexical_end)));
119
+
120
+ const ereCompound = ($, opening, payload, closing) =>
121
+ prec(3, seq(opening, ereRequiredPayload($, payload, closing)));
122
+
123
+ const header = ($, keyword, rest) =>
124
+ seq(keyword, rest, optionalNewlineLayout($));
125
+
126
+ const conditionalHeader = ($, keyword) =>
127
+ header($, keyword, continuedExpressionMember($, $._parenthesized_condition));
128
+
129
+ const keywordHeader = ($, keyword) => seq(keyword, optionalNewlineLayout($));
130
+
131
+ const doWhileTail = ($) =>
132
+ seq(
133
+ $.while_keyword,
134
+ continuedExpressionMember($, $._parenthesized_condition),
135
+ );
136
+
137
+ const continuedDoTail = ($) =>
138
+ continuedMember($, $._lc_before_do_tail, doWhileTail($));
139
+
140
+ const controlStatements = ($, body) => [
141
+ seq($._if_header, field("consequence", body)),
142
+ seq(
143
+ $._if_header,
144
+ field("consequence", $.terminated_statement),
145
+ continuedMember($, $._lc_before_else, $._else_header),
146
+ field("alternative", body),
147
+ ),
148
+ seq($._while_header, field("body", body)),
149
+ seq($._for_header, field("body", body)),
150
+ ];
151
+
152
+ const actionBoundaryControlBody = ($) =>
153
+ alias($._action_body_boundary_control, $.unterminated_statement);
154
+
155
+ const statementTerminatedBy = ($, terminator) =>
156
+ seq(
157
+ field("statement", $.terminatable_statement),
158
+ repeat($.line_continuation),
159
+ field("terminator", terminator),
160
+ statementEndLayout($),
161
+ );
162
+
163
+ const parenthesizedPrintStatement = ($, keyword) =>
164
+ seq(
165
+ keyword,
166
+ continuedExpressionMember($, "("),
167
+ repeat($.line_continuation),
168
+ field("arguments", $.multiple_expr_list),
169
+ $._continued_close_parenthesis,
170
+ );
171
+
172
+ // The call parenthesis has its own continuation marker: after a bare
173
+ // builtin_func_name the parser must decide between the call and the
174
+ // name-as-atom concatenation at the marker itself, one token before the
175
+ // parenthesis would disambiguate them.
176
+ const callArguments = ($) =>
177
+ seq(
178
+ continuedMember($, $._lc_before_call_parenthesis, "("),
179
+ choice(
180
+ $._continued_close_parenthesis,
181
+ seq(
182
+ repeat($.line_continuation),
183
+ $.expr_list,
184
+ $._continued_close_parenthesis,
185
+ ),
186
+ ),
187
+ );
188
+
189
+ const subscriptedName = ($, subscripts) =>
190
+ seq(
191
+ $.name,
192
+ continuedMember($, $._lc_before_open_bracket, "["),
193
+ repeat($.line_continuation),
194
+ subscripts,
195
+ $._continued_close_bracket,
196
+ );
197
+
198
+ const terminatedItemWith = ($, item, terminator) =>
199
+ withLineContinuations(
200
+ $,
201
+ field("item", item),
202
+ field("terminator", terminator),
203
+ );
204
+
205
+ const boundaryTerminatedItem = ($) =>
206
+ choice(
207
+ withLineContinuations(
208
+ $,
209
+ field("item", alias($._closed_item, $.item)),
210
+ $._closed_item_boundary,
211
+ ),
212
+ withLineContinuations(
213
+ $,
214
+ field("item", alias($._normal_pattern_item, $.item)),
215
+ $._normal_pattern_item_boundary,
216
+ ),
217
+ );
218
+
219
+ const terminatedStatements = ($) =>
220
+ seq(
221
+ $.terminated_statement,
222
+ repeat(seq(repeat($.line_continuation), $.terminated_statement)),
223
+ );
224
+
225
+ const statementListWithTail = ($, tail) =>
226
+ choice(tail, withLineContinuations($, terminatedStatements($), tail));
227
+
228
+ const actionBoundaryBody = ($) =>
229
+ actionBody(
230
+ $,
231
+ alias(
232
+ statementListWithTail($, actionBoundaryControlBody($)),
233
+ $.unterminated_statement_list,
234
+ ),
235
+ );
236
+
237
+ const rawNewlines = ($) =>
238
+ seq($.newline, repeat(seq(repeat($.line_continuation), $.newline)));
239
+
240
+ const EXPRESSION_CONTEXT = {
241
+ normal: {
242
+ prefix: "normal",
243
+ expression: "expr",
244
+ unaryExpression: "unary_expr",
245
+ nonUnaryExpression: "non_unary_expr",
246
+ comparison: true,
247
+ input: true,
248
+ },
249
+ print: {
250
+ prefix: "print",
251
+ expression: "print_expr",
252
+ unaryExpression: "unary_print_expr",
253
+ nonUnaryExpression: "non_unary_print_expr",
254
+ comparison: false,
255
+ input: false,
256
+ },
257
+ };
258
+
259
+ const classTierName = (context, classification, tier) =>
260
+ `_${context.prefix}_${classification}_${tier}_expr`;
261
+
262
+ const anyTierName = (context, tier) => `_${context.prefix}_${tier}_expr`;
263
+
264
+ const classTier = ($, context, classification, tier) =>
265
+ $[classTierName(context, classification, tier)];
266
+
267
+ const aliasedClassTier = ($, context, classification, tier) =>
268
+ alias(
269
+ classTier($, context, classification, tier),
270
+ $[
271
+ classification === "unary"
272
+ ? context.unaryExpression
273
+ : context.nonUnaryExpression
274
+ ],
275
+ );
276
+
277
+ const aliasedAnyTier = ($, context, tier) =>
278
+ alias($[anyTierName(context, tier)], $[context.expression]);
279
+
280
+ const expressionTargetGuard = ($, context) =>
281
+ context.input ? $._expression_target_guard : $._print_expression_target_guard;
282
+
283
+ const nonUnaryAtom = ($, context) => {
284
+ const atoms = [
285
+ $._parenthesized_expression,
286
+ $.number,
287
+ $.string,
288
+ $.lvalue,
289
+ $.ere,
290
+ $._user_function_call,
291
+ $._builtin_function_call,
292
+ $.builtin_func_name,
293
+ ];
294
+ if (context.input) {
295
+ atoms.push($.non_unary_input_function);
296
+ }
297
+ return choice(...atoms);
298
+ };
299
+
300
+ const tieredExpressionRules = (context) => {
301
+ const rules = {};
302
+ const unary = (tier) => classTierName(context, "unary", tier);
303
+ const nonUnary = (tier) => classTierName(context, "non_unary", tier);
304
+ const any = (tier) => anyTierName(context, tier);
305
+ const not = `_${context.prefix}_not_expr`;
306
+ const assignmentRight = `_${context.prefix}_assignment_right_expr`;
307
+ const conditionalConsequence = `_${context.prefix}_conditional_consequence_expr`;
308
+ const conditionalAlternative = `_${context.prefix}_conditional_alternative_expr`;
309
+ const conditionalTail = `_${context.prefix}_conditional_tail`;
310
+ const expression = ($) => $[context.expression];
311
+ const requiredTierName = (name, tier) =>
312
+ `_${context.prefix}_required_${name}_${tier}_expr`;
313
+ const requiredTier = ($, name, tier) => $[requiredTierName(name, tier)];
314
+
315
+ const rightTiers = [
316
+ "logical_and",
317
+ "membership",
318
+ ...(context.comparison ? ["comparison"] : []),
319
+ "concatenation",
320
+ "multiplicative",
321
+ "unary",
322
+ ];
323
+ for (const tier of rightTiers) {
324
+ rules[requiredTierName("right", tier)] = ($) =>
325
+ continuedExpression($, "right", aliasedAnyTier($, context, tier));
326
+ }
327
+ rules[requiredTierName("operand", "unary")] = ($) =>
328
+ continuedExpression($, "operand", aliasedAnyTier($, context, "unary"));
329
+ rules[assignmentRight] = ($) =>
330
+ continuedExpression($, "right", expression($));
331
+ rules[conditionalConsequence] = ($) =>
332
+ continuedExpression($, "consequence", expression($));
333
+ rules[conditionalAlternative] = ($) =>
334
+ continuedExpression($, "alternative", expression($));
335
+ rules[conditionalTail] = ($) =>
336
+ seq(
337
+ $._continued_conditional_question,
338
+ $[conditionalConsequence],
339
+ $._continued_conditional_colon,
340
+ $[conditionalAlternative],
341
+ );
342
+
343
+ const addAnyTier = (tier) => {
344
+ rules[any(tier)] = ($) =>
345
+ prec(
346
+ 1,
347
+ choice(
348
+ aliasedClassTier($, context, "unary", tier),
349
+ aliasedClassTier($, context, "non_unary", tier),
350
+ ),
351
+ );
352
+ };
353
+
354
+ const addLeftAssociativeTier = (tier, nextTier, operator, precedence) => {
355
+ addAnyTier(nextTier);
356
+ const tail = `_${context.prefix}_${tier}_tail`;
357
+ rules[tail] = ($) => seq(operator($), requiredTier($, "right", nextTier));
358
+ for (const classification of ["unary", "non_unary"]) {
359
+ rules[classTierName(context, classification, tier)] = ($) =>
360
+ choice(
361
+ classTier($, context, classification, nextTier),
362
+ prec.left(
363
+ precedence,
364
+ seq(
365
+ field("left", aliasedClassTier($, context, classification, tier)),
366
+ $[tail],
367
+ ),
368
+ ),
369
+ );
370
+ }
371
+ };
372
+
373
+ const addNonAssociativeTier = (tier, nextTier, operator, precedence) => {
374
+ addAnyTier(nextTier);
375
+ const tail = `_${context.prefix}_${tier}_tail`;
376
+ rules[tail] = ($) => seq(operator($), requiredTier($, "right", nextTier));
377
+ for (const classification of ["unary", "non_unary"]) {
378
+ rules[classTierName(context, classification, tier)] = ($) =>
379
+ choice(
380
+ classTier($, context, classification, nextTier),
381
+ prec(
382
+ precedence,
383
+ seq(
384
+ field(
385
+ "left",
386
+ aliasedClassTier($, context, classification, nextTier),
387
+ ),
388
+ $[tail],
389
+ ),
390
+ ),
391
+ );
392
+ }
393
+ };
394
+
395
+ rules[unary("assignment")] = ($) =>
396
+ classTier($, context, "unary", "conditional");
397
+ rules[nonUnary("assignment")] = ($) =>
398
+ choice(
399
+ classTier($, context, "non_unary", "conditional"),
400
+ prec.right(
401
+ PRECEDENCE.assignment,
402
+ seq(
403
+ field("left", $.lvalue),
404
+ continuedOperator(
405
+ $,
406
+ choice(
407
+ $.pow_assign,
408
+ $.mod_assign,
409
+ $.mul_assign,
410
+ $.div_assign,
411
+ $.add_assign,
412
+ $.sub_assign,
413
+ "=",
414
+ ),
415
+ ),
416
+ $[assignmentRight],
417
+ ),
418
+ ),
419
+ );
420
+
421
+ for (const classification of ["unary", "non_unary"]) {
422
+ rules[classTierName(context, classification, "conditional")] = ($) =>
423
+ choice(
424
+ classTier($, context, classification, "logical_or"),
425
+ prec.right(
426
+ PRECEDENCE.conditional,
427
+ seq(
428
+ field(
429
+ "condition",
430
+ aliasedClassTier($, context, classification, "logical_or"),
431
+ ),
432
+ $[conditionalTail],
433
+ ),
434
+ ),
435
+ );
436
+ }
437
+
438
+ const addNewlineContinuedTier = (tier, nextTier, operator, precedence) => {
439
+ addAnyTier(nextTier);
440
+ const tail = `_${context.prefix}_${tier}_tail`;
441
+ rules[tail] = ($) =>
442
+ seq(
443
+ operator($),
444
+ requiredAfterOptionalNewline(
445
+ $,
446
+ expressionTargetGuard($, context),
447
+ continuedExpression($, "right", aliasedAnyTier($, context, nextTier)),
448
+ requiredTier($, "right", nextTier),
449
+ ),
450
+ );
451
+ for (const classification of ["unary", "non_unary"]) {
452
+ rules[classTierName(context, classification, tier)] = ($) =>
453
+ choice(
454
+ classTier($, context, classification, nextTier),
455
+ prec.left(
456
+ precedence,
457
+ seq(
458
+ field("left", aliasedClassTier($, context, classification, tier)),
459
+ $[tail],
460
+ ),
461
+ ),
462
+ );
463
+ }
464
+ };
465
+
466
+ addNewlineContinuedTier(
467
+ "logical_or",
468
+ "logical_and",
469
+ ($) => $._continued_logical_or_operator,
470
+ PRECEDENCE.logicalOr,
471
+ );
472
+
473
+ addNewlineContinuedTier(
474
+ "logical_and",
475
+ "membership",
476
+ ($) => $._continued_logical_and_operator,
477
+ PRECEDENCE.logicalAnd,
478
+ );
479
+
480
+ const membershipTail = `_${context.prefix}_membership_tail`;
481
+ rules[membershipTail] = ($) =>
482
+ seq(
483
+ $._continued_membership_operator,
484
+ continuedExpression($, "right", $.name),
485
+ );
486
+ for (const classification of ["unary", "non_unary"]) {
487
+ rules[classTierName(context, classification, "membership")] = ($) => {
488
+ const members = [
489
+ classTier($, context, classification, "match"),
490
+ prec.left(
491
+ PRECEDENCE.membership,
492
+ seq(
493
+ field(
494
+ "left",
495
+ aliasedClassTier($, context, classification, "membership"),
496
+ ),
497
+ $[membershipTail],
498
+ ),
499
+ ),
500
+ ];
501
+ if (classification === "non_unary") {
502
+ members.push(
503
+ prec(
504
+ PRECEDENCE.membership,
505
+ seq(
506
+ "(",
507
+ repeat($.line_continuation),
508
+ field("left", $.multiple_expr_list),
509
+ $._continued_close_parenthesis,
510
+ $._continued_membership_operator,
511
+ continuedExpression($, "right", $.name),
512
+ ),
513
+ ),
514
+ );
515
+ }
516
+ return choice(...members);
517
+ };
518
+ }
519
+
520
+ const comparisonTier = context.comparison ? "comparison" : "concatenation";
521
+ addNonAssociativeTier(
522
+ "match",
523
+ comparisonTier,
524
+ ($) => $._continued_match_operator,
525
+ PRECEDENCE.match,
526
+ );
527
+
528
+ if (context.comparison) {
529
+ addNonAssociativeTier(
530
+ "comparison",
531
+ "concatenation",
532
+ ($) => $._continued_comparison_operator,
533
+ PRECEDENCE.comparison,
534
+ );
535
+ }
536
+
537
+ for (const classification of ["unary", "non_unary"]) {
538
+ rules[classTierName(context, classification, "concatenation")] = ($) =>
539
+ choice(
540
+ classTier($, context, classification, "additive"),
541
+ prec.left(
542
+ PRECEDENCE.concatenation,
543
+ seq(
544
+ field(
545
+ "left",
546
+ aliasedClassTier($, context, classification, "concatenation"),
547
+ ),
548
+ continuedExpressionMember(
549
+ $,
550
+ field(
551
+ "right",
552
+ aliasedClassTier($, context, "non_unary", "additive"),
553
+ ),
554
+ ),
555
+ ),
556
+ ),
557
+ );
558
+ }
559
+
560
+ addLeftAssociativeTier(
561
+ "additive",
562
+ "multiplicative",
563
+ ($) => $._continued_additive_operator,
564
+ PRECEDENCE.additive,
565
+ );
566
+
567
+ addLeftAssociativeTier(
568
+ "multiplicative",
569
+ "unary",
570
+ ($) => $._continued_multiplicative_operator,
571
+ PRECEDENCE.multiplicative,
572
+ );
573
+
574
+ const unaryAlternatives = ($) => {
575
+ const alternatives = [];
576
+ if (context.input) {
577
+ alternatives.push(classTier($, context, "unary", "exponentiation"));
578
+ }
579
+ alternatives.push(
580
+ ...["+", "-"].map((operator) =>
581
+ prec.right(
582
+ PRECEDENCE.unary,
583
+ seq(field("operator", operator), requiredTier($, "operand", "unary")),
584
+ ),
585
+ ),
586
+ );
587
+ return alternatives;
588
+ };
589
+ rules[unary("unary")] = ($) => choice(...unaryAlternatives($));
590
+ rules[not] = ($) =>
591
+ prec.right(
592
+ PRECEDENCE.unary,
593
+ seq(field("operator", "!"), requiredTier($, "operand", "unary")),
594
+ );
595
+ rules[nonUnary("unary")] = ($) =>
596
+ choice(classTier($, context, "non_unary", "exponentiation"), $[not]);
597
+
598
+ const exponentiationClassifications = context.input
599
+ ? ["unary", "non_unary"]
600
+ : ["non_unary"];
601
+ const exponentiationTail = `_${context.prefix}_exponentiation_tail`;
602
+ rules[exponentiationTail] = ($) =>
603
+ seq(
604
+ $._continued_exponentiation_operator,
605
+ requiredTier($, "right", "unary"),
606
+ );
607
+ for (const classification of exponentiationClassifications) {
608
+ rules[classTierName(context, classification, "exponentiation")] = ($) =>
609
+ choice(
610
+ classTier($, context, classification, "update"),
611
+ prec.right(
612
+ PRECEDENCE.exponentiation,
613
+ seq(
614
+ field(
615
+ "left",
616
+ aliasedClassTier($, context, classification, "update"),
617
+ ),
618
+ $[exponentiationTail],
619
+ ),
620
+ ),
621
+ );
622
+ }
623
+ if (context.input) {
624
+ rules[unary("update")] = ($) => $.unary_input_function;
625
+ }
626
+
627
+ rules[nonUnary("update")] = ($) =>
628
+ choice(
629
+ classTier($, context, "non_unary", "atom"),
630
+ $._prefix_update_expr,
631
+ prec.left(
632
+ PRECEDENCE.postfixUpdate,
633
+ seq(
634
+ field("operand", $.lvalue),
635
+ continuedOperator($, choice($.incr, $.decr)),
636
+ ),
637
+ ),
638
+ );
639
+
640
+ rules[nonUnary("atom")] = ($) => nonUnaryAtom($, context);
641
+
642
+ return rules;
643
+ };
644
+
645
+ const normalExpressionRules = tieredExpressionRules(EXPRESSION_CONTEXT.normal);
646
+ const printExpressionRules = tieredExpressionRules(EXPRESSION_CONTEXT.print);
647
+
648
+ const continuedListElementWith = ($, targetGuard, element) =>
649
+ seq(
650
+ continuedMember($, $._lc_before_comma, ","),
651
+ requiredAfterOptionalNewline(
652
+ $,
653
+ targetGuard,
654
+ continuedExpressionMember($, element),
655
+ ),
656
+ );
657
+
658
+ const continuedListElement = ($, element) =>
659
+ continuedListElementWith($, $._expression_target_guard, element);
660
+
661
+ const continuedPipeGet = ($) =>
662
+ continuedExpressionMember($, field("get", $.simple_get));
663
+
664
+ const continuedPrintListElement = ($, element) =>
665
+ continuedListElementWith($, $._print_expression_target_guard, element);
666
+
667
+ module.exports = grammar({
668
+ name: "posix_awk",
669
+
670
+ externals: ($) => [
671
+ $._begin_word,
672
+ $._end_word,
673
+ $._function_word,
674
+ $._print_word,
675
+ $._break_word,
676
+ $._continue_word,
677
+ $._delete_word,
678
+ $._do_word,
679
+ $._else_word,
680
+ $._exit_word,
681
+ $._for_word,
682
+ $._if_word,
683
+ $._next_word,
684
+ $._nextfile_word,
685
+ $._printf_word,
686
+ $._return_word,
687
+ $._while_word,
688
+ $._name_word,
689
+ $._getline_word,
690
+ $._in_word,
691
+ $._builtin_func_name_word,
692
+ $._func_name_word,
693
+ $._number_integer,
694
+ $._number_fraction,
695
+ $._number_exponent,
696
+ $._division_slash,
697
+ $._ere_opening_slash,
698
+ $._div_assign_operator,
699
+ $._add_assign_operator,
700
+ $._sub_assign_operator,
701
+ $._mul_assign_operator,
702
+ $._mod_assign_operator,
703
+ $._pow_assign_operator,
704
+ $._or_operator,
705
+ $._and_operator,
706
+ $._no_match_operator,
707
+ $._eq_operator,
708
+ $._le_operator,
709
+ $._ge_operator,
710
+ $._ne_operator,
711
+ $._incr_operator,
712
+ $._decr_operator,
713
+ $._append_operator,
714
+ $._output_greater_guard,
715
+ $._lc_before_operator,
716
+ $._lc_before_additive_operator,
717
+ $._lc_before_multiplicative_operator,
718
+ $._lc_before_exponentiation_operator,
719
+ $._lc_before_comparison_operator,
720
+ $._lc_before_match_operator,
721
+ $._lc_before_membership_operator,
722
+ $._lc_before_logical_and_operator,
723
+ $._lc_before_logical_or_operator,
724
+ $._lc_before_conditional_question,
725
+ $._lc_before_conditional_colon,
726
+ $._lc_before_less_than,
727
+ $._lc_before_input_pipe,
728
+ $._lc_before_output_redirection,
729
+ $._lc_before_else,
730
+ $._lc_before_do_tail,
731
+ $._lc_before_for_semicolon,
732
+ $._lc_before_for_update,
733
+ $._lc_before_expression,
734
+ $._lc_before_comma,
735
+ $._lc_before_open_bracket,
736
+ $._lc_before_call_parenthesis,
737
+ $._lc_before_close_parenthesis,
738
+ $._lc_before_close_bracket,
739
+ $._closed_item_boundary,
740
+ $._normal_pattern_item_boundary,
741
+ $._ere_compound_open_guard,
742
+ $._ere_dot_close_guard,
743
+ $._ere_equal_close_guard,
744
+ $._ere_colon_close_guard,
745
+ $._ere_escape_start,
746
+ $._ere_escaped_delimiter_start,
747
+ $._ere_escaped_delimiter_end,
748
+ $._ere_lexical_end,
749
+ $._ere_closing,
750
+ $._expression_target_guard,
751
+ $._print_expression_target_guard,
752
+ $._action_target_guard,
753
+ $._parameter_target_guard,
754
+ $._error_sentinel,
755
+ ],
756
+
757
+ extras: ($) => [token(repeat1(choice(" ", "\t"))), $.comment],
758
+
759
+ inline: ($) => [
760
+ $._normal_unary_assignment_expr,
761
+ $._normal_unary_update_expr,
762
+ $._print_unary_assignment_expr,
763
+ ],
764
+
765
+ conflicts: ($) => [
766
+ [$.item_list, $._terminated_items],
767
+ [$._terminated_item, $.item],
768
+ [$._normal_pattern_item, $.pattern],
769
+ [$.item_list],
770
+ [$.newline_opt],
771
+ [$.terminator],
772
+ [$.terminated_statement, $.unterminated_statement],
773
+ [$.action, $.terminated_statement_list, $.unterminated_statement_list],
774
+ [
775
+ $._action_body_boundary_control,
776
+ $._self_terminating_statement,
777
+ $.unterminated_statement,
778
+ ],
779
+ [$.terminated_statement],
780
+ [$._self_terminating_statement],
781
+ [$._if_header],
782
+ [$._while_header],
783
+ [$._for_header],
784
+ [$._for_in_clause, $.lvalue],
785
+ [
786
+ $._normal_non_unary_atom_expr,
787
+ $._normal_non_unary_field_atom_expr,
788
+ $._builtin_function_call,
789
+ ],
790
+ [$.single_expression, $.start_range],
791
+ [$._initial_close_single_expression, $._initial_close_start_range],
792
+ [$.bracket_list, $.collating_element],
793
+ [$.range_expression, $.collating_element],
794
+ [$._initial_close_range_expression, $.collating_element],
795
+ ],
796
+
797
+ rules: {
798
+ program: ($) =>
799
+ choice(
800
+ repeat($.line_continuation),
801
+ seq(
802
+ repeat($.line_continuation),
803
+ choice(
804
+ $.item_list,
805
+ field("item", $.item),
806
+ withLineContinuations($, $.item_list, field("item", $.item)),
807
+ ),
808
+ repeat($.line_continuation),
809
+ ),
810
+ ),
811
+
812
+ _continued_expression_layout: ($) =>
813
+ seq($._lc_before_expression, repeat1($.line_continuation)),
814
+
815
+ _continued_close_parenthesis: ($) =>
816
+ continuedMember($, $._lc_before_close_parenthesis, ")"),
817
+
818
+ _continued_close_bracket: ($) =>
819
+ continuedMember($, $._lc_before_close_bracket, "]"),
820
+
821
+ _continued_additive_operator: ($) =>
822
+ continuedOperatorWith(
823
+ $,
824
+ $._lc_before_additive_operator,
825
+ choice("+", "-"),
826
+ ),
827
+
828
+ _continued_multiplicative_operator: ($) =>
829
+ continuedOperatorWith(
830
+ $,
831
+ $._lc_before_multiplicative_operator,
832
+ choice("*", alias($._division_slash, "/"), "%"),
833
+ ),
834
+
835
+ _continued_exponentiation_operator: ($) =>
836
+ continuedOperatorWith($, $._lc_before_exponentiation_operator, "^"),
837
+
838
+ _continued_comparison_operator: ($) =>
839
+ choice(
840
+ continuedOperatorWith($, $._lc_before_less_than, "<"),
841
+ continuedOperatorWith(
842
+ $,
843
+ $._lc_before_comparison_operator,
844
+ choice($.le, $.ne, $.eq, ">", $.ge),
845
+ ),
846
+ ),
847
+
848
+ _continued_match_operator: ($) =>
849
+ continuedOperatorWith(
850
+ $,
851
+ $._lc_before_match_operator,
852
+ choice("~", $.no_match),
853
+ ),
854
+
855
+ _continued_membership_operator: ($) =>
856
+ continuedOperatorWith($, $._lc_before_membership_operator, $.in_keyword),
857
+
858
+ _continued_logical_and_operator: ($) =>
859
+ continuedOperatorWith($, $._lc_before_logical_and_operator, $.and),
860
+
861
+ _continued_logical_or_operator: ($) =>
862
+ continuedOperatorWith($, $._lc_before_logical_or_operator, $.or),
863
+
864
+ _continued_conditional_question: ($) =>
865
+ continuedMember($, $._lc_before_conditional_question, "?"),
866
+
867
+ _continued_conditional_colon: ($) =>
868
+ continuedMember($, $._lc_before_conditional_colon, ":"),
869
+
870
+ _continued_input_redirect: ($) =>
871
+ continuedMember($, $._lc_before_less_than, "<"),
872
+
873
+ _continued_input_pipe: ($) =>
874
+ continuedMember($, $._lc_before_input_pipe, "|"),
875
+
876
+ item_list: ($) =>
877
+ choice(
878
+ field("leading", $.newline_opt),
879
+ $._terminated_items,
880
+ withLineContinuations(
881
+ $,
882
+ field("leading", $.newline_opt),
883
+ $._terminated_items,
884
+ ),
885
+ ),
886
+
887
+ _terminated_item: ($) =>
888
+ choice(
889
+ terminatedItemWith($, $.item, $.terminator),
890
+ boundaryTerminatedItem($),
891
+ ),
892
+
893
+ _terminated_items: ($) =>
894
+ choice(
895
+ $._terminated_item,
896
+ seq(
897
+ $._terminated_items,
898
+ repeat($.line_continuation),
899
+ $._terminated_item,
900
+ ),
901
+ ),
902
+
903
+ item: ($) => choice($._normal_pattern_item, $._closed_item),
904
+
905
+ _closed_item: ($) =>
906
+ choice($._action_item, $._pattern_action_item, $._function_item),
907
+
908
+ _action_item: ($) => field("action", $.action),
909
+
910
+ _pattern_action_item: ($) =>
911
+ withLineContinuations(
912
+ $,
913
+ field("pattern", $.pattern),
914
+ field("action", $.action),
915
+ ),
916
+
917
+ _normal_pattern_item: ($) => field("pattern", $.normal_pattern),
918
+
919
+ _function_item: ($) => seq($._function_header, functionBody($)),
920
+
921
+ _function_header_prefix: ($) =>
922
+ seq(
923
+ $.function_keyword,
924
+ repeat($.line_continuation),
925
+ field("name", choice($.name, $.func_name)),
926
+ continuedExpressionMember($, "("),
927
+ repeat($.line_continuation),
928
+ ),
929
+
930
+ _function_header: ($) =>
931
+ seq(
932
+ $._function_header_prefix,
933
+ optional(field("parameters", $.param_list)),
934
+ $._continued_close_parenthesis,
935
+ ),
936
+
937
+ param_list: ($) => seq($.name, repeat(continuedParameter($))),
938
+
939
+ pattern: ($) => choice($.normal_pattern, $.special_pattern),
940
+
941
+ normal_pattern: ($) =>
942
+ choice(
943
+ $.expr,
944
+ prec.right(
945
+ 1,
946
+ seq(
947
+ field("left", $.expr),
948
+ continuedMember($, $._lc_before_comma, field("separator", ",")),
949
+ choice(
950
+ requiredAfterOptionalNewline(
951
+ $,
952
+ $._expression_target_guard,
953
+ continuedExpression($, "right", $.expr),
954
+ ),
955
+ // Reachable only during recovery: when the right arm is
956
+ // missing, the action guard keeps an action on the next line
957
+ // inside this item.
958
+ seq(
959
+ repeat($.line_continuation),
960
+ $._action_target_guard,
961
+ $.newline_opt,
962
+ continuedExpression($, "right", $.expr),
963
+ ),
964
+ ),
965
+ ),
966
+ ),
967
+ ),
968
+
969
+ special_pattern: ($) => choice($.begin_keyword, $.end_keyword),
970
+
971
+ begin_keyword: ($) => prec(10, $._begin_word),
972
+
973
+ end_keyword: ($) => prec(10, $._end_word),
974
+
975
+ function_keyword: ($) => prec(10, $._function_word),
976
+
977
+ _action_opening_layout: ($) =>
978
+ seq(field("opening", "{"), optionalNewlineLayout($)),
979
+
980
+ _action_body: ($) =>
981
+ actionBody(
982
+ $,
983
+ choice($.terminated_statement_list, $.unterminated_statement_list),
984
+ ),
985
+
986
+ _action_body_boundary_control: ($) =>
987
+ choice(
988
+ $._if_header,
989
+ $._while_header,
990
+ $._for_header,
991
+ ...controlStatements($, actionBoundaryControlBody($)),
992
+ ),
993
+
994
+ action: ($) =>
995
+ choice(
996
+ seq($._action_opening_layout, field("closing", "}")),
997
+ seq($._action_opening_layout, $._action_body, field("closing", "}")),
998
+ seq(
999
+ $._action_opening_layout,
1000
+ actionBoundaryBody($),
1001
+ field("closing", "}"),
1002
+ ),
1003
+ ),
1004
+
1005
+ terminated_statement_list: ($) => terminatedStatements($),
1006
+
1007
+ unterminated_statement_list: ($) =>
1008
+ statementListWithTail($, $.unterminated_statement),
1009
+
1010
+ _parenthesized_condition: ($) =>
1011
+ seq(
1012
+ "(",
1013
+ continuedExpression($, "condition", $.expr),
1014
+ $._continued_close_parenthesis,
1015
+ ),
1016
+
1017
+ _if_header: ($) => conditionalHeader($, $.if_keyword),
1018
+
1019
+ _else_header: ($) => keywordHeader($, $.else_keyword),
1020
+
1021
+ _while_header: ($) => conditionalHeader($, $.while_keyword),
1022
+
1023
+ _do_header: ($) => keywordHeader($, $.do_keyword),
1024
+
1025
+ _for_classic_clause: ($) =>
1026
+ seq(
1027
+ optional(field("initializer", $.simple_statement)),
1028
+ continuedMember($, $._lc_before_for_semicolon, ";"),
1029
+ repeat($.line_continuation),
1030
+ optional(field("condition", $.expr)),
1031
+ continuedMember($, $._lc_before_for_semicolon, ";"),
1032
+ optional(
1033
+ choice(
1034
+ field("update", $.simple_statement),
1035
+ seq(
1036
+ $._lc_before_for_update,
1037
+ repeat1($.line_continuation),
1038
+ field("update", $.simple_statement),
1039
+ ),
1040
+ ),
1041
+ ),
1042
+ ),
1043
+
1044
+ _for_in_clause: ($) =>
1045
+ seq(
1046
+ field("variable", $.name),
1047
+ continuedMember($, $._lc_before_membership_operator, $.in_keyword),
1048
+ continuedExpressionMember($, field("array", $.name)),
1049
+ ),
1050
+
1051
+ _for_header: ($) =>
1052
+ header(
1053
+ $,
1054
+ $.for_keyword,
1055
+ seq(
1056
+ continuedExpressionMember($, "("),
1057
+ repeat($.line_continuation),
1058
+ choice($._for_classic_clause, $._for_in_clause),
1059
+ $._continued_close_parenthesis,
1060
+ ),
1061
+ ),
1062
+
1063
+ _self_terminating_statement: ($) =>
1064
+ choice(...controlStatements($, $.terminated_statement)),
1065
+
1066
+ terminated_statement: ($) =>
1067
+ choice(
1068
+ seq($.action, statementEndLayout($)),
1069
+ $._self_terminating_statement,
1070
+ seq(field("terminator", ";"), statementEndLayout($)),
1071
+ statementTerminatedBy($, $.newline),
1072
+ statementTerminatedBy($, ";"),
1073
+ ),
1074
+
1075
+ unterminated_statement: ($) =>
1076
+ choice(
1077
+ field("statement", $.terminatable_statement),
1078
+ ...controlStatements($, $.unterminated_statement),
1079
+ ),
1080
+
1081
+ terminatable_statement: ($) =>
1082
+ choice(
1083
+ $.simple_statement,
1084
+ $.break_keyword,
1085
+ $.continue_keyword,
1086
+ $.next_keyword,
1087
+ $.nextfile_keyword,
1088
+ seq($.exit_keyword, optional(continuedExpressionMember($, $.expr))),
1089
+ seq($.return_keyword, optional(continuedExpressionMember($, $.expr))),
1090
+ seq(
1091
+ $._do_header,
1092
+ field("body", $.terminated_statement),
1093
+ continuedDoTail($),
1094
+ ),
1095
+ ),
1096
+
1097
+ simple_statement: ($) =>
1098
+ choice(
1099
+ seq(
1100
+ $.delete_keyword,
1101
+ continuedExpressionMember($, field("array", $.name)),
1102
+ optional(
1103
+ seq(
1104
+ continuedMember($, $._lc_before_open_bracket, "["),
1105
+ repeat($.line_continuation),
1106
+ field("subscripts", $.expr_list),
1107
+ $._continued_close_bracket,
1108
+ ),
1109
+ ),
1110
+ ),
1111
+ $.expr,
1112
+ $.print_statement,
1113
+ ),
1114
+
1115
+ print_statement: ($) =>
1116
+ prec.right(
1117
+ choice(
1118
+ field("statement", $.simple_print_statement),
1119
+ seq(
1120
+ field("statement", $.simple_print_statement),
1121
+ continuedMember(
1122
+ $,
1123
+ $._lc_before_output_redirection,
1124
+ field("redirection", $.output_redirection),
1125
+ ),
1126
+ ),
1127
+ ),
1128
+ ),
1129
+
1130
+ simple_print_statement: ($) =>
1131
+ choice(
1132
+ prec.right(
1133
+ seq(
1134
+ $.print_keyword,
1135
+ optional(
1136
+ continuedExpressionMember(
1137
+ $,
1138
+ field("arguments", $.print_expr_list),
1139
+ ),
1140
+ ),
1141
+ ),
1142
+ ),
1143
+ parenthesizedPrintStatement($, $.print_keyword),
1144
+ prec.right(
1145
+ seq(
1146
+ $.printf_keyword,
1147
+ continuedExpressionMember($, field("arguments", $.print_expr_list)),
1148
+ ),
1149
+ ),
1150
+ parenthesizedPrintStatement($, $.printf_keyword),
1151
+ ),
1152
+
1153
+ output_redirection: ($) =>
1154
+ seq(
1155
+ choice(seq($._output_greater_guard, choice(">", $.append)), "|"),
1156
+ continuedExpressionMember($, $.expr),
1157
+ ),
1158
+
1159
+ print_keyword: ($) => prec(10, $._print_word),
1160
+
1161
+ printf_keyword: ($) => prec(10, $._printf_word),
1162
+
1163
+ break_keyword: ($) => $._break_word,
1164
+
1165
+ continue_keyword: ($) => $._continue_word,
1166
+
1167
+ delete_keyword: ($) => $._delete_word,
1168
+
1169
+ do_keyword: ($) => $._do_word,
1170
+
1171
+ else_keyword: ($) => $._else_word,
1172
+
1173
+ exit_keyword: ($) => $._exit_word,
1174
+
1175
+ for_keyword: ($) => $._for_word,
1176
+
1177
+ if_keyword: ($) => $._if_word,
1178
+
1179
+ next_keyword: ($) => $._next_word,
1180
+
1181
+ nextfile_keyword: ($) => $._nextfile_word,
1182
+
1183
+ return_keyword: ($) => $._return_word,
1184
+
1185
+ while_keyword: ($) => $._while_word,
1186
+
1187
+ print_expr_list: ($) =>
1188
+ prec.left(
1189
+ seq($.print_expr, repeat(continuedPrintListElement($, $.print_expr))),
1190
+ ),
1191
+
1192
+ expr_list: ($) => choice($.expr, $.multiple_expr_list),
1193
+
1194
+ multiple_expr_list: ($) =>
1195
+ prec.left(
1196
+ seq(
1197
+ $.expr,
1198
+ continuedListElement($, $.expr),
1199
+ repeat(continuedListElement($, $.expr)),
1200
+ ),
1201
+ ),
1202
+
1203
+ print_expr: ($) => choice($.unary_print_expr, $.non_unary_print_expr),
1204
+
1205
+ unary_print_expr: ($) => $._print_unary_assignment_expr,
1206
+
1207
+ non_unary_print_expr: ($) => $._print_non_unary_assignment_expr,
1208
+
1209
+ expr: ($) => choice($.unary_expr, $.non_unary_expr),
1210
+
1211
+ unary_expr: ($) => $._normal_unary_assignment_expr,
1212
+
1213
+ non_unary_expr: ($) => $._normal_non_unary_assignment_expr,
1214
+
1215
+ _prefix_update_expr: ($) =>
1216
+ prec.right(
1217
+ PRECEDENCE.prefixUpdate,
1218
+ seq(
1219
+ field("operator", choice($.incr, $.decr)),
1220
+ continuedExpression($, "operand", $.lvalue),
1221
+ ),
1222
+ ),
1223
+
1224
+ ...normalExpressionRules,
1225
+
1226
+ ...printExpressionRules,
1227
+
1228
+ _normal_field_expr: ($) =>
1229
+ choice(
1230
+ alias($._normal_unary_field_expr, $.unary_expr),
1231
+ alias($._normal_non_unary_field_expr, $.non_unary_expr),
1232
+ ),
1233
+
1234
+ _normal_unary_field_expr: ($) =>
1235
+ prec(PRECEDENCE.field, $._normal_unary_unary_expr),
1236
+
1237
+ _normal_non_unary_field_atom_expr: ($) =>
1238
+ prec(PRECEDENCE.field, nonUnaryAtom($, EXPRESSION_CONTEXT.normal)),
1239
+
1240
+ _normal_non_unary_field_expr: ($) =>
1241
+ prec(
1242
+ PRECEDENCE.field,
1243
+ choice(
1244
+ $._normal_non_unary_field_atom_expr,
1245
+ $._normal_not_expr,
1246
+ $._prefix_update_expr,
1247
+ ),
1248
+ ),
1249
+
1250
+ _parenthesized_expression: ($) =>
1251
+ seq(
1252
+ "(",
1253
+ repeat($.line_continuation),
1254
+ $.expr,
1255
+ $._continued_close_parenthesis,
1256
+ ),
1257
+
1258
+ _user_function_call: ($) => seq($.func_name, callArguments($)),
1259
+
1260
+ _builtin_function_call: ($) =>
1261
+ prec.right(PRECEDENCE.field, seq($.builtin_func_name, callArguments($))),
1262
+
1263
+ lvalue: ($) =>
1264
+ choice(
1265
+ $.name,
1266
+ prec.right(PRECEDENCE.field, subscriptedName($, $.expr_list)),
1267
+ prec(
1268
+ PRECEDENCE.field,
1269
+ seq(
1270
+ field("operator", "$"),
1271
+ continuedExpression(
1272
+ $,
1273
+ "operand",
1274
+ alias($._normal_field_expr, $.expr),
1275
+ ),
1276
+ ),
1277
+ ),
1278
+ ),
1279
+
1280
+ non_unary_input_function: ($) =>
1281
+ choice(
1282
+ field("get", $.simple_get),
1283
+ prec.right(
1284
+ PRECEDENCE.field,
1285
+ seq(
1286
+ field("get", $.simple_get),
1287
+ $._continued_input_redirect,
1288
+ continuedExpression($, "source", $.expr),
1289
+ ),
1290
+ ),
1291
+ prec.right(
1292
+ PRECEDENCE.field,
1293
+ seq(
1294
+ field("source", $.non_unary_expr),
1295
+ $._continued_input_pipe,
1296
+ continuedPipeGet($),
1297
+ ),
1298
+ ),
1299
+ ),
1300
+
1301
+ unary_input_function: ($) =>
1302
+ prec.right(
1303
+ PRECEDENCE.field,
1304
+ seq(
1305
+ field("source", $.unary_expr),
1306
+ $._continued_input_pipe,
1307
+ continuedPipeGet($),
1308
+ ),
1309
+ ),
1310
+
1311
+ simple_get: ($) =>
1312
+ prec.right(
1313
+ PRECEDENCE.field,
1314
+ seq(
1315
+ $.getline_keyword,
1316
+ optional(continuedExpressionMember($, field("target", $.lvalue))),
1317
+ ),
1318
+ ),
1319
+
1320
+ getline_keyword: ($) => $._getline_word,
1321
+
1322
+ in_keyword: ($) => $._in_word,
1323
+
1324
+ func_name: ($) => $._func_name_word,
1325
+
1326
+ builtin_func_name: ($) => $._builtin_func_name_word,
1327
+
1328
+ name: ($) => $._name_word,
1329
+
1330
+ number: ($) =>
1331
+ choice($._number_integer, $._number_fraction, $._number_exponent),
1332
+
1333
+ string: ($) =>
1334
+ seq(
1335
+ field("opening", '"'),
1336
+ repeat(choice($.string_content, $.escape_sequence)),
1337
+ field("closing", token.immediate('"')),
1338
+ ),
1339
+
1340
+ string_content: () => token.immediate(prec(1, /[^"\\\n]+/)),
1341
+
1342
+ escape_sequence: ($) =>
1343
+ seq(
1344
+ $._escape_introducer,
1345
+ choice($._escape_character, $._escape_octal_digits),
1346
+ ),
1347
+
1348
+ add_assign: ($) => $._add_assign_operator,
1349
+
1350
+ sub_assign: ($) => $._sub_assign_operator,
1351
+
1352
+ mul_assign: ($) => $._mul_assign_operator,
1353
+
1354
+ div_assign: ($) => $._div_assign_operator,
1355
+
1356
+ mod_assign: ($) => $._mod_assign_operator,
1357
+
1358
+ pow_assign: ($) => $._pow_assign_operator,
1359
+
1360
+ or: ($) => $._or_operator,
1361
+
1362
+ and: ($) => $._and_operator,
1363
+
1364
+ no_match: ($) => $._no_match_operator,
1365
+
1366
+ eq: ($) => $._eq_operator,
1367
+
1368
+ le: ($) => $._le_operator,
1369
+
1370
+ ge: ($) => $._ge_operator,
1371
+
1372
+ ne: ($) => $._ne_operator,
1373
+
1374
+ incr: ($) => $._incr_operator,
1375
+
1376
+ decr: ($) => $._decr_operator,
1377
+
1378
+ append: ($) => $._append_operator,
1379
+
1380
+ ere: ($) =>
1381
+ seq(
1382
+ field("opening", alias($._ere_opening_slash, "/")),
1383
+ choice(
1384
+ seq(
1385
+ field("expression", $.extended_reg_exp),
1386
+ field("closing", alias($._ere_closing, "/")),
1387
+ ),
1388
+ seq(
1389
+ optional(field("expression", $.extended_reg_exp)),
1390
+ $._ere_lexical_end,
1391
+ ),
1392
+ ),
1393
+ ),
1394
+
1395
+ extended_reg_exp: ($) =>
1396
+ choice(
1397
+ $.ere_branch,
1398
+ prec.left(
1399
+ 1,
1400
+ seq(
1401
+ field("left", $.extended_reg_exp),
1402
+ field("operator", token.immediate("|")),
1403
+ field("right", $.ere_branch),
1404
+ ),
1405
+ ),
1406
+ ),
1407
+
1408
+ ere_branch: ($) =>
1409
+ choice(
1410
+ $.ere_expression,
1411
+ prec.left(
1412
+ seq(field("left", $.ere_branch), field("right", $.ere_expression)),
1413
+ ),
1414
+ ),
1415
+
1416
+ ere_expression: ($) =>
1417
+ choice(
1418
+ $.one_char_or_coll_elem_ere,
1419
+ $.left_anchor,
1420
+ $.right_anchor,
1421
+ seq(
1422
+ field("opening", token.immediate("(")),
1423
+ field("expression", $.extended_reg_exp),
1424
+ field("closing", $._ere_close_parenthesis),
1425
+ ),
1426
+ prec.left(
1427
+ 2,
1428
+ seq(
1429
+ field("operand", $.ere_expression),
1430
+ field("operator", $.ere_dupl_symbol),
1431
+ ),
1432
+ ),
1433
+ ),
1434
+
1435
+ one_char_or_coll_elem_ere: ($) =>
1436
+ choice(
1437
+ $.ordinary_character,
1438
+ $.quoted_character,
1439
+ $.wildcard,
1440
+ $.bracket_expression,
1441
+ alias($._ere_octal_escape_sequence, $.escape_sequence),
1442
+ alias($._ere_undefined_escape_sequence, $.escape_sequence),
1443
+ ),
1444
+
1445
+ ordinary_character: ($) =>
1446
+ choice(
1447
+ $._ordinary_character,
1448
+ $._ere_ordinary_close_parenthesis,
1449
+ $._ere_ordinary_close_brace,
1450
+ $.escaped_delimiter,
1451
+ alias($._ere_named_escape_sequence, $.escape_sequence),
1452
+ ),
1453
+
1454
+ quoted_character: ($) =>
1455
+ alias($._ere_quoted_escape_sequence, $.escape_sequence),
1456
+
1457
+ wildcard: () => token.immediate("."),
1458
+
1459
+ left_anchor: () => token.immediate("^"),
1460
+
1461
+ right_anchor: () => token.immediate("$"),
1462
+
1463
+ ere_dupl_symbol: ($) =>
1464
+ prec.right(
1465
+ 3,
1466
+ seq(
1467
+ choice(
1468
+ token.immediate("*"),
1469
+ token.immediate("+"),
1470
+ token.immediate("?"),
1471
+ $._ere_interval,
1472
+ ),
1473
+ optional(field("modifier", $.repetition_modifier)),
1474
+ ),
1475
+ ),
1476
+
1477
+ repetition_modifier: () => token.immediate("?"),
1478
+
1479
+ _ere_interval: ($) =>
1480
+ seq(
1481
+ token.immediate("{"),
1482
+ ereRequiredPayload(
1483
+ $,
1484
+ seq(
1485
+ $.dup_count,
1486
+ optional(seq(token.immediate(","), optional($.dup_count))),
1487
+ ),
1488
+ $._ere_close_brace,
1489
+ ),
1490
+ ),
1491
+
1492
+ dup_count: ($) => $._number_digit_chunk,
1493
+
1494
+ bracket_expression: ($) =>
1495
+ prec(
1496
+ 2,
1497
+ seq(
1498
+ token.immediate("["),
1499
+ choice($.matching_list, $.nonmatching_list),
1500
+ $._ere_close_bracket,
1501
+ ),
1502
+ ),
1503
+
1504
+ matching_list: ($) => $.bracket_list,
1505
+
1506
+ nonmatching_list: ($) => seq(token.immediate(prec(3, "^")), $.bracket_list),
1507
+
1508
+ bracket_list: ($) =>
1509
+ choice(
1510
+ ...ereBracketListAlternatives($, $.follow_list),
1511
+ ...ereBracketListAlternatives(
1512
+ $,
1513
+ alias($._initial_close_follow_list, $.follow_list),
1514
+ ),
1515
+ ),
1516
+
1517
+ _initial_close_follow_list: ($) =>
1518
+ choice(
1519
+ alias($._initial_close_expression_term, $.expression_term),
1520
+ prec.left(
1521
+ seq(
1522
+ alias($._initial_close_follow_list, $.follow_list),
1523
+ $.expression_term,
1524
+ ),
1525
+ ),
1526
+ ),
1527
+
1528
+ _initial_close_expression_term: ($) =>
1529
+ choice(
1530
+ alias($._initial_close_single_expression, $.single_expression),
1531
+ alias($._initial_close_range_expression, $.range_expression),
1532
+ ),
1533
+
1534
+ _initial_close_single_expression: ($) =>
1535
+ alias($._initial_close_end_range, $.end_range),
1536
+
1537
+ _initial_close_range_expression: ($) =>
1538
+ ereRangeExpressionWith(
1539
+ $,
1540
+ alias($._initial_close_start_range, $.start_range),
1541
+ ),
1542
+
1543
+ _initial_close_start_range: ($) =>
1544
+ seq(
1545
+ alias($._initial_close_end_range, $.end_range),
1546
+ $._ere_bracket_hyphen,
1547
+ ),
1548
+
1549
+ _initial_close_end_range: ($) =>
1550
+ alias($._ere_bracket_close_character, $.collating_element),
1551
+
1552
+ follow_list: ($) =>
1553
+ choice(
1554
+ $.expression_term,
1555
+ prec.left(seq($.follow_list, $.expression_term)),
1556
+ ),
1557
+
1558
+ expression_term: ($) => choice($.single_expression, $.range_expression),
1559
+
1560
+ single_expression: ($) =>
1561
+ choice($.end_range, $.character_class, $.equivalence_class),
1562
+
1563
+ range_expression: ($) => ereRangeExpressionWith($, $.start_range),
1564
+
1565
+ start_range: ($) => seq($.end_range, $._ere_bracket_hyphen),
1566
+
1567
+ end_range: ($) => choice($.collating_element, $.collating_symbol),
1568
+
1569
+ collating_element: ($) =>
1570
+ choice(
1571
+ $._ere_bracket_character,
1572
+ $._ere_bracket_open_character,
1573
+ $._ere_bracket_hyphen,
1574
+ $.escaped_delimiter,
1575
+ alias($._ere_bracket_escape_sequence, $.escape_sequence),
1576
+ ),
1577
+
1578
+ collating_symbol: ($) =>
1579
+ ereCompound(
1580
+ $,
1581
+ $._ere_open_dot,
1582
+ choice(
1583
+ alias($._ere_compound_collating_element, $.collating_element),
1584
+ $.meta_character,
1585
+ ),
1586
+ $._ere_dot_close,
1587
+ ),
1588
+
1589
+ equivalence_class: ($) =>
1590
+ ereCompound(
1591
+ $,
1592
+ $._ere_open_equal,
1593
+ choice(
1594
+ alias($._ere_compound_collating_element, $.collating_element),
1595
+ alias($._ere_compound_meta_character, $.collating_element),
1596
+ ),
1597
+ $._ere_equal_close,
1598
+ ),
1599
+
1600
+ character_class: ($) =>
1601
+ ereCompound($, $._ere_open_colon, $.class_name, $._ere_colon_close),
1602
+
1603
+ class_name: ($) => $._ere_class_name_spelling,
1604
+
1605
+ meta_character: ($) => $._ere_compound_meta_character,
1606
+
1607
+ _ere_compound_collating_element: ($) =>
1608
+ choice(
1609
+ $._ere_compound_nonmeta_atom,
1610
+ seq(
1611
+ $._ere_compound_atom,
1612
+ $._ere_compound_atom,
1613
+ repeat($._ere_compound_atom),
1614
+ ),
1615
+ ),
1616
+
1617
+ _ere_compound_atom: ($) =>
1618
+ choice($._ere_compound_nonmeta_atom, $._ere_compound_meta_character),
1619
+
1620
+ _ere_compound_nonmeta_atom: ($) =>
1621
+ choice(
1622
+ $._ere_compound_nonmeta_character,
1623
+ $.escaped_delimiter,
1624
+ alias($._ere_bracket_escape_sequence, $.escape_sequence),
1625
+ ),
1626
+
1627
+ _ere_open_dot: ($) => ereCompoundOpening($, "."),
1628
+
1629
+ _ere_dot_close: ($) => ereCompoundClosing($._ere_dot_close_guard, "."),
1630
+
1631
+ _ere_open_equal: ($) => ereCompoundOpening($, "="),
1632
+
1633
+ _ere_equal_close: ($) => ereCompoundClosing($._ere_equal_close_guard, "="),
1634
+
1635
+ _ere_open_colon: ($) => ereCompoundOpening($, ":"),
1636
+
1637
+ _ere_colon_close: ($) => ereCompoundClosing($._ere_colon_close_guard, ":"),
1638
+
1639
+ escaped_delimiter: ($) =>
1640
+ seq(
1641
+ $._ere_escaped_delimiter_start,
1642
+ $._escape_introducer,
1643
+ alias($._ere_escaped_delimiter_end, "/"),
1644
+ ),
1645
+
1646
+ _ere_named_escape_sequence: ($) =>
1647
+ ereEscapeWithCharacter($, $._ere_named_escape_character),
1648
+
1649
+ _ere_quoted_escape_sequence: ($) =>
1650
+ ereEscapeWithCharacter($, $._ere_quoted_escape_character),
1651
+
1652
+ _ere_octal_escape_sequence: ($) =>
1653
+ ereEscapeWithCharacter($, $._escape_octal_digits),
1654
+
1655
+ _ere_undefined_escape_sequence: ($) =>
1656
+ ereEscapeWithCharacter($, $._ere_undefined_escape_character),
1657
+
1658
+ _ere_bracket_escape_sequence: ($) =>
1659
+ choice(
1660
+ $._ere_named_escape_sequence,
1661
+ $._ere_quoted_escape_sequence,
1662
+ $._ere_octal_escape_sequence,
1663
+ $._ere_undefined_escape_sequence,
1664
+ ),
1665
+
1666
+ // Tree-sitter rejects the POSIX bracket spelling for these delimiter
1667
+ // characters, so the exclusion sets use its hexadecimal regex escape.
1668
+ // The comment extra competes in ERE lexical states, so every ERE token
1669
+ // whose set contains "#" carries a lexical precedence above the comment.
1670
+ _ordinary_character: () =>
1671
+ token.immediate(prec(1, /[^.\x5B\x5C*^$+?{|}()/\n]/)),
1672
+
1673
+ _ere_ordinary_close_parenthesis: () => token.immediate(prec(1, ")")),
1674
+
1675
+ _ere_ordinary_close_brace: () => token.immediate(prec(1, "}")),
1676
+
1677
+ _ere_close_parenthesis: () => token.immediate(prec(2, ")")),
1678
+
1679
+ _ere_close_brace: () => token.immediate(prec(2, "}")),
1680
+
1681
+ _ere_close_bracket: () => token.immediate(prec(3, "]")),
1682
+
1683
+ _ere_bracket_hyphen: () => token.immediate("-"),
1684
+
1685
+ _ere_bracket_character: () =>
1686
+ token.immediate(prec(1, /[^\x2D\x2F\x5B\x5C\x5D\n]/)),
1687
+
1688
+ _ere_bracket_open_character: () => token.immediate(prec(1, "[")),
1689
+
1690
+ _ere_bracket_close_character: () => token.immediate(prec(1, "]")),
1691
+
1692
+ _ere_compound_nonmeta_character: () =>
1693
+ token.immediate(prec(1, /[^\x2D\x2F\x5C\x5D\x5E\n]/)),
1694
+
1695
+ _ere_compound_meta_character: () => token.immediate(/[\x2D\x5D\x5E]/),
1696
+
1697
+ _ere_class_name_spelling: () => token.immediate(/[A-Za-z][A-Za-z0-9]*/),
1698
+
1699
+ _ere_named_escape_character: () => token.immediate(prec(2, /[abfnrtv]/)),
1700
+
1701
+ _ere_quoted_escape_character: () =>
1702
+ token.immediate(prec(2, /[().*+?{}|^$\x5B\x5C\x5D]/)),
1703
+
1704
+ _ere_undefined_escape_character: () =>
1705
+ token.immediate(prec(1, /[^0-7\x2F\x5C\n]/)),
1706
+
1707
+ newline_opt: ($) => rawNewlines($),
1708
+
1709
+ terminator: ($) =>
1710
+ choice(
1711
+ rawNewlines($),
1712
+ seq(";", repeat(seq(repeat($.line_continuation), $.newline))),
1713
+ ),
1714
+
1715
+ newline: () => "\n",
1716
+
1717
+ line_continuation: () => token(seq("\\", "\n")),
1718
+
1719
+ comment: () => token(seq("#", /[^\n]*/)),
1720
+
1721
+ _number_digit_chunk: () => token.immediate(/[0-9]+/),
1722
+
1723
+ _escape_introducer: () => token.immediate(/\\/),
1724
+
1725
+ _escape_character: () =>
1726
+ choice(token.immediate(/\\/), token.immediate(prec(1, /[^0-7\\\n]/))),
1727
+
1728
+ _escape_octal_digits: () => token.immediate(/[0-7]{1,3}/),
1729
+ },
1730
+ });