@exercism/codemirror-lang-ocaml 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 +36 -0
- package/LICENSE +21 -0
- package/README.md +169 -2
- package/dist/index.cjs +1260 -0
- package/dist/index.d.cts +147 -0
- package/dist/index.d.ts +147 -0
- package/dist/index.js +1253 -0
- package/package.json +55 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 1.0.0
|
|
4
|
+
|
|
5
|
+
This is Exercism's first release of the package, published as
|
|
6
|
+
`@exercism/codemirror-lang-ocaml`. It adds a `Callee` node for the function an
|
|
7
|
+
application calls, completion of the names in scope, snippets, and folding for
|
|
8
|
+
definitions that span several lines.
|
|
9
|
+
|
|
10
|
+
## 0.1.2 (2026-08-15)
|
|
11
|
+
|
|
12
|
+
Four things published OCaml writes that the grammar did not read, all found by
|
|
13
|
+
running `ocamlc -stop-after parsing` over the same files and keeping only the
|
|
14
|
+
disagreements.
|
|
15
|
+
|
|
16
|
+
An attribute may stand on either side of a record field's semicolon, which is
|
|
17
|
+
how every ppx-derived binding is written. `new` names a class and is a value, so
|
|
18
|
+
what follows it are that class's arguments. An operator may open with a hash,
|
|
19
|
+
which is where js_of_ocaml's `o##m` and `o##.f` come from. And an object type
|
|
20
|
+
may end `; ..`, or lend its methods from another object type named in place of
|
|
21
|
+
a field.
|
|
22
|
+
|
|
23
|
+
## 0.1.1 (2026-08-06)
|
|
24
|
+
|
|
25
|
+
The repository link, which npm renders as a link and a browser cannot follow
|
|
26
|
+
with `git+` in front of it.
|
|
27
|
+
|
|
28
|
+
## 0.1.0 (2026-08-05)
|
|
29
|
+
|
|
30
|
+
First release.
|
|
31
|
+
|
|
32
|
+
A Lezer grammar for OCaml, with highlighting, indentation, folding and
|
|
33
|
+
completion for the standard library. Comments nest and hold strings and
|
|
34
|
+
characters, an apostrophe is sorted into a type variable, a character or the end
|
|
35
|
+
of a name, the semicolon that ends a sequence is told from the one that
|
|
36
|
+
separates, and a capitalised word is told from a module by the dot after it.
|
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,170 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @exercism/codemirror-lang-ocaml
|
|
2
2
|
|
|
3
|
-
This
|
|
3
|
+
This is Exercism's fork of
|
|
4
|
+
[@codincod/codemirror-lang-ocaml](https://codeberg.org/reeven/codemirror-lang-ocaml),
|
|
5
|
+
written by CodinCod. Exercism uses it in the OCaml editor on
|
|
6
|
+
[exercism.org](https://exercism.org). Our changes are on the `main` branch.
|
|
7
|
+
|
|
8
|
+
The fork adds four things:
|
|
9
|
+
|
|
10
|
+
- The parser puts the function being called into a `Callee` node, so `f` in
|
|
11
|
+
`f x` and `map` in `List.map f xs` are highlighted as function calls.
|
|
12
|
+
- Completion offers the names declared in the file that are in scope at the
|
|
13
|
+
cursor. That covers top-level values and functions, types, constructors,
|
|
14
|
+
record fields, exceptions, modules and `val` declarations, and also
|
|
15
|
+
parameters, the names bound by `let ... in`, and the variables bound by match
|
|
16
|
+
arms, `fun` and `for` loops. Inside a `module ... = struct ... end`, it offers
|
|
17
|
+
that module's own definitions too.
|
|
18
|
+
- Completion offers snippets for `let`, `let rec`, `let ... in`, `match`,
|
|
19
|
+
`function`, `fun`, `if`, `try`, `for`, `while`, variant and record types,
|
|
20
|
+
modules, module types, exceptions and `List.fold_left`. It offers nothing
|
|
21
|
+
inside strings and comments.
|
|
22
|
+
- A definition that runs over several lines folds from the end of its first
|
|
23
|
+
line, and loops and `begin ... end` fold between their keywords.
|
|
24
|
+
|
|
25
|
+
## Install
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
npm install @exercism/codemirror-lang-ocaml
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
The rest of this README is upstream's.
|
|
32
|
+
|
|
33
|
+
This package implements [OCaml](https://ocaml.org/) language support for the
|
|
34
|
+
[CodeMirror](https://codemirror.net/) code editor: a
|
|
35
|
+
[Lezer](https://lezer.codemirror.net/) grammar, highlighting, indentation,
|
|
36
|
+
folding and completion for the standard library.
|
|
37
|
+
|
|
38
|
+
The grammar is written from scratch. OCaml has never had a Lezer grammar, and
|
|
39
|
+
what a CodeMirror 6 editor could reach for until now was the `mllike` stream
|
|
40
|
+
mode in `@codemirror/legacy-modes`, which reads one keyword list for OCaml,
|
|
41
|
+
F# and Standard ML and has no idea where a definition ends.
|
|
42
|
+
|
|
43
|
+
Written in part for [CodinCod](https://codincod.com/), a competitive coding
|
|
44
|
+
platform, where it colours the editor people solve puzzles in.
|
|
45
|
+
|
|
46
|
+
This code is released under an
|
|
47
|
+
[MIT license](https://codeberg.org/reeven/codemirror-lang-ocaml/src/branch/master/LICENSE).
|
|
48
|
+
|
|
49
|
+
## Usage
|
|
50
|
+
|
|
51
|
+
```javascript
|
|
52
|
+
import {EditorView, basicSetup} from "codemirror"
|
|
53
|
+
import {ocaml} from "@exercism/codemirror-lang-ocaml"
|
|
54
|
+
|
|
55
|
+
const view = new EditorView({
|
|
56
|
+
parent: document.body,
|
|
57
|
+
doc: `type shape =
|
|
58
|
+
| Circle of float
|
|
59
|
+
| Rect of {width : float; height : float}
|
|
60
|
+
|
|
61
|
+
let area = function
|
|
62
|
+
| Circle r -> Float.pi *. r *. r
|
|
63
|
+
| Rect {width; height} -> width *. height
|
|
64
|
+
|
|
65
|
+
let () =
|
|
66
|
+
[Circle 1.0; Rect {width = 2.0; height = 3.0}]
|
|
67
|
+
|> List.map area
|
|
68
|
+
|> List.iter (Printf.printf "%.2f\\n")
|
|
69
|
+
`,
|
|
70
|
+
extensions: [basicSetup, ocaml()]
|
|
71
|
+
})
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## What it reads
|
|
75
|
+
|
|
76
|
+
Measured against the stream mode it replaces, over 1011 files of OCaml from
|
|
77
|
+
CodinCod's language-guessing pool: the stream mode carries a highlighting tag on
|
|
78
|
+
**90.34%** of the text in 8 colours, with 54.99% of it in the busiest one. This
|
|
79
|
+
package tags **97.76%** in 15 colours, with 37.22% in the busiest. Over the same
|
|
80
|
+
1011 files, **88.03%** parse with no error node.
|
|
81
|
+
|
|
82
|
+
Over the OCaml compiler and standard library, Jane Street's `core` and the dune
|
|
83
|
+
build system, 2996 files and 17.98 MiB, **99.00%** parse with no error node.
|
|
84
|
+
|
|
85
|
+
Over 20000 files of published OCaml, 144 MiB out of a thousand repositories,
|
|
86
|
+
**98.49%**. The denominator there is the compiler's: `ocamlc -stop-after
|
|
87
|
+
parsing` turns down 1137 of those files, most of them HOL Light and camlp4
|
|
88
|
+
writing terms between backticks, which is a preprocessor's syntax rather than
|
|
89
|
+
the language's. Of the 18863 files it does parse, 285 still hold an error node,
|
|
90
|
+
and every one of those is a thing to fix.
|
|
91
|
+
|
|
92
|
+
Four things took most of the work.
|
|
93
|
+
|
|
94
|
+
* **A comment holds a language.** `(* (* *) *)` nests, so the depth has to be
|
|
95
|
+
counted rather than matched. A string inside a comment is read as a string
|
|
96
|
+
and a character literal as a character, which is OCaml's own rule and not a
|
|
97
|
+
nicety: `(* the "*)" case *)` and `(* '"' *)` both end where their author
|
|
98
|
+
meant them to, and a tokenizer that stopped at the first star-paren would
|
|
99
|
+
colour the rest of the file as a comment.
|
|
100
|
+
* **An apostrophe is three things.** `'a` is a type variable, `'a'` is a
|
|
101
|
+
character, and `x'` is an ordinary name. The name is read first so the quote
|
|
102
|
+
in it never reaches the tokenizer that would open a literal, the character is
|
|
103
|
+
tried next because it is the only one with a closing quote, and what is left
|
|
104
|
+
is a type variable. OCaml lexes the quote and the name as two tokens, so
|
|
105
|
+
`' a` is one variable and is read as one.
|
|
106
|
+
* **The semicolon that ends a sequence.** `begin f (); g (); end` may dangle
|
|
107
|
+
its last separator, and so may a record, a list and the arm of a clause. The
|
|
108
|
+
grammar cannot see that the statement after the separator is missing until it
|
|
109
|
+
has read the token that closes the block, so the tokenizer decides: a
|
|
110
|
+
semicolon standing in front of a closer, or in front of a word that cannot
|
|
111
|
+
open an expression, is a different token from the one that separates. Spelling
|
|
112
|
+
it in the grammar instead cost two and a half points of the corpus, because
|
|
113
|
+
the parser weighed an empty statement against the bar that ends a clause and
|
|
114
|
+
got the bar wrong in every file that wrote one.
|
|
115
|
+
* **A capitalised word is a module or a constructor.** OCaml spells the two the
|
|
116
|
+
same, and the dot that follows one of them is the whole of the difference.
|
|
117
|
+
That dot arrives too late for the grammar, so the tokenizer reads it: a
|
|
118
|
+
capitalised word with a dot against it is a module segment and can be nothing
|
|
119
|
+
else. `Some x` is a constructor, `Set.empty` is a module, and a theme can
|
|
120
|
+
colour them apart.
|
|
121
|
+
|
|
122
|
+
Smaller things worth naming.
|
|
123
|
+
|
|
124
|
+
* **Operators are not declared.** `>>=`, `|>` and `+.` are ordinary names in
|
|
125
|
+
OCaml and a library may write its own, so they are classified by their first
|
|
126
|
+
character rather than listed. A leading `!`, `~` or `?` binds tighter than
|
|
127
|
+
application, which is what hands `f !r` what the reference holds rather than
|
|
128
|
+
applying `f` to an exclamation mark.
|
|
129
|
+
* **Binding operators are one word.** `let*` and `and+` are a keyword with an
|
|
130
|
+
operator against it, and reading the two apart would leave the grammar with a
|
|
131
|
+
`let` whose name was an operator. A percent is deliberately not one of the
|
|
132
|
+
characters they may be spelled with: `let%lwt` hands the binding to a
|
|
133
|
+
preprocessor and is a `let` with an extension after it.
|
|
134
|
+
* **Attributes and extensions are read whole.** `[@inline]`, `[@@deriving]`,
|
|
135
|
+
`[@@@warning]`, `[%blob]` and `[%%if]` reach the tree with their name apart
|
|
136
|
+
from their payload, and the payload keeps whatever colour the code inside it
|
|
137
|
+
earned. The name may be a reserved word, because the name comes first and
|
|
138
|
+
nothing else can stand there.
|
|
139
|
+
* **Quoted strings take no escapes.** `{|raw|}` and `{sql|select 1|sql}` end at
|
|
140
|
+
the delimiter they opened with and at nothing else.
|
|
141
|
+
|
|
142
|
+
## What it does not read
|
|
143
|
+
|
|
144
|
+
* **Labelled tuples.** `(~x, y)` as a pattern and `(x:int * y:string)` as a
|
|
145
|
+
type are OCaml 5.4 syntax that the compiler's own sources have started using.
|
|
146
|
+
The parenthesised form collides with the label a function type already puts in
|
|
147
|
+
the same place, and it is most of the gap in the 99.00% above.
|
|
148
|
+
* **An arm mixing a value pattern and an exception pattern.** `| None
|
|
149
|
+
| exception _ ->` is legal and rare. Either kind alone works, and so does
|
|
150
|
+
`| exception (A | B) ->`.
|
|
151
|
+
* **Indexing operators.** `let (.%()) map e = ...` defines one, and the name is
|
|
152
|
+
read as an operator in brackets rather than as the whole of `.%()`.
|
|
153
|
+
* **A record path that starts lowercase and continues capitalised.**
|
|
154
|
+
`{flag.Flag.Internal.count with ...}` opens a module halfway along a field
|
|
155
|
+
path.
|
|
156
|
+
* **`M.[...]` in a pattern.** A module opened for the length of a pattern works
|
|
157
|
+
with parentheses, and a record works with braces; the list form is the one
|
|
158
|
+
shape that would make every list pattern ambiguous with a constructor applied
|
|
159
|
+
to one.
|
|
160
|
+
* **A tree that groups `a :: b as x` and `a | b, c` the way the manual does.**
|
|
161
|
+
Both parse. OCaml binds `as` loosest and `|` looser than `,`; this grammar
|
|
162
|
+
binds the tuple loosest, which changes how the pattern nests and nothing else.
|
|
163
|
+
|
|
164
|
+
## Building and testing
|
|
165
|
+
|
|
166
|
+
```
|
|
167
|
+
npm install # installs the dependencies and builds the package into dist/
|
|
168
|
+
npm run build # regenerates the parser, then rebuilds the package
|
|
169
|
+
npm test # builds the package, then runs the tests
|
|
170
|
+
```
|