@konomanoasa/tree-sitter-posix-awk 0.17.0 → 0.19.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 CHANGED
@@ -1,90 +1,131 @@
1
- const PRECEDENCE = {
2
- assignment: 2,
3
- logicalOr: 4,
4
- logicalAnd: 6,
5
- membership: 8,
6
- match: 10,
7
- comparison: 12,
8
- concatenation: 14,
9
- additive: 16,
10
- multiplicative: 18,
11
- field: 22,
1
+ export const EXPRESSION = {
2
+ assignment: { precedence: 2, associativity: "right" },
3
+ conditional: { precedence: 3, associativity: "right" },
4
+ logical_or: { precedence: 4, associativity: "left" },
5
+ logical_and: { precedence: 6, associativity: "left" },
6
+ membership: { precedence: 8, associativity: "left" },
7
+ match: { precedence: 10, associativity: "none" },
8
+ comparison: { precedence: 12, associativity: "none" },
9
+ concatenation: { precedence: 14, associativity: "left" },
10
+ additive: { precedence: 16, associativity: "left" },
11
+ multiplicative: { precedence: 18, associativity: "left" },
12
+ unary: { precedence: 20, associativity: "right" },
13
+ exponentiation: {
14
+ precedence: 21,
15
+ associativity: "right",
16
+ rightOperand: "unary",
17
+ },
18
+ postfix: { precedence: 23, associativity: "left" },
19
+ field: { precedence: 24, associativity: "right" },
12
20
  };
13
21
 
14
- const KEYWORDS = [
15
- "begin",
16
- "end",
17
- "function",
18
- "print",
19
- "break",
20
- "continue",
21
- "delete",
22
- "do",
23
- "else",
24
- "exit",
25
- "for",
26
- "if",
27
- "next",
28
- "nextfile",
29
- "printf",
30
- "return",
31
- "while",
32
- ];
22
+ export const RESERVED_WORDS = {
23
+ keyword: [
24
+ "BEGIN",
25
+ "END",
26
+ "break",
27
+ "continue",
28
+ "delete",
29
+ "do",
30
+ "else",
31
+ "exit",
32
+ "for",
33
+ "function",
34
+ "if",
35
+ "next",
36
+ "nextfile",
37
+ "print",
38
+ "printf",
39
+ "return",
40
+ "while",
41
+ ],
42
+ membership: ["in"],
43
+ getline: ["getline"],
44
+ builtin: [
45
+ "atan2",
46
+ "close",
47
+ "cos",
48
+ "exp",
49
+ "fflush",
50
+ "gsub",
51
+ "index",
52
+ "int",
53
+ "length",
54
+ "log",
55
+ "match",
56
+ "rand",
57
+ "sin",
58
+ "split",
59
+ "sprintf",
60
+ "sqrt",
61
+ "srand",
62
+ "sub",
63
+ "substr",
64
+ "system",
65
+ "tolower",
66
+ "toupper",
67
+ ],
68
+ };
33
69
 
34
- const TWO_CHARACTER_TOKENS = [
35
- "div_assign",
36
- "add_assign",
37
- "sub_assign",
38
- "mul_assign",
39
- "mod_assign",
40
- "pow_assign",
41
- "or",
42
- "and",
43
- "no_match",
44
- "eq",
45
- "le",
46
- "ge",
47
- "ne",
48
- "incr",
49
- "decr",
50
- "append",
51
- ];
70
+ const OPERAND_PRECEDENCE = 2;
71
+ const CLASSIFICATIONS = ["unary", "non_unary"];
52
72
 
53
- const SINGLE_CHARACTER_OPERATORS = {
54
- "+": "plus",
55
- "-": "minus",
56
- "*": "star",
57
- "%": "percent",
58
- "^": "caret",
59
- "!": "bang",
60
- "<": "less",
61
- ">": "greater",
62
- "=": "equal",
63
- "|": "pipe",
64
- };
73
+ function nextExpressionTier(tier, comparison) {
74
+ return Object.keys(EXPRESSION)
75
+ .filter(
76
+ (name) =>
77
+ (comparison || name !== "comparison") &&
78
+ EXPRESSION[name].precedence > EXPRESSION[tier].precedence,
79
+ )
80
+ .sort(
81
+ (left, right) =>
82
+ EXPRESSION[left].precedence - EXPRESSION[right].precedence,
83
+ )[0];
84
+ }
85
+
86
+ const bind = ({ precedence, associativity }, rule) =>
87
+ associativity === "left"
88
+ ? prec.left(precedence, rule)
89
+ : associativity === "right"
90
+ ? prec.right(precedence, rule)
91
+ : prec(precedence, rule);
65
92
 
66
93
  // Skipping blanks can reach a backslash the external scanner never saw.
67
- const continuationMarker = token.immediate("\\");
94
+ const continuationMarker = () => token.immediate("\\");
68
95
 
69
- const singleOperator = ($, character) =>
70
- alias($[`_${SINGLE_CHARACTER_OPERATORS[character]}_operator`], character);
96
+ const signOperator = ($, character) =>
97
+ alias(character === "+" ? $._plus_operator : $._minus_operator, character);
71
98
 
72
99
  const twoCharacterTokenRules = Object.fromEntries(
73
- TWO_CHARACTER_TOKENS.map((name) => [name, ($) => $[`_${name}_operator`]]),
100
+ Object.entries({
101
+ add_assign: "+=",
102
+ sub_assign: "-=",
103
+ mul_assign: "*=",
104
+ div_assign: "/=",
105
+ mod_assign: "%=",
106
+ pow_assign: "^=",
107
+ or: "||",
108
+ and: "&&",
109
+ no_match: "!~",
110
+ eq: "==",
111
+ le: "<=",
112
+ ge: ">=",
113
+ ne: "!=",
114
+ append: ">>",
115
+ }).map(([name, spelling]) => [name, () => spelling]),
74
116
  );
75
117
 
118
+ const trailingUpdateOperators = ($) => [
119
+ alias($.trailing_incr, $.incr),
120
+ alias($.trailing_decr, $.decr),
121
+ ];
122
+
76
123
  const newlineLayout = ($) => optional($.newline_opt);
77
124
 
78
125
  const afterOptionalNewline = ($, member) => seq(newlineLayout($), member);
79
126
 
80
- // A separate closing-hyphen token avoids competing with range separators.
81
127
  const ereClosingHyphen = ($) => alias($._ere_closing_hyphen, "-");
82
128
 
83
- const ereBracketListAlternatives = ($, followList) => [
84
- followList,
85
- seq(followList, ereClosingHyphen($)),
86
- ];
87
-
88
129
  const ereRangeExpressionWith = ($, startRange) =>
89
130
  seq(
90
131
  startRange,
@@ -95,9 +136,9 @@ const initialFollowListRules = (prefix, firstElement) => {
95
136
  const name = (part) => `_initial_${prefix}_${part}`;
96
137
  return {
97
138
  [name("follow_list")]: ($) =>
98
- choice(
139
+ seq(
99
140
  alias($[name("expression_term")], $.expression_term),
100
- seq(alias($[name("follow_list")], $.follow_list), $.expression_term),
141
+ repeat($.expression_term),
101
142
  ),
102
143
  [name("expression_term")]: ($) =>
103
144
  choice(
@@ -110,12 +151,18 @@ const initialFollowListRules = (prefix, firstElement) => {
110
151
  ereRangeExpressionWith($, alias($[name("start_range")], $.start_range)),
111
152
  [name("start_range")]: ($) =>
112
153
  seq(alias($[name("end_range")], $.end_range), $._ere_bracket_hyphen),
113
- [name("end_range")]: ($) => alias(firstElement($), $.collating_element),
154
+ [name("end_range")]: ($) => firstElement($),
114
155
  };
115
156
  };
116
157
 
117
- const ereCompoundOpening = ($, punctuation) =>
118
- seq(alias($._ere_compound_opening, "["), token.immediate(punctuation));
158
+ const ereCompoundOpening = ($, punctuation, escaped = false) =>
159
+ seq(
160
+ alias(
161
+ escaped ? $._ere_escaped_compound_opening : $._ere_compound_opening,
162
+ "[",
163
+ ),
164
+ token.immediate(punctuation),
165
+ );
119
166
 
120
167
  const ereCompoundClosing = (closing, punctuation) =>
121
168
  seq(alias(closing, punctuation), token.immediate("]"));
@@ -126,61 +173,47 @@ const ereExpressionRules = (insideGroup) => {
126
173
  insideGroup ? alias($[name(rule)], $[rule]) : $[rule];
127
174
  return {
128
175
  [name("extended_reg_exp")]: ($) =>
129
- choice(
176
+ seq(
130
177
  member($, "ere_branch"),
131
- seq(
132
- field("left", member($, "extended_reg_exp")),
133
- field("operator", token.immediate("|")),
134
- field("right", member($, "ere_branch")),
135
- ),
178
+ repeat(seq(token.immediate("|"), member($, "ere_branch"))),
136
179
  ),
137
180
 
138
- [name("ere_branch")]: ($) =>
139
- choice(
140
- member($, "ere_expression"),
141
- seq(
142
- field("left", member($, "ere_branch")),
143
- field("right", member($, "ere_expression")),
144
- ),
145
- ),
181
+ [name("ere_branch")]: ($) => repeat1(member($, "ere_expression")),
146
182
 
147
183
  [name("ere_expression")]: ($) =>
148
- choice(
149
- member($, "one_char_or_coll_elem_ere"),
150
- $.left_anchor,
151
- $.right_anchor,
152
- seq(
153
- field("opening", token.immediate("(")),
154
- field(
155
- "expression",
156
- alias($.group_extended_reg_exp, $.extended_reg_exp),
184
+ seq(
185
+ choice(
186
+ member($, "one_char_or_coll_elem_ere"),
187
+ token.immediate("^"),
188
+ token.immediate("$"),
189
+ seq(
190
+ field("opening", token.immediate("(")),
191
+ optional(
192
+ field(
193
+ "expression",
194
+ alias($.group_extended_reg_exp, $.extended_reg_exp),
195
+ ),
196
+ ),
197
+ field("closing", $._ere_close_parenthesis),
157
198
  ),
158
- field("closing", $._ere_close_parenthesis),
159
- ),
160
- seq(
161
- field("operand", member($, "ere_expression")),
162
- field("operator", $.ere_dupl_symbol),
163
199
  ),
200
+ repeat($.ere_dupl_symbol),
164
201
  ),
165
202
 
166
203
  [name("one_char_or_coll_elem_ere")]: ($) =>
167
204
  choice(
168
- member($, "ordinary_character"),
205
+ alias($._ordinary_character, $.ordinary_character),
206
+ ...(insideGroup
207
+ ? []
208
+ : [alias(token.immediate(")"), $.ordinary_character)]),
209
+ alias(token.immediate("}"), $.ordinary_character),
210
+ $.ordinary_character,
169
211
  $.quoted_character,
170
- $.wildcard,
212
+ token.immediate("."),
171
213
  $.bracket_expression,
172
214
  alias($.ere_octal_escape_sequence, $.escape_sequence),
173
215
  alias($.ere_undefined_escape_sequence, $.escape_sequence),
174
216
  ),
175
-
176
- [name("ordinary_character")]: ($) =>
177
- choice(
178
- alias($._ordinary_character, $.ordinary_character_content),
179
- ...(insideGroup ? [] : [$._ere_ordinary_close_parenthesis]),
180
- $._ere_ordinary_close_brace,
181
- $.escaped_delimiter,
182
- alias($.ere_named_escape_sequence, $.escape_sequence),
183
- ),
184
217
  };
185
218
  };
186
219
 
@@ -203,13 +236,6 @@ const controlStatements = ($, body) => [
203
236
  seq($._for_header, field("body", body)),
204
237
  ];
205
238
 
206
- const statementTerminatedBy = ($, terminator) =>
207
- seq(
208
- field("statement", $.terminatable_statement),
209
- field("terminator", terminator),
210
- newlineLayout($),
211
- );
212
-
213
239
  const parenthesized = (member) => seq("(", member, ")");
214
240
 
215
241
  const parenthesizedPrintStatement = ($, keyword) =>
@@ -219,28 +245,51 @@ const callArguments = ($) => parenthesized(optional($.expr_list));
219
245
 
220
246
  const subscript = (subscripts) => seq("[", subscripts, "]");
221
247
 
222
- const directInputFunction = ($, get) =>
223
- choice(
224
- field("get", get),
225
- prec.right(
226
- PRECEDENCE.field,
227
- seq(field("get", get), singleOperator($, "<"), field("source", $.expr)),
228
- ),
248
+ const redirectedInput = ($, get, source = $.expr) =>
249
+ prec.right(
250
+ EXPRESSION.field.precedence,
251
+ seq(field("get", get), "<", field("source", source)),
229
252
  );
230
253
 
231
- const getWithTarget = ($, target) =>
232
- prec(1, prec.dynamic(1, seq($.getline_keyword, field("target", target))));
233
-
234
254
  const pipedInput = ($, source, get) =>
235
255
  prec.right(
236
- PRECEDENCE.field,
237
- seq(field("source", source), singleOperator($, "|"), field("get", get)),
256
+ EXPRESSION.field.precedence,
257
+ seq(
258
+ field("source", source),
259
+ alias($._getline_pipe, "|"),
260
+ field("get", get),
261
+ ),
238
262
  );
239
263
 
240
- const omittedGet = ($) => alias($.postfix_omitted_get, $.simple_get);
241
-
242
264
  const rawNewlines = ($) => repeat1($.newline);
243
265
 
266
+ const simpleGet = ($, sourceContext = false) => {
267
+ const keyword = (rule) => alias(rule, $.getline_keyword);
268
+ return choice(
269
+ $.getline_keyword,
270
+ keyword($.omitted_getline_keyword),
271
+ keyword($.preferred_omitted_getline_keyword),
272
+ seq(
273
+ choice(
274
+ keyword($.target_getline_keyword),
275
+ keyword($.preferred_target_getline_keyword),
276
+ ),
277
+ field("target", $._lvalue),
278
+ ),
279
+ prec.dynamic(-1, keyword($.preferred_target_getline_keyword)),
280
+ prec.dynamic(
281
+ -1,
282
+ seq(
283
+ choice(
284
+ keyword($.preferred_omitted_getline_keyword),
285
+ ...(sourceContext ? [keyword($.omitted_getline_keyword)] : []),
286
+ ),
287
+ field("target", $._lvalue),
288
+ ),
289
+ ),
290
+ );
291
+ };
292
+
244
293
  const EXPRESSION_CONTEXT = {
245
294
  normal: {
246
295
  prefix: "normal",
@@ -250,10 +299,25 @@ const EXPRESSION_CONTEXT = {
250
299
  comparison: true,
251
300
  input: ($) =>
252
301
  choice(
253
- $._direct_input_function,
302
+ alias($.direct_input_function, $.non_unary_input_function),
254
303
  alias($.piped_input_function, $.non_unary_input_function),
255
304
  ),
256
- unaryInput: true,
305
+ unaryInput: ($) => $.unary_input_function,
306
+ },
307
+ source: {
308
+ prefix: "source",
309
+ expression: "source_expr",
310
+ publicExpression: "expr",
311
+ unaryExpression: "unary_expr",
312
+ nonUnaryExpression: "non_unary_expr",
313
+ comparison: true,
314
+ input: ($) =>
315
+ choice(
316
+ alias($.source_direct_input_function, $.non_unary_input_function),
317
+ alias($.source_piped_input_function, $.non_unary_input_function),
318
+ ),
319
+ unaryInput: ($) =>
320
+ alias($.source_unary_input_function, $.unary_input_function),
257
321
  },
258
322
  print: {
259
323
  prefix: "print",
@@ -267,15 +331,14 @@ const EXPRESSION_CONTEXT = {
267
331
  expression: "expr",
268
332
  unaryExpression: "unary_expr",
269
333
  nonUnaryExpression: "non_unary_expr",
270
- input: ($) => $._direct_input_function,
334
+ input: ($) => alias($.direct_input_function, $.non_unary_input_function),
271
335
  },
272
336
  };
273
337
 
274
338
  const classTierName = (context, classification, tier) =>
275
339
  `_${context.prefix}_${classification}_${tier}_expr`;
276
340
 
277
- // Hidden operand rules leak inner fields into the parent; aliased visible rules
278
- // prevent this.
341
+ // Hidden operand rules leak inner fields into the parent.
279
342
  const classOperandName = (context, classification, tier) =>
280
343
  `${context.prefix}_${classification}_${tier}_operand`;
281
344
 
@@ -294,8 +357,6 @@ const aliasedClassTier = ($, context, classification, tier) =>
294
357
  ],
295
358
  );
296
359
 
297
- const CLASSIFICATIONS = ["unary", "non_unary"];
298
-
299
360
  const tierHelpers = (context, rules) => ({
300
361
  unary: (tier) => classTierName(context, "unary", tier),
301
362
  nonUnary: (tier) => classTierName(context, "non_unary", tier),
@@ -304,7 +365,10 @@ const tierHelpers = (context, rules) => ({
304
365
  });
305
366
 
306
367
  const aliasedAnyTier = ($, context, tier) =>
307
- alias($[anyTierName(context, tier)], $[context.expression]);
368
+ alias(
369
+ $[anyTierName(context, tier)],
370
+ $[context.publicExpression ?? context.expression],
371
+ );
308
372
 
309
373
  const nonUnaryAtom = ($, context, lvalue = $._lvalue) => {
310
374
  const atoms = [
@@ -325,7 +389,7 @@ const nonUnaryAtom = ($, context, lvalue = $._lvalue) => {
325
389
 
326
390
  const classOperandRules = (context, classification, tier) => ({
327
391
  [classOperandName(context, classification, tier)]: ($) =>
328
- prec(1, classTier($, context, classification, tier)),
392
+ prec(OPERAND_PRECEDENCE, classTier($, context, classification, tier)),
329
393
  });
330
394
 
331
395
  const anyTierRules = (context, tier) => ({
@@ -339,7 +403,8 @@ const anyTierRules = (context, tier) => ({
339
403
  });
340
404
 
341
405
  const unaryExpressionRules = (context) => {
342
- const rules = anyTierRules(context, "unary");
406
+ const rules = {};
407
+ Object.assign(rules, anyTierRules(context, "unary"));
343
408
  const { unary, nonUnary, addOperand } = tierHelpers(context, rules);
344
409
  const not = `_${context.prefix}_not_expr`;
345
410
  rules[unary("unary")] = ($) =>
@@ -349,14 +414,14 @@ const unaryExpressionRules = (context) => {
349
414
  : []),
350
415
  ...["+", "-"].map((operator) =>
351
416
  seq(
352
- field("operator", singleOperator($, operator)),
417
+ field("operator", signOperator($, operator)),
353
418
  field("operand", aliasedAnyTier($, context, "unary")),
354
419
  ),
355
420
  ),
356
421
  );
357
422
  rules[not] = ($) =>
358
423
  seq(
359
- field("operator", singleOperator($, "!")),
424
+ field("operator", "!"),
360
425
  field("operand", aliasedAnyTier($, context, "unary")),
361
426
  );
362
427
  rules[nonUnary("unary")] = ($) =>
@@ -366,7 +431,10 @@ const unaryExpressionRules = (context) => {
366
431
  rules[exponentiationTail] = ($) =>
367
432
  seq(
368
433
  $._exponentiation_operator,
369
- field("right", aliasedAnyTier($, context, "unary")),
434
+ field(
435
+ "right",
436
+ aliasedAnyTier($, context, EXPRESSION.exponentiation.rightOperand),
437
+ ),
370
438
  );
371
439
  const exponentiationClassifications = context.unaryInput
372
440
  ? CLASSIFICATIONS
@@ -383,7 +451,7 @@ const unaryExpressionRules = (context) => {
383
451
  );
384
452
  }
385
453
  if (context.unaryInput) {
386
- rules[unary("update")] = ($) => $.unary_input_function;
454
+ rules[unary("update")] = context.unaryInput;
387
455
  }
388
456
 
389
457
  rules[nonUnary("update")] = ($) =>
@@ -392,11 +460,17 @@ const unaryExpressionRules = (context) => {
392
460
  $._prefix_update_expr,
393
461
  seq(
394
462
  field("operand", alias($.postfix_lvalue, $.lvalue)),
395
- field("operator", choice($.incr, $.decr)),
463
+ field(
464
+ "operator",
465
+ choice($.incr, $.decr, ...trailingUpdateOperators($)),
466
+ ),
467
+ ),
468
+ seq(
469
+ field("operand", alias($.redirected_field, $.lvalue)),
470
+ field("operator", choice(...trailingUpdateOperators($))),
396
471
  ),
397
472
  );
398
473
 
399
- // A postfix update must win over forwarding its lvalue as a complete atom.
400
474
  rules[nonUnary("atom")] = ($) =>
401
475
  nonUnaryAtom($, context, prec(-1, $._lvalue));
402
476
 
@@ -407,7 +481,10 @@ const tieredExpressionRules = (context) => {
407
481
  const rules = {};
408
482
  const { unary, nonUnary, addOperand } = tierHelpers(context, rules);
409
483
  const conditionalTail = `_${context.prefix}_conditional_tail`;
410
- const expression = ($) => $[context.expression];
484
+ const expression = ($) =>
485
+ context.publicExpression
486
+ ? alias($[context.expression], $[context.publicExpression])
487
+ : $[context.expression];
411
488
  rules[conditionalTail] = ($) =>
412
489
  seq(
413
490
  "?",
@@ -421,11 +498,12 @@ const tieredExpressionRules = (context) => {
421
498
 
422
499
  const addBinaryTier = (
423
500
  tier,
424
- nextTier,
425
501
  operator,
426
- precedence,
427
- { leftAssociative, newlineAfterOperator = false },
502
+ { newlineAfterOperator = false } = {},
428
503
  ) => {
504
+ const binding = EXPRESSION[tier];
505
+ const nextTier = nextExpressionTier(tier, context.comparison);
506
+ const leftAssociative = binding.associativity === "left";
429
507
  addAnyTier(nextTier);
430
508
  const tail = `_${context.prefix}_${tier}_tail`;
431
509
  rules[tail] = ($) => {
@@ -455,9 +533,7 @@ const tieredExpressionRules = (context) => {
455
533
  rules[classTierName(context, classification, tier)] = ($) =>
456
534
  choice(
457
535
  classTier($, context, classification, nextTier),
458
- leftAssociative
459
- ? prec.left(precedence, operation($))
460
- : prec(precedence, operation($)),
536
+ bind(binding, operation($)),
461
537
  );
462
538
  }
463
539
  };
@@ -467,8 +543,8 @@ const tieredExpressionRules = (context) => {
467
543
  rules[nonUnary("assignment")] = ($) =>
468
544
  choice(
469
545
  classTier($, context, "non_unary", "conditional"),
470
- prec.right(
471
- PRECEDENCE.assignment,
546
+ bind(
547
+ EXPRESSION.assignment,
472
548
  seq(
473
549
  field("left", $._lvalue),
474
550
  field(
@@ -480,7 +556,7 @@ const tieredExpressionRules = (context) => {
480
556
  $.div_assign,
481
557
  $.add_assign,
482
558
  $.sub_assign,
483
- singleOperator($, "="),
559
+ "=",
484
560
  ),
485
561
  ),
486
562
  field("right", expression($)),
@@ -504,21 +580,13 @@ const tieredExpressionRules = (context) => {
504
580
  );
505
581
  }
506
582
 
507
- addBinaryTier(
508
- "logical_or",
509
- "logical_and",
510
- ($) => $._logical_or_operator,
511
- PRECEDENCE.logicalOr,
512
- { leftAssociative: true, newlineAfterOperator: true },
513
- );
583
+ addBinaryTier("logical_or", ($) => $._logical_or_operator, {
584
+ newlineAfterOperator: true,
585
+ });
514
586
 
515
- addBinaryTier(
516
- "logical_and",
517
- "membership",
518
- ($) => $._logical_and_operator,
519
- PRECEDENCE.logicalAnd,
520
- { leftAssociative: true, newlineAfterOperator: true },
521
- );
587
+ addBinaryTier("logical_and", ($) => $._logical_and_operator, {
588
+ newlineAfterOperator: true,
589
+ });
522
590
 
523
591
  const membershipTail = `_${context.prefix}_membership_tail`;
524
592
  rules[membershipTail] = ($) =>
@@ -527,8 +595,8 @@ const tieredExpressionRules = (context) => {
527
595
  rules[classTierName(context, classification, "membership")] = ($) => {
528
596
  const members = [
529
597
  classTier($, context, classification, "match"),
530
- prec.left(
531
- PRECEDENCE.membership,
598
+ bind(
599
+ EXPRESSION.membership,
532
600
  seq(
533
601
  field(
534
602
  "left",
@@ -551,92 +619,28 @@ const tieredExpressionRules = (context) => {
551
619
  };
552
620
  }
553
621
 
554
- const comparisonTier = context.comparison ? "comparison" : "concatenation";
555
- addBinaryTier(
556
- "match",
557
- comparisonTier,
558
- ($) => $._match_operator,
559
- PRECEDENCE.match,
560
- { leftAssociative: false },
561
- );
622
+ addBinaryTier("match", ($) => $._match_operator);
562
623
 
563
624
  if (context.comparison) {
564
- addBinaryTier(
565
- "comparison",
566
- "concatenation",
567
- ($) => $._comparison_operator,
568
- PRECEDENCE.comparison,
569
- { leftAssociative: false },
570
- );
625
+ addBinaryTier("comparison", ($) => $._comparison_operator);
571
626
  }
572
627
 
573
- // A following postfix update still needs the targetless getline path.
574
- if (context.prefix === "normal") {
575
- const prefix = "getline_updated";
576
- const name = (tier) => `_${prefix}_${tier}`;
577
- const operand = (tier) => `${prefix}_${tier}_operand`;
578
- const wrapped = ($, tier) => alias($[operand(tier)], $.non_unary_expr);
579
- rules[name("update")] = ($) =>
580
- seq(
581
- field("operand", alias($.postfix_lvalue, $.lvalue)),
582
- field("operator", choice($.incr, $.decr)),
583
- );
584
- rules[operand("update")] = ($) => prec(1, $[name("update")]);
585
- rules[name("exponentiation")] = ($) =>
586
- choice(
587
- $[name("update")],
588
- seq(field("left", wrapped($, "update")), $._normal_exponentiation_tail),
589
- );
590
- for (const [tier, next, precedence] of [
591
- ["multiplicative", "exponentiation", PRECEDENCE.multiplicative],
592
- ["additive", "multiplicative", PRECEDENCE.additive],
593
- ]) {
594
- rules[name(tier)] = ($) =>
595
- choice(
596
- $[name(next)],
597
- prec.left(
598
- precedence,
599
- seq(field("left", wrapped($, tier)), $[`_normal_${tier}_tail`]),
600
- ),
601
- );
602
- rules[operand(tier)] = ($) => prec(1, $[name(tier)]);
603
- }
604
- }
605
628
  const concatenationTail = `_${context.prefix}_concatenation_tail`;
629
+ // Reusing an arithmetic left operand here can bypass postfix lookahead.
630
+ const concatenationOperand = `${context.prefix}_concatenation_operand`;
631
+ rules[concatenationOperand] = ($) =>
632
+ prec(1, classTier($, context, "non_unary", "additive"));
606
633
  rules[concatenationTail] = ($) =>
607
- field("right", aliasedClassTier($, context, "non_unary", "additive"));
634
+ field(
635
+ "right",
636
+ alias($[concatenationOperand], $[context.nonUnaryExpression]),
637
+ );
608
638
  for (const classification of CLASSIFICATIONS) {
609
639
  rules[classTierName(context, classification, "concatenation")] = ($) =>
610
640
  choice(
611
641
  classTier($, context, classification, "additive"),
612
- ...(context.prefix === "normal"
613
- ? [
614
- prec.left(
615
- PRECEDENCE.concatenation,
616
- seq(
617
- field(
618
- "left",
619
- alias(
620
- classification === "unary"
621
- ? $.omitted_unary_get_operand
622
- : $.omitted_get_operand,
623
- $[
624
- classification === "unary"
625
- ? "unary_expr"
626
- : "non_unary_expr"
627
- ],
628
- ),
629
- ),
630
- field(
631
- "right",
632
- alias($.getline_updated_additive_operand, $.non_unary_expr),
633
- ),
634
- ),
635
- ),
636
- ]
637
- : []),
638
- prec.left(
639
- PRECEDENCE.concatenation,
642
+ bind(
643
+ EXPRESSION.concatenation,
640
644
  seq(
641
645
  field(
642
646
  "left",
@@ -648,53 +652,38 @@ const tieredExpressionRules = (context) => {
648
652
  );
649
653
  }
650
654
 
651
- addBinaryTier(
652
- "additive",
653
- "multiplicative",
654
- ($) => $._additive_operator,
655
- PRECEDENCE.additive,
656
- { leftAssociative: true },
657
- );
655
+ addBinaryTier("additive", ($) => $._additive_operator);
658
656
 
659
- addBinaryTier(
660
- "multiplicative",
661
- "unary",
662
- ($) => $._multiplicative_operator,
663
- PRECEDENCE.multiplicative,
664
- { leftAssociative: true },
665
- );
657
+ addBinaryTier("multiplicative", ($) => $._multiplicative_operator);
666
658
 
667
659
  return { ...rules, ...unaryExpressionRules(context) };
668
660
  };
669
661
 
670
- const normalExpressionRules = tieredExpressionRules(EXPRESSION_CONTEXT.normal);
671
- const printExpressionRules = tieredExpressionRules(EXPRESSION_CONTEXT.print);
672
- const fieldExpressionRules = unaryExpressionRules(EXPRESSION_CONTEXT.field);
673
-
674
662
  const listElementTail = ($, element) =>
675
663
  seq(",", afterOptionalNewline($, element));
676
664
 
677
- export default grammar({
665
+ export default globalThis.grammar?.({
678
666
  name: "posix_awk",
679
667
 
680
668
  externals: ($) => [
681
- ...KEYWORDS.map((keyword) => $[`${keyword}_keyword`]),
682
- $._name_word,
683
- $._for_in_variable_word,
684
669
  $._getline_word,
685
- $._in_word,
686
- $._builtin_func_name_word,
687
- $._builtin_call_word,
670
+ $._getline_target_word,
671
+ $._getline_omitted_word,
672
+ $._getline_field_word,
673
+ $._getline_prefer_target_word,
674
+ $._getline_prefer_omitted_word,
688
675
  $._func_name_word,
689
- $._number,
690
676
  $._division_slash,
691
677
  $._ere_opening_slash,
692
- ...TWO_CHARACTER_TOKENS.map((name) => $[`_${name}_operator`]),
693
- ...Object.values(SINGLE_CHARACTER_OPERATORS).map(
694
- (name) => $[`_${name}_operator`],
695
- ),
696
- $._output_greater,
678
+ $._incr_operator,
679
+ $._decr_operator,
680
+ $._plus_operator,
681
+ $._minus_operator,
682
+ $._getline_pipe,
697
683
  $._ere_compound_opening,
684
+ $._ere_escaped_compound_opening,
685
+ $._ere_compound_single,
686
+ $._ere_compound_multi,
698
687
  $._ere_dot_closing,
699
688
  $._ere_equal_closing,
700
689
  $._ere_colon_closing,
@@ -704,82 +693,87 @@ export default grammar({
704
693
  $._ere_closing,
705
694
  $._string_opening,
706
695
  $._string_closing,
707
- $._string_content,
708
696
  $._string_escape,
709
697
  $._ere_named_escape,
710
698
  $._ere_quoted_escape,
711
699
  $._ere_octal_escape,
712
700
  $._ere_undefined_escape,
713
701
  $._ere_escaped_delimiter,
714
- $._ere_class_name,
715
- $._ere_dup_count,
716
702
  $.comment,
717
- continuationMarker,
703
+ continuationMarker(),
718
704
  $._continuation_newline,
705
+ $._literal_break,
719
706
  $._stray_backslash,
720
707
  $._error_sentinel,
721
708
  ],
722
709
 
710
+ word: ($) => $.name,
711
+
712
+ reserved: {
713
+ global: ($) => [
714
+ ...[...RESERVED_WORDS.keyword, ...RESERVED_WORDS.membership].map(
715
+ (word) => $[`${word.toLowerCase()}_keyword`],
716
+ ),
717
+ $.builtin_func_name,
718
+ ],
719
+ },
720
+
723
721
  extras: ($) => [
724
722
  token(repeat1(choice(" ", "\t"))),
725
723
  $.comment,
726
- continuationMarker,
724
+ continuationMarker(),
727
725
  $._continuation_newline,
726
+ $._literal_break,
728
727
  ],
729
728
 
730
729
  inline: ($) => [
730
+ // Shared operator reductions can freeze reused operands after edits.
731
+ $._additive_operator,
732
+ $._multiplicative_operator,
733
+ $._exponentiation_operator,
734
+ $._comparison_operator,
735
+ $._match_operator,
736
+ $._membership_operator,
737
+ $._logical_and_operator,
738
+ $._logical_or_operator,
731
739
  $._lvalue,
732
- $._simple_get,
733
- $._direct_input_function,
740
+ $._field_simple_get,
734
741
  $._item,
735
742
  // A recovered tail can retain errors when reused after its prefix changes.
736
743
  $._normal_conditional_tail,
737
- // Unit-reduction merging must not inherit update fields through atom children.
744
+ // Unit-reduction merging can leak update fields through atom children.
738
745
  $._normal_non_unary_update_expr,
739
746
  $._print_non_unary_update_expr,
740
747
  $._field_non_unary_update_expr,
741
748
  $._normal_unary_assignment_expr,
742
749
  $._normal_unary_update_expr,
743
750
  $._print_unary_assignment_expr,
751
+ $._source_non_unary_update_expr,
752
+ $._source_unary_assignment_expr,
753
+ $._source_unary_update_expr,
754
+ $._source_conditional_tail,
744
755
  ],
745
756
 
746
757
  conflicts: ($) => [
747
- [$.postfix_omitted_get, $.postfix_simple_get],
748
- [$.postfix_omitted_get, $.nonpostfix_simple_get, $.postfix_simple_get],
749
- // The final item joins item_list only after its terminator is known.
758
+ [$._for_in_clause, $.postfix_lvalue],
750
759
  [$.item_list, $._item_list],
760
+ [$.simple_get],
761
+ [$.source_simple_get],
751
762
  ],
752
763
 
753
764
  rules: {
754
765
  program: ($) => seq(optional($.item_list), optional($._item)),
755
766
 
756
767
  _additive_operator: ($) =>
757
- field("operator", choice(singleOperator($, "+"), singleOperator($, "-"))),
768
+ field("operator", choice(signOperator($, "+"), signOperator($, "-"))),
758
769
 
759
770
  _multiplicative_operator: ($) =>
760
- field(
761
- "operator",
762
- choice(
763
- singleOperator($, "*"),
764
- alias($._division_slash, "/"),
765
- singleOperator($, "%"),
766
- ),
767
- ),
771
+ field("operator", choice("*", alias($._division_slash, "/"), "%")),
768
772
 
769
- _exponentiation_operator: ($) => field("operator", singleOperator($, "^")),
773
+ _exponentiation_operator: () => field("operator", "^"),
770
774
 
771
775
  _comparison_operator: ($) =>
772
- field(
773
- "operator",
774
- choice(
775
- singleOperator($, "<"),
776
- $.le,
777
- $.ne,
778
- $.eq,
779
- singleOperator($, ">"),
780
- $.ge,
781
- ),
782
- ),
776
+ field("operator", choice("<", $.le, $.ne, $.eq, ">", $.ge)),
783
777
 
784
778
  _match_operator: ($) => field("operator", choice("~", $.no_match)),
785
779
 
@@ -829,8 +823,7 @@ export default grammar({
829
823
  ")",
830
824
  ),
831
825
 
832
- param_list: ($) =>
833
- seq($.name, repeat(seq(",", afterOptionalNewline($, $.name)))),
826
+ param_list: ($) => seq($.name, repeat(listElementTail($, $.name))),
834
827
 
835
828
  pattern: ($) => choice($.normal_pattern, $.special_pattern),
836
829
 
@@ -884,13 +877,7 @@ export default grammar({
884
877
  ),
885
878
 
886
879
  _for_in_clause: ($) =>
887
- seq(
888
- field("variable", alias($.for_in_variable, $.name)),
889
- $.in_keyword,
890
- field("array", $.name),
891
- ),
892
-
893
- for_in_variable: ($) => $._for_in_variable_word,
880
+ seq(field("variable", $.name), $.in_keyword, field("array", $.name)),
894
881
 
895
882
  _for_header: ($) =>
896
883
  header(
@@ -907,8 +894,11 @@ export default grammar({
907
894
  seq($.action, newlineLayout($)),
908
895
  $._self_terminating_statement,
909
896
  seq(field("terminator", ";"), newlineLayout($)),
910
- statementTerminatedBy($, $.newline),
911
- statementTerminatedBy($, ";"),
897
+ seq(
898
+ field("statement", $.terminatable_statement),
899
+ field("terminator", choice($.newline, ";")),
900
+ newlineLayout($),
901
+ ),
912
902
  ),
913
903
 
914
904
  unterminated_statement: ($) =>
@@ -959,23 +949,14 @@ export default grammar({
959
949
  parenthesizedPrintStatement($, $.printf_keyword),
960
950
  ),
961
951
 
962
- output_redirection: ($) =>
963
- seq(
964
- choice(alias($._output_greater, ">"), $.append, singleOperator($, "|")),
965
- $.expr,
966
- ),
952
+ output_redirection: ($) => seq(choice(">", $.append, "|"), $.expr),
967
953
 
968
954
  print_expr_list: ($) =>
969
955
  seq($.print_expr, repeat(listElementTail($, $.print_expr))),
970
956
 
971
957
  expr_list: ($) => choice($.expr, $.multiple_expr_list),
972
958
 
973
- multiple_expr_list: ($) =>
974
- seq(
975
- $.expr,
976
- listElementTail($, $.expr),
977
- repeat(listElementTail($, $.expr)),
978
- ),
959
+ multiple_expr_list: ($) => seq($.expr, repeat1(listElementTail($, $.expr))),
979
960
 
980
961
  print_expr: ($) => choice($.unary_print_expr, $.non_unary_print_expr),
981
962
 
@@ -992,15 +973,26 @@ export default grammar({
992
973
  _prefix_update_expr: ($) =>
993
974
  seq(
994
975
  field("operator", choice($.incr, $.decr)),
995
- field("operand", $._lvalue),
976
+ field("operand", choice($._lvalue, alias($.prefix_field, $.lvalue))),
996
977
  ),
997
978
 
998
- ...normalExpressionRules,
979
+ ...tieredExpressionRules(EXPRESSION_CONTEXT.normal),
980
+
981
+ source_expr: ($) =>
982
+ choice(
983
+ alias($.source_full_unary_expr, $.unary_expr),
984
+ alias($.source_full_non_unary_expr, $.non_unary_expr),
985
+ ),
986
+
987
+ source_full_unary_expr: ($) => $._source_unary_assignment_expr,
988
+
989
+ source_full_non_unary_expr: ($) => $._source_non_unary_assignment_expr,
990
+
991
+ ...tieredExpressionRules(EXPRESSION_CONTEXT.source),
999
992
 
1000
- ...printExpressionRules,
993
+ ...tieredExpressionRules(EXPRESSION_CONTEXT.print),
1001
994
 
1002
- // A pending field operand cannot start a competing pipe source.
1003
- ...fieldExpressionRules,
995
+ ...unaryExpressionRules(EXPRESSION_CONTEXT.field),
1004
996
 
1005
997
  nonpostfix_field_expr: ($) =>
1006
998
  choice(
@@ -1009,16 +1001,16 @@ export default grammar({
1009
1001
  ),
1010
1002
 
1011
1003
  normal_unary_field_expr: ($) =>
1012
- prec(PRECEDENCE.field, $._field_unary_unary_expr),
1004
+ prec(EXPRESSION.field.precedence, $._field_unary_unary_expr),
1013
1005
 
1014
1006
  nonpostfix_non_unary_field_expr: ($) =>
1015
1007
  prec(
1016
- PRECEDENCE.field,
1008
+ EXPRESSION.field.precedence,
1017
1009
  choice(
1018
1010
  $._field_not_expr,
1019
1011
  $._prefix_update_expr,
1020
1012
  alias($.nonpostfix_lvalue, $.lvalue),
1021
- alias($.nonpostfix_direct_input_function, $.non_unary_input_function),
1013
+ alias($.nonpostfix_field_input_function, $.non_unary_input_function),
1022
1014
  ),
1023
1015
  ),
1024
1016
 
@@ -1027,9 +1019,10 @@ export default grammar({
1027
1019
  _user_function_call: ($) => seq($.func_name, callArguments($)),
1028
1020
 
1029
1021
  _builtin_function_call: ($) =>
1030
- seq(alias($.builtin_call_name, $.builtin_func_name), callArguments($)),
1031
-
1032
- builtin_call_name: ($) => $._builtin_call_word,
1022
+ prec(
1023
+ EXPRESSION.field.precedence + 1,
1024
+ seq($.builtin_func_name, callArguments($)),
1025
+ ),
1033
1026
 
1034
1027
  _lvalue: ($) =>
1035
1028
  choice(
@@ -1045,8 +1038,7 @@ export default grammar({
1045
1038
 
1046
1039
  postfix_lvalue: ($) =>
1047
1040
  choice(
1048
- $.name,
1049
- seq($.name, subscript($.expr_list)),
1041
+ seq($.name, optional(subscript($.expr_list))),
1050
1042
  seq(
1051
1043
  field("operator", "$"),
1052
1044
  field("operand", alias($.postfix_field_expr, $.expr)),
@@ -1058,79 +1050,164 @@ export default grammar({
1058
1050
 
1059
1051
  postfix_non_unary_field_expr: ($) =>
1060
1052
  prec(
1061
- PRECEDENCE.field,
1053
+ EXPRESSION.field.precedence,
1062
1054
  nonUnaryAtom(
1063
1055
  $,
1064
1056
  {
1065
1057
  ...EXPRESSION_CONTEXT.field,
1066
1058
  input: ($) =>
1067
- alias(
1068
- $.postfix_direct_input_function,
1069
- $.non_unary_input_function,
1070
- ),
1059
+ alias($.postfix_field_input_function, $.non_unary_input_function),
1071
1060
  },
1072
1061
  alias($.postfix_lvalue, $.lvalue),
1073
1062
  ),
1074
1063
  ),
1075
1064
 
1076
- _direct_input_function: ($) =>
1065
+ prefix_field: ($) =>
1066
+ seq(
1067
+ field("operator", "$"),
1068
+ field("operand", alias($.prefix_field_expr, $.expr)),
1069
+ ),
1070
+
1071
+ prefix_field_expr: ($) =>
1072
+ alias($.prefix_non_unary_field_expr, $.non_unary_expr),
1073
+
1074
+ prefix_non_unary_field_expr: ($) =>
1075
+ prec(
1076
+ EXPRESSION.field.precedence,
1077
+ choice(
1078
+ alias($.direct_input_function, $.non_unary_input_function),
1079
+ alias($.prefix_field, $.lvalue),
1080
+ ),
1081
+ ),
1082
+
1083
+ redirected_field: ($) =>
1084
+ seq(
1085
+ field("operator", "$"),
1086
+ field("operand", alias($.redirected_field_expr, $.expr)),
1087
+ ),
1088
+
1089
+ redirected_field_expr: ($) =>
1090
+ alias($.redirected_non_unary_field_expr, $.non_unary_expr),
1091
+
1092
+ redirected_non_unary_field_expr: ($) =>
1093
+ prec(
1094
+ EXPRESSION.field.precedence,
1095
+ choice(
1096
+ alias($.redirected_field_input_function, $.non_unary_input_function),
1097
+ alias($.redirected_field, $.lvalue),
1098
+ ),
1099
+ ),
1100
+
1101
+ redirected_field_input_function: ($) =>
1077
1102
  choice(
1078
- alias($.postfix_direct_input_function, $.non_unary_input_function),
1079
- alias($.nonpostfix_direct_input_function, $.non_unary_input_function),
1103
+ prec(1, $._redirected_field_input),
1104
+ field("get", alias($.redirected_field_simple_get, $.simple_get)),
1080
1105
  ),
1081
1106
 
1082
- nonpostfix_direct_input_function: ($) =>
1083
- directInputFunction($, alias($.nonpostfix_simple_get, $.simple_get)),
1107
+ redirected_field_simple_get: ($) =>
1108
+ seq(
1109
+ alias($.field_getline_keyword, $.getline_keyword),
1110
+ field("target", alias($.redirected_field, $.lvalue)),
1111
+ ),
1084
1112
 
1085
- postfix_direct_input_function: ($) =>
1086
- directInputFunction($, alias($.postfix_simple_get, $.simple_get)),
1113
+ _redirected_field_input: ($) =>
1114
+ redirectedInput($, $._field_simple_get, alias($.source_expr, $.expr)),
1087
1115
 
1088
- piped_input_function: ($) => pipedInput($, $.non_unary_expr, $._simple_get),
1116
+ nonpostfix_field_input_function: ($) =>
1117
+ choice(
1118
+ field("get", alias($.nonpostfix_field_simple_get, $.simple_get)),
1119
+ $._redirected_field_input,
1120
+ ),
1089
1121
 
1090
- unary_input_function: ($) => pipedInput($, $.unary_expr, $._simple_get),
1122
+ postfix_field_input_function: ($) =>
1123
+ field("get", alias($.postfix_field_simple_get, $.simple_get)),
1091
1124
 
1092
- _simple_get: ($) =>
1125
+ _field_simple_get: ($) =>
1093
1126
  choice(
1094
- alias($.postfix_simple_get, $.simple_get),
1095
- alias($.nonpostfix_simple_get, $.simple_get),
1127
+ alias($.postfix_field_simple_get, $.simple_get),
1128
+ alias($.nonpostfix_field_simple_get, $.simple_get),
1096
1129
  ),
1097
1130
 
1098
- nonpostfix_simple_get: ($) =>
1099
- getWithTarget($, alias($.nonpostfix_lvalue, $.lvalue)),
1131
+ nonpostfix_field_simple_get: ($) =>
1132
+ seq(
1133
+ alias($.field_getline_keyword, $.getline_keyword),
1134
+ field("target", alias($.nonpostfix_lvalue, $.lvalue)),
1135
+ ),
1100
1136
 
1101
- postfix_simple_get: ($) =>
1137
+ postfix_field_simple_get: ($) =>
1102
1138
  choice(
1103
- $.getline_keyword,
1104
- getWithTarget($, alias($.postfix_lvalue, $.lvalue)),
1139
+ prec(-1, alias($.field_getline_keyword, $.getline_keyword)),
1140
+ seq(
1141
+ alias($.field_getline_keyword, $.getline_keyword),
1142
+ field("target", alias($.postfix_lvalue, $.lvalue)),
1143
+ ),
1105
1144
  ),
1106
1145
 
1107
- omitted_get_operand: ($) =>
1108
- alias($.omitted_get_input, $.non_unary_input_function),
1146
+ direct_input_function: ($) =>
1147
+ choice(field("get", $.simple_get), redirectedInput($, $.simple_get)),
1109
1148
 
1110
- omitted_get_input: ($) =>
1149
+ source_direct_input_function: ($) =>
1111
1150
  choice(
1112
- field("get", omittedGet($)),
1113
- pipedInput($, $.non_unary_expr, omittedGet($)),
1151
+ field("get", alias($.source_simple_get, $.simple_get)),
1152
+ redirectedInput(
1153
+ $,
1154
+ alias($.source_simple_get, $.simple_get),
1155
+ alias($.source_expr, $.expr),
1156
+ ),
1157
+ ),
1158
+
1159
+ source_piped_input_function: ($) =>
1160
+ pipedInput(
1161
+ $,
1162
+ alias($.source_full_non_unary_expr, $.non_unary_expr),
1163
+ alias($.source_simple_get, $.simple_get),
1164
+ ),
1165
+
1166
+ source_unary_input_function: ($) =>
1167
+ pipedInput(
1168
+ $,
1169
+ alias($.source_full_unary_expr, $.unary_expr),
1170
+ alias($.source_simple_get, $.simple_get),
1114
1171
  ),
1115
1172
 
1116
- omitted_unary_get_operand: ($) =>
1117
- alias($.omitted_unary_get_input, $.unary_input_function),
1173
+ piped_input_function: ($) => pipedInput($, $.non_unary_expr, $.simple_get),
1174
+
1175
+ unary_input_function: ($) => pipedInput($, $.unary_expr, $.simple_get),
1176
+
1177
+ simple_get: ($) => simpleGet($),
1178
+
1179
+ source_simple_get: ($) => simpleGet($, true),
1118
1180
 
1119
- omitted_unary_get_input: ($) => pipedInput($, $.unary_expr, omittedGet($)),
1181
+ trailing_incr: () => "++",
1120
1182
 
1121
- postfix_omitted_get: ($) => prec(1, $.getline_keyword),
1183
+ trailing_decr: () => "--",
1122
1184
 
1123
1185
  getline_keyword: ($) => $._getline_word,
1124
1186
 
1125
- in_keyword: ($) => $._in_word,
1187
+ target_getline_keyword: ($) => $._getline_target_word,
1188
+
1189
+ omitted_getline_keyword: ($) => $._getline_omitted_word,
1190
+
1191
+ field_getline_keyword: ($) => $._getline_field_word,
1192
+
1193
+ preferred_target_getline_keyword: ($) => $._getline_prefer_target_word,
1194
+
1195
+ preferred_omitted_getline_keyword: ($) => $._getline_prefer_omitted_word,
1196
+
1197
+ ...Object.fromEntries(
1198
+ [...RESERVED_WORDS.keyword, ...RESERVED_WORDS.membership].map(
1199
+ (keyword) => [`${keyword.toLowerCase()}_keyword`, () => keyword],
1200
+ ),
1201
+ ),
1126
1202
 
1127
1203
  func_name: ($) => $._func_name_word,
1128
1204
 
1129
- builtin_func_name: ($) => $._builtin_func_name_word,
1205
+ builtin_func_name: () => token(choice(...RESERVED_WORDS.builtin)),
1130
1206
 
1131
- name: ($) => $._name_word,
1207
+ name: () => /[A-Za-z_][A-Za-z0-9_]*/,
1132
1208
 
1133
- number: ($) => $._number,
1209
+ number: () =>
1210
+ /(([0-9]+[.][0-9]*|[.][0-9]+)([eE][+-]?[0-9]+)?|[0-9]+[eE][+-]?[0-9]+)[fFlL]?|[0-9]+/,
1134
1211
 
1135
1212
  string: ($) =>
1136
1213
  seq(
@@ -1139,31 +1216,37 @@ export default grammar({
1139
1216
  field("closing", alias($._string_closing, '"')),
1140
1217
  ),
1141
1218
 
1142
- string_content: ($) => $._string_content,
1219
+ string_content: () =>
1220
+ token.immediate(repeat1(choice(/[^"\\\n]/, prec(1, "\0")))),
1143
1221
 
1144
1222
  escape_sequence: ($) => $._string_escape,
1145
1223
 
1146
1224
  ...twoCharacterTokenRules,
1147
1225
 
1226
+ incr: ($) => $._incr_operator,
1227
+
1228
+ decr: ($) => $._decr_operator,
1229
+
1148
1230
  ere: ($) =>
1149
1231
  seq(
1150
1232
  field("opening", alias($._ere_opening_slash, "/")),
1151
- field("expression", $.extended_reg_exp),
1233
+ optional(field("expression", $.extended_reg_exp)),
1152
1234
  field("closing", alias($._ere_closing, "/")),
1153
1235
  ),
1154
1236
 
1155
1237
  ...ereExpressionRules(false),
1156
1238
  ...ereExpressionRules(true),
1157
1239
 
1158
- quoted_character: ($) =>
1159
- alias($.ere_quoted_escape_sequence, $.escape_sequence),
1160
-
1161
- // Aliasing preserves the wrapper even when no other rule shares the token.
1162
- wildcard: () => alias(token.immediate("."), "."),
1240
+ quoted_character: ($) => $._ere_quoted_escape,
1163
1241
 
1164
- left_anchor: () => token.immediate("^"),
1165
-
1166
- right_anchor: () => token.immediate("$"),
1242
+ ordinary_character: ($) =>
1243
+ field(
1244
+ "escape",
1245
+ choice(
1246
+ $.escaped_delimiter,
1247
+ alias($.ere_named_escape_sequence, $.escape_sequence),
1248
+ ),
1249
+ ),
1167
1250
 
1168
1251
  ere_dupl_symbol: ($) =>
1169
1252
  prec.right(
@@ -1174,21 +1257,29 @@ export default grammar({
1174
1257
  token.immediate("?"),
1175
1258
  $._ere_interval,
1176
1259
  ),
1177
- optional(field("modifier", $.repetition_modifier)),
1260
+ optional(
1261
+ field(
1262
+ "modifier",
1263
+ alias(token.immediate("?"), $.repetition_modifier),
1264
+ ),
1265
+ ),
1178
1266
  ),
1179
1267
  ),
1180
1268
 
1181
- repetition_modifier: () => alias(token.immediate("?"), "?"),
1182
-
1183
1269
  _ere_interval: ($) =>
1184
1270
  seq(
1185
1271
  token.immediate("{"),
1186
- $.dup_count,
1187
- optional(seq(token.immediate(","), optional($.dup_count))),
1272
+ field("minimum", $.duplication_count),
1273
+ optional(
1274
+ seq(
1275
+ token.immediate(","),
1276
+ optional(field("maximum", $.duplication_count)),
1277
+ ),
1278
+ ),
1188
1279
  $._ere_close_brace,
1189
1280
  ),
1190
1281
 
1191
- dup_count: ($) => $._ere_dup_count,
1282
+ duplication_count: () => token.immediate(/[0-9]+/),
1192
1283
 
1193
1284
  bracket_expression: ($) =>
1194
1285
  seq(
@@ -1202,16 +1293,13 @@ export default grammar({
1202
1293
  nonmatching_list: ($) => seq(token.immediate(prec(1, "^")), $.bracket_list),
1203
1294
 
1204
1295
  bracket_list: ($) =>
1205
- choice(
1206
- ...ereBracketListAlternatives($, $.follow_list),
1207
- ...ereBracketListAlternatives(
1208
- $,
1296
+ seq(
1297
+ choice(
1298
+ $.follow_list,
1209
1299
  alias($._initial_close_follow_list, $.follow_list),
1210
- ),
1211
- ...ereBracketListAlternatives(
1212
- $,
1213
1300
  alias($._initial_hyphen_follow_list, $.follow_list),
1214
1301
  ),
1302
+ optional(ereClosingHyphen($)),
1215
1303
  ),
1216
1304
 
1217
1305
  ...initialFollowListRules("close", ($) => $._ere_initial_close),
@@ -1219,14 +1307,14 @@ export default grammar({
1219
1307
  ...initialFollowListRules("hyphen", ($) => $._ere_initial_hyphen),
1220
1308
 
1221
1309
  _ere_initial_close: ($) =>
1222
- alias(token.immediate("]"), $.collating_element_content),
1223
-
1224
- // Inlining lets the hyphen alias replace the collating_element wrapper.
1310
+ alias(token.immediate("]"), $.collating_element_single),
1225
1311
  _ere_initial_hyphen: ($) =>
1226
- choice($._ere_bracket_hyphen, ereClosingHyphen($)),
1312
+ choice(
1313
+ alias(token.immediate("-"), $.collating_element_single),
1314
+ alias($._ere_closing_hyphen, $.collating_element_single),
1315
+ ),
1227
1316
 
1228
- follow_list: ($) =>
1229
- choice($.expression_term, seq($.follow_list, $.expression_term)),
1317
+ follow_list: ($) => repeat1($.expression_term),
1230
1318
 
1231
1319
  expression_term: ($) => choice($.single_expression, $.range_expression),
1232
1320
 
@@ -1237,37 +1325,69 @@ export default grammar({
1237
1325
 
1238
1326
  start_range: ($) => seq($.end_range, $._ere_bracket_hyphen),
1239
1327
 
1240
- end_range: ($) => choice($.collating_element, $.collating_symbol),
1328
+ end_range: ($) =>
1329
+ choice(
1330
+ alias($._ere_bracket_character, $.collating_element_single),
1331
+ alias($._ere_bracket_literal_open, $.collating_element_single),
1332
+ $.collating_element,
1333
+ $.collating_symbol,
1334
+ ),
1241
1335
 
1242
1336
  collating_element: ($) =>
1243
1337
  choice(
1244
- alias($._ere_bracket_character, $.collating_element_content),
1245
- alias($._ere_bracket_literal_open, $.collating_element_content),
1246
1338
  $.escaped_delimiter,
1247
1339
  alias($.ere_bracket_escape_sequence, $.escape_sequence),
1248
1340
  ),
1249
1341
 
1250
1342
  collating_symbol: ($) =>
1251
1343
  seq(
1252
- $._ere_open_dot,
1253
1344
  choice(
1254
- alias($._ere_compound_collating_element, $.collating_element),
1255
- $.meta_character,
1345
+ seq(
1346
+ ereCompoundOpening($, "."),
1347
+ field(
1348
+ "element",
1349
+ choice($._ere_raw_collating_element, $.meta_character),
1350
+ ),
1351
+ ),
1352
+ seq(
1353
+ ereCompoundOpening($, ".", true),
1354
+ field(
1355
+ "element",
1356
+ alias($._ere_compound_collating_element, $.collating_element),
1357
+ ),
1358
+ ),
1256
1359
  ),
1257
1360
  $._ere_dot_close,
1258
1361
  ),
1259
1362
 
1260
1363
  equivalence_class: ($) =>
1261
1364
  seq(
1262
- $._ere_open_equal,
1263
- alias($._ere_compound_collating_element, $.collating_element),
1365
+ choice(
1366
+ seq(
1367
+ ereCompoundOpening($, "="),
1368
+ field("element", $._ere_raw_collating_element),
1369
+ ),
1370
+ seq(
1371
+ ereCompoundOpening($, "=", true),
1372
+ field(
1373
+ "element",
1374
+ alias($._ere_compound_collating_element, $.collating_element),
1375
+ ),
1376
+ ),
1377
+ ),
1264
1378
  $._ere_equal_close,
1265
1379
  ),
1266
1380
 
1267
1381
  character_class: ($) =>
1268
- seq($._ere_open_colon, $.class_name, $._ere_colon_close),
1382
+ seq($._ere_open_colon, field("name", $.class_name), $._ere_colon_close),
1383
+
1384
+ _ere_raw_collating_element: ($) =>
1385
+ choice(
1386
+ alias($._ere_compound_single, $.collating_element_single),
1387
+ alias($._ere_compound_multi, $.collating_element_multi),
1388
+ ),
1269
1389
 
1270
- class_name: ($) => $._ere_class_name,
1390
+ class_name: () => token.immediate(/[A-Za-z][A-Za-z0-9]*/),
1271
1391
 
1272
1392
  meta_character: ($) => $._ere_compound_meta_character,
1273
1393
 
@@ -1295,12 +1415,8 @@ export default grammar({
1295
1415
  alias($.ere_bracket_escape_sequence, $.escape_sequence),
1296
1416
  ),
1297
1417
 
1298
- _ere_open_dot: ($) => ereCompoundOpening($, "."),
1299
-
1300
1418
  _ere_dot_close: ($) => ereCompoundClosing($._ere_dot_closing, "."),
1301
1419
 
1302
- _ere_open_equal: ($) => ereCompoundOpening($, "="),
1303
-
1304
1420
  _ere_equal_close: ($) => ereCompoundClosing($._ere_equal_closing, "="),
1305
1421
 
1306
1422
  _ere_open_colon: ($) => ereCompoundOpening($, ":"),
@@ -1311,8 +1427,6 @@ export default grammar({
1311
1427
 
1312
1428
  ere_named_escape_sequence: ($) => $._ere_named_escape,
1313
1429
 
1314
- ere_quoted_escape_sequence: ($) => $._ere_quoted_escape,
1315
-
1316
1430
  ere_octal_escape_sequence: ($) => $._ere_octal_escape,
1317
1431
 
1318
1432
  ere_undefined_escape_sequence: ($) => $._ere_undefined_escape,
@@ -1325,14 +1439,9 @@ export default grammar({
1325
1439
  $._ere_undefined_escape,
1326
1440
  ),
1327
1441
 
1328
- // Tree-sitter rejects the POSIX bracket spelling for these excluded
1329
- // delimiters.
1442
+ // Tree-sitter rejects POSIX bracket spelling for these delimiters.
1330
1443
  _ordinary_character: () => token.immediate(/[^.\x5B\x5C*^$+?{|}()/\n]/),
1331
1444
 
1332
- _ere_ordinary_close_parenthesis: () => token.immediate(")"),
1333
-
1334
- _ere_ordinary_close_brace: () => token.immediate("}"),
1335
-
1336
1445
  _ere_close_parenthesis: () => token.immediate(prec(1, ")")),
1337
1446
 
1338
1447
  _ere_close_brace: () => token.immediate(prec(1, "}")),