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 ( -- )
|
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 ;
|
|
|
|
|
2006-08-11 16:55:43 -04:00
|
|
|
SYMBOL: parse-hook
|
|
|
|
|
2005-09-14 00:37:50 -04:00
|
|
|
: parse-stream ( stream name -- quot )
|
2006-08-11 16:55:43 -04:00
|
|
|
[
|
|
|
|
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
|
|
|
|
2006-08-11 16:55:43 -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
|
|
|
|
2006-05-24 18:40:54 -04: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
|
2006-06-11 16:16:45 -04:00
|
|
|
[ <resource-reader> "resource:" ] keep append parse-stream ;
|
2005-08-19 21:46:12 -04:00
|
|
|
|
2006-08-11 16:55:43 -04:00
|
|
|
: run-resource ( file -- )
|
|
|
|
parse-resource call ;
|