Module Ppx_deriving
Public API of ppx_deriving executable.
Registration
type deriver={}A type of deriving plugins.
A structure or signature deriving function accepts a list of
~options, a~pathof modules for the type declaration currently being processed (with[]for toplevel phrases), and a type declaration item (type t = .. and t' = ..), and returns a list of items to be appended after the type declaration item in structure and signature. It is invoked by[\@\@deriving]annotations.A type deriving function accepts a type and returns a corresponding derived expression. It is invoked by
[%derive.foo:]and[%foo:]annotations. If this function is missing, the corresponding[%foo:]annotation is ignored.The structure and signature deriving functions are invoked in the order in which they appear in the source code.
val register : deriver -> unitregister deriverregistersderiveraccording to itsnamefield.
val add_register_hook : (deriver -> unit) -> unitadd_register_hook hookaddshookto be executed whenever a new deriver is registered.
val derivers : unit -> deriver listderivers ()returns all currently registered derivers.
val create : string -> ?core_type:(Ppxlib.core_type -> Ppxlib.expression) -> ?type_ext_str:(options:(string * Ppxlib.expression) list -> path:string list -> Ppxlib.type_extension -> Ppxlib.structure) -> ?type_ext_sig:(options:(string * Ppxlib.expression) list -> path:string list -> Ppxlib.type_extension -> Ppxlib.signature) -> ?type_decl_str:(options:(string * Ppxlib.expression) list -> path:string list -> Ppxlib.type_declaration list -> Ppxlib.structure) -> ?type_decl_sig:(options:(string * Ppxlib.expression) list -> path:string list -> Ppxlib.type_declaration list -> Ppxlib.signature) -> ?module_type_decl_str:(options:(string * Ppxlib.expression) list -> path:string list -> Ppxlib.module_type_declaration -> Ppxlib.structure) -> ?module_type_decl_sig:(options:(string * Ppxlib.expression) list -> path:string list -> Ppxlib.module_type_declaration -> Ppxlib.signature) -> unit -> deriverCreating
deriverstructure.
val lookup : string -> deriver optionlookup namelooks up a deriver calledname.
Coercions
val string_of_core_type : Ppxlib.Parsetree.core_type -> stringstring_of_core_type typunparsestyp, omitting any attributes.
Option parsing
Hygiene
val create_quoter : unit -> quoterquoter ()creates an empty quoter.
val quote : quoter:quoter -> Ppxlib.expression -> Ppxlib.expressionquote quoter exprrecords a pure expressionexprwithinquoterand returns an expression which has the same value asexprin the context thatsanitizeprovides.
val sanitize : ?module_:Ppxlib.Longident.t -> ?quoter:quoter -> Ppxlib.expression -> Ppxlib.expressionsanitize module_ quoter exprwrapsexprin a way that ensures that the contents ofmodule_andPervasives, as well as the identifiers in expressions returned byquoteare in scope, and returns the wrapped expression.module_defaults toPpx_deriving_runtimeif it's not provided
val with_quoter : (quoter -> 'a -> Ppxlib.expression) -> 'a -> Ppxlib.expressionwith_quoter fn≡fun fn a -> let quoter = create_quoter () in sanitize ~quoter (fn quoter a)
AST manipulation
val expand_path : path:string list -> string -> stringexpand_path namereturnsnamewith thepathmodule path prepended, e.g.expand_path ["Foo";"M"] "t"="Foo.M.t"andexpand_path [] "t"="t"
val path_of_type_decl : path:string list -> Ppxlib.type_declaration -> string listpath_of_type_decl ~path type_returnspathiftype_does not have a manifest or the manifest is not a constructor, and the module path of manifest otherwise.path_of_type_declis useful when determining the canonical path location of fields and constructors; e.g. fortype bar = M.foo = A | B, it will return["M"].
val mangle_type_decl : ?fixpoint:string -> [ `Prefix of string | `Suffix of string | `PrefixSuffix of string * string ] -> Ppxlib.type_declaration -> stringmangle_type_decl ~fixpoint affix type_derives a function name fromtype_name by doing nothing iftype_is namedfixpoint("t"by default), or appending and/or prependingaffixvia an underscore.
val mangle_lid : ?fixpoint:string -> [ `Prefix of string | `Suffix of string | `PrefixSuffix of string * string ] -> Ppxlib.Longident.t -> Ppxlib.Longident.tmangle_lid ~fixpoint affix liddoes the same asmangle_type_decl, but for the last component oflid.
val attr : deriver:string -> string -> Ppxlib.attributes -> Ppxlib.attribute optionattr ~deriver name attrssearches for an attribute[\@deriving.deriver.attr]inattrsif any attribute with name starting with\@deriving.deriverexists, or[\@deriver.attr]if any attribute with name starting with\@deriverexists, or[\@attr]otherwise.
val attr_warning : Ppxlib.expression -> Ppxlib.attributeattr_warning exprbuilds the attribute\@ocaml.warning expr
val free_vars_in_core_type : Ppxlib.core_type -> tyvar listfree_vars_in_core_type typreturns unique free variables intypin lexical order.
val remove_pervasives : deriver:string -> Ppxlib.core_type -> Ppxlib.core_typeremove_pervasives ~deriver typremoves the leading "Pervasives." module name in longidents. Type expressions marked with[\@nobuiltin]are ignored.The name of the deriving plugin should be passed as
deriver; it is used in error messages.
val fresh_var : string list -> stringfresh_var boundreturns a fresh variable name not present inbound. The name is selected in alphabetical succession.
val fold_left_type_decl : ('a -> tyvar -> 'a) -> 'a -> Ppxlib.type_declaration -> 'afold_left_type_decl fn accum type_performs a left fold over all type variable (i.e. not wildcard) parameters intype_.
val fold_right_type_decl : (tyvar -> 'a -> 'a) -> Ppxlib.type_declaration -> 'a -> 'afold_right_type_decl fn accum type_performs a right fold over all type variable (i.e. not wildcard) parameters intype_.
val fold_left_type_ext : ('a -> tyvar -> 'a) -> 'a -> Ppxlib.type_extension -> 'afold_left_type_ext fn accum type_performs a left fold over all type variable (i.e. not wildcard) parameters intype_.
val fold_right_type_ext : (tyvar -> 'a -> 'a) -> Ppxlib.type_extension -> 'a -> 'afold_right_type_ext fn accum type_performs a right fold over all type variable (i.e. not wildcard) parameters intype_.
val poly_fun_of_type_decl : Ppxlib.type_declaration -> Ppxlib.expression -> Ppxlib.expressionpoly_fun_of_type_decl type_ exprwrapsexprintofun poly_N -> ...for every type parameter'Npresent intype_. For example, iftype_refers totype ('a, 'b) map,exprwill be wrapped intofun poly_a poly_b -> [%e expr]._parameters are ignored.
val poly_fun_of_type_ext : Ppxlib.type_extension -> Ppxlib.expression -> Ppxlib.expressionSame as
poly_fun_of_type_declbut for type extension.
val poly_apply_of_type_decl : Ppxlib.type_declaration -> Ppxlib.expression -> Ppxlib.expressionpoly_apply_of_type_decl type_ exprwrapsexprintoexpr poly_Nfor every type parameter'Npresent intype_. For example, iftype_refers totype ('a, 'b) map,exprwill be wrapped into[%e expr] poly_a poly_b._parameters are ignored.
val poly_apply_of_type_ext : Ppxlib.type_extension -> Ppxlib.expression -> Ppxlib.expressionSame as
poly_apply_of_type_declbut for type extension.
val poly_arrow_of_type_decl : (Ppxlib.core_type -> Ppxlib.core_type) -> Ppxlib.type_declaration -> Ppxlib.core_type -> Ppxlib.core_typepoly_arrow_of_type_decl fn type_ typwrapstypin an arrow withfn [%type: 'N]as argument for every type parameter'Npresent intype_. For example, iftype_refers totype ('a, 'b) mapandfnisfun var -> [%type: [%t var] -> string],typwill be wrapped into('a -> string) -> ('b -> string) -> [%t typ]._parameters are ignored.
val poly_arrow_of_type_ext : (Ppxlib.core_type -> Ppxlib.core_type) -> Ppxlib.type_extension -> Ppxlib.core_type -> Ppxlib.core_typeSame as
poly_arrow_of_type_declbut for type extension.
val core_type_of_type_decl : Ppxlib.type_declaration -> Ppxlib.core_typecore_type_of_type_decl type_constructs type('a, 'b, ...) tfor type declarationtype ('a, 'b, ...) t = ....
val core_type_of_type_ext : Ppxlib.type_extension -> Ppxlib.core_typeSame as
core_type_of_type_declbut for type extension.
val instantiate : string list -> Ppxlib.type_declaration -> Ppxlib.core_type * string list * string listinstantiate bound type_returnstyp, vars, bound'wheretypis a type instantiated from type declarationtype_,vars≡free_vars_in_core_type typandbound'≡bound @ vars.
val fold_exprs : ?unit:Ppxlib.expression -> (Ppxlib.expression -> Ppxlib.expression -> Ppxlib.expression) -> Ppxlib.expression list -> Ppxlib.expressionfold_exprs ~unit fn exprsfoldsexprsusing head ofexprsas initial accumulator value, orunitifexprs = [].See also
seq_reduceandbinop_reduce.
val seq_reduce : ?sep:Ppxlib.expression -> Ppxlib.expression -> Ppxlib.expression -> Ppxlib.expressionWhen
sepis present:seq_reduce≡fun x a b -> [%expr [%e a]; [%e x]; [%e b]]. Whensepis missing:seq_reduce≡fun a b -> [%expr [%e a]; [%e b]].
Miscellanea
module Ast_convenience : sig ... end