1 ! Copyright (C) 2005, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs classes combinators
4 compiler.units continuations definitions effects io
5 io.encodings.utf8 io.files kernel lexer math.parser namespaces
6 parser.notes quotations sequences sets slots source-files
7 vectors vocabs vocabs.parser words words.symbol ;
8 FROM: sets => members ;
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-last-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 ! True branch is a singleton public word with no name conflicts
40 ! False branch, singleton private words need confirmation regardless
42 : no-word ( name -- newword )
43 dup words-named ignore-forwards
45 [ [ f ] [ first private? not ] if-empty ] bi and
47 [ nip first no-word-restarted ]
48 [ <no-word-error> throw-restarts no-word-restarted ]
51 : parse-word ( string -- word )
52 dup search [ ] [ no-word ] ?if ;
54 ERROR: number-expected ;
56 : parse-number ( string -- number )
57 string>number [ number-expected ] unless* ;
59 : parse-datum ( string -- word/number )
61 dup string>number [ ] [ no-word ] ?if
64 : ?scan-datum ( -- word/number/f )
65 ?scan-token dup [ parse-datum ] when ;
67 : scan-datum ( -- word/number )
68 ?scan-datum [ \ word throw-unexpected-eof ] unless* ;
70 : scan-word ( -- word )
71 ?scan-token parse-word ;
73 : scan-number ( -- number )
74 ?scan-token parse-number ;
76 ERROR: invalid-word-name string ;
78 : scan-word-name ( -- string )
80 dup "\"" = [ t ] [ dup string>number ] if
81 [ invalid-word-name ] when ;
83 : scan-new ( -- word )
84 scan-word-name create-in ;
86 : scan-new-word ( -- word )
87 scan-new dup reset-generic ;
89 ERROR: staging-violation word ;
91 : (execute-parsing) ( accum word -- accum )
93 execute( accum -- accum )
94 pop-parsing-word ; inline
96 : execute-parsing ( accum word -- accum )
97 dup changed-definitions get in? [ staging-violation ] when
100 : scan-object ( -- object )
103 V{ } clone swap execute-parsing first
106 : scan-class ( -- class )
109 : parse-until-step ( accum end -- accum ? )
111 { [ 2dup eq? ] [ 2drop f ] }
112 { [ dup not ] [ drop throw-unexpected-eof t ] }
113 { [ dup delimiter? ] [ unexpected t ] }
114 { [ dup parsing-word? ] [ nip execute-parsing t ] }
118 : (parse-until) ( accum end -- accum )
119 [ parse-until-step ] keep swap [ (parse-until) ] [ drop ] if ;
121 : parse-until ( end -- vec )
122 100 <vector> swap (parse-until) ;
124 SYMBOL: quotation-parser
126 HOOK: parse-quotation quotation-parser ( -- quot )
128 M: f parse-quotation \ ] parse-until >quotation ;
130 : (parse-lines) ( lexer -- quot )
131 [ f parse-until >quotation ] with-lexer ;
133 : parse-lines ( lines -- quot )
134 >array <lexer> (parse-lines) ;
136 : parse-literal ( accum end quot -- accum )
137 [ parse-until ] dip call suffix! ; inline
139 : parse-definition ( -- quot )
140 \ ; parse-until >quotation ;
144 : scan-base ( base -- n )
145 scan-token swap base> [ bad-number ] unless* ;
147 SYMBOL: bootstrap-syntax
149 : with-file-vocabs ( quot -- )
152 bootstrap-syntax get [ use-words ] when*
154 ] with-manifest ; inline
156 SYMBOL: print-use-hook
158 print-use-hook [ [ ] ] initialize
160 : parse-fresh ( lines -- quot )
163 auto-used? [ print-use-hook get call( -- ) ] when
166 : parsing-file ( file -- )
167 parser-quiet? get [ drop ] [ "Loading " write print flush ] if ;
169 : filter-moved ( set1 set2 -- seq )
172 { [ dup where dup [ first ] when file get path>> = not ] [ f ] }
173 { [ dup reader-method? ] [ f ] }
174 { [ dup writer-method? ] [ f ] }
179 : removed-definitions ( -- set1 set2 )
180 new-definitions old-definitions
181 [ get first2 union ] bi@ ;
183 : removed-classes ( -- set1 set2 )
184 new-definitions old-definitions
187 : forget-removed-definitions ( -- )
188 removed-definitions filter-moved forget-all ;
190 : reset-removed-classes ( -- )
192 filter-moved [ class? ] filter [ forget-class ] each ;
194 : fix-class-words ( -- )
195 #! If a class word had a compound definition which was
196 #! removed, it must go back to being a symbol.
197 new-definitions get first2
198 filter-moved [ [ reset-generic ] [ define-symbol ] bi ] each ;
200 : forget-smudged ( -- )
201 forget-removed-definitions
202 reset-removed-classes
205 : finish-parsing ( lines quot -- )
207 [ record-top-level-form ]
208 [ record-definitions ]
212 : parse-stream ( stream name -- quot )
215 stream-lines dup parse-fresh
216 [ nip ] [ finish-parsing ] 2bi
219 ] with-compilation-unit ;
221 : parse-file-restarts ( file -- restarts )
222 "Load " " again" surround t 2array 1array ;
224 : parse-file ( file -- quot )
226 [ parsing-file ] keep
227 [ utf8 <file-reader> ] keep
230 over parse-file-restarts rethrow-restarts
234 : run-file ( file -- )
235 parse-file call( -- ) ;
237 : ?run-file ( path -- )
238 dup exists? [ run-file ] [ drop ] if ;
240 ERROR: version-control-merge-conflict ;