@@ -156,7 +156,7 @@ let print_loc ppf (loc : t) =
156
156
fprintf ppf " @{<filename>%a@}%a" print_filename loc.loc_start.pos_fname dim_loc normalized_range
157
157
;;
158
158
159
- let print ~message_kind intro ppf (loc : t ) =
159
+ let print ?( src = None ) ~message_kind intro ppf (loc : t ) =
160
160
begin match message_kind with
161
161
| `warning -> fprintf ppf " @[@{<info>%s@}@]@," intro
162
162
| `warning_as_error -> fprintf ppf " @[@{<error>%s@} (configured as error) @]@," intro
@@ -193,7 +193,12 @@ let print ~message_kind intro ppf (loc : t) =
193
193
| None -> ()
194
194
| Some _ -> begin
195
195
try
196
- let src = Ext_io. load_file file in
196
+ (* Print a syntax error that is a list of Res_diagnostics.t.
197
+ Instead of reading file for every error, it uses the source that the parser already has. *)
198
+ let src = match src with
199
+ | Some src -> src
200
+ | None -> Ext_io. load_file file
201
+ in
197
202
(* we're putting the line break `@,` here rather than above, because this
198
203
branch might not be reached (aka no inline file content display) so
199
204
we don't wanna end up with two line breaks in the the consequent *)
@@ -329,17 +334,22 @@ let error_of_exn exn =
329
334
330
335
(* taken from https://github.com/rescript-lang/ocaml/blob/d4144647d1bf9bc7dc3aadc24c25a7efa3a67915/parsing/location.ml#L380 *)
331
336
(* This is the error report entry point. We'll replace the default reporter with this one. *)
332
- let rec default_error_reporter ppf ({loc; msg; sub} ) =
337
+ let rec default_error_reporter ?( src = None ) ppf ({loc; msg; sub} ) =
333
338
setup_colors () ;
334
339
(* open a vertical box. Everything in our message is indented 2 spaces *)
335
- Format. fprintf ppf " @[<v>@, %a@, %s@,@]" (print ~message_kind: `error " We've found a bug for you!" ) loc msg;
336
- List. iter (Format. fprintf ppf " @,@[%a@]" default_error_reporter) sub
340
+ (* If src is given, it will display a syntax error after parsing. *)
341
+ let intro = match src with
342
+ | Some _ -> " Syntax error!"
343
+ | None -> " We've found a bug for you!"
344
+ in
345
+ Format. fprintf ppf " @[<v>@, %a@, %s@,@]" (print ~src ~message_kind: `error intro) loc msg;
346
+ List. iter (Format. fprintf ppf " @,@[%a@]" (default_error_reporter ~src )) sub
337
347
(* no need to flush here; location's report_exception (which uses this ultimately) flushes *)
338
348
339
349
let error_reporter = ref default_error_reporter
340
350
341
- let report_error ppf err =
342
- ! error_reporter ppf err
351
+ let report_error ?( src = None ) ppf err =
352
+ ! error_reporter ~src ppf err
343
353
;;
344
354
345
355
let error_of_printer loc print x =
@@ -375,7 +385,7 @@ let rec report_exception_rec n ppf exn =
375
385
match error_of_exn exn with
376
386
| None -> reraise exn
377
387
| Some `Already_displayed -> ()
378
- | Some (`Ok err ) -> fprintf ppf " @[%a@]@." report_error err
388
+ | Some (`Ok err ) -> fprintf ppf " @[%a@]@." ( report_error ~src: None ) err
379
389
with exn when n > 0 -> report_exception_rec (n-1 ) ppf exn
380
390
381
391
let report_exception ppf exn = report_exception_rec 5 ppf exn
0 commit comments