@exercism/codemirror-lang-shell 0.0.0-stage → 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/CHANGELOG.md +161 -0
- package/LICENSE +21 -0
- package/README.md +285 -2
- package/dist/index.cjs +2188 -0
- package/dist/index.d.cts +297 -0
- package/dist/index.d.ts +297 -0
- package/dist/index.js +2170 -0
- package/package.json +60 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
## 1.0.0
|
|
2
|
+
|
|
3
|
+
This is Exercism's first release of the package, published as
|
|
4
|
+
`@exercism/codemirror-lang-shell`. It adds completion of the names declared in
|
|
5
|
+
the file, snippets and more builtins, parses more of the Bash in Exercism's
|
|
6
|
+
solutions, and exports `bash()` as an alias for `shell()`.
|
|
7
|
+
|
|
8
|
+
### New features
|
|
9
|
+
|
|
10
|
+
Completion offers the names declared in the file, each only where it is in
|
|
11
|
+
scope: functions, variables, loop variables, the targets of `read` and
|
|
12
|
+
`mapfile`, and the names after `local` and `declare`. After `$` and `${` it
|
|
13
|
+
offers variables and the positional parameters.
|
|
14
|
+
|
|
15
|
+
Snippets for functions, `if` with `else` and `elif`, both `for` loops,
|
|
16
|
+
`while`, `until`, `case`, `[[ ]]`, `(( ))`, `$(( ))` and `$( )`.
|
|
17
|
+
|
|
18
|
+
The names after `local`, `declare`, `typeset`, `readonly` and `export` are
|
|
19
|
+
definitions in the tree.
|
|
20
|
+
|
|
21
|
+
An `if` folds from its `then`.
|
|
22
|
+
|
|
23
|
+
`bash()` is `shell()` under the shell's usual name.
|
|
24
|
+
|
|
25
|
+
### Bug fixes
|
|
26
|
+
|
|
27
|
+
A quoted piece inside a `=~` pattern keeps its spaces: `[[ $s =~ [$' \t'] ]]`.
|
|
28
|
+
|
|
29
|
+
A closing word may follow a compound command with no `;`: `then { exit; } fi`.
|
|
30
|
+
|
|
31
|
+
Pieces that touch inside arithmetic are one operand, `$((0x$hex))`, and an
|
|
32
|
+
operator may be held in a variable, `$((a $op b))`.
|
|
33
|
+
|
|
34
|
+
An extended pattern may start in the middle of a word: `?(.+([0-9]))`.
|
|
35
|
+
|
|
36
|
+
## 0.6.0 (2026-08-27)
|
|
37
|
+
|
|
38
|
+
### New features
|
|
39
|
+
|
|
40
|
+
zsh lets the body of an `if`, an `elif`, an `else` or a `do` be empty, where
|
|
41
|
+
every other shell wants a command in it. A branch commented out rather than
|
|
42
|
+
deleted is what that looks like in a file people keep, and the grammar read the
|
|
43
|
+
`fi` after one as a command and then took the rest of the file for the body.
|
|
44
|
+
This was the largest single thing wrong with the parse: ninety files failed on
|
|
45
|
+
it and each of them failed at its last line.
|
|
46
|
+
|
|
47
|
+
zsh asks an array a question with a subscript flag, `$widgets[(I)$name]`, and
|
|
48
|
+
arithmetic now reads one. A `$name` inside `(( ))` may be subscripted at all,
|
|
49
|
+
which it could not be before; only a bare name and `$+name` could.
|
|
50
|
+
|
|
51
|
+
### Bug fixes
|
|
52
|
+
|
|
53
|
+
A case pattern may be broken across lines. `--a=*|\` and the next line's
|
|
54
|
+
`--b=*)` are one pattern of two words, and the backslash was being read as a
|
|
55
|
+
piece of the word after the `|` rather than as the space it is. A backslash
|
|
56
|
+
joins two lines wherever what precedes it does not already end a word.
|
|
57
|
+
|
|
58
|
+
The pattern after `=~` keeps a space written `\ `, and every space inside a
|
|
59
|
+
group. bash reads a parenthesis there as the regular expression's own, so
|
|
60
|
+
`[[ $l =~ ^(.*\ -F)\ *([^ ]*)$ ]]` is one pattern rather than four words.
|
|
61
|
+
|
|
62
|
+
### The corpus
|
|
63
|
+
|
|
64
|
+
`tools/shell-oracle.sh` is the shells themselves used as an oracle. A `.sh`, a
|
|
65
|
+
`.bash` and a `.zsh` are three languages sharing an extension, so a file is
|
|
66
|
+
turned down only where `bash -n`, `zsh -n` and `sh -n` all refuse it. That is
|
|
67
|
+
272 of 19626, and what goes is not a shell disagreeing with the grammar: it is
|
|
68
|
+
the self-extracting installer with a C program after its last line, the
|
|
69
|
+
terminal session saved with the prompt still in it, the template with `{{ }}`
|
|
70
|
+
where a word belongs.
|
|
71
|
+
|
|
72
|
+
19626 published scripts read 95.35% clean before any of this. The 19354 the
|
|
73
|
+
shells will parse read 97.16% after it.
|
|
74
|
+
|
|
75
|
+
## 0.3.0 (2026-08-13)
|
|
76
|
+
|
|
77
|
+
### New features
|
|
78
|
+
|
|
79
|
+
More of zsh, measured against 6697 shell scripts published by their authors
|
|
80
|
+
rather than the corpus of small task scripts the earlier releases were written on. That
|
|
81
|
+
corpus went from 93.98% to 96.86% clean.
|
|
82
|
+
|
|
83
|
+
`(( $+commands[git] ))` is how a script says it has a program, and it was the
|
|
84
|
+
largest single thing this grammar could not read. `&|` backgrounds a command and
|
|
85
|
+
disowns it, and `>!` writes past `noclobber` on any of the writing forms, so
|
|
86
|
+
`&>>!` is an operator too.
|
|
87
|
+
|
|
88
|
+
A function may have no name. `() { ... } "$@"` defines one and runs it where it
|
|
89
|
+
stands, which is how a plugin keeps its variables to itself.
|
|
90
|
+
|
|
91
|
+
An expansion may apply a flag to a value it does not name: `${(%):-%N}` asks the
|
|
92
|
+
prompt machinery for the name of the running script, and `${(f)"$(git remote)"}`
|
|
93
|
+
splits what a command printed into lines. `${~var}` reads what comes out as a
|
|
94
|
+
pattern, and a glob qualifier may be written as the flag `(#qN)`.
|
|
95
|
+
|
|
96
|
+
`${x@Q}` and `${!prefix@}` are bash's, and are read now as well.
|
|
97
|
+
|
|
98
|
+
## 0.2.2 (2026-08-06)
|
|
99
|
+
|
|
100
|
+
The repository link, which npm renders as a link and a browser cannot follow
|
|
101
|
+
with `git+` in front of it.
|
|
102
|
+
|
|
103
|
+
## 0.2.1 (2026-07-29)
|
|
104
|
+
|
|
105
|
+
A line in the README saying what this was written for, and a link to the
|
|
106
|
+
platform it was written for. Nothing in the code changed.
|
|
107
|
+
|
|
108
|
+
## 0.2.0 (2026-07-27)
|
|
109
|
+
|
|
110
|
+
### New features
|
|
111
|
+
|
|
112
|
+
`shell()` takes the name of a shell. `"sh"`, `"bash"`, `"zsh"` and `"ksh"` are
|
|
113
|
+
all the one grammar; `"csh"`, `"tcsh"` and `"fish"` each have their own. `csh()`
|
|
114
|
+
and `fish()` are the same thing said shorter.
|
|
115
|
+
|
|
116
|
+
The shell grammar reads zsh: the expansion flags in `${(kv@)h}`, an expansion
|
|
117
|
+
whose name is another expansion, `$#array`, several names in one `for`, a list
|
|
118
|
+
in parentheses, the short forms that drop the `do` and the `then`, and the
|
|
119
|
+
qualifier in `*.txt(D)`. There is no dialect switch, the way this package's AWK
|
|
120
|
+
grammar reads gawk.
|
|
121
|
+
|
|
122
|
+
It reads ksh93's compound types too. Which of an array and a compound value a
|
|
123
|
+
`=(` opens is decided by looking for a `;` inside it, since that is a syntax
|
|
124
|
+
error in one and the punctuation between two commands in the other.
|
|
125
|
+
|
|
126
|
+
The C shell has a grammar of its own, `cshLanguage`, with the expression
|
|
127
|
+
language `if`, `while` and `@` take, the switch and foreach statements, `set`
|
|
128
|
+
and `setenv`, and the modifiers a variable is hung with. It is not a dialect of
|
|
129
|
+
the shell command language and is not read as one.
|
|
130
|
+
|
|
131
|
+
fish has a grammar of its own too, `fishLanguage`. It is the shortest of the
|
|
132
|
+
three: fish threw out word splitting, heredocs, `${}`, backticks and the
|
|
133
|
+
subshell, and gave every block the same `end`. What it added is a `[` that
|
|
134
|
+
opens a subscript where it touches a word and names the test command where it
|
|
135
|
+
does not.
|
|
136
|
+
|
|
137
|
+
The corpus of 754 small task scripts now parses 93.50% clean, up from
|
|
138
|
+
90.58%. 12 of its 13 csh files parse clean under the csh grammar, and all 21
|
|
139
|
+
fish files under the fish one.
|
|
140
|
+
|
|
141
|
+
## 0.1.0 (2026-07-27)
|
|
142
|
+
|
|
143
|
+
### First release
|
|
144
|
+
|
|
145
|
+
Shell language support: a Lezer grammar covering the POSIX shell command
|
|
146
|
+
language with the bash extensions, plus highlighting, folding, indentation and
|
|
147
|
+
completion.
|
|
148
|
+
|
|
149
|
+
CodeMirror has a shell mode in `@codemirror/legacy-modes`, and it is a stream
|
|
150
|
+
tokenizer with a word list. It has no tree, so an editor using it gets no
|
|
151
|
+
folding, no indentation and no structural selection. It also reads `echo a=b`
|
|
152
|
+
as an assignment, and the `done` in `while true; do echo done; done` as the end
|
|
153
|
+
of the loop rather than as the word the loop prints.
|
|
154
|
+
|
|
155
|
+
A backtick substitution is read as one token, because the character that opens
|
|
156
|
+
it is the character that closes it; the commands inside are parsed by handing
|
|
157
|
+
that range back to the same parser.
|
|
158
|
+
|
|
159
|
+
The grammar was developed against 754 small shell task scripts from a public code-sample site, of which
|
|
160
|
+
90.58% parse with no error node. Most of the rest are csh, zsh, `rc`, ksh93
|
|
161
|
+
compound types, or terminal sessions saved with the prompt still in them.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 CodinCod
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,286 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @exercism/codemirror-lang-shell
|
|
2
2
|
|
|
3
|
-
This
|
|
3
|
+
This is Exercism's fork of
|
|
4
|
+
[@codincod/codemirror-lang-shell](https://codeberg.org/reeven/codemirror-lang-shell),
|
|
5
|
+
written by CodinCod. Exercism uses it for Bash in the editor on
|
|
6
|
+
[exercism.org](https://exercism.org). Our changes are on the `main` branch.
|
|
7
|
+
|
|
8
|
+
The fork adds completion for the names declared in the file, snippets, and a
|
|
9
|
+
longer list of builtins and common commands. It reads the names after `local`,
|
|
10
|
+
`declare` and `export` as definitions, folds an `if` from its `then`, and parses
|
|
11
|
+
a few more forms found in Exercism's Bash solutions. It also exports `bash()`,
|
|
12
|
+
an alias for `shell()`.
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
npm install @exercism/codemirror-lang-shell
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Exercism's Bash editor calls `bash()`:
|
|
21
|
+
|
|
22
|
+
```javascript
|
|
23
|
+
import {EditorView, basicSetup} from "codemirror"
|
|
24
|
+
import {bash} from "@exercism/codemirror-lang-shell"
|
|
25
|
+
|
|
26
|
+
new EditorView({
|
|
27
|
+
parent: document.body,
|
|
28
|
+
doc: `echo "Hello, World!"\n`,
|
|
29
|
+
extensions: [basicSetup, bash()]
|
|
30
|
+
})
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
The rest of this README is upstream's.
|
|
34
|
+
|
|
35
|
+
## @codincod/codemirror-lang-shell
|
|
36
|
+
|
|
37
|
+
[ [**CHANGELOG**](https://codeberg.org/reeven/codemirror-lang-shell/src/branch/master/CHANGELOG.md) ]
|
|
38
|
+
|
|
39
|
+
This package implements shell language support for the
|
|
40
|
+
[CodeMirror](https://codemirror.net/) code editor, using a
|
|
41
|
+
[Lezer](https://lezer.codemirror.net/) grammar written for this package.
|
|
42
|
+
|
|
43
|
+
CodeMirror ships a shell mode in `@codemirror/legacy-modes`. It colours
|
|
44
|
+
keywords, strings and comments from a word list and a small state machine, and
|
|
45
|
+
it produces no tree, so nothing above the token level is available: no folding,
|
|
46
|
+
no indentation, no structural selection. It also has to guess, and guesses
|
|
47
|
+
wrong in both directions. `echo a=b` comes out as an assignment. The `done` in
|
|
48
|
+
`while true; do echo done; done` comes out as a keyword, when it is the word
|
|
49
|
+
the loop prints.
|
|
50
|
+
|
|
51
|
+
Written in part for [CodinCod](https://codincod.com/), a competitive coding
|
|
52
|
+
platform, where it colours the editor people solve puzzles in.
|
|
53
|
+
|
|
54
|
+
This code is released under an
|
|
55
|
+
[MIT license](https://codeberg.org/reeven/codemirror-lang-shell/src/branch/master/LICENSE).
|
|
56
|
+
|
|
57
|
+
## Usage
|
|
58
|
+
|
|
59
|
+
```javascript
|
|
60
|
+
import {EditorView, basicSetup} from "codemirror"
|
|
61
|
+
import {shell} from "@exercism/codemirror-lang-shell"
|
|
62
|
+
|
|
63
|
+
const view = new EditorView({
|
|
64
|
+
parent: document.body,
|
|
65
|
+
doc: `#!/bin/sh
|
|
66
|
+
for f in *.txt; do
|
|
67
|
+
echo "$(wc -l < "$f") $f"
|
|
68
|
+
done
|
|
69
|
+
`,
|
|
70
|
+
extensions: [basicSetup, shell()]
|
|
71
|
+
})
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
`shell()` on its own reads the shell command language, which is what `sh`,
|
|
75
|
+
`bash`, `zsh` and `ksh` all are. The two shells that are not take a name:
|
|
76
|
+
|
|
77
|
+
```javascript
|
|
78
|
+
shell({shell: "fish"}) // or "csh", or "tcsh"
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
`csh()` and `fish()` are the same thing said shorter, and `shellLanguageFor`
|
|
82
|
+
answers with the `LRLanguage` for a name if you are assembling the extensions
|
|
83
|
+
yourself.
|
|
84
|
+
|
|
85
|
+
## Coverage
|
|
86
|
+
|
|
87
|
+
The shell command language of IEEE 1003.1, with the bash extensions that are
|
|
88
|
+
everywhere in practice: `[[ ]]`, `(( ))`, `$(( ))`, arrays and associative
|
|
89
|
+
arrays, process substitution, here-strings, `&>`, `+=`, the `${x/a/b}` family
|
|
90
|
+
of expansions, and the extended patterns `@(a|b)` that ksh has always had and
|
|
91
|
+
bash has under `shopt -s extglob`.
|
|
92
|
+
|
|
93
|
+
zsh is read by the same grammar rather than by a switch, the way this package's
|
|
94
|
+
AWK grammar reads gawk: the expansion flags in `${(kv@)h}`, an expansion whose
|
|
95
|
+
name is another expansion, `$#array`, several names in one `for`, a list in
|
|
96
|
+
parentheses, the short forms that drop the `do` and the `then`, and the
|
|
97
|
+
qualifier in `*.txt(D)`. Somebody reading a shell script does not always know
|
|
98
|
+
which shell wrote it, and reading more than one shell writes costs an editor
|
|
99
|
+
nothing.
|
|
100
|
+
|
|
101
|
+
ksh93's compound types are read too. `typeset -T Point_t=( ... )` puts commands
|
|
102
|
+
where an array puts words, and which of the two a `=(` opens is decided by
|
|
103
|
+
looking for a `;` inside it, since that is a syntax error in an array and the
|
|
104
|
+
punctuation between two commands everywhere else. A field reached through a
|
|
105
|
+
subscript, `${people[i].name}`, comes with them.
|
|
106
|
+
|
|
107
|
+
Measured against 19354 scripts published on GitHub, one per repository, sifted
|
|
108
|
+
for the Python, Perl and Ruby that a `.sh` extension also names and then for
|
|
109
|
+
what no shell will parse, 97.16% parse with no error node.
|
|
110
|
+
|
|
111
|
+
Against 754 small task scripts from a public code-sample site the figure is 93.50%, and every
|
|
112
|
+
zsh-labelled file among them is clean. Of the 49 that are not, most are not
|
|
113
|
+
this shell: 13 are csh and 5 are `rc`. The rest are terminal sessions with the
|
|
114
|
+
prompt still in them, program output saved as source, and files the wiki
|
|
115
|
+
mangled, where a tab under a `<<-` became spaces or a quote became a
|
|
116
|
+
typographic one.
|
|
117
|
+
|
|
118
|
+
### The C shell
|
|
119
|
+
|
|
120
|
+
csh is not a dialect of the shell command language and does not share a grammar
|
|
121
|
+
with it here. It shares the word layer, the quoting, the pipelines and the
|
|
122
|
+
redirections, and replaces everything above them: `if` takes an expression
|
|
123
|
+
rather than a list of commands, arithmetic is a command of its own spelled `@`,
|
|
124
|
+
a loop ends with `end` rather than `done`, and there are no functions at all.
|
|
125
|
+
So it has a grammar of its own in this package, `csh.grammar`, built from the
|
|
126
|
+
same three questions and answering to a name of its own.
|
|
127
|
+
|
|
128
|
+
What it adds is an expression language, and it is the C operator table rather
|
|
129
|
+
than anything the shell has: `==` and `!=` compare, `=~` matches a pattern,
|
|
130
|
+
`-e` and its relatives ask about a file, and `{ cmd }` stands for the status a
|
|
131
|
+
command exits with. The precedence is C's, so `$n % 15 == 0` is the remainder
|
|
132
|
+
compared against nothing else.
|
|
133
|
+
|
|
134
|
+
Two of its questions have no counterpart on the sh side. A number is a number
|
|
135
|
+
only inside an expression, because `echo 1` passes a word and csh has no
|
|
136
|
+
numbers anywhere else. And `@ n++` ends the name before the `++` where `echo
|
|
137
|
+
a++` does not, which cannot be asked of the parser, since the word the operator
|
|
138
|
+
would follow has not been reduced yet; it is asked of the text instead, which
|
|
139
|
+
says the same thing.
|
|
140
|
+
|
|
141
|
+
Of the 13 csh files in the corpus, 12 parse with no error node. The one that
|
|
142
|
+
does not is an `alias` whose body is a whole csh program inside a raw string,
|
|
143
|
+
escaped a character at a time.
|
|
144
|
+
|
|
145
|
+
### fish
|
|
146
|
+
|
|
147
|
+
fish gets a grammar of its own as well, and it is the shortest of the three,
|
|
148
|
+
because fish threw out most of what makes a shell hard to parse. There is no
|
|
149
|
+
word splitting. There are no heredocs. There is no `${}`, no `$(())`, no
|
|
150
|
+
backticks and no subshell: a parenthesis is a command whose output stands in
|
|
151
|
+
for it, everywhere, and that is the only thing it is. Every block ends with
|
|
152
|
+
`end`.
|
|
153
|
+
|
|
154
|
+
It has one question of its own. A `[` opens a subscript where it touches the
|
|
155
|
+
word in front of it and is the name of the test command where it does not:
|
|
156
|
+
`$argv[1]` against `[ -f $file ]`. Both readings are open at once, so the
|
|
157
|
+
tokenizer settles it and hands the parser one or the other, never both. A
|
|
158
|
+
closing parenthesis is the exception it makes, since `(seq 10)[-1]` indexes
|
|
159
|
+
what the command printed.
|
|
160
|
+
|
|
161
|
+
`else if` is one line and an `else` on its own is another, so the line ending
|
|
162
|
+
tells a continued conditional from a nested one. Neither of the other two
|
|
163
|
+
grammars here can be read that way, and fish can because it has no `then`.
|
|
164
|
+
|
|
165
|
+
Every fish file in the corpus parses with no error node, though there are only
|
|
166
|
+
21 of them; that site files fish under two headings and one of them is a
|
|
167
|
+
different language with the same name.
|
|
168
|
+
|
|
169
|
+
## What a tokenizer has to settle
|
|
170
|
+
|
|
171
|
+
Shell is not a language of expressions with a few strings in it. It is a
|
|
172
|
+
language of words, and nearly every hard question here is about where a word
|
|
173
|
+
begins and ends. Seven of them are settled by asking the parser what it has
|
|
174
|
+
room for.
|
|
175
|
+
|
|
176
|
+
**A word is a run of pieces that touch.** `foo"bar"$baz` is one argument and
|
|
177
|
+
`foo "bar" $baz` is three, so the pieces of a word are parsed with nothing
|
|
178
|
+
skipped between them. What closes a word is a token of no width, produced where
|
|
179
|
+
the shell would end one: at whitespace, or at one of `; & | ( ) < >`. The
|
|
180
|
+
comment rule falls out of that and needs no code of its own. Nothing is skipped
|
|
181
|
+
inside a word, so `a#b` is one word and `a #b` is a word and a comment.
|
|
182
|
+
|
|
183
|
+
**A reserved word is only reserved where a command may start.** `if` opens a
|
|
184
|
+
conditional and `echo if` prints two letters, and the difference is nothing but
|
|
185
|
+
position. A command cannot start until the one before it has been terminated,
|
|
186
|
+
so the parser has room for `done` after a `;` and not after `echo`, which is
|
|
187
|
+
the whole rule. The same question settles `{`, `}`, `[[`, `]]` and `!`, each of
|
|
188
|
+
which a word would otherwise swallow.
|
|
189
|
+
|
|
190
|
+
**An assignment is a word in the right place.** `x=1` sets a variable and
|
|
191
|
+
`echo x=1` prints one, and again only position tells them apart. The value has
|
|
192
|
+
to touch the `=`, because `x= cmd` runs `cmd` with `x` set to nothing.
|
|
193
|
+
|
|
194
|
+
**A newline ends a command where a command can end,** and is skipped everywhere
|
|
195
|
+
else. That one rule covers every place the shell allows a line to be broken
|
|
196
|
+
without a marker: after `|`, `&&`, `do`, `then`, `else`, `{`, `(` or a comma in
|
|
197
|
+
an array.
|
|
198
|
+
|
|
199
|
+
**A heredoc's body is nowhere near the `<<` that promised it.** `<<EOF` says the
|
|
200
|
+
body starts on the next line, and one line may promise several. Rather than
|
|
201
|
+
carry the promise from token to token, the body is read at the newline by
|
|
202
|
+
looking back over the line that just ended, and is then skipped like
|
|
203
|
+
whitespace.
|
|
204
|
+
|
|
205
|
+
**A backslash before a newline is space between words and a letter inside one.**
|
|
206
|
+
`grep foo \` continued on the next line passes two arguments, and `echo ab\`
|
|
207
|
+
continued on the next passes one, and the only difference is the space before
|
|
208
|
+
the backslash. A skipped token cannot also be shifted, so the second case is
|
|
209
|
+
produced where a word is already under way.
|
|
210
|
+
|
|
211
|
+
**A parenthesis after a glob narrows it, in zsh.** `*.txt(D)` matches the hidden
|
|
212
|
+
files as well. A parenthesis ends a word everywhere else, so this is asked
|
|
213
|
+
first, and only where a word has already begun; `(cd /tmp)` at the start of one
|
|
214
|
+
is still a subshell.
|
|
215
|
+
|
|
216
|
+
Two more are settled by counting rather than by asking.
|
|
217
|
+
|
|
218
|
+
**Three parentheses in a row are ambiguous,** and how they close says which was
|
|
219
|
+
meant. `((( n % 2 == 0 )) && echo even)` is a subshell holding an arithmetic
|
|
220
|
+
command; `(((x += 2) <= 8))` is arithmetic holding a parenthesised sum. The
|
|
221
|
+
shell decides this by parsing the arithmetic and taking the other reading when
|
|
222
|
+
that fails, which a parser with no backtracking cannot do. Counting the parens
|
|
223
|
+
gets the same answer.
|
|
224
|
+
|
|
225
|
+
**A closing brace needs no semicolon after a compound command.** `f() { if x;
|
|
226
|
+
then y; fi }` is accepted by bash and `f() { echo hi }` is not, because `}` is a
|
|
227
|
+
reserved word where a command may start and a plain word where an argument may.
|
|
228
|
+
What the line ended with decides it, which is the question bash asks of the
|
|
229
|
+
token it read last.
|
|
230
|
+
|
|
231
|
+
One is settled by parsing the same text twice.
|
|
232
|
+
|
|
233
|
+
**A backtick substitution is closed by the character that opens it,** so a
|
|
234
|
+
grammar reading left to right cannot tell which of the two it has. It is one
|
|
235
|
+
token, and the commands inside it are parsed afterwards, by handing the range
|
|
236
|
+
between the backticks back to the same parser. Across the corpus that is 197
|
|
237
|
+
backticks and 2 that hold something this grammar cannot read, both of them Tcl
|
|
238
|
+
filed under the wrong heading.
|
|
239
|
+
|
|
240
|
+
## What the tree does not say
|
|
241
|
+
|
|
242
|
+
Three things are deliberately shallower than the language reference.
|
|
243
|
+
|
|
244
|
+
**A bracket expression is text.** `*` and `?` are marked as the wildcards they
|
|
245
|
+
are, and `[a-z]` is left alone, because `[` is also the name of a command and
|
|
246
|
+
`[ -f x ]` is a great deal more common than a glob.
|
|
247
|
+
|
|
248
|
+
**Arithmetic and words are separate languages.** Inside `$(( ))` a bare name is
|
|
249
|
+
a variable and `2` is a number. Outside it, `echo 42` passes a word, because the
|
|
250
|
+
shell has no numbers anywhere else and colouring one there would be a lie.
|
|
251
|
+
|
|
252
|
+
**A `${...}` keeps its operator and loses the shape of what follows.**
|
|
253
|
+
`${x:-default}` is an expansion with an operator in it; the default itself is
|
|
254
|
+
read as the text and expansions it is made of, without being told apart from a
|
|
255
|
+
pattern or a replacement.
|
|
256
|
+
|
|
257
|
+
## What it gives an editor
|
|
258
|
+
|
|
259
|
+
Syntax highlighting, folding for groups, subshells, loop bodies, `case`
|
|
260
|
+
statements and command substitutions, indentation that knows `fi`, `done`,
|
|
261
|
+
`esac`, `}` and `;;` close what came before them, and completion for the
|
|
262
|
+
reserved words, the shell builtins and the variables the shell sets.
|
|
263
|
+
|
|
264
|
+
In this fork, completion also offers snippets, common commands, and the names
|
|
265
|
+
declared in the file. Functions are offered everywhere. A variable set with
|
|
266
|
+
`local` or `declare` inside a function is offered only inside that function,
|
|
267
|
+
and any other variable everywhere. After `$` and `${` only variables and the
|
|
268
|
+
positional parameters are offered, and inside `(( ))` and `$(( ))` only
|
|
269
|
+
variables. Nothing is offered inside comments or single quotes.
|
|
270
|
+
`localCompletionSource`, `shellKeywordCompletion` and `snippets` are exported
|
|
271
|
+
for use on their own.
|
|
272
|
+
|
|
273
|
+
`shellLanguage` is exported for use with `LanguageSupport`. `parser` is the
|
|
274
|
+
grammar on its own, without the highlighting props and without the re-entry
|
|
275
|
+
into backticks; `shellLanguage.parser` is the one an editor runs.
|
|
276
|
+
|
|
277
|
+
`cshLanguage`, `cshCompletion` and `cshParser` are the same three things for
|
|
278
|
+
the C shell, and `fishLanguage`, `fishCompletion` and `fishParser` for fish.
|
|
279
|
+
|
|
280
|
+
## Building and testing
|
|
281
|
+
|
|
282
|
+
```
|
|
283
|
+
npm install # installs the dependencies and builds the package into dist/
|
|
284
|
+
npm run build # regenerates the three parsers, then rebuilds the package
|
|
285
|
+
npm test # builds the package, then runs the tests
|
|
286
|
+
```
|