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-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 ;
|
|
|
|
|
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
|
|
|
|
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-01-03 17:43:29 -05:00
|
|
|
: run-resource ( file -- ) parse-resource call ;
|
2005-08-31 21:06:13 -04:00
|
|
|
|
2006-08-02 03:49:13 -04:00
|
|
|
GENERIC: where ( spec -- loc )
|
2005-08-31 21:06:13 -04:00
|
|
|
|
2006-08-02 03:49:13 -04:00
|
|
|
M: word where "loc" word-prop ;
|
|
|
|
|
|
|
|
M: method-spec where
|
|
|
|
dup first2 "methods" word-prop hash method-loc
|
|
|
|
[ ] [ second where ] ?if ;
|
|
|
|
|
|
|
|
: ?resource-path ( path -- path )
|
|
|
|
"resource:/" ?head [ resource-path ] when ;
|
|
|
|
|
|
|
|
: reload ( spec -- )
|
|
|
|
where first [ ?resource-path run-file ] when* ;
|