1 USING: prettyprint.backend prettyprint.config prettyprint.custom
2 prettyprint.sections prettyprint.private help.markup help.syntax
3 io kernel words definitions quotations strings generic classes ;
6 ARTICLE: "prettyprint-numbers" "Prettyprinting numbers"
7 "The " { $link . } " word prints numbers in decimal. A set of words in the " { $vocab-link "prettyprint" } " vocabulary is provided to print integers using another base."
12 ARTICLE: "prettyprint-stacks" "Prettyprinting stacks"
13 "Prettyprinting the current data, retain, call stacks:"
17 "Prettyprinting any stack:"
18 { $subsection stack. }
19 "Prettyprinting any call stack:"
20 { $subsection callstack. }
21 "Note that calls to " { $link .s } " can also be included inside words as a debugging aid, however a more convenient way to achieve this is to use the annotation facility. See " { $link "tools.annotations" } "." ;
23 ARTICLE: "prettyprint-variables" "Prettyprint control variables"
24 "The following variables affect the " { $link . } " and " { $link pprint } " words if set in the current dynamic scope:"
25 { $subsection tab-size }
26 { $subsection margin }
27 { $subsection nesting-limit }
28 { $subsection length-limit }
29 { $subsection line-limit }
30 { $subsection string-limit? }
31 { $subsection boa-tuples? }
32 "Note that the " { $link short. } " and " { $link pprint-short } " variables override some of these variables."
34 $warning "Treat the global variables as essentially being constants. Only ever rebind them in a nested scope."
36 "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."
39 ARTICLE: "prettyprint-limitations" "Prettyprinter limitations"
40 "When using the prettyprinter as a serialization mechanism, keep the following points in mind:"
42 { "When printing words, " { $link POSTPONE: USING: } " declarations are only output if the " { $link pprint-use } " or " { $link unparse-use } " words are used." }
43 { "Long output will be truncated if certain " { $link "prettyprint-variables" } " are set." }
44 "Shared structure is not reflected in the printed output; if the output is parsed back in, fresh objects are created for all literal denotations."
45 { "Circular structure is not printed in a readable way. For example, try this:"
46 { $code "{ f } dup dup set-first ." }
48 "Floating point numbers might not equal themselves after being printed and read, since a decimal representation of a float is inexact."
50 "On a final note, the " { $link short. } " and " { $link pprint-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." ;
52 ARTICLE: "prettyprint-section-protocol" "Prettyprinter section protocol"
53 "Prettyprinter sections must subclass " { $link section } ", and they must also obey a protocol."
56 { $subsection section-fits? }
57 { $subsection indent-section? }
58 { $subsection unindent-first-line? }
59 { $subsection newline-after? }
60 { $subsection short-section? }
62 { $subsection short-section }
63 { $subsection long-section }
64 "Utilities to use when implementing sections:"
65 { $subsection new-section }
66 { $subsection new-block }
67 { $subsection add-section } ;
69 ARTICLE: "prettyprint-sections" "Prettyprinter sections"
70 "The prettyprinter's formatting engine can be used directly:"
71 { $subsection with-pprint }
72 "Code in a " { $link with-pprint } " block or a method on " { $link pprint* } " can build up a tree of " { $emphasis "sections" } ". A section is either a text node or a " { $emphasis "block" } " which itself consists of sections."
74 "Once the output sections have been generated, the tree of sections is traversed and intelligent decisions are made about indentation and line breaks. Finally, text is output."
75 { $subsection section }
76 "Adding leaf sections:"
77 { $subsection line-break }
79 { $subsection styled-text }
80 "Nesting and denesting sections:"
81 { $subsection <object }
82 { $subsection <block }
83 { $subsection <inset }
85 { $subsection <colon }
86 { $subsection block> }
87 "New types of sections can be defined."
88 { $subsection "prettyprint-section-protocol" } ;
90 ARTICLE: "prettyprint-literal" "Literal prettyprinting protocol"
91 "Most custom data types have a literal syntax which resembles a sequence. An easy way to define such a syntax is to add a method to the " { $link pprint* } " generic word which calls " { $link pprint-object } ", and then to provide methods on two other generic words:"
92 { $subsection pprint-delims }
93 { $subsection >pprint-sequence }
94 "For example, consider the following data type, together with a parsing word for creating literals:"
100 " scan-word \\ * assert="
102 " scan-word \\ ] assert="
103 " <rect> parsed ; parsing"
105 "An example literal might be:"
106 { $code "RECT[ 100 * 200 ]" }
107 "Without further effort, the literal does not print in the same way:"
108 { $unchecked-example "RECT[ 100 * 200 ] ." "T{ rect f 100 200 }" }
109 "However, we can define three methods easily enough:"
111 "M: rect pprint-delims drop \\ RECT[ \\ ] ;"
112 "M: rect >pprint-sequence dup rect-w \\ * rot rect-h 3array ;"
113 "M: rect pprint* pprint-object ;"
115 "Now, it will be printed in a custom way:"
116 { $unchecked-example "RECT[ 100 * 200 ] ." "RECT[ 100 * 200 ]" } ;
118 ARTICLE: "prettyprint-literal-more" "Prettyprinting more complex literals"
119 "If the " { $link "prettyprint-literal" } " is insufficient, a method can be defined to control prettyprinting directly:"
120 { $subsection pprint* }
121 "Some utilities which can be called from methods on " { $link pprint* } ":"
122 { $subsection pprint-object }
123 { $subsection pprint-word }
124 { $subsection pprint-elements }
125 { $subsection pprint-string }
126 { $subsection pprint-prefix }
127 "Custom methods defined on " { $link pprint* } " do not perform I/O directly, instead they call prettyprinter words to construct " { $emphasis "sections" } " of output. See " { $link "prettyprint-sections" } "." ;
129 ARTICLE: "prettyprint-extension" "Extending the prettyprinter"
130 "One can define literal syntax for a new class using the " { $link "parser" } " together with corresponding prettyprinting methods which print instances of the class using this syntax."
131 { $subsection "prettyprint-literal" }
132 { $subsection "prettyprint-literal-more" }
133 "The prettyprinter actually exposes a general source code output engine and is not limited to printing object structure."
134 { $subsection "prettyprint-sections" } ;
136 ARTICLE: "prettyprint" "The prettyprinter"
137 "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."
139 "Prettyprinter words are found in the " { $vocab-link "prettyprint" } " vocabulary."
141 "The key words to print an object to " { $link output-stream } "; the first two emit a trailing newline, the second two do not:"
143 { $subsection short. }
144 { $subsection pprint }
145 { $subsection pprint-short }
146 { $subsection pprint-use }
147 "The string representation of an object can be requested:"
148 { $subsection unparse }
149 { $subsection unparse-use }
150 "Utility for tabular output:"
151 { $subsection pprint-cell }
152 "Printing a definition (see " { $link "definitions" } "):"
154 "Printing the methods defined on a generic word or class (see " { $link "objects" } "):"
155 { $subsection see-methods }
156 "More prettyprinter usage:"
157 { $subsection "prettyprint-numbers" }
158 { $subsection "prettyprint-stacks" }
159 "Prettyprinter customization:"
160 { $subsection "prettyprint-variables" }
161 { $subsection "prettyprint-extension" }
162 { $subsection "prettyprint-limitations" }
163 { $see-also "number-strings" } ;
168 { $values { "obj" object } { "quot" quotation } }
169 { $description "Sets up the prettyprinter and calls the quotation in a new scope. The quotation should add sections to the top-level block. When the quotation returns, the top-level block is printed to " { $link output-stream } "." } ;
172 { $values { "obj" object } }
173 { $description "Prettyprints an object to " { $link output-stream } ". Output is influenced by many variables; see " { $link "prettyprint-variables" } "." }
175 "Unparsing a large object can take a long time and consume a lot of memory. If you need to print large objects, use " { $link pprint-short } " or set some " { $link "prettyprint-variables" } " to limit output size."
178 { pprint pprint* with-pprint } related-words
181 { $values { "obj" object } }
182 { $description "Prettyprints an object to " { $link output-stream } " with a trailing line break. Output is influenced by many variables; see " { $link "prettyprint-variables" } "." }
184 "Printing a large object can take a long time and consume a lot of memory. If you need to print large objects, use " { $link short. } " or set some " { $link "prettyprint-variables" } " to limit output size."
188 { $values { "obj" object } { "str" "Factor source string" } }
189 { $description "Outputs a prettyprinted string representation of an object. Output is influenced by many variables; see " { $link "prettyprint-variables" } "." }
191 "Unparsing a large object can take a long time and consume a lot of memory. If you need to unparse large objects, use " { $link unparse-short } " or set some " { $link "prettyprint-variables" } " to limit output size."
195 { $values { "obj" object } }
196 { $description "Prettyprints an object to " { $link output-stream } ". This word rebinds printer control variables to enforce “shorter” output. See " { $link "prettyprint-variables" } "." } ;
199 { $values { "obj" object } }
200 { $description "Prettyprints an object to " { $link output-stream } " with a trailing line break. This word rebinds printer control variables to enforce “shorter” output." } ;
203 { $values { "n" "an integer" } }
204 { $description "Outputs an integer in binary." } ;
207 { $values { "n" "an integer" } }
208 { $description "Outputs an integer in octal." } ;
211 { $values { "n" "an integer" } }
212 { $description "Outputs an integer in hexadecimal." } ;
215 { $values { "seq" "a sequence" } }
216 { $description "Prints a the elements of the sequence, one per line." }
217 { $notes "This word is used in the implementation of " { $link .s } " and " { $link .r } "." } ;
220 { $values { "callstack" callstack } }
221 { $description "Displays a sequence output by " { $link callstack } " in a nice way, by highlighting the current execution point in every call frame with " { $link -> } "." } ;
224 { $description "Displays the contents of the call stack, with the top of the stack printed first." } ;
227 { $description "Displays the contents of the retain stack, with the top of the stack printed first." } ;
230 { $description "Displays the contents of the data stack, with the top of the stack printed first." } ;
233 { $values { "vocab" "a vocabulary specifier" } }
234 { $description "Prettyprints a " { $snippet "IN:" } " declaration." }
235 $prettyprinting-note ;
238 { $values { "defspec" "a definition specifier" } { "str" string } }
239 { $contract "Prettyprints the prologue of a definition." } ;
242 { $values { "defspec" "a definition specifier" } }
243 { $contract "Adds sections to the current block corresponding to a the prologue of a definition, in source code-like form." }
244 { $notes "This word should only be called from inside the " { $link with-pprint } " combinator. Client code should call " { $link synopsis } " instead." } ;
247 { $values { "string" "a string" } }
248 { $description "Prettyprints some text with the comment style." }
249 $prettyprinting-note ;
252 { $values { "defspec" "a definition specifier" } }
253 { $contract "Prettyprints a definition." } ;
256 { $values { "word" "a " { $link generic } " or a " { $link class } } }
257 { $contract "Prettyprints the methods defined on a generic word or class." } ;
260 { $values { "defspec" "a definition specifier" } { "start" word } { "end" "a word or " { $link f } } }
261 { $contract "Outputs the parsing words which delimit the definition." }
263 { $example "USING: definitions prettyprint ;"
265 ": foo ; \\ foo definer . ."
268 { $example "USING: definitions prettyprint ;"
270 "SYMBOL: foo \\ foo definer . ."
271 "f\nPOSTPONE: SYMBOL:"
274 { $notes "This word is used in the implementation of " { $link see } "." } ;
277 { $values { "defspec" "a definition specifier" } { "seq" "a sequence" } }
278 { $contract "Outputs the body of a definition." }
280 { $example "USING: definitions math prettyprint ;" "\\ sq definition ." "[ dup * ]" }
282 { $notes "This word is used in the implementation of " { $link see } "." } ;