diff --git a/extra/peg/peg.factor b/extra/peg/peg.factor index 8940fc87c6..247d52a19c 100644 --- a/extra/peg/peg.factor +++ b/extra/peg/peg.factor @@ -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 = ; : ( 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 ; diff --git a/extra/peg/pl0/pl0.factor b/extra/peg/pl0/pl0.factor index b37009238d..b6b030f56c 100644 --- a/extra/peg/pl0/pl0.factor +++ b/extra/peg/pl0/pl0.factor @@ -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 ;