Add syntax word for ebnf

db4
Chris Double 2008-03-19 17:37:08 +13:00
parent 7578538122
commit 708d55fb8e
1 changed files with 13 additions and 8 deletions

View File

@ -99,6 +99,11 @@ M: ebnf (generate-parser) ( ast -- id )
DEFER: 'rhs' DEFER: 'rhs'
: syntax ( string -- parser )
#! Parses the string, ignoring white space, and
#! does not put the result in the AST.
token sp hide ;
: '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
@ -144,23 +149,23 @@ DEFER: 'choice'
: 'group' ( -- parser ) : 'group' ( -- parser )
[ [
"(" token sp hide , "(" syntax ,
[ 'choice' sp ] delay , [ 'choice' sp ] delay ,
")" token sp hide , ")" syntax ,
] seq* [ first ] action ; ] seq* [ first ] action ;
: 'repeat0' ( -- parser ) : 'repeat0' ( -- parser )
[ [
"{" token sp hide , "{" syntax ,
[ 'choice' sp ] delay , [ 'choice' sp ] delay ,
"}" token sp hide , "}" syntax ,
] seq* [ first <ebnf-repeat0> ] action ; ] seq* [ first <ebnf-repeat0> ] action ;
: 'optional' ( -- parser ) : 'optional' ( -- parser )
[ [
"[" token sp hide , "[" syntax ,
[ 'choice' sp ] delay , [ 'choice' sp ] delay ,
"]" token sp hide , "]" syntax ,
] seq* [ first <ebnf-optional> ] action ; ] seq* [ first <ebnf-optional> ] action ;
: 'sequence' ( -- parser ) : 'sequence' ( -- parser )
@ -196,12 +201,12 @@ DEFER: 'choice'
: 'rule' ( -- parser ) : 'rule' ( -- parser )
[ [
'non-terminal' [ ebnf-non-terminal-symbol ] action , 'non-terminal' [ ebnf-non-terminal-symbol ] action ,
"=" token sp hide , "=" syntax ,
'rhs' , 'rhs' ,
] seq* [ first2 <ebnf-rule> ] action ; ] seq* [ first2 <ebnf-rule> ] action ;
: 'ebnf' ( -- parser ) : 'ebnf' ( -- parser )
'rule' sp "." token sp hide list-of [ <ebnf> ] action ; 'rule' sp "." syntax list-of [ <ebnf> ] action ;
: ebnf>quot ( string -- quot ) : ebnf>quot ( string -- quot )
'ebnf' parse [ 'ebnf' parse [