author | Claus Gittinger <cg@exept.de> |
Tue, 25 Jun 2019 11:09:04 +0200 | |
changeset 5048 | 63f42cfbaf59 |
parent 4556 | 861d607dc91f |
permissions | -rw-r--r-- |
478 | 1 |
" |
2 |
COPYRIGHT (c) 1996 by eXept Software AG |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
478
diff
changeset
|
3 |
All Rights Reserved |
478 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
2376 | 12 |
"{ Package: 'stx:libbasic2' }" |
1239 | 13 |
|
3540 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
478 | 16 |
FilteringStream subclass:#LineNumberReadStream |
1283 | 17 |
instanceVariableNames:'lineNumber lineStartPosition' |
478 | 18 |
classVariableNames:'' |
19 |
poolDictionaries:'' |
|
20 |
category:'Streams-Misc' |
|
21 |
! |
|
22 |
||
23 |
!LineNumberReadStream class methodsFor:'documentation'! |
|
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1996 by eXept Software AG |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
478
diff
changeset
|
28 |
All Rights Reserved |
478 | 29 |
|
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
||
38 |
||
39 |
! |
|
40 |
||
41 |
documentation |
|
42 |
" |
|
1283 | 43 |
This filter keeps track of the current line, and optionally |
2538 | 44 |
the current line's start position (if the input stream is positionable). |
1283 | 45 |
while passing text from its inputStream to its outputStream. |
478 | 46 |
|
2538 | 47 |
Can be placed in-between text processing and the text's |
478 | 48 |
input stream, and let it keep track of the lineNumber. |
4456 | 49 |
|
50 |
For example, this is useful if you have a parser which does not keep track of |
|
51 |
line numbers, but want to present an error-line-number in a parse-error message. |
|
478 | 52 |
|
53 |
[see also:] |
|
2538 | 54 |
ReadStream WriteStream ExternalStream FileStream |
55 |
FilteringStream FilteringLineStream |
|
478 | 56 |
|
57 |
[author:] |
|
2538 | 58 |
Claus Gittinger |
478 | 59 |
" |
60 |
! |
|
61 |
||
62 |
examples |
|
63 |
" |
|
64 |
count lines in a Makefile: |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
478
diff
changeset
|
65 |
[exBegin] |
478 | 66 |
|in filter| |
67 |
||
68 |
in := 'Makefile' asFilename readStream. |
|
69 |
||
70 |
filter := LineNumberReadStream readingFrom:in. |
|
71 |
||
72 |
filter upToEnd. |
|
73 |
filter close. |
|
74 |
||
75 |
Transcript showCR:('Makefile has %1 lines' bindWith:(filter lineNumber - 1)). |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
478
diff
changeset
|
76 |
[exEnd] |
478 | 77 |
|
78 |
||
79 |
||
80 |
find a specific string and output its lineNr: |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
478
diff
changeset
|
81 |
[exBegin] |
478 | 82 |
|in filter| |
83 |
||
84 |
in := 'Makefile' asFilename readStream. |
|
85 |
||
86 |
filter := LineNumberReadStream readingFrom:in. |
|
87 |
||
88 |
filter skipThroughAll:'start of'. |
|
89 |
filter close. |
|
90 |
||
91 |
Transcript showCR:('found in line %1' bindWith:(filter lineNumber)). |
|
885
c31412b26306
package-definitions fixed/updated
Claus Gittinger <cg@exept.de>
parents:
478
diff
changeset
|
92 |
[exEnd] |
478 | 93 |
" |
94 |
||
95 |
||
96 |
! ! |
|
97 |
||
98 |
!LineNumberReadStream methodsFor:'accessing'! |
|
99 |
||
100 |
lineNumber |
|
101 |
"return the current lineNumber" |
|
102 |
||
103 |
^ lineNumber |
|
104 |
||
105 |
"Created: 11.1.1997 / 16:57:10 / cg" |
|
106 |
"Modified: 11.1.1997 / 16:57:19 / cg" |
|
107 |
! |
|
108 |
||
109 |
lineNumber:something |
|
110 |
"set the current lineNumber" |
|
111 |
||
112 |
lineNumber := something. |
|
113 |
||
114 |
"Created: 11.1.1997 / 16:57:10 / cg" |
|
115 |
"Modified: 11.1.1997 / 16:57:22 / cg" |
|
1283 | 116 |
! |
117 |
||
118 |
lineStartPosition |
|
119 |
"return the current lines start position" |
|
120 |
||
121 |
^ lineStartPosition |
|
478 | 122 |
! ! |
123 |
||
3180 | 124 |
!LineNumberReadStream methodsFor:'converting'! |
125 |
||
126 |
asLineNumberReadStream |
|
127 |
^ self |
|
128 |
! ! |
|
129 |
||
478 | 130 |
!LineNumberReadStream methodsFor:'initialization'! |
131 |
||
132 |
initialize |
|
133 |
super initialize. |
|
134 |
||
135 |
lineNumber := 1. |
|
1291 | 136 |
(inputStream isPositionable) ifTrue:[ |
3329 | 137 |
lineStartPosition := inputStream position. |
1283 | 138 |
]. |
478 | 139 |
filter := [:char | |
3329 | 140 |
char == Character cr ifTrue:[ |
141 |
lineNumber := lineNumber + 1. |
|
142 |
lineStartPosition notNil ifTrue:[ |
|
143 |
lineStartPosition := inputStream position. |
|
144 |
]. |
|
145 |
]. |
|
146 |
char |
|
147 |
]. |
|
478 | 148 |
|
149 |
"Modified: 11.1.1997 / 16:58:42 / cg" |
|
150 |
! ! |
|
151 |
||
3329 | 152 |
!LineNumberReadStream methodsFor:'positioning'! |
153 |
||
154 |
position:aPosition |
|
155 |
"this method fails if inputStream is not positionable" |
|
156 |
||
157 |
readAhead := nil. |
|
158 |
inputStream position:aPosition. |
|
159 |
(lineStartPosition notNil and:[aPosition < lineStartPosition]) ifTrue:[ |
|
160 |
"we detect backPosition over a single line, but not over multiple lines and |
|
161 |
no forward positioning!!" |
|
162 |
lineNumber := lineNumber - 1. |
|
163 |
lineStartPosition := aPosition. |
|
164 |
]. |
|
165 |
! |
|
166 |
||
167 |
skip:numberToSkip |
|
168 |
"this method fails if inputStream is not positionable" |
|
169 |
||
170 |
readAhead := nil. |
|
171 |
inputStream skip:numberToSkip. |
|
172 |
(lineStartPosition notNil and:[numberToSkip < lineStartPosition negated]) ifTrue:[ |
|
173 |
"we detect backPosition over a single line, but not over multiple lines and |
|
174 |
no forward positioning!!" |
|
175 |
lineNumber := lineNumber - 1. |
|
176 |
lineStartPosition := lineStartPosition + numberToSkip. |
|
177 |
]. |
|
178 |
! ! |
|
179 |
||
2084 | 180 |
!LineNumberReadStream methodsFor:'queries'! |
181 |
||
182 |
atEnd |
|
183 |
"return true, if the receiver stream is at the end" |
|
184 |
||
185 |
readAhead notNil ifTrue:[^ false]. |
|
186 |
^ inputStream atEnd |
|
187 |
! |
|
188 |
||
3180 | 189 |
isLineNumberReadStream |
190 |
^ true |
|
4455 | 191 |
! |
192 |
||
193 |
isPositionable |
|
194 |
^ inputStream isPositionable |
|
195 |
||
196 |
"Created: / 13-07-2017 / 12:23:21 / cg" |
|
3329 | 197 |
! ! |
198 |
||
199 |
!LineNumberReadStream methodsFor:'reading'! |
|
200 |
||
4201 | 201 |
contents |
202 |
^ inputStream contents |
|
203 |
! |
|
204 |
||
205 |
contentsAsString |
|
206 |
^ inputStream contentsAsString |
|
207 |
! |
|
208 |
||
3329 | 209 |
upToAll_positionBefore:aCollection |
3540 | 210 |
"read until a subcollection consisting of the elements in aCollection is encountered. |
3329 | 211 |
Return everything read excluding the elements in aCollection. |
212 |
The position is left before the collection; i.e. the next |
|
213 |
read operations will return those elements. |
|
214 |
If no such subcollection is encountered, all elements up to the end |
|
215 |
are read and returned. |
|
216 |
See also #throughAll: which also reads up to some objects |
|
217 |
but positions behind it and DOES include it in the returned |
|
218 |
collection. |
|
3180 | 219 |
|
3329 | 220 |
This is a copy from PositionableStream>>#upToAll_positionBefore:. |
221 |
It fails if inputStream is not positionable" |
|
222 |
||
223 |
|answerStream element last| |
|
2084 | 224 |
|
3329 | 225 |
last := aCollection last. |
4556 | 226 |
answerStream := self contentsSpecies writeStream:100. |
3329 | 227 |
[(element := self nextOrNil) notNil] whileTrue:[ |
228 |
answerStream nextPut:element. |
|
229 |
(element = last and:[answerStream endsBeforePositionWith:aCollection]) ifTrue:[ |
|
230 |
|backStep| |
|
231 |
backStep := aCollection size negated. |
|
232 |
self skip:backStep. |
|
233 |
answerStream skip:backStep. |
|
234 |
^ answerStream contents |
|
235 |
]. |
|
2084 | 236 |
]. |
3329 | 237 |
^ answerStream contents |
4556 | 238 |
|
239 |
"Modified: / 10-01-2018 / 18:34:30 / stefan" |
|
2084 | 240 |
! ! |
241 |
||
478 | 242 |
!LineNumberReadStream class methodsFor:'documentation'! |
243 |
||
244 |
version |
|
4201 | 245 |
^ '$Header$' |
2376 | 246 |
! |
247 |
||
248 |
version_CVS |
|
4201 | 249 |
^ '$Header$' |
478 | 250 |
! ! |
3180 | 251 |