]> gitweb.factorcode.org Git - factor.git/blob - core/parser/parser.factor
Fixes #2966
[factor.git] / core / parser / parser.factor
1 ! Copyright (C) 2005, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays classes combinators compiler.units
4 continuations definitions effects io io.encodings.utf8 io.files
5 kernel lexer math.parser namespaces parser.notes quotations
6 sequences sets slots source-files vectors vocabs vocabs.parser
7 words words.symbol ;
8 IN: parser
9
10 : location ( -- loc )
11     current-source-file get lexer get line>> 2dup and
12     [ [ path>> ] dip 2array ] [ 2drop f ] if ;
13
14 : save-location ( definition -- )
15     location remember-definition ;
16
17 M: parsing-word stack-effect drop ( parsed -- parsed ) ;
18
19 : create-word-in ( str -- word )
20     current-vocab create-word dup set-last-word dup save-location ;
21
22 SYMBOL: auto-use?
23
24 : no-word-restarted ( restart-value -- word )
25     dup word? [
26         dup vocabulary>>
27         [ auto-use-vocab ]
28         [ "Added \"" "\" vocabulary to search path" surround note. ] bi
29     ] [ create-word-in ] if ;
30
31 : ignore-forwards ( seq -- seq' )
32     [ forward-reference? ] reject ;
33
34 : private? ( word -- ? ) vocabulary>> ".private" tail? ;
35
36 : use-first-word? ( words -- ? )
37     [ length 1 = ] [ ?first dup [ private? not ] [ ] ?if ] bi and
38     auto-use? get and ;
39
40 ! True branch is a singleton public word with no name conflicts
41 ! False branch, singleton private words need confirmation regardless
42 ! of name conflicts
43 : no-word ( name -- newword )
44     dup words-named ignore-forwards
45     dup use-first-word? [ nip first ] [ <no-word-error> throw-restarts ] if
46     no-word-restarted ;
47
48 : parse-word ( string -- word )
49     dup search [ ] [ no-word ] ?if ;
50
51 ERROR: number-expected ;
52
53 : parse-number ( string -- number )
54     string>number [ number-expected ] unless* ;
55
56 : parse-datum ( string -- word/number )
57     dup search [ ] [
58         dup string>number [ ] [ no-word ] ?if
59     ] ?if ;
60
61 : ?scan-datum ( -- word/number/f )
62     ?scan-token dup [ parse-datum ] when ;
63
64 : scan-datum ( -- word/number )
65     ?scan-datum [ \ word throw-unexpected-eof ] unless* ;
66
67 : scan-word ( -- word )
68     ?scan-token parse-word ;
69
70 : scan-number ( -- number )
71     ?scan-token parse-number ;
72
73 ERROR: invalid-word-name string ;
74
75 : check-word-name ( string -- string )
76     dup "\"" = [ t ] [ dup string>number ] if
77     [ invalid-word-name ] when ;
78
79 : scan-word-name ( -- string )
80     scan-token check-word-name ;
81
82 : scan-new ( -- word )
83     scan-word-name create-word-in ;
84
85 : scan-new-word ( -- word )
86     scan-new dup reset-generic ;
87
88 ERROR: staging-violation word ;
89
90 : (execute-parsing) ( accum word -- accum )
91     dup push-parsing-word
92     execute( accum -- accum )
93     pop-parsing-word ; inline
94
95 : execute-parsing ( accum word -- accum )
96     dup changed-definitions get in? [ staging-violation ] when
97     (execute-parsing) ;
98
99 : ?execute-parsing ( word/number -- seq )
100     dup parsing-word?
101     [ V{ } clone swap execute-parsing ] [ 1array ] if ;
102
103 : scan-object ( -- object )
104     scan-datum
105     dup parsing-word? [
106         V{ } clone swap execute-parsing first
107     ] when ;
108
109 ERROR: classoid-expected object ;
110
111 : scan-class ( -- class )
112     scan-object \ f or
113     dup classoid? [ classoid-expected ] unless ;
114
115 : parse-until-step ( accum end -- accum ? )
116     ?scan-datum {
117         { [ 2dup eq? ] [ 2drop f ] }
118         { [ dup not ] [ drop throw-unexpected-eof t ] }
119         { [ dup delimiter? ] [ unexpected t ] }
120         { [ dup parsing-word? ] [ nip execute-parsing t ] }
121         [ pick push drop t ]
122     } cond ;
123
124 : (parse-until) ( accum end -- accum )
125     [ parse-until-step ] keep swap [ (parse-until) ] [ drop ] if ;
126
127 : parse-until ( end -- vec )
128     100 <vector> swap (parse-until) ;
129
130 SYMBOL: quotation-parser
131
132 HOOK: parse-quotation quotation-parser ( -- quot )
133
134 M: f parse-quotation \ ] parse-until >quotation ;
135
136 : (parse-lines) ( lexer -- quot )
137     [ f parse-until >quotation ] with-lexer ;
138
139 : parse-lines ( lines -- quot )
140     >array <lexer> (parse-lines) ;
141
142 : parse-literal ( accum end quot -- accum )
143     [ parse-until ] dip call suffix! ; inline
144
145 : parse-definition ( -- quot )
146     \ ; parse-until >quotation ;
147
148 : parse-array-def ( -- array )
149     \ ; parse-until >array ;
150
151 ERROR: bad-number ;
152
153 : scan-base ( base -- n )
154     scan-token swap base> [ bad-number ] unless* ;
155
156 SYMBOL: bootstrap-syntax
157
158 : with-file-vocabs ( quot -- )
159     [
160         "syntax" use-vocab
161         bootstrap-syntax get [ use-words ] when*
162         call
163     ] with-manifest ; inline
164
165 : parse-fresh ( lines -- quot )
166     [
167         parse-lines
168         auto-used? [ print-use-hook get call( -- ) ] when
169     ] with-file-vocabs ;
170
171 : parsing-file ( path -- )
172     parser-quiet? get [ drop ] [ "Loading " write print flush ] if ;
173
174 : filter-moved ( set1 set2 -- seq )
175     swap diff members [
176         {
177             { [ dup where ?first current-source-file get path>> = not ] [ f ] }
178             { [ dup reader-method? ] [ f ] }
179             { [ dup writer-method? ] [ f ] }
180             [ t ]
181         } cond nip
182     ] filter ;
183
184 : removed-definitions ( -- set1 set2 )
185     new-definitions old-definitions
186     [ get first2 union ] bi@ ;
187
188 : removed-classes ( -- set1 set2 )
189     new-definitions old-definitions
190     [ get second ] bi@ ;
191
192 : forget-removed-definitions ( -- )
193     removed-definitions filter-moved forget-all ;
194
195 : reset-removed-classes ( -- )
196     removed-classes
197     filter-moved [ class? ] filter [ forget-class ] each ;
198
199 : fix-class-words ( -- )
200     ! If a class word had a compound definition which was
201     ! removed, it must go back to being a symbol.
202     new-definitions get first2
203     filter-moved [ [ reset-generic ] [ define-symbol ] bi ] each ;
204
205 : forget-smudged ( -- )
206     forget-removed-definitions
207     reset-removed-classes
208     fix-class-words ;
209
210 : finish-parsing ( lines quot -- )
211     current-source-file get
212     [ record-top-level-form ]
213     [ record-definitions ]
214     [ record-checksum ]
215     tri ;
216
217 : parse-stream ( stream name -- quot )
218     [
219         [
220             stream-lines dup parse-fresh
221             [ nip ] [ finish-parsing ] 2bi
222             forget-smudged
223         ] with-source-file
224     ] with-compilation-unit ;
225
226 : parse-file-restarts ( path -- restarts )
227     "Load " " again" surround t 2array 1array ;
228
229 : parse-file ( path -- quot )
230     [
231         [ parsing-file ] keep
232         [ utf8 <file-reader> ] keep
233         parse-stream
234     ] [
235         over parse-file-restarts rethrow-restarts
236         drop parse-file
237     ] recover ;
238
239 : run-file ( path -- )
240     parse-file call( -- ) ;
241
242 : ?run-file ( path -- )
243     dup file-exists? [ run-file ] [ drop ] if ;
244
245 ERROR: version-control-merge-conflict ;