factor/library/syntax/parser.factor

175 lines
4.4 KiB
Factor
Raw Normal View History

! 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
USING: errors kernel lists math namespaces streams strings words
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
: parsing? ( word -- ? )
dup word? [ "parsing" word-prop ] [ drop f ] ifte ;
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 -- ? ).
>r 2dup string-length < [
2dup string-nth r> dup >r call [
2004-08-12 02:13:43 -04:00
r> 2drop
] [
>r 1 + r> r> skip
2004-08-12 02:13:43 -04:00
] ifte
] [
r> drop nip string-length
] 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 ".
"\"" string-contains? ;
2004-08-12 02:13:43 -04:00
: skip-word ( n line -- n )
2dup string-nth denotation? [
drop 1 +
2004-07-16 02:26:21 -04:00
] [
[ blank? ] skip
2004-07-16 02:26:21 -04:00
] ifte ;
: (scan) ( n line -- start end )
[ skip-blank dup ] keep
2dup string-length < [ skip-word ] [ drop ] ifte ;
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
2dup = [ r> 3drop f ] [ r> substring ] ifte ;
2004-07-16 02:26:21 -04: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
: scan-word ( -- obj )
scan dup [
dup ";" = not string-mode get and [
2005-03-21 14:39:46 -05:00
dup "use" get search [ ] [ str>number ] ?ifte
] unless
] when ;
: parse-loop ( -- )
scan-word [
dup parsing? [ execute ] [ swons ] ifte parse-loop
] when* ;
: (parse) ( str -- )
"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.
f swap (parse) reverse ;
2004-07-16 02:26:21 -04:00
: eval ( "X" -- X )
parse call ;
! 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 )
"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 )
"\n" ch-search dup -1 = [ drop "line" get string-length ] when ;
2004-10-27 23:13:00 -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
: save-location ( word -- )
#! Remember where this word was defined.
dup set-word
dup line-number get "line" set-word-prop
dup "col" get "col" set-word-prop
file get "file" set-word-prop ;
2005-03-23 22:49:40 -05:00
: create-in "in" get create dup save-location ;
2005-03-23 22:49:40 -05:00
: CREATE ( -- word ) scan create-in ;
: escape ( ch -- esc )
[
[[ 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: \" ]]
] assoc dup [ "Bad escape" throw ] unless ;
: next-escape ( n str -- ch n )
2dup string-nth CHAR: u = [
swap 1 + dup 4 + [ rot substring hex> ] keep
] [
over 1 + >r string-nth escape r>
] ifte ;
: next-char ( n str -- ch n )
2dup string-nth CHAR: \\ = [
>r 1 + r> next-escape
] [
over 1 + >r string-nth r>
] ifte ;
: doc-comment-here? ( parsed -- ? )
not "in-definition" get and ;
: parsed-stack-effect ( parsed str -- parsed )
over doc-comment-here? [
word "stack-effect" word-prop [
drop
] [
word swap "stack-effect" set-word-prop
] ifte
] [
drop
] ifte ;
: documentation+ ( word str -- )
over "documentation" word-prop [
swap "\n" swap cat3
] when*
"documentation" set-word-prop ;
: parsed-documentation ( parsed str -- parsed )
over doc-comment-here? [
word swap documentation+
] [
drop
] ifte ;