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 ;
11 current-source-file get lexer get line>> 2dup and
12 [ [ path>> ] dip 2array ] [ 2drop f ] if ;
14 : save-location ( definition -- )
15 location remember-definition ;
17 M: parsing-word stack-effect drop ( parsed -- parsed ) ;
19 : create-word-in ( str -- word )
20 current-vocab create-word dup set-last-word dup save-location ;
24 : auto-use ( -- ) auto-use? on ;
26 : no-word-restarted ( restart-value -- word )
30 [ "Added \"" "\" vocabulary to search path" surround note. ] bi
31 ] [ create-word-in ] if ;
33 : ignore-forwards ( seq -- seq' )
34 [ forward-reference? ] reject ;
36 : private? ( word -- ? ) vocabulary>> ".private" tail? ;
38 : use-first-word? ( words -- ? )
39 [ length 1 = ] [ ?first dup [ private? not ] [ ] ?if ] bi and
42 ! True branch is a singleton public word with no name conflicts
43 ! False branch, singleton private words need confirmation regardless
45 : no-word ( name -- newword )
46 dup words-named ignore-forwards
47 dup use-first-word? [ nip first ] [ <no-word-error> throw-restarts ] if
50 : parse-word ( string -- word )
51 dup search [ ] [ no-word ] ?if ;
53 ERROR: number-expected ;
55 : parse-number ( string -- number )
56 string>number [ throw-number-expected ] unless* ;
58 : parse-datum ( string -- word/number )
60 dup string>number [ ] [ no-word ] ?if
63 : ?scan-datum ( -- word/number/f )
64 ?scan-token dup [ parse-datum ] when ;
66 : scan-datum ( -- word/number )
67 ?scan-datum [ \ word throw-unexpected-eof ] unless* ;
69 : scan-word ( -- word )
70 ?scan-token parse-word ;
72 : scan-number ( -- number )
73 ?scan-token parse-number ;
75 ERROR: invalid-word-name string ;
77 : scan-word-name ( -- string )
79 dup "\"" = [ t ] [ dup string>number ] if
80 [ throw-invalid-word-name ] when ;
82 : scan-new ( -- word )
83 scan-word-name create-word-in ;
85 : scan-new-word ( -- word )
86 scan-new dup reset-generic ;
88 ERROR: staging-violation word ;
90 : (execute-parsing) ( accum word -- accum )
92 execute( accum -- accum )
93 pop-parsing-word ; inline
95 : execute-parsing ( accum word -- accum )
96 dup changed-definitions get in? [ throw-staging-violation ] when
99 : scan-object ( -- object )
102 V{ } clone swap execute-parsing first
105 : scan-class ( -- class )
108 : parse-until-step ( accum end -- accum ? )
110 { [ 2dup eq? ] [ 2drop f ] }
111 { [ dup not ] [ drop throw-unexpected-eof t ] }
112 { [ dup delimiter? ] [ unexpected t ] }
113 { [ dup parsing-word? ] [ nip execute-parsing t ] }
117 : (parse-until) ( accum end -- accum )
118 [ parse-until-step ] keep swap [ (parse-until) ] [ drop ] if ;
120 : parse-until ( end -- vec )
121 100 <vector> swap (parse-until) ;
123 SYMBOL: quotation-parser
125 HOOK: parse-quotation quotation-parser ( -- quot )
127 M: f parse-quotation \ ] parse-until >quotation ;
129 : (parse-lines) ( lexer -- quot )
130 [ f parse-until >quotation ] with-lexer ;
132 : parse-lines ( lines -- quot )
133 >array <lexer> (parse-lines) ;
135 : parse-literal ( accum end quot -- accum )
136 [ parse-until ] dip call suffix! ; inline
138 : parse-definition ( -- quot )
139 \ ; parse-until >quotation ;
143 : scan-base ( base -- n )
144 scan-token swap base> [ bad-number ] unless* ;
146 SYMBOL: bootstrap-syntax
148 : with-file-vocabs ( quot -- )
151 bootstrap-syntax get [ use-words ] when*
153 ] with-manifest ; inline
155 SYMBOL: print-use-hook
157 print-use-hook [ [ ] ] initialize
159 : parse-fresh ( lines -- quot )
162 auto-used? [ print-use-hook get call( -- ) ] when
165 : parsing-file ( path -- )
166 parser-quiet? get [ drop ] [ "Loading " write print flush ] if ;
168 : filter-moved ( set1 set2 -- seq )
171 { [ dup where dup [ first ] when current-source-file get path>> = not ] [ f ] }
172 { [ dup reader-method? ] [ f ] }
173 { [ dup writer-method? ] [ f ] }
178 : removed-definitions ( -- set1 set2 )
179 new-definitions old-definitions
180 [ get first2 union ] bi@ ;
182 : removed-classes ( -- set1 set2 )
183 new-definitions old-definitions
186 : forget-removed-definitions ( -- )
187 removed-definitions filter-moved forget-all ;
189 : reset-removed-classes ( -- )
191 filter-moved [ class? ] filter [ forget-class ] each ;
193 : fix-class-words ( -- )
194 #! If a class word had a compound definition which was
195 #! removed, it must go back to being a symbol.
196 new-definitions get first2
197 filter-moved [ [ reset-generic ] [ define-symbol ] bi ] each ;
199 : forget-smudged ( -- )
200 forget-removed-definitions
201 reset-removed-classes
204 : finish-parsing ( lines quot -- )
205 current-source-file get
206 [ record-top-level-form ]
207 [ record-definitions ]
211 : parse-stream ( stream name -- quot )
214 stream-lines dup parse-fresh
215 [ nip ] [ finish-parsing ] 2bi
218 ] with-compilation-unit ;
220 : parse-file-restarts ( path -- restarts )
221 "Load " " again" surround t 2array 1array ;
223 : parse-file ( path -- quot )
225 [ parsing-file ] keep
226 [ utf8 <file-reader> ] keep
229 over parse-file-restarts rethrow-restarts
233 : run-file ( path -- )
234 parse-file call( -- ) ;
236 : ?run-file ( path -- )
237 dup exists? [ run-file ] [ drop ] if ;
239 ERROR: version-control-merge-conflict ;