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 : parse-word ( string -- word/number )
45 dup string>number [ ] [ no-word ] ?if
48 : scan-word ( -- word/number/f )
49 scan dup [ parse-word ] when ;
51 ERROR: staging-violation word ;
53 : execute-parsing ( accum word -- accum )
54 dup changed-definitions get key? [ staging-violation ] when
55 execute( accum -- accum ) ;
57 : scan-object ( -- object )
58 scan-word dup parsing-word?
59 [ V{ } clone swap execute-parsing first ] when ;
61 : parse-step ( accum end -- accum ? )
63 { [ 2dup eq? ] [ 2drop f ] }
64 { [ dup not ] [ drop unexpected-eof t ] }
65 { [ dup delimiter? ] [ unexpected t ] }
66 { [ dup parsing-word? ] [ nip execute-parsing t ] }
70 : (parse-until) ( accum end -- accum )
71 [ parse-step ] keep swap [ (parse-until) ] [ drop ] if ;
73 : parse-until ( end -- vec )
74 100 <vector> swap (parse-until) ;
76 SYMBOL: quotation-parser
78 HOOK: parse-quotation quotation-parser ( -- quot )
80 M: f parse-quotation \ ] parse-until >quotation ;
82 : (parse-lines) ( lexer -- quot )
83 [ f parse-until >quotation ] with-lexer ;
85 : parse-lines ( lines -- quot )
86 lexer-factory get call( lines -- lexer ) (parse-lines) ;
88 : parse-literal ( accum end quot -- accum )
89 [ parse-until ] dip call suffix! ; inline
91 : parse-definition ( -- quot )
92 \ ; parse-until >quotation ;
94 : (:) ( -- word def effect )
97 parse-definition swap ;
101 : scan-base ( base -- n )
102 scan swap base> [ bad-number ] unless* ;
104 : parse-base ( parsed base -- parsed )
107 SYMBOL: bootstrap-syntax
109 : with-file-vocabs ( quot -- )
111 <manifest> manifest set
113 bootstrap-syntax get [ use-words ] when*
115 ] with-scope ; inline
117 SYMBOL: print-use-hook
119 print-use-hook [ [ ] ] initialize
121 : parse-fresh ( lines -- quot )
124 auto-used? [ print-use-hook get call( -- ) ] when
127 : parsing-file ( file -- )
128 "quiet" get [ drop ] [ "Loading " write print flush ] if ;
130 : filter-moved ( assoc1 assoc2 -- seq )
131 swap assoc-diff keys [
133 { [ dup where dup [ first ] when file get path>> = not ] [ f ] }
134 { [ dup reader-method? ] [ f ] }
135 { [ dup writer-method? ] [ f ] }
140 : removed-definitions ( -- assoc1 assoc2 )
141 new-definitions old-definitions
142 [ get first2 assoc-union ] bi@ ;
144 : removed-classes ( -- assoc1 assoc2 )
145 new-definitions old-definitions
148 : forget-removed-definitions ( -- )
149 removed-definitions filter-moved forget-all ;
151 : reset-removed-classes ( -- )
153 filter-moved [ class? ] filter [ forget-class ] each ;
155 : fix-class-words ( -- )
156 #! If a class word had a compound definition which was
157 #! removed, it must go back to being a symbol.
158 new-definitions get first2
159 filter-moved [ [ reset-generic ] [ define-symbol ] bi ] each ;
161 : forget-smudged ( -- )
162 forget-removed-definitions
163 reset-removed-classes
166 : finish-parsing ( lines quot -- )
168 [ record-top-level-form ]
169 [ record-definitions ]
173 : parse-stream ( stream name -- quot )
176 stream-lines dup parse-fresh
177 [ nip ] [ finish-parsing ] 2bi
180 ] with-compilation-unit ;
182 : parse-file-restarts ( file -- restarts )
183 "Load " " again" surround t 2array 1array ;
185 : parse-file ( file -- quot )
187 [ parsing-file ] keep
188 [ utf8 <file-reader> ] keep
191 over parse-file-restarts rethrow-restarts
195 : run-file ( file -- )
196 parse-file call( -- ) ;
198 : ?run-file ( path -- )
199 dup exists? [ run-file ] [ drop ] if ;