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