@willbooster/tree-sitter-bash 0.25.1 → 1.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/README.md +33 -25
- package/grammar.js +200 -32
- package/package.json +19 -2
- package/prebuilds/darwin-arm64/tree-sitter-bash.node +0 -0
- package/prebuilds/darwin-x64/tree-sitter-bash.node +0 -0
- package/prebuilds/linux-arm64/tree-sitter-bash.node +0 -0
- package/prebuilds/linux-x64/tree-sitter-bash.node +0 -0
- package/queries/highlights.scm +33 -12
- package/src/grammar.json +821 -60
- package/src/node-types.json +206 -66
- package/src/parser.c +219488 -88692
- package/src/scanner.c +651 -92
- package/tree-sitter.json +2 -2
package/README.md
CHANGED
|
@@ -1,44 +1,52 @@
|
|
|
1
|
-
# tree-sitter-bash
|
|
1
|
+
# @willbooster/tree-sitter-bash
|
|
2
2
|
|
|
3
|
-
[![CI][ci]](https://github.com/
|
|
4
|
-
[![discord][discord]](https://discord.gg/w7nTvsVJhm)
|
|
5
|
-
[![matrix][matrix]](https://matrix.to/#/#tree-sitter-chat:matrix.org)
|
|
6
|
-
[![crates][crates]](https://crates.io/crates/tree-sitter-bash)
|
|
3
|
+
[![CI][ci]](https://github.com/WillBooster/tree-sitter-bash/actions/workflows/ci.yml)
|
|
7
4
|
[![npm][npm]](https://www.npmjs.com/package/@willbooster/tree-sitter-bash)
|
|
8
|
-
[![pypi][pypi]](https://pypi.org/project/tree-sitter-bash)
|
|
9
5
|
|
|
10
|
-
Bash grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter)
|
|
6
|
+
Bash grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter), rewritten from
|
|
7
|
+
[tree-sitter/tree-sitter-bash](https://github.com/tree-sitter/tree-sitter-bash). The syntax trees differ from the
|
|
8
|
+
original grammar's.
|
|
11
9
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
- Heredoc bodies are parsed wherever bash reads them: from the newline that ends the line holding the `<<` operator,
|
|
11
|
+
in operator order. Any statement may follow the operator on that line (`cat <<EOF; echo done`), and several heredocs
|
|
12
|
+
may share a line. A `heredoc_body` node appears as an extra next to the command that opened it. A heredoc whose
|
|
13
|
+
delimiter does not fit the parser's 1 KiB scanner state is an `ERROR` rather than a guessed body.
|
|
14
|
+
- Redirections stay inside the command they belong to, in source order (`npm > out run x`), and a command may
|
|
15
|
+
consist of assignments and redirections only.
|
|
16
|
+
- `$((…))` is always an arithmetic expansion, including inside heredoc bodies; escaped `\$` and `` \` `` are literal.
|
|
17
|
+
- A newline is a statement terminator only where one may end a statement; elsewhere it is whitespace.
|
|
15
18
|
|
|
16
|
-
|
|
17
|
-
npm install
|
|
18
|
-
```
|
|
19
|
+
## Usage
|
|
19
20
|
|
|
20
|
-
|
|
21
|
+
```js
|
|
22
|
+
const Parser = require('tree-sitter');
|
|
23
|
+
const Bash = require('@willbooster/tree-sitter-bash');
|
|
21
24
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
+
const parser = new Parser();
|
|
26
|
+
parser.setLanguage(Bash);
|
|
27
|
+
const tree = parser.parse('cat <<EOF; echo done\n$(date)\nEOF\n');
|
|
25
28
|
```
|
|
26
29
|
|
|
27
|
-
|
|
30
|
+
## Development
|
|
28
31
|
|
|
29
32
|
```sh
|
|
30
|
-
|
|
33
|
+
mise install
|
|
34
|
+
bun install --frozen-lockfile
|
|
35
|
+
bun run lint
|
|
36
|
+
bun run test
|
|
37
|
+
bun run tree-sitter generate
|
|
38
|
+
bun run tree-sitter test
|
|
39
|
+
script/parse-examples
|
|
31
40
|
```
|
|
32
41
|
|
|
42
|
+
`script/parse-examples` clones real-world scripts into `examples/`, parses them with the local
|
|
43
|
+
tree-sitter CLI, and rewrites `script/known-failures.txt`, the list of files that CI expects to fail.
|
|
44
|
+
|
|
33
45
|
### References
|
|
34
46
|
|
|
35
47
|
- [Bash man page](http://man7.org/linux/man-pages/man1/bash.1.html#SHELL_GRAMMAR)
|
|
36
48
|
- [Shell command language specification](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html)
|
|
37
|
-
- [
|
|
49
|
+
- [mvdan/sh - a shell parser in go](https://github.com/mvdan/sh)
|
|
38
50
|
|
|
39
|
-
[ci]: https://img.shields.io/github/actions/workflow/status/
|
|
40
|
-
[discord]: https://img.shields.io/discord/1063097320771698699?logo=discord&label=discord
|
|
41
|
-
[matrix]: https://img.shields.io/matrix/tree-sitter-chat%3Amatrix.org?logo=matrix&label=matrix
|
|
51
|
+
[ci]: https://img.shields.io/github/actions/workflow/status/WillBooster/tree-sitter-bash/ci.yml?logo=github&label=CI
|
|
42
52
|
[npm]: https://img.shields.io/npm/v/%40willbooster%2Ftree-sitter-bash?logo=npm
|
|
43
|
-
[crates]: https://img.shields.io/crates/v/tree-sitter-bash?logo=rust
|
|
44
|
-
[pypi]: https://img.shields.io/pypi/v/tree-sitter-bash?logo=pypi&logoColor=ffd242
|
package/grammar.js
CHANGED
|
@@ -10,7 +10,14 @@
|
|
|
10
10
|
// expansion. `#` only starts a comment at the beginning of a word, so it is handled separately.
|
|
11
11
|
const WORD_BREAKS = ['\\s', '|', '&', ';', '(', ')', '<', '>', '"', '\'', '`', '$', '\\\\'];
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
// Extglob groups nest (`@(a|+([0-9]))`); a regular expression expresses a fixed depth, and three levels
|
|
14
|
+
// cover every nested pattern in the example corpora.
|
|
15
|
+
// The depth must match EXTGLOB_LITERAL_DEPTH in src/scanner.c.
|
|
16
|
+
const EXTGLOB_GROUP = extglobGroup('[?*+@]', 3);
|
|
17
|
+
const EXTGLOB_NEGATION = extglobGroup('!', 3);
|
|
18
|
+
|
|
19
|
+
// Text inside `${…}` and subscripts stops before `<(`/`>(`, so a process substitution there is parsed.
|
|
20
|
+
const SUBSCRIPT_TEXT = /([^\[\]$`"'\\<>]|[<>]+[^(\[\]$`"'\\<>]|\\.)+/;
|
|
14
21
|
|
|
15
22
|
const PREC = {
|
|
16
23
|
COMMA: -1,
|
|
@@ -60,6 +67,14 @@ module.exports = grammar({
|
|
|
60
67
|
$._backtick_open,
|
|
61
68
|
$._backtick_close,
|
|
62
69
|
$._newline,
|
|
70
|
+
$._substitution_start,
|
|
71
|
+
$._substitution_end,
|
|
72
|
+
$._brace_substitution_end,
|
|
73
|
+
$._bracket_substitution_end,
|
|
74
|
+
$._extglob_prefix,
|
|
75
|
+
$._line_continuation,
|
|
76
|
+
$._brace_substitution_start,
|
|
77
|
+
$._bracket_substitution_start,
|
|
63
78
|
$.__error_recovery,
|
|
64
79
|
],
|
|
65
80
|
|
|
@@ -68,6 +83,7 @@ module.exports = grammar({
|
|
|
68
83
|
$.heredoc_body,
|
|
69
84
|
/\s/,
|
|
70
85
|
/\\\r?\n/,
|
|
86
|
+
$._line_continuation,
|
|
71
87
|
],
|
|
72
88
|
|
|
73
89
|
supertypes: $ => [
|
|
@@ -152,8 +168,12 @@ module.exports = grammar({
|
|
|
152
168
|
'coproc',
|
|
153
169
|
choice(
|
|
154
170
|
$.command,
|
|
155
|
-
|
|
156
|
-
|
|
171
|
+
// Like bash (`coproc [NAME] command [redirections]`), a compound body takes redirections.
|
|
172
|
+
seq(
|
|
173
|
+
optional(field('name', $.word)),
|
|
174
|
+
$._compound_command,
|
|
175
|
+
repeat(field('redirect', $._redirect)),
|
|
176
|
+
),
|
|
157
177
|
),
|
|
158
178
|
)),
|
|
159
179
|
|
|
@@ -168,7 +188,7 @@ module.exports = grammar({
|
|
|
168
188
|
seq(
|
|
169
189
|
repeat($._command_prefix),
|
|
170
190
|
field('name', $.command_name),
|
|
171
|
-
repeat(choice(field('argument', $.
|
|
191
|
+
repeat(choice(field('argument', $._argument), field('redirect', $._redirect))),
|
|
172
192
|
),
|
|
173
193
|
repeat1($._command_prefix),
|
|
174
194
|
)),
|
|
@@ -181,7 +201,7 @@ module.exports = grammar({
|
|
|
181
201
|
repeat($._command_prefix),
|
|
182
202
|
field('name', alias(choice('declare', 'typeset', 'export', 'readonly', 'local'), $.command_name)),
|
|
183
203
|
repeat(choice(
|
|
184
|
-
field('argument', choice($.variable_assignment, $.
|
|
204
|
+
field('argument', choice($.variable_assignment, $._argument)),
|
|
185
205
|
field('redirect', $._redirect),
|
|
186
206
|
)),
|
|
187
207
|
)),
|
|
@@ -189,9 +209,21 @@ module.exports = grammar({
|
|
|
189
209
|
variable_assignment: $ => seq(
|
|
190
210
|
field('name', choice($.variable_name, $.subscript)),
|
|
191
211
|
field('operator', token.immediate(choice('=', '+='))),
|
|
192
|
-
field('value', choice($.
|
|
212
|
+
field('value', choice($._argument, $._hash_value, $.array, $._empty_value)),
|
|
193
213
|
),
|
|
194
214
|
|
|
215
|
+
// A value is not at the start of a word, so a leading `#` is literal (`color=#fff`).
|
|
216
|
+
_hash_value: $ => choice(alias($._hash_word, $.word), alias($._hash_concatenation, $.concatenation)),
|
|
217
|
+
|
|
218
|
+
_hash_concatenation: $ => prec.right(seq(
|
|
219
|
+
alias($._hash_word, $.word),
|
|
220
|
+
repeat1(seq($._concat, choice(
|
|
221
|
+
$._word_part,
|
|
222
|
+
alias($._hash_word, $.word),
|
|
223
|
+
alias($._negated_extglob_pattern, $.extglob_pattern),
|
|
224
|
+
))),
|
|
225
|
+
)),
|
|
226
|
+
|
|
195
227
|
subscript: $ => seq(
|
|
196
228
|
field('name', $.variable_name),
|
|
197
229
|
token.immediate('['),
|
|
@@ -199,11 +231,18 @@ module.exports = grammar({
|
|
|
199
231
|
']',
|
|
200
232
|
),
|
|
201
233
|
|
|
234
|
+
// Like bash, the index extends to the matching `]`, so it may hold bracketed groups at any depth
|
|
235
|
+
// (`A[x[1[2]]]`), blanks, and newlines.
|
|
202
236
|
_subscript_index: $ => repeat1(choice(
|
|
203
|
-
alias(token.immediate(prec(1,
|
|
237
|
+
alias(token.immediate(prec(1, SUBSCRIPT_TEXT)), $.word),
|
|
238
|
+
alias(token(prec(1, SUBSCRIPT_TEXT)), $.word),
|
|
239
|
+
alias($._angle_text, $.word),
|
|
204
240
|
$._quoted_or_expansion,
|
|
241
|
+
$._subscript_group,
|
|
205
242
|
)),
|
|
206
243
|
|
|
244
|
+
_subscript_group: $ => seq('[', optional($._subscript_index), ']'),
|
|
245
|
+
|
|
207
246
|
// Word parts that carry their own delimiters, usable where a bare word would overrun a closing
|
|
208
247
|
// `]` or `}`.
|
|
209
248
|
_quoted_or_expansion: $ => choice(
|
|
@@ -215,11 +254,12 @@ module.exports = grammar({
|
|
|
215
254
|
$.expansion,
|
|
216
255
|
$.command_substitution,
|
|
217
256
|
$.arithmetic_expansion,
|
|
257
|
+
$.process_substitution,
|
|
218
258
|
),
|
|
219
259
|
|
|
220
260
|
array: $ => seq(
|
|
221
261
|
token.immediate('('),
|
|
222
|
-
repeat($.
|
|
262
|
+
repeat($._argument),
|
|
223
263
|
')',
|
|
224
264
|
),
|
|
225
265
|
|
|
@@ -232,7 +272,7 @@ module.exports = grammar({
|
|
|
232
272
|
choice(
|
|
233
273
|
seq(
|
|
234
274
|
field('operator', choice('<', '>', '>>', '>|', '<>', '&>', '&>>', '<&', '>&')),
|
|
235
|
-
field('destination', $.
|
|
275
|
+
field('destination', $._argument),
|
|
236
276
|
),
|
|
237
277
|
field('operator', choice('<&-', '>&-')),
|
|
238
278
|
),
|
|
@@ -241,7 +281,7 @@ module.exports = grammar({
|
|
|
241
281
|
herestring_redirect: $ => seq(
|
|
242
282
|
optional(field('descriptor', $.file_descriptor)),
|
|
243
283
|
field('operator', '<<<'),
|
|
244
|
-
field('value', $.
|
|
284
|
+
field('value', $._argument),
|
|
245
285
|
),
|
|
246
286
|
|
|
247
287
|
heredoc_redirect: $ => seq(
|
|
@@ -272,7 +312,9 @@ module.exports = grammar({
|
|
|
272
312
|
|
|
273
313
|
arithmetic_command: $ => seq(
|
|
274
314
|
'((',
|
|
315
|
+
$._substitution_start,
|
|
275
316
|
optional($._arithmetic_expression),
|
|
317
|
+
$._substitution_end,
|
|
276
318
|
'))',
|
|
277
319
|
),
|
|
278
320
|
|
|
@@ -290,13 +332,14 @@ module.exports = grammar({
|
|
|
290
332
|
|
|
291
333
|
unary_test: $ => prec(PREC.UNARY, seq(
|
|
292
334
|
field('operator', alias(choice(...TEST_UNARY_OPERATORS), $.test_operator)),
|
|
293
|
-
field('operand', $.
|
|
335
|
+
field('operand', $._argument),
|
|
294
336
|
)),
|
|
295
337
|
|
|
338
|
+
// The right side of `==`/`!=` is a pattern matched as if extglob were enabled.
|
|
296
339
|
binary_test: $ => prec(PREC.COMPARE, seq(
|
|
297
340
|
field('left', $._word),
|
|
298
341
|
field('operator', alias(choice(...TEST_BINARY_OPERATORS), $.test_operator)),
|
|
299
|
-
field('right', $.
|
|
342
|
+
field('right', $._argument),
|
|
300
343
|
)),
|
|
301
344
|
|
|
302
345
|
regex_test: $ => prec(PREC.COMPARE, seq(
|
|
@@ -344,19 +387,24 @@ module.exports = grammar({
|
|
|
344
387
|
for_statement: $ => seq(
|
|
345
388
|
field('keyword', choice('for', 'select')),
|
|
346
389
|
field('variable', alias($.word, $.variable_name)),
|
|
347
|
-
optional
|
|
348
|
-
|
|
390
|
+
// Without a word list the separator is optional (`for i do …`); a newline may precede `in`.
|
|
391
|
+
choice(
|
|
392
|
+
seq(optional($._newline), 'in', repeat(field('value', $._argument)), $._terminator),
|
|
393
|
+
optional($._terminator),
|
|
394
|
+
),
|
|
349
395
|
field('body', choice($.do_group, $.compound_statement)),
|
|
350
396
|
),
|
|
351
397
|
|
|
352
398
|
c_style_for_statement: $ => seq(
|
|
353
399
|
'for',
|
|
354
400
|
'((',
|
|
401
|
+
$._substitution_start,
|
|
355
402
|
field('initializer', optional($._arithmetic_expression)),
|
|
356
403
|
';',
|
|
357
404
|
field('condition', optional($._arithmetic_expression)),
|
|
358
405
|
';',
|
|
359
406
|
field('update', optional($._arithmetic_expression)),
|
|
407
|
+
$._substitution_end,
|
|
360
408
|
'))',
|
|
361
409
|
optional($._terminator),
|
|
362
410
|
field('body', choice($.do_group, $.compound_statement)),
|
|
@@ -364,7 +412,7 @@ module.exports = grammar({
|
|
|
364
412
|
|
|
365
413
|
case_statement: $ => seq(
|
|
366
414
|
'case',
|
|
367
|
-
field('value', $.
|
|
415
|
+
field('value', $._argument),
|
|
368
416
|
'in',
|
|
369
417
|
repeat($.case_item),
|
|
370
418
|
optional(alias($._last_case_item, $.case_item)),
|
|
@@ -382,8 +430,8 @@ module.exports = grammar({
|
|
|
382
430
|
|
|
383
431
|
_case_patterns: $ => seq(
|
|
384
432
|
optional('('),
|
|
385
|
-
field('pattern', $.
|
|
386
|
-
repeat(seq('|', field('pattern', $.
|
|
433
|
+
field('pattern', $._argument),
|
|
434
|
+
repeat(seq('|', field('pattern', $._argument))),
|
|
387
435
|
')',
|
|
388
436
|
),
|
|
389
437
|
|
|
@@ -399,13 +447,44 @@ module.exports = grammar({
|
|
|
399
447
|
|
|
400
448
|
_word: $ => choice($._word_part, $.concatenation),
|
|
401
449
|
|
|
450
|
+
// Outside command position a leading `!(` is a negated extglob group rather than `!` negating a
|
|
451
|
+
// subshell.
|
|
452
|
+
_argument: $ => choice(
|
|
453
|
+
$._word,
|
|
454
|
+
alias($._negated_extglob_word, $.word),
|
|
455
|
+
alias($._negated_extglob_pattern, $.extglob_pattern),
|
|
456
|
+
alias($._negated_extglob_concatenation, $.concatenation),
|
|
457
|
+
),
|
|
458
|
+
|
|
459
|
+
// A word led by a negated group (`!($x)b`, `!(a)$x`).
|
|
460
|
+
_negated_extglob_concatenation: $ => prec.right(seq(
|
|
461
|
+
choice(alias($._negated_extglob_pattern, $.extglob_pattern), alias($._negated_extglob_word, $.word)),
|
|
462
|
+
repeat1(seq($._concat, choice(
|
|
463
|
+
$._word_part,
|
|
464
|
+
alias($._hash_word, $.word),
|
|
465
|
+
alias($._negated_extglob_pattern, $.extglob_pattern),
|
|
466
|
+
))),
|
|
467
|
+
)),
|
|
468
|
+
|
|
469
|
+
_negated_extglob_word: _ => token(prec(1, seq(
|
|
470
|
+
EXTGLOB_NEGATION,
|
|
471
|
+
repeat(choice(noneOf(...WORD_BREAKS), /\\[^\r\n]/, EXTGLOB_GROUP, EXTGLOB_NEGATION)),
|
|
472
|
+
))),
|
|
473
|
+
|
|
402
474
|
concatenation: $ => prec.right(seq(
|
|
403
475
|
$._word_part,
|
|
404
|
-
repeat1(seq($._concat, choice(
|
|
476
|
+
repeat1(seq($._concat, choice(
|
|
477
|
+
$._word_part,
|
|
478
|
+
alias($._hash_word, $.word),
|
|
479
|
+
alias($._negated_extglob_pattern, $.extglob_pattern),
|
|
480
|
+
))),
|
|
405
481
|
)),
|
|
406
482
|
|
|
407
483
|
_word_part: $ => choice(
|
|
408
484
|
$.word,
|
|
485
|
+
// Literal text that the scanner ends before a structured extglob group (`foo@($x)`).
|
|
486
|
+
alias($._extglob_prefix, $.word),
|
|
487
|
+
$.extglob_pattern,
|
|
409
488
|
$.string,
|
|
410
489
|
$.raw_string,
|
|
411
490
|
$.ansi_c_string,
|
|
@@ -422,11 +501,34 @@ module.exports = grammar({
|
|
|
422
501
|
// before `(` negates a subshell.
|
|
423
502
|
word: _ => token(seq(
|
|
424
503
|
choice(noneOf('#', ...WORD_BREAKS), /\\[^\r\n]/, EXTGLOB_GROUP),
|
|
425
|
-
repeat(choice(noneOf(...WORD_BREAKS), /\\[^\r\n]/, EXTGLOB_GROUP,
|
|
504
|
+
repeat(choice(noneOf(...WORD_BREAKS), /\\[^\r\n]/, EXTGLOB_GROUP, EXTGLOB_NEGATION)),
|
|
426
505
|
)),
|
|
427
506
|
|
|
428
507
|
// A `#` inside a word (`a$b#c`) is literal; only a `#` that begins a word starts a comment.
|
|
429
|
-
|
|
508
|
+
// A group whose alternatives hold expansions (`@($(cmd)|b)`); a literal group stays inside a
|
|
509
|
+
// `word`, whose token is longer than the group's opening token.
|
|
510
|
+
extglob_pattern: $ => seq(
|
|
511
|
+
field('operator', choice('?(', '*(', '+(', '@(')),
|
|
512
|
+
optional($._extglob_alternative),
|
|
513
|
+
repeat(seq('|', optional($._extglob_alternative))),
|
|
514
|
+
')',
|
|
515
|
+
),
|
|
516
|
+
|
|
517
|
+
_negated_extglob_pattern: $ => seq(
|
|
518
|
+
field('operator', '!('),
|
|
519
|
+
optional($._extglob_alternative),
|
|
520
|
+
repeat(seq('|', optional($._extglob_alternative))),
|
|
521
|
+
')',
|
|
522
|
+
),
|
|
523
|
+
|
|
524
|
+
// An alternative may hold unquoted blanks, so each of its words is an `alternative` field; like
|
|
525
|
+
// an assignment value, a word there may start with `#` (`@($x #b)`).
|
|
526
|
+
_extglob_alternative: $ => repeat1(field('alternative', choice($._argument, $._hash_value))),
|
|
527
|
+
|
|
528
|
+
_hash_word: _ => token(prec(1, seq(
|
|
529
|
+
'#',
|
|
530
|
+
repeat(choice(noneOf(...WORD_BREAKS), /\\[^\r\n]/, EXTGLOB_GROUP, EXTGLOB_NEGATION)),
|
|
531
|
+
))),
|
|
430
532
|
|
|
431
533
|
string: $ => seq(
|
|
432
534
|
'"',
|
|
@@ -471,20 +573,33 @@ module.exports = grammar({
|
|
|
471
573
|
alias(token.immediate(/[A-Za-z_][A-Za-z0-9_]*/), $.variable_name),
|
|
472
574
|
alias(token.immediate(/[0-9]+|[*@?$_-]/), $.special_variable_name),
|
|
473
575
|
alias(token.immediate(choice('#', '!')), $.special_variable_name),
|
|
474
|
-
$._expansion_subscript,
|
|
576
|
+
alias($._expansion_subscript, $.subscript),
|
|
577
|
+
// A blank directly after `${` opens a function substitution, so these only take effect
|
|
578
|
+
// after a prefix (`${# y}`), which bash parses and rejects only when expanding.
|
|
579
|
+
alias(/[A-Za-z_][A-Za-z0-9_]*/, $.variable_name),
|
|
580
|
+
alias(/[0-9]+|[*@?$_-]/, $.special_variable_name),
|
|
475
581
|
)),
|
|
476
582
|
optional($._expansion_operation),
|
|
477
583
|
),
|
|
478
584
|
field('parameter', alias(token.immediate(choice('#', '!')), $.special_variable_name)),
|
|
585
|
+
// Bash rejects `${}` and a body without a parameter (`${=1}`, `${"x"}`) only when expanding. A
|
|
586
|
+
// `$name` expansion is left out, since its `$` would tie with the special parameter in `${$}`.
|
|
587
|
+
repeat(field('argument', choice(
|
|
588
|
+
alias($._expansion_text, $.word),
|
|
589
|
+
alias($._angle_text, $.word),
|
|
590
|
+
$.string,
|
|
591
|
+
$.raw_string,
|
|
592
|
+
$.ansi_c_string,
|
|
593
|
+
$.translated_string,
|
|
594
|
+
$.command_substitution,
|
|
595
|
+
))),
|
|
479
596
|
),
|
|
480
597
|
'}',
|
|
481
598
|
),
|
|
482
599
|
|
|
483
600
|
_expansion_subscript: $ => seq(
|
|
484
601
|
field('name', alias(token.immediate(/[A-Za-z_][A-Za-z0-9_]*/), $.variable_name)),
|
|
485
|
-
token.immediate('['),
|
|
486
|
-
field('index', $._subscript_index),
|
|
487
|
-
']',
|
|
602
|
+
repeat1(seq(token.immediate('['), field('index', $._subscript_index), ']')),
|
|
488
603
|
),
|
|
489
604
|
|
|
490
605
|
_expansion_operation: $ => choice(
|
|
@@ -496,25 +611,55 @@ module.exports = grammar({
|
|
|
496
611
|
)),
|
|
497
612
|
$.operator,
|
|
498
613
|
)),
|
|
499
|
-
repeat(field('argument', choice(
|
|
614
|
+
repeat(field('argument', choice(
|
|
615
|
+
alias($._expansion_text, $.word),
|
|
616
|
+
alias($._angle_text, $.word),
|
|
617
|
+
$._quoted_or_expansion,
|
|
618
|
+
alias($._bare_dollar, $.word),
|
|
619
|
+
))),
|
|
500
620
|
),
|
|
501
621
|
field('operator', alias(token.immediate(choice('*', '@')), $.operator)),
|
|
622
|
+
// Text without an operator (`${x y}`) is rejected by bash only when expanding.
|
|
623
|
+
repeat1(field('argument', choice(
|
|
624
|
+
alias($._expansion_text, $.word),
|
|
625
|
+
alias($._angle_text, $.word),
|
|
626
|
+
$._quoted_or_expansion,
|
|
627
|
+
))),
|
|
502
628
|
),
|
|
503
629
|
|
|
504
|
-
_expansion_text: _ => token(prec(-1, /([^}$`"'
|
|
630
|
+
_expansion_text: _ => token(prec(-1, /([^}$`"'\\<>]|[<>]+[^(}$`"'\\<>]|\\(.|\r?\n))+/)),
|
|
505
631
|
|
|
632
|
+
// A `<` or `>` that the text above cannot end with, e.g. before `}`. A single character, so that a
|
|
633
|
+
// run before `(` leaves its last `<`/`>` to open a process substitution (`${x:-a<<(b)}`).
|
|
634
|
+
_angle_text: _ => token(prec(-1, /[<>]/)),
|
|
635
|
+
|
|
636
|
+
// The zero-width substitution markers let the scanner track nesting, since a heredoc opened
|
|
637
|
+
// inside a substitution has its body read before the substitution ends.
|
|
506
638
|
command_substitution: $ => choice(
|
|
507
|
-
seq('$(', optional($._statements), ')'),
|
|
639
|
+
seq('$(', $._substitution_start, optional($._statements), $._substitution_end, ')'),
|
|
508
640
|
// bash 5.3 runs `${ list; }` and `${| list; }` in the current shell.
|
|
509
|
-
seq(
|
|
641
|
+
seq(
|
|
642
|
+
alias(token(/\$\{[\s|]/), '${'),
|
|
643
|
+
$._brace_substitution_start,
|
|
644
|
+
optional($._terminated_statements),
|
|
645
|
+
$._brace_substitution_end,
|
|
646
|
+
'}',
|
|
647
|
+
),
|
|
510
648
|
seq(alias($._backtick_open, '`'), optional($._statements), alias($._backtick_close, '`')),
|
|
511
649
|
),
|
|
512
650
|
|
|
513
|
-
process_substitution: $ => seq(
|
|
651
|
+
process_substitution: $ => seq(
|
|
652
|
+
choice('<(', '>('),
|
|
653
|
+
$._substitution_start,
|
|
654
|
+
optional($._statements),
|
|
655
|
+
$._substitution_end,
|
|
656
|
+
')',
|
|
657
|
+
),
|
|
514
658
|
|
|
659
|
+
// Arithmetic may span lines, and a heredoc body starts only after it, so it counts as nesting.
|
|
515
660
|
arithmetic_expansion: $ => choice(
|
|
516
|
-
seq('$((', optional($._arithmetic_expression), '))'),
|
|
517
|
-
seq('$[', optional($._arithmetic_expression), ']'),
|
|
661
|
+
seq('$((', $._substitution_start, optional($._arithmetic_expression), $._substitution_end, '))'),
|
|
662
|
+
seq('$[', $._bracket_substitution_start, optional($._arithmetic_expression), $._bracket_substitution_end, ']'),
|
|
518
663
|
),
|
|
519
664
|
|
|
520
665
|
// Arithmetic
|
|
@@ -535,7 +680,11 @@ module.exports = grammar({
|
|
|
535
680
|
alias($._arithmetic_parenthesized, $.parenthesized_expression),
|
|
536
681
|
),
|
|
537
682
|
|
|
538
|
-
number:
|
|
683
|
+
number: $ => choice(
|
|
684
|
+
/(0[xX][0-9a-fA-F]+|[0-9]+#[0-9a-zA-Z@_]+|[0-9]+)/,
|
|
685
|
+
// The digits of a based number may come from an expansion (`10#${x}`).
|
|
686
|
+
seq(/[0-9]+#/, choice($.simple_expansion, $.expansion)),
|
|
687
|
+
),
|
|
539
688
|
|
|
540
689
|
_arithmetic_variable: _ => /[A-Za-z_][A-Za-z0-9_]*/,
|
|
541
690
|
|
|
@@ -602,3 +751,22 @@ module.exports = grammar({
|
|
|
602
751
|
function noneOf(...characters) {
|
|
603
752
|
return new RegExp(`[^${characters.join('')}]`);
|
|
604
753
|
}
|
|
754
|
+
|
|
755
|
+
/**
|
|
756
|
+
* A regular expression matching a literal extglob group opened by `prefix`, whose body may contain
|
|
757
|
+
* groups nested up to `depth` levels.
|
|
758
|
+
*
|
|
759
|
+
* @param {string} prefix
|
|
760
|
+
* @param {number} depth
|
|
761
|
+
* @returns {RegExp}
|
|
762
|
+
*/
|
|
763
|
+
function extglobGroup(prefix, depth) {
|
|
764
|
+
// Quotes and expansions are left to `extglob_pattern`, which parses them. Blanks and newlines are
|
|
765
|
+
// part of a group, as bash's extglob lexer keeps them in the word.
|
|
766
|
+
const literal = '[^()\\\\$`\'"]';
|
|
767
|
+
let body = `(?:${literal}|\\\\.)`;
|
|
768
|
+
for (let level = 1; level < depth; level++) {
|
|
769
|
+
body = `(?:${literal}|\\\\.|[?*+@!]\\(${body}*\\))`;
|
|
770
|
+
}
|
|
771
|
+
return new RegExp(`${prefix}\\(${body}*\\)`);
|
|
772
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@willbooster/tree-sitter-bash",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Bash grammar for tree-sitter",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -38,9 +38,15 @@
|
|
|
38
38
|
"node-gyp-build": "^4.8.2"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
+
"@semantic-release/exec": "7.1.0",
|
|
42
|
+
"@willbooster/wb": "22.7.0",
|
|
43
|
+
"conventional-changelog-conventionalcommits": "9.3.1",
|
|
41
44
|
"eslint": "^9.12.0",
|
|
42
45
|
"eslint-config-treesitter": "^1.0.2",
|
|
46
|
+
"node-gyp": "13.0.2",
|
|
43
47
|
"prebuildify": "^6.0.1",
|
|
48
|
+
"semantic-release": "25.0.5",
|
|
49
|
+
"tree-sitter": "^0.25.0",
|
|
44
50
|
"tree-sitter-cli": "^0.27.0"
|
|
45
51
|
},
|
|
46
52
|
"peerDependencies": {
|
|
@@ -51,11 +57,22 @@
|
|
|
51
57
|
"optional": true
|
|
52
58
|
}
|
|
53
59
|
},
|
|
60
|
+
"patchedDependencies": {
|
|
61
|
+
"eslint-config-treesitter@1.0.2": "patches/eslint-config-treesitter@1.0.2.patch"
|
|
62
|
+
},
|
|
63
|
+
"trustedDependencies": [
|
|
64
|
+
"tree-sitter",
|
|
65
|
+
"tree-sitter-cli"
|
|
66
|
+
],
|
|
54
67
|
"scripts": {
|
|
55
68
|
"install": "node-gyp-build",
|
|
56
69
|
"lint": "eslint grammar.js",
|
|
57
70
|
"prestart": "tree-sitter build --wasm",
|
|
71
|
+
"release": "wb release",
|
|
72
|
+
"release-test": "node node_modules/semantic-release/bin/semantic-release.js --dry-run --plugins @semantic-release/commit-analyzer @semantic-release/release-notes-generator",
|
|
58
73
|
"start": "tree-sitter playground",
|
|
59
|
-
"test": "node --test bindings/node/*_test.js"
|
|
74
|
+
"test": "node --test bindings/node/*_test.js",
|
|
75
|
+
"build-prebuilds": "prebuildify --napi --strip --name tree-sitter-bash",
|
|
76
|
+
"build/ci": "tree-sitter generate"
|
|
60
77
|
}
|
|
61
78
|
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/queries/highlights.scm
CHANGED
|
@@ -1,22 +1,31 @@
|
|
|
1
1
|
[
|
|
2
2
|
(string)
|
|
3
3
|
(raw_string)
|
|
4
|
+
(ansi_c_string)
|
|
5
|
+
(translated_string)
|
|
4
6
|
(heredoc_body)
|
|
5
7
|
(heredoc_start)
|
|
6
8
|
] @string
|
|
7
9
|
|
|
10
|
+
(regex) @string.regexp
|
|
11
|
+
|
|
8
12
|
(command_name) @function
|
|
9
13
|
|
|
14
|
+
(function_definition
|
|
15
|
+
name: (word) @function)
|
|
16
|
+
|
|
10
17
|
(variable_name) @property
|
|
11
18
|
|
|
19
|
+
(special_variable_name) @variable.builtin
|
|
20
|
+
|
|
12
21
|
[
|
|
13
22
|
"case"
|
|
23
|
+
"coproc"
|
|
14
24
|
"do"
|
|
15
25
|
"done"
|
|
16
26
|
"elif"
|
|
17
27
|
"else"
|
|
18
28
|
"esac"
|
|
19
|
-
"export"
|
|
20
29
|
"fi"
|
|
21
30
|
"for"
|
|
22
31
|
"function"
|
|
@@ -24,33 +33,45 @@
|
|
|
24
33
|
"in"
|
|
25
34
|
"select"
|
|
26
35
|
"then"
|
|
27
|
-
"
|
|
36
|
+
"time"
|
|
28
37
|
"until"
|
|
29
38
|
"while"
|
|
30
39
|
] @keyword
|
|
31
40
|
|
|
32
41
|
(comment) @comment
|
|
33
42
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
(
|
|
43
|
+
[
|
|
44
|
+
(file_descriptor)
|
|
45
|
+
(number)
|
|
46
|
+
] @number
|
|
37
47
|
|
|
38
48
|
[
|
|
39
49
|
(command_substitution)
|
|
40
50
|
(process_substitution)
|
|
41
51
|
(expansion)
|
|
42
|
-
|
|
52
|
+
(arithmetic_expansion)
|
|
53
|
+
] @embedded
|
|
54
|
+
|
|
55
|
+
(test_operator) @operator
|
|
43
56
|
|
|
44
57
|
[
|
|
45
|
-
"
|
|
58
|
+
"!"
|
|
46
59
|
"&&"
|
|
60
|
+
"||"
|
|
61
|
+
"|"
|
|
62
|
+
"|&"
|
|
47
63
|
">"
|
|
48
64
|
">>"
|
|
49
65
|
"<"
|
|
50
|
-
"
|
|
66
|
+
"<<<"
|
|
67
|
+
"&>"
|
|
68
|
+
"&>>"
|
|
69
|
+
"<&"
|
|
70
|
+
">&"
|
|
71
|
+
">|"
|
|
72
|
+
"=~"
|
|
51
73
|
] @operator
|
|
52
74
|
|
|
53
|
-
(
|
|
54
|
-
|
|
55
|
-
(#match? @constant "^-")
|
|
56
|
-
)
|
|
75
|
+
((command
|
|
76
|
+
argument: (word) @constant)
|
|
77
|
+
(#match? @constant "^-"))
|