@exercism/codemirror-lang-raku 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/README.md CHANGED
@@ -1,3 +1,345 @@
1
- # Temporary Holding Version
1
+ # codemirror-lang-raku
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
+ [![npm version](https://img.shields.io/npm/v/codemirror-lang-raku)](https://www.npmjs.com/package/codemirror-lang-raku)
4
+ [![Test](https://github.com/kan/codemirror-lang-raku/actions/workflows/test.yml/badge.svg)](https://github.com/kan/codemirror-lang-raku/actions/workflows/test.yml)
5
+ [![License: MIT](https://img.shields.io/npm/l/codemirror-lang-raku)](LICENSE)
6
+
7
+ > This is Exercism's fork of
8
+ > [kan/codemirror-lang-raku](https://github.com/kan/codemirror-lang-raku) by
9
+ > Kan Fushihara, used in the editor on [exercism.org](https://exercism.org).
10
+ > Our changes are on the `exercism` branch, where the package is named
11
+ > `@exercism/codemirror-lang-raku`. The branch adds snippets for the
12
+ > common constructs (`sub`, `class`, `if`, `for`, `given` and others) to
13
+ > the completion. Everything else follows upstream.
14
+
15
+ [Raku](https://raku.org/) (formerly Perl 6) language support for the
16
+ [CodeMirror 6](https://codemirror.net/) code editor: syntax highlighting,
17
+ indentation, code folding, bracket matching and completion, built on a
18
+ [Lezer](https://lezer.codemirror.net/) grammar.
19
+
20
+ It covers comments and Pod, quotes of all kinds (`'…'`, `"…"`, `「…」`,
21
+ curly quotes, `q` / `qq` / `Q` with any delimiter, heredocs,
22
+ `<word lists>`) with interpolation, regexes and grammars, numbers,
23
+ variables with sigils and twigils, keywords, operators, including the
24
+ ones written in Unicode, blocks and declarations.
25
+
26
+ Try it in the [demo](https://kan.github.io/codemirror-lang-raku/), which
27
+ shows the syntax tree next to the editor.
28
+
29
+ > **Status: 0.x.** The node names of the syntax tree can still change
30
+ > between minor versions. See [Known limitations](#known-limitations) for
31
+ > what is missing.
32
+
33
+ ## Installation
34
+
35
+ ```sh
36
+ npm install @exercism/codemirror-lang-raku
37
+ ```
38
+
39
+ ## Usage
40
+
41
+ ```javascript
42
+ import {EditorView, basicSetup} from "codemirror"
43
+ import {raku} from "@exercism/codemirror-lang-raku"
44
+
45
+ new EditorView({
46
+ doc: 'say "Hello, Raku!";',
47
+ extensions: [basicSetup, raku()],
48
+ parent: document.body
49
+ })
50
+ ```
51
+
52
+ Highlighting uses only the standard tags of `@lezer/highlight`, so any
53
+ CodeMirror theme or highlight style applies.
54
+
55
+ To load the language on demand, the way `@codemirror/language-data` does:
56
+
57
+ ```javascript
58
+ import {LanguageDescription} from "@codemirror/language"
59
+
60
+ const rakuDescription = LanguageDescription.of({
61
+ name: "Raku",
62
+ alias: ["perl6"],
63
+ extensions: ["raku", "rakumod", "rakutest", "rakudoc", "p6", "pl6", "pm6", "pod6"],
64
+ load: () => import("@exercism/codemirror-lang-raku").then(m => m.raku())
65
+ })
66
+ ```
67
+
68
+ ## API Reference
69
+
70
+ <dl>
71
+ <dt><code><strong>raku</strong>() → LanguageSupport</code></dt>
72
+ <dd><p>Raku language support, with completion, and with block comment
73
+ tokens that fit the selection (<code>#`[ … ]</code> around text that holds an
74
+ unbalanced parenthesis).</p></dd>
75
+ <dt><code><strong>rakuLanguage</strong>: LRLanguage</code></dt>
76
+ <dd><p>A language provider based on the Lezer Raku parser, extended with
77
+ highlighting and indentation information.</p></dd>
78
+ <dt><code><strong>rakuCompletion</strong>: Extension</code></dt>
79
+ <dd><p>Completion of Raku keywords, of the commonly used built-in types,
80
+ routines, methods and special variables, and of the variables, routines,
81
+ types and methods that the document declares. <code>raku()</code> includes it. To
82
+ leave it out, use <code>rakuLanguage</code> in place of <code>raku()</code>.</p></dd>
83
+ <dt><code><strong>parser</strong>: LRParser</code></dt>
84
+ <dd><p>The Lezer parser for Raku, without the editor-specific node props.
85
+ Use <code>rakuLanguage.parser</code> for the configured one.</p></dd>
86
+ </dl>
87
+
88
+ ## Syntax tree
89
+
90
+ The grammar does not parse statements and expressions. It tokenizes the
91
+ input, nests brackets, and recognizes declarations, which is enough for
92
+ highlighting, folding and building an outline, and keeps the tree usable
93
+ on incomplete code.
94
+
95
+ | Node | Covers |
96
+ |---|---|
97
+ | `PackageDeclaration` | `class`, `role`, `grammar`, `module`, `package`, with a `PackageName` child, up to the body `Block` or the `;` of a `unit` declaration |
98
+ | `RoutineDeclaration` | `sub`, `method`, `submethod`, with a `RoutineName` child (absent for anonymous routines), up to the body `Block`. Also the `foo(…) { }` of `multi foo(…) { }`, which declares a sub without the word `sub`: that node starts with its `RoutineName` |
99
+ | `RegexDeclaration` | `token`, `rule`, `regex`, with a `RegexName` child, up to the body `Block`, which holds one `Regex`. The name includes a `:sym<…>`, with the blanks before it if there are any (`word :sym<long>`), and the brackets in it (`infix:sym<{ }>`) |
100
+ | `EnumDeclaration`, `SubsetDeclaration`, `ConstantDeclaration` | The declarator and its `EnumName`, `SubsetName` or `ConstantName`. The name of a constant includes its sigil if it has one (`constant $LIMIT`). A constant without a sigil that is declared with a backslash (`constant \x`) has no `ConstantName` |
101
+ | `Block`, `Parens`, `Brackets` | `{ }`, `( )`, `[ ]` |
102
+ | `VariableName`, `AttributeName`, `SpecialVariable` | `$x` / `$^a`, `$!x` / `$.x`, `$*x` / `$?x`. An operator that is referred to as a routine is one `VariableName`: `&infix:<+>`, `&[+]` |
103
+ | `Identifier`, `TypeName` | A bare name (`say`, `my-sub`), and a capitalized one (`Int`, `Foo::Bar`, the type capture `::T`, `::?CLASS`), with its smiley if it has one (`Int:D`) |
104
+ | `MethodCall` | The `.` and the `MethodName` of `.name`, `.?name` and the like. The arguments are the `Parens` after it |
105
+ | `PairKey` | `:key`, `:!key`. The value is the node after it |
106
+ | `Number`, `Version` | `42`, `1.5e3`, `0x1F`, `:16<FF>`, `½`, `Ⅻ`, `∞`, and `v6.d` |
107
+ | `True`, `False`, `Nil` | These three words |
108
+ | `Operator` | An operator made of symbols, a word operator where an operator is expected (`x`, `min`, `before`, `Z+`, `Zmin`), a set operator in parentheses (`(elem)`) |
109
+ | `StringLiteral` | A quote of any kind. One that interpolates has `Escape`, `Interpolation`, variable, `Brackets`, `Parens` and `MethodCall` children, and a `NestedDelimiters` for each pair of its own delimiters in it (the `[b]` of `qq[a [b] c]`) |
110
+ | `Heredoc` | The text of a heredoc, from the line after its `q:to/END/` opener through its terminator. The opener is a `StringLiteral`. The text of a `qq:to` heredoc has the same children as a string that interpolates. The texts of the heredocs that one line opens are a single node |
111
+ | `Regex` | `/…/`, `rx//`, `m//`, `s///`, `tr///`, or the body of a regex declaration. Its children are `CharacterClass` (`<[a..z]>`), `Assertion` (`<name>`, `<?before …>`), `StringLiteral`, `Escape` (`\d`), `Operator` (quantifiers, `\|`, anchors), `VariableName` (`$x`, `$<name>`), `LineComment`, `BlockComment`, and a `Block` for each `{ }` of code |
112
+ | `LineComment`, `DocComment`, `BlockComment` | `#`, `#\|` / `#=`, `` #`( ) ``. A declarator comment with brackets, `#\|( … )`, is one `DocComment` up to its closing bracket |
113
+ | `Pod` | `=begin` … `=end` and the other Pod blocks. Its children are `PodDirective` (`=begin`, `=head1`, `=end pod`), `PodHeading` (the text after `=head1`), and the formatting codes `PodStrong` (`B<…>`), `PodEmphasis` (`I<…>`), `PodCode` (`C<…>`), `PodLink` (`L<…>`) and `PodFormat` (the others) |
114
+
115
+ Each keyword is a node named after the keyword (`if`, `for`, `my`,
116
+ `is`, `eq`, …), and so is each phaser (`BEGIN`, `LEAVE`, …). A
117
+ declarator keyword is a child of its declaration node (`class`, `sub`,
118
+ `token`, …). The words `unit`, `multi`, `proto`, `only`, `my` and
119
+ `our` are not part of the declaration node. They are the nodes right
120
+ before it, also in `multi foo(…) { }`, where the declaration node has no
121
+ declarator keyword.
122
+
123
+ Statements and expressions have no nodes: `if $x { }` is the nodes `if`,
124
+ `VariableName` and `Block` next to each other.
125
+
126
+ ## Known limitations
127
+
128
+ Not supported yet:
129
+
130
+ - Nothing is highlighted in the replacement of `s/…/…/`, nor in
131
+ `tr/…/…/`. The second pair of brackets of `tr[…][…]` is read as code.
132
+ A replacement that is a block of code with the delimiter in it, as in
133
+ `s/x/{ "a/b" }/`, ends at that delimiter.
134
+ - In a regex, a `<…>` is one
135
+ token, with the code or the nested `<…>` in it, and has to be closed on
136
+ its line unless it is a character class. Groups (`[ ]`, `( )`) and
137
+ adverbs (`:i`, `:my`) are plain text, so the text after `:my` is read
138
+ as regex, and so is the code in `$( … )`. Such code does not end the
139
+ regex when it holds the delimiter (`/ :my $m = $/; /`), if it is on
140
+ one line.
141
+ - A heredoc is interpolated when everything interpolates in it, as in
142
+ `qq:to`. One that interpolates in part (`q:c:to`, `qq:!s:to`) is not
143
+ highlighted inside. When a line opens several heredocs, they are
144
+ interpolated only if all of them are. A `{` that is not closed in the
145
+ text takes the rest of the heredoc, with its terminator, for code.
146
+ - After an edit that makes a heredoc's opener no longer open it, such as
147
+ commenting out its line, the text of the heredoc can stay a heredoc
148
+ instead of turning back into code. An edit in the text, or in the code
149
+ shortly after it, makes it code again, as a parse from scratch reads
150
+ it. This takes a fix in `@lezer/common`
151
+ ([lezer/lezer#79](https://code.haverbeke.berlin/lezer/lezer/issues/79)).
152
+ - In Pod, a formatting code is one token: the codes inside it
153
+ (`B<I<…>>`) are not told apart. Formatting codes are left alone in a
154
+ block that is code (`=begin code`, `=code`, `=for code`), but not in a
155
+ code block inside another block, nor in an indented code paragraph.
156
+ Tables and the configuration after a directive (`:numbered`) are plain
157
+ text. A block in a block of the same name (`=begin pod` in
158
+ `=begin pod`) does not nest: the first `=end pod` closes the outer one.
159
+ - A `<<…>>` or `«…»` word list that holds `{ }`, `( )` or `;` can span up
160
+ to 10 lines. A longer one is not taken to be a word list. Nothing is
161
+ interpolated in a list that holds an unbalanced `{` or its own
162
+ delimiter: it is a single token.
163
+ - A Pod block between a declarator and its name, as between `class` and
164
+ the name of the class, hides the name: the declaration then has no
165
+ `PackageName`. A comment there does not.
166
+ - In a quote that has braces for delimiters, a `{ }` is taken to be a
167
+ nested pair of the delimiters, not a block of code: the `{$b + 1}` of
168
+ `qq{a {$b + 1} c}` is text, in which `$b` is interpolated. With other
169
+ delimiters, as in `qq[a {$b + 1} c]`, it is a block.
170
+ - A quote adverb is switched off by the argument `False` or `0`, as in
171
+ `qq:c(False)[…]`. With any other argument, such as a variable, it
172
+ counts as switched on.
173
+ - The brackets of a block comment are picked for the `toggleBlockComment`
174
+ command. Text that unbalances every bracket that is tried gets
175
+ `` #`( `` … `)`, which then does not cover exactly that text.
176
+ - A user-defined operator is recognized where it is declared
177
+ (`sub infix:<+++>`) and as a routine (`&infix:<+++>`). Where it is
178
+ used, it is an operator when it is made of operator characters or of
179
+ symbols outside of ASCII (`1 ⚡ 2`). One that is a word is a plain
180
+ name, also between hyper marks (`@a »plus« @b`). A term or a circumfix of one's own is not known: one that is
181
+ a symbol is highlighted as an operator, or is an error when the symbol
182
+ is no operator character (`℃`), and one that is a letter and a number,
183
+ such as `ℵ₀`, is a name and a number.
184
+ - A number that is one character is a `Number` when it is one of the
185
+ fractions, the Roman numerals, the subscripts, the circled numbers and
186
+ the ideographic numbers (`½`, `Ⅻ`, `₀`, `①`, `〇`). Others, as in the
187
+ scripts that have letters (`፩`, `௰`), are names, and so are the digits
188
+ of such scripts (`٤٢`). The full-width digits (`42`) are errors.
189
+ - After a name and a space, `+<`, `~<` and `!<` with no space after them
190
+ are taken for a prefix and a word list, as in a call (`say +<a b>`),
191
+ also when the name is a sigilless variable (`first +<a b>`). With a
192
+ space after them, they are the infix (`first +< 64`). After a
193
+ variable, they are always the infix, as in Raku.
194
+ - After a name and a space, double angles start a word list, also when
195
+ the name is a sigilless variable and they start a hyper infix
196
+ (`x «+« 1`). After a variable or a whatever star, they are the hyper
197
+ (`$x «+« 1`, `* «+« 1`).
198
+ - Double angles right after a prefix are taken for a hyper prefix
199
+ (`-<<@a`), so a word list there needs a space: `say + «a b»`, not
200
+ `say +«a b»`. After an infix with no space, they start a word list
201
+ only when the infix is one character or ends in `=` (`@a=«a b»`,
202
+ `$a==<<a b>>`), not after `~~`. A word list right after a hyper
203
+ operator (`@a»+«<a b>`) is read as part of the operator.
204
+ - In a hyper assignment with brackets, `=<<` and `=«` close the hyper
205
+ (`@a >>[&f]=<< @b`) only when the brackets are on one line and hold
206
+ about 20 characters at most, and no word follows the angles. With a
207
+ name right after them (`@a >>[&f]=<<foo`), or a longer infix in the
208
+ brackets, they start a word list, as after a hyper subscript
209
+ (`@a>>[0]=<<a b>>`). Brackets that start with a digit, `$` or `@` are
210
+ taken for a hyper subscript.
211
+ - The `native` declarator, which NativeCall uses internally, does not
212
+ make a declaration node.
213
+ - Completion does not know types. After a `.` it offers the built-in
214
+ methods and every method that the document declares, whatever the
215
+ invocant is. Names from other files are not offered.
216
+ - Completion takes a variable to be declared by `my`, `our`, `state` or
217
+ `has`, in a signature, after the `->` of a pointy block, or by being a
218
+ placeholder variable (`$^a`, `$:a`). A sigilless variable is offered
219
+ when it is declared with a backslash (`my \x`, `sub f(\x)`, `-> \x`),
220
+ and a constant when it is declared with `constant`. A term that is
221
+ declared as a routine (`sub term:<x>`) is not offered.
222
+ - In a string, only the code in `{ }` and in a subscript is completed,
223
+ not the method name of `"$x.name()"`.
224
+
225
+ Heuristics that can be wrong:
226
+
227
+ - A line is indented as the continuation of a statement when the line
228
+ before it ends in an operator or a comma, or when it starts with a
229
+ method call, with an operator that has a space after it, or with a
230
+ trait (`is`, `does`, `returns`, `handles`, `where`, `will`, as in
231
+ `is export {`). A statement that continues in another way is not
232
+ indented further. A line that starts with one of these words for
233
+ another reason, such as a call of the `is` of `Test`, is indented when
234
+ the line before it does not end in a `;` or a `}`. Inside `( )` and
235
+ `[ ]`, indentation follows the brackets only.
236
+ - A capitalized word is taken to be a type name.
237
+ - A symbol outside of ASCII is taken to be an operator, whatever it was
238
+ declared as. The built-in terms `∞` and `∅` are not: `∞` is a number,
239
+ and `∅` is followed by an operator.
240
+ - A set operator in parentheses, such as `(elem)` or `(<=)`, is an
241
+ operator after a term and a space: `$a (elem) $b`. Right after the
242
+ term, the parentheses hold arguments: `$f(elem)`.
243
+ - An operator in brackets, `&[+]`, is a routine when the operator is at
244
+ most 11 characters long, and not after a term, where the `&` is an
245
+ operator.
246
+ - An operator name in double angles, as in `infix:<< plus >>`, has to
247
+ end within 20 characters of where it starts.
248
+ - Whether `/` starts a regex and `<` starts a word list is decided from
249
+ the token before it. After a bare name, spacing decides: `say /x/` and
250
+ `say <a b>` are a regex and a word list, `foo / 2` and `n < 3` are not.
251
+ The built-in terms `pi`, `π`, `tau`, `τ`, `now`, `time` and `rand` are
252
+ followed by an operator whatever the spacing (`pi /2`), also when a
253
+ routine of your own has one of these names. After a closing brace, a
254
+ line break decides. After the value of a pair in angles, as in
255
+ `:ver<1>`, spacing decides as after a name, so that the import list of
256
+ `use Foo:ver<1> <a b>` is a word list.
257
+ - A `<…>` right after a term is a subscript only when it holds plain words
258
+ on one line: `%h<key>`, but not `$a<$b`, nor a key with a quote or
259
+ other punctuation in it (`%h<a"b>`). A `<<…>>` or `«…»` that touches
260
+ the term before it is a subscript when it is at most 24 characters
261
+ long and starts and ends in a word, a variable or a quote: `%h«$key»`,
262
+ but not the hyper operators of `@a<<+>>@b` and `@a«R-»@b`. A hyper
263
+ operator that is a word and touches both of its operands, as in
264
+ `@a<<min>>@b`, is misread as a subscript.
265
+ - A `<<` or `«` where a term is expected starts a word list when a `>>`
266
+ or `»` follows, within 10 lines if there is a `{ }`, `( )` or `;` in
267
+ between. One that is being typed takes the code up to there for its
268
+ content.
269
+ - In a `<<…>>` word list, a `>>` in a subscript or a call that is
270
+ interpolated ends the list: `<<@a[1 >> 2]>>`, and so does the `>` of
271
+ a subscript or a named capture right before the end, as in
272
+ `<<x %h<b>>>`, `<<x %*h<b>>>` and `<<x $<b>>>`. A heredoc that
273
+ is opened in a block in a word list or in a regex literal is not read
274
+ as one.
275
+ - `q`, `qq`, `Q`, `m`, `rx`, `s`, `tr` and their variants start a quote
276
+ when a delimiter follows directly: a bracket, one of
277
+ ``/ ! | ~ ^ % @ " ' ` ``, or a symbol outside of ASCII. So a
278
+ sigilless `s/2` is misread.
279
+ `q`, `qq` and `Q` also take a bracket after blanks (`q {…}`), so a
280
+ routine or a sigilless variable of your own named `q`, before a block
281
+ or a subscript, is misread. The
282
+ regex words do not: `m {…}` and `s {…}` are calls.
283
+ - In a string, a method call after a variable is interpolated when it
284
+ has an argument list, or when the calls chained to it end in one
285
+ within 24 characters: `"$x.a.b()"`, but not `"$x.a.b"`. For an `@`,
286
+ `%` or `&` variable, the 24 characters count from its sigil.
287
+ - `%name` and `&name` are variables, except between two terms with no
288
+ space on either side: `$a%b`.
289
+ - After `multi`, `proto` or `only`, a name is taken to declare a sub when
290
+ a `(` or a `{` follows it, on the same line or the next:
291
+ `multi foo($x) { }`. One that follows a comment or more than 20
292
+ characters of whitespace is not seen, and a call such as `only foo(1)`, of a
293
+ routine of your own that is named `only`, is read as a declaration.
294
+ - A word spelled like a keyword is a keyword wherever it appears, except
295
+ as a method name, as a declared name, before `=>`, after a backslash
296
+ (`sub f(\class)`), for a declarator that a method is called on
297
+ (`class.say`), and for the keywords that are routines (`take`,
298
+ `return`, `next`, `not`, …) right before a `(`: `take(1)`. Elsewhere, a
299
+ sigilless variable or a routine of your own that is named like a
300
+ keyword is highlighted as one, and one that is named like a
301
+ declarator, as in `say role` or `method($n)`, starts a declaration.
302
+ - In a regex, a curly quote or a corner quote opens a quote, as Raku
303
+ has it. A lone one, as in `/‘/`, takes the text up to the next
304
+ closing quote on its line.
305
+ - A `$/` in the replacement of `s/…/…/` is the match variable when a `/`
306
+ follows on the same line, and a `$` before the closing delimiter
307
+ otherwise.
308
+ - A `:sym<…>` after the name of a declaration and blanks is part of the
309
+ name, also for a declaration that is not a regex.
310
+ - A quote that does not interpolate (`'…'`, `q{…}`), a regex and a
311
+ heredoc have to be closed. Otherwise their opener is read as ordinary
312
+ code.
313
+ - A quote that interpolates (`"…`, `qq[…`), a Pod block or an embedded
314
+ comment that is not closed runs to the end of the document. A
315
+ declarator comment with brackets (`#|( …`) that is not closed ends
316
+ with its line.
317
+ - The body of a regex declaration that is not closed runs up to the next
318
+ line that starts a `token`, `rule` or `regex` declaration, or to the
319
+ end of the document.
320
+
321
+ ## Development
322
+
323
+ ```sh
324
+ npm install
325
+ npm test # builds, then runs the tests in test/
326
+ npm run dev # serves demo/, an editor next to the syntax tree of its content
327
+ ```
328
+
329
+ The demo loads the sources, so a change to the grammar shows up on reload.
330
+
331
+ Changes go in through pull requests. A pull request that changes what
332
+ users get adds its entry under `## [Unreleased]` in `CHANGELOG.md`. One
333
+ that adds a feature also adds a sample of it to
334
+ `test/fixtures/new-in-<next minor version>.raku` (`new-in-0.5.raku`, say),
335
+ and is labelled `enhancement` to make the next release a minor one.
336
+
337
+ To release from the `exercism` branch, set the version in `package.json`
338
+ and push a tag of the same version (`v1.0.0`, say).
339
+ The tag starts `.github/workflows/publish.yml`. It checks that the
340
+ tag matches `package.json`, builds and tests the package, and publishes it to npm as
341
+ `@exercism/codemirror-lang-raku`.
342
+
343
+ ## License
344
+
345
+ [MIT](LICENSE)