PPStream.st
changeset 0 739fe9b7253e
child 4 90de244a7fa2
equal deleted inserted replaced
-1:000000000000 0:739fe9b7253e
       
     1 "{ Package: 'squeak:petitparser' }"
       
     2 
       
     3 ReadStream subclass:#PPStream
       
     4 	instanceVariableNames:''
       
     5 	classVariableNames:''
       
     6 	poolDictionaries:''
       
     7 	category:'PetitParser-Core'
       
     8 !
       
     9 
       
    10 PPStream comment:'A positional stream implementation used for parsing. It overrides some methods for optimization reasons.'
       
    11 !
       
    12 
       
    13 
       
    14 !PPStream methodsFor:'accessing'!
       
    15 
       
    16 next: anInteger 
       
    17 	"Answer up to anInteger elements of my collection. Overridden for efficiency."
       
    18 
       
    19 	| answer endPosition |
       
    20 	endPosition := position + anInteger min: readLimit.
       
    21 	answer := collection copyFrom: position + 1 to: endPosition.
       
    22 	position := endPosition.
       
    23 	^ answer
       
    24 !
       
    25 
       
    26 peek
       
    27 	"An improved version of peek, that is slightly faster than the built in version."
       
    28 
       
    29 	^ self atEnd ifFalse: [ collection at: position + 1 ]
       
    30 !
       
    31 
       
    32 position: anInteger
       
    33 	"The receiver does not check for invalid arguments passed to this method, as it is solely used with valid indexes for backtracking."
       
    34 
       
    35 	position := anInteger
       
    36 !
       
    37 
       
    38 uncheckedPeek
       
    39 	"An unchecked version of peek that throws an error if we try to peek over the end of the stream, even faster than #peek."
       
    40 
       
    41 	^ collection at: position + 1
       
    42 ! !
       
    43 
       
    44 !PPStream methodsFor:'converting'!
       
    45 
       
    46 asPetitStream
       
    47 	^ self
       
    48 ! !
       
    49 
       
    50 !PPStream methodsFor:'printing'!
       
    51 
       
    52 printOn: aStream
       
    53 	collection isString
       
    54 		ifFalse: [ ^ super printOn: aStream ].
       
    55 	aStream
       
    56 		nextPutAll: (collection copyFrom: 1 to: position);
       
    57 		nextPutAll: '·';
       
    58 		nextPutAll: (collection copyFrom: position + 1 to: readLimit)
       
    59 ! !
       
    60 
       
    61 !PPStream class methodsFor:'documentation'!
       
    62 
       
    63 version_SVN
       
    64     ^ '$Id: PPStream.st,v 1.1 2011-08-18 18:56:17 cg Exp $'
       
    65 ! !