package ppx_deriving

  1. Overview
  2. Docs

Arg contains convenience functions that extract constants from AST fragments, to be used when parsing options or [\@attributes] attached to types, fields or constructors.

The ~name argument is used in error messages and should receive the name of the deriving plugin, e.g. "show".

val expr : Parsetree.expression -> [> `Ok of Parsetree.expression ]

expr returns the input expression as-is.

val bool : Parsetree.expression -> [ `Ok of bool | `Error of string ]

bool expr extracts a boolean constant from expr, or returns `Error "boolean" if expr does not contain a boolean literal.

val int : Parsetree.expression -> [ `Ok of int | `Error of string ]

int expr extracts an integer constant from expr, or returns `Error "integer" if expr does not contain an integer literal.

val string : Parsetree.expression -> [ `Ok of string | `Error of string ]

string expr extracts a string constant from expr, or returns `Error "string" if expr does not contain a string literal.

val char : Parsetree.expression -> [ `Ok of char | `Error of string ]

char expr extracts a char constant from expr, or returns `Error "char" if expr does not contain a char literal.

val enum : string list -> Parsetree.expression -> [ `Ok of string | `Error of string ]

enum values expr extracts a polymorphic variant constant from expr, or returns `Error "one of: `a, `b, ..." if expr does not contain a polymorphic variant constructor included in values.

val list : (Parsetree.expression -> [ `Ok of 'a | `Error of string ]) -> Parsetree.expression -> [ `Ok of 'a list | `Error of string ]

list f expr extracts a list constant from expr and maps every element through f, or returns `Error "list:..." where ... is the error returned by f, or returns `Error "list" if expr does not contain a list.

val get_attr : deriver:string -> (Parsetree.expression -> [ `Ok of 'a | `Error of string ]) -> Parsetree.attribute option -> 'a option

get_attr ~deriver conv attr extracts the expression from attr and converts it with conv, raising Location.Error if attr is not a structure with a single expression or conv fails; or returns None if attr is None. The name of the deriving plugin should be passed as deriver; it is used in error messages.

Example usage:

let deriver = "index"
(* ... *)
  let kind =
    match Ppx_deriving.attr ~deriver "kind" pcd_attributes |>
          Ppx_deriving.Arg.(get_attr ~deriver (enum ["flat"; "nested"])) with
    | Some "flat" -> `flat | Some "nested" -> `nested | None -> `default
  in ..
val get_flag : deriver:string -> Parsetree.attribute option -> bool

get_flag ~deriver attr returns true if attr is an empty attribute or false if it is absent, raising Location.Error if attr is not a structure. The name of the deriving plugin should be passed as deriver; it is used in error messages.

val get_expr : deriver:string -> (Parsetree.expression -> [ `Ok of 'a | `Error of string ]) -> Parsetree.expression -> 'a

get_expr ~deriver conv exp converts expression exp with conv, raising Location.Error if conv fails. The name of the deriving plugin should be passed as deriver; it is used in error messages.

OCaml

Innovation. Community. Security.