Module Ppx_deriving.Arg
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"
.
type 'a conv
= Parsetree.expression -> ('a, string) Result.result
A type of conversion functions.
A conversion function of type
'a conv
converts a raw expression into an argument of type'a
. Or returnsResult.Error "error"
if conversion fails.
val expr : Parsetree.expression conv
expr
returns the input expression as-is.
val bool : bool conv
bool expr
extracts a boolean constant fromexpr
, or returnsResult.Error "boolean"
ifexpr
does not contain a boolean literal.
val int : int conv
int expr
extracts an integer constant fromexpr
, or returnsResult.Error "integer"
ifexpr
does not contain an integer literal.
val string : string conv
string expr
extracts a string constant fromexpr
, or returnsResult.Error "string"
ifexpr
does not contain a string literal.
val char : char conv
char expr
extracts a char constant fromexpr
, or returnsResult.Error "char"
ifexpr
does not contain a char literal.
val enum : string list -> string conv
enum values expr
extracts a polymorphic variant constant fromexpr
, or returnsResult.Error "one of: `a, `b, ..."
ifexpr
does not contain a polymorphic variant constructor included invalues
.
val list : 'a conv -> 'a list conv
list f expr
extracts a list constant fromexpr
and maps every element throughf
, or returnsResult.Error "list:..."
where...
is the error returned byf
, or returnsResult.Error "list"
ifexpr
does not contain a list.
val get_attr : deriver:string -> 'a conv -> Parsetree.attribute option -> 'a option
get_attr ~deriver conv attr
extracts the expression fromattr
and converts it withconv
, raisingLocation.Error
ifattr
is not a structure with a single expression orconv
fails; or returnsNone
ifattr
isNone
. The name of the deriving plugin should be passed asderiver
; 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
returnstrue
ifattr
is an empty attribute orfalse
if it is absent, raisingLocation.Error
ifattr
is not a structure. The name of the deriving plugin should be passed asderiver
; it is used in error messages.
val get_expr : deriver:string -> 'a conv -> Parsetree.expression -> 'a
get_expr ~deriver conv exp
converts expressionexp
withconv
, raisingLocation.Error
ifconv
fails. The name of the deriving plugin should be passed asderiver
; it is used in error messages.