Add delayed errors in Message

This commit is contained in:
vbot 2024-06-17 15:36:38 +02:00
parent 01b03b69a0
commit 421d281fc1
No known key found for this signature in database
GPG Key ID: A102739F983C6C72
2 changed files with 42 additions and 2 deletions

View File

@ -327,6 +327,7 @@ open Content
(** {1 Error exception} *) (** {1 Error exception} *)
exception CompilerError of Content.t exception CompilerError of Content.t
exception CompilerErrors of Content.t list
(** {1 Error printing} *) (** {1 Error printing} *)
@ -404,3 +405,31 @@ let result = make ~level:Result ~cont:emit
let results r = emit (List.flatten (List.map of_result r)) Result let results r = emit (List.flatten (List.map of_result r)) Result
let warning = make ~level:Warning ~cont:emit let warning = make ~level:Warning ~cont:emit
let error = make ~level:Error ~cont:(fun m _ -> raise (CompilerError m)) let error = make ~level:Error ~cont:(fun m _ -> raise (CompilerError m))
(* Multiple errors handling *)
let global_errors = ref None
let delayed_error x =
make ~level:Error ~cont:(fun m _ ->
match !global_errors with
| None ->
failwith
"delayed error called outside scope: encapsulate using \
'with_delayed_errors' first"
| Some l ->
global_errors := Some (m :: l);
x)
let with_delayed_errors (f : unit -> 'a) : 'a =
(match !global_errors with
| None -> global_errors := Some []
| Some _ -> failwith "delayed error scope already initialized");
let r = f () in
match !global_errors with
| None -> assert false
| Some [] ->
global_errors := None;
r
| Some errs ->
global_errors := None;
raise (CompilerErrors (List.rev errs))

View File

@ -60,9 +60,10 @@ end
(** This functions emits the message according to the emission type defined by (** This functions emits the message according to the emission type defined by
[Cli.message_format_flag]. *) [Cli.message_format_flag]. *)
(** {1 Error exception} *) (** {1 Error exceptions} *)
exception CompilerError of Content.t exception CompilerError of Content.t
exception CompilerErrors of Content.t list
(** {1 Some formatting helpers}*) (** {1 Some formatting helpers}*)
@ -98,5 +99,15 @@ val log : ('a, unit) emitter
val debug : ('a, unit) emitter val debug : ('a, unit) emitter
val result : ('a, unit) emitter val result : ('a, unit) emitter
val warning : ('a, unit) emitter val warning : ('a, unit) emitter
val error : ('a, 'b) emitter val error : ('a, 'exn) emitter
val results : Content.message list -> unit val results : Content.message list -> unit
(** Multiple errors *)
val with_delayed_errors : (unit -> 'a) -> 'a
(** [with_delayed_errors f] calls [f] and registers each error triggered using
[delayed_error].
@raise CompilerErrors when delayed errors were registered. *)
val delayed_error : 'b -> ('a, 'b) emitter