]> gitweb.factorcode.org Git - factor.git/blob - core/vocabs/parser/parser.factor
vocabs.parser: cleanup update-manifest, make update-search-vocabs a little faster.
[factor.git] / core / vocabs / parser / parser.factor
1 ! Copyright (C) 2007, 2010 Daniel Ehrenberg, Bruno Deferrari,
2 ! Slava Pestov.
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: accessors arrays assocs combinators compiler.units
5 continuations hash-sets hashtables kernel math namespaces
6 parser.notes sequences sets sorting splitting vectors vocabs
7 words ;
8 IN: vocabs.parser
9
10 ERROR: no-word-error name ;
11
12 : word-restarts ( possibilities -- restarts )
13     natural-sort
14     [ [ vocabulary>> "Use the " " vocabulary" surround ] keep ] { } map>assoc ;
15
16 : word-restarts-with-defer ( name possibilities -- restarts )
17     word-restarts
18     swap "Defer word in current vocabulary" swap 2array
19     suffix ;
20
21 : <no-word-error> ( name possibilities -- error restarts )
22     [ drop \ no-word-error boa ] [ word-restarts-with-defer ] 2bi ;
23
24 TUPLE: manifest
25 current-vocab
26 { search-vocab-names hash-set }
27 { search-vocabs vector }
28 { qualified-vocabs vector }
29 { auto-used vector } ;
30
31 : <manifest> ( -- manifest )
32     manifest new
33         HS{ } clone >>search-vocab-names
34         V{ } clone >>search-vocabs
35         V{ } clone >>qualified-vocabs
36         V{ } clone >>auto-used ;
37
38 M: manifest clone
39     call-next-method
40         [ clone ] change-search-vocab-names
41         [ clone ] change-search-vocabs
42         [ clone ] change-qualified-vocabs
43         [ clone ] change-auto-used ;
44
45 TUPLE: extra-words words ;
46
47 M: extra-words equal?
48     over extra-words? [ [ words>> ] bi@ eq? ] [ 2drop f ] if ;
49
50 C: <extra-words> extra-words
51
52 ERROR: no-word-in-vocab word vocab ;
53
54 <PRIVATE
55
56 : (from) ( vocab words -- vocab words words' vocab )
57     2dup swap load-vocab ;
58
59 : extract-words ( seq vocab -- assoc )
60     [ words>> extract-keys dup ] [ name>> ] bi
61     [ swap [ 2drop ] [ no-word-in-vocab ] if ] curry assoc-each ;
62
63 : excluding-words ( seq vocab -- assoc )
64     [ nip words>> ] [ extract-words ] 2bi assoc-diff ;
65
66 : qualified-words ( prefix vocab -- assoc )
67     words>> swap [ swap [ swap ":" glue ] dip ] curry assoc-map ;
68
69 : (lookup) ( name assoc -- word/f )
70     at* [ dup forward-reference? [ drop f ] when ] when ;
71
72 PRIVATE>
73
74 : qualified-vocabs ( -- qualified-vocabs )
75     manifest get qualified-vocabs>> ;
76
77 : set-current-vocab ( name -- )
78     create-vocab
79     [ manifest get current-vocab<< ]
80     [ qualified-vocabs push ] bi ;
81
82 : with-current-vocab ( name quot -- )
83     manifest get clone manifest [
84         [ set-current-vocab ] dip call
85     ] with-variable ; inline
86
87 TUPLE: no-current-vocab-error ;
88
89 : no-current-vocab ( -- vocab )
90     no-current-vocab-error boa
91     { { "Define words in scratchpad vocabulary" "scratchpad" } }
92     throw-restarts dup set-current-vocab ;
93
94 : current-vocab ( -- vocab )
95     manifest get current-vocab>> [ no-current-vocab ] unless* ;
96
97 : begin-private ( -- )
98     current-vocab name>> ".private" ?tail
99     [ drop ] [ ".private" append set-current-vocab ] if ;
100
101 : end-private ( -- )
102     current-vocab name>> ".private" ?tail
103     [ set-current-vocab ] [ drop ] if ;
104
105 : using-vocab? ( vocab -- ? )
106     vocab-name manifest get search-vocab-names>> in? ;
107
108 : use-vocab ( vocab -- )
109     dup using-vocab?
110     [ vocab-name "Already using ``" "'' vocabulary" surround note. ] [
111         manifest get
112         [ [ load-vocab ] dip search-vocabs>> push ]
113         [ [ vocab-name ] dip search-vocab-names>> adjoin ]
114         2bi
115     ] if ;
116
117 : auto-use-vocab ( vocab -- )
118     [ use-vocab ] [ manifest get auto-used>> push ] bi ;
119
120 : auto-used? ( -- ? )
121     manifest get auto-used>> length 0 > ;
122
123 : unuse-vocab ( vocab -- )
124     dup using-vocab? [
125         manifest get
126         [ [ load-vocab ] dip search-vocabs>> remove-eq! drop ]
127         [ [ vocab-name ] dip search-vocab-names>> delete ]
128         2bi
129     ] [ drop ] if ;
130
131 TUPLE: qualified vocab prefix words ;
132
133 : <qualified> ( vocab prefix -- qualified )
134     (from) qualified-words qualified boa ;
135
136 : add-qualified ( vocab prefix -- )
137     <qualified> qualified-vocabs push ;
138
139 TUPLE: from vocab names words ;
140
141 : <from> ( vocab words -- from )
142     (from) extract-words from boa ;
143
144 : add-words-from ( vocab words -- )
145     <from> qualified-vocabs push ;
146
147 TUPLE: exclude vocab names words ;
148
149 : <exclude> ( vocab words -- from )
150     (from) excluding-words exclude boa ;
151
152 : add-words-excluding ( vocab words -- )
153     <exclude> qualified-vocabs push ;
154
155 TUPLE: rename word vocab words ;
156
157 : <rename> ( word vocab new-name -- rename )
158     [
159         2dup load-vocab words>> dupd at
160         [ ] [ swap no-word-in-vocab ] ?if
161     ] dip associate rename boa ;
162
163 : add-renamed-word ( word vocab new-name -- )
164     <rename> qualified-vocabs push ;
165
166 : use-words ( assoc -- )
167     <extra-words> qualified-vocabs push ;
168
169 : unuse-words ( assoc -- )
170     <extra-words> qualified-vocabs remove! drop ;
171
172 TUPLE: ambiguous-use-error words ;
173
174 : <ambiguous-use-error> ( words -- error restarts )
175     [ ambiguous-use-error boa ] [ word-restarts ] bi ;
176
177 <PRIVATE
178
179 : (vocab-search) ( name assocs -- words n )
180     [ words>> (lookup) ] with map sift dup length ;
181
182 : vocab-search ( name manifest -- word/f )
183     search-vocabs>>
184     (vocab-search) {
185         { 0 [ drop f ] }
186         { 1 [ first ] }
187         [
188             drop <ambiguous-use-error> throw-restarts
189             dup [ vocabulary>> ] [ name>> 1array ] bi add-words-from
190         ]
191     } case ;
192
193 : qualified-search ( name manifest -- word/f )
194     qualified-vocabs>>
195     (vocab-search) 0 = [ drop f ] [ last ] if ;
196
197 PRIVATE>
198
199 : search-manifest ( name manifest -- word/f )
200     2dup qualified-search dup [ 2nip ] [ drop vocab-search ] if ;
201
202 : search ( name -- word/f )
203     manifest get search-manifest ;
204
205 <PRIVATE
206
207 GENERIC: update ( search-path-elt -- valid? )
208
209 : trim-forgotten ( qualified-vocab -- valid? )
210     [ [ nip "forgotten" word-prop ] assoc-reject ] change-words
211     words>> assoc-empty? not ;
212
213 M: from update trim-forgotten ;
214 M: rename update trim-forgotten ;
215 M: extra-words update trim-forgotten ;
216 M: exclude update trim-forgotten ;
217
218 M: qualified update
219     dup vocab>> lookup-vocab [
220         dup [ prefix>> ] [ vocab>> load-vocab ] bi qualified-words
221         >>words
222     ] [ drop f ] if ;
223
224 M: vocab update dup name>> lookup-vocab eq? ;
225
226 : update-current-vocab ( manifest -- manifest )
227     [ dup [ name>> lookup-vocab ] when ] change-current-vocab ; inline
228
229 : compute-search-vocabs ( manifest -- search-vocab-names search-vocabs )
230     search-vocab-names>> members dup length <vector> [
231         [ push ] curry [ when* ] curry
232         [ lookup-vocab dup ] prepose filter fast-set
233     ] keep ; inline
234
235 : update-search-vocabs ( manifest -- manifest )
236     dup compute-search-vocabs
237     [ >>search-vocab-names ] [ >>search-vocabs ] bi* ; inline
238
239 : update-qualified-vocabs ( manifest -- manifest )
240     dup qualified-vocabs>> [ update ] filter! drop ; inline
241
242 : update-manifest ( manifest -- manifest )
243     update-current-vocab
244     update-search-vocabs
245     update-qualified-vocabs ; inline
246
247 M: manifest definitions-changed
248     nip update-manifest drop ;
249
250 PRIVATE>
251
252 : with-manifest ( quot -- )
253     <manifest> manifest [
254         [ call ] [
255             [ manifest get add-definition-observer call ]
256             [ manifest get remove-definition-observer ]
257             [ ]
258             cleanup
259         ] if-bootstrapping
260     ] with-variable ; inline