@exercism/codemirror-lang-red 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,38 @@
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-red`. It adds `Function` and `Loop` nodes for
9
+ functions and loops, highlighting for Red's builtins, and completion through
10
+ `red()`.
11
+
12
+ ## 0.1.0 (2026-08-10)
13
+
14
+ First release. A Lezer grammar for Rebol and Red, with highlighting, folding
15
+ and indentation.
16
+
17
+ The whole of the language is in `src/tokens.ts`, because the whole of the
18
+ language is the lexer. A value's datatype is written into its spelling and
19
+ nowhere else, and no two of those spellings can be told apart from the left, so
20
+ a run is read to the next delimiter and classified afterwards. The regular
21
+ expressions that classify it were checked against Oldes' Sublime syntax for
22
+ each language, which is what GitHub colours these two with, and against
23
+ Pygments' `rebol.py`. Where those two disagreed, the corpus settled it.
24
+
25
+ Three decisions the grammar cannot make are settled there too: whether a `<`
26
+ opens a tag or spells a word, where a braced string that holds braces ends, and
27
+ whether `#!` is a shebang or the issue `#!date!` looks like.
28
+
29
+ Red is the same grammar under a dialect flag, which recognises its preprocessor
30
+ and its hex integers. Red/System needs nothing further; a `.reds` file is the
31
+ same syntax with other words in it.
32
+
33
+ Measured against 969 files of real code: Red's compiler and runtime, Rebol 3 as
34
+ Oldes maintains it, and Rebol 2's mezzanine. 99.90% of them parse with no error
35
+ node, and the one that does not is missing a bracket upstream. What that figure
36
+ is worth is in the README, along with the one that matters more, which is that
37
+ 100.00% of the non-whitespace characters come back coloured across twelve
38
+ colours.
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,198 @@
1
- # Temporary Holding Version
1
+ # @exercism/codemirror-lang-red
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
+ This package implements [Rebol](http://www.rebol.com/) and
4
+ [Red](https://www.red-lang.org/) language support for the
5
+ [CodeMirror](https://codemirror.net/) code editor: a
6
+ [Lezer](https://lezer.codemirror.net/) grammar, highlighting, folding and
7
+ indentation. Exercism uses it in the Red editor on
8
+ [exercism.org](https://exercism.org).
9
+
10
+ Two languages, one lexer. Red is a second implementation of Rebol's syntax with
11
+ a preprocessor of its own, and Red/System, which a `.reds` file is written in,
12
+ is that syntax again with other words in it. All three read here, and the
13
+ dialect decides only whether `#include` is a directive or the issue it is
14
+ spelled like.
15
+
16
+ No grammar for either existed in the Lezer generation, and none in the
17
+ CodeMirror 5 modes either. There is no tree-sitter grammar for them. What did
18
+ exist, and was worth reading first, is
19
+ [Oldes' Sublime syntax for Rebol](https://github.com/Oldes/Sublime-REBOL) and
20
+ [the one for Red](https://github.com/Oldes/Sublime-Red), which is what GitHub
21
+ itself colours these two languages with, and
22
+ [Pygments' `rebol.py`](https://github.com/pygments/pygments/blob/master/pygments/lexers/rebol.py),
23
+ which keeps both lexers in one file for the same reason this package does.
24
+
25
+ This code is released under an [MIT license](LICENSE).
26
+
27
+ ## Install
28
+
29
+ ```
30
+ npm install @exercism/codemirror-lang-red
31
+ ```
32
+
33
+ ## Usage
34
+
35
+ ```javascript
36
+ import {EditorView, basicSetup} from "codemirror"
37
+ import {rebol} from "@exercism/codemirror-lang-red"
38
+
39
+ const view = new EditorView({
40
+ parent: document.body,
41
+ doc: `Rebol [
42
+ title: "Greeting"
43
+ date: 1-Jan-2020
44
+ ]
45
+
46
+ greet: func [name [string!] /loud][
47
+ print either loud [uppercase name][name]
48
+ ]
49
+
50
+ greet "world"
51
+ `,
52
+ extensions: [basicSetup, rebol()]
53
+ })
54
+ ```
55
+
56
+ `red()` is the same grammar with Red's preprocessor recognised.
57
+
58
+ Exercism's Red editor calls `red()`:
59
+
60
+ ```javascript
61
+ import {EditorView, basicSetup} from "codemirror"
62
+ import {red} from "@exercism/codemirror-lang-red"
63
+
64
+ new EditorView({
65
+ parent: document.body,
66
+ doc: `Red []\nprint "Hello, World!"\n`,
67
+ extensions: [basicSetup, red()]
68
+ })
69
+ ```
70
+
71
+ ## Completion
72
+
73
+ `red()` offers completion of:
74
+
75
+ - The functions, datatypes and constants available when Red starts. The list
76
+ comes from Red's own source, read by `scripts/red-words.mjs`, which copies
77
+ only names and signatures.
78
+ - After a `/`, the refinements of the function, for Red's functions and for
79
+ the functions in the file.
80
+ - Snippets for `func`, `function`, `does`, `has`, objects, `if`, `unless`,
81
+ `either`, `case`, `switch`, `foreach`, `repeat`, `loop`, `while`, `until`,
82
+ `collect` and `parse`.
83
+ - The words set in the file. Inside a function, its arguments, refinements
84
+ and locals are offered too. In a `function`, the set-words in the body are
85
+ locals, as Red makes them, so they are offered inside that function and
86
+ nowhere else.
87
+ - The words of `parse` inside its rules, and `keep` inside `collect`.
88
+
89
+ Nothing is offered inside strings, comments and the other literal values.
90
+ `rebol()` offers the words set in the file, without Red's lists.
91
+
92
+ To update the word list from a checkout of Red:
93
+
94
+ ```bash
95
+ npm run words -- ../red
96
+ ```
97
+
98
+ ## What it reads
99
+
100
+ Rebol states the whole of its syntax in a sentence. A block is a series of
101
+ values, loading a script produces a block, and running the script is evaluating
102
+ that block.
103
+
104
+ Everything difficult follows from it.
105
+
106
+ **There are no keywords.** `if`, `print` and `func` are words bound in a context
107
+ to functions, and a script may rebind any of them. What this package colours as
108
+ a control word or a builtin is a claim about what a word will most likely mean,
109
+ never about what it is. The tables are in `src/tokens.ts`.
110
+
111
+ **The datatype of a value is written into its spelling and nowhere else.** `a/b`
112
+ is a path, `/b` a refinement, `a:` a set-word, `:a` a get-word, `'a` a lit-word,
113
+ `a@b` an email, `a:b` a url, `%a` a file, `<a>` a tag, `1x2` a pair, `1.2.3` a
114
+ tuple, `10:30` a time and `1-Jan-2020/10:30+2:00` a date. None of that can be
115
+ told apart from the left, because the first character of every one of them is a
116
+ character a word may start with. So the run is read to the next delimiter and
117
+ asked about afterwards, which is what Rebol's own scanner does.
118
+
119
+ The slash is the sharp end of it. It ends a token in the scanner and it starts
120
+ a path, so `a/b` is one value and `a / b` is three, and the difference is the
121
+ space. Reading the whole path as one run is what keeps those apart.
122
+
123
+ **A `<` opens a tag unless what follows makes a word of it.** `<html>` is a tag
124
+ and `<=`, `<>`, `-->` and `<|` are words. Rebol decides this the same way, which
125
+ is why `if a <b [...]` is a syntax error in a language that has no syntax.
126
+
127
+ **A braced string counts its braces.** `{a {b} c}` is one string, and a `^{`
128
+ is the brace that does not count. That is how a string holding a line break is
129
+ written, and it is why the string scanner cannot be a regular expression.
130
+
131
+ **Functions and loops have their own nodes.** `func`, `function`, `has`,
132
+ `funct` and `closure` make a `Function` node, with a `ParamList` for the spec
133
+ and a `Body`. Each argument and local in the spec is a `Param`, which is
134
+ highlighted as a definition. `foreach`, `repeat`, `remove-each`, `map-each` and
135
+ `for` make a `Loop` node, and each word that they set is a `LoopVar`. In Red,
136
+ every function available when a Red console starts is highlighted as a builtin.
137
+
138
+ ## What the clean figure is worth
139
+
140
+ Almost nothing, and it is worth saying so.
141
+
142
+ Parsed against 3154 files of real code, this grammar leaves no error node in
143
+ 97.56% of them. That number does not mean what the same number means for a
144
+ language with keywords. Rebol has two brackets and no statement, so the only
145
+ thing that can fail a parse is a bracket with no partner. A page of English
146
+ parses. So does a JSON document.
147
+
148
+ Most of the work behind that figure was getting the corpus to hold Rebol.
149
+ `.red` is Redcode's extension as well, and a Core War warrior is a page of
150
+ three-letter opcodes that a bracket-counting parser reads without complaint;
151
+ `.r` is R's, and `.red` is also REDUCE's. Those three between them were more
152
+ than a third of what the extensions gathered, and the same corpus read 88.51%
153
+ with them still in it. That number was about the extensions.
154
+
155
+ The figure that does mean something is how much of a file comes back carrying a
156
+ highlight tag, and on how many distinct colours. Against CodinCod's pool that is
157
+ 100.00% of the non-whitespace characters for both languages, across twelve
158
+ colours for Rebol and eleven for Red, with the busiest of them taking 28.85% and
159
+ 32.33%. What the generated fallback managed on the same snippets was 21.78% and
160
+ 23.41%, on two colours.
161
+
162
+ The corpus holds Red's own compiler and runtime, Rebol 3 as Oldes maintains it,
163
+ and Carl Sassenrath's Rebol 2 mezzanine, alongside everything else the search
164
+ found. What still fails is mostly a bracket left open upstream, or a file in a
165
+ fourth language the extensions collect.
166
+
167
+ ## What it does not read
168
+
169
+ A block written in a dialect is a block of ordinary values that another function
170
+ interprets. `parse [some rules]` and `draw [line 0x0 10x10]` are coloured as
171
+ the values they are made of, because a grammar that coloured them as a dialect
172
+ would be guessing at a binding it cannot see. This package makes one exception:
173
+ the spec after `func` and the other function words is read as a spec, because
174
+ the arguments in it are what completion needs.
175
+
176
+ `comment [...]` is the same problem wearing a hat. Rebol has no block comment;
177
+ `comment` is a function that throws its argument away. This package colours the
178
+ block as a block.
179
+
180
+ ## Building and testing
181
+
182
+ ```
183
+ npm install # installs the dependencies and builds the package into dist/
184
+ npm run build # regenerates the parser, then rebuilds the package
185
+ npm test # builds the package, then runs the parse specs, the highlighting, the folding and indentation
186
+ ```
187
+
188
+ No corpus ships here, because none of that code is ours
189
+ to redistribute. [red/red](https://github.com/red/red),
190
+ [Oldes/Rebol3](https://github.com/Oldes/Rebol3) and
191
+ [rebol/rebol](https://github.com/rebol/rebol) are what these figures were taken
192
+ against, and `rebol.org`'s script library is the larger collection of code
193
+ people wrote by hand.
194
+
195
+ ## Contributing
196
+
197
+ Please read about how to [contribute to Exercism](https://exercism.org/docs/building).
198
+ Be sure to read the Exercism [Code of Conduct](https://exercism.org/docs/using/legal/code-of-conduct).