2004-07-22 19:48:50 -04:00
|
|
|
! :folding=indent:collapseFolds=1:
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
! $Id$
|
|
|
|
!
|
|
|
|
! Copyright (C) 2004 Slava Pestov.
|
|
|
|
!
|
|
|
|
! Redistribution and use in source and binary forms, with or without
|
|
|
|
! modification, are permitted provided that the following conditions are met:
|
|
|
|
!
|
|
|
|
! 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
! this list of conditions and the following disclaimer.
|
|
|
|
!
|
|
|
|
! 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
! this list of conditions and the following disclaimer in the documentation
|
|
|
|
! and/or other materials provided with the distribution.
|
|
|
|
!
|
|
|
|
! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
IN: parser
|
|
|
|
USE: errors
|
|
|
|
USE: kernel
|
|
|
|
USE: lists
|
2004-08-26 22:21:17 -04:00
|
|
|
USE: math
|
2004-07-16 02:26:21 -04:00
|
|
|
USE: namespaces
|
|
|
|
USE: strings
|
|
|
|
USE: words
|
2004-07-19 00:34:03 -04:00
|
|
|
USE: 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.
|
|
|
|
|
2004-07-21 19:26:41 -04:00
|
|
|
: parsing? ( word -- ? )
|
2005-01-02 23:57:54 -05:00
|
|
|
dup word? [ "parsing" word-property ] [ drop f ] ifte ;
|
2004-07-21 19:26:41 -04:00
|
|
|
|
2004-09-14 23:23:05 -04:00
|
|
|
: end? ( -- ? )
|
|
|
|
"col" get "line" get str-length >= ;
|
|
|
|
|
|
|
|
: (with-parser) ( quot -- )
|
|
|
|
end? [ drop ] [ [ call ] keep (with-parser) ] ifte ;
|
|
|
|
|
|
|
|
: with-parser ( text quot -- )
|
|
|
|
#! Keep calling the quotation until we reach the end of the
|
|
|
|
#! input.
|
|
|
|
swap "line" set 0 "col" set
|
|
|
|
(with-parser)
|
|
|
|
"line" off "col" off ;
|
|
|
|
|
2004-08-18 19:22:15 -04:00
|
|
|
: ch ( -- ch ) "col" get "line" get str-nth ;
|
2004-12-29 03:35:46 -05:00
|
|
|
: advance ( -- ) "col" [ 1 + ] change ;
|
2004-07-16 02:26:21 -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 -- ? ).
|
|
|
|
>r 2dup str-length < [
|
|
|
|
2dup str-nth r> dup >r call [
|
|
|
|
r> 2drop
|
|
|
|
] [
|
2004-12-29 03:35:46 -05:00
|
|
|
>r 1 + r> r> skip
|
2004-08-12 02:13:43 -04:00
|
|
|
] ifte
|
|
|
|
] [
|
|
|
|
r> drop nip str-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
|
|
|
: skip-word ( n line -- n )
|
|
|
|
[ blank? ] skip ;
|
|
|
|
|
|
|
|
: 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 ".
|
2004-08-12 02:13:43 -04:00
|
|
|
"\"" str-contains? ;
|
|
|
|
|
|
|
|
: (scan) ( n line -- start end )
|
|
|
|
dup >r skip-blank dup r>
|
|
|
|
2dup str-length < [
|
|
|
|
2dup str-nth denotation? [
|
2004-12-29 03:35:46 -05:00
|
|
|
drop 1 +
|
2004-08-12 02:13:43 -04:00
|
|
|
] [
|
|
|
|
skip-word
|
|
|
|
] ifte
|
2004-07-16 02:26:21 -04:00
|
|
|
] [
|
2004-08-12 02:13:43 -04:00
|
|
|
drop
|
2004-07-16 02:26:21 -04:00
|
|
|
] 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
|
2004-08-12 02:13:43 -04:00
|
|
|
2dup = [
|
|
|
|
r> 3drop f
|
|
|
|
] [
|
|
|
|
r> substring
|
|
|
|
] ifte ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2004-09-14 23:23:05 -04:00
|
|
|
: scan-word ( -- obj )
|
|
|
|
scan dup [
|
2005-01-02 23:57:54 -05:00
|
|
|
dup "use" get search [ str>number ] ?unless
|
2004-09-14 23:23:05 -04:00
|
|
|
] when ;
|
2004-07-21 19:26:41 -04:00
|
|
|
|
2004-09-14 23:23:05 -04:00
|
|
|
: (parse) ( str -- )
|
2004-07-16 02:26:21 -04:00
|
|
|
[
|
2004-09-14 23:23:05 -04:00
|
|
|
scan-word [
|
2005-01-13 19:49:47 -05:00
|
|
|
dup parsing? [ execute ] [ swons ] ifte
|
2004-09-14 23:23:05 -04:00
|
|
|
] when*
|
|
|
|
] with-parser ;
|
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 )
|
|
|
|
"\n" ch-search dup -1 = [ drop "line" get str-length ] when ;
|
|
|
|
|
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
|
|
|
|
2004-07-19 00:34:03 -04:00
|
|
|
: next-ch ( -- ch )
|
|
|
|
end? [ "Unexpected EOF" throw ] [ ch advance ] ifte ;
|
2004-08-12 02:13:43 -04:00
|
|
|
|
|
|
|
: next-word-ch ( -- ch )
|
2004-08-18 19:22:15 -04:00
|
|
|
"col" get "line" get skip-blank "col" set next-ch ;
|
2004-08-20 18:48:08 -04:00
|
|
|
|
2004-12-15 16:57:29 -05:00
|
|
|
: CREATE ( -- word )
|
|
|
|
scan "in" get create dup set-word
|
|
|
|
dup f "documentation" set-word-property
|
|
|
|
dup f "stack-effect" set-word-property
|
|
|
|
dup "line-number" get "line" set-word-property
|
|
|
|
dup "col" get "col" set-word-property
|
|
|
|
dup "file" get "file" set-word-property ;
|
|
|
|
|
|
|
|
! \x
|
|
|
|
: unicode-escape>ch ( -- esc )
|
|
|
|
#! Read \u....
|
|
|
|
next-ch digit> 16 *
|
|
|
|
next-ch digit> + 16 *
|
|
|
|
next-ch digit> + 16 *
|
|
|
|
next-ch digit> + ;
|
|
|
|
|
|
|
|
: ascii-escape>ch ( ch -- esc )
|
|
|
|
[
|
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: \" ]]
|
2004-12-15 16:57:29 -05:00
|
|
|
] assoc ;
|
|
|
|
|
|
|
|
: escape ( ch -- esc )
|
|
|
|
dup CHAR: u = [
|
|
|
|
drop unicode-escape>ch
|
|
|
|
] [
|
|
|
|
ascii-escape>ch
|
|
|
|
] ifte ;
|
|
|
|
|
|
|
|
: parse-escape ( -- )
|
|
|
|
next-ch escape dup [ drop "Bad escape" throw ] unless ;
|
|
|
|
|
|
|
|
: parse-ch ( ch -- ch )
|
|
|
|
dup CHAR: \\ = [ drop parse-escape ] when ;
|
|
|
|
|
|
|
|
: doc-comment-here? ( parsed -- ? )
|
|
|
|
not "in-definition" get and ;
|
|
|
|
|
|
|
|
: parsed-stack-effect ( parsed str -- parsed )
|
|
|
|
over doc-comment-here? [
|
|
|
|
word stack-effect [
|
|
|
|
drop
|
|
|
|
] [
|
|
|
|
word swap "stack-effect" set-word-property
|
|
|
|
] ifte
|
|
|
|
] [
|
|
|
|
drop
|
|
|
|
] ifte ;
|
2004-11-20 16:57:01 -05:00
|
|
|
|
2004-12-15 16:57:29 -05:00
|
|
|
: documentation+ ( word str -- )
|
|
|
|
over "documentation" word-property [
|
|
|
|
swap "\n" swap cat3
|
|
|
|
] when*
|
|
|
|
"documentation" set-word-property ;
|
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 ;
|