package merlin-lib

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Module Merlin_utils.Format_docSource

Composable document for the Format formatting engine.

This module introduces a pure and immutable document type which represents a sequence of formatting instructions to be printed by a formatting engine at later point. At the same time, it also provides format string interpreter which produces this document type from format string and their associated printers.

The module is designed to be source compatible with code defining format printers: replacing `Format` by `Format_doc` in your code will convert `Format` printers to `Format_doc` printers.

Sourcemodule Doc : sig ... end

Definitions and immutable API for composing documents

Compatibility API

The functions and types below provides source compatibility with format printers and conversion function from Format_doc printers to Format printers. The reverse direction is implemented using an escape hatch in the formatting instruction and should only be used to preserve backward compatibility.

Sourcetype doc = Doc.t
Sourcetype t = doc
Sourcetype formatter
Sourcetype 'a printer = formatter -> 'a -> unit
Sourceval formatter : doc ref -> formatter

formatter rdoc creates a formatter that updates the rdoc reference

Sourcetype 'a format_printer = Format.formatter -> 'a -> unit

Translate a Format_doc printer to a Format one.

Sourceval compat : 'a printer -> 'a format_printer
Sourceval compat1 : ('p1 -> 'a printer) -> 'p1 -> 'a format_printer
Sourceval compat2 : ('p1 -> 'p2 -> 'a printer) -> 'p1 -> 'p2 -> 'a format_printer
Sourceval deprecated_printer : (Format.formatter -> unit) -> formatter -> unit

If necessary, embbed a Format printer inside a formatting instruction stream. This breaks every guarantees provided by Format_doc.

Sourceval deprecated : 'a format_printer -> 'a printer
Sourceval deprecated1 : ('p1 -> 'a format_printer) -> 'p1 -> 'a printer

Format string interpreters

Sourceval fprintf : formatter -> ('a, formatter, unit) format -> 'a
Sourceval kfprintf : (formatter -> 'a) -> formatter -> ('b, formatter, unit, 'a) format4 -> 'b
Sourceval asprintf : ('a, formatter, unit, string) format4 -> 'a
Sourceval kasprintf : (string -> 'a) -> ('b, formatter, unit, 'a) format4 -> 'b
Sourceval dprintf : ('a, formatter, unit, formatter -> unit) format4 -> 'a
Sourceval kdprintf : ((formatter -> unit) -> 'a) -> ('b, formatter, unit, 'a) format4 -> 'b
Sourceval doc_printf : ('a, formatter, unit, doc) format4 -> 'a

doc_printf and kdoc_printf creates a document directly

Sourceval kdoc_printf : (doc -> 'r) -> ('a, formatter, unit, 'r) format4 -> 'a

Compatibility with Doc

Sourceval doc_printer : 'a printer -> 'a Doc.printer
Sourceval pp_doc : doc printer

Source compatibility with Format

String printers

Sourceval pp_print_string : string printer
Sourceval pp_print_substring : pos:int -> len:int -> string printer
Sourceval pp_print_text : string printer
Sourceval pp_print_bytes : bytes printer
Sourceval pp_print_as : formatter -> int -> string -> unit
Sourceval pp_print_substring_as : pos:int -> len:int -> formatter -> int -> string -> unit

Primitive type printers

Sourceval pp_print_char : char printer
Sourceval pp_print_int : int printer
Sourceval pp_print_float : float printer
Sourceval pp_print_bool : bool printer
Sourceval pp_print_nothing : unit printer

Printer combinators

Sourceval pp_print_iter : ?pp_sep:unit printer -> (('a -> unit) -> 'b -> unit) -> 'a printer -> 'b printer
Sourceval pp_print_list : ?pp_sep:unit printer -> 'a printer -> 'a list printer
Sourceval pp_print_array : ?pp_sep:unit printer -> 'a printer -> 'a array printer
Sourceval pp_print_seq : ?pp_sep:unit printer -> 'a printer -> 'a Seq.t printer
Sourceval pp_print_option : ?none:unit printer -> 'a printer -> 'a option printer
Sourceval pp_print_result : ok:'a printer -> error:'e printer -> ('a, 'e) result printer
Sourceval pp_print_either : left:'a printer -> right:'b printer -> ('a, 'b) Either.t printer

Boxes and tags

Sourceval pp_open_stag : Format.stag printer
Sourceval pp_close_stag : unit printer
Sourceval pp_open_box : int printer
Sourceval pp_close_box : unit printer

Break hints

Sourceval pp_print_space : unit printer
Sourceval pp_print_cut : unit printer
Sourceval pp_print_break : formatter -> int -> int -> unit
Sourceval pp_print_custom_break : formatter -> fits:(string * int * string) as 'c -> breaks:'c -> unit

Tabulations

Sourceval pp_open_tbox : unit printer
Sourceval pp_close_tbox : unit printer
Sourceval pp_set_tab : unit printer
Sourceval pp_print_tab : unit printer
Sourceval pp_print_tbreak : formatter -> int -> int -> unit

Newlines and flushing

Sourceval pp_print_if_newline : unit printer
Sourceval pp_force_newline : unit printer
Sourceval pp_print_flush : unit printer
Sourceval pp_print_newline : unit printer

Compiler specific functions

Separators

Sourceval comma : unit printer

Compiler output

Sourceval pp_two_columns : ?sep:string -> ?max_lines:int -> formatter -> (string * string) list -> unit

pp_two_columns ?sep ?max_lines ppf l prints the lines in l as two columns separated by sep ("|" by default). max_lines can be used to indicate a maximum number of lines to print -- an ellipsis gets inserted at the middle if the input has too many lines.

Example:

pp_two_columns ~max_lines:3 Format.std_formatter [
  "abc", "hello";
  "def", "zzz";
  "a"  , "bllbl";
  "bb" , "dddddd";
]

prints

abc | hello
...
bb  | dddddd
OCaml

Innovation. Community. Security.