Module Catala_utils.Message

Interface for emitting compiler messages.

All messages are expected to use the Format module. Flush, "@?", "@.", "%!" etc. are not supposed to be used outside of this module.

WARNING: this module performs side-effects at load time, adding support for ocolor tags (e.g. "@{<blue>text@}") to the standard string formatter used by e.g. Format.sprintf. (In this case, the tags are ignored, for color output you should use the functions of this module that toggle support depending on cli flags and terminal support).

Message content

type level =
  1. | Error
  2. | Warning
  3. | Debug
  4. | Log
  5. | Result
module Content : sig ... end

This functions emits the message according to the emission type defined by Cli.message_format_flag.

Error exceptions

exception CompilerError of Content.t
exception CompilerErrors of (Content.t * Stdlib.Printexc.raw_backtrace) list
type lsp_error_kind =
  1. | Lexing
  2. | Parsing
  3. | Typing
  4. | Generic
  5. | Warning
  6. | AssertFailure
type lsp_error = {
  1. kind : lsp_error_kind;
  2. message : Content.message;
  3. pos : Pos.t option;
  4. suggestion : string list option;
}
val register_lsp_error_notifier : (lsp_error -> unit) -> unit
val register_lsp_error_absorber : (lsp_error -> bool) -> unit

The raised error is absorbed if the hook returns false

Some formatting helpers

val unformat : (Stdlib.Format.formatter -> unit) -> string

Converts f to a string, discarding formatting and skipping newlines and indents

val has_color : Stdlib.out_channel -> bool
val set_terminal_width_function : (unit -> int) -> unit
val terminal_columns : unit -> int
val pad : int -> string -> Stdlib.Format.formatter -> unit

Prints the given character the given number of times (assuming it is of width 1)

val std_ppf : unit -> Stdlib.Format.formatter
val err_ppf : unit -> Stdlib.Format.formatter
val ignore_ppf : unit -> Stdlib.Format.formatter
val formatter_of_out_channel : ?nocolor:bool -> Stdlib.out_channel -> unit -> Stdlib.Format.formatter

Creates a new formatter from the given out channel, with correct handling of the ocolor tags. Actual use of escape codes in the output depends on Cli.style_flag -- and wether the channel is a tty if that is set to auto.

Simple interface for various message emission

type ('a, 'b) emitter = ?header:Content.message -> ?internal:bool -> ?main_pos:Pos.t -> ?pos:Pos.t -> ?pos_msg:Content.message -> ?extra_pos:(string * Pos.t) list -> ?fmt_pos:(Content.message * Pos.t) list -> ?outcome:Content.message list -> ?suggestion:string list -> ('a, Stdlib.Format.formatter, unit, 'b) Stdlib.format4 -> 'a
val log : ('a, unit) emitter
val debug : ('a, unit) emitter
val result : ('a, unit) emitter
val warning : ('a, unit) emitter
val error : ?kind:lsp_error_kind -> ('a, 'exn) emitter
val results : ?title:string -> Content.message list -> unit

Multiple errors

val report_delayed_errors_if_any : unit -> unit

report_delayed_errors_if_any checks whether some delayed errors are registered and raises the pending errors if any are present. Current registered delayed errors are also deleted.

val delayed_error : ?kind:lsp_error_kind -> 'b -> ('a, 'b) emitter
val wrap_to_delayed_error : ?kind:lsp_error_kind -> 'a -> (unit -> 'a) -> 'a

wrap_to_delayed_error ?kind dft_val f protects with a try-with the call of f and converts fatal errors (i.e., error) into delayed errors. This is useful when no good default value can be provided in a callee without heavy refactoring . The position is guessed by scanning locations from the message's content.

val combine_with_pending_errors : Content.t -> Stdlib.Printexc.raw_backtrace -> (Content.t * Stdlib.Printexc.raw_backtrace) list

combine_with_pending_errors error bt adds the given error and its backtrace bt to the current pending errors (if any) and returns the ordered list of errors to eventually emit with Content.emit_n.