Module Ppxlib__.Ast_pattern

First class AST patterns

PPX rewriters often need to recognize fragments the OCaml AST, for instance to parse the payload of an attribute/expression. You can do that with a pattern matching and manual error reporting when the input is not what you expect but this has proven to quickly become extremely verbose and unreadable.

This module aims to help with that by providing first class AST patterns.

To understand how to use it, let's consider the example of ppx_inline_test. We want to recognize patterns of the form:

      let%test "name" = expr

Which is a syntactic sugar for:

      [%%test let "name" = expr]

If we wanted to write a function that recognizes the payload of %%test using normal pattern matching we would write:

      let match_payload = function
        | Pstr [ { pstr_desc = Pstr_value (Nonrecursive,
                                           [ { pvb_pat = Ppat_constant (Constant_string
                                                                          (name, None))
                                             ; pvb_expr = e
                                             ; _ } ])
                 ; _ } ] ->
          (name, e)
        | _ -> Location.raisef ...

This is quite cumbersome, and this is still not right: this function drops all attributes without notice.

Now let's imagine we wanted to construct the payload instead, using Ast_builder one would write:

      let build_payload ~loc name expr =
        let (module B) = Ast_builder.with_loc loc in
        let open B in
        pstr [ pstr_value Nonrecursive (value_binding ~pat:(pstring name) ~expr) ]

Constructing a first class pattern is almost as simple as replacing Ast_builder by Ast_pattern:

      let payload_pattern name expr =
        let open Ast_pattern in
        pstr (pstr_value nonrecursive (value_binding ~pat:(pstring __) ~expr:__) ^:: nil)

Notice that the place-holders for name and expr have been replaced by __. The following pattern with have type:

(payload, string -> expression -> 'a, 'a) Ast_pattern.t

which means that it matches values of type payload and captures a string and expression from it. The two captured elements comes from the use of __.

