]> gitweb.factorcode.org Git - factor.git/blob - basis/prettyprint/prettyprint-docs.factor
Cleaning up USING: lists for new strict semantics
[factor.git] / basis / prettyprint / prettyprint-docs.factor
1 USING: prettyprint.backend prettyprint.config prettyprint.custom
2 prettyprint.sections help.markup help.syntax
3 io kernel words definitions quotations strings generic classes
4 prettyprint.private ;
5 IN: prettyprint
6
7 ARTICLE: "prettyprint-numbers" "Prettyprinting numbers"
8 "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."
9 { $subsection .b }
10 { $subsection .o }
11 { $subsection .h } ;
12
13 ARTICLE: "prettyprint-stacks" "Prettyprinting stacks"
14 "Prettyprinting the current data, retain, call stacks:"
15 { $subsection .s }
16 { $subsection .r }
17 { $subsection .c }
18 "Prettyprinting any stack:"
19 { $subsection stack. }
20 "Prettyprinting any call stack:"
21 { $subsection callstack. }
22 "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
24 ARTICLE: "prettyprint-variables" "Prettyprint control variables"
25 "The following variables affect the " { $link . } " and " { $link pprint } " words if set in the current dynamic scope:"
26 { $subsection tab-size }
27 { $subsection margin }
28 { $subsection nesting-limit }
29 { $subsection length-limit }
30 { $subsection line-limit }
31 { $subsection string-limit? }
32 { $subsection boa-tuples? }
33 "Note that the " { $link short. } " and " { $link pprint-short } " variables override some of these variables."
34 {
35     $warning "Treat the global variables as essentially being constants. Only ever rebind them in a nested scope."
36     $nl
37     "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."
38 } ;
39
40 ARTICLE: "prettyprint-limitations" "Prettyprinter limitations"
41 "When using the prettyprinter as a serialization mechanism, keep the following points in mind:"
42 { $list
43     { "When printing words, " { $link POSTPONE: USING: } " declarations are only output if the " { $link pprint-use } " or " { $link unparse-use } "  words are used." }
44     { "Long output will be truncated if certain " { $link "prettyprint-variables" } " are set." }
45     "Shared structure is not reflected in the printed output; if the output is parsed back in, fresh objects are created for all literal denotations."
46     { "Circular structure is not printed in a readable way. For example, try this:"
47         { $code "{ f } dup dup set-first ." }
48     }
49     "Floating point numbers might not equal themselves after being printed and read, since a decimal representation of a float is inexact."
50 }
51 "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
53 ARTICLE: "prettyprint-section-protocol" "Prettyprinter section protocol"
54 "Prettyprinter sections must subclass " { $link section } ", and they must also obey a protocol."
55 $nl
56 "Layout queries:"
57 { $subsection section-fits? }
58 { $subsection indent-section? }
59 { $subsection unindent-first-line? }
60 { $subsection newline-after? }
61 { $subsection short-section? }
62 "Printing sections:"
63 { $subsection short-section }
64 { $subsection long-section }
65 "Utilities to use when implementing sections:"
66 { $subsection new-section }
67 { $subsection new-block }
68 { $subsection add-section } ;
69
70 ARTICLE: "prettyprint-sections" "Prettyprinter sections"
71 "The prettyprinter's formatting engine can be used directly:"
72 { $subsection with-pprint }
73 "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 $nl
75 "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."
76 { $subsection section }
77 "Adding leaf sections:"
78 { $subsection line-break }
79 { $subsection text }
80 { $subsection styled-text }
81 "Nesting and denesting sections:"
82 { $subsection <object }
83 { $subsection <block }
84 { $subsection <inset }
85 { $subsection <flow }
86 { $subsection <colon }
87 { $subsection block> }
88 "New types of sections can be defined."
89 { $subsection "prettyprint-section-protocol" } ;
90
91 ARTICLE: "prettyprint-literal" "Literal prettyprinting protocol"
92 "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:"
93 { $subsection pprint-delims }
94 { $subsection >pprint-sequence }
95 "For example, consider the following data type, together with a parsing word for creating literals:"
96 { $code
97     "TUPLE: rect w h ;"
98     ""
99     "SYNTAX: RECT["
100     "    scan-word"
101     "    scan-word \\ * assert="
102     "    scan-word"
103     "    scan-word \\ ] assert="
104     "    <rect> parsed ;"
105 }
106 "An example literal might be:"
107 { $code "RECT[ 100 * 200 ]" }
108 "Without further effort, the literal does not print in the same way:"
109 { $unchecked-example "RECT[ 100 * 200 ] ." "T{ rect f 100 200 }" }
110 "However, we can define three methods easily enough:"
111 { $code
112     "M: rect pprint-delims drop \\ RECT[ \\ ] ;"
113     "M: rect >pprint-sequence dup rect-w \\ * rot rect-h 3array ;"
114     "M: rect pprint* pprint-object ;"
115 }
116 "Now, it will be printed in a custom way:"
117 { $unchecked-example "RECT[ 100 * 200 ] ." "RECT[ 100 * 200 ]" } ;
118
119 ARTICLE: "prettyprint-literal-more" "Prettyprinting more complex literals"
120 "If the " { $link "prettyprint-literal" } " is insufficient, a method can be defined to control prettyprinting directly:"
121 { $subsection pprint* }
122 "Some utilities which can be called from methods on " { $link pprint* } ":"
123 { $subsection pprint-object }
124 { $subsection pprint-word }
125 { $subsection pprint-elements }
126 { $subsection pprint-string }
127 { $subsection pprint-prefix }
128 "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
130 ARTICLE: "prettyprint-extension" "Extending the prettyprinter"
131 "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."
132 { $subsection "prettyprint-literal" }
133 { $subsection "prettyprint-literal-more" }
134 "The prettyprinter actually exposes a general source code output engine and is not limited to printing object structure."
135 { $subsection "prettyprint-sections" } ;
136
137 ARTICLE: "prettyprint" "The prettyprinter"
138 "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 $nl
140 "Prettyprinter words are found in the " { $vocab-link "prettyprint" } " vocabulary."
141 $nl
142 "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 . }
144 { $subsection short. }
145 { $subsection pprint }
146 { $subsection pprint-short }
147 { $subsection pprint-use }
148 "The string representation of an object can be requested:"
149 { $subsection unparse }
150 { $subsection unparse-use }
151 "Utility for tabular output:"
152 { $subsection pprint-cell }
153 "More prettyprinter usage:"
154 { $subsection "prettyprint-numbers" }
155 { $subsection "prettyprint-stacks" }
156 "Prettyprinter customization:"
157 { $subsection "prettyprint-variables" }
158 { $subsection "prettyprint-extension" }
159 { $subsection "prettyprint-limitations" }
160 { $see-also "number-strings" "see" } ;
161
162 ABOUT: "prettyprint"
163
164 HELP: with-pprint
165 { $values { "obj" object } { "quot" quotation } }
166 { $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 } "." } ;
167
168 HELP: pprint
169 { $values { "obj" object } }
170 { $description "Prettyprints an object to " { $link output-stream } ". Output is influenced by many variables; see " { $link "prettyprint-variables" } "." }
171 { $warning
172     "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."
173 } ;
174
175 { pprint pprint* with-pprint } related-words
176
177 HELP: .
178 { $values { "obj" object } }
179 { $description "Prettyprints an object to " { $link output-stream } " with a trailing line break. Output is influenced by many variables; see " { $link "prettyprint-variables" } "." }
180 { $warning
181     "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."
182 } ;
183
184 HELP: unparse
185 { $values { "obj" object } { "str" "Factor source string" } }
186 { $description "Outputs a prettyprinted string representation of an object. Output is influenced by many variables; see " { $link "prettyprint-variables" } "." }
187 { $warning
188     "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."
189 } ;
190
191 HELP: pprint-short
192 { $values { "obj" object } }
193 { $description "Prettyprints an object to " { $link output-stream } ". This word rebinds printer control variables to enforce “shorter” output. See " { $link "prettyprint-variables" } "." } ;
194
195 HELP: short.
196 { $values { "obj" object } }
197 { $description "Prettyprints an object to " { $link output-stream } " with a trailing line break. This word rebinds printer control variables to enforce “shorter” output." } ;
198
199 HELP: .b
200 { $values { "n" "an integer" } }
201 { $description "Outputs an integer in binary." } ;
202
203 HELP: .o
204 { $values { "n" "an integer" } }
205 { $description "Outputs an integer in octal." } ;
206
207 HELP: .h
208 { $values { "n" "an integer" } }
209 { $description "Outputs an integer in hexadecimal." } ;
210
211 HELP: stack.
212 { $values { "seq" "a sequence" } }
213 { $description "Prints a the elements of the sequence, one per line." }
214 { $notes "This word is used in the implementation of " { $link .s } " and " { $link .r } "." } ;
215
216 HELP: callstack.
217 { $values { "callstack" callstack } }
218 { $description "Displays a sequence output by " { $link callstack } " in a nice way, by highlighting the current execution point in every call frame with " { $link -> } "." } ;
219
220 HELP: .c
221 { $description "Displays the contents of the call stack, with the top of the stack printed first." } ;
222
223 HELP: .r
224 { $description "Displays the contents of the retain stack, with the top of the stack printed first." } ;
225
226 HELP: .s
227 { $description "Displays the contents of the data stack, with the top of the stack printed first." } ;
228
229 HELP: in.
230 { $values { "vocab" "a vocabulary specifier" } }
231 { $description "Prettyprints a " { $snippet "IN:" } " declaration." }
232 $prettyprinting-note ;