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 words.symbol prettyprint.backend prettyprint.custom
6 prettyprint.sections prettyprint.config sorting splitting
7 grouping math.parser vocabs definitions effects classes.builtin
8 classes.tuple io.pathnames classes continuations hashtables
9 classes.mixin classes.union classes.intersection
10 classes.predicate classes.singleton combinators quotations sets
11 accessors colors parser summary vocabs.parser ;
14 : make-pprint ( obj quot -- block in use )
17 H{ } clone pprinter-use set
18 V{ } clone recursion-check set
19 V{ } clone pprinter-stack set
27 : with-pprint ( obj quot -- )
28 make-pprint 2drop do-pprint ; inline
30 : pprint-vocab ( vocab -- )
31 dup vocab present-text ;
33 : write-in ( vocab -- )
34 [ \ IN: pprint-word pprint-vocab ] with-pprint ;
37 [ write-in nl ] when* ;
48 : use/in. ( in use -- )
49 dupd remove [ { "syntax" "scratchpad" } member? not ] filter
52 : vocab-names ( words -- vocabs )
54 [ [ words>> eq? nip ] with assoc-find 2drop ] curry map sift ;
57 in get use get vocab-names use/in. ;
61 "Restarts were invoked adding vocabularies to the search path." print
62 "To avoid doing this in the future, add the following USING:" print
63 "and IN: forms at the top of the source file:" print nl
66 ] print-use-hook set-global
68 : with-use ( obj quot -- )
69 make-pprint use/in. do-pprint ; inline
71 : with-in ( obj quot -- )
72 make-pprint drop [ write-in bl ] when* do-pprint ; inline
74 : pprint ( obj -- ) [ pprint* ] with-pprint ;
76 : . ( obj -- ) pprint nl ;
78 : pprint-use ( obj -- ) [ pprint* ] with-use ;
80 : unparse ( obj -- str ) [ pprint ] with-string-writer ;
82 : unparse-use ( obj -- str ) [ pprint-use ] with-string-writer ;
84 : pprint-short ( obj -- )
91 } clone [ pprint ] bind ;
93 : unparse-short ( obj -- str )
94 [ pprint-short ] with-string-writer ;
96 : short. ( obj -- ) pprint-short nl ;
98 : .b ( n -- ) >bin print ;
99 : .o ( n -- ) >oct print ;
100 : .h ( n -- ) >hex print ;
102 : stack. ( seq -- ) [ short. ] each ;
104 : .s ( -- ) datastack stack. ;
105 : .r ( -- ) retainstack stack. ;
112 { { foreground T{ rgba f 1 1 1 1 } } { background T{ rgba f 0 0 0 1 } } }
113 "word-style" set-word-prop
115 : remove-step-into ( word -- )
116 building get [ nip pop wrapped>> ] unless-empty , ;
118 : (remove-breakpoints) ( quot -- newquot )
122 { [ dup word? not ] [ , ] }
123 { [ dup "break?" word-prop ] [ drop ] }
124 { [ dup "step-into?" word-prop ] [ remove-step-into ] }
130 : remove-breakpoints ( quot pos -- quot' )
132 1+ cut [ (remove-breakpoints) ] bi@
140 : callstack. ( callstack -- )
141 callstack>array 2 <groups> [
150 : .c ( -- ) callstack callstack. ;
152 : pprint-cell ( obj -- ) [ pprint ] with-cell ;
154 : simple-table. ( values -- )
155 standard-table-style [
160 [ [ write ] with-cell ]
168 GENERIC: see ( defspec -- )
170 : comment. ( string -- )
171 [ H{ { font-style italic } } styled-text ] when* ;
173 : seeing-word ( word -- )
174 vocabulary>> pprinter-in set ;
176 : definer. ( defspec -- )
177 definer drop pprint-word ;
179 : stack-effect. ( word -- )
180 [ [ parsing-word? ] [ symbol? ] bi or not ] [ stack-effect ] bi and
181 [ effect>string comment. ] when* ;
183 : word-synopsis ( word -- )
191 M: word synopsis* word-synopsis ;
193 M: simple-generic synopsis* word-synopsis ;
195 M: standard-generic synopsis*
200 [ dispatch# pprint* ]
204 M: hook-generic synopsis*
209 [ "combination" word-prop var>> pprint* ]
213 M: method-spec synopsis*
214 first2 method synopsis* ;
216 M: method-body synopsis*
218 [ "method-class" word-prop pprint-word ]
219 [ "method-generic" word-prop pprint-word ] tri ;
221 M: mixin-instance synopsis*
223 [ class>> pprint-word ]
224 [ mixin>> pprint-word ] tri ;
226 M: pathname synopsis* pprint* ;
228 : synopsis ( defspec -- str )
232 [ synopsis* ] with-in
233 ] with-string-writer ;
235 M: word summary synopsis ;
237 : synopsis-alist ( definitions -- alist )
238 [ dup synopsis swap ] { } map>assoc ;
240 : definitions. ( alist -- )
241 [ write-object nl ] assoc-each ;
243 : sorted-definitions. ( definitions -- )
244 synopsis-alist sort-keys definitions. ;
246 GENERIC: declarations. ( obj -- )
248 M: object declarations. drop ;
250 : declaration. ( word prop -- )
251 [ nip ] [ name>> word-prop ] 2bi
252 [ pprint-word ] [ drop ] if ;
254 M: word declarations.
262 } [ declaration. ] with each ;
264 : pprint-; ( -- ) \ ; pprint-word ;
271 <block dup definition pprint-elements block>
272 dup definer nip [ pprint-word ] when* declarations.
279 GENERIC: see-class* ( word -- )
281 M: union-class see-class*
282 <colon \ UNION: pprint-word
284 members pprint-elements pprint-; block> ;
286 M: intersection-class see-class*
287 <colon \ INTERSECTION: pprint-word
289 participants pprint-elements pprint-; block> ;
291 M: mixin-class see-class*
292 <block \ MIXIN: pprint-word
293 dup pprint-word <block
296 \ INSTANCE: pprint-word pprint-word pprint-word
297 ] with each block> block> ;
299 M: predicate-class see-class*
300 <colon \ PREDICATE: pprint-word
303 dup superclass pprint-word
305 "predicate-definition" word-prop pprint-elements
306 pprint-; block> block> ;
308 M: singleton-class see-class* ( class -- )
309 \ SINGLETON: pprint-word pprint-word ;
311 GENERIC: pprint-slot-name ( object -- )
313 M: string pprint-slot-name text ;
315 M: array pprint-slot-name
316 <flow \ { pprint-word
317 f <inset unclip text pprint-elements block>
318 \ } pprint-word block> ;
320 : unparse-slot ( slot-spec -- array )
323 dup class>> object eq? [
334 : pprint-slot ( slot-spec -- )
336 dup length 1 = [ first ] when
339 M: tuple-class see-class*
340 <colon \ TUPLE: pprint-word
342 dup superclass tuple eq? [
343 "<" text dup superclass pprint-word
345 <block "slots" word-prop [ pprint-slot ] each block>
348 M: word see-class* drop ;
350 M: builtin-class see-class*
351 drop "! Built-in class" comment. ;
353 : see-class ( class -- )
356 dup seeing-word dup see-class*
362 [ [ class? ] [ symbol? not ] bi and [ nl ] when ]
364 dup [ class? ] [ symbol? ] bi and
365 [ drop ] [ call-next-method ] if
369 natural-sort [ nl ] [ see ] interleave ;
371 : (see-implementors) ( class -- seq )
372 dup implementors [ method ] with map natural-sort ;
374 : (see-methods) ( generic -- seq )
375 "methods" word-prop values natural-sort ;
377 : methods ( word -- seq )
379 dup class? [ dup (see-implementors) % ] when
380 dup generic? [ dup (see-methods) % ] when
384 : see-methods ( word -- )