@willbooster/tree-sitter-bash 1.1.4 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/grammar.js CHANGED
@@ -10,7 +10,23 @@
10
10
  // expansion. `#` only starts a comment at the beginning of a word, so it is handled separately. Like
11
11
  // bash, only a space, a tab, and a newline separate words; any other whitespace (CR, a no-break space)
12
12
  // is part of a word.
13
- const WORD_BREAKS = [' ', '\\t', '\\n', '|', '&', ';', '(', ')', '<', '>', '"', '\'', '`', '$', '\\\\'];
13
+ const WORD_BREAKS = [
14
+ ' ',
15
+ String.raw`\t`,
16
+ String.raw`\n`,
17
+ '|',
18
+ '&',
19
+ ';',
20
+ '(',
21
+ ')',
22
+ '<',
23
+ '>',
24
+ '"',
25
+ "'",
26
+ '`',
27
+ '$',
28
+ String.raw`\\`,
29
+ ];
14
30
 
15
31
  // Extglob groups nest (`@(a|+([0-9]))`); a regular expression expresses a fixed depth, and three levels
16
32
  // cover every nested pattern in the example corpora.
@@ -19,7 +35,8 @@ const EXTGLOB_GROUP = extglobGroup('[?*+@]', 3);
19
35
  const EXTGLOB_NEGATION = extglobGroup('!', 3);
20
36
 
21
37
  // Text inside `${…}` and subscripts stops before `<(`/`>(`, so a process substitution there is parsed.
