PositionableStream.st
author Stefan Vogel <sv@exept.de>
Thu, 14 Dec 1995 23:42:02 +0100
changeset 757 93d5f6b86e98
parent 718 6f8222ff2ff0
child 759 908363ce8a32
permissions -rw-r--r--
Add SemaphoreSet.

"
 COPYRIGHT (c) 1989 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

PeekableStream subclass:#PositionableStream
	 instanceVariableNames:'collection position readLimit writeLimit'
	 classVariableNames:'ErrorDuringFileInSignal ChunkSeparator'
	 poolDictionaries:''
	 category:'Streams'
!

!PositionableStream class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1989 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

documentation
"
    Instances of PositionableStream allow positioning the read pointer.
    The PositionableStream class also adds methods for source-chunk reading
    and writing, and for filing-in/out of source code.
    This is an abstract class.
"
! !

!PositionableStream class methodsFor:'initialization'!

initialize
    "setup the signal used to handle errors during fileIn"

    ErrorDuringFileInSignal isNil ifTrue:[
	ErrorDuringFileInSignal := ErrorSignal newSignalMayProceed:true.
	ErrorDuringFileInSignal nameClass:self message:#errorDuringFileInSignal.
	ErrorDuringFileInSignal notifierString:'error during fileIn'.

	ChunkSeparator := $!!
    ]
! !

!PositionableStream class methodsFor:'instance creation'!

on:aCollection
    "return a new PositionableStream streaming on aCollection"

    ^ (self basicNew) on:aCollection
!

on:aCollection from:first to:last
    "return a new PositionableStream streaming on aCollection
     from first to last"

    ^ (self basicNew) on:aCollection from:first to:last
!

with:aCollection
    "return a new PositionableStream streaming on aCollection,
     the stream is positioned to the end of the collection."

    ^ (self basicNew) with:aCollection
! !

!PositionableStream class methodsFor:'constants'!

chunkSeparator
    "return the chunk-separation character"

    ^ ChunkSeparator
! !

!PositionableStream methodsFor:'accessing'!

contents
    "return the entire contents of the stream"

    ^ collection
!

peek
    "look ahead for and return the next element"

    |peekObject|

    peekObject := self next.
    self backStep.
    ^ peekObject
!

peekFor:something
    "return true and move past if next == something; 
     otherwise stay and let position unchanged"

    self next == something ifTrue:[
	^ true
    ].
    self backStep.
    ^ false
!

readLimit:aNumber
    "set the read-limit; thats the position at which EOF is returned"

    readLimit := aNumber
!

writeLimit:aNumber
    "set the writeLimit; thats the position after which writing is prohibited"

    writeLimit := aNumber
! !

!PositionableStream methodsFor:'chunk input/output'!

nextChunk
    "return the next chunk, i.e. all characters up to the next
     exclamation mark. Within the chunk, exclamation marks have to be doubled,
     they are undoubled here.
     Except for primitive code, in which doubling is not needed (allowed).
     This exception was added to make it easier to edit primitive code with 
     external editors. However, this means, that other Smalltalks cannot always 
     read chunks containing primitive code 
     - but that doesnt really matter, since C-primitives are an ST/X feature anyway."

    |theString sep newString done thisChar nextChar inPrimitive
     index    "{ Class:SmallInteger }"
     currSize "{ Class:SmallInteger }" |

    sep := ChunkSeparator.
    theString := String new:500.
    currSize := 500.
    thisChar := self skipSeparators.
    thisChar := self next.
    index := 0.
    done := false.
    inPrimitive := false.

    [done] whileFalse:[
	((index + 2) <= currSize) ifFalse:[
	    newString := String new:(currSize * 2).
	    newString replaceFrom:1 to:currSize with:theString.
	    currSize := currSize * 2.
	    theString := newString
	].
	thisChar isNil ifTrue:[
	    done := true
	] ifFalse:[
	    (thisChar == $% ) ifTrue:[
		nextChar := self peek.
		(nextChar == ${ ) ifTrue:[
		    inPrimitive := true.
		    index := index + 1.
		    theString at:index put:thisChar.
		    thisChar := self next
		] ifFalse:[
		    (nextChar == $} ) ifTrue:[
			inPrimitive := false.
			index := index + 1.
			theString at:index put:thisChar.
			thisChar := self next
		    ]
		]
	    ] ifFalse:[
		inPrimitive ifFalse:[
		    (thisChar == sep) ifTrue:[
			(self peek == sep) ifFalse:[
			    done := true
			] ifTrue:[
			    self next
			]
		    ]
		]
	    ]
	].
	done ifFalse:[
	    index := index + 1.
	    theString at:index put:thisChar.
	    thisChar := self next
	]
    ].
    (index == 0) ifTrue:[^ ''].
    ^ theString copyTo:index
