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
8 slots parser.notes classes.algebra ;
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 ;
25 : auto-use ( -- ) auto-use? on ;
27 : no-word-restarted ( restart-value -- word )
31 [ "Added \"" "\" vocabulary to search path" surround note. ] bi
34 : ignore-forwards ( seq -- seq' )
35 [ forward-reference? not ] filter ;
37 : private? ( word -- ? ) vocabulary>> ".private" tail? ;
39 : ignore-privates ( seq -- seq' )
40 dup [ private? ] all? [ [ private? not ] filter ] unless ;
42 : no-word ( name -- newword )
43 dup words-named ignore-forwards
44 dup ignore-privates dup length 1 = auto-use? get and
45 [ 2nip first no-word-restarted ]
46 [ drop <no-word-error> throw-restarts no-word-restarted ]
49 : parse-word ( string -- word )
50 dup search [ ] [ no-word ] ?if ;
52 ERROR: number-expected ;
54 : parse-number ( string -- number )
55 string>number [ number-expected ] unless* ;
57 : parse-datum ( string -- word/number )
59 dup string>number [ ] [ no-word ] ?if
62 : (scan-datum) ( -- word/number/f )
63 (scan-token) dup [ parse-datum ] when ;
65 : scan-datum ( -- word/number )
66 (scan-datum) [ \ word unexpected-eof ] unless* ;
68 : scan-word ( -- word )
69 (scan-token) parse-word ;
71 : scan-number ( -- number )
72 (scan-token) parse-number ;
74 : scan-word-name ( -- string )
77 "Word names cannot be numbers" throw
80 : scan-new ( -- word )
81 scan-word-name create-in ;
83 : scan-new-word ( -- word )
84 scan-new dup reset-generic ;
86 ERROR: staging-violation word ;
88 : (execute-parsing) ( accum word -- accum )
90 execute( accum -- accum )
91 pop-parsing-word ; inline
93 : execute-parsing ( accum word -- accum )
94 dup changed-definitions get key? [ staging-violation ] when
97 : scan-object ( -- object )
100 V{ } clone swap execute-parsing first
103 : scan-class ( -- class )
106 : parse-until-step ( accum end -- accum ? )
108 { [ 2dup eq? ] [ 2drop f ] }
109 { [ dup not ] [ drop unexpected-eof t ] }
110 { [ dup delimiter? ] [ unexpected t ] }
111 { [ dup parsing-word? ] [ nip execute-parsing t ] }
115 : (parse-until) ( accum end -- accum )
116 [ parse-until-step ] keep swap [ (parse-until) ] [ drop ] if ;
118 : parse-until ( end -- vec )
119 100 <vector> swap (parse-until) ;
121 SYMBOL: quotation-parser
123 HOOK: parse-quotation quotation-parser ( -- quot )
125 M: f parse-quotation \ ] parse-until >quotation ;
127 : (parse-lines) ( lexer -- quot )
128 [ f parse-until >quotation ] with-lexer ;
130 : parse-lines ( lines -- quot )
131 >array <lexer> (parse-lines) ;
133 : parse-literal ( accum end quot -- accum )
134 [ parse-until ] dip call suffix! ; inline
136 : parse-definition ( -- quot )
137 \ ; parse-until >quotation ;
141 : scan-base ( base -- n )
142 scan-token swap base> [ bad-number ] unless* ;
144 SYMBOL: bootstrap-syntax
146 : with-file-vocabs ( quot -- )
149 bootstrap-syntax get [ use-words ] when*
151 ] with-manifest ; inline
153 SYMBOL: print-use-hook
155 print-use-hook [ [ ] ] initialize
157 : parse-fresh ( lines -- quot )
160 auto-used? [ print-use-hook get call( -- ) ] when
163 : parsing-file ( file -- )
164 parser-quiet? get [ drop ] [ "Loading " write print flush ] if ;
166 : filter-moved ( assoc1 assoc2 -- seq )
167 swap assoc-diff keys [
169 { [ dup where dup [ first ] when file get path>> = not ] [ f ] }
170 { [ dup reader-method? ] [ f ] }
171 { [ dup writer-method? ] [ f ] }
176 : removed-definitions ( -- assoc1 assoc2 )
177 new-definitions old-definitions
178 [ get first2 assoc-union ] bi@ ;
180 : removed-classes ( -- assoc1 assoc2 )
181 new-definitions old-definitions
184 : forget-removed-definitions ( -- )
185 removed-definitions filter-moved forget-all ;
187 : reset-removed-classes ( -- )
189 filter-moved [ class? ] filter [ forget-class ] each ;
191 : fix-class-words ( -- )
192 #! If a class word had a compound definition which was
193 #! removed, it must go back to being a symbol.
194 new-definitions get first2
195 filter-moved [ [ reset-generic ] [ define-symbol ] bi ] each ;
197 : forget-smudged ( -- )
198 forget-removed-definitions
199 reset-removed-classes
202 : finish-parsing ( lines quot -- )
204 [ record-top-level-form ]
205 [ record-definitions ]
209 : parse-stream ( stream name -- quot )
212 stream-lines dup parse-fresh
213 [ nip ] [ finish-parsing ] 2bi
216 ] with-compilation-unit ;
218 : parse-file-restarts ( file -- restarts )
219 "Load " " again" surround t 2array 1array ;
221 : parse-file ( file -- quot )
223 [ parsing-file ] keep
224 [ utf8 <file-reader> ] keep
227 over parse-file-restarts rethrow-restarts
231 : run-file ( file -- )
232 parse-file call( -- ) ;
234 : ?run-file ( path -- )
235 dup exists? [ run-file ] [ drop ] if ;
237 ERROR: version-control-merge-conflict ;