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