PPrintA package of all of the above.
include module type of struct include PPrintEngine endDocuments must be built in memory before they are rendered. This may seem costly, but it is a simple approach, and works well.
The following operations form a set of basic (low-level) combinators for building documents. On top of these combinators, higher-level combinators can be defined: see PPrintCombinators.
type document = PPrintEngine.documentThis is the abstract type of documents.
The following basic (low-level) combinators allow constructing documents.
val empty : documentempty is the empty document.
val char : char -> documentchar c is a document that consists of the single character c. This character must not be a newline.
val string : string -> documentstring s is a document that consists of the string s. This string must not contain a newline.
val substring : string -> int -> int -> documentsubstring s ofs len is a document that consists of the portion of the string s delimited by the offset ofs and the length len. This portion must not contain a newline.
val fancystring : string -> int -> documentfancystring s apparent_length is a document that consists of the string s. This string must not contain a newline. The string may contain fancy characters: color escape characters, UTF-8 or multi-byte characters, etc. Thus, its apparent length (which measures how many columns the text will take up on screen) differs from its length in bytes.
val fancysubstring : string -> int -> int -> int -> documentfancysubstring s ofs len apparent_length is a document that consists of the portion of the string s delimited by the offset ofs and the length len. This portion must contain a newline. The string may contain fancy characters.
val utf8string : string -> documentutf8string s is a document that consists of the UTF-8-encoded string s. This string must not contain a newline.
utf8format format <args>... is equivalent to utf8string (Printf.sprintf format <args>...).
val hardline : documenthardline is a forced newline document. This document forces all enclosing groups to be printed in non-flattening mode. In other words, any enclosing groups are dissolved.
val blank : int -> documentblank n is a document that consists of n blank characters. A blank character is like an ordinary ASCII space character char ' ', except that blank characters that appear at the end of a line are automatically suppressed.
val space : documentspace is a synonym for blank 1. It consists of one blank character. It is therefore not equivalent to char ' '.
val break : int -> documentbreak n is a document which consists of either n blank characters, when forced to display on a single line, or a single newline character, otherwise. Note that there is no choice at this point: choices are encoded by the group combinator.
doc1 ^^ doc2 is the concatenation of the documents doc1 and doc2.
nest j doc is the document doc, in which the indentation level has been increased by j, that is, in which j blanks have been inserted after every newline character. Read this again: indentation is inserted after every newline character. No indentation is inserted at the beginning of the document.
group doc encodes a choice. If possible, then the entire document group doc is rendered on a single line. Otherwise, the group is dissolved, and doc is rendered. There might be further groups within doc, whose presence will lead to further choices being explored.
ifflat doc1 doc2 is rendered as doc1 if part of a group that can be successfully flattened, and is rendered as doc2 otherwise. Use this operation with caution. Because the pretty-printer is free to choose between doc1 and doc2, these documents should be semantically equivalent.
align doc is the document doc, in which the indentation level has been set to the current column. Thus, doc is rendered within a box whose upper left corner is the current position.
range hook doc is printed exactly like the document doc, but allows the caller to register a hook that is applied, when the document is printed, to the range occupied by this document in the output text. This offers a way of mapping positions in the output text back to (sub)documents.
module ToChannel = PPrintEngine.ToChannelThis renderer sends its output into an output channel.
module ToBuffer = PPrintEngine.ToBufferThis renderer sends its output into a memory buffer.
module ToFormatter = PPrintEngine.ToFormatterThis renderer sends its output into a formatter channel.
A width requirement is expressed as an integer, where the value max_int is reserved and represents infinity.
val infinity : requirementAn output channel is represented abstractly as an object equipped with methods for displaying one character and for displaying a substring.
class type output = object ... endThe rendering engine maintains the following internal state. Its structure is subject to change in future versions of the library. Nevertheless, it is exposed to the user who wishes to define custom documents.
type state = PPrintEngine.state = {width : int;The line width. This parameter is fixed throughout the execution of the renderer.
*)ribbon : int;The ribbon width. This parameter is fixed throughout the execution of the renderer.
*)mutable last_indent : int;The number of blanks that were printed at the beginning of the current line. This field is updated (only) when a hardline is emitted. It is used (only) to determine whether the ribbon width constraint is respected.
*)mutable line : int;The current line. This field is updated (only) when a hardline is emitted. It is not used by the pretty-printing engine itself.
*)mutable column : int;The current column. This field must be updated whenever something is sent to the output channel. It is used (only) to determine whether the width constraint is respected.
*)}A custom document is defined by implementing the following methods.
class type custom = object ... endThe function custom constructs a custom document. In other words, it converts an object of type custom to a document.
The key functions of the library are exposed, in the hope that they may be useful to authors of custom (leaf and non-leaf) documents. In the case of a leaf document, they can help perform certain basic functions; for instance, applying the function pretty to the document hardline is a simple way of printing a hardline, while respecting the indentation parameters and updating the state in a correct manner. Similarly, applying pretty to the document blank n is a simple way of printing n spaces. In the case of a non-leaf document (i.e., one which contains sub-documents), these functions are essential: they allow computing the width requirement of a sub-document and displaying a sub-document.
val requirement : document -> requirementrequirement doc computes the width requirement of the document doc. It works in constant time.
pretty output state indent flatten doc prints the document doc. See the documentation of the method pretty.
include module type of struct include PPrintCombinators endThe following constant documents consist of a single character.
val lparen : PPrintEngine.documentval rparen : PPrintEngine.documentval langle : PPrintEngine.documentval rangle : PPrintEngine.documentval lbrace : PPrintEngine.documentval rbrace : PPrintEngine.documentval lbracket : PPrintEngine.documentval rbracket : PPrintEngine.documentval squote : PPrintEngine.documentval dquote : PPrintEngine.documentval bquote : PPrintEngine.documentval semi : PPrintEngine.documentval colon : PPrintEngine.documentval comma : PPrintEngine.documentval dot : PPrintEngine.documentval sharp : PPrintEngine.documentval slash : PPrintEngine.documentval backslash : PPrintEngine.documentval equals : PPrintEngine.documentval qmark : PPrintEngine.documentval tilde : PPrintEngine.documentval at : PPrintEngine.documentval percent : PPrintEngine.documentval dollar : PPrintEngine.documentval caret : PPrintEngine.documentval ampersand : PPrintEngine.documentval star : PPrintEngine.documentval plus : PPrintEngine.documentval minus : PPrintEngine.documentval underscore : PPrintEngine.documentval bang : PPrintEngine.documentval bar : PPrintEngine.documentval precede :
PPrintEngine.document ->
PPrintEngine.document ->
PPrintEngine.documentprecede l x is l ^^ x.
val terminate :
PPrintEngine.document ->
PPrintEngine.document ->
PPrintEngine.documentterminate r x is x ^^ r.
val enclose :
PPrintEngine.document ->
PPrintEngine.document ->
PPrintEngine.document ->
PPrintEngine.documentenclose l r x is l ^^ x ^^ r.
The following combinators enclose a document within a pair of delimiters. They are partial applications of enclose. No whitespace or line break is introduced.
val squotes : PPrintEngine.document -> PPrintEngine.documentval dquotes : PPrintEngine.document -> PPrintEngine.documentval bquotes : PPrintEngine.document -> PPrintEngine.documentval braces : PPrintEngine.document -> PPrintEngine.documentval parens : PPrintEngine.document -> PPrintEngine.documentval angles : PPrintEngine.document -> PPrintEngine.documentval brackets : PPrintEngine.document -> PPrintEngine.documentval twice : PPrintEngine.document -> PPrintEngine.documenttwice doc is the document obtained by concatenating two copies of the document doc.
val repeat : int -> PPrintEngine.document -> PPrintEngine.documentrepeat n doc is the document obtained by concatenating n copies of the document doc.
val concat : PPrintEngine.document list -> PPrintEngine.documentconcat docs is the concatenation of the documents in the list docs.
val separate :
PPrintEngine.document ->
PPrintEngine.document list ->
PPrintEngine.documentseparate sep docs is the concatenation of the documents in the list docs. The separator sep is inserted between every two adjacent documents.
val concat_map :
('a -> PPrintEngine.document) ->
'a list ->
PPrintEngine.documentconcat_map f xs is equivalent to concat (List.map f xs).
val separate_map :
PPrintEngine.document ->
('a -> PPrintEngine.document) ->
'a list ->
PPrintEngine.documentseparate_map sep f xs is equivalent to separate sep (List.map f xs).
val separate2 :
PPrintEngine.document ->
PPrintEngine.document ->
PPrintEngine.document list ->
PPrintEngine.documentseparate2 sep last_sep docs is the concatenation of the documents in the list docs. The separator sep is inserted between every two adjacent documents, except between the last two documents, where the separator last_sep is used instead.
val optional :
('a -> PPrintEngine.document) ->
'a option ->
PPrintEngine.documentoptional f None is the empty document. optional f (Some x) is the document f x.
val lines : string -> PPrintEngine.document listlines s is the list of documents obtained by splitting s at newline characters, and turning each line into a document via substring. This code is not UTF-8 aware.
val arbitrary_string : string -> PPrintEngine.documentarbitrary_string s is equivalent to separate (break 1) (lines s). It is analogous to string s, but is valid even if the string s contains newline characters.
val words : string -> PPrintEngine.document listwords s is the list of documents obtained by splitting s at whitespace characters, and turning each word into a document via substring. All whitespace is discarded. This code is not UTF-8 aware.
val split : (char -> bool) -> string -> PPrintEngine.document listsplit ok s splits the string s before and after every occurrence of a character that satisfies the predicate ok. The substrings thus obtained are turned into documents, and a list of documents is returned. No information is lost: the concatenation of the documents yields the original string. This code is not UTF-8 aware.
val flow :
PPrintEngine.document ->
PPrintEngine.document list ->
PPrintEngine.documentflow sep docs separates the documents in the list docs with the separator sep and arranges for a new line to begin whenever a document does not fit on the current line. This is useful for typesetting free-flowing, ragged-right text. A typical choice of sep is break b, where b is the number of spaces that must be inserted between two consecutive words (when displayed on the same line).
val flow_map :
PPrintEngine.document ->
('a -> PPrintEngine.document) ->
'a list ->
PPrintEngine.documentflow_map sep f docs is equivalent to flow sep (List.map f docs).
val url : string -> PPrintEngine.documenturl s is a possible way of displaying the URL s. A potential line break is inserted immediately before and immediately after every slash and dot character.
val hang : int -> PPrintEngine.document -> PPrintEngine.documentval prefix :
int ->
int ->
PPrintEngine.document ->
PPrintEngine.document ->
PPrintEngine.documentprefix n b left right has the following flat layout:
left rightand the following non-flat layout:
left
rightThe parameter n controls the nesting of right (when not flat). The parameter b controls the number of spaces between left and right (when flat).
val jump : int -> int -> PPrintEngine.document -> PPrintEngine.documentjump n b right is equivalent to prefix n b empty right.
val infix :
int ->
int ->
PPrintEngine.document ->
PPrintEngine.document ->
PPrintEngine.document ->
PPrintEngine.documentinfix n b middle left right has the following flat layout:
left middle rightand the following non-flat layout:
left middle
rightThe parameter n controls the nesting of right (when not flat). The parameter b controls the number of spaces between left and middle (always) and between middle and right (when flat).
val surround :
int ->
int ->
PPrintEngine.document ->
PPrintEngine.document ->
PPrintEngine.document ->
PPrintEngine.documentsurround n b opening contents closing has the following flat layout:
opening contents closingand the following non-flat layout:
opening
contents
closingThe parameter n controls the nesting of contents (when not flat). The parameter b controls the number of spaces between opening and contents and between contents and closing (when flat).
val soft_surround :
int ->
int ->
PPrintEngine.document ->
PPrintEngine.document ->
PPrintEngine.document ->
PPrintEngine.documentsoft_surround is analogous to surround, but involves more than one group, so it offers possibilities other than the completely flat layout (where opening, contents, and closing appear on a single line) and the completely developed layout (where opening, contents, and closing appear on separate lines). It tries to place the beginning of contents on the same line as opening, and to place closing on the same line as the end of contents, if possible.
val surround_separate :
int ->
int ->
PPrintEngine.document ->
PPrintEngine.document ->
PPrintEngine.document ->
PPrintEngine.document ->
PPrintEngine.document list ->
PPrintEngine.documentsurround_separate n b void opening sep closing docs is equivalent to surround n b opening (separate sep docs) closing, except when the list docs is empty, in which case it reduces to void.
val surround_separate_map :
int ->
int ->
PPrintEngine.document ->
PPrintEngine.document ->
PPrintEngine.document ->
PPrintEngine.document ->
('a -> PPrintEngine.document) ->
'a list ->
PPrintEngine.documentsurround_separate_map n b void opening sep closing f xs is equivalent to surround_separate n b void opening sep closing (List.map f xs).
val (!^) : string -> PPrintEngine.document!^s is a short-hand for string s.
val (^/^) :
PPrintEngine.document ->
PPrintEngine.document ->
PPrintEngine.documentx ^/^ y separates x and y with a breakable space. It is a short-hand for x ^^ break 1 ^^ y.
val (^//^) :
PPrintEngine.document ->
PPrintEngine.document ->
PPrintEngine.documentx ^//^ y is a short-hand for prefix 2 1 x y.
module OCaml = PPrintOCaml