0
|
1 |
"{ Package: 'squeak:petitparser' }"
|
|
2 |
|
|
3 |
PPDelegateParser subclass:#PPMemoizedParser
|
|
4 |
instanceVariableNames:'stream buffer'
|
|
5 |
classVariableNames:''
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'PetitParser-Parsers'
|
|
8 |
!
|
|
9 |
|
|
10 |
PPMemoizedParser comment:'A memoized parser, for refraining redundant computations.
|
|
11 |
Instance Variables:
|
|
12 |
stream <PositionableStream> The stream of the associated memento objects.
|
|
13 |
buffer <Array of: PPMemento> The buffer of memento objects.
|
|
14 |
'
|
|
15 |
!
|
|
16 |
|
|
17 |
|
|
18 |
!PPMemoizedParser methodsFor:'operations'!
|
|
19 |
|
|
20 |
memoized
|
|
21 |
"Ther is no point in memoizing more than once."
|
|
22 |
|
|
23 |
^ self
|
|
24 |
! !
|
|
25 |
|
|
26 |
!PPMemoizedParser methodsFor:'parsing'!
|
|
27 |
|
|
28 |
parseOn: aStream
|
|
29 |
| memento |
|
|
30 |
stream == aStream
|
|
31 |
ifFalse: [ self reset: aStream ].
|
|
32 |
memento := (buffer at: stream position + 1)
|
|
33 |
ifNil: [ buffer at: stream position + 1 put: PPMemento new ].
|
|
34 |
memento position isNil
|
|
35 |
ifTrue: [
|
|
36 |
memento result: (stream size - stream position + 2 < memento count
|
|
37 |
ifTrue: [ PPFailure message: 'overflow' at: stream position ]
|
|
38 |
ifFalse: [ memento increment. parser parseOn: stream ]).
|
|
39 |
memento position: stream position ]
|
|
40 |
ifFalse: [ stream position: memento position ].
|
|
41 |
^ memento result
|
|
42 |
! !
|
|
43 |
|
|
44 |
!PPMemoizedParser methodsFor:'private'!
|
|
45 |
|
|
46 |
reset: aStream
|
|
47 |
stream := aStream.
|
|
48 |
buffer := Array new: aStream size + 1
|
|
49 |
! !
|
|
50 |
|
|
51 |
!PPMemoizedParser class methodsFor:'documentation'!
|
|
52 |
|
|
53 |
version_SVN
|
|
54 |
^ '$Id: PPMemoizedParser.st,v 1.1 2011-08-18 18:56:17 cg Exp $'
|
|
55 |
! !
|