1 ! Copyright (C) 2005, 2010 Slava Pestov.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays classes combinators compiler.units
4 continuations definitions effects io io.encodings.utf8 io.files
5 kernel lexer math.parser namespaces parser.notes quotations
6 sequences sets slots source-files vectors vocabs vocabs.parser
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 : no-word-restarted ( restart-value -- word )
28 [ "Added \"" "\" vocabulary to search path" surround note. ] bi
29 ] [ create-word-in ] if ;
31 : ignore-forwards ( seq -- seq' )
32 [ forward-reference? ] reject ;
34 : private? ( word -- ? ) vocabulary>> ".private" tail? ;
36 : use-first-word? ( words -- ? )
37 [ length 1 = ] [ ?first dup [ private? not ] [ ] ?if ] bi and
40 ! True branch is a singleton public word with no name conflicts
41 ! False branch, singleton private words need confirmation regardless
43 : no-word ( name -- newword )
44 dup words-named ignore-forwards
45 dup use-first-word? [ nip first ] [ <no-word-error> throw-restarts ] if
48 : parse-word ( string -- word )
49 dup search [ ] [ no-word ] ?if ;
51 ERROR: number-expected ;
53 : parse-number ( string -- number )
54 string>number [ number-expected ] unless* ;
56 : parse-datum ( string -- word/number )
58 dup string>number [ ] [ no-word ] ?if
61 : ?scan-datum ( -- word/number/f )
62 ?scan-token dup [ parse-datum ] when ;
64 : scan-datum ( -- word/number )
65 ?scan-datum [ \ word throw-unexpected-eof ] unless* ;
67 : scan-word ( -- word )
68 ?scan-token parse-word ;
70 : scan-number ( -- number )
71 ?scan-token parse-number ;
73 ERROR: invalid-word-name string ;
75 : check-word-name ( string -- string )
76 dup "\"" = [ t ] [ dup string>number ] if
77 [ invalid-word-name ] when ;
79 : scan-word-name ( -- string )
80 scan-token check-word-name ;
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? [ staging-violation ] when
99 : ?execute-parsing ( word/number -- seq )
101 [ V{ } clone swap execute-parsing ] [ 1array ] if ;
103 : scan-object ( -- object )
106 V{ } clone swap execute-parsing first
109 ERROR: classoid-expected object ;
111 : scan-class ( -- class )
113 dup classoid? [ classoid-expected ] unless ;
115 : parse-until-step ( accum end -- accum ? )
117 { [ 2dup eq? ] [ 2drop f ] }
118 { [ dup not ] [ drop throw-unexpected-eof t ] }
119 { [ dup delimiter? ] [ unexpected t ] }
120 { [ dup parsing-word? ] [ nip execute-parsing t ] }
124 : (parse-until) ( accum end -- accum )
125 [ parse-until-step ] keep swap [ (parse-until) ] [ drop ] if ;
127 : parse-until ( end -- vec )
128 100 <vector> swap (parse-until) ;
130 SYMBOL: quotation-parser
132 HOOK: parse-quotation quotation-parser ( -- quot )
134 M: f parse-quotation \ ] parse-until >quotation ;
136 : (parse-lines) ( lexer -- quot )
137 [ f parse-until >quotation ] with-lexer ;
139 : parse-lines ( lines -- quot )
140 >array <lexer> (parse-lines) ;
142 : parse-literal ( accum end quot -- accum )
143 [ parse-until ] dip call suffix! ; inline
145 : parse-definition ( -- quot )
146 \ ; parse-until >quotation ;
148 : parse-array-def ( -- array )
149 \ ; parse-until >array ;
153 : scan-base ( base -- n )
154 scan-token swap base> [ bad-number ] unless* ;
156 SYMBOL: bootstrap-syntax
158 : with-file-vocabs ( quot -- )
161 bootstrap-syntax get [ use-words ] when*
163 ] with-manifest ; inline
165 : parse-fresh ( lines -- quot )
168 auto-used? [ print-use-hook get call( -- ) ] when
171 : parsing-file ( path -- )
172 parser-quiet? get [ drop ] [ "Loading " write print flush ] if ;
174 : filter-moved ( set1 set2 -- seq )
177 { [ dup where ?first current-source-file get path>> = not ] [ f ] }
178 { [ dup reader-method? ] [ f ] }
179 { [ dup writer-method? ] [ f ] }
184 : removed-definitions ( -- set1 set2 )
185 new-definitions old-definitions
186 [ get first2 union ] bi@ ;
188 : removed-classes ( -- set1 set2 )
189 new-definitions old-definitions
192 : forget-removed-definitions ( -- )
193 removed-definitions filter-moved forget-all ;
195 : reset-removed-classes ( -- )
197 filter-moved [ class? ] filter [ forget-class ] each ;
199 : fix-class-words ( -- )
200 ! If a class word had a compound definition which was
201 ! removed, it must go back to being a symbol.
202 new-definitions get first2
203 filter-moved [ [ reset-generic ] [ define-symbol ] bi ] each ;
205 : forget-smudged ( -- )
206 forget-removed-definitions
207 reset-removed-classes
210 : finish-parsing ( lines quot -- )
211 current-source-file get
212 [ record-top-level-form ]
213 [ record-definitions ]
217 : parse-stream ( stream name -- quot )
220 stream-lines dup parse-fresh
221 [ nip ] [ finish-parsing ] 2bi
224 ] with-compilation-unit ;
226 : parse-file-restarts ( path -- restarts )
227 "Load " " again" surround t 2array 1array ;
229 : parse-file ( path -- quot )
231 [ parsing-file ] keep
232 [ utf8 <file-reader> ] keep
235 over parse-file-restarts rethrow-restarts
239 : run-file ( path -- )
240 parse-file call( -- ) ;
242 : ?run-file ( path -- )
243 dup file-exists? [ run-file ] [ drop ] if ;
245 ERROR: version-control-merge-conflict ;