Testcase for packrat behaviour
parent
bc5f82255f
commit
4c50daed22
|
@ -158,3 +158,23 @@ IN: peg.tests
|
|||
"a]" "[" token hide "a" token "]" token hide 3array seq parse
|
||||
] unit-test
|
||||
|
||||
|
||||
{ V{ "1" "-" "1" } V{ "1" "+" "1" } } [
|
||||
[
|
||||
[ "1" token , "-" token , "1" token , ] seq* ,
|
||||
[ "1" token , "+" token , "1" token , ] seq* ,
|
||||
] choice*
|
||||
"1-1" over parse parse-result-ast swap
|
||||
"1+1" swap parse parse-result-ast
|
||||
] unit-test
|
||||
|
||||
{ V{ "1" "-" "1" } V{ "1" "+" "1" } } [
|
||||
[
|
||||
[
|
||||
[ "1" token , "-" token , "1" token , ] seq* ,
|
||||
[ "1" token , "+" token , "1" token , ] seq* ,
|
||||
] choice*
|
||||
"1-1" over parse parse-result-ast swap
|
||||
"1+1" swap parse parse-result-ast
|
||||
] with-packrat
|
||||
] unit-test
|
|
@ -29,12 +29,22 @@ GENERIC: (compile) ( parser -- quot )
|
|||
#! input slice is based on.
|
||||
dup slice? [ slice-from ] [ drop 0 ] if ;
|
||||
|
||||
: input-cache ( quot cache -- cache )
|
||||
#! From the packrat cache, obtain the cache for the parser quotation
|
||||
#! that maps the input string position to the parser result.
|
||||
[ drop H{ } clone ] cache ;
|
||||
|
||||
: cached-result ( n input-cache input quot -- result )
|
||||
#! Get the cached result for input position n
|
||||
#! 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.
|
||||
[ nip ] swap compose curry cache ; inline
|
||||
|
||||
:: run-packrat-parser ( input quot c -- result )
|
||||
input input-from
|
||||
quot c [ drop H{ } clone ] cache
|
||||
[
|
||||
drop input quot call
|
||||
] cache ; inline
|
||||
quot c input-cache
|
||||
input quot cached-result ; inline
|
||||
|
||||
: run-parser ( input quot -- result )
|
||||
#! If a packrat cache is available, use memoization for
|
||||
|
|
Loading…
Reference in New Issue