@willbooster/tree-sitter-bash 1.1.1 → 1.1.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 +6 -11
- package/grammar.js +25 -10
- package/package.json +2 -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/src/grammar.json +137 -27
- package/src/node-types.json +4 -5
- package/src/parser.c +97265 -105158
- package/src/scanner.c +121 -28
- package/tree-sitter-bash.wasm +0 -0
- package/tree-sitter.json +2 -7
package/README.md
CHANGED
|
@@ -6,18 +6,13 @@
|
|
|
6
6
|
[](https://github.com/semantic-release/semantic-release)
|
|
7
7
|
[](https://github.com/WillBooster/shared/tree/main/packages/wbfy)
|
|
8
8
|
|
|
9
|
-
Bash grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter),
|
|
10
|
-
[tree-sitter/tree-sitter-bash](https://github.com/tree-sitter/tree-sitter-bash).
|
|
11
|
-
|
|
9
|
+
Bash grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter), forked from
|
|
10
|
+
[tree-sitter/tree-sitter-bash](https://github.com/tree-sitter/tree-sitter-bash). We are grateful to its authors and
|
|
11
|
+
contributors. This is not an official release of that project.
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
delimiter does not fit the parser's 1 KiB scanner state is an `ERROR` rather than a guessed body.
|
|
17
|
-
- Redirections stay inside the command they belong to, in source order (`npm > out run x`), and a command may
|
|
18
|
-
consist of assignments and redirections only.
|
|
19
|
-
- `$((…))` is always an arithmetic expansion, including inside heredoc bodies; escaped `\$` and `` \` `` are literal.
|
|
20
|
-
- A newline is a statement terminator only where one may end a statement; elsewhere it is whitespace.
|
|
13
|
+
This fork aims to parse scripts exactly as bash runs them, so that tools can tell code from data in untrusted
|
|
14
|
+
scripts. It fixes parsing bugs and raises conformance with bash, checked by differential tests against bash itself.
|
|
15
|
+
As a result, its syntax trees differ from the original grammar's; review your queries when migrating.
|
|
21
16
|
|
|
22
17
|
## Usage
|
|
23
18
|
|
package/grammar.js
CHANGED
|
@@ -7,8 +7,10 @@
|
|
|
7
7
|
// @ts-check
|
|
8
8
|
|
|
9
9
|
// Characters that end an unquoted word part: metacharacters, quotes, and characters that begin an
|
|
10
|
-
// expansion. `#` only starts a comment at the beginning of a word, so it is handled separately.
|
|
11
|
-
|
|
10
|
+
// expansion. `#` only starts a comment at the beginning of a word, so it is handled separately. Like
|
|
11
|
+
// bash, only a space, a tab, and a newline separate words; any other whitespace (CR, a no-break space)
|
|
12
|
+
// is part of a word.
|
|
13
|
+
const WORD_BREAKS = [' ', '\\t', '\\n', '|', '&', ';', '(', ')', '<', '>', '"', '\'', '`', '$', '\\\\'];
|
|
12
14
|
|
|
13
15
|
// Extglob groups nest (`@(a|+([0-9]))`); a regular expression expresses a fixed depth, and three levels
|
|
14
16
|
// cover every nested pattern in the example corpora.
|
|
@@ -17,6 +19,7 @@ const EXTGLOB_GROUP = extglobGroup('[?*+@]', 3);
|
|
|
17
19
|
const EXTGLOB_NEGATION = extglobGroup('!', 3);
|
|
18
20
|
|
|
19
21
|
// Text inside `${…}` and subscripts stops before `<(`/`>(`, so a process substitution there is parsed.
|
|
22
|
+
const EXPANSION_TEXT_BREAKS = ['}', '$', '`', '"', '\'', '\\\\', '<', '>'];
|
|
20
23
|
const SUBSCRIPT_TEXT = /([^\[\]$`"'\\<>]|[<>]+[^(\[\]$`"'\\<>]|\\.)+/;
|
|
21
24
|
|
|
22
25
|
const PREC = {
|
|
@@ -81,7 +84,7 @@ module.exports = grammar({
|
|
|
81
84
|
extras: $ => [
|
|
82
85
|
$.comment,
|
|
83
86
|
$.heredoc_body,
|
|
84
|
-
|
|
87
|
+
/[ \t\n]/,
|
|
85
88
|
/\\\n/,
|
|
86
89
|
$._line_continuation,
|
|
87
90
|
],
|
|
@@ -550,7 +553,7 @@ module.exports = grammar({
|
|
|
550
553
|
|
|
551
554
|
raw_string: _ => /'[^']*'/,
|
|
552
555
|
|
|
553
|
-
ansi_c_string: _ => /\$'([^'\\]|\\(.|\
|
|
556
|
+
ansi_c_string: _ => /\$'([^'\\]|\\(.|\n))*'/,
|
|
554
557
|
|
|
555
558
|
translated_string: $ => seq('$"', repeat(choice(
|
|
556
559
|
$.string_content,
|
|
@@ -580,10 +583,6 @@ module.exports = grammar({
|
|
|
580
583
|
alias(token.immediate(/[0-9]+|[*@?$_-]/), $.special_variable_name),
|
|
581
584
|
alias(token.immediate(choice('#', '!')), $.special_variable_name),
|
|
582
585
|
alias($._expansion_subscript, $.subscript),
|
|
583
|
-
// A blank directly after `${` opens a function substitution, so these only take effect
|
|
584
|
-
// after a prefix (`${# y}`), which bash parses and rejects only when expanding.
|
|
585
|
-
alias(/[A-Za-z_][A-Za-z0-9_]*/, $.variable_name),
|
|
586
|
-
alias(/[0-9]+|[*@?$_-]/, $.special_variable_name),
|
|
587
586
|
)),
|
|
588
587
|
optional($._expansion_operation),
|
|
589
588
|
),
|
|
@@ -592,6 +591,7 @@ module.exports = grammar({
|
|
|
592
591
|
// `$name` expansion is left out, since its `$` would tie with the special parameter in `${$}`.
|
|
593
592
|
repeat(field('argument', choice(
|
|
594
593
|
alias($._expansion_text, $.word),
|
|
594
|
+
alias($._blank_expansion_text, $.word),
|
|
595
595
|
alias($._angle_text, $.word),
|
|
596
596
|
$.string,
|
|
597
597
|
$.raw_string,
|
|
@@ -619,6 +619,7 @@ module.exports = grammar({
|
|
|
619
619
|
)),
|
|
620
620
|
repeat(field('argument', choice(
|
|
621
621
|
alias($._expansion_text, $.word),
|
|
622
|
+
alias($._blank_expansion_text, $.word),
|
|
622
623
|
alias($._angle_text, $.word),
|
|
623
624
|
$._quoted_or_expansion,
|
|
624
625
|
alias($._bare_dollar, $.word),
|
|
@@ -628,12 +629,21 @@ module.exports = grammar({
|
|
|
628
629
|
// Text without an operator (`${x y}`) is rejected by bash only when expanding.
|
|
629
630
|
repeat1(field('argument', choice(
|
|
630
631
|
alias($._expansion_text, $.word),
|
|
632
|
+
alias($._blank_expansion_text, $.word),
|
|
631
633
|
alias($._angle_text, $.word),
|
|
632
634
|
$._quoted_or_expansion,
|
|
633
635
|
))),
|
|
634
636
|
),
|
|
635
637
|
|
|
636
|
-
_expansion_text: _ => token(prec(-1,
|
|
638
|
+
_expansion_text: _ => token(prec(-1, seq(
|
|
639
|
+
choice(noneOf(...EXPANSION_TEXT_BREAKS, ' ', '\\t', '\\n'), ...expansionTextUnits().slice(1)),
|
|
640
|
+
repeat(choice(...expansionTextUnits())),
|
|
641
|
+
))),
|
|
642
|
+
|
|
643
|
+
// Text led by a blank outranks `}`: otherwise the lexer, having consumed the blank as the start of
|
|
644
|
+
// text, extends it into a `}` token that swallows the blank (`${x:- }`), which a reparse after an
|
|
645
|
+
// 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()))))),
|
|
637
647
|
|
|
638
648
|
// A `<` or `>` that the text above cannot end with, e.g. before `}`. A single character, so that a
|
|
639
649
|
// run before `(` leaves its last `<`/`>` to open a process substitution (`${x:-a<<(b)}`).
|
|
@@ -645,7 +655,7 @@ module.exports = grammar({
|
|
|
645
655
|
seq('$(', $._substitution_start, optional($._statements), $._substitution_end, ')'),
|
|
646
656
|
// bash 5.3 runs `${ list; }` and `${| list; }` in the current shell.
|
|
647
657
|
seq(
|
|
648
|
-
alias(token(/\$\{[\
|
|
658
|
+
alias(token(/\$\{[ \t\n|]/), '${'),
|
|
649
659
|
$._brace_substitution_start,
|
|
650
660
|
optional($._terminated_statements),
|
|
651
661
|
$._brace_substitution_end,
|
|
@@ -758,6 +768,11 @@ function noneOf(...characters) {
|
|
|
758
768
|
return new RegExp(`[^${characters.join('')}]`);
|
|
759
769
|
}
|
|
760
770
|
|
|
771
|
+
// The units of text inside `${…}`: a character, a run of `<`/`>` not before `(`, and an escape.
|
|
772
|
+
function expansionTextUnits() {
|
|
773
|
+
return [noneOf(...EXPANSION_TEXT_BREAKS), seq(/[<>]+/, noneOf('(', ...EXPANSION_TEXT_BREAKS)), /\\(.|\n)/];
|
|
774
|
+
}
|
|
775
|
+
|
|
761
776
|
/**
|
|
762
777
|
* A regular expression matching a literal extglob group opened by `prefix`, whose body may contain
|
|
763
778
|
* groups nested up to `depth` levels.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@willbooster/tree-sitter-bash",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.3",
|
|
4
4
|
"description": "Bash grammar for tree-sitter",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"incremental",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@semantic-release/exec": "7.1.0",
|
|
54
54
|
"@willbooster/wb": "22.8.0",
|
|
55
|
-
"conventional-changelog-conventionalcommits": "
|
|
55
|
+
"conventional-changelog-conventionalcommits": "10.4.0",
|
|
56
56
|
"lefthook": "2.1.14",
|
|
57
57
|
"node-gyp": "13.0.2",
|
|
58
58
|
"prebuildify": "^6.0.1",
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/src/grammar.json
CHANGED
|
@@ -2467,7 +2467,7 @@
|
|
|
2467
2467
|
"members": [
|
|
2468
2468
|
{
|
|
2469
2469
|
"type": "PATTERN",
|
|
2470
|
-
"value": "[
|
|
2470
|
+
"value": "[^ \\t\\n|&;()<>\"'`$\\\\]"
|
|
2471
2471
|
},
|
|
2472
2472
|
{
|
|
2473
2473
|
"type": "PATTERN",
|
|
@@ -2617,7 +2617,7 @@
|
|
|
2617
2617
|
"members": [
|
|
2618
2618
|
{
|
|
2619
2619
|
"type": "PATTERN",
|
|
2620
|
-
"value": "[
|
|
2620
|
+
"value": "[^# \\t\\n|&;()<>\"'`$\\\\]"
|
|
2621
2621
|
},
|
|
2622
2622
|
{
|
|
2623
2623
|
"type": "PATTERN",
|
|
@@ -2636,7 +2636,7 @@
|
|
|
2636
2636
|
"members": [
|
|
2637
2637
|
{
|
|
2638
2638
|
"type": "PATTERN",
|
|
2639
|
-
"value": "[
|
|
2639
|
+
"value": "[^ \\t\\n|&;()<>\"'`$\\\\]"
|
|
2640
2640
|
},
|
|
2641
2641
|
{
|
|
2642
2642
|
"type": "PATTERN",
|
|
@@ -2818,7 +2818,7 @@
|
|
|
2818
2818
|
"members": [
|
|
2819
2819
|
{
|
|
2820
2820
|
"type": "PATTERN",
|
|
2821
|
-
"value": "[
|
|
2821
|
+
"value": "[^ \\t\\n|&;()<>\"'`$\\\\]"
|
|
2822
2822
|
},
|
|
2823
2823
|
{
|
|
2824
2824
|
"type": "PATTERN",
|
|
@@ -2886,7 +2886,7 @@
|
|
|
2886
2886
|
},
|
|
2887
2887
|
"ansi_c_string": {
|
|
2888
2888
|
"type": "PATTERN",
|
|
2889
|
-
"value": "\\$'([^'\\\\]|\\\\(.|\\
|
|
2889
|
+
"value": "\\$'([^'\\\\]|\\\\(.|\\n))*'"
|
|
2890
2890
|
},
|
|
2891
2891
|
"translated_string": {
|
|
2892
2892
|
"type": "SEQ",
|
|
@@ -3072,24 +3072,6 @@
|
|
|
3072
3072
|
},
|
|
3073
3073
|
"named": true,
|
|
3074
3074
|
"value": "subscript"
|
|
3075
|
-
},
|
|
3076
|
-
{
|
|
3077
|
-
"type": "ALIAS",
|
|
3078
|
-
"content": {
|
|
3079
|
-
"type": "PATTERN",
|
|
3080
|
-
"value": "[A-Za-z_][A-Za-z0-9_]*"
|
|
3081
|
-
},
|
|
3082
|
-
"named": true,
|
|
3083
|
-
"value": "variable_name"
|
|
3084
|
-
},
|
|
3085
|
-
{
|
|
3086
|
-
"type": "ALIAS",
|
|
3087
|
-
"content": {
|
|
3088
|
-
"type": "PATTERN",
|
|
3089
|
-
"value": "[0-9]+|[*@?$_-]"
|
|
3090
|
-
},
|
|
3091
|
-
"named": true,
|
|
3092
|
-
"value": "special_variable_name"
|
|
3093
3075
|
}
|
|
3094
3076
|
]
|
|
3095
3077
|
}
|
|
@@ -3150,6 +3132,15 @@
|
|
|
3150
3132
|
"named": true,
|
|
3151
3133
|
"value": "word"
|
|
3152
3134
|
},
|
|
3135
|
+
{
|
|
3136
|
+
"type": "ALIAS",
|
|
3137
|
+
"content": {
|
|
3138
|
+
"type": "SYMBOL",
|
|
3139
|
+
"name": "_blank_expansion_text"
|
|
3140
|
+
},
|
|
3141
|
+
"named": true,
|
|
3142
|
+
"value": "word"
|
|
3143
|
+
},
|
|
3153
3144
|
{
|
|
3154
3145
|
"type": "ALIAS",
|
|
3155
3146
|
"content": {
|
|
@@ -3375,6 +3366,15 @@
|
|
|
3375
3366
|
"named": true,
|
|
3376
3367
|
"value": "word"
|
|
3377
3368
|
},
|
|
3369
|
+
{
|
|
3370
|
+
"type": "ALIAS",
|
|
3371
|
+
"content": {
|
|
3372
|
+
"type": "SYMBOL",
|
|
3373
|
+
"name": "_blank_expansion_text"
|
|
3374
|
+
},
|
|
3375
|
+
"named": true,
|
|
3376
|
+
"value": "word"
|
|
3377
|
+
},
|
|
3378
3378
|
{
|
|
3379
3379
|
"type": "ALIAS",
|
|
3380
3380
|
"content": {
|
|
@@ -3445,6 +3445,15 @@
|
|
|
3445
3445
|
"named": true,
|
|
3446
3446
|
"value": "word"
|
|
3447
3447
|
},
|
|
3448
|
+
{
|
|
3449
|
+
"type": "ALIAS",
|
|
3450
|
+
"content": {
|
|
3451
|
+
"type": "SYMBOL",
|
|
3452
|
+
"name": "_blank_expansion_text"
|
|
3453
|
+
},
|
|
3454
|
+
"named": true,
|
|
3455
|
+
"value": "word"
|
|
3456
|
+
},
|
|
3448
3457
|
{
|
|
3449
3458
|
"type": "ALIAS",
|
|
3450
3459
|
"content": {
|
|
@@ -3470,8 +3479,109 @@
|
|
|
3470
3479
|
"type": "PREC",
|
|
3471
3480
|
"value": -1,
|
|
3472
3481
|
"content": {
|
|
3473
|
-
"type": "
|
|
3474
|
-
"
|
|
3482
|
+
"type": "SEQ",
|
|
3483
|
+
"members": [
|
|
3484
|
+
{
|
|
3485
|
+
"type": "CHOICE",
|
|
3486
|
+
"members": [
|
|
3487
|
+
{
|
|
3488
|
+
"type": "PATTERN",
|
|
3489
|
+
"value": "[^}$`\"'\\\\<> \\t\\n]"
|
|
3490
|
+
},
|
|
3491
|
+
{
|
|
3492
|
+
"type": "SEQ",
|
|
3493
|
+
"members": [
|
|
3494
|
+
{
|
|
3495
|
+
"type": "PATTERN",
|
|
3496
|
+
"value": "[<>]+"
|
|
3497
|
+
},
|
|
3498
|
+
{
|
|
3499
|
+
"type": "PATTERN",
|
|
3500
|
+
"value": "[^(}$`\"'\\\\<>]"
|
|
3501
|
+
}
|
|
3502
|
+
]
|
|
3503
|
+
},
|
|
3504
|
+
{
|
|
3505
|
+
"type": "PATTERN",
|
|
3506
|
+
"value": "\\\\(.|\\n)"
|
|
3507
|
+
}
|
|
3508
|
+
]
|
|
3509
|
+
},
|
|
3510
|
+
{
|
|
3511
|
+
"type": "REPEAT",
|
|
3512
|
+
"content": {
|
|
3513
|
+
"type": "CHOICE",
|
|
3514
|
+
"members": [
|
|
3515
|
+
{
|
|
3516
|
+
"type": "PATTERN",
|
|
3517
|
+
"value": "[^}$`\"'\\\\<>]"
|
|
3518
|
+
},
|
|
3519
|
+
{
|
|
3520
|
+
"type": "SEQ",
|
|
3521
|
+
"members": [
|
|
3522
|
+
{
|
|
3523
|
+
"type": "PATTERN",
|
|
3524
|
+
"value": "[<>]+"
|
|
3525
|
+
},
|
|
3526
|
+
{
|
|
3527
|
+
"type": "PATTERN",
|
|
3528
|
+
"value": "[^(}$`\"'\\\\<>]"
|
|
3529
|
+
}
|
|
3530
|
+
]
|
|
3531
|
+
},
|
|
3532
|
+
{
|
|
3533
|
+
"type": "PATTERN",
|
|
3534
|
+
"value": "\\\\(.|\\n)"
|
|
3535
|
+
}
|
|
3536
|
+
]
|
|
3537
|
+
}
|
|
3538
|
+
}
|
|
3539
|
+
]
|
|
3540
|
+
}
|
|
3541
|
+
}
|
|
3542
|
+
},
|
|
3543
|
+
"_blank_expansion_text": {
|
|
3544
|
+
"type": "TOKEN",
|
|
3545
|
+
"content": {
|
|
3546
|
+
"type": "PREC",
|
|
3547
|
+
"value": 1,
|
|
3548
|
+
"content": {
|
|
3549
|
+
"type": "SEQ",
|
|
3550
|
+
"members": [
|
|
3551
|
+
{
|
|
3552
|
+
"type": "PATTERN",
|
|
3553
|
+
"value": "[ \\t\\n]"
|
|
3554
|
+
},
|
|
3555
|
+
{
|
|
3556
|
+
"type": "REPEAT",
|
|
3557
|
+
"content": {
|
|
3558
|
+
"type": "CHOICE",
|
|
3559
|
+
"members": [
|
|
3560
|
+
{
|
|
3561
|
+
"type": "PATTERN",
|
|
3562
|
+
"value": "[^}$`\"'\\\\<>]"
|
|
3563
|
+
},
|
|
3564
|
+
{
|
|
3565
|
+
"type": "SEQ",
|
|
3566
|
+
"members": [
|
|
3567
|
+
{
|
|
3568
|
+
"type": "PATTERN",
|
|
3569
|
+
"value": "[<>]+"
|
|
3570
|
+
},
|
|
3571
|
+
{
|
|
3572
|
+
"type": "PATTERN",
|
|
3573
|
+
"value": "[^(}$`\"'\\\\<>]"
|
|
3574
|
+
}
|
|
3575
|
+
]
|
|
3576
|
+
},
|
|
3577
|
+
{
|
|
3578
|
+
"type": "PATTERN",
|
|
3579
|
+
"value": "\\\\(.|\\n)"
|
|
3580
|
+
}
|
|
3581
|
+
]
|
|
3582
|
+
}
|
|
3583
|
+
}
|
|
3584
|
+
]
|
|
3475
3585
|
}
|
|
3476
3586
|
}
|
|
3477
3587
|
},
|
|
@@ -3531,7 +3641,7 @@
|
|
|
3531
3641
|
"type": "TOKEN",
|
|
3532
3642
|
"content": {
|
|
3533
3643
|
"type": "PATTERN",
|
|
3534
|
-
"value": "\\$\\{[\\
|
|
3644
|
+
"value": "\\$\\{[ \\t\\n|]"
|
|
3535
3645
|
}
|
|
3536
3646
|
},
|
|
3537
3647
|
"named": false,
|
|
@@ -4601,7 +4711,7 @@
|
|
|
4601
4711
|
},
|
|
4602
4712
|
{
|
|
4603
4713
|
"type": "PATTERN",
|
|
4604
|
-
"value": "\\
|
|
4714
|
+
"value": "[ \\t\\n]"
|
|
4605
4715
|
},
|
|
4606
4716
|
{
|
|
4607
4717
|
"type": "PATTERN",
|
package/src/node-types.json
CHANGED
|
@@ -3093,11 +3093,6 @@
|
|
|
3093
3093
|
}
|
|
3094
3094
|
}
|
|
3095
3095
|
},
|
|
3096
|
-
{
|
|
3097
|
-
"type": "variable_name",
|
|
3098
|
-
"named": true,
|
|
3099
|
-
"fields": {}
|
|
3100
|
-
},
|
|
3101
3096
|
{
|
|
3102
3097
|
"type": "while_statement",
|
|
3103
3098
|
"named": true,
|
|
@@ -3547,6 +3542,10 @@
|
|
|
3547
3542
|
"type": "until",
|
|
3548
3543
|
"named": false
|
|
3549
3544
|
},
|
|
3545
|
+
{
|
|
3546
|
+
"type": "variable_name",
|
|
3547
|
+
"named": true
|
|
3548
|
+
},
|
|
3550
3549
|
{
|
|
3551
3550
|
"type": "while",
|
|
3552
3551
|
"named": false
|