1Ast_mapper(3)                    OCaml library                   Ast_mapper(3)
2
3
4

NAME

6       Ast_mapper - The interface of a -ppx rewriter
7

Module

9       Module   Ast_mapper
10

Documentation

12       Module Ast_mapper
13        : sig end
14
15
16       The interface of a -ppx rewriter
17
18       A  -ppx rewriter is a program that accepts a serialized abstract syntax
19       tree and outputs another,  possibly  modified,  abstract  syntax  tree.
20       This  module  encapsulates  the  interface between the compiler and the
21       -ppx rewriters, handling such details as the serialization format, for‐
22       warding of command-line flags, and storing state.
23
24
25       Ast_mapper.mapper  enables AST rewriting using open recursion.  A typi‐
26       cal mapper would be based on Ast_mapper.default_mapper , a  deep  iden‐
27       tity  mapper,  and will fall back on it for handling the syntax it does
28       not modify. For example:
29
30
31       open Asttypes
32       open Parsetree
33       open Ast_mapper
34
35       let test_mapper argv =
36         { default_mapper with
37           expr = fun mapper expr ->
38             match expr with
39             | { pexp_desc = Pexp_extension ({ txt = "test" }, PStr [])} ->
40               Ast_helper.Exp.constant (Const_int 42)
41             | other -> default_mapper.expr mapper other; }
42
43       let () =
44         register "ppx_test" test_mapper
45
46       This -ppx rewriter, which replaces [%test] in expressions with the con‐
47       stant  42  , can be compiled using ocamlc -o ppx_test -I +compiler-libs
48       ocamlcommon.cma ppx_test.ml .
49
50       Warning: this module is unstable and part of Compiler_libs .
51
52
53
54
55
56
57
58   A generic Parsetree mapper
59       type mapper = {
60        attribute : mapper -> Parsetree.attribute -> Parsetree.attribute ;
61        attributes : mapper -> Parsetree.attribute list -> Parsetree.attribute
62       list ;
63        binding_op : mapper -> Parsetree.binding_op -> Parsetree.binding_op ;
64        case : mapper -> Parsetree.case -> Parsetree.case ;
65        cases : mapper -> Parsetree.case list -> Parsetree.case list ;
66        class_declaration  :  mapper  -> Parsetree.class_declaration -> Parse‐
67       tree.class_declaration ;
68        class_description : mapper ->  Parsetree.class_description  ->  Parse‐
69       tree.class_description ;
70        class_expr : mapper -> Parsetree.class_expr -> Parsetree.class_expr ;
71        class_field : mapper -> Parsetree.class_field -> Parsetree.class_field
72       ;
73        class_signature  :  mapper  ->  Parsetree.class_signature  ->   Parse‐
74       tree.class_signature ;
75        class_structure   :  mapper  ->  Parsetree.class_structure  ->  Parse‐
76       tree.class_structure ;
77        class_type : mapper -> Parsetree.class_type -> Parsetree.class_type ;
78        class_type_declaration : mapper -> Parsetree.class_type_declaration ->
79       Parsetree.class_type_declaration ;
80        class_type_field  :  mapper  ->  Parsetree.class_type_field  -> Parse‐
81       tree.class_type_field ;
82        constructor_declaration : mapper ->  Parsetree.constructor_declaration
83       -> Parsetree.constructor_declaration ;
84        expr : mapper -> Parsetree.expression -> Parsetree.expression ;
85        extension : mapper -> Parsetree.extension -> Parsetree.extension ;
86        extension_constructor  :  mapper -> Parsetree.extension_constructor ->
87       Parsetree.extension_constructor ;
88        include_declaration  :  mapper  ->  Parsetree.include_declaration   ->
89       Parsetree.include_declaration ;
90        include_description   :  mapper  ->  Parsetree.include_description  ->
91       Parsetree.include_description ;
92        label_declaration : mapper ->  Parsetree.label_declaration  ->  Parse‐
93       tree.label_declaration ;
94        location : mapper -> Location.t -> Location.t ;
95        module_binding  : mapper -> Parsetree.module_binding -> Parsetree.mod‐
96       ule_binding ;
97        module_declaration : mapper -> Parsetree.module_declaration ->  Parse‐
98       tree.module_declaration ;
99        module_substitution   :  mapper  ->  Parsetree.module_substitution  ->
100       Parsetree.module_substitution ;
101        module_expr : mapper -> Parsetree.module_expr -> Parsetree.module_expr
102       ;
103        module_type : mapper -> Parsetree.module_type -> Parsetree.module_type
104       ;
105        module_type_declaration : mapper ->  Parsetree.module_type_declaration
106       -> Parsetree.module_type_declaration ;
107        open_declaration  :  mapper  ->  Parsetree.open_declaration  -> Parse‐
108       tree.open_declaration ;
109        open_description :  mapper  ->  Parsetree.open_description  ->  Parse‐
110       tree.open_description ;
111        pat : mapper -> Parsetree.pattern -> Parsetree.pattern ;
112        payload : mapper -> Parsetree.payload -> Parsetree.payload ;
113        signature : mapper -> Parsetree.signature -> Parsetree.signature ;
114        signature_item  : mapper -> Parsetree.signature_item -> Parsetree.sig‐
115       nature_item ;
116        structure : mapper -> Parsetree.structure -> Parsetree.structure ;
117        structure_item  :  mapper  ->   Parsetree.structure_item   ->   Parse‐
118       tree.structure_item ;
119        typ : mapper -> Parsetree.core_type -> Parsetree.core_type ;
120        type_declaration  :  mapper  ->  Parsetree.type_declaration  -> Parse‐
121       tree.type_declaration ;
122        type_extension  :  mapper  ->   Parsetree.type_extension   ->   Parse‐
123       tree.type_extension ;
124        type_exception   :   mapper   ->  Parsetree.type_exception  ->  Parse‐
125       tree.type_exception ;
126        type_kind : mapper -> Parsetree.type_kind -> Parsetree.type_kind ;
127        value_binding  :   mapper   ->   Parsetree.value_binding   ->   Parse‐
128       tree.value_binding ;
129        value_description  :  mapper  -> Parsetree.value_description -> Parse‐
130       tree.value_description ;
131        with_constraint  :  mapper  ->  Parsetree.with_constraint  ->   Parse‐
132       tree.with_constraint ;
133        }
134
135
136       A  mapper  record implements one "method" per syntactic category, using
137       an open recursion style: each method takes as its  first  argument  the
138       mapper to be applied to children in the syntax tree.
139
140
141
142       val default_mapper : mapper
143
144       A default mapper, which implements a "deep identity" mapping.
145
146
147
148
149   Apply mappers to compilation units
150       val tool_name : unit -> string
151
152       Can  be used within a ppx preprocessor to know which tool is calling it
153       "ocamlc" , "ocamlopt" , "ocamldoc" , "ocamldep" , "ocaml" ,  ...   Some
154       global  variables  that  reflect command-line options are automatically
155       synchronized  between  the  calling  tool  and  the  ppx  preprocessor:
156       Clflags.include_dirs    ,    Load_path    ,    Clflags.open_modules   ,
157       Clflags.for_package , Clflags.debug .
158
159
160
161       val apply : source:string -> target:string -> mapper -> unit
162
163       Apply a mapper (parametrized by the unit name) to  a  dumped  parsetree
164       found  in  the  source  file and put the result in the target file. The
165       structure or signature field of the mapper is applied to the  implemen‐
166       tation or interface.
167
168
169
170       val run_main : (string list -> mapper) -> unit
171
172       Entry point to call to implement a standalone -ppx rewriter from a map‐
173       per, parametrized by the command line arguments.  The current unit name
174       can  be  obtained  from Location.input_name .  This function implements
175       proper error reporting for uncaught exceptions.
176
177
178
179
180   Registration API
181       val register_function : (string -> (string list -> mapper) -> unit) ref
182
183
184
185
186       val register : string -> (string list -> mapper) -> unit
187
188       Apply the register_function .  The default behavior is to run the  map‐
189       per  immediately, taking arguments from the process command line.  This
190       is to support a scenario where a mapper is linked as a stand-alone exe‐
191       cutable.
192
193       It is possible to overwrite the register_function to define "-ppx driv‐
194       ers", which combine several mappers in a single process.  Typically,  a
195       driver starts by defining register_function to a custom implementation,
196       then lets ppx rewriters (linked  statically  or  dynamically)  register
197       themselves,  and  then run all or some of them.  It is also possible to
198       have -ppx drivers apply rewriters to only specific parts of an AST.
199
200       The first argument to register is a symbolic name to be used by the ppx
201       driver.
202
203
204
205
206   Convenience functions to write mappers
207       val map_opt : ('a -> 'b) -> 'a option -> 'b option
208
209
210
211
212       val extension_of_error : Location.error -> Parsetree.extension
213
214       Encode  an  error  into  an  'ocaml.error'  extension node which can be
215       inserted in a generated Parsetree.  The compiler  will  be  responsible
216       for reporting the error.
217
218
219
220       val attribute_of_warning : Location.t -> string -> Parsetree.attribute
221
222       Encode  a warning message into an 'ocaml.ppwarning' attribute which can
223       be inserted in a generated Parsetree.  The compiler will be responsible
224       for reporting the warning.
225
226
227
228
229   Helper functions to call external mappers
230       val  add_ppx_context_str  :  tool_name:string -> Parsetree.structure ->
231       Parsetree.structure
232
233       Extract information from the current environment and encode it into  an
234       attribute which is prepended to the list of structure items in order to
235       pass the information to an external processor.
236
237
238
239       val add_ppx_context_sig : tool_name:string  ->  Parsetree.signature  ->
240       Parsetree.signature
241
242       Same as add_ppx_context_str , but for signatures.
243
244
245
246       val  drop_ppx_context_str  :  restore:bool  ->  Parsetree.structure  ->
247       Parsetree.structure
248
249       Drop the ocaml.ppx.context attribute from a structure.  If  restore  is
250       true, also restore the associated data in the current process.
251
252
253
254       val  drop_ppx_context_sig  :  restore:bool  ->  Parsetree.signature  ->
255       Parsetree.signature
256
257       Same as drop_ppx_context_str , but for signatures.
258
259
260
261
262   Cookies
263       Cookies are used to pass information from a ppx processor to a  further
264       invocation  of  itself,  when  called from the OCaml toplevel (or other
265       tools that support cookies).
266
267       val set_cookie : string -> Parsetree.expression -> unit
268
269
270
271
272       val get_cookie : string -> Parsetree.expression option
273
274
275
276
277
278
279OCamldoc                          2020-02-27                     Ast_mapper(3)
Impressum