A generic Parsetree mapper
type mapper = {}A mapper record implements one "method" per syntactic category, using an open recursion style: each method takes as its first argument the mapper to be applied to children in the syntax tree.
val default_mapper : mapperA default mapper, which implements a "deep identity" mapping.
Apply mappers to compilation units
val tool_name : unit -> stringCan be used within a ppx preprocessor to know which tool is calling it
"ocamlc","ocamlopt","ocamldoc","ocamldep","ocaml", ... Some global variables that reflect command-line options are automatically synchronized between the calling tool and the ppx preprocessor:Clflags.include_dirs,Load_path,Clflags.open_modules,Clflags.for_package,Clflags.debug.
val apply : source:string -> target:string -> mapper -> unitApply a mapper (parametrized by the unit name) to a dumped parsetree found in the
sourcefile and put the result in thetargetfile. Thestructureorsignaturefield of the mapper is applied to the implementation or interface.
val run_main : (string list -> mapper) -> unitEntry point to call to implement a standalone -ppx rewriter from a mapper, parametrized by the command line arguments. The current unit name can be obtained from
Location.input_name. This function implements proper error reporting for uncaught exceptions.
Registration API
val register_function : (string -> (string list -> mapper) -> unit) Stdlib.refval register : string -> (string list -> mapper) -> unitApply the
register_function. The default behavior is to run the mapper immediately, taking arguments from the process command line. This is to support a scenario where a mapper is linked as a stand-alone executable.It is possible to overwrite the
register_functionto define "-ppx drivers", which combine several mappers in a single process. Typically, a driver starts by definingregister_functionto a custom implementation, then lets ppx rewriters (linked statically or dynamically) register themselves, and then run all or some of them. It is also possible to have -ppx drivers apply rewriters to only specific parts of an AST.The first argument to
registeris a symbolic name to be used by the ppx driver.
Convenience functions to write mappers
val map_opt : ('a -> 'b) -> 'a option -> 'b optionval extension_of_error : Location.error -> Parsetree.extensionEncode an error into an 'ocaml.error' extension node which can be inserted in a generated Parsetree. The compiler will be responsible for reporting the error.
val attribute_of_warning : Location.t -> string -> Parsetree.attributeEncode a warning message into an 'ocaml.ppwarning' attribute which can be inserted in a generated Parsetree. The compiler will be responsible for reporting the warning.
Helper functions to call external mappers
val add_ppx_context_str : tool_name:string -> Parsetree.structure -> Parsetree.structureExtract information from the current environment and encode it into an attribute which is prepended to the list of structure items in order to pass the information to an external processor.
val add_ppx_context_sig : tool_name:string -> Parsetree.signature -> Parsetree.signatureSame as
add_ppx_context_str, but for signatures.
val drop_ppx_context_str : restore:bool -> Parsetree.structure -> Parsetree.structureDrop the ocaml.ppx.context attribute from a structure. If
restoreis true, also restore the associated data in the current process.
val drop_ppx_context_sig : restore:bool -> Parsetree.signature -> Parsetree.signatureSame as
drop_ppx_context_str, but for signatures.
Cookies
val set_cookie : string -> Parsetree.expression -> unitval get_cookie : string -> Parsetree.expression option