Skip to content

Commit 35f6246

Browse files
Inline not-compat-anymore functions into the module
1 parent 4ba9878 commit 35f6246

File tree

3 files changed

+104
-173
lines changed

3 files changed

+104
-173
lines changed

lib/top/compat_top.ml

Lines changed: 0 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,3 @@
1-
let map_error_loc ~f (error : Location.error) =
2-
let f_msg (msg : Location.msg) =
3-
{ msg with loc = f msg.loc}
4-
in
5-
{ error with main = f_msg error.main;
6-
sub = List.map f_msg error.sub; }
7-
8-
let error_of_exn exn =
9-
match Location.error_of_exn exn with
10-
| None -> None
11-
| Some `Already_displayed -> None
12-
| Some (`Ok error) -> Some error
13-
14-
let rec get_id_in_path = function
15-
| Path.Pident id -> id
16-
| Path.Pdot (p, _) -> get_id_in_path p
17-
| Path.Papply (_, p) -> get_id_in_path p
18-
191
let lookup_type typ env =
202
#if OCAML_VERSION >= (4, 10, 0)
213
Env.find_type_by_name typ env |> fst
@@ -90,67 +72,6 @@ let type_structure env str loc =
9072
in
9173
tstr, env
9274

93-
let sig_value id desc =
94-
Types.Sig_value (id, desc, Exported)
95-
96-
let sig_type id desc =
97-
Types.Sig_type (id, desc, Trec_not, Exported)
98-
99-
let sig_typext id ext =
100-
Types.Sig_typext (id, ext, Text_exception, Exported)
101-
102-
let sig_module id md =
103-
Types.Sig_module (id, Mp_present, md, Trec_not, Exported)
104-
105-
let mty_path =
106-
let open Types in
107-
function
108-
| Mty_alias path -> Some path
109-
| Mty_ident _
110-
| Mty_signature _
111-
| Mty_functor _ ->
112-
None
113-
114-
let sig_modtype id desc =
115-
Types.Sig_modtype (id, desc, Exported)
116-
117-
let sig_class id desc =
118-
Types.Sig_class (id, desc, Trec_not, Exported)
119-
120-
let sig_class_type id desc =
121-
Types.Sig_class_type (id, desc, Trec_not, Exported)
122-
123-
let add_directive ~name ~doc kind =
124-
let directive = match kind with
125-
| `Bool f -> Toploop.Directive_bool f
126-
| `Show_prim to_sig ->
127-
let show_prim to_sig lid =
128-
let env = !Toploop.toplevel_env in
129-
let loc = Location.none in
130-
try
131-
let s =
132-
match lid with
133-
| Longident.Lident s -> s
134-
| Longident.Ldot (_,s) -> s
135-
| Longident.Lapply _ ->
136-
Format.printf "Invalid path %a@." Printtyp.longident lid;
137-
raise Exit
138-
in
139-
let id = Ident.create_persistent s in
140-
let sg = to_sig env loc id lid in
141-
Printtyp.wrap_printing_env ~error:false env (fun () ->
142-
Format.printf "@[%a@]@." Printtyp.signature sg
143-
)
144-
with
145-
| Not_found -> Format.printf "@[Unknown element.@]@."
146-
| Exit -> ()
147-
in
148-
(Toploop.Directive_ident (show_prim to_sig))
149-
in
150-
Toploop.add_directive name
151-
directive
152-
{ section = "Environment queries"; doc }
153-
15475
let extension_constructor
15576
~ext_type_path
15677
~ext_type_params
@@ -176,35 +97,6 @@ let extension_constructor
17697
#endif
17798
}
17899

