Fix packrat caching issue

release
Chris Double 2007-11-29 23:42:46 +13:00
parent b51e4f642e
commit 362f2d3436
2 changed files with 17 additions and 7 deletions

View File

@ -12,6 +12,10 @@ GENERIC: (parse) ( state parser -- result )
SYMBOL: packrat-cache
SYMBOL: ignore
SYMBOL: not-in-cache
: not-in-cache? ( result -- ? )
not-in-cache = ;
: <parse-result> ( remaining ast -- parse-result )
parse-result construct-boa ;
@ -30,7 +34,9 @@ TUPLE: parser id ;
dup slice? [ slice-from ] [ drop 0 ] if ;
: get-cached ( input parser -- result )
[ from ] dip parser-id packrat-cache get at at ;
[ from ] dip parser-id packrat-cache get at at* [
drop not-in-cache
] unless ;
: put-cached ( result input parser -- )
parser-id dup packrat-cache get at [
@ -44,9 +50,13 @@ PRIVATE>
: parse ( input parser -- result )
packrat-cache get [
2dup get-cached [
[ (parse) dup ] 2keep put-cached
] unless*
2dup get-cached dup not-in-cache? [
! "cache missed: " write over parser-id number>string write " - " write nl ! pick .
drop [ (parse) dup ] 2keep put-cached
] [
! "cache hit: " write over parser-id number>string write " - " write nl ! pick .
2nip
] if
] [
(parse)
] if ;

View File

@ -1,15 +1,15 @@
! Copyright (C) 2007 Chris Double.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel arrays strings math.parser sequences peg peg.ebnf ;
USING: kernel arrays strings math.parser sequences peg peg.ebnf memoize ;
IN: peg.pl0
#! Grammar for PL/0 based on http://en.wikipedia.org/wiki/PL/0
: ident ( -- parser )
MEMO: ident ( -- parser )
CHAR: a CHAR: z range
CHAR: A CHAR: Z range 2array choice repeat1
[ >string ] action ;
: number ( -- parser )
MEMO: number ( -- parser )
CHAR: 0 CHAR: 9 range repeat1 [ string>number ] action ;
<EBNF