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