Skip to content

Commit c508c47

Browse files
committed
colorize error and warning messages, refactor Location
(Simon Cruanes) git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@16349 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
1 parent cb3bb15 commit c508c47

File tree

3 files changed

+77
-36
lines changed

3 files changed

+77
-36
lines changed

parsing/location.ml

+53-19
Original file line numberDiff line numberDiff line change
@@ -250,37 +250,53 @@ let get_pos_info pos =
250250
(pos.pos_fname, pos.pos_lnum, pos.pos_cnum - pos.pos_bol)
251251
;;
252252

253+
let setup_colors () =
254+
Misc.Color.setup !Clflags.color
255+
253256
let print_loc ppf loc =
257+
setup_colors ();
254258
let (file, line, startchar) = get_pos_info loc.loc_start in
255259
let endchar = loc.loc_end.pos_cnum - loc.loc_start.pos_cnum + startchar in
256260
if file = "//toplevel//" then begin
257261
if highlight_locations ppf [loc] then () else
258262
fprintf ppf "Characters %i-%i"
259263
loc.loc_start.pos_cnum loc.loc_end.pos_cnum
260264
end else begin
261-
fprintf ppf "%s%a%s%i" msg_file print_filename file msg_line line;
265+
fprintf ppf "%s@{<loc>%a%s%i" msg_file print_filename file msg_line line;
262266
if startchar >= 0 then
263-
fprintf ppf "%s%i%s%i" msg_chars startchar msg_to endchar
267+
fprintf ppf "%s%i%s%i" msg_chars startchar msg_to endchar;
268+
fprintf ppf "@}"
264269
end
265270
;;
266271

267272
let print ppf loc =
273+
setup_colors ();
268274
if loc.loc_start.pos_fname = "//toplevel//"
269275
&& highlight_locations ppf [loc] then ()
270-
else fprintf ppf "%a%s@." print_loc loc msg_colon
276+
else fprintf ppf "@{<loc>%a@}%s@." print_loc loc msg_colon
277+
;;
278+
279+
let error_prefix = "Error"
280+
let warning_prefix = "Warning"
281+
282+
let print_error_prefix ppf () =
283+
setup_colors ();
284+
fprintf ppf "@{<error>%s@}:" error_prefix;
285+
()
271286
;;
272287

273288
let print_error ppf loc =
274289
print ppf loc;
275-
fprintf ppf "Error: ";
290+
print_error_prefix ppf ()
276291
;;
277292

278-
let print_error_cur_file ppf = print_error ppf (in_file !input_name);;
293+
let print_error_cur_file ppf () = print_error ppf (in_file !input_name);;
279294

280295
let default_warning_printer loc ppf w =
281296
if Warnings.is_active w then begin
297+
setup_colors ();
282298
print ppf loc;
283-
fprintf ppf "Warning %a@." Warnings.print w
299+
fprintf ppf "@{<warning>%s@} %a@." warning_prefix Warnings.print w
284300
end
285301
;;
286302

@@ -314,8 +330,31 @@ type error =
314330
if_highlight: string; (* alternative message if locations are highlighted *)
315331
}
316332

317-
let errorf ?(loc = none) ?(sub = []) ?(if_highlight = "") =
318-
Printf.ksprintf (fun msg -> {loc; msg; sub; if_highlight})
333+
let pp_ksprintf ?before k fmt =
334+
let buf = Buffer.create 64 in
335+
let ppf = Format.formatter_of_buffer buf in
336+
Misc.Color.set_color_tag_handling ppf;
337+
begin match before with
338+
| None -> ()
339+
| Some f -> f ppf
340+
end;
341+
kfprintf
342+
(fun _ ->
343+
pp_print_flush ppf ();
344+
let msg = Buffer.contents buf in
345+
k msg)
346+
ppf fmt
347+
348+
let errorf ?(loc = none) ?(sub = []) ?(if_highlight = "") fmt =
349+
pp_ksprintf
350+
(fun msg -> {loc; msg; sub; if_highlight})
351+
fmt
352+
353+
let errorf_prefixed ?(loc=none) ?(sub=[]) ?(if_highlight="") fmt =
354+
pp_ksprintf
355+
~before:(fun ppf -> fprintf ppf "%a " print_error_prefix ())
356+
(fun msg -> {loc; msg; sub; if_highlight})
357+
fmt
319358

320359
let error ?(loc = none) ?(sub = []) ?(if_highlight = "") msg =
321360
{loc; msg; sub; if_highlight}
@@ -361,13 +400,7 @@ let report_error ppf err =
361400
;;
362401

363402
let error_of_printer loc print x =
364-
let buf = Buffer.create 64 in
365-
let ppf = Format.formatter_of_buffer buf in
366-
pp_print_string ppf "Error: ";
367-
print ppf x;
368-
pp_print_flush ppf ();
369-
let msg = Buffer.contents buf in
370-
errorf ~loc "%s" msg
403+
errorf_prefixed ~loc "%a@?" print x
371404

