@exercism/codemirror-lang-ocaml 0.0.0-stage → 1.0.1

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