factor/library/syntax/parse-stream.factor

99 lines
2.5 KiB
Factor
Raw Normal View History

2006-08-02 15:17:13 -04:00
! Copyright (C) 2004, 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
2005-08-19 21:46:12 -04:00
IN: parser
2006-09-06 18:06:11 -04:00
USING: arrays errors generic hashtables io kernel math
namespaces sequences words ;
2005-08-19 21:46:12 -04:00
2006-09-29 23:03:27 -04:00
SYMBOL: source-files
TUPLE: source-file path modified definitions ;
: source-file-modified* ( source-file -- n )
source-file-path ?resource-path
file-modified [ 0 ] unless* ;
: record-modified ( file -- )
dup source-file-modified* swap set-source-file-modified ;
: reset-modified ( -- )
source-files get hash-values [ record-modified ] each ;
C: source-file ( path -- source-file )
[ set-source-file-path ] keep
V{ } clone over set-source-file-definitions
dup record-modified ;
: source-modified? ( file -- ? )
source-files get hash [
dup source-file-modified swap source-file-modified*
[ < ] [ drop f ] if*
] [
t
] if* ;
2005-08-19 21:46:12 -04:00
: file-vocabs ( -- )
"scratchpad" set-in { "syntax" "scratchpad" } set-use ;
2005-08-19 21:46:12 -04:00
: with-parser ( quot -- )
2006-09-23 15:54:37 -04:00
[
[
dup [ parse-error? ] is? [ <parse-error> ] unless
rethrow
] recover
] with-scope ;
2006-01-03 17:43:29 -05:00
2005-09-14 00:37:50 -04:00
: parse-lines ( lines -- quot )
2005-08-19 21:46:12 -04:00
[
dup length f [ 1+ line-number set (parse) ] 2reduce
2006-05-15 01:01:47 -04:00
>quotation
2005-08-19 21:46:12 -04:00
] with-parser ;
2006-08-16 21:55:53 -04:00
: parse ( str -- quot ) <string-reader> lines parse-lines ;
2006-01-03 17:43:29 -05:00
2006-08-16 21:55:53 -04:00
: eval ( str -- ) parse call ;
2006-01-03 17:43:29 -05:00
SYMBOL: parse-hook
: do-parse-hook ( -- ) parse-hook get call ;
2005-09-14 00:37:50 -04:00
: parse-stream ( stream name -- quot )
[
file set file-vocabs
lines parse-lines
do-parse-hook
] with-scope ;
2005-08-19 21:46:12 -04:00
2006-09-29 23:03:27 -04:00
: parsing-file ( file -- )
2006-10-06 20:27:40 -04:00
"Loading " write write-pathname terpri flush ;
2006-09-29 23:03:27 -04:00
: record-file ( file -- )
[ <source-file> ] keep source-files get set-hash ;
2005-12-11 14:27:36 -05:00
2006-09-06 18:06:11 -04:00
: parse-file-restarts ( file -- restarts )
"Load " swap " again" append3 t 2array 1array ;
2006-09-29 23:03:27 -04:00
: parse-file ( file -- quot )
[
dup parsing-file dup record-file
[ ?resource-path <file-reader> ] keep parse-stream
] [
2006-10-19 14:12:47 -04:00
over parse-file-restarts condition drop parse-file
2006-09-29 23:03:27 -04:00
] recover ;
2005-08-19 21:46:12 -04:00
2006-01-03 17:43:29 -05:00
: run-file ( file -- ) parse-file call ;
2005-08-19 21:46:12 -04:00
2006-10-14 00:27:43 -04:00
: no-parse-hook ( quot -- )
[ parse-hook off call ] with-scope ; inline
2006-10-13 22:49:14 -04:00
: run-files ( seq -- )
[
bootstrapping? get
[ parse-file % ] [ run-file ] ? each
] no-parse-hook ;
2006-08-25 00:02:30 -04:00
: ?run-file ( file -- )
dup exists? [ [ [ run-file ] keep ] try ] when drop ;
2005-12-31 20:51:58 -05:00
: eval>string ( str -- str )
[ [ [ eval ] keep ] try drop ] string-out ;