Add syntax-pack and grouped to ebnf refactoring

db4
Chris Double 2008-03-19 17:52:22 +13:00
parent 708d55fb8e
commit 9403d97e22
1 changed files with 17 additions and 18 deletions

View File

@ -104,6 +104,11 @@ DEFER: 'rhs'
#! does not put the result in the AST. #! does not put the result in the AST.
token sp hide ; token sp hide ;
: syntax-pack ( begin parser end -- parser )
#! Parse 'parser' surrounded by syntax elements
#! begin and end.
[ syntax ] dipd syntax pack ;
: 'identifier' ( -- parser ) : 'identifier' ( -- parser )
#! Return a parser that parses an identifer delimited by #! Return a parser that parses an identifer delimited by
#! a quotation character. The quotation can be single #! a quotation character. The quotation can be single
@ -147,26 +152,20 @@ DEFER: 'rhs'
DEFER: 'choice' DEFER: 'choice'
: grouped ( begin quot end -- parser )
#! Parse a group of choices, where the delimiter for the
#! group is specified by 'begin' and 'end'. The quotation
#! should produce the AST to be the result of the parser.
[ [ 'choice' sp ] delay swap action ] dip syntax-pack ;
: 'group' ( -- parser ) : 'group' ( -- parser )
[ "(" [ ] ")" grouped ;
"(" syntax ,
[ 'choice' sp ] delay ,
")" syntax ,
] seq* [ first ] action ;
: 'repeat0' ( -- parser ) : 'repeat0' ( -- parser )
[ "{" [ <ebnf-repeat0> ] "}" grouped ;
"{" syntax ,
[ 'choice' sp ] delay ,
"}" syntax ,
] seq* [ first <ebnf-repeat0> ] action ;
: 'optional' ( -- parser ) : 'optional' ( -- parser )
[ "[" [ <ebnf-optional> ] "]" grouped ;
"[" syntax ,
[ 'choice' sp ] delay ,
"]" syntax ,
] seq* [ first <ebnf-optional> ] action ;
: 'sequence' ( -- parser ) : 'sequence' ( -- parser )
[ [