PPStream.st
changeset 427 a7f5e6de19d2
parent 421 7e08b31e0dae
child 642 77d5fddb6462
--- a/PPStream.st	Mon Apr 13 14:19:55 2015 +0100
+++ b/PPStream.st	Mon Apr 13 22:00:44 2015 +0100
@@ -1,14 +1,15 @@
 "{ Package: 'stx:goodies/petitparser' }"
 
+"{ NameSpace: Smalltalk }"
+
 ReadStream subclass:#PPStream
-	instanceVariableNames:''
+	instanceVariableNames:'newlines'
 	classVariableNames:''
 	poolDictionaries:''
 	category:'PetitParser-Core'
 !
 
 
-
 !PPStream methodsFor:'accessing'!
 
 collection
@@ -59,6 +60,56 @@
 	^ self
 ! !
 
+!PPStream methodsFor:'positioning'!
+
+column: pos
+	| nl |
+	(pos = -1) ifTrue: [  ^ 0 ].
+	(pos > readLimit) ifTrue: [ ^ self error: 'Out of limit' ].
+	
+	nl := self newlines.
+	nl keysAndValuesDo: [ :index :value |
+		(value > pos) ifTrue: [ ^ pos - (nl at: (index - 1)) + 1]
+	].	
+
+	^ pos - (nl at: (nl size )) + 1
+!
+
+fillNewlines
+	| tmp line |
+	newlines := OrderedCollection new.	
+	
+	tmp := position.
+	line := 0.
+	
+	(0 to: readLimit) do: [:index |
+		position := index.
+		self isStartOfLine ifTrue: [ newlines add: position ]
+	].
+	position := tmp.
+	newlines := newlines asArray.
+	^ newlines
+!
+
+line: pos
+	| nl |
+	(pos = -1) ifTrue: [  ^ 0 ].
+	(pos > readLimit) ifTrue: [ ^ self error: 'Out of limit' ].
+	
+	nl := self newlines.
+	nl keysAndValuesDo: [ :index :value |
+		(value > pos) ifTrue: [ ^ (index - 1)]
+	].	
+
+	^ nl size
+!
+
+newlines
+	^ newlines ifNil: [ 
+		newlines := self fillNewlines.
+	]
+! !
+
 !PPStream methodsFor:'printing'!
 
 printOn: aStream
@@ -76,28 +127,6 @@
 	^ 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 ].
 	
@@ -120,23 +149,6 @@
 
 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'!