!

nextChunkPut:aString
    "put aString as a chunk onto the receiver;
     double all exclamation marks except within primitives and append a 
     single delimiting exclamation mark at the end.
     This modification of the chunk format (not doubling exclas in primitive code)
     was done to have primitive code more readable and easier be edited in the fileBrowser
     or other editors.
     Its no incompatibility, since inline primitives are an ST/X special
     and code containing ST/X primitives cannot be loaded into other smalltalks anyway."

    self nextPutAllAsChunk:aString.
    self nextPut:ChunkSeparator

    "Modified: 9.12.1995 / 15:56:54 / cg"
!

nextPutAllAsChunk:aString
    "put aString as a chunk onto the receiver;
     double all exclamation marks except within primitives.
     This modification of the chunk format (not doubling exclas in primitive code)
     was done to have primitive code more readable and easier be edited in the fileBrowser
     or other editors.
     Its no incompatibility, since inline primitives are an ST/X special
     and code containing ST/X primitives cannot be loaded into other smalltalks anyway."

    |sep stopChars inPrimitive character
     index    "{ Class:SmallInteger }"
     endIndex "{ Class:SmallInteger }"
     stop     "{ Class:SmallInteger }"
     next     "{ Class:SmallInteger }"|

    sep := ChunkSeparator.
    stopChars := '{}' copyWith:sep.

    inPrimitive := false.
    index := 1.
    endIndex := aString size.
    stop := endIndex + 1.

    [index <= endIndex] whileTrue:[
        "
         find position of next interresting character; 
         output stuff up to that one in one piece
        "
        next := aString indexOfAny:stopChars startingAt:index ifAbsent:stop.

        ((index == 1) and:[next == stop]) ifTrue:[
            self nextPutAll:aString
        ] ifFalse:[
            self nextPutAll:aString startingAt:index to:(next - 1)
        ].

        index := next.
        (index <= endIndex) ifTrue:[
            character := aString at:index.

            (character == ${ ) ifTrue:[
                "/ starts a primitive
                ((index > 1) and:[(aString at:index-1) == $%]) ifTrue:[
                    inPrimitive := true
                ]
            ] ifFalse:[
                "/ ends a primitive
                (character == $} ) ifTrue:[
                    ((index > 1) and:[(aString at:index-1) == $%]) ifTrue:[
                        inPrimitive := false
                    ]
                ] ifFalse:[
                    "/
                    "/ exclas have to be doubled - except if within a primitive
                    "/
                    inPrimitive ifFalse:[
                        (character == sep) ifTrue:[
                            self nextPut:sep
                        ]
                    ]
                ]
            ].

            self nextPut:character.
            index := index + 1
        ]
    ].

    "Modified: 9.12.1995 / 15:56:47 / cg"
!

nextPutChunkSeparator
    "append a chunk separator character"

    self nextPut:ChunkSeparator

    "Created: 13.9.1995 / 17:39:26 / claus"
! !

!PositionableStream methodsFor:'fileIn'!

askForDebug:message
    "launch a box asking if a debugger is wanted - used when an error
     occurs while filing in"

    Smalltalk isInitialized ifFalse:[
	'error during startup: ' errorPrint. message errorPrintNL.
	^ #debug
    ].
    ^ OptionBox 
	  request:message 
	  label:'Error in fileIn'
	  form:(WarningBox iconBitmap)
	  buttonLabels:#('abort' 'debug' 'continue')
	  values:#(#abort #debug #continue)
	  default:#continue.
!

fileIn
    "file in from the receiver, i.e. read chunks and evaluate them -
     return the value of the last chunk."

    ^ self fileInNotifying:(SourceFileLoader on:self) passChunk:true
!

fileInNextChunkNotifying:someone
    "read next chunk, evaluate it and return the result;
     someone (which is usually some codeView) is notified of errors.
     Filein is done as follows:
	read a chunk
	if it started with an excla, evaluate it, and let the resulting object
	fileIn more chunks.
	This is a nice trick, since the methodsFor: expression evaluates to
	a ClassCategoryReader which reads and compiles chunks for its class.
	However, other than methodsFor expressions are possible - you can
	(in theory) create readers for any syntax.
    "

    ^ self fileInNextChunkNotifying:someone passChunk:false
!

