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 ;
23 : CREATE ( -- word ) scan create-in ;
25 : CREATE-WORD ( -- word ) CREATE dup reset-generic ;
29 : no-word-restarted ( restart-value -- word )
33 [ "Added \"" "\" vocabulary to search path" surround note. ] bi
36 : ignore-forwards ( seq -- seq' )
37 [ forward-reference? not ] filter ;
39 : private? ( word -- ? ) vocabulary>> ".private" tail? ;
41 : ignore-privates ( seq -- seq' )
42 dup [ private? ] all? [ [ private? not ] filter ] unless ;
44 : no-word ( name -- newword )
45 dup words-named ignore-forwards
46 dup ignore-privates dup length 1 = auto-use? get and
47 [ 2nip first no-word-restarted ]
48 [ drop <no-word-error> throw-restarts no-word-restarted ]
51 : parse-word ( string -- word/number )
53 dup string>number [ ] [ no-word ] ?if
56 : scan-word ( -- word/number/f )
57 scan dup [ parse-word ] when ;
59 ERROR: staging-violation word ;
61 : execute-parsing ( accum word -- accum )
62 dup changed-definitions get key? [ staging-violation ] when
63 execute( accum -- accum ) ;
65 : scan-object ( -- object )
67 { [ dup not ] [ unexpected-eof ] }
68 { [ dup parsing-word? ] [ V{ } clone swap execute-parsing first ] }
72 : parse-step ( accum end -- accum ? )
74 { [ 2dup eq? ] [ 2drop f ] }
75 { [ dup not ] [ drop unexpected-eof t ] }
76 { [ dup delimiter? ] [ unexpected t ] }
77 { [ dup parsing-word? ] [ nip execute-parsing t ] }
81 : (parse-until) ( accum end -- accum )
82 [ parse-step ] keep swap [ (parse-until) ] [ drop ] if ;
84 : parse-until ( end -- vec )
85 100 <vector> swap (parse-until) ;
87 SYMBOL: quotation-parser
89 HOOK: parse-quotation quotation-parser ( -- quot )
91 M: f parse-quotation \ ] parse-until >quotation ;
93 : (parse-lines) ( lexer -- quot )
94 [ f parse-until >quotation ] with-lexer ;
96 : parse-lines ( lines -- quot )
97 lexer-factory get call( lines -- lexer ) (parse-lines) ;
99 : parse-literal ( accum end quot -- accum )
100 [ parse-until ] dip call suffix! ; inline
102 : parse-definition ( -- quot )
103 \ ; parse-until >quotation ;
107 : scan-base ( base -- n )
108 scan swap base> [ bad-number ] unless* ;
110 : parse-base ( parsed base -- parsed )
113 SYMBOL: bootstrap-syntax
115 : with-file-vocabs ( quot -- )
118 bootstrap-syntax get [ use-words ] when*
120 ] with-manifest ; inline
122 SYMBOL: print-use-hook
124 print-use-hook [ [ ] ] initialize
126 : parse-fresh ( lines -- quot )
129 auto-used? [ print-use-hook get call( -- ) ] when
132 : parsing-file ( file -- )
133 "quiet" get [ drop ] [ "Loading " write print flush ] if ;
135 : filter-moved ( assoc1 assoc2 -- seq )
136 swap assoc-diff keys [
138 { [ dup where dup [ first ] when file get path>> = not ] [ f ] }
139 { [ dup reader-method? ] [ f ] }
140 { [ dup writer-method? ] [ f ] }
145 : removed-definitions ( -- assoc1 assoc2 )
146 new-definitions old-definitions
147 [ get first2 assoc-union ] bi@ ;
149 : removed-classes ( -- assoc1 assoc2 )
150 new-definitions old-definitions
153 : forget-removed-definitions ( -- )
154 removed-definitions filter-moved forget-all ;
156 : reset-removed-classes ( -- )
158 filter-moved [ class? ] filter [ forget-class ] each ;
160 : fix-class-words ( -- )
161 #! If a class word had a compound definition which was
162 #! removed, it must go back to being a symbol.
163 new-definitions get first2
164 filter-moved [ [ reset-generic ] [ define-symbol ] bi ] each ;
166 : forget-smudged ( -- )
167 forget-removed-definitions
168 reset-removed-classes
171 : finish-parsing ( lines quot -- )
173 [ record-top-level-form ]
174 [ record-definitions ]
178 : parse-stream ( stream name -- quot )
181 stream-lines dup parse-fresh
182 [ nip ] [ finish-parsing ] 2bi
185 ] with-compilation-unit ;
187 : parse-file-restarts ( file -- restarts )
188 "Load " " again" surround t 2array 1array ;
190 : parse-file ( file -- quot )
192 [ parsing-file ] keep
193 [ utf8 <file-reader> ] keep
196 over parse-file-restarts rethrow-restarts
200 : run-file ( file -- )
201 parse-file call( -- ) ;
203 : ?run-file ( path -- )
204 dup exists? [ run-file ] [ drop ] if ;