USING: help io prettyprint prettyprint-internals ; ARTICLE: "prettyprint" "The prettyprinter" "One of Factor's key features is the ability to print almost any object as a valid source literal expression. This greatly aids debugging and provides the building blocks for light-weight object serialization facilities." $terpri "Prettyprinter words are found in the " { $vocab-link "prettyprint" } " vocabulary." $terpri "The key words to print an object to the " { $link stdio } " stream; the first two emit a trailing newline, the second two do not:" { $subsection . } { $subsection short. } { $subsection pprint } { $subsection pprint-short } "The string representation of an object can be requested:" { $subsection unparse } { $subsection unparse-short } "Advanced topics:" { $subsection "prettyprint-limitations" } { $subsection "prettyprint-variables" } { $subsection "prettyprint-extension" } ; ARTICLE: "prettyprint-limitations" "Prettyprinter limitations" "The prettyprinter has some limitations; namely, the following objects may not print in a readable form:" { $list { "When printing words, no " { $link POSTPONE: USE: } " declarations are output, hence the result may not be immediately readable without prefixing appropriate declarations." } "Shared structure is not reflected in the printed output; if the output is parsed back in, fresh objects are created for all literal denotations." { "Circular structure is not printed in a readable way. Circular references print as " { $snippet "#" } "." } "Floating point numbers might not equal themselves after being printed and read, since a decimal representation of a float is inexact." } "On a final note, the " { $link short. } ", " { $link pprint-short } " and " { $link unparse-short } " words restrict the length and nesting of printed sequences, their output will very likely not be valid syntax. They are only intended for interactive use." ; ARTICLE: "prettyprint-variables" "Prettyprint control variables" "The following variables affect the " { $link . } " and " { $link pprint } " words if set in the current dynamic scope:" { $subsection tab-size } { $subsection margin } { $subsection nesting-limit } { $subsection length-limit } { $subsection line-limit } { $subsection string-limit } "Note that the " { $link short. } " and " { $link pprint-short } " variables override some of these variables." { $warning "Treat the global variables as essentially being constants. Only ever rebind them in a nested scope." $terpri "Some of the globals are safe to change, like the tab size and wrap margin. However setting limits globally could break code which uses the prettyprinter as a serialization mechanism." } ; ARTICLE: "prettyprint-extension" "Extending the prettyprinter" "It is possible to define literal syntax for a new class using the " { $link "parser" } ", and then define a corresponding prettyprint method for the class which reproduces the literal syntax." $terpri "The prettyprinter maintains some internal state while prettyprinting. First, the object graph is traversed and a tree of " { $emphasis "sections" } " is produced. A section is either a text node or a " { $emphasis "block" } " which itself consists of sections." $terpri "Once the output is divided into sections, the tree is traversed and intelligent decisions are made about indentation and line breaks. If a block does not fit on the remainder of the current line, a newline is output before and after the block, and additional indentation is used when printing the block." $terpri "The following generic word is called to output a prettyprinting section for an object:" { $subsection pprint* } "Two types of leaf sections:" { $subsection text } { $subsection newline } "Nesting and denesting is done using a set words:" { $subsection } "Recall that since " { $link text } " sections take style hashtables as input, any type of formatted text can be output, including presentations. See " { $link "styles" } " to explore the possibility." ;