islands/PPMemoizingIsland.st
changeset 387 e2b2ccaa4de6
child 420 b2f2f15cef26
equal deleted inserted replaced
386:a409905f7f2d 387:e2b2ccaa4de6
       
     1 "{ Package: 'stx:goodies/petitparser/islands' }"
       
     2 
       
     3 PPIsland subclass:#PPMemoizingIsland
       
     4 	instanceVariableNames:'rootParser memoizationDictionaries'
       
     5 	classVariableNames:''
       
     6 	poolDictionaries:''
       
     7 	category:'PetitIslands-Parsers'
       
     8 !
       
     9 
       
    10 PPMemoizingIsland comment:'A PPMemoizingIsland is memoized version of PPIsland. Use this one, unless you don''t mind really bad performance. If the memoized version is not working flawlessly, its a bug!!
       
    11 
       
    12 Please see help of the PPIsland for how to use...
       
    13 
       
    14 Instance Variables
       
    15 	memoizationDictionaries:		<Object>
       
    16 	rootParser:		<Object>
       
    17 
       
    18 memoizationDictionaries
       
    19 	- memoization cache
       
    20 
       
    21 rootParser
       
    22 	- used for memoizing, once the root changes, flushes the caches
       
    23 '
       
    24 !
       
    25 
       
    26 !PPMemoizingIsland class methodsFor:'as yet unclassified'!
       
    27 
       
    28 initialize 
       
    29 	super initialize 
       
    30 ! !
       
    31 
       
    32 !PPMemoizingIsland methodsFor:'accessing'!
       
    33 
       
    34 island: anObject
       
    35 
       
    36 	island ifNil: [
       
    37 		super island: anObject.
       
    38 	] ifNotNil: [
       
    39 		self error: 'JK: I do not want to do this' .
       
    40 	] 
       
    41 ! !
       
    42 
       
    43 !PPMemoizingIsland methodsFor:'initialization'!
       
    44 
       
    45 initialize 
       
    46 	super initialize.
       
    47 
       
    48 	memoizationDictionaries := IdentityDictionary new.
       
    49 ! !
       
    50 
       
    51 !PPMemoizingIsland methodsFor:'memoization'!
       
    52 
       
    53 memoizationDictionaryForContext: aPPContext
       
    54 	^ memoizationDictionaries at: aPPContext ifAbsentPut: [IdentityDictionary new].
       
    55 !
       
    56 
       
    57 memoizeResult: result onContext: aPPContext position: pos
       
    58 	| memento |
       
    59 	
       
    60 	memento := PPMemento new.
       
    61 	memento contextMemento: aPPContext remember.
       
    62 	memento result: result.
       
    63 	
       
    64 	(self memoizationDictionaryForContext: aPPContext) at: pos put: memento.
       
    65 !
       
    66 
       
    67 memoizedResult: aPPContext
       
    68 	^ (self memoizationDictionaryForContext: aPPContext) at: (aPPContext position) ifAbsent: [ nil ].
       
    69 	
       
    70 ! !
       
    71 
       
    72 !PPMemoizingIsland methodsFor:'parsing'!
       
    73 
       
    74 memoized
       
    75 	"We have our own implementation of memoization"
       
    76 	^ self
       
    77 !
       
    78 
       
    79 nonMemoized 
       
    80 	^ PPIsland new
       
    81 		island: self island;
       
    82 		yourself
       
    83 !
       
    84 
       
    85 parseOn: aPPContext 
       
    86 	|  memoizedResult parsingPosition retval |
       
    87 	memoizedResult := self memoizedResult: aPPContext.
       
    88 	memoizedResult ifNotNil: [ 
       
    89 		aPPContext restore: memoizedResult contextMemento.
       
    90 		^ memoizedResult result 
       
    91 	].
       
    92 	parsingPosition := aPPContext position.
       
    93 	
       
    94 	retval := super parseOn: aPPContext.
       
    95 
       
    96 	(aPPContext waterPosition == aPPContext position) ifFalse: [ 
       
    97 		self memoizeResult: retval onContext: aPPContext position: parsingPosition.
       
    98 	].
       
    99 
       
   100 	^ retval.
       
   101 
       
   102 ! !
       
   103 
       
   104 
       
   105 PPMemoizingIsland initialize!