Skip to content

Commit

Permalink
[Pretty errors] Don't try to display position source when pos points …
Browse files Browse the repository at this point in the history
…to file itself
  • Loading branch information
kLabz committed Apr 5, 2024
1 parent 556439d commit a190906
Showing 1 changed file with 48 additions and 44 deletions.
92 changes: 48 additions & 44 deletions src/compiler/messageReporting.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,57 @@ open Common
open CompilationContext

let resolve_source file l1 p1 l2 p2 =
let ch = open_in_bin file in
let curline = ref 1 in
let lines = ref [] in
let rec loop p line =
let inc i line =
if (!curline >= l1) && (!curline <= l2) then lines := (!curline, line) :: !lines;
curline := !curline + 1;
(i, "")
in

let input_char_or_done ch line =
try input_char ch with End_of_file -> begin
ignore(inc 0 line);
raise End_of_file
end
in
if l1 = l2 && p1 = p2 && l1 = 1 && p1 = 1 then []
else begin
let ch = open_in_bin file in
let curline = ref 1 in
let lines = ref [] in
let rec loop p line =
let inc i line =
if (!curline >= l1) && (!curline <= l2) then lines := (!curline, line) :: !lines;
curline := !curline + 1;
(i, "")
in

let read_char line = match input_char_or_done ch line with
| '\n' -> inc 1 line
| '\r' ->
ignore(input_char_or_done ch line);
inc 2 line
| c -> begin
let line = ref (line ^ (String.make 1 c)) in
let rec skip n =
if n > 0 then begin
let c = input_char_or_done ch !line in
line := !line ^ (String.make 1 c);
skip (n - 1)
end
in
let input_char_or_done ch line =
try input_char ch with End_of_file -> begin
ignore(inc 0 line);
raise End_of_file
end
in

let code = int_of_char c in
if code < 0xC0 then ()
else if code < 0xE0 then skip 1
else if code < 0xF0 then skip 2
else skip 3;
let read_char line = match input_char_or_done ch line with
| '\n' -> inc 1 line
| '\r' ->
ignore(input_char_or_done ch line);
inc 2 line
| c -> begin
let line = ref (line ^ (String.make 1 c)) in
let rec skip n =
if n > 0 then begin
let c = input_char_or_done ch !line in
line := !line ^ (String.make 1 c);
skip (n - 1)
end
in

let code = int_of_char c in
if code < 0xC0 then ()
else if code < 0xE0 then skip 1
else if code < 0xF0 then skip 2
else skip 3;

(1, !line)
end
in

(1, !line)
end
let (delta, line) = read_char line in
loop (p + delta) line
in

let (delta, line) = read_char line in
loop (p + delta) line
in

try loop 0 ""; with End_of_file -> close_in ch;
List.rev !lines
try loop 0 ""; with End_of_file -> close_in ch;
List.rev !lines
end

let resolve_file ctx f =
let ext = StringHelper.extension f in
Expand Down Expand Up @@ -100,7 +103,8 @@ let compiler_pretty_message_string com ectx cm =
let l1, p1, l2, p2 = Lexer.get_pos_coords cm.cm_pos in
let lines = resolve_source f l1 p1 l2 p2 in
let epos =
if ectx.absolute_positions then TPrinting.Printer.s_pos cm.cm_pos
if lines = [] then cm.cm_pos.pfile
else if ectx.absolute_positions then TPrinting.Printer.s_pos cm.cm_pos
else Lexer.get_error_pos error_printer cm.cm_pos
in
(l1, p1, l2, p2, epos, lines)
Expand Down

0 comments on commit a190906

Please sign in to comment.