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> [
126 3 nesting-limit [ . ] with-variable
129 : .c ( -- ) callstack callstack. ;
131 : pprint-cell ( obj -- ) [ pprint ] with-cell ;
133 GENERIC: see ( defspec -- )
135 : comment. ( string -- )
136 [ H{ { font-style italic } } styled-text ] when* ;
138 : seeing-word ( word -- )
139 vocabulary>> pprinter-in set ;
141 : definer. ( defspec -- )
142 definer drop pprint-word ;
144 : stack-effect. ( word -- )
145 [ [ parsing-word? ] [ symbol? ] bi or not ] [ stack-effect ] bi and
146 [ effect>string comment. ] when* ;
148 : word-synopsis ( word -- )
156 M: word synopsis* word-synopsis ;
158 M: simple-generic synopsis* word-synopsis ;
160 M: standard-generic synopsis*
165 [ dispatch# pprint* ]
169 M: hook-generic synopsis*
174 [ "combination" word-prop var>> pprint* ]
178 M: method-spec synopsis*
179 first2 method synopsis* ;
181 M: method-body synopsis*
183 [ "method-class" word-prop pprint-word ]
184 [ "method-generic" word-prop pprint-word ] tri ;
186 M: mixin-instance synopsis*
188 [ class>> pprint-word ]
189 [ mixin>> pprint-word ] tri ;
191 M: pathname synopsis* pprint* ;
193 : synopsis ( defspec -- str )
197 [ synopsis* ] with-in
198 ] with-string-writer ;
200 : synopsis-alist ( definitions -- alist )
201 [ dup synopsis swap ] { } map>assoc ;
203 : definitions. ( alist -- )
204 [ write-object nl ] assoc-each ;
206 : sorted-definitions. ( definitions -- )
207 synopsis-alist sort-keys definitions. ;
209 GENERIC: declarations. ( obj -- )
211 M: object declarations. drop ;
213 : declaration. ( word prop -- )
214 tuck name>> word-prop [ pprint-word ] [ drop ] if ;
216 M: word declarations.
224 } [ declaration. ] with each ;
226 : pprint-; ( -- ) \ ; pprint-word ;
230 <block dup definition pprint-elements block>
231 dup definer nip [ pprint-word ] when* declarations.
235 [ (see) ] with-use nl ;
237 GENERIC: see-class* ( word -- )
239 M: union-class see-class*
240 <colon \ UNION: pprint-word
242 members pprint-elements pprint-; block> ;
244 M: intersection-class see-class*
245 <colon \ INTERSECTION: pprint-word
247 participants pprint-elements pprint-; block> ;
249 M: mixin-class see-class*
250 <block \ MIXIN: pprint-word
251 dup pprint-word <block
254 \ INSTANCE: pprint-word pprint-word pprint-word
255 ] with each block> block> ;
257 M: predicate-class see-class*
258 <colon \ PREDICATE: pprint-word
261 dup superclass pprint-word
263 "predicate-definition" word-prop pprint-elements
264 pprint-; block> block> ;
266 M: singleton-class see-class* ( class -- )
267 \ SINGLETON: pprint-word pprint-word ;
269 GENERIC: pprint-slot-name ( object -- )
271 M: string pprint-slot-name text ;
273 M: array pprint-slot-name
274 <flow \ { pprint-word
275 f <inset unclip text pprint-elements block>
276 \ } pprint-word block> ;
278 : unparse-slot ( slot-spec -- array )
281 dup class>> object eq? [
292 : pprint-slot ( slot-spec -- )
294 dup length 1 = [ first ] when
297 M: tuple-class see-class*
298 <colon \ TUPLE: pprint-word
300 dup superclass tuple eq? [
301 "<" text dup superclass pprint-word
303 <block "slots" word-prop [ pprint-slot ] each block>
306 M: word see-class* drop ;
308 M: builtin-class see-class*
309 drop "! Built-in class" comment. ;
311 : see-class ( class -- )
314 dup seeing-word dup see-class*
320 dup class? over symbol? not and [
323 dup class? over symbol? and not [
324 [ dup (see) ] with-use nl
329 natural-sort [ nl ] [ see ] interleave ;
331 : (see-implementors) ( class -- seq )
332 dup implementors [ method ] with map natural-sort ;
334 : (see-methods) ( generic -- seq )
335 "methods" word-prop values natural-sort ;
337 : see-methods ( word -- )
339 dup class? [ dup (see-implementors) % ] when
340 dup generic? [ dup (see-methods) % ] when
342 ] { } make prune see-all ;