Pretty printing of documents.
A document is a structured tree of text with formatting instructions.
It can be rendered into a string ("pretty printed"), see
This follows Wadler's paper "A prettier printer", but with some changes in the rendering part because we can't rely on lazyness to make the algebraic implementation efficient.
Some general considerations: the type
t is the type of documents, a tree with text leaves that is pretty printed within a given width.
Layout is controlled via the combination of a few primitives:
newlinewill either print a space or a newline. It is similar to
"@ "in that sense. A big difference with
Formatis that by default
newlineis actually a newline. It only becomes a space if it's in a
groupsmall enough to fit in the remainder of the current line.
group dtries to write
don a single line if there's room. If not, it has no effect.
nest n dincreases the indentation level inside
d. Any newline that is rendered as a new line is indented by
nmore spaces (which are cumulative with surrounding
append a b(or
a ^ b) just prints
fill dis a bit like
groupbut it will try to cram as much as possible on each line. It is not all-or-nothing like
val nil : t
val char : char -> t
Text, with a
textpf "%d-%d" 4 2 is like
nest n d increases indentation by
d. If current indentation is
m, then every newline inside
d will be followed by
n + m leading spaces.
Group the documents inside this.
Newlines immediately inside this group will either render as new lines or as spaces, depending on the width available.
val newline : t
A line break.
fill sep l resembles
group (append_l ~sep l), except it tries to put as many items of
l as possible on each line.
In terms of
Format, this is like the hov box.
module Out : sig ... end
module Ext : sig ... end
ext e v d wraps
d with value
It is a document that has the same shape (and size) as
d, except that additional data will be output when it is rendered using extension
When this is rendered, first
e.pre out v is called; then
d is printed; then
e.post out v is called. Here
out is the output buffer/stream for rendering.
module Pretty : sig ... end
module Flatten : sig ... end
Trivial printing, on a single line.
module Infix : sig ... end
val sp : t
A single space
append_l ?sep l is the concatenation of elements of
l, separated by
val bool : bool -> t
val int : int -> t
val float : float -> t
val float_hex : float -> t
val text_quoted : string -> t
text_quoted s is
text (spf "%S" s)
val text_zero_width : string -> t
Text but we assume it takes no space on screen.
of_list f l maps each element of
l to a document and concatenates them.
bracket2 l d r groups
d, indented by 2, between brackets
module Dump : sig ... end
Printers that correspond closely to OCaml's syntax.
module Term_color : sig ... end
Simple colors in terminals