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