module Pretty:Utility functions for pretty-printing. The major features provided by this module aresig
..end
fprintf
-style interface with support for user-defined printersPretty.doc
object that encodes all of the elements to be
printed
along with alignment specifiers and optional and mandatory newlinesPretty.doc
to a certain width and emit it as a string, to an
output stream or pass it to a user-defined functiontype
doc
Pretty.dprintf
function with a printf
-like interface.
The Pretty.dprintf
method is slightly slower so we do not use it for
large jobs such as the output routines for a compiler. But we use it for
small jobs such as logging and error messages.val nil : doc
val (++) : doc -> doc -> doc
val concat : doc -> doc -> doc
val text : string -> doc
val num : int -> doc
val num64 : int64 -> doc
val real : float -> doc
val chr : char -> doc
Pretty.text
with a one-character string.val line : doc
(text
"\n")
. The new line will be indented to the current indentation level,
unless you use Pretty.leftflush
right after this.val leftflush : doc
Pretty.line
to prevent the indentation. Whatever follows
next will be flushed left. Indentation resumes on the next line.val break : doc
val align : doc
val unalign : doc
val mark : doc
val unmark : doc
val indent : int -> doc -> doc
((text " ") ++ align ++ doc ++ unalign)
,
with the specified number of spaces.val markup : doc -> doc
val seq : sep:doc -> doit:('a -> doc) -> elements:'a list -> doc
sep
is a separator, doit
is a function that
converts an element to a document.val docList : ?sep:doc -> ('a -> doc) -> unit -> 'a list -> doc
unit
argument is there
to make this function more easily usable with the Pretty.dprintf
interface. The first argument is a separator, by default a comma.val d_list : string -> (unit -> 'a -> doc) -> unit -> 'a list -> doc
Pretty.dprintf
does, and itself works
in the dprintf context. Also accepts
a string as the separator since that's by far the most common.val docArray : ?sep:doc ->
(int -> 'a -> doc) -> unit -> 'a array -> doc
val docOpt : ('a -> doc) -> unit -> 'a option -> doc
'a option
with None
or Some
val d_int32 : int32 -> doc
val f_int32 : unit -> int32 -> doc
val d_int64 : int64 -> doc
val f_int64 : unit -> int64 -> doc
module MakeMapPrinter:functor (
Map
:
sig
type
key
type
'a
tval fold :(key -> 'a -> 'b -> 'b) ->
'a t -> 'b -> 'bend
) ->
sig
..end
module MakeSetPrinter:
val insert : unit -> doc -> doc
printf
-like interfaceval dprintf : ('a, unit, doc, doc) format4 -> 'a
doc
objects. The first argument for this function is a format string
argument (of type ('a, unit, doc) format
; if you insist on
understanding what that means see the module Printf
). The format string
is like that for the printf
function in C, except that it understands a
few more formatting controls, all starting with the @ character.
See the gprintf function if you want to pipe the result of dprintf into some other functions.
The following special formatting characters are understood (these do not correspond to arguments of the function):
Pretty.align
. Every format string must have matching
Pretty.align
and Pretty.unalign
. Pretty.unalign
.Pretty.line
. Just like "\n"Pretty.break
.Pretty.mark
. Pretty.unmark
.Pretty.leftflush
Should be used immediately after @! or "\n".printf
% formatting characters the following two
new characters are supported:unit -> doc
. This argument is
invoked to produce a documentunit -> 'a -> doc
and the second of type 'a
. (The extra unit
is do to the
peculiarities of the built-in support for format strings in Ocaml. It
turns out that it is not a major problem.) Here is an example of how
you use this:dprintf "Name=%s, SSN=%7d, Children=@[%a@]\n" pers.name pers.ssn (docList (chr ',' ++ break) text) pers.children
The result of dprintf
is a Pretty.doc
. You can format the document and
emit it using the functions Pretty.fprint
and Pretty.sprint
.
val gprintf : (doc -> 'a) -> ('b, unit, doc, 'a) format4 -> 'b
Pretty.dprintf
but more general. It also takes a function that is
invoked on the constructed document but before any formatting is done. The
type of the format argument means that 'a is the type of the parameters of
this function, unit is the type of the first argument to %a and %t
formats, doc is the type of the intermediate result, and 'b is the type of
the result of gprintf.val fprint : out_channel -> width:int -> doc -> unit
val sprint : width:int -> doc -> string
val fprintf : out_channel -> ('a, unit, doc) format -> 'a
val printf : ('a, unit, doc) format -> 'a
val eprintf : ('a, unit, doc) format -> 'a
val withPrintDepth : int -> (unit -> unit) -> unit
val printDepth : int ref
align
/unalign
pairs at which
everything is replaced with ellipsisval printIndent : bool ref
val fastMode : bool ref
true
then optional breaks are taken only when the document
has exceeded the given width. This means that the printout will looked
more ragged but it will be fasterval flushOften : bool ref
val flattenBeforePrint : bool ref
val countNewLines : int ref
val auto_printer : string -> 'a