Add syntax-pack and grouped to ebnf refactoring
parent
708d55fb8e
commit
9403d97e22
|
@ -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 )
|
||||||
[
|
[
|
||||||
|
|
Loading…
Reference in New Issue