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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
(** Error formatting and helper functions *)
(**{1 Terminal formatting}*)
let color_formatter ppf =
Ocolor_format.prettify_formatter ppf;
ppf
let unstyle_formatter ppf =
Format.pp_set_mark_tags ppf false;
ppf
let () = ignore (unstyle_formatter Format.str_formatter)
let terminal_columns, set_terminal_width_function =
let get_cols = ref (fun () -> 80) in
(fun () -> !get_cols ()), fun f -> get_cols := f
let has_color_raw ~(tty : bool Lazy.t) =
match Global.options.color with
| Global.Never -> false
| Always -> true
| Auto -> Lazy.force tty
let has_color oc =
has_color_raw ~tty:(lazy Unix.(isatty (descr_of_out_channel oc)))
let formatter_of_out_channel oc =
let tty = lazy Unix.(isatty (descr_of_out_channel oc)) in
let ppf = Format.formatter_of_out_channel oc in
let ppf =
if has_color_raw ~tty then color_formatter ppf else unstyle_formatter ppf
in
let out, flush = Format.pp_get_formatter_output_functions ppf () in
let flush () =
if Lazy.force tty then Format.pp_set_margin ppf (terminal_columns ());
flush ()
in
Format.pp_set_formatter_output_functions ppf out flush;
ppf
let std_ppf = lazy (formatter_of_out_channel stdout)
let err_ppf = lazy (formatter_of_out_channel stderr)
let ignore_ppf = lazy (Format.make_formatter (fun _ _ _ -> ()) (fun () -> ()))
let unformat (f : Format.formatter -> unit) : string =
let buf = Buffer.create 1024 in
let ppf = unstyle_formatter (Format.formatter_of_buffer buf) in
Format.pp_set_margin ppf max_int;
let out_funs = Format.pp_get_formatter_out_functions ppf () in
Format.pp_set_formatter_out_functions ppf
{
out_funs with
Format.out_newline = (fun () -> out_funs.out_string " " 0 1);
Format.out_indent = (fun _ -> ());
};
f ppf;
Format.pp_print_flush ppf ();
Buffer.contents buf
(**{2 Message types and output helpers *)
type level = Error | Warning | Debug | Log | Result
let get_ppf = function
| Result -> Lazy.force std_ppf
| Debug when not Global.options.debug -> Lazy.force ignore_ppf
| Warning when Global.options.disable_warnings -> Lazy.force ignore_ppf
| Error | Log | Debug | Warning -> Lazy.force err_ppf
(**{3 Markers}*)
let print_time_marker =
let time : float ref = ref (Unix.gettimeofday ()) in
fun ppf () ->
let new_time = Unix.gettimeofday () in
let old_time = !time in
time := new_time;
let delta = (new_time -. old_time) *. 1000. in
if delta > 50. then
Format.fprintf ppf "@{<bold;black>[TIME] %.0fms@}@\n" delta
let pp_marker target ppf =
let open Ocolor_types in
let tags, str =
match target with
| Debug -> [Bold; Fg (C4 magenta)], "DEBUG"
| Error -> [Bold; Fg (C4 red)], "ERROR"
| Warning -> [Bold; Fg (C4 yellow)], "WARNING"
| Result -> [Bold; Fg (C4 green)], "RESULT"
| Log -> [Bold; Fg (C4 black)], "LOG"
in
if target = Debug then print_time_marker ppf ();
Format.pp_open_stag ppf (Ocolor_format.Ocolor_styles_tag tags);
Format.pp_print_string ppf str;
Format.pp_close_stag ppf ()
(**{2 Printers}*)
(** {1 Message content} *)
module Content = struct
type message = Format.formatter -> unit
type position = { pos_message : message option; pos : Pos.t }
type message_element =
| MainMessage of message
| Position of position
| Suggestion of string list
| Outcome of message
type t = message_element list
let of_message (message : message) : t = [MainMessage message]
let of_result (message : message) : t = [Outcome message]
let prepend_message (content : t) prefix : t = MainMessage prefix :: content
let to_internal_error (content : t) : t =
let internal_error_prefix ppf =
Format.pp_print_string ppf
"Internal Error, please report to \
https://github.com/CatalaLang/catala/issues."
in
prepend_message content internal_error_prefix
let add_suggestion (content : t) (suggestion : string list) =
content @ [Suggestion suggestion]
let add_position (content : t) ?(message : message option) (position : Pos.t)
=
content @ [Position { pos = position; pos_message = message }]
let of_string (s : string) : t =
[MainMessage (fun ppf -> Format.pp_print_text ppf s)]
let basic_msg ppf target content =
Format.pp_open_vbox ppf 0;
Format.pp_print_list
~pp_sep:(fun ppf () -> Format.fprintf ppf "@,@,")
(fun ppf -> function
| Position pos ->
Option.iter
(fun msg -> Format.fprintf ppf "@[<hov>%t@]@," msg)
pos.pos_message;
Pos.format_loc_text ppf pos.pos
| MainMessage msg ->
Format.fprintf ppf "@[<hov 2>[%t] %t@]" (pp_marker target) msg
| Outcome msg ->
Format.fprintf ppf "@[<hov>[%t]@ %t@]" (pp_marker target) msg
| Suggestion suggestions_list -> Suggestions.format ppf suggestions_list)
ppf content;
Format.pp_close_box ppf ();
Format.pp_print_newline ppf ()
let fancy_msg ppf target content =
let ppf_out_fcts = Format.pp_get_formatter_out_functions ppf () in
let restore_ppf () =
Format.pp_print_flush ppf ();
Format.pp_set_formatter_out_functions ppf ppf_out_fcts
in
let getcolorstr pp =
let buf = Buffer.create 17 in
let ppfb = Format.formatter_of_buffer buf in
Format.pp_set_formatter_stag_functions ppfb
(Format.pp_get_formatter_stag_functions ppf ());
Format.pp_set_mark_tags ppfb (Format.pp_get_mark_tags ppf ());
pp ppfb;
Format.pp_print_flush ppfb ();
Buffer.contents buf
in
Format.pp_set_formatter_out_functions ppf
{
ppf_out_fcts with
out_indent =
(fun n ->
let lead =
getcolorstr (fun ppf -> Format.fprintf ppf "@{<blue>@<1>%s@}" "│")
in
if n >= 1 then ppf_out_fcts.out_string lead 0 (String.length lead);
if n >= 2 then ppf_out_fcts.out_indent (n - 1));
};
Format.pp_open_vbox ppf 1;
Format.fprintf ppf "@{<blue>@<2>%s[%t]@<2>%s@}" "┌─" (pp_marker target) "─";
let print_elt ppf ?(islast = false) = function
| MainMessage msg ->
Format.fprintf ppf "@,@[<v 2>@,@[<hov>%t@]@]" msg;
if islast then Format.pp_print_cut ppf ();
true
| Position pos -> (
Format.pp_print_cut ppf ();
Option.iter
(fun msg -> Format.fprintf ppf "@[<v 1>@,@[<hov 2>%t@]@]" msg)
pos.pos_message;
Format.pp_print_break ppf 0 (-1);
let pr_head, pr_context, pr_legal = Pos.format_loc_text_parts pos.pos in
Format.pp_open_vbox ppf 2;
Format.fprintf ppf "@{<blue>@<1>%s@}%t" "├" pr_head;
pr_context ppf;
Format.pp_close_box ppf ();
match pr_legal with
| None -> true
| Some pr_legal ->
Format.pp_print_break ppf 0 (-1);
if islast then (
restore_ppf ();
Format.pp_open_vbox ppf 3;
Format.fprintf ppf "@{<blue>@<3>%s@}%t" "└─ " pr_legal)
else (
Format.pp_open_vbox ppf 3;
Format.fprintf ppf "@{<blue>@<3>%s@}%t" "├─ " pr_legal);
Format.pp_close_box ppf ();
not islast)
| Outcome msg ->
Format.fprintf ppf "@;<0 1>@[<v 1>@[<hov 2>%t@]@]" msg;
true
| Suggestion suggestions_list ->
Format.fprintf ppf "@,@[<v 1>@,@[<hov 2>%a@]@]" Suggestions.format
suggestions_list;
true
in
let rec print_lines ppf = function
| [elt] ->
let finalise = print_elt ppf ~islast:true elt in
Format.pp_close_box ppf ();
if finalise then Format.fprintf ppf "@,@{<blue>@<2>%s@}" "└─"
| elt :: r ->
let _ = print_elt ppf elt in
print_lines ppf r
| [] ->
Format.pp_close_box ppf ();
Format.pp_print_cut ppf ()
in
print_lines ppf content;
Format.pp_close_box ppf ();
restore_ppf ();
Format.pp_print_newline ppf ()
let emit (content : t) (target : level) : unit =
match Global.options.message_format with
| Global.Human -> (
let ppf = get_ppf target in
match target with
| Debug | Log -> basic_msg ppf target content
| Result | Warning | Error -> fancy_msg ppf target content)
| Global.GNU ->
let ppf = get_ppf target in
Format.pp_print_list ~pp_sep:Format.pp_print_newline
(fun ppf elt ->
let pos, message =
match elt with
| MainMessage m ->
let pos =
List.find_map
(function
| Position { pos_message = None; pos } -> Some pos
| _ -> None)
content
|> function
| None ->
List.find_map
(function
| Position { pos_message = _; pos } -> Some pos
| _ -> None)
content
| some -> some
in
pos, Some m
| Position { pos_message; pos } -> Some pos, pos_message
| Outcome m -> None, Some m
| Suggestion sl -> None, Some (fun ppf -> Suggestions.format ppf sl)
in
Option.iter
(fun pos ->
Format.fprintf ppf "@{<blue>%s@}: " (Pos.to_string_short pos))
pos;
Format.fprintf ppf "[%t]" (pp_marker target);
match message with
| Some message ->
Format.pp_print_char ppf ' ';
Format.pp_print_string ppf (unformat message)
| None -> ())
ppf content;
Format.pp_print_newline ppf ()
end
open Content
(** {1 Error exception} *)
exception CompilerError of Content.t
(** {1 Error printing} *)
type ('a, 'b) emitter =
?header:Content.message ->
?internal:bool ->
?pos:Pos.t ->
?pos_msg:Content.message ->
?extra_pos:(string * Pos.t) list ->
?fmt_pos:(Content.message * Pos.t) list ->
?outcome:Content.message list ->
?suggestion:string list ->
('a, Format.formatter, unit, 'b) format4 ->
'a
let make
?
?(internal = false)
?pos
?pos_msg
?
?fmt_pos
?(outcome = [])
?(suggestion = [])
~cont
~level =
match level with
| Debug when not Global.options.debug ->
Format.ikfprintf (fun _ -> cont [] level) (Lazy.force ignore_ppf)
| Warning when Global.options.disable_warnings ->
Format.ikfprintf (fun _ -> cont [] level) (Lazy.force ignore_ppf)
| _ ->
Format.kdprintf
@@ fun message ->
let t =
match level with Result -> of_result message | _ -> of_message message
in
let t = match header with Some h -> prepend_message t h | None -> t in
let t = if internal then to_internal_error t else t in
let t =
match outcome with [] -> t | o -> t @ List.map (fun o -> Outcome o) o
in
let t =
match pos with Some p -> add_position t ?message:pos_msg p | None -> t
in
let t =
match extra_pos with
| Some pl ->
List.fold_left
(fun t (message, p) ->
let message =
if message = "" then None
else Some (fun ppf -> Format.pp_print_text ppf message)
in
add_position t ?message p)
t pl
| None -> t
in
let t =
match fmt_pos with
| Some pl ->
List.fold_left
(fun t (message, p) ->
let message = if message == ignore then None else Some message in
add_position t ?message p)
t pl
| None -> t
in
let t = match suggestion with [] -> t | s -> add_suggestion t s in
cont t level
let debug = make ~level:Debug ~cont:emit
let log = make ~level:Log ~cont:emit
let result = make ~level:Result ~cont:emit
let results r = emit (List.flatten (List.map of_result r)) Result
let warning = make ~level:Warning ~cont:emit
let error = make ~level:Error ~cont:(fun m _ -> raise (CompilerError m))