package ocamlformat

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
include module type of Ocaml_common.Location
type t = Ocaml_common.Warnings.loc = {
  1. loc_start : Stdlib.Lexing.position;
  2. loc_end : Stdlib.Lexing.position;
  3. loc_ghost : bool;
}

Note on the use of Lexing.position in this module. If pos_fname = "", then use !input_name instead. If pos_lnum = -1, then pos_bol = 0. Use pos_cnum and re-parse the file to get the line and character numbers. Else all fields are correct.

val none : t

An arbitrary value of type t; describes an empty ghost range.

val is_none : t -> bool

True for Location.none, false any other location

val in_file : string -> t

Return an empty ghost range located in a given file.

val init : Stdlib.Lexing.lexbuf -> string -> unit

Set the file name and line number of the lexbuf to be the start of the named file.

val curr : Stdlib.Lexing.lexbuf -> t

Get the location of the current token from the lexbuf.

val symbol_rloc : unit -> t
val symbol_gloc : unit -> t
val rhs_loc : int -> t

rhs_loc n returns the location of the symbol at position n, starting at 1, in the current parser rule.

val rhs_interval : int -> int -> t
val get_pos_info : Stdlib.Lexing.position -> string * int * int

file, line, char

type 'a loc = {
  1. txt : 'a;
  2. loc : t;
}
val mknoloc : 'a -> 'a loc
val mkloc : 'a -> t -> 'a loc

Input info

val input_name : string Stdlib.ref
val input_lexbuf : Stdlib.Lexing.lexbuf option Stdlib.ref
val input_phrase_buffer : Stdlib.Buffer.t option Stdlib.ref

Toplevel-specific functions

val echo_eof : unit -> unit
val reset : unit -> unit

Printing locations

val rewrite_absolute_path : string -> string

