This module and all submodules are new.
module Ast_mapper:sig
..end
A -ppx rewriter is a program that accepts a serialized abstract syntax tree and outputs another, possibly modified, abstract syntax tree. This module encapsulates the interface between the compiler and the -ppx rewriters, handling such details as the serialization format, forwarding of command-line flags, and storing state.
Ast_mapper.mapper
allows to implement AST rewriting using open recursion.
A typical mapper would be based on Ast_mapper.default_mapper
, a deep
identity mapper, and will fall back on it for handling the syntax it
does not modify. For example:
open Asttypes
open Parsetree
open Ast_mapper
let test_mapper argv =
{ default_mapper with
expr = fun mapper expr ->
match expr with
| { pexp_desc = Pexp_extension ({ txt = "test" }, PStr [])} ->
Ast_helper.Exp.constant (Const_int 42)
| other -> default_mapper.expr mapper other; }
let () =
register "ppx_test" test_mapper
This -ppx rewriter, which replaces [%test]
in expressions with
the constant 42
, can be compiled using
ocamlc -o ppx_test -I +compiler-libs ocamlcommon.cma ppx_test.ml
.
type
mapper = {
val default_mapper : mapper
val tool_name : unit -> string
"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
,
Config.load_path
, Clflags.open_modules
, Clflags.for_package
,
Clflags.debug
.val apply : source:string -> target:string -> mapper -> unit
source
file and put the result in the
target
file. The structure
or signature
field of the mapper
is applied to the implementation or interface.val run_main : (string list -> mapper) -> unit
Location.input_name
. This
function implements proper error reporting for uncaught
exceptions.val register_function : (string -> (string list -> mapper) -> unit) ref
val register : string -> (string list -> mapper) -> unit
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_function
to define
"-ppx drivers", which combine several mappers in a single process.
Typically, a driver starts by defining register_function
to 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 register
is a symbolic name to be used by
the ppx driver.
val map_opt : ('a -> 'b) -> 'a option -> 'b option
val extension_of_error : Location.error -> Parsetree.extension
val attribute_of_warning : Location.t -> string -> Parsetree.attribute
val add_ppx_context_str : tool_name:string -> Parsetree.structure -> Parsetree.structure
val add_ppx_context_sig : tool_name:string -> Parsetree.signature -> Parsetree.signature
add_ppx_context_str
, but for signatures.val drop_ppx_context_str : restore:bool -> Parsetree.structure -> Parsetree.structure
restore
is true, also restore the associated data in the current
process.val drop_ppx_context_sig : restore:bool -> Parsetree.signature -> Parsetree.signature
drop_ppx_context_str
, but for signatures.string -> Parsetree.expression -> unit
: string -> Parsetree.expression option
: