ocaml-base-compiler
Legend:
Library
Module
Module type
Parameter
Class
Class type

Abstract syntax tree produced by parsing

Warning: this module is unstable and part of compiler-libs.

type constant =
  1. | Pconst_integer of string * char option
    (*

    Integer constants such as 3 3l 3L 3n.

    Suffixes [g-z][G-Z] are accepted by the parser. Suffixes except 'l', 'L' and 'n' are rejected by the typechecker

    *)
  2. | Pconst_char of char
    (*

    Character such as 'c'.

    *)
  3. | Pconst_string of string * Location.t * string option
    (*

    Constant string such as "constant" or {delim|other constant|delim}.

    The location span the content of the string, without the delimiters.

    *)
  4. | Pconst_float of string * char option
    (*

    Float constant such as 3.4, 2e5 or 1.4e-4.

    Suffixes g-zG-Z are accepted by the parser. Suffixes are rejected by the typechecker.

    *)
type location_stack = Location.t list

Extension points

type attribute = {
  1. attr_name : string Asttypes.loc;
  2. attr_payload : payload;
  3. attr_loc : Location.t;
}

Attributes such as [\@id ARG] and [\@\@id ARG].

Metadata containers passed around within the AST. The compiler ignores unknown attributes.

and extension = string Asttypes.loc * payload

Extension points such as [%id ARG] and [%%id ARG].

Sub-language placeholder -- rejected by the typechecker.

and attributes = attribute list
and payload =
  1. | PStr of structure
  2. | PSig of signature
    (*

    : SIG in an attribute or an extension point

    *)
  3. | PTyp of core_type
    (*

    : T in an attribute or an extension point

    *)
  4. | PPat of pattern * expression option
    (*

    ? P or ? P when E, in an attribute or an extension point

    *)

Core language

Type expressions

and core_type = {
  1. ptyp_desc : core_type_desc;
  2. ptyp_loc : Location.t;
  3. ptyp_loc_stack : location_stack;
  4. ptyp_attributes : attributes;
    (*

    ... [\@id1] [\@id2]

    *)
}
and core_type_desc =
  1. | Ptyp_any
    (*

    _

    *)
  2. | Ptyp_var of string
    (*

    A type variable such as 'a

    *)
  3. | Ptyp_arrow of Asttypes.arg_label * core_type * core_type
    (*

    Ptyp_arrow(lbl, T1, T2) represents:

    *)
  4. | Ptyp_tuple of core_type list
    (*

    Ptyp_tuple([T1 ; ... ; Tn]) represents a product type T1 * ... * Tn.

    Invariant: n >= 2.

    *)
  5. | Ptyp_constr of Longident.t Asttypes.loc * core_type list
    (*

    Ptyp_constr(lident, l) represents:

    • tconstr when l=[],
    • T tconstr when l=[T],
    • (T1, ..., Tn) tconstr when l=[T1 ; ... ; Tn].
    *)
  6. | Ptyp_object of object_field list * Asttypes.closed_flag
    (*

    Ptyp_object([ l1:T1; ...; ln:Tn ], flag) represents:

    • < l1:T1; ...; ln:Tn > when flag is Closed,
    • < l1:T1; ...; ln:Tn; .. > when flag is Open.
    *)
  7. | Ptyp_class of Longident.t Asttypes.loc * core_type list
    (*

    Ptyp_class(tconstr, l) represents:

    • #tconstr when l=[],
    • T #tconstr when l=[T],
    • (T1, ..., Tn) #tconstr when l=[T1 ; ... ; Tn].
    *)
  8. | Ptyp_alias of core_type * string
    (*

    T as 'a.

    *)
  9. | Ptyp_variant of row_field list * Asttypes.closed_flag * Asttypes.label list option
    (*

    Ptyp_variant([`A;`B], flag, labels) represents:

    • [ `A|`B ] when flag is Closed, and labels is None,
    • [> `A|`B ] when flag is Open, and labels is None,
    • [< `A|`B ] when flag is Closed, and labels is Some [],
    • [< `A|`B > `X `Y ] when flag is Closed, and labels is Some ["X";"Y"].
    *)
  10. | Ptyp_poly of string Asttypes.loc list * core_type
    (*

    'a1 ... 'an. T

    Can only appear in the following context:

    • As the core_type of a Ppat_constraint node corresponding to a constraint on a let-binding:

      let x : 'a1 ... 'an. T = e ...
    *)
  11. | Ptyp_package of package_type
    (*

    (module S).

    *)
  12. | Ptyp_extension of extension
    (*

    [%id].

    *)

