1 ! Copyright (C) 2003, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays generic generic.standard assocs io kernel math
4 namespaces make sequences strings io.styles io.streams.string
5 vectors words prettyprint.backend prettyprint.sections
6 prettyprint.config sorting splitting grouping math.parser vocabs
7 definitions effects classes.builtin classes.tuple io.files
8 classes continuations hashtables classes.mixin classes.union
9 classes.intersection classes.predicate classes.singleton
10 combinators quotations sets accessors colors ;
13 : make-pprint ( obj quot -- block in use )
16 H{ } clone pprinter-use set
17 V{ } clone recursion-check set
18 V{ } clone pprinter-stack set
26 : with-pprint ( obj quot -- )
27 make-pprint 2drop do-pprint ; inline
29 : pprint-vocab ( vocab -- )
30 dup vocab present-text ;
32 : write-in ( vocab -- )
33 [ \ IN: pprint-word pprint-vocab ] with-pprint ;
36 [ write-in nl ] when* ;
47 : vocabs. ( in use -- )
48 dupd remove [ { "syntax" "scratchpad" } member? not ] filter
51 : with-use ( obj quot -- )
52 make-pprint vocabs. do-pprint ; inline
54 : with-in ( obj quot -- )
55 make-pprint drop [ write-in bl ] when* do-pprint ; inline
57 : pprint ( obj -- ) [ pprint* ] with-pprint ;
59 : . ( obj -- ) pprint nl ;
61 : pprint-use ( obj -- ) [ pprint* ] with-use ;
63 : unparse ( obj -- str ) [ pprint ] with-string-writer ;
65 : unparse-use ( obj -- str ) [ pprint-use ] with-string-writer ;
67 : pprint-short ( obj -- )
74 } clone [ pprint ] bind ;
76 : unparse-short ( obj -- str )
77 [ pprint-short ] with-string-writer ;
79 : short. ( obj -- ) pprint-short nl ;
81 : .b ( n -- ) >bin print ;
82 : .o ( n -- ) >oct print ;
83 : .h ( n -- ) >hex print ;
85 : stack. ( seq -- ) [ short. ] each ;
87 : .s ( -- ) datastack stack. ;
88 : .r ( -- ) retainstack stack. ;
95 { { foreground T{ rgba f 1 1 1 1 } } { background T{ rgba f 0 0 0 1 } } }
96 "word-style" set-word-prop
98 : remove-step-into ( word -- )
99 building get [ nip pop wrapped>> ] unless-empty , ;
101 : (remove-breakpoints) ( quot -- newquot )
105 { [ dup word? not ] [ , ] }
106 { [ dup "break?" word-prop ] [ drop ] }
107 { [ dup "step-into?" word-prop ] [ remove-step-into ] }
113 : remove-breakpoints ( quot pos -- quot' )
115 1+ cut [ (remove-breakpoints) ] bi@
123 : callstack. ( callstack -- )
124 callstack>array 2 <groups> [
133 : .c ( -- ) callstack callstack. ;
135 : pprint-cell ( obj -- ) [ pprint ] with-cell ;
137 GENERIC: see ( defspec -- )
139 : comment. ( string -- )
140 [ H{ { font-style italic } } styled-text ] when* ;
142 : seeing-word ( word -- )
143 vocabulary>> pprinter-in set ;
145 : definer. ( defspec -- )
146 definer drop pprint-word ;
148 : stack-effect. ( word -- )
149 [ [ parsing-word? ] [ symbol? ] bi or not ] [ stack-effect ] bi and
150 [ effect>string comment. ] when* ;
152 : word-synopsis ( word -- )
160 M: word synopsis* word-synopsis ;
162 M: simple-generic synopsis* word-synopsis ;
164 M: standard-generic synopsis*
169 [ dispatch# pprint* ]
173 M: hook-generic synopsis*
178 [ "combination" word-prop var>> pprint* ]
182 M: method-spec synopsis*
183 first2 method synopsis* ;
185 M: method-body synopsis*
187 [ "method-class" word-prop pprint-word ]
188 [ "method-generic" word-prop pprint-word ] tri ;
190 M: mixin-instance synopsis*
192 [ class>> pprint-word ]
193 [ mixin>> pprint-word ] tri ;
195 M: pathname synopsis* pprint* ;
197 : synopsis ( defspec -- str )
201 [ synopsis* ] with-in
202 ] with-string-writer ;
204 : synopsis-alist ( definitions -- alist )
205 [ dup synopsis swap ] { } map>assoc ;
207 : definitions. ( alist -- )
208 [ write-object nl ] assoc-each ;
210 : sorted-definitions. ( definitions -- )
211 synopsis-alist sort-keys definitions. ;
213 GENERIC: declarations. ( obj -- )
215 M: object declarations. drop ;
217 : declaration. ( word prop -- )
218 tuck name>> word-prop [ pprint-word ] [ drop ] if ;
220 M: word declarations.
228 } [ declaration. ] with each ;
230 : pprint-; ( -- ) \ ; pprint-word ;
237 <block dup definition pprint-elements block>
238 dup definer nip [ pprint-word ] when* declarations.
242 GENERIC: see-class* ( word -- )
244 M: union-class see-class*
245 <colon \ UNION: pprint-word
247 members pprint-elements pprint-; block> ;
249 M: intersection-class see-class*
250 <colon \ INTERSECTION: pprint-word
252 participants pprint-elements pprint-; block> ;
254 M: mixin-class see-class*
255 <block \ MIXIN: pprint-word
256 dup pprint-word <block
259 \ INSTANCE: pprint-word pprint-word pprint-word
260 ] with each block> block> ;
262 M: predicate-class see-class*
263 <colon \ PREDICATE: pprint-word
266 dup superclass pprint-word
268 "predicate-definition" word-prop pprint-elements
269 pprint-; block> block> ;
271 M: singleton-class see-class* ( class -- )
272 \ SINGLETON: pprint-word pprint-word ;
274 GENERIC: pprint-slot-name ( object -- )
276 M: string pprint-slot-name text ;
278 M: array pprint-slot-name
279 <flow \ { pprint-word
280 f <inset unclip text pprint-elements block>
281 \ } pprint-word block> ;
283 : unparse-slot ( slot-spec -- array )
286 dup class>> object eq? [
297 : pprint-slot ( slot-spec -- )
299 dup length 1 = [ first ] when
302 M: tuple-class see-class*
303 <colon \ TUPLE: pprint-word
305 dup superclass tuple eq? [
306 "<" text dup superclass pprint-word
308 <block "slots" word-prop [ pprint-slot ] each block>
311 M: word see-class* drop ;
313 M: builtin-class see-class*
314 drop "! Built-in class" comment. ;
316 : see-class ( class -- )
319 dup seeing-word dup see-class*
325 dup class? over symbol? not and [
328 dup [ class? ] [ symbol? ] bi and
329 [ drop ] [ call-next-method ] if ;
332 natural-sort [ nl ] [ see ] interleave ;
334 : (see-implementors) ( class -- seq )
335 dup implementors [ method ] with map natural-sort ;
337 : (see-methods) ( generic -- seq )
338 "methods" word-prop values natural-sort ;
340 : see-methods ( word -- )
342 dup class? [ dup (see-implementors) % ] when
343 dup generic? [ dup (see-methods) % ] when
345 ] { } make prune see-all ;