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
8 effects.parser slots parser.notes ;
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 : no-word ( name -- newword )
37 dup words-named [ forward-reference? not ] filter
38 dup length 1 = auto-use? get and
39 [ nip first no-word-restarted ]
40 [ <no-word-error> throw-restarts no-word-restarted ]
43 : scan-word ( -- word/number/f )
46 dup string>number [ ] [ no-word ] ?if
50 ERROR: staging-violation word ;
52 : execute-parsing ( accum word -- accum )
53 dup changed-definitions get key? [ staging-violation ] when
54 execute( accum -- accum ) ;
56 : scan-object ( -- object )
57 scan-word dup parsing-word?
58 [ V{ } clone swap execute-parsing first ] when ;
60 : parse-step ( accum end -- accum ? )
62 { [ 2dup eq? ] [ 2drop f ] }
63 { [ dup not ] [ drop unexpected-eof t ] }
64 { [ dup delimiter? ] [ unexpected t ] }
65 { [ dup parsing-word? ] [ nip execute-parsing t ] }
69 : (parse-until) ( accum end -- accum )
70 [ parse-step ] keep swap [ (parse-until) ] [ drop ] if ;
72 : parse-until ( end -- vec )
73 100 <vector> swap (parse-until) ;
75 SYMBOL: quotation-parser
77 HOOK: parse-quotation quotation-parser ( -- quot )
79 M: f parse-quotation \ ] parse-until >quotation ;
81 : parsed ( accum obj -- accum ) over push ;
83 : (parse-lines) ( lexer -- quot )
84 [ f parse-until >quotation ] with-lexer ;
86 : parse-lines ( lines -- quot )
87 lexer-factory get call( lines -- lexer ) (parse-lines) ;
89 : parse-literal ( accum end quot -- accum )
90 [ parse-until ] dip call parsed ; inline
92 : parse-definition ( -- quot )
93 \ ; parse-until >quotation ;
95 : (:) ( -- word def effect )
98 parse-definition swap ;
102 : parse-base ( parsed base -- parsed )
103 scan swap base> [ bad-number ] unless* parsed ;
105 SYMBOL: bootstrap-syntax
107 : with-file-vocabs ( quot -- )
109 <manifest> manifest set
111 bootstrap-syntax get [ use-words ] when*
113 ] with-scope ; inline
115 SYMBOL: print-use-hook
117 print-use-hook [ [ ] ] initialize
119 : parse-fresh ( lines -- quot )
122 auto-used? [ print-use-hook get call( -- ) ] when
125 : parsing-file ( file -- )
126 "quiet" get [ drop ] [ "Loading " write print flush ] if ;
128 : filter-moved ( assoc1 assoc2 -- seq )
129 swap assoc-diff keys [
131 { [ dup where dup [ first ] when file get path>> = not ] [ f ] }
132 { [ dup reader-method? ] [ f ] }
133 { [ dup writer-method? ] [ f ] }
138 : removed-definitions ( -- assoc1 assoc2 )
139 new-definitions old-definitions
140 [ get first2 assoc-union ] bi@ ;
142 : removed-classes ( -- assoc1 assoc2 )
143 new-definitions old-definitions
146 : forget-removed-definitions ( -- )
147 removed-definitions filter-moved forget-all ;
149 : reset-removed-classes ( -- )
151 filter-moved [ class? ] filter [ forget-class ] each ;
153 : fix-class-words ( -- )
154 #! If a class word had a compound definition which was
155 #! removed, it must go back to being a symbol.
156 new-definitions get first2
157 filter-moved [ [ reset-generic ] [ define-symbol ] bi ] each ;
159 : forget-smudged ( -- )
160 forget-removed-definitions
161 reset-removed-classes
164 : finish-parsing ( lines quot -- )
166 [ record-top-level-form ]
167 [ record-definitions ]
171 : parse-stream ( stream name -- quot )
174 stream-lines dup parse-fresh
175 [ nip ] [ finish-parsing ] 2bi
178 ] with-compilation-unit ;
180 : parse-file-restarts ( file -- restarts )
181 "Load " " again" surround t 2array 1array ;
183 : parse-file ( file -- quot )
185 [ parsing-file ] keep
186 [ utf8 <file-reader> ] keep
189 over parse-file-restarts rethrow-restarts
193 : run-file ( file -- )
194 parse-file call( -- ) ;
196 : ?run-file ( path -- )
197 dup exists? [ run-file ] [ drop ] if ;