372405
let error_of_printer_file print x =
373406
error_of_printer (in_file !input_name) print x
@@ -376,11 +409,12 @@ let () =
376409
register_error_of_exn
377410
(function
378411
| Sys_error msg ->
379-
Some (errorf ~loc:(in_file !input_name) "Error: I/O error: %s" msg)
412+
Some (errorf_prefixed ~loc:(in_file !input_name)
413+
"I/O error: %s" msg)
380414
| Warnings.Errors n ->
381415
Some
382-
(errorf ~loc:(in_file !input_name)
383-
"Error: Some fatal warnings were triggered (%d occurrences)" n)
416+
(errorf_prefixed ~loc:(in_file !input_name)
417+
"Some fatal warnings were triggered (%d occurrences)" n)
384418
| _ ->
385419
None
386420
)
@@ -407,4 +441,4 @@ let () =
407441
)
408442

409443
let raise_errorf ?(loc = none) ?(sub = []) ?(if_highlight = "") =
410-
Printf.ksprintf (fun msg -> raise (Error ({loc; msg; sub; if_highlight})))
444+
pp_ksprintf (fun msg -> raise (Error ({loc; msg; sub; if_highlight})))

parsing/location.mli

+11-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ val input_lexbuf: Lexing.lexbuf option ref
5353
val get_pos_info: Lexing.position -> string * int * int (* file, line, char *)
5454
val print_loc: formatter -> t -> unit
5555
val print_error: formatter -> t -> unit
56-
val print_error_cur_file: formatter -> unit
56+
val print_error_cur_file: formatter -> unit -> unit
5757
val print_warning: t -> formatter -> Warnings.t -> unit
5858
val formatter_for_warnings : formatter ref
5959
val prerr_warning: t -> Warnings.t -> unit
@@ -88,7 +88,6 @@ val show_filename: string -> string
8888

8989
val absname: bool ref
9090

91-
9291
(* Support for located errors *)
9392

9493
type error =
@@ -101,13 +100,21 @@ type error =
101100

102101
exception Error of error
103102

103+
val print_error_prefix: formatter -> unit -> unit
104+
(* print the prefix "Error:" possibly with style *)
105+
104106
val error: ?loc:t -> ?sub:error list -> ?if_highlight:string -> string -> error
105107

106108
val errorf: ?loc:t -> ?sub:error list -> ?if_highlight:string
107-
-> ('a, unit, string, error) format4 -> 'a
109+
-> ('a, Format.formatter, unit, error) format4 -> 'a
110+
111+
val errorf_prefixed : ?loc:t -> ?sub:error list -> ?if_highlight:string
112+
-> ('a, Format.formatter, unit, error) format4 -> 'a
113+
(* same as {!errorf}, but prints the error prefix "Error:" before yielding
114+
* to the format string *)
108115

109116
val raise_errorf: ?loc:t -> ?sub:error list -> ?if_highlight:string
110-
-> ('a, unit, string, 'b) format4 -> 'a
117+
-> ('a, Format.formatter, unit, 'b) format4 -> 'a
111118

112119
val error_of_printer: t -> (formatter -> 'a -> unit) -> 'a -> error
113120

parsing/syntaxerr.ml

+13-13
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,34 @@ exception Escape_error
2626

2727
let prepare_error = function
2828
| Unclosed(opening_loc, opening, closing_loc, closing) ->
29-
Location.errorf ~loc:closing_loc
29+
Location.errorf_prefixed ~loc:closing_loc
3030
~sub:[
31-
Location.error ~loc:opening_loc
32-
(Printf.sprintf "Error: This '%s' might be unmatched" opening)
31+
Location.errorf_prefixed ~loc:opening_loc
32+
"This '%s' might be unmatched" opening
3333
]
3434
~if_highlight:
3535
(Printf.sprintf "Syntax error: '%s' expected, \
3636
the highlighted '%s' might be unmatched"
3737
closing opening)
38-
"Error: Syntax error: '%s' expected" closing
38+
"Syntax error: '%s' expected" closing
3939

4040
| Expecting (loc, nonterm) ->
41-
Location.errorf ~loc "Error: Syntax error: %s expected." nonterm
41+
Location.errorf_prefixed ~loc "Syntax error: %s expected." nonterm
4242
| Not_expecting (loc, nonterm) ->
43-
Location.errorf ~loc "Error: Syntax error: %s not expected." nonterm
43+
Location.errorf_prefixed ~loc "Syntax error: %s not expected." nonterm
4444
| Applicative_path loc ->
45-
Location.errorf ~loc
46-
"Error: Syntax error: applicative paths of the form F(X).t \
45+
Location.errorf_prefixed ~loc
46+
"Syntax error: applicative paths of the form F(X).t \
4747
are not supported when the option -no-app-func is set."
4848
| Variable_in_scope (loc, var) ->
49-
Location.errorf ~loc
50-
"Error: In this scoped type, variable '%s \
49+
Location.errorf_prefixed ~loc
50+
"In this scoped type, variable '%s \
5151
is reserved for the local type %s."
52-
var var
52+
var var
5353
| Other loc ->
54-
Location.error ~loc "Error: Syntax error"
54+
Location.errorf_prefixed ~loc "Syntax error"
5555
| Ill_formed_ast (loc, s) ->
56-
Location.errorf ~loc "Error: broken invariant in parsetree: %s" s
56+
Location.errorf_prefixed ~loc "broken invariant in parsetree: %s" s
5757

5858
let () =
5959
Location.register_error_of_exn

0 commit comments

Comments
 (0)