improvement over error messages (with code ;) )
This commit is contained in:
parent
298e88f1a5
commit
ef0effeb1f
@ -65,19 +65,21 @@ let _ =
|
|||||||
(** Main functionality below *)
|
(** Main functionality below *)
|
||||||
print_verbose "Parsing the source file...";
|
print_verbose "Parsing the source file...";
|
||||||
let ast =
|
let ast =
|
||||||
|
let inchan = open_in !source_file in
|
||||||
try
|
try
|
||||||
begin
|
begin
|
||||||
let inchan = open_in !source_file in
|
|
||||||
let res = Parser.main Lexer.token (Lexing.from_channel inchan) in
|
let res = Parser.main Lexer.token (Lexing.from_channel inchan) in
|
||||||
close_in inchan; res
|
close_in inchan; res
|
||||||
end
|
end
|
||||||
with
|
with
|
||||||
| Lexer.Lexing_error s ->
|
| Lexer.Lexing_error s ->
|
||||||
(exit_error (Format.sprintf "Error code:\n\t%s\n\n" s); exit 0)
|
(close_in_noerr inchan;
|
||||||
|
exit_error (Format.sprintf "Error code:\n\t%s\n\n" s); exit 0)
|
||||||
| Utils.MyParsingError (s, l) ->
|
| Utils.MyParsingError (s, l) ->
|
||||||
begin
|
begin
|
||||||
|
close_in_noerr inchan;
|
||||||
Format.printf "Syntax error at %a: %s\n\n"
|
Format.printf "Syntax error at %a: %s\n\n"
|
||||||
Pp.pp_loc l s;
|
Pp.pp_loc (l, !source_file) s;
|
||||||
exit 0
|
exit 0
|
||||||
end in
|
end in
|
||||||
|
|
||||||
|
25
src/pp.ml
25
src/pp.ml
@ -6,12 +6,25 @@ open Ast
|
|||||||
| TBool :: t -> Format.fprintf fmt "bool %a" debug_type_pp t
|
| TBool :: t -> Format.fprintf fmt "bool %a" debug_type_pp t
|
||||||
| TReal :: t -> Format.fprintf fmt "real %a" debug_type_pp t
|
| TReal :: t -> Format.fprintf fmt "real %a" debug_type_pp t
|
||||||
|
|
||||||
let pp_loc fmt (start, stop) =
|
let pp_loc fmt ((start, stop), file) =
|
||||||
Lexing.(
|
let spos, epos =
|
||||||
Format.fprintf fmt "%s: <l: %d, c: %d> -- <l: %d, c: %d>"
|
Lexing.(start.pos_cnum, stop.pos_cnum) in
|
||||||
start.pos_fname
|
let f = open_in file in
|
||||||
start.pos_lnum start.pos_cnum
|
try
|
||||||
stop.pos_lnum stop.pos_cnum)
|
begin
|
||||||
|
let rec aux linenum curpos =
|
||||||
|
let line = input_line f in
|
||||||
|
let nextpos = curpos + (String.length line) + 1 in
|
||||||
|
if nextpos >= epos then
|
||||||
|
Format.fprintf fmt "<line %d: %s >" linenum line
|
||||||
|
else
|
||||||
|
aux (linenum + 1) nextpos
|
||||||
|
in
|
||||||
|
aux 1 0;
|
||||||
|
close_in f
|
||||||
|
end
|
||||||
|
with e ->
|
||||||
|
(close_in_noerr f; Format.fprintf fmt "???")
|
||||||
|
|
||||||
let rec pp_varlist fmt : t_varlist -> unit = function
|
let rec pp_varlist fmt : t_varlist -> unit = function
|
||||||
| ([], []) -> ()
|
| ([], []) -> ()
|
||||||
|
Loading…
Reference in New Issue
Block a user