1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
open Lexing
|
|
|
|
|
2011-12-20 02:35:43 -08:00
|
|
|
let absname = ref false
|
2012-03-08 11:52:03 -08:00
|
|
|
(* This reference should be in Clflags, but it would create an additional
|
|
|
|
dependency and make bootstrapping Camlp4 more difficult. *)
|
2011-12-20 02:35:43 -08:00
|
|
|
|
2002-11-01 09:06:47 -08:00
|
|
|
type t = { loc_start: position; loc_end: position; loc_ghost: bool };;
|
|
|
|
|
2005-03-24 09:20:54 -08:00
|
|
|
let in_file name =
|
|
|
|
let loc = {
|
|
|
|
pos_fname = name;
|
|
|
|
pos_lnum = 1;
|
|
|
|
pos_bol = 0;
|
|
|
|
pos_cnum = -1;
|
|
|
|
} in
|
|
|
|
{ loc_start = loc; loc_end = loc; loc_ghost = true }
|
|
|
|
;;
|
|
|
|
|
2011-08-04 07:59:13 -07:00
|
|
|
let none = in_file "_none_";;
|
|
|
|
|
2002-11-01 09:06:47 -08:00
|
|
|
let curr lexbuf = {
|
|
|
|
loc_start = lexbuf.lex_start_p;
|
|
|
|
loc_end = lexbuf.lex_curr_p;
|
|
|
|
loc_ghost = false
|
|
|
|
};;
|
|
|
|
|
|
|
|
let init lexbuf fname =
|
|
|
|
lexbuf.lex_curr_p <- {
|
|
|
|
pos_fname = fname;
|
|
|
|
pos_lnum = 1;
|
|
|
|
pos_bol = 0;
|
|
|
|
pos_cnum = 0;
|
|
|
|
}
|
|
|
|
;;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2002-11-01 09:06:47 -08:00
|
|
|
let symbol_rloc () = {
|
|
|
|
loc_start = Parsing.symbol_start_pos ();
|
|
|
|
loc_end = Parsing.symbol_end_pos ();
|
|
|
|
loc_ghost = false;
|
|
|
|
};;
|
1999-09-08 10:42:36 -07:00
|
|
|
|
2002-11-01 09:06:47 -08:00
|
|
|
let symbol_gloc () = {
|
|
|
|
loc_start = Parsing.symbol_start_pos ();
|
|
|
|
loc_end = Parsing.symbol_end_pos ();
|
|
|
|
loc_ghost = true;
|
|
|
|
};;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2002-11-01 09:06:47 -08:00
|
|
|
let rhs_loc n = {
|
|
|
|
loc_start = Parsing.rhs_start_pos n;
|
|
|
|
loc_end = Parsing.rhs_end_pos n;
|
|
|
|
loc_ghost = false;
|
|
|
|
};;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2007-12-04 05:38:58 -08:00
|
|
|
let input_name = ref "_none_"
|
1995-05-04 03:15:53 -07:00
|
|
|
let input_lexbuf = ref (None : lexbuf option)
|
|
|
|
|
|
|
|
(* Terminal info *)
|
|
|
|
|
1998-09-02 11:21:00 -07:00
|
|
|
let status = ref Terminfo.Uninitialised
|
|
|
|
|
1995-09-08 01:55:59 -07:00
|
|
|
let num_loc_lines = ref 0 (* number of lines already printed after input *)
|
|
|
|
|
2008-01-11 08:13:18 -08:00
|
|
|
(* Highlight the locations using standout mode. *)
|
2002-06-26 07:51:03 -07:00
|
|
|
|
|
|
|
let highlight_terminfo ppf num_lines lb loc1 loc2 =
|
2008-01-11 08:13:18 -08:00
|
|
|
Format.pp_print_flush ppf (); (* avoid mixing Format and normal output *)
|
2002-06-26 07:51:03 -07:00
|
|
|
(* Char 0 is at offset -lb.lex_abs_pos in lb.lex_buffer. *)
|
|
|
|
let pos0 = -lb.lex_abs_pos in
|
|
|
|
(* Do nothing if the buffer does not contain the whole phrase. *)
|
|
|
|
if pos0 < 0 then raise Exit;
|
|
|
|
(* Count number of lines in phrase *)
|
|
|
|
let lines = ref !num_loc_lines in
|
|
|
|
for i = pos0 to lb.lex_buffer_len - 1 do
|
|
|
|
if lb.lex_buffer.[i] = '\n' then incr lines
|
|
|
|
done;
|
|
|
|
(* If too many lines, give up *)
|
|
|
|
if !lines >= num_lines - 2 then raise Exit;
|
|
|
|
(* Move cursor up that number of lines *)
|
|
|
|
flush stdout; Terminfo.backup !lines;
|
|
|
|
(* Print the input, switching to standout for the location *)
|
|
|
|
let bol = ref false in
|
|
|
|
print_string "# ";
|
|
|
|
for pos = 0 to lb.lex_buffer_len - pos0 - 1 do
|
|
|
|
if !bol then (print_string " "; bol := false);
|
2002-11-01 09:06:47 -08:00
|
|
|
if pos = loc1.loc_start.pos_cnum || pos = loc2.loc_start.pos_cnum then
|
2002-06-26 07:51:03 -07:00
|
|
|
Terminfo.standout true;
|
2002-11-01 09:06:47 -08:00
|
|
|
if pos = loc1.loc_end.pos_cnum || pos = loc2.loc_end.pos_cnum then
|
2002-06-26 07:51:03 -07:00
|
|
|
Terminfo.standout false;
|
|
|
|
let c = lb.lex_buffer.[pos + pos0] in
|
|
|
|
print_char c;
|
|
|
|
bol := (c = '\n')
|
|
|
|
done;
|
|
|
|
(* Make sure standout mode is over *)
|
|
|
|
Terminfo.standout false;
|
|
|
|
(* Position cursor back to original location *)
|
|
|
|
Terminfo.resume !num_loc_lines;
|
|
|
|
flush stdout
|
|
|
|
|
|
|
|
(* Highlight the location by printing it again. *)
|
|
|
|
|
|
|
|
let highlight_dumb ppf lb loc =
|
|
|
|
(* Char 0 is at offset -lb.lex_abs_pos in lb.lex_buffer. *)
|
|
|
|
let pos0 = -lb.lex_abs_pos in
|
|
|
|
(* Do nothing if the buffer does not contain the whole phrase. *)
|
|
|
|
if pos0 < 0 then raise Exit;
|
|
|
|
let end_pos = lb.lex_buffer_len - pos0 - 1 in
|
|
|
|
(* Determine line numbers for the start and end points *)
|
|
|
|
let line_start = ref 0 and line_end = ref 0 in
|
|
|
|
for pos = 0 to end_pos do
|
|
|
|
if lb.lex_buffer.[pos + pos0] = '\n' then begin
|
2002-11-01 09:06:47 -08:00
|
|
|
if loc.loc_start.pos_cnum > pos then incr line_start;
|
|
|
|
if loc.loc_end.pos_cnum > pos then incr line_end;
|
2002-06-26 07:51:03 -07:00
|
|
|
end
|
|
|
|
done;
|
|
|
|
(* Print character location (useful for Emacs) *)
|
2002-11-01 09:06:47 -08:00
|
|
|
Format.fprintf ppf "Characters %i-%i:@."
|
|
|
|
loc.loc_start.pos_cnum loc.loc_end.pos_cnum;
|
2002-06-26 07:51:03 -07:00
|
|
|
(* Print the input, underlining the location *)
|
2008-01-11 08:13:18 -08:00
|
|
|
Format.pp_print_string ppf " ";
|
2002-06-26 07:51:03 -07:00
|
|
|
let line = ref 0 in
|
|
|
|
let pos_at_bol = ref 0 in
|
|
|
|
for pos = 0 to end_pos do
|
|
|
|
let c = lb.lex_buffer.[pos + pos0] in
|
|
|
|
if c <> '\n' then begin
|
|
|
|
if !line = !line_start && !line = !line_end then
|
|
|
|
(* loc is on one line: print whole line *)
|
2008-01-11 08:13:18 -08:00
|
|
|
Format.pp_print_char ppf c
|
2002-06-26 07:51:03 -07:00
|
|
|
else if !line = !line_start then
|
|
|
|
(* first line of multiline loc: print ... before loc_start *)
|
2002-11-01 09:06:47 -08:00
|
|
|
if pos < loc.loc_start.pos_cnum
|
2008-01-11 08:13:18 -08:00
|
|
|
then Format.pp_print_char ppf '.'
|
|
|
|
else Format.pp_print_char ppf c
|
2002-06-26 07:51:03 -07:00
|
|
|
else if !line = !line_end then
|
|
|
|
(* last line of multiline loc: print ... after loc_end *)
|
2002-11-01 09:06:47 -08:00
|
|
|
if pos < loc.loc_end.pos_cnum
|
2008-01-11 08:13:18 -08:00
|
|
|
then Format.pp_print_char ppf c
|
|
|
|
else Format.pp_print_char ppf '.'
|
2002-06-26 07:51:03 -07:00
|
|
|
else if !line > !line_start && !line < !line_end then
|
|
|
|
(* intermediate line of multiline loc: print whole line *)
|
2008-01-11 08:13:18 -08:00
|
|
|
Format.pp_print_char ppf c
|
2002-06-26 07:51:03 -07:00
|
|
|
end else begin
|
|
|
|
if !line = !line_start && !line = !line_end then begin
|
|
|
|
(* loc is on one line: underline location *)
|
2008-01-11 08:13:18 -08:00
|
|
|
Format.fprintf ppf "@. ";
|
2002-11-01 09:06:47 -08:00
|
|
|
for i = !pos_at_bol to loc.loc_start.pos_cnum - 1 do
|
2008-01-11 08:13:18 -08:00
|
|
|
Format.pp_print_char ppf ' '
|
2002-06-26 07:51:03 -07:00
|
|
|
done;
|
2002-11-01 09:06:47 -08:00
|
|
|
for i = loc.loc_start.pos_cnum to loc.loc_end.pos_cnum - 1 do
|
2008-01-11 08:13:18 -08:00
|
|
|
Format.pp_print_char ppf '^'
|
2002-06-26 07:51:03 -07:00
|
|
|
done
|
|
|
|
end;
|
|
|
|
if !line >= !line_start && !line <= !line_end then begin
|
2008-01-11 08:13:18 -08:00
|
|
|
Format.fprintf ppf "@.";
|
|
|
|
if pos < loc.loc_end.pos_cnum then Format.pp_print_string ppf " "
|
2002-06-26 07:51:03 -07:00
|
|
|
end;
|
|
|
|
incr line;
|
|
|
|
pos_at_bol := pos + 1;
|
|
|
|
end
|
|
|
|
done
|
|
|
|
|
|
|
|
(* Highlight the location using one of the supported modes. *)
|
|
|
|
|
2002-04-18 01:50:43 -07:00
|
|
|
let rec highlight_locations ppf loc1 loc2 =
|
1995-05-04 03:15:53 -07:00
|
|
|
match !status with
|
1998-09-02 11:21:00 -07:00
|
|
|
Terminfo.Uninitialised ->
|
2002-04-18 01:50:43 -07:00
|
|
|
status := Terminfo.setup stdout; highlight_locations ppf loc1 loc2
|
1998-09-02 11:21:00 -07:00
|
|
|
| Terminfo.Bad_term ->
|
2001-09-08 12:16:19 -07:00
|
|
|
begin match !input_lexbuf with
|
|
|
|
None -> false
|
|
|
|
| Some lb ->
|
2002-06-26 07:51:03 -07:00
|
|
|
let norepeat =
|
|
|
|
try Sys.getenv "TERM" = "norepeat" with Not_found -> false in
|
|
|
|
if norepeat then false else
|
|
|
|
try highlight_dumb ppf lb loc1; true
|
|
|
|
with Exit -> false
|
2001-09-08 12:16:19 -07:00
|
|
|
end
|
1998-09-02 11:21:00 -07:00
|
|
|
| Terminfo.Good_term num_lines ->
|
2002-06-26 07:51:03 -07:00
|
|
|
begin match !input_lexbuf with
|
1995-05-04 03:15:53 -07:00
|
|
|
None -> false
|
|
|
|
| Some lb ->
|
2002-06-26 07:51:03 -07:00
|
|
|
try highlight_terminfo ppf num_lines lb loc1 loc2; true
|
1998-09-02 11:21:00 -07:00
|
|
|
with Exit -> false
|
2002-06-26 07:51:03 -07:00
|
|
|
end
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* Print the location in some way or another *)
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
open Format
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2011-12-20 02:35:43 -08:00
|
|
|
let absolute_path s = (* This function could go into Filename *)
|
|
|
|
let open Filename in
|
|
|
|
let s = if is_relative s then concat (Sys.getcwd ()) s else s in
|
|
|
|
(* Now simplify . and .. components *)
|
|
|
|
let rec aux s =
|
|
|
|
let base = basename s in
|
|
|
|
let dir = dirname s in
|
2012-03-06 02:54:45 -08:00
|
|
|
if dir = s then dir
|
|
|
|
else if base = current_dir_name then aux dir
|
2011-12-20 02:35:43 -08:00
|
|
|
else if base = parent_dir_name then dirname (aux dir)
|
|
|
|
else concat (aux dir) base
|
|
|
|
in
|
|
|
|
aux s
|
|
|
|
|
2012-03-07 09:27:59 -08:00
|
|
|
let show_filename file =
|
|
|
|
if !absname then absolute_path file else file
|
|
|
|
|
2011-12-20 02:35:43 -08:00
|
|
|
let print_filename ppf file =
|
2012-03-07 09:27:59 -08:00
|
|
|
Format.fprintf ppf "%s" (show_filename file)
|
2011-12-20 02:35:43 -08:00
|
|
|
|
1995-09-08 01:55:59 -07:00
|
|
|
let reset () =
|
|
|
|
num_loc_lines := 0
|
|
|
|
|
2012-04-17 01:02:02 -07:00
|
|
|
let (msg_file, msg_line, msg_chars, msg_to, msg_colon) =
|
|
|
|
("File \"", "\", line ", ", characters ", "-", ":")
|
1996-11-02 10:03:23 -08:00
|
|
|
|
2002-11-26 09:14:28 -08:00
|
|
|
(* return file, line, char from the given position *)
|
|
|
|
let get_pos_info pos =
|
2011-08-04 07:59:13 -07:00
|
|
|
(pos.pos_fname, pos.pos_lnum, pos.pos_cnum - pos.pos_bol)
|
2002-11-26 09:14:28 -08:00
|
|
|
;;
|
|
|
|
|
2011-10-20 20:26:35 -07:00
|
|
|
let print_loc ppf loc =
|
2002-11-26 09:14:28 -08:00
|
|
|
let (file, line, startchar) = get_pos_info loc.loc_start in
|
|
|
|
let endchar = loc.loc_end.pos_cnum - loc.loc_start.pos_cnum + startchar in
|
2011-08-04 07:59:13 -07:00
|
|
|
if file = "//toplevel//" then begin
|
2002-04-18 01:50:43 -07:00
|
|
|
if highlight_locations ppf loc none then () else
|
2012-04-17 01:02:02 -07:00
|
|
|
fprintf ppf "Characters %i-%i"
|
2002-11-01 09:06:47 -08:00
|
|
|
loc.loc_start.pos_cnum loc.loc_end.pos_cnum
|
2002-11-26 09:14:28 -08:00
|
|
|
end else begin
|
2011-12-20 02:35:43 -08:00
|
|
|
fprintf ppf "%s%a%s%i" msg_file print_filename file msg_line line;
|
2011-08-04 07:59:13 -07:00
|
|
|
if startchar >= 0 then
|
2011-10-20 20:26:35 -07:00
|
|
|
fprintf ppf "%s%i%s%i" msg_chars startchar msg_to endchar
|
1995-05-04 03:15:53 -07:00
|
|
|
end
|
2007-12-04 05:38:58 -08:00
|
|
|
;;
|
|
|
|
|
2011-10-20 20:26:35 -07:00
|
|
|
let print ppf loc =
|
|
|
|
if loc.loc_start.pos_fname = "//toplevel//"
|
|
|
|
&& highlight_locations ppf loc none then ()
|
2012-04-17 01:02:02 -07:00
|
|
|
else fprintf ppf "%a%s@." print_loc loc msg_colon
|
|
|
|
;;
|
2011-10-20 20:26:35 -07:00
|
|
|
|
2007-12-04 05:38:58 -08:00
|
|
|
let print_error ppf loc =
|
|
|
|
print ppf loc;
|
|
|
|
fprintf ppf "Error: ";
|
|
|
|
;;
|
|
|
|
|
|
|
|
let print_error_cur_file ppf = print_error ppf (in_file !input_name);;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let print_warning loc ppf w =
|
2000-10-26 06:38:43 -07:00
|
|
|
if Warnings.is_active w then begin
|
|
|
|
let printw ppf w =
|
|
|
|
let n = Warnings.print ppf w in
|
|
|
|
num_loc_lines := !num_loc_lines + n
|
|
|
|
in
|
2011-10-20 20:26:35 -07:00
|
|
|
print ppf loc;
|
2004-11-10 04:47:20 -08:00
|
|
|
fprintf ppf "Warning %a@." printw w;
|
2000-10-31 06:55:30 -08:00
|
|
|
pp_print_flush ppf ();
|
2000-10-26 06:38:43 -07:00
|
|
|
incr num_loc_lines;
|
|
|
|
end
|
1998-11-05 00:02:52 -08:00
|
|
|
;;
|
1995-09-08 01:55:59 -07:00
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let prerr_warning loc w = print_warning loc err_formatter w;;
|
|
|
|
|
1997-04-15 12:18:41 -07:00
|
|
|
let echo_eof () =
|
|
|
|
print_newline ();
|
1997-05-13 07:45:28 -07:00
|
|
|
incr num_loc_lines
|