2012-11-08 06:30:38 -08:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* 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 Q Public License version 1.0. *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* A generic Parsetree mapping class *)
|
|
|
|
|
2012-06-29 02:36:32 -07:00
|
|
|
open Location
|
|
|
|
open Config
|
|
|
|
open Parsetree
|
|
|
|
open Asttypes
|
|
|
|
|
|
|
|
(* First, some helpers to build AST fragments *)
|
|
|
|
|
2012-06-29 03:04:17 -07:00
|
|
|
let map_snd f (x, y) = (x, f y)
|
2012-07-24 01:40:50 -07:00
|
|
|
let map_tuple f1 f2 (x, y) = (f1 x, f2 y)
|
2013-03-04 09:39:07 -08:00
|
|
|
let map_tuple3 f1 f2 f3 (x, y, z) = (f1 x, f2 y, f3 z)
|
2012-07-24 01:40:50 -07:00
|
|
|
let map_opt f = function None -> None | Some x -> Some (f x)
|
2012-06-29 02:36:32 -07:00
|
|
|
|
2013-01-07 06:13:01 -08:00
|
|
|
let map_loc sub {loc; txt} = {loc = sub # location loc; txt}
|
2013-03-04 04:54:57 -08:00
|
|
|
let map_attributes sub attrs = List.map (sub # attribute) attrs
|
2013-01-07 06:13:01 -08:00
|
|
|
|
2012-09-18 08:55:30 -07:00
|
|
|
module T = struct
|
|
|
|
(* Type expressions for the core language *)
|
|
|
|
|
|
|
|
let mk ?(loc = Location.none) x = {ptyp_desc = x; ptyp_loc = loc}
|
|
|
|
let any ?loc () = mk ?loc Ptyp_any
|
|
|
|
let var ?loc a = mk ?loc (Ptyp_var a)
|
|
|
|
let arrow ?loc a b c = mk ?loc (Ptyp_arrow (a, b, c))
|
|
|
|
let tuple ?loc a = mk ?loc (Ptyp_tuple a)
|
|
|
|
let constr ?loc a b = mk ?loc (Ptyp_constr (a, b))
|
|
|
|
let object_ ?loc a = mk ?loc (Ptyp_object a)
|
|
|
|
let class_ ?loc a b c = mk ?loc (Ptyp_class (a, b, c))
|
|
|
|
let alias ?loc a b = mk ?loc (Ptyp_alias (a, b))
|
|
|
|
let variant ?loc a b c = mk ?loc (Ptyp_variant (a, b, c))
|
|
|
|
let poly ?loc a b = mk ?loc (Ptyp_poly (a, b))
|
|
|
|
let package ?loc a b = mk ?loc (Ptyp_package (a, b))
|
2013-03-01 04:44:04 -08:00
|
|
|
let attribute ?loc a b = mk ?loc (Ptyp_attribute (a, b))
|
|
|
|
let extension ?loc a = mk ?loc (Ptyp_extension a)
|
2012-09-18 08:55:30 -07:00
|
|
|
|
|
|
|
let field_type ?(loc = Location.none) x = {pfield_desc = x; pfield_loc = loc}
|
|
|
|
let field ?loc s t =
|
|
|
|
let t =
|
|
|
|
(* The type-checker expects the field to be a Ptyp_poly. Maybe
|
|
|
|
it should wrap the type automatically... *)
|
|
|
|
match t.ptyp_desc with
|
|
|
|
| Ptyp_poly _ -> t
|
|
|
|
| _ -> poly ?loc [] t
|
|
|
|
in
|
|
|
|
field_type ?loc (Pfield (s, t))
|
|
|
|
let field_var ?loc () = field_type ?loc Pfield_var
|
|
|
|
|
2013-01-07 06:13:01 -08:00
|
|
|
let core_field_type sub {pfield_desc = desc; pfield_loc = loc} =
|
|
|
|
let loc = sub # location loc in
|
|
|
|
match desc with
|
|
|
|
| Pfield (s, d) -> field ~loc:(sub # location loc) s (sub # typ d)
|
|
|
|
| Pfield_var -> field_var ~loc ()
|
2012-09-18 08:55:30 -07:00
|
|
|
|
|
|
|
let row_field sub = function
|
|
|
|
| Rtag (l, b, tl) -> Rtag (l, b, List.map (sub # typ) tl)
|
|
|
|
| Rinherit t -> Rinherit (sub # typ t)
|
|
|
|
|
|
|
|
let map sub {ptyp_desc = desc; ptyp_loc = loc} =
|
2013-01-07 06:13:01 -08:00
|
|
|
let loc = sub # location loc in
|
2012-09-18 08:55:30 -07:00
|
|
|
match desc with
|
|
|
|
| Ptyp_any -> any ~loc ()
|
|
|
|
| Ptyp_var s -> var ~loc s
|
|
|
|
| Ptyp_arrow (lab, t1, t2) -> arrow ~loc lab (sub # typ t1) (sub # typ t2)
|
|
|
|
| Ptyp_tuple tyl -> tuple ~loc (List.map (sub # typ) tyl)
|
2013-01-07 06:13:01 -08:00
|
|
|
| Ptyp_constr (lid, tl) -> constr ~loc (map_loc sub lid) (List.map (sub # typ) tl)
|
2012-09-18 08:55:30 -07:00
|
|
|
| Ptyp_object l -> object_ ~loc (List.map (core_field_type sub) l)
|
2013-01-07 06:13:01 -08:00
|
|
|
| Ptyp_class (lid, tl, ll) -> class_ ~loc (map_loc sub lid) (List.map (sub # typ) tl) ll
|
2012-09-18 08:55:30 -07:00
|
|
|
| Ptyp_alias (t, s) -> alias ~loc (sub # typ t) s
|
|
|
|
| Ptyp_variant (rl, b, ll) -> variant ~loc (List.map (row_field sub) rl) b ll
|
|
|
|
| Ptyp_poly (sl, t) -> poly ~loc sl (sub # typ t)
|
2013-01-07 06:13:01 -08:00
|
|
|
| Ptyp_package (lid, l) -> package ~loc (map_loc sub lid) (List.map (map_tuple (map_loc sub) (sub # typ)) l)
|
2013-03-01 04:44:04 -08:00
|
|
|
| Ptyp_attribute (body, x) -> attribute ~loc (sub # typ body) (sub # attribute x)
|
|
|
|
| Ptyp_extension x -> extension ~loc (sub # extension x)
|
2012-09-18 08:55:30 -07:00
|
|
|
|
2013-03-06 03:47:59 -08:00
|
|
|
let map_type_declaration sub
|
|
|
|
{ptype_name; ptype_params; ptype_cstrs;
|
|
|
|
ptype_kind;
|
|
|
|
ptype_private;
|
|
|
|
ptype_manifest;
|
|
|
|
ptype_variance;
|
|
|
|
ptype_attributes;
|
|
|
|
ptype_loc} =
|
|
|
|
{ptype_name = map_loc sub ptype_name;
|
|
|
|
ptype_params = List.map (map_opt (map_loc sub)) ptype_params;
|
|
|
|
ptype_private;
|
|
|
|
ptype_variance;
|
2012-09-18 08:55:30 -07:00
|
|
|
ptype_cstrs =
|
|
|
|
List.map
|
2013-01-07 06:13:01 -08:00
|
|
|
(fun (ct1, ct2, loc) -> sub # typ ct1, sub # typ ct2, sub # location loc)
|
2013-03-06 03:47:59 -08:00
|
|
|
ptype_cstrs;
|
|
|
|
ptype_kind = sub # type_kind ptype_kind;
|
|
|
|
ptype_manifest = map_opt (sub # typ) ptype_manifest;
|
|
|
|
ptype_loc = sub # location ptype_loc;
|
|
|
|
ptype_attributes = map_attributes sub ptype_attributes;
|
2012-09-18 08:55:30 -07:00
|
|
|
}
|
|
|
|
|
2013-03-04 07:35:47 -08:00
|
|
|
let constructor_decl ?res ?(loc = Location.none) ?(attributes = []) name args =
|
|
|
|
{
|
|
|
|
pcd_name = name;
|
|
|
|
pcd_args = args;
|
|
|
|
pcd_res = res;
|
|
|
|
pcd_loc = loc;
|
|
|
|
pcd_attributes = attributes;
|
|
|
|
}
|
|
|
|
|
2013-03-06 05:51:18 -08:00
|
|
|
let label_decl ?(loc = Location.none) ?(mut = Immutable) ?(attributes = []) name typ =
|
|
|
|
{
|
|
|
|
pld_name = name;
|
|
|
|
pld_type = typ;
|
|
|
|
pld_loc = loc;
|
|
|
|
pld_mutable = mut;
|
|
|
|
pld_attributes = attributes;
|
|
|
|
}
|
2013-03-04 07:35:47 -08:00
|
|
|
|
2012-09-18 08:55:30 -07:00
|
|
|
let map_type_kind sub = function
|
|
|
|
| Ptype_abstract -> Ptype_abstract
|
2013-03-06 05:51:18 -08:00
|
|
|
| Ptype_variant l -> Ptype_variant (List.map (sub # constructor_declaration) l)
|
|
|
|
| Ptype_record l -> Ptype_record (List.map (sub # label_declaration) l)
|
2012-09-18 08:55:30 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
module CT = struct
|
|
|
|
(* Type expressions for the class language *)
|
|
|
|
|
|
|
|
let mk ?(loc = Location.none) x = {pcty_loc = loc; pcty_desc = x}
|
|
|
|
|
|
|
|
let constr ?loc a b = mk ?loc (Pcty_constr (a, b))
|
|
|
|
let signature ?loc a = mk ?loc (Pcty_signature a)
|
|
|
|
let fun_ ?loc a b c = mk ?loc (Pcty_fun (a, b, c))
|
|
|
|
|
|
|
|
let map sub {pcty_loc = loc; pcty_desc = desc} =
|
2013-01-07 06:13:01 -08:00
|
|
|
let loc = sub # location loc in
|
2012-09-18 08:55:30 -07:00
|
|
|
match desc with
|
2013-01-07 06:13:01 -08:00
|
|
|
| Pcty_constr (lid, tys) -> constr ~loc (map_loc sub lid) (List.map (sub # typ) tys)
|
2012-09-18 08:55:30 -07:00
|
|
|
| Pcty_signature x -> signature ~loc (sub # class_signature x)
|
|
|
|
| Pcty_fun (lab, t, ct) ->
|
|
|
|
fun_ ~loc lab
|
|
|
|
(sub # typ t)
|
|
|
|
(sub # class_type ct)
|
|
|
|
|
|
|
|
let mk_field ?(loc = Location.none) x = {pctf_desc = x; pctf_loc = loc}
|
|
|
|
|
|
|
|
let inher ?loc a = mk_field ?loc (Pctf_inher a)
|
|
|
|
let val_ ?loc a b c d = mk_field ?loc (Pctf_val (a, b, c, d))
|
|
|
|
let virt ?loc a b c = mk_field ?loc (Pctf_virt (a, b, c))
|
|
|
|
let meth ?loc a b c = mk_field ?loc (Pctf_meth (a, b, c))
|
|
|
|
let cstr ?loc a b = mk_field ?loc (Pctf_cstr (a, b))
|
|
|
|
|
|
|
|
let map_field sub {pctf_desc = desc; pctf_loc = loc} =
|
2013-01-07 06:13:01 -08:00
|
|
|
let loc = sub # location loc in
|
2012-09-18 08:55:30 -07:00
|
|
|
match desc with
|
|
|
|
| Pctf_inher ct -> inher ~loc (sub # class_type ct)
|
|
|
|
| Pctf_val (s, m, v, t) -> val_ ~loc s m v (sub # typ t)
|
|
|
|
| Pctf_virt (s, p, t) -> virt ~loc s p (sub # typ t)
|
|
|
|
| Pctf_meth (s, p, t) -> meth ~loc s p (sub # typ t)
|
|
|
|
| Pctf_cstr (t1, t2) -> cstr ~loc (sub # typ t1) (sub # typ t2)
|
|
|
|
|
|
|
|
let map_signature sub {pcsig_self; pcsig_fields; pcsig_loc} =
|
|
|
|
{
|
|
|
|
pcsig_self = sub # typ pcsig_self;
|
|
|
|
pcsig_fields = List.map (sub # class_type_field) pcsig_fields;
|
2013-01-07 06:13:01 -08:00
|
|
|
pcsig_loc = sub # location pcsig_loc ;
|
2012-09-18 08:55:30 -07:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
module MT = struct
|
|
|
|
(* Type expressions for the module language *)
|
|
|
|
|
|
|
|
let mk ?(loc = Location.none) x = {pmty_desc = x; pmty_loc = loc}
|
|
|
|
let ident ?loc a = mk ?loc (Pmty_ident a)
|
|
|
|
let signature ?loc a = mk ?loc (Pmty_signature a)
|
|
|
|
let functor_ ?loc a b c = mk ?loc (Pmty_functor (a, b, c))
|
|
|
|
let with_ ?loc a b = mk ?loc (Pmty_with (a, b))
|
|
|
|
let typeof_ ?loc a = mk ?loc (Pmty_typeof a)
|
2013-03-04 06:11:15 -08:00
|
|
|
let attribute ?loc a b = mk ?loc (Pmty_attribute (a, b))
|
|
|
|
let extension ?loc a = mk ?loc (Pmty_extension a)
|
2012-09-18 08:55:30 -07:00
|
|
|
|
|
|
|
let map sub {pmty_desc = desc; pmty_loc = loc} =
|
2013-01-07 06:13:01 -08:00
|
|
|
let loc = sub # location loc in
|
2012-09-18 08:55:30 -07:00
|
|
|
match desc with
|
2013-01-07 06:13:01 -08:00
|
|
|
| Pmty_ident s -> ident ~loc (map_loc sub s)
|
2012-09-18 08:55:30 -07:00
|
|
|
| Pmty_signature sg -> signature ~loc (sub # signature sg)
|
2013-01-07 06:13:01 -08:00
|
|
|
| Pmty_functor (s, mt1, mt2) -> functor_ ~loc (map_loc sub s) (sub # module_type mt1) (sub # module_type mt2)
|
|
|
|
| Pmty_with (mt, l) -> with_ ~loc (sub # module_type mt) (List.map (map_tuple (map_loc sub) (sub # with_constraint)) l)
|
2012-09-18 08:55:30 -07:00
|
|
|
| Pmty_typeof me -> typeof_ ~loc (sub # module_expr me)
|
2013-03-04 06:11:15 -08:00
|
|
|
| Pmty_attribute (body, x) -> attribute ~loc (sub # module_type body) (sub # attribute x)
|
|
|
|
| Pmty_extension x -> extension ~loc (sub # extension x)
|
2012-09-18 08:55:30 -07:00
|
|
|
|
|
|
|
let map_with_constraint sub = function
|
|
|
|
| Pwith_type d -> Pwith_type (sub # type_declaration d)
|
2013-01-07 06:13:01 -08:00
|
|
|
| Pwith_module s -> Pwith_module (map_loc sub s)
|
2012-09-18 08:55:30 -07:00
|
|
|
| Pwith_typesubst d -> Pwith_typesubst (sub # type_declaration d)
|
2013-01-07 06:13:01 -08:00
|
|
|
| Pwith_modsubst s -> Pwith_modsubst (map_loc sub s)
|
2012-09-18 08:55:30 -07:00
|
|
|
|
|
|
|
let mk_item ?(loc = Location.none) x = {psig_desc = x; psig_loc = loc}
|
|
|
|
|
2013-03-06 04:00:18 -08:00
|
|
|
let value ?loc a = mk_item ?loc (Psig_value a)
|
2012-09-18 08:55:30 -07:00
|
|
|
let type_ ?loc a = mk_item ?loc (Psig_type a)
|
2013-03-05 04:37:17 -08:00
|
|
|
let exception_ ?loc a = mk_item ?loc (Psig_exception a)
|
2013-03-04 09:39:07 -08:00
|
|
|
let module_ ?loc a = mk_item ?loc (Psig_module a)
|
2012-09-18 08:55:30 -07:00
|
|
|
let rec_module ?loc a = mk_item ?loc (Psig_recmodule a)
|
2013-03-06 04:14:02 -08:00
|
|
|
let modtype ?loc a = mk_item ?loc (Psig_modtype a)
|
2013-03-04 08:36:32 -08:00
|
|
|
let open_ ?loc ?(attributes = []) a = mk_item ?loc (Psig_open (a, attributes))
|
|
|
|
let include_ ?loc ?(attributes = []) a = mk_item ?loc (Psig_include (a, attributes))
|
2012-09-18 08:55:30 -07:00
|
|
|
let class_ ?loc a = mk_item ?loc (Psig_class a)
|
|
|
|
let class_type ?loc a = mk_item ?loc (Psig_class_type a)
|
2013-03-05 03:46:25 -08:00
|
|
|
let extension ?loc ?(attributes = []) a = mk_item ?loc (Psig_extension (a, attributes))
|
2013-03-06 04:27:32 -08:00
|
|
|
let attribute ?loc a = mk_item ?loc (Psig_attribute a)
|
2012-09-18 08:55:30 -07:00
|
|
|
|
|
|
|
let map_signature_item sub {psig_desc = desc; psig_loc = loc} =
|
2013-01-07 06:13:01 -08:00
|
|
|
let loc = sub # location loc in
|
2012-09-18 08:55:30 -07:00
|
|
|
match desc with
|
2013-03-06 04:00:18 -08:00
|
|
|
| Psig_value vd -> value ~loc (sub # value_description vd)
|
2013-03-06 03:47:59 -08:00
|
|
|
| Psig_type l -> type_ ~loc (List.map (sub # type_declaration) l)
|
2013-03-05 04:37:17 -08:00
|
|
|
| Psig_exception ed -> exception_ ~loc (sub # exception_declaration ed)
|
2013-03-04 09:39:07 -08:00
|
|
|
| Psig_module x -> module_ ~loc (sub # module_declaration x)
|
|
|
|
| Psig_recmodule l -> rec_module ~loc (List.map (sub # module_declaration) l)
|
2013-03-06 04:14:02 -08:00
|
|
|
| Psig_modtype x -> modtype ~loc (sub # module_type_declaration x)
|
2013-03-04 08:36:32 -08:00
|
|
|
| Psig_open (lid, attrs) -> open_ ~loc ~attributes:(map_attributes sub attrs) (map_loc sub lid)
|
|
|
|
| Psig_include (mt, attrs) -> include_ ~loc (sub # module_type mt) ~attributes:(map_attributes sub attrs)
|
2012-09-18 08:55:30 -07:00
|
|
|
| Psig_class l -> class_ ~loc (List.map (sub # class_description) l)
|
|
|
|
| Psig_class_type l -> class_type ~loc (List.map (sub # class_type_declaration) l)
|
2013-03-05 03:46:25 -08:00
|
|
|
| Psig_extension (x, attrs) -> extension ~loc (sub # extension x) ~attributes:(map_attributes sub attrs)
|
2013-03-06 04:27:32 -08:00
|
|
|
| Psig_attribute x -> attribute ~loc (sub # attribute x)
|
2012-09-18 08:55:30 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
module M = struct
|
|
|
|
(* Value expressions for the module language *)
|
|
|
|
|
|
|
|
let mk ?(loc = Location.none) x = {pmod_desc = x; pmod_loc = loc}
|
|
|
|
let ident ?loc x = mk ?loc (Pmod_ident x)
|
|
|
|
let structure ?loc x = mk ?loc (Pmod_structure x)
|
|
|
|
let functor_ ?loc arg arg_ty body = mk ?loc (Pmod_functor (arg, arg_ty, body))
|
|
|
|
let apply ?loc m1 m2 = mk ?loc (Pmod_apply (m1, m2))
|
|
|
|
let constraint_ ?loc m mty = mk ?loc (Pmod_constraint (m, mty))
|
|
|
|
let unpack ?loc e = mk ?loc (Pmod_unpack e)
|
2013-03-04 04:54:57 -08:00
|
|
|
let attribute ?loc a b = mk ?loc (Pmod_attribute (a, b))
|
|
|
|
let extension ?loc a = mk ?loc (Pmod_extension a)
|
2012-09-18 08:55:30 -07:00
|
|
|
|
|
|
|
let map sub {pmod_loc = loc; pmod_desc = desc} =
|
2013-01-07 06:13:01 -08:00
|
|
|
let loc = sub # location loc in
|
2012-09-18 08:55:30 -07:00
|
|
|
match desc with
|
2013-01-07 06:13:01 -08:00
|
|
|
| Pmod_ident x -> ident ~loc (map_loc sub x)
|
2012-09-18 08:55:30 -07:00
|
|
|
| Pmod_structure str -> structure ~loc (sub # structure str)
|
2013-01-07 06:13:01 -08:00
|
|
|
| Pmod_functor (arg, arg_ty, body) -> functor_ ~loc (map_loc sub arg) (sub # module_type arg_ty) (sub # module_expr body)
|
2012-09-18 08:55:30 -07:00
|
|
|
| Pmod_apply (m1, m2) -> apply ~loc (sub # module_expr m1) (sub # module_expr m2)
|
|
|
|
| Pmod_constraint (m, mty) -> constraint_ ~loc (sub # module_expr m) (sub # module_type mty)
|
|
|
|
| Pmod_unpack e -> unpack ~loc (sub # expr e)
|
2013-03-04 04:54:57 -08:00
|
|
|
| Pmod_attribute (body, x) -> attribute ~loc (sub # module_expr body) (sub # attribute x)
|
|
|
|
| Pmod_extension x -> extension ~loc (sub # extension x)
|
2012-09-18 08:55:30 -07:00
|
|
|
|
|
|
|
let mk_item ?(loc = Location.none) x = {pstr_desc = x; pstr_loc = loc}
|
|
|
|
let eval ?loc a = mk_item ?loc (Pstr_eval a)
|
|
|
|
let value ?loc a b = mk_item ?loc (Pstr_value (a, b))
|
2013-03-06 04:00:18 -08:00
|
|
|
let primitive ?loc a = mk_item ?loc (Pstr_primitive a)
|
2012-09-18 08:55:30 -07:00
|
|
|
let type_ ?loc a = mk_item ?loc (Pstr_type a)
|
2013-03-05 04:37:17 -08:00
|
|
|
let exception_ ?loc a = mk_item ?loc (Pstr_exception a)
|
2013-03-05 04:44:40 -08:00
|
|
|
let exn_rebind ?loc ?(attributes = []) a b = mk_item ?loc (Pstr_exn_rebind (a, b, attributes))
|
2013-03-05 08:50:05 -08:00
|
|
|
let module_ ?loc a = mk_item ?loc (Pstr_module a)
|
2012-09-18 08:55:30 -07:00
|
|
|
let rec_module ?loc a = mk_item ?loc (Pstr_recmodule a)
|
2013-03-06 02:49:44 -08:00
|
|
|
let modtype ?loc a = mk_item ?loc (Pstr_modtype a)
|
2013-03-04 04:54:57 -08:00
|
|
|
let open_ ?loc ?(attributes = []) a = mk_item ?loc (Pstr_open (a, attributes))
|
2012-09-18 08:55:30 -07:00
|
|
|
let class_ ?loc a = mk_item ?loc (Pstr_class a)
|
|
|
|
let class_type ?loc a = mk_item ?loc (Pstr_class_type a)
|
2013-03-04 04:54:57 -08:00
|
|
|
let include_ ?loc ?(attributes = []) a = mk_item ?loc (Pstr_include (a, attributes))
|
2013-03-05 03:46:25 -08:00
|
|
|
let extension ?loc ?(attributes = []) a = mk_item ?loc (Pstr_extension (a, attributes))
|
2013-03-06 04:27:32 -08:00
|
|
|
let attribute ?loc a = mk_item ?loc (Pstr_attribute a)
|
2012-09-18 08:55:30 -07:00
|
|
|
|
2013-03-01 04:44:04 -08:00
|
|
|
let map_structure_item sub {pstr_loc = loc; pstr_desc = desc} =
|
2013-01-07 06:13:01 -08:00
|
|
|
let loc = sub # location loc in
|
2012-07-24 01:40:50 -07:00
|
|
|
match desc with
|
|
|
|
| Pstr_eval x -> eval ~loc (sub # expr x)
|
|
|
|
| Pstr_value (r, pel) -> value ~loc r (List.map (map_tuple (sub # pat) (sub # expr)) pel)
|
2013-03-06 04:00:18 -08:00
|
|
|
| Pstr_primitive vd -> primitive ~loc (sub # value_description vd)
|
2013-03-06 03:47:59 -08:00
|
|
|
| Pstr_type l -> type_ ~loc (List.map (sub # type_declaration) l)
|
2013-03-05 04:37:17 -08:00
|
|
|
| Pstr_exception ed -> exception_ ~loc (sub # exception_declaration ed)
|
2013-03-05 04:44:40 -08:00
|
|
|
| Pstr_exn_rebind (s, lid, attrs) -> exn_rebind ~loc (map_loc sub s) (map_loc sub lid) ~attributes:(map_attributes sub attrs)
|
2013-03-05 08:50:05 -08:00
|
|
|
| Pstr_module x -> module_ ~loc (sub # module_binding x)
|
|
|
|
| Pstr_recmodule l -> rec_module ~loc (List.map (sub # module_binding) l)
|
2013-03-06 02:49:44 -08:00
|
|
|
| Pstr_modtype x -> modtype ~loc (sub # module_type_binding x)
|
2013-03-04 04:54:57 -08:00
|
|
|
| Pstr_open (lid, attrs) -> open_ ~loc ~attributes:(map_attributes sub attrs) (map_loc sub lid)
|
2012-07-24 01:40:50 -07:00
|
|
|
| Pstr_class l -> class_ ~loc (List.map (sub # class_declaration) l)
|
|
|
|
| Pstr_class_type l -> class_type ~loc (List.map (sub # class_type_declaration) l)
|
2013-03-04 04:54:57 -08:00
|
|
|
| Pstr_include (e, attrs) -> include_ ~loc (sub # module_expr e) ~attributes:(map_attributes sub attrs)
|
2013-03-05 03:46:25 -08:00
|
|
|
| Pstr_extension (x, attrs) -> extension ~loc (sub # extension x) ~attributes:(map_attributes sub attrs)
|
2013-03-06 04:27:32 -08:00
|
|
|
| Pstr_attribute x -> attribute ~loc (sub # attribute x)
|
2012-06-29 02:36:32 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
module E = struct
|
2012-09-18 08:55:30 -07:00
|
|
|
(* Value expressions for the core language *)
|
2012-06-29 02:36:32 -07:00
|
|
|
|
|
|
|
let mk ?(loc = Location.none) x = {pexp_desc = x; pexp_loc = loc}
|
2012-07-24 01:40:50 -07:00
|
|
|
|
|
|
|
let ident ?loc a = mk ?loc (Pexp_ident a)
|
2012-09-18 08:55:30 -07:00
|
|
|
let constant ?loc a = mk ?loc (Pexp_constant a)
|
2012-07-24 01:40:50 -07:00
|
|
|
let let_ ?loc a b c = mk ?loc (Pexp_let (a, b, c))
|
2012-09-18 08:55:30 -07:00
|
|
|
let function_ ?loc a b c = mk ?loc (Pexp_function (a, b, c))
|
|
|
|
let apply ?loc a b = mk ?loc (Pexp_apply (a, b))
|
2012-07-24 01:40:50 -07:00
|
|
|
let match_ ?loc a b = mk ?loc (Pexp_match (a, b))
|
|
|
|
let try_ ?loc a b = mk ?loc (Pexp_try (a, b))
|
|
|
|
let tuple ?loc a = mk ?loc (Pexp_tuple a)
|
2012-09-18 08:55:30 -07:00
|
|
|
let construct ?loc a b c = mk ?loc (Pexp_construct (a, b, c))
|
2012-07-24 01:40:50 -07:00
|
|
|
let variant ?loc a b = mk ?loc (Pexp_variant (a, b))
|
|
|
|
let record ?loc a b = mk ?loc (Pexp_record (a, b))
|
|
|
|
let field ?loc a b = mk ?loc (Pexp_field (a, b))
|
|
|
|
let setfield ?loc a b c = mk ?loc (Pexp_setfield (a, b, c))
|
|
|
|
let array ?loc a = mk ?loc (Pexp_array a)
|
|
|
|
let ifthenelse ?loc a b c = mk ?loc (Pexp_ifthenelse (a, b, c))
|
|
|
|
let sequence ?loc a b = mk ?loc (Pexp_sequence (a, b))
|
|
|
|
let while_ ?loc a b = mk ?loc (Pexp_while (a, b))
|
|
|
|
let for_ ?loc a b c d e = mk ?loc (Pexp_for (a, b, c, d, e))
|
|
|
|
let constraint_ ?loc a b c = mk ?loc (Pexp_constraint (a, b, c))
|
|
|
|
let when_ ?loc a b = mk ?loc (Pexp_when (a, b))
|
|
|
|
let send ?loc a b = mk ?loc (Pexp_send (a, b))
|
|
|
|
let new_ ?loc a = mk ?loc (Pexp_new a)
|
|
|
|
let setinstvar ?loc a b = mk ?loc (Pexp_setinstvar (a, b))
|
|
|
|
let override ?loc a = mk ?loc (Pexp_override a)
|
|
|
|
let letmodule ?loc (a, b, c)= mk ?loc (Pexp_letmodule (a, b, c))
|
|
|
|
let assert_ ?loc a = mk ?loc (Pexp_assert a)
|
|
|
|
let assertfalse ?loc () = mk ?loc Pexp_assertfalse
|
|
|
|
let lazy_ ?loc a = mk ?loc (Pexp_lazy a)
|
|
|
|
let poly ?loc a b = mk ?loc (Pexp_poly (a, b))
|
|
|
|
let object_ ?loc a = mk ?loc (Pexp_object a)
|
|
|
|
let newtype ?loc a b = mk ?loc (Pexp_newtype (a, b))
|
|
|
|
let pack ?loc a = mk ?loc (Pexp_pack a)
|
|
|
|
let open_ ?loc a b = mk ?loc (Pexp_open (a, b))
|
2013-03-01 04:44:04 -08:00
|
|
|
let attribute ?loc a b = mk ?loc (Pexp_attribute (a, b))
|
|
|
|
let extension ?loc a = mk ?loc (Pexp_extension a)
|
2012-07-24 01:40:50 -07:00
|
|
|
|
2012-06-29 02:36:32 -07:00
|
|
|
let lid ?(loc = Location.none) lid = ident ~loc (mkloc (Longident.parse lid) loc)
|
2012-09-18 08:55:30 -07:00
|
|
|
let apply_nolabs ?loc f el = apply ?loc f (List.map (fun e -> ("", e)) el)
|
|
|
|
let strconst ?loc x = constant ?loc (Const_string x)
|
2012-07-24 01:40:50 -07:00
|
|
|
|
|
|
|
let map sub {pexp_loc = loc; pexp_desc = desc} =
|
2013-01-07 06:13:01 -08:00
|
|
|
let loc = sub # location loc in
|
2012-07-24 01:40:50 -07:00
|
|
|
match desc with
|
2013-01-07 06:13:01 -08:00
|
|
|
| Pexp_ident x -> ident ~loc (map_loc sub x)
|
2012-09-18 08:55:30 -07:00
|
|
|
| Pexp_constant x -> constant ~loc x
|
2012-07-24 01:40:50 -07:00
|
|
|
| Pexp_let (r, pel, e) -> let_ ~loc r (List.map (map_tuple (sub # pat) (sub # expr)) pel) (sub # expr e)
|
2012-09-18 08:55:30 -07:00
|
|
|
| Pexp_function (lab, def, pel) -> function_ ~loc lab (map_opt (sub # expr) def) (List.map (map_tuple (sub # pat) (sub # expr)) pel)
|
|
|
|
| Pexp_apply (e, l) -> apply ~loc (sub # expr e) (List.map (map_snd (sub # expr)) l)
|
2012-07-24 01:40:50 -07:00
|
|
|
| Pexp_match (e, l) -> match_ ~loc (sub # expr e) (List.map (map_tuple (sub # pat) (sub # expr)) l)
|
|
|
|
| Pexp_try (e, l) -> try_ ~loc (sub # expr e) (List.map (map_tuple (sub # pat) (sub # expr)) l)
|
|
|
|
| Pexp_tuple el -> tuple ~loc (List.map (sub # expr) el)
|
2013-01-07 06:13:01 -08:00
|
|
|
| Pexp_construct (lid, arg, b) -> construct ~loc (map_loc sub lid) (map_opt (sub # expr) arg) b
|
2012-07-24 01:40:50 -07:00
|
|
|
| Pexp_variant (lab, eo) -> variant ~loc lab (map_opt (sub # expr) eo)
|
2013-01-07 06:13:01 -08:00
|
|
|
| Pexp_record (l, eo) -> record ~loc (List.map (map_tuple (map_loc sub) (sub # expr)) l) (map_opt (sub # expr) eo)
|
|
|
|
| Pexp_field (e, lid) -> field ~loc (sub # expr e) (map_loc sub lid)
|
|
|
|
| Pexp_setfield (e1, lid, e2) -> setfield ~loc (sub # expr e1) (map_loc sub lid) (sub # expr e2)
|
2012-07-24 01:40:50 -07:00
|
|
|
| Pexp_array el -> array ~loc (List.map (sub # expr) el)
|
|
|
|
| Pexp_ifthenelse (e1, e2, e3) -> ifthenelse ~loc (sub # expr e1) (sub # expr e2) (map_opt (sub # expr) e3)
|
|
|
|
| Pexp_sequence (e1, e2) -> sequence ~loc (sub # expr e1) (sub # expr e2)
|
|
|
|
| Pexp_while (e1, e2) -> while_ ~loc (sub # expr e1) (sub # expr e2)
|
2013-01-07 06:13:01 -08:00
|
|
|
| Pexp_for (id, e1, e2, d, e3) -> for_ ~loc (map_loc sub id) (sub # expr e1) (sub # expr e2) d (sub # expr e3)
|
2012-07-24 01:40:50 -07:00
|
|
|
| Pexp_constraint (e, t1, t2) -> constraint_ ~loc (sub # expr e) (map_opt (sub # typ) t1) (map_opt (sub # typ) t2)
|
|
|
|
| Pexp_when (e1, e2) -> when_ ~loc (sub # expr e1) (sub # expr e2)
|
|
|
|
| Pexp_send (e, s) -> send ~loc (sub # expr e) s
|
2013-01-07 06:13:01 -08:00
|
|
|
| Pexp_new lid -> new_ ~loc (map_loc sub lid)
|
|
|
|
| Pexp_setinstvar (s, e) -> setinstvar ~loc (map_loc sub s) (sub # expr e)
|
|
|
|
| Pexp_override sel -> override ~loc (List.map (map_tuple (map_loc sub) (sub # expr)) sel)
|
|
|
|
| Pexp_letmodule (s, me, e) -> letmodule ~loc (map_loc sub s, sub # module_expr me, sub # expr e)
|
2012-07-24 01:40:50 -07:00
|
|
|
| Pexp_assert e -> assert_ ~loc (sub # expr e)
|
|
|
|
| Pexp_assertfalse -> assertfalse ~loc ()
|
|
|
|
| Pexp_lazy e -> lazy_ ~loc (sub # expr e)
|
|
|
|
| Pexp_poly (e, t) -> poly ~loc (sub # expr e) (map_opt (sub # typ) t)
|
|
|
|
| Pexp_object cls -> object_ ~loc (sub # class_structure cls)
|
|
|
|
| Pexp_newtype (s, e) -> newtype ~loc s (sub # expr e)
|
|
|
|
| Pexp_pack me -> pack ~loc (sub # module_expr me)
|
2013-01-07 06:13:01 -08:00
|
|
|
| Pexp_open (lid, e) -> open_ ~loc (map_loc sub lid) (sub # expr e)
|
2013-03-01 04:44:04 -08:00
|
|
|
| Pexp_attribute (body, x) -> attribute ~loc (sub # expr body) (sub # attribute x)
|
|
|
|
| Pexp_extension x -> extension ~loc (sub # extension x)
|
2012-06-29 02:36:32 -07:00
|
|
|
end
|
|
|
|
|
2012-07-24 05:48:39 -07:00
|
|
|
module P = struct
|
|
|
|
(* Patterns *)
|
|
|
|
|
|
|
|
let mk ?(loc = Location.none) x = {ppat_desc = x; ppat_loc = loc}
|
|
|
|
let any ?loc () = mk ?loc Ppat_any
|
|
|
|
let var ?loc a = mk ?loc (Ppat_var a)
|
|
|
|
let alias ?loc a b = mk ?loc (Ppat_alias (a, b))
|
|
|
|
let constant ?loc a = mk ?loc (Ppat_constant a)
|
|
|
|
let tuple ?loc a = mk ?loc (Ppat_tuple a)
|
2012-09-18 08:55:30 -07:00
|
|
|
let construct ?loc a b c = mk ?loc (Ppat_construct (a, b, c))
|
2012-07-24 05:48:39 -07:00
|
|
|
let variant ?loc a b = mk ?loc (Ppat_variant (a, b))
|
|
|
|
let record ?loc a b = mk ?loc (Ppat_record (a, b))
|
|
|
|
let array ?loc a = mk ?loc (Ppat_array a)
|
|
|
|
let or_ ?loc a b = mk ?loc (Ppat_or (a, b))
|
|
|
|
let constraint_ ?loc a b = mk ?loc (Ppat_constraint (a, b))
|
|
|
|
let type_ ?loc a = mk ?loc (Ppat_type a)
|
|
|
|
let lazy_ ?loc a = mk ?loc (Ppat_lazy a)
|
|
|
|
let unpack ?loc a = mk ?loc (Ppat_unpack a)
|
2013-03-04 05:52:23 -08:00
|
|
|
let attribute ?loc a b = mk ?loc (Ppat_attribute (a, b))
|
|
|
|
let extension ?loc a = mk ?loc (Ppat_extension a)
|
2012-07-24 01:40:50 -07:00
|
|
|
|
2012-09-18 08:55:30 -07:00
|
|
|
let map sub {ppat_desc = desc; ppat_loc = loc} =
|
2013-01-07 06:13:01 -08:00
|
|
|
let loc = sub # location loc in
|
2012-07-24 01:40:50 -07:00
|
|
|
match desc with
|
2012-09-18 08:55:30 -07:00
|
|
|
| Ppat_any -> any ~loc ()
|
2013-01-07 06:13:01 -08:00
|
|
|
| Ppat_var s -> var ~loc (map_loc sub s)
|
|
|
|
| Ppat_alias (p, s) -> alias ~loc (sub # pat p) (map_loc sub s)
|
2012-09-18 08:55:30 -07:00
|
|
|
| Ppat_constant c -> constant ~loc c
|
|
|
|
| Ppat_tuple pl -> tuple ~loc (List.map (sub # pat) pl)
|
2013-01-07 06:13:01 -08:00
|
|
|
| Ppat_construct (l, p, b) -> construct ~loc (map_loc sub l) (map_opt (sub # pat) p) b
|
2012-09-18 08:55:30 -07:00
|
|
|
| Ppat_variant (l, p) -> variant ~loc l (map_opt (sub # pat) p)
|
|
|
|
| Ppat_record (lpl, cf) ->
|
2013-01-07 06:13:01 -08:00
|
|
|
record ~loc (List.map (map_tuple (map_loc sub) (sub # pat)) lpl) cf
|
2012-09-18 08:55:30 -07:00
|
|
|
| Ppat_array pl -> array ~loc (List.map (sub # pat) pl)
|
|
|
|
| Ppat_or (p1, p2) -> or_ ~loc (sub # pat p1) (sub # pat p2)
|
|
|
|
| Ppat_constraint (p, t) -> constraint_ ~loc (sub # pat p) (sub # typ t)
|
2013-01-07 06:13:01 -08:00
|
|
|
| Ppat_type s -> type_ ~loc (map_loc sub s)
|
2012-09-18 08:55:30 -07:00
|
|
|
| Ppat_lazy p -> lazy_ ~loc (sub # pat p)
|
2013-01-07 06:13:01 -08:00
|
|
|
| Ppat_unpack s -> unpack ~loc (map_loc sub s)
|
2013-03-04 05:52:23 -08:00
|
|
|
| Ppat_attribute (body, x) -> attribute ~loc (sub # pat body) (sub # attribute x)
|
|
|
|
| Ppat_extension x -> extension ~loc (sub # extension x)
|
2012-06-29 02:36:32 -07:00
|
|
|
end
|
|
|
|
|
2012-09-18 08:55:30 -07:00
|
|
|
module CE = struct
|
|
|
|
(* Value expressions for the class language *)
|
2012-06-29 02:36:32 -07:00
|
|
|
|
2012-09-18 08:55:30 -07:00
|
|
|
let mk ?(loc = Location.none) x = {pcl_loc = loc; pcl_desc = x}
|
2012-07-24 01:40:50 -07:00
|
|
|
|
2012-09-18 08:55:30 -07:00
|
|
|
let constr ?loc a b = mk ?loc (Pcl_constr (a, b))
|
|
|
|
let structure ?loc a = mk ?loc (Pcl_structure a)
|
|
|
|
let fun_ ?loc a b c d = mk ?loc (Pcl_fun (a, b, c, d))
|
|
|
|
let apply ?loc a b = mk ?loc (Pcl_apply (a, b))
|
|
|
|
let let_ ?loc a b c = mk ?loc (Pcl_let (a, b, c))
|
|
|
|
let constraint_ ?loc a b = mk ?loc (Pcl_constraint (a, b))
|
|
|
|
|
|
|
|
let map sub {pcl_loc = loc; pcl_desc = desc} =
|
2013-01-07 06:13:01 -08:00
|
|
|
let loc = sub # location loc in
|
2012-09-18 08:55:30 -07:00
|
|
|
match desc with
|
2013-01-07 06:13:01 -08:00
|
|
|
| Pcl_constr (lid, tys) -> constr ~loc (map_loc sub lid) (List.map (sub # typ) tys)
|
2012-09-18 08:55:30 -07:00
|
|
|
| Pcl_structure s ->
|
|
|
|
structure ~loc (sub # class_structure s)
|
|
|
|
| Pcl_fun (lab, e, p, ce) ->
|
|
|
|
fun_ ~loc lab
|
|
|
|
(map_opt (sub # expr) e)
|
|
|
|
(sub # pat p)
|
|
|
|
(sub # class_expr ce)
|
|
|
|
| Pcl_apply (ce, l) ->
|
|
|
|
apply ~loc (sub # class_expr ce) (List.map (map_snd (sub # expr)) l)
|
|
|
|
| Pcl_let (r, pel, ce) ->
|
|
|
|
let_ ~loc r
|
|
|
|
(List.map (map_tuple (sub # pat) (sub # expr)) pel)
|
|
|
|
(sub # class_expr ce)
|
|
|
|
| Pcl_constraint (ce, ct) ->
|
|
|
|
constraint_ ~loc (sub # class_expr ce) (sub # class_type ct)
|
|
|
|
|
|
|
|
|
|
|
|
let mk_field ?(loc = Location.none) x = {pcf_desc = x; pcf_loc = loc}
|
|
|
|
|
|
|
|
let inher ?loc a b c = mk_field ?loc (Pcf_inher (a, b, c))
|
|
|
|
let valvirt ?loc a b c = mk_field ?loc (Pcf_valvirt (a, b, c))
|
|
|
|
let val_ ?loc a b c d = mk_field ?loc (Pcf_val (a, b, c, d))
|
|
|
|
let virt ?loc a b c = mk_field ?loc (Pcf_virt (a, b, c))
|
|
|
|
let meth ?loc a b c d = mk_field ?loc (Pcf_meth (a, b, c, d))
|
|
|
|
let constr ?loc a b = mk_field ?loc (Pcf_constr (a, b))
|
|
|
|
let init ?loc a = mk_field ?loc (Pcf_init a)
|
|
|
|
|
|
|
|
let map_field sub {pcf_desc = desc; pcf_loc = loc} =
|
2013-01-07 06:13:01 -08:00
|
|
|
let loc = sub # location loc in
|
2012-09-18 08:55:30 -07:00
|
|
|
match desc with
|
|
|
|
| Pcf_inher (o, ce, s) -> inher ~loc o (sub # class_expr ce) s
|
2013-01-07 06:13:01 -08:00
|
|
|
| Pcf_valvirt (s, m, t) -> valvirt ~loc (map_loc sub s) m (sub # typ t)
|
|
|
|
| Pcf_val (s, m, o, e) -> val_ ~loc (map_loc sub s) m o (sub # expr e)
|
|
|
|
| Pcf_virt (s, p, t) -> virt ~loc (map_loc sub s) p (sub # typ t)
|
|
|
|
| Pcf_meth (s, p, o, e) -> meth ~loc (map_loc sub s) p o (sub # expr e)
|
2012-09-18 08:55:30 -07:00
|
|
|
| Pcf_constr (t1, t2) -> constr ~loc (sub # typ t1) (sub # typ t2)
|
|
|
|
| Pcf_init e -> init ~loc (sub # expr e)
|
|
|
|
|
|
|
|
let map_structure sub {pcstr_pat; pcstr_fields} =
|
|
|
|
{
|
|
|
|
pcstr_pat = sub # pat pcstr_pat;
|
|
|
|
pcstr_fields = List.map (sub # class_field) pcstr_fields;
|
|
|
|
}
|
2013-01-07 06:13:01 -08:00
|
|
|
|
2013-03-05 04:06:38 -08:00
|
|
|
let class_infos sub f {pci_virt; pci_params = (pl, ploc); pci_name; pci_expr; pci_variance; pci_loc; pci_attributes} =
|
2013-01-07 06:13:01 -08:00
|
|
|
{
|
|
|
|
pci_virt;
|
|
|
|
pci_params = List.map (map_loc sub) pl, sub # location ploc;
|
|
|
|
pci_name = map_loc sub pci_name;
|
|
|
|
pci_expr = f pci_expr;
|
|
|
|
pci_variance;
|
|
|
|
pci_loc = sub # location pci_loc;
|
2013-03-05 04:06:38 -08:00
|
|
|
pci_attributes = map_attributes sub pci_attributes;
|
2013-01-07 06:13:01 -08:00
|
|
|
}
|
2012-09-18 08:55:30 -07:00
|
|
|
end
|
2012-07-24 01:40:50 -07:00
|
|
|
|
2012-06-29 02:36:32 -07:00
|
|
|
(* Now, a generic AST mapper class, to be extended to cover all kinds
|
|
|
|
and cases of the OCaml grammar. The default behavior of the mapper
|
|
|
|
is the identity. *)
|
|
|
|
|
2013-01-16 08:10:29 -08:00
|
|
|
class mapper =
|
2012-06-29 02:36:32 -07:00
|
|
|
object(this)
|
2012-07-24 01:40:50 -07:00
|
|
|
method implementation (input_name : string) ast = (input_name, this # structure ast)
|
|
|
|
method interface (input_name: string) ast = (input_name, this # signature ast)
|
2013-03-01 04:44:04 -08:00
|
|
|
method structure l = List.map (this # structure_item) l
|
|
|
|
method structure_item si = M.map_structure_item this si
|
2012-07-24 01:40:50 -07:00
|
|
|
method module_expr = M.map this
|
|
|
|
|
2013-03-01 04:44:04 -08:00
|
|
|
method signature l = List.map (this # signature_item) l
|
|
|
|
method signature_item si = MT.map_signature_item this si
|
2012-09-18 08:55:30 -07:00
|
|
|
method module_type = MT.map this
|
|
|
|
method with_constraint c = MT.map_with_constraint this c
|
|
|
|
|
2013-01-07 06:13:01 -08:00
|
|
|
method class_declaration = CE.class_infos this (this # class_expr)
|
2012-09-18 08:55:30 -07:00
|
|
|
method class_expr = CE.map this
|
|
|
|
method class_field = CE.map_field this
|
|
|
|
method class_structure = CE.map_structure this
|
|
|
|
|
|
|
|
method class_type = CT.map this
|
|
|
|
method class_type_field = CT.map_field this
|
|
|
|
method class_signature = CT.map_signature this
|
|
|
|
|
2013-01-07 06:13:01 -08:00
|
|
|
method class_type_declaration = CE.class_infos this (this # class_type)
|
|
|
|
method class_description = CE.class_infos this (this # class_type)
|
2012-09-18 08:55:30 -07:00
|
|
|
|
|
|
|
method type_declaration = T.map_type_declaration this
|
|
|
|
method type_kind = T.map_type_kind this
|
2012-07-24 01:40:50 -07:00
|
|
|
method typ = T.map this
|
|
|
|
|
2013-03-06 04:00:18 -08:00
|
|
|
method value_description {pval_name; pval_type; pval_prim; pval_loc; pval_attributes} =
|
2013-01-07 06:13:01 -08:00
|
|
|
{
|
2013-03-06 04:00:18 -08:00
|
|
|
pval_name = map_loc this pval_name;
|
2013-01-07 06:13:01 -08:00
|
|
|
pval_type = this # typ pval_type;
|
|
|
|
pval_prim;
|
|
|
|
pval_loc = this # location pval_loc;
|
2013-03-04 08:03:20 -08:00
|
|
|
pval_attributes = map_attributes this pval_attributes;
|
2013-01-07 06:13:01 -08:00
|
|
|
}
|
2012-09-18 08:55:30 -07:00
|
|
|
method pat = P.map this
|
2012-07-24 01:40:50 -07:00
|
|
|
method expr = E.map this
|
2012-09-18 08:55:30 -07:00
|
|
|
|
2013-03-04 08:20:49 -08:00
|
|
|
method exception_declaration ped =
|
|
|
|
{
|
2013-03-05 04:37:17 -08:00
|
|
|
ped_name = map_loc this ped.ped_name;
|
2013-03-04 08:20:49 -08:00
|
|
|
ped_args = List.map (this # typ) ped.ped_args;
|
|
|
|
ped_attributes = map_attributes this ped.ped_attributes;
|
|
|
|
}
|
2013-03-04 09:39:07 -08:00
|
|
|
method module_declaration pmd =
|
|
|
|
{
|
2013-03-05 04:37:17 -08:00
|
|
|
pmd_name = map_loc this pmd.pmd_name;
|
|
|
|
pmd_type = this # module_type pmd.pmd_type;
|
2013-03-04 09:39:07 -08:00
|
|
|
pmd_attributes = map_attributes this pmd.pmd_attributes;
|
|
|
|
}
|
2013-03-06 04:14:02 -08:00
|
|
|
method module_type_declaration {pmtd_name; pmtd_type; pmtd_attributes} =
|
|
|
|
{
|
|
|
|
pmtd_name = map_loc this pmtd_name;
|
|
|
|
pmtd_type = map_opt (this # module_type) pmtd_type;
|
|
|
|
pmtd_attributes = map_attributes this pmtd_attributes;
|
|
|
|
}
|
2013-03-05 08:50:05 -08:00
|
|
|
method module_binding x =
|
|
|
|
{
|
|
|
|
pmb_name = map_loc this x.pmb_name;
|
|
|
|
pmb_expr = this # module_expr x.pmb_expr;
|
|
|
|
pmb_attributes = map_attributes this x.pmb_attributes;
|
|
|
|
}
|
2013-03-06 02:49:44 -08:00
|
|
|
method module_type_binding x =
|
|
|
|
{
|
|
|
|
pmtb_name = map_loc this x.pmtb_name;
|
|
|
|
pmtb_type = this # module_type x.pmtb_type;
|
|
|
|
pmtb_attributes = map_attributes this x.pmtb_attributes;
|
|
|
|
}
|
2013-01-07 06:13:01 -08:00
|
|
|
|
2013-03-06 05:51:18 -08:00
|
|
|
method constructor_declaration {pcd_name; pcd_args; pcd_res; pcd_loc; pcd_attributes} =
|
|
|
|
T.constructor_decl
|
|
|
|
(map_loc this pcd_name)
|
|
|
|
(List.map (this # typ) pcd_args)
|
|
|
|
?res:(map_opt (this # typ) pcd_res)
|
|
|
|
~loc:(this # location pcd_loc)
|
|
|
|
~attributes:(map_attributes this pcd_attributes)
|
|
|
|
|
|
|
|
method label_declaration {pld_name; pld_type; pld_loc; pld_mutable; pld_attributes} =
|
|
|
|
T.label_decl
|
|
|
|
(map_loc this pld_name)
|
|
|
|
(this # typ pld_type)
|
|
|
|
~mut:pld_mutable
|
|
|
|
~loc:(this # location pld_loc)
|
|
|
|
~attributes:(map_attributes this pld_attributes)
|
|
|
|
|
|
|
|
|
2013-01-07 06:13:01 -08:00
|
|
|
method location l = l
|
2013-03-01 04:44:04 -08:00
|
|
|
|
|
|
|
method extension (s, e) = (s, this # expr e)
|
|
|
|
method attribute (s, e) = (s, this # expr e)
|
2012-06-29 02:36:32 -07:00
|
|
|
end
|
2012-07-24 05:48:39 -07:00
|
|
|
|
2013-01-23 01:15:41 -08:00
|
|
|
class type main_entry_points =
|
|
|
|
object
|
|
|
|
method implementation: string -> structure -> string * structure
|
|
|
|
method interface: string -> signature -> string * signature
|
|
|
|
end
|
2012-07-24 05:48:39 -07:00
|
|
|
|
2013-01-16 08:10:29 -08:00
|
|
|
let apply ~source ~target mapper =
|
|
|
|
let ic = open_in_bin source in
|
|
|
|
let magic = String.create (String.length ast_impl_magic_number) in
|
|
|
|
really_input ic magic 0 (String.length magic);
|
|
|
|
if magic <> ast_impl_magic_number && magic <> ast_intf_magic_number then
|
|
|
|
failwith "Bad magic";
|
|
|
|
let input_name = input_value ic in
|
|
|
|
let ast = input_value ic in
|
|
|
|
close_in ic;
|
|
|
|
|
|
|
|
let (input_name, ast) =
|
|
|
|
if magic = ast_impl_magic_number
|
|
|
|
then Obj.magic (mapper # implementation input_name (Obj.magic ast))
|
|
|
|
else Obj.magic (mapper # interface input_name (Obj.magic ast))
|
|
|
|
in
|
|
|
|
let oc = open_out_bin target in
|
|
|
|
output_string oc magic;
|
|
|
|
output_value oc input_name;
|
|
|
|
output_value oc ast;
|
|
|
|
close_out oc
|
|
|
|
|
2013-01-23 01:15:41 -08:00
|
|
|
let run_main mapper =
|
2013-01-16 08:10:29 -08:00
|
|
|
try
|
2013-01-23 01:15:41 -08:00
|
|
|
let a = Sys.argv in
|
|
|
|
let n = Array.length a in
|
2013-01-23 00:37:01 -08:00
|
|
|
if n > 2 then
|
2013-01-23 01:15:41 -08:00
|
|
|
apply ~source:a.(n - 2) ~target:a.(n - 1) (mapper (Array.to_list (Array.sub a 1 (n - 3))))
|
2013-01-16 08:10:29 -08:00
|
|
|
else begin
|
2013-01-23 02:15:45 -08:00
|
|
|
Printf.eprintf "Usage: %s [extra_args] <infile> <outfile>\n%!" Sys.executable_name;
|
2013-01-16 08:10:29 -08:00
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
with exn ->
|
|
|
|
prerr_endline (Printexc.to_string exn);
|
|
|
|
exit 2
|
2012-07-24 05:48:39 -07:00
|
|
|
|
2013-01-23 01:15:41 -08:00
|
|
|
let main mapper = run_main (fun _ -> mapper)
|
|
|
|
|
2013-01-23 02:15:45 -08:00
|
|
|
let register_function = ref (fun _name f -> run_main f)
|
|
|
|
let register name f = !register_function name (f :> string list -> mapper)
|