@exercism/codemirror-lang-scheme 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 +21 -0
- package/README.md +164 -2
- package/dist/index.cjs +722 -0
- package/dist/index.d.cts +24 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +710 -0
- package/package.json +57 -4
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,165 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Scheme language support for CodeMirror
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
This package gives the [CodeMirror 6](https://codemirror.net/) editor support for Scheme and Racket.
|
|
6
|
+
Exercism uses it in the editors for the Scheme and Racket tracks.
|
|
7
|
+
|
|
8
|
+
It has:
|
|
9
|
+
|
|
10
|
+
- a [Lezer](https://lezer.codemirror.net/) grammar for R7RS Scheme, which also reads the Guile and Chez extensions used on the track (square brackets, `#:keywords`, `define*`, `#nil`, `#!r6rs` and so on).
|
|
11
|
+
Its `racket` dialect adds Racket's forms, such as `struct`, `match`, `for/list` and `module+`.
|
|
12
|
+
- highlighting for special forms, definitions, procedure calls, operators, strings, characters, numbers, booleans and comments.
|
|
13
|
+
- indentation in the usual Lisp style, and folding for lists, vectors and block comments.
|
|
14
|
+
- completion for R7RS syntax and procedures (or Racket's), snippets for the common forms, and the names defined in the file.
|
|
15
|
+
|
|
16
|
+
⚠️ To ensure that the package can be integrated on the Exercism website, don't change the packages' versions:
|
|
17
|
+
|
|
18
|
+
```json
|
|
19
|
+
"@codemirror/language": "^6.10.1",
|
|
20
|
+
"@lezer/highlight": "^1.2.0",
|
|
21
|
+
"@lezer/lr": "^1.4.0"
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Install
|
|
25
|
+
|
|
26
|
+
- NPM: `npm install @exercism/codemirror-lang-scheme`
|
|
27
|
+
- Yarn: `yarn add @exercism/codemirror-lang-scheme`
|
|
28
|
+
- Bun: `bun add @exercism/codemirror-lang-scheme`
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
import { EditorView, basicSetup } from "codemirror";
|
|
34
|
+
import { scheme } from "@exercism/codemirror-lang-scheme";
|
|
35
|
+
|
|
36
|
+
new EditorView({
|
|
37
|
+
doc: "(define (square x) (* x x))",
|
|
38
|
+
extensions: [basicSetup, scheme()],
|
|
39
|
+
parent: document.querySelector("#editor"),
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
For Racket, use `racket()` in the same way:
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
import { racket } from "@exercism/codemirror-lang-scheme";
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The package also exports `schemeLanguage` and `racketLanguage`, the completion sources (`builtinCompletionSource`, `racketCompletionSource` and `localCompletionSource`), and the name lists (`r7rsSyntax`, `r7rsProcedures`, `racketSyntax` and `racketProcedures`).
|
|
50
|
+
|
|
51
|
+
## How the grammar works
|
|
52
|
+
|
|
53
|
+
A Scheme program is a list of data, so the grammar reads lists, vectors, atoms and quotes.
|
|
54
|
+
The forms that bind names get their own shape on top of that:
|
|
55
|
+
`define`, `define-values`, `define-syntax`, `define-record-type`, `lambda`, `case-lambda`, `let` (and `let*`, `letrec`, `letrec*` and named `let`), `let-values`, `receive`, `do` and `guard`.
|
|
56
|
+
In those forms, the tree marks each name with `VarName` (a definition), `LocalName` (a parameter or local binding), `LoopName` (a named `let`), `TypeName` or `FieldName` (a record).
|
|
57
|
+
Every other symbol is a `Symbol`, and a symbol at the head of a list is a `Callee`.
|
|
58
|
+
|
|
59
|
+
Each binding form also parses as a plain list.
|
|
60
|
+
If a form doesn't fit its shape, for example while it is still being typed, the parser will fall back to the plain list and report no error.
|
|
61
|
+
|
|
62
|
+
The lists of special forms are at the end of `src/syntax.grammar`.
|
|
63
|
+
The names offered by completion are in `src/builtins.ts`.
|
|
64
|
+
|
|
65
|
+
### Racket
|
|
66
|
+
|
|
67
|
+
Racket uses the same grammar with the `racket` dialect turned on.
|
|
68
|
+
The grammar reads `#lang` lines, braces, regexps such as `#px"\\d+"`, byte strings and `#hash(...)` literals in both languages, because none of them clash with Scheme.
|
|
69
|
+
The dialect adds Racket's special forms, which are in their own block at the end of `src/syntax.grammar`.
|
|
70
|
+
Without the dialect, `struct`, `match`, `for/list` and the rest are ordinary names, so Scheme code parses as plain Scheme.
|
|
71
|
+
|
|
72
|
+
The dialect gives these forms their own shape:
|
|
73
|
+
|
|
74
|
+
- `define/contract` and the class forms `define/public` and so on parse like `define`.
|
|
75
|
+
- `struct` marks the type name with `TypeName` and the fields with `FieldName`.
|
|
76
|
+
- `match`, `match*`, `match-lambda` and `match-define` wrap each pattern in a `Pattern` node.
|
|
77
|
+
- the `for` forms mark the names in their clauses with `LocalName`. `for/fold`, `for/foldr` and `for/lists` also have a list of accumulators.
|
|
78
|
+
- `let/ec` and `let/cc` mark their continuation with `LocalName`.
|
|
79
|
+
|
|
80
|
+
Other Racket forms, such as `provide`, `require`, `module+` and `->`, are highlighted as keywords.
|
|
81
|
+
The Racket entry point is in `src/racket.ts`, and its completion names are in `src/racket-builtins.ts`.
|
|
82
|
+
|
|
83
|
+
## Completion
|
|
84
|
+
|
|
85
|
+
For Scheme, the completion offers:
|
|
86
|
+
|
|
87
|
+
- R7RS syntax and procedures, and some R6RS, SRFI-1 and Guile procedures often used in the track's solutions.
|
|
88
|
+
- snippets for `define`, `lambda`, `let`, named `let`, `let*`, `letrec`, `if`, `cond`, `case`, `when`, `unless`, `do`, `define-record-type` and `import`.
|
|
89
|
+
- the names defined or bound in the file, where they are in scope.
|
|
90
|
+
A top-level definition is offered everywhere. A parameter is offered inside its procedure. A `let` binding is offered in the body of its `let`. For `let*`, it is also offered in the later bindings. For `letrec`, it is offered in all of the bindings.
|
|
91
|
+
|
|
92
|
+
For Racket, the completion offers:
|
|
93
|
+
|
|
94
|
+
- the syntax and procedures of `racket/base`, and the common ones from the libraries loaded by `#lang racket` (`racket/list`, `racket/string`, `racket/math`, `racket/set`, `racket/function` and `racket/contract`), plus rackunit's checks.
|
|
95
|
+
- snippets for `define`, `define/contract`, `lambda`, `let`, named `let`, `let*`, `if`, `cond`, `case`, `when`, `unless`, `match`, `struct`, `for`, `for/list`, `for/fold`, `provide`, `require` and a `module+ test` submodule. They use square brackets in the Racket style.
|
|
96
|
+
- the names in scope, as for Scheme, and also:
|
|
97
|
+
a struct's name, predicate, accessors and setters (such as `point?`, `point-x` and `set-point-x!`),
|
|
98
|
+
a `match` clause's pattern variables in that clause,
|
|
99
|
+
a `for` form's loop variables in its body and later clauses,
|
|
100
|
+
and a `for/fold` form's accumulators in its body.
|
|
101
|
+
|
|
102
|
+
Completion switches off inside strings, regexps and comments, and while a new name is being typed.
|
|
103
|
+
|
|
104
|
+
## Prerequisites
|
|
105
|
+
|
|
106
|
+
- [Bun](https://bun.sh/)
|
|
107
|
+
|
|
108
|
+
## Setup
|
|
109
|
+
|
|
110
|
+
Run `bun install` to install all dependencies.
|
|
111
|
+
|
|
112
|
+
## Developing
|
|
113
|
+
|
|
114
|
+
Run `bun run dev` to start a [Vite](https://vite.dev/) dev server (usually at http://localhost:5173/).
|
|
115
|
+
It renders `index.html`, which shows some sample Scheme code in an editor that uses the grammar in `src/syntax.grammar`.
|
|
116
|
+
The page reloads when the grammar changes.
|
|
117
|
+
|
|
118
|
+
## Structure
|
|
119
|
+
|
|
120
|
+
```text
|
|
121
|
+
.
|
|
122
|
+
├── src
|
|
123
|
+
│ ├── index.ts (the Scheme language support and the package's exports)
|
|
124
|
+
│ ├── racket.ts (the Racket language support)
|
|
125
|
+
│ ├── language.ts (highlighting, indentation and folding for both)
|
|
126
|
+
│ ├── complete.ts (the completion sources)
|
|
127
|
+
│ ├── builtins.ts (the Scheme names offered by completion)
|
|
128
|
+
│ ├── racket-builtins.ts (the Racket names offered by completion)
|
|
129
|
+
│ ├── syntax.grammar (the Lezer grammar)
|
|
130
|
+
│ └── syntax.grammar.d.ts (the grammar typings)
|
|
131
|
+
├── test
|
|
132
|
+
│ ├── cases (grammar test cases)
|
|
133
|
+
│ ├── grammar.test.ts (runs the grammar test cases)
|
|
134
|
+
│ ├── completion.test.ts (tests for Scheme completion and indentation)
|
|
135
|
+
│ ├── racket.test.ts (tests for Racket completion and indentation)
|
|
136
|
+
│ └── lezer-plugin.ts (lets bun import the grammar)
|
|
137
|
+
├── index.html (dev server page)
|
|
138
|
+
├── rollup.config.js (bundling config)
|
|
139
|
+
├── tsconfig.json (typescript config)
|
|
140
|
+
└── vite.config.js (vite dev server config)
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Testing
|
|
144
|
+
|
|
145
|
+
Run `bun test`.
|
|
146
|
+
Each file in `test/cases` holds grammar tests in the Lezer test format.
|
|
147
|
+
The files named `racket-*.txt` run with the `racket` dialect.
|
|
148
|
+
`test/completion.test.ts` checks which completions appear at a given position, and how lines are indented.
|
|
149
|
+
|
|
150
|
+
## Building
|
|
151
|
+
|
|
152
|
+
Run `bun run prepare` to build the package into `dist`.
|
|
153
|
+
|
|
154
|
+
## Publishing
|
|
155
|
+
|
|
156
|
+
To publish a new version, set `version` in `package.json` and commit it.
|
|
157
|
+
Then push a git tag named `v` followed by that version, such as `v1.0.1` for version 1.0.1.
|
|
158
|
+
The publish workflow builds and tests the package, then publishes it to npm.
|
|
159
|
+
|
|
160
|
+
## Resources
|
|
161
|
+
|
|
162
|
+
- [CodeMirror](https://codemirror.net/docs/)
|
|
163
|
+
- [Lezer](https://lezer.codemirror.net/docs/guide/)
|
|
164
|
+
- [R7RS-small](https://small.r7rs.org/)
|
|
165
|
+
- [The Racket Reference](https://docs.racket-lang.org/reference/)
|