2005-02-09 22:35:11 -05:00
|
|
|
! Copyright (C) 2005 Slava Pestov.
|
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
2004-07-16 02:26:21 -04:00
|
|
|
IN: parser
|
2005-02-24 20:52:17 -05:00
|
|
|
USING: errors kernel lists math namespaces streams strings words
|
2005-02-09 22:35:11 -05:00
|
|
|
unparser ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
! The parser uses a number of variables:
|
|
|
|
! line - the line being parsed
|
|
|
|
! pos - position in the line
|
|
|
|
! use - list of vocabularies
|
|
|
|
! in - vocabulary for new words
|
|
|
|
!
|
|
|
|
! When a token is scanned, it is searched for in the 'use' list
|
|
|
|
! of vocabularies. If it is a parsing word, it is executed
|
|
|
|
! immediately. Otherwise it is appended to the parse tree.
|
|
|
|
|
2005-03-21 20:53:26 -05:00
|
|
|
SYMBOL: file
|
|
|
|
|
2004-07-21 19:26:41 -04:00
|
|
|
: parsing? ( word -- ? )
|
2005-03-05 14:45:23 -05:00
|
|
|
dup word? [ "parsing" word-prop ] [ drop f ] ifte ;
|
2004-07-21 19:26:41 -04:00
|
|
|
|
2004-08-12 02:13:43 -04:00
|
|
|
: skip ( n line quot -- n )
|
|
|
|
#! Find the next character that satisfies the quotation,
|
|
|
|
#! which should have stack effect ( ch -- ? ).
|
2005-03-05 16:33:40 -05:00
|
|
|
>r 2dup string-length < [
|
|
|
|
2dup string-nth r> dup >r call [
|
2004-08-12 02:13:43 -04:00
|
|
|
r> 2drop
|
|
|
|
] [
|
2004-12-29 03:35:46 -05:00
|
|
|
>r 1 + r> r> skip
|
2004-08-12 02:13:43 -04:00
|
|
|
] ifte
|
|
|
|
] [
|
2005-03-05 16:33:40 -05:00
|
|
|
r> drop nip string-length
|
2004-12-15 16:57:29 -05:00
|
|
|
] ifte ; inline
|
2004-08-12 02:13:43 -04:00
|
|
|
|
|
|
|
: skip-blank ( n line -- n )
|
|
|
|
[ blank? not ] skip ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2004-08-12 02:13:43 -04:00
|
|
|
: denotation? ( ch -- ? )
|
2004-07-16 02:26:21 -04:00
|
|
|
#! Hard-coded for now. Make this customizable later.
|
2004-08-12 02:13:43 -04:00
|
|
|
#! A 'denotation' is a character that is treated as its
|
2004-07-16 02:26:21 -04:00
|
|
|
#! own word, eg:
|
|
|
|
#!
|
|
|
|
#! "hello world"
|
|
|
|
#!
|
|
|
|
#! Will call the parsing word ".
|
2005-03-05 16:33:40 -05:00
|
|
|
"\"" string-contains? ;
|
2004-08-12 02:13:43 -04:00
|
|
|
|
2005-01-14 14:56:19 -05:00
|
|
|
: skip-word ( n line -- n )
|
2005-03-05 16:33:40 -05:00
|
|
|
2dup string-nth denotation? [
|
2005-01-14 14:56:19 -05:00
|
|
|
drop 1 +
|
2004-07-16 02:26:21 -04:00
|
|
|
] [
|
2005-01-14 14:56:19 -05:00
|
|
|
[ blank? ] skip
|
2004-07-16 02:26:21 -04:00
|
|
|
] ifte ;
|
|
|
|
|
2005-01-14 14:56:19 -05:00
|
|
|
: (scan) ( n line -- start end )
|
|
|
|
[ skip-blank dup ] keep
|
2005-03-05 16:33:40 -05:00
|
|
|
2dup string-length < [ skip-word ] [ drop ] ifte ;
|
2005-01-14 14:56:19 -05:00
|
|
|
|
2004-08-12 02:13:43 -04:00
|
|
|
: scan ( -- token )
|
2004-08-18 19:22:15 -04:00
|
|
|
"col" get "line" get dup >r (scan) dup "col" set
|
2005-01-14 14:56:19 -05:00
|
|
|
2dup = [ r> 3drop f ] [ r> substring ] ifte ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-01-29 14:18:28 -05:00
|
|
|
! If this variable is on, the parser does not internalize words;
|
|
|
|
! it just appends strings to the parse tree as they are read.
|
|
|
|
SYMBOL: string-mode
|
|
|
|
global [ string-mode off ] bind
|
|
|
|
|
2004-09-14 23:23:05 -04:00
|
|
|
: scan-word ( -- obj )
|
|
|
|
scan dup [
|
2005-01-29 14:18:28 -05:00
|
|
|
dup ";" = not string-mode get and [
|
2005-03-21 14:39:46 -05:00
|
|
|
dup "use" get search [ ] [ str>number ] ?ifte
|
2005-01-29 14:18:28 -05:00
|
|
|
] unless
|
2004-09-14 23:23:05 -04:00
|
|
|
] when ;
|
2004-07-21 19:26:41 -04:00
|
|
|
|
2005-01-14 14:56:19 -05:00
|
|
|
: parse-loop ( -- )
|
|
|
|
scan-word [
|
|
|
|
dup parsing? [ execute ] [ swons ] ifte parse-loop
|
|
|
|
] when* ;
|
|
|
|
|
2004-09-14 23:23:05 -04:00
|
|
|
: (parse) ( str -- )
|
2005-01-14 14:56:19 -05:00
|
|
|
"line" set 0 "col" set
|
|
|
|
parse-loop
|
|
|
|
"line" off "col" off ;
|
2004-07-18 19:52:01 -04:00
|
|
|
|
|
|
|
: parse ( str -- code )
|
2004-07-16 02:26:21 -04:00
|
|
|
#! Parse the string into a parse tree that can be executed.
|
2004-10-12 01:11:35 -04:00
|
|
|
f swap (parse) reverse ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
: eval ( "X" -- X )
|
|
|
|
parse call ;
|
|
|
|
|
2004-07-22 19:48:50 -04:00
|
|
|
! Used by parsing words
|
2004-07-16 02:26:21 -04:00
|
|
|
: ch-search ( ch -- index )
|
2004-08-18 19:22:15 -04:00
|
|
|
"col" get "line" get rot index-of* ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
: (until) ( index -- str )
|
2004-12-29 03:35:46 -05:00
|
|
|
"col" get swap dup 1 + "col" set "line" get substring ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
: until ( ch -- str )
|
|
|
|
ch-search (until) ;
|
|
|
|
|
2004-10-27 23:13:00 -04:00
|
|
|
: (until-eol) ( -- index )
|
2005-03-05 16:33:40 -05:00
|
|
|
"\n" ch-search dup -1 = [ drop "line" get string-length ] when ;
|
2004-10-27 23:13:00 -04:00
|
|
|
|
2004-07-22 19:48:50 -04:00
|
|
|
: until-eol ( -- str )
|
2004-10-27 23:13:00 -04:00
|
|
|
#! This is just a hack to get "eval" to work with multiline
|
|
|
|
#! strings from jEdit with EOL comments. Normally, input to
|
|
|
|
#! the parser is already line-tokenized.
|
|
|
|
(until-eol) (until) ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-02-09 22:35:11 -05:00
|
|
|
: save-location ( word -- )
|
|
|
|
#! Remember where this word was defined.
|
|
|
|
dup set-word
|
2005-03-05 14:45:23 -05:00
|
|
|
dup line-number get "line" set-word-prop
|
|
|
|
dup "col" get "col" set-word-prop
|
|
|
|
file get "file" set-word-prop ;
|
2005-02-09 22:35:11 -05:00
|
|
|
|
2005-03-23 22:49:40 -05:00
|
|
|
: create-in "in" get create dup save-location ;
|
2005-02-09 22:35:11 -05:00
|
|
|
|
2005-03-23 22:49:40 -05:00
|
|
|
: CREATE ( -- word ) scan create-in ;
|
2004-12-15 16:57:29 -05:00
|
|
|
|
2005-01-14 14:56:19 -05:00
|
|
|
: escape ( ch -- esc )
|
2004-12-15 16:57:29 -05:00
|
|
|
[
|
2005-01-13 19:49:47 -05:00
|
|
|
[[ CHAR: e CHAR: \e ]]
|
|
|
|
[[ CHAR: n CHAR: \n ]]
|
|
|
|
[[ CHAR: r CHAR: \r ]]
|
|
|
|
[[ CHAR: t CHAR: \t ]]
|
|
|
|
[[ CHAR: s CHAR: \s ]]
|
|
|
|
[[ CHAR: \s CHAR: \s ]]
|
|
|
|
[[ CHAR: 0 CHAR: \0 ]]
|
|
|
|
[[ CHAR: \\ CHAR: \\ ]]
|
|
|
|
[[ CHAR: \" CHAR: \" ]]
|
2005-01-14 14:56:19 -05:00
|
|
|
] assoc dup [ "Bad escape" throw ] unless ;
|
2004-12-15 16:57:29 -05:00
|
|
|
|
2005-01-14 14:56:19 -05:00
|
|
|
: next-escape ( n str -- ch n )
|
2005-03-05 16:33:40 -05:00
|
|
|
2dup string-nth CHAR: u = [
|
2005-01-14 14:56:19 -05:00
|
|
|
swap 1 + dup 4 + [ rot substring hex> ] keep
|
2004-12-15 16:57:29 -05:00
|
|
|
] [
|
2005-03-05 16:33:40 -05:00
|
|
|
over 1 + >r string-nth escape r>
|
2004-12-15 16:57:29 -05:00
|
|
|
] ifte ;
|
|
|
|
|
2005-01-14 14:56:19 -05:00
|
|
|
: next-char ( n str -- ch n )
|
2005-03-05 16:33:40 -05:00
|
|
|
2dup string-nth CHAR: \\ = [
|
2005-01-14 14:56:19 -05:00
|
|
|
>r 1 + r> next-escape
|
|
|
|
] [
|
2005-03-05 16:33:40 -05:00
|
|
|
over 1 + >r string-nth r>
|
2005-01-14 14:56:19 -05:00
|
|
|
] ifte ;
|
2004-12-15 16:57:29 -05:00
|
|
|
|
|
|
|
: doc-comment-here? ( parsed -- ? )
|
|
|
|
not "in-definition" get and ;
|
|
|
|
|
|
|
|
: parsed-stack-effect ( parsed str -- parsed )
|
|
|
|
over doc-comment-here? [
|
2005-03-05 14:45:23 -05:00
|
|
|
word "stack-effect" word-prop [
|
2004-12-15 16:57:29 -05:00
|
|
|
drop
|
|
|
|
] [
|
2005-03-05 14:45:23 -05:00
|
|
|
word swap "stack-effect" set-word-prop
|
2004-12-15 16:57:29 -05:00
|
|
|
] ifte
|
|
|
|
] [
|
|
|
|
drop
|
|
|
|
] ifte ;
|
2004-11-20 16:57:01 -05:00
|
|
|
|
2004-12-15 16:57:29 -05:00
|
|
|
: documentation+ ( word str -- )
|
2005-03-05 14:45:23 -05:00
|
|
|
over "documentation" word-prop [
|
2004-12-15 16:57:29 -05:00
|
|
|
swap "\n" swap cat3
|
|
|
|
] when*
|
2005-03-05 14:45:23 -05:00
|
|
|
"documentation" set-word-prop ;
|
2004-11-20 16:57:01 -05:00
|
|
|
|
2004-12-15 16:57:29 -05:00
|
|
|
: parsed-documentation ( parsed str -- parsed )
|
|
|
|
over doc-comment-here? [
|
|
|
|
word swap documentation+
|
|
|
|
] [
|
|
|
|
drop
|
|
|
|
] ifte ;
|