fileInNextChunkNotifying:someone passChunk:passChunk
    "read next chunk, evaluate it and return the result;
     someone (which is usually some codeView) is notified of errors.
     Filein is done as follows:
	read a chunk
	if it started with an excla, evaluate it, and let the resulting object
	fileIn more chunks.
	This is a nice trick, since the methodsFor: expression evaluates to
	a ClassCategoryReader which reads and compiles chunks for its class.
	However, other than methodsFor expressions are possible - you can
	(in theory) create readers for any syntax.
    "

    |aString sawExcla rslt done|

    self skipSeparators.
    self atEnd ifFalse:[
	sawExcla := self peekFor:ChunkSeparator.
	aString := self nextChunk.
	aString size ~~ 0 ifTrue:[
	    passChunk ifTrue:[
		someone source:aString
	    ].
	    sawExcla ifFalse:[
		rslt := Compiler evaluate:aString notifying:someone.
	    ] ifTrue:[
		rslt := Compiler evaluate:aString notifying:someone compile:false.

		"
		 usually, the above chunk consists of some methodsFor:-expression
		 in this case, the returned value is a ClassCategoryReader,
		 which is used to load & compile the methods ...
		"
		rslt isNil ifTrue:[
		    "
		     however, if that was nil (i.e. some error), we skip chunks
		     up to the next empty chunk.
		    "
		    Transcript showCr:'skipping chunks ...'.
		    done := false.
		    [done] whileFalse:[
			aString := self nextChunk.
			done := (aString size == 0) or:[aString isEmpty].
		    ]
		] ifFalse:[
		    rslt := rslt fileInFrom:self notifying:someone  passChunk:passChunk
		]
	    ]
	]
    ].
    ^ rslt
!

fileInNotifying:someone passChunk:passChunk
    "file in from the receiver, i.e. read chunks and evaluate them -
     return the value of the last chunk.
     Someone (which is usually some codeView) is notified of errors."

    |lastValue|

    "
     catch any errors during fileIn 
     - offer debug/abort/continue choice
    "
    Object errorSignal handle:[:ex |
	|action what sender|

	what := ex errorString.
	what isNil ifTrue:[
	    what := ex signal notifierString.
	].

	"handle the case where no GUI has been built in,
	 just abort the fileIn with a notification"

	Display isNil ifTrue:[
	    sender := ex suspendedContext sender.
	    self notify:(what , 
			 ' in ' , sender receiver class name ,
			 '>>>' , sender selector).
	    ex return
	].

	"otherwise ask what should be done now and either
	 continue or abort the fileIn"

	action := self askForDebug:('error in fileIn: ' , what) withCRs.
	action == #continue ifTrue:[
	    ex proceed
	].
	action == #abort ifTrue:[
	    ex return
	].
"/        Debugger enter:ex suspendedContext
	(ex signal) enterDebuggerWith:ex message:what.
	ex reject
    ] do:[
	[self atEnd] whileFalse:[
	    lastValue := self fileInNextChunkNotifying:someone passChunk:passChunk
	]
    ].
    ^ lastValue
! !

!PositionableStream methodsFor:'positioning'!

backStep
    "move backward read position by one"

    position <= 0 ifTrue: [^ self positionError].
    position := position - 1
!

position
    "return the read position"

    ^ position
!

position:index
    "set the read position"

    "/ FIX: allow positioning right after last element of stream
    "/ ((index > readLimit) or:[index < 0]) ifTrue: [^ self positionError].

    ((index > (readLimit+1)) or:[index < 0]) ifTrue: [^ self positionError].
    position := index
!

reset
    "set the read position to the beginning of the collection"

    position := "0" 1
!

setToEnd
    "set the read position to the end of the collection"

    position := readLimit
!

skip:numberToSkip
    "skip the next numberToSkip elements"

    self position:(position + numberToSkip)
! !

!PositionableStream methodsFor:'private'!

contentsSpecies
    "return a class of which instances will be returned, when
     parts of the collection are asked for. 
     (see upTo-kind of methods in subclasses)"

    ^ collection species
!

on:aCollection
    "setup for streaming on aCollection"

    collection := aCollection.
    readLimit := aCollection size.
    position := "0" 1
!

on:aCollection from:first to:last
    "setup for streaming on aCollection from first to last"

    collection := aCollection.
    position := first.
    readLimit := last
!

positionError
    "report an error when positioning past the end"

    ^ self error:'cannot position past end of collection'
!

with:aCollection
    "setup for streaming to the end of aCollection"

    collection := aCollection.
    self setToEnd
! !

!PositionableStream methodsFor:'queries'!

isPositionable
    "return true, if the stream supports positioning (this one is)"

    ^ true
! !

!PositionableStream methodsFor:'testing'!

atEnd
    "return true, if the read-position is at the end"

    ^ position > readLimit
!

isEmpty
    "return true, if the contents of the stream is empty"

    ^ readLimit == 0
! !

!PositionableStream class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic/PositionableStream.st,v 1.36 1995-12-09 14:57:29 cg Exp $'
! !
PositionableStream initialize!