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