Skip to content

Commit e1f3541

Browse files
committed
remove the repl printing complexity
1 parent a1df288 commit e1f3541

File tree

2 files changed

+27
-169
lines changed

2 files changed

+27
-169
lines changed

parsing/location.ml

Lines changed: 19 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -72,153 +72,9 @@ let set_input_name name =
7272
if name <> "" then input_name := name
7373
(* Terminal info *)
7474

75-
let status = ref Terminfo.Uninitialised
7675

77-
let num_loc_lines = ref 0 (* number of lines already printed after input *)
7876

79-
let print_updating_num_loc_lines ppf f arg =
80-
let open Format in
81-
let out_functions = pp_get_formatter_out_functions ppf () in
82-
let out_string str start len =
83-
let rec count i c =
84-
if i = start + len then c
85-
else if String.get str i = '\n' then count (succ i) (succ c)
86-
else count (succ i) c in
87-
num_loc_lines := !num_loc_lines + count start 0 ;
88-
out_functions.out_string str start len in
89-
pp_set_formatter_out_functions ppf
90-
{ out_functions with out_string } ;
91-
f ppf arg ;
92-
pp_print_flush ppf ();
93-
pp_set_formatter_out_functions ppf out_functions
94-
95-
(* Highlight the locations using standout mode. *)
96-
97-
let highlight_terminfo ppf num_lines lb locs =
98-
Format.pp_print_flush ppf (); (* avoid mixing Format and normal output *)
99-
(* Char 0 is at offset -lb.lex_abs_pos in lb.lex_buffer. *)
100-
let pos0 = -lb.lex_abs_pos in
101-
(* Do nothing if the buffer does not contain the whole phrase. *)
102-
if pos0 < 0 then raise Exit;
103-
(* Count number of lines in phrase *)
104-
let lines = ref !num_loc_lines in
105-
for i = pos0 to lb.lex_buffer_len - 1 do
106-
if Bytes.get lb.lex_buffer i = '\n' then incr lines
107-
done;
108-
(* If too many lines, give up *)
109-
if !lines >= num_lines - 2 then raise Exit;
110-
(* Move cursor up that number of lines *)
111-
flush stdout; Terminfo.backup !lines;
112-
(* Print the input, switching to standout for the location *)
113-
let bol = ref false in
114-
print_string "# ";
115-
for pos = 0 to lb.lex_buffer_len - pos0 - 1 do
116-
if !bol then (print_string " "; bol := false);
117-
if List.exists (fun loc -> pos = loc.loc_start.pos_cnum) locs then
118-
Terminfo.standout true;
119-
if List.exists (fun loc -> pos = loc.loc_end.pos_cnum) locs then
120-
Terminfo.standout false;
121-
let c = Bytes.get lb.lex_buffer (pos + pos0) in
122-
print_char c;
123-
bol := (c = '\n')
124-
done;
125-
(* Make sure standout mode is over *)
126-
Terminfo.standout false;
127-
(* Position cursor back to original location *)
128-
Terminfo.resume !num_loc_lines;
129-
flush stdout
130-
131-
(* Highlight the location by printing it again. *)
132-
133-
let highlight_dumb ppf lb loc =
134-
(* Char 0 is at offset -lb.lex_abs_pos in lb.lex_buffer. *)
135-
let pos0 = -lb.lex_abs_pos in
136-
(* Do nothing if the buffer does not contain the whole phrase. *)
137-
if pos0 < 0 then raise Exit;
138-
let end_pos = lb.lex_buffer_len - pos0 - 1 in
139-
(* Determine line numbers for the start and end points *)
140-
let line_start = ref 0 and line_end = ref 0 in
141-
for pos = 0 to end_pos do
142-
if Bytes.get lb.lex_buffer (pos + pos0) = '\n' then begin
143-
if loc.loc_start.pos_cnum > pos then incr line_start;
144-
if loc.loc_end.pos_cnum > pos then incr line_end;
145-
end
146-
done;
147-
(* Print character location (useful for Emacs) *)
148-
Format.fprintf ppf "@[<v>Characters %i-%i:@,"
149-
loc.loc_start.pos_cnum loc.loc_end.pos_cnum;
150-
(* Print the input, underlining the location *)
151-
Format.pp_print_string ppf " ";
152-
let line = ref 0 in
153-
let pos_at_bol = ref 0 in
154-
for pos = 0 to end_pos do
155-
match Bytes.get lb.lex_buffer (pos + pos0) with
156-
| '\n' ->
157-
if !line = !line_start && !line = !line_end then begin
158-
(* loc is on one line: underline location *)
159-
Format.fprintf ppf "@, ";
160-
for _i = !pos_at_bol to loc.loc_start.pos_cnum - 1 do
161-
Format.pp_print_char ppf ' '
162-
done;
163-
for _i = loc.loc_start.pos_cnum to loc.loc_end.pos_cnum - 1 do
164-
Format.pp_print_char ppf '^'
165-
done
166-
end;
167-
if !line >= !line_start && !line <= !line_end then begin
168-
Format.fprintf ppf "@,";
169-
if pos < loc.loc_end.pos_cnum then Format.pp_print_string ppf " "
170-
end;
171-
incr line;
172-
pos_at_bol := pos + 1
173-
| '\r' -> () (* discard *)
174-
| c ->
175-
if !line = !line_start && !line = !line_end then
176-
(* loc is on one line: print whole line *)
177-
Format.pp_print_char ppf c
178-
else if !line = !line_start then
179-
(* first line of multiline loc:
180-
print a dot for each char before loc_start *)
181-
if pos < loc.loc_start.pos_cnum then
182-
Format.pp_print_char ppf '.'
183-
else
184-
Format.pp_print_char ppf c
185-
else if !line = !line_end then
186-
(* last line of multiline loc: print a dot for each char
187-
after loc_end, even whitespaces *)
188-
if pos < loc.loc_end.pos_cnum then
189-
Format.pp_print_char ppf c
190-
else
191-
Format.pp_print_char ppf '.'
192-
else if !line > !line_start && !line < !line_end then
193-
(* intermediate line of multiline loc: print whole line *)
194-
Format.pp_print_char ppf c
195-
done;
196-
Format.fprintf ppf "@]"
197-
198-
(* Highlight the location using one of the supported modes. *)
199-
200-
let rec highlight_locations ppf locs =
201-
match !status with
202-
Terminfo.Uninitialised ->
203-
status := Terminfo.setup stdout; highlight_locations ppf locs
204-
| Terminfo.Bad_term ->
205-
begin match !input_lexbuf with
206-
None -> false
207-
| Some lb ->
208-
let norepeat =
209-
try Sys.getenv "TERM" = "norepeat" with Not_found -> false in
210-
if norepeat then false else
211-
let loc1 = List.hd locs in
212-
try highlight_dumb ppf lb loc1; true
213-
with Exit -> false
214-
end
215-
| Terminfo.Good_term num_lines ->
216-
begin match !input_lexbuf with
217-
None -> false
218-
| Some lb ->
219-
try highlight_terminfo ppf num_lines lb locs; true
220-
with Exit -> false
221-
end
77+
let num_loc_lines = ref 0 (* number of lines already printed after input *)
22278