22
- const EXPANSION_TEXT_BREAKS = ['}', '$', '`', '"', '\'', '\\\\', '<', '>'];
38
+ const EXPANSION_TEXT_BREAKS = ['}', '$', '`', '"', "'", String.raw`\\`, '<', '>'];
39
+ // oxlint-disable-next-line no-useless-escape -- Tree-sitter's regex parser requires the escaped opening brackets.
23
40
  const SUBSCRIPT_TEXT = /([^\[\]$`"'\\<>]|[<>]+[^(\[\]$`"'\\<>]|\\.)+/;
24
41
 
25
42
  const PREC = {
@@ -43,17 +60,55 @@ const PREC = {
43
60
  };
44
61
 
45
62
  const TEST_UNARY_OPERATORS = [
46
- '-a', '-b', '-c', '-d', '-e', '-f', '-g', '-h', '-k', '-p', '-r', '-s', '-t', '-u', '-w', '-x',
47
- '-G', '-L', '-N', '-O', '-S', '-z', '-n', '-o', '-v', '-R',
63
+ '-a',
64
+ '-b',
65
+ '-c',
66
+ '-d',
67
+ '-e',
68
+ '-f',
69
+ '-g',
70
+ '-h',
71
+ '-k',
72
+ '-p',
73
+ '-r',
74
+ '-s',
75
+ '-t',
76
+ '-u',
77
+ '-w',
78
+ '-x',
79
+ '-G',
80
+ '-L',
81
+ '-N',
82
+ '-O',
83
+ '-S',
84
+ '-z',
85
+ '-n',
86
+ '-o',
87
+ '-v',
88
+ '-R',
48
89
  ];
49
90
  const TEST_BINARY_OPERATORS = [
50
- '-eq', '-ne', '-lt', '-le', '-gt', '-ge', '-nt', '-ot', '-ef', '==', '=', '!=', '<', '>',
91
+ '-eq',
92
+ '-ne',
93
+ '-lt',
94
+ '-le',
95
+ '-gt',
96
+ '-ge',
97
+ '-nt',
98
+ '-ot',
99
+ '-ef',
100
+ '==',
101
+ '=',
102
+ '!=',
103
+ '<',
104
+ '>',
51
105
  ];
52
106
 
107
+ // oxlint-disable-next-line unicorn/prefer-module -- Tree-sitter loads grammar.js as CommonJS.
53
108
  module.exports = grammar({
54
109
  name: 'bash',
55
110
 
56
- externals: $ => [
111
+ externals: ($) => [
57
112
  $.heredoc_start,
58
113
  $._heredoc_body_start,
59
114
  $.heredoc_content,
@@ -88,665 +143,715 @@ module.exports = grammar({
88
143
  $.__error_recovery,
89
144
  ],
90
145
 
91
- extras: $ => [
92
- $.comment,
93
- $.heredoc_body,
94
- /[ \t\n]/,
95
- /\\\n/,
96
- $._line_continuation,
97
- ],
146
+ extras: ($) => [$.comment, $.heredoc_body, /[ \t\n]/, /\\\n/, $._line_continuation],
98
147
 
99
- supertypes: $ => [
100
- $._statement,
101
- $._arithmetic_expression,
102
- ],
148
+ supertypes: ($) => [$._statement, $._arithmetic_expression],
103
149
 
104
- inline: $ => [
105
- $._terminator,
106
- $._command_statement,
107
- ],
150
+ inline: ($) => [$._terminator, $._command_statement],
108
151
 
109
152
  // Until `;;` or `esac`, a case item's statements may belong to a middle item or to the last one.
110
- conflicts: $ => [
111
- [$._statements, $._last_case_item],
112
- ],
153
+ conflicts: ($) => [[$._statements, $._last_case_item]],
113
154
 
114
- word: $ => $.word,
155
+ word: ($) => $.word,
115
156
 
116
157
  rules: {
117
- program: $ => optional($._statements),
158
+ program: ($) => optional($._statements),
118
159
 
119
160
  // Statements
120
161
 
121
- _statements: $ => seq(
122
- repeat(seq($._statement, $._terminator)),
123
- $._statement,
124
- optional($._terminator),
125
- ),
162
+ _statements: ($) => seq(repeat(seq($._statement, $._terminator)), $._statement, optional($._terminator)),
126
163
 
127
- _terminated_statements: $ => repeat1(seq($._statement, $._terminator)),
164
+ _terminated_statements: ($) => repeat1(seq($._statement, $._terminator)),
128
165
 
129
166
  // A newline is significant only where it can end a statement; elsewhere it is whitespace.
130
- _terminator: $ => choice(';', '&', $._newline),
131
-
132
- _statement: $ => choice(
133
- $._command_statement,
134
- $.pipeline,
135
- $.list,
136
- $.negated_command,
137
- $.timed_command,
138
- $.coproc_command,
139
- ),
140
-
141
- _command_statement: $ => choice(
142
- $.command,
143
- $.declaration_command,
144
- $.redirected_statement,
145
- $._compound_command,
146
- $.function_definition,
147
- ),
148
-
149
- _compound_command: $ => choice(
150
- $.compound_statement,
151
- $.subshell,
152
- $.arithmetic_command,
153
- $.test_command,
154
- $.if_statement,
155
- $.while_statement,
156
- $.for_statement,
157
- $.c_style_for_statement,
158
- $.case_statement,
159
- ),
160
-
161
- list: $ => prec.left(1, seq(
162
- field('left', $._statement),
163
- field('operator', choice('&&', '||')),
164
- field('right', $._statement),
165
- )),
166
-
167
- pipeline: $ => prec.left(3, seq(
168
- $._pipeline_element,
169
- repeat1(seq(choice('|', '|&'), $._pipeline_element)),
170
- )),
171
-
172
- _pipeline_element: $ => choice($._command_statement, $.coproc_command),
173
-
174
- negated_command: $ => prec.right(2, seq('!', choice($._command_statement, $.pipeline, $.timed_command))),
175
-
176
- timed_command: $ => prec.right(2, seq(
177
- 'time',
178
- optional(field('option', alias('-p', $.word))),
179
- optional(choice($._command_statement, $.pipeline, $.negated_command)),
180
- )),
181
-
182
- coproc_command: $ => prec.right(seq(
183
- 'coproc',
167
+ _terminator: ($) => choice(';', '&', $._newline),
168
+
169
+ _statement: ($) =>
170
+ choice($._command_statement, $.pipeline, $.list, $.negated_command, $.timed_command, $.coproc_command),
171
+
172
+ _command_statement: ($) =>
173
+ choice($.command, $.declaration_command, $.redirected_statement, $._compound_command, $.function_definition),
174
+
175
+ _compound_command: ($) =>
184
176
  choice(
185
- $.command,
186
- // Like bash (`coproc [NAME] command [redirections]`), a compound body takes redirections.
177
+ $.compound_statement,
178
+ $.subshell,
179
+ $.arithmetic_command,
180
+ $.test_command,
181
+ $.if_statement,
182
+ $.while_statement,
183
+ $.for_statement,
184
+ $.c_style_for_statement,
185
+ $.case_statement
186
+ ),
187
+
188
+ list: ($) =>
189
+ prec.left(
190
+ 1,
191
+ seq(field('left', $._statement), field('operator', choice('&&', '||')), field('right', $._statement))
192
+ ),
193
+
194
+ pipeline: ($) => prec.left(3, seq($._pipeline_element, repeat1(seq(choice('|', '|&'), $._pipeline_element)))),
195
+
196
+ _pipeline_element: ($) => choice($._command_statement, $.coproc_command),
197
+
198
+ negated_command: ($) => prec.right(2, seq('!', choice($._command_statement, $.pipeline, $.timed_command))),
199
+
200
+ timed_command: ($) =>
201
+ prec.right(
202
+ 2,
187
203
  seq(
188
- optional(field('name', $.word)),
189
- $._compound_command,
190
- repeat(field('redirect', $._redirect)),
191
- ),
204
+ 'time',
205
+ optional(field('option', alias('-p', $.word))),
206
+ optional(choice($._command_statement, $.pipeline, $.negated_command))
207
+ )
192
208
  ),
193
- )),
194
209
 
195
- redirected_statement: $ => prec.left(seq(
196
- field('body', choice($._compound_command, $.function_definition)),
197
- repeat1(field('redirect', $._redirect)),
198
- )),
210
+ coproc_command: ($) =>
211
+ prec.right(
212
+ seq(
213
+ 'coproc',
214
+ choice(
215
+ $.command,
216
+ // Like bash (`coproc [NAME] command [redirections]`), a compound body takes redirections.
217
+ seq(optional(field('name', $.word)), $._compound_command, repeat(field('redirect', $._redirect)))
218
+ )
219
+ )
220
+ ),
221
+
222
+ redirected_statement: ($) =>
223
+ prec.left(
224
+ seq(field('body', choice($._compound_command, $.function_definition)), repeat1(field('redirect', $._redirect)))
225
+ ),
199
226
 
200
227
  // Simple commands
201
228
 
202
- command: $ => prec.left(choice(
203
- seq(
204
- repeat($._command_prefix),
205
- field('name', $.command_name),
206
- repeat(choice(field('argument', $._argument), field('redirect', $._redirect))),
229
+ command: ($) =>
230
+ prec.left(
231
+ choice(
232
+ seq(
233
+ repeat($._command_prefix),
234
+ field('name', $.command_name),
235
+ repeat(choice(field('argument', $._argument), field('redirect', $._redirect)))
236
+ ),
237
+ repeat1($._command_prefix)
238
+ )
207
239
  ),
208
- repeat1($._command_prefix),
209
- )),
210
240
 
211
- _command_prefix: $ => choice(field('assignment', $.variable_assignment), field('redirect', $._redirect)),
241
+ _command_prefix: ($) => choice(field('assignment', $.variable_assignment), field('redirect', $._redirect)),
212
242
 
213
- command_name: $ => $._word,
243
+ command_name: ($) => $._word,
214
244
 
215
- declaration_command: $ => prec.left(seq(
216
- repeat($._command_prefix),
217
- field('name', alias(choice('declare', 'typeset', 'export', 'readonly', 'local'), $.command_name)),
218
- repeat(choice(
219
- field('argument', choice($.variable_assignment, $._argument)),
220
- field('redirect', $._redirect),
221
- )),
222
- )),
245
+ declaration_command: ($) =>
246
+ prec.left(
247
+ seq(
248
+ repeat($._command_prefix),
249
+ field('name', alias(choice('declare', 'typeset', 'export', 'readonly', 'local'), $.command_name)),
250
+ repeat(choice(field('argument', choice($.variable_assignment, $._argument)), field('redirect', $._redirect)))
251
+ )
252
+ ),
223
253
 
224
- variable_assignment: $ => seq(
225
- field('name', choice($.variable_name, $.subscript)),
226
- field('operator', token.immediate(choice('=', '+='))),
227
- field('value', choice($._argument, $._hash_value, $.array, $._empty_value)),
228
- ),
254
+ variable_assignment: ($) =>
255
+ seq(
256
+ field('name', choice($.variable_name, $.subscript)),
257
+ field('operator', token.immediate(choice('=', '+='))),
258
+ field('value', choice($._argument, $._hash_value, $.array, $._empty_value))
259
+ ),
229
260
 
230
261
  // A value is not at the start of a word, so a leading `#` is literal (`color=#fff`).
231
- _hash_value: $ => choice(alias($._hash_word, $.word), alias($._hash_concatenation, $.concatenation)),
262
+ _hash_value: ($) => choice(alias($._hash_word, $.word), alias($._hash_concatenation, $.concatenation)),
232
263
 
233
- _hash_concatenation: $ => prec.right(seq(
234
- alias($._hash_word, $.word),
235
- repeat1(seq($._concat, choice(
236
- $._word_part,
237
- alias($._hash_word, $.word),
238
- alias($._negated_extglob_pattern, $.extglob_pattern),
239
- ))),
240
- )),
264
+ _hash_concatenation: ($) =>
265
+ prec.right(
266
+ seq(
267
+ alias($._hash_word, $.word),
268
+ repeat1(
269
+ seq(
270
+ $._concat,
271
+ choice($._word_part, alias($._hash_word, $.word), alias($._negated_extglob_pattern, $.extglob_pattern))
272
+ )
273
+ )
274
+ )
275
+ ),
241
276
 
242
- subscript: $ => seq(
243
- field('name', $.variable_name),
244
- token.immediate('['),
245
- field('index', $._subscript_index),
246
- ']',
247
- ),
277
+ subscript: ($) =>
278
+ seq(field('name', $.variable_name), token.immediate('['), field('index', $._subscript_index), ']'),
248
279
 
249
280
  // Like bash, the index extends to the matching `]`, so it may hold bracketed groups at any depth
250
281
  // (`A[x[1[2]]]`), blanks, and newlines.
251
- _subscript_index: $ => repeat1(choice(
252
- alias(token.immediate(prec(1, SUBSCRIPT_TEXT)), $.word),
253
- alias(token(prec(1, SUBSCRIPT_TEXT)), $.word),
254
- alias($._angle_text, $.word),
255
- $._quoted_or_expansion,
256
- $._subscript_group,
257
- )),
282
+ _subscript_index: ($) =>
283
+ repeat1(
284
+ choice(
285
+ alias(token.immediate(prec(1, SUBSCRIPT_TEXT)), $.word),
286
+ alias(token(prec(1, SUBSCRIPT_TEXT)), $.word),
287
+ alias($._angle_text, $.word),
288
+ $._quoted_or_expansion,
289
+ $._subscript_group
290
+ )
291
+ ),
258
292
 
259
- _subscript_group: $ => seq('[', optional($._subscript_index), ']'),
293
+ _subscript_group: ($) => seq('[', optional($._subscript_index), ']'),
260
294
 
261
295
  // Word parts that carry their own delimiters, usable where a bare word would overrun a closing
262
296
  // `]` or `}`.
263
- _quoted_or_expansion: $ => choice(
264
- $.string,
265
- $.raw_string,
266
- $.ansi_c_string,
267
- $.translated_string,
268
- $.simple_expansion,
269
- $.expansion,
270
- $.command_substitution,
271
- $.arithmetic_expansion,
272
- $.process_substitution,
273
- ),
274
-
275
- array: $ => seq(
276
- token.immediate('('),
277
- repeat($._argument),
278
- ')',
279
- ),
297
+ _quoted_or_expansion: ($) =>
298
+ choice(
299
+ $.string,
300
+ $.raw_string,
301
+ $.ansi_c_string,
302
+ $.translated_string,
303
+ $.simple_expansion,
304
+ $.expansion,
305
+ $.command_substitution,
306
+ $.arithmetic_expansion,
307
+ $.process_substitution
308
+ ),
309
+
310
+ array: ($) => seq(token.immediate('('), repeat($._argument), ')'),
280
311
 
281
312
  // Redirections
282
313
 
283
- _redirect: $ => choice($.file_redirect, $.heredoc_redirect, $.herestring_redirect),
314
+ _redirect: ($) => choice($.file_redirect, $.heredoc_redirect, $.herestring_redirect),
284
315
 
285
- file_redirect: $ => seq(
286
- optional(field('descriptor', $.file_descriptor)),
287
- choice(
288
- seq(
289
- field('operator', choice('<', '>', '>>', '>|', '<>', '&>', '&>>', '<&', '>&')),
290
- field('destination', $._argument),
291
- ),
292
- field('operator', choice('<&-', '>&-')),
316
+ file_redirect: ($) =>
317
+ seq(
318
+ optional(field('descriptor', $.file_descriptor)),
319
+ choice(
320
+ seq(
321
+ field('operator', choice('<', '>', '>>', '>|', '<>', '&>', '&>>', '<&', '>&')),
322
+ field('destination', $._argument)
323
+ ),
324
+ field('operator', choice('<&-', '>&-'))
325
+ )
293
326
  ),
294
- ),
295
327
 
296
- herestring_redirect: $ => seq(
297
- optional(field('descriptor', $.file_descriptor)),
298
- field('operator', '<<<'),
299
- field('value', $._argument),
300
- ),
328
+ herestring_redirect: ($) =>
329
+ seq(optional(field('descriptor', $.file_descriptor)), field('operator', '<<<'), field('value', $._argument)),
301
330
 
302
- heredoc_redirect: $ => seq(
303
- optional(field('descriptor', $.file_descriptor)),
304
- field('operator', choice(alias($._heredoc_arrow, '<<'), alias($._heredoc_arrow_dash, '<<-'))),
305
- field('delimiter', $.heredoc_start),
306
- ),
331
+ heredoc_redirect: ($) =>
332
+ seq(
333
+ optional(field('descriptor', $.file_descriptor)),
334
+ field('operator', choice(alias($._heredoc_arrow, '<<'), alias($._heredoc_arrow_dash, '<<-'))),
335
+ field('delimiter', $.heredoc_start)
336
+ ),
307
337
 
308
338
  // The scanner opens a body at the newline that ends the line holding its `<<` operator, so bodies
309
339
  // appear, in operator order, wherever that newline falls.
310
- heredoc_body: $ => seq(
311
- $._heredoc_body_start,
312
- repeat(choice(
313
- $.heredoc_content,
314
- $.simple_expansion,
315
- $.expansion,
316
- $.command_substitution,
317
- $.arithmetic_expansion,
318
- )),
319
- $.heredoc_end,
320
- ),
340
+ heredoc_body: ($) =>
341
+ seq(
342
+ $._heredoc_body_start,
343
+ repeat(
344
+ choice($.heredoc_content, $.simple_expansion, $.expansion, $.command_substitution, $.arithmetic_expansion)
345
+ ),
346
+ $.heredoc_end
347
+ ),
321
348
 
322
349
  // Compound commands
323
350
 
324
- compound_statement: $ => seq('{', optional($._terminated_statements), '}'),
351
+ compound_statement: ($) => seq('{', optional($._terminated_statements), '}'),
325
352
 
326
- subshell: $ => seq('(', optional($._statements), ')'),
353
+ subshell: ($) => seq('(', optional($._statements), ')'),
327
354
 
328
- arithmetic_command: $ => seq(
329
- '((',
330
- $._substitution_start,
331
- optional($._arithmetic_expression),
332
- $._substitution_end,
333
- '))',
334
- ),
355
+ arithmetic_command: ($) =>
356
+ seq('((', $._substitution_start, optional($._arithmetic_expression), $._substitution_end, '))'),
335
357
 
336
- test_command: $ => seq('[[', $._test_expression, ']]'),
358
+ test_command: ($) => seq('[[', $._test_expression, ']]'),
337
359
 
338
- _test_expression: $ => choice(
339
- $._word,
340
- $.unary_test,
341
- $.binary_test,
342
- $.regex_test,
343
- alias($._test_negation, $.negated_test),
344
- alias($._test_logical, $.logical_test),
345
- alias($._test_parenthesized, $.parenthesized_test),
346
- ),
360
+ _test_expression: ($) =>
361
+ choice(
362
+ $._word,
363
+ $.unary_test,
364
+ $.binary_test,
365
+ $.regex_test,
366
+ alias($._test_negation, $.negated_test),
367
+ alias($._test_logical, $.logical_test),
368
+ alias($._test_parenthesized, $.parenthesized_test)
369
+ ),
347
370
 
348
- unary_test: $ => prec(PREC.UNARY, seq(
349
- field('operator', alias(choice(...TEST_UNARY_OPERATORS), $.test_operator)),
350
- field('operand', $._argument),
351
- )),
371
+ unary_test: ($) =>
372
+ prec(
373
+ PREC.UNARY,
374
+ seq(field('operator', alias(choice(...TEST_UNARY_OPERATORS), $.test_operator)), field('operand', $._argument))
375
+ ),
352
376
 
353
377
  // The right side of `==`/`!=` is a pattern matched as if extglob were enabled.
354
- binary_test: $ => prec(PREC.COMPARE, seq(
355
- field('left', $._word),
356
- field('operator', alias(choice(...TEST_BINARY_OPERATORS), $.test_operator)),
357
- field('right', $._argument),
358
- )),
378
+ binary_test: ($) =>
379
+ prec(
380
+ PREC.COMPARE,
381
+ seq(
382
+ field('left', $._word),
383
+ field('operator', alias(choice(...TEST_BINARY_OPERATORS), $.test_operator)),
384
+ field('right', $._argument)
385
+ )
386
+ ),
359
387
 
360
388
  // Like bash, the right side is one word whose parenthesized groups may hold blanks and
361
389
  // metacharacters; its quotes and expansions are parsed as in any other word.
362
- regex_test: $ => prec(PREC.COMPARE, seq(
363
- field('left', $._word),
364
- field('operator', '=~'),
365
- $._regex_start,
366
- field('right', choice($._regex_part, alias($._regex_concatenation, $.concatenation))),
367
- )),
368
-
369
- _regex_concatenation: $ => prec.right(seq($._regex_part, repeat1(seq($._regex_concat, $._regex_part)))),
370
-
371
- _regex_part: $ => choice($.regex, $._quoted_or_expansion),
372
-
373
- _test_negation: $ => prec(PREC.UNARY, seq('!', $._test_expression)),
374
-
375
- _test_logical: $ => choice(
376
- prec.left(PREC.LOGICAL_AND, seq($._test_expression, '&&', $._test_expression)),
377
- prec.left(PREC.LOGICAL_OR, seq($._test_expression, '||', $._test_expression)),
378
- ),
379
-
380
- _test_parenthesized: $ => seq('(', $._test_expression, ')'),
381
-
382
- if_statement: $ => seq(
383
- 'if',
384
- field('condition', $._terminated_statements),
385
- 'then',
386
- optional(field('consequence', $._terminated_statements)),
387
- repeat($.elif_clause),
388
- optional($.else_clause),
389
- 'fi',
390
- ),
391
-
392
- elif_clause: $ => seq(
393
- 'elif',
394
- field('condition', $._terminated_statements),
395
- 'then',
396
- optional(field('consequence', $._terminated_statements)),
397
- ),
398
-
399
- else_clause: $ => seq('else', optional($._terminated_statements)),
400
-
401
- while_statement: $ => seq(
402
- field('keyword', choice('while', 'until')),
403
- field('condition', $._terminated_statements),
404
- field('body', $.do_group),
405
- ),
406
-
407
- do_group: $ => seq('do', optional($._terminated_statements), 'done'),
408
-
409
- for_statement: $ => seq(
410
- field('keyword', choice('for', 'select')),
411
- field('variable', alias($.word, $.variable_name)),
412
- // Without a word list the separator is optional (`for i do …`); a newline may precede `in`.
390
+ regex_test: ($) =>
391
+ prec(
392
+ PREC.COMPARE,
393
+ seq(
394
+ field('left', $._word),
395
+ field('operator', '=~'),
396
+ $._regex_start,
397
+ field('right', choice($._regex_part, alias($._regex_concatenation, $.concatenation)))
398
+ )
399
+ ),
400
+
401
+ _regex_concatenation: ($) => prec.right(seq($._regex_part, repeat1(seq($._regex_concat, $._regex_part)))),
402
+
403
+ _regex_part: ($) => choice($.regex, $._quoted_or_expansion),
404
+
405
+ _test_negation: ($) => prec(PREC.UNARY, seq('!', $._test_expression)),
406
+
407
+ _test_logical: ($) =>
413
408
  choice(
414
- seq(optional($._newline), 'in', repeat(field('value', $._argument)), $._terminator),
409
+ prec.left(PREC.LOGICAL_AND, seq($._test_expression, '&&', $._test_expression)),
410
+ prec.left(PREC.LOGICAL_OR, seq($._test_expression, '||', $._test_expression))
411
+ ),
412
+
413
+ _test_parenthesized: ($) => seq('(', $._test_expression, ')'),
414
+
415
+ if_statement: ($) =>
416
+ seq(
417
+ 'if',
418
+ field('condition', $._terminated_statements),
419
+ 'then',
420
+ optional(field('consequence', $._terminated_statements)),
421
+ repeat($.elif_clause),
422
+ optional($.else_clause),
423
+ 'fi'
424
+ ),
425
+
426
+ elif_clause: ($) =>
427
+ seq(
428
+ 'elif',
429
+ field('condition', $._terminated_statements),
430
+ 'then',
431
+ optional(field('consequence', $._terminated_statements))
432
+ ),
433
+
434
+ else_clause: ($) => seq('else', optional($._terminated_statements)),
435
+
436
+ while_statement: ($) =>
437
+ seq(
438
+ field('keyword', choice('while', 'until')),
439
+ field('condition', $._terminated_statements),
440
+ field('body', $.do_group)
441
+ ),
442
+
443
+ do_group: ($) => seq('do', optional($._terminated_statements), 'done'),
444
+
445
+ for_statement: ($) =>
446
+ seq(
447
+ field('keyword', choice('for', 'select')),
448
+ field('variable', alias($.word, $.variable_name)),
449
+ // Without a word list the separator is optional (`for i do …`); a newline may precede `in`.
450
+ choice(
451
+ seq(optional($._newline), 'in', repeat(field('value', $._argument)), $._terminator),
452
+ optional($._terminator)
453
+ ),
454
+ field('body', choice($.do_group, $.compound_statement))
455
+ ),
456
+
457
+ c_style_for_statement: ($) =>
458
+ seq(
459
+ 'for',
460
+ '((',
461
+ $._substitution_start,
462
+ field('initializer', optional($._arithmetic_expression)),
463
+ ';',
464
+ field('condition', optional($._arithmetic_expression)),
465
+ ';',
466
+ field('update', optional($._arithmetic_expression)),
467
+ $._substitution_end,
468
+ '))',
415
469
  optional($._terminator),
470
+ field('body', choice($.do_group, $.compound_statement))
416
471
  ),
417
- field('body', choice($.do_group, $.compound_statement)),
418
- ),
419
-
420
- c_style_for_statement: $ => seq(
421
- 'for',
422
- '((',
423
- $._substitution_start,
424
- field('initializer', optional($._arithmetic_expression)),
425
- ';',
426
- field('condition', optional($._arithmetic_expression)),
427
- ';',
428
- field('update', optional($._arithmetic_expression)),
429
- $._substitution_end,
430
- '))',
431
- optional($._terminator),
432
- field('body', choice($.do_group, $.compound_statement)),
433
- ),
434
-
435
- case_statement: $ => seq(
436
- 'case',
437
- field('value', $._argument),
438
- 'in',
439
- repeat($.case_item),
440
- optional(alias($._last_case_item, $.case_item)),
441
- 'esac',
442
- ),
443
-
444
- case_item: $ => seq(
445
- $._case_patterns,
446
- optional($._statements),
447
- field('terminator', choice(';;', ';&', ';;&')),
448
- ),
472
+
473
+ case_statement: ($) =>
474
+ seq(
475
+ 'case',
476
+ field('value', $._argument),
477
+ 'in',
478
+ repeat($.case_item),
479
+ optional(alias($._last_case_item, $.case_item)),
480
+ 'esac'
481
+ ),
482
+
483
+ case_item: ($) => seq($._case_patterns, optional($._statements), field('terminator', choice(';;', ';&', ';;&'))),
449
484
 
450
485
  // Only the last item may omit its terminator, but its statements still end before `esac`, which is
451
486
  // otherwise an argument (`a) echo esac`).
452
- _last_case_item: $ => seq($._case_patterns, optional($._terminated_statements)),
487
+ _last_case_item: ($) => seq($._case_patterns, optional($._terminated_statements)),
453
488
 
454
- _case_patterns: $ => seq(
455
- optional('('),
456
- field('pattern', $._argument),
457
- repeat(seq('|', field('pattern', $._argument))),
458
- ')',
459
- ),
489
+ _case_patterns: ($) =>
490
+ seq(optional('('), field('pattern', $._argument), repeat(seq('|', field('pattern', $._argument))), ')'),
460
491
 
461
- function_definition: $ => seq(
462
- choice(
463
- seq('function', field('name', $.word), optional(seq('(', ')'))),
464
- seq(field('name', $.word), '(', ')'),
492
+ function_definition: ($) =>
493
+ seq(
494
+ choice(seq('function', field('name', $.word), optional(seq('(', ')'))), seq(field('name', $.word), '(', ')')),
495
+ field('body', $._compound_command)
465
496
  ),
466
- field('body', $._compound_command),
467
- ),
468
497
 
469
498
  // Words
470
499
 
471
- _word: $ => choice($._word_part, $.concatenation),
500
+ _word: ($) => choice($._word_part, $.concatenation),
472
501
 
473
502
  // Outside command position a leading `!(` is a negated extglob group rather than `!` negating a
474
503
  // subshell.
475
- _argument: $ => choice(
476
- $._word,
477
- alias($._negated_extglob_word, $.word),
478
- alias($._negated_extglob_pattern, $.extglob_pattern),
479
- alias($._negated_extglob_concatenation, $.concatenation),
480
- ),
504
+ _argument: ($) =>
505
+ choice(
506
+ $._word,
507
+ alias($._negated_extglob_word, $.word),
508
+ alias($._negated_extglob_pattern, $.extglob_pattern),
509
+ alias($._negated_extglob_concatenation, $.concatenation)
510
+ ),
481
511
 
482
512
  // A word led by a negated group (`!($x)b`, `!(a)$x`).
483
- _negated_extglob_concatenation: $ => prec.right(seq(
484
- choice(alias($._negated_extglob_pattern, $.extglob_pattern), alias($._negated_extglob_word, $.word)),
485
- repeat1(seq($._concat, choice(
486
- $._word_part,
487
- alias($._hash_word, $.word),
488
- alias($._negated_extglob_pattern, $.extglob_pattern),
489
- ))),
490
- )),
491
-
492
- _negated_extglob_word: _ => token(prec(1, seq(
493
- EXTGLOB_NEGATION,
494
- repeat(choice(noneOf(...WORD_BREAKS), /\\[^\n]/, EXTGLOB_GROUP, EXTGLOB_NEGATION)),
495
- ))),
496
-
497
- concatenation: $ => prec.right(seq(
498
- $._word_part,
499
- repeat1(seq($._concat, choice(
500
- $._word_part,
501
- alias($._hash_word, $.word),
502
- alias($._negated_extglob_pattern, $.extglob_pattern),
503
- ))),
504
- )),
505
-
506
- _word_part: $ => choice(
507
- $.word,
508
- // Literal text that the scanner ends before a structured extglob group (`foo@($x)`).
509
- alias($._extglob_prefix, $.word),
510
- $.extglob_pattern,
511
- $.string,
512
- $.raw_string,
513
- $.ansi_c_string,
514
- $.translated_string,
515
- $.simple_expansion,
516
- $.expansion,
517
- $.command_substitution,
518
- $.arithmetic_expansion,
519
- $.process_substitution,
520
- alias($._bare_dollar, $.word),
521
- ),
513
+ _negated_extglob_concatenation: ($) =>
514
+ prec.right(
515
+ seq(
516
+ choice(alias($._negated_extglob_pattern, $.extglob_pattern), alias($._negated_extglob_word, $.word)),
517
+ repeat1(
518
+ seq(
519
+ $._concat,
520
+ choice($._word_part, alias($._hash_word, $.word), alias($._negated_extglob_pattern, $.extglob_pattern))
521
+ )
522
+ )
523
+ )
524
+ ),
525
+
526
+ _negated_extglob_word: () =>
527
+ token(
528
+ prec(
529
+ 1,
530
+ seq(EXTGLOB_NEGATION, repeat(choice(noneOf(...WORD_BREAKS), /\\[^\n]/, EXTGLOB_GROUP, EXTGLOB_NEGATION)))
531
+ )
532
+ ),
533
+
534
+ concatenation: ($) =>
535
+ prec.right(
536
+ seq(
537
+ $._word_part,
538
+ repeat1(
539
+ seq(
540
+ $._concat,
541
+ choice($._word_part, alias($._hash_word, $.word), alias($._negated_extglob_pattern, $.extglob_pattern))
542
+ )
543
+ )
544
+ )
545
+ ),
546
+
547
+ _word_part: ($) =>
548
+ choice(
549
+ $.word,
550
+ // Literal text that the scanner ends before a structured extglob group (`foo@($x)`).
551
+ alias($._extglob_prefix, $.word),
552
+ $.extglob_pattern,
553
+ $.string,
554
+ $.raw_string,
555
+ $.ansi_c_string,
556
+ $.translated_string,
557
+ $.simple_expansion,
558
+ $.expansion,
559
+ $.command_substitution,
560
+ $.arithmetic_expansion,
561
+ $.process_substitution,
562
+ alias($._bare_dollar, $.word)
563
+ ),
522
564
 
523
565
  // An extglob group (`@(a|b)`) is part of the word; `!(` only inside a word, since a leading `!`
524
566
  // before `(` negates a subshell.
525
- word: _ => token(seq(
526
- choice(noneOf('#', ...WORD_BREAKS), /\\[^\n]/, EXTGLOB_GROUP),
527
- repeat(choice(noneOf(...WORD_BREAKS), /\\[^\n]/, EXTGLOB_GROUP, EXTGLOB_NEGATION)),
528
- )),
567
+ word: () =>
568
+ token(
569
+ seq(
570
+ choice(noneOf('#', ...WORD_BREAKS), /\\[^\n]/, EXTGLOB_GROUP),
571
+ repeat(choice(noneOf(...WORD_BREAKS), /\\[^\n]/, EXTGLOB_GROUP, EXTGLOB_NEGATION))
572
+ )
573
+ ),
529
574
 
530
575
  // A `#` inside a word (`a$b#c`) is literal; only a `#` that begins a word starts a comment.
531
576
  // A group whose alternatives hold expansions (`@($(cmd)|b)`); a literal group stays inside a
532
577
  // `word`, whose token is longer than the group's opening token.
533
- extglob_pattern: $ => seq(
534
- field('operator', choice('?(', '*(', '+(', '@(')),
535
- optional($._extglob_alternative),
536
- repeat(seq('|', optional($._extglob_alternative))),
537
- ')',
538
- ),
539
-
540
- _negated_extglob_pattern: $ => seq(
541
- field('operator', '!('),
542
- optional($._extglob_alternative),
543
- repeat(seq('|', optional($._extglob_alternative))),
544
- ')',
545
- ),
578
+ extglob_pattern: ($) =>
579
+ seq(
580
+ field('operator', choice('?(', '*(', '+(', '@(')),
581
+ optional($._extglob_alternative),
582
+ repeat(seq('|', optional($._extglob_alternative))),
583
+ ')'
584
+ ),
585
+
586
+ _negated_extglob_pattern: ($) =>
587
+ seq(
588
+ field('operator', '!('),
589
+ optional($._extglob_alternative),
590
+ repeat(seq('|', optional($._extglob_alternative))),
591
+ ')'
592
+ ),
546
593
 
547
594
  // An alternative may hold unquoted blanks, so each of its words is an `alternative` field; like
548
595
  // an assignment value, a word there may start with `#` (`@($x #b)`).
549
- _extglob_alternative: $ => repeat1(field('alternative', choice($._argument, $._hash_value))),
596
+ _extglob_alternative: ($) => repeat1(field('alternative', choice($._argument, $._hash_value))),
550
597
 
551
- _hash_word: _ => token(prec(1, seq(
552
- '#',
553
- repeat(choice(noneOf(...WORD_BREAKS), /\\[^\n]/, EXTGLOB_GROUP, EXTGLOB_NEGATION)),
554
- ))),
598
+ _hash_word: () =>
599
+ token(prec(1, seq('#', repeat(choice(noneOf(...WORD_BREAKS), /\\[^\n]/, EXTGLOB_GROUP, EXTGLOB_NEGATION))))),
555
600
 
556
- string: $ => seq(
557
- '"',
558
- repeat(choice(
559
- $.string_content,
560
- $.simple_expansion,
561
- $.expansion,
562
- $.command_substitution,
563
- $.arithmetic_expansion,
564
- )),
565
- '"',
566
- ),
601
+ string: ($) =>
602
+ seq(
603
+ '"',
604
+ repeat(
605
+ choice($.string_content, $.simple_expansion, $.expansion, $.command_substitution, $.arithmetic_expansion)
606
+ ),
607
+ '"'
608
+ ),
567
609
 
568
610
  // Where a heredoc inside `$( )` ends on a line that holds the `)`, bash reads the rest of that line
569
611
  // with its line continuations removed, even inside single quotes, and reads the bodies still queued
570
612
  // before that line right after it, even where it ends inside quotes; the scanner then splits the
571
613
  // quoted text around the continuations and the bodies.
572
- raw_string: $ => choice(
573
- /'[^']*'/,
574
- seq($._closer_line_raw_string_start, repeat(alias($._closer_line_raw_string_content, $.string_content)), '\''),
575
- ),
614
+ raw_string: ($) =>
615
+ choice(
616
+ /'[^']*'/,
617
+ seq($._closer_line_raw_string_start, repeat(alias($._closer_line_raw_string_content, $.string_content)), "'")
618
+ ),
576
619
 
577
- ansi_c_string: $ => choice(
578
- /\$'([^'\\]|\\(.|\n))*'/,
579
- seq(
580
- $._closer_line_ansi_c_string_start,
581
- repeat(alias($._closer_line_ansi_c_string_content, $.string_content)),
582
- '\'',
583
- ),
584
- ),
585
-
586
- translated_string: $ => seq('$"', repeat(choice(
587
- $.string_content,
588
- $.simple_expansion,
589
- $.expansion,
590
- $.command_substitution,
591
- $.arithmetic_expansion,
592
- )), '"'),
593
-
594
- simple_expansion: $ => seq(
595
- '$',
620
+ ansi_c_string: ($) =>
596
621
  choice(
597
- alias(token.immediate(/[A-Za-z_][A-Za-z0-9_]*/), $.variable_name),
598
- alias(token.immediate(/[0-9*@#?$!_-]/), $.special_variable_name),
622
+ /\$'([^'\\]|\\(.|\n))*'/,
623
+ seq(
624
+ $._closer_line_ansi_c_string_start,
625
+ repeat(alias($._closer_line_ansi_c_string_content, $.string_content)),
626
+ "'"
627
+ )
628
+ ),
629
+
630
+ translated_string: ($) =>
631
+ seq(
632
+ '$"',
633
+ repeat(
634
+ choice($.string_content, $.simple_expansion, $.expansion, $.command_substitution, $.arithmetic_expansion)
635
+ ),
636
+ '"'
637
+ ),
638
+
639
+ simple_expansion: ($) =>
640
+ seq(
641
+ '$',
642
+ choice(
643
+ alias(token.immediate(/[A-Za-z_][A-Za-z0-9_]*/), $.variable_name),
644
+ alias(token.immediate(/[0-9*@#?$!_-]/), $.special_variable_name)
645
+ )
599
646
  ),
600
- ),
601
647
 
602
648
  // `${#}` and `${!}` are the special parameters `#` and `!`; with a following name they are the
603
649
  // length and indirection prefixes.
604
- expansion: $ => seq(
605
- '${',
650
+ expansion: ($) =>
651
+ seq(
652
+ '${',
653
+ choice(
654
+ seq(
655
+ optional(field('prefix', alias(token.immediate(choice('#', '!')), $.operator))),
656
+ field(
657
+ 'parameter',
658
+ choice(
659
+ alias(token.immediate(/[A-Za-z_][A-Za-z0-9_]*/), $.variable_name),
660
+ alias(token.immediate(/[0-9]+|[*@?$_-]/), $.special_variable_name),
661
+ alias(token.immediate(choice('#', '!')), $.special_variable_name),
662
+ alias($._expansion_subscript, $.subscript)
663
+ )
664
+ ),
665
+ optional($._expansion_operation)
666
+ ),
667
+ field('parameter', alias(token.immediate(choice('#', '!')), $.special_variable_name)),
668
+ // Bash rejects `${}` and a body without a parameter (`${=1}`, `${"x"}`) only when expanding. A
669
+ // `$name` expansion is left out, since its `$` would tie with the special parameter in `${$}`.
670
+ repeat(
671
+ field(
672
+ 'argument',
673
+ choice(
674
+ alias($._expansion_text, $.word),
675
+ alias($._blank_expansion_text, $.word),
676
+ alias($._angle_text, $.word),
677
+ $.string,
678
+ $.raw_string,
679
+ $.ansi_c_string,
680
+ $.translated_string,
681
+ $.command_substitution
682
+ )
683
+ )
684
+ )
685
+ ),
686
+ '}'
687
+ ),
688
+
689
+ _expansion_subscript: ($) =>
690
+ seq(
691
+ field('name', alias(token.immediate(/[A-Za-z_][A-Za-z0-9_]*/), $.variable_name)),
692
+ repeat1(seq(token.immediate('['), field('index', $._subscript_index), ']'))
693
+ ),
694
+
695
+ _expansion_operation: ($) =>
606
696
  choice(
607
697
  seq(
608
- optional(field('prefix', alias(token.immediate(choice('#', '!')), $.operator))),
609
- field('parameter', choice(
610
- alias(token.immediate(/[A-Za-z_][A-Za-z0-9_]*/), $.variable_name),
611
- alias(token.immediate(/[0-9]+|[*@?$_-]/), $.special_variable_name),
612
- alias(token.immediate(choice('#', '!')), $.special_variable_name),
613
- alias($._expansion_subscript, $.subscript),
614
- )),
615
- optional($._expansion_operation),
698
+ field(
699
+ 'operator',
700
+ alias(
701
+ token.immediate(
702
+ choice(
703
+ ':-',
704
+ '-',
705
+ ':=',
706
+ '=',
707
+ ':?',
708
+ '?',
709
+ ':+',
710
+ '+',
711
+ '#',
712
+ '##',
713
+ '%',
714
+ '%%',
715
+ '^',
716
+ '^^',
717
+ ',',
718
+ ',,',
719
+ '~',
720
+ '~~',
721
+ '/',
722
+ '//',
723
+ '/#',
724
+ '/%',
725
+ ':',
726
+ '@'
727
+ )
728
+ ),
729
+ $.operator
730
+ )
731
+ ),
732
+ repeat(
733
+ field(
734
+ 'argument',
735
+ choice(
736
+ alias($._expansion_text, $.word),
737
+ alias($._blank_expansion_text, $.word),
738
+ alias($._angle_text, $.word),
739
+ $._quoted_or_expansion,
740
+ alias($._bare_dollar, $.word)
741
+ )
742
+ )
743
+ )
616
744
  ),
617
- field('parameter', alias(token.immediate(choice('#', '!')), $.special_variable_name)),
618
- // Bash rejects `${}` and a body without a parameter (`${=1}`, `${"x"}`) only when expanding. A
619
- // `$name` expansion is left out, since its `$` would tie with the special parameter in `${$}`.
620
- repeat(field('argument', choice(
621
- alias($._expansion_text, $.word),
622
- alias($._blank_expansion_text, $.word),
623
- alias($._angle_text, $.word),
624
- $.string,
625
- $.raw_string,
626
- $.ansi_c_string,
627
- $.translated_string,
628
- $.command_substitution,
629
- ))),
630
- ),
631
- '}',
632
- ),
633
-
634
- _expansion_subscript: $ => seq(
635
- field('name', alias(token.immediate(/[A-Za-z_][A-Za-z0-9_]*/), $.variable_name)),
636
- repeat1(seq(token.immediate('['), field('index', $._subscript_index), ']')),
637
- ),
638
-
639
- _expansion_operation: $ => choice(
640
- seq(
641
- field('operator', alias(
642
- token.immediate(choice(
643
- ':-', '-', ':=', '=', ':?', '?', ':+', '+', '#', '##', '%', '%%',
644
- '^', '^^', ',', ',,', '~', '~~', '/', '//', '/#', '/%', ':', '@',
645
- )),
646
- $.operator,
647
- )),
648
- repeat(field('argument', choice(
649
- alias($._expansion_text, $.word),
650
- alias($._blank_expansion_text, $.word),
651
- alias($._angle_text, $.word),
652
- $._quoted_or_expansion,
653
- alias($._bare_dollar, $.word),
654
- ))),
655
- ),
656
- field('operator', alias(token.immediate(choice('*', '@')), $.operator)),
657
- // Text without an operator (`${x y}`) is rejected by bash only when expanding.
658
- repeat1(field('argument', choice(
659
- alias($._expansion_text, $.word),
660
- alias($._blank_expansion_text, $.word),
661
- alias($._angle_text, $.word),
662
- $._quoted_or_expansion,
663
- ))),
664
- ),
665
-
666
- _expansion_text: _ => token(prec(-1, seq(
667
- choice(noneOf(...EXPANSION_TEXT_BREAKS, ' ', '\\t', '\\n'), ...expansionTextUnits().slice(1)),
668
- repeat(choice(...expansionTextUnits())),
669
- ))),
745
+ field('operator', alias(token.immediate(choice('*', '@')), $.operator)),
746
+ // Text without an operator (`${x y}`) is rejected by bash only when expanding.
747
+ repeat1(
748
+ field(
749
+ 'argument',
750
+ choice(
751
+ alias($._expansion_text, $.word),
752
+ alias($._blank_expansion_text, $.word),
753
+ alias($._angle_text, $.word),
754
+ $._quoted_or_expansion
755
+ )
756
+ )
757
+ )
758
+ ),
759
+
760
+ _expansion_text: () =>
761
+ token(
762
+ prec(
763
+ -1,
764
+ seq(
765
+ choice(
766
+ noneOf(...EXPANSION_TEXT_BREAKS, ' ', String.raw`\t`, String.raw`\n`),
767
+ ...expansionTextUnits().slice(1)
768
+ ),
769
+ repeat(choice(...expansionTextUnits()))
770
+ )
771
+ )
772
+ ),
670
773
 
671
774
  // Text led by a blank outranks `}`: otherwise the lexer, having consumed the blank as the start of
672
775
  // text, extends it into a `}` token that swallows the blank (`${x:- }`), which a reparse after an
673
776
  // edit does not reproduce, and the blank, which bash expands, would be lost.
674
- _blank_expansion_text: _ => token(prec(1, seq(/[ \t\n]/, repeat(choice(...expansionTextUnits()))))),
777
+ _blank_expansion_text: () => token(prec(1, seq(/[ \t\n]/, repeat(choice(...expansionTextUnits()))))),
675
778
 
676
779
  // A `<` or `>` that the text above cannot end with, e.g. before `}`. A single character, so that a
677
780
  // run before `(` leaves its last `<`/`>` to open a process substitution (`${x:-a<<(b)}`).
678
- _angle_text: _ => token(prec(-1, /[<>]/)),
781
+ _angle_text: () => token(prec(-1, /[<>]/)),
679
782
 
680
783
  // The zero-width substitution markers let the scanner track nesting, since a heredoc opened
681
784
  // inside a substitution has its body read before the substitution ends.
682
- command_substitution: $ => choice(
683
- seq('$(', $._substitution_start, optional($._statements), $._substitution_end, ')'),
684
- // bash 5.3 runs `${ list; }` and `${| list; }` in the current shell.
685
- seq(
686
- alias(token(/\$\{[ \t\n|]/), '${'),
687
- $._brace_substitution_start,
688
- optional($._terminated_statements),
689
- $._brace_substitution_end,
690
- '}',
691
- ),
692
- seq(alias($._backtick_open, '`'), optional($._statements), alias($._backtick_close, '`')),
693
- ),
694
-
695
- process_substitution: $ => seq(
696
- choice('<(', '>('),
697
- $._substitution_start,
698
- optional($._statements),
699
- $._substitution_end,
700
- ')',
701
- ),
785
+ command_substitution: ($) =>
786
+ choice(
787
+ seq('$(', $._substitution_start, optional($._statements), $._substitution_end, ')'),
788
+ // bash 5.3 runs `${ list; }` and `${| list; }` in the current shell.
789
+ seq(
790
+ alias(token(/\$\{[ \t\n|]/), '${'),
791
+ $._brace_substitution_start,
792
+ optional($._terminated_statements),
793
+ $._brace_substitution_end,
794
+ '}'
795
+ ),
796
+ seq(alias($._backtick_open, '`'), optional($._statements), alias($._backtick_close, '`'))
797
+ ),
798
+
799
+ process_substitution: ($) =>
800
+ seq(choice('<(', '>('), $._substitution_start, optional($._statements), $._substitution_end, ')'),
702
801
 
703
802
  // Arithmetic may span lines, and a heredoc body starts only after it, so it counts as nesting.
704
- arithmetic_expansion: $ => choice(
705
- seq('$((', $._substitution_start, optional($._arithmetic_expression), $._substitution_end, '))'),
706
- seq('$[', $._bracket_substitution_start, optional($._arithmetic_expression), $._bracket_substitution_end, ']'),
707
- ),
803
+ arithmetic_expansion: ($) =>
804
+ choice(
805
+ seq('$((', $._substitution_start, optional($._arithmetic_expression), $._substitution_end, '))'),
806
+ seq('$[', $._bracket_substitution_start, optional($._arithmetic_expression), $._bracket_substitution_end, ']')
807
+ ),
708
808
 
709
809
  // Arithmetic
710
810
 
711
- _arithmetic_expression: $ => choice(
712
- $.number,
713
- alias($._arithmetic_variable, $.variable_name),
714
- alias($._arithmetic_subscript, $.subscript),
715
- $.simple_expansion,
716
- $.expansion,
717
- $.command_substitution,
718
- $.arithmetic_expansion,
719
- $.string,
720
- alias($._arithmetic_unary, $.unary_expression),
721
- alias($._arithmetic_postfix, $.postfix_expression),
722
- alias($._arithmetic_binary, $.binary_expression),
723
- alias($._arithmetic_ternary, $.ternary_expression),
724
- alias($._arithmetic_parenthesized, $.parenthesized_expression),
725
- ),
726
-
727
- number: $ => choice(
728
- /(0[xX][0-9a-fA-F]+|[0-9]+#[0-9a-zA-Z@_]+|[0-9]+)/,
729
- // The digits of a based number may come from an expansion (`10#${x}`).
730
- seq(/[0-9]+#/, choice($.simple_expansion, $.expansion)),
731
- ),
732
-
733
- _arithmetic_variable: _ => /[A-Za-z_][A-Za-z0-9_]*/,
734
-
735
- _arithmetic_subscript: $ => seq(
736
- field('name', alias($._arithmetic_variable, $.variable_name)),
737
- token.immediate('['),
738
- field('index', $._arithmetic_expression),
739
- ']',
740
- ),
741
-
742
- _arithmetic_unary: $ => choice(
743
- prec(PREC.PREFIX, seq(field('operator', choice('++', '--')), $._arithmetic_expression)),
744
- prec(PREC.UNARY, seq(field('operator', choice('-', '+', '!', '~')), $._arithmetic_expression)),
745
- ),
746
-
747
- _arithmetic_postfix: $ => prec(PREC.POSTFIX, seq($._arithmetic_expression, field('operator', choice('++', '--')))),
748
-
749
- _arithmetic_binary: $ => {
811
+ _arithmetic_expression: ($) =>
812
+ choice(
813
+ $.number,
814
+ alias($._arithmetic_variable, $.variable_name),
815
+ alias($._arithmetic_subscript, $.subscript),
816
+ $.simple_expansion,
817
+ $.expansion,
818
+ $.command_substitution,
819
+ $.arithmetic_expansion,
820
+ $.string,
821
+ alias($._arithmetic_unary, $.unary_expression),
822
+ alias($._arithmetic_postfix, $.postfix_expression),
823
+ alias($._arithmetic_binary, $.binary_expression),
824
+ alias($._arithmetic_ternary, $.ternary_expression),
825
+ alias($._arithmetic_parenthesized, $.parenthesized_expression)
826
+ ),
827
+
828
+ number: ($) =>
829
+ choice(
830
+ /(0[xX][0-9a-fA-F]+|[0-9]+#[0-9a-zA-Z@_]+|[0-9]+)/,
831
+ // The digits of a based number may come from an expansion (`10#${x}`).
832
+ seq(/[0-9]+#/, choice($.simple_expansion, $.expansion))
833
+ ),
834
+
835
+ _arithmetic_variable: () => /[A-Za-z_][A-Za-z0-9_]*/,
836
+
837
+ _arithmetic_subscript: ($) =>
838
+ seq(
839
+ field('name', alias($._arithmetic_variable, $.variable_name)),
840
+ token.immediate('['),
841
+ field('index', $._arithmetic_expression),
842
+ ']'
843
+ ),
844
+
845
+ _arithmetic_unary: ($) =>
846
+ choice(
847
+ prec(PREC.PREFIX, seq(field('operator', choice('++', '--')), $._arithmetic_expression)),
848
+ prec(PREC.UNARY, seq(field('operator', choice('-', '+', '!', '~')), $._arithmetic_expression))
849
+ ),
850
+
851
+ _arithmetic_postfix: ($) =>
852
+ prec(PREC.POSTFIX, seq($._arithmetic_expression, field('operator', choice('++', '--')))),
853
+
854
+ _arithmetic_binary: ($) => {
750
855
  const table = [
751
856
  [PREC.COMMA, ','],
752
857
  [PREC.ASSIGN, choice('=', '+=', '-=', '*=', '/=', '%=', '**=', '<<=', '>>=', '&=', '^=', '|=')],
@@ -762,28 +867,36 @@ module.exports = grammar({
762
867
  [PREC.MULTIPLY, choice('*', '/', '%')],
763
868
  [PREC.EXPONENT, '**'],
764
869
  ];
765
- return choice(...table.map(([precedence, operator]) => {
766
- const associativity = precedence === PREC.ASSIGN || precedence === PREC.EXPONENT ? prec.right : prec.left;
767
- return associativity(/** @type {number} */ (precedence), seq(
768
- field('left', $._arithmetic_expression),
769
- field('operator', /** @type {RuleOrLiteral} */ (operator)),
770
- field('right', $._arithmetic_expression),
771
- ));
772
- }));
870
+ return choice(
871
+ ...table.map(([precedence, operator]) => {
872
+ const rule = seq(
873
+ field('left', $._arithmetic_expression),
874
+ field('operator', /** @type {RuleOrLiteral} */ (operator)),
875
+ field('right', $._arithmetic_expression)
876
+ );
877
+ return precedence === PREC.ASSIGN || precedence === PREC.EXPONENT
878
+ ? prec.right(/** @type {number} */ (precedence), rule)
879
+ : prec.left(/** @type {number} */ (precedence), rule);
880
+ })
881
+ );
773
882
  },
774
883
 
775
- _arithmetic_ternary: $ => prec.right(PREC.TERNARY, seq(
776
- field('condition', $._arithmetic_expression),
777
- '?',
778
- field('consequence', $._arithmetic_expression),
779
- ':',
780
- field('alternative', $._arithmetic_expression),
781
- )),
884
+ _arithmetic_ternary: ($) =>
885
+ prec.right(
886
+ PREC.TERNARY,
887
+ seq(
888
+ field('condition', $._arithmetic_expression),
889
+ '?',
890
+ field('consequence', $._arithmetic_expression),
891
+ ':',
892
+ field('alternative', $._arithmetic_expression)
893
+ )
894
+ ),
782
895
 
783
- _arithmetic_parenthesized: $ => seq('(', $._arithmetic_expression, ')'),
896
+ _arithmetic_parenthesized: ($) => seq('(', $._arithmetic_expression, ')'),
784
897
 
785
898
  // A comment on such a line also continues past each line continuation.
786
- comment: $ => choice(token(prec(-10, /#.*/)), $._joined_comment),
899
+ comment: ($) => choice(token(prec(-10, /#.*/)), $._joined_comment),
787
900
  },
788
901
  });
789
902