Handle left recursion by failing again

db4
Chris Double 2008-03-29 00:49:39 +13:00
parent cca4700e49
commit 010ce80076
2 changed files with 11 additions and 8 deletions

View File

@ -144,16 +144,17 @@ IN: peg.ebnf.tests
"Z" [EBNF foo=[^A-Z] EBNF] call "Z" [EBNF foo=[^A-Z] EBNF] call
] unit-test ] unit-test
[ { V{ 49 } } [
#! Test direct left recursion. Currently left recursion should cause a #! Test direct left recursion. Currently left recursion should cause a
#! failure of that parser. #! failure of that parser.
"1+1" [EBNF num=([0-9])+ expr=expr "+" num | num EBNF] call #! Using packrat, so first part of expr fails, causing 2nd choice to be used
] must-fail "1+1" [EBNF num=([0-9])+ expr=expr "+" num | num EBNF] call parse-result-ast
] unit-test
[ { V{ 49 } } [
#! Test indirect left recursion. Currently left recursion should cause a #! Test indirect left recursion. Currently left recursion should cause a
#! failure of that parser. #! failure of that parser.
#! Using packrat, so first part of expr fails, causing 2nd choice to be used #! Using packrat, so first part of expr fails, causing 2nd choice to be used
"1+1" [EBNF num=([0-9])+ x=expr expr=x "+" num | num EBNF] call "1+1" [EBNF num=([0-9])+ x=expr expr=x "+" num | num EBNF] call parse-result-ast
] must-fail ] unit-test

View File

@ -67,10 +67,12 @@ C: <memo-entry> memo-entry
:: apply-non-memo-rule ( r p -- ast ) :: apply-non-memo-rule ( r p -- ast )
[let* | [let* |
m [ fail p <memo-entry> dup p r set-memo ]
ans [ r eval-rule ] ans [ r eval-rule ]
m [ ans pos get <memo-entry> ]
| |
m p r set-memo ans ans m (>>ans)
pos get m (>>pos)
ans
] ; ] ;
: apply-memo-rule ( m -- ast ) : apply-memo-rule ( m -- ast )