Module PPrintEngine
A pretty-printing engine and a set of basic document combinators.
Building documents
val empty : documentemptyis the empty document.
val char : char -> documentchar cis a document that consists of the single characterc. This character must not be a newline.
val string : string -> documentstring sis a document that consists of the strings. This string must not contain a newline.
val substring : string -> int -> int -> documentsubstring s ofs lenis a document that consists of the portion of the stringsdelimited by the offsetofsand the lengthlen. This portion must not contain a newline.
val fancystring : string -> int -> documentfancystring s apparent_lengthis a document that consists of the strings. 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_lengthis a document that consists of the portion of the stringsdelimited by the offsetofsand the lengthlen. This portion must contain a newline. The string may contain fancy characters.
val utf8string : string -> documentutf8string sis a document that consists of the UTF-8-encoded strings. This string must not contain a newline.
val hardline : documenthardlineis 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 nis a document that consists ofnblank characters.
val break : int -> documentbreak nis a document which consists of eithernblank 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 thegroupcombinator.
val (^^) : document -> document -> documentdoc1 ^^ doc2is the concatenation of the documentsdoc1anddoc2.
val nest : int -> document -> documentnest j docis the documentdoc, in which the indentation level has been increased byj, that is, in whichjblanks 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.
val group : document -> documentgroup docencodes a choice. If possible, then the entire documentgroup docis rendered on a single line. Otherwise, the group is dissolved, anddocis rendered. There might be further groups withindoc, whose presence will lead to further choices being explored.
val ifflat : document -> document -> documentifflat doc1 doc2is rendered asdoc1if part of a group that can be successfully flattened, and is rendered asdoc2otherwise. Use this operation with caution. Because the pretty-printer is free to choose betweendoc1anddoc2, these documents should be semantically equivalent.
val align : document -> documentalign docis the documentdoc, in which the indentation level has been set to the current column. Thus,docis rendered within a box whose upper left corner is the current position.
val range : (range -> unit) -> document -> documentrange hook docis printed exactly like the documentdoc, 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.
Rendering documents
module ToChannel : PPrintRenderer.RENDERER with type channel = Stdlib.out_channel and type document = documentThis renderer sends its output into an output channel.
module ToBuffer : PPrintRenderer.RENDERER with type channel = Stdlib.Buffer.t and type document = documentThis renderer sends its output into a memory buffer.
module ToFormatter : PPrintRenderer.RENDERER with type channel = Stdlib.Format.formatter and type document = documentThis renderer sends its output into a formatter channel.
Defining custom documents
val infinity : requirement
class type output = object ... endtype state={}
class type custom = object ... endval custom : custom -> documentThe function
customconstructs a custom document. In other words, it converts an object of typecustomto a document.
val requirement : document -> requirementrequirement doccomputes the width requirement of the documentdoc. It works in constant time.