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 ;
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 ;
63 } clone [ pprint ] bind nl ;
65 : pprint-use ( obj -- ) [ pprint* ] with-use ;
67 : unparse ( obj -- str ) [ pprint ] with-string-writer ;
69 : unparse-use ( obj -- str ) [ pprint-use ] with-string-writer ;
71 : pprint-short ( obj -- )
77 } clone [ pprint ] bind ;
79 : unparse-short ( obj -- str )
80 [ pprint-short ] with-string-writer ;
82 : short. ( obj -- ) pprint-short nl ;
84 : .b ( n -- ) >bin print ;
85 : .o ( n -- ) >oct print ;
86 : .h ( n -- ) >hex print ;
88 : stack. ( seq -- ) [ short. ] each ;
90 : .s ( -- ) datastack stack. ;
91 : .r ( -- ) retainstack stack. ;
98 { { foreground { 1 1 1 1 } } { background { 0 0 0 1 } } }
99 "word-style" set-word-prop
101 : remove-step-into ( word -- )
102 building get dup empty? [ drop ] [ nip pop wrapped>> ] if , ;
104 : (remove-breakpoints) ( quot -- newquot )
108 { [ dup word? not ] [ , ] }
109 { [ dup "break?" word-prop ] [ drop ] }
110 { [ dup "step-into?" word-prop ] [ remove-step-into ] }
116 : remove-breakpoints ( quot pos -- quot' )
118 1+ cut [ (remove-breakpoints) ] bi@
126 : callstack. ( callstack -- )
127 callstack>array 2 <groups> [
129 2 nesting-limit [ . ] with-variable
132 : .c ( -- ) callstack callstack. ;
134 : pprint-cell ( obj -- ) [ pprint ] with-cell ;
136 GENERIC: see ( defspec -- )
138 : comment. ( string -- )
139 [ H{ { font-style italic } } styled-text ] when* ;
141 : seeing-word ( word -- )
142 vocabulary>> pprinter-in set ;
144 : definer. ( defspec -- )
145 definer drop pprint-word ;
147 : stack-effect. ( word -- )
148 [ [ parsing-word? ] [ symbol? ] bi or not ] [ stack-effect ] bi and
149 [ effect>string comment. ] when* ;
151 : word-synopsis ( word -- )
159 M: word synopsis* word-synopsis ;
161 M: simple-generic synopsis* word-synopsis ;
163 M: standard-generic synopsis*
168 [ dispatch# pprint* ]
172 M: hook-generic synopsis*
177 [ "combination" word-prop hook-combination-var pprint* ]
181 M: method-spec synopsis*
182 first2 method synopsis* ;
184 M: method-body synopsis*
186 [ "method-class" word-prop pprint-word ]
187 [ "method-generic" word-prop pprint-word ] tri ;
189 M: mixin-instance synopsis*
191 [ class>> pprint-word ]
192 [ mixin>> pprint-word ] tri ;
194 M: pathname synopsis* pprint* ;
196 : synopsis ( defspec -- str )
200 [ synopsis* ] with-in
201 ] with-string-writer ;
203 : synopsis-alist ( definitions -- alist )
204 [ dup synopsis swap ] { } map>assoc ;
206 : definitions. ( alist -- )
207 [ write-object nl ] assoc-each ;
209 : sorted-definitions. ( definitions -- )
210 synopsis-alist sort-keys definitions. ;
212 GENERIC: declarations. ( obj -- )
214 M: object declarations. drop ;
216 : declaration. ( word prop -- )
217 tuck name>> word-prop [ pprint-word ] [ drop ] if ;
219 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 M: tuple-class see-class*
272 <colon \ TUPLE: pprint-word
274 dup superclass tuple eq? [
275 "<" text dup superclass pprint-word
277 slot-names [ text ] each
280 M: word see-class* drop ;
282 M: builtin-class see-class*
283 drop "! Built-in class" comment. ;
286 natural-sort [ nl see ] each ;
288 : see-implementors ( class -- seq )
293 : see-class ( class -- )
296 dup seeing-word dup see-class*
300 : see-methods ( generic -- seq )
301 "methods" word-prop values natural-sort ;
305 dup class? over symbol? not and [
308 dup class? over symbol? and not [
309 [ dup (see) ] with-use nl
312 dup class? [ dup see-implementors % ] when
313 dup generic? [ dup see-methods % ] when
315 ] { } make prune see-all ;