@exercism/codemirror-lang-factor 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (C) 2024 by Erik Schierboom <erik_schierboom@hotmail.com> and others
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
13
+ all 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
21
+ THE SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,106 @@
1
- # Temporary Holding Version
1
+ # Factor language support for CodeMirror
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/@exercism/codemirror-lang-factor)
4
+
5
+ This package gives the [CodeMirror 6](https://codemirror.net/) editor support for [Factor](https://factorcode.org/).
6
+ Exercism uses it in the editor for the Factor track.
7
+
8
+ It has:
9
+
10
+ - a [Lezer](https://lezer.codemirror.net/) grammar for Factor source files.
11
+ - highlighting for parsing words, definitions, words, stack effects, locals, vocabularies, strings, characters, numbers, booleans and comments.
12
+ - indentation for definitions, quotations and arrays, and folding for the same.
13
+ - completion for parsing words, snippets, the words of Factor's common vocabularies (with their stack effects), vocabulary names after `USING:`, and the words and locals defined in the file.
14
+
15
+ ⚠️ Keep the versions of these dependencies as they are, so that the package works on the Exercism website:
16
+
17
+ ```json
18
+ "@codemirror/language": "^6.10.1",
19
+ "@lezer/highlight": "^1.2.0",
20
+ "@lezer/lr": "^1.4.0"
21
+ ```
22
+
23
+ ## Install
24
+
25
+ - NPM: `npm install @exercism/codemirror-lang-factor`
26
+ - Yarn: `yarn add @exercism/codemirror-lang-factor`
27
+ - Bun: `bun add @exercism/codemirror-lang-factor`
28
+
29
+ ## Usage
30
+
31
+ ```js
32
+ import { EditorView, basicSetup } from "codemirror";
33
+ import { factor } from "@exercism/codemirror-lang-factor";
34
+
35
+ new EditorView({
36
+ doc: ": square ( n -- n^2 ) dup * ;",
37
+ extensions: [basicSetup, factor()],
38
+ parent: document.querySelector("#editor"),
39
+ });
40
+ ```
41
+
42
+ The package also exports `factorLanguage`, the two completion sources (`builtinCompletionSource` and `localCompletionSource`), and the lists used by those sources (`vocabularyWords`, `vocabularyNames`, `definitionWords` and `bodyWords`).
43
+
44
+ ## How the grammar works
45
+
46
+ Factor splits its source at whitespace, and every piece is a word.
47
+ The tokenizer in `src/tokens.ts` reads one piece at a time and sorts it:
48
+
49
+ - a piece that reads as a number, such as `42`, `-1/2` or `0xff`, is a `Number`. `2dup` and `1+` are words.
50
+ - a piece that ends with `:`, such as `USING:` or `quot:`, is a `ColonWord`.
51
+ - a piece that ends with `{`, such as `H{` or `V{`, opens an array.
52
+ - `R/ ... /` is a regular expression, and `[[ ... ]]` is a raw string.
53
+ - the piece after `CHAR:`, `\` or `POSTPONE:` is read as it is, so `CHAR: [` doesn't open a quotation.
54
+ - every other piece is a `Word`.
55
+
56
+ The grammar then picks out the parsing words by their text.
57
+ Each definition form has its own node, such as `WordDefinition` for `:`, `LocalsDefinition` for `::`, `TupleDefinition` for `TUPLE:` and `MethodDefinition` for `M:`.
58
+ In those forms, the tree marks the new name with `DefName`, `TypeName` or `SlotName`, and stack effects with `StackEffect`.
59
+ Locals are marked with `LocalName`, in `[| a b | ... ]` and after `:>`.
60
+
61
+ A word that isn't a parsing word is a `Word`, so a file with an unknown parsing word still parses.
62
+ Parsing words that take their own syntax, such as `FUNCTION:` or `EBNF:`, can give a tree that doesn't match their meaning.
63
+
64
+ ## Completion
65
+
66
+ `localCompletionSource` offers the words defined at the top level of the file.
67
+ A tuple, error or other class also gives its predicate (such as `point?`), and each slot gives its accessors (`x>>`, `>>x` and `change-x`).
68
+ It offers locals where they are in scope: the inputs of a `::` or `M::` definition in its body, the names of a `[| ... |` quotation inside it, and names bound with `:>` after the binding in the same quotation.
69
+
70
+ `builtinCompletionSource` offers:
71
+
72
+ - the words of Factor's common vocabularies, such as `kernel`, `sequences`, `math`, `assocs` and `combinators`, with each word's stack effect.
73
+ - parsing words such as `:>`, `[|`, `H{` and `CHAR:`. Definition words, such as `TUPLE:` and `M:`, are offered at the top level only.
74
+ - snippets for a word definition, a `::` definition, a `USING:` and `IN:` header, a tuple, a generic word and a method, and for `if` with two quotations, `when`, `cond`, `case`, `each`, `map`, `reduce` and others.
75
+ - vocabulary names after `USING:`, `USE:`, `IN:` and the like.
76
+
77
+ Completion is switched off in strings, comments and stack effects, and where a new name is being typed.
78
+
79
+ `src/vocabulary.ts` is generated by `scripts/vocabulary.py` from a checkout of [Factor's sources](https://github.com/factor/factor).
80
+ It holds only the names, stack effects and vocabularies of the public words.
81
+
82
+ ## Prerequisites
83
+
84
+ - [Bun](https://bun.sh/)
85
+
86
+ ## Developing
87
+
88
+ Run `bun install` to install the dependencies, and `bun run dev` to start a [Vite](https://vite.dev/) dev server with an editor at http://localhost:5173/.
89
+
90
+ ## Testing
91
+
92
+ Run `bun run test`.
93
+ It builds the package first, because the tests use the built parser with its external tokenizer.
94
+ The grammar tests are in `test/cases`, and the completion and editor tests are in `test/*.test.ts`.
95
+
96
+ ## Building
97
+
98
+ Run `bun run prepare` to build the package into `dist/`.
99
+ `bun install` runs the same command, so a fresh install also builds the package.
100
+
101
+ ## Publishing
102
+
103
+ To publish a new version, set `version` in `package.json` and commit it.
104
+ Then push a tag made of `v` followed by that version, for example `v1.0.0`.
105
+ The tag starts the Publish workflow, which builds and tests the package and publishes it to npm.
106
+ If the tag differs from `version`, the workflow stops before it publishes.