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.resultA type of conversion functions.
A conversion function of type
'a convconverts a raw expression into an argument of type'a. Or returnsResult.Error "error"if conversion fails.
val expr : Parsetree.expression convexprreturns the input expression as-is.
val bool : bool convbool exprextracts a boolean constant fromexpr, or returnsResult.Error "boolean"ifexprdoes not contain a boolean literal.
val int : int convint exprextracts an integer constant fromexpr, or returnsResult.Error "integer"ifexprdoes not contain an integer literal.
val string : string convstring exprextracts a string constant fromexpr, or returnsResult.Error "string"ifexprdoes not contain a string literal.
val char : char convchar exprextracts a char constant fromexpr, or returnsResult.Error "char"ifexprdoes not contain a char literal.
val enum : string list -> string convenum values exprextracts a polymorphic variant constant fromexpr, or returnsResult.Error "one of: `a, `b, ..."ifexprdoes not contain a polymorphic variant constructor included invalues.
val list : 'a conv -> 'a list convlist f exprextracts a list constant fromexprand maps every element throughf, or returnsResult.Error "list:..."where...is the error returned byf, or returnsResult.Error "list"ifexprdoes not contain a list.
val get_attr : deriver:string -> 'a conv -> Parsetree.attribute option -> 'a optionget_attr ~deriver conv attrextracts the expression fromattrand converts it withconv, raisingLocation.Errorifattris not a structure with a single expression orconvfails; or returnsNoneifattrisNone. 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 -> boolget_flag ~deriver attrreturnstrueifattris an empty attribute orfalseif it is absent, raisingLocation.Errorifattris 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 -> 'aget_expr ~deriver conv expconverts expressionexpwithconv, raisingLocation.Errorifconvfails. The name of the deriving plugin should be passed asderiver; it is used in error messages.