Source file error_format.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
let rec skip_lines in_chan = function
| n when n <= 0 -> ()
| n -> ignore (input_line in_chan); skip_lines in_chan (n - 1)
let rec read_lines in_chan = function
| n when n <= 0 -> []
| n ->
let l = input_line in_chan in
let ls = read_lines in_chan (n - 1) in
l :: ls
type formatter = {
indent : string;
endline : string -> unit;
loc_color : string -> string
}
let err_formatter = {
indent = "";
endline = prerr_endline;
loc_color = Util.red
}
let buffer_formatter b = {
indent = "";
endline = (fun str -> Buffer.add_string b (str ^ "\n"));
loc_color = Util.red
}
let format_endline str ppf = ppf.endline (ppf.indent ^ (Str.global_replace (Str.regexp_string "\n") ("\n" ^ ppf.indent) str))
let underline_single color cnum_from cnum_to =
if (cnum_from + 1) >= cnum_to then
Util.(String.make cnum_from ' ' ^ clear (color "^"))
else
Util.(String.make cnum_from ' ' ^ clear (color ("^" ^ String.make (cnum_to - cnum_from - 2) '-' ^ "^")))
let format_hint color = function
| Some hint -> " " ^ Util.(hint |> color |> clear)
| None -> ""
let format_code_single' prefix hint fname in_chan lnum cnum_from cnum_to contents ppf =
skip_lines in_chan (lnum - 1);
let line = input_line in_chan in
let line_prefix = string_of_int lnum ^ Util.(clear (cyan " |")) in
let blank_prefix = String.make (String.length (string_of_int lnum)) ' ' ^ Util.(clear (ppf.loc_color " |")) in
format_endline (Printf.sprintf "%s%s:%d.%d-%d:" prefix Util.(fname |> cyan |> clear) lnum cnum_from cnum_to) ppf;
format_endline (line_prefix ^ line) ppf;
format_endline (blank_prefix ^ underline_single ppf.loc_color cnum_from cnum_to ^ format_hint ppf.loc_color hint) ppf;
contents { ppf with indent = blank_prefix ^ " " }
let underline_double_from color cnum_from eol =
Util.(String.make cnum_from ' ' ^ clear (color ("^" ^ String.make (eol - cnum_from - 1) '-')))
let underline_double_to color cnum_to =
if cnum_to = 0 then
Util.(clear (color "^"))
else
Util.(clear (color (String.make (cnum_to - 1) '-' ^ "^")))
let format_code_double' prefix fname in_chan lnum_from cnum_from lnum_to cnum_to contents ppf =
skip_lines in_chan (lnum_from - 1);
let line_from = input_line in_chan in
skip_lines in_chan (lnum_to - lnum_from - 1);
let line_to = input_line in_chan in
let line_to_prefix = string_of_int lnum_to ^ Util.(clear (cyan " |")) in
let line_from_padding = String.make (String.length (string_of_int lnum_to) - String.length (string_of_int lnum_from)) ' ' in
let line_from_prefix = string_of_int lnum_from ^ line_from_padding ^ Util.(clear (cyan " |")) in
let blank_prefix = String.make (String.length (string_of_int lnum_to)) ' ' ^ Util.(clear (ppf.loc_color " |")) in
format_endline (Printf.sprintf "%s%s:%d.%d-%d.%d:" prefix Util.(fname |> cyan |> clear) lnum_from cnum_from lnum_to cnum_to) ppf;
format_endline (line_from_prefix ^ line_from) ppf;
format_endline (blank_prefix ^ underline_double_from ppf.loc_color cnum_from (String.length line_from)) ppf;
format_endline (line_to_prefix ^ line_to) ppf;
format_endline (blank_prefix ^ underline_double_to ppf.loc_color cnum_to) ppf;
contents { ppf with indent = blank_prefix ^ " " }
let format_code_single_fallback prefix fname lnum cnum_from cnum_to contents ppf =
let blank_prefix = String.make (String.length (string_of_int lnum)) ' ' ^ Util.(clear (ppf.loc_color " |")) in
format_endline (Printf.sprintf "%s%s:%d.%d-%d:" prefix Util.(fname |> cyan |> clear) lnum cnum_from cnum_to) ppf;
contents { ppf with indent = blank_prefix ^ " " }
let format_code_single prefix hint fname lnum cnum_from cnum_to contents ppf =
try
let in_chan = open_in fname in
begin
try format_code_single' prefix hint fname in_chan lnum cnum_from cnum_to contents ppf; close_in in_chan
with
| _ ->
format_code_single_fallback prefix fname lnum cnum_from cnum_to contents ppf;
close_in_noerr in_chan
end
with
| _ -> format_code_single_fallback prefix fname lnum cnum_from cnum_to contents ppf
let format_code_double_fallback prefix fname lnum_from cnum_from lnum_to cnum_to contents ppf =
let blank_prefix = String.make (String.length (string_of_int lnum_to)) ' ' ^ Util.(clear (ppf.loc_color " |")) in
format_endline (Printf.sprintf "%s%s:%d.%d-%d.%d:" prefix Util.(fname |> cyan |> clear) lnum_from cnum_from lnum_to cnum_to) ppf;
contents { ppf with indent = blank_prefix ^ " " }
let format_code_double prefix fname lnum_from cnum_from lnum_to cnum_to contents ppf =
try
let in_chan = open_in fname in
begin
try format_code_double' prefix fname in_chan lnum_from cnum_from lnum_to cnum_to contents ppf; close_in in_chan
with
| _ ->
format_code_double_fallback prefix fname lnum_from cnum_from lnum_to cnum_to contents ppf;
close_in_noerr in_chan
end
with
| _ -> format_code_double_fallback prefix fname lnum_from cnum_from lnum_to cnum_to contents ppf
let format_pos prefix hint p1 p2 contents ppf =
let open Lexing in
if p1.pos_lnum == p2.pos_lnum
then format_code_single prefix hint p1.pos_fname p1.pos_lnum (p1.pos_cnum - p1.pos_bol) (p2.pos_cnum - p2.pos_bol) contents ppf
else format_code_double prefix p1.pos_fname p1.pos_lnum (p1.pos_cnum - p1.pos_bol) p2.pos_lnum (p2.pos_cnum - p2.pos_bol) contents ppf
let rec format_loc prefix hint l contents =
match l with
| Parse_ast.Unknown -> contents
| Parse_ast.Range (p1, p2) -> format_pos prefix hint p1 p2 contents
| Parse_ast.Unique (_, l) -> format_loc prefix hint l contents
| Parse_ast.Documented (_, l) -> format_loc prefix hint l contents
| Parse_ast.Hint (hint', l1, l2) ->
fun ppf -> format_loc prefix (Some hint') l1 (fun _ -> ()) { ppf with loc_color = Util.green }; format_loc prefix hint l2 contents ppf
| Parse_ast.Generated l ->
fun ppf ->
format_endline "Code generated nearby:" ppf;
format_loc prefix hint l contents ppf
type message =
| Location of string * string option * Parse_ast.l * message
| Line of string
| List of (string * message) list
| Seq of message list
| With of (formatter -> formatter) * message
let bullet = Util.(clear (blue "*"))
let rec format_message msg ppf =
match msg with
| Location (prefix, hint, l, msg) ->
format_loc prefix hint l (format_message msg) ppf
| Line str ->
format_endline str ppf
| Seq messages ->
List.iter (fun msg -> format_message msg ppf) messages
| List list ->
let format_list_item ppf (, msg) =
format_endline (Util.(clear (blue "*")) ^ " " ^ header) ppf;
format_message msg { ppf with indent = ppf.indent ^ " " }
in
List.iter (format_list_item ppf) list
| With (f, msg) -> format_message msg (f ppf)