Skip to content

Commit c6100bc

Browse files
committed
Use ast mappers both ways.
1 parent a1a438d commit c6100bc

File tree

4 files changed

+1336
-70
lines changed

4 files changed

+1336
-70
lines changed

compiler/ml/ast_helper0.ml

Lines changed: 369 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,369 @@
1+
(**************************************************************************)
2+
(* *)
3+
(* OCaml *)
4+
(* *)
5+
(* Alain Frisch, LexiFi *)
6+
(* *)
7+
(* Copyright 2012 Institut National de Recherche en Informatique et *)
8+
(* en Automatique. *)
9+
(* *)
10+
(* All rights reserved. This file is distributed under the terms of *)
11+
(* the GNU Lesser General Public License version 2.1, with the *)
12+
(* special exception on linking described in the file LICENSE. *)
13+
(* *)
14+
(**************************************************************************)
15+
16+
(** Helpers to produce Parsetree fragments *)
17+
18+
open Asttypes
19+
open Parsetree0
20+
21+
type lid = Longident.t loc
22+
type str = string loc
23+
type loc = Location.t
24+
type attrs = attribute list
25+
26+
let default_loc = ref Location.none
27+
28+
let with_default_loc l f =
29+
let old = !default_loc in
30+
default_loc := l;
31+
try
32+
let r = f () in
33+
default_loc := old;
34+
r
35+
with exn ->
36+
default_loc := old;
37+
raise exn
38+
39+
module Const = struct
40+
let integer ?suffix i = Pconst_integer (i, suffix)
41+
let int ?suffix i = integer ?suffix (string_of_int i)
42+
let int32 ?(suffix = 'l') i = integer ~suffix (Int32.to_string i)
43+
let int64 ?(suffix = 'L') i = integer ~suffix (Int64.to_string i)
44+
let nativeint ?(suffix = 'n') i = integer ~suffix (Nativeint.to_string i)
45+
let float ?suffix f = Pconst_float (f, suffix)
46+
let char c = Pconst_char (Char.code c)
47+
let string ?quotation_delimiter s = Pconst_string (s, quotation_delimiter)
48+
end
49+
50+
module Typ = struct
51+
let mk ?(loc = !default_loc) ?(attrs = []) d =
52+
{ptyp_desc = d; ptyp_loc = loc; ptyp_attributes = attrs}
53+
let attr d a = {d with ptyp_attributes = d.ptyp_attributes @ [a]}
54+
55+
let any ?loc ?attrs () = mk ?loc ?attrs Ptyp_any
56+
let var ?loc ?attrs a = mk ?loc ?attrs (Ptyp_var a)
57+
let arrow ?loc ?attrs a b c = mk ?loc ?attrs (Ptyp_arrow (a, b, c))
58+
let tuple ?loc ?attrs a = mk ?loc ?attrs (Ptyp_tuple a)
59+
let constr ?loc ?attrs a b = mk ?loc ?attrs (Ptyp_constr (a, b))
60+
let object_ ?loc ?attrs a b = mk ?loc ?attrs (Ptyp_object (a, b))
61+
let alias ?loc ?attrs a b = mk ?loc ?attrs (Ptyp_alias (a, b))
62+
let variant ?loc ?attrs a b c = mk ?loc ?attrs (Ptyp_variant (a, b, c))
63+
let poly ?loc ?attrs a b = mk ?loc ?attrs (Ptyp_poly (a, b))
64+
let package ?loc ?attrs a b = mk ?loc ?attrs (Ptyp_package (a, b))
65+
let extension ?loc ?attrs a = mk ?loc ?attrs (Ptyp_extension a)
66+
67+
let force_poly t =
68+
match t.ptyp_desc with
69+
| Ptyp_poly _ -> t
70+
| _ -> poly ~loc:t.ptyp_loc [] t (* -> ghost? *)
71+
72+
let varify_constructors var_names t =
73+
let check_variable vl loc v =
74+
if List.mem v vl then raise Syntaxerr.(Error (Variable_in_scope (loc, v)))
75+
in
76+
let var_names = List.map (fun v -> v.txt) var_names in
77+
let rec loop t =
78+
let desc =
79+
match t.ptyp_desc with
80+
| Ptyp_any -> Ptyp_any
81+
| Ptyp_var x ->
82+
check_variable var_names t.ptyp_loc x;
83+
Ptyp_var x
84+
| Ptyp_arrow (label, core_type, core_type') ->
85+
Ptyp_arrow (label, loop core_type, loop core_type')
86+
| Ptyp_tuple lst -> Ptyp_tuple (List.map loop lst)
87+
| Ptyp_constr ({txt = Longident.Lident s}, []) when List.mem s var_names
88+
->
89+
Ptyp_var s
90+
| Ptyp_constr (longident, lst) ->
91+
Ptyp_constr (longident, List.map loop lst)
92+
| Ptyp_object (lst, o) -> Ptyp_object (List.map loop_object_field lst, o)
93+
| Ptyp_class () -> assert false
94+
| Ptyp_alias (core_type, string) ->
95+
check_variable var_names t.ptyp_loc string;
96+
Ptyp_alias (loop core_type, string)
97+
| Ptyp_variant (row_field_list, flag, lbl_lst_option) ->
98+
Ptyp_variant
99+
(List.map loop_row_field row_field_list, flag, lbl_lst_option)
100+
| Ptyp_poly (string_lst, core_type) ->
101+
List.iter
102+
(fun v -> check_variable var_names t.ptyp_loc v.txt)
103+
string_lst;
104+
Ptyp_poly (string_lst, loop core_type)
105+
| Ptyp_package (longident, lst) ->
106+
Ptyp_package (longident, List.map (fun (n, typ) -> (n, loop typ)) lst)
107+
| Ptyp_extension (s, arg) -> Ptyp_extension (s, arg)
108+
in
109+
{t with ptyp_desc = desc}
110+
and loop_row_field = function
111+
| Rtag (label, attrs, flag, lst) ->
112+
Rtag (label, attrs, flag, List.map loop lst)
113+
| Rinherit t -> Rinherit (loop t)
114+
and loop_object_field = function
115+
| Otag (label, attrs, t) -> Otag (label, attrs, loop t)
116+
| Oinherit t -> Oinherit (loop t)
117+
in
118+
loop t
119+
end
120+
121+
module Pat = struct
122+
let mk ?(loc = !default_loc) ?(attrs = []) d =
123+
{ppat_desc = d; ppat_loc = loc; ppat_attributes = attrs}
124+
let attr d a = {d with ppat_attributes = d.ppat_attributes @ [a]}
125+
126+
let any ?loc ?attrs () = mk ?loc ?attrs Ppat_any
127+
let var ?loc ?attrs a = mk ?loc ?attrs (Ppat_var a)
128+
let alias ?loc ?attrs a b = mk ?loc ?attrs (Ppat_alias (a, b))
129+
let constant ?loc ?attrs a = mk ?loc ?attrs (Ppat_constant a)
130+
let interval ?loc ?attrs a b = mk ?loc ?attrs (Ppat_interval (a, b))
131+
let tuple ?loc ?attrs a = mk ?loc ?attrs (Ppat_tuple a)
132+
let construct ?loc ?attrs a b = mk ?loc ?attrs (Ppat_construct (a, b))
133+
let variant ?loc ?attrs a b = mk ?loc ?attrs (Ppat_variant (a, b))
134+
let record ?loc ?attrs a b = mk ?loc ?attrs (Ppat_record (a, b))
135+
let array ?loc ?attrs a = mk ?loc ?attrs (Ppat_array a)
136+
let or_ ?loc ?attrs a b = mk ?loc ?attrs (Ppat_or (a, b))
137+
let constraint_ ?loc ?attrs a b = mk ?loc ?attrs (Ppat_constraint (a, b))
138+
let type_ ?loc ?attrs a = mk ?loc ?attrs (Ppat_type a)
139+
let lazy_ ?loc ?attrs a = mk ?loc ?attrs (Ppat_lazy a)
140+
let unpack ?loc ?attrs a = mk ?loc ?attrs (Ppat_unpack a)
141+
let open_ ?loc ?attrs a b = mk ?loc ?attrs (Ppat_open (a, b))
142+
let exception_ ?loc ?attrs a = mk ?loc ?attrs (Ppat_exception a)
143+
let extension ?loc ?attrs a = mk ?loc ?attrs (Ppat_extension a)
144+
end
145+
146+
module Exp = struct
147+
let mk ?(loc = !default_loc) ?(attrs = []) d =
148+
{pexp_desc = d; pexp_loc = loc; pexp_attributes = attrs}
149+
let attr d a = {d with pexp_attributes = d.pexp_attributes @ [a]}
150+
151+
let ident ?loc ?attrs a = mk ?loc ?attrs (Pexp_ident a)
152+
let constant ?loc ?attrs a = mk ?loc ?attrs (Pexp_constant a)
153+
let let_ ?loc ?attrs a b c = mk ?loc ?attrs (Pexp_let (a, b, c))
154+
let fun_ ?loc ?attrs a b c d = mk ?loc ?attrs (Pexp_fun (a, b, c, d))
155+
let function_ ?loc ?attrs a = mk ?loc ?attrs (Pexp_function a)
156+
let apply ?loc ?attrs a b = mk ?loc ?attrs (Pexp_apply (a, b))
157+
let match_ ?loc ?attrs a b = mk ?loc ?attrs (Pexp_match (a, b))
158+
let try_ ?loc ?attrs a b = mk ?loc ?attrs (Pexp_try (a, b))
159+
let tuple ?loc ?attrs a = mk ?loc ?attrs (Pexp_tuple a)
160+
let construct ?loc ?attrs a b = mk ?loc ?attrs (Pexp_construct (a, b))
161+
let variant ?loc ?attrs a b = mk ?loc ?attrs (Pexp_variant (a, b))
162+
let record ?loc ?attrs a b = mk ?loc ?attrs (Pexp_record (a, b))
163+
let field ?loc ?attrs a b = mk ?loc ?attrs (Pexp_field (a, b))
164+
let setfield ?loc ?attrs a b c = mk ?loc ?attrs (Pexp_setfield (a, b, c))
165+
let array ?loc ?attrs a = mk ?loc ?attrs (Pexp_array a)
166+
let ifthenelse ?loc ?attrs a b c = mk ?loc ?attrs (Pexp_ifthenelse (a, b, c))
167+
let sequence ?loc ?attrs a b = mk ?loc ?attrs (Pexp_sequence (a, b))
168+
let while_ ?loc ?attrs a b = mk ?loc ?attrs (Pexp_while (a, b))
169+
let for_ ?loc ?attrs a b c d e = mk ?loc ?attrs (Pexp_for (a, b, c, d, e))
170+
let constraint_ ?loc ?attrs a b = mk ?loc ?attrs (Pexp_constraint (a, b))
171+
let coerce ?loc ?attrs a c = mk ?loc ?attrs (Pexp_coerce (a, (), c))
172+
let send ?loc ?attrs a b = mk ?loc ?attrs (Pexp_send (a, b))
173+
let new_ ?loc ?attrs a = mk ?loc ?attrs (Pexp_new a)
174+
let setinstvar ?loc ?attrs a b = mk ?loc ?attrs (Pexp_setinstvar (a, b))
175+
let override ?loc ?attrs a = mk ?loc ?attrs (Pexp_override a)
176+
let letmodule ?loc ?attrs a b c = mk ?loc ?attrs (Pexp_letmodule (a, b, c))
177+
let letexception ?loc ?attrs a b = mk ?loc ?attrs (Pexp_letexception (a, b))
178+
let assert_ ?loc ?attrs a = mk ?loc ?attrs (Pexp_assert a)
179+
let lazy_ ?loc ?attrs a = mk ?loc ?attrs (Pexp_lazy a)
180+
let poly ?loc ?attrs a b = mk ?loc ?attrs (Pexp_poly (a, b))
181+
let newtype ?loc ?attrs a b = mk ?loc ?attrs (Pexp_newtype (a, b))
182+
let pack ?loc ?attrs a = mk ?loc ?attrs (Pexp_pack a)
183+
let open_ ?loc ?attrs a b c = mk ?loc ?attrs (Pexp_open (a, b, c))
184+
let extension ?loc ?attrs a = mk ?loc ?attrs (Pexp_extension a)
185+
let unreachable ?loc ?attrs () = mk ?loc ?attrs Pexp_unreachable
186+
187+
let case lhs ?guard rhs = {pc_lhs = lhs; pc_guard = guard; pc_rhs = rhs}
188+
end
189+
190+
module Mty = struct
191+
let mk ?(loc = !default_loc) ?(attrs = []) d =
192+
{pmty_desc = d; pmty_loc = loc; pmty_attributes = attrs}
193+
let attr d a = {d with pmty_attributes = d.pmty_attributes @ [a]}
194+
195+
let ident ?loc ?attrs a = mk ?loc ?attrs (Pmty_ident a)
196+
let alias ?loc ?attrs a = mk ?loc ?attrs (Pmty_alias a)
197+
let signature ?loc ?attrs a = mk ?loc ?attrs (Pmty_signature a)
198+
let functor_ ?loc ?attrs a b c = mk ?loc ?attrs (Pmty_functor (a, b, c))
199+
let with_ ?loc ?attrs a b = mk ?loc ?attrs (Pmty_with (a, b))
200+
let typeof_ ?loc ?attrs a = mk ?loc ?attrs (Pmty_typeof a)
201+
let extension ?loc ?attrs a = mk ?loc ?attrs (Pmty_extension a)
202+
end
203+
204+
module Mod = struct
205+
let mk ?(loc = !default_loc) ?(attrs = []) d =
206+
{pmod_desc = d; pmod_loc = loc; pmod_attributes = attrs}
207+
let attr d a = {d with pmod_attributes = d.pmod_attributes @ [a]}
208+
209+
let ident ?loc ?attrs x = mk ?loc ?attrs (Pmod_ident x)
210+
let structure ?loc ?attrs x = mk ?loc ?attrs (Pmod_structure x)
211+
let functor_ ?loc ?attrs arg arg_ty body =
212+
mk ?loc ?attrs (Pmod_functor (arg, arg_ty, body))
213+
let apply ?loc ?attrs m1 m2 = mk ?loc ?attrs (Pmod_apply (m1, m2))
214+
let constraint_ ?loc ?attrs m mty = mk ?loc ?attrs (Pmod_constraint (m, mty))
215+
let unpack ?loc ?attrs e = mk ?loc ?attrs (Pmod_unpack e)
216+
let extension ?loc ?attrs a = mk ?loc ?attrs (Pmod_extension a)
217+
end
218+
219+
module Sig = struct
220+
let mk ?(loc = !default_loc) d = {psig_desc = d; psig_loc = loc}
221+
222+
let value ?loc a = mk ?loc (Psig_value a)
223+
let type_ ?loc rec_flag a = mk ?loc (Psig_type (rec_flag, a))
224+
let type_extension ?loc a = mk ?loc (Psig_typext a)
225+
let exception_ ?loc a = mk ?loc (Psig_exception a)
226+
let module_ ?loc a = mk ?loc (Psig_module a)
227+
let rec_module ?loc a = mk ?loc (Psig_recmodule a)
228+
let modtype ?loc a = mk ?loc (Psig_modtype a)
229+
let open_ ?loc a = mk ?loc (Psig_open a)
230+
let include_ ?loc a = mk ?loc (Psig_include a)
231+
232+
let extension ?loc ?(attrs = []) a = mk ?loc (Psig_extension (a, attrs))
233+
let attribute ?loc a = mk ?loc (Psig_attribute a)
234+
end
235+
236+
module Str = struct
237+
let mk ?(loc = !default_loc) d = {pstr_desc = d; pstr_loc = loc}
238+
239+
let eval ?loc ?(attrs = []) a = mk ?loc (Pstr_eval (a, attrs))
240+
let value ?loc a b = mk ?loc (Pstr_value (a, b))
241+
let primitive ?loc a = mk ?loc (Pstr_primitive a)
242+
let type_ ?loc rec_flag a = mk ?loc (Pstr_type (rec_flag, a))
243+
let type_extension ?loc a = mk ?loc (Pstr_typext a)
244+
let exception_ ?loc a = mk ?loc (Pstr_exception a)
245+
let module_ ?loc a = mk ?loc (Pstr_module a)
246+
let rec_module ?loc a = mk ?loc (Pstr_recmodule a)
247+
let modtype ?loc a = mk ?loc (Pstr_modtype a)
248+
let open_ ?loc a = mk ?loc (Pstr_open a)
249+
let include_ ?loc a = mk ?loc (Pstr_include a)
250+
let extension ?loc ?(attrs = []) a = mk ?loc (Pstr_extension (a, attrs))
251+
let attribute ?loc a = mk ?loc (Pstr_attribute a)
252+
end
253+
254+
module Val = struct
255+
let mk ?(loc = !default_loc) ?(attrs = []) ?(prim = []) name typ =
256+
{
257+
pval_name = name;
258+
pval_type = typ;
259+
pval_attributes = attrs;
260+
pval_loc = loc;
261+
pval_prim = prim;
262+
}
263+
end
264+
265+
module Md = struct
266+
let mk ?(loc = !default_loc) ?(attrs = []) name typ =
267+
{pmd_name = name; pmd_type = typ; pmd_attributes = attrs; pmd_loc = loc}
268+
end
269+
270+
module Mtd = struct
271+
let mk ?(loc = !default_loc) ?(attrs = []) ?typ name =
272+
{pmtd_name = name; pmtd_type = typ; pmtd_attributes = attrs; pmtd_loc = loc}
273+
end
274+
275+
module Mb = struct
276+
let mk ?(loc = !default_loc) ?(attrs = []) name expr =
277+
{pmb_name = name; pmb_expr = expr; pmb_attributes = attrs; pmb_loc = loc}
278+
end
279+
280+
module Opn = struct
281+
let mk ?(loc = !default_loc) ?(attrs = []) ?(override = Fresh) lid =
282+
{
283+
popen_lid = lid;
284+
popen_override = override;
285+
popen_loc = loc;
286+
popen_attributes = attrs;
287+
}
288+
end
289+
290+
module Incl = struct
291+
let mk ?(loc = !default_loc) ?(attrs = []) mexpr =
292+
{pincl_mod = mexpr; pincl_loc = loc; pincl_attributes = attrs}
293+
end
294+
295+
module Vb = struct
296+
let mk ?(loc = !default_loc) ?(attrs = []) pat expr =
297+
{pvb_pat = pat; pvb_expr = expr; pvb_attributes = attrs; pvb_loc = loc}
298+
end
299+
300+
module Type = struct
301+
let mk ?(loc = !default_loc) ?(attrs = []) ?(params = []) ?(cstrs = [])
302+
?(kind = Ptype_abstract) ?(priv = Public) ?manifest name =
303+
{
304+
ptype_name = name;
305+
ptype_params = params;
306+
ptype_cstrs = cstrs;
307+
ptype_kind = kind;
308+
ptype_private = priv;
309+
ptype_manifest = manifest;
310+
ptype_attributes = attrs;
311+
ptype_loc = loc;
312+
}
313+
314+
let constructor ?(loc = !default_loc) ?(attrs = []) ?(args = Pcstr_tuple [])
315+
?res name =
316+
{
317+
pcd_name = name;
318+
pcd_args = args;
319+
pcd_res = res;
320+
pcd_loc = loc;
321+
pcd_attributes = attrs;
322+
}
323+
324+
let field ?(loc = !default_loc) ?(attrs = []) ?(mut = Immutable) name typ =
325+
{
326+
pld_name = name;
327+
pld_mutable = mut;
328+
pld_type = typ;
329+
pld_loc = loc;
330+
pld_attributes = attrs;
331+
}
332+
end
333+
334+
(** Type extensions *)
335+
module Te = struct
336+
let mk ?(attrs = []) ?(params = []) ?(priv = Public) path constructors =
337+
{
338+
ptyext_path = path;
339+
ptyext_params = params;
340+
ptyext_constructors = constructors;
341+
ptyext_private = priv;
342+
ptyext_attributes = attrs;
343+
}
344+
345+
let constructor ?(loc = !default_loc) ?(attrs = []) name kind =
346+
{
347+
pext_name = name;
348+
pext_kind = kind;
349+
pext_loc = loc;
350+
pext_attributes = attrs;
351+
}
352+
353+
let decl ?(loc = !default_loc) ?(attrs = []) ?(args = Pcstr_tuple []) ?res
354+
name =
355+
{
356+
pext_name = name;
357+
pext_kind = Pext_decl (args, res);
358+
pext_loc = loc;
359+
pext_attributes = attrs;
360+
}
361+
362+
let rebind ?(loc = !default_loc) ?(attrs = []) name lid =
363+
{
364+
pext_name = name;
365+
pext_kind = Pext_rebind lid;
366+
pext_loc = loc;
367+
pext_attributes = attrs;
368+
}
369+
end

0 commit comments

Comments
 (0)