Skip to main content

Module ocaml

Module ocaml 

Source
Expand description

Rendering the OCaml CST. Spec §5.

One style, the one ocamlformat produces with its defaults: two spaces per level, a leading | on every variant constructor and every match arm, (** … *) for documentation.

Two things are decided here that are not decided in the sibling renderers, and both are lexical:

  1. A comment’s text is sanitised. OCaml comments nest and contain string literals: the manual’s “Lexical conventions” require a comment to hold well-formed strings, and ocamlc reports This comment contains an unterminated string literal for a (* … " … *). A *) inside a comment closes it early and a (* opens a nested one that then never closes. None of those is a hazard in C, Go, Rust or Java, and a documentation comment here is assembled from IDL text. comment_safe is where the three are neutralised.
  2. Types are postfix and left-associative. int32 array option is an option of an array; int32 option array is an array of options. Nesting therefore needs no brackets at all — except around a tuple, which is why Type::Tuple always renders parenthesised.

Nothing else is decided. What is a group and what is a single declaration is settled in zerodds_lang_rules::adapter::ocaml, because it is a property of the type graph and not of the text.

Functions§

codec_chain
A chain of codec functions: let rec f … and g …. One chain, so a recursive or mutually recursive type’s codecs resolve whatever order the walk visited them in.
comment_safe
Text that cannot end, nest or unbalance the comment holding it.
decl
Render one declaration, for a caller that has only one.
file
Render a whole file.
ident
The identifier a name renders as, for a caller assembling one.
type_
Render one type, for a caller that has only one.
value_chain
A chain of value functions — the Table 9 defaulters.