This directory owns the parsetree, ReScript source parsing, comment
attachment, and printing, as three libraries of the rescript-syntax opam
package that depend on the standard library only and build with OCaml 4.14
or later: ast/ (rescript-syntax.ast, module Rescript_ast), printer/
(rescript-syntax.printer, module Rescript_printer) and parser/
(rescript-syntax.parser, module Rescript_parser), which uses the printer
for some error messages. The compiler opens these modules with -open flags
in its dune files, so its code uses the inner modules (Parsetree,
Res_printer, ...) directly. The parser is hand-written and produces the parsetree consumed by
compiler/typing. The built-in JSX transformation, which rewrites that
parsetree, lives in compiler/frontend.
parser/res_scanner.mltokenizes source text.parser/res_core.mlimplements the grammar: parsing and error recovery.parser/res_parser.mlholds the parser state: the token cursor, lookahead, breadcrumbs and diagnostics.parser/res_grammar.mldefines the grammar contexts (breadcrumbs) and the token classes that recovery uses to decide where a construct starts or ends.printer/res_comment.mlandprinter/res_comments_table.mlretain and attach comments for printing.printer/res_printer.ml,printer/res_doc.ml, andprinter/res_parens.mlimplement formatting.compiler/syntax_cli/res_cli.mlprovides the repository-onlyres_parserdiagnostic tool, with its AST, token and OCaml-syntax debug printers incompiler/syntax_cli/. Production compiler code calls the syntax library APIs.
See Formatter.md for formatter policy.
ParserCursor.md states the parser's token-cursor invariants (peek versus next, lookahead and try_parse rollback, pending comments and diagnostics); read it before changing Res_parser consumption or recovery.
Run commands from the repository root:
make # build the compiler and build system
make test-syntax # parser and printer tests
make test-syntax-roundtrip # parse/print round-trip tests
make checkformat # check repository formattingUse the repository diagnostic CLI to inspect one file:
dune exec res_parser -- example.res
dune exec res_parser -- -print tokens example.res
dune exec res_parser -- -print ast -recover example.res
dune exec res_parser -- -print comments example.res
dune exec res_parser -- -print doc example.res
dune exec res_parser -- -print ml example.res
dune exec res_parser -- -print res -width 80 example.resThe CLI is for compiler development and tests; it is not a supported public parser interface.
A syntax change can affect more than the grammar. Check each relevant layer:
- scanner tokens and parser recovery;
- the current parsetree in
syntax/ast/parsetree.ml; - printing, parentheses, and comment attachment;
- the v0 AST bridges in
compiler/ast0/ast_mapper_from0.mlandcompiler/ast0/ast_mapper_to0.ml; - type checking and every later compiler representation that carries the construct;
- parser, round-trip, type-error, and end-to-end tests.
Do not modify compiler/ast0/parsetree0.ml. It is the frozen input/output shape
for existing PPX integrations. When the current parsetree changes, define an
explicit compatibility mapping in both directions; do not use a wildcard to
discard a new construct.
Parser tests should cover valid input, recovery from invalid input, printing, and comment placement where applicable. Run the round-trip suite whenever a change affects parsing or printing, even when the intended AST is unchanged.
Public language behavior belongs on the ReScript website. This directory keeps
implementation-facing documentation needed to change the parser, printer, or
built-in transformations. Put API contracts in .mli files and local parsing
or printing invariants beside their implementation.