PPStream.st
changeset 427 a7f5e6de19d2
parent 421 7e08b31e0dae
child 642 77d5fddb6462
equal deleted inserted replaced
426:2a65c972b937 427:a7f5e6de19d2
     1 "{ Package: 'stx:goodies/petitparser' }"
     1 "{ Package: 'stx:goodies/petitparser' }"
     2 
     2 
       
     3 "{ NameSpace: Smalltalk }"
       
     4 
     3 ReadStream subclass:#PPStream
     5 ReadStream subclass:#PPStream
     4 	instanceVariableNames:''
     6 	instanceVariableNames:'newlines'
     5 	classVariableNames:''
     7 	classVariableNames:''
     6 	poolDictionaries:''
     8 	poolDictionaries:''
     7 	category:'PetitParser-Core'
     9 	category:'PetitParser-Core'
     8 !
    10 !
     9 
       
    10 
    11 
    11 
    12 
    12 !PPStream methodsFor:'accessing'!
    13 !PPStream methodsFor:'accessing'!
    13 
    14 
    14 collection
    15 collection
    57 
    58 
    58 asPetitStream
    59 asPetitStream
    59 	^ self
    60 	^ self
    60 ! !
    61 ! !
    61 
    62 
       
    63 !PPStream methodsFor:'positioning'!
       
    64 
       
    65 column: pos
       
    66 	| nl |
       
    67 	(pos = -1) ifTrue: [  ^ 0 ].
       
    68 	(pos > readLimit) ifTrue: [ ^ self error: 'Out of limit' ].
       
    69 	
       
    70 	nl := self newlines.
       
    71 	nl keysAndValuesDo: [ :index :value |
       
    72 		(value > pos) ifTrue: [ ^ pos - (nl at: (index - 1)) + 1]
       
    73 	].	
       
    74 
       
    75 	^ pos - (nl at: (nl size )) + 1
       
    76 !
       
    77 
       
    78 fillNewlines
       
    79 	| tmp line |
       
    80 	newlines := OrderedCollection new.	
       
    81 	
       
    82 	tmp := position.
       
    83 	line := 0.
       
    84 	
       
    85 	(0 to: readLimit) do: [:index |
       
    86 		position := index.
       
    87 		self isStartOfLine ifTrue: [ newlines add: position ]
       
    88 	].
       
    89 	position := tmp.
       
    90 	newlines := newlines asArray.
       
    91 	^ newlines
       
    92 !
       
    93 
       
    94 line: pos
       
    95 	| nl |
       
    96 	(pos = -1) ifTrue: [  ^ 0 ].
       
    97 	(pos > readLimit) ifTrue: [ ^ self error: 'Out of limit' ].
       
    98 	
       
    99 	nl := self newlines.
       
   100 	nl keysAndValuesDo: [ :index :value |
       
   101 		(value > pos) ifTrue: [ ^ (index - 1)]
       
   102 	].	
       
   103 
       
   104 	^ nl size
       
   105 !
       
   106 
       
   107 newlines
       
   108 	^ newlines ifNil: [ 
       
   109 		newlines := self fillNewlines.
       
   110 	]
       
   111 ! !
       
   112 
    62 !PPStream methodsFor:'printing'!
   113 !PPStream methodsFor:'printing'!
    63 
   114 
    64 printOn: aStream
   115 printOn: aStream
    65 	collection isString
   116 	collection isString
    66 		ifFalse: [ ^ super printOn: aStream ].
   117 		ifFalse: [ ^ super printOn: aStream ].
    72 
   123 
    73 !PPStream methodsFor:'queries'!
   124 !PPStream methodsFor:'queries'!
    74 
   125 
    75 column
   126 column
    76 	^ self column: position.
   127 	^ self column: position.
    77 !
       
    78 
       
    79 column: pos
       
    80 	| column clear tmp |
       
    81 	
       
    82 	pos > readLimit ifTrue: [ ^ Error signal: 'Oot of bounds' ].	
       
    83 
       
    84 	tmp := position.
       
    85 	column := 0.	
       
    86 	clear := true.
       
    87 
       
    88 	(0 to: pos) do: 
       
    89 	[:index |
       
    90 		position := index.
       
    91 		self isStartOfLine ifTrue: [ clear := true ].
       
    92 
       
    93 		clear ifTrue: [ column := 0. clear := false ].
       
    94 		column := column + 1.
       
    95 		(position > readLimit) ifTrue: [ position := tmp. ^ column ].
       
    96 	].
       
    97 	position := tmp.
       
    98 	^ column
       
    99 !
   128 !
   100 
   129 
   101 insideCRLF
   130 insideCRLF
   102 	(position < 1) ifTrue: [ ^ false ].
   131 	(position < 1) ifTrue: [ ^ false ].
   103 	
   132 	
   118 	^ (self peekBack = (Character codePoint: 13)) or: [ self peekBack = (Character codePoint: 10)].
   147 	^ (self peekBack = (Character codePoint: 13)) or: [ self peekBack = (Character codePoint: 10)].
   119 !
   148 !
   120 
   149 
   121 line
   150 line
   122 	^ self line: position
   151 	^ self line: position
   123 !
       
   124 
       
   125 line: pos
       
   126 	| tmp line |
       
   127 	(pos = -1) ifTrue: [  ^ 0 ].
       
   128 	(pos > readLimit) ifTrue: [ ^ self error: 'Out of limit' ].
       
   129 	
       
   130 	tmp := position.
       
   131 	line := 0.
       
   132 	
       
   133 	(0 to: pos) do: 
       
   134 	[:index |
       
   135 		position := index.
       
   136 		self isStartOfLine ifTrue: [ line := line + 1 ]
       
   137 	].
       
   138 	position := tmp.
       
   139 	^ line
       
   140 ! !
   152 ! !
   141 
   153 
   142 !PPStream class methodsFor:'documentation'!
   154 !PPStream class methodsFor:'documentation'!
   143 
   155 
   144 version
   156 version