@exercism/codemirror-lang-shell 0.0.0-stage → 1.0.1

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