Add tests for left recusion in pegs
parent
4e29081e93
commit
f6b7f8197e
|
@ -143,3 +143,31 @@ IN: peg.ebnf.tests
|
|||
{ f } [
|
||||
"Z" [EBNF foo=[^A-Z] EBNF] call
|
||||
] unit-test
|
||||
|
||||
[
|
||||
#! Test direct left recursion. Currently left recursion should cause a
|
||||
#! failure of that parser.
|
||||
#! Not using packrat, so recursion causes data stack overflow
|
||||
"1+1" [EBNF num=([0-9])+ expr=expr "+" num | num EBNF] call
|
||||
] must-fail
|
||||
|
||||
{ V{ 49 } } [
|
||||
#! Test direct left recursion. Currently left recursion should cause a
|
||||
#! failure of that parser.
|
||||
#! Using packrat, so first part of expr fails, causing 2nd choice to be used
|
||||
"1+1" [ [EBNF num=([0-9])+ expr=expr "+" num | num EBNF] call ] with-packrat parse-result-ast
|
||||
] unit-test
|
||||
|
||||
[
|
||||
#! Test indirect left recursion. Currently left recursion should cause a
|
||||
#! failure of that parser.
|
||||
#! Not using packrat, so recursion causes data stack overflow
|
||||
"1+1" [EBNF num=([0-9])+ x=expr expr=x "+" num | num EBNF] call
|
||||
] must-fail
|
||||
|
||||
{ V{ 49 } } [
|
||||
#! Test indirect left recursion. Currently left recursion should cause a
|
||||
#! failure of that parser.
|
||||
#! 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 ] with-packrat parse-result-ast
|
||||
] unit-test
|
||||
|
|
|
@ -266,7 +266,7 @@ M: ebnf-non-terminal (transform) ( ast -- parser )
|
|||
] [ ] make delay sp ;
|
||||
|
||||
: transform-ebnf ( string -- object )
|
||||
'ebnf' parse parse-result-ast transform ;
|
||||
'ebnf' [ parse ] packrat-parse parse-result-ast transform ;
|
||||
|
||||
: check-parse-result ( result -- result )
|
||||
dup [
|
||||
|
@ -281,7 +281,7 @@ M: ebnf-non-terminal (transform) ( ast -- parser )
|
|||
] if ;
|
||||
|
||||
: ebnf>quot ( string -- hashtable quot )
|
||||
'ebnf' parse check-parse-result
|
||||
'ebnf' [ parse ] with-packrat check-parse-result
|
||||
parse-result-ast transform dup main swap at compile 1quotation ;
|
||||
|
||||
: [EBNF "EBNF]" parse-multiline-string ebnf>quot nip parsed ; parsing
|
||||
|
|
|
@ -180,3 +180,19 @@ IN: peg.tests
|
|||
"1+1" swap parse parse-result-ast
|
||||
] with-packrat
|
||||
] unit-test
|
||||
|
||||
: expr ( -- parser )
|
||||
#! Test direct left recursion. Currently left recursion should cause a
|
||||
#! failure of that parser.
|
||||
[ expr ] delay "+" token "1" token 3seq "1" token 2choice ;
|
||||
|
||||
[
|
||||
#! Not using packrat, so recursion causes data stack overflow
|
||||
"1+1" expr parse parse-result-ast
|
||||
] must-fail
|
||||
|
||||
{ "1" } [
|
||||
#! Using packrat, so expr fails, causing the 2nd choice to be used.
|
||||
"1+1" expr [ parse ] with-packrat parse-result-ast
|
||||
] unit-test
|
||||
|
||||
|
|
|
@ -39,7 +39,11 @@ GENERIC: (compile) ( parser -- quot )
|
|||
#! from the input cache. If the item is not in the cache,
|
||||
#! call 'quot' with 'input' on the stack to get the result
|
||||
#! and store that in the cache and return it.
|
||||
n input-cache [ drop input quot call ] cache ; inline
|
||||
n input-cache [
|
||||
drop
|
||||
f n input-cache set-at
|
||||
input quot call
|
||||
] cache ; inline
|
||||
|
||||
:: run-packrat-parser ( input quot c -- result )
|
||||
input input-from
|
||||
|
|
Loading…
Reference in New Issue