Fix precedence between choice/sequence in ebnf

release
Chris Double 2007-11-28 15:14:11 +13:00
parent 28e9c0e6e0
commit 15b1533f20
2 changed files with 22 additions and 8 deletions

View File

@ -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

View File

@ -97,12 +97,15 @@ DEFER: 'rhs'
: 'sequence' ( -- parser )
'element' sp
"|" token sp ensure-not 2array seq [ first ] action
repeat1 [ <ebnf-sequence> ] action ;
: 'choice' ( -- parser )
'element' sp "|" token sp list-of [ <ebnf-choice> ] action ;
repeat1 [
dup length 1 = [ first ] [ <ebnf-sequence> ] if
] action ;
: 'choice' ( -- parser )
'sequence' sp "|" token sp list-of [
dup length 1 = [ first ] [ <ebnf-choice> ] 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