@willbooster/tree-sitter-bash 4.0.1 → 4.0.3
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 +9 -13
- package/grammar.js +158 -129
- package/package.json +11 -13
- package/tree-sitter-bash.wasm +0 -0
- package/tree-sitter.json +1 -1
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
[](https://www.npmjs.com/package/@willbooster/tree-sitter-bash)
|
|
5
5
|
[](https://github.com/WillBooster/tree-sitter-bash/actions/workflows/test.yml)
|
|
6
6
|
[](https://github.com/semantic-release/semantic-release)
|
|
7
|
-
[](https://github.com/WillBooster/shared/tree/main/packages/wbfy)
|
|
8
8
|
|
|
9
9
|
Bash grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter), forked from
|
|
10
10
|
[tree-sitter/tree-sitter-bash](https://github.com/tree-sitter/tree-sitter-bash). We are grateful to its authors and
|
|
@@ -17,12 +17,12 @@ As a result, its syntax trees differ from the original grammar's; review your qu
|
|
|
17
17
|
## Usage
|
|
18
18
|
|
|
19
19
|
The npm package ships `tree-sitter-bash.wasm` for
|
|
20
|
-
[@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter) 2.
|
|
20
|
+
[@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter) 2.x, a peer dependency.
|
|
21
21
|
The parser carries the ABI number of WillBooster/tree-sitter (`0x57420001`), so only that runtime loads it; the
|
|
22
|
-
original tree-sitter runtime
|
|
22
|
+
original tree-sitter runtime and other major versions of `@willbooster/web-tree-sitter` reject it:
|
|
23
23
|
|
|
24
24
|
```sh
|
|
25
|
-
npm install @willbooster/tree-sitter-bash @willbooster/web-tree-sitter
|
|
25
|
+
npm install @willbooster/tree-sitter-bash @willbooster/web-tree-sitter@^2.0.0
|
|
26
26
|
```
|
|
27
27
|
|
|
28
28
|
In Node.js and Bun, load the grammar from its path:
|
|
@@ -66,18 +66,14 @@ parser.setLanguage(await Language.load(bash));
|
|
|
66
66
|
|
|
67
67
|
The package also ships the queries in `queries/` and the node types in `src/node-types.json`.
|
|
68
68
|
|
|
69
|
-
|
|
70
|
-
|
|
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.
|
|
69
|
+
Recovery trees for malformed scripts can differ between releases, so review consumers that depend on the exact
|
|
70
|
+
shape of `ERROR` nodes.
|
|
75
71
|
|
|
76
72
|
Inside backquotes, the text of a node is the source text: consumers that read the value of a word or a string there
|
|
77
73
|
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.
|
|
79
|
-
|
|
80
|
-
|
|
74
|
+
before they remove quotes and escapes as usual. Ordinary word and double-quoted string scanning account for those
|
|
75
|
+
removals when recognizing expansions, quotes, blanks, and shell operators. Backquotes that delimit the substitution
|
|
76
|
+
retain their source escape parity. Line continuations, heredoc bodies, parameter-expansion text, regexes, nested
|
|
81
77
|
backquotes, and PID expansions split by a removed escape retain their existing parsing limitations.
|
|
82
78
|
|
|
83
79
|
## Development
|
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,48 +111,48 @@ 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
|
-
|
|
152
|
-
|
|
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'),
|
|
153
156
|
],
|
|
154
157
|
|
|
155
158
|
// Bash removes the backslash before `$` (and before `"` where the backquotes are inside double quotes)
|
|
@@ -160,9 +163,9 @@ module.exports = grammar({
|
|
|
160
163
|
$.heredoc_body,
|
|
161
164
|
/[ \t\n]/,
|
|
162
165
|
/\\\n/,
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
+
sym('_line_continuation'),
|
|
167
|
+
sym('_arithmetic_cache_reset'),
|
|
168
|
+
sym('_backquote_escape'),
|
|
166
169
|
],
|
|
167
170
|
|
|
168
171
|
supertypes: ($) => [$._statement, $._arithmetic_expression],
|
|
@@ -182,7 +185,7 @@ module.exports = grammar({
|
|
|
182
185
|
_terminated_statements: ($) => repeat1(seq($._statement, $._terminator)),
|
|
183
186
|
|
|
184
187
|
// A newline is significant only where it can end a statement; elsewhere it is whitespace.
|
|
185
|
-
_terminator: (
|
|
188
|
+
_terminator: () => choice(';', '&', sym('_newline')),
|
|
186
189
|
|
|
187
190
|
_statement: ($) =>
|
|
188
191
|
choice($._command_statement, $.pipeline, $.list, $.negated_command, $.timed_command, $.coproc_command),
|
|
@@ -219,9 +222,9 @@ module.exports = grammar({
|
|
|
219
222
|
prec.right(
|
|
220
223
|
2,
|
|
221
224
|
seq(
|
|
222
|
-
choice('time', alias(
|
|
223
|
-
optional(field('option', alias(choice('-p',
|
|
224
|
-
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)),
|
|
225
228
|
optional(choice($._command_statement, $.pipeline, $.negated_command))
|
|
226
229
|
)
|
|
227
230
|
),
|
|
@@ -251,7 +254,7 @@ module.exports = grammar({
|
|
|
251
254
|
field('name', $.command_name),
|
|
252
255
|
repeat(
|
|
253
256
|
seq(
|
|
254
|
-
optional(
|
|
257
|
+
optional(sym('_backtick_comment_boundary')),
|
|
255
258
|
choice(field('argument', $._argument), field('redirect', $._redirect))
|
|
256
259
|
)
|
|
257
260
|
)
|
|
@@ -262,7 +265,7 @@ module.exports = grammar({
|
|
|
262
265
|
|
|
263
266
|
_command_prefix: ($) => choice(field('assignment', $.variable_assignment), field('redirect', $._redirect)),
|
|
264
267
|
|
|
265
|
-
command_name: ($) => seq(optional(
|
|
268
|
+
command_name: ($) => seq(optional(sym('_backtick_comment_boundary')), $._word),
|
|
266
269
|
|
|
267
270
|
declaration_command: ($) =>
|
|
268
271
|
prec.left(
|
|
@@ -271,7 +274,7 @@ module.exports = grammar({
|
|
|
271
274
|
field('name', alias(choice('declare', 'typeset', 'export', 'readonly', 'local'), $.command_name)),
|
|
272
275
|
repeat(
|
|
273
276
|
seq(
|
|
274
|
-
optional(
|
|
277
|
+
optional(sym('_backtick_comment_boundary')),
|
|
275
278
|
choice(field('argument', choice($.variable_assignment, $._argument)), field('redirect', $._redirect))
|
|
276
279
|
)
|
|
277
280
|
)
|
|
@@ -280,9 +283,9 @@ module.exports = grammar({
|
|
|
280
283
|
|
|
281
284
|
variable_assignment: ($) =>
|
|
282
285
|
seq(
|
|
283
|
-
field('name', choice(
|
|
286
|
+
field('name', choice(sym('variable_name'), $.subscript)),
|
|
284
287
|
field('operator', token.immediate(choice('=', '+='))),
|
|
285
|
-
field('value', choice($._argument, $._hash_value, $.array,
|
|
288
|
+
field('value', choice($._argument, $._hash_value, $.array, sym('_empty_value')))
|
|
286
289
|
),
|
|
287
290
|
|
|
288
291
|
// A value is not at the start of a word, so a leading `#` is literal (`color=#fff`).
|
|
@@ -294,7 +297,7 @@ module.exports = grammar({
|
|
|
294
297
|
alias($._hash_word, $.word),
|
|
295
298
|
repeat1(
|
|
296
299
|
seq(
|
|
297
|
-
|
|
300
|
+
sym('_concat'),
|
|
298
301
|
choice($._word_part, alias($._hash_word, $.word), alias($._negated_extglob_pattern, $.extglob_pattern))
|
|
299
302
|
)
|
|
300
303
|
)
|
|
@@ -302,7 +305,7 @@ module.exports = grammar({
|
|
|
302
305
|
),
|
|
303
306
|
|
|
304
307
|
subscript: ($) =>
|
|
305
|
-
seq(field('name',
|
|
308
|
+
seq(field('name', sym('variable_name')), token.immediate('['), field('index', $._subscript_index), ']'),
|
|
306
309
|
|
|
307
310
|
// Like bash, the index extends to the matching `]`, so it may hold bracketed groups at any depth
|
|
308
311
|
// (`A[x[1[2]]]`), blanks, and newlines.
|
|
@@ -340,7 +343,7 @@ module.exports = grammar({
|
|
|
340
343
|
|
|
341
344
|
file_redirect: ($) =>
|
|
342
345
|
seq(
|
|
343
|
-
optional(field('descriptor',
|
|
346
|
+
optional(field('descriptor', sym('file_descriptor'))),
|
|
344
347
|
choice(
|
|
345
348
|
seq(
|
|
346
349
|
field('operator', choice('<', '>', '>>', '>|', '<>', '&>', '&>>', '<&', '>&')),
|
|
@@ -351,24 +354,30 @@ module.exports = grammar({
|
|
|
351
354
|
),
|
|
352
355
|
|
|
353
356
|
herestring_redirect: ($) =>
|
|
354
|
-
seq(optional(field('descriptor',
|
|
357
|
+
seq(optional(field('descriptor', sym('file_descriptor'))), field('operator', '<<<'), field('value', $._argument)),
|
|
355
358
|
|
|
356
|
-
heredoc_redirect: (
|
|
359
|
+
heredoc_redirect: () =>
|
|
357
360
|
seq(
|
|
358
|
-
optional(field('descriptor',
|
|
359
|
-
field('operator', choice(alias(
|
|
360
|
-
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'))
|
|
361
364
|
),
|
|
362
365
|
|
|
363
366
|
// The scanner opens a body at the newline that ends the line holding its `<<` operator, so bodies
|
|
364
367
|
// appear, in operator order, wherever that newline falls.
|
|
365
368
|
heredoc_body: ($) =>
|
|
366
369
|
seq(
|
|
367
|
-
|
|
370
|
+
sym('_heredoc_body_start'),
|
|
368
371
|
repeat(
|
|
369
|
-
choice(
|
|
372
|
+
choice(
|
|
373
|
+
sym('heredoc_content'),
|
|
374
|
+
$.simple_expansion,
|
|
375
|
+
$.expansion,
|
|
376
|
+
$.command_substitution,
|
|
377
|
+
$.arithmetic_expansion
|
|
378
|
+
)
|
|
370
379
|
),
|
|
371
|
-
|
|
380
|
+
sym('heredoc_end')
|
|
372
381
|
),
|
|
373
382
|
|
|
374
383
|
compound_statement: ($) => seq('{', optional($._terminated_statements), '}'),
|
|
@@ -376,7 +385,7 @@ module.exports = grammar({
|
|
|
376
385
|
subshell: ($) => seq('(', optional($._statements), ')'),
|
|
377
386
|
|
|
378
387
|
arithmetic_command: ($) =>
|
|
379
|
-
seq('((',
|
|
388
|
+
seq('((', sym('_substitution_start'), optional($._arithmetic_expression), sym('_substitution_end'), '))'),
|
|
380
389
|
|
|
381
390
|
test_command: ($) => seq('[[', $._test_expression, ']]'),
|
|
382
391
|
|
|
@@ -386,15 +395,18 @@ module.exports = grammar({
|
|
|
386
395
|
$.unary_test,
|
|
387
396
|
$.binary_test,
|
|
388
397
|
$.regex_test,
|
|
389
|
-
alias($._test_negation,
|
|
390
|
-
alias($._test_logical,
|
|
391
|
-
alias($._test_parenthesized,
|
|
398
|
+
alias($._test_negation, sym('negated_test')),
|
|
399
|
+
alias($._test_logical, sym('logical_test')),
|
|
400
|
+
alias($._test_parenthesized, sym('parenthesized_test'))
|
|
392
401
|
),
|
|
393
402
|
|
|
394
403
|
unary_test: ($) =>
|
|
395
404
|
prec(
|
|
396
405
|
PREC.UNARY,
|
|
397
|
-
seq(
|
|
406
|
+
seq(
|
|
407
|
+
field('operator', alias(choice(...TEST_UNARY_OPERATORS), sym('test_operator'))),
|
|
408
|
+
field('operand', $._argument)
|
|
409
|
+
)
|
|
398
410
|
),
|
|
399
411
|
|
|
400
412
|
// The right side of `==`/`!=` is a pattern matched as if extglob were enabled.
|
|
@@ -403,7 +415,7 @@ module.exports = grammar({
|
|
|
403
415
|
PREC.COMPARE,
|
|
404
416
|
seq(
|
|
405
417
|
field('left', $._word),
|
|
406
|
-
field('operator', alias(choice(...TEST_BINARY_OPERATORS),
|
|
418
|
+
field('operator', alias(choice(...TEST_BINARY_OPERATORS), sym('test_operator'))),
|
|
407
419
|
field('right', $._argument)
|
|
408
420
|
)
|
|
409
421
|
),
|
|
@@ -416,14 +428,14 @@ module.exports = grammar({
|
|
|
416
428
|
seq(
|
|
417
429
|
field('left', $._word),
|
|
418
430
|
field('operator', '=~'),
|
|
419
|
-
|
|
431
|
+
sym('_regex_start'),
|
|
420
432
|
field('right', choice($._regex_part, alias($._regex_concatenation, $.concatenation)))
|
|
421
433
|
)
|
|
422
434
|
),
|
|
423
435
|
|
|
424
|
-
_regex_concatenation: ($) => prec.right(seq($._regex_part, repeat1(seq(
|
|
436
|
+
_regex_concatenation: ($) => prec.right(seq($._regex_part, repeat1(seq(sym('_regex_concat'), $._regex_part)))),
|
|
425
437
|
|
|
426
|
-
_regex_part: ($) => choice(
|
|
438
|
+
_regex_part: ($) => choice(sym('regex'), $._quoted_or_expansion),
|
|
427
439
|
|
|
428
440
|
_test_negation: ($) => prec(PREC.UNARY, seq('!', $._test_expression)),
|
|
429
441
|
|
|
@@ -463,20 +475,20 @@ module.exports = grammar({
|
|
|
463
475
|
field('body', $.do_group)
|
|
464
476
|
),
|
|
465
477
|
|
|
466
|
-
do_group: ($) => seq(optional(
|
|
478
|
+
do_group: ($) => seq(optional(sym('_backtick_comment_boundary')), 'do', optional($._terminated_statements), 'done'),
|
|
467
479
|
|
|
468
480
|
for_statement: ($) =>
|
|
469
481
|
seq(
|
|
470
482
|
field('keyword', choice('for', 'select')),
|
|
471
|
-
field('variable', alias($.word,
|
|
483
|
+
field('variable', alias($.word, sym('variable_name'))),
|
|
472
484
|
// Without a word list the separator is optional (`for i do …`); a newline may precede `in`.
|
|
473
485
|
choice(
|
|
474
486
|
seq(
|
|
475
|
-
optional(
|
|
476
|
-
optional(
|
|
487
|
+
optional(sym('_newline')),
|
|
488
|
+
optional(sym('_backtick_comment_boundary')),
|
|
477
489
|
'in',
|
|
478
|
-
repeat(seq(optional(
|
|
479
|
-
optional(
|
|
490
|
+
repeat(seq(optional(sym('_backtick_comment_boundary')), field('value', $._argument))),
|
|
491
|
+
optional(sym('_backtick_comment_boundary')),
|
|
480
492
|
$._terminator
|
|
481
493
|
),
|
|
482
494
|
optional($._terminator)
|
|
@@ -488,13 +500,13 @@ module.exports = grammar({
|
|
|
488
500
|
seq(
|
|
489
501
|
'for',
|
|
490
502
|
'((',
|
|
491
|
-
|
|
503
|
+
sym('_substitution_start'),
|
|
492
504
|
field('initializer', optional($._arithmetic_expression)),
|
|
493
505
|
';',
|
|
494
506
|
field('condition', optional($._arithmetic_expression)),
|
|
495
507
|
';',
|
|
496
508
|
field('update', optional($._arithmetic_expression)),
|
|
497
|
-
|
|
509
|
+
sym('_substitution_end'),
|
|
498
510
|
'))',
|
|
499
511
|
optional($._terminator),
|
|
500
512
|
field('body', choice($.do_group, $.compound_statement))
|
|
@@ -504,7 +516,7 @@ module.exports = grammar({
|
|
|
504
516
|
seq(
|
|
505
517
|
'case',
|
|
506
518
|
field('value', $._argument),
|
|
507
|
-
optional(
|
|
519
|
+
optional(sym('_backtick_comment_boundary')),
|
|
508
520
|
'in',
|
|
509
521
|
repeat($.case_item),
|
|
510
522
|
optional(alias($._last_case_item, $.case_item)),
|
|
@@ -519,7 +531,7 @@ module.exports = grammar({
|
|
|
519
531
|
|
|
520
532
|
_case_patterns: ($) =>
|
|
521
533
|
seq(
|
|
522
|
-
optional(
|
|
534
|
+
optional(sym('_backtick_comment_boundary')),
|
|
523
535
|
optional('('),
|
|
524
536
|
field('pattern', $._argument),
|
|
525
537
|
repeat(seq('|', field('pattern', $._argument))),
|
|
@@ -529,7 +541,7 @@ module.exports = grammar({
|
|
|
529
541
|
function_definition: ($) =>
|
|
530
542
|
seq(
|
|
531
543
|
choice(seq('function', field('name', $.word), optional(seq('(', ')'))), seq(field('name', $.word), '(', ')')),
|
|
532
|
-
optional(
|
|
544
|
+
optional(sym('_backtick_comment_boundary')),
|
|
533
545
|
field('body', $._compound_command)
|
|
534
546
|
),
|
|
535
547
|
|
|
@@ -552,7 +564,7 @@ module.exports = grammar({
|
|
|
552
564
|
choice(alias($._negated_extglob_pattern, $.extglob_pattern), alias($._negated_extglob_word, $.word)),
|
|
553
565
|
repeat1(
|
|
554
566
|
seq(
|
|
555
|
-
|
|
567
|
+
sym('_concat'),
|
|
556
568
|
choice($._word_part, alias($._hash_word, $.word), alias($._negated_extglob_pattern, $.extglob_pattern))
|
|
557
569
|
)
|
|
558
570
|
)
|
|
@@ -573,7 +585,7 @@ module.exports = grammar({
|
|
|
573
585
|
$._word_part,
|
|
574
586
|
repeat1(
|
|
575
587
|
seq(
|
|
576
|
-
|
|
588
|
+
sym('_concat'),
|
|
577
589
|
choice($._word_part, alias($._hash_word, $.word), alias($._negated_extglob_pattern, $.extglob_pattern))
|
|
578
590
|
)
|
|
579
591
|
)
|
|
@@ -584,7 +596,7 @@ module.exports = grammar({
|
|
|
584
596
|
choice(
|
|
585
597
|
$.word,
|
|
586
598
|
// Literal text that the scanner ends before a structured extglob group (`foo@($x)`).
|
|
587
|
-
alias(
|
|
599
|
+
alias(sym('_extglob_prefix'), $.word),
|
|
588
600
|
$.extglob_pattern,
|
|
589
601
|
$.string,
|
|
590
602
|
$.raw_string,
|
|
@@ -595,7 +607,7 @@ module.exports = grammar({
|
|
|
595
607
|
$.command_substitution,
|
|
596
608
|
$.arithmetic_expansion,
|
|
597
609
|
$.process_substitution,
|
|
598
|
-
alias(
|
|
610
|
+
alias(sym('_bare_dollar'), $.word)
|
|
599
611
|
),
|
|
600
612
|
|
|
601
613
|
// An extglob group (`@(a|b)`) is part of the word; `!(` only inside a word, since a leading `!`
|
|
@@ -638,7 +650,7 @@ module.exports = grammar({
|
|
|
638
650
|
seq(
|
|
639
651
|
'"',
|
|
640
652
|
repeat(
|
|
641
|
-
choice(
|
|
653
|
+
choice(sym('string_content'), $.simple_expansion, $.expansion, $.command_substitution, $.arithmetic_expansion)
|
|
642
654
|
),
|
|
643
655
|
'"'
|
|
644
656
|
),
|
|
@@ -647,18 +659,22 @@ module.exports = grammar({
|
|
|
647
659
|
// with its line continuations removed, even inside single quotes, and reads the bodies still queued
|
|
648
660
|
// before that line right after it, even where it ends inside quotes; the scanner then splits the
|
|
649
661
|
// quoted text around the continuations and the bodies.
|
|
650
|
-
raw_string: (
|
|
662
|
+
raw_string: () =>
|
|
651
663
|
choice(
|
|
652
664
|
/'[^']*'/,
|
|
653
|
-
seq(
|
|
665
|
+
seq(
|
|
666
|
+
sym('_closer_line_raw_string_start'),
|
|
667
|
+
repeat(alias(sym('_closer_line_raw_string_content'), sym('string_content'))),
|
|
668
|
+
"'"
|
|
669
|
+
)
|
|
654
670
|
),
|
|
655
671
|
|
|
656
|
-
ansi_c_string: (
|
|
672
|
+
ansi_c_string: () =>
|
|
657
673
|
choice(
|
|
658
674
|
/\$'([^'\\]|\\(.|\n))*'/,
|
|
659
675
|
seq(
|
|
660
|
-
|
|
661
|
-
repeat(alias(
|
|
676
|
+
sym('_closer_line_ansi_c_string_start'),
|
|
677
|
+
repeat(alias(sym('_closer_line_ansi_c_string_content'), sym('string_content'))),
|
|
662
678
|
"'"
|
|
663
679
|
)
|
|
664
680
|
),
|
|
@@ -667,17 +683,17 @@ module.exports = grammar({
|
|
|
667
683
|
seq(
|
|
668
684
|
'$"',
|
|
669
685
|
repeat(
|
|
670
|
-
choice(
|
|
686
|
+
choice(sym('string_content'), $.simple_expansion, $.expansion, $.command_substitution, $.arithmetic_expansion)
|
|
671
687
|
),
|
|
672
688
|
'"'
|
|
673
689
|
),
|
|
674
690
|
|
|
675
|
-
simple_expansion: (
|
|
691
|
+
simple_expansion: () =>
|
|
676
692
|
seq(
|
|
677
693
|
'$',
|
|
678
694
|
choice(
|
|
679
|
-
alias(token.immediate(/[A-Za-z_][A-Za-z0-9_]*/),
|
|
680
|
-
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'))
|
|
681
697
|
)
|
|
682
698
|
),
|
|
683
699
|
|
|
@@ -688,19 +704,19 @@ module.exports = grammar({
|
|
|
688
704
|
'${',
|
|
689
705
|
choice(
|
|
690
706
|
seq(
|
|
691
|
-
optional(field('prefix', alias(token.immediate(choice('#', '!')),
|
|
707
|
+
optional(field('prefix', alias(token.immediate(choice('#', '!')), sym('operator')))),
|
|
692
708
|
field(
|
|
693
709
|
'parameter',
|
|
694
710
|
choice(
|
|
695
|
-
alias(token.immediate(/[A-Za-z_][A-Za-z0-9_]*/),
|
|
696
|
-
alias(token.immediate(/[0-9]+|[*@?$_-]/),
|
|
697
|
-
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')),
|
|
698
714
|
alias($._expansion_subscript, $.subscript)
|
|
699
715
|
)
|
|
700
716
|
),
|
|
701
717
|
optional($._expansion_operation)
|
|
702
718
|
),
|
|
703
|
-
field('parameter', alias(token.immediate(choice('#', '!')),
|
|
719
|
+
field('parameter', alias(token.immediate(choice('#', '!')), sym('special_variable_name'))),
|
|
704
720
|
// Bash rejects `${}` and a body without a parameter (`${=1}`, `${"x"}`) only when expanding. A
|
|
705
721
|
// `$name` expansion is left out, since its `$` would tie with the special parameter in `${$}`.
|
|
706
722
|
repeat(
|
|
@@ -724,7 +740,7 @@ module.exports = grammar({
|
|
|
724
740
|
|
|
725
741
|
_expansion_subscript: ($) =>
|
|
726
742
|
seq(
|
|
727
|
-
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'))),
|
|
728
744
|
repeat1(seq(token.immediate('['), field('index', $._subscript_index), ']'))
|
|
729
745
|
),
|
|
730
746
|
|
|
@@ -762,7 +778,7 @@ module.exports = grammar({
|
|
|
762
778
|
'@'
|
|
763
779
|
)
|
|
764
780
|
),
|
|
765
|
-
|
|
781
|
+
sym('operator')
|
|
766
782
|
)
|
|
767
783
|
),
|
|
768
784
|
repeat(
|
|
@@ -773,12 +789,12 @@ module.exports = grammar({
|
|
|
773
789
|
alias($._blank_expansion_text, $.word),
|
|
774
790
|
alias($._angle_text, $.word),
|
|
775
791
|
$._quoted_or_expansion,
|
|
776
|
-
alias(
|
|
792
|
+
alias(sym('_bare_dollar'), $.word)
|
|
777
793
|
)
|
|
778
794
|
)
|
|
779
795
|
)
|
|
780
796
|
),
|
|
781
|
-
field('operator', alias(token.immediate(choice('*', '@')),
|
|
797
|
+
field('operator', alias(token.immediate(choice('*', '@')), sym('operator'))),
|
|
782
798
|
// Text without an operator (`${x y}`) is rejected by bash only when expanding.
|
|
783
799
|
repeat1(
|
|
784
800
|
field(
|
|
@@ -821,49 +837,61 @@ module.exports = grammar({
|
|
|
821
837
|
command_substitution: ($) =>
|
|
822
838
|
choice(
|
|
823
839
|
seq(
|
|
824
|
-
|
|
840
|
+
sym('_command_substitution_start'),
|
|
825
841
|
'$(',
|
|
826
|
-
|
|
842
|
+
sym('_substitution_start'),
|
|
827
843
|
optional($._statements),
|
|
828
|
-
|
|
844
|
+
sym('_substitution_end'),
|
|
829
845
|
')'
|
|
830
846
|
),
|
|
831
847
|
// bash 5.3 runs `${ list; }` and `${| list; }` in the current shell.
|
|
832
848
|
seq(
|
|
833
849
|
alias(token(/\$\{[ \t\n|]/), '${'),
|
|
834
|
-
|
|
850
|
+
sym('_brace_substitution_start'),
|
|
835
851
|
optional($._terminated_statements),
|
|
836
|
-
|
|
852
|
+
sym('_brace_substitution_end'),
|
|
837
853
|
'}'
|
|
838
854
|
),
|
|
839
|
-
seq(alias(
|
|
855
|
+
seq(alias(sym('_backtick_open'), '`'), optional($._statements), alias(sym('_backtick_close'), '`'))
|
|
840
856
|
),
|
|
841
857
|
|
|
842
858
|
process_substitution: ($) =>
|
|
843
|
-
seq(choice('<(', '>('),
|
|
859
|
+
seq(choice('<(', '>('), sym('_substitution_start'), optional($._statements), sym('_substitution_end'), ')'),
|
|
844
860
|
|
|
845
861
|
// Arithmetic may span lines, and a heredoc body starts only after it, so it counts as nesting.
|
|
846
862
|
arithmetic_expansion: ($) =>
|
|
847
863
|
choice(
|
|
848
|
-
seq(
|
|
849
|
-
|
|
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
|
+
)
|
|
850
878
|
),
|
|
851
879
|
|
|
852
880
|
_arithmetic_expression: ($) =>
|
|
853
881
|
choice(
|
|
854
882
|
$.number,
|
|
855
|
-
alias($._arithmetic_variable,
|
|
883
|
+
alias($._arithmetic_variable, sym('variable_name')),
|
|
856
884
|
alias($._arithmetic_subscript, $.subscript),
|
|
857
885
|
$.simple_expansion,
|
|
858
886
|
$.expansion,
|
|
859
887
|
$.command_substitution,
|
|
860
888
|
$.arithmetic_expansion,
|
|
861
889
|
$.string,
|
|
862
|
-
alias($._arithmetic_unary,
|
|
863
|
-
alias($._arithmetic_postfix,
|
|
864
|
-
alias($._arithmetic_binary,
|
|
865
|
-
alias($._arithmetic_ternary,
|
|
866
|
-
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'))
|
|
867
895
|
),
|
|
868
896
|
|
|
869
897
|
number: ($) =>
|
|
@@ -877,7 +905,7 @@ module.exports = grammar({
|
|
|
877
905
|
|
|
878
906
|
_arithmetic_subscript: ($) =>
|
|
879
907
|
seq(
|
|
880
|
-
field('name', alias($._arithmetic_variable,
|
|
908
|
+
field('name', alias($._arithmetic_variable, sym('variable_name'))),
|
|
881
909
|
token.immediate('['),
|
|
882
910
|
field('index', $._arithmetic_expression),
|
|
883
911
|
']'
|
|
@@ -893,6 +921,7 @@ module.exports = grammar({
|
|
|
893
921
|
prec(PREC.POSTFIX, seq($._arithmetic_expression, field('operator', choice('++', '--')))),
|
|
894
922
|
|
|
895
923
|
_arithmetic_binary: ($) => {
|
|
924
|
+
/** @type {[number, import('@willbooster/web-tree-sitter/dsl').RuleOrLiteral][]} */
|
|
896
925
|
const table = [
|
|
897
926
|
[PREC.COMMA, ','],
|
|
898
927
|
[PREC.ASSIGN, choice('=', '+=', '-=', '*=', '/=', '%=', '**=', '<<=', '>>=', '&=', '^=', '|=')],
|
|
@@ -912,12 +941,12 @@ module.exports = grammar({
|
|
|
912
941
|
...table.map(([precedence, operator]) => {
|
|
913
942
|
const rule = seq(
|
|
914
943
|
field('left', $._arithmetic_expression),
|
|
915
|
-
field('operator',
|
|
944
|
+
field('operator', operator),
|
|
916
945
|
field('right', $._arithmetic_expression)
|
|
917
946
|
);
|
|
918
947
|
return precedence === PREC.ASSIGN || precedence === PREC.EXPONENT
|
|
919
|
-
? prec.right(
|
|
920
|
-
: prec.left(
|
|
948
|
+
? prec.right(precedence, rule)
|
|
949
|
+
: prec.left(precedence, rule);
|
|
921
950
|
})
|
|
922
951
|
);
|
|
923
952
|
},
|
|
@@ -936,7 +965,7 @@ module.exports = grammar({
|
|
|
936
965
|
|
|
937
966
|
_arithmetic_parenthesized: ($) => seq('(', $._arithmetic_expression, ')'),
|
|
938
967
|
|
|
939
|
-
comment: (
|
|
968
|
+
comment: () => choice(token(prec(-10, /#.*/)), sym('_joined_comment'), sym('_backtick_comment')),
|
|
940
969
|
},
|
|
941
970
|
});
|
|
942
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.3",
|
|
4
4
|
"description": "Bash grammar for tree-sitter",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"incremental",
|
|
@@ -45,21 +45,19 @@
|
|
|
45
45
|
"@vitest/browser-playwright": "5.0.3",
|
|
46
46
|
"@willbooster/oxfmt-config": "1.2.2",
|
|
47
47
|
"@willbooster/oxlint-config": "1.4.8",
|
|
48
|
-
"@willbooster/wb": "22.
|
|
49
|
-
"@willbooster/web-tree-sitter": "2.0.
|
|
50
|
-
"conventional-changelog-conventionalcommits": "10.4.
|
|
51
|
-
"lefthook": "2.1
|
|
52
|
-
"oxfmt": "0.
|
|
53
|
-
"oxlint": "1.
|
|
54
|
-
"oxlint-tsgolint": "7.0.
|
|
55
|
-
"playwright": "1.
|
|
48
|
+
"@willbooster/wb": "22.26.1",
|
|
49
|
+
"@willbooster/web-tree-sitter": "2.0.26",
|
|
50
|
+
"conventional-changelog-conventionalcommits": "10.4.1",
|
|
51
|
+
"lefthook": "2.2.1",
|
|
52
|
+
"oxfmt": "0.72.0",
|
|
53
|
+
"oxlint": "1.87.0",
|
|
54
|
+
"oxlint-tsgolint": "7.0.2004",
|
|
55
|
+
"playwright": "1.64.0",
|
|
56
56
|
"semantic-release": "25.0.9",
|
|
57
57
|
"sort-package-json": "4.0.0",
|
|
58
|
-
"
|
|
59
|
-
"typescript": "7.0.2",
|
|
60
|
-
"vite": "8.3.2",
|
|
58
|
+
"vite": "8.3.4",
|
|
61
59
|
"vitest": "5.0.3",
|
|
62
|
-
"wrangler": "4.
|
|
60
|
+
"wrangler": "4.149.0"
|
|
63
61
|
},
|
|
64
62
|
"peerDependencies": {
|
|
65
63
|
"@willbooster/web-tree-sitter": "^2.0.0"
|
package/tree-sitter-bash.wasm
CHANGED
|
Binary file
|