From d68a78c4a62c12552ca99aff83d5182ea1e0f23f Mon Sep 17 00:00:00 2001 From: Chris Double Date: Wed, 28 Nov 2007 15:32:04 +1300 Subject: [PATCH] fix grouping of repeat0 in ebnf --- extra/peg/ebnf/ebnf-tests.factor | 20 ++++++++++++++++++++ extra/peg/ebnf/ebnf.factor | 18 ++++++++---------- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/extra/peg/ebnf/ebnf-tests.factor b/extra/peg/ebnf/ebnf-tests.factor index 13d0ce887e..a19bc0188c 100644 --- a/extra/peg/ebnf/ebnf-tests.factor +++ b/extra/peg/ebnf/ebnf-tests.factor @@ -64,4 +64,24 @@ IN: temporary } } [ "one (two | three)" 'choice' parse parse-result-ast +] unit-test + +{ + T{ ebnf-sequence f + V{ + T{ ebnf-non-terminal f "one" } + T{ ebnf-repeat0 f + T{ ebnf-sequence f + V{ + T{ ebnf-choice f + V{ T{ ebnf-non-terminal f "two" } T{ ebnf-non-terminal f "three" } } + } + T{ ebnf-non-terminal f "four" } + } + } + } + } + } +} [ + "one {(two | three) four}" '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 4a1d7b341b..352390c5cc 100644 --- a/extra/peg/ebnf/ebnf.factor +++ b/extra/peg/ebnf/ebnf.factor @@ -103,8 +103,14 @@ DEFER: 'choice' ")" token sp hide 3array seq [ first ] action ; +: 'repeat0' ( -- parser ) + "{" token sp hide + [ 'choice' sp ] delay + "}" token sp hide + 3array seq [ first ] action ; + : 'sequence' ( -- parser ) - 'element' sp 'group' sp 2array choice + 'element' sp 'group' sp 'repeat0' sp 3array choice repeat1 [ dup length 1 = [ first ] [ ] if ] action ; @@ -114,21 +120,13 @@ DEFER: 'choice' dup length 1 = [ first ] [ ] if ] action ; -: 'repeat0' ( -- parser ) - "{" token sp hide - [ 'rhs' sp ] delay - "}" token sp hide - 3array seq [ first ] action ; - : 'action' ( -- parser ) "=>" token hide [ blank? ] satisfy ensure-not [ drop t ] satisfy 2array seq [ first ] action repeat1 [ >string ] action sp 2array seq [ first ] action ; : 'rhs' ( -- parser ) - 'repeat0' - 'choice' - 2array choice 'action' sp optional 2array seq ; + 'choice' 'action' sp optional 2array seq ; : 'rule' ( -- parser ) 'non-terminal' [ ebnf-non-terminal-symbol ] action