Skip to content

Commit

Permalink
Add file extraction in error output
Browse files Browse the repository at this point in the history
  • Loading branch information
epatrizio committed Sep 12, 2022
1 parent 42531f6 commit e889faa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ let process source_code_file no_typing =
localisation l1 source_code_file;
localisation l2 source_code_file;
eprintf "Typing error: %s@." s;
extract_in_file source_code_file l1 l2;
exit 1
| Compiler.Error ((l1,l2),s) ->
localisation l1 source_code_file;
localisation l2 source_code_file;
eprintf "Compilation error: %s@." s;
extract_in_file source_code_file l1 l2;
exit 1

let _ =
Expand Down
15 changes: 15 additions & 0 deletions utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,20 @@ let localisation pos file =
let c = pos.pos_cnum - pos.pos_bol + 1 in
eprintf "File \"%s\", line %d, characters %d-%d:\n" file l (c-1) c

let extract_in_file file pos1 pos2 =
let current_line = ref "" in
let l1 = pos1.pos_lnum in
let l2 = pos2.pos_lnum in
try
let ic = open_in file in
eprintf "---- view in file ----\n";
for i=0 to (l2-1) do
current_line := input_line ic;
if i >= (l1-1) && i <= (l2-1) then eprintf "%s\n" !current_line
done;
eprintf "----\n";
close_in ic;
with exc -> ()

let counter_from n =
let r = ref (n-1) in fun () -> incr r; !r

0 comments on commit e889faa

Please sign in to comment.