22379
(* Print the location in some way or another *)
22480

@@ -262,13 +118,15 @@ let setup_colors () =
262118
let print_loc ppf loc =
263119
setup_colors ();
264120
let (file, line, startchar) = get_pos_info loc.loc_start in
265-
#if undefined BS_NO_COMPILER_PATCH then
121+
#if true then
266122
let startchar =
267123
if Clflags.bs_vscode then startchar + 1 else startchar in
268124
#end
269125
let endchar = loc.loc_end.pos_cnum - loc.loc_start.pos_cnum + startchar in
270126
if file = "//toplevel//" then begin
127+
#if false then
271128
if highlight_locations ppf [loc] then () else
129+
#end
272130
fprintf ppf "Characters %i-%i"
273131
loc.loc_start.pos_cnum loc.loc_end.pos_cnum
274132
end else begin
@@ -281,9 +139,12 @@ let print_loc ppf loc =
281139

282140
let default_printer ppf loc =
283141
setup_colors ();
142+
#if false then
284143
if loc.loc_start.pos_fname = "//toplevel//"
285-
&& highlight_locations ppf [loc] then ()
286-
else fprintf ppf "@{<loc>%a@}%s@," print_loc loc msg_colon
144+
&& highlight_locations ppf [loc] then ()
145+
else
146+
#end
147+
fprintf ppf "@{<loc>%a@}%s@," print_loc loc msg_colon
287148
;;
288149

