Chapter 12 Language extensions

16 Extension-only syntax

(Introduced in OCaml 4.02.2, extended in 4.03)

Some syntactic constructions are accepted during parsing and rejected during type checking. These syntactic constructions can therefore not be used directly in vanilla OCaml. However, -ppx rewriters and other external tools can exploit this parser leniency to extend the language with these new syntactic constructions by rewriting them to vanilla constructions.

16.1 Extension operators

(Introduced in OCaml 4.02.2, extended to unary operators in OCaml 4.12.0)

infix-symbol::= ...
 # { operator-char } # { operator-char ∣ # }
 
prefix-symbol::= ...
  (? ∣ ~ ∣ !) { operator-char } # { operator-char ∣ # }
 

There are two classes of operators available for extensions: infix operators with a name starting with a # character and containing more than one # character, and unary operators with a name (starting with a ?, ~, or ! character) containing at least one # character.

For instance:

# let infix x y = x##y;;
Error: ## is not a valid value identifier.
# let prefix x = !#x;;
Error: !# is not a valid value identifier.

Note that both ## and !# must be eliminated by a ppx rewriter to make this example valid.

16.2 Extension literals

(Introduced in OCaml 4.03)

float-literal::= ...
  [-] (09) { 09 ∣ _ } [. { 09 ∣ _ }] [(e ∣ E) [+ ∣ -] (09) { 09 ∣ _ }] [gz ∣ GZ]
  [-] (0x ∣ 0X) (09 ∣ AF ∣ af) { 09 ∣ AF ∣ af ∣ _ } [. { 09 ∣ AF ∣ af ∣ _ }] [(p ∣ P) [+ ∣ -] (09) { 09 ∣ _ }] [gz ∣ GZ]
 
int-literal::= ...
  [-] (09) { 09 ∣ _ }[gz ∣ GZ]
  [-] (0x ∣ 0X) (09 ∣ AF ∣ af) { 09 ∣ AF ∣ af ∣ _ } [gz ∣ GZ]
  [-] (0o ∣ 0O) (07) { 07 ∣ _ } [gz ∣ GZ]
  [-] (0b ∣ 0B) (01) { 01 ∣ _ } [gz ∣ GZ]
 

Int and float literals followed by an one-letter identifier in the range [g..zG..Z] are extension-only literals.