1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
159
|
3 |
All Rights Reserved
|
1
|
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 |
|
57
|
13 |
PeekableStream subclass:#PositionableStream
|
10
|
14 |
instanceVariableNames:'collection position readLimit'
|
276
|
15 |
classVariableNames:'ErrorDuringFileInSignal ChunkSeparator'
|
1
|
16 |
poolDictionaries:''
|
|
17 |
category:'Streams'
|
|
18 |
!
|
|
19 |
|
|
20 |
PositionableStream comment:'
|
88
|
21 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
159
|
22 |
All Rights Reserved
|
93
|
23 |
|
282
|
24 |
$Header: /cvs/stx/stx/libbasic/PositionableStream.st,v 1.21 1995-02-24 16:37:50 claus Exp $
|
88
|
25 |
'!
|
1
|
26 |
|
88
|
27 |
!PositionableStream class methodsFor:'documentation'!
|
|
28 |
|
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
159
|
32 |
All Rights Reserved
|
1
|
33 |
|
88
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
10
|
42 |
|
88
|
43 |
version
|
|
44 |
"
|
282
|
45 |
$Header: /cvs/stx/stx/libbasic/PositionableStream.st,v 1.21 1995-02-24 16:37:50 claus Exp $
|
88
|
46 |
"
|
|
47 |
!
|
|
48 |
|
|
49 |
documentation
|
|
50 |
"
|
|
51 |
Instances of PositionableStream allow positioning the read pointer.
|
|
52 |
The PositionableStream class also adds methods for source-chunk reading
|
|
53 |
and writing, and for filing-in/out of source code.
|
|
54 |
This is an abstract class.
|
|
55 |
"
|
|
56 |
! !
|
1
|
57 |
|
10
|
58 |
!PositionableStream class methodsFor:'initialization'!
|
1
|
59 |
|
10
|
60 |
initialize
|
44
|
61 |
"setup the signal used to handle errors during fileIn"
|
|
62 |
|
10
|
63 |
ErrorDuringFileInSignal isNil ifTrue:[
|
159
|
64 |
Object initialize.
|
|
65 |
|
|
66 |
ErrorDuringFileInSignal := Object errorSignal newSignalMayProceed:true.
|
|
67 |
ErrorDuringFileInSignal nameClass:self message:#errorDuringFileInSignal.
|
|
68 |
ErrorDuringFileInSignal notifierString:'error during fileIn'.
|
276
|
69 |
|
|
70 |
ChunkSeparator := $!!
|
10
|
71 |
]
|
|
72 |
! !
|
1
|
73 |
|
|
74 |
!PositionableStream class methodsFor:'constants'!
|
|
75 |
|
|
76 |
chunkSeparator
|
|
77 |
"return the chunk-separation character"
|
|
78 |
|
276
|
79 |
^ ChunkSeparator
|
1
|
80 |
! !
|
|
81 |
|
|
82 |
!PositionableStream class methodsFor:'instance creation'!
|
|
83 |
|
|
84 |
on:aCollection
|
|
85 |
"return a new PositionableStream streaming on aCollection"
|
|
86 |
|
|
87 |
^ (self basicNew) on:aCollection
|
|
88 |
!
|
|
89 |
|
|
90 |
on:aCollection from:first to:last
|
|
91 |
"return a new PositionableStream streaming on aCollection
|
|
92 |
from first to last"
|
|
93 |
|
|
94 |
|newStream|
|
|
95 |
|
|
96 |
newStream := (self basicNew) on:aCollection.
|
57
|
97 |
newStream position:first "-1".
|
1
|
98 |
newStream readLimit:last.
|
|
99 |
^ newStream
|
57
|
100 |
!
|
|
101 |
|
|
102 |
with:aCollection
|
|
103 |
"return a new PositionableStream streaming on aCollection,
|
|
104 |
the stream is positioned to the end of the collection."
|
|
105 |
|
|
106 |
^ (self on:aCollection) setToEnd
|
1
|
107 |
! !
|
|
108 |
|
|
109 |
!PositionableStream methodsFor:'private'!
|
|
110 |
|
|
111 |
on:aCollection
|
|
112 |
"setup for streaming on aCollection"
|
|
113 |
|
|
114 |
collection := aCollection.
|
|
115 |
readLimit := aCollection size.
|
57
|
116 |
position := "0" 1
|
1
|
117 |
!
|
|
118 |
|
|
119 |
positionError
|
|
120 |
"report an error when positioning past the end"
|
|
121 |
|
|
122 |
^ self error:'cannot position past end of collection'
|
|
123 |
! !
|
|
124 |
|
57
|
125 |
!PositionableStream methodsFor:'private'!
|
|
126 |
|
|
127 |
contentsSpecies
|
88
|
128 |
"return a class of which instances will be returned, when
|
|
129 |
parts of the collection are asked for.
|
|
130 |
(see upTo-kind of methods in subclasses)"
|
|
131 |
|
57
|
132 |
^ collection species
|
|
133 |
! !
|
|
134 |
|
1
|
135 |
!PositionableStream methodsFor:'accessing'!
|
|
136 |
|
|
137 |
contents
|
|
138 |
"return the entire contents of the stream"
|
|
139 |
|
|
140 |
^ collection
|
|
141 |
!
|
|
142 |
|
|
143 |
peek
|
|
144 |
"look ahead for and return the next element"
|
|
145 |
|
|
146 |
|peekObject|
|
|
147 |
|
|
148 |
peekObject := self next.
|
10
|
149 |
self backStep.
|
1
|
150 |
^ peekObject
|
|
151 |
!
|
|
152 |
|
|
153 |
peekFor:something
|
10
|
154 |
"return true and move past if next == something;
|
|
155 |
otherwise stay and let position unchanged"
|
1
|
156 |
|
|
157 |
self next == something ifTrue:[
|
159
|
158 |
^ true
|
1
|
159 |
].
|
10
|
160 |
self backStep.
|
1
|
161 |
^ false
|
|
162 |
!
|
|
163 |
|
|
164 |
readLimit:aNumber
|
|
165 |
"set the read-limit"
|
|
166 |
|
|
167 |
readLimit := aNumber
|
|
168 |
! !
|
|
169 |
|
|
170 |
!PositionableStream methodsFor:'testing'!
|
|
171 |
|
|
172 |
atEnd
|
|
173 |
"return true, if the read-position is at the end"
|
|
174 |
|
|
175 |
^ position > readLimit
|
|
176 |
!
|
|
177 |
|
|
178 |
isEmpty
|
|
179 |
"return true, if the contents of the stream is empty"
|
|
180 |
|
|
181 |
^ readLimit == 0
|
|
182 |
! !
|
|
183 |
|
|
184 |
!PositionableStream methodsFor:'positioning'!
|
|
185 |
|
|
186 |
position
|
|
187 |
"return the read position"
|
|
188 |
|
|
189 |
^ position
|
|
190 |
!
|
|
191 |
|
|
192 |
position:index
|
|
193 |
"set the read position"
|
|
194 |
|
57
|
195 |
((index > readLimit) or:[index < 0]) ifTrue: [^ self positionError].
|
1
|
196 |
position := index
|
|
197 |
!
|
|
198 |
|
10
|
199 |
backStep
|
|
200 |
"move backward read position by one"
|
|
201 |
|
57
|
202 |
position <= 0 ifTrue: [^ self positionError].
|
10
|
203 |
position := position - 1
|
|
204 |
!
|
|
205 |
|
1
|
206 |
reset
|
|
207 |
"set the read position to the beginning of the collection"
|
|
208 |
|
57
|
209 |
position := "0" 1
|
1
|
210 |
!
|
|
211 |
|
|
212 |
setToEnd
|
|
213 |
"set the read position to the end of the collection"
|
|
214 |
|
|
215 |
position := readLimit
|
|
216 |
!
|
|
217 |
|
|
218 |
skip:numberToSkip
|
|
219 |
"skip the next numberToSkip elements"
|
|
220 |
|
|
221 |
self position:(position + numberToSkip)
|
|
222 |
! !
|
|
223 |
|
44
|
224 |
!PositionableStream methodsFor:'chunk input/output'!
|
1
|
225 |
|
|
226 |
nextChunk
|
|
227 |
"return the next chunk, i.e. all characters up to the next
|
217
|
228 |
exclamation mark. Within the chunk, exclamation marks have to be doubled,
|
|
229 |
they are undoubled here.
|
|
230 |
Except for primitive code, in which doubling is not needed (allowed).
|
|
231 |
This exception was added to make it easier to edit primitive code with
|
|
232 |
external editors. However, this means, that other Smalltalks cannot always
|
|
233 |
read chunks containing primitive code
|
|
234 |
- but that doesnt really matter, since C-primitives are an ST/X feature anyway."
|
1
|
235 |
|
|
236 |
|theString sep newString done thisChar nextChar inPrimitive
|
|
237 |
index "{ Class:SmallInteger }"
|
|
238 |
currSize "{ Class:SmallInteger }" |
|
|
239 |
|
276
|
240 |
sep := ChunkSeparator.
|
1
|
241 |
theString := String new:500.
|
|
242 |
currSize := 500.
|
|
243 |
thisChar := self skipSeparators.
|
|
244 |
thisChar := self next.
|
|
245 |
index := 0.
|
|
246 |
done := false.
|
|
247 |
inPrimitive := false.
|
|
248 |
|
|
249 |
[done] whileFalse:[
|
159
|
250 |
((index + 2) <= currSize) ifFalse:[
|
|
251 |
newString := String new:(currSize * 2).
|
|
252 |
newString replaceFrom:1 to:currSize with:theString.
|
|
253 |
currSize := currSize * 2.
|
|
254 |
theString := newString
|
|
255 |
].
|
|
256 |
thisChar isNil ifTrue:[
|
|
257 |
done := true
|
|
258 |
] ifFalse:[
|
|
259 |
(thisChar == $% ) ifTrue:[
|
|
260 |
nextChar := self peek.
|
|
261 |
(nextChar == ${ ) ifTrue:[
|
|
262 |
inPrimitive := true.
|
|
263 |
index := index + 1.
|
|
264 |
theString at:index put:thisChar.
|
|
265 |
thisChar := self next
|
|
266 |
] ifFalse:[
|
|
267 |
(nextChar == $} ) ifTrue:[
|
|
268 |
inPrimitive := false.
|
|
269 |
index := index + 1.
|
|
270 |
theString at:index put:thisChar.
|
|
271 |
thisChar := self next
|
|
272 |
]
|
|
273 |
]
|
|
274 |
] ifFalse:[
|
|
275 |
inPrimitive ifFalse:[
|
|
276 |
(thisChar == sep) ifTrue:[
|
|
277 |
(self peek == sep) ifFalse:[
|
|
278 |
done := true
|
|
279 |
] ifTrue:[
|
|
280 |
self next
|
|
281 |
]
|
|
282 |
]
|
|
283 |
]
|
|
284 |
]
|
|
285 |
].
|
|
286 |
done ifFalse:[
|
|
287 |
index := index + 1.
|
|
288 |
theString at:index put:thisChar.
|
|
289 |
thisChar := self next
|
|
290 |
]
|
1
|
291 |
].
|
|
292 |
(index == 0) ifTrue:[^ ''].
|
57
|
293 |
^ theString copyTo:index
|
1
|
294 |
!
|
|
295 |
|
|
296 |
nextChunkPut:aString
|
|
297 |
"put aString as a chunk onto the receiver;
|
217
|
298 |
double all exclamation marks except within primitives and append a
|
276
|
299 |
single delimiting exclamation mark at the end.
|
|
300 |
This modification of the chunk format was done to have primitive
|
|
301 |
code more readable. (since it must be edited using the fileBrowser).
|
|
302 |
Its no incompatibility, since inline primitives are an ST/X special
|
|
303 |
anyway."
|
1
|
304 |
|
|
305 |
|sep gotPercent inPrimitive character
|
|
306 |
index "{ Class:SmallInteger }"
|
|
307 |
endIndex "{ Class:SmallInteger }"
|
217
|
308 |
stop "{ Class:SmallInteger }"
|
253
|
309 |
next "{ Class:SmallInteger }"|
|
1
|
310 |
|
276
|
311 |
sep := ChunkSeparator.
|
1
|
312 |
inPrimitive := false.
|
|
313 |
gotPercent := false.
|
|
314 |
index := 1.
|
|
315 |
endIndex := aString size.
|
217
|
316 |
stop := endIndex + 1.
|
1
|
317 |
|
|
318 |
[index <= endIndex] whileTrue:[
|
217
|
319 |
"
|
|
320 |
find position of next interresting character;
|
|
321 |
output stuff up to that one in one piece
|
|
322 |
"
|
|
323 |
next := aString indexOf:$% startingAt:index ifAbsent:stop.
|
159
|
324 |
next := next min:
|
217
|
325 |
(aString indexOf:${ startingAt:index ifAbsent:stop).
|
|
326 |
next := next min:
|
|
327 |
(aString indexOf:$} startingAt:index ifAbsent:stop).
|
159
|
328 |
next := next min:
|
217
|
329 |
(aString indexOf:sep startingAt:index ifAbsent:stop).
|
1
|
330 |
|
217
|
331 |
((index == 1) and:[next == stop]) ifTrue:[
|
159
|
332 |
self nextPutAll:aString
|
|
333 |
] ifFalse:[
|
217
|
334 |
self nextPutAll:aString startingAt:index to:(next - 1)
|
159
|
335 |
].
|
1
|
336 |
|
159
|
337 |
index := next.
|
|
338 |
(index <= endIndex) ifTrue:[
|
|
339 |
character := aString at:index.
|
276
|
340 |
|
159
|
341 |
(character == $% ) ifTrue:[
|
|
342 |
gotPercent := true
|
|
343 |
] ifFalse:[
|
|
344 |
(character == ${ ) ifTrue:[
|
|
345 |
gotPercent ifTrue:[
|
|
346 |
inPrimitive := true
|
|
347 |
]
|
|
348 |
] ifFalse:[
|
|
349 |
(character == $} ) ifTrue:[
|
|
350 |
gotPercent ifTrue:[
|
|
351 |
inPrimitive := false
|
|
352 |
]
|
|
353 |
] ifFalse:[
|
|
354 |
inPrimitive ifFalse:[
|
|
355 |
(character == sep) ifTrue:[
|
|
356 |
self nextPut:sep
|
|
357 |
]
|
|
358 |
]
|
|
359 |
]
|
|
360 |
].
|
|
361 |
gotPercent := false
|
|
362 |
].
|
276
|
363 |
|
159
|
364 |
self nextPut:character.
|
|
365 |
index := index + 1
|
|
366 |
]
|
1
|
367 |
].
|
|
368 |
self nextPut:sep
|
44
|
369 |
! !
|
|
370 |
|
|
371 |
!PositionableStream methodsFor:'fileIn'!
|
|
372 |
|
|
373 |
fileIn
|
|
374 |
"file in from the receiver, i.e. read chunks and evaluate them -
|
|
375 |
return the value of the last chunk."
|
|
376 |
|
282
|
377 |
^ self fileInNotifying:(CompilationErrorHandler on:self) passChunk:true
|
44
|
378 |
!
|
|
379 |
|
282
|
380 |
fileInNotifying:someone passChunk:passChunk
|
44
|
381 |
"file in from the receiver, i.e. read chunks and evaluate them -
|
|
382 |
return the value of the last chunk.
|
93
|
383 |
Someone (which is usually some codeView) is notified of errors."
|
44
|
384 |
|
|
385 |
|lastValue|
|
|
386 |
|
282
|
387 |
"
|
|
388 |
catch any errors during fileIn
|
|
389 |
- offer debug/abort/continue choice
|
|
390 |
"
|
159
|
391 |
Object errorSignal handle:[:ex |
|
|
392 |
|action what|
|
44
|
393 |
|
159
|
394 |
"handle the case where no GUI has been built in,
|
|
395 |
just abort the fileIn with a notification"
|
44
|
396 |
|
159
|
397 |
Display isNil ifTrue:[
|
|
398 |
self notify:(ex signal notifierString ,
|
|
399 |
' in ' , ex suspendedContext sender receiver class name ,
|
|
400 |
'>>>' , ex suspendedContext sender selector).
|
|
401 |
ex return
|
|
402 |
].
|
44
|
403 |
|
159
|
404 |
"otherwise ask what should be done now and either
|
|
405 |
continue or abort the fileIn"
|
44
|
406 |
|
159
|
407 |
what := ex errorString.
|
|
408 |
what isNil ifTrue:[
|
|
409 |
what := ex signal notifierString.
|
|
410 |
].
|
|
411 |
action := self askForDebug:('error in fileIn: ' , what) withCRs.
|
|
412 |
action == #continue ifTrue:[
|
|
413 |
ex proceed
|
|
414 |
].
|
|
415 |
action == #abort ifTrue:[
|
|
416 |
ex return
|
|
417 |
].
|
253
|
418 |
"/ Debugger enter:ex suspendedContext
|
|
419 |
(ex signal) enterDebuggerWith:ex message:(ex errorString).
|
159
|
420 |
"ex reject"
|
44
|
421 |
] do:[
|
159
|
422 |
[self atEnd] whileFalse:[
|
282
|
423 |
lastValue := self fileInNextChunkNotifying:someone passChunk:passChunk
|
159
|
424 |
]
|
44
|
425 |
].
|
|
426 |
^ lastValue
|
|
427 |
!
|
|
428 |
|
1
|
429 |
fileInNextChunkNotifying:someone
|
|
430 |
"read next chunk, evaluate it and return the result;
|
276
|
431 |
someone (which is usually some codeView) is notified of errors.
|
|
432 |
Filein is done as follows:
|
|
433 |
read a chunk
|
|
434 |
if it started with an excla, evaluate it, and let the resulting object
|
|
435 |
fileIn more chunks.
|
|
436 |
This is a nice trick, since the methodsFor: expression evaluates to
|
|
437 |
a ClassCategoryReader which reads and compiles chunks for its class.
|
|
438 |
However, other than methodsFor expressions are possible - you can
|
|
439 |
(in theory) create readers for any syntax.
|
|
440 |
"
|
1
|
441 |
|
282
|
442 |
^ self fileInNextChunkNotifying:someone passChunk:false
|
|
443 |
!
|
|
444 |
|
|
445 |
fileInNextChunkNotifying:someone passChunk:passChunk
|
|
446 |
"read next chunk, evaluate it and return the result;
|
|
447 |
someone (which is usually some codeView) is notified of errors.
|
|
448 |
Filein is done as follows:
|
|
449 |
read a chunk
|
|
450 |
if it started with an excla, evaluate it, and let the resulting object
|
|
451 |
fileIn more chunks.
|
|
452 |
This is a nice trick, since the methodsFor: expression evaluates to
|
|
453 |
a ClassCategoryReader which reads and compiles chunks for its class.
|
|
454 |
However, other than methodsFor expressions are possible - you can
|
|
455 |
(in theory) create readers for any syntax.
|
|
456 |
"
|
|
457 |
|
276
|
458 |
|aString sawExcla rslt done|
|
1
|
459 |
|
|
460 |
self skipSeparators.
|
|
461 |
self atEnd ifFalse:[
|
276
|
462 |
sawExcla := self peekFor:ChunkSeparator.
|
159
|
463 |
aString := self nextChunk.
|
|
464 |
aString size ~~ 0 ifTrue:[
|
282
|
465 |
passChunk ifTrue:[
|
|
466 |
someone source:aString
|
|
467 |
].
|
|
468 |
sawExcla ifFalse:[
|
|
469 |
rslt := Compiler evaluate:aString notifying:someone.
|
|
470 |
] ifTrue:[
|
|
471 |
rslt := Compiler evaluate:aString notifying:someone compile:false.
|
|
472 |
|
159
|
473 |
"
|
|
474 |
usually, the above chunk consists of some methodsFor:-expression
|
|
475 |
in this case, the returned value is a ClassCategoryReader,
|
|
476 |
which is used to load & compile the methods ...
|
|
477 |
"
|
253
|
478 |
rslt isNil ifTrue:[
|
|
479 |
"
|
|
480 |
however, if that was nil (i.e. some error), we skip chunks
|
|
481 |
up to the next empty chunk.
|
|
482 |
"
|
|
483 |
Transcript showCr:'skipping chunks ...'.
|
|
484 |
done := false.
|
|
485 |
[done] whileFalse:[
|
|
486 |
aString := self nextChunk.
|
276
|
487 |
done := (aString size == 0) or:[aString isEmpty].
|
253
|
488 |
]
|
|
489 |
] ifFalse:[
|
|
490 |
rslt := rslt fileInFrom:self notifying:someone
|
|
491 |
]
|
159
|
492 |
]
|
|
493 |
]
|
1
|
494 |
].
|
159
|
495 |
^ rslt
|
1
|
496 |
!
|
|
497 |
|
|
498 |
askForDebug:message
|
41
|
499 |
"launch a box asking if a debugger is wanted - used when an error
|
|
500 |
occurs while filing in"
|
|
501 |
|
180
|
502 |
Smalltalk isInitialized ifFalse:[
|
|
503 |
'error during startup: ' errorPrint. message errorPrintNL.
|
|
504 |
^ #debug
|
|
505 |
].
|
282
|
506 |
^ OptionBox
|
|
507 |
request:message
|
|
508 |
label:'Error in fileIn'
|
|
509 |
form:(WarningBox iconBitmap)
|
|
510 |
buttonLabels:#('abort' 'debug' 'continue')
|
|
511 |
values:#(#abort #debug #continue).
|
44
|
512 |
! !
|