2016-02-18 07:11:59 -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 GNU Lesser General Public License version 2.1, with the *)
|
|
|
|
(* special exception on linking described in the file LICENSE. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
2012-11-08 06:30:38 -08:00
|
|
|
|
|
|
|
(* A generic Parsetree mapping class *)
|
|
|
|
|
2014-04-14 07:59:57 -07:00
|
|
|
(*
|
2014-04-22 08:28:20 -07:00
|
|
|
[@@@ocaml.warning "+9"]
|
2014-03-24 03:04:24 -07:00
|
|
|
(* Ensure that record patterns don't miss any field. *)
|
2014-04-14 07:59:57 -07:00
|
|
|
*)
|
2014-03-24 03:04:24 -07:00
|
|
|
|
2012-06-29 02:36:32 -07:00
|
|
|
open Parsetree
|
2013-03-08 06:59:45 -08:00
|
|
|
open Ast_helper
|
2013-09-25 08:14:38 -07:00
|
|
|
open Location
|
|
|
|
|
2018-07-23 05:19:41 -07:00
|
|
|
module String = Misc.Stdlib.String
|
|
|
|
|
2013-09-25 08:14:38 -07:00
|
|
|
type mapper = {
|
|
|
|
attribute: mapper -> attribute -> attribute;
|
|
|
|
attributes: mapper -> attribute list -> attribute list;
|
2018-07-29 14:29:01 -07:00
|
|
|
binding_op: mapper -> binding_op -> binding_op;
|
2013-09-25 08:14:38 -07:00
|
|
|
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;
|
2014-04-12 03:17:02 -07:00
|
|
|
class_type_declaration: mapper -> class_type_declaration
|
|
|
|
-> class_type_declaration;
|
2013-09-25 08:14:38 -07:00
|
|
|
class_type_field: mapper -> class_type_field -> class_type_field;
|
2019-07-22 09:38:28 -07:00
|
|
|
constant: mapper -> constant -> constant;
|
2014-04-12 03:17:02 -07:00
|
|
|
constructor_declaration: mapper -> constructor_declaration
|
|
|
|
-> constructor_declaration;
|
2013-09-25 08:14:38 -07:00
|
|
|
expr: mapper -> expression -> expression;
|
|
|
|
extension: mapper -> extension -> extension;
|
2014-08-07 02:44:18 -07:00
|
|
|
extension_constructor: mapper -> extension_constructor
|
|
|
|
-> extension_constructor;
|
2014-04-15 04:26:00 -07:00
|
|
|
include_declaration: mapper -> include_declaration -> include_declaration;
|
|
|
|
include_description: mapper -> include_description -> include_description;
|
2013-09-25 08:14:38 -07:00
|
|
|
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;
|
2018-10-26 07:47:15 -07:00
|
|
|
module_substitution: mapper -> module_substitution -> module_substitution;
|
2013-09-25 08:14:38 -07:00
|
|
|
module_expr: mapper -> module_expr -> module_expr;
|
|
|
|
module_type: mapper -> module_type -> module_type;
|
2014-04-12 03:17:02 -07:00
|
|
|
module_type_declaration: mapper -> module_type_declaration
|
|
|
|
-> module_type_declaration;
|
2018-04-08 01:51:15 -07:00
|
|
|
open_declaration: mapper -> open_declaration -> open_declaration;
|
2014-04-15 04:26:00 -07:00
|
|
|
open_description: mapper -> open_description -> open_description;
|
2013-09-25 08:14:38 -07:00
|
|
|
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;
|
|
|
|
typ: mapper -> core_type -> core_type;
|
|
|
|
type_declaration: mapper -> type_declaration -> type_declaration;
|
2014-05-05 06:04:01 -07:00
|
|
|
type_extension: mapper -> type_extension -> type_extension;
|
2018-04-06 10:02:46 -07:00
|
|
|
type_exception: mapper -> type_exception -> type_exception;
|
2013-09-25 08:14:38 -07:00
|
|
|
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;
|
|
|
|
}
|
2012-06-29 02:36:32 -07:00
|
|
|
|
2013-04-02 08:33:35 -07:00
|
|
|
let map_fst f (x, y) = (f x, y)
|
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-09-25 08:14:38 -07:00
|
|
|
let map_loc sub {loc; txt} = {loc = sub.location sub loc; txt}
|
2013-01-07 06:13:01 -08:00
|
|
|
|
2019-07-22 09:38:28 -07:00
|
|
|
module C = struct
|
|
|
|
(* Constants *)
|
|
|
|
|
|
|
|
let map sub c = match c with
|
|
|
|
| Pconst_integer _
|
|
|
|
| Pconst_char _
|
|
|
|
| Pconst_float _
|
|
|
|
-> c
|
|
|
|
| Pconst_string (s, loc, quotation_delimiter) ->
|
|
|
|
let loc = sub.location sub loc in
|
|
|
|
Const.string ~loc ?quotation_delimiter s
|
|
|
|
end
|
|
|
|
|
2012-09-18 08:55:30 -07:00
|
|
|
module T = struct
|
|
|
|
(* Type expressions for the core language *)
|
|
|
|
|
2018-07-14 14:10:39 -07:00
|
|
|
let row_field sub {
|
|
|
|
prf_desc;
|
|
|
|
prf_loc;
|
2018-08-16 23:36:36 -07:00
|
|
|
prf_attributes;
|
2018-07-14 14:10:39 -07:00
|
|
|
} =
|
|
|
|
let loc = sub.location sub prf_loc in
|
2018-08-16 23:36:36 -07:00
|
|
|
let attrs = sub.attributes sub prf_attributes in
|
2018-07-14 14:10:39 -07:00
|
|
|
let desc = match prf_desc with
|
2018-08-16 23:36:36 -07:00
|
|
|
| Rtag (l, b, tl) -> Rtag (map_loc sub l, b, List.map (sub.typ sub) tl)
|
2018-07-14 14:10:39 -07:00
|
|
|
| Rinherit t -> Rinherit (sub.typ sub t)
|
|
|
|
in
|
2018-08-16 23:36:36 -07:00
|
|
|
Rf.mk ~loc ~attrs desc
|
2012-09-18 08:55:30 -07:00
|
|
|
|
2018-07-14 14:10:39 -07:00
|
|
|
let object_field sub {
|
|
|
|
pof_desc;
|
|
|
|
pof_loc;
|
2018-08-16 23:36:36 -07:00
|
|
|
pof_attributes;
|
2018-07-14 14:10:39 -07:00
|
|
|
} =
|
|
|
|
let loc = sub.location sub pof_loc in
|
2018-08-16 23:36:36 -07:00
|
|
|
let attrs = sub.attributes sub pof_attributes in
|
2018-07-14 14:10:39 -07:00
|
|
|
let desc = match pof_desc with
|
2018-08-16 23:36:36 -07:00
|
|
|
| Otag (l, t) -> Otag (map_loc sub l, sub.typ sub t)
|
2018-07-14 14:10:39 -07:00
|
|
|
| Oinherit t -> Oinherit (sub.typ sub t)
|
|
|
|
in
|
2018-08-16 23:36:36 -07:00
|
|
|
Of.mk ~loc ~attrs desc
|
2017-03-09 20:46:48 -08:00
|
|
|
|
2013-03-08 06:59:45 -08:00
|
|
|
let map sub {ptyp_desc = desc; ptyp_loc = loc; ptyp_attributes = attrs} =
|
|
|
|
let open Typ in
|
2013-09-26 01:49:46 -07:00
|
|
|
let loc = sub.location sub loc in
|
|
|
|
let attrs = sub.attributes sub attrs in
|
2012-09-18 08:55:30 -07:00
|
|
|
match desc with
|
2013-03-08 06:59:45 -08:00
|
|
|
| Ptyp_any -> any ~loc ~attrs ()
|
|
|
|
| Ptyp_var s -> var ~loc ~attrs s
|
2013-09-04 08:12:37 -07:00
|
|
|
| Ptyp_arrow (lab, t1, t2) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
arrow ~loc ~attrs lab (sub.typ sub t1) (sub.typ sub t2)
|
|
|
|
| Ptyp_tuple tyl -> tuple ~loc ~attrs (List.map (sub.typ sub) tyl)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Ptyp_constr (lid, tl) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
constr ~loc ~attrs (map_loc sub lid) (List.map (sub.typ sub) tl)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Ptyp_object (l, o) ->
|
2017-03-09 20:46:48 -08:00
|
|
|
object_ ~loc ~attrs (List.map (object_field sub) l) o
|
2013-09-04 08:12:37 -07:00
|
|
|
| Ptyp_class (lid, tl) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
class_ ~loc ~attrs (map_loc sub lid) (List.map (sub.typ sub) tl)
|
|
|
|
| Ptyp_alias (t, s) -> alias ~loc ~attrs (sub.typ sub t) s
|
2013-09-04 08:12:37 -07:00
|
|
|
| Ptyp_variant (rl, b, ll) ->
|
|
|
|
variant ~loc ~attrs (List.map (row_field sub) rl) b ll
|
2016-08-29 07:21:38 -07:00
|
|
|
| Ptyp_poly (sl, t) -> poly ~loc ~attrs
|
|
|
|
(List.map (map_loc sub) sl) (sub.typ sub t)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Ptyp_package (lid, l) ->
|
|
|
|
package ~loc ~attrs (map_loc sub lid)
|
2013-09-26 01:49:46 -07:00
|
|
|
(List.map (map_tuple (map_loc sub) (sub.typ sub)) l)
|
2013-09-25 08:14:38 -07:00
|
|
|
| Ptyp_extension x -> extension ~loc ~attrs (sub.extension sub 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_attributes;
|
|
|
|
ptype_loc} =
|
2018-08-16 06:40:53 -07:00
|
|
|
let loc = sub.location sub ptype_loc in
|
|
|
|
let attrs = sub.attributes sub ptype_attributes in
|
|
|
|
Type.mk ~loc ~attrs (map_loc sub ptype_name)
|
2014-05-04 16:08:45 -07:00
|
|
|
~params:(List.map (map_fst (sub.typ sub)) ptype_params)
|
2013-03-08 06:59:45 -08:00
|
|
|
~priv:ptype_private
|
2014-04-12 03:17:02 -07:00
|
|
|
~cstrs:(List.map
|
|
|
|
(map_tuple3 (sub.typ sub) (sub.typ sub) (sub.location sub))
|
2013-09-25 08:14:38 -07:00
|
|
|
ptype_cstrs)
|
2013-09-26 01:49:46 -07:00
|
|
|
~kind:(sub.type_kind sub ptype_kind)
|
|
|
|
?manifest:(map_opt (sub.typ sub) ptype_manifest)
|
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-09-04 08:12:37 -07:00
|
|
|
| Ptype_variant l ->
|
2013-09-26 01:49:46 -07:00
|
|
|
Ptype_variant (List.map (sub.constructor_declaration sub) l)
|
|
|
|
| Ptype_record l -> Ptype_record (List.map (sub.label_declaration sub) l)
|
2014-05-04 16:08:45 -07:00
|
|
|
| Ptype_open -> Ptype_open
|
|
|
|
|
2014-10-14 08:51:30 -07:00
|
|
|
let map_constructor_arguments sub = function
|
|
|
|
| Pcstr_tuple l -> Pcstr_tuple (List.map (sub.typ sub) l)
|
|
|
|
| Pcstr_record l ->
|
|
|
|
Pcstr_record (List.map (sub.label_declaration sub) l)
|
|
|
|
|
2014-05-04 16:08:45 -07:00
|
|
|
let map_type_extension sub
|
|
|
|
{ptyext_path; ptyext_params;
|
|
|
|
ptyext_constructors;
|
|
|
|
ptyext_private;
|
2018-08-16 06:40:53 -07:00
|
|
|
ptyext_loc;
|
2014-05-04 16:08:45 -07:00
|
|
|
ptyext_attributes} =
|
2018-08-16 06:40:53 -07:00
|
|
|
let loc = sub.location sub ptyext_loc in
|
|
|
|
let attrs = sub.attributes sub ptyext_attributes in
|
|
|
|
Te.mk ~loc ~attrs
|
2014-05-04 16:08:45 -07:00
|
|
|
(map_loc sub ptyext_path)
|
|
|
|
(List.map (sub.extension_constructor sub) ptyext_constructors)
|
|
|
|
~params:(List.map (map_fst (sub.typ sub)) ptyext_params)
|
|
|
|
~priv:ptyext_private
|
|
|
|
|
2018-08-16 06:40:53 -07:00
|
|
|
let map_type_exception sub
|
|
|
|
{ptyexn_constructor; ptyexn_loc; ptyexn_attributes} =
|
|
|
|
let loc = sub.location sub ptyexn_loc in
|
|
|
|
let attrs = sub.attributes sub ptyexn_attributes in
|
|
|
|
Te.mk_exception ~loc ~attrs
|
2018-04-06 10:02:46 -07:00
|
|
|
(sub.extension_constructor sub ptyexn_constructor)
|
|
|
|
|
2014-05-04 16:08:45 -07:00
|
|
|
let map_extension_constructor_kind sub = function
|
|
|
|
Pext_decl(ctl, cto) ->
|
2014-10-14 08:51:30 -07:00
|
|
|
Pext_decl(map_constructor_arguments sub ctl, map_opt (sub.typ sub) cto)
|
2014-05-04 16:08:45 -07:00
|
|
|
| Pext_rebind li ->
|
|
|
|
Pext_rebind (map_loc sub li)
|
|
|
|
|
|
|
|
let map_extension_constructor sub
|
|
|
|
{pext_name;
|
|
|
|
pext_kind;
|
|
|
|
pext_loc;
|
|
|
|
pext_attributes} =
|
2018-08-16 06:40:53 -07:00
|
|
|
let loc = sub.location sub pext_loc in
|
|
|
|
let attrs = sub.attributes sub pext_attributes in
|
|
|
|
Te.constructor ~loc ~attrs
|
2014-05-04 16:08:45 -07:00
|
|
|
(map_loc sub pext_name)
|
|
|
|
(map_extension_constructor_kind sub pext_kind)
|
|
|
|
|
2012-09-18 08:55:30 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
module CT = struct
|
|
|
|
(* Type expressions for the class language *)
|
|
|
|
|
2013-04-10 10:44:15 -07:00
|
|
|
let map sub {pcty_loc = loc; pcty_desc = desc; pcty_attributes = attrs} =
|
2013-03-08 06:59:45 -08:00
|
|
|
let open Cty in
|
2013-09-26 01:49:46 -07:00
|
|
|
let loc = sub.location sub loc in
|
2015-07-17 07:31:05 -07:00
|
|
|
let attrs = sub.attributes sub attrs in
|
2012-09-18 08:55:30 -07:00
|
|
|
match desc with
|
2013-09-04 08:12:37 -07:00
|
|
|
| Pcty_constr (lid, tys) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
constr ~loc ~attrs (map_loc sub lid) (List.map (sub.typ sub) tys)
|
|
|
|
| Pcty_signature x -> signature ~loc ~attrs (sub.class_signature sub x)
|
2013-04-16 01:59:09 -07:00
|
|
|
| Pcty_arrow (lab, t, ct) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
arrow ~loc ~attrs lab (sub.typ sub t) (sub.class_type sub ct)
|
2013-09-25 08:14:38 -07:00
|
|
|
| Pcty_extension x -> extension ~loc ~attrs (sub.extension sub x)
|
2018-04-08 01:51:15 -07:00
|
|
|
| Pcty_open (o, ct) ->
|
|
|
|
open_ ~loc ~attrs (sub.open_description sub o) (sub.class_type sub ct)
|
2012-09-18 08:55:30 -07:00
|
|
|
|
2013-09-04 08:12:37 -07:00
|
|
|
let map_field sub {pctf_desc = desc; pctf_loc = loc; pctf_attributes = attrs}
|
2013-09-25 08:14:38 -07:00
|
|
|
=
|
2013-03-08 06:59:45 -08:00
|
|
|
let open Ctf in
|
2013-09-26 01:49:46 -07:00
|
|
|
let loc = sub.location sub loc in
|
2015-07-17 07:31:05 -07:00
|
|
|
let attrs = sub.attributes sub attrs in
|
2012-09-18 08:55:30 -07:00
|
|
|
match desc with
|
2013-09-26 01:49:46 -07:00
|
|
|
| Pctf_inherit ct -> inherit_ ~loc ~attrs (sub.class_type sub ct)
|
2016-08-29 07:21:38 -07:00
|
|
|
| Pctf_val (s, m, v, t) ->
|
|
|
|
val_ ~loc ~attrs (map_loc sub s) m v (sub.typ sub t)
|
|
|
|
| Pctf_method (s, p, v, t) ->
|
|
|
|
method_ ~loc ~attrs (map_loc sub s) p v (sub.typ sub t)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Pctf_constraint (t1, t2) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
constraint_ ~loc ~attrs (sub.typ sub t1) (sub.typ sub t2)
|
2014-05-04 13:42:34 -07:00
|
|
|
| Pctf_attribute x -> attribute ~loc (sub.attribute sub x)
|
2013-09-25 08:14:38 -07:00
|
|
|
| Pctf_extension x -> extension ~loc ~attrs (sub.extension sub x)
|
2012-09-18 08:55:30 -07:00
|
|
|
|
2013-04-16 02:21:05 -07:00
|
|
|
let map_signature sub {pcsig_self; pcsig_fields} =
|
2013-03-08 06:59:45 -08:00
|
|
|
Csig.mk
|
2013-09-26 01:49:46 -07:00
|
|
|
(sub.typ sub pcsig_self)
|
|
|
|
(List.map (sub.class_type_field sub) pcsig_fields)
|
2012-09-18 08:55:30 -07:00
|
|
|
end
|
|
|
|
|
2019-10-09 06:15:37 -07:00
|
|
|
let map_functor_param sub = function
|
|
|
|
| Unit -> Unit
|
|
|
|
| Named (s, mt) -> Named (map_loc sub s, sub.module_type sub mt)
|
|
|
|
|
2012-09-18 08:55:30 -07:00
|
|
|
module MT = struct
|
|
|
|
(* Type expressions for the module language *)
|
|
|
|
|
2013-03-08 06:59:45 -08:00
|
|
|
let map sub {pmty_desc = desc; pmty_loc = loc; pmty_attributes = attrs} =
|
|
|
|
let open Mty in
|
2013-09-26 01:49:46 -07:00
|
|
|
let loc = sub.location sub loc in
|
|
|
|
let attrs = sub.attributes sub attrs in
|
2012-09-18 08:55:30 -07:00
|
|
|
match desc with
|
2013-03-08 06:59:45 -08:00
|
|
|
| Pmty_ident s -> ident ~loc ~attrs (map_loc sub s)
|
2013-09-29 00:22:34 -07:00
|
|
|
| Pmty_alias s -> alias ~loc ~attrs (map_loc sub s)
|
2013-09-25 08:14:38 -07:00
|
|
|
| Pmty_signature sg -> signature ~loc ~attrs (sub.signature sub sg)
|
2019-10-09 06:15:37 -07:00
|
|
|
| Pmty_functor (param, mt) ->
|
|
|
|
functor_ ~loc ~attrs
|
|
|
|
(map_functor_param sub param)
|
|
|
|
(sub.module_type sub mt)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Pmty_with (mt, l) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
with_ ~loc ~attrs (sub.module_type sub mt)
|
|
|
|
(List.map (sub.with_constraint sub) l)
|
|
|
|
| Pmty_typeof me -> typeof_ ~loc ~attrs (sub.module_expr sub me)
|
2013-09-25 08:14:38 -07:00
|
|
|
| Pmty_extension x -> extension ~loc ~attrs (sub.extension sub x)
|
2012-09-18 08:55:30 -07:00
|
|
|
|
|
|
|
let map_with_constraint sub = function
|
2013-04-16 03:47:45 -07:00
|
|
|
| Pwith_type (lid, d) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
Pwith_type (map_loc sub lid, sub.type_declaration sub d)
|
2013-04-16 03:47:45 -07:00
|
|
|
| Pwith_module (lid, lid2) ->
|
|
|
|
Pwith_module (map_loc sub lid, map_loc sub lid2)
|
2016-08-29 21:16:28 -07:00
|
|
|
| Pwith_typesubst (lid, d) ->
|
|
|
|
Pwith_typesubst (map_loc sub lid, sub.type_declaration sub d)
|
2013-04-16 03:47:45 -07:00
|
|
|
| Pwith_modsubst (s, lid) ->
|
|
|
|
Pwith_modsubst (map_loc sub s, map_loc sub lid)
|
2012-09-18 08:55:30 -07:00
|
|
|
|
|
|
|
let map_signature_item sub {psig_desc = desc; psig_loc = loc} =
|
2013-03-08 06:59:45 -08:00
|
|
|
let open Sig in
|
2013-09-26 01:49:46 -07:00
|
|
|
let loc = sub.location sub loc in
|
2012-09-18 08:55:30 -07:00
|
|
|
match desc with
|
2013-09-26 01:49:46 -07:00
|
|
|
| Psig_value vd -> value ~loc (sub.value_description sub vd)
|
2018-10-26 07:47:15 -07:00
|
|
|
| Psig_type (rf, l) ->
|
|
|
|
type_ ~loc rf (List.map (sub.type_declaration sub) l)
|
|
|
|
| Psig_typesubst l ->
|
|
|
|
type_subst ~loc (List.map (sub.type_declaration sub) l)
|
2014-05-04 16:08:45 -07:00
|
|
|
| Psig_typext te -> type_extension ~loc (sub.type_extension sub te)
|
2018-04-06 10:02:46 -07:00
|
|
|
| Psig_exception ed -> exception_ ~loc (sub.type_exception sub ed)
|
2013-09-26 01:49:46 -07:00
|
|
|
| Psig_module x -> module_ ~loc (sub.module_declaration sub x)
|
2018-10-26 07:47:15 -07:00
|
|
|
| Psig_modsubst x -> mod_subst ~loc (sub.module_substitution sub x)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Psig_recmodule l ->
|
2013-09-26 01:49:46 -07:00
|
|
|
rec_module ~loc (List.map (sub.module_declaration sub) l)
|
|
|
|
| Psig_modtype x -> modtype ~loc (sub.module_type_declaration sub x)
|
2014-04-15 04:26:00 -07:00
|
|
|
| Psig_open x -> open_ ~loc (sub.open_description sub x)
|
|
|
|
| Psig_include x -> include_ ~loc (sub.include_description sub x)
|
2013-09-26 01:49:46 -07:00
|
|
|
| Psig_class l -> class_ ~loc (List.map (sub.class_description sub) l)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Psig_class_type l ->
|
2013-09-26 01:49:46 -07:00
|
|
|
class_type ~loc (List.map (sub.class_type_declaration sub) l)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Psig_extension (x, attrs) ->
|
2018-08-16 06:40:53 -07:00
|
|
|
let attrs = sub.attributes sub attrs in
|
|
|
|
extension ~loc ~attrs (sub.extension sub x)
|
2013-09-25 08:14:38 -07:00
|
|
|
| Psig_attribute x -> attribute ~loc (sub.attribute sub x)
|
2012-09-18 08:55:30 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
module M = struct
|
|
|
|
(* Value expressions for the module language *)
|
|
|
|
|
2013-03-08 06:59:45 -08:00
|
|
|
let map sub {pmod_loc = loc; pmod_desc = desc; pmod_attributes = attrs} =
|
|
|
|
let open Mod in
|
2013-09-26 01:49:46 -07:00
|
|
|
let loc = sub.location sub loc in
|
|
|
|
let attrs = sub.attributes sub attrs in
|
2012-09-18 08:55:30 -07:00
|
|
|
match desc with
|
2013-03-08 06:59:45 -08:00
|
|
|
| Pmod_ident x -> ident ~loc ~attrs (map_loc sub x)
|
2013-09-25 08:14:38 -07:00
|
|
|
| Pmod_structure str -> structure ~loc ~attrs (sub.structure sub str)
|
2019-10-09 06:15:37 -07:00
|
|
|
| Pmod_functor (param, body) ->
|
|
|
|
functor_ ~loc ~attrs
|
|
|
|
(map_functor_param sub param)
|
2013-09-26 01:49:46 -07:00
|
|
|
(sub.module_expr sub body)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Pmod_apply (m1, m2) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
apply ~loc ~attrs (sub.module_expr sub m1) (sub.module_expr sub m2)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Pmod_constraint (m, mty) ->
|
2014-04-12 03:17:02 -07:00
|
|
|
constraint_ ~loc ~attrs (sub.module_expr sub m)
|
|
|
|
(sub.module_type sub mty)
|
2013-09-26 01:49:46 -07:00
|
|
|
| Pmod_unpack e -> unpack ~loc ~attrs (sub.expr sub e)
|
2013-09-25 08:14:38 -07:00
|
|
|
| Pmod_extension x -> extension ~loc ~attrs (sub.extension sub x)
|
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-03-08 06:59:45 -08:00
|
|
|
let open Str in
|
2013-09-26 01:49:46 -07:00
|
|
|
let loc = sub.location sub loc in
|
2012-07-24 01:40:50 -07:00
|
|
|
match desc with
|
2013-09-04 08:12:37 -07:00
|
|
|
| Pstr_eval (x, attrs) ->
|
2018-08-16 06:40:53 -07:00
|
|
|
let attrs = sub.attributes sub attrs in
|
|
|
|
eval ~loc ~attrs (sub.expr sub x)
|
2013-09-26 01:49:46 -07:00
|
|
|
| Pstr_value (r, vbs) -> value ~loc r (List.map (sub.value_binding sub) vbs)
|
|
|
|
| Pstr_primitive vd -> primitive ~loc (sub.value_description sub vd)
|
2015-03-13 04:07:57 -07:00
|
|
|
| Pstr_type (rf, l) -> type_ ~loc rf (List.map (sub.type_declaration sub) l)
|
2014-05-04 16:08:45 -07:00
|
|
|
| Pstr_typext te -> type_extension ~loc (sub.type_extension sub te)
|
2018-04-06 10:02:46 -07:00
|
|
|
| Pstr_exception ed -> exception_ ~loc (sub.type_exception sub ed)
|
2013-09-26 01:49:46 -07:00
|
|
|
| Pstr_module x -> module_ ~loc (sub.module_binding sub x)
|
|
|
|
| Pstr_recmodule l -> rec_module ~loc (List.map (sub.module_binding sub) l)
|
|
|
|
| Pstr_modtype x -> modtype ~loc (sub.module_type_declaration sub x)
|
2018-04-08 01:51:15 -07:00
|
|
|
| Pstr_open x -> open_ ~loc (sub.open_declaration sub x)
|
2013-09-26 01:49:46 -07:00
|
|
|
| Pstr_class l -> class_ ~loc (List.map (sub.class_declaration sub) l)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Pstr_class_type l ->
|
2013-09-26 01:49:46 -07:00
|
|
|
class_type ~loc (List.map (sub.class_type_declaration sub) l)
|
2014-04-15 04:26:00 -07:00
|
|
|
| Pstr_include x -> include_ ~loc (sub.include_declaration sub x)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Pstr_extension (x, attrs) ->
|
2018-08-16 06:40:53 -07:00
|
|
|
let attrs = sub.attributes sub attrs in
|
|
|
|
extension ~loc ~attrs (sub.extension sub x)
|
2013-09-25 08:14:38 -07:00
|
|
|
| Pstr_attribute x -> attribute ~loc (sub.attribute sub 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
|
|
|
|
2013-03-08 06:59:45 -08:00
|
|
|
let map sub {pexp_loc = loc; pexp_desc = desc; pexp_attributes = attrs} =
|
|
|
|
let open Exp in
|
2013-09-26 01:49:46 -07:00
|
|
|
let loc = sub.location sub loc in
|
|
|
|
let attrs = sub.attributes sub attrs in
|
2012-07-24 01:40:50 -07:00
|
|
|
match desc with
|
2013-03-08 06:59:45 -08:00
|
|
|
| Pexp_ident x -> ident ~loc ~attrs (map_loc sub x)
|
2019-07-22 09:38:28 -07:00
|
|
|
| Pexp_constant x -> constant ~loc ~attrs (sub.constant sub x)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Pexp_let (r, vbs, e) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
let_ ~loc ~attrs r (List.map (sub.value_binding sub) vbs)
|
|
|
|
(sub.expr sub e)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Pexp_fun (lab, def, p, e) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
fun_ ~loc ~attrs lab (map_opt (sub.expr sub) def) (sub.pat sub p)
|
|
|
|
(sub.expr sub e)
|
|
|
|
| Pexp_function pel -> function_ ~loc ~attrs (sub.cases sub pel)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Pexp_apply (e, l) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
apply ~loc ~attrs (sub.expr sub e) (List.map (map_snd (sub.expr sub)) l)
|
|
|
|
| Pexp_match (e, pel) ->
|
|
|
|
match_ ~loc ~attrs (sub.expr sub e) (sub.cases sub pel)
|
|
|
|
| Pexp_try (e, pel) -> try_ ~loc ~attrs (sub.expr sub e) (sub.cases sub pel)
|
|
|
|
| Pexp_tuple el -> tuple ~loc ~attrs (List.map (sub.expr sub) el)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Pexp_construct (lid, arg) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
construct ~loc ~attrs (map_loc sub lid) (map_opt (sub.expr sub) arg)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Pexp_variant (lab, eo) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
variant ~loc ~attrs lab (map_opt (sub.expr sub) eo)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Pexp_record (l, eo) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
record ~loc ~attrs (List.map (map_tuple (map_loc sub) (sub.expr sub)) l)
|
|
|
|
(map_opt (sub.expr sub) eo)
|
|
|
|
| Pexp_field (e, lid) ->
|
|
|
|
field ~loc ~attrs (sub.expr sub e) (map_loc sub lid)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Pexp_setfield (e1, lid, e2) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
setfield ~loc ~attrs (sub.expr sub e1) (map_loc sub lid)
|
|
|
|
(sub.expr sub e2)
|
|
|
|
| Pexp_array el -> array ~loc ~attrs (List.map (sub.expr sub) el)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Pexp_ifthenelse (e1, e2, e3) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
ifthenelse ~loc ~attrs (sub.expr sub e1) (sub.expr sub e2)
|
|
|
|
(map_opt (sub.expr sub) e3)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Pexp_sequence (e1, e2) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
sequence ~loc ~attrs (sub.expr sub e1) (sub.expr sub e2)
|
2014-04-12 03:17:02 -07:00
|
|
|
| Pexp_while (e1, e2) ->
|
|
|
|
while_ ~loc ~attrs (sub.expr sub e1) (sub.expr sub e2)
|
2013-12-02 10:00:18 -08:00
|
|
|
| Pexp_for (p, e1, e2, d, e3) ->
|
|
|
|
for_ ~loc ~attrs (sub.pat sub p) (sub.expr sub e1) (sub.expr sub e2) d
|
2013-09-26 01:49:46 -07:00
|
|
|
(sub.expr sub e3)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Pexp_coerce (e, t1, t2) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
coerce ~loc ~attrs (sub.expr sub e) (map_opt (sub.typ sub) t1)
|
|
|
|
(sub.typ sub t2)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Pexp_constraint (e, t) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
constraint_ ~loc ~attrs (sub.expr sub e) (sub.typ sub t)
|
2016-08-29 07:21:38 -07:00
|
|
|
| Pexp_send (e, s) ->
|
|
|
|
send ~loc ~attrs (sub.expr sub e) (map_loc sub s)
|
2013-03-08 06:59:45 -08:00
|
|
|
| Pexp_new lid -> new_ ~loc ~attrs (map_loc sub lid)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Pexp_setinstvar (s, e) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
setinstvar ~loc ~attrs (map_loc sub s) (sub.expr sub e)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Pexp_override sel ->
|
|
|
|
override ~loc ~attrs
|
2013-09-26 01:49:46 -07:00
|
|
|
(List.map (map_tuple (map_loc sub) (sub.expr sub)) sel)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Pexp_letmodule (s, me, e) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
letmodule ~loc ~attrs (map_loc sub s) (sub.module_expr sub me)
|
|
|
|
(sub.expr sub e)
|
2015-11-20 01:18:49 -08:00
|
|
|
| Pexp_letexception (cd, e) ->
|
|
|
|
letexception ~loc ~attrs
|
|
|
|
(sub.extension_constructor sub cd)
|
|
|
|
(sub.expr sub e)
|
2013-09-26 01:49:46 -07:00
|
|
|
| Pexp_assert e -> assert_ ~loc ~attrs (sub.expr sub e)
|
|
|
|
| Pexp_lazy e -> lazy_ ~loc ~attrs (sub.expr sub e)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Pexp_poly (e, t) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
poly ~loc ~attrs (sub.expr sub e) (map_opt (sub.typ sub) t)
|
|
|
|
| Pexp_object cls -> object_ ~loc ~attrs (sub.class_structure sub cls)
|
2016-08-29 07:21:38 -07:00
|
|
|
| Pexp_newtype (s, e) ->
|
|
|
|
newtype ~loc ~attrs (map_loc sub s) (sub.expr sub e)
|
2013-09-26 01:49:46 -07:00
|
|
|
| Pexp_pack me -> pack ~loc ~attrs (sub.module_expr sub me)
|
2018-04-08 01:51:15 -07:00
|
|
|
| Pexp_open (o, e) ->
|
|
|
|
open_ ~loc ~attrs (sub.open_declaration sub o) (sub.expr sub e)
|
2018-07-29 14:29:01 -07:00
|
|
|
| Pexp_letop {let_; ands; body} ->
|
|
|
|
letop ~loc ~attrs (sub.binding_op sub let_)
|
|
|
|
(List.map (sub.binding_op sub) ands) (sub.expr sub body)
|
2013-09-25 08:14:38 -07:00
|
|
|
| Pexp_extension x -> extension ~loc ~attrs (sub.extension sub x)
|
2015-10-15 17:13:40 -07:00
|
|
|
| Pexp_unreachable -> unreachable ~loc ~attrs ()
|
2018-07-29 14:29:01 -07:00
|
|
|
|
|
|
|
let map_binding_op sub {pbop_op; pbop_pat; pbop_exp; pbop_loc} =
|
|
|
|
let open Exp in
|
|
|
|
let op = map_loc sub pbop_op in
|
|
|
|
let pat = sub.pat sub pbop_pat in
|
|
|
|
let exp = sub.expr sub pbop_exp in
|
|
|
|
let loc = sub.location sub pbop_loc in
|
|
|
|
binding_op op pat exp loc
|
|
|
|
|
2012-06-29 02:36:32 -07:00
|
|
|
end
|
|
|
|
|
2012-07-24 05:48:39 -07:00
|
|
|
module P = struct
|
|
|
|
(* Patterns *)
|
|
|
|
|
2013-03-08 06:59:45 -08:00
|
|
|
let map sub {ppat_desc = desc; ppat_loc = loc; ppat_attributes = attrs} =
|
|
|
|
let open Pat in
|
2013-09-26 01:49:46 -07:00
|
|
|
let loc = sub.location sub loc in
|
|
|
|
let attrs = sub.attributes sub attrs in
|
2012-07-24 01:40:50 -07:00
|
|
|
match desc with
|
2013-03-08 06:59:45 -08:00
|
|
|
| Ppat_any -> any ~loc ~attrs ()
|
|
|
|
| Ppat_var s -> var ~loc ~attrs (map_loc sub s)
|
2013-09-26 01:49:46 -07:00
|
|
|
| Ppat_alias (p, s) -> alias ~loc ~attrs (sub.pat sub p) (map_loc sub s)
|
2019-07-22 09:38:28 -07:00
|
|
|
| Ppat_constant c -> constant ~loc ~attrs (sub.constant sub c)
|
2013-05-23 08:12:04 -07:00
|
|
|
| Ppat_interval (c1, c2) -> interval ~loc ~attrs c1 c2
|
2013-09-26 01:49:46 -07:00
|
|
|
| Ppat_tuple pl -> tuple ~loc ~attrs (List.map (sub.pat sub) pl)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Ppat_construct (l, p) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
construct ~loc ~attrs (map_loc sub l) (map_opt (sub.pat sub) p)
|
|
|
|
| Ppat_variant (l, p) -> variant ~loc ~attrs l (map_opt (sub.pat sub) p)
|
2012-09-18 08:55:30 -07:00
|
|
|
| Ppat_record (lpl, cf) ->
|
2014-04-12 03:17:02 -07:00
|
|
|
record ~loc ~attrs
|
|
|
|
(List.map (map_tuple (map_loc sub) (sub.pat sub)) lpl) cf
|
2013-09-26 01:49:46 -07:00
|
|
|
| Ppat_array pl -> array ~loc ~attrs (List.map (sub.pat sub) pl)
|
|
|
|
| Ppat_or (p1, p2) -> or_ ~loc ~attrs (sub.pat sub p1) (sub.pat sub p2)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Ppat_constraint (p, t) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
constraint_ ~loc ~attrs (sub.pat sub p) (sub.typ sub t)
|
2013-03-08 06:59:45 -08:00
|
|
|
| Ppat_type s -> type_ ~loc ~attrs (map_loc sub s)
|
2013-09-26 01:49:46 -07:00
|
|
|
| Ppat_lazy p -> lazy_ ~loc ~attrs (sub.pat sub p)
|
2013-03-08 06:59:45 -08:00
|
|
|
| Ppat_unpack s -> unpack ~loc ~attrs (map_loc sub s)
|
2016-05-05 10:50:56 -07:00
|
|
|
| Ppat_open (lid,p) -> open_ ~loc ~attrs (map_loc sub lid) (sub.pat sub p)
|
2014-05-05 04:49:37 -07:00
|
|
|
| Ppat_exception p -> exception_ ~loc ~attrs (sub.pat sub p)
|
2013-09-25 08:14:38 -07:00
|
|
|
| Ppat_extension x -> extension ~loc ~attrs (sub.extension sub 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
|
|
|
|
2013-04-10 10:26:55 -07:00
|
|
|
let map sub {pcl_loc = loc; pcl_desc = desc; pcl_attributes = attrs} =
|
2013-03-08 06:59:45 -08:00
|
|
|
let open Cl in
|
2013-09-26 01:49:46 -07:00
|
|
|
let loc = sub.location sub loc in
|
2015-07-17 07:31:05 -07:00
|
|
|
let attrs = sub.attributes sub attrs in
|
2012-09-18 08:55:30 -07:00
|
|
|
match desc with
|
2013-09-04 08:12:37 -07:00
|
|
|
| Pcl_constr (lid, tys) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
constr ~loc ~attrs (map_loc sub lid) (List.map (sub.typ sub) tys)
|
2012-09-18 08:55:30 -07:00
|
|
|
| Pcl_structure s ->
|
2013-09-26 01:49:46 -07:00
|
|
|
structure ~loc ~attrs (sub.class_structure sub s)
|
2012-09-18 08:55:30 -07:00
|
|
|
| Pcl_fun (lab, e, p, ce) ->
|
2013-04-10 10:26:55 -07:00
|
|
|
fun_ ~loc ~attrs lab
|
2013-09-26 01:49:46 -07:00
|
|
|
(map_opt (sub.expr sub) e)
|
|
|
|
(sub.pat sub p)
|
|
|
|
(sub.class_expr sub ce)
|
2012-09-18 08:55:30 -07:00
|
|
|
| Pcl_apply (ce, l) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
apply ~loc ~attrs (sub.class_expr sub ce)
|
|
|
|
(List.map (map_snd (sub.expr sub)) l)
|
2013-06-03 08:14:19 -07:00
|
|
|
| Pcl_let (r, vbs, ce) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
let_ ~loc ~attrs r (List.map (sub.value_binding sub) vbs)
|
|
|
|
(sub.class_expr sub ce)
|
2012-09-18 08:55:30 -07:00
|
|
|
| Pcl_constraint (ce, ct) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
constraint_ ~loc ~attrs (sub.class_expr sub ce) (sub.class_type sub ct)
|
2013-09-25 08:14:38 -07:00
|
|
|
| Pcl_extension x -> extension ~loc ~attrs (sub.extension sub x)
|
2018-04-08 01:51:15 -07:00
|
|
|
| Pcl_open (o, ce) ->
|
|
|
|
open_ ~loc ~attrs (sub.open_description sub o) (sub.class_expr sub ce)
|
2012-09-18 08:55:30 -07:00
|
|
|
|
2013-04-10 04:17:41 -07:00
|
|
|
let map_kind sub = function
|
2013-09-26 01:49:46 -07:00
|
|
|
| Cfk_concrete (o, e) -> Cfk_concrete (o, sub.expr sub e)
|
|
|
|
| Cfk_virtual t -> Cfk_virtual (sub.typ sub t)
|
2012-09-18 08:55:30 -07:00
|
|
|
|
2013-04-10 10:26:55 -07:00
|
|
|
let map_field sub {pcf_desc = desc; pcf_loc = loc; pcf_attributes = attrs} =
|
2013-03-08 06:59:45 -08:00
|
|
|
let open Cf in
|
2013-09-26 01:49:46 -07:00
|
|
|
let loc = sub.location sub loc in
|
2015-07-17 07:31:05 -07:00
|
|
|
let attrs = sub.attributes sub attrs in
|
2012-09-18 08:55:30 -07:00
|
|
|
match desc with
|
2016-08-29 07:21:38 -07:00
|
|
|
| Pcf_inherit (o, ce, s) ->
|
|
|
|
inherit_ ~loc ~attrs o (sub.class_expr sub ce)
|
|
|
|
(map_opt (map_loc sub) s)
|
2013-04-10 10:26:55 -07:00
|
|
|
| Pcf_val (s, m, k) -> val_ ~loc ~attrs (map_loc sub s) m (map_kind sub k)
|
2013-09-04 08:12:37 -07:00
|
|
|
| Pcf_method (s, p, k) ->
|
|
|
|
method_ ~loc ~attrs (map_loc sub s) p (map_kind sub k)
|
|
|
|
| Pcf_constraint (t1, t2) ->
|
2013-09-26 01:49:46 -07:00
|
|
|
constraint_ ~loc ~attrs (sub.typ sub t1) (sub.typ sub t2)
|
|
|
|
| Pcf_initializer e -> initializer_ ~loc ~attrs (sub.expr sub e)
|
2014-05-04 13:42:34 -07:00
|
|
|
| Pcf_attribute x -> attribute ~loc (sub.attribute sub x)
|
2013-09-25 08:14:38 -07:00
|
|
|
| Pcf_extension x -> extension ~loc ~attrs (sub.extension sub x)
|
2012-09-18 08:55:30 -07:00
|
|
|
|
2013-04-10 02:35:09 -07:00
|
|
|
let map_structure sub {pcstr_self; pcstr_fields} =
|
2012-09-18 08:55:30 -07:00
|
|
|
{
|
2013-09-26 01:49:46 -07:00
|
|
|
pcstr_self = sub.pat sub pcstr_self;
|
|
|
|
pcstr_fields = List.map (sub.class_field sub) pcstr_fields;
|
2012-09-18 08:55:30 -07:00
|
|
|
}
|
2013-01-07 06:13:01 -08:00
|
|
|
|
2013-09-04 08:12:37 -07:00
|
|
|
let class_infos sub f {pci_virt; pci_params = pl; pci_name; pci_expr;
|
|
|
|
pci_loc; pci_attributes} =
|
2018-08-16 06:40:53 -07:00
|
|
|
let loc = sub.location sub pci_loc in
|
|
|
|
let attrs = sub.attributes sub pci_attributes in
|
|
|
|
Ci.mk ~loc ~attrs
|
2014-05-04 16:08:45 -07:00
|
|
|
~virt:pci_virt
|
|
|
|
~params:(List.map (map_fst (sub.typ sub)) pl)
|
2013-03-08 06:59:45 -08:00
|
|
|
(map_loc sub pci_name)
|
|
|
|
(f pci_expr)
|
2012-09-18 08:55:30 -07:00
|
|
|
end
|
2012-07-24 01:40:50 -07:00
|
|
|
|
2013-09-25 08:14:38 -07:00
|
|
|
(* Now, a generic AST mapper, to be extended to cover all kinds and
|
|
|
|
cases of the OCaml grammar. The default behavior of the mapper is
|
|
|
|
the identity. *)
|
|
|
|
|
|
|
|
let default_mapper =
|
|
|
|
{
|
2019-07-22 09:38:28 -07:00
|
|
|
constant = C.map;
|
2013-09-25 08:14:38 -07:00
|
|
|
structure = (fun this l -> List.map (this.structure_item this) l);
|
|
|
|
structure_item = M.map_structure_item;
|
|
|
|
module_expr = M.map;
|
|
|
|
signature = (fun this l -> List.map (this.signature_item this) l);
|
|
|
|
signature_item = MT.map_signature_item;
|
|
|
|
module_type = MT.map;
|
|
|
|
with_constraint = MT.map_with_constraint;
|
2013-09-29 00:22:34 -07:00
|
|
|
class_declaration =
|
|
|
|
(fun this -> CE.class_infos this (this.class_expr this));
|
2013-09-25 08:14:38 -07:00
|
|
|
class_expr = CE.map;
|
|
|
|
class_field = CE.map_field;
|
|
|
|
class_structure = CE.map_structure;
|
|
|
|
class_type = CT.map;
|
|
|
|
class_type_field = CT.map_field;
|
|
|
|
class_signature = CT.map_signature;
|
2013-09-29 00:22:34 -07:00
|
|
|
class_type_declaration =
|
|
|
|
(fun this -> CE.class_infos this (this.class_type this));
|
|
|
|
class_description =
|
|
|
|
(fun this -> CE.class_infos this (this.class_type this));
|
2013-09-25 08:14:38 -07:00
|
|
|
type_declaration = T.map_type_declaration;
|
|
|
|
type_kind = T.map_type_kind;
|
|
|
|
typ = T.map;
|
2014-05-04 16:08:45 -07:00
|
|
|
type_extension = T.map_type_extension;
|
2018-04-06 10:02:46 -07:00
|
|
|
type_exception = T.map_type_exception;
|
2014-05-04 16:08:45 -07:00
|
|
|
extension_constructor = T.map_extension_constructor;
|
2013-09-25 08:14:38 -07:00
|
|
|
value_description =
|
|
|
|
(fun this {pval_name; pval_type; pval_prim; pval_loc;
|
|
|
|
pval_attributes} ->
|
|
|
|
Val.mk
|
|
|
|
(map_loc this pval_name)
|
|
|
|
(this.typ this pval_type)
|
|
|
|
~attrs:(this.attributes this pval_attributes)
|
|
|
|
~loc:(this.location this pval_loc)
|
|
|
|
~prim:pval_prim
|
|
|
|
);
|
|
|
|
|
|
|
|
pat = P.map;
|
|
|
|
expr = E.map;
|
2018-07-29 14:29:01 -07:00
|
|
|
binding_op = E.map_binding_op;
|
2013-09-25 08:14:38 -07:00
|
|
|
|
|
|
|
module_declaration =
|
2014-01-30 04:18:34 -08:00
|
|
|
(fun this {pmd_name; pmd_type; pmd_attributes; pmd_loc} ->
|
2013-09-25 08:14:38 -07:00
|
|
|
Md.mk
|
|
|
|
(map_loc this pmd_name)
|
|
|
|
(this.module_type this pmd_type)
|
|
|
|
~attrs:(this.attributes this pmd_attributes)
|
2014-01-30 04:18:34 -08:00
|
|
|
~loc:(this.location this pmd_loc)
|
2013-09-25 08:14:38 -07:00
|
|
|
);
|
|
|
|
|
2018-10-26 07:47:15 -07:00
|
|
|
module_substitution =
|
|
|
|
(fun this {pms_name; pms_manifest; pms_attributes; pms_loc} ->
|
|
|
|
Ms.mk
|
|
|
|
(map_loc this pms_name)
|
|
|
|
(map_loc this pms_manifest)
|
|
|
|
~attrs:(this.attributes this pms_attributes)
|
|
|
|
~loc:(this.location this pms_loc)
|
|
|
|
);
|
|
|
|
|
2013-09-25 08:14:38 -07:00
|
|
|
module_type_declaration =
|
2014-01-30 04:18:34 -08:00
|
|
|
(fun this {pmtd_name; pmtd_type; pmtd_attributes; pmtd_loc} ->
|
|
|
|
Mtd.mk
|
|
|
|
(map_loc this pmtd_name)
|
|
|
|
?typ:(map_opt (this.module_type this) pmtd_type)
|
|
|
|
~attrs:(this.attributes this pmtd_attributes)
|
|
|
|
~loc:(this.location this pmtd_loc)
|
2013-09-25 08:14:38 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
module_binding =
|
2014-01-30 04:18:34 -08:00
|
|
|
(fun this {pmb_name; pmb_expr; pmb_attributes; pmb_loc} ->
|
2013-09-25 08:14:38 -07:00
|
|
|
Mb.mk (map_loc this pmb_name) (this.module_expr this pmb_expr)
|
|
|
|
~attrs:(this.attributes this pmb_attributes)
|
2014-01-30 04:18:34 -08:00
|
|
|
~loc:(this.location this pmb_loc)
|
2013-09-25 08:14:38 -07:00
|
|
|
);
|
|
|
|
|
2014-04-15 04:26:00 -07:00
|
|
|
|
2018-04-08 01:51:15 -07:00
|
|
|
open_declaration =
|
|
|
|
(fun this {popen_expr; popen_override; popen_attributes; popen_loc} ->
|
|
|
|
Opn.mk (this.module_expr this popen_expr)
|
2014-04-15 04:26:00 -07:00
|
|
|
~override:popen_override
|
2014-04-22 08:28:20 -07:00
|
|
|
~loc:(this.location this popen_loc)
|
2014-04-15 04:26:00 -07:00
|
|
|
~attrs:(this.attributes this popen_attributes)
|
|
|
|
);
|
|
|
|
|
2018-04-08 01:51:15 -07:00
|
|
|
open_description =
|
|
|
|
(fun this {popen_expr; popen_override; popen_attributes; popen_loc} ->
|
|
|
|
Opn.mk (map_loc this popen_expr)
|
|
|
|
~override:popen_override
|
|
|
|
~loc:(this.location this popen_loc)
|
|
|
|
~attrs:(this.attributes this popen_attributes)
|
|
|
|
);
|
2014-04-15 04:26:00 -07:00
|
|
|
|
|
|
|
include_description =
|
2014-04-22 08:28:20 -07:00
|
|
|
(fun this {pincl_mod; pincl_attributes; pincl_loc} ->
|
2014-04-15 04:26:00 -07:00
|
|
|
Incl.mk (this.module_type this pincl_mod)
|
2014-04-22 08:28:20 -07:00
|
|
|
~loc:(this.location this pincl_loc)
|
2014-04-15 04:26:00 -07:00
|
|
|
~attrs:(this.attributes this pincl_attributes)
|
|
|
|
);
|
|
|
|
|
|
|
|
include_declaration =
|
2014-04-22 08:28:20 -07:00
|
|
|
(fun this {pincl_mod; pincl_attributes; pincl_loc} ->
|
2014-04-15 04:26:00 -07:00
|
|
|
Incl.mk (this.module_expr this pincl_mod)
|
2014-04-22 08:28:20 -07:00
|
|
|
~loc:(this.location this pincl_loc)
|
2014-04-15 04:26:00 -07:00
|
|
|
~attrs:(this.attributes this pincl_attributes)
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2013-09-25 08:14:38 -07:00
|
|
|
value_binding =
|
2014-04-22 08:28:20 -07:00
|
|
|
(fun this {pvb_pat; pvb_expr; pvb_attributes; pvb_loc} ->
|
2013-09-25 08:14:38 -07:00
|
|
|
Vb.mk
|
|
|
|
(this.pat this pvb_pat)
|
|
|
|
(this.expr this pvb_expr)
|
2014-04-22 08:28:20 -07:00
|
|
|
~loc:(this.location this pvb_loc)
|
2013-09-25 08:14:38 -07:00
|
|
|
~attrs:(this.attributes this pvb_attributes)
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
constructor_declaration =
|
|
|
|
(fun this {pcd_name; pcd_args; pcd_res; pcd_loc; pcd_attributes} ->
|
|
|
|
Type.constructor
|
|
|
|
(map_loc this pcd_name)
|
2014-10-14 08:51:30 -07:00
|
|
|
~args:(T.map_constructor_arguments this pcd_args)
|
2013-09-25 08:14:38 -07:00
|
|
|
?res:(map_opt (this.typ this) pcd_res)
|
|
|
|
~loc:(this.location this pcd_loc)
|
|
|
|
~attrs:(this.attributes this pcd_attributes)
|
|
|
|
);
|
|
|
|
|
|
|
|
label_declaration =
|
|
|
|
(fun this {pld_name; pld_type; pld_loc; pld_mutable; pld_attributes} ->
|
|
|
|
Type.field
|
|
|
|
(map_loc this pld_name)
|
|
|
|
(this.typ this pld_type)
|
|
|
|
~mut:pld_mutable
|
|
|
|
~loc:(this.location this pld_loc)
|
|
|
|
~attrs:(this.attributes this pld_attributes)
|
|
|
|
);
|
|
|
|
|
|
|
|
cases = (fun this l -> List.map (this.case this) l);
|
|
|
|
case =
|
|
|
|
(fun this {pc_lhs; pc_guard; pc_rhs} ->
|
|
|
|
{
|
|
|
|
pc_lhs = this.pat this pc_lhs;
|
|
|
|
pc_guard = map_opt (this.expr this) pc_guard;
|
|
|
|
pc_rhs = this.expr this pc_rhs;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-09 02:40:16 -08:00
|
|
|
location = (fun _this l -> l);
|
2013-09-25 08:14:38 -07:00
|
|
|
|
|
|
|
extension = (fun this (s, e) -> (map_loc this s, this.payload this e));
|
2018-07-21 05:04:53 -07:00
|
|
|
attribute = (fun this a ->
|
|
|
|
{
|
|
|
|
attr_name = map_loc this a.attr_name;
|
|
|
|
attr_payload = this.payload this a.attr_payload;
|
|
|
|
attr_loc = this.location this a.attr_loc
|
|
|
|
}
|
|
|
|
);
|
2013-09-25 08:14:38 -07:00
|
|
|
attributes = (fun this l -> List.map (this.attribute this) l);
|
2013-09-26 01:24:38 -07:00
|
|
|
payload =
|
|
|
|
(fun this -> function
|
|
|
|
| PStr x -> PStr (this.structure this x)
|
2015-12-02 06:20:26 -08:00
|
|
|
| PSig x -> PSig (this.signature this x)
|
2013-09-26 01:24:38 -07:00
|
|
|
| PTyp x -> PTyp (this.typ this x)
|
|
|
|
| PPat (x, g) -> PPat (this.pat this x, map_opt (this.expr this) g)
|
|
|
|
);
|
2013-09-25 08:14:38 -07:00
|
|
|
}
|
2013-03-01 04:44:04 -08:00
|
|
|
|
2018-07-27 05:22:22 -07:00
|
|
|
let extension_of_error {kind; main; sub} =
|
|
|
|
if kind <> Location.Report_error then
|
|
|
|
raise (Invalid_argument "extension_of_error: expected kind Report_error");
|
2018-08-12 12:38:44 -07:00
|
|
|
let str_of_pp pp_msg = Format.asprintf "%t" pp_msg in
|
2018-07-27 05:22:22 -07:00
|
|
|
let extension_of_sub sub =
|
|
|
|
{ loc = sub.loc; txt = "ocaml.error" },
|
2019-07-22 09:04:09 -07:00
|
|
|
PStr ([Str.eval (Exp.constant
|
|
|
|
(Pconst_string (str_of_pp sub.txt, sub.loc, None)))])
|
2018-07-27 05:22:22 -07:00
|
|
|
in
|
|
|
|
{ loc = main.loc; txt = "ocaml.error" },
|
2019-07-22 09:04:09 -07:00
|
|
|
PStr (Str.eval (Exp.constant
|
|
|
|
(Pconst_string (str_of_pp main.txt, main.loc, None))) ::
|
2018-07-27 05:22:22 -07:00
|
|
|
List.map (fun msg -> Str.extension (extension_of_sub msg)) sub)
|
2012-07-24 05:48:39 -07:00
|
|
|
|
2014-05-07 07:52:19 -07:00
|
|
|
let attribute_of_warning loc s =
|
2018-07-21 05:04:53 -07:00
|
|
|
Attr.mk
|
|
|
|
{loc; txt = "ocaml.ppwarning" }
|
2019-07-22 09:04:09 -07:00
|
|
|
(PStr ([Str.eval ~loc (Exp.constant (Pconst_string (s, loc, None)))]))
|
2014-08-07 02:46:34 -07:00
|
|
|
|
2018-07-23 05:19:41 -07:00
|
|
|
let cookies = ref String.Map.empty
|
2014-09-23 07:54:01 -07:00
|
|
|
|
|
|
|
let get_cookie k =
|
2018-07-23 05:19:41 -07:00
|
|
|
try Some (String.Map.find k !cookies)
|
2014-09-23 07:54:01 -07:00
|
|
|
with Not_found -> None
|
|
|
|
|
|
|
|
let set_cookie k v =
|
2018-07-23 05:19:41 -07:00
|
|
|
cookies := String.Map.add k v !cookies
|
2014-09-23 07:54:01 -07:00
|
|
|
|
2014-08-07 02:46:34 -07:00
|
|
|
let tool_name_ref = ref "_none_"
|
|
|
|
|
|
|
|
let tool_name () = !tool_name_ref
|
|
|
|
|
2014-09-23 07:54:01 -07:00
|
|
|
|
|
|
|
module PpxContext = struct
|
|
|
|
open Longident
|
|
|
|
open Asttypes
|
|
|
|
open Ast_helper
|
|
|
|
|
|
|
|
let lid name = { txt = Lident name; loc = Location.none }
|
|
|
|
|
2019-07-22 09:04:09 -07:00
|
|
|
let make_string s = Exp.constant (Const.string s)
|
2014-09-23 07:54:01 -07:00
|
|
|
|
|
|
|
let make_bool x =
|
|
|
|
if x
|
|
|
|
then Exp.construct (lid "true") None
|
|
|
|
else Exp.construct (lid "false") None
|
|
|
|
|
|
|
|
let rec make_list f lst =
|
|
|
|
match lst with
|
|
|
|
| x :: rest ->
|
|
|
|
Exp.construct (lid "::") (Some (Exp.tuple [f x; make_list f rest]))
|
|
|
|
| [] ->
|
|
|
|
Exp.construct (lid "[]") None
|
|
|
|
|
|
|
|
let make_pair f1 f2 (x1, x2) =
|
|
|
|
Exp.tuple [f1 x1; f2 x2]
|
|
|
|
|
|
|
|
let make_option f opt =
|
|
|
|
match opt with
|
|
|
|
| Some x -> Exp.construct (lid "Some") (Some (f x))
|
|
|
|
| None -> Exp.construct (lid "None") None
|
|
|
|
|
|
|
|
let get_cookies () =
|
|
|
|
lid "cookies",
|
|
|
|
make_list (make_pair make_string (fun x -> x))
|
2018-07-23 05:19:41 -07:00
|
|
|
(String.Map.bindings !cookies)
|
2014-09-23 07:54:01 -07:00
|
|
|
|
|
|
|
let mk fields =
|
2018-07-21 05:04:53 -07:00
|
|
|
{
|
|
|
|
attr_name = { txt = "ocaml.ppx.context"; loc = Location.none };
|
|
|
|
attr_payload = Parsetree.PStr [Str.eval (Exp.record fields None)];
|
|
|
|
attr_loc = Location.none
|
|
|
|
}
|
2014-09-23 07:54:01 -07:00
|
|
|
|
|
|
|
let make ~tool_name () =
|
|
|
|
let fields =
|
|
|
|
[
|
|
|
|
lid "tool_name", make_string tool_name;
|
|
|
|
lid "include_dirs", make_list make_string !Clflags.include_dirs;
|
2018-09-18 06:49:18 -07:00
|
|
|
lid "load_path", make_list make_string (Load_path.get_paths ());
|
2014-09-23 07:54:01 -07:00
|
|
|
lid "open_modules", make_list make_string !Clflags.open_modules;
|
|
|
|
lid "for_package", make_option make_string !Clflags.for_package;
|
|
|
|
lid "debug", make_bool !Clflags.debug;
|
2017-09-15 10:14:14 -07:00
|
|
|
lid "use_threads", make_bool !Clflags.use_threads;
|
2019-03-13 02:46:30 -07:00
|
|
|
lid "use_vmthreads", make_bool false;
|
2018-07-20 00:01:30 -07:00
|
|
|
lid "recursive_types", make_bool !Clflags.recursive_types;
|
|
|
|
lid "principal", make_bool !Clflags.principal;
|
|
|
|
lid "transparent_modules", make_bool !Clflags.transparent_modules;
|
|
|
|
lid "unboxed_types", make_bool !Clflags.unboxed_types;
|
|
|
|
lid "unsafe_string", make_bool !Clflags.unsafe_string;
|
2014-09-23 07:54:01 -07:00
|
|
|
get_cookies ()
|
|
|
|
]
|
|
|
|
in
|
|
|
|
mk fields
|
|
|
|
|
|
|
|
let get_fields = function
|
2014-08-07 02:46:34 -07:00
|
|
|
| PStr [{pstr_desc = Pstr_eval
|
|
|
|
({ pexp_desc = Pexp_record (fields, None) }, [])}] ->
|
|
|
|
fields
|
|
|
|
| _ ->
|
|
|
|
raise_errorf "Internal error: invalid [@@@ocaml.ppx.context] syntax"
|
2014-09-23 07:54:01 -07:00
|
|
|
|
|
|
|
let restore fields =
|
|
|
|
let field name payload =
|
|
|
|
let rec get_string = function
|
2019-07-22 09:04:09 -07:00
|
|
|
| { pexp_desc = Pexp_constant (Pconst_string (str, _, None)) } -> str
|
2015-09-11 04:58:31 -07:00
|
|
|
| _ -> raise_errorf "Internal error: invalid [@@@ocaml.ppx.context \
|
|
|
|
{ %s }] string syntax" name
|
2014-09-23 07:54:01 -07:00
|
|
|
and get_bool pexp =
|
|
|
|
match pexp with
|
2015-09-11 04:58:31 -07:00
|
|
|
| {pexp_desc = Pexp_construct ({txt = Longident.Lident "true"},
|
|
|
|
None)} ->
|
2014-09-23 07:54:01 -07:00
|
|
|
true
|
2015-09-11 04:58:31 -07:00
|
|
|
| {pexp_desc = Pexp_construct ({txt = Longident.Lident "false"},
|
|
|
|
None)} ->
|
2014-09-23 07:54:01 -07:00
|
|
|
false
|
2015-09-11 04:58:31 -07:00
|
|
|
| _ -> raise_errorf "Internal error: invalid [@@@ocaml.ppx.context \
|
|
|
|
{ %s }] bool syntax" name
|
2014-09-23 07:54:01 -07:00
|
|
|
and get_list elem = function
|
|
|
|
| {pexp_desc =
|
|
|
|
Pexp_construct ({txt = Longident.Lident "::"},
|
|
|
|
Some {pexp_desc = Pexp_tuple [exp; rest]}) } ->
|
|
|
|
elem exp :: get_list elem rest
|
|
|
|
| {pexp_desc =
|
|
|
|
Pexp_construct ({txt = Longident.Lident "[]"}, None)} ->
|
|
|
|
[]
|
2015-09-11 04:58:31 -07:00
|
|
|
| _ -> raise_errorf "Internal error: invalid [@@@ocaml.ppx.context \
|
|
|
|
{ %s }] list syntax" name
|
2014-09-23 07:54:01 -07:00
|
|
|
and get_pair f1 f2 = function
|
|
|
|
| {pexp_desc = Pexp_tuple [e1; e2]} ->
|
|
|
|
(f1 e1, f2 e2)
|
2015-09-11 04:58:31 -07:00
|
|
|
| _ -> raise_errorf "Internal error: invalid [@@@ocaml.ppx.context \
|
|
|
|
{ %s }] pair syntax" name
|
2014-09-23 07:54:01 -07:00
|
|
|
and get_option elem = function
|
|
|
|
| { pexp_desc =
|
|
|
|
Pexp_construct ({ txt = Longident.Lident "Some" }, Some exp) } ->
|
|
|
|
Some (elem exp)
|
|
|
|
| { pexp_desc =
|
|
|
|
Pexp_construct ({ txt = Longident.Lident "None" }, None) } ->
|
|
|
|
None
|
2015-09-11 04:58:31 -07:00
|
|
|
| _ -> raise_errorf "Internal error: invalid [@@@ocaml.ppx.context \
|
|
|
|
{ %s }] option syntax" name
|
2014-09-23 07:54:01 -07:00
|
|
|
in
|
|
|
|
match name with
|
|
|
|
| "tool_name" ->
|
|
|
|
tool_name_ref := get_string payload
|
|
|
|
| "include_dirs" ->
|
|
|
|
Clflags.include_dirs := get_list get_string payload
|
|
|
|
| "load_path" ->
|
2018-09-18 06:49:18 -07:00
|
|
|
Load_path.init (get_list get_string payload)
|
2014-09-23 07:54:01 -07:00
|
|
|
| "open_modules" ->
|
|
|
|
Clflags.open_modules := get_list get_string payload
|
|
|
|
| "for_package" ->
|
|
|
|
Clflags.for_package := get_option get_string payload
|
|
|
|
| "debug" ->
|
|
|
|
Clflags.debug := get_bool payload
|
2017-09-15 10:14:14 -07:00
|
|
|
| "use_threads" ->
|
|
|
|
Clflags.use_threads := get_bool payload
|
2019-03-13 02:46:30 -07:00
|
|
|
| "use_vmthreads" ->
|
|
|
|
if get_bool payload then
|
|
|
|
raise_errorf "Internal error: vmthreads not supported after 4.09.0"
|
2018-07-20 00:01:30 -07:00
|
|
|
| "recursive_types" ->
|
|
|
|
Clflags.recursive_types := get_bool payload
|
|
|
|
| "principal" ->
|
|
|
|
Clflags.principal := get_bool payload
|
|
|
|
| "transparent_modules" ->
|
|
|
|
Clflags.transparent_modules := get_bool payload
|
|
|
|
| "unboxed_types" ->
|
|
|
|
Clflags.unboxed_types := get_bool payload
|
|
|
|
| "unsafe_string" ->
|
|
|
|
Clflags.unsafe_string := get_bool payload
|
2014-09-23 07:54:01 -07:00
|
|
|
| "cookies" ->
|
|
|
|
let l = get_list (get_pair get_string (fun x -> x)) payload in
|
|
|
|
cookies :=
|
|
|
|
List.fold_left
|
2018-07-23 05:19:41 -07:00
|
|
|
(fun s (k, v) -> String.Map.add k v s) String.Map.empty
|
2014-09-23 07:54:01 -07:00
|
|
|
l
|
2014-08-07 02:46:34 -07:00
|
|
|
| _ ->
|
2014-09-23 07:54:01 -07:00
|
|
|
()
|
2014-08-07 02:46:34 -07:00
|
|
|
in
|
2014-09-23 07:54:01 -07:00
|
|
|
List.iter (function ({txt=Lident name}, x) -> field name x | _ -> ()) fields
|
2014-05-07 07:52:19 -07:00
|
|
|
|
2014-09-23 07:54:01 -07:00
|
|
|
let update_cookies fields =
|
|
|
|
let fields =
|
|
|
|
List.filter
|
|
|
|
(function ({txt=Lident "cookies"}, _) -> false | _ -> true)
|
|
|
|
fields
|
|
|
|
in
|
|
|
|
fields @ [get_cookies ()]
|
|
|
|
end
|
|
|
|
|
|
|
|
let ppx_context = PpxContext.make
|
|
|
|
|
2017-03-08 01:16:01 -08:00
|
|
|
let extension_of_exn exn =
|
Fix #7098 (loss of ppx context in toplevel after an exception)
When a ppx raised an exception, it was returned as an error "extension node" to
the caller, without including the "ppx context" which includes in particular ppx
cookies. This was fine when only one ppx was used, since in that case, the
missing context was not restored.
When multiple ppx are chained, however, the second ppx sees the result of the
first one, and it it does not contains a context, an empty one is created and
returned to the caller, which has the effect of destroying the context for the
next invocation of ppxs.
The current fix is to ensure that when a "error extension node" is returned, the
context is correctly added to it. In addition to fixing the reported bug, it also
allow ppx to update their cookies before raising an exception, which can be
useful.
2015-12-17 05:59:33 -08:00
|
|
|
match error_of_exn exn with
|
2017-03-08 01:16:01 -08:00
|
|
|
| Some (`Ok error) -> extension_of_error error
|
2017-08-12 13:24:41 -07:00
|
|
|
| Some `Already_displayed ->
|
|
|
|
{ loc = Location.none; txt = "ocaml.error" }, PStr []
|
Fix #7098 (loss of ppx context in toplevel after an exception)
When a ppx raised an exception, it was returned as an error "extension node" to
the caller, without including the "ppx context" which includes in particular ppx
cookies. This was fine when only one ppx was used, since in that case, the
missing context was not restored.
When multiple ppx are chained, however, the second ppx sees the result of the
first one, and it it does not contains a context, an empty one is created and
returned to the caller, which has the effect of destroying the context for the
next invocation of ppxs.
The current fix is to ensure that when a "error extension node" is returned, the
context is correctly added to it. In addition to fixing the reported bug, it also
allow ppx to update their cookies before raising an exception, which can be
useful.
2015-12-17 05:59:33 -08:00
|
|
|
| None -> raise exn
|
|
|
|
|
2014-09-23 07:54:01 -07:00
|
|
|
|
|
|
|
let apply_lazy ~source ~target mapper =
|
2014-08-07 02:46:34 -07:00
|
|
|
let implem ast =
|
Fix #7098 (loss of ppx context in toplevel after an exception)
When a ppx raised an exception, it was returned as an error "extension node" to
the caller, without including the "ppx context" which includes in particular ppx
cookies. This was fine when only one ppx was used, since in that case, the
missing context was not restored.
When multiple ppx are chained, however, the second ppx sees the result of the
first one, and it it does not contains a context, an empty one is created and
returned to the caller, which has the effect of destroying the context for the
next invocation of ppxs.
The current fix is to ensure that when a "error extension node" is returned, the
context is correctly added to it. In addition to fixing the reported bug, it also
allow ppx to update their cookies before raising an exception, which can be
useful.
2015-12-17 05:59:33 -08:00
|
|
|
let fields, ast =
|
|
|
|
match ast with
|
2018-07-21 05:04:53 -07:00
|
|
|
| {pstr_desc = Pstr_attribute ({attr_name = {txt = "ocaml.ppx.context"};
|
|
|
|
attr_payload = x})} :: l ->
|
Fix #7098 (loss of ppx context in toplevel after an exception)
When a ppx raised an exception, it was returned as an error "extension node" to
the caller, without including the "ppx context" which includes in particular ppx
cookies. This was fine when only one ppx was used, since in that case, the
missing context was not restored.
When multiple ppx are chained, however, the second ppx sees the result of the
first one, and it it does not contains a context, an empty one is created and
returned to the caller, which has the effect of destroying the context for the
next invocation of ppxs.
The current fix is to ensure that when a "error extension node" is returned, the
context is correctly added to it. In addition to fixing the reported bug, it also
allow ppx to update their cookies before raising an exception, which can be
useful.
2015-12-17 05:59:33 -08:00
|
|
|
PpxContext.get_fields x, l
|
|
|
|
| _ -> [], ast
|
|
|
|
in
|
|
|
|
PpxContext.restore fields;
|
|
|
|
let ast =
|
|
|
|
try
|
|
|
|
let mapper = mapper () in
|
|
|
|
mapper.structure mapper ast
|
|
|
|
with exn ->
|
2017-03-08 01:16:01 -08:00
|
|
|
[{pstr_desc = Pstr_extension (extension_of_exn exn, []);
|
Fix #7098 (loss of ppx context in toplevel after an exception)
When a ppx raised an exception, it was returned as an error "extension node" to
the caller, without including the "ppx context" which includes in particular ppx
cookies. This was fine when only one ppx was used, since in that case, the
missing context was not restored.
When multiple ppx are chained, however, the second ppx sees the result of the
first one, and it it does not contains a context, an empty one is created and
returned to the caller, which has the effect of destroying the context for the
next invocation of ppxs.
The current fix is to ensure that when a "error extension node" is returned, the
context is correctly added to it. In addition to fixing the reported bug, it also
allow ppx to update their cookies before raising an exception, which can be
useful.
2015-12-17 05:59:33 -08:00
|
|
|
pstr_loc = Location.none}]
|
|
|
|
in
|
|
|
|
let fields = PpxContext.update_cookies fields in
|
|
|
|
Str.attribute (PpxContext.mk fields) :: ast
|
2013-01-16 08:10:29 -08:00
|
|
|
in
|
2014-08-07 02:46:34 -07:00
|
|
|
let iface ast =
|
Fix #7098 (loss of ppx context in toplevel after an exception)
When a ppx raised an exception, it was returned as an error "extension node" to
the caller, without including the "ppx context" which includes in particular ppx
cookies. This was fine when only one ppx was used, since in that case, the
missing context was not restored.
When multiple ppx are chained, however, the second ppx sees the result of the
first one, and it it does not contains a context, an empty one is created and
returned to the caller, which has the effect of destroying the context for the
next invocation of ppxs.
The current fix is to ensure that when a "error extension node" is returned, the
context is correctly added to it. In addition to fixing the reported bug, it also
allow ppx to update their cookies before raising an exception, which can be
useful.
2015-12-17 05:59:33 -08:00
|
|
|
let fields, ast =
|
|
|
|
match ast with
|
2018-07-21 05:04:53 -07:00
|
|
|
| {psig_desc = Psig_attribute ({attr_name = {txt = "ocaml.ppx.context"};
|
|
|
|
attr_payload = x;
|
|
|
|
attr_loc = _})} :: l ->
|
Fix #7098 (loss of ppx context in toplevel after an exception)
When a ppx raised an exception, it was returned as an error "extension node" to
the caller, without including the "ppx context" which includes in particular ppx
cookies. This was fine when only one ppx was used, since in that case, the
missing context was not restored.
When multiple ppx are chained, however, the second ppx sees the result of the
first one, and it it does not contains a context, an empty one is created and
returned to the caller, which has the effect of destroying the context for the
next invocation of ppxs.
The current fix is to ensure that when a "error extension node" is returned, the
context is correctly added to it. In addition to fixing the reported bug, it also
allow ppx to update their cookies before raising an exception, which can be
useful.
2015-12-17 05:59:33 -08:00
|
|
|
PpxContext.get_fields x, l
|
|
|
|
| _ -> [], ast
|
|
|
|
in
|
|
|
|
PpxContext.restore fields;
|
|
|
|
let ast =
|
|
|
|
try
|
|
|
|
let mapper = mapper () in
|
|
|
|
mapper.signature mapper ast
|
|
|
|
with exn ->
|
2017-03-08 01:16:01 -08:00
|
|
|
[{psig_desc = Psig_extension (extension_of_exn exn, []);
|
Fix #7098 (loss of ppx context in toplevel after an exception)
When a ppx raised an exception, it was returned as an error "extension node" to
the caller, without including the "ppx context" which includes in particular ppx
cookies. This was fine when only one ppx was used, since in that case, the
missing context was not restored.
When multiple ppx are chained, however, the second ppx sees the result of the
first one, and it it does not contains a context, an empty one is created and
returned to the caller, which has the effect of destroying the context for the
next invocation of ppxs.
The current fix is to ensure that when a "error extension node" is returned, the
context is correctly added to it. In addition to fixing the reported bug, it also
allow ppx to update their cookies before raising an exception, which can be
useful.
2015-12-17 05:59:33 -08:00
|
|
|
psig_loc = Location.none}]
|
|
|
|
in
|
|
|
|
let fields = PpxContext.update_cookies fields in
|
|
|
|
Sig.attribute (PpxContext.mk fields) :: ast
|
2014-08-07 02:46:34 -07:00
|
|
|
in
|
2014-12-13 14:35:55 -08:00
|
|
|
|
|
|
|
let ic = open_in_bin source in
|
|
|
|
let magic =
|
|
|
|
really_input_string ic (String.length Config.ast_impl_magic_number)
|
|
|
|
in
|
|
|
|
|
|
|
|
let rewrite transform =
|
|
|
|
Location.input_name := input_value ic;
|
|
|
|
let ast = input_value ic in
|
|
|
|
close_in ic;
|
|
|
|
let ast = transform ast in
|
|
|
|
let oc = open_out_bin target in
|
|
|
|
output_string oc magic;
|
|
|
|
output_value oc !Location.input_name;
|
|
|
|
output_value oc ast;
|
|
|
|
close_out oc
|
|
|
|
and fail () =
|
|
|
|
close_in ic;
|
|
|
|
failwith "Ast_mapper: OCaml version mismatch or malformed input";
|
2014-08-07 02:46:34 -07:00
|
|
|
in
|
2014-12-13 14:35:55 -08:00
|
|
|
|
|
|
|
if magic = Config.ast_impl_magic_number then
|
|
|
|
rewrite (implem : structure -> structure)
|
|
|
|
else if magic = Config.ast_intf_magic_number then
|
|
|
|
rewrite (iface : signature -> signature)
|
|
|
|
else fail ()
|
2013-01-16 08:10:29 -08:00
|
|
|
|
2014-09-23 07:54:01 -07:00
|
|
|
let drop_ppx_context_str ~restore = function
|
2018-07-21 05:04:53 -07:00
|
|
|
| {pstr_desc = Pstr_attribute
|
|
|
|
{attr_name = {Location.txt = "ocaml.ppx.context"};
|
|
|
|
attr_payload = a;
|
|
|
|
attr_loc = _}}
|
2014-09-23 07:54:01 -07:00
|
|
|
:: items ->
|
|
|
|
if restore then
|
|
|
|
PpxContext.restore (PpxContext.get_fields a);
|
|
|
|
items
|
|
|
|
| items -> items
|
|
|
|
|
|
|
|
let drop_ppx_context_sig ~restore = function
|
2018-07-21 05:04:53 -07:00
|
|
|
| {psig_desc = Psig_attribute
|
|
|
|
{attr_name = {Location.txt = "ocaml.ppx.context"};
|
|
|
|
attr_payload = a;
|
|
|
|
attr_loc = _}}
|
2014-09-23 07:54:01 -07:00
|
|
|
:: items ->
|
|
|
|
if restore then
|
|
|
|
PpxContext.restore (PpxContext.get_fields a);
|
|
|
|
items
|
|
|
|
| items -> items
|
|
|
|
|
|
|
|
let add_ppx_context_str ~tool_name ast =
|
|
|
|
Ast_helper.Str.attribute (ppx_context ~tool_name ()) :: ast
|
|
|
|
|
|
|
|
let add_ppx_context_sig ~tool_name ast =
|
|
|
|
Ast_helper.Sig.attribute (ppx_context ~tool_name ()) :: ast
|
|
|
|
|
|
|
|
|
|
|
|
let apply ~source ~target mapper =
|
|
|
|
apply_lazy ~source ~target (fun () -> mapper)
|
|
|
|
|
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
|
2014-09-23 07:54:01 -07:00
|
|
|
let mapper () =
|
2014-08-07 02:42:30 -07:00
|
|
|
try mapper (Array.to_list (Array.sub a 1 (n - 3)))
|
|
|
|
with exn ->
|
2017-08-10 03:59:23 -07:00
|
|
|
(* PR#6463 *)
|
2014-08-07 02:42:30 -07:00
|
|
|
let f _ _ = raise exn in
|
|
|
|
{default_mapper with structure = f; signature = f}
|
|
|
|
in
|
2014-09-23 07:54:01 -07:00
|
|
|
apply_lazy ~source:a.(n - 2) ~target:a.(n - 1) mapper
|
2013-01-16 08:10:29 -08:00
|
|
|
else begin
|
2013-09-04 08:12:37 -07:00
|
|
|
Printf.eprintf "Usage: %s [extra_args] <infile> <outfile>\n%!"
|
|
|
|
Sys.executable_name;
|
2013-09-26 01:03:49 -07:00
|
|
|
exit 2
|
2013-01-16 08:10:29 -08:00
|
|
|
end
|
|
|
|
with exn ->
|
2014-05-07 01:26:17 -07:00
|
|
|
prerr_endline (Printexc.to_string exn);
|
2013-01-16 08:10:29 -08:00
|
|
|
exit 2
|
2012-07-24 05:48:39 -07:00
|
|
|
|
2013-01-23 02:15:45 -08:00
|
|
|
let register_function = ref (fun _name f -> run_main f)
|
2013-09-26 01:03:49 -07:00
|
|
|
let register name f = !register_function name f
|