179-
let is_predef_or_global id =
180-
Ident.is_predef id || Ident.global id
181-
182-
let map_sig_attributes ~f =
183-
let open Types in
184-
List.map (function
185-
| Sig_module (id, mp, md, rs, visibility) ->
186-
Sig_module (
187-
id,
188-
mp,
189-
{md with md_attributes = f md.md_attributes },
190-
rs,
191-
visibility
192-
)
193-
| item -> item)
194-
195-
let attribute ~name ~payload =
196-
{ Parsetree.attr_name = name
197-
; attr_payload = payload
198-
; attr_loc = Location.none
199-
}
200-
201-
module Linked = struct
202-
include (Topdirs : sig end)
203-
include (Ephemeron : sig end)
204-
include (Uchar : sig end)
205-
include (Condition : sig end)
206-
end
207-
208100
let match_env
209101
~value
210102
~empty
@@ -251,20 +143,6 @@ let match_env
251143
#endif
252144
| Env_persistent (summary, _) -> persistent summary
253145

254-
let top_directive_name (toplevel_phrase : Parsetree.toplevel_phrase) =
255-
match toplevel_phrase with
256-
| Ptop_def _ -> None
257-
| Ptop_dir { pdir_name = { txt; _}; _ } -> Some txt
258-
259-
let top_directive_require pkg =
260-
Parsetree.Ptop_dir
261-
{
262-
pdir_name = { txt = "require"; loc = Location.none };
263-
pdir_arg =
264-
Some { pdira_desc = Pdir_string pkg; pdira_loc = Location.none };
265-
pdir_loc = Location.none;
266-
}
267-
268146
let ctype_is_equal =
269147
#if OCAML_VERSION >= (4, 13, 0)
270148
Ctype.is_equal

lib/top/compat_top.mli

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
val map_error_loc :
2-
f:(Location.t -> Location.t) -> Location.error -> Location.error
3-
4-
val error_of_exn : exn -> Location.error option
5-
6-
val get_id_in_path : Path.t -> Ident.t
7-
81
val lookup_type : Longident.t -> Env.t -> Path.t
92

103
val lookup_value : Longident.t -> Env.t -> Path.t * Types.value_description
@@ -33,31 +26,6 @@ val find_class_type :
3326
val type_structure :
3427
Env.t -> Parsetree.structure -> Location.t -> Typedtree.structure * Env.t
3528

36-
val sig_value : Ident.t -> Types.value_description -> Types.signature_item
37-
38-
val sig_type : Ident.t -> Types.type_declaration -> Types.signature_item
39-
40-
val sig_typext : Ident.t -> Types.extension_constructor -> Types.signature_item
41-
42-
val sig_module : Ident.t -> Types.module_declaration -> Types.signature_item
43-
44-
val mty_path : Types.module_type -> Path.t option
45-
46-
val sig_modtype : Ident.t -> Types.modtype_declaration -> Types.signature_item
47-
48-
val sig_class : Ident.t -> Types.class_declaration -> Types.signature_item
49-
50-
val sig_class_type :
51-
Ident.t -> Types.class_type_declaration -> Types.signature_item
52-
53-
val add_directive :
54-
name:string ->
55-
doc:string ->
56-
[ `Bool of bool -> unit
57-
| `Show_prim of
58-
Env.t -> Location.t -> Ident.t -> Longident.t -> Types.signature ] ->
59-
unit
60-
6129
val extension_constructor :
6230
ext_type_path:Path.t ->
6331
ext_type_params:Types.type_expr list ->
@@ -68,16 +36,6 @@ val extension_constructor :
6836
ext_attributes:Parsetree.attributes ->
6937
Types.extension_constructor
7038