type ('a, 'b, 'c) t = ('a'b'cPpxlib__.Ast_pattern0.t

Type of a pattern:

val parse : ('a'b'ct ‑> Ppxlib.Location.t ‑> ?⁠on_error:(Ppxlib__.Import.unit ‑> 'c) ‑> 'a ‑> 'b ‑> 'c

Matches a value against a pattern.

module Packed : sig ... end with type (a, b, c) pattern := (a, b, c) t
val __ : ('a'a ‑> 'b'bt

Pattern that captures its input.

val __' : ('a'a Ppxlib.Loc.t ‑> 'b'bt

Same as __ but also captures the location.

Note: this should only be used for types that do not embed a location. For instance you can use it to capture a string constant:

      estring __'

but using it to capture an expression would not yield the expected result:

      pair (eint (int 42)) __'

In the latter case you should use the pexp_loc field of the captured expression instead.

val alt : ('a'b'ct ‑> ('a'b'ct ‑> ('a'b'ct

alt stands for `alternatives'. It matches either the first pattern or the second one.

val alt_option : ('a'v ‑> 'b'ct ‑> ('a'b'ct ‑> ('a'v Ppxlib__.Import.option ‑> 'b'ct

Same as alt, for the common case where the left-hand-side captures a value but not the right-hand-side.

val (|||) : ('a'b'ct ‑> ('a'b'ct ‑> ('a'b'ct

Same as alt

val map : ('a'b'ct ‑> f:('d ‑> 'b) ‑> ('a'd'ct
val map' : ('a'b'ct ‑> f:(Ppxlib.Location.t ‑> 'd ‑> 'b) ‑> ('a'd'ct
val map_result : ('a'b'ct ‑> f:('c ‑> 'd) ‑> ('a'b'dt
val (>>|) : ('a'b'ct ‑> ('d ‑> 'b) ‑> ('a'd'ct

Same as map

val map0 : ('a'b'ct ‑> f:'v ‑> ('a'v ‑> 'b'ct
val map1 : ('a'v1 ‑> 'b'ct ‑> f:('v1 ‑> 'v) ‑> ('a'v ‑> 'b'ct
val map2 : ('a'v1 ‑> 'v2 ‑> 'b'ct ‑> f:('v1 ‑> 'v2 ‑> 'v) ‑> ('a'v ‑> 'b'ct
val map0' : ('a'b'ct ‑> f:(Ppxlib.Location.t ‑> 'v) ‑> ('a'v ‑> 'b'ct
val map1' : ('a'v1 ‑> 'b'ct ‑> f:(Ppxlib.Location.t ‑> 'v1 ‑> 'v) ‑> ('a'v ‑> 'b'ct
val map2' : ('a'v1 ‑> 'v2 ‑> 'b'ct ‑> f:(Ppxlib.Location.t ‑> 'v1 ‑> 'v2 ‑> 'v) ‑> ('a'v ‑> 'b'ct
val nil : (_ Ppxlib__.Import.list'a'at
val (^::) : ('a'b'ct ‑> ('a Ppxlib__.Import.list'c'dt ‑> ('a Ppxlib__.Import.list'b'dt
val many : ('a'b ‑> 'b'ct ‑> ('a Ppxlib__.Import.list'c Ppxlib__.Import.list ‑> 'd'dt
val int : Ppxlib__.Import.int ‑> (Ppxlib__.Import.int'a'at
val char : Ppxlib__.Import.char ‑> (Ppxlib__.Import.char'a'at
val string : Ppxlib__.Import.string ‑> (Ppxlib__.Import.string'a'at
val float : Ppxlib__.Import.float ‑> (Ppxlib__.Import.float'a'at
val int32 : Ppxlib__.Import.int32 ‑> (Ppxlib__.Import.int32'a'at
val int64 : Ppxlib__.Import.int64 ‑> (Ppxlib__.Import.int64'a'at
val nativeint : Ppxlib__.Import.nativeint ‑> (Ppxlib__.Import.nativeint'a'at
val bool : Ppxlib__.Import.bool ‑> (Ppxlib__.Import.bool'a'at
val cst : to_string:('a ‑> Ppxlib__.Import.string) ‑> ?⁠equal:('a ‑> 'a ‑> Ppxlib__.Import.bool) ‑> 'a ‑> ('a'b'bt
val none : (_ Ppxlib__.Import.option'a'at
val some : ('a'b'ct ‑> ('a Ppxlib__.Import.option'b'ct
val pair : ('a1'b'ct ‑> ('a2'c'dt ‑> ('a1 * 'a2'b'dt
val (**) : ('a1'b'ct ‑> ('a2'c'dt ‑> ('a1 * 'a2'b'dt
val triple : ('a1'b'ct ‑> ('a2'c'dt ‑> ('a3'd'et ‑> ('a1 * 'a2 * 'a3'b'et
val loc : ('a'b'ct ‑> ('a Ppxlib.Loc.t'b'ct
val pack0 : ('a'b'ct ‑> ('aPpxlib__.Import.unit ‑> 'b'ct
val pack2 : ('a'b ‑> 'c ‑> 'd'et ‑> ('a, ('b * 'c) ‑> 'd'et
val pack3 : ('a'b ‑> 'c ‑> 'd ‑> 'e'ft ‑> ('a, ('b * 'c * 'd) ‑> 'e'ft

AST patterns for each constructur/record of the parsetree are generated in the same way AST builders are generated. In addition, for every wrapper we generate a pattern to match the loc and attributes fields. For instanct for the expression type:

      val pexp_loc
        :  (Location.t, 'a, 'b) t
        -> (expression, 'b, 'c) t
        -> (expression, 'a, 'c) t

      val pexp_attributes
        :  (attributes, 'a, 'b) t
        -> (expression, 'b, 'c) t
        -> (expression, 'a, 'c) t
include module type of Ppxlib__.Ast_pattern_generated
val labelled : (string, 'a'bPpxlib__.Ast_pattern0.t ‑> (Ppxlib__.Import.arg_label'a'bPpxlib__.Ast_pattern0.t
val optional : (string, 'a'bPpxlib__.Ast_pattern0.t ‑> (Ppxlib__.Import.arg_label'a'bPpxlib__.Ast_pattern0.t
val class_infos : virt:(Ppxlib__.Import.virtual_flag'a'bPpxlib__.Ast_pattern0.t ‑> params:((Ppxlib__.Import.core_type * Ppxlib__.Import.variance) list, 'b'cPpxlib__.Ast_pattern0.t ‑> name:(string, 'c'dPpxlib__.Ast_pattern0.t ‑> expr:('e'd'fPpxlib__.Ast_pattern0.t ‑> ('e Ppxlib__.Import.class_infos'a'fPpxlib__.Ast_pattern0.t
val pconst_integer : (string, 'a'bPpxlib__.Ast_pattern0.t ‑> (char option, 'b'cPpxlib__.Ast_pattern0.t ‑> (Ppxlib__.Import.constant'a'cPpxlib__.Ast_pattern0.t
val pconst_char : (char, 'a'bPpxlib__.Ast_pattern0.t ‑> (Ppxlib__.Import.constant'a'bPpxlib__.Ast_pattern0.t
val pconst_string : (string, 'a'bPpxlib__.Ast_pattern0.t ‑> (string option, 'b'cPpxlib__.Ast_pattern0.t ‑> (Ppxlib__.Import.constant'a'cPpxlib__.Ast_pattern0.t
val pconst_float : (string, 'a'bPpxlib__.Ast_pattern0.t ‑> (char option, 'b'cPpxlib__.Ast_pattern0.t ‑> (Ppxlib__.Import.constant'a'cPpxlib__.Ast_pattern0.t
val constructor_declaration : name:(string, 'a'bPpxlib__.Ast_pattern0.t ‑> args:(Ppxlib__.Import.constructor_arguments'b'cPpxlib__.Ast_pattern0.t ‑> res:(Ppxlib__.Import.core_type option, 'c'dPpxlib__.Ast_pattern0.t ‑> (Ppxlib__.Import.constructor_declaration'a'dPpxlib__.Ast_pattern0.t
val ptyp_var : (string, 'a'bPpxlib__.Ast_pattern0.t ‑> (Ppxlib__.Import.core_type'a'bPpxlib__.Ast_pattern0.t
val pdir_string : (string, 'a'bPpxlib__.Ast_pattern0.t ‑> (Ppxlib__.Import.directive_argument'a'bPpxlib__.Ast_pattern0.t
val pdir_int : (string, 'a'bPpxlib__.Ast_pattern0.t ‑> (char option, 'b'cPpxlib__.Ast_pattern0.t ‑> (Ppxlib__.Import.directive_argument'a'cPpxlib__.Ast_pattern0.t
val pdir_bool : (bool, 'a'bPpxlib__.Ast_pattern0.t ‑> (Ppxlib__.Import.directive_argument'a'bPpxlib__.Ast_pattern0.t
val pexp_setinstvar : (string, 'a'bPpxlib__.Ast_pattern0.t ‑> (Ppxlib__.Import.expression'b'cPpxlib__.Ast_pattern0.t ‑> (Ppxlib__.Import.expression'a'cPpxlib__.Ast_pattern0.t
val pexp_unreachable : (Ppxlib__.Import.expression'a'aPpxlib__.Ast_pattern0.t
val include_infos : mod_:('a'b'cPpxlib__.Ast_pattern0.t ‑> ('a Ppxlib__.Import.include_infos'b'cPpxlib__.Ast_pattern0.t
val label_declaration : name:(string, 'a'bPpxlib__.Ast_pattern0.t ‑> mutable_:(Ppxlib__.Import.mutable_flag'b'cPpxlib__.Ast_pattern0.t ‑> type_:(Ppxlib__.Import.core_type'c'dPpxlib__.Ast_pattern0.t ‑> (Ppxlib__.Import.label_declaration'a'dPpxlib__.Ast_pattern0.t
val location : start:(Ppxlib__.Import.position'a'bPpxlib__.Ast_pattern0.t ‑> end_:(Ppxlib__.Import.position'b'cPpxlib__.Ast_pattern0.t ‑> ghost:(bool, 'c'dPpxlib__.Ast_pattern0.t ‑> (Ppxlib__.Import.location'a'dPpxlib__.Ast_pattern0.t
val lident : (string, 'a'bPpxlib__.Ast_pattern0.t ‑> (Ppxlib__.Import.longident'a'bPpxlib__.Ast_pattern0.t
val module_binding : name:(string, 'a'bPpxlib__.Ast_pattern0.t ‑> expr:(Ppxlib__.Import.module_expr'b'cPpxlib__.Ast_pattern0.t ‑> (Ppxlib__.Import.module_binding'a'cPpxlib__.Ast_pattern0.t
val module_declaration : name:(string, 'a'bPpxlib__.Ast_pattern0.t ‑> type_:(Ppxlib__.Import.module_type'b'cPpxlib__.Ast_pattern0.t ‑> (Ppxlib__.Import.module_declaration'a'cPpxlib__.Ast_pattern0.t
val module_type_declaration : name:(string, 'a'bPpxlib__.Ast_pattern0.t ‑> type_:(Ppxlib__.Import.module_type option, 'b'cPpxlib__.Ast_pattern0.t ‑> (Ppxlib__.Import.module_type_declaration'a'cPpxlib__.Ast_pattern0.t
val ppat_var : (string, 'a'bPpxlib__.Ast_pattern0.t ‑> (Ppxlib__.Import.pattern'a'bPpxlib__.Ast_pattern0.t
val ppat_alias : (Ppxlib__.Import.pattern'a'bPpxlib__.Ast_pattern0.t ‑> (string, 'b'cPpxlib__.Ast_pattern0.t ‑> (Ppxlib__.Import.pattern'a'cPpxlib__.Ast_pattern0.t
val ppat_unpack : (string, 'a'bPpxlib__.Ast_pattern0.t ‑> (Ppxlib__.Import.pattern'a'bPpxlib__.Ast_pattern0.t
val position : fname:(string, 'a'bPpxlib__.Ast_pattern0.t ‑> lnum:(int, 'b'cPpxlib__.Ast_pattern0.t ‑> bol:(int, 'c'dPpxlib__.Ast_pattern0.t ‑> cnum:(int, 'd'ePpxlib__.Ast_pattern0.t ‑> (Ppxlib__.Import.position'a'ePpxlib__.Ast_pattern0.t
val nonrecursive : (Ppxlib__.Import.rec_flag'a'aPpxlib__.Ast_pattern0.t
val ptype_abstract : (Ppxlib__.Import.type_kind'a'aPpxlib__.Ast_pattern0.t
val value_description : name:(string, 'a'bPpxlib__.Ast_pattern0.t ‑> type_:(Ppxlib__.Import.core_type'b'cPpxlib__.Ast_pattern0.t ‑> prim:(string list, 'c'dPpxlib__.Ast_pattern0.t ‑> (Ppxlib__.Import.value_description'a'dPpxlib__.Ast_pattern0.t
val contravariant : (Ppxlib__.Import.variance'a'aPpxlib__.Ast_pattern0.t
val true_ : (Ppxlib__.Import.bool'a'at
val false_ : (Ppxlib__.Import.bool'a'at
val eint : (Ppxlib__.Import.int'a'bt ‑> (Ppxlib__.Import.expression'a'bt
val echar : (Ppxlib__.Import.char'a'bt ‑> (Ppxlib__.Import.expression'a'bt
val estring : (Ppxlib__.Import.string'a'bt ‑> (Ppxlib__.Import.expression'a'bt
val efloat : (Ppxlib__.Import.string'a'bt ‑> (Ppxlib__.Import.expression'a'bt
val eint32 : (Ppxlib__.Import.int32'a'bt ‑> (Ppxlib__.Import.expression'a'bt
val eint64 : (Ppxlib__.Import.int64'a'bt ‑> (Ppxlib__.Import.expression'a'bt
val enativeint : (Ppxlib__.Import.nativeint'a'bt ‑> (Ppxlib__.Import.expression'a'bt
val pint : (Ppxlib__.Import.int'a'bt ‑> (Ppxlib__.Import.pattern'a'bt
val pchar : (Ppxlib__.Import.char'a'bt ‑> (Ppxlib__.Import.pattern'a'bt
val pstring : (Ppxlib__.Import.string'a'bt ‑> (Ppxlib__.Import.pattern'a'bt
val pfloat : (Ppxlib__.Import.string'a'bt ‑> (Ppxlib__.Import.pattern'a'bt
val pint32 : (Ppxlib__.Import.int32'a'bt ‑> (Ppxlib__.Import.pattern'a'bt
val pint64 : (Ppxlib__.Import.int64'a'bt ‑> (Ppxlib__.Import.pattern'a'bt
val pnativeint : (Ppxlib__.Import.nativeint'a'bt ‑> (Ppxlib__.Import.pattern'a'bt
val single_expr_payload : (Ppxlib__.Import.expression'a'bt ‑> (Ppxlib__.Import.payload'a'bt
val no_label : (Ppxlib__.Import.expression'a'bt ‑> (Ppxlib__.Import.Asttypes.arg_label * Ppxlib__.Import.expression'a'bt
val attribute : (Ppxlib__.Import.string'a'bt ‑> (Ppxlib__.Import.payload'b'ct ‑> (Ppxlib__.Import.attribute'a'ct
val extension : (Ppxlib__.Import.string'a'bt ‑> (Ppxlib__.Import.payload'b'ct ‑> (Ppxlib__.Import.attribute'a'ct
val elist : (Ppxlib__.Import.expression'a ‑> 'a'bt ‑> (Ppxlib__.Import.expression'b Ppxlib__.Import.list ‑> 'c'ct
type context
val of_func : (context ‑> Ppxlib.Location.t ‑> 'a ‑> 'b ‑> 'c) ‑> ('a'b'ct
val to_func : ('a'b'ct ‑> context ‑> Ppxlib.Location.t ‑> 'a ‑> 'b ‑> 'c