Scanner.st
changeset 7 6c2bc76f0b8f
parent 4 f6fd83437415
child 10 73e97b6175c4
equal deleted inserted replaced
6:0cd4e7480440 7:6c2bc76f0b8f
    14        instanceVariableNames:'source 
    14        instanceVariableNames:'source 
    15                               token tokenType tokenPosition tokenValue
    15                               token tokenType tokenPosition tokenValue
    16                               tokenName tokenLineNr
    16                               tokenName tokenLineNr
    17                               thisChar peekChar
    17                               thisChar peekChar
    18                               requestor exitBlock
    18                               requestor exitBlock
    19                               errorFlag
    19                               errorFlag ignoreErrors
    20 			      saveComments currentComments'
    20                               saveComments currentComments'
    21           classVariableNames:'typeArray actionArray'
    21           classVariableNames:'typeArray actionArray'
    22             poolDictionaries:''
    22             poolDictionaries:''
    23                     category:'System-Compiler'
    23                     category:'System-Compiler'
    24 !
    24 !
    25 
    25 
    27 
    27 
    28 COPYRIGHT (c) 1989 by Claus Gittinger
    28 COPYRIGHT (c) 1989 by Claus Gittinger
    29              All Rights Reserved
    29              All Rights Reserved
    30 
    30 
    31 Scanner reads from a stream and returns individual smalltalk tokens
    31 Scanner reads from a stream and returns individual smalltalk tokens
    32 $Header: /cvs/stx/stx/libcomp/Scanner.st,v 1.3 1993-10-13 02:41:45 claus Exp $
    32 $Header: /cvs/stx/stx/libcomp/Scanner.st,v 1.4 1993-12-11 01:09:49 claus Exp $
    33 '!
    33 '!
    34 
    34 
    35 !Scanner class methodsFor:'instance creation'!
    35 !Scanner class methodsFor:'instance creation'!
    36 
    36 
    37 for:aStream
    37 for:aStream
    51     errorFlag := false.
    51     errorFlag := false.
    52     tokenLineNr := 1.
    52     tokenLineNr := 1.
    53     source := aStream.
    53     source := aStream.
    54     currentComments := nil.
    54     currentComments := nil.
    55     saveComments := false.
    55     saveComments := false.
       
    56     ignoreErrors := false.
    56 
    57 
    57     actionArray isNil ifTrue:[
    58     actionArray isNil ifTrue:[
    58         actionArray := Array new:256.
    59         actionArray := Array new:256.
    59         typeArray := Array new:256.
    60         typeArray := Array new:256.
    60 
    61 
    98 
    99 
    99     errorFlag := false.
   100     errorFlag := false.
   100     tokenLineNr := 1.
   101     tokenLineNr := 1.
   101     currentComments := nil.
   102     currentComments := nil.
   102     saveComments := false.
   103     saveComments := false.
       
   104     ignoreErrors := false.
   103 !
   105 !
   104 
   106 
   105 notifying:anObject
   107 notifying:anObject
   106     "set the requestor to be notified"
   108     "set the requestor to be notified"
   107 
   109 
   108     requestor := anObject
   110     requestor := anObject
       
   111 !
       
   112 
       
   113 ignoreErrors
       
   114     "turn off notification of errors"
       
   115 
       
   116     ignoreErrors := true
   109 !
   117 !
   110 
   118 
   111 backupPosition
   119 backupPosition
   112     "if reading from a stream, at the end we might have read
   120     "if reading from a stream, at the end we might have read
   113      one token too many"
   121      one token too many"
   120 !Scanner methodsFor:'error handling'!
   128 !Scanner methodsFor:'error handling'!
   121 
   129 
   122 showErrorMessage:aMessage position:pos
   130 showErrorMessage:aMessage position:pos
   123     "show an errormessage on the Transcript"
   131     "show an errormessage on the Transcript"
   124 
   132 
   125     Transcript showCr:(pos printString , ' ' , aMessage)
   133     ignoreErrors ifFalse:[
       
   134         Transcript showCr:(pos printString , ' ' , aMessage)
       
   135     ]
   126 !
   136 !
   127 
   137 
   128 notifyError:aMessage position:position to:endPos
   138 notifyError:aMessage position:position to:endPos
   129     "notify requestor of an error - if there is no requestor
   139     "notify requestor of an error - if there is no requestor
   130      put it on the transcript.
   140      put it on the transcript.
   132 
   142 
   133     requestor isNil ifTrue:[
   143     requestor isNil ifTrue:[
   134         self showErrorMessage:aMessage position:position.
   144         self showErrorMessage:aMessage position:position.
   135         ^ false
   145         ^ false
   136     ].
   146     ].
       
   147 
   137     ^ requestor error:aMessage position:position to:endPos
   148     ^ requestor error:aMessage position:position to:endPos
   138 !
   149 !
   139 
   150 
   140 notifyWarning:aMessage position:position to:endPos
   151 notifyWarning:aMessage position:position to:endPos
   141     "notify requestor of an warning - if there is no requestor
   152     "notify requestor of an warning - if there is no requestor
   188 ! !
   199 ! !
   189 
   200 
   190 !Scanner methodsFor:'reading next token'!
   201 !Scanner methodsFor:'reading next token'!
   191 
   202 
   192 skipComment
   203 skipComment
   193     |comment|
   204     |comment startPos|
   194 
   205 
   195     comment := ''.
   206     comment := ''.
   196 
   207 
       
   208     startPos := source position.
   197     source next.
   209     source next.
   198     thisChar := source peek.
   210     thisChar := source peek.
   199     [thisChar notNil and:[thisChar ~~ (Character doubleQuote)]] whileTrue:[
   211     [thisChar notNil and:[thisChar ~~ (Character doubleQuote)]] whileTrue:[
   200         thisChar == (Character cr) ifTrue:[
   212         thisChar == (Character cr) ifTrue:[
   201             tokenLineNr := tokenLineNr + 1.
   213             tokenLineNr := tokenLineNr + 1.
   202         ].
   214         ].
   203 	saveComments ifTrue:[
   215         saveComments ifTrue:[
   204 	    comment := comment copyWith:thisChar
   216             comment := comment copyWith:thisChar
   205 	].
   217         ].
   206         source next.
   218         source next.
   207         thisChar := source peek
   219         thisChar := source peek
   208     ].
   220     ].
   209     saveComments ifTrue:[
   221     saveComments ifTrue:[
   210         currentComments isNil ifTrue:[
   222         currentComments isNil ifTrue:[
   211 	    currentComments := OrderedCollection with:comment
   223             currentComments := OrderedCollection with:comment
   212         ] ifFalse:[
   224         ] ifFalse:[
   213 	    currentComments add:comment
   225             currentComments add:comment
   214         ]
   226         ]
       
   227     ].
       
   228 
       
   229     thisChar isNil ifTrue:[
       
   230         self warning:'unclosed comment' position:startPos to:(source position)
   215     ].
   231     ].
   216     "skip final dQuote"
   232     "skip final dQuote"
   217     source next.
   233     source next.
   218 !
   234 !
   219 
   235 
   232             thisChar == (Character cr) ifTrue:[
   248             thisChar == (Character cr) ifTrue:[
   233                 tokenLineNr := tokenLineNr + 1.
   249                 tokenLineNr := tokenLineNr + 1.
   234                 source next
   250                 source next
   235             ] ifFalse:[
   251             ] ifFalse:[
   236                 thisChar == (Character doubleQuote) ifTrue:[
   252                 thisChar == (Character doubleQuote) ifTrue:[
   237 		    "start of a comment"
   253                     "start of a comment"
   238 
   254 
   239 		    self skipComment.
   255                     self skipComment.
   240                     thisChar := source peek.
   256                     thisChar := source peek.
   241                 ] ifFalse:[
   257                 ] ifFalse:[
   242                     skipping := false
   258                     skipping := false
   243                 ]
   259                 ]
   244             ]
   260             ]
   517             [nextChar notNil and:[nextChar isAlphaNumeric]] whileTrue:[
   533             [nextChar notNil and:[nextChar isAlphaNumeric]] whileTrue:[
   518                 string := string , (source nextWord "self nextId").
   534                 string := string , (source nextWord "self nextId").
   519                 nextChar := source peek.
   535                 nextChar := source peek.
   520                 (nextChar == $:) ifFalse:[
   536                 (nextChar == $:) ifFalse:[
   521                     tokenValue := string asSymbol.
   537                     tokenValue := string asSymbol.
   522             	    tokenType := #Symbol.
   538                     tokenType := #Symbol.
   523                     ^ tokenType
   539                     ^ tokenType
   524                 ].
   540                 ].
   525                 string := string copyWith:nextChar.
   541                 string := string copyWith:nextChar.
   526                 nextChar := source nextPeek
   542                 nextChar := source nextPeek
   527             ].
   543             ].