@exercism/codemirror-lang-rescript
v1.0.2
Published
ReScript language support for CodeMirror, with a Lezer grammar and autocompletion
Readme
rescript-lezer
Exercism's fork
This is Exercism's fork of tsnobip/rescript-lezer, by tsnobip.
Exercism uses it in the ReScript editor on exercism.org.
It is published on npm as @exercism/codemirror-lang-rescript.
Our changes are on the exercism branch, and the main branch follows upstream.
Below this section, the README is upstream's.
The package keeps upstream's parser export and adds CodeMirror 6 support.
rescript() gives highlighting, indentation, folding and completion. Completion offers:
- keywords, global functions such as
ignore, the standard library's module names, and constructors such asSomeandOk. - snippets for
let, functions,switch,ifandif/else, record and variant types,module,for,whileandtry. - the functions in each standard library module after its name and a dot, as in
Array.,String.,List.,Option.,Int.andBelt.Map.Int.. Each one shows its type. Afteropen Belt,Array.offers Belt.Array's functions too. - the names defined in the file (bindings, parameters, pattern variables, types, constructors and modules), where they are in scope. After a module defined in the file and a dot, it offers that module's names.
- built-in and file-defined types inside a type annotation.
src/builtins.js lists the standard library's functions. scripts/generate-builtins.mjs writes it from the interface files in the @rescript/runtime package. To update it, install that package without saving it, then run:
npm install --no-save @rescript/runtime@12
npm run build
npm run builtins -- node_modules/@rescript/runtime/lib/ocamlThe fork also changes the grammar so that it parses all of the ReScript track's example solutions:
%between two expressions is the remainder operator, as in ReScript 12.- Value signatures such as
let keep: array<int> => array<int>can appear at the top level. Exercism's editor uses one language for both.resand.resifiles. - In
list{first, ...rest},restis a binding.
Install and use
To install it, run:
npm install @exercism/codemirror-lang-rescriptThen add rescript() to the editor's extensions:
import {rescript} from "@exercism/codemirror-lang-rescript"
new EditorView({extensions: [basicSetup, rescript()], parent: document.body})Building and testing
Run npm install, which also builds the package into dist/. Run npm run build to rebuild it and npm test to run the tests.
.github/workflows/test.yml runs npm test.
To run upstream's grammar audit, described below, first run npm install --no-save @rescript/runtime@12.
Releasing
To release from the exercism branch, set version in package.json and push a tag of the same version:
npm version patch # or minor / major
git push origin HEAD --follow-tagsnpm version updates the package and lockfile, commits the change, and creates the version tag. The tag starts .github/workflows/publish.yml. The workflow checks that the tag matches package.json, builds and tests the package, and publishes it to npm as @exercism/codemirror-lang-rescript, using the exercism organisation's NPM_TOKEN secret.
Contributing
Contributions to this fork follow the Exercism Code of Conduct. Please read about how to contribute to Exercism.
This is a ReScript grammar for the lezer parser system.
The top option can be set to "SingleExpression" to parse an expression instead of a
full program.
Grammar Audit
Use the audit script to find remaining grammar bugs against the official ReScript parser.
The compiler and corpus are pinned in the vendored/rescript Git submodule.
Initialize it once, then build the parser comparator with ReScript's OCaml/dune
dependencies installed (see its CONTRIBUTING.md):
git submodule update --init vendored/rescript
cd vendored/rescript
dune build compiler/bsc/rescript_compiler_main.exe
cd ../..Quick run
npm run build
npm run audit:grammarCorpus regression check
npm run test:corpusThis checks the syntax fixtures, runtime packages, and scripts in the pinned checkout. It also compares fresh and incremental trees across 1,000 reproducible edits. It fails on compiler-valid parse errors, incremental mismatches, or crashes. Compiler-rejected fixtures remain visible in the full differential audit.
GitHub Actions runs npm test and this corpus check on pushes and pull requests,
and saves the JSON audit report as the grammar-audit artifact.
Strict agreement
npm run audit:grammar:strictCustom run
node scripts/audit-grammar.mjs \
--mode all \
--max-files 10000 \
--fuzz-cases 500 \
--incremental-cases 300 \
--include-idempotency \
--report /tmp/rescript-lezer-audit.jsonWhat it checks
- Differential parse agreement vs ReScript (
-only-parse) on upstream corpus files - Mutation fuzzing with mismatch detection
- Incremental parse consistency (incremental tree vs fresh parse)
--corpus adds directories to the default corpus. Reports include error positions
and the full source and edit for reproducing fuzz and incremental failures. Use a fixed
--seed to reproduce edits, and --rescript-root or RESCRIPT_ROOT to compare
against another compiler checkout.
Publishing
Version tags (v0.9.1, for example) trigger .github/workflows/publish.yml.
The workflow runs the full test suite and corpus check, verifies that the tag
matches package.json, then builds and publishes to npm. Only stable versions
are supported.
Before the first automated release, push the workflow and configure a
Trusted Publisher in the npm settings
for @tsnobip/rescript-lezer:
- GitHub user:
tsnobip - Repository:
rescript-lezer - Workflow filename:
publish.yml - Allow direct publishing with
npm publish
Publishing uses OIDC, so no npm token secret is needed. To release from a clean checkout of the release branch:
npm version patch # or minor / major
git push origin HEAD --follow-tagsnpm version updates the package and lockfile, commits the change, and creates
the version tag. Failed workflow runs can be rerun from GitHub Actions.
The code is licensed under an MIT license.
