@willbooster/tree-sitter-bash 1.1.2 → 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 CHANGED
@@ -6,20 +6,13 @@
6
6
  [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
7
7
  [![wbfy](https://img.shields.io/badge/wbfy-20.20.0-1e90ff.svg)](https://github.com/WillBooster/shared/tree/main/packages/wbfy)
8
8
 
9
- Bash grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter), rewritten from
10
- [tree-sitter/tree-sitter-bash](https://github.com/tree-sitter/tree-sitter-bash). The syntax trees differ from the
11
- original grammar's.
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
- - Heredoc bodies are parsed wherever bash reads them: from the newline that ends the line holding the `<<` operator,
14
- in operator order. Any statement may follow the operator on that line (`cat <<EOF; echo done`), and several heredocs
15
- may share a line. A `heredoc_body` node appears as an extra next to the command that opened it. A heredoc whose
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.
21
- - Like bash, only a space, a tab, and a newline separate words. Any other whitespace is part of a word, including
22
- the CR of a CRLF line ending, so a CRLF script parses as bash runs it (`echo a` + CRLF passes `a` + CR).
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.
23
16
 
24
17
  ## Usage
25
18
 
package/grammar.js CHANGED
@@ -19,6 +19,7 @@ const EXTGLOB_GROUP = extglobGroup('[?*+@]', 3);
19
19
  const EXTGLOB_NEGATION = extglobGroup('!', 3);
20
20
 
21
21
  // Text inside `${…}` and subscripts stops before `<(`/`>(`, so a process substitution there is parsed.
22
+ const EXPANSION_TEXT_BREAKS = ['}', '$', '`', '"', '\'', '\\\\', '<', '>'];
22
23
  const SUBSCRIPT_TEXT = /([^\[\]$`"'\\<>]|[<>]+[^(\[\]$`"'\\<>]|\\.)+/;
23
24
 
24
25
  const PREC = {
@@ -582,10 +583,6 @@ module.exports = grammar({
582
583
  alias(token.immediate(/[0-9]+|[*@?$_-]/), $.special_variable_name),
583
584
  alias(token.immediate(choice('#', '!')), $.special_variable_name),
584
585
  alias($._expansion_subscript, $.subscript),
585
- // A blank directly after `${` opens a function substitution, so these only take effect
586
- // after a prefix (`${# y}`), which bash parses and rejects only when expanding.
587
- alias(/[A-Za-z_][A-Za-z0-9_]*/, $.variable_name),
588
- alias(/[0-9]+|[*@?$_-]/, $.special_variable_name),
589
586
  )),
590
587
  optional($._expansion_operation),
591
588
  ),
@@ -594,6 +591,7 @@ module.exports = grammar({
594
591
  // `$name` expansion is left out, since its `$` would tie with the special parameter in `${$}`.
595
592
  repeat(field('argument', choice(
596
593
  alias($._expansion_text, $.word),
594
+ alias($._blank_expansion_text, $.word),
597
595
  alias($._angle_text, $.word),
598
596
  $.string,
599
597
  $.raw_string,
@@ -621,6 +619,7 @@ module.exports = grammar({
621
619
  )),
622
620
  repeat(field('argument', choice(
623
621
  alias($._expansion_text, $.word),
622
+ alias($._blank_expansion_text, $.word),
624
623
  alias($._angle_text, $.word),
625
624
  $._quoted_or_expansion,
626
625
  alias($._bare_dollar, $.word),
@@ -630,12 +629,21 @@ module.exports = grammar({
630
629
  // Text without an operator (`${x y}`) is rejected by bash only when expanding.
631
630
  repeat1(field('argument', choice(
632
631
  alias($._expansion_text, $.word),
632
+ alias($._blank_expansion_text, $.word),
633
633
  alias($._angle_text, $.word),
634
634
  $._quoted_or_expansion,
635
635
  ))),
636
636
  ),
637
637
 
638
- _expansion_text: _ => token(prec(-1, /([^}$`"'\\<>]|[<>]+[^(}$`"'\\<>]|\\(.|\n))+/)),
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()))))),
639
647
 
640
648
  // A `<` or `>` that the text above cannot end with, e.g. before `}`. A single character, so that a
641
649
  // run before `(` leaves its last `<`/`>` to open a process substitution (`${x:-a<<(b)}`).
@@ -760,6 +768,11 @@ function noneOf(...characters) {
760
768
  return new RegExp(`[^${characters.join('')}]`);
761
769
  }
762
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
+
763
776
  /**
764
777
  * A regular expression matching a literal extglob group opened by `prefix`, whose body may contain
765
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.2",
3
+ "version": "1.1.3",
4
4
  "description": "Bash grammar for tree-sitter",
5
5
  "keywords": [
6
6
  "incremental",
package/src/grammar.json CHANGED
@@ -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": "PATTERN",
3474
- "value": "([^}$`\"'\\\\<>]|[<>]+[^(}$`\"'\\\\<>]|\\\\(.|\\n))+"
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
  },
@@ -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