289150
let printer = ref default_printer
@@ -298,9 +159,12 @@ let print_error_prefix ppf =
298159
;;
299160

300161
let print_compact ppf loc =
162+
#if false then
301163
if loc.loc_start.pos_fname = "//toplevel//"
302164
&& highlight_locations ppf [loc] then ()
303-
else begin
165+
else
166+
#end
167+
begin
304168
let (file, line, startchar) = get_pos_info loc.loc_start in
305169
let endchar = loc.loc_end.pos_cnum - loc.loc_start.pos_cnum + startchar in
306170
fprintf ppf "%a:%i" print_filename file line;
@@ -336,8 +200,8 @@ let default_warning_printer loc ppf w =
336200

337201
let warning_printer = ref default_warning_printer ;;
338202

339-
let print_warning loc ppf w =
340-
print_updating_num_loc_lines ppf (!warning_printer loc) w
203+
let print_warning loc ppf w =
204+
!warning_printer loc ppf w
341205
;;
342206

343207
let formatter_for_warnings = ref err_formatter;;
@@ -413,29 +277,16 @@ let error_of_exn exn =
413277
in
414278
loop !error_of_exn
415279

416-
let rec default_error_reporter ppf ({loc; msg; sub; if_highlight} as err) =
417-
let highlighted =
418-
if if_highlight <> "" && loc.loc_start.pos_fname = "//toplevel//" then
419-
let rec collect_locs locs {loc; sub; _} =
420-
List.fold_left collect_locs (loc :: locs) sub
421-
in
422-
let locs = collect_locs [] err in
423-
highlight_locations ppf locs
424-
else
425-
false
426-
in
427-
if highlighted then
428-
Format.pp_print_string ppf if_highlight
429-
else begin
280+
281+
let rec default_error_reporter ppf ({loc; msg; sub}) =
430282
fprintf ppf "@[<v>%a %s" print_error loc msg;
431283
List.iter (Format.fprintf ppf "@,@[<2>%a@]" default_error_reporter) sub;
432284
fprintf ppf "@]"
433-
end
434-
285+
435286
let error_reporter = ref default_error_reporter
436287

437288
let report_error ppf err =
438-
print_updating_num_loc_lines ppf !error_reporter err
289+
!error_reporter ppf err
439290
;;
440291

441292
let error_of_printer loc print x =

utils/warnings.mli

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ type t =
5050
| Wildcard_arg_to_constant_constr (* 28 *)
5151
| Eol_in_string (* 29 *)
5252
| Duplicate_definitions of string * string * string * string (* 30 *)
53+
#if undefined BS_ONLY then
5354
| Multiple_definition of string * string * string (* 31 *)
55+
#end
5456
| Unused_value_declaration of string (* 32 *)
5557
| Unused_open of string (* 33 *)
5658
| Unused_type_declaration of string (* 34 *)
@@ -70,14 +72,18 @@ type t =
7072
| Eliminated_optional_arguments of string list (* 48 *)
7173
| No_cmi_file of string * string option (* 49 *)
7274
| Bad_docstring of bool (* 50 *)
75+
#if undefined BS_ONLY then
7376
| Expect_tailcall (* 51 *)
77+
#end
7478
| Fragile_literal_pattern (* 52 *)
7579
| Misplaced_attribute of string (* 53 *)
7680
| Duplicated_attribute of string (* 54 *)
7781
| Inlining_impossible of string (* 55 *)
7882
| Unreachable_case (* 56 *)
7983
| Ambiguous_pattern of string list (* 57 *)
84+
#if undefined BS_ONLY then
8085
| No_cmx_file of string (* 58 *)
86+
#end
8187
| Assignment_to_non_mutable_value (* 59 *)
8288
| Unused_module of string (* 60 *)
8389
| Unboxable_type_in_prim_decl of string (* 61 *)
@@ -127,7 +133,8 @@ val mk_lazy: (unit -> 'a) -> 'a Lazy.t
127133
(** Like [Lazy.of_fun], but the function is applied with
128134
the warning settings at the time [mk_lazy] is called. *)
129135

130-
#if undefined BS_NO_COMPILER_PATCH then
136+
#if true then
137+
val nerrors : int ref
131138
val message : t -> string
132139
val number: t -> int
133140
val super_report :

0 commit comments

Comments
 (0)