1 ! Copyright (C) 2005, 2008 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 quotations io
5 combinators sorting splitting math.parser effects continuations
6 io.files io.streams.string vocabs io.encodings.utf8 source-files
7 classes hashtables compiler.errors compiler.units accessors sets
12 file get lexer get line>> 2dup and
13 [ >r path>> r> 2array ] [ 2drop f ] if ;
15 : save-location ( definition -- )
16 location remember-definition ;
20 t parser-notes set-global
22 : parser-notes? ( -- ? )
23 parser-notes get "quiet" get not and ;
27 file get [ path>> write ":" write ] when*
28 lexer get line>> number>string write ": " write
29 "Note: " write dup print
36 vocab-words use get push ;
41 : add-use ( seq -- ) [ use+ ] each ;
44 [ vocab-words ] V{ } map-as sift use set ;
46 : check-vocab-string ( name -- name )
48 [ "Vocabulary name must be a string" throw ] unless ;
51 check-vocab-string dup in set create-vocab (use+) ;
53 M: parsing-word stack-effect drop (( parsed -- parsed )) ;
55 ERROR: no-current-vocab ;
57 : current-vocab ( -- str )
58 in get [ no-current-vocab ] unless* ;
60 : create-in ( str -- word )
61 current-vocab create dup set-word dup save-location ;
63 : CREATE ( -- word ) scan create-in ;
65 : CREATE-WORD ( -- word ) CREATE dup reset-generic ;
67 : word-restarts ( possibilities -- restarts )
70 "Use the " swap vocabulary>> " vocabulary" 3append
74 TUPLE: no-word-error name ;
76 : no-word ( name -- newword )
78 swap words-named [ forward-reference? not ] filter
79 word-restarts throw-restarts
80 dup vocabulary>> (use+) ;
82 : check-forward ( str word -- word/f )
83 dup forward-reference? [
87 [ forward-reference? not ] find nip
92 : search ( str -- word/f )
93 dup use get assoc-stack check-forward ;
95 : scan-word ( -- word/number/f )
98 dup string>number [ ] [ no-word ] ?if
102 ERROR: staging-violation word ;
104 : execute-parsing ( word -- )
105 dup changed-definitions get key? [ staging-violation ] when
108 : scan-object ( -- object )
109 scan-word dup parsing-word?
110 [ V{ } clone swap execute-parsing first ] when ;
112 : parse-step ( accum end -- accum ? )
114 { [ 2dup eq? ] [ 2drop f ] }
115 { [ dup not ] [ drop unexpected-eof t ] }
116 { [ dup delimiter? ] [ unexpected t ] }
117 { [ dup parsing-word? ] [ nip execute-parsing t ] }
121 : (parse-until) ( accum end -- accum )
122 dup >r parse-step [ r> (parse-until) ] [ r> drop ] if ;
124 : parse-until ( end -- vec )
125 100 <vector> swap (parse-until) ;
127 : parsed ( accum obj -- accum ) over push ;
129 : (parse-lines) ( lexer -- quot )
130 [ f parse-until >quotation ] with-lexer ;
132 : parse-lines ( lines -- quot )
133 lexer-factory get call (parse-lines) ;
135 : parse-literal ( accum end quot -- accum )
136 >r parse-until r> call parsed ; inline
138 : parse-definition ( -- quot )
139 \ ; parse-until >quotation ;
141 : (:) ( -- word def ) CREATE-WORD parse-definition ;
145 : parse-base ( parsed base -- parsed )
146 scan swap base> [ bad-number ] unless* parsed ;
148 SYMBOL: bootstrap-syntax
150 : with-file-vocabs ( quot -- )
152 f in set { "syntax" } set-use
153 bootstrap-syntax get [ use get push ] when*
155 ] with-scope ; inline
157 SYMBOL: interactive-vocabs
200 } interactive-vocabs set-global
202 : with-interactive-vocabs ( quot -- )
205 interactive-vocabs get set-use
207 ] with-scope ; inline
209 : parse-fresh ( lines -- quot )
210 [ parse-lines ] with-file-vocabs ;
212 : parsing-file ( file -- )
216 "Loading " write print flush
219 : filter-moved ( assoc1 assoc2 -- seq )
221 drop where dup [ first ] when
223 ] assoc-filter keys ;
225 : removed-definitions ( -- assoc1 assoc2 )
226 new-definitions old-definitions
227 [ get first2 assoc-union ] bi@ ;
229 : removed-classes ( -- assoc1 assoc2 )
230 new-definitions old-definitions
233 : forget-removed-definitions ( -- )
234 removed-definitions filter-moved forget-all ;
236 : reset-removed-classes ( -- )
238 filter-moved [ class? ] filter [ forget-class ] each ;
240 : fix-class-words ( -- )
241 #! If a class word had a compound definition which was
242 #! removed, it must go back to being a symbol.
243 new-definitions get first2
244 filter-moved [ [ reset-generic ] [ define-symbol ] bi ] each ;
246 : forget-smudged ( -- )
247 forget-removed-definitions
248 reset-removed-classes
251 : finish-parsing ( lines quot -- )
254 [ record-definitions ]
258 : parse-stream ( stream name -- quot )
261 lines dup parse-fresh
265 ] with-compilation-unit ;
267 : parse-file-restarts ( file -- restarts )
268 "Load " swap " again" 3append t 2array 1array ;
270 : parse-file ( file -- quot )
273 [ parsing-file ] keep
274 [ utf8 <file-reader> ] keep
276 ] with-compiler-errors
278 over parse-file-restarts rethrow-restarts
282 : run-file ( file -- )
283 [ dup parse-file call ] assert-depth drop ;
285 : ?run-file ( path -- )
286 dup exists? [ run-file ] [ drop ] if ;