@exercism/codemirror-lang-lean 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 +235 -2
- package/dist/index.cjs +873 -0
- package/dist/index.d.cts +79 -0
- package/dist/index.d.ts +79 -0
- package/dist/index.js +867 -0
- package/package.json +52 -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,236 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Lean language support for CodeMirror
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
This repo contains the grammar for the Lean 4 language.
|
|
6
|
+
|
|
7
|
+
Lean's syntax is user-extensible, so no fixed grammar can describe it. This one
|
|
8
|
+
highlights Lean's tokens — keywords, sorts and core types, literals, comments and
|
|
9
|
+
operators — and nests them inside balanced `()`, `[]`, `{}` and `⟨⟩` nodes so
|
|
10
|
+
that bracket matching and folding work. A catch-all token means no input,
|
|
11
|
+
however malformed, can stop the parse.
|
|
12
|
+
|
|
13
|
+
Indentation follows the [Lean 4 VS Code extension][vscode-lean4]: a new line
|
|
14
|
+
starts where the line before it starts, one level deeper after an opening
|
|
15
|
+
bracket or under a focus dot (`· tactic`). Lean's layout carries meaning, so
|
|
16
|
+
lines that already exist are never re-indented.
|
|
17
|
+
|
|
18
|
+
Two more things are taken from that extension. `sorry`, `admit` and `#exit` are
|
|
19
|
+
tagged as invalid rather than as keywords, so that code left unfinished stands
|
|
20
|
+
out. And `'`, `!` and `?` count as parts of a word, so that a double click or a
|
|
21
|
+
word motion takes `x'`, `get!` and `find?` whole.
|
|
22
|
+
|
|
23
|
+
The highlighting covers what that extension's grammar marks without a Lean
|
|
24
|
+
server: the name a declaration introduces (`def add`, `structure Point`),
|
|
25
|
+
attributes (`@[simp]`, `attribute [simp]`), modifiers (`private`, `partial`),
|
|
26
|
+
and the strings of `m!`, `panic!`, `throwError`, `dbg_trace` and
|
|
27
|
+
`reportIssue!`, whose `{…}` hold code like those of `s!`. What it adds on top
|
|
28
|
+
is a curated list of core type names (`Nat`, `Std.HashMap`,
|
|
29
|
+
`Std.Time.PlainDateTime`), which VS Code leaves plain. What needs the Lean
|
|
30
|
+
server (local variables, fields, tactics defined by the user) is out of reach.
|
|
31
|
+
|
|
32
|
+
The lexical rules follow the Lean 4 sources: `src/Init/Meta/Defs.lean` for what
|
|
33
|
+
may appear in an identifier, and `src/Lean/Parser/Basic.lean` for comments,
|
|
34
|
+
strings, characters and numbers. The keyword list is drawn from
|
|
35
|
+
`src/Lean/Parser/{Command,Term,Do}.lean` plus the core tactics, and has every
|
|
36
|
+
keyword the VS Code extension's grammar has; being a curated
|
|
37
|
+
list rather than a closed one, it is the part most likely to need extending.
|
|
38
|
+
|
|
39
|
+
A tactic's name is a keyword to Lean only where a tactic can start, and an
|
|
40
|
+
ordinary name anywhere else, which a grammar of tokens cannot tell apart. So
|
|
41
|
+
the tactics named with everyday words (`case`, `cases`, `change`, `clear`,
|
|
42
|
+
`delta`, `done`, `ext`, `fail`, `first`, `focus`, `iterate`, `left`, `next`,
|
|
43
|
+
`replace`, `right`, `skip`, `split`) are deliberately not in the list: in
|
|
44
|
+
`(left right : Tree α)`, `next : Option Nat` or `.fail` they are names, and
|
|
45
|
+
that is how programs use them far more often than proofs do.
|
|
46
|
+
|
|
47
|
+
⚠️ To ensure that the package can be integrated on the Exercism website, don't change the packages' versions:
|
|
48
|
+
|
|
49
|
+
```json
|
|
50
|
+
"@codemirror/language": "^6.10.1",
|
|
51
|
+
"@codemirror/state": "^6.0.0",
|
|
52
|
+
"@codemirror/view": "^6.0.0",
|
|
53
|
+
"@lezer/highlight": "^1.2.0",
|
|
54
|
+
"@lezer/lr": "^1.4.0"
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
The `@codemirror/state` and `@codemirror/view` ranges are wide on purpose. An
|
|
58
|
+
editor extension only works with the copy of those two packages that the editor
|
|
59
|
+
itself was built from, so the ranges have to accept whatever version the website
|
|
60
|
+
has installed, rather than make the package manager install a second one.
|
|
61
|
+
|
|
62
|
+
## Unicode input
|
|
63
|
+
|
|
64
|
+
`lean()` also brings Lean's `\`-abbreviations: type `\to` and get `→`, `\<>` and
|
|
65
|
+
get `⟨⟩` with the cursor in between, `\alpha` (or just `\a`) and get `α`.
|
|
66
|
+
|
|
67
|
+
They behave as in the [Lean 4 VS Code extension][vscode-lean4], because they are
|
|
68
|
+
the same: the table of abbreviations and the rules for replacing them come from
|
|
69
|
+
[`@leanprover/unicode-input`][unicode-input], the package that extension is built
|
|
70
|
+
on. This repository contains neither, only the code that connects that package to
|
|
71
|
+
CodeMirror (`src/unicode-input.ts`), so updating the dependency is all it takes
|
|
72
|
+
to follow upstream.
|
|
73
|
+
|
|
74
|
+
- The abbreviation being typed is underlined.
|
|
75
|
+
- It is replaced when the next character can't be part of an abbreviation
|
|
76
|
+
(`\to` followed by a space), or at once when nothing longer starts with it
|
|
77
|
+
(`\forall`).
|
|
78
|
+
- Moving the cursor away, pressing <kbd>Tab</kbd> or leaving the editor replaces
|
|
79
|
+
it too. <kbd>Tab</kbd> is only taken while an abbreviation is being typed.
|
|
80
|
+
- What was typed needn't be complete: `\al` gives `∀`, the shortest abbreviation
|
|
81
|
+
that starts with `al`.
|
|
82
|
+
- `\\` gives a single `\`.
|
|
83
|
+
- Undo brings back the text as typed.
|
|
84
|
+
- Resting the pointer on a symbol shows how to type it: "Type ⟨ using `\<` or
|
|
85
|
+
`\langle`. ⟨ can be auto-closed with ⟩ using `\<>`."
|
|
86
|
+
|
|
87
|
+
One thing differs from VS Code. There, abbreviations are also replaced inside
|
|
88
|
+
string and character literals, which turns the escapes `\t`, `\r` and `\"`
|
|
89
|
+
into `▸`, `→` and `Ä` as you type them. Here a `\` typed inside a literal is left
|
|
90
|
+
alone (the code inside an `s!"…{…}…"` interpolation is still code). Pass
|
|
91
|
+
`inStringLiterals: true` for VS Code's behaviour.
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
import { lean } from "@exercism/codemirror-lang-lean";
|
|
95
|
+
|
|
96
|
+
lean(); // abbreviations on, with VS Code's defaults
|
|
97
|
+
lean({ unicodeInput: false }); // highlighting only
|
|
98
|
+
lean({
|
|
99
|
+
unicodeInput: {
|
|
100
|
+
leader: "\\", // lean4.input.leader
|
|
101
|
+
eagerReplacement: true, // lean4.input.eagerReplacementEnabled
|
|
102
|
+
customTranslations: { foo: "☺" }, // lean4.input.customTranslations
|
|
103
|
+
inStringLiterals: false,
|
|
104
|
+
hover: true, // the hints on symbols
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
The extension is exported on its own as `leanUnicodeInput()`.
|
|
110
|
+
`convertLeanAbbreviations` is a command that replaces what is being typed right
|
|
111
|
+
away (what <kbd>Tab</kbd> runs), and `abbreviationHintAt(state, pos)` returns
|
|
112
|
+
what the hover shows, for a host that wants to present it differently.
|
|
113
|
+
|
|
114
|
+
[vscode-lean4]: https://github.com/leanprover/vscode-lean4
|
|
115
|
+
[unicode-input]: https://www.npmjs.com/package/@leanprover/unicode-input
|
|
116
|
+
|
|
117
|
+
## Install
|
|
118
|
+
|
|
119
|
+
To keep the package working on the Exercism website, don't change the versions of these packages:
|
|
120
|
+
|
|
121
|
+
```json
|
|
122
|
+
"@codemirror/language": "^6.10.1",
|
|
123
|
+
"@codemirror/state": "^6.0.0",
|
|
124
|
+
"@codemirror/view": "^6.0.0",
|
|
125
|
+
"@lezer/highlight": "^1.2.0",
|
|
126
|
+
"@lezer/lr": "^1.4.0"
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
A range the website's lockfile doesn't already have makes it install a second
|
|
130
|
+
copy of that package, and the editor extensions built on the second copy then
|
|
131
|
+
do nothing.
|
|
132
|
+
|
|
133
|
+
- NPM: `npm install @exercism/codemirror-lang-lean`
|
|
134
|
+
- Yarn: `yarn add @exercism/codemirror-lang-lean`
|
|
135
|
+
- Bun: `bun add @exercism/codemirror-lang-lean`
|
|
136
|
+
|
|
137
|
+
## Prerequisites
|
|
138
|
+
|
|
139
|
+
- [Bun](https://bun.sh/)
|
|
140
|
+
|
|
141
|
+
## Resources
|
|
142
|
+
|
|
143
|
+
- [CodeMirror](https://codemirror.net/docs/)
|
|
144
|
+
- [Lezer](https://lezer.codemirror.net/docs/guide/)
|
|
145
|
+
- [Writing a Grammar](https://lezer.codemirror.net/docs/guide/#writing-a-grammar)
|
|
146
|
+
- [Examples](https://lezer.codemirror.net/examples/)
|
|
147
|
+
- [Existing grammar files](https://github.com/search?q=org%3Alezer-parser+path%3A%2F.grammar%24%2F&type=code)
|
|
148
|
+
|
|
149
|
+
## Structure
|
|
150
|
+
|
|
151
|
+
The repo's source files are defined in [TypeScript](https://www.typescriptlang.org/).
|
|
152
|
+
|
|
153
|
+
The repo is structured as follows:
|
|
154
|
+
|
|
155
|
+
```text
|
|
156
|
+
.
|
|
157
|
+
├── .github/workflows
|
|
158
|
+
│ ├── test.yml (builds and tests every pull request and push to main)
|
|
159
|
+
│ ├── publish.yml (publishes to npm when a v* tag is pushed)
|
|
160
|
+
│ └── sync-labels.yml (keeps the issue labels in step with Exercism's)
|
|
161
|
+
├── src
|
|
162
|
+
│ ├── index.ts (the main plugin: language, highlighting, `lean()`)
|
|
163
|
+
│ ├── syntax.grammar (the Lezer grammar)
|
|
164
|
+
│ ├── syntax.grammar.d.ts, syntax.grammar.terms.d.ts (the grammar typings)
|
|
165
|
+
│ ├── type-names.ts (the curated type names, kept in step with the grammar)
|
|
166
|
+
│ ├── ident-utils.ts (what may appear in an identifier)
|
|
167
|
+
│ ├── interp-prefix.ts, std-namespace.ts, qualified-type.ts (hand-written tokenizers)
|
|
168
|
+
│ ├── tokens.ts, std-tokens.ts, qualified-type-tokens.ts (what the grammar imports them through)
|
|
169
|
+
│ ├── indent.ts (indentation on Enter)
|
|
170
|
+
│ └── unicode-input.ts (the `\`-abbreviations)
|
|
171
|
+
├── test
|
|
172
|
+
│ ├── cases (test cases)
|
|
173
|
+
│ │ └ *.txt (test case)
|
|
174
|
+
│ ├── grammar.test.ts (test file)
|
|
175
|
+
│ ├── indent.test.ts (test file)
|
|
176
|
+
│ ├── parser.ts (the parser the tests share)
|
|
177
|
+
│ └── unicode-input.test.ts (test file)
|
|
178
|
+
├── tools
|
|
179
|
+
│ └── parse-corpus.mjs (parses a tree of .lean files, dev only)
|
|
180
|
+
├── index.html (dev server file)
|
|
181
|
+
├── rollup.config.js (bundling config)
|
|
182
|
+
├── tsconfig.json (typescript config)
|
|
183
|
+
└── vite.config.js (vite dev server config)
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## Setup
|
|
187
|
+
|
|
188
|
+
Run `bun install` to install all dependencies.
|
|
189
|
+
|
|
190
|
+
## Developing
|
|
191
|
+
|
|
192
|
+
To help with development, run `bun run dev`.
|
|
193
|
+
This will start a [Vite](https://vite.dev/) dev server (usually at http://localhost:5173/) that renders the `index.html` file.
|
|
194
|
+
The `#editor` element gets populated with some sample source code of your choosing and then it will get transformed by the grammar defined in `src/syntax.grammar`.
|
|
195
|
+
Any changes to the grammar will auto-refresh the dev server's rendered contents.
|
|
196
|
+
|
|
197
|
+
## Testing
|
|
198
|
+
|
|
199
|
+
The `test/cases` directory contains the tests files.
|
|
200
|
+
Run `bun run test` to build the package and then run these tests (`bun test`
|
|
201
|
+
alone runs them without building; the tests build their own parser from the
|
|
202
|
+
grammar). The Test workflow runs the same command on every pull request and on
|
|
203
|
+
every push to `main`.
|
|
204
|
+
|
|
205
|
+
Note: test (case) files should be relatively small and focus on a single aspect of a grammar.
|
|
206
|
+
|
|
207
|
+
For wider coverage, `tools/parse-corpus.mjs` parses every `.lean` file under the
|
|
208
|
+
directories you give it and reports both error nodes and `Unknown` tokens (a
|
|
209
|
+
character the tokenizer has no rule for):
|
|
210
|
+
|
|
211
|
+
```shell
|
|
212
|
+
bun tools/parse-corpus.mjs ../lean/exercises
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
Point it at a checkout of the [Lean track][track], or at Lean's own `src/Init`
|
|
216
|
+
and `src/Std`.
|
|
217
|
+
|
|
218
|
+
[track]: https://github.com/exercism/lean
|
|
219
|
+
|
|
220
|
+
## Building
|
|
221
|
+
|
|
222
|
+
Run `bun run prepare` to build the package into `dist/`.
|
|
223
|
+
`bun install` runs the same command, so a fresh install also builds the package.
|
|
224
|
+
Only `dist/` is published, along with this README and the licence.
|
|
225
|
+
|
|
226
|
+
## Publishing
|
|
227
|
+
|
|
228
|
+
To publish a new version, set `version` in `package.json` and commit it.
|
|
229
|
+
Then push a tag made of `v` followed by that version, for example `v1.0.0`.
|
|
230
|
+
The tag starts the Publish workflow, which builds and tests the package and publishes it to npm.
|
|
231
|
+
If the tag differs from `version`, the workflow stops before it publishes.
|
|
232
|
+
|
|
233
|
+
The workflow calls Exercism's shared
|
|
234
|
+
[`npm-publish.yml`](https://github.com/exercism/github-actions/blob/main/.github/workflows/npm-publish.yml),
|
|
235
|
+
which publishes through npm's trusted publishing, so the repository needs no npm token.
|
|
236
|
+
The package on npmjs.com needs a trusted publisher that names this repository and `publish.yml`.
|