]> gitweb.factorcode.org Git - factor.git/blob - basis/see/see.factor
basis: removing unnecessary method stack effects.
[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.error classes.intersection classes.mixin
5 classes.predicate classes.singleton classes.tuple classes.union
6 combinators definitions effects generic generic.hook
7 generic.single generic.standard io io.pathnames
8 io.streams.string io.styles kernel make namespaces prettyprint
9 prettyprint.backend prettyprint.config prettyprint.custom
10 prettyprint.sections sequences sets slots sorting strings
11 summary vocabs words words.alias words.constant words.symbol ;
12 IN: see
13
14 GENERIC: synopsis* ( defspec -- )
15
16 GENERIC: see* ( defspec -- )
17
18 : see ( defspec -- ) see* nl ;
19
20 : synopsis ( defspec -- str )
21     [
22         string-limit? off
23         0 margin namespaces:set
24         1 line-limit namespaces:set
25         [ synopsis* ] with-in
26     ] with-string-writer ;
27
28 : definer. ( defspec -- )
29     definer drop pprint-word ;
30
31 : comment. ( text -- )
32     H{ { font-style italic } } styled-text ;
33
34 GENERIC: print-stack-effect? ( word -- ? )
35
36 M: parsing-word print-stack-effect? drop f ;
37 M: symbol print-stack-effect? drop f ;
38 M: constant print-stack-effect? drop f ;
39 M: alias print-stack-effect? drop f ;
40 M: word print-stack-effect? drop t ;
41
42 : stack-effect. ( word -- )
43     [ print-stack-effect? ] [ stack-effect ] bi and
44     [ pprint-effect ] when* ;
45
46 <PRIVATE
47
48 : seeing-word ( word -- )
49     vocabulary>> dup [ lookup-vocab ] when pprinter-in namespaces:set ;
50
51 : word-synopsis ( word -- )
52     {
53         [ seeing-word ]
54         [ definer. ]
55         [ pprint-word ]
56         [ stack-effect. ]
57     } cleave ;
58
59 M: word synopsis* word-synopsis ;
60
61 M: simple-generic synopsis* word-synopsis ;
62
63 M: standard-generic synopsis*
64     {
65         [ definer. ]
66         [ seeing-word ]
67         [ pprint-word ]
68         [ dispatch# pprint* ]
69         [ stack-effect. ]
70     } cleave ;
71
72 M: hook-generic synopsis*
73     {
74         [ definer. ]
75         [ seeing-word ]
76         [ pprint-word ]
77         [ "combination" word-prop var>> pprint* ]
78         [ stack-effect. ]
79     } cleave ;
80
81 M: method synopsis*
82     [ definer. ]
83     [ "method-class" word-prop pprint-class ]
84     [ "method-generic" word-prop pprint-word ] tri ;
85
86 M: mixin-instance synopsis*
87     [ definer. ]
88     [ class>> pprint-word ]
89     [ mixin>> pprint-word ] tri ;
90
91 M: pathname synopsis* pprint* ;
92
93 M: alias summary
94     [
95         0 margin namespaces:set
96         1 line-limit namespaces: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 namespaces:set
132         100 length-limit namespaces: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     class-members pprint-elements pprint-; block> ;
145
146 M: intersection-class see-class*
147     <colon \ INTERSECTION: pprint-word
148     dup pprint-word
149     class-participants pprint-elements pprint-; block> ;
150
151 M: mixin-class see-class*
152     <block \ MIXIN: pprint-word
153     dup pprint-word <block
154     dup class-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-of pprint-word
164     <block
165     "predicate-definition" word-prop pprint-elements
166     pprint-; block> block> ;
167
168 M: singleton-class see-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-of 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 M: error-class see-class*
242     <colon \ ERROR: pprint-word
243     {
244         [ pprint-word ]
245         [ superclass. ]
246         [ <block "slots" word-prop [ name>> pprint-slot-name ] each block> pprint-; ]
247         [ tuple-declarations. ]
248     } cleave
249     block> ;
250
251 M: error-class see* see-class ;
252
253 : seeing-implementors ( class -- seq )
254     dup implementors
255     [ [ reader? ] [ writer? ] bi or ] reject
256     [ lookup-method ] with map
257     natural-sort ;
258
259 : seeing-methods ( generic -- seq )
260     "methods" word-prop values natural-sort ;
261
262 PRIVATE>
263
264 : see-all ( seq -- )
265     natural-sort [ nl nl ] [ see* ] interleave ;
266
267 : methods ( word -- seq )
268     [
269         dup class? [ dup seeing-implementors % ] when
270         dup generic? [ dup seeing-methods % ] when
271         drop
272     ] { } make members ;
273
274 : see-methods ( word -- )
275     methods see-all nl ;