1 ! Copyright (C) 2005, 2009 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 ;
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 ] when*
29 "Note:" print dup print
32 M: parsing-word stack-effect drop (( parsed -- parsed )) ;
34 TUPLE: no-current-vocab ;
36 : no-current-vocab ( -- vocab )
37 \ no-current-vocab boa
38 { { "Define words in scratchpad vocabulary" "scratchpad" } }
39 throw-restarts dup set-in ;
41 : current-vocab ( -- str )
42 in get [ no-current-vocab ] unless* ;
44 : create-in ( str -- word )
45 current-vocab create dup set-word dup save-location ;
47 : CREATE ( -- word ) scan create-in ;
49 : CREATE-WORD ( -- word ) CREATE dup reset-generic ;
55 : no-word-restarted ( restart-value -- word )
59 [ amended-use get dup [ push ] [ 2drop ] if ]
60 [ "Added \"" "\" vocabulary to search path" surround note. ]
64 : no-word ( name -- newword )
65 dup words-named [ forward-reference? not ] filter
66 dup length 1 = auto-use? get and
67 [ nip first no-word-restarted ]
68 [ <no-word-error> throw-restarts no-word-restarted ]
71 : scan-word ( -- word/number/f )
74 dup string>number [ ] [ no-word ] ?if
78 ERROR: staging-violation word ;
80 : execute-parsing ( accum word -- accum )
81 dup changed-definitions get key? [ staging-violation ] when
82 execute( accum -- accum ) ;
84 : scan-object ( -- object )
85 scan-word dup parsing-word?
86 [ V{ } clone swap execute-parsing first ] when ;
88 : parse-step ( accum end -- accum ? )
90 { [ 2dup eq? ] [ 2drop f ] }
91 { [ dup not ] [ drop unexpected-eof t ] }
92 { [ dup delimiter? ] [ unexpected t ] }
93 { [ dup parsing-word? ] [ nip execute-parsing t ] }
97 : (parse-until) ( accum end -- accum )
98 [ parse-step ] keep swap [ (parse-until) ] [ drop ] if ;
100 : parse-until ( end -- vec )
101 100 <vector> swap (parse-until) ;
103 SYMBOL: quotation-parser
105 HOOK: parse-quotation quotation-parser ( -- quot )
107 M: f parse-quotation \ ] parse-until >quotation ;
109 : parsed ( accum obj -- accum ) over push ;
111 : (parse-lines) ( lexer -- quot )
112 [ f parse-until >quotation ] with-lexer ;
114 : parse-lines ( lines -- quot )
115 lexer-factory get call( lines -- lexer ) (parse-lines) ;
117 : parse-literal ( accum end quot -- accum )
118 [ parse-until ] dip call parsed ; inline
120 : parse-definition ( -- quot )
121 \ ; parse-until >quotation ;
123 : (:) ( -- word def effect )
126 parse-definition swap ;
130 : parse-base ( parsed base -- parsed )
131 scan swap base> [ bad-number ] unless* parsed ;
133 SYMBOL: bootstrap-syntax
135 : with-file-vocabs ( quot -- )
137 f in set { "syntax" } set-use
138 bootstrap-syntax get [ use get push ] when*
140 ] with-scope ; inline
142 SYMBOL: interactive-vocabs
194 } interactive-vocabs set-global
196 : with-interactive-vocabs ( quot -- )
199 interactive-vocabs get set-use
201 ] with-scope ; inline
203 SYMBOL: print-use-hook
205 print-use-hook [ [ ] ] initialize
207 : parse-fresh ( lines -- quot )
209 V{ } clone amended-use set
211 amended-use get empty? [ print-use-hook get call( -- ) ] unless
214 : parsing-file ( file -- )
215 "quiet" get [ drop ] [ "Loading " write print flush ] if ;
217 : filter-moved ( assoc1 assoc2 -- seq )
218 swap assoc-diff keys [
220 { [ dup where dup [ first ] when file get path>> = not ] [ f ] }
221 { [ dup reader-method? ] [ f ] }
222 { [ dup writer-method? ] [ f ] }
227 : removed-definitions ( -- assoc1 assoc2 )
228 new-definitions old-definitions
229 [ get first2 assoc-union ] bi@ ;
231 : removed-classes ( -- assoc1 assoc2 )
232 new-definitions old-definitions
235 : forget-removed-definitions ( -- )
236 removed-definitions filter-moved forget-all ;
238 : reset-removed-classes ( -- )
240 filter-moved [ class? ] filter [ forget-class ] each ;
242 : fix-class-words ( -- )
243 #! If a class word had a compound definition which was
244 #! removed, it must go back to being a symbol.
245 new-definitions get first2
246 filter-moved [ [ reset-generic ] [ define-symbol ] bi ] each ;
248 : forget-smudged ( -- )
249 forget-removed-definitions
250 reset-removed-classes
253 : finish-parsing ( lines quot -- )
255 [ record-top-level-form ]
256 [ record-definitions ]
260 : parse-stream ( stream name -- quot )
263 stream-lines dup parse-fresh
264 [ nip ] [ finish-parsing ] 2bi
267 ] with-compilation-unit ;
269 : parse-file-restarts ( file -- restarts )
270 "Load " " again" surround t 2array 1array ;
272 : parse-file ( file -- quot )
274 [ parsing-file ] keep
275 [ utf8 <file-reader> ] keep
278 over parse-file-restarts rethrow-restarts
282 : run-file ( file -- )
283 parse-file call( -- ) ;
285 : ?run-file ( path -- )
286 dup exists? [ run-file ] [ drop ] if ;