Scanner.st
changeset 3 b63b8a6b71fb
parent 0 7ad01559b262
child 4 f6fd83437415
equal deleted inserted replaced
2:0aae80a0ae84 3:b63b8a6b71fb
    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
       
    20 			      saveComments currentComments'
    20           classVariableNames:'typeArray actionArray'
    21           classVariableNames:'typeArray actionArray'
    21             poolDictionaries:''
    22             poolDictionaries:''
    22                     category:'System-Compiler'
    23                     category:'System-Compiler'
    23 !
    24 !
    24 
    25 
    26 
    27 
    27 COPYRIGHT (c) 1989-93 by Claus Gittinger
    28 COPYRIGHT (c) 1989-93 by Claus Gittinger
    28              All Rights Reserved
    29              All Rights Reserved
    29 
    30 
    30 Scanner reads from a stream and returns individual smalltalk tokens
    31 Scanner reads from a stream and returns individual smalltalk tokens
    31 %W% %E%
    32 $Header: /cvs/stx/stx/libcomp/Scanner.st,v 1.2 1993-10-13 00:26:15 claus Exp $
    32 '!
    33 '!
    33 
    34 
    34 !Scanner class methodsFor:'instance creation'!
    35 !Scanner class methodsFor:'instance creation'!
    35 
    36 
    36 for:aStream
    37 for:aStream
    48     |block|
    49     |block|
    49 
    50 
    50     errorFlag := false.
    51     errorFlag := false.
    51     tokenLineNr := 1.
    52     tokenLineNr := 1.
    52     source := aStream.
    53     source := aStream.
       
    54     currentComments := nil.
       
    55     saveComments := false.
    53 
    56 
    54     actionArray isNil ifTrue:[
    57     actionArray isNil ifTrue:[
    55         actionArray := Array new:256.
    58         actionArray := Array new:256.
    56         typeArray := Array new:256.
    59         typeArray := Array new:256.
    57 
    60 
    88         actionArray at:($% asciiValue) put:[:s :char | s nextPrimitive].
    91         actionArray at:($% asciiValue) put:[:s :char | s nextPrimitive].
    89         actionArray at:($: asciiValue) put:[:s :char | s nextColonOrAssign]
    92         actionArray at:($: asciiValue) put:[:s :char | s nextColonOrAssign]
    90     ]
    93     ]
    91 !
    94 !
    92 
    95 
       
    96 initialize
       
    97     "prepare a scan"
       
    98 
       
    99     errorFlag := false.
       
   100     tokenLineNr := 1.
       
   101     currentComments := nil.
       
   102     saveComments := false.
       
   103 !
       
   104 
    93 notifying:anObject
   105 notifying:anObject
    94     "set the requestor to be notified"
   106     "set the requestor to be notified"
    95 
   107 
    96     requestor := anObject
   108     requestor := anObject
    97 !
       
    98 
       
    99 initialize
       
   100     "prepare a scan"
       
   101 
       
   102     errorFlag := false.
       
   103     tokenLineNr := 1
       
   104 !
   109 !
   105 
   110 
   106 backupPosition
   111 backupPosition
   107     "if reading from a stream, at the end we might have read
   112     "if reading from a stream, at the end we might have read
   108      one token too many"
   113      one token too many"
   182     ^ self warning:aMessage position:tokenPosition
   187     ^ self warning:aMessage position:tokenPosition
   183 ! !
   188 ! !
   184 
   189 
   185 !Scanner methodsFor:'reading next token'!
   190 !Scanner methodsFor:'reading next token'!
   186 
   191 
       
   192 skipComment
       
   193     |comment|
       
   194 
       
   195     comment := ''.
       
   196 
       
   197     source next.
       
   198     thisChar := source peek.
       
   199     [thisChar notNil and:[thisChar ~~ (Character doubleQuote)]] whileTrue:[
       
   200         thisChar == (Character cr) ifTrue:[
       
   201             tokenLineNr := tokenLineNr + 1.
       
   202         ].
       
   203 	saveComments ifTrue:[
       
   204 	    comment := comment copyWith:thisChar
       
   205 	].
       
   206         source next.
       
   207         thisChar := source peek
       
   208     ].
       
   209     saveComments ifTrue:[
       
   210         currentComments isNil ifTrue:[
       
   211 	    currentComments := OrderedCollection with:comment
       
   212         ] ifFalse:[
       
   213 	    currentComments add:comment
       
   214         ]
       
   215     ].
       
   216     "skip final dQuote"
       
   217     source next.
       
   218 !
       
   219 
   187 nextToken
   220 nextToken
   188     "return the next token from the source-stream"
   221     "return the next token from the source-stream"
   189 
   222 
   190     |skipping actionBlock|
   223     |skipping actionBlock comment|
   191 
   224 
   192     peekChar notNil ifTrue:[
   225     peekChar notNil ifTrue:[
   193         thisChar := peekChar.
   226         thisChar := peekChar.
   194         peekChar := nil
   227         peekChar := nil
   195     ] ifFalse:[
   228     ] ifFalse:[
   199             thisChar == (Character cr) ifTrue:[
   232             thisChar == (Character cr) ifTrue:[
   200                 tokenLineNr := tokenLineNr + 1.
   233                 tokenLineNr := tokenLineNr + 1.
   201                 source next
   234                 source next
   202             ] ifFalse:[
   235             ] ifFalse:[
   203                 thisChar == (Character doubleQuote) ifTrue:[
   236                 thisChar == (Character doubleQuote) ifTrue:[
   204                     source next.
   237 		    "start of a comment"
       
   238 
       
   239 		    self skipComment.
   205                     thisChar := source peek.
   240                     thisChar := source peek.
   206                     [thisChar notNil and:[thisChar ~~ (Character doubleQuote)]] whileTrue:[
       
   207                         thisChar == (Character cr) ifTrue:[
       
   208                             tokenLineNr := tokenLineNr + 1.
       
   209                         ].
       
   210                         source next.
       
   211                         thisChar := source peek
       
   212                     ].
       
   213                     source next.
       
   214                     thisChar := source peek.
       
   215 "
       
   216                     thisChar == (Character cr) ifTrue:[
       
   217                         tokenLineNr := tokenLineNr + 1.
       
   218                     ].
       
   219 "
       
   220                     "thisChar := source skipFor:(Character doubleQuote) "
       
   221                 ] ifFalse:[
   241                 ] ifFalse:[
   222                     skipping := false
   242                     skipping := false
   223                 ]
   243                 ]
   224             ]
   244             ]
   225         ].
   245         ].
   343         nextChar := source nextPeek.
   363         nextChar := source nextPeek.
   344         (nextChar notNil and:[nextChar isDigitRadix:radix]) ifTrue:[
   364         (nextChar notNil and:[nextChar isDigitRadix:radix]) ifTrue:[
   345             value := value asFloat + (self nextMantissa:radix).
   365             value := value asFloat + (self nextMantissa:radix).
   346             nextChar := source peek
   366             nextChar := source peek
   347         ] ifFalse:[
   367         ] ifFalse:[
       
   368             nextChar == (Character cr) ifTrue:[
       
   369                 tokenLineNr := tokenLineNr + 1.
       
   370             ].
   348             peekChar := $.
   371             peekChar := $.
   349         ]
   372         ]
   350     ].
   373     ].
   351     (nextChar == $e) ifTrue:[
   374     (nextChar == $e) ifTrue:[
   352         nextChar := source nextPeek.
   375         nextChar := source nextPeek.
   485 !
   508 !
   486 
   509 
   487 nextHash
   510 nextHash
   488     |nextChar string|
   511     |nextChar string|
   489 
   512 
   490     tokenType := #Symbol.
       
   491     nextChar := source nextPeek.
   513     nextChar := source nextPeek.
   492     nextChar notNil ifTrue:[
   514     nextChar notNil ifTrue:[
   493         nextChar isAlphaNumeric ifTrue:[
   515         nextChar isAlphaNumeric ifTrue:[
   494             string := ''.
   516             string := ''.
   495             [nextChar notNil and:[nextChar isAlphaNumeric]] whileTrue:[
   517             [nextChar notNil and:[nextChar isAlphaNumeric]] whileTrue:[
   496                 string := string , (source nextWord "self nextId").
   518                 string := string , (source nextWord "self nextId").
   497                 nextChar := source peek.
   519                 nextChar := source peek.
   498                 (nextChar == $:) ifFalse:[
   520                 (nextChar == $:) ifFalse:[
   499                     tokenValue := string asSymbol.
   521                     tokenValue := string asSymbol.
   500                     ^ self
   522             	    tokenType := #Symbol.
       
   523                     ^ tokenType
   501                 ].
   524                 ].
   502                 string := string copyWith:nextChar.
   525                 string := string copyWith:nextChar.
   503                 nextChar := source nextPeek
   526                 nextChar := source nextPeek
   504             ].
   527             ].
   505             tokenValue := string asSymbol.
   528             tokenValue := string asSymbol.
       
   529             tokenType := #Symbol.
   506             ^ tokenType
   530             ^ tokenType
   507         ].
   531         ].
   508         (nextChar == $( ) ifTrue:[
   532         (nextChar == $( ) ifTrue:[
   509             source next.
   533             source next.
   510             tokenType := #HashLeftParen.
   534             tokenType := #HashLeftParen.
   533                     source next.
   557                     source next.
   534                     string := string copyWith:nextChar
   558                     string := string copyWith:nextChar
   535                 ]
   559                 ]
   536             ].
   560             ].
   537             tokenValue := string asSymbol.
   561             tokenValue := string asSymbol.
       
   562             tokenType := #Symbol.
   538             ^ tokenType
   563             ^ tokenType
   539         ]
   564         ]
   540     ].
   565     ].
       
   566     "this allows hash to be used as binop -
       
   567      I dont know, if this is correct ..."
       
   568 
       
   569     tokenName := '#'.
       
   570     tokenType := BinaryOperator.
       
   571     ^ tokenType
       
   572 "
   541     self syntaxError:'unexpected end-of-input in Symbol'
   573     self syntaxError:'unexpected end-of-input in Symbol'
   542             position:tokenPosition to:(tokenPosition + 1).
   574             position:tokenPosition to:(tokenPosition + 1).
   543     ^ #Error
   575     ^ #Error
       
   576 "
   544 !
   577 !
   545 
   578 
   546 nextString
   579 nextString
   547     |nextChar string pos
   580     |nextChar string pos
   548      index "{ Class: SmallInteger }"
   581      index "{ Class: SmallInteger }"