1 ! Copyright (C) 2005, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays definitions generic assocs kernel math namespaces
4 sequences strings vectors words words.symbol quotations io
5 combinators sorting splitting math.parser effects continuations
6 io.files vocabs io.encodings.utf8 source-files classes
7 hashtables compiler.units accessors sets lexer vocabs.parser
12 file get lexer get line>> 2dup and
13 [ [ path>> ] dip 2array ] [ 2drop f ] if ;
15 : save-location ( definition -- )
16 location remember-definition ;
18 M: parsing-word stack-effect drop ( parsed -- parsed ) ;
20 : create-in ( str -- word )
21 current-vocab create dup set-word dup save-location ;
25 : no-word-restarted ( restart-value -- word )
29 [ "Added \"" "\" vocabulary to search path" surround note. ] bi
32 : ignore-forwards ( seq -- seq' )
33 [ forward-reference? not ] filter ;
35 : private? ( word -- ? ) vocabulary>> ".private" tail? ;
37 : ignore-privates ( seq -- seq' )
38 dup [ private? ] all? [ [ private? not ] filter ] unless ;
40 : no-word ( name -- newword )
41 dup words-named ignore-forwards
42 dup ignore-privates dup length 1 = auto-use? get and
43 [ 2nip first no-word-restarted ]
44 [ drop <no-word-error> throw-restarts no-word-restarted ]
47 : parse-word ( string -- word )
48 dup search [ ] [ no-word ] ?if ;
50 ERROR: number-expected ;
52 : parse-number ( string -- number )
53 string>number [ number-expected ] unless* ;
55 : parse-datum ( string -- word/number )
57 dup string>number [ ] [ no-word ] ?if
60 : (scan-datum) ( -- word/number/f )
61 (scan-token) dup [ parse-datum ] when ;
63 : scan-datum ( -- word/number )
64 (scan-datum) [ \ word unexpected-eof ] unless* ;
66 : scan-word ( -- word )
67 (scan-token) parse-word ;
69 : scan-number ( -- number )
70 (scan-token) parse-number ;
72 : scan-word-name ( -- string )
75 "Word names cannot be numbers" throw
78 : scan-new ( -- word )
79 scan-word-name create-in ;
81 : scan-new-word ( -- word )
82 scan-new dup reset-generic ;
84 ERROR: staging-violation word ;
86 : (execute-parsing) ( accum word -- accum )
88 execute( accum -- accum )
89 pop-parsing-word ; inline
91 : execute-parsing ( accum word -- accum )
92 dup changed-definitions get key? [ staging-violation ] when
95 : scan-object ( -- object )
98 V{ } clone swap execute-parsing first
101 : parse-step ( accum end -- accum ? )
103 { [ 2dup eq? ] [ 2drop f ] }
104 { [ dup not ] [ drop unexpected-eof t ] }
105 { [ dup delimiter? ] [ unexpected t ] }
106 { [ dup parsing-word? ] [ nip execute-parsing t ] }
110 : (parse-until) ( accum end -- accum )
111 [ parse-step ] keep swap [ (parse-until) ] [ drop ] if ;
113 : parse-until ( end -- vec )
114 100 <vector> swap (parse-until) ;
116 SYMBOL: quotation-parser
118 HOOK: parse-quotation quotation-parser ( -- quot )
120 M: f parse-quotation \ ] parse-until >quotation ;
122 : (parse-lines) ( lexer -- quot )
123 [ f parse-until >quotation ] with-lexer ;
125 : parse-lines ( lines -- quot )
126 lexer-factory get call( lines -- lexer ) (parse-lines) ;
128 : parse-literal ( accum end quot -- accum )
129 [ parse-until ] dip call suffix! ; inline
131 : parse-definition ( -- quot )
132 \ ; parse-until >quotation ;
136 : scan-base ( base -- n )
137 scan-token swap base> [ bad-number ] unless* ;
139 : parse-base ( parsed base -- parsed )
142 SYMBOL: bootstrap-syntax
144 : with-file-vocabs ( quot -- )
147 bootstrap-syntax get [ use-words ] when*
149 ] with-manifest ; inline
151 SYMBOL: print-use-hook
153 print-use-hook [ [ ] ] initialize
155 : parse-fresh ( lines -- quot )
158 auto-used? [ print-use-hook get call( -- ) ] when
161 : parsing-file ( file -- )
162 parser-quiet? get [ drop ] [ "Loading " write print flush ] if ;
164 : filter-moved ( assoc1 assoc2 -- seq )
165 swap assoc-diff keys [
167 { [ dup where dup [ first ] when file get path>> = not ] [ f ] }
168 { [ dup reader-method? ] [ f ] }
169 { [ dup writer-method? ] [ f ] }
174 : removed-definitions ( -- assoc1 assoc2 )
175 new-definitions old-definitions
176 [ get first2 assoc-union ] bi@ ;
178 : removed-classes ( -- assoc1 assoc2 )
179 new-definitions old-definitions
182 : forget-removed-definitions ( -- )
183 removed-definitions filter-moved forget-all ;
185 : reset-removed-classes ( -- )
187 filter-moved [ class? ] filter [ forget-class ] each ;
189 : fix-class-words ( -- )
190 #! If a class word had a compound definition which was
191 #! removed, it must go back to being a symbol.
192 new-definitions get first2
193 filter-moved [ [ reset-generic ] [ define-symbol ] bi ] each ;
195 : forget-smudged ( -- )
196 forget-removed-definitions
197 reset-removed-classes
200 : finish-parsing ( lines quot -- )
202 [ record-top-level-form ]
203 [ record-definitions ]
207 : parse-stream ( stream name -- quot )
210 stream-lines dup parse-fresh
211 [ nip ] [ finish-parsing ] 2bi
214 ] with-compilation-unit ;
216 : parse-file-restarts ( file -- restarts )
217 "Load " " again" surround t 2array 1array ;
219 : parse-file ( file -- quot )
221 [ parsing-file ] keep
222 [ utf8 <file-reader> ] keep
225 over parse-file-restarts rethrow-restarts
229 : run-file ( file -- )
230 parse-file call( -- ) ;
232 : ?run-file ( path -- )
233 dup exists? [ run-file ] [ drop ] if ;
235 ERROR: version-control-merge-conflict ;