Ast_mapper • man page

Ast_mapper • man page

Ast_mapper (3)

NAME

Ast_mapper - The interface of a -ppx rewriter

Module

Module Ast_mapper

Documentation

Module
Ast_mapper :
sig end

The interface of a -ppx rewriter

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:

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
:
mapper

A default mapper, which implements a "deep identity" mapping.

=== Apply mappers to compilation units===

val tool_name
:
unit -> string

Can 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
,
Config.load_path
,
Clflags.open_modules
,
Clflags.for_package
,
Clflags.debug
.

val apply
:
source:string -> target:string -> mapper -> unit

Apply a mapper (parametrized by the unit name) to a dumped
parsetree found in the
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

Entry 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.

Apply 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_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.

=== Convenience functions to write mappers===

val map_opt
:
('a -> 'b) -> 'a option -> 'b option

val extension_of_error
:
Location.error -> Parsetree.extension

Encode 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.