2005-08-19 21:46:12 -04:00
|
|
|
! Copyright (C) 2004, 2005 Slava Pestov.
|
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
|
|
|
IN: parser
|
2006-05-15 01:01:47 -04:00
|
|
|
USING: errors generic io kernel math namespaces sequences
|
2006-01-03 17:43:29 -05:00
|
|
|
words ;
|
2005-08-19 21:46:12 -04:00
|
|
|
|
|
|
|
: file-vocabs ( -- )
|
2005-12-17 09:55:00 -05:00
|
|
|
"scratchpad" set-in { "syntax" "scratchpad" } set-use ;
|
2005-08-19 21:46:12 -04:00
|
|
|
|
2006-05-24 04:29:25 -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
|
|
|
[
|
2006-05-17 19:05:44 -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 ;
|
|
|
|
|
2005-09-14 00:37:50 -04:00
|
|
|
: parse-stream ( stream name -- quot )
|
|
|
|
[ file set file-vocabs lines parse-lines ] 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
|
|
|
|
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
|
|
|
|
2005-08-19 21:46:12 -04:00
|
|
|
: parse-resource ( path -- quot )
|
2005-12-11 15:14:41 -05:00
|
|
|
dup parsing-file
|
2005-09-14 00:37:50 -04:00
|
|
|
[ <resource-stream> "resource:" ] keep append parse-stream ;
|
2005-08-19 21:46:12 -04:00
|
|
|
|
2006-01-03 17:43:29 -05:00
|
|
|
: run-resource ( file -- ) parse-resource call ;
|
2005-08-31 21:06:13 -04:00
|
|
|
|
|
|
|
: word-file ( word -- file )
|
2005-09-14 00:37:50 -04:00
|
|
|
"file" word-prop dup
|
|
|
|
[ "resource:/" ?head [ resource-path ] when ] when ;
|
2005-08-31 21:06:13 -04:00
|
|
|
|
2006-01-03 17:43:29 -05:00
|
|
|
: reload ( word -- ) word-file run-file ;
|