Source file b0_scope.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
open B0_std
let exit_b0_file_error = 121
type name = string
type qualified_name = string
let pp_name = Fmt.code
let sep = "."
let lib_root = sep
let is_name_valid name =
name <> "" && match String.index_opt name sep.[0] with
| None -> true | Some _ -> false
exception Error of string
let raise_error fmt = Fmt.kstr (fun s -> raise (Error s)) fmt
let raise_no_scope_error ~kind ~name =
raise_error "No open file or library scope to define %s %a" kind pp_name name
type file =
{ qname : qualified_name;
file : Fpath.t;
dir : Fpath.t }
type lib =
{ module' : string;
qname : qualified_name; }
type t =
| Lib of lib
| File of file * file list
let equal = Stdlib.( = )
let file = function File (file, _) -> Some file.file | Lib _ -> None
let dir = function File (file, _) -> Some file.dir | Lib _ -> None
let is_root = function File ({ qname = ""}, _) -> true | _ -> false
let path = function
| Lib lib -> String.split_on_char '.' lib.qname
| File ({ qname }, _) ->
if qname = "" then [] else String.split_on_char '.' qname
let raise_invalid_name_error ~kind ~name = match name with
| "" -> raise_error "The empty string is not a valid %s name" kind
| name ->
raise_error
"%a is not a valid %s name, dots are not allowed" pp_name name kind
let raise_duplicate_error ~kind ~name =
raise_error "%s %a already defined in scope."
(String.Ascii.capitalize kind) pp_name name
let qualify_name scope ~kind name = match scope with
| File ({ qname; _ }, _) ->
if not (is_name_valid name) then raise_invalid_name_error ~kind ~name else
if qname <> "" then String.concat sep [qname; name] else name
| Lib lib ->
if name = "" then lib.qname else
if not (is_name_valid name)
then raise_invalid_name_error ~kind ~name
else String.concat sep [lib.qname; name]
let make_unique_qualified_name scope ~defs ~kind name =
let qname = qualify_name scope ~kind name in
if String.Map.mem qname defs then raise_duplicate_error ~kind ~name else
qname
let current = ref None
let sealed = ref false
let list = ref []
let check_no_scope () = match !current with
| None -> ()
| Some Lib lib -> Fmt.invalid_arg "Unclosed library scope %s" lib.qname
| Some File (file, _) ->
Fmt.invalid_arg "Unclosed file scope %s %a" file.qname Fpath.pp file.file
exception After_seal of string
let seal () =
check_no_scope ();
sealed := true
let sealed () = !sealed
let raise_after_seal fmt = Fmt.kstr (fun s -> raise (After_seal s)) fmt
let raise_create_after_seal ~kind ~name =
raise_after_seal
"%s %a illegaly created after b0 file initialization."
(String.Ascii.capitalize kind) pp_name name
let current_scope_prefix () = match !current with
| None -> ""
| Some Lib lib -> lib.qname
| Some File (file, _) -> file.qname
let qualify_name_in_current ~kind name = match !current with
| None -> name
| Some scope -> qualify_name scope ~kind name
let close () = match !current with
| None -> invalid_arg "No scope to close"
| Some Lib _ -> current := None
| Some File (file, parents) ->
list := (file.qname, file.file) :: !list;
let parent = match parents with
| [] -> None | file :: parents -> Some (File (file, parents))
in
current := parent
let open_lib ~module' lib =
if lib = "" then Fmt.invalid_arg "Library scope name cannot be empty" else
let qname = String.concat "" [lib_root; lib] in
check_no_scope ();
current := Some (Lib { module'; qname })
let root_scope file = File ({ qname = ""; file; dir = Fpath.parent file}, [])
let current_is_root () = match !current with
| Some scope -> is_root scope | _ -> false
let name_list () =
if sealed () then List.rev !list else
invalid_arg "B0_scope.seal () has not been called yet."
let pp_uncaught_exn ppf (exn, bt) =
let current_location use_bt =
let location_in_backtrace file bt = match Printexc.backtrace_slots bt with
| None -> None
| Some slots ->
let rec loop file found slots i max = match i > max with
| true -> found
| false ->
match Printexc.Slot.location slots.(i) with
| None -> loop file found slots (i + 1) max
| Some loc ->
match String.equal loc.Printexc.filename file with
| false -> loop file found slots (i + 1) max
| true -> loop file (Some loc) slots (i + 1) max
in
loop (Fpath.to_string file) None slots 0 (Array.length slots - 1)
in
match !current with
| None -> invalid_arg "no current scope"
| Some Lib lib -> Fmt.str "Library %a:" pp_name lib.qname
| Some File (file, _) ->
let loc = match use_bt with
| None -> "line 1"
| Some bt ->
match location_in_backtrace file.file bt with
| None -> "line 1"
| Some loc ->
Fmt.str "line %d, characters %d-%d"
loc.Printexc.line_number
loc.Printexc.start_char
loc.Printexc.end_char
in
Fmt.str "File %S, %s:" (Fpath.to_string file.file) loc
in
let pp_error_label ppf () = Fmt.st [`Fg `Red; `Bold] ppf "Error" in
let pp_error ppf (err, bt) =
Fmt.pf ppf "@[<v>%s@,%a: %s@]"
(current_location (Some bt)) pp_error_label () err
in
let pp_uncaught ppf (exn, bt) =
Fmt.pf ppf
"@[<v>%s@,%a: b0 file raised an uncaught exception.@, @[<v>%a@]@]"
(current_location None) pp_error_label () Fmt.exn_backtrace (exn, bt)
in
match exn with
| Error err -> pp_error ppf (err, bt)
| exn -> pp_uncaught ppf (exn, bt)
let open_root file =
let catch_exn exn bt =
Fmt.epr "@[%a@]@." pp_uncaught_exn (exn, bt);
Stdlib.exit exit_b0_file_error
in
current := Some (root_scope file);
Printexc.record_backtrace true;
Printexc.set_uncaught_exception_handler catch_exn
let open_file name file = match !current with
| Some File ({ qname; _ } as parent, parents) ->
let qname = if qname <> "" then String.concat sep [qname; name] else name in
let new' = { qname; file; dir = Fpath.parent file } in
current := Some (File (new', parent :: parents))
| _ ->
Fmt.invalid_arg
"No root scope found to open scope %s for file %a" name Fpath.pp file
let current () = !current
let is_current scope = match current () with
| None -> false | Some current -> equal scope current
let current_make_unique_qualified_name ~defs ~kind name = match current () with
| Some scope ->
let qname = make_unique_qualified_name scope ~defs ~kind name in
qname, scope
| None when sealed () ->
raise_create_after_seal ~kind ~name
| None ->
raise_no_scope_error ~kind ~name