Skip to content

RFC: Syntax registry and machine-readable syntax manifest #9399

Open

Description

@fhammerschmidt

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:

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;
}
  • internal decides 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 (the res.* family), tool-specific escape hatches, and Pervasives functions that are ordinary library functions rather than syntax.
  • Pervasives is 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 from Pervasives.res instead of being duplicated. This keeps former syntax like debugger() in the lookup, keeps internal helpers out, and makes sure a newly added Pervasives value can't go unclassified.
  • removed keeps entries after removal, so the diff shows "%debugger removed in 13.0, use debugger()", 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 Pervasives value 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-manifest command that prints the registry as JSON.
  • Add syntax.json to 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 removed field.

Steps

  1. Introduce the registry and move the decorators from completion_decorators.ml into it. Add @tag and @ignore, and decide whether @meth is still supported.
  2. Classify every Pervasives value.
  3. Add keywords from res_token.ml, the operators and the extension points, including removed ones.
  4. Add the consistency test.
  5. Add rescript-tools syntax-manifest and publish syntax.json with 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?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions