1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
3 |
All Rights Reserved
|
|
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 |
"
|
|
12 |
|
|
13 |
Stream subclass:#PositionableStream
|
10
|
14 |
instanceVariableNames:'collection position readLimit'
|
|
15 |
classVariableNames:'ErrorDuringFileInSignal'
|
1
|
16 |
poolDictionaries:''
|
|
17 |
category:'Streams'
|
|
18 |
!
|
|
19 |
|
|
20 |
PositionableStream comment:'
|
|
21 |
|
5
|
22 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
23 |
All Rights Reserved
|
|
24 |
|
10
|
25 |
Instances of PositionableStream allow positioning the read pointer.
|
|
26 |
PositionableStream also adds methods for source-chunk reading and writing
|
1
|
27 |
and for filing-in/out of source code.
|
10
|
28 |
This is an abstract class.
|
|
29 |
|
44
|
30 |
$Header: /cvs/stx/stx/libbasic/PositionableStream.st,v 1.10 1994-01-16 03:44:22 claus Exp $
|
10
|
31 |
'!
|
1
|
32 |
|
10
|
33 |
!PositionableStream class methodsFor:'initialization'!
|
1
|
34 |
|
10
|
35 |
initialize
|
44
|
36 |
"setup the signal used to handle errors during fileIn"
|
|
37 |
|
10
|
38 |
ErrorDuringFileInSignal isNil ifTrue:[
|
|
39 |
ErrorDuringFileInSignal := (Signal new) mayProceed:true.
|
|
40 |
ErrorDuringFileInSignal notifierString:'error during fileIn'.
|
|
41 |
]
|
|
42 |
! !
|
1
|
43 |
|
|
44 |
!PositionableStream class methodsFor:'constants'!
|
|
45 |
|
|
46 |
chunkSeparator
|
|
47 |
"return the chunk-separation character"
|
|
48 |
|
|
49 |
^ $!!
|
|
50 |
! !
|
|
51 |
|
|
52 |
!PositionableStream class methodsFor:'instance creation'!
|
|
53 |
|
|
54 |
on:aCollection
|
|
55 |
"return a new PositionableStream streaming on aCollection"
|
|
56 |
|
|
57 |
^ (self basicNew) on:aCollection
|
|
58 |
!
|
|
59 |
|
|
60 |
on:aCollection from:first to:last
|
|
61 |
"return a new PositionableStream streaming on aCollection
|
|
62 |
from first to last"
|
|
63 |
|
|
64 |
|newStream|
|
|
65 |
|
|
66 |
newStream := (self basicNew) on:aCollection.
|
|
67 |
newStream position:first.
|
|
68 |
newStream readLimit:last.
|
|
69 |
^ newStream
|
|
70 |
! !
|
|
71 |
|
|
72 |
!PositionableStream methodsFor:'private'!
|
|
73 |
|
|
74 |
on:aCollection
|
|
75 |
"setup for streaming on aCollection"
|
|
76 |
|
|
77 |
collection := aCollection.
|
|
78 |
readLimit := aCollection size.
|
|
79 |
position := 1
|
|
80 |
!
|
|
81 |
|
|
82 |
positionError
|
|
83 |
"report an error when positioning past the end"
|
|
84 |
|
|
85 |
^ self error:'cannot position past end of collection'
|
|
86 |
! !
|
|
87 |
|
|
88 |
!PositionableStream methodsFor:'accessing'!
|
|
89 |
|
|
90 |
contents
|
|
91 |
"return the entire contents of the stream"
|
|
92 |
|
|
93 |
^ collection
|
|
94 |
!
|
|
95 |
|
|
96 |
peek
|
|
97 |
"look ahead for and return the next element"
|
|
98 |
|
|
99 |
|peekObject|
|
|
100 |
|
|
101 |
peekObject := self next.
|
10
|
102 |
self backStep.
|
1
|
103 |
^ peekObject
|
|
104 |
!
|
|
105 |
|
|
106 |
peekFor:something
|
10
|
107 |
"return true and move past if next == something;
|
|
108 |
otherwise stay and let position unchanged"
|
1
|
109 |
|
|
110 |
self next == something ifTrue:[
|
|
111 |
^ true
|
|
112 |
].
|
10
|
113 |
self backStep.
|
1
|
114 |
^ false
|
|
115 |
!
|
|
116 |
|
|
117 |
readLimit:aNumber
|
|
118 |
"set the read-limit"
|
|
119 |
|
|
120 |
readLimit := aNumber
|
|
121 |
!
|
|
122 |
|
|
123 |
upTo:element
|
|
124 |
"return a collection of the elements up-to
|
|
125 |
(but excluding) the argument, element.
|
|
126 |
Return nil if the stream-end is reached before."
|
|
127 |
|
|
128 |
|newColl e|
|
|
129 |
|
|
130 |
newColl := collection species new.
|
|
131 |
e := self next.
|
|
132 |
[e = element] whileFalse:[
|
|
133 |
newColl := newColl copyWith:e.
|
|
134 |
e := self next.
|
|
135 |
self atEnd ifTrue:[^ nil]
|
|
136 |
].
|
|
137 |
^ newColl
|
|
138 |
|
|
139 |
"(ReadStream on:'1234567890') upTo:$5"
|
|
140 |
"(ReadStream on:'123456') upTo:$7"
|
2
|
141 |
!
|
|
142 |
|
|
143 |
upToEnd
|
44
|
144 |
"return a collection of the elements up-to the end.
|
2
|
145 |
Return nil if the stream-end is reached before."
|
|
146 |
|
10
|
147 |
|newColl|
|
|
148 |
|
|
149 |
"this implementation has stupid (o-square) runtime behavior -
|
44
|
150 |
should I add a query for resizability of collection and use
|
10
|
151 |
add: instead ?"
|
2
|
152 |
|
|
153 |
newColl := collection species new.
|
|
154 |
[self atEnd] whileFalse:[
|
|
155 |
newColl := newColl copyWith:(self next).
|
|
156 |
].
|
|
157 |
^ newColl
|
|
158 |
|
|
159 |
"(ReadStream on:'1234567890') upToEnd"
|
|
160 |
"((ReadStream on:'123456') next; next) upToEnd"
|
|
161 |
!
|
|
162 |
|
|
163 |
upToSeparator
|
|
164 |
"Return the next elements up to but not including the next separator."
|
|
165 |
|
|
166 |
|stream ch|
|
|
167 |
|
|
168 |
stream := WriteStream on: (collection species new).
|
|
169 |
[(ch := self peek) == nil] whileFalse:[
|
|
170 |
ch isSeparator ifTrue: [
|
|
171 |
^ stream contents
|
|
172 |
] ifFalse: [
|
|
173 |
self skip: 1.
|
|
174 |
stream nextPut: ch
|
|
175 |
]
|
|
176 |
].
|
|
177 |
^ stream contents
|
1
|
178 |
! !
|
|
179 |
|
|
180 |
!PositionableStream methodsFor:'testing'!
|
|
181 |
|
|
182 |
atEnd
|
|
183 |
"return true, if the read-position is at the end"
|
|
184 |
|
|
185 |
^ position > readLimit
|
|
186 |
!
|
|
187 |
|
|
188 |
isEmpty
|
|
189 |
"return true, if the contents of the stream is empty"
|
|
190 |
|
|
191 |
^ readLimit == 0
|
|
192 |
! !
|
|
193 |
|
|
194 |
!PositionableStream methodsFor:'positioning'!
|
|
195 |
|
|
196 |
position
|
|
197 |
"return the read position"
|
|
198 |
|
|
199 |
^ position
|
|
200 |
!
|
|
201 |
|
|
202 |
position:index
|
|
203 |
"set the read position"
|
|
204 |
|
13
|
205 |
((index > (readLimit + 1)) or:[index < 1]) ifTrue: [^ self positionError].
|
1
|
206 |
position := index
|
|
207 |
!
|
|
208 |
|
10
|
209 |
backStep
|
|
210 |
"move backward read position by one"
|
|
211 |
|
|
212 |
position <= 1 ifTrue: [^ self positionError].
|
|
213 |
position := position - 1
|
|
214 |
!
|
|
215 |
|
1
|
216 |
reset
|
|
217 |
"set the read position to the beginning of the collection"
|
|
218 |
|
|
219 |
position := 1
|
|
220 |
!
|
|
221 |
|
|
222 |
setToEnd
|
|
223 |
"set the read position to the end of the collection"
|
|
224 |
|
|
225 |
position := readLimit
|
|
226 |
!
|
|
227 |
|
|
228 |
skip:numberToSkip
|
|
229 |
"skip the next numberToSkip elements"
|
|
230 |
|
|
231 |
self position:(position + numberToSkip)
|
|
232 |
! !
|
|
233 |
|
44
|
234 |
!PositionableStream methodsFor:'chunk input/output'!
|
1
|
235 |
|
|
236 |
skipSeparators
|
|
237 |
"skip all whitespace; so that next will return next non-white-space
|
|
238 |
element"
|
|
239 |
|
|
240 |
|nextOne|
|
|
241 |
|
|
242 |
nextOne := self peek.
|
|
243 |
[(nextOne notNil) and:[nextOne isSeparator]] whileTrue:[
|
|
244 |
self next.
|
|
245 |
nextOne := self peek
|
|
246 |
].
|
|
247 |
^ nextOne
|
|
248 |
!
|
|
249 |
|
|
250 |
skipSeparatorsExceptCR
|
|
251 |
"skip all whitespace except newlines;
|
|
252 |
next will return next non-white-space element"
|
|
253 |
|
|
254 |
|nextOne|
|
|
255 |
|
|
256 |
nextOne := self peek.
|
|
257 |
[(nextOne notNil) and:[nextOne isSeparator]] whileTrue:[
|
|
258 |
nextOne isEndOfLineCharacter ifTrue:[^ nextOne].
|
|
259 |
self next.
|
|
260 |
nextOne := self peek
|
|
261 |
].
|
|
262 |
^ nextOne
|
|
263 |
!
|
|
264 |
|
|
265 |
skipFor:anObject
|
|
266 |
"skip all objects up-to and including anObject; return the element
|
|
267 |
after"
|
|
268 |
|
|
269 |
|nextOne|
|
|
270 |
|
|
271 |
nextOne := self next.
|
|
272 |
[nextOne ~~ anObject] whileTrue:[
|
|
273 |
self atEnd ifTrue:[^ nil].
|
|
274 |
self next.
|
|
275 |
nextOne := self peek
|
|
276 |
].
|
|
277 |
^ self next
|
|
278 |
!
|
|
279 |
|
|
280 |
nextChunk
|
|
281 |
"return the next chunk, i.e. all characters up to the next
|
|
282 |
non-doubled exclamation mark; undouble doubled exclamation marks"
|
|
283 |
|
|
284 |
|theString sep newString done thisChar nextChar inPrimitive
|
|
285 |
index "{ Class:SmallInteger }"
|
|
286 |
currSize "{ Class:SmallInteger }" |
|
|
287 |
|
|
288 |
sep := self class chunkSeparator.
|
|
289 |
theString := String new:500.
|
|
290 |
currSize := 500.
|
|
291 |
thisChar := self skipSeparators.
|
|
292 |
thisChar := self next.
|
|
293 |
index := 0.
|
|
294 |
done := false.
|
|
295 |
inPrimitive := false.
|
|
296 |
|
|
297 |
[done] whileFalse:[
|
|
298 |
((index + 2) <= currSize) ifFalse:[
|
|
299 |
newString := String new:(currSize * 2).
|
|
300 |
newString replaceFrom:1 to:currSize with:theString.
|
|
301 |
currSize := currSize * 2.
|
|
302 |
theString := newString
|
|
303 |
].
|
|
304 |
thisChar isNil ifTrue:[
|
|
305 |
done := true
|
|
306 |
] ifFalse:[
|
|
307 |
(thisChar == $% ) ifTrue:[
|
|
308 |
nextChar := self peek.
|
|
309 |
(nextChar == ${ ) ifTrue:[
|
|
310 |
inPrimitive := true.
|
|
311 |
index := index + 1.
|
|
312 |
theString at:index put:thisChar.
|
|
313 |
thisChar := self next
|
|
314 |
] ifFalse:[
|
|
315 |
(nextChar == $} ) ifTrue:[
|
|
316 |
inPrimitive := false.
|
|
317 |
index := index + 1.
|
|
318 |
theString at:index put:thisChar.
|
|
319 |
thisChar := self next
|
|
320 |
]
|
|
321 |
]
|
|
322 |
] ifFalse:[
|
|
323 |
inPrimitive ifFalse:[
|
|
324 |
(thisChar == sep) ifTrue:[
|
|
325 |
(self peek == sep) ifFalse:[
|
|
326 |
done := true
|
|
327 |
] ifTrue:[
|
|
328 |
self next
|
|
329 |
]
|
|
330 |
]
|
|
331 |
]
|
|
332 |
]
|
|
333 |
].
|
|
334 |
done ifFalse:[
|
|
335 |
index := index + 1.
|
|
336 |
theString at:index put:thisChar.
|
|
337 |
thisChar := self next
|
|
338 |
]
|
|
339 |
].
|
|
340 |
(index == 0) ifTrue:[^ ''].
|
|
341 |
^ theString copyFrom:1 to:index
|
|
342 |
!
|
|
343 |
|
|
344 |
nextChunkPut:aString
|
|
345 |
"put aString as a chunk onto the receiver;
|
|
346 |
double all exclamation marks and append an exclamation mark"
|
|
347 |
|
|
348 |
|sep gotPercent inPrimitive character
|
|
349 |
index "{ Class:SmallInteger }"
|
|
350 |
endIndex "{ Class:SmallInteger }"
|
|
351 |
next "{ Class:SmallInteger }" |
|
|
352 |
|
|
353 |
sep := self class chunkSeparator.
|
|
354 |
inPrimitive := false.
|
|
355 |
gotPercent := false.
|
|
356 |
index := 1.
|
|
357 |
endIndex := aString size.
|
|
358 |
|
|
359 |
[index <= endIndex] whileTrue:[
|
|
360 |
next := aString indexOf:$% startingAt:index ifAbsent:[endIndex + 1].
|
|
361 |
next := next min:
|
|
362 |
(aString indexOf:${ startingAt:index ifAbsent:[endIndex + 1]).
|
|
363 |
next := next min:
|
|
364 |
(aString indexOf:$} startingAt:index ifAbsent:[endIndex + 1]).
|
|
365 |
next := next min:
|
|
366 |
(aString indexOf:sep startingAt:index ifAbsent:[endIndex + 1]).
|
|
367 |
|
|
368 |
((index == 1) and:[next == (endIndex + 1)]) ifTrue:[
|
|
369 |
self nextPutAll:aString
|
|
370 |
] ifFalse:[
|
|
371 |
self nextPutAll:(aString copyFrom:index to:(next - 1))
|
|
372 |
].
|
|
373 |
|
|
374 |
index := next.
|
|
375 |
(index <= endIndex) ifTrue:[
|
|
376 |
character := aString at:index.
|
|
377 |
(character == $% ) ifTrue:[
|
|
378 |
gotPercent := true
|
|
379 |
] ifFalse:[
|
|
380 |
(character == ${ ) ifTrue:[
|
|
381 |
gotPercent ifTrue:[
|
|
382 |
inPrimitive := true
|
|
383 |
]
|
|
384 |
] ifFalse:[
|
|
385 |
(character == $} ) ifTrue:[
|
|
386 |
gotPercent ifTrue:[
|
|
387 |
inPrimitive := false
|
|
388 |
]
|
|
389 |
] ifFalse:[
|
|
390 |
inPrimitive ifFalse:[
|
|
391 |
(character == sep) ifTrue:[
|
|
392 |
self nextPut:sep
|
|
393 |
]
|
|
394 |
]
|
|
395 |
]
|
|
396 |
].
|
|
397 |
gotPercent := false
|
|
398 |
].
|
|
399 |
self nextPut:character.
|
|
400 |
index := index + 1
|
|
401 |
]
|
|
402 |
].
|
|
403 |
self nextPut:sep
|
44
|
404 |
! !
|
|
405 |
|
|
406 |
!PositionableStream methodsFor:'fileIn'!
|
|
407 |
|
|
408 |
fileIn
|
|
409 |
"file in from the receiver, i.e. read chunks and evaluate them -
|
|
410 |
return the value of the last chunk."
|
|
411 |
|
|
412 |
^ self fileInNotifying:self
|
|
413 |
!
|
|
414 |
|
|
415 |
fileInNotifying:someone
|
|
416 |
"file in from the receiver, i.e. read chunks and evaluate them -
|
|
417 |
return the value of the last chunk.
|
|
418 |
someone (which is usually some codeView) is notified of errors."
|
|
419 |
|
|
420 |
|lastValue|
|
|
421 |
|
|
422 |
SignalSet anySignal handle:[:ex |
|
|
423 |
|action what|
|
|
424 |
|
|
425 |
"handle the case where no GUI has been built in,
|
|
426 |
just abort with a notification"
|
|
427 |
|
|
428 |
Display isNil ifTrue:[
|
|
429 |
self notify:(ex signal notifierString).
|
|
430 |
ex return
|
|
431 |
].
|
|
432 |
|
|
433 |
"otherwise ask what should be done now and either
|
|
434 |
continue or abort the fileIn"
|
|
435 |
|
|
436 |
what := ex errorString.
|
|
437 |
what isNil ifTrue:[
|
|
438 |
what := ex signal notifierString.
|
|
439 |
].
|
|
440 |
action := self askForDebug:('error in fileIn: ' , what) withCRs.
|
|
441 |
action == #continue ifTrue:[
|
|
442 |
ex proceed
|
|
443 |
].
|
|
444 |
action == #abort ifTrue:[
|
|
445 |
ex return
|
|
446 |
].
|
|
447 |
Debugger enter:ex suspendedContext
|
|
448 |
"ex reject"
|
|
449 |
] do:[
|
|
450 |
[self atEnd] whileFalse:[
|
|
451 |
lastValue := self fileInNextChunkNotifying:someone
|
|
452 |
]
|
|
453 |
].
|
|
454 |
^ lastValue
|
|
455 |
!
|
|
456 |
|
|
457 |
fileInNextChunk
|
|
458 |
"read next chunk, evaluate it and return the result"
|
|
459 |
|
|
460 |
^ self fileInNextChunkNotifying:nil
|
1
|
461 |
!
|
|
462 |
|
|
463 |
fileInNextChunkNotifying:someone
|
|
464 |
"read next chunk, evaluate it and return the result;
|
44
|
465 |
someone (which is usually some codeView) is notified of errors."
|
1
|
466 |
|
|
467 |
|aString sawExcla sep|
|
|
468 |
|
|
469 |
sep := self class chunkSeparator.
|
|
470 |
self skipSeparators.
|
|
471 |
self atEnd ifFalse:[
|
|
472 |
sawExcla := self peekFor:sep.
|
|
473 |
aString := self nextChunk.
|
|
474 |
aString size ~~ 0 ifTrue:[
|
|
475 |
sawExcla ifFalse:[
|
|
476 |
^ Compiler evaluate:aString notifying:someone
|
|
477 |
].
|
|
478 |
^ (Compiler evaluate:aString notifying:someone)
|
|
479 |
fileInFrom:self notifying:someone
|
|
480 |
]
|
|
481 |
].
|
|
482 |
^ nil
|
|
483 |
|
|
484 |
!
|
|
485 |
|
|
486 |
askForDebug:message
|
41
|
487 |
"launch a box asking if a debugger is wanted - used when an error
|
|
488 |
occurs while filing in"
|
|
489 |
|
1
|
490 |
|box|
|
|
491 |
|
|
492 |
box := OptionBox title:message numberOfOptions:3.
|
|
493 |
box actions:(Array with:[^ #abort]
|
|
494 |
with:[^ #debug]
|
|
495 |
with:[^ #continue]).
|
|
496 |
box buttonTitles:#('abort' 'debug' 'continue').
|
|
497 |
box showAtPointer.
|
|
498 |
^ #abort
|
44
|
499 |
! !
|
10
|
500 |
|
44
|
501 |
!PositionableStream methodsFor:'fileIn error handling'!
|
1
|
502 |
|
|
503 |
error:aMessage position:position to:endPos
|
44
|
504 |
"error notification during fileIn with no requestor.
|
|
505 |
This is sent by the compiler/evaluator if it detects errors."
|
1
|
506 |
|
|
507 |
position printOn:Transcript.
|
|
508 |
Transcript show:' '.
|
|
509 |
Transcript showCr:aMessage.
|
|
510 |
^ false
|
|
511 |
!
|
|
512 |
|
|
513 |
correctableError:aMessage position:position to:endPos
|
44
|
514 |
"error notification during fileIn with no requestor.
|
|
515 |
This is sent by the compiler/evaluator if it detects errors."
|
1
|
516 |
|
|
517 |
^ self error:aMessage position:position to:endPos
|
|
518 |
!
|
|
519 |
|
|
520 |
warning:aMessage position:position to:endPos
|
44
|
521 |
"warning notification during fileIn with no requestor - ignore it.
|
|
522 |
This is sent by the compiler/evaluator if it detects errors."
|
1
|
523 |
|
|
524 |
^ self
|
|
525 |
! !
|