factor/library/syntax/parse-stream.factor

53 lines
1.4 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-08-02 03:49:13 -04:00
USING: errors generic hashtables io kernel math namespaces
sequences words ;
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 -- )
[ [ <parse-error> 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-01-03 17:43:29 -05:00
: parse ( str -- code ) <string-reader> lines parse-lines ;
: eval ( "X" -- X ) parse call ;
SYMBOL: parse-hook
2005-09-14 00:37:50 -04:00
: parse-stream ( stream name -- quot )
[
file set file-vocabs
lines parse-lines
parse-hook get call
] with-scope ;
2005-08-19 21:46:12 -04:00
2005-12-16 21:12:35 -05:00
: parsing-file ( file -- ) "Loading " write print flush ;
2005-12-11 14:27:36 -05:00
2005-08-19 21:46:12 -04:00
: parse-file ( file -- quot )
2006-01-03 17:43:29 -05:00
dup parsing-file [ <file-reader> ] keep parse-stream ;
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
: no-parse-hook ( quot -- )
[ parse-hook off call ] with-scope ; inline
2006-01-03 17:43:29 -05:00
: try-run-file ( file -- ) [ [ run-file ] keep ] try drop ;
2005-12-31 20:51:58 -05:00
: eval>string ( str -- str )
[ [ [ eval ] keep ] try drop ] string-out ;
2005-08-19 21:46:12 -04:00
: parse-resource ( path -- quot )
2005-12-11 15:14:41 -05:00
dup parsing-file
[ <resource-reader> "resource:" ] keep append parse-stream ;
2005-08-19 21:46:12 -04:00
: run-resource ( file -- )
parse-resource call ;