diff --git a/extra/peg/ebnf/ebnf-tests.factor b/extra/peg/ebnf/ebnf-tests.factor index 37b4867d34..56f0d63445 100644 --- a/extra/peg/ebnf/ebnf-tests.factor +++ b/extra/peg/ebnf/ebnf-tests.factor @@ -38,4 +38,17 @@ IN: temporary } } [ "digit = '1' '2'" 'rule' parse parse-result-ast +] unit-test + +{ + T{ ebnf-choice f + V{ + T{ ebnf-sequence f + V{ T{ ebnf-non-terminal f "one" } T{ ebnf-non-terminal f "two" } } + } + T{ ebnf-non-terminal f "three" } + } + } +} [ + "one two | three" 'choice' parse parse-result-ast ] unit-test \ No newline at end of file diff --git a/extra/peg/ebnf/ebnf.factor b/extra/peg/ebnf/ebnf.factor index cfbd4f9e23..9723316b33 100644 --- a/extra/peg/ebnf/ebnf.factor +++ b/extra/peg/ebnf/ebnf.factor @@ -97,12 +97,15 @@ DEFER: 'rhs' : 'sequence' ( -- parser ) 'element' sp - "|" token sp ensure-not 2array seq [ first ] action - repeat1 [ ] action ; - -: 'choice' ( -- parser ) - 'element' sp "|" token sp list-of [ ] action ; + repeat1 [ + dup length 1 = [ first ] [ ] if + ] action ; +: 'choice' ( -- parser ) + 'sequence' sp "|" token sp list-of [ + dup length 1 = [ first ] [ ] if + ] action ; + : 'repeat0' ( -- parser ) "{" token sp hide [ 'rhs' sp ] delay @@ -116,10 +119,8 @@ DEFER: 'rhs' : 'rhs' ( -- parser ) 'repeat0' - 'sequence' 'choice' - 'element' - 4array choice 'action' sp optional 2array seq ; + 2array choice 'action' sp optional 2array seq ; : 'rule' ( -- parser ) 'non-terminal' [ ebnf-non-terminal-symbol ] action