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