1 ! Copyright (C) 2005, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays definitions generic assocs kernel math namespaces
4 prettyprint sequences strings vectors words quotations inspector
5 io.styles io combinators sorting splitting math.parser effects
6 continuations debugger io.files io.streams.string vocabs
7 io.encodings.utf8 source-files classes classes.tuple hashtables
8 compiler.errors compiler.units accessors sets ;
11 TUPLE: lexer text line line-text line-length column ;
13 : next-line ( lexer -- )
14 dup [ line>> ] [ text>> ] bi ?nth >>line-text
15 dup line-text>> length >>line-length
20 : new-lexer ( text class -- lexer )
24 dup next-line ; inline
26 : <lexer> ( text -- lexer )
30 file get lexer get lexer-line 2dup and
31 [ >r source-file-path r> 2array ] [ 2drop f ] if ;
33 : save-location ( definition -- )
34 location remember-definition ;
36 : save-class-location ( class -- )
37 location remember-class ;
41 t parser-notes set-global
43 : parser-notes? ( -- ? )
44 parser-notes get "quiet" get not and ;
48 source-file-path <pathname> pprint
57 lexer-line number>string print
61 "Note: " write dup print
64 : skip ( i seq ? -- n )
66 [ swap CHAR: \s eq? xor ] curry find-from drop
67 [ r> drop ] [ r> length ] if* ;
69 : change-lexer-column ( lexer quot -- )
71 [ dup lexer-column swap lexer-line-text rot call ] keep
72 set-lexer-column ; inline
74 GENERIC: skip-blank ( lexer -- )
76 M: lexer skip-blank ( lexer -- )
77 [ t skip ] change-lexer-column ;
79 GENERIC: skip-word ( lexer -- )
81 M: lexer skip-word ( lexer -- )
83 2dup nth CHAR: " eq? [ drop 1+ ] [ f skip ] if
84 ] change-lexer-column ;
86 : still-parsing? ( lexer -- ? )
87 dup lexer-line swap lexer-text length <= ;
89 : still-parsing-line? ( lexer -- ? )
90 dup lexer-column swap lexer-line-length < ;
92 : (parse-token) ( lexer -- str )
96 lexer-line-text subseq ;
98 : parse-token ( lexer -- str/f )
101 dup still-parsing-line?
102 [ (parse-token) ] [ dup next-line parse-token ] if
105 : scan ( -- str/f ) lexer get parse-token ;
109 M: bad-escape summary drop "Bad escape code" ;
111 : escape ( escape -- ch )
119 { CHAR: \s CHAR: \s }
121 { CHAR: \\ CHAR: \\ }
122 { CHAR: \" CHAR: \" }
123 } at [ bad-escape ] unless* ;
125 SYMBOL: name>char-hook
127 name>char-hook global [
128 [ "Unicode support not available" throw ] or
131 : unicode-escape ( str -- ch str' )
133 CHAR: } over index cut-slice
134 >r >string name>char-hook get call r>
137 6 cut-slice >r hex> r>
140 : next-escape ( str -- ch str' )
144 unclip-slice escape swap
147 : (parse-string) ( str -- m )
148 dup [ "\"\\" member? ] find dup [
149 >r cut-slice >r % r> rest-slice r>
153 drop next-escape >r , r> (parse-string)
156 "Unterminated string" throw
159 : parse-string ( -- str )
161 [ swap tail-slice (parse-string) ] "" make swap
162 ] change-lexer-column ;
164 TUPLE: parse-error file line column line-text error ;
166 : <parse-error> ( msg -- error )
169 lexer get line>> >>line
170 lexer get column>> >>column
171 lexer get line-text>> >>line-text
174 : parse-dump ( error -- )
177 [ line>> number>string print ]
178 [ line-text>> dup string? [ print ] [ drop ] if ]
179 [ column>> 0 or CHAR: \s <string> write ]
183 M: parse-error error.
184 [ parse-dump ] [ error>> error. ] bi ;
186 M: parse-error summary
189 M: parse-error compute-restarts
190 error>> compute-restarts ;
192 M: parse-error error-help
198 : word/vocab% ( word -- )
199 "(" % dup word-vocabulary % " " % word-name % ")" % ;
201 : (use+) ( vocab -- )
202 vocab-words use get push ;
207 : add-use ( seq -- ) [ use+ ] each ;
210 [ vocab-words ] V{ } map-as sift use set ;
212 : check-vocab-string ( name -- name )
214 [ "Vocabulary name must be a string" throw ] unless ;
217 check-vocab-string dup in set create-vocab (use+) ;
219 ERROR: unexpected want got ;
221 PREDICATE: unexpected-eof < unexpected
224 M: parsing-word stack-effect drop (( parsed -- parsed )) ;
226 : unexpected-eof ( word -- * ) f unexpected ;
228 : (parse-tokens) ( accum end -- accum )
232 [ pick push (parse-tokens) ] [ unexpected-eof ] if*
235 : parse-tokens ( end -- seq )
236 100 <vector> swap (parse-tokens) >array ;
238 ERROR: no-current-vocab ;
240 M: no-current-vocab summary ( obj -- )
241 drop "Not in a vocabulary; IN: form required" ;
243 : current-vocab ( -- str )
244 in get [ no-current-vocab ] unless* ;
246 : create-in ( str -- word )
247 current-vocab create dup set-word dup save-location ;
249 : CREATE ( -- word ) scan create-in ;
251 : CREATE-GENERIC ( -- word ) CREATE dup reset-word ;
253 : CREATE-WORD ( -- word ) CREATE dup reset-generic ;
255 : create-class-in ( word -- word )
257 dup save-class-location
258 dup predicate-word dup set-word save-location ;
260 : CREATE-CLASS ( -- word )
261 scan create-class-in ;
263 : word-restarts ( possibilities -- restarts )
265 [ "Use the word " swap summary append ] keep
268 TUPLE: no-word-error name ;
270 M: no-word-error summary
271 drop "Word not found in current vocabulary search path" ;
273 : no-word ( name -- newword )
274 dup no-word-error boa
275 swap words-named [ forward-reference? not ] filter
276 word-restarts throw-restarts
277 dup word-vocabulary (use+) ;
279 : check-forward ( str word -- word/f )
280 dup forward-reference? [
284 [ forward-reference? not ] find nip
289 : search ( str -- word/f )
290 dup use get assoc-stack check-forward ;
292 : scan-word ( -- word/number/f )
295 dup string>number [ ] [ no-word ] ?if
299 : create-method-in ( class generic -- method )
300 create-method f set-word dup save-location ;
302 : CREATE-METHOD ( -- method )
303 scan-word bootstrap-word scan-word create-method-in ;
305 : shadowed-slots ( superclass slots -- shadowed )
306 >r all-slot-names r> intersect ;
308 : check-slot-shadowing ( class superclass slots -- )
311 "Definition of slot ``" %
315 "'' shadows a superclass slot" %
319 ERROR: invalid-slot-name name ;
321 M: invalid-slot-name summary
323 "Invalid slot name" ;
325 : (parse-tuple-slots) ( -- )
326 #! This isn't meant to enforce any kind of policy, just
327 #! to check for mistakes of this form:
329 #! TUPLE: blahblah foo bing
333 { [ dup not ] [ unexpected-eof ] }
334 { [ dup { ":" "(" "<" } member? ] [ invalid-slot-name ] }
335 { [ dup ";" = ] [ drop ] }
336 [ , (parse-tuple-slots) ]
339 : parse-tuple-slots ( -- seq )
340 [ (parse-tuple-slots) ] { } make ;
342 : parse-tuple-definition ( -- class superclass slots )
346 { "<" [ scan-word parse-tuple-slots ] }
347 [ >r tuple parse-tuple-slots r> prefix ]
348 } case 3dup check-slot-shadowing ;
350 ERROR: not-in-a-method-error ;
352 M: not-in-a-method-error summary
353 drop "call-next-method can only be called in a method definition" ;
355 ERROR: staging-violation word ;
357 M: staging-violation summary
359 "A parsing word cannot be used in the same file it is defined in." ;
361 : execute-parsing ( word -- )
362 dup changed-definitions get key? [ staging-violation ] when
365 : parse-step ( accum end -- accum ? )
367 { [ 2dup eq? ] [ 2drop f ] }
368 { [ dup not ] [ drop unexpected-eof t ] }
369 { [ dup delimiter? ] [ unexpected t ] }
370 { [ dup parsing-word? ] [ nip execute-parsing t ] }
374 : (parse-until) ( accum end -- accum )
375 dup >r parse-step [ r> (parse-until) ] [ r> drop ] if ;
377 : parse-until ( end -- vec )
378 100 <vector> swap (parse-until) ;
380 : parsed ( accum obj -- accum ) over push ;
382 : with-parser ( lexer quot -- newquot )
384 [ call >quotation ] [ <parse-error> rethrow ] recover ;
386 : (parse-lines) ( lexer -- quot )
387 [ f parse-until ] with-parser ;
389 SYMBOL: lexer-factory
391 [ <lexer> ] lexer-factory set-global
393 : parse-lines ( lines -- quot )
394 lexer-factory get call (parse-lines) ;
396 ! Parsing word utilities
397 : parse-effect ( end -- effect )
398 parse-tokens dup { "(" "((" } intersect empty? [
399 { "--" } split1 dup [
402 "Stack effect declaration must contain --" throw
405 "Stack effect declaration must not contain ( or ((" throw
410 : parse-base ( parsed base -- parsed )
411 scan swap base> [ bad-number ] unless* parsed ;
413 : parse-literal ( accum end quot -- accum )
414 >r parse-until r> call parsed ; inline
416 : parse-definition ( -- quot )
417 \ ; parse-until >quotation ;
419 : (:) ( -- word def ) CREATE-WORD parse-definition ;
421 SYMBOL: current-class
422 SYMBOL: current-generic
424 : with-method-definition ( quot -- parsed )
427 [ "method-class" word-prop current-class set ]
428 [ "method-generic" word-prop current-generic set ]
431 ] with-scope ; inline
433 : (M:) ( method def -- )
434 CREATE-METHOD [ parse-definition ] with-method-definition ;
436 : scan-object ( -- object )
437 scan-word dup parsing-word?
438 [ V{ } clone swap execute first ] when ;
440 GENERIC: expected>string ( obj -- str )
442 M: f expected>string drop "end of input" ;
443 M: word expected>string word-name ;
444 M: string expected>string ;
448 dup unexpected-want expected>string write
450 unexpected-got expected>string print ;
452 M: bad-number summary
453 drop "Bad number literal" ;
455 SYMBOL: bootstrap-syntax
457 : with-file-vocabs ( quot -- )
459 f in set { "syntax" } set-use
460 bootstrap-syntax get [ use get push ] when*
462 ] with-scope ; inline
464 SYMBOL: interactive-vocabs
504 } interactive-vocabs set-global
506 : with-interactive-vocabs ( quot -- )
509 interactive-vocabs get set-use
511 ] with-scope ; inline
513 : parse-fresh ( lines -- quot )
514 [ parse-lines ] with-file-vocabs ;
516 : parsing-file ( file -- )
520 "Loading " write <pathname> . flush
523 : filter-moved ( assoc1 assoc2 -- seq )
525 drop where dup [ first ] when
526 file get source-file-path =
527 ] assoc-filter keys ;
529 : removed-definitions ( -- assoc1 assoc2 )
530 new-definitions old-definitions
531 [ get first2 assoc-union ] bi@ ;
533 : removed-classes ( -- assoc1 assoc2 )
534 new-definitions old-definitions
537 : forget-removed-definitions ( -- )
538 removed-definitions filter-moved forget-all ;
540 : reset-removed-classes ( -- )
542 filter-moved [ class? ] filter [ reset-class ] each ;
544 : fix-class-words ( -- )
545 #! If a class word had a compound definition which was
546 #! removed, it must go back to being a symbol.
547 new-definitions get first2
548 filter-moved [ [ reset-generic ] [ define-symbol ] bi ] each ;
550 : forget-smudged ( -- )
551 forget-removed-definitions
552 reset-removed-classes
555 : finish-parsing ( lines quot -- )
558 [ record-definitions ]
562 : parse-stream ( stream name -- quot )
565 lines dup parse-fresh
569 ] with-compilation-unit ;
571 : parse-file-restarts ( file -- restarts )
572 "Load " swap " again" 3append t 2array 1array ;
574 : parse-file ( file -- quot )
577 [ parsing-file ] keep
578 [ utf8 <file-reader> ] keep
580 ] with-compiler-errors
582 over parse-file-restarts rethrow-restarts
586 : run-file ( file -- )
587 [ dup parse-file call ] assert-depth drop ;
589 : ?run-file ( path -- )
590 dup exists? [ run-file ] [ drop ] if ;
592 : bootstrap-file ( path -- )
593 [ parse-file % ] [ run-file ] if-bootstrapping ;
596 [ string-lines parse-fresh ] with-compilation-unit call ;
598 : eval>string ( str -- output )
601 [ [ eval ] keep ] try drop
602 ] with-string-writer ;