1 (**************************************************************************)
2 (* *)
3 (* OCaml *)
4 (* *)
5 (* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
6 (* *)
7 (* Copyright 1996 Institut National de Recherche en Informatique et *)
8 (* en Automatique. *)
9 (* *)
10 (* All rights reserved. This file is distributed under the terms of *)
11 (* the GNU Lesser General Public License version 2.1, with the *)
12 (* special exception on linking described in the file LICENSE. *)
13 (* *)
14 (**************************************************************************)
15
16 open Format
17
18 (* Accessors for the table of toplevel value bindings. These functions
19 must appear as first and second exported functions in this module.
20 (See module Translmod.) *)
21 val getvalue : string -> Obj.t
22 val setvalue : string -> Obj.t -> unit
23
24 (* Set the load paths, before running anything *)
25
26 val set_paths : unit -> unit
27
28 (* The interactive toplevel loop *)
29
30 val loop : formatter -> unit
31
32 (* Read and execute a script from the given file *)
33
34 val run_script : formatter -> string -> string array -> bool
35 (* true if successful, false if error *)
36
37 (* Interface with toplevel directives *)
38
39 type directive_fun =
40 | Directive_none of (unit -> unit)
41 | Directive_string of (string -> unit)
42 | Directive_int of (int -> unit)
43 | Directive_ident of (Longident.t -> unit)
44 | Directive_bool of (bool -> unit)
45
46 type directive_info = {
47 section: string;
48 doc: string;
49 }
50
51 val add_directive : string -> directive_fun -> directive_info -> unit
52 (* Add toplevel directive and its documentation.
53
54 @since 4.03 *)
55
56 val directive_table : (string, directive_fun) Hashtbl.t
57 (* Deprecated: please use [add_directive] instead of inserting
58 in this table directly. *)
59
60 val directive_info_table : (string, directive_info) Hashtbl.t
61
62 val toplevel_env : Env.t ref
63 (* Typing environment for the toplevel *)
64 val initialize_toplevel_env : unit -> unit
65 (* Initialize the typing environment for the toplevel *)
66 val print_exception_outcome : formatter -> exn -> unit
67 (* Print an exception resulting from the evaluation of user code. *)
68 val execute_phrase : bool -> formatter -> Parsetree.toplevel_phrase -> bool
69 (* Execute the given toplevel phrase. Return [true] if the
70 phrase executed with no errors and [false] otherwise.
71 First bool says whether the values and types of the results
72 should be printed. Uncaught exceptions are always printed. *)
73 val preprocess_phrase :
74 formatter -> Parsetree.toplevel_phrase -> Parsetree.toplevel_phrase
75 (* Preprocess the given toplevel phrase using regular and ppx
76 preprocessors. Return the updated phrase. *)
77 val use_file : formatter -> string -> bool
78 val use_silently : formatter -> string -> bool
79 val mod_use_file : formatter -> string -> bool
80 (* Read and execute commands from a file.
81 [use_file] prints the types and values of the results.
82 [use_silently] does not print them.
83 [mod_use_file] wrap the file contents into a module. *)
84 val eval_module_path: Env.t -> Path.t -> Obj.t
85 val eval_value_path: Env.t -> Path.t -> Obj.t
86 val eval_extension_path: Env.t -> Path.t -> Obj.t
87 val eval_class_path: Env.t -> Path.t -> Obj.t
88 (* Return the toplevel object referred to by the given path *)
89 val record_backtrace : unit -> unit
90
91 (* Printing of values *)
92
93 val print_value: Env.t -> Obj.t -> formatter -> Types.type_expr -> unit
94 val print_untyped_exception: formatter -> Obj.t -> unit
95
96 type ('a, 'b) gen_printer =
97 | Zero of 'b
98 | Succ of ('a -> ('a, 'b) gen_printer)
99
100 val install_printer :
101 Path.t -> Types.type_expr -> (formatter -> Obj.t -> unit) -> unit
102 val install_generic_printer :
103 Path.t -> Path.t ->
104 (int -> (int -> Obj.t -> Outcometree.out_value,
105 Obj.t -> Outcometree.out_value) gen_printer) -> unit
106 val install_generic_printer' :
107 Path.t -> Path.t -> (formatter -> Obj.t -> unit,
108 formatter -> Obj.t -> unit) gen_printer -> unit
109 val remove_printer : Path.t -> unit
110
111 val max_printer_depth: int ref
112 val max_printer_steps: int ref
113
114 (* Hooks for external parsers and printers *)
115
116 val parse_toplevel_phrase : (Lexing.lexbuf -> Parsetree.toplevel_phrase) ref
117 val parse_use_file : (Lexing.lexbuf -> Parsetree.toplevel_phrase list) ref
118 val print_location : formatter -> Location.t -> unit
119 val print_error : formatter -> Location.error -> unit
120 val print_warning : Location.t -> formatter -> Warnings.t -> unit
121 val input_name : string ref
122
123 val print_out_value :
124 (formatter -> Outcometree.out_value -> unit) ref
125 val print_out_type :
126 (formatter -> Outcometree.out_type -> unit) ref
127 val print_out_class_type :
128 (formatter -> Outcometree.out_class_type -> unit) ref
129 val print_out_module_type :
130 (formatter -> Outcometree.out_module_type -> unit) ref
131 val print_out_type_extension :
132 (formatter -> Outcometree.out_type_extension -> unit) ref
133 val print_out_sig_item :
134 (formatter -> Outcometree.out_sig_item -> unit) ref
135 val print_out_signature :
136 (formatter -> Outcometree.out_sig_item list -> unit) ref
137 val print_out_phrase :
138 (formatter -> Outcometree.out_phrase -> unit) ref
139
140 (* Hooks for external line editor *)
141
142 val read_interactive_input : (string -> bytes -> int -> int * bool) ref
143
144 (* Hooks *)
145
146 val toplevel_startup_hook : (unit -> unit) ref
147
148 type event = ..
149 type event +=
150 | Startup
151 | After_setup
152 (* Just after the setup, when the toplevel is ready to evaluate user
153 input. This happens before the toplevel has evaluated any kind of
154 user input, in particular this happens before loading the
155 [.ocamlinit] file. *)
156
157 val add_hook : (event -> unit) -> unit
158 (* Add a function that will be called at key points of the toplevel
159 initialization process. *)
160
161 val run_hooks : event -> unit
162 (* Run all the registered hooks. *)
163
164 (* Used by Trace module *)
165
166 val may_trace : bool ref
167
168 (* Misc *)
169
170 val override_sys_argv : string array -> unit
171 (* [override_sys_argv args] replaces the contents of [Sys.argv] by [args]
172 and reset [Arg.current] to [0].
173
174 This is called by [run_script] so that [Sys.argv] represents
175 "script.ml args..." instead of the full command line:
176 "ocamlrun unix.cma ... script.ml args...". *)
177