]> gitweb.factorcode.org Git - factor.git/blob - basis/prettyprint/prettyprint.factor
Merge branch 'master' into new_ui
[factor.git] / basis / prettyprint / prettyprint.factor
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 ;
12 IN: prettyprint
13
14 : make-pprint ( obj quot -- block in use )
15     [
16         0 position set
17         H{ } clone pprinter-use set
18         V{ } clone recursion-check set
19         V{ } clone pprinter-stack set
20         over <object
21         call
22         pprinter-block
23         pprinter-in get
24         pprinter-use get keys
25     ] with-scope ; inline
26
27 : with-pprint ( obj quot -- )
28     make-pprint 2drop do-pprint ; inline
29
30 : pprint-vocab ( vocab -- )
31     dup vocab present-text ;
32
33 : write-in ( vocab -- )
34     [ \ IN: pprint-word pprint-vocab ] with-pprint ;
35
36 : in. ( vocab -- )
37     [ write-in nl ] when* ;
38
39 : use. ( seq -- )
40     [
41         natural-sort [
42             \ USING: pprint-word
43             [ pprint-vocab ] each
44             \ ; pprint-word
45         ] with-pprint nl
46     ] unless-empty ;
47
48 : use/in. ( in use -- )
49     dupd remove [ { "syntax" "scratchpad" } member? not ] filter
50     use. in. ;
51
52 : vocab-names ( words -- vocabs )
53     dictionary get
54     [ [ words>> eq? nip ] with assoc-find 2drop ] curry map sift ;
55
56 : prelude. ( -- )
57     in get use get vocab-names use/in. ;
58
59 [
60     nl
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
64     prelude.
65     nl
66 ] print-use-hook set-global
67
68 : with-use ( obj quot -- )
69     make-pprint use/in. do-pprint ; inline
70
71 : with-in ( obj quot -- )
72     make-pprint drop [ write-in bl ] when* do-pprint ; inline
73
74 : pprint ( obj -- ) [ pprint* ] with-pprint ;
75
76 : . ( obj -- ) pprint nl ;
77
78 : pprint-use ( obj -- ) [ pprint* ] with-use ;
79
80 : unparse ( obj -- str ) [ pprint ] with-string-writer ;
81
82 : unparse-use ( obj -- str ) [ pprint-use ] with-string-writer ;
83
84 : pprint-short ( obj -- )
85     H{
86        { line-limit 1 }
87        { length-limit 15 }
88        { nesting-limit 2 }
89        { string-limit? t }
90        { boa-tuples? t }
91     } clone [ pprint ] bind ;
92
93 : unparse-short ( obj -- str )
94     [ pprint-short ] with-string-writer ;
95
96 : short. ( obj -- ) pprint-short nl ;
97
98 : .b ( n -- ) >bin print ;
99 : .o ( n -- ) >oct print ;
100 : .h ( n -- ) >hex print ;
101
102 : stack. ( seq -- ) [ short. ] each ;
103
104 : .s ( -- ) datastack stack. ;
105 : .r ( -- ) retainstack stack. ;
106
107 <PRIVATE
108
109 SYMBOL: ->
110
111 \ ->
112 { { foreground T{ rgba f 1 1 1 1 } } { background T{ rgba f 0 0 0 1 } } }
113 "word-style" set-word-prop
114
115 : remove-step-into ( word -- )
116     building get [ nip pop wrapped>> ] unless-empty , ;
117
118 : (remove-breakpoints) ( quot -- newquot )
119     [
120         [
121             {
122                 { [ dup word? not ] [ , ] }
123                 { [ dup "break?" word-prop ] [ drop ] }
124                 { [ dup "step-into?" word-prop ] [ remove-step-into ] }
125                 [ , ]
126             } cond
127         ] each
128     ] [ ] make ;
129
130 : remove-breakpoints ( quot pos -- quot' )
131     over quotation? [
132         1+ cut [ (remove-breakpoints) ] bi@
133         [ -> ] glue 
134     ] [
135         drop
136     ] if ;
137
138 PRIVATE>
139
140 : callstack. ( callstack -- )
141     callstack>array 2 <groups> [
142         remove-breakpoints
143         [
144             3 nesting-limit set
145             100 length-limit set
146             .
147         ] with-scope
148     ] assoc-each ;
149
150 : .c ( -- ) callstack callstack. ;
151
152 : pprint-cell ( obj -- ) [ pprint ] with-cell ;
153
154 : simple-table. ( values -- )
155     standard-table-style [
156         [
157             [
158                 [
159                     dup string?
160                     [ [ write ] with-cell ]
161                     [ pprint-cell ]
162                     if
163                 ] each
164             ] with-row
165         ] each
166     ] tabular-output ;
167
168 GENERIC: see ( defspec -- )
169
170 : comment. ( string -- )
171     [ H{ { font-style italic } } styled-text ] when* ;
172
173 : seeing-word ( word -- )
174     vocabulary>> pprinter-in set ;
175
176 : definer. ( defspec -- )
177     definer drop pprint-word ;
178
179 : stack-effect. ( word -- )
180     [ [ parsing-word? ] [ symbol? ] bi or not ] [ stack-effect ] bi and
181     [ effect>string comment. ] when* ;
182
183 : word-synopsis ( word -- )
184     {
185         [ seeing-word ]
186         [ definer. ]
187         [ pprint-word ]
188         [ stack-effect. ] 
189     } cleave ;
190
191 M: word synopsis* word-synopsis ;
192
193 M: simple-generic synopsis* word-synopsis ;
194
195 M: standard-generic synopsis*
196     {
197         [ definer. ]
198         [ seeing-word ]
199         [ pprint-word ]
200         [ dispatch# pprint* ]
201         [ stack-effect. ]
202     } cleave ;
203
204 M: hook-generic synopsis*
205     {
206         [ definer. ]
207         [ seeing-word ]
208         [ pprint-word ]
209         [ "combination" word-prop var>> pprint* ]
210         [ stack-effect. ]
211     } cleave ;
212
213 M: method-spec synopsis*
214     first2 method synopsis* ;
215
216 M: method-body synopsis*
217     [ definer. ]
218     [ "method-class" word-prop pprint-word ]
219     [ "method-generic" word-prop pprint-word ] tri ;
220
221 M: mixin-instance synopsis*
222     [ definer. ]
223     [ class>> pprint-word ]
224     [ mixin>> pprint-word ] tri ;
225
226 M: pathname synopsis* pprint* ;
227
228 : synopsis ( defspec -- str )
229     [
230         0 margin set
231         1 line-limit set
232         [ synopsis* ] with-in
233     ] with-string-writer ;
234
235 M: word summary synopsis ;
236
237 GENERIC: declarations. ( obj -- )
238
239 M: object declarations. drop ;
240
241 : declaration. ( word prop -- )
242     tuck name>> word-prop [ pprint-word ] [ drop ] if ;
243
244 M: word declarations.
245     {
246         POSTPONE: parsing
247         POSTPONE: delimiter
248         POSTPONE: inline
249         POSTPONE: recursive
250         POSTPONE: foldable
251         POSTPONE: flushable
252     } [ declaration. ] with each ;
253
254 : pprint-; ( -- ) \ ; pprint-word ;
255
256 M: object see
257     [
258         12 nesting-limit set
259         100 length-limit set
260         <colon dup synopsis*
261         <block dup definition pprint-elements block>
262         dup definer nip [ pprint-word ] when* declarations.
263         block>
264     ] with-use nl ;
265
266 M: method-spec see
267     first2 method see ;
268
269 GENERIC: see-class* ( word -- )
270
271 M: union-class see-class*
272     <colon \ UNION: pprint-word
273     dup pprint-word
274     members pprint-elements pprint-; block> ;
275
276 M: intersection-class see-class*
277     <colon \ INTERSECTION: pprint-word
278     dup pprint-word
279     participants pprint-elements pprint-; block> ;
280
281 M: mixin-class see-class*
282     <block \ MIXIN: pprint-word
283     dup pprint-word <block
284     dup members [
285         hard line-break
286         \ INSTANCE: pprint-word pprint-word pprint-word
287     ] with each block> block> ;
288
289 M: predicate-class see-class*
290     <colon \ PREDICATE: pprint-word
291     dup pprint-word
292     "<" text
293     dup superclass pprint-word
294     <block
295     "predicate-definition" word-prop pprint-elements
296     pprint-; block> block> ;
297
298 M: singleton-class see-class* ( class -- )
299     \ SINGLETON: pprint-word pprint-word ;
300
301 GENERIC: pprint-slot-name ( object -- )
302
303 M: string pprint-slot-name text ;
304
305 M: array pprint-slot-name
306     <flow \ { pprint-word
307     f <inset unclip text pprint-elements block>
308     \ } pprint-word block> ;
309
310 : unparse-slot ( slot-spec -- array )
311     [
312         dup name>> ,
313         dup class>> object eq? [
314             dup class>> ,
315             initial: ,
316             dup initial>> ,
317         ] unless
318         dup read-only>> [
319             read-only ,
320         ] when
321         drop
322     ] { } make ;
323
324 : pprint-slot ( slot-spec -- )
325     unparse-slot
326     dup length 1 = [ first ] when
327     pprint-slot-name ;
328
329 M: tuple-class see-class*
330     <colon \ TUPLE: pprint-word
331     dup pprint-word
332     dup superclass tuple eq? [
333         "<" text dup superclass pprint-word
334     ] unless
335     <block "slots" word-prop [ pprint-slot ] each block>
336     pprint-; block> ;
337
338 M: word see-class* drop ;
339
340 M: builtin-class see-class*
341     drop "! Built-in class" comment. ;
342
343 : see-class ( class -- )
344     dup class? [
345         [
346             dup seeing-word dup see-class*
347         ] with-use nl
348     ] when drop ;
349
350 M: word see
351     [ see-class ]
352     [ [ class? ] [ symbol? not ] bi and [ nl ] when ]
353     [
354         dup [ class? ] [ symbol? ] bi and
355         [ drop ] [ call-next-method ] if
356     ] tri ;
357
358 : see-all ( seq -- )
359     natural-sort [ nl ] [ see ] interleave ;
360
361 : (see-implementors) ( class -- seq )
362     dup implementors [ method ] with map natural-sort ;
363
364 : (see-methods) ( generic -- seq )
365     "methods" word-prop values natural-sort ;
366
367 : methods ( word -- seq )
368     [
369         dup class? [ dup (see-implementors) % ] when
370         dup generic? [ dup (see-methods) % ] when
371         drop
372     ] { } make prune ;
373
374 : see-methods ( word -- )
375     methods see-all ;