PPStream.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Mon, 24 Nov 2014 11:38:11 +0000
changeset 423 f3b99c7c1b20
parent 421 7e08b31e0dae
child 427 a7f5e6de19d2
permissions -rw-r--r--
Added benchmark package to set of exported .mcz

"{ Package: 'stx:goodies/petitparser' }"

ReadStream subclass:#PPStream
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'PetitParser-Core'
!



!PPStream methodsFor:'accessing'!

collection
	"Answer the underlying collection."
	
	^ collection
!

next: anInteger 
	"Answer up to anInteger elements of my collection. Overridden for efficiency."

	| answer endPosition |
	endPosition := position + anInteger min: readLimit.
	answer := collection copyFrom: position + 1 to: endPosition.
	position := endPosition.
	^ answer
!

peek
	"An improved version of peek, that is slightly faster than the built in version."

	^ self atEnd ifFalse: [ collection at: position + 1 ]
!

position: anInteger
	"The receiver does not check for invalid arguments passed to this method, as it is solely used with valid indexes for backtracking."

	position := anInteger
!

size
	"
		The same implementation as a ReadStream. Implemented here for compatibility with Smalltalk/X
		that has different implementation in a ReadStream
	"
	^readLimit
!

uncheckedPeek
	"An unchecked version of peek that throws an error if we try to peek over the end of the stream, even faster than #peek."

	^ collection at: position + 1
! !

!PPStream methodsFor:'converting'!

asPetitStream
	^ self
! !

!PPStream methodsFor:'printing'!

printOn: aStream
	collection isString
		ifFalse: [ ^ super printOn: aStream ].
	aStream
		nextPutAll: (collection copyFrom: 1 to: position);
		nextPutAll: '·';
		nextPutAll: (collection copyFrom: position + 1 to: readLimit)
! !

!PPStream methodsFor:'queries'!

column
	^ self column: position.
!

column: pos
	| column clear tmp |
	
	pos > readLimit ifTrue: [ ^ Error signal: 'Oot of bounds' ].	

	tmp := position.
	column := 0.	
	clear := true.

	(0 to: pos) do: 
	[:index |
		position := index.
		self isStartOfLine ifTrue: [ clear := true ].

		clear ifTrue: [ column := 0. clear := false ].
		column := column + 1.
		(position > readLimit) ifTrue: [ position := tmp. ^ column ].
	].
	position := tmp.
	^ column
!

insideCRLF
	(position < 1) ifTrue: [ ^ false ].
	
	^ (self peek = (Character codePoint: 10)) and: [ self peekBack = (Character codePoint: 13) ]
!

isEndOfLine
	self atEnd ifTrue: [ ^ true ].
	self insideCRLF ifTrue: [ ^ false ].
	^ (self peek = (Character codePoint: 13) or: [ self peek = (Character codePoint: 10)]).
!

isStartOfLine
	(position = 0) ifTrue: [ ^ true ].

	self insideCRLF ifTrue: [ ^ false ].
	
	^ (self peekBack = (Character codePoint: 13)) or: [ self peekBack = (Character codePoint: 10)].
!

line
	^ self line: position
!

line: pos
	| tmp line |
	(pos = -1) ifTrue: [  ^ 0 ].
	(pos > readLimit) ifTrue: [ ^ self error: 'Out of limit' ].
	
	tmp := position.
	line := 0.
	
	(0 to: pos) do: 
	[:index |
		position := index.
		self isStartOfLine ifTrue: [ line := line + 1 ]
	].
	position := tmp.
	^ line
! !

!PPStream class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/goodies/petitparser/PPStream.st,v 1.4 2014-03-04 14:32:00 cg Exp $'
!

version_CVS
    ^ '$Header: /cvs/stx/stx/goodies/petitparser/PPStream.st,v 1.4 2014-03-04 14:32:00 cg Exp $'
!

version_HG

    ^ '$Changeset: <not expanded> $'
!

version_SVN
    ^ '$Id: PPStream.st,v 1.4 2014-03-04 14:32:00 cg Exp $'
! !