71-
val is_predef_or_global : Ident.t -> bool
72-
73-
val map_sig_attributes :
74-
f:(Parsetree.attributes -> Parsetree.attributes) ->
75-
Types.signature ->
76-
Types.signature
77-
78-
val attribute :
79-
name:string Location.loc -> payload:Parsetree.payload -> Parsetree.attribute
80-
8139
val match_env :
8240
value:(Env.summary -> Ident.t -> 'a) ->
8341
empty:(unit -> 'a) ->
@@ -97,12 +55,5 @@ val match_env :
9755
Env.summary ->
9856
'a
9957

100-
val top_directive_name : Parsetree.toplevel_phrase -> string option
101-
(** Returns the name of the toplevel directive or [None] if the given phrase
102-
is not a directive *)
103-
104-
val top_directive_require : string -> Parsetree.toplevel_phrase
105-
(** [top_directive require "pkg"] builds the AST for [#require "pkg"] *)
106-
10758
val ctype_is_equal :
10859
Env.t -> bool -> Types.type_expr list -> Types.type_expr list -> bool

lib/top/mdx_top.ml

Lines changed: 104 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ module Lexbuf = struct
7878
| Parser.SEMISEMI -> lexbuf.Lexing.lex_last_action
7979
| _ -> assert false
8080

81+
let map_error_loc ~f (error : Location.error) =
82+
let f_msg (msg : Location.msg) = { msg with loc = f msg.loc } in
83+
{ error with main = f_msg error.main; sub = List.map f_msg error.sub }
84+
8185
let shift_location_error start =
8286
map_error_loc ~f:(shift_toplevel_location ~start)
8387

@@ -105,6 +109,12 @@ module Phrase = struct
105109

106110
let start t = t.startpos
107111

112+
let error_of_exn exn =
113+
match Location.error_of_exn exn with
114+
| None -> None
115+
| Some `Already_displayed -> None
116+
| Some (`Ok error) -> Some error
117+
108118
let parse lines =
109119
let contents = String.concat "\n" lines in
110120
let lexbuf = Lexing.from_string contents in
@@ -143,14 +153,21 @@ module Phrase = struct
143153
let lines = if ends_by_semi_semi lines then lines else lines @ [ ";;" ] in
144154
match parse lines with exception End_of_file -> None | t -> Some t
145155

156+
(** Returns the name of the toplevel directive or [None] if the given phrase
157+
is not a directive *)
158+
let top_directive_name (toplevel_phrase : Parsetree.toplevel_phrase) =
159+
match toplevel_phrase with
160+
| Ptop_def _ -> None
161+
| Ptop_dir { pdir_name = { txt; _ }; _ } -> Some txt
162+
146163
let is_findlib_directive =
147164
let findlib_directive = function
148165
| "require" | "use" | "camlp4o" | "camlp4r" | "thread" -> true
149166
| _ -> false
150167
in
151168
function
152169
| { parsed = Ok toplevel_phrase; _ } -> (
153-
match Compat_top.top_directive_name toplevel_phrase with
170+
match top_directive_name toplevel_phrase with
154171
| Some dir -> findlib_directive dir
155172
| None -> false)
156173
| _ -> false
@@ -212,6 +229,11 @@ module Rewrite = struct
212229
| _ -> path)
213230
| _ -> path
214231

232+
let rec get_id_in_path = function
233+
| Path.Pident id -> id
234+
| Path.Pdot (p, _) -> get_id_in_path p
235+
| Path.Papply (_, p) -> get_id_in_path p
236+
215237
let is_persistent_value env longident =
216238
let is_persistent_path p = Ident.persistent (get_id_in_path p) in
217239
try is_persistent_path (fst (Compat_top.lookup_value longident env))
@@ -271,9 +293,19 @@ module Rewrite = struct
271293
Ptop_def pstr)
272294
| _ -> phrase
273295

296+
(** [top_directive require "pkg"] builds the AST for [#require "pkg"] *)
297+
let top_directive_require pkg =
298+
Parsetree.Ptop_dir
299+
{
300+
pdir_name = { txt = "require"; loc = Location.none };
301+
pdir_arg =
302+
Some { pdira_desc = Pdir_string pkg; pdira_loc = Location.none };
303+
pdir_loc = Location.none;
304+
}
305+
274306
let preload verbose ppf =
275307
let require pkg =
276-
let p = Compat_top.top_directive_require pkg in
308+
let p = top_directive_require pkg in
277309
let _ = Toploop.execute_phrase verbose ppf p in
278310
()
279311
in
@@ -428,12 +460,55 @@ let eval t cmd =
428460
|> List.concat
429461
|> fun x -> if !errors then Result.Error x else Result.Ok x))
430462

463+
let add_directive ~name ~doc kind =
464+
let directive =
465+
match kind with
466+
| `Bool f -> Toploop.Directive_bool f
467+
| `Show_prim to_sig ->
468+
let show_prim to_sig lid =
469+
let env = !Toploop.toplevel_env in
470+
let loc = Location.none in
471+
try
472+
let s =
473+
match lid with
474+
| Longident.Lident s -> s
475+
| Longident.Ldot (_, s) -> s
476+
| Longident.Lapply _ ->
477+
Format.printf "Invalid path %a@." Printtyp.longident lid;
478+
raise Exit
479+
in
480+
let id = Ident.create_persistent s in
481+
let sg = to_sig env loc id lid in
482+
Printtyp.wrap_printing_env ~error:false env (fun () ->
483+
Format.printf "@[%a@]@." Printtyp.signature sg)
484+
with
485+
| Not_found -> Format.printf "@[Unknown element.@]@."
486+
| Exit -> ()
487+
in
488+
Toploop.Directive_ident (show_prim to_sig)
489+
in
490+
Toploop.add_directive name directive { section = "Environment queries"; doc }
491+
431492
let all_show_funs = ref []
432493

433494
let reg_show_prim name to_sig doc =
434495
all_show_funs := to_sig :: !all_show_funs;
435496
add_directive ~name ~doc (`Show_prim to_sig)
436497

498+
let sig_value id desc = Types.Sig_value (id, desc, Exported)
499+
500+
let sig_type id desc = Types.Sig_type (id, desc, Trec_not, Exported)
501+
502+
let sig_typext id ext = Types.Sig_typext (id, ext, Text_exception, Exported)
503+
504+
let sig_module id md = Types.Sig_module (id, Mp_present, md, Trec_not, Exported)
505+
506+
let sig_modtype id desc = Types.Sig_modtype (id, desc, Exported)
507+
508+
let sig_class id desc = Types.Sig_class (id, desc, Trec_not, Exported)
509+
510+
let sig_class_type id desc = Types.Sig_class_type (id, desc, Trec_not, Exported)
511+
437512
let show_val () =
438513
reg_show_prim "show_val"
439514
(fun env loc id lid ->
@@ -469,6 +544,31 @@ let show_exception () =
469544
[ sig_typext id ext ])
470545
"Print the signature of the corresponding exception."
471546

547+
let mty_path =
548+
let open Types in
549+
function
550+
| Mty_alias path -> Some path
551+
| Mty_ident _ | Mty_signature _ | Mty_functor _ -> None
552+
553+
let map_sig_attributes ~f =
554+
let open Types in
555+
List.map (function
556+
| Sig_module (id, mp, md, rs, visibility) ->
557+
Sig_module
558+
( id,
559+
mp,
560+
{ md with md_attributes = f md.md_attributes },
561+
rs,
562+
visibility )
563+
| item -> item)
564+
565+
let attribute ~name ~payload =
566+
{
567+
Parsetree.attr_name = name;
568+
attr_payload = payload;
569+
attr_loc = Location.none;
570+
}
571+
472572
let show_module () =
473573
let open Types in
474574
let trim_signature = function
@@ -632,6 +732,8 @@ let init ~verbose:v ~silent:s ~verbose_findlib ~directives ~packages ~predicates
632732

633733
let envs = Hashtbl.create 8
634734

735+
let is_predef_or_global id = Ident.is_predef id || Ident.global id
736+
635737
let rec save_summary acc s =
636738
let default_case summary = save_summary acc summary in
637739
let add summary id =

0 commit comments

Comments
 (0)