]> gitweb.factorcode.org Git - factor.git/blob - basis/prettyprint/prettyprint.factor
Change tabular-output and smash-pane behavior to fix panes unit tests; re-organize...
[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-short ] with-cell ;
153
154 SYMBOL: pprint-string-cells?
155
156 : simple-table. ( values -- )
157     standard-table-style [
158         [
159             [
160                 [
161                     dup string? pprint-string-cells? get not and
162                     [ [ write ] with-cell ]
163                     [ pprint-cell ]
164                     if
165                 ] each
166             ] with-row
167         ] each
168     ] tabular-output nl ;
169
170 GENERIC: see ( defspec -- )
171
172 : comment. ( string -- )
173     [ H{ { font-style italic } } styled-text ] when* ;
174
175 : seeing-word ( word -- )
176     vocabulary>> pprinter-in set ;
177
178 : definer. ( defspec -- )
179     definer drop pprint-word ;
180
181 : stack-effect. ( word -- )
182     [ [ parsing-word? ] [ symbol? ] bi or not ] [ stack-effect ] bi and
183     [ effect>string comment. ] when* ;
184
185 : word-synopsis ( word -- )
186     {
187         [ seeing-word ]
188         [ definer. ]
189         [ pprint-word ]
190         [ stack-effect. ] 
191     } cleave ;
192
193 M: word synopsis* word-synopsis ;
194
195 M: simple-generic synopsis* word-synopsis ;
196
197 M: standard-generic synopsis*
198     {
199         [ definer. ]
200         [ seeing-word ]
201         [ pprint-word ]
202         [ dispatch# pprint* ]
203         [ stack-effect. ]
204     } cleave ;
205
206 M: hook-generic synopsis*
207     {
208         [ definer. ]
209         [ seeing-word ]
210         [ pprint-word ]
211         [ "combination" word-prop var>> pprint* ]
212         [ stack-effect. ]
213     } cleave ;
214
215 M: method-spec synopsis*
216     first2 method synopsis* ;
217
218 M: method-body synopsis*
219     [ definer. ]
220     [ "method-class" word-prop pprint-word ]
221     [ "method-generic" word-prop pprint-word ] tri ;
222
223 M: mixin-instance synopsis*
224     [ definer. ]
225     [ class>> pprint-word ]
226     [ mixin>> pprint-word ] tri ;
227
228 M: pathname synopsis* pprint* ;
229
230 : synopsis ( defspec -- str )
231     [
232         0 margin set
233         1 line-limit set
234         [ synopsis* ] with-in
235     ] with-string-writer ;
236
237 M: word summary synopsis ;
238
239 GENERIC: declarations. ( obj -- )
240
241 M: object declarations. drop ;
242
243 : declaration. ( word prop -- )
244     [ nip ] [ name>> word-prop ] 2bi
245     [ pprint-word ] [ drop ] if ;
246
247 M: word declarations.
248     {
249         POSTPONE: parsing
250         POSTPONE: delimiter
251         POSTPONE: inline
252         POSTPONE: recursive
253         POSTPONE: foldable
254         POSTPONE: flushable
255     } [ declaration. ] with each ;
256
257 : pprint-; ( -- ) \ ; pprint-word ;
258
259 M: object see
260     [
261         12 nesting-limit set
262         100 length-limit set
263         <colon dup synopsis*
264         <block dup definition pprint-elements block>
265         dup definer nip [ pprint-word ] when* declarations.
266         block>
267     ] with-use nl ;
268
269 M: method-spec see
270     first2 method see ;
271
272 GENERIC: see-class* ( word -- )
273
274 M: union-class see-class*
275     <colon \ UNION: pprint-word
276     dup pprint-word
277     members pprint-elements pprint-; block> ;
278
279 M: intersection-class see-class*
280     <colon \ INTERSECTION: pprint-word
281     dup pprint-word
282     participants pprint-elements pprint-; block> ;
283
284 M: mixin-class see-class*
285     <block \ MIXIN: pprint-word
286     dup pprint-word <block
287     dup members [
288         hard line-break
289         \ INSTANCE: pprint-word pprint-word pprint-word
290     ] with each block> block> ;
291
292 M: predicate-class see-class*
293     <colon \ PREDICATE: pprint-word
294     dup pprint-word
295     "<" text
296     dup superclass pprint-word
297     <block
298     "predicate-definition" word-prop pprint-elements
299     pprint-; block> block> ;
300
301 M: singleton-class see-class* ( class -- )
302     \ SINGLETON: pprint-word pprint-word ;
303
304 GENERIC: pprint-slot-name ( object -- )
305
306 M: string pprint-slot-name text ;
307
308 M: array pprint-slot-name
309     <flow \ { pprint-word
310     f <inset unclip text pprint-elements block>
311     \ } pprint-word block> ;
312
313 : unparse-slot ( slot-spec -- array )
314     [
315         dup name>> ,
316         dup class>> object eq? [
317             dup class>> ,
318             initial: ,
319             dup initial>> ,
320         ] unless
321         dup read-only>> [
322             read-only ,
323         ] when
324         drop
325     ] { } make ;
326
327 : pprint-slot ( slot-spec -- )
328     unparse-slot
329     dup length 1 = [ first ] when
330     pprint-slot-name ;
331
332 M: tuple-class see-class*
333     <colon \ TUPLE: pprint-word
334     dup pprint-word
335     dup superclass tuple eq? [
336         "<" text dup superclass pprint-word
337     ] unless
338     <block "slots" word-prop [ pprint-slot ] each block>
339     pprint-; block> ;
340
341 M: word see-class* drop ;
342
343 M: builtin-class see-class*
344     drop "! Built-in class" comment. ;
345
346 : see-class ( class -- )
347     dup class? [
348         [
349             dup seeing-word dup see-class*
350         ] with-use nl
351     ] when drop ;
352
353 M: word see
354     [ see-class ]
355     [ [ class? ] [ symbol? not ] bi and [ nl ] when ]
356     [
357         dup [ class? ] [ symbol? ] bi and
358         [ drop ] [ call-next-method ] if
359     ] tri ;
360
361 : see-all ( seq -- )
362     natural-sort [ nl ] [ see ] interleave ;
363
364 : (see-implementors) ( class -- seq )
365     dup implementors [ method ] with map natural-sort ;
366
367 : (see-methods) ( generic -- seq )
368     "methods" word-prop values natural-sort ;
369
370 : methods ( word -- seq )
371     [
372         dup class? [ dup (see-implementors) % ] when
373         dup generic? [ dup (see-methods) % ] when
374         drop
375     ] { } make prune ;
376
377 : see-methods ( word -- )
378     methods see-all ;