Repository navigation
Description
Summary
Keep one registry in the compiler of the language's user-facing syntax elements: decorators, extension points, keywords, operators, and the special values and built-in functions in Pervasives. Export it with every release as a machine-readable syntax.json, the way the API docs are exported today.
The main use is diffing syntax.json between two compiler versions. The diff lists what was added, deprecated or removed, and becomes the task list for updating the docs. A maintainer, or an agent, then adds or adapts the website's syntax lookup pages and their examples. A new decorator in a minor release then reaches the docs without anyone having to remember it.
Today
The knowledge of which syntax exists is spread across the codebase:
| Element | Where it lives today |
|---|---|
| Decorators | analysis/src/completion_decorators.ml has a hand-curated list of 41 decorators with docstrings, used for editor completion. Handling is spread across the frontend, for example compiler/typing/ast_attributes.ml, and usage is tracked dynamically through Used_attributes, so there is no static list of the decorators the compiler actually handles. |
| Extension points | compiler/frontend/ast_exp_extension.ml handles raw, ffi and todo. Others are handled in the parser and elsewhere. Removed extensions (%re, %debugger) only exist as ad hoc parser errors. |
| Keywords | The keyword table in syntax/printer/res_token.ml. Complete, but not exported and has no docs. |
| Operators | Parser precedence logic and Pervasives externals. |
| Special values and built-in functions | Pervasives.res (__LOC__, __POS__, __MODULE__, debugger(), …), which already has docstrings. |
Without a single source, the docs drift in both directions. Compared with the compiler's completion list:
- Documented on the website, missing from completion:
@tagand@ignore, which the compiler handles. Also@meth, for which code search finds no handling in the compiler at all. - In completion, missing from the website:
@instanceOf,@typeOf,@obj,@jsxConfig,@editor.completeFrom,@jsx.componentWithPropsand the@genType.as,@genType.importand@genType.opaquevariants. - Syntax that changed shape: 13.0.0-alpha.7 replaced
%debuggerwith thedebugger()function inPervasives(Replace the %debugger extension with a debugger() function #9163). The website kept documenting%debuggeruntil someone hit a compile error (v13 docs:%debuggerexamples no longer compile (removed in 13.0.0-alpha.7) rescript-lang.org#1487). Nothing told the docs that the extension point had become a built-in function.
Proposal
1. A syntax registry
Add one module that lists every syntax element. Sketch of an entry:
type category =
| Decorator | Extension_point | Keyword | Operator
| Special_value (* __LOC__, __MODULE__, … *)
| Builtin_function (* debugger(), … *)
type entry = {
id : string; (* "decorator/as", "extension/raw", "builtin/debugger" *)
category : category;
name : string; (* "@as", "%raw", "debugger()" *)
docstring : string; (* short Markdown description *)
since : string option; (* "11.0.0" *)
deprecated : (string * string) option; (* version, message *)
removed : (string * string) option; (* version, replacement hint *)
internal : bool;
}internaldecides whether an element is presented as language syntax, meaning whether it belongs in the syntax lookup. Examples of internal elements are attributes the parser and printer attach themselves (theres.*family), tool-specific escape hatches, andPervasivesfunctions that are ordinary library functions rather than syntax.Pervasivesis classified entry by entry. Every value it exports appears in the registry, and each must be marked as a special value, a built-in function or internal. Docstrings are taken fromPervasives.resinstead of being duplicated. This keeps former syntax likedebugger()in the lookup, keeps internal helpers out, and makes sure a newly addedPervasivesvalue can't go unclassified.removedkeeps entries after removal, so the diff shows "%debuggerremoved in 13.0, usedebugger()", and the docs can point readers to the replacement.
2. A consistency test
A test checks that:
- every decorator and extension point the frontend handles has a registry entry;
- every
Pervasivesvalue is classified; - every non-internal, non-removed entry has a docstring.
The test is the part that makes the manifest trustworthy. Without it the registry drifts like the existing lists, and the docs keep the same blind spots.
3. Export with every release
- Add a
rescript-tools syntax-manifestcommand that prints the registry as JSON. - Add
syntax.jsonto the API docs artifact that the publish workflow already commits to the website branch for each major version. The website can then diff the new manifest against the previous one, and report pages without an entry and public entries without a page.
Optional follow-ups
These build on the registry but aren't needed for the manifest:
- Editor completion and hover read decorators from the registry, replacing the list in
completion_decorators.ml. - The parser's errors for removed syntax take their messages from the
removedfield.
Steps
- Introduce the registry and move the decorators from
completion_decorators.mlinto it. Add@tagand@ignore, and decide whether@methis still supported. - Classify every
Pervasivesvalue. - Add keywords from
res_token.ml, the operators and the extension points, including removed ones. - Add the consistency test.
- Add
rescript-tools syntax-manifestand publishsyntax.jsonwith the API docs artifact.
Open questions
- How fine-grained should operators be? For example, one entry per operator or one per family, such as the arithmetic operators.
- Backfilling
since: do we fill it in for existing elements, or only track it from v13 on? - Language constructs: should constructs such as
...spread,~arg=?and regex literals live in the registry, or are they better left to the website?
Activity