module Flx_cil_pretty:Flx_cil_utility functions for pretty-printing. The major features provided by this module aresig
..end
fprintf
-style interface with support for user-defined printersFlx_cil_pretty.doc
object that encodes all of the elements to be
printed
along with alignment specifiers and optional and mandatory newlinesFlx_cil_pretty.doc
to a certain width and emit it as a string, to an
output stream or pass it to a user-defined functiontype
doc
Flx_cil_pretty.dprintf
function with a printf
-like interface.
* The Flx_cil_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 text : string -> doc
val num : int -> doc
val real : float -> doc
val chr : char -> doc
Flx_cil_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 Flx_cil_pretty.leftflush
right after this.val leftflush : doc
Flx_cil_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 : doc ->
('a -> doc) -> unit -> 'a list -> doc
unit
argument is there
* to make this function more easily usable with the Flx_cil_pretty.dprintf
* interface.val d_list : string ->
(unit -> 'a -> doc) -> unit -> 'a list -> doc
Flx_cil_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 : doc ->
(int -> 'a -> doc) -> unit -> 'a array -> doc
val docOpt : (unit -> 'a -> doc) -> unit -> 'a option -> doc
'a option
with None
or Some
val insert : unit -> doc -> doc
printf
-like interfaceval dprintf : ('a, unit, doc) Pervasives.format -> '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.
The following special formatting characters are understood (these do not correspond to arguments of the function):
Flx_cil_pretty.align
. Every format string must have matching
Flx_cil_pretty.align
and Flx_cil_pretty.unalign
.Flx_cil_pretty.unalign
.Flx_cil_pretty.line
. Just like "\n"Flx_cil_pretty.break
.Flx_cil_pretty.mark
.Flx_cil_pretty.unmark
.Flx_cil_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 Flx_cil_pretty.doc
. You can format the document and
emit it using the functions Flx_cil_pretty.fprint
and Flx_cil_pretty.sprint
.
val fprint : Pervasives.out_channel -> width:int -> doc -> unit
val sprint : width:int -> doc -> string
val fprintf : Pervasives.out_channel ->
('a, unit, doc) Pervasives.format -> 'a
val printf : ('a, unit, doc) Pervasives.format -> 'a
val eprintf : ('a, unit, doc) Pervasives.format -> 'a
val gprintf : (doc -> doc) ->
('a, unit, doc) Pervasives.format -> 'a
Flx_cil_pretty.dprintf
but more general. It also takes a function that is
* invoked on the constructed document but before any formatting is done.val withPrintDepth : int -> (unit -> unit) -> unit
val printDepth : int Pervasives.ref
align
/unalign
pairs at which
everything is replaced with ellipsisval printIndent : bool Pervasives.ref
val fastMode : bool Pervasives.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 Pervasives.ref
val countNewLines : int Pervasives.ref