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-old ] 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-old ;
51 ERROR: number-expected ;
53 : parse-number ( string -- number )
54 string>number [ number-expected ] unless* ;
56 : parse-datum ( string -- word/number )
58 [ [ string>number ] [ no-word ] ?unless ] ?unless ;
60 : ?scan-datum ( -- word/number/f )
61 ?scan-token [ parse-datum ] ?transmute ;
63 : scan-datum ( -- word/number )
64 ?scan-datum [ \ word throw-unexpected-eof ] unless* ;
66 : scan-word ( -- word )
67 ?scan-token parse-word ;
69 : scan-number ( -- number )
70 ?scan-token parse-number ;
72 ERROR: invalid-word-name string ;
74 : check-word-name ( string -- string )
75 dup "\"" = [ t ] [ dup string>number ] if
76 [ invalid-word-name ] when ;
78 : scan-word-name ( -- string )
79 scan-token check-word-name ;
81 : scan-new ( -- word )
82 scan-word-name create-word-in ;
84 : scan-new-word ( -- word )
85 scan-new dup reset-generic ;
87 ERROR: staging-violation word ;
89 : (execute-parsing) ( accum word -- accum )
91 execute( accum -- accum )
92 pop-parsing-word ; inline
94 : execute-parsing ( accum word -- accum )
95 dup changed-definitions get in? [ staging-violation ] when
98 : ?execute-parsing ( word/number -- seq )
100 [ V{ } clone swap execute-parsing ] [ 1array ] if ;
102 : scan-object ( -- object )
105 V{ } clone swap execute-parsing first
108 ERROR: classoid-expected object ;
110 : scan-class ( -- class )
112 dup classoid? [ classoid-expected ] unless ;
114 : parse-until-step ( accum end -- accum ? )
116 { [ 2dup eq? ] [ 2drop f ] }
117 { [ dup not ] [ drop throw-unexpected-eof t ] }
118 { [ dup delimiter? ] [ unexpected t ] }
119 { [ dup parsing-word? ] [ nip execute-parsing t ] }
123 : (parse-until) ( accum end -- accum )
124 [ parse-until-step ] keep swap [ (parse-until) ] [ drop ] if ;
126 : parse-until ( end -- vec )
127 100 <vector> swap (parse-until) ;
129 SYMBOL: quotation-parser
131 HOOK: parse-quotation quotation-parser ( -- quot )
133 M: f parse-quotation \ ] parse-until >quotation ;
135 : (parse-lines) ( lexer -- quot )
136 [ f parse-until >quotation ] with-lexer ;
138 : parse-lines ( lines -- quot )
139 >array <lexer> (parse-lines) ;
141 : parse-literal ( accum end quot -- accum )
142 [ parse-until ] dip call suffix! ; inline
144 : parse-definition ( -- quot )
145 \ ; parse-until >quotation ;
147 : parse-array-def ( -- array )
148 \ ; parse-until >array ;
152 : scan-base ( base -- n )
153 scan-token swap base> [ bad-number ] unless* ;
155 SYMBOL: bootstrap-syntax
157 : with-file-vocabs ( quot -- )
160 bootstrap-syntax get [ use-words ] when*
162 ] with-manifest ; inline
164 : parse-fresh ( lines -- quot )
167 auto-used? [ print-use-hook get call( -- ) ] when
170 : parsing-file ( path -- )
171 parser-quiet? get [ drop ] [ "Loading " write print flush ] if ;
173 : filter-moved ( set1 set2 -- seq )
176 { [ dup where ?first current-source-file get path>> = not ] [ f ] }
177 { [ dup reader-method? ] [ f ] }
178 { [ dup writer-method? ] [ f ] }
183 : removed-definitions ( -- set1 set2 )
184 new-definitions old-definitions
185 [ get first2 union ] bi@ ;
187 : removed-classes ( -- set1 set2 )
188 new-definitions old-definitions
191 : forget-removed-definitions ( -- )
192 removed-definitions filter-moved forget-all ;
194 : reset-removed-classes ( -- )
196 filter-moved [ class? ] filter [ forget-class ] each ;
198 : fix-class-words ( -- )
199 ! If a class word had a compound definition which was
200 ! removed, it must go back to being a symbol.
201 new-definitions get first2
202 filter-moved [ [ reset-generic ] [ define-symbol ] bi ] each ;
204 : forget-smudged ( -- )
205 forget-removed-definitions
206 reset-removed-classes
209 : finish-parsing ( lines quot -- )
210 current-source-file get
211 [ record-top-level-form ]
212 [ record-definitions ]
216 : parse-stream ( stream name -- quot )
219 stream-lines dup parse-fresh
220 [ nip ] [ finish-parsing ] 2bi
223 ] with-compilation-unit ;
225 : parse-file-restarts ( path -- restarts )
226 "Load " " again" surround t 2array 1array ;
228 : parse-file ( path -- quot )
230 [ parsing-file ] keep
231 [ utf8 <file-reader> ] keep
234 over parse-file-restarts rethrow-restarts
238 : run-file ( path -- )
239 parse-file call( -- ) ;
241 : ?run-file ( path -- )
242 dup file-exists? [ run-file ] [ drop ] if ;
244 ERROR: version-control-merge-conflict ;