As package_type typed values:

  • (S, []) represents (module S),
  • (S, [(t1, T1) ; ... ; (tn, Tn)]) represents (module S with type t1 = T1 and ... and tn = Tn).
and row_field = {
  1. prf_desc : row_field_desc;
  2. prf_loc : Location.t;
  3. prf_attributes : attributes;
}
and row_field_desc =
  1. | Rtag of Asttypes.label Asttypes.loc * bool * core_type list
    (*

    Rtag(`A, b, l) represents:

    • `A when b is true and l is [],
    • `A of T when b is false and l is [T],
    • `A of T1 & .. & Tn when b is false and l is [T1;...Tn],
    • `A of & T1 & .. & Tn when b is true and l is [T1;...Tn].
    • The bool field is true if the tag contains a constant (empty) constructor.
    • & occurs when several types are used for the same constructor (see 4.2 in the manual)
    *)
  2. | Rinherit of core_type
    (*

    [ | t ]

    *)
and object_field = {
  1. pof_desc : object_field_desc;
  2. pof_loc : Location.t;
  3. pof_attributes : attributes;
}
and object_field_desc =
  1. | Otag of Asttypes.label Asttypes.loc * core_type
  2. | Oinherit of core_type

Patterns

and pattern = {
  1. ppat_desc : pattern_desc;
  2. ppat_loc : Location.t;
  3. ppat_loc_stack : location_stack;
  4. ppat_attributes : attributes;
    (*

    ... [\@id1] [\@id2]

    *)
}
and pattern_desc =
  1. | Ppat_any
    (*

    The pattern _.

    *)
  2. | Ppat_var of string Asttypes.loc
    (*

    A variable pattern such as x

    *)
  3. | Ppat_alias of pattern * string Asttypes.loc
    (*

    An alias pattern such as P as 'a

    *)
  4. | Ppat_constant of constant
    (*

    Patterns such as 1, 'a', "true", 1.0, 1l, 1L, 1n

    *)
  5. | Ppat_interval of constant * constant
    (*

    Patterns such as 'a'..'z'.

    Other forms of interval are recognized by the parser but rejected by the type-checker.

    *)
  6. | Ppat_tuple of pattern list
    (*

    Patterns (P1, ..., Pn).

    Invariant: n >= 2

    *)
  7. | Ppat_construct of Longident.t Asttypes.loc * (string Asttypes.loc list * pattern) option
    (*

    Ppat_construct(C, args) represents:

    • C when args is None,
    • C P when args is Some ([], P)
    • C (P1, ..., Pn) when args is Some ([], Ppat_tuple [P1; ...; Pn])
    • C (type a b) P when args is Some ([a; b], P)
    *)
  8. | Ppat_variant of Asttypes.label * pattern option
    (*

    Ppat_variant(`A, pat) represents:

    • `A when pat is None,
    • `A P when pat is Some P
    *)
  9. | Ppat_record of (Longident.t Asttypes.loc * pattern) list * Asttypes.closed_flag
    (*

    Ppat_record([(l1, P1) ; ... ; (ln, Pn)], flag) represents:

    • { l1=P1; ...; ln=Pn } when flag is Closed
    • { l1=P1; ...; ln=Pn; _} when flag is Open

    Invariant: n > 0

    *)
  10. | Ppat_array of pattern list
    (*

    Pattern [| P1; ...; Pn |]