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 ;
25 : no-word-restarted ( restart-value -- word )
29 [ "Added \"" "\" vocabulary to search path" surround note. ] bi
32 : ignore-forwards ( seq -- seq' )
33 [ forward-reference? not ] filter ;
35 : private? ( word -- ? ) vocabulary>> ".private" tail? ;
37 : ignore-privates ( seq -- seq' )
38 dup [ private? ] all? [ [ private? not ] filter ] unless ;
40 : no-word ( name -- newword )
41 dup words-named ignore-forwards
42 dup ignore-privates dup length 1 = auto-use? get and
43 [ 2nip first no-word-restarted ]
44 [ drop <no-word-error> throw-restarts no-word-restarted ]
47 : parse-word ( string -- word )
48 dup search [ ] [ no-word ] ?if ;
50 : parse-word/number ( string -- word/number )
52 dup string>number [ ] [ no-word ] ?if
55 : (scan-word) ( -- word/number/f )
56 (scan-token) dup [ parse-word/number ] when ;
58 : scan-word ( -- word/number )
59 (scan-word) [ \ word unexpected-eof ] unless* ;
61 : scan-word-name ( -- string )
64 "Word names cannot be numbers" throw
67 : scan-new ( -- word )
68 scan-word-name create-in ;
70 : scan-new-word ( -- word )
71 scan-new dup reset-generic ;
73 ERROR: staging-violation word ;
75 : (execute-parsing) ( accum word -- accum )
77 execute( accum -- accum )
78 pop-parsing-word ; inline
80 : execute-parsing ( accum word -- accum )
81 dup changed-definitions get key? [ staging-violation ] when
84 : scan-object ( -- object )
87 V{ } clone swap execute-parsing first
90 : parse-step ( accum end -- accum ? )
92 { [ 2dup eq? ] [ 2drop f ] }
93 { [ dup not ] [ drop unexpected-eof t ] }
94 { [ dup delimiter? ] [ unexpected t ] }
95 { [ dup parsing-word? ] [ nip execute-parsing t ] }
99 : (parse-until) ( accum end -- accum )
100 [ parse-step ] keep swap [ (parse-until) ] [ drop ] if ;
102 : parse-until ( end -- vec )
103 100 <vector> swap (parse-until) ;
105 SYMBOL: quotation-parser
107 HOOK: parse-quotation quotation-parser ( -- quot )
109 M: f parse-quotation \ ] parse-until >quotation ;
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 suffix! ; inline
120 : parse-definition ( -- quot )
121 \ ; parse-until >quotation ;
125 : scan-base ( base -- n )
126 scan-token swap base> [ bad-number ] unless* ;
128 : parse-base ( parsed base -- parsed )
131 SYMBOL: bootstrap-syntax
133 : with-file-vocabs ( quot -- )
136 bootstrap-syntax get [ use-words ] when*
138 ] with-manifest ; inline
140 SYMBOL: print-use-hook
142 print-use-hook [ [ ] ] initialize
144 : parse-fresh ( lines -- quot )
147 auto-used? [ print-use-hook get call( -- ) ] when
150 : parsing-file ( file -- )
151 parser-quiet? get [ drop ] [ "Loading " write print flush ] if ;
153 : filter-moved ( assoc1 assoc2 -- seq )
154 swap assoc-diff keys [
156 { [ dup where dup [ first ] when file get path>> = not ] [ f ] }
157 { [ dup reader-method? ] [ f ] }
158 { [ dup writer-method? ] [ f ] }
163 : removed-definitions ( -- assoc1 assoc2 )
164 new-definitions old-definitions
165 [ get first2 assoc-union ] bi@ ;
167 : removed-classes ( -- assoc1 assoc2 )
168 new-definitions old-definitions
171 : forget-removed-definitions ( -- )
172 removed-definitions filter-moved forget-all ;
174 : reset-removed-classes ( -- )
176 filter-moved [ class? ] filter [ forget-class ] each ;
178 : fix-class-words ( -- )
179 #! If a class word had a compound definition which was
180 #! removed, it must go back to being a symbol.
181 new-definitions get first2
182 filter-moved [ [ reset-generic ] [ define-symbol ] bi ] each ;
184 : forget-smudged ( -- )
185 forget-removed-definitions
186 reset-removed-classes
189 : finish-parsing ( lines quot -- )
191 [ record-top-level-form ]
192 [ record-definitions ]
196 : parse-stream ( stream name -- quot )
199 stream-lines dup parse-fresh
200 [ nip ] [ finish-parsing ] 2bi
203 ] with-compilation-unit ;
205 : parse-file-restarts ( file -- restarts )
206 "Load " " again" surround t 2array 1array ;
208 : parse-file ( file -- quot )
210 [ parsing-file ] keep
211 [ utf8 <file-reader> ] keep
214 over parse-file-restarts rethrow-restarts
218 : run-file ( file -- )
219 parse-file call( -- ) ;
221 : ?run-file ( path -- )
222 dup exists? [ run-file ] [ drop ] if ;
224 ERROR: version-control-merge-conflict ;