@konomanoasa/tree-sitter-posix-awk 0.18.0 → 0.20.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,129 @@
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
- ];
33
-
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
- ];
52
-
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",
22
+ export const FILE_SOURCE_TIER = "additive";
23
+
24
+ export const RESERVED_WORDS = {
25
+ keyword: [
26
+ "BEGIN",
27
+ "END",
28
+ "break",
29
+ "continue",
30
+ "delete",
31
+ "do",
32
+ "else",
33
+ "exit",
34
+ "for",
35
+ "function",
36
+ "if",
37
+ "next",
38
+ "nextfile",
39
+ "print",
40
+ "printf",
41
+ "return",
42
+ "while",
43
+ ],
44
+ membership: ["in"],
45
+ getline: ["getline"],
46
+ builtin: [
47
+ "atan2",
48
+ "close",
49
+ "cos",
50
+ "exp",
51
+ "fflush",
52
+ "gsub",
53
+ "index",
54
+ "int",
55
+ "length",
56
+ "log",
57
+ "match",
58
+ "rand",
59
+ "sin",
60
+ "split",
61
+ "sprintf",
62
+ "sqrt",
63
+ "srand",
64
+ "sub",
65
+ "substr",
66
+ "system",
67
+ "tolower",
68
+ "toupper",
69
+ ],
64
70
  };
65
71
 
66
72
  // Skipping blanks can reach a backslash the external scanner never saw.
67
- const continuationMarker = token.immediate("\\");
73
+ const continuationMarker = () => token.immediate("\\");
74
+
75
+ const signOperator = ($, character) =>
76
+ alias(character === "+" ? $._plus_operator : $._minus_operator, character);
77
+
78
+ const assignmentOperator = ($) =>
79
+ choice(
80
+ "=",
81
+ $.pow_assign,
82
+ $.mod_assign,
83
+ $.mul_assign,
84
+ $.div_assign,
85
+ $.add_assign,
86
+ $.sub_assign,
87
+ );
88
+
89
+ const binaryOperators = ($) => ({
90
+ additive: [signOperator($, "+"), signOperator($, "-")],
91
+ multiplicative: ["*", alias($._division_slash, "/"), "%"],
92
+ exponentiation: ["^"],
93
+ match: ["~", $.no_match],
94
+ comparison: ["<", ">", $.le, $.ge, $.eq, $.ne],
95
+ logical_and: [$.and],
96
+ logical_or: [$.or],
97
+ });
68
98
 
69
- const singleOperator = ($, character) =>
70
- alias($[`_${SINGLE_CHARACTER_OPERATORS[character]}_operator`], character);
99
+ const oneOf = (members) =>
100
+ members.length === 1 ? members[0] : choice(...members);
71
101
 
72
102
  const twoCharacterTokenRules = Object.fromEntries(
73
- TWO_CHARACTER_TOKENS.map((name) => [name, ($) => $[`_${name}_operator`]]),
103
+ Object.entries({
104
+ add_assign: "+=",
105
+ sub_assign: "-=",
106
+ mul_assign: "*=",
107
+ div_assign: "/=",
108
+ mod_assign: "%=",
109
+ pow_assign: "^=",
110
+ or: "||",
111
+ and: "&&",
112
+ no_match: "!~",
113
+ eq: "==",
114
+ le: "<=",
115
+ ge: ">=",
116
+ ne: "!=",
117
+ append: ">>",
118
+ }).map(([name, spelling]) => [name, () => spelling]),
74
119
  );
75
120
 
76
121
  const newlineLayout = ($) => optional($.newline_opt);
77
122
 
78
123
  const afterOptionalNewline = ($, member) => seq(newlineLayout($), member);
79
124
 
80
- // A separate closing-hyphen token avoids competing with range separators.
81
125
  const ereClosingHyphen = ($) => alias($._ere_closing_hyphen, "-");
82
126
 
83
- const ereBracketListAlternatives = ($, followList) => [
84
- followList,
85
- seq(followList, ereClosingHyphen($)),
86
- ];
87
-
88
127
  const ereRangeExpressionWith = ($, startRange) =>
89
128
  seq(
90
129
  startRange,
@@ -95,9 +134,9 @@ const initialFollowListRules = (prefix, firstElement) => {
95
134
  const name = (part) => `_initial_${prefix}_${part}`;
96
135
  return {
97
136
  [name("follow_list")]: ($) =>
98
- choice(
137
+ seq(
99
138
  alias($[name("expression_term")], $.expression_term),
100
- seq(alias($[name("follow_list")], $.follow_list), $.expression_term),
139
+ repeat($.expression_term),
101
140
  ),
102
141
  [name("expression_term")]: ($) =>
103
142
  choice(
@@ -110,12 +149,18 @@ const initialFollowListRules = (prefix, firstElement) => {
110
149
  ereRangeExpressionWith($, alias($[name("start_range")], $.start_range)),
111
150
  [name("start_range")]: ($) =>
112
151
  seq(alias($[name("end_range")], $.end_range), $._ere_bracket_hyphen),
113
- [name("end_range")]: ($) => alias(firstElement($), $.collating_element),
152
+ [name("end_range")]: ($) => firstElement($),
114
153
  };
115
154
  };
116
155
 
117
- const ereCompoundOpening = ($, punctuation) =>
118
- seq(alias($._ere_compound_opening, "["), token.immediate(punctuation));
156
+ const ereCompoundOpening = ($, punctuation, escaped = false) =>
157
+ seq(
158
+ alias(
159
+ escaped ? $._ere_escaped_compound_opening : $._ere_compound_opening,
160
+ "[",
161
+ ),
162
+ token.immediate(punctuation),
163
+ );
119
164
 
120
165
  const ereCompoundClosing = (closing, punctuation) =>
121
166
  seq(alias(closing, punctuation), token.immediate("]"));
@@ -126,61 +171,47 @@ const ereExpressionRules = (insideGroup) => {
126
171
  insideGroup ? alias($[name(rule)], $[rule]) : $[rule];
127
172
  return {
128
173
  [name("extended_reg_exp")]: ($) =>
129
- choice(
174
+ seq(
130
175
  member($, "ere_branch"),
131
- seq(
132
- field("left", member($, "extended_reg_exp")),
133
- field("operator", token.immediate("|")),
134
- field("right", member($, "ere_branch")),
135
- ),
176
+ repeat(seq(token.immediate("|"), member($, "ere_branch"))),
136
177
  ),
137
178
 
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
- ),
179
+ [name("ere_branch")]: ($) => repeat1(member($, "ere_expression")),
146
180
 
147
181
  [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),
182
+ seq(
183
+ choice(
184
+ member($, "one_char_or_coll_elem_ere"),
185
+ token.immediate("^"),
186
+ token.immediate("$"),
187
+ seq(
188
+ field("opening", token.immediate("(")),
189
+ optional(
190
+ field(
191
+ "expression",
192
+ alias($.group_extended_reg_exp, $.extended_reg_exp),
193
+ ),
194
+ ),
195
+ field("closing", $._ere_close_parenthesis),
157
196
  ),
158
- field("closing", $._ere_close_parenthesis),
159
- ),
160
- seq(
161
- field("operand", member($, "ere_expression")),
162
- field("operator", $.ere_dupl_symbol),
163
197
  ),
198
+ repeat($.ere_dupl_symbol),
164
199
  ),
165
200
 
166
201
  [name("one_char_or_coll_elem_ere")]: ($) =>
167
202
  choice(
168
- member($, "ordinary_character"),
203
+ alias($._ordinary_character, $.ordinary_character),
204
+ ...(insideGroup
205
+ ? []
206
+ : [alias(token.immediate(")"), $.ordinary_character)]),
207
+ alias(token.immediate("}"), $.ordinary_character),
208
+ $.ordinary_character,
169
209
  $.quoted_character,
170
- $.wildcard,
210
+ token.immediate("."),
171
211
  $.bracket_expression,
172
212
  alias($.ere_octal_escape_sequence, $.escape_sequence),
173
213
  alias($.ere_undefined_escape_sequence, $.escape_sequence),
174
214
  ),
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
215
  };
185
216
  };
186
217
 
@@ -203,498 +234,539 @@ const controlStatements = ($, body) => [
203
234
  seq($._for_header, field("body", body)),
204
235
  ];
205
236
 
206
- const statementTerminatedBy = ($, terminator) =>
207
- seq(
208
- field("statement", $.terminatable_statement),
209
- field("terminator", terminator),
210
- newlineLayout($),
211
- );
212
-
213
237
  const parenthesized = (member) => seq("(", member, ")");
214
238
 
215
239
  const parenthesizedPrintStatement = ($, keyword) =>
216
- seq(keyword, parenthesized(field("arguments", $.multiple_expr_list)));
217
-
218
- const callArguments = ($) => parenthesized(optional($.expr_list));
219
-
220
- const subscript = (subscripts) => seq("[", subscripts, "]");
221
-
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
- ),
229
- );
230
-
231
- const getWithTarget = ($, target) =>
232
- prec(1, prec.dynamic(1, seq($.getline_keyword, field("target", target))));
233
-
234
- const pipedInput = ($, source, get) =>
235
- prec.right(
236
- PRECEDENCE.field,
237
- seq(field("source", source), singleOperator($, "|"), field("get", get)),
240
+ seq(
241
+ keyword,
242
+ $._print_arguments,
243
+ parenthesized(field("arguments", $.multiple_expr_list)),
238
244
  );
239
245
 
240
- const omittedGet = ($) => alias($.postfix_omitted_get, $.simple_get);
241
-
242
246
  const rawNewlines = ($) => repeat1($.newline);
243
247
 
244
- const EXPRESSION_CONTEXT = {
245
- normal: {
246
- prefix: "normal",
247
- expression: "expr",
248
- unaryExpression: "unary_expr",
249
- nonUnaryExpression: "non_unary_expr",
250
- comparison: true,
251
- input: ($) =>
252
- choice(
253
- $._direct_input_function,
254
- alias($.piped_input_function, $.non_unary_input_function),
255
- ),
256
- unaryInput: true,
257
- },
258
- print: {
259
- prefix: "print",
260
- expression: "print_expr",
261
- unaryExpression: "unary_print_expr",
262
- nonUnaryExpression: "non_unary_print_expr",
263
- comparison: false,
264
- },
265
- field: {
266
- prefix: "field",
267
- expression: "expr",
268
- unaryExpression: "unary_expr",
269
- nonUnaryExpression: "non_unary_expr",
270
- input: ($) => $._direct_input_function,
271
- },
272
- };
273
-
274
- const classTierName = (context, classification, tier) =>
275
- `_${context.prefix}_${classification}_${tier}_expr`;
276
-
277
- // Hidden operand rules leak inner fields into the parent; aliased visible rules
278
- // prevent this.
279
- const classOperandName = (context, classification, tier) =>
280
- `${context.prefix}_${classification}_${tier}_operand`;
281
-
282
- const anyTierName = (context, tier) => `${context.prefix}_${tier}_expr`;
283
-
284
- const classTier = ($, context, classification, tier) =>
285
- $[classTierName(context, classification, tier)];
286
-
287
- const aliasedClassTier = ($, context, classification, tier) =>
288
- alias(
289
- $[classOperandName(context, classification, tier)],
290
- $[
291
- classification === "unary"
292
- ? context.unaryExpression
293
- : context.nonUnaryExpression
294
- ],
295
- );
296
-
297
- const CLASSIFICATIONS = ["unary", "non_unary"];
298
-
299
- const tierHelpers = (context, rules) => ({
300
- unary: (tier) => classTierName(context, "unary", tier),
301
- nonUnary: (tier) => classTierName(context, "non_unary", tier),
302
- addOperand: (classification, tier) =>
303
- Object.assign(rules, classOperandRules(context, classification, tier)),
304
- });
305
-
306
- const aliasedAnyTier = ($, context, tier) =>
307
- alias($[anyTierName(context, tier)], $[context.expression]);
308
-
309
- const nonUnaryAtom = ($, context, lvalue = $._lvalue) => {
310
- const atoms = [
311
- $._parenthesized_expression,
312
- $.number,
313
- $.string,
314
- lvalue,
315
- $._user_function_call,
316
- $._builtin_function_call,
317
- $.builtin_func_name,
318
- $.ere,
319
- ];
320
- if (context.input) {
321
- atoms.push(context.input($));
322
- }
323
- return choice(...atoms);
324
- };
248
+ const listElementTail = ($, element) =>
249
+ seq(",", afterOptionalNewline($, element));
325
250
 
326
- const classOperandRules = (context, classification, tier) => ({
327
- [classOperandName(context, classification, tier)]: ($) =>
328
- prec(1, classTier($, context, classification, tier)),
329
- });
251
+ const boundaryKinds = [
252
+ "atom",
253
+ "name",
254
+ "group",
255
+ "call",
256
+ "subscript",
257
+ "get",
258
+ "target_get",
259
+ "file",
260
+ "field",
261
+ "prefix",
262
+ "postfix",
263
+ "binary",
264
+ "concat",
265
+ "assignment",
266
+ "conditional",
267
+ "branches",
268
+ "pipe",
269
+ "membership",
270
+ "tuple",
271
+ ];
272
+ const boundaryDigits = [0, 1, 2, 3];
273
+ const boundaryExternals = ($) => [
274
+ $._root,
275
+ $._print_root,
276
+ $._for_in,
277
+ $._print_arguments,
278
+ $._begin_child,
279
+ $._end_child,
280
+ $._resume,
281
+ $._next_argument,
282
+ ...boundaryDigits.map((digit) => $[`_save_${digit}`]),
283
+ ...boundaryDigits.map((digit) => $[`_restore_${digit}`]),
284
+ $._class_unary,
285
+ $._class_non_unary,
286
+ ...boundaryKinds.map((kind) => $[`_select_${kind}`]),
287
+ $._local_root,
288
+ $._local_print_root,
289
+ ];
330
290
 
331
- const anyTierRules = (context, tier) => ({
332
- ...classOperandRules(context, "unary", tier),
333
- ...classOperandRules(context, "non_unary", tier),
334
- [anyTierName(context, tier)]: ($) =>
335
- choice(
336
- aliasedClassTier($, context, "unary", tier),
337
- aliasedClassTier($, context, "non_unary", tier),
291
+ const boundaryRules = {};
292
+ for (const [kind, body] of Object.entries({
293
+ expr: ($) => alias($._boundary_expr, $.expr),
294
+ unary: ($) => $.unary_expr,
295
+ non_unary: ($) => $.non_unary_expr,
296
+ print_expr: ($) => alias($._boundary_print_expr, $.print_expr),
297
+ print_unary: ($) => $.unary_print_expr,
298
+ print_non_unary: ($) => $.non_unary_print_expr,
299
+ name: ($) => seq($._select_name, $.name),
300
+ tuple: ($) =>
301
+ seq(
302
+ $._select_group,
303
+ "(",
304
+ field(
305
+ "left",
306
+ alias($._boundary_multiple_arguments, $.multiple_expr_list),
307
+ ),
308
+ ")",
338
309
  ),
339
- });
340
-
341
- const unaryExpressionRules = (context) => {
342
- const rules = anyTierRules(context, "unary");
343
- const { unary, nonUnary, addOperand } = tierHelpers(context, rules);
344
- const not = `_${context.prefix}_not_expr`;
345
- rules[unary("unary")] = ($) =>
310
+ lvalue: ($) => $.lvalue,
311
+ get: ($) => $.simple_get,
312
+ branches: ($) => $._branches,
313
+ print_branches: ($) => $._print_branches,
314
+ })) {
315
+ boundaryRules[`_checkpoint_${kind}`] = ($) =>
346
316
  choice(
347
- ...(context.unaryInput
348
- ? [classTier($, context, "unary", "exponentiation")]
349
- : []),
350
- ...["+", "-"].map((operator) =>
317
+ ...boundaryDigits.map((digit) =>
351
318
  seq(
352
- field("operator", singleOperator($, operator)),
353
- field("operand", aliasedAnyTier($, context, "unary")),
319
+ $[`_save_${digit}`],
320
+ $[`_checkpoint_${kind}`],
321
+ $[`_restore_${digit}`],
354
322
  ),
355
323
  ),
324
+ seq($._begin_child, body($), $._end_child),
356
325
  );
357
- rules[not] = ($) =>
358
- seq(
359
- field("operator", singleOperator($, "!")),
360
- field("operand", aliasedAnyTier($, context, "unary")),
361
- );
362
- rules[nonUnary("unary")] = ($) =>
363
- choice($[nonUnary("exponentiation")], $[not]);
364
-
365
- const exponentiationTail = `_${context.prefix}_exponentiation_tail`;
366
- rules[exponentiationTail] = ($) =>
367
- seq(
368
- $._exponentiation_operator,
369
- field("right", aliasedAnyTier($, context, "unary")),
370
- );
371
- const exponentiationClassifications = context.unaryInput
372
- ? CLASSIFICATIONS
373
- : ["non_unary"];
374
- for (const classification of exponentiationClassifications) {
375
- addOperand(classification, "update");
376
- rules[classTierName(context, classification, "exponentiation")] = ($) =>
377
- choice(
378
- classTier($, context, classification, "update"),
379
- seq(
380
- field("left", aliasedClassTier($, context, classification, "update")),
381
- $[exponentiationTail],
326
+ boundaryRules[`_child_${kind}`] = ($) =>
327
+ seq($[`_checkpoint_${kind}`], $._resume);
328
+ }
329
+
330
+ for (const print of [false, true]) {
331
+ const prefix = print ? "print_" : "";
332
+ const expr = ($) => $[`_child_${prefix}expr`];
333
+ for (const unary of [false, true]) {
334
+ const classification = unary ? "unary" : "non_unary";
335
+ const klass = ($) => $[`_child_${prefix}${classification}`];
336
+ boundaryRules[`${classification}_${prefix}expr`] = ($) =>
337
+ seq(
338
+ unary ? $._class_unary : $._class_non_unary,
339
+ choice(
340
+ seq(
341
+ $._select_prefix,
342
+ field(
343
+ "operator",
344
+ unary
345
+ ? choice(signOperator($, "+"), signOperator($, "-"))
346
+ : alias($._not_operator, "!"),
347
+ ),
348
+ field("operand", expr($)),
349
+ ),
350
+ seq(
351
+ $._select_binary,
352
+ field("left", klass($)),
353
+ field(
354
+ "operator",
355
+ choice(
356
+ ...Object.entries(binaryOperators($)).flatMap(
357
+ ([tier, operators]) =>
358
+ // A unary left operand of ^ is a pipe, which print excludes.
359
+ (print && tier === "comparison") ||
360
+ (print && unary && tier === "exponentiation")
361
+ ? []
362
+ : operators,
363
+ ),
364
+ ),
365
+ ),
366
+ newlineLayout($),
367
+ field("right", expr($)),
368
+ ),
369
+ seq(
370
+ $._select_concat,
371
+ field("left", klass($)),
372
+ field("right", $[`_child_${prefix}non_unary`]),
373
+ ),
374
+ seq(
375
+ $._select_conditional,
376
+ field("condition", klass($)),
377
+ "?",
378
+ $[`_child_${prefix}branches`],
379
+ ),
380
+ seq(
381
+ $._select_membership,
382
+ field("left", klass($)),
383
+ $._membership_operator,
384
+ field("right", $._child_name),
385
+ ),
386
+ ...(!print
387
+ ? [unary ? $.unary_input_function : $.non_unary_input_function]
388
+ : []),
389
+ ...(unary
390
+ ? []
391
+ : [
392
+ seq(
393
+ $._select_tuple,
394
+ $._child_tuple,
395
+ $._membership_operator,
396
+ field("right", $._child_name),
397
+ ),
398
+ seq(
399
+ $._select_atom,
400
+ choice($.number, $.string, $.ere, $.builtin_func_name),
401
+ ),
402
+ $.lvalue,
403
+ seq($._select_group, "(", $._child_expr, ")"),
404
+ seq(
405
+ $._select_call,
406
+ choice($.func_name, $.builtin_func_name),
407
+ "(",
408
+ optional(alias($._boundary_arguments, $.expr_list)),
409
+ ")",
410
+ ),
411
+ seq(
412
+ $._select_postfix,
413
+ field("operand", $._child_lvalue),
414
+ field("operator", choice($.incr, $.decr)),
415
+ ),
416
+ seq(
417
+ $._select_prefix,
418
+ field("operator", choice($.incr, $.decr)),
419
+ field("operand", $._child_lvalue),
420
+ ),
421
+ seq(
422
+ $._select_assignment,
423
+ field("left", $._child_lvalue),
424
+ field("operator", assignmentOperator($)),
425
+ field("right", expr($)),
426
+ ),
427
+ ]),
382
428
  ),
383
429
  );
384
430
  }
385
- if (context.unaryInput) {
386
- rules[unary("update")] = ($) => $.unary_input_function;
387
- }
388
-
389
- rules[nonUnary("update")] = ($) =>
431
+ boundaryRules[`_${prefix}branches`] = ($) =>
432
+ seq(
433
+ $._select_branches,
434
+ field("consequence", expr($)),
435
+ ":",
436
+ field("alternative", expr($)),
437
+ );
438
+ }
439
+ Object.assign(boundaryRules, {
440
+ _boundary_expr: ($) => choice($.unary_expr, $.non_unary_expr),
441
+ _boundary_print_expr: ($) =>
442
+ choice($.unary_print_expr, $.non_unary_print_expr),
443
+ lvalue: ($) =>
390
444
  choice(
391
- $[nonUnary("atom")],
392
- $._prefix_update_expr,
445
+ seq($._select_name, $.name),
393
446
  seq(
394
- field("operand", alias($.postfix_lvalue, $.lvalue)),
395
- field("operator", choice($.incr, $.decr)),
447
+ $._select_subscript,
448
+ $.name,
449
+ "[",
450
+ alias($._boundary_arguments, $.expr_list),
451
+ "]",
396
452
  ),
397
- );
398
-
399
- // A postfix update must win over forwarding its lvalue as a complete atom.
400
- rules[nonUnary("atom")] = ($) =>
401
- nonUnaryAtom($, context, prec(-1, $._lvalue));
402
-
403
- return rules;
404
- };
405
-
406
- const tieredExpressionRules = (context) => {
407
- const rules = {};
408
- const { unary, nonUnary, addOperand } = tierHelpers(context, rules);
409
- const conditionalTail = `_${context.prefix}_conditional_tail`;
410
- const expression = ($) => $[context.expression];
411
- rules[conditionalTail] = ($) =>
453
+ seq(
454
+ $._select_field,
455
+ field("operator", alias($._field_operator, "$")),
456
+ field("operand", $._child_expr),
457
+ ),
458
+ ),
459
+ _boundary_arguments: ($) =>
460
+ choice(
461
+ $._child_expr,
462
+ alias($._boundary_multiple_arguments, $.multiple_expr_list),
463
+ ),
464
+ _boundary_multiple_arguments: ($) =>
412
465
  seq(
413
- "?",
414
- field("consequence", expression($)),
415
- ":",
416
- field("alternative", aliasedAnyTier($, context, "conditional")),
417
- );
466
+ $._child_expr,
467
+ repeat1(seq(",", newlineLayout($), $._next_argument, $._child_expr)),
468
+ ),
469
+ simple_get: ($) =>
470
+ choice(
471
+ seq($._select_get, $.getline_keyword),
472
+ seq(
473
+ $._select_target_get,
474
+ $.getline_keyword,
475
+ field("target", $._child_lvalue),
476
+ ),
477
+ ),
478
+ non_unary_input_function: ($) =>
479
+ choice(
480
+ field("get", $.simple_get),
481
+ seq(
482
+ $._select_file,
483
+ field("get", $._child_get),
484
+ "<",
485
+ field("source", $._child_expr),
486
+ ),
487
+ seq(
488
+ $._select_pipe,
489
+ field("source", $._child_non_unary),
490
+ alias($._getline_pipe, "|"),
491
+ field("get", $._child_get),
492
+ ),
493
+ ),
494
+ unary_input_function: ($) =>
495
+ seq(
496
+ $._select_pipe,
497
+ field("source", $._child_unary),
498
+ alias($._getline_pipe, "|"),
499
+ field("get", $._child_get),
500
+ ),
501
+ getline_keyword: ($) => $._getline_word,
502
+ });
418
503
 
419
- const addAnyTier = (tier) =>
420
- Object.assign(rules, anyTierRules(context, tier));
421
-
422
- const addBinaryTier = (
423
- tier,
424
- nextTier,
425
- operator,
426
- precedence,
427
- { leftAssociative, newlineAfterOperator = false },
428
- ) => {
429
- addAnyTier(nextTier);
430
- const tail = `_${context.prefix}_${tier}_tail`;
431
- rules[tail] = ($) => {
432
- const right = field("right", aliasedAnyTier($, context, nextTier));
433
- return seq(
434
- operator($),
435
- newlineAfterOperator ? afterOptionalNewline($, right) : right,
436
- );
437
- };
438
- for (const classification of CLASSIFICATIONS) {
439
- if (leftAssociative) {
440
- addOperand(classification, tier);
441
- }
442
- const operation = ($) =>
504
+ const localRules = {};
505
+ const localNode = ($, name) => alias($[`local_${name}`], $[name]);
506
+ const localAtoms = ($, print, lvalue = localNode($, "lvalue")) =>
507
+ choice(
508
+ $._local_atom,
509
+ lvalue,
510
+ ...(!print ? [localNode($, "non_unary_input_function")] : []),
511
+ );
512
+ for (const print of [false, true]) {
513
+ const prefix = print ? "print_" : "";
514
+ const expression = ($) => localNode($, `${prefix}expr`);
515
+ const tiers = Object.keys(EXPRESSION).filter(
516
+ (name) => name !== "field" && (!print || name !== "comparison"),
517
+ );
518
+ const classifications = (name) =>
519
+ name === "atom" || EXPRESSION[name].precedence > EXPRESSION.unary.precedence
520
+ ? ["non_unary"]
521
+ : ["unary", "non_unary"];
522
+ const tier = (classification, name) =>
523
+ `_local_${prefix}${classification}_${name}`;
524
+ const operand = (classification, name) =>
525
+ `local_${prefix}${classification}_${name}`;
526
+ const union = (name) => `local_${prefix}${name}`;
527
+ const classified = (classification, name) => ($) =>
528
+ alias(
529
+ $[operand(classification, name)],
530
+ $[`${classification}_${prefix}expr`],
531
+ );
532
+ const referenced = new Set();
533
+ const member = (classification, name) => {
534
+ referenced.add(operand(classification, name));
535
+ return classified(classification, name);
536
+ };
537
+ const any = (name) => {
538
+ referenced.add(union(name));
539
+ for (const classification of classifications(name))
540
+ member(classification, name);
541
+ return ($) => alias($[union(name)], $[`${prefix}expr`]);
542
+ };
543
+ const tierOperations = (classification, name, next) => {
544
+ if (name === "assignment") {
545
+ return ($) =>
546
+ classification === "unary"
547
+ ? []
548
+ : [
549
+ seq(
550
+ field("left", localNode($, "lvalue")),
551
+ field("operator", assignmentOperator($)),
552
+ field("right", expression($)),
553
+ ),
554
+ ];
555
+ }
556
+ if (name === "conditional") {
557
+ const condition = member(classification, next);
558
+ const alternative = any(name);
559
+ return ($) => [
443
560
  seq(
444
- field(
445
- "left",
446
- aliasedClassTier(
447
- $,
448
- context,
449
- classification,
450
- leftAssociative ? tier : nextTier,
451
- ),
452
- ),
453
- $[tail],
454
- );
455
- rules[classTierName(context, classification, tier)] = ($) =>
456
- choice(
457
- classTier($, context, classification, nextTier),
458
- leftAssociative
459
- ? prec.left(precedence, operation($))
460
- : prec(precedence, operation($)),
461
- );
561
+ field("condition", condition($)),
562
+ "?",
563
+ field("consequence", expression($)),
564
+ ":",
565
+ field("alternative", alternative($)),
566
+ ),
567
+ ];
462
568
  }
463
- };
464
-
465
- rules[unary("assignment")] = ($) =>
466
- classTier($, context, "unary", "conditional");
467
- rules[nonUnary("assignment")] = ($) =>
468
- choice(
469
- classTier($, context, "non_unary", "conditional"),
470
- prec.right(
471
- PRECEDENCE.assignment,
569
+ if (name === "membership") {
570
+ const left = member(classification, name);
571
+ return ($) => [
572
+ seq(
573
+ field("left", left($)),
574
+ $._membership_operator,
575
+ field("right", $.name),
576
+ ),
577
+ ...(classification === "non_unary"
578
+ ? [
579
+ seq(
580
+ "(",
581
+ field("left", localNode($, "multiple_expr_list")),
582
+ ")",
583
+ $._membership_operator,
584
+ field("right", $.name),
585
+ ),
586
+ ]
587
+ : []),
588
+ ];
589
+ }
590
+ if (name === "concatenation") {
591
+ const left = member(classification, name);
592
+ const right = member("non_unary", next);
593
+ return ($) => [seq(field("left", left($)), field("right", right($)))];
594
+ }
595
+ if (name === "unary") {
596
+ const operand = any(name);
597
+ return ($) => [
472
598
  seq(
473
- field("left", $._lvalue),
474
599
  field(
475
600
  "operator",
476
- choice(
477
- $.pow_assign,
478
- $.mod_assign,
479
- $.mul_assign,
480
- $.div_assign,
481
- $.add_assign,
482
- $.sub_assign,
483
- singleOperator($, "="),
484
- ),
601
+ classification === "unary"
602
+ ? choice(signOperator($, "+"), signOperator($, "-"))
603
+ : alias($._not_operator, "!"),
485
604
  ),
486
- field("right", expression($)),
605
+ field("operand", operand($)),
487
606
  ),
488
- ),
489
- );
490
-
491
- addAnyTier("conditional");
492
- for (const classification of CLASSIFICATIONS) {
493
- addOperand(classification, "logical_or");
494
- rules[classTierName(context, classification, "conditional")] = ($) =>
495
- choice(
496
- classTier($, context, classification, "logical_or"),
607
+ ];
608
+ }
609
+ if (name === "postfix") {
610
+ return ($) => [
497
611
  seq(
498
- field(
499
- "condition",
500
- aliasedClassTier($, context, classification, "logical_or"),
501
- ),
502
- $[conditionalTail],
612
+ field("operand", alias($.local_postfix_lvalue, $.lvalue)),
613
+ field("operator", choice($.incr, $.decr)),
503
614
  ),
504
- );
505
- }
506
-
507
- addBinaryTier(
508
- "logical_or",
509
- "logical_and",
510
- ($) => $._logical_or_operator,
511
- PRECEDENCE.logicalOr,
512
- { leftAssociative: true, newlineAfterOperator: true },
513
- );
514
-
515
- addBinaryTier(
516
- "logical_and",
517
- "membership",
518
- ($) => $._logical_and_operator,
519
- PRECEDENCE.logicalAnd,
520
- { leftAssociative: true, newlineAfterOperator: true },
521
- );
522
-
523
- const membershipTail = `_${context.prefix}_membership_tail`;
524
- rules[membershipTail] = ($) =>
525
- seq($._membership_operator, field("right", $.name));
526
- for (const classification of CLASSIFICATIONS) {
527
- rules[classTierName(context, classification, "membership")] = ($) => {
528
- const members = [
529
- classTier($, context, classification, "match"),
530
- prec.left(
531
- PRECEDENCE.membership,
532
- seq(
533
- field(
534
- "left",
535
- aliasedClassTier($, context, classification, "membership"),
536
- ),
537
- $[membershipTail],
538
- ),
615
+ seq(
616
+ field("operator", choice($.incr, $.decr)),
617
+ field("operand", localNode($, "lvalue")),
539
618
  ),
540
619
  ];
541
- if (classification === "non_unary") {
542
- members.push(
543
- seq(
544
- parenthesized(field("left", $.multiple_expr_list)),
545
- $._membership_operator,
546
- field("right", $.name),
547
- ),
548
- );
549
- }
550
- return choice(...members);
551
- };
552
- }
553
-
554
- const comparisonTier = context.comparison ? "comparison" : "concatenation";
555
- addBinaryTier(
556
- "match",
557
- comparisonTier,
558
- ($) => $._match_operator,
559
- PRECEDENCE.match,
560
- { leftAssociative: false },
561
- );
562
-
563
- if (context.comparison) {
564
- addBinaryTier(
565
- "comparison",
566
- "concatenation",
567
- ($) => $._comparison_operator,
568
- PRECEDENCE.comparison,
569
- { leftAssociative: false },
620
+ }
621
+ const left = member(
622
+ classification,
623
+ EXPRESSION[name].associativity === "left" ? name : next,
570
624
  );
571
- }
572
-
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")] = ($) =>
625
+ const right = any(EXPRESSION[name].rightOperand ?? next);
626
+ return ($) => [
580
627
  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)] = ($) =>
628
+ field("left", left($)),
629
+ field("operator", oneOf(binaryOperators($)[name])),
630
+ ...(name === "logical_or" || name === "logical_and"
631
+ ? [newlineLayout($)]
632
+ : []),
633
+ field("right", right($)),
634
+ ),
635
+ ];
636
+ };
637
+ const tierRules = {};
638
+ for (const classification of ["unary", "non_unary"]) {
639
+ for (const [index, name] of tiers.entries()) {
640
+ if (!classifications(name).includes(classification)) continue;
641
+ const next = tiers[index + 1] ?? "atom";
642
+ const operations = tierOperations(classification, name, next);
643
+ const fallback = name !== "unary" || classification !== "unary";
644
+ tierRules[tier(classification, name)] = ($) =>
645
+ oneOf([
646
+ ...(fallback ? [$[tier(classification, next)]] : []),
647
+ ...operations($),
648
+ ]);
649
+ }
650
+ }
651
+ const assignments = ["unary", "non_unary"].map((classification) =>
652
+ member(classification, "assignment"),
653
+ );
654
+ for (const name of [...tiers, "atom"]) {
655
+ for (const classification of classifications(name)) {
656
+ if (referenced.has(operand(classification, name)))
657
+ localRules[operand(classification, name)] = ($) =>
658
+ prec(
659
+ EXPRESSION[name]?.precedence ?? EXPRESSION.field.precedence,
660
+ $[tier(classification, name)],
661
+ );
662
+ }
663
+ if (referenced.has(union(name)))
664
+ localRules[union(name)] = ($) =>
595
665
  choice(
596
- $[name(next)],
597
- prec.left(
598
- precedence,
599
- seq(field("left", wrapped($, tier)), $[`_normal_${tier}_tail`]),
666
+ ...classifications(name).map((classification) =>
667
+ classified(classification, name)($),
600
668
  ),
601
669
  );
602
- rules[operand(tier)] = ($) => prec(1, $[name(tier)]);
603
- }
604
670
  }
605
- const concatenationTail = `_${context.prefix}_concatenation_tail`;
606
- rules[concatenationTail] = ($) =>
607
- field("right", aliasedClassTier($, context, "non_unary", "additive"));
608
- for (const classification of CLASSIFICATIONS) {
609
- rules[classTierName(context, classification, "concatenation")] = ($) =>
671
+ Object.assign(localRules, tierRules);
672
+ localRules[tier("non_unary", "atom")] = ($) => localAtoms($, print);
673
+ localRules[`local_${prefix}expr`] = ($) =>
674
+ choice(...assignments.map((assignment) => assignment($)));
675
+ }
676
+ Object.assign(localRules, {
677
+ _local_atom: ($) =>
678
+ choice(
679
+ $.number,
680
+ $.string,
681
+ $.ere,
682
+ $.builtin_func_name,
683
+ seq("(", localNode($, "expr"), ")"),
684
+ prec(
685
+ 1,
686
+ seq(
687
+ choice($.func_name, $.builtin_func_name),
688
+ "(",
689
+ optional(localNode($, "expr_list")),
690
+ ")",
691
+ ),
692
+ ),
693
+ ),
694
+ local_lvalue: ($) =>
695
+ choice(
696
+ seq($.name, optional(seq("[", localNode($, "expr_list"), "]"))),
697
+ seq(
698
+ field("operator", alias($._field_operator, "$")),
699
+ field("operand", alias($.local_field_expr, $.expr)),
700
+ ),
701
+ ),
702
+ local_postfix_lvalue: ($) =>
703
+ prec(
704
+ 1,
610
705
  choice(
611
- 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,
640
- seq(
641
- field(
642
- "left",
643
- aliasedClassTier($, context, classification, "concatenation"),
644
- ),
645
- $[concatenationTail],
646
- ),
706
+ seq($.name, optional(seq("[", localNode($, "expr_list"), "]"))),
707
+ seq(
708
+ field("operator", alias($._field_operator, "$")),
709
+ field("operand", alias($.local_postfix_field_expr, $.expr)),
647
710
  ),
648
- );
649
- }
650
-
651
- addBinaryTier(
652
- "additive",
653
- "multiplicative",
654
- ($) => $._additive_operator,
655
- PRECEDENCE.additive,
656
- { leftAssociative: true },
657
- );
658
-
659
- addBinaryTier(
660
- "multiplicative",
661
- "unary",
662
- ($) => $._multiplicative_operator,
663
- PRECEDENCE.multiplicative,
664
- { leftAssociative: true },
665
- );
666
-
667
- return { ...rules, ...unaryExpressionRules(context) };
668
- };
669
-
670
- const normalExpressionRules = tieredExpressionRules(EXPRESSION_CONTEXT.normal);
671
- const printExpressionRules = tieredExpressionRules(EXPRESSION_CONTEXT.print);
672
- const fieldExpressionRules = unaryExpressionRules(EXPRESSION_CONTEXT.field);
673
-
674
- const listElementTail = ($, element) =>
675
- seq(",", afterOptionalNewline($, element));
711
+ ),
712
+ ),
713
+ local_postfix_field_expr: ($) =>
714
+ alias($.local_postfix_field_atom, $.non_unary_expr),
715
+ local_postfix_field_atom: ($) =>
716
+ prec(1, localAtoms($, false, alias($.local_postfix_lvalue, $.lvalue))),
717
+ local_field_expr: ($) =>
718
+ choice(
719
+ alias($.local_unary_unary, $.unary_expr),
720
+ alias($.local_field_non_unary, $.non_unary_expr),
721
+ ),
722
+ local_field_non_unary: ($) =>
723
+ choice(
724
+ $._local_non_unary_atom,
725
+ seq(
726
+ field("operator", alias($._not_operator, "!")),
727
+ field("operand", alias($.local_unary, $.expr)),
728
+ ),
729
+ seq(
730
+ field("operator", choice($.incr, $.decr)),
731
+ field("operand", localNode($, "lvalue")),
732
+ ),
733
+ ),
734
+ local_expr_list: ($) =>
735
+ choice(localNode($, "expr"), localNode($, "multiple_expr_list")),
736
+ local_multiple_expr_list: ($) =>
737
+ seq(
738
+ localNode($, "expr"),
739
+ repeat1(listElementTail($, localNode($, "expr"))),
740
+ ),
741
+ local_non_unary_input_function: ($) =>
742
+ field("get", localNode($, "simple_get")),
743
+ local_simple_get: ($) =>
744
+ prec.right(
745
+ EXPRESSION.field.precedence,
746
+ seq($.getline_keyword, optional(field("target", localNode($, "lvalue")))),
747
+ ),
748
+ });
676
749
 
677
- export default grammar({
750
+ const definition = {
678
751
  name: "posix_awk",
679
752
 
680
753
  externals: ($) => [
681
- ...KEYWORDS.map((keyword) => $[`${keyword}_keyword`]),
682
- $._name_word,
683
- $._for_in_variable_word,
754
+ ...boundaryExternals($),
684
755
  $._getline_word,
685
- $._in_word,
686
- $._builtin_func_name_word,
687
- $._builtin_call_word,
688
756
  $._func_name_word,
689
- $._number,
690
757
  $._division_slash,
691
758
  $._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,
759
+ $._incr_operator,
760
+ $._decr_operator,
761
+ $._plus_operator,
762
+ $._minus_operator,
763
+ $._field_operator,
764
+ $._not_operator,
765
+ $._getline_pipe,
697
766
  $._ere_compound_opening,
767
+ $._ere_escaped_compound_opening,
768
+ $._ere_compound_single,
769
+ $._ere_compound_multi,
698
770
  $._ere_dot_closing,
699
771
  $._ere_equal_closing,
700
772
  $._ere_colon_closing,
@@ -704,91 +776,53 @@ export default grammar({
704
776
  $._ere_closing,
705
777
  $._string_opening,
706
778
  $._string_closing,
707
- $._string_content,
708
779
  $._string_escape,
709
780
  $._ere_named_escape,
710
781
  $._ere_quoted_escape,
711
782
  $._ere_octal_escape,
712
783
  $._ere_undefined_escape,
713
784
  $._ere_escaped_delimiter,
714
- $._ere_class_name,
715
- $._ere_dup_count,
716
785
  $.comment,
717
- continuationMarker,
786
+ continuationMarker(),
718
787
  $._continuation_newline,
788
+ $._literal_break,
719
789
  $._stray_backslash,
720
790
  $._error_sentinel,
721
791
  ],
722
792
 
793
+ word: ($) => $.name,
794
+
795
+ reserved: {
796
+ global: ($) => [
797
+ ...[...RESERVED_WORDS.keyword, ...RESERVED_WORDS.membership].map(
798
+ (word) => $[`${word.toLowerCase()}_keyword`],
799
+ ),
800
+ $.builtin_func_name,
801
+ ],
802
+ },
803
+
723
804
  extras: ($) => [
724
805
  token(repeat1(choice(" ", "\t"))),
725
806
  $.comment,
726
- continuationMarker,
807
+ continuationMarker(),
727
808
  $._continuation_newline,
809
+ $._literal_break,
728
810
  ],
729
811
 
730
812
  inline: ($) => [
731
- $._lvalue,
732
- $._simple_get,
733
- $._direct_input_function,
734
813
  $._item,
735
- // A recovered tail can retain errors when reused after its prefix changes.
736
- $._normal_conditional_tail,
737
- // Unit-reduction merging must not inherit update fields through atom children.
738
- $._normal_non_unary_update_expr,
739
- $._print_non_unary_update_expr,
740
- $._field_non_unary_update_expr,
741
- $._normal_unary_assignment_expr,
742
- $._normal_unary_update_expr,
743
- $._print_unary_assignment_expr,
744
- ],
745
-
746
- 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.
750
- [$.item_list, $._item_list],
814
+ $._membership_operator,
815
+ ...Object.keys(localRules)
816
+ .filter((name) => name.startsWith("_") && name !== "_local_atom")
817
+ .map((name) => $[name]),
751
818
  ],
819
+ conflicts: ($) => [[$.item_list, $._item_list]],
752
820
 
753
821
  rules: {
754
822
  program: ($) => seq(optional($.item_list), optional($._item)),
755
823
 
756
- _additive_operator: ($) =>
757
- field("operator", choice(singleOperator($, "+"), singleOperator($, "-"))),
758
-
759
- _multiplicative_operator: ($) =>
760
- field(
761
- "operator",
762
- choice(
763
- singleOperator($, "*"),
764
- alias($._division_slash, "/"),
765
- singleOperator($, "%"),
766
- ),
767
- ),
768
-
769
- _exponentiation_operator: ($) => field("operator", singleOperator($, "^")),
770
-
771
- _comparison_operator: ($) =>
772
- field(
773
- "operator",
774
- choice(
775
- singleOperator($, "<"),
776
- $.le,
777
- $.ne,
778
- $.eq,
779
- singleOperator($, ">"),
780
- $.ge,
781
- ),
782
- ),
783
-
784
- _match_operator: ($) => field("operator", choice("~", $.no_match)),
785
-
786
824
  _membership_operator: ($) => field("operator", $.in_keyword),
787
825
 
788
- _logical_and_operator: ($) => field("operator", $.and),
789
-
790
- _logical_or_operator: ($) => field("operator", $.or),
791
-
792
826
  item_list: ($) => $._item_list,
793
827
 
794
828
  _item_list: ($) =>
@@ -829,8 +863,7 @@ export default grammar({
829
863
  ")",
830
864
  ),
831
865
 
832
- param_list: ($) =>
833
- seq($.name, repeat(seq(",", afterOptionalNewline($, $.name)))),
866
+ param_list: ($) => seq($.name, repeat(listElementTail($, $.name))),
834
867
 
835
868
  pattern: ($) => choice($.normal_pattern, $.special_pattern),
836
869
 
@@ -885,13 +918,12 @@ export default grammar({
885
918
 
886
919
  _for_in_clause: ($) =>
887
920
  seq(
888
- field("variable", alias($.for_in_variable, $.name)),
921
+ $._for_in,
922
+ field("variable", $.name),
889
923
  $.in_keyword,
890
924
  field("array", $.name),
891
925
  ),
892
926
 
893
- for_in_variable: ($) => $._for_in_variable_word,
894
-
895
927
  _for_header: ($) =>
896
928
  header(
897
929
  $,
@@ -907,8 +939,11 @@ export default grammar({
907
939
  seq($.action, newlineLayout($)),
908
940
  $._self_terminating_statement,
909
941
  seq(field("terminator", ";"), newlineLayout($)),
910
- statementTerminatedBy($, $.newline),
911
- statementTerminatedBy($, ";"),
942
+ seq(
943
+ field("statement", $.terminatable_statement),
944
+ field("terminator", choice($.newline, ";")),
945
+ newlineLayout($),
946
+ ),
912
947
  ),
913
948
 
914
949
  unterminated_statement: ($) =>
@@ -939,7 +974,7 @@ export default grammar({
939
974
  seq(
940
975
  $.delete_keyword,
941
976
  field("array", $.name),
942
- optional(subscript(field("subscripts", $.expr_list))),
977
+ optional(seq("[", field("subscripts", $.expr_list), "]")),
943
978
  ),
944
979
  $.expr,
945
980
  $.print_statement,
@@ -959,178 +994,29 @@ export default grammar({
959
994
  parenthesizedPrintStatement($, $.printf_keyword),
960
995
  ),
961
996
 
962
- output_redirection: ($) =>
963
- seq(
964
- choice(alias($._output_greater, ">"), $.append, singleOperator($, "|")),
965
- $.expr,
966
- ),
997
+ output_redirection: ($) => seq(choice(">", $.append, "|"), $.expr),
967
998
 
968
999
  print_expr_list: ($) =>
969
1000
  seq($.print_expr, repeat(listElementTail($, $.print_expr))),
970
1001
 
971
1002
  expr_list: ($) => choice($.expr, $.multiple_expr_list),
972
1003
 
973
- multiple_expr_list: ($) =>
974
- seq(
975
- $.expr,
976
- listElementTail($, $.expr),
977
- repeat(listElementTail($, $.expr)),
978
- ),
979
-
980
- print_expr: ($) => choice($.unary_print_expr, $.non_unary_print_expr),
981
-
982
- unary_print_expr: ($) => $._print_unary_assignment_expr,
983
-
984
- non_unary_print_expr: ($) => $._print_non_unary_assignment_expr,
985
-
986
- expr: ($) => choice($.unary_expr, $.non_unary_expr),
1004
+ multiple_expr_list: ($) => seq($.expr, repeat1(listElementTail($, $.expr))),
987
1005
 
988
- unary_expr: ($) => $._normal_unary_assignment_expr,
989
-
990
- non_unary_expr: ($) => $._normal_non_unary_assignment_expr,
991
-
992
- _prefix_update_expr: ($) =>
993
- seq(
994
- field("operator", choice($.incr, $.decr)),
995
- field("operand", $._lvalue),
996
- ),
997
-
998
- ...normalExpressionRules,
999
-
1000
- ...printExpressionRules,
1001
-
1002
- // A pending field operand cannot start a competing pipe source.
1003
- ...fieldExpressionRules,
1004
-
1005
- nonpostfix_field_expr: ($) =>
1006
- choice(
1007
- alias($.normal_unary_field_expr, $.unary_expr),
1008
- alias($.nonpostfix_non_unary_field_expr, $.non_unary_expr),
1006
+ ...Object.fromEntries(
1007
+ [...RESERVED_WORDS.keyword, ...RESERVED_WORDS.membership].map(
1008
+ (keyword) => [`${keyword.toLowerCase()}_keyword`, () => keyword],
1009
1009
  ),
1010
-
1011
- normal_unary_field_expr: ($) =>
1012
- prec(PRECEDENCE.field, $._field_unary_unary_expr),
1013
-
1014
- nonpostfix_non_unary_field_expr: ($) =>
1015
- prec(
1016
- PRECEDENCE.field,
1017
- choice(
1018
- $._field_not_expr,
1019
- $._prefix_update_expr,
1020
- alias($.nonpostfix_lvalue, $.lvalue),
1021
- alias($.nonpostfix_direct_input_function, $.non_unary_input_function),
1022
- ),
1023
- ),
1024
-
1025
- _parenthesized_expression: ($) => parenthesized($.expr),
1026
-
1027
- _user_function_call: ($) => seq($.func_name, callArguments($)),
1028
-
1029
- _builtin_function_call: ($) =>
1030
- seq(alias($.builtin_call_name, $.builtin_func_name), callArguments($)),
1031
-
1032
- builtin_call_name: ($) => $._builtin_call_word,
1033
-
1034
- _lvalue: ($) =>
1035
- choice(
1036
- alias($.postfix_lvalue, $.lvalue),
1037
- alias($.nonpostfix_lvalue, $.lvalue),
1038
- ),
1039
-
1040
- nonpostfix_lvalue: ($) =>
1041
- seq(
1042
- field("operator", "$"),
1043
- field("operand", alias($.nonpostfix_field_expr, $.expr)),
1044
- ),
1045
-
1046
- postfix_lvalue: ($) =>
1047
- choice(
1048
- $.name,
1049
- seq($.name, subscript($.expr_list)),
1050
- seq(
1051
- field("operator", "$"),
1052
- field("operand", alias($.postfix_field_expr, $.expr)),
1053
- ),
1054
- ),
1055
-
1056
- postfix_field_expr: ($) =>
1057
- alias($.postfix_non_unary_field_expr, $.non_unary_expr),
1058
-
1059
- postfix_non_unary_field_expr: ($) =>
1060
- prec(
1061
- PRECEDENCE.field,
1062
- nonUnaryAtom(
1063
- $,
1064
- {
1065
- ...EXPRESSION_CONTEXT.field,
1066
- input: ($) =>
1067
- alias(
1068
- $.postfix_direct_input_function,
1069
- $.non_unary_input_function,
1070
- ),
1071
- },
1072
- alias($.postfix_lvalue, $.lvalue),
1073
- ),
1074
- ),
1075
-
1076
- _direct_input_function: ($) =>
1077
- choice(
1078
- alias($.postfix_direct_input_function, $.non_unary_input_function),
1079
- alias($.nonpostfix_direct_input_function, $.non_unary_input_function),
1080
- ),
1081
-
1082
- nonpostfix_direct_input_function: ($) =>
1083
- directInputFunction($, alias($.nonpostfix_simple_get, $.simple_get)),
1084
-
1085
- postfix_direct_input_function: ($) =>
1086
- directInputFunction($, alias($.postfix_simple_get, $.simple_get)),
1087
-
1088
- piped_input_function: ($) => pipedInput($, $.non_unary_expr, $._simple_get),
1089
-
1090
- unary_input_function: ($) => pipedInput($, $.unary_expr, $._simple_get),
1091
-
1092
- _simple_get: ($) =>
1093
- choice(
1094
- alias($.postfix_simple_get, $.simple_get),
1095
- alias($.nonpostfix_simple_get, $.simple_get),
1096
- ),
1097
-
1098
- nonpostfix_simple_get: ($) =>
1099
- getWithTarget($, alias($.nonpostfix_lvalue, $.lvalue)),
1100
-
1101
- postfix_simple_get: ($) =>
1102
- choice(
1103
- $.getline_keyword,
1104
- getWithTarget($, alias($.postfix_lvalue, $.lvalue)),
1105
- ),
1106
-
1107
- omitted_get_operand: ($) =>
1108
- alias($.omitted_get_input, $.non_unary_input_function),
1109
-
1110
- omitted_get_input: ($) =>
1111
- choice(
1112
- field("get", omittedGet($)),
1113
- pipedInput($, $.non_unary_expr, omittedGet($)),
1114
- ),
1115
-
1116
- omitted_unary_get_operand: ($) =>
1117
- alias($.omitted_unary_get_input, $.unary_input_function),
1118
-
1119
- omitted_unary_get_input: ($) => pipedInput($, $.unary_expr, omittedGet($)),
1120
-
1121
- postfix_omitted_get: ($) => prec(1, $.getline_keyword),
1122
-
1123
- getline_keyword: ($) => $._getline_word,
1124
-
1125
- in_keyword: ($) => $._in_word,
1010
+ ),
1126
1011
 
1127
1012
  func_name: ($) => $._func_name_word,
1128
1013
 
1129
- builtin_func_name: ($) => $._builtin_func_name_word,
1014
+ builtin_func_name: () => token(choice(...RESERVED_WORDS.builtin)),
1130
1015
 
1131
- name: ($) => $._name_word,
1016
+ name: () => /[A-Za-z_][A-Za-z0-9_]*/,
1132
1017
 
1133
- number: ($) => $._number,
1018
+ number: () =>
1019
+ /(([0-9]+[.][0-9]*|[.][0-9]+)([eE][+-]?[0-9]+)?|[0-9]+[eE][+-]?[0-9]+)[fFlL]?|[0-9]+/,
1134
1020
 
1135
1021
  string: ($) =>
1136
1022
  seq(
@@ -1139,31 +1025,37 @@ export default grammar({
1139
1025
  field("closing", alias($._string_closing, '"')),
1140
1026
  ),
1141
1027
 
1142
- string_content: ($) => $._string_content,
1028
+ string_content: () =>
1029
+ token.immediate(repeat1(choice(/[^"\\\n]/, prec(1, "\0")))),
1143
1030
 
1144
1031
  escape_sequence: ($) => $._string_escape,
1145
1032
 
1146
1033
  ...twoCharacterTokenRules,
1147
1034
 
1035
+ incr: ($) => $._incr_operator,
1036
+
1037
+ decr: ($) => $._decr_operator,
1038
+
1148
1039
  ere: ($) =>
1149
1040
  seq(
1150
1041
  field("opening", alias($._ere_opening_slash, "/")),
1151
- field("expression", $.extended_reg_exp),
1042
+ optional(field("expression", $.extended_reg_exp)),
1152
1043
  field("closing", alias($._ere_closing, "/")),
1153
1044
  ),
1154
1045
 
1155
1046
  ...ereExpressionRules(false),
1156
1047
  ...ereExpressionRules(true),
1157
1048
 
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("."), "."),
1163
-
1164
- left_anchor: () => token.immediate("^"),
1049
+ quoted_character: ($) => $._ere_quoted_escape,
1165
1050
 
1166
- right_anchor: () => token.immediate("$"),
1051
+ ordinary_character: ($) =>
1052
+ field(
1053
+ "escape",
1054
+ choice(
1055
+ $.escaped_delimiter,
1056
+ alias($.ere_named_escape_sequence, $.escape_sequence),
1057
+ ),
1058
+ ),
1167
1059
 
1168
1060
  ere_dupl_symbol: ($) =>
1169
1061
  prec.right(
@@ -1174,21 +1066,29 @@ export default grammar({
1174
1066
  token.immediate("?"),
1175
1067
  $._ere_interval,
1176
1068
  ),
1177
- optional(field("modifier", $.repetition_modifier)),
1069
+ optional(
1070
+ field(
1071
+ "modifier",
1072
+ alias(token.immediate("?"), $.repetition_modifier),
1073
+ ),
1074
+ ),
1178
1075
  ),
1179
1076
  ),
1180
1077
 
1181
- repetition_modifier: () => alias(token.immediate("?"), "?"),
1182
-
1183
1078
  _ere_interval: ($) =>
1184
1079
  seq(
1185
1080
  token.immediate("{"),
1186
- $.dup_count,
1187
- optional(seq(token.immediate(","), optional($.dup_count))),
1081
+ field("minimum", $.duplication_count),
1082
+ optional(
1083
+ seq(
1084
+ token.immediate(","),
1085
+ optional(field("maximum", $.duplication_count)),
1086
+ ),
1087
+ ),
1188
1088
  $._ere_close_brace,
1189
1089
  ),
1190
1090
 
1191
- dup_count: ($) => $._ere_dup_count,
1091
+ duplication_count: () => token.immediate(/[0-9]+/),
1192
1092
 
1193
1093
  bracket_expression: ($) =>
1194
1094
  seq(
@@ -1202,16 +1102,13 @@ export default grammar({
1202
1102
  nonmatching_list: ($) => seq(token.immediate(prec(1, "^")), $.bracket_list),
1203
1103
 
1204
1104
  bracket_list: ($) =>
1205
- choice(
1206
- ...ereBracketListAlternatives($, $.follow_list),
1207
- ...ereBracketListAlternatives(
1208
- $,
1105
+ seq(
1106
+ choice(
1107
+ $.follow_list,
1209
1108
  alias($._initial_close_follow_list, $.follow_list),
1210
- ),
1211
- ...ereBracketListAlternatives(
1212
- $,
1213
1109
  alias($._initial_hyphen_follow_list, $.follow_list),
1214
1110
  ),
1111
+ optional(ereClosingHyphen($)),
1215
1112
  ),
1216
1113
 
1217
1114
  ...initialFollowListRules("close", ($) => $._ere_initial_close),
@@ -1219,14 +1116,14 @@ export default grammar({
1219
1116
  ...initialFollowListRules("hyphen", ($) => $._ere_initial_hyphen),
1220
1117
 
1221
1118
  _ere_initial_close: ($) =>
1222
- alias(token.immediate("]"), $.collating_element_content),
1223
-
1224
- // Inlining lets the hyphen alias replace the collating_element wrapper.
1119
+ alias(token.immediate("]"), $.collating_element_single),
1225
1120
  _ere_initial_hyphen: ($) =>
1226
- choice($._ere_bracket_hyphen, ereClosingHyphen($)),
1121
+ choice(
1122
+ alias(token.immediate("-"), $.collating_element_single),
1123
+ alias($._ere_closing_hyphen, $.collating_element_single),
1124
+ ),
1227
1125
 
1228
- follow_list: ($) =>
1229
- choice($.expression_term, seq($.follow_list, $.expression_term)),
1126
+ follow_list: ($) => repeat1($.expression_term),
1230
1127
 
1231
1128
  expression_term: ($) => choice($.single_expression, $.range_expression),
1232
1129
 
@@ -1237,37 +1134,69 @@ export default grammar({
1237
1134
 
1238
1135
  start_range: ($) => seq($.end_range, $._ere_bracket_hyphen),
1239
1136
 
1240
- end_range: ($) => choice($.collating_element, $.collating_symbol),
1137
+ end_range: ($) =>
1138
+ choice(
1139
+ alias($._ere_bracket_character, $.collating_element_single),
1140
+ alias($._ere_bracket_literal_open, $.collating_element_single),
1141
+ $.collating_element,
1142
+ $.collating_symbol,
1143
+ ),
1241
1144
 
1242
1145
  collating_element: ($) =>
1243
1146
  choice(
1244
- alias($._ere_bracket_character, $.collating_element_content),
1245
- alias($._ere_bracket_literal_open, $.collating_element_content),
1246
1147
  $.escaped_delimiter,
1247
1148
  alias($.ere_bracket_escape_sequence, $.escape_sequence),
1248
1149
  ),
1249
1150
 
1250
1151
  collating_symbol: ($) =>
1251
1152
  seq(
1252
- $._ere_open_dot,
1253
1153
  choice(
1254
- alias($._ere_compound_collating_element, $.collating_element),
1255
- $.meta_character,
1154
+ seq(
1155
+ ereCompoundOpening($, "."),
1156
+ field(
1157
+ "element",
1158
+ choice($._ere_raw_collating_element, $.meta_character),
1159
+ ),
1160
+ ),
1161
+ seq(
1162
+ ereCompoundOpening($, ".", true),
1163
+ field(
1164
+ "element",
1165
+ alias($._ere_compound_collating_element, $.collating_element),
1166
+ ),
1167
+ ),
1256
1168
  ),
1257
1169
  $._ere_dot_close,
1258
1170
  ),
1259
1171
 
1260
1172
  equivalence_class: ($) =>
1261
1173
  seq(
1262
- $._ere_open_equal,
1263
- alias($._ere_compound_collating_element, $.collating_element),
1174
+ choice(
1175
+ seq(
1176
+ ereCompoundOpening($, "="),
1177
+ field("element", $._ere_raw_collating_element),
1178
+ ),
1179
+ seq(
1180
+ ereCompoundOpening($, "=", true),
1181
+ field(
1182
+ "element",
1183
+ alias($._ere_compound_collating_element, $.collating_element),
1184
+ ),
1185
+ ),
1186
+ ),
1264
1187
  $._ere_equal_close,
1265
1188
  ),
1266
1189
 
1267
1190
  character_class: ($) =>
1268
- seq($._ere_open_colon, $.class_name, $._ere_colon_close),
1191
+ seq($._ere_open_colon, field("name", $.class_name), $._ere_colon_close),
1192
+
1193
+ _ere_raw_collating_element: ($) =>
1194
+ choice(
1195
+ alias($._ere_compound_single, $.collating_element_single),
1196
+ alias($._ere_compound_multi, $.collating_element_multi),
1197
+ ),
1269
1198
 
1270
- class_name: ($) => $._ere_class_name,
1199
+ class_name: () => token.immediate(/[A-Za-z][A-Za-z0-9]*/),
1271
1200
 
1272
1201
  meta_character: ($) => $._ere_compound_meta_character,
1273
1202
 
@@ -1295,12 +1224,8 @@ export default grammar({
1295
1224
  alias($.ere_bracket_escape_sequence, $.escape_sequence),
1296
1225
  ),
1297
1226
 
1298
- _ere_open_dot: ($) => ereCompoundOpening($, "."),
1299
-
1300
1227
  _ere_dot_close: ($) => ereCompoundClosing($._ere_dot_closing, "."),
1301
1228
 
1302
- _ere_open_equal: ($) => ereCompoundOpening($, "="),
1303
-
1304
1229
  _ere_equal_close: ($) => ereCompoundClosing($._ere_equal_closing, "="),
1305
1230
 
1306
1231
  _ere_open_colon: ($) => ereCompoundOpening($, ":"),
@@ -1311,8 +1236,6 @@ export default grammar({
1311
1236
 
1312
1237
  ere_named_escape_sequence: ($) => $._ere_named_escape,
1313
1238
 
1314
- ere_quoted_escape_sequence: ($) => $._ere_quoted_escape,
1315
-
1316
1239
  ere_octal_escape_sequence: ($) => $._ere_octal_escape,
1317
1240
 
1318
1241
  ere_undefined_escape_sequence: ($) => $._ere_undefined_escape,
@@ -1325,14 +1248,9 @@ export default grammar({
1325
1248
  $._ere_undefined_escape,
1326
1249
  ),
1327
1250
 
1328
- // Tree-sitter rejects the POSIX bracket spelling for these excluded
1329
- // delimiters.
1251
+ // Tree-sitter rejects POSIX bracket spelling for these delimiters.
1330
1252
  _ordinary_character: () => token.immediate(/[^.\x5B\x5C*^$+?{|}()/\n]/),
1331
1253
 
1332
- _ere_ordinary_close_parenthesis: () => token.immediate(")"),
1333
-
1334
- _ere_ordinary_close_brace: () => token.immediate("}"),
1335
-
1336
1254
  _ere_close_parenthesis: () => token.immediate(prec(1, ")")),
1337
1255
 
1338
1256
  _ere_close_brace: () => token.immediate(prec(1, "}")),
@@ -1353,5 +1271,30 @@ export default grammar({
1353
1271
  terminator: ($) => choice(rawNewlines($), seq(";", repeat($.newline))),
1354
1272
 
1355
1273
  newline: () => "\n",
1274
+ ...boundaryRules,
1275
+ ...localRules,
1276
+ expr: ($) =>
1277
+ choice(
1278
+ seq($._root, $._boundary_expr),
1279
+ seq($._local_root, alias($.local_unary_assignment, $.unary_expr)),
1280
+ seq(
1281
+ $._local_root,
1282
+ alias($.local_non_unary_assignment, $.non_unary_expr),
1283
+ ),
1284
+ ),
1285
+ print_expr: ($) =>
1286
+ choice(
1287
+ seq($._print_root, $._boundary_print_expr),
1288
+ seq(
1289
+ $._local_print_root,
1290
+ alias($.local_print_unary_assignment, $.unary_print_expr),
1291
+ ),
1292
+ seq(
1293
+ $._local_print_root,
1294
+ alias($.local_print_non_unary_assignment, $.non_unary_print_expr),
1295
+ ),
1296
+ ),
1356
1297
  },
1357
- });
1298
+ };
1299
+
1300
+ export default globalThis.grammar?.(definition);