Add 'delay' parser to peg

release
Chris Double 2007-11-27 16:16:21 +13:00
parent ea2d4ea261
commit e49d84ce97
2 changed files with 18 additions and 0 deletions

View File

@ -138,3 +138,13 @@ HELP: hide
"Returns a parser that succeeds if the original parser succeeds, but does not " "Returns a parser that succeeds if the original parser succeeds, but does not "
"put any result in the AST. Useful for ignoring 'syntax' in the AST." } "put any result in the AST. Useful for ignoring 'syntax' in the AST." }
{ $example "\"[\" token hide number \"]\" token hide 3array seq" } ; { $example "\"[\" token hide number \"]\" token hide 3array seq" } ;
HELP: delay
{ $values
{ "quot" "a quotation with stack effect ( -- parser )" }
{ "parser" "a parser" }
}
{ $description
"Delays the construction of a parser until it is actually required to parse. This "
"allows for calling a parser that results in a recursive call to itself. The quotation "
"should return the constructed parser." } ;

View File

@ -168,6 +168,11 @@ TUPLE: sp-parser p1 ;
M: sp-parser parse ( state parser -- result ) M: sp-parser parse ( state parser -- result )
[ left-trim-slice ] dip sp-parser-p1 parse ; [ left-trim-slice ] dip sp-parser-p1 parse ;
TUPLE: delay-parser quot ;
M: delay-parser parse ( state parser -- result )
delay-parser-quot call parse ;
PRIVATE> PRIVATE>
: token ( string -- parser ) : token ( string -- parser )
@ -208,3 +213,6 @@ PRIVATE>
: hide ( parser -- parser ) : hide ( parser -- parser )
[ drop ignore ] action ; [ drop ignore ] action ;
: delay ( parser -- parser )
delay-parser construct-boa init-parser ;