1 ! Copyright (C) 2003, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
4 USING: arrays generic generic.standard assocs io kernel
5 math namespaces sequences strings io.styles io.streams.string
6 vectors words prettyprint.backend prettyprint.sections
7 prettyprint.config sorting splitting grouping math.parser vocabs
8 definitions effects classes.builtin classes.tuple io.files
9 classes continuations hashtables classes.mixin classes.union
10 classes.intersection classes.predicate classes.singleton
11 combinators quotations sets accessors colors ;
15 : make-pprint ( obj quot -- block in use )
18 H{ } clone pprinter-use set
19 V{ } clone recursion-check set
20 V{ } clone pprinter-stack set
28 : with-pprint ( obj quot -- )
29 make-pprint 2drop do-pprint ; inline
31 : pprint-vocab ( vocab -- )
32 dup vocab present-text ;
34 : write-in ( vocab -- )
35 [ \ IN: pprint-word pprint-vocab ] with-pprint ;
38 [ write-in nl ] when* ;
49 : vocabs. ( in use -- )
50 dupd remove [ { "syntax" "scratchpad" } member? not ] filter
53 : with-use ( obj quot -- )
54 make-pprint vocabs. do-pprint ; inline
56 : with-in ( obj quot -- )
57 make-pprint drop [ write-in bl ] when* do-pprint ; inline
59 : pprint ( obj -- ) [ pprint* ] with-pprint ;
61 : . ( obj -- ) pprint nl ;
63 : pprint-use ( obj -- ) [ pprint* ] with-use ;
65 : unparse ( obj -- str ) [ pprint ] with-string-writer ;
67 : unparse-use ( obj -- str ) [ pprint-use ] with-string-writer ;
69 : pprint-short ( obj -- )
76 } clone [ pprint ] bind ;
78 : unparse-short ( obj -- str )
79 [ pprint-short ] with-string-writer ;
81 : short. ( obj -- ) pprint-short nl ;
83 : .b ( n -- ) >bin print ;
84 : .o ( n -- ) >oct print ;
85 : .h ( n -- ) >hex print ;
87 : stack. ( seq -- ) [ short. ] each ;
89 : .s ( -- ) datastack stack. ;
90 : .r ( -- ) retainstack stack. ;
97 { { foreground T{ rgba f 1 1 1 1 } } { background T{ rgba f 0 0 0 1 } } }
98 "word-style" set-word-prop
100 : remove-step-into ( word -- )
101 building get [ nip pop wrapped>> ] unless-empty , ;
103 : (remove-breakpoints) ( quot -- newquot )
107 { [ dup word? not ] [ , ] }
108 { [ dup "break?" word-prop ] [ drop ] }
109 { [ dup "step-into?" word-prop ] [ remove-step-into ] }
115 : remove-breakpoints ( quot pos -- quot' )
117 1+ cut [ (remove-breakpoints) ] bi@
125 : callstack. ( callstack -- )
126 callstack>array 2 <groups> [
128 3 nesting-limit [ . ] with-variable
131 : .c ( -- ) callstack callstack. ;
133 : pprint-cell ( obj -- ) [ pprint ] with-cell ;
135 GENERIC: see ( defspec -- )
137 : comment. ( string -- )
138 [ H{ { font-style italic } } styled-text ] when* ;
140 : seeing-word ( word -- )
141 vocabulary>> pprinter-in set ;
143 : definer. ( defspec -- )
144 definer drop pprint-word ;
146 : stack-effect. ( word -- )
147 [ [ parsing-word? ] [ symbol? ] bi or not ] [ stack-effect ] bi and
148 [ effect>string comment. ] when* ;
150 : word-synopsis ( word -- )
158 M: word synopsis* word-synopsis ;
160 M: simple-generic synopsis* word-synopsis ;
162 M: standard-generic synopsis*
167 [ dispatch# pprint* ]
171 M: hook-generic synopsis*
176 [ "combination" word-prop var>> pprint* ]
180 M: method-spec synopsis*
181 first2 method synopsis* ;
183 M: method-body synopsis*
185 [ "method-class" word-prop pprint-word ]
186 [ "method-generic" word-prop pprint-word ] tri ;
188 M: mixin-instance synopsis*
190 [ class>> pprint-word ]
191 [ mixin>> pprint-word ] tri ;
193 M: pathname synopsis* pprint* ;
195 : synopsis ( defspec -- str )
199 [ synopsis* ] with-in
200 ] with-string-writer ;
202 : synopsis-alist ( definitions -- alist )
203 [ dup synopsis swap ] { } map>assoc ;
205 : definitions. ( alist -- )
206 [ write-object nl ] assoc-each ;
208 : sorted-definitions. ( definitions -- )
209 synopsis-alist sort-keys definitions. ;
211 GENERIC: declarations. ( obj -- )
213 M: object declarations. drop ;
215 : declaration. ( word prop -- )
216 tuck name>> word-prop [ pprint-word ] [ drop ] if ;
218 M: word declarations.
226 } [ declaration. ] with each ;
228 : pprint-; ( -- ) \ ; pprint-word ;
232 <block dup definition pprint-elements block>
233 dup definer nip [ pprint-word ] when* declarations.
237 [ (see) ] with-use nl ;
239 GENERIC: see-class* ( word -- )
241 M: union-class see-class*
242 <colon \ UNION: pprint-word
244 members pprint-elements pprint-; block> ;
246 M: intersection-class see-class*
247 <colon \ INTERSECTION: pprint-word
249 participants pprint-elements pprint-; block> ;
251 M: mixin-class see-class*
252 <block \ MIXIN: pprint-word
253 dup pprint-word <block
256 \ INSTANCE: pprint-word pprint-word pprint-word
257 ] with each block> block> ;
259 M: predicate-class see-class*
260 <colon \ PREDICATE: pprint-word
263 dup superclass pprint-word
265 "predicate-definition" word-prop pprint-elements
266 pprint-; block> block> ;
268 M: singleton-class see-class* ( class -- )
269 \ SINGLETON: pprint-word pprint-word ;
271 GENERIC: pprint-slot-name ( object -- )
273 M: string pprint-slot-name text ;
275 M: array pprint-slot-name
276 <flow \ { pprint-word
277 f <inset unclip text pprint-elements block>
278 \ } pprint-word block> ;
280 : unparse-slot ( slot-spec -- array )
283 dup class>> object eq? [
294 : pprint-slot ( slot-spec -- )
296 dup length 1 = [ first ] when
299 M: tuple-class see-class*
300 <colon \ TUPLE: pprint-word
302 dup superclass tuple eq? [
303 "<" text dup superclass pprint-word
305 <block "slots" word-prop [ pprint-slot ] each block>
308 M: word see-class* drop ;
310 M: builtin-class see-class*
311 drop "! Built-in class" comment. ;
313 : see-class ( class -- )
316 dup seeing-word dup see-class*
322 dup class? over symbol? not and [
325 dup class? over symbol? and not [
326 [ dup (see) ] with-use nl
331 natural-sort [ nl ] [ see ] interleave ;
333 : (see-implementors) ( class -- seq )
334 dup implementors [ method ] with map natural-sort ;
336 : (see-methods) ( generic -- seq )
337 "methods" word-prop values natural-sort ;
339 : see-methods ( word -- )
341 dup class? [ dup (see-implementors) % ] when
342 dup generic? [ dup (see-methods) % ] when
344 ] { } make prune see-all ;