@willbooster/tree-sitter-bash 4.0.0 → 4.0.2
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/README.md +11 -2
- package/grammar.js +167 -126
- package/package.json +1 -2
- package/tree-sitter-bash.wasm +0 -0
- package/tree-sitter.json +1 -1
package/README.md
CHANGED
|
@@ -68,8 +68,17 @@ The package also ships the queries in `queries/` and the node types in `src/node
|
|
|
68
68
|
|
|
69
69
|
When upgrading from 3.x, upgrade `@willbooster/web-tree-sitter` to 2.x in the same change: each major of this package
|
|
70
70
|
loads only with the runtime major it was generated for. Scripts that parse without errors get the same syntax trees as
|
|
71
|
-
with 3.x
|
|
72
|
-
the
|
|
71
|
+
with 3.x, except where backslashes inside backquotes precede `$` (or `"` when the backquotes are inside double quotes):
|
|
72
|
+
like bash, the parser removes one level of those backslashes before it parses the text, so `` `echo \$x` `` holds an
|
|
73
|
+
expansion and `` `echo \\$x` `` a literal `$x`. Recovery trees for malformed scripts can differ from every earlier
|
|
74
|
+
major, so review consumers that depend on the exact shape of `ERROR` nodes.
|
|
75
|
+
|
|
76
|
+
Inside backquotes, the text of a node is the source text: consumers that read the value of a word or a string there
|
|
77
|
+
remove a backslash before `$`, a backquote, or a backslash (and before `"` when the backquotes are inside double quotes)
|
|
78
|
+
before they remove quotes and escapes as usual. The parser normalizes runs before `$`, and before `"` when the
|
|
79
|
+
backquotes opened inside double quotes, only in ordinary word and double-quoted string scanning. Runs before other
|
|
80
|
+
characters (including whitespace and metacharacters), heredoc bodies, parameter-expansion text, regexes, nested
|
|
81
|
+
backquotes, and PID expansions split by a removed escape retain their existing parsing limitations.
|
|
73
82
|
|
|
74
83
|
## Development
|
|
75
84
|
|
package/grammar.js
CHANGED
|
@@ -3,9 +3,12 @@
|
|
|
3
3
|
* @license MIT
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
/// <reference types="tree-sitter-cli/dsl" />
|
|
7
6
|
// @ts-check
|
|
8
7
|
|
|
8
|
+
// The CLI injects the DSL before loading this CommonJS grammar.
|
|
9
|
+
const { grammar, alias, choice, field, optional, prec, repeat, repeat1, seq, sym, token } =
|
|
10
|
+
/** @type {typeof globalThis & import('@willbooster/web-tree-sitter/dsl').DSL} */ (globalThis);
|
|
11
|
+
|
|
9
12
|
// Characters that end an unquoted word part: metacharacters, quotes, and characters that begin an
|
|
10
13
|
// expansion. `#` only starts a comment at the beginning of a word, so it is handled separately. Like
|
|
11
14
|
// bash, only a space, a tab, and a newline separate words; any other whitespace (CR, a no-break space)
|
|
@@ -108,50 +111,62 @@ const TEST_BINARY_OPERATORS = [
|
|
|
108
111
|
module.exports = grammar({
|
|
109
112
|
name: 'bash',
|
|
110
113
|
|
|
111
|
-
externals: (
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
114
|
+
externals: () => [
|
|
115
|
+
sym('heredoc_start'),
|
|
116
|
+
sym('_heredoc_body_start'),
|
|
117
|
+
sym('heredoc_content'),
|
|
118
|
+
sym('heredoc_end'),
|
|
119
|
+
sym('_heredoc_arrow'),
|
|
120
|
+
sym('_heredoc_arrow_dash'),
|
|
121
|
+
sym('_concat'),
|
|
122
|
+
sym('file_descriptor'),
|
|
123
|
+
sym('variable_name'),
|
|
124
|
+
sym('_bare_dollar'),
|
|
125
|
+
sym('_empty_value'),
|
|
126
|
+
sym('regex'),
|
|
127
|
+
sym('string_content'),
|
|
128
|
+
sym('_backtick_open'),
|
|
129
|
+
sym('_backtick_close'),
|
|
130
|
+
sym('_newline'),
|
|
131
|
+
sym('_substitution_start'),
|
|
132
|
+
sym('_substitution_end'),
|
|
133
|
+
sym('_brace_substitution_end'),
|
|
134
|
+
sym('_bracket_substitution_end'),
|
|
135
|
+
sym('_extglob_prefix'),
|
|
136
|
+
sym('_line_continuation'),
|
|
137
|
+
sym('_brace_substitution_start'),
|
|
138
|
+
sym('_bracket_substitution_start'),
|
|
139
|
+
sym('_regex_start'),
|
|
140
|
+
sym('_regex_concat'),
|
|
141
|
+
sym('_closer_line_raw_string_start'),
|
|
142
|
+
sym('_closer_line_ansi_c_string_start'),
|
|
143
|
+
sym('_closer_line_raw_string_content'),
|
|
144
|
+
sym('_closer_line_ansi_c_string_content'),
|
|
145
|
+
sym('_joined_comment'),
|
|
146
|
+
sym('_backtick_comment'),
|
|
147
|
+
sym('_backtick_comment_boundary'),
|
|
148
|
+
sym('__error_recovery'),
|
|
149
|
+
sym('_command_substitution_start'),
|
|
150
|
+
sym('_arithmetic_substitution_start'),
|
|
151
|
+
sym('_arithmetic_cache_reset'),
|
|
152
|
+
sym('_timing_keyword'),
|
|
153
|
+
sym('_timing_option'),
|
|
154
|
+
sym('_timing_terminator'),
|
|
155
|
+
sym('_backquote_escape'),
|
|
152
156
|
],
|
|
153
157
|
|
|
154
|
-
|
|
158
|
+
// Bash removes the backslash before `$` (and before `"` where the backquotes are inside double quotes)
|
|
159
|
+
// when it reads the text of backquotes, so the scanner returns that backslash on its own and what
|
|
160
|
+
// follows it is parsed as an expansion or a quote.
|
|
161
|
+
extras: ($) => [
|
|
162
|
+
$.comment,
|
|
163
|
+
$.heredoc_body,
|
|
164
|
+
/[ \t\n]/,
|
|
165
|
+
/\\\n/,
|
|
166
|
+
sym('_line_continuation'),
|
|
167
|
+
sym('_arithmetic_cache_reset'),
|
|
168
|
+
sym('_backquote_escape'),
|
|
169
|
+
],
|
|
155
170
|
|
|
156
171
|
supertypes: ($) => [$._statement, $._arithmetic_expression],
|
|
157
172
|
|
|
@@ -170,7 +185,7 @@ module.exports = grammar({
|
|
|
170
185
|
_terminated_statements: ($) => repeat1(seq($._statement, $._terminator)),
|
|
171
186
|
|
|
172
187
|
// A newline is significant only where it can end a statement; elsewhere it is whitespace.
|
|
173
|
-
_terminator: (
|
|
188
|
+
_terminator: () => choice(';', '&', sym('_newline')),
|
|
174
189
|
|
|
175
190
|
_statement: ($) =>
|
|
176
191
|
choice($._command_statement, $.pipeline, $.list, $.negated_command, $.timed_command, $.coproc_command),
|
|
@@ -207,9 +222,9 @@ module.exports = grammar({
|
|
|
207
222
|
prec.right(
|
|
208
223
|
2,
|
|
209
224
|
seq(
|
|
210
|
-
choice('time', alias(
|
|
211
|
-
optional(field('option', alias(choice('-p',
|
|
212
|
-
optional(alias(
|
|
225
|
+
choice('time', alias(sym('_timing_keyword'), 'time')),
|
|
226
|
+
optional(field('option', alias(choice('-p', sym('_timing_option')), $.word))),
|
|
227
|
+
optional(alias(sym('_timing_terminator'), $.word)),
|
|
213
228
|
optional(choice($._command_statement, $.pipeline, $.negated_command))
|
|
214
229
|
)
|
|
215
230
|
),
|
|
@@ -239,7 +254,7 @@ module.exports = grammar({
|
|
|
239
254
|
field('name', $.command_name),
|
|
240
255
|
repeat(
|
|
241
256
|
seq(
|
|
242
|
-
optional(
|
|
257
|
+
optional(sym('_backtick_comment_boundary')),
|
|
243
258
|
choice(field('argument', $._argument), field('redirect', $._redirect))
|
|
244
259
|
)
|
|
245
260
|
)
|
|
@@ -250,7 +265,7 @@ module.exports = grammar({
|
|
|
250
265
|
|
|
251
266
|
_command_prefix: ($) => choice(field('assignment', $.variable_assignment), field('redirect', $._redirect)),
|
|
252
267
|
|
|
253
|
-
command_name: ($) => seq(optional(
|
|
268
|
+
command_name: ($) => seq(optional(sym('_backtick_comment_boundary')), $._word),
|
|
254
269
|
|
|
255
270
|
declaration_command: ($) =>
|
|
256
271
|
prec.left(
|
|
@@ -259,7 +274,7 @@ module.exports = grammar({
|
|
|
259
274
|
field('name', alias(choice('declare', 'typeset', 'export', 'readonly', 'local'), $.command_name)),
|
|
260
275
|
repeat(
|
|
261
276
|
seq(
|
|
262
|
-
optional(
|
|
277
|
+
optional(sym('_backtick_comment_boundary')),
|
|
263
278
|
choice(field('argument', choice($.variable_assignment, $._argument)), field('redirect', $._redirect))
|
|
264
279
|
)
|
|
265
280
|
)
|
|
@@ -268,9 +283,9 @@ module.exports = grammar({
|
|
|
268
283
|
|
|
269
284
|
variable_assignment: ($) =>
|
|
270
285
|
seq(
|
|
271
|
-
field('name', choice(
|
|
286
|
+
field('name', choice(sym('variable_name'), $.subscript)),
|
|
272
287
|
field('operator', token.immediate(choice('=', '+='))),
|
|
273
|
-
field('value', choice($._argument, $._hash_value, $.array,
|
|
288
|
+
field('value', choice($._argument, $._hash_value, $.array, sym('_empty_value')))
|
|
274
289
|
),
|
|
275
290
|
|
|
276
291
|
// A value is not at the start of a word, so a leading `#` is literal (`color=#fff`).
|
|
@@ -282,7 +297,7 @@ module.exports = grammar({
|
|
|
282
297
|
alias($._hash_word, $.word),
|
|
283
298
|
repeat1(
|
|
284
299
|
seq(
|
|
285
|
-
|
|
300
|
+
sym('_concat'),
|
|
286
301
|
choice($._word_part, alias($._hash_word, $.word), alias($._negated_extglob_pattern, $.extglob_pattern))
|
|
287
302
|
)
|
|
288
303
|
)
|
|
@@ -290,7 +305,7 @@ module.exports = grammar({
|
|
|
290
305
|
),
|
|
291
306
|
|
|
292
307
|
subscript: ($) =>
|
|
293
|
-
seq(field('name',
|
|
308
|
+
seq(field('name', sym('variable_name')), token.immediate('['), field('index', $._subscript_index), ']'),
|
|
294
309
|
|
|
295
310
|
// Like bash, the index extends to the matching `]`, so it may hold bracketed groups at any depth
|
|
296
311
|
// (`A[x[1[2]]]`), blanks, and newlines.
|
|
@@ -328,7 +343,7 @@ module.exports = grammar({
|
|
|
328
343
|
|
|
329
344
|
file_redirect: ($) =>
|
|
330
345
|
seq(
|
|
331
|
-
optional(field('descriptor',
|
|
346
|
+
optional(field('descriptor', sym('file_descriptor'))),
|
|
332
347
|
choice(
|
|
333
348
|
seq(
|
|
334
349
|
field('operator', choice('<', '>', '>>', '>|', '<>', '&>', '&>>', '<&', '>&')),
|
|
@@ -339,24 +354,30 @@ module.exports = grammar({
|
|
|
339
354
|
),
|
|
340
355
|
|
|
341
356
|
herestring_redirect: ($) =>
|
|
342
|
-
seq(optional(field('descriptor',
|
|
357
|
+
seq(optional(field('descriptor', sym('file_descriptor'))), field('operator', '<<<'), field('value', $._argument)),
|
|
343
358
|
|
|
344
|
-
heredoc_redirect: (
|
|
359
|
+
heredoc_redirect: () =>
|
|
345
360
|
seq(
|
|
346
|
-
optional(field('descriptor',
|
|
347
|
-
field('operator', choice(alias(
|
|
348
|
-
field('delimiter',
|
|
361
|
+
optional(field('descriptor', sym('file_descriptor'))),
|
|
362
|
+
field('operator', choice(alias(sym('_heredoc_arrow'), '<<'), alias(sym('_heredoc_arrow_dash'), '<<-'))),
|
|
363
|
+
field('delimiter', sym('heredoc_start'))
|
|
349
364
|
),
|
|
350
365
|
|
|
351
366
|
// The scanner opens a body at the newline that ends the line holding its `<<` operator, so bodies
|
|
352
367
|
// appear, in operator order, wherever that newline falls.
|
|
353
368
|
heredoc_body: ($) =>
|
|
354
369
|
seq(
|
|
355
|
-
|
|
370
|
+
sym('_heredoc_body_start'),
|
|
356
371
|
repeat(
|
|
357
|
-
choice(
|
|
372
|
+
choice(
|
|
373
|
+
sym('heredoc_content'),
|
|
374
|
+
$.simple_expansion,
|
|
375
|
+
$.expansion,
|
|
376
|
+
$.command_substitution,
|
|
377
|
+
$.arithmetic_expansion
|
|
378
|
+
)
|
|
358
379
|
),
|
|
359
|
-
|
|
380
|
+
sym('heredoc_end')
|
|
360
381
|
),
|
|
361
382
|
|
|
362
383
|
compound_statement: ($) => seq('{', optional($._terminated_statements), '}'),
|
|
@@ -364,7 +385,7 @@ module.exports = grammar({
|
|
|
364
385
|
subshell: ($) => seq('(', optional($._statements), ')'),
|
|
365
386
|
|
|
366
387
|
arithmetic_command: ($) =>
|
|
367
|
-
seq('((',
|
|
388
|
+
seq('((', sym('_substitution_start'), optional($._arithmetic_expression), sym('_substitution_end'), '))'),
|
|
368
389
|
|
|
369
390
|
test_command: ($) => seq('[[', $._test_expression, ']]'),
|
|
370
391
|
|
|
@@ -374,15 +395,18 @@ module.exports = grammar({
|
|
|
374
395
|
$.unary_test,
|
|
375
396
|
$.binary_test,
|
|
376
397
|
$.regex_test,
|
|
377
|
-
alias($._test_negation,
|
|
378
|
-
alias($._test_logical,
|
|
379
|
-
alias($._test_parenthesized,
|
|
398
|
+
alias($._test_negation, sym('negated_test')),
|
|
399
|
+
alias($._test_logical, sym('logical_test')),
|
|
400
|
+
alias($._test_parenthesized, sym('parenthesized_test'))
|
|
380
401
|
),
|
|
381
402
|
|
|
382
403
|
unary_test: ($) =>
|
|
383
404
|
prec(
|
|
384
405
|
PREC.UNARY,
|
|
385
|
-
seq(
|
|
406
|
+
seq(
|
|
407
|
+
field('operator', alias(choice(...TEST_UNARY_OPERATORS), sym('test_operator'))),
|
|
408
|
+
field('operand', $._argument)
|
|
409
|
+
)
|
|
386
410
|
),
|
|
387
411
|
|
|
388
412
|
// The right side of `==`/`!=` is a pattern matched as if extglob were enabled.
|
|
@@ -391,7 +415,7 @@ module.exports = grammar({
|
|
|
391
415
|
PREC.COMPARE,
|
|
392
416
|
seq(
|
|
393
417
|
field('left', $._word),
|
|
394
|
-
field('operator', alias(choice(...TEST_BINARY_OPERATORS),
|
|
418
|
+
field('operator', alias(choice(...TEST_BINARY_OPERATORS), sym('test_operator'))),
|
|
395
419
|
field('right', $._argument)
|
|
396
420
|
)
|
|
397
421
|
),
|
|
@@ -404,14 +428,14 @@ module.exports = grammar({
|
|
|
404
428
|
seq(
|
|
405
429
|
field('left', $._word),
|
|
406
430
|
field('operator', '=~'),
|
|
407
|
-
|
|
431
|
+
sym('_regex_start'),
|
|
408
432
|
field('right', choice($._regex_part, alias($._regex_concatenation, $.concatenation)))
|
|
409
433
|
)
|
|
410
434
|
),
|
|
411
435
|
|
|
412
|
-
_regex_concatenation: ($) => prec.right(seq($._regex_part, repeat1(seq(
|
|
436
|
+
_regex_concatenation: ($) => prec.right(seq($._regex_part, repeat1(seq(sym('_regex_concat'), $._regex_part)))),
|
|
413
437
|
|
|
414
|
-
_regex_part: ($) => choice(
|
|
438
|
+
_regex_part: ($) => choice(sym('regex'), $._quoted_or_expansion),
|
|
415
439
|
|
|
416
440
|
_test_negation: ($) => prec(PREC.UNARY, seq('!', $._test_expression)),
|
|
417
441
|
|
|
@@ -451,20 +475,20 @@ module.exports = grammar({
|
|
|
451
475
|
field('body', $.do_group)
|
|
452
476
|
),
|
|
453
477
|
|
|
454
|
-
do_group: ($) => seq(optional(
|
|
478
|
+
do_group: ($) => seq(optional(sym('_backtick_comment_boundary')), 'do', optional($._terminated_statements), 'done'),
|
|
455
479
|
|
|
456
480
|
for_statement: ($) =>
|
|
457
481
|
seq(
|
|
458
482
|
field('keyword', choice('for', 'select')),
|
|
459
|
-
field('variable', alias($.word,
|
|
483
|
+
field('variable', alias($.word, sym('variable_name'))),
|
|
460
484
|
// Without a word list the separator is optional (`for i do …`); a newline may precede `in`.
|
|
461
485
|
choice(
|
|
462
486
|
seq(
|
|
463
|
-
optional(
|
|
464
|
-
optional(
|
|
487
|
+
optional(sym('_newline')),
|
|
488
|
+
optional(sym('_backtick_comment_boundary')),
|
|
465
489
|
'in',
|
|
466
|
-
repeat(seq(optional(
|
|
467
|
-
optional(
|
|
490
|
+
repeat(seq(optional(sym('_backtick_comment_boundary')), field('value', $._argument))),
|
|
491
|
+
optional(sym('_backtick_comment_boundary')),
|
|
468
492
|
$._terminator
|
|
469
493
|
),
|
|
470
494
|
optional($._terminator)
|
|
@@ -476,13 +500,13 @@ module.exports = grammar({
|
|
|
476
500
|
seq(
|
|
477
501
|
'for',
|
|
478
502
|
'((',
|
|
479
|
-
|
|
503
|
+
sym('_substitution_start'),
|
|
480
504
|
field('initializer', optional($._arithmetic_expression)),
|
|
481
505
|
';',
|
|
482
506
|
field('condition', optional($._arithmetic_expression)),
|
|
483
507
|
';',
|
|
484
508
|
field('update', optional($._arithmetic_expression)),
|
|
485
|
-
|
|
509
|
+
sym('_substitution_end'),
|
|
486
510
|
'))',
|
|
487
511
|
optional($._terminator),
|
|
488
512
|
field('body', choice($.do_group, $.compound_statement))
|
|
@@ -492,7 +516,7 @@ module.exports = grammar({
|
|
|
492
516
|
seq(
|
|
493
517
|
'case',
|
|
494
518
|
field('value', $._argument),
|
|
495
|
-
optional(
|
|
519
|
+
optional(sym('_backtick_comment_boundary')),
|
|
496
520
|
'in',
|
|
497
521
|
repeat($.case_item),
|
|
498
522
|
optional(alias($._last_case_item, $.case_item)),
|
|
@@ -507,7 +531,7 @@ module.exports = grammar({
|
|
|
507
531
|
|
|
508
532
|
_case_patterns: ($) =>
|
|
509
533
|
seq(
|
|
510
|
-
optional(
|
|
534
|
+
optional(sym('_backtick_comment_boundary')),
|
|
511
535
|
optional('('),
|
|
512
536
|
field('pattern', $._argument),
|
|
513
537
|
repeat(seq('|', field('pattern', $._argument))),
|
|
@@ -517,7 +541,7 @@ module.exports = grammar({
|
|
|
517
541
|
function_definition: ($) =>
|
|
518
542
|
seq(
|
|
519
543
|
choice(seq('function', field('name', $.word), optional(seq('(', ')'))), seq(field('name', $.word), '(', ')')),
|
|
520
|
-
optional(
|
|
544
|
+
optional(sym('_backtick_comment_boundary')),
|
|
521
545
|
field('body', $._compound_command)
|
|
522
546
|
),
|
|
523
547
|
|
|
@@ -540,7 +564,7 @@ module.exports = grammar({
|
|
|
540
564
|
choice(alias($._negated_extglob_pattern, $.extglob_pattern), alias($._negated_extglob_word, $.word)),
|
|
541
565
|
repeat1(
|
|
542
566
|
seq(
|
|
543
|
-
|
|
567
|
+
sym('_concat'),
|
|
544
568
|
choice($._word_part, alias($._hash_word, $.word), alias($._negated_extglob_pattern, $.extglob_pattern))
|
|
545
569
|
)
|
|
546
570
|
)
|
|
@@ -561,7 +585,7 @@ module.exports = grammar({
|
|
|
561
585
|
$._word_part,
|
|
562
586
|
repeat1(
|
|
563
587
|
seq(
|
|
564
|
-
|
|
588
|
+
sym('_concat'),
|
|
565
589
|
choice($._word_part, alias($._hash_word, $.word), alias($._negated_extglob_pattern, $.extglob_pattern))
|
|
566
590
|
)
|
|
567
591
|
)
|
|
@@ -572,7 +596,7 @@ module.exports = grammar({
|
|
|
572
596
|
choice(
|
|
573
597
|
$.word,
|
|
574
598
|
// Literal text that the scanner ends before a structured extglob group (`foo@($x)`).
|
|
575
|
-
alias(
|
|
599
|
+
alias(sym('_extglob_prefix'), $.word),
|
|
576
600
|
$.extglob_pattern,
|
|
577
601
|
$.string,
|
|
578
602
|
$.raw_string,
|
|
@@ -583,7 +607,7 @@ module.exports = grammar({
|
|
|
583
607
|
$.command_substitution,
|
|
584
608
|
$.arithmetic_expansion,
|
|
585
609
|
$.process_substitution,
|
|
586
|
-
alias(
|
|
610
|
+
alias(sym('_bare_dollar'), $.word)
|
|
587
611
|
),
|
|
588
612
|
|
|
589
613
|
// An extglob group (`@(a|b)`) is part of the word; `!(` only inside a word, since a leading `!`
|
|
@@ -626,7 +650,7 @@ module.exports = grammar({
|
|
|
626
650
|
seq(
|
|
627
651
|
'"',
|
|
628
652
|
repeat(
|
|
629
|
-
choice(
|
|
653
|
+
choice(sym('string_content'), $.simple_expansion, $.expansion, $.command_substitution, $.arithmetic_expansion)
|
|
630
654
|
),
|
|
631
655
|
'"'
|
|
632
656
|
),
|
|
@@ -635,18 +659,22 @@ module.exports = grammar({
|
|
|
635
659
|
// with its line continuations removed, even inside single quotes, and reads the bodies still queued
|
|
636
660
|
// before that line right after it, even where it ends inside quotes; the scanner then splits the
|
|
637
661
|
// quoted text around the continuations and the bodies.
|
|
638
|
-
raw_string: (
|
|
662
|
+
raw_string: () =>
|
|
639
663
|
choice(
|
|
640
664
|
/'[^']*'/,
|
|
641
|
-
seq(
|
|
665
|
+
seq(
|
|
666
|
+
sym('_closer_line_raw_string_start'),
|
|
667
|
+
repeat(alias(sym('_closer_line_raw_string_content'), sym('string_content'))),
|
|
668
|
+
"'"
|
|
669
|
+
)
|
|
642
670
|
),
|
|
643
671
|
|
|
644
|
-
ansi_c_string: (
|
|
672
|
+
ansi_c_string: () =>
|
|
645
673
|
choice(
|
|
646
674
|
/\$'([^'\\]|\\(.|\n))*'/,
|
|
647
675
|
seq(
|
|
648
|
-
|
|
649
|
-
repeat(alias(
|
|
676
|
+
sym('_closer_line_ansi_c_string_start'),
|
|
677
|
+
repeat(alias(sym('_closer_line_ansi_c_string_content'), sym('string_content'))),
|
|
650
678
|
"'"
|
|
651
679
|
)
|
|
652
680
|
),
|
|
@@ -655,17 +683,17 @@ module.exports = grammar({
|
|
|
655
683
|
seq(
|
|
656
684
|
'$"',
|
|
657
685
|
repeat(
|
|
658
|
-
choice(
|
|
686
|
+
choice(sym('string_content'), $.simple_expansion, $.expansion, $.command_substitution, $.arithmetic_expansion)
|
|
659
687
|
),
|
|
660
688
|
'"'
|
|
661
689
|
),
|
|
662
690
|
|
|
663
|
-
simple_expansion: (
|
|
691
|
+
simple_expansion: () =>
|
|
664
692
|
seq(
|
|
665
693
|
'$',
|
|
666
694
|
choice(
|
|
667
|
-
alias(token.immediate(/[A-Za-z_][A-Za-z0-9_]*/),
|
|
668
|
-
alias(token.immediate(/[0-9*@#?$!_-]/),
|
|
695
|
+
alias(token.immediate(/[A-Za-z_][A-Za-z0-9_]*/), sym('variable_name')),
|
|
696
|
+
alias(token.immediate(/[0-9*@#?$!_-]/), sym('special_variable_name'))
|
|
669
697
|
)
|
|
670
698
|
),
|
|
671
699
|
|
|
@@ -676,19 +704,19 @@ module.exports = grammar({
|
|
|
676
704
|
'${',
|
|
677
705
|
choice(
|
|
678
706
|
seq(
|
|
679
|
-
optional(field('prefix', alias(token.immediate(choice('#', '!')),
|
|
707
|
+
optional(field('prefix', alias(token.immediate(choice('#', '!')), sym('operator')))),
|
|
680
708
|
field(
|
|
681
709
|
'parameter',
|
|
682
710
|
choice(
|
|
683
|
-
alias(token.immediate(/[A-Za-z_][A-Za-z0-9_]*/),
|
|
684
|
-
alias(token.immediate(/[0-9]+|[*@?$_-]/),
|
|
685
|
-
alias(token.immediate(choice('#', '!')),
|
|
711
|
+
alias(token.immediate(/[A-Za-z_][A-Za-z0-9_]*/), sym('variable_name')),
|
|
712
|
+
alias(token.immediate(/[0-9]+|[*@?$_-]/), sym('special_variable_name')),
|
|
713
|
+
alias(token.immediate(choice('#', '!')), sym('special_variable_name')),
|
|
686
714
|
alias($._expansion_subscript, $.subscript)
|
|
687
715
|
)
|
|
688
716
|
),
|
|
689
717
|
optional($._expansion_operation)
|
|
690
718
|
),
|
|
691
|
-
field('parameter', alias(token.immediate(choice('#', '!')),
|
|
719
|
+
field('parameter', alias(token.immediate(choice('#', '!')), sym('special_variable_name'))),
|
|
692
720
|
// Bash rejects `${}` and a body without a parameter (`${=1}`, `${"x"}`) only when expanding. A
|
|
693
721
|
// `$name` expansion is left out, since its `$` would tie with the special parameter in `${$}`.
|
|
694
722
|
repeat(
|
|
@@ -712,7 +740,7 @@ module.exports = grammar({
|
|
|
712
740
|
|
|
713
741
|
_expansion_subscript: ($) =>
|
|
714
742
|
seq(
|
|
715
|
-
field('name', alias(token.immediate(/[A-Za-z_][A-Za-z0-9_]*/),
|
|
743
|
+
field('name', alias(token.immediate(/[A-Za-z_][A-Za-z0-9_]*/), sym('variable_name'))),
|
|
716
744
|
repeat1(seq(token.immediate('['), field('index', $._subscript_index), ']'))
|
|
717
745
|
),
|
|
718
746
|
|
|
@@ -750,7 +778,7 @@ module.exports = grammar({
|
|
|
750
778
|
'@'
|
|
751
779
|
)
|
|
752
780
|
),
|
|
753
|
-
|
|
781
|
+
sym('operator')
|
|
754
782
|
)
|
|
755
783
|
),
|
|
756
784
|
repeat(
|
|
@@ -761,12 +789,12 @@ module.exports = grammar({
|
|
|
761
789
|
alias($._blank_expansion_text, $.word),
|
|
762
790
|
alias($._angle_text, $.word),
|
|
763
791
|
$._quoted_or_expansion,
|
|
764
|
-
alias(
|
|
792
|
+
alias(sym('_bare_dollar'), $.word)
|
|
765
793
|
)
|
|
766
794
|
)
|
|
767
795
|
)
|
|
768
796
|
),
|
|
769
|
-
field('operator', alias(token.immediate(choice('*', '@')),
|
|
797
|
+
field('operator', alias(token.immediate(choice('*', '@')), sym('operator'))),
|
|
770
798
|
// Text without an operator (`${x y}`) is rejected by bash only when expanding.
|
|
771
799
|
repeat1(
|
|
772
800
|
field(
|
|
@@ -809,49 +837,61 @@ module.exports = grammar({
|
|
|
809
837
|
command_substitution: ($) =>
|
|
810
838
|
choice(
|
|
811
839
|
seq(
|
|
812
|
-
|
|
840
|
+
sym('_command_substitution_start'),
|
|
813
841
|
'$(',
|
|
814
|
-
|
|
842
|
+
sym('_substitution_start'),
|
|
815
843
|
optional($._statements),
|
|
816
|
-
|
|
844
|
+
sym('_substitution_end'),
|
|
817
845
|
')'
|
|
818
846
|
),
|
|
819
847
|
// bash 5.3 runs `${ list; }` and `${| list; }` in the current shell.
|
|
820
848
|
seq(
|
|
821
849
|
alias(token(/\$\{[ \t\n|]/), '${'),
|
|
822
|
-
|
|
850
|
+
sym('_brace_substitution_start'),
|
|
823
851
|
optional($._terminated_statements),
|
|
824
|
-
|
|
852
|
+
sym('_brace_substitution_end'),
|
|
825
853
|
'}'
|
|
826
854
|
),
|
|
827
|
-
seq(alias(
|
|
855
|
+
seq(alias(sym('_backtick_open'), '`'), optional($._statements), alias(sym('_backtick_close'), '`'))
|
|
828
856
|
),
|
|
829
857
|
|
|
830
858
|
process_substitution: ($) =>
|
|
831
|
-
seq(choice('<(', '>('),
|
|
859
|
+
seq(choice('<(', '>('), sym('_substitution_start'), optional($._statements), sym('_substitution_end'), ')'),
|
|
832
860
|
|
|
833
861
|
// Arithmetic may span lines, and a heredoc body starts only after it, so it counts as nesting.
|
|
834
862
|
arithmetic_expansion: ($) =>
|
|
835
863
|
choice(
|
|
836
|
-
seq(
|
|
837
|
-
|
|
864
|
+
seq(
|
|
865
|
+
sym('_arithmetic_substitution_start'),
|
|
866
|
+
'$((',
|
|
867
|
+
optional($._arithmetic_expression),
|
|
868
|
+
sym('_substitution_end'),
|
|
869
|
+
'))'
|
|
870
|
+
),
|
|
871
|
+
seq(
|
|
872
|
+
'$[',
|
|
873
|
+
sym('_bracket_substitution_start'),
|
|
874
|
+
optional($._arithmetic_expression),
|
|
875
|
+
sym('_bracket_substitution_end'),
|
|
876
|
+
']'
|
|
877
|
+
)
|
|
838
878
|
),
|
|
839
879
|
|
|
840
880
|
_arithmetic_expression: ($) =>
|
|
841
881
|
choice(
|
|
842
882
|
$.number,
|
|
843
|
-
alias($._arithmetic_variable,
|
|
883
|
+
alias($._arithmetic_variable, sym('variable_name')),
|
|
844
884
|
alias($._arithmetic_subscript, $.subscript),
|
|
845
885
|
$.simple_expansion,
|
|
846
886
|
$.expansion,
|
|
847
887
|
$.command_substitution,
|
|
848
888
|
$.arithmetic_expansion,
|
|
849
889
|
$.string,
|
|
850
|
-
alias($._arithmetic_unary,
|
|
851
|
-
alias($._arithmetic_postfix,
|
|
852
|
-
alias($._arithmetic_binary,
|
|
853
|
-
alias($._arithmetic_ternary,
|
|
854
|
-
alias($._arithmetic_parenthesized,
|
|
890
|
+
alias($._arithmetic_unary, sym('unary_expression')),
|
|
891
|
+
alias($._arithmetic_postfix, sym('postfix_expression')),
|
|
892
|
+
alias($._arithmetic_binary, sym('binary_expression')),
|
|
893
|
+
alias($._arithmetic_ternary, sym('ternary_expression')),
|
|
894
|
+
alias($._arithmetic_parenthesized, sym('parenthesized_expression'))
|
|
855
895
|
),
|
|
856
896
|
|
|
857
897
|
number: ($) =>
|
|
@@ -865,7 +905,7 @@ module.exports = grammar({
|
|
|
865
905
|
|
|
866
906
|
_arithmetic_subscript: ($) =>
|
|
867
907
|
seq(
|
|
868
|
-
field('name', alias($._arithmetic_variable,
|
|
908
|
+
field('name', alias($._arithmetic_variable, sym('variable_name'))),
|
|
869
909
|
token.immediate('['),
|
|
870
910
|
field('index', $._arithmetic_expression),
|
|
871
911
|
']'
|
|
@@ -881,6 +921,7 @@ module.exports = grammar({
|
|
|
881
921
|
prec(PREC.POSTFIX, seq($._arithmetic_expression, field('operator', choice('++', '--')))),
|
|
882
922
|
|
|
883
923
|
_arithmetic_binary: ($) => {
|
|
924
|
+
/** @type {[number, import('@willbooster/web-tree-sitter/dsl').RuleOrLiteral][]} */
|
|
884
925
|
const table = [
|
|
885
926
|
[PREC.COMMA, ','],
|
|
886
927
|
[PREC.ASSIGN, choice('=', '+=', '-=', '*=', '/=', '%=', '**=', '<<=', '>>=', '&=', '^=', '|=')],
|
|
@@ -900,12 +941,12 @@ module.exports = grammar({
|
|
|
900
941
|
...table.map(([precedence, operator]) => {
|
|
901
942
|
const rule = seq(
|
|
902
943
|
field('left', $._arithmetic_expression),
|
|
903
|
-
field('operator',
|
|
944
|
+
field('operator', operator),
|
|
904
945
|
field('right', $._arithmetic_expression)
|
|
905
946
|
);
|
|
906
947
|
return precedence === PREC.ASSIGN || precedence === PREC.EXPONENT
|
|
907
|
-
? prec.right(
|
|
908
|
-
: prec.left(
|
|
948
|
+
? prec.right(precedence, rule)
|
|
949
|
+
: prec.left(precedence, rule);
|
|
909
950
|
})
|
|
910
951
|
);
|
|
911
952
|
},
|
|
@@ -924,7 +965,7 @@ module.exports = grammar({
|
|
|
924
965
|
|
|
925
966
|
_arithmetic_parenthesized: ($) => seq('(', $._arithmetic_expression, ')'),
|
|
926
967
|
|
|
927
|
-
comment: (
|
|
968
|
+
comment: () => choice(token(prec(-10, /#.*/)), sym('_joined_comment'), sym('_backtick_comment')),
|
|
928
969
|
},
|
|
929
970
|
});
|
|
930
971
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@willbooster/tree-sitter-bash",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.2",
|
|
4
4
|
"description": "Bash grammar for tree-sitter",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"incremental",
|
|
@@ -55,7 +55,6 @@
|
|
|
55
55
|
"playwright": "1.63.0",
|
|
56
56
|
"semantic-release": "25.0.9",
|
|
57
57
|
"sort-package-json": "4.0.0",
|
|
58
|
-
"tree-sitter-cli": "0.27.0",
|
|
59
58
|
"typescript": "7.0.2",
|
|
60
59
|
"vite": "8.3.2",
|
|
61
60
|
"vitest": "5.0.3",
|
package/tree-sitter-bash.wasm
CHANGED
|
Binary file
|