From a71db86d8819d364d60e4ad07cf45c4c260e57d8 Mon Sep 17 00:00:00 2001 From: Jeremie Dimino Date: Sun, 13 Nov 2016 12:32:12 +0000 Subject: [PATCH] better error reporting --- src/import.ml | 10 +++++++++- src/main.ml | 15 ++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/import.ml b/src/import.ml index ef96321c..f5b19da8 100644 --- a/src/import.ml +++ b/src/import.ml @@ -52,7 +52,15 @@ let with_file_in fn ~f = protectx (open_in fn) ~finally:close_in ~f let with_lexbuf_from_file fn ~f = - with_file_in fn ~f:(fun ic -> f (Lexing.from_channel ic)) + with_file_in fn ~f:(fun ic -> + let lb = Lexing.from_channel ic in + lb.lex_curr_p <- + { pos_fname = fn + ; pos_lnum = 1 + ; pos_bol = 0 + ; pos_cnum = 0 + }; + f lb) let lines_of_file fn = let rec loop ic acc = diff --git a/src/main.ml b/src/main.ml index be2795bc..1884b047 100644 --- a/src/main.ml +++ b/src/main.ml @@ -7,7 +7,7 @@ let internal argv = | _ -> () -let () = +let main () = let argv = Sys.argv in let argc = Array.length argv in let compact () = @@ -19,3 +19,16 @@ let () = match argv.(1) with | "internal" -> internal (compact ()) | _ -> () + +let () = + try + main () + with + | Loc.Error ({ start; stop }, msg) -> + let start_c = start.pos_cnum - start.pos_bol in + let stop_c = stop.pos_cnum - start.pos_bol in + Printf.eprintf + "File \"%s\", line %d, characters %d-%d:\n\ + Error: %s\n%!" + start.pos_fname start.pos_lnum start_c stop_c msg +