-
Notifications
You must be signed in to change notification settings - Fork 76
/
ast_mapper.mli
229 lines (188 loc) · 10.1 KB
/
ast_mapper.mli
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Alain Frisch, LexiFi *)
(* *)
(* Copyright 2012 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
(** 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.
{!mapper} enables AST rewriting using open recursion.
A typical mapper would be based on {!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 (Pconst_integer ("42", None))
| 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].
{b Warning:} this module is unstable and part of
{{!Compiler_libs}compiler-libs}.
*)
open Parsetree
(** {1 A generic Parsetree mapper} *)
type mapper = {
attribute: mapper -> attribute -> attribute;
attributes: mapper -> attribute list -> attribute list;
modes : mapper -> modes -> modes;
modalities : mapper -> modalities -> modalities;
binding_op: mapper -> binding_op -> binding_op;
case: mapper -> case -> case;
cases: mapper -> case list -> case list;
class_declaration: mapper -> class_declaration -> class_declaration;
class_description: mapper -> class_description -> class_description;
class_expr: mapper -> class_expr -> class_expr;
class_field: mapper -> class_field -> class_field;
class_signature: mapper -> class_signature -> class_signature;
class_structure: mapper -> class_structure -> class_structure;
class_type: mapper -> class_type -> class_type;
class_type_declaration: mapper -> class_type_declaration
-> class_type_declaration;
class_type_field: mapper -> class_type_field -> class_type_field;
constant: mapper -> constant -> constant;
constructor_declaration: mapper -> constructor_declaration
-> constructor_declaration;
directive_argument: mapper -> directive_argument -> directive_argument;
expr: mapper -> expression -> expression;
extension: mapper -> extension -> extension;
extension_constructor: mapper -> extension_constructor
-> extension_constructor;
include_declaration: mapper -> include_declaration -> include_declaration;
(** In Jane Street's OCaml, this may be called for [include functor]s, and
thus the thing being included might be a functor and not a plain module
type *)
include_description: mapper -> include_description -> include_description;
(** In Jane Street's OCaml, this may be called for [include functor]s, and
thus the thing being included might be a functor and not a plain module
type *)
jkind_annotation: mapper -> jkind_annotation -> jkind_annotation;
label_declaration: mapper -> label_declaration -> label_declaration;
location: mapper -> Location.t -> Location.t;
module_binding: mapper -> module_binding -> module_binding;
module_declaration: mapper -> module_declaration -> module_declaration;
module_substitution: mapper -> module_substitution -> module_substitution;
module_expr: mapper -> module_expr -> module_expr;
module_type: mapper -> module_type -> module_type;
module_type_declaration: mapper -> module_type_declaration
-> module_type_declaration;
open_declaration: mapper -> open_declaration -> open_declaration;
open_description: mapper -> open_description -> open_description;
pat: mapper -> pattern -> pattern;
payload: mapper -> payload -> payload;
signature: mapper -> signature -> signature;
signature_item: mapper -> signature_item -> signature_item;
structure: mapper -> structure -> structure;
structure_item: mapper -> structure_item -> structure_item;
toplevel_directive: mapper -> toplevel_directive -> toplevel_directive;
toplevel_phrase: mapper -> toplevel_phrase -> toplevel_phrase;
typ: mapper -> core_type -> core_type;
type_declaration: mapper -> type_declaration -> type_declaration;
type_extension: mapper -> type_extension -> type_extension;
type_exception: mapper -> type_exception -> type_exception;
type_kind: mapper -> type_kind -> type_kind;
value_binding: mapper -> value_binding -> value_binding;
value_description: mapper -> value_description -> value_description;
with_constraint: mapper -> with_constraint -> with_constraint;
}
(** 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. *)
(** {1 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},
{!Clflags.hidden_include_dirs}, {!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. *)
(** {1 Registration API} *)
val register_function: (string -> (string list -> mapper) -> unit) ref
val register: string -> (string list -> mapper) -> unit
(** 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. *)
(** {1 Convenience functions to write mappers} *)
val map_opt: ('a -> 'b) -> 'a option -> 'b option
val extension_of_error: Location.error -> 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. *)
val attribute_of_warning: Location.t -> string -> attribute
(** Encode 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. *)
(** {1 Helper functions to call external mappers} *)
val add_ppx_context_str:
tool_name:string -> Parsetree.structure -> Parsetree.structure
(** Extract 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.signature
(** Same as [add_ppx_context_str], but for signatures. *)
val add_ppx_context_sig_items:
tool_name:string -> Parsetree.signature_item list -> Parsetree.signature_item list
val drop_ppx_context_str:
restore:bool -> Parsetree.structure -> Parsetree.structure
(** Drop the ocaml.ppx.context attribute from a structure. If
[restore] is true, also restore the associated data in the current
process. *)
val drop_ppx_context_sig:
restore:bool -> Parsetree.signature -> Parsetree.signature
(** Same as [drop_ppx_context_str], but for signatures. *)
val drop_ppx_context_sig_items:
restore:bool -> Parsetree.signature_item list -> Parsetree.signature_item list
(** {1 Cookies} *)
(** Cookies are used to pass information from a ppx processor to
a further invocation of itself, when called from the OCaml
toplevel (or other tools that support cookies). *)
val set_cookie: string -> Parsetree.expression -> unit
val get_cookie: string -> Parsetree.expression option