2007-11-19 22:36:38 -05:00
|
|
|
! Copyright (C) 2007 Chris Double.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2007-11-28 18:35:45 -05:00
|
|
|
USING: kernel sequences strings namespaces math assocs shuffle
|
2008-03-20 08:25:45 -04:00
|
|
|
vectors arrays combinators.lib math.parser match
|
2008-03-03 17:57:30 -05:00
|
|
|
unicode.categories sequences.lib compiler.units parser
|
2008-03-26 18:23:58 -04:00
|
|
|
words quotations effects memoize accessors
|
|
|
|
combinators.cleave locals ;
|
2007-11-19 22:36:38 -05:00
|
|
|
IN: peg
|
|
|
|
|
2007-11-20 21:31:23 -05:00
|
|
|
TUPLE: parse-result remaining ast ;
|
2007-11-19 22:36:38 -05:00
|
|
|
|
2007-11-26 18:22:33 -05:00
|
|
|
SYMBOL: ignore
|
|
|
|
|
2007-11-20 21:31:23 -05:00
|
|
|
: <parse-result> ( remaining ast -- parse-result )
|
2007-11-19 22:36:38 -05:00
|
|
|
parse-result construct-boa ;
|
|
|
|
|
2008-03-26 18:23:58 -04:00
|
|
|
SYMBOL: packrat
|
2008-03-20 10:05:21 -04:00
|
|
|
|
2008-03-26 22:21:38 -04:00
|
|
|
: compiled-parsers ( -- cache )
|
|
|
|
\ compiled-parsers get-global [ H{ } clone dup \ compiled-parsers set-global ] unless* ;
|
|
|
|
|
|
|
|
: reset-compiled-parsers ( -- )
|
|
|
|
H{ } clone \ compiled-parsers set-global ;
|
|
|
|
|
2008-03-20 10:05:21 -04:00
|
|
|
GENERIC: (compile) ( parser -- quot )
|
|
|
|
|
2008-03-27 00:24:05 -04:00
|
|
|
: input-from ( input -- n )
|
|
|
|
#! Return the index from the original string that the
|
|
|
|
#! input slice is based on.
|
|
|
|
dup slice? [ slice-from ] [ drop 0 ] if ;
|
|
|
|
|
2008-03-27 19:00:36 -04:00
|
|
|
: input-cache ( id -- cache )
|
2008-03-27 00:45:59 -04:00
|
|
|
#! From the packrat cache, obtain the cache for the parser quotation
|
|
|
|
#! that maps the input string position to the parser result.
|
2008-03-27 19:00:36 -04:00
|
|
|
packrat get [ drop H{ } clone ] cache ;
|
2008-03-27 00:45:59 -04:00
|
|
|
|
2008-03-27 07:50:46 -04:00
|
|
|
TUPLE: left-recursion detected? ;
|
|
|
|
C: <left-recursion> left-recursion
|
|
|
|
|
|
|
|
USE: prettyprint
|
2008-03-27 20:40:26 -04:00
|
|
|
USE: io
|
|
|
|
|
2008-03-27 07:50:46 -04:00
|
|
|
|
|
|
|
:: handle-left-recursive-result ( result -- result )
|
|
|
|
#! If the result is from a left-recursive call,
|
|
|
|
#! note this and fail, otherwise return normal result
|
|
|
|
#! See figure 4 of packrat_TR-2007-002.pdf.
|
2008-03-27 20:40:26 -04:00
|
|
|
">>handle-left-recursive-result " write result .
|
2008-03-27 07:50:46 -04:00
|
|
|
result [
|
|
|
|
[let* | ast [ result ast>> ] |
|
|
|
|
ast left-recursion? [ t ast (>>detected?) f ] [ result ] if
|
|
|
|
]
|
|
|
|
] [
|
|
|
|
f
|
2008-03-27 20:40:26 -04:00
|
|
|
] if
|
|
|
|
"<<handle-left-recursive-result " write dup . ;
|
2008-03-27 07:50:46 -04:00
|
|
|
|
2008-03-27 20:40:26 -04:00
|
|
|
:: grow-lr ( input quot parser m -- result )
|
2008-03-27 07:50:46 -04:00
|
|
|
#! 'Grow the Seed' algorithm to handle left recursion
|
2008-03-27 20:40:26 -04:00
|
|
|
">>grow-lr " write input . " for parser " write parser . " m is " write m .
|
2008-03-27 07:50:46 -04:00
|
|
|
[let* | ans [ input quot call ] |
|
|
|
|
[ ans not ] [ ans [ ans remaining>> input-from m remaining>> input-from <= ] [ f ] if ] 2array || [
|
|
|
|
"recursion exiting with = " write ans . "m was " write m .
|
|
|
|
ans
|
|
|
|
] [
|
|
|
|
"recursion with = " write ans .
|
|
|
|
input quot ans grow-lr
|
|
|
|
] if
|
2008-03-27 20:40:26 -04:00
|
|
|
]
|
|
|
|
"<<grow-lr " write input . " for parser " write parser . " m is " write m . " result is " write dup .
|
|
|
|
;
|
2008-03-27 07:50:46 -04:00
|
|
|
|
2008-03-27 20:40:26 -04:00
|
|
|
:: cached-result ( input-cache input quot parser -- result )
|
2008-03-27 19:00:36 -04:00
|
|
|
#! Get the cached result for input position
|
2008-03-27 00:45:59 -04:00
|
|
|
#! from the input cache. If the item is not in the cache,
|
|
|
|
#! call 'quot' with 'input' on the stack to get the result
|
|
|
|
#! and store that in the cache and return it.
|
2008-03-27 07:50:46 -04:00
|
|
|
#! See figure 4 of packrat_TR-2007-002.pdf.
|
2008-03-27 20:40:26 -04:00
|
|
|
">>cached-result " write input . " for parser " write parser .
|
2008-03-27 19:00:36 -04:00
|
|
|
input input-from input-cache [
|
2008-03-27 06:54:34 -04:00
|
|
|
drop
|
2008-03-27 07:50:46 -04:00
|
|
|
[let* | lr [ f <left-recursion> ]
|
|
|
|
m [ input lr <parse-result> ]
|
|
|
|
ans [ m input input-from input-cache set-at input quot call ]
|
|
|
|
|
|
2008-03-27 20:40:26 -04:00
|
|
|
"--lr is " write lr . " ans is " write ans . " for parser " write parser .
|
|
|
|
ans input input-from input-cache set-at
|
2008-03-27 07:50:46 -04:00
|
|
|
lr detected?>> ans and [
|
2008-03-27 20:40:26 -04:00
|
|
|
input quot parser ans grow-lr
|
2008-03-27 07:50:46 -04:00
|
|
|
] [
|
|
|
|
ans
|
|
|
|
] if
|
|
|
|
]
|
|
|
|
] cache
|
2008-03-27 20:40:26 -04:00
|
|
|
dup [ handle-left-recursive-result ] when
|
|
|
|
"<<cached-result " write dup . " for parser " write parser . ;
|
2008-03-27 00:45:59 -04:00
|
|
|
|
2008-03-27 20:40:26 -04:00
|
|
|
:: run-packrat-parser ( input quot parser -- result )
|
|
|
|
parser id>> input-cache
|
|
|
|
input quot parser cached-result ; inline
|
2008-03-26 18:23:58 -04:00
|
|
|
|
2008-03-26 00:38:30 -04:00
|
|
|
: run-parser ( input quot -- result )
|
2008-03-26 18:23:58 -04:00
|
|
|
#! If a packrat cache is available, use memoization for
|
|
|
|
#! packrat parsing, otherwise do a standard peg call.
|
|
|
|
packrat get [ run-packrat-parser ] [ call ] if* ; inline
|
2008-03-26 00:38:30 -04:00
|
|
|
|
2008-03-27 19:00:36 -04:00
|
|
|
:: parser-body ( parser -- quot )
|
|
|
|
#! Return the body of the word that is the compiled version
|
|
|
|
#! of the parser.
|
|
|
|
[let* | parser-quot [ parser (compile) ]
|
|
|
|
|
|
|
|
|
[
|
|
|
|
packrat get [
|
2008-03-27 20:40:26 -04:00
|
|
|
parser-quot parser run-packrat-parser
|
2008-03-27 19:00:36 -04:00
|
|
|
] [
|
|
|
|
parser-quot call
|
|
|
|
] if
|
|
|
|
]
|
|
|
|
] ;
|
|
|
|
|
2008-03-20 10:05:21 -04:00
|
|
|
: compiled-parser ( parser -- word )
|
2008-03-21 08:59:16 -04:00
|
|
|
#! Look to see if the given parser has been compiled.
|
2008-03-20 10:05:21 -04:00
|
|
|
#! If not, compile it to a temporary word, cache it,
|
|
|
|
#! and return it. Otherwise return the existing one.
|
2008-03-27 21:10:33 -04:00
|
|
|
#! Circular parsers are supported by getting the word
|
|
|
|
#! name and storing it in the cache, before compiling,
|
|
|
|
#! so it is picked up when re-entered.
|
|
|
|
dup id>> compiled-parsers [
|
|
|
|
drop dup gensym swap 2dup id>> compiled-parsers set-at
|
|
|
|
2dup parser-body define
|
|
|
|
dupd "peg" set-word-prop
|
|
|
|
] cache nip ;
|
2008-03-20 10:05:21 -04:00
|
|
|
|
2008-03-25 23:43:03 -04:00
|
|
|
: compile ( parser -- word )
|
2008-03-26 22:21:38 -04:00
|
|
|
[ compiled-parser ] with-compilation-unit ;
|
2008-03-20 10:05:21 -04:00
|
|
|
|
|
|
|
: parse ( state parser -- result )
|
2008-03-27 07:04:08 -04:00
|
|
|
compile execute ; inline
|
2008-03-20 10:05:21 -04:00
|
|
|
|
2008-03-26 22:50:27 -04:00
|
|
|
: with-packrat ( quot -- result )
|
|
|
|
#! Run the quotation with a packrat cache active.
|
2008-03-27 07:04:08 -04:00
|
|
|
[ H{ } clone packrat ] dip with-variable ; inline
|
|
|
|
|
|
|
|
: packrat-parse ( state parser -- result )
|
|
|
|
[ parse ] with-packrat ;
|
|
|
|
|
|
|
|
: packrat-call ( state quot -- result )
|
|
|
|
with-packrat ; inline
|
2008-03-26 22:50:27 -04:00
|
|
|
|
2007-11-28 18:35:45 -05:00
|
|
|
<PRIVATE
|
|
|
|
|
2008-03-27 19:00:36 -04:00
|
|
|
SYMBOL: id
|
|
|
|
|
|
|
|
: next-id ( -- n )
|
|
|
|
#! Return the next unique id for a parser
|
|
|
|
id get-global [
|
|
|
|
dup 1+ id set-global
|
|
|
|
] [
|
|
|
|
1 id set-global 0
|
|
|
|
] if* ;
|
|
|
|
|
|
|
|
TUPLE: parser id ;
|
|
|
|
M: parser equal? [ id>> ] 2apply = ;
|
|
|
|
C: <parser> parser
|
|
|
|
|
|
|
|
: delegates ( -- cache )
|
|
|
|
\ delegates get-global [ H{ } clone dup \ delegates set-global ] unless* ;
|
|
|
|
|
|
|
|
: reset-delegates ( -- )
|
|
|
|
H{ } clone \ delegates set-global ;
|
|
|
|
|
|
|
|
: init-parser ( parser -- parser )
|
|
|
|
#! Set the delegate for the parser. Equivalent parsers
|
|
|
|
#! get a delegate with the same id.
|
|
|
|
dup clone delegates [
|
|
|
|
drop next-id <parser>
|
|
|
|
] cache over set-delegate ;
|
|
|
|
|
2007-11-19 22:36:38 -05:00
|
|
|
TUPLE: token-parser symbol ;
|
|
|
|
|
2007-12-20 19:16:14 -05:00
|
|
|
MATCH-VARS: ?token ;
|
2007-11-26 21:08:16 -05:00
|
|
|
|
2008-03-25 22:40:17 -04:00
|
|
|
: parse-token ( input string -- result )
|
|
|
|
#! Parse the string, returning a parse result
|
|
|
|
2dup head? [
|
|
|
|
dup >r length tail-slice r> <parse-result>
|
|
|
|
] [
|
|
|
|
2drop f
|
|
|
|
] if ;
|
|
|
|
|
2008-03-20 10:05:21 -04:00
|
|
|
M: token-parser (compile) ( parser -- quot )
|
2008-03-25 23:08:14 -04:00
|
|
|
symbol>> [ parse-token ] curry ;
|
2007-12-20 19:16:14 -05:00
|
|
|
|
|
|
|
TUPLE: satisfy-parser quot ;
|
|
|
|
|
|
|
|
MATCH-VARS: ?quot ;
|
|
|
|
|
|
|
|
: satisfy-pattern ( -- quot )
|
|
|
|
[
|
|
|
|
dup empty? [
|
|
|
|
drop f
|
|
|
|
] [
|
|
|
|
unclip-slice dup ?quot call [
|
|
|
|
<parse-result>
|
|
|
|
] [
|
|
|
|
2drop f
|
|
|
|
] if
|
|
|
|
] if
|
|
|
|
] ;
|
|
|
|
|
2008-03-20 10:05:21 -04:00
|
|
|
M: satisfy-parser (compile) ( parser -- quot )
|
2008-03-25 23:08:14 -04:00
|
|
|
quot>> \ ?quot satisfy-pattern match-replace ;
|
2007-11-26 21:08:16 -05:00
|
|
|
|
2007-11-19 22:36:38 -05:00
|
|
|
TUPLE: range-parser min max ;
|
|
|
|
|
2007-12-20 19:16:14 -05:00
|
|
|
MATCH-VARS: ?min ?max ;
|
|
|
|
|
|
|
|
: range-pattern ( -- quot )
|
|
|
|
[
|
|
|
|
dup empty? [
|
|
|
|
drop f
|
2007-11-19 22:36:38 -05:00
|
|
|
] [
|
2007-12-20 19:16:14 -05:00
|
|
|
0 over nth dup
|
|
|
|
?min ?max between? [
|
|
|
|
[ 1 tail-slice ] dip <parse-result>
|
|
|
|
] [
|
|
|
|
2drop f
|
|
|
|
] if
|
|
|
|
] if
|
|
|
|
] ;
|
|
|
|
|
2008-03-20 10:05:21 -04:00
|
|
|
M: range-parser (compile) ( parser -- quot )
|
2007-12-20 19:16:14 -05:00
|
|
|
T{ range-parser _ ?min ?max } range-pattern match-replace ;
|
2007-11-19 22:36:38 -05:00
|
|
|
|
|
|
|
TUPLE: seq-parser parsers ;
|
|
|
|
|
2007-12-20 19:16:14 -05:00
|
|
|
: seq-pattern ( -- quot )
|
|
|
|
[
|
|
|
|
dup [
|
2008-03-25 23:08:14 -04:00
|
|
|
dup remaining>> ?quot [
|
|
|
|
[ remaining>> swap (>>remaining) ] 2keep
|
|
|
|
ast>> dup ignore = [
|
2007-12-20 19:16:14 -05:00
|
|
|
drop
|
|
|
|
] [
|
2008-03-25 23:08:14 -04:00
|
|
|
swap [ ast>> push ] keep
|
2007-12-20 19:16:14 -05:00
|
|
|
] if
|
|
|
|
] [
|
|
|
|
drop f
|
|
|
|
] if*
|
|
|
|
] [
|
|
|
|
drop f
|
|
|
|
] if
|
|
|
|
] ;
|
2007-11-19 22:36:38 -05:00
|
|
|
|
2008-03-20 10:05:21 -04:00
|
|
|
M: seq-parser (compile) ( parser -- quot )
|
2007-12-20 19:16:14 -05:00
|
|
|
[
|
|
|
|
[ V{ } clone <parse-result> ] %
|
2008-03-25 23:08:14 -04:00
|
|
|
parsers>> [ compiled-parser \ ?quot seq-pattern match-replace % ] each
|
2007-12-20 19:16:14 -05:00
|
|
|
] [ ] make ;
|
2007-11-19 22:36:38 -05:00
|
|
|
|
2007-11-19 23:58:11 -05:00
|
|
|
TUPLE: choice-parser parsers ;
|
2007-12-20 19:16:14 -05:00
|
|
|
|
|
|
|
: choice-pattern ( -- quot )
|
|
|
|
[
|
|
|
|
dup [
|
|
|
|
|
2007-11-19 23:58:11 -05:00
|
|
|
] [
|
2008-03-20 10:05:21 -04:00
|
|
|
drop dup ?quot
|
2007-12-20 19:16:14 -05:00
|
|
|
] if
|
|
|
|
] ;
|
2007-11-19 23:58:11 -05:00
|
|
|
|
2008-03-20 10:05:21 -04:00
|
|
|
M: choice-parser (compile) ( parser -- quot )
|
2007-12-20 19:16:14 -05:00
|
|
|
[
|
|
|
|
f ,
|
2008-03-25 23:08:14 -04:00
|
|
|
parsers>> [ compiled-parser \ ?quot choice-pattern match-replace % ] each
|
2007-12-20 19:16:14 -05:00
|
|
|
\ nip ,
|
|
|
|
] [ ] make ;
|
2007-11-19 23:58:11 -05:00
|
|
|
|
2007-11-20 21:01:44 -05:00
|
|
|
TUPLE: repeat0-parser p1 ;
|
|
|
|
|
2007-12-20 19:16:14 -05:00
|
|
|
: (repeat0) ( quot result -- result )
|
2008-03-25 23:08:14 -04:00
|
|
|
2dup remaining>> swap call [
|
|
|
|
[ remaining>> swap (>>remaining) ] 2keep
|
|
|
|
ast>> swap [ ast>> push ] keep
|
2007-12-20 19:16:14 -05:00
|
|
|
(repeat0)
|
2007-11-20 21:01:44 -05:00
|
|
|
] [
|
|
|
|
nip
|
2007-12-20 19:16:14 -05:00
|
|
|
] if* ; inline
|
2007-11-20 21:01:44 -05:00
|
|
|
|
2007-12-20 19:16:14 -05:00
|
|
|
: repeat0-pattern ( -- quot )
|
|
|
|
[
|
2008-03-20 10:05:21 -04:00
|
|
|
[ ?quot ] swap (repeat0)
|
2007-12-20 19:16:14 -05:00
|
|
|
] ;
|
2007-11-20 21:01:44 -05:00
|
|
|
|
2008-03-20 10:05:21 -04:00
|
|
|
M: repeat0-parser (compile) ( parser -- quot )
|
2007-12-20 19:16:14 -05:00
|
|
|
[
|
|
|
|
[ V{ } clone <parse-result> ] %
|
2008-03-25 23:08:14 -04:00
|
|
|
p1>> compiled-parser \ ?quot repeat0-pattern match-replace %
|
2007-12-20 19:16:14 -05:00
|
|
|
] [ ] make ;
|
2007-11-20 21:01:44 -05:00
|
|
|
|
|
|
|
TUPLE: repeat1-parser p1 ;
|
|
|
|
|
2007-12-20 19:16:14 -05:00
|
|
|
: repeat1-pattern ( -- quot )
|
|
|
|
[
|
2008-03-20 10:05:21 -04:00
|
|
|
[ ?quot ] swap (repeat0) [
|
2008-03-25 23:08:14 -04:00
|
|
|
dup ast>> empty? [
|
2007-12-20 19:16:14 -05:00
|
|
|
drop f
|
|
|
|
] when
|
|
|
|
] [
|
|
|
|
f
|
|
|
|
] if*
|
|
|
|
] ;
|
|
|
|
|
2008-03-20 10:05:21 -04:00
|
|
|
M: repeat1-parser (compile) ( parser -- quot )
|
2007-12-20 19:16:14 -05:00
|
|
|
[
|
|
|
|
[ V{ } clone <parse-result> ] %
|
2008-03-25 23:08:14 -04:00
|
|
|
p1>> compiled-parser \ ?quot repeat1-pattern match-replace %
|
2007-12-20 19:16:14 -05:00
|
|
|
] [ ] make ;
|
2007-11-20 21:01:44 -05:00
|
|
|
|
2007-11-20 21:50:47 -05:00
|
|
|
TUPLE: optional-parser p1 ;
|
|
|
|
|
2007-12-20 19:16:14 -05:00
|
|
|
: optional-pattern ( -- quot )
|
|
|
|
[
|
2008-03-20 10:05:21 -04:00
|
|
|
dup ?quot swap f <parse-result> or
|
2007-12-20 19:16:14 -05:00
|
|
|
] ;
|
|
|
|
|
2008-03-20 10:05:21 -04:00
|
|
|
M: optional-parser (compile) ( parser -- quot )
|
2008-03-25 23:08:14 -04:00
|
|
|
p1>> compiled-parser \ ?quot optional-pattern match-replace ;
|
2007-11-20 21:50:47 -05:00
|
|
|
|
2007-11-20 22:06:02 -05:00
|
|
|
TUPLE: ensure-parser p1 ;
|
|
|
|
|
2007-12-20 19:16:14 -05:00
|
|
|
: ensure-pattern ( -- quot )
|
|
|
|
[
|
2008-03-20 10:05:21 -04:00
|
|
|
dup ?quot [
|
2007-12-20 19:16:14 -05:00
|
|
|
ignore <parse-result>
|
|
|
|
] [
|
|
|
|
drop f
|
|
|
|
] if
|
|
|
|
] ;
|
|
|
|
|
2008-03-20 10:05:21 -04:00
|
|
|
M: ensure-parser (compile) ( parser -- quot )
|
2008-03-25 23:08:14 -04:00
|
|
|
p1>> compiled-parser \ ?quot ensure-pattern match-replace ;
|
2007-11-20 22:06:02 -05:00
|
|
|
|
2007-11-20 22:11:49 -05:00
|
|
|
TUPLE: ensure-not-parser p1 ;
|
|
|
|
|
2007-12-20 19:16:14 -05:00
|
|
|
: ensure-not-pattern ( -- quot )
|
|
|
|
[
|
2008-03-20 10:05:21 -04:00
|
|
|
dup ?quot [
|
2007-12-20 19:16:14 -05:00
|
|
|
drop f
|
|
|
|
] [
|
|
|
|
ignore <parse-result>
|
|
|
|
] if
|
|
|
|
] ;
|
|
|
|
|
2008-03-20 10:05:21 -04:00
|
|
|
M: ensure-not-parser (compile) ( parser -- quot )
|
2008-03-25 23:08:14 -04:00
|
|
|
p1>> compiled-parser \ ?quot ensure-not-pattern match-replace ;
|
2007-11-20 22:11:49 -05:00
|
|
|
|
2007-11-20 22:21:23 -05:00
|
|
|
TUPLE: action-parser p1 quot ;
|
|
|
|
|
2007-12-20 19:16:14 -05:00
|
|
|
MATCH-VARS: ?action ;
|
|
|
|
|
|
|
|
: action-pattern ( -- quot )
|
|
|
|
[
|
2008-03-20 10:05:21 -04:00
|
|
|
?quot dup [
|
2008-03-25 23:08:14 -04:00
|
|
|
dup ast>> ?action call
|
|
|
|
>>ast
|
2007-12-20 19:16:14 -05:00
|
|
|
] when
|
|
|
|
] ;
|
|
|
|
|
2008-03-20 10:05:21 -04:00
|
|
|
M: action-parser (compile) ( parser -- quot )
|
2008-03-25 23:21:33 -04:00
|
|
|
{ [ p1>> ] [ quot>> ] } cleave [ compiled-parser ] dip
|
2007-12-20 19:16:14 -05:00
|
|
|
2array { ?quot ?action } action-pattern match-replace ;
|
2007-11-20 22:21:23 -05:00
|
|
|
|
2007-11-26 21:36:26 -05:00
|
|
|
: left-trim-slice ( string -- string )
|
|
|
|
#! Return a new string without any leading whitespace
|
|
|
|
#! from the original string.
|
|
|
|
dup empty? [
|
|
|
|
dup first blank? [ 1 tail-slice left-trim-slice ] when
|
|
|
|
] unless ;
|
|
|
|
|
|
|
|
TUPLE: sp-parser p1 ;
|
|
|
|
|
2008-03-20 10:05:21 -04:00
|
|
|
M: sp-parser (compile) ( parser -- quot )
|
2007-12-20 19:16:14 -05:00
|
|
|
[
|
2008-03-25 23:08:14 -04:00
|
|
|
\ left-trim-slice , p1>> compiled-parser ,
|
2007-12-20 19:16:14 -05:00
|
|
|
] [ ] make ;
|
2007-11-26 21:36:26 -05:00
|
|
|
|
2007-11-26 22:16:21 -05:00
|
|
|
TUPLE: delay-parser quot ;
|
|
|
|
|
2008-03-20 10:05:21 -04:00
|
|
|
M: delay-parser (compile) ( parser -- quot )
|
2008-03-21 08:59:16 -04:00
|
|
|
#! For efficiency we memoize the quotation.
|
|
|
|
#! This way it is run only once and the
|
|
|
|
#! parser constructed once at run time.
|
2007-12-20 19:16:14 -05:00
|
|
|
[
|
2008-03-25 23:08:14 -04:00
|
|
|
quot>> % \ compile ,
|
2008-03-21 08:59:16 -04:00
|
|
|
] [ ] make
|
|
|
|
{ } { "word" } <effect> memoize-quot
|
|
|
|
[ % \ execute , ] [ ] make ;
|
2007-11-26 22:16:21 -05:00
|
|
|
|
2007-11-26 18:22:33 -05:00
|
|
|
PRIVATE>
|
|
|
|
|
2008-03-27 19:00:36 -04:00
|
|
|
: token ( string -- parser )
|
|
|
|
token-parser construct-boa init-parser ;
|
2007-11-26 18:22:33 -05:00
|
|
|
|
2008-03-27 19:00:36 -04:00
|
|
|
: satisfy ( quot -- parser )
|
|
|
|
satisfy-parser construct-boa init-parser ;
|
2007-11-26 21:08:16 -05:00
|
|
|
|
2008-03-27 19:00:36 -04:00
|
|
|
: range ( min max -- parser )
|
|
|
|
range-parser construct-boa init-parser ;
|
2007-11-26 18:22:33 -05:00
|
|
|
|
2008-03-27 19:00:36 -04:00
|
|
|
: seq ( seq -- parser )
|
|
|
|
seq-parser construct-boa init-parser ;
|
2007-11-26 18:22:33 -05:00
|
|
|
|
2008-03-27 19:00:36 -04:00
|
|
|
: 2seq ( parser1 parser2 -- parser )
|
2008-02-26 16:17:17 -05:00
|
|
|
2array seq ;
|
|
|
|
|
2008-03-27 19:00:36 -04:00
|
|
|
: 3seq ( parser1 parser2 parser3 -- parser )
|
2008-02-26 16:17:17 -05:00
|
|
|
3array seq ;
|
|
|
|
|
2008-03-27 19:00:36 -04:00
|
|
|
: 4seq ( parser1 parser2 parser3 parser4 -- parser )
|
2008-03-03 14:28:53 -05:00
|
|
|
4array seq ;
|
|
|
|
|
2008-03-27 18:30:46 -04:00
|
|
|
: seq* ( quot -- paser )
|
2008-02-13 16:39:37 -05:00
|
|
|
{ } make seq ; inline
|
|
|
|
|
2008-03-27 19:00:36 -04:00
|
|
|
: choice ( seq -- parser )
|
|
|
|
choice-parser construct-boa init-parser ;
|
2007-11-26 18:22:33 -05:00
|
|
|
|
2008-03-27 19:00:36 -04:00
|
|
|
: 2choice ( parser1 parser2 -- parser )
|
2008-02-26 18:13:15 -05:00
|
|
|
2array choice ;
|
|
|
|
|
2008-03-27 19:00:36 -04:00
|
|
|
: 3choice ( parser1 parser2 parser3 -- parser )
|
2008-02-26 18:13:15 -05:00
|
|
|
3array choice ;
|
|
|
|
|
2008-03-27 19:00:36 -04:00
|
|
|
: 4choice ( parser1 parser2 parser3 parser4 -- parser )
|
2008-03-03 14:28:53 -05:00
|
|
|
4array choice ;
|
|
|
|
|
2008-03-27 18:30:46 -04:00
|
|
|
: choice* ( quot -- paser )
|
2008-02-13 16:39:37 -05:00
|
|
|
{ } make choice ; inline
|
|
|
|
|
2008-03-27 19:00:36 -04:00
|
|
|
: repeat0 ( parser -- parser )
|
|
|
|
repeat0-parser construct-boa init-parser ;
|
2007-11-26 18:22:33 -05:00
|
|
|
|
2008-03-27 19:00:36 -04:00
|
|
|
: repeat1 ( parser -- parser )
|
|
|
|
repeat1-parser construct-boa init-parser ;
|
2007-11-26 18:22:33 -05:00
|
|
|
|
2008-03-27 19:00:36 -04:00
|
|
|
: optional ( parser -- parser )
|
|
|
|
optional-parser construct-boa init-parser ;
|
2007-11-26 18:22:33 -05:00
|
|
|
|
2008-03-27 19:00:36 -04:00
|
|
|
: ensure ( parser -- parser )
|
|
|
|
ensure-parser construct-boa init-parser ;
|
2007-11-26 18:22:33 -05:00
|
|
|
|
2008-03-27 19:00:36 -04:00
|
|
|
: ensure-not ( parser -- parser )
|
|
|
|
ensure-not-parser construct-boa init-parser ;
|
2007-11-26 18:22:33 -05:00
|
|
|
|
2008-03-27 19:00:36 -04:00
|
|
|
: action ( parser quot -- parser )
|
|
|
|
action-parser construct-boa init-parser ;
|
2007-11-26 21:36:26 -05:00
|
|
|
|
2008-03-27 19:00:36 -04:00
|
|
|
: sp ( parser -- parser )
|
|
|
|
sp-parser construct-boa init-parser ;
|
2007-11-26 21:45:00 -05:00
|
|
|
|
2008-03-20 08:25:45 -04:00
|
|
|
: hide ( parser -- parser )
|
2007-11-26 21:45:00 -05:00
|
|
|
[ drop ignore ] action ;
|
2007-11-26 22:16:21 -05:00
|
|
|
|
2008-03-27 19:00:36 -04:00
|
|
|
: delay ( quot -- parser )
|
|
|
|
delay-parser construct-boa init-parser ;
|
2008-03-03 17:57:30 -05:00
|
|
|
|
|
|
|
: PEG:
|
|
|
|
(:) [
|
|
|
|
[
|
2008-03-21 07:58:53 -04:00
|
|
|
call compile 1quotation
|
2008-03-03 17:57:30 -05:00
|
|
|
[ dup [ parse-result-ast ] [ "Parse failed" throw ] if ]
|
|
|
|
append define
|
|
|
|
] with-compilation-unit
|
|
|
|
] 2curry over push-all ; parsing
|