Use choice* and seq* in ebnf
parent
4b37c9098e
commit
cc9a17b551
|
@ -100,33 +100,46 @@ M: ebnf (generate-parser) ( ast -- id )
|
||||||
DEFER: 'rhs'
|
DEFER: 'rhs'
|
||||||
|
|
||||||
: 'non-terminal' ( -- parser )
|
: 'non-terminal' ( -- parser )
|
||||||
CHAR: a CHAR: z range "-" token [ first ] action 2array choice repeat1 [ >string <ebnf-non-terminal> ] action ;
|
[
|
||||||
|
CHAR: a CHAR: z range ,
|
||||||
|
"-" token [ first ] action ,
|
||||||
|
] choice* repeat1 [ >string <ebnf-non-terminal> ] action ;
|
||||||
|
|
||||||
: 'terminal' ( -- parser )
|
: 'terminal' ( -- parser )
|
||||||
"'" token hide [ CHAR: ' = not ] satisfy repeat1 "'" token hide 3array seq [ first >string <ebnf-terminal> ] action ;
|
[
|
||||||
|
"'" token hide ,
|
||||||
|
[ CHAR: ' = not ] satisfy repeat1 ,
|
||||||
|
"'" token hide ,
|
||||||
|
] seq* [ first >string <ebnf-terminal> ] action ;
|
||||||
|
|
||||||
: 'element' ( -- parser )
|
: 'element' ( -- parser )
|
||||||
'non-terminal' 'terminal' 2array choice ;
|
[
|
||||||
|
'non-terminal' ,
|
||||||
|
'terminal' ,
|
||||||
|
] choice* ;
|
||||||
|
|
||||||
DEFER: 'choice'
|
DEFER: 'choice'
|
||||||
|
|
||||||
: 'group' ( -- parser )
|
: 'group' ( -- parser )
|
||||||
"(" token sp hide
|
[
|
||||||
[ 'choice' sp ] delay
|
"(" token sp hide ,
|
||||||
")" token sp hide
|
[ 'choice' sp ] delay ,
|
||||||
3array seq [ first ] action ;
|
")" token sp hide ,
|
||||||
|
] seq* [ first ] action ;
|
||||||
|
|
||||||
: 'repeat0' ( -- parser )
|
: 'repeat0' ( -- parser )
|
||||||
"{" token sp hide
|
[
|
||||||
[ 'choice' sp ] delay
|
"{" token sp hide ,
|
||||||
"}" token sp hide
|
[ 'choice' sp ] delay ,
|
||||||
3array seq [ first <ebnf-repeat0> ] action ;
|
"}" token sp hide ,
|
||||||
|
] seq* [ first <ebnf-repeat0> ] action ;
|
||||||
|
|
||||||
: 'optional' ( -- parser )
|
: 'optional' ( -- parser )
|
||||||
"[" token sp hide
|
[
|
||||||
[ 'choice' sp ] delay
|
"[" token sp hide ,
|
||||||
"]" token sp hide
|
[ 'choice' sp ] delay ,
|
||||||
3array seq [ first <ebnf-optional> ] action ;
|
"]" token sp hide ,
|
||||||
|
] seq* [ first <ebnf-optional> ] action ;
|
||||||
|
|
||||||
: 'sequence' ( -- parser )
|
: 'sequence' ( -- parser )
|
||||||
[
|
[
|
||||||
|
@ -134,8 +147,7 @@ DEFER: 'choice'
|
||||||
'group' sp ,
|
'group' sp ,
|
||||||
'repeat0' sp ,
|
'repeat0' sp ,
|
||||||
'optional' sp ,
|
'optional' sp ,
|
||||||
] { } make choice
|
] choice* repeat1 [
|
||||||
repeat1 [
|
|
||||||
dup length 1 = [ first ] [ <ebnf-sequence> ] if
|
dup length 1 = [ first ] [ <ebnf-sequence> ] if
|
||||||
] action ;
|
] action ;
|
||||||
|
|
||||||
|
@ -145,18 +157,26 @@ DEFER: 'choice'
|
||||||
] action ;
|
] action ;
|
||||||
|
|
||||||
: 'action' ( -- parser )
|
: 'action' ( -- parser )
|
||||||
"=>" token hide
|
[
|
||||||
[ blank? ] satisfy ensure-not [ drop t ] satisfy 2array seq [ first ] action repeat1 [ >string ] action sp
|
"=>" token hide ,
|
||||||
2array seq [ first <ebnf-action> ] action ;
|
[
|
||||||
|
[ blank? ] satisfy ensure-not ,
|
||||||
|
[ drop t ] satisfy ,
|
||||||
|
] seq* [ first ] action repeat1 [ >string ] action sp ,
|
||||||
|
] seq* [ first <ebnf-action> ] action ;
|
||||||
|
|
||||||
: 'rhs' ( -- parser )
|
: 'rhs' ( -- parser )
|
||||||
'choice' 'action' sp optional 2array seq ;
|
[
|
||||||
|
'choice' ,
|
||||||
|
'action' sp optional ,
|
||||||
|
] seq* ;
|
||||||
|
|
||||||
: 'rule' ( -- parser )
|
: 'rule' ( -- parser )
|
||||||
'non-terminal' [ ebnf-non-terminal-symbol ] action
|
[
|
||||||
"=" token sp hide
|
'non-terminal' [ ebnf-non-terminal-symbol ] action ,
|
||||||
'rhs'
|
"=" token sp hide ,
|
||||||
3array seq [ first2 <ebnf-rule> ] action ;
|
'rhs' ,
|
||||||
|
] seq* [ first2 <ebnf-rule> ] action ;
|
||||||
|
|
||||||
: 'ebnf' ( -- parser )
|
: 'ebnf' ( -- parser )
|
||||||
'rule' sp "." token sp hide list-of [ <ebnf> ] action ;
|
'rule' sp "." token sp hide list-of [ <ebnf> ] action ;
|
||||||
|
|
Loading…
Reference in New Issue