factor/core/parser/parser.factor

238 lines
6.0 KiB
Factor
Raw Normal View History

! Copyright (C) 2005, 2010 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
2013-03-05 13:34:47 -05:00
USING: accessors arrays assocs classes combinators
compiler.units continuations definitions effects io
io.encodings.utf8 io.files kernel lexer math.parser namespaces
2013-03-10 19:12:40 -04:00
parser.notes quotations sequences sets slots source-files
vectors vocabs vocabs.parser words words.symbol ;
2013-03-10 21:04:37 -04:00
FROM: sets => members ;
2007-09-20 18:09:08 -04:00
IN: parser
: location ( -- loc )
2008-06-25 04:25:08 -04:00
file get lexer get line>> 2dup and
[ [ path>> ] dip 2array ] [ 2drop f ] if ;
2007-09-20 18:09:08 -04:00
: save-location ( definition -- )
2007-12-24 17:18:26 -05:00
location remember-definition ;
M: parsing-word stack-effect drop ( parsed -- parsed ) ;
2008-06-08 16:32:55 -04:00
: create-in ( str -- word )
current-vocab create dup set-last-word dup save-location ;
2008-03-16 03:43:00 -04:00
2008-11-20 21:35:01 -05:00
SYMBOL: auto-use?
2008-11-19 17:27:16 -05:00
: auto-use ( -- ) auto-use? on ;
: no-word-restarted ( restart-value -- word )
2008-11-20 21:35:01 -05:00
dup word? [
dup vocabulary>>
2009-05-16 05:26:45 -04:00
[ auto-use-vocab ]
[ "Added \"" "\" vocabulary to search path" surround note. ] bi
2008-11-20 21:35:01 -05:00
] [ create-in ] if ;
: ignore-forwards ( seq -- seq' )
[ forward-reference? not ] filter ;
: private? ( word -- ? ) vocabulary>> ".private" tail? ;
: ignore-privates ( seq -- seq' )
dup [ private? ] all? [ [ private? not ] filter ] unless ;
2007-09-20 18:09:08 -04:00
: no-word ( name -- newword )
dup words-named ignore-forwards
dup ignore-privates dup length 1 = auto-use? get and
[ 2nip first no-word-restarted ]
[ drop <no-word-error> throw-restarts no-word-restarted ]
2008-11-19 17:27:16 -05:00
if ;
2007-09-20 18:09:08 -04:00
: parse-word ( string -- word )
dup search [ ] [ no-word ] ?if ;
ERROR: number-expected ;
: parse-number ( string -- number )
string>number [ number-expected ] unless* ;
: parse-datum ( string -- word/number )
2009-10-16 13:26:31 -04:00
dup search [ ] [
dup string>number [ ] [ no-word ] ?if
] ?if ;
: (scan-datum) ( -- word/number/f )
(scan-token) dup [ parse-datum ] when ;
: scan-datum ( -- word/number )
(scan-datum) [ \ word unexpected-eof ] unless* ;
: scan-word ( -- word )
(scan-token) parse-word ;
: scan-number ( -- number )
(scan-token) parse-number ;
: scan-word-name ( -- string )
scan-token
dup string>number [
"Word names cannot be numbers" throw
] when ;
: scan-new ( -- word )
scan-word-name create-in ;
: scan-new-word ( -- word )
scan-new dup reset-generic ;
2007-09-20 18:09:08 -04:00
2008-03-20 16:00:49 -04:00
ERROR: staging-violation word ;
: (execute-parsing) ( accum word -- accum )
dup push-parsing-word
execute( accum -- accum )
pop-parsing-word ; inline
2009-03-16 21:11:36 -04:00
: execute-parsing ( accum word -- accum )
2013-03-10 19:12:40 -04:00
dup changed-definitions get in? [ staging-violation ] when
(execute-parsing) ;
2008-06-25 04:25:08 -04:00
: scan-object ( -- object )
scan-datum
dup parsing-word? [
V{ } clone swap execute-parsing first
] when ;
2008-06-25 04:25:08 -04:00
: scan-class ( -- class )
scan-object \ f or ;
: parse-until-step ( accum end -- accum ? )
(scan-datum) {
2007-09-20 18:09:08 -04:00
{ [ 2dup eq? ] [ 2drop f ] }
{ [ dup not ] [ drop unexpected-eof t ] }
{ [ dup delimiter? ] [ unexpected t ] }
2008-06-08 16:32:55 -04:00
{ [ dup parsing-word? ] [ nip execute-parsing t ] }
2008-04-11 13:53:22 -04:00
[ pick push drop t ]
2007-09-20 18:09:08 -04:00
} cond ;
: (parse-until) ( accum end -- accum )
[ parse-until-step ] keep swap [ (parse-until) ] [ drop ] if ;
2007-09-20 18:09:08 -04:00
: parse-until ( end -- vec )
100 <vector> swap (parse-until) ;
SYMBOL: quotation-parser
HOOK: parse-quotation quotation-parser ( -- quot )
M: f parse-quotation \ ] parse-until >quotation ;
2007-09-20 18:09:08 -04:00
: (parse-lines) ( lexer -- quot )
[ f parse-until >quotation ] with-lexer ;
2007-09-20 18:09:08 -04:00
: parse-lines ( lines -- quot )
2012-07-27 22:06:22 -04:00
>array <lexer> (parse-lines) ;
2007-09-20 18:09:08 -04:00
: parse-literal ( accum end quot -- accum )
2009-10-28 14:38:27 -04:00
[ parse-until ] dip call suffix! ; inline
2007-09-20 18:09:08 -04:00
: parse-definition ( -- quot )
\ ; parse-until >quotation ;
2008-06-25 04:25:08 -04:00
ERROR: bad-number ;
2007-09-20 18:09:08 -04:00
: scan-base ( base -- n )
scan-token swap base> [ bad-number ] unless* ;
2007-09-20 18:09:08 -04:00
SYMBOL: bootstrap-syntax
: with-file-vocabs ( quot -- )
[
"syntax" use-vocab
bootstrap-syntax get [ use-words ] when*
call
] with-manifest ; inline
SYMBOL: print-use-hook
print-use-hook [ [ ] ] initialize
2009-01-27 05:11:51 -05:00
2007-09-20 18:09:08 -04:00
: parse-fresh ( lines -- quot )
[
parse-lines
2009-05-16 05:26:45 -04:00
auto-used? [ print-use-hook get call( -- ) ] when
] with-file-vocabs ;
2007-09-20 18:09:08 -04:00
: parsing-file ( file -- )
parser-quiet? get [ drop ] [ "Loading " write print flush ] if ;
2007-09-20 18:09:08 -04:00
2013-03-10 21:04:37 -04:00
: filter-moved ( set1 set2 -- seq )
swap diff members [
{
{ [ dup where dup [ first ] when file get path>> = not ] [ f ] }
{ [ dup reader-method? ] [ f ] }
{ [ dup writer-method? ] [ f ] }
[ t ]
} cond nip
] filter ;
2008-04-03 05:58:37 -04:00
2013-03-10 21:04:37 -04:00
: removed-definitions ( -- set1 set2 )
2008-04-03 05:58:37 -04:00
new-definitions old-definitions
2013-03-10 21:04:37 -04:00
[ get first2 union ] bi@ ;
2007-09-20 18:09:08 -04:00
2013-03-10 21:04:37 -04:00
: removed-classes ( -- set1 set2 )
2007-12-30 17:14:15 -05:00
new-definitions old-definitions
2008-04-03 05:58:37 -04:00
[ get second ] bi@ ;
2007-12-24 17:18:26 -05:00
2008-04-03 22:43:41 -04:00
: forget-removed-definitions ( -- )
removed-definitions filter-moved forget-all ;
: reset-removed-classes ( -- )
removed-classes
2008-06-11 18:40:33 -04:00
filter-moved [ class? ] filter [ forget-class ] each ;
2007-09-20 18:09:08 -04:00
2008-02-24 01:26:54 -05:00
: fix-class-words ( -- )
#! If a class word had a compound definition which was
#! removed, it must go back to being a symbol.
2008-04-03 01:21:53 -04:00
new-definitions get first2
2008-04-03 22:43:41 -04:00
filter-moved [ [ reset-generic ] [ define-symbol ] bi ] each ;
2008-02-24 01:26:54 -05:00
2007-09-20 18:09:08 -04:00
: forget-smudged ( -- )
2008-04-03 22:43:41 -04:00
forget-removed-definitions
reset-removed-classes
2008-02-24 01:26:54 -05:00
fix-class-words ;
2007-09-20 18:09:08 -04:00
2008-01-12 17:25:29 -05:00
: finish-parsing ( lines quot -- )
file get
[ record-top-level-form ]
2008-04-03 01:21:53 -04:00
[ record-definitions ]
[ record-checksum ]
tri ;
2007-09-20 18:09:08 -04:00
: parse-stream ( stream name -- quot )
[
[
stream-lines dup parse-fresh
2009-01-23 19:20:47 -05:00
[ nip ] [ finish-parsing ] 2bi
forget-smudged
] with-source-file
] with-compilation-unit ;
2007-09-20 18:09:08 -04:00
: parse-file-restarts ( file -- restarts )
2008-12-06 19:42:41 -05:00
"Load " " again" surround t 2array 1array ;
2007-09-20 18:09:08 -04:00
: parse-file ( file -- quot )
[
[ parsing-file ] keep
[ utf8 <file-reader> ] keep
parse-stream
2007-09-20 18:09:08 -04:00
] [
over parse-file-restarts rethrow-restarts
drop parse-file
] recover ;
: run-file ( file -- )
2009-03-16 21:11:36 -04:00
parse-file call( -- ) ;
2007-09-20 18:09:08 -04:00
: ?run-file ( path -- )
dup exists? [ run-file ] [ drop ] if ;
ERROR: version-control-merge-conflict ;