]> gitweb.factorcode.org Git - factor.git/blob - basis/see/see.factor
Update actions, because Node.js 16 actions are deprecated, to Node.js 20
[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     H{
22         { string-limit? f }
23         { margin 0 }
24         { line-limit 1 }
25     } clone [
26         [ [ synopsis* ] with-in ] with-string-writer
27     ] with-variables ;
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 namespaces: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 namespaces:set
97         1 line-limit namespaces:set
98         [
99             {
100                 [ seeing-word ]
101                 [ definer. ]
102                 [ pprint-word ]
103                 [ stack-effect pprint-effect ]
104             } cleave
105         ] with-in
106     ] with-string-writer ;
107
108 M: word summary synopsis ;
109
110 GENERIC: declarations. ( obj -- )
111
112 M: object declarations. drop ;
113
114 : declaration. ( word prop -- )
115     [ nip ] [ name>> word-prop ] 2bi
116     [ pprint-word ] [ drop ] if ;
117
118 M: word declarations.
119     {
120         POSTPONE: delimiter
121         POSTPONE: deprecated
122         POSTPONE: inline
123         POSTPONE: recursive
124         POSTPONE: foldable
125         POSTPONE: flushable
126     } [ declaration. ] with each ;
127
128 : pprint-; ( -- ) \ ; pprint-word ;
129
130 M: object see*
131     [
132         12 nesting-limit namespaces:set
133         100 length-limit namespaces:set
134         <colon dup synopsis*
135         <block dup definition pprint-elements block>
136         dup definer nip [ pprint-word ] when* declarations.
137         block>
138     ] with-use ;
139
140 GENERIC: see-class* ( word -- )
141
142 M: union-class see-class*
143     <colon \ UNION: pprint-word
144     dup pprint-word
145     class-members pprint-elements pprint-; block> ;
146
147 M: intersection-class see-class*
148     <colon \ INTERSECTION: pprint-word
149     dup pprint-word
150     class-participants pprint-elements pprint-; block> ;
151
152 M: mixin-class see-class*
153     <block \ MIXIN: pprint-word
154     dup pprint-word <block
155     dup class-members [
156         hard add-line-break
157         \ INSTANCE: pprint-word pprint-word pprint-word
158     ] with each block> block> ;
159
160 M: predicate-class see-class*
161     <colon \ PREDICATE: pprint-word
162     dup pprint-word
163     "<" text
164     dup superclass-of pprint-word
165     <block
166     "predicate-definition" word-prop pprint-elements
167     pprint-; block> block> ;
168
169 M: singleton-class see-class*
170     \ SINGLETON: pprint-word pprint-word ;
171
172 GENERIC: pprint-slot-name ( object -- )
173
174 M: string pprint-slot-name text ;
175
176 M: array pprint-slot-name
177     <flow \ { pprint-word
178     f <inset unclip text pprint-elements block>
179     \ } pprint-word block> ;
180
181 : unparse-slot ( slot-spec -- array )
182     [
183         dup name>> ,
184         dup class>> object eq? [
185             dup class>> ,
186         ] unless
187         dup read-only>> [
188             read-only ,
189         ] when
190         dup [ class>> object eq? not ] [ initial>> ] bi or [
191             initial: ,
192             dup initial>> ,
193         ] when
194         drop
195     ] { } make ;
196
197 : pprint-slot ( slot-spec -- )
198     unparse-slot
199     dup length 1 = [ first ] when
200     pprint-slot-name ;
201
202 : tuple-declarations. ( class -- )
203     \ final declaration. ;
204
205 : superclass. ( class -- )
206     superclass-of dup tuple eq? [ drop ] [ "<" text pprint-word ] if ;
207
208 M: tuple-class see-class*
209     <colon \ TUPLE: pprint-word
210     {
211         [ pprint-word ]
212         [ superclass. ]
213         [ <block "slots" word-prop [ pprint-slot ] each block> pprint-; ]
214         [ tuple-declarations. ]
215     } cleave
216     block> ;
217
218 M: word see-class* drop ;
219
220 M: builtin-class see-class*
221     <block
222     \ BUILTIN: pprint-word
223     [ pprint-word ]
224     [ <block "slots" word-prop [ pprint-slot ] each pprint-; block> ] bi
225     block> ;
226
227 : see-class ( class -- )
228     dup class? [
229         [
230             [ seeing-word ] [ see-class* ] bi
231         ] with-use
232     ] [ drop ] if ;
233
234 M: word see*
235     [ see-class ]
236     [ [ class? ] [ symbol? not ] bi and [ nl nl ] when ]
237     [
238         dup [ class? ] [ symbol? ] bi and
239         [ drop ] [ call-next-method ] if
240     ] tri ;
241
242 M: error-class see-class*
243     <colon \ ERROR: pprint-word
244     {
245         [ pprint-word ]
246         [ superclass. ]
247         [ <block "slots" word-prop [ name>> pprint-slot-name ] each block> pprint-; ]
248         [ tuple-declarations. ]
249     } cleave
250     block> ;
251
252 M: error-class see* see-class ;
253
254 : seeing-implementors ( class -- seq )
255     dup implementors
256     [ [ reader? ] [ writer? ] bi or ] reject
257     [ lookup-method ] with map
258     natural-sort ;
259
260 : seeing-methods ( generic -- seq )
261     "methods" word-prop values natural-sort ;
262
263 PRIVATE>
264
265 : see-all ( seq -- )
266     natural-sort [ nl nl ] [ see* ] interleave ;
267
268 : methods ( word -- seq )
269     [
270         dup class? [ dup seeing-implementors % ] when
271         dup generic? [ dup seeing-methods % ] when
272         drop
273     ] { } make members ;
274
275 : see-methods ( word -- )
276     methods see-all nl ;