]> gitweb.factorcode.org Git - factor.git/blob - basis/see/see.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / basis / see / see.factor
1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs classes classes.builtin
4 classes.intersection classes.mixin classes.predicate classes.singleton
5 classes.tuple classes.union combinators definitions effects generic
6 generic.single generic.standard generic.hook io io.pathnames
7 io.streams.string io.styles kernel make namespaces prettyprint
8 prettyprint.backend prettyprint.config prettyprint.custom
9 prettyprint.sections sequences sets sorting strings summary words
10 words.symbol words.constant words.alias ;
11 IN: see
12
13 GENERIC: synopsis* ( defspec -- )
14
15 GENERIC: see* ( defspec -- )
16
17 : see ( defspec -- ) see* nl ;
18
19 : synopsis ( defspec -- str )
20     [
21         0 margin set
22         1 line-limit set
23         [ synopsis* ] with-in
24     ] with-string-writer ;
25
26 : definer. ( defspec -- )
27     definer drop pprint-word ;
28
29 : comment. ( text -- )
30     H{ { font-style italic } } styled-text ;
31
32 GENERIC: print-stack-effect? ( word -- ? )
33
34 M: parsing-word print-stack-effect? drop f ;
35 M: symbol print-stack-effect? drop f ;
36 M: constant print-stack-effect? drop f ;
37 M: alias print-stack-effect? drop f ;
38 M: word print-stack-effect? drop t ;
39
40 : stack-effect. ( word -- )
41     [ print-stack-effect? ] [ stack-effect ] bi and
42     [ effect>string comment. ] when* ;
43
44 <PRIVATE
45
46 : seeing-word ( word -- )
47     vocabulary>> pprinter-in set ;
48
49 : word-synopsis ( word -- )
50     {
51         [ seeing-word ]
52         [ definer. ]
53         [ pprint-word ]
54         [ stack-effect. ] 
55     } cleave ;
56
57 M: word synopsis* word-synopsis ;
58
59 M: simple-generic synopsis* word-synopsis ;
60
61 M: standard-generic synopsis*
62     {
63         [ definer. ]
64         [ seeing-word ]
65         [ pprint-word ]
66         [ dispatch# pprint* ]
67         [ stack-effect. ]
68     } cleave ;
69
70 M: hook-generic synopsis*
71     {
72         [ definer. ]
73         [ seeing-word ]
74         [ pprint-word ]
75         [ "combination" word-prop var>> pprint* ]
76         [ stack-effect. ]
77     } cleave ;
78
79 M: method-body synopsis*
80     [ definer. ]
81     [ "method-class" word-prop pprint-word ]
82     [ "method-generic" word-prop pprint-word ] tri ;
83
84 M: mixin-instance synopsis*
85     [ definer. ]
86     [ class>> pprint-word ]
87     [ mixin>> pprint-word ] tri ;
88
89 M: pathname synopsis* pprint* ;
90
91 M: word summary synopsis ;
92
93 GENERIC: declarations. ( obj -- )
94
95 M: object declarations. drop ;
96
97 : declaration. ( word prop -- )
98     [ nip ] [ name>> word-prop ] 2bi
99     [ pprint-word ] [ drop ] if ;
100
101 M: word declarations.
102     {
103         POSTPONE: delimiter
104         POSTPONE: inline
105         POSTPONE: recursive
106         POSTPONE: foldable
107         POSTPONE: flushable
108     } [ declaration. ] with each ;
109
110 : pprint-; ( -- ) \ ; pprint-word ;
111
112 M: object see*
113     [
114         12 nesting-limit set
115         100 length-limit set
116         <colon dup synopsis*
117         <block dup definition pprint-elements block>
118         dup definer nip [ pprint-word ] when* declarations.
119         block>
120     ] with-use ;
121
122 GENERIC: see-class* ( word -- )
123
124 M: union-class see-class*
125     <colon \ UNION: pprint-word
126     dup pprint-word
127     members pprint-elements pprint-; block> ;
128
129 M: intersection-class see-class*
130     <colon \ INTERSECTION: pprint-word
131     dup pprint-word
132     participants pprint-elements pprint-; block> ;
133
134 M: mixin-class see-class*
135     <block \ MIXIN: pprint-word
136     dup pprint-word <block
137     dup members [
138         hard line-break
139         \ INSTANCE: pprint-word pprint-word pprint-word
140     ] with each block> block> ;
141
142 M: predicate-class see-class*
143     <colon \ PREDICATE: pprint-word
144     dup pprint-word
145     "<" text
146     dup superclass pprint-word
147     <block
148     "predicate-definition" word-prop pprint-elements
149     pprint-; block> block> ;
150
151 M: singleton-class see-class* ( class -- )
152     \ SINGLETON: pprint-word pprint-word ;
153
154 GENERIC: pprint-slot-name ( object -- )
155
156 M: string pprint-slot-name text ;
157
158 M: array pprint-slot-name
159     <flow \ { pprint-word
160     f <inset unclip text pprint-elements block>
161     \ } pprint-word block> ;
162
163 : unparse-slot ( slot-spec -- array )
164     [
165         dup name>> ,
166         dup class>> object eq? [
167             dup class>> ,
168             initial: ,
169             dup initial>> ,
170         ] unless
171         dup read-only>> [
172             read-only ,
173         ] when
174         drop
175     ] { } make ;
176
177 : pprint-slot ( slot-spec -- )
178     unparse-slot
179     dup length 1 = [ first ] when
180     pprint-slot-name ;
181
182 M: tuple-class see-class*
183     <colon \ TUPLE: pprint-word
184     dup pprint-word
185     dup superclass tuple eq? [
186         "<" text dup superclass pprint-word
187     ] unless
188     <block "slots" word-prop [ pprint-slot ] each block>
189     pprint-; block> ;
190
191 M: word see-class* drop ;
192
193 M: builtin-class see-class*
194     drop "! Built-in class" comment. ;
195
196 : see-class ( class -- )
197     dup class? [
198         [
199             [ seeing-word ] [ see-class* ] bi
200         ] with-use
201     ] [ drop ] if ;
202
203 M: word see*
204     [ see-class ]
205     [ [ class? ] [ symbol? not ] bi and [ nl nl ] when ]
206     [
207         dup [ class? ] [ symbol? ] bi and
208         [ drop ] [ call-next-method ] if
209     ] tri ;
210
211 : seeing-implementors ( class -- seq )
212     dup implementors [ method ] with map natural-sort ;
213
214 : seeing-methods ( generic -- seq )
215     "methods" word-prop values natural-sort ;
216
217 PRIVATE>
218
219 : see-all ( seq -- )
220     natural-sort [ nl nl ] [ see* ] interleave ;
221
222 : methods ( word -- seq )
223     [
224         dup class? [ dup seeing-implementors % ] when
225         dup generic? [ dup seeing-methods % ] when
226         drop
227     ] { } make prune ;
228
229 : see-methods ( word -- )
230     methods see-all nl ;