rewrite absolute path to honor the BUILD_PATH_PREFIX_MAP variable (https://reproducible-builds.org/specs/build-path-prefix-map/) if it is set.

val absolute_path : string -> string
val show_filename : string -> string

In -absname mode, return the absolute path for this filename. Otherwise, returns the filename unchanged.

val print_filename : Stdlib.Format.formatter -> string -> unit
val print_loc : Stdlib.Format.formatter -> t -> unit
val print_locs : Stdlib.Format.formatter -> t list -> unit

Toplevel-specific location highlighting

val highlight_terminfo : Stdlib.Lexing.lexbuf -> Stdlib.Format.formatter -> t list -> unit

Reporting errors and warnings

The type of reports and report printers

type msg = (Stdlib.Format.formatter -> unit) loc
val msg : ?loc:t -> ('a, Stdlib.Format.formatter, unit, msg) Stdlib.format4 -> 'a
type report_kind =
  1. | Report_error
  2. | Report_warning of string
  3. | Report_warning_as_error of string
  4. | Report_alert of string
  5. | Report_alert_as_error of string
type report = {
  1. kind : report_kind;
  2. main : msg;
  3. sub : msg list;
}
type report_printer = {
  1. pp : report_printer -> Stdlib.Format.formatter -> report -> unit;
  2. pp_report_kind : report_printer -> report -> Stdlib.Format.formatter -> report_kind -> unit;
  3. pp_main_loc : report_printer -> report -> Stdlib.Format.formatter -> t -> unit;
  4. pp_main_txt : report_printer -> report -> Stdlib.Format.formatter -> (Stdlib.Format.formatter -> unit) -> unit;
  5. pp_submsgs : report_printer -> report -> Stdlib.Format.formatter -> msg list -> unit;
  6. pp_submsg : report_printer -> report -> Stdlib.Format.formatter -> msg -> unit;
  7. pp_submsg_loc : report_printer -> report -> Stdlib.Format.formatter -> t -> unit;
  8. pp_submsg_txt : report_printer -> report -> Stdlib.Format.formatter -> (Stdlib.Format.formatter -> unit) -> unit;
}

A printer for reports, defined using open-recursion. The goal is to make it easy to define new printers by re-using code from existing ones.

Report printers used in the compiler

val batch_mode_printer : report_printer
val terminfo_toplevel_printer : Stdlib.Lexing.lexbuf -> report_printer
val best_toplevel_printer : unit -> report_printer

Detects the terminal capabilities and selects an adequate printer

Printing a report

val print_report : Stdlib.Format.formatter -> report -> unit

Display an error or warning report.

val report_printer : (unit -> report_printer) Stdlib.ref

Hook for redefining the printer of reports.

The hook is a unit -> report_printer and not simply a report_printer: this is useful so that it can detect the type of the output (a file, a terminal, ...) and select a printer accordingly.

val default_report_printer : unit -> report_printer

Original report printer for use in hooks.

Reporting warnings

Converting a Warnings.t into a report

val report_warning : t -> Ocaml_common.Warnings.t -> report option

report_warning loc w produces a report for the given warning w, or None if the warning is not to be printed.

val warning_reporter : (t -> Ocaml_common.Warnings.t -> report option) Stdlib.ref

Hook for intercepting warnings.

val default_warning_reporter : t -> Ocaml_common.Warnings.t -> report option

Original warning reporter for use in hooks.

Printing warnings

val formatter_for_warnings : Stdlib.Format.formatter Stdlib.ref
val print_warning : t -> Stdlib.Format.formatter -> Ocaml_common.Warnings.t -> unit

Prints a warning. This is simply the composition of report_warning and print_report.

val prerr_warning : t -> Ocaml_common.Warnings.t -> unit

Same as print_warning, but uses !formatter_for_warnings as output formatter.

Reporting alerts

Converting an Alert.t into a report

val report_alert : t -> Ocaml_common.Warnings.alert -> report option

report_alert loc w produces a report for the given alert w, or None if the alert is not to be printed.

val alert_reporter : (t -> Ocaml_common.Warnings.alert -> report option) Stdlib.ref

Hook for intercepting alerts.

val default_alert_reporter : t -> Ocaml_common.Warnings.alert -> report option

Original alert reporter for use in hooks.

Printing alerts

val print_alert : t -> Stdlib.Format.formatter -> Ocaml_common.Warnings.alert -> unit

Prints an alert. This is simply the composition of report_alert and print_report.

val prerr_alert : t -> Ocaml_common.Warnings.alert -> unit

Same as print_alert, but uses !formatter_for_warnings as output formatter.

val deprecated : ?def:t -> ?use:t -> t -> string -> unit

Prints a deprecation alert.

val alert : ?def:t -> ?use:t -> kind:string -> t -> string -> unit

Prints an arbitrary alert.

Reporting errors

type error = report

An error is a report which report_kind must be Report_error.

val error : ?loc:t -> ?sub:msg list -> string -> error
val errorf : ?loc:t -> ?sub:msg list -> ('a, Stdlib.Format.formatter, unit, error) Stdlib.format4 -> 'a
val error_of_printer : ?loc:t -> ?sub:msg list -> (Stdlib.Format.formatter -> 'a -> unit) -> 'a -> error
val error_of_printer_file : (Stdlib.Format.formatter -> 'a -> unit) -> 'a -> error

Automatically reporting errors for raised exceptions

val register_error_of_exn : (exn -> error option) -> unit

Each compiler module which defines a custom type of exception which can surface as a user-visible error should register a "printer" for this exception using register_error_of_exn. The result of the printer is an error value containing a location, a message, and optionally sub-messages (each of them being located as well).

val error_of_exn : exn -> [ `Ok of error | `Already_displayed ] option
exception Error of error

Raising Error e signals an error e; the exception will be caught and the error will be printed.

exception Already_displayed_error

Raising Already_displayed_error signals an error which has already been printed. The exception will be caught, but nothing will be printed

val raise_errorf : ?loc:t -> ?sub:msg list -> ('a, Stdlib.Format.formatter, unit, 'b) Stdlib.format4 -> 'a
val report_exception : Stdlib.Format.formatter -> exn -> unit

Reraise the exception if it is unknown.

type comparator_witness
val comparator : (t, comparator_witness) Ocamlformat_stdlib.Comparator.t
val contains : t -> t -> Ocamlformat_stdlib.bool
val sexp_of_t : t -> Ocamlformat_stdlib.Sexp.t
val compare_width_decreasing : t -> t -> Ocamlformat_stdlib.int

Compare, in order:

  • start
  • end (in reverse order)
  • ghostness

Locs (start and end) are compared using Position.compare.

val compare : t -> t -> Ocamlformat_stdlib.int
val compare_start : t -> t -> Ocamlformat_stdlib.int
val compare_start_col : t -> t -> Ocamlformat_stdlib.int
val compare_end : t -> t -> Ocamlformat_stdlib.int
val compare_end_col : t -> t -> Ocamlformat_stdlib.int
val line_difference : t -> t -> Ocamlformat_stdlib.int

line_difference x y returns the difference between the line at the start of y and at the end of x. x must precede y, undefined behavior otherwise, or if one location includes the other.

val fmt : Ocamlformat_stdlib.Format.formatter -> t -> Ocamlformat_stdlib.unit
val smallest : t -> t Ocamlformat_stdlib.list -> t
val width : t -> Ocamlformat_stdlib.int
val of_lexbuf : Lexing.lexbuf -> t
val print : Ocamlformat_stdlib.Format.formatter -> t -> Ocamlformat_stdlib.unit
OCaml

Innovation. Community. Security.