]> gitweb.factorcode.org Git - factor.git/blob - core/vocabs/parser/parser.factor
dccf1f1be6afbdc69b92f175d62412f9c0da2c9e
[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: assocs hashtables kernel namespaces sequences
5 sets strings vocabs sorting accessors arrays compiler.units
6 combinators vectors splitting continuations math words
7 parser.notes ;
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 hashtable }
27 { search-vocabs vector }
28 { qualified-vocabs vector }
29 { auto-used vector } ;
30
31 : <manifest> ( -- manifest )
32     manifest new
33         H{ } 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 : (add-qualified) ( qualified -- )
57     manifest get qualified-vocabs>> push ;
58
59 : (from) ( vocab words -- vocab words words' vocab )
60     2dup swap load-vocab ;
61
62 : extract-words ( seq vocab -- assoc )
63     [ words>> extract-keys dup ] [ name>> ] bi
64     [ swap [ 2drop ] [ no-word-in-vocab ] if ] curry assoc-each ;
65
66 : excluding-words ( seq vocab -- assoc )
67     [ nip words>> ] [ extract-words ] 2bi assoc-diff ;
68
69 : qualified-words ( prefix vocab -- assoc )
70     words>> swap [ swap [ swap ":" glue ] dip ] curry assoc-map ;
71
72 : (lookup) ( name assoc -- word/f )
73     at* [ dup forward-reference? [ drop f ] when ] when ;
74
75 : (use-words) ( assoc -- extra-words seq )
76     <extra-words> manifest get qualified-vocabs>> ;
77
78 PRIVATE>
79
80 : set-current-vocab ( name -- )
81     create-vocab
82     [ manifest get current-vocab<< ] [ (add-qualified) ] bi ;
83
84 : with-current-vocab ( name quot -- )
85     manifest get clone manifest [
86         [ set-current-vocab ] dip call
87     ] with-variable ; inline
88
89 TUPLE: no-current-vocab-error ;
90
91 : no-current-vocab ( -- vocab )
92     \ no-current-vocab-error boa
93     { { "Define words in scratchpad vocabulary" "scratchpad" } }
94     throw-restarts dup set-current-vocab ;
95
96 : current-vocab ( -- vocab )
97     manifest get current-vocab>> [ no-current-vocab ] unless* ;
98
99 : begin-private ( -- )
100     current-vocab name>> ".private" ?tail
101     [ drop ] [ ".private" append set-current-vocab ] if ;
102
103 : end-private ( -- )
104     current-vocab name>> ".private" ?tail
105     [ set-current-vocab ] [ drop ] if ;
106
107 : using-vocab? ( vocab -- ? )
108     vocab-name manifest get search-vocab-names>> key? ;
109
110 : use-vocab ( vocab -- )
111     dup using-vocab?
112     [ vocab-name "Already using ``" "'' vocabulary" surround note. ] [
113         manifest get
114         [ [ load-vocab ] dip search-vocabs>> push ]
115         [ [ vocab-name ] dip search-vocab-names>> conjoin ]
116         2bi
117     ] if ;
118
119 : auto-use-vocab ( vocab -- )
120     [ use-vocab ] [ manifest get auto-used>> push ] bi ;
121
122 : auto-used? ( -- ? ) manifest get auto-used>> length 0 > ;
123
124 : unuse-vocab ( vocab -- )
125     dup using-vocab? [
126         manifest get
127         [ [ load-vocab ] dip search-vocabs>> remove-eq! drop ]
128         [ [ vocab-name ] dip search-vocab-names>> delete-at ]
129         2bi
130     ] [ drop ] if ;
131
132 TUPLE: qualified vocab prefix words ;
133
134 : <qualified> ( vocab prefix -- qualified )
135     (from) qualified-words qualified boa ;
136
137 : add-qualified ( vocab prefix -- )
138     <qualified> (add-qualified) ;
139
140 TUPLE: from vocab names words ;
141
142 : <from> ( vocab words -- from )
143     (from) extract-words from boa ;
144
145 : add-words-from ( vocab words -- )
146     <from> (add-qualified) ;
147
148 TUPLE: exclude vocab names words ;
149
150 : <exclude> ( vocab words -- from )
151     (from) excluding-words exclude boa ;
152
153 : add-words-excluding ( vocab words -- )
154     <exclude> (add-qualified) ;
155
156 TUPLE: rename word vocab words ;
157
158 : <rename> ( word vocab new-name -- rename )
159     [ 2dup load-vocab words>> dupd at [ ] [ swap no-word-in-vocab ] ?if ] dip
160     associate rename boa ;
161
162 : add-renamed-word ( word vocab new-name -- )
163     <rename> (add-qualified) ;
164
165 : use-words ( assoc -- ) (use-words) push ;
166
167 : unuse-words ( assoc -- ) (use-words) remove! drop ;
168
169 TUPLE: ambiguous-use-error words ;
170
171 : <ambiguous-use-error> ( words -- error restarts )
172     [ \ ambiguous-use-error boa ] [ word-restarts ] bi ;
173
174 <PRIVATE
175
176 : (vocab-search) ( name assocs -- words n )
177     [ words>> (lookup) ] with map
178     sift dup length ;
179
180 : vocab-search ( name manifest -- word/f )
181     search-vocabs>>
182     (vocab-search) {
183         { 0 [ drop f ] }
184         { 1 [ first ] }
185         [
186             drop <ambiguous-use-error> throw-restarts
187             dup [ vocabulary>> ] [ name>> 1array ] bi add-words-from
188         ]
189     } case ;
190
191 : qualified-search ( name manifest -- word/f )
192     qualified-vocabs>>
193     (vocab-search) 0 = [ drop f ] [ last ] if ;
194
195 PRIVATE>
196
197 : search-manifest ( name manifest -- word/f )
198     2dup qualified-search dup [ 2nip ] [ drop vocab-search ] if ;
199
200 : search ( name -- word/f )
201     manifest get search-manifest ;
202
203 <PRIVATE
204
205 GENERIC: update ( search-path-elt -- valid? )
206
207 : trim-forgotten ( qualified-vocab -- valid? )
208     [ [ nip "forgotten" word-prop not ] assoc-filter ] change-words
209     words>> assoc-empty? not ;
210
211 M: from update trim-forgotten ;
212 M: rename update trim-forgotten ;
213 M: extra-words update trim-forgotten ;
214 M: exclude update trim-forgotten ;
215
216 M: qualified update
217     dup vocab>> lookup-vocab [
218         dup [ prefix>> ] [ vocab>> load-vocab ] bi qualified-words
219         >>words
220     ] [ drop f ] if ;
221
222 M: vocab update dup name>> lookup-vocab eq? ;
223
224 : update-manifest ( manifest -- )
225     [ dup [ name>> lookup-vocab ] when ] change-current-vocab
226     [ [ drop lookup-vocab ] assoc-filter ] change-search-vocab-names
227     dup search-vocab-names>> keys [ lookup-vocab ] V{ } map-as >>search-vocabs
228     qualified-vocabs>> [ update ] filter! drop ;
229
230 M: manifest definitions-changed ( assoc manifest -- )
231     nip update-manifest ;
232
233 PRIVATE>
234
235 : with-manifest ( quot -- )
236     <manifest> manifest [
237         [ call ] [
238             [ manifest get add-definition-observer call ]
239             [ manifest get remove-definition-observer ]
240             [ ]
241             cleanup
242         ] if-bootstrapping
243     ] with-variable ; inline