Scanner.st
changeset 20 f8dd8ba75205
parent 17 f06d70d785dc
child 30 26e2f849916a
equal deleted inserted replaced
19:84a1ddf215a5 20:f8dd8ba75205
    16                               tokenName tokenLineNr
    16                               tokenName tokenLineNr
    17                               thisChar peekChar
    17                               thisChar peekChar
    18                               requestor exitBlock
    18                               requestor exitBlock
    19                               errorFlag 
    19                               errorFlag 
    20                               ignoreErrors ignoreWarnings
    20                               ignoreErrors ignoreWarnings
    21                               saveComments currentComments'
    21                               saveComments currentComments
       
    22                               warnSTXSpecialComment
       
    23                               outStream outCol'
    22           classVariableNames:'typeArray ActionArray'
    24           classVariableNames:'typeArray ActionArray'
    23             poolDictionaries:''
    25             poolDictionaries:''
    24                     category:'System-Compiler'
    26                     category:'System-Compiler'
    25 !
    27 !
    26 
    28 
    27 Scanner comment:'
    29 Scanner comment:'
    28 
       
    29 COPYRIGHT (c) 1989 by Claus Gittinger
    30 COPYRIGHT (c) 1989 by Claus Gittinger
    30              All Rights Reserved
    31              All Rights Reserved
    31 
       
    32 Scanner reads from a stream and returns individual smalltalk tokens
       
    33 $Header: /cvs/stx/stx/libcomp/Scanner.st,v 1.7 1994-03-30 10:07:33 claus Exp $
       
    34 '!
    32 '!
       
    33 
       
    34 !Scanner class methodsFor:'documentation'!
       
    35 
       
    36 copyright
       
    37 "
       
    38  COPYRIGHT (c) 1989 by Claus Gittinger
       
    39               All Rights Reserved
       
    40 
       
    41  This software is furnished under a license and may be used
       
    42  only in accordance with the terms of that license and with the
       
    43  inclusion of the above copyright notice.   This software may not
       
    44  be provided or otherwise made available to, or used by, any
       
    45  other person.  No title to or ownership of the software is
       
    46  hereby transferred.
       
    47 "
       
    48 !
       
    49 
       
    50 version
       
    51 "
       
    52 $Header: /cvs/stx/stx/libcomp/Scanner.st,v 1.8 1994-06-02 20:26:16 claus Exp $
       
    53 "
       
    54 !
       
    55 
       
    56 documentation
       
    57 "
       
    58     Scanner reads from a stream and returns individual smalltalk tokens
       
    59 "
       
    60 ! !
    35 
    61 
    36 !Scanner class methodsFor:'instance creation'!
    62 !Scanner class methodsFor:'instance creation'!
    37 
    63 
    38 for:aStream
    64 for:aStream
    39     "return a new scanner reading from aStream"
    65     "return a new scanner reading from aStream"
    54     source := aStream.
    80     source := aStream.
    55     currentComments := nil.
    81     currentComments := nil.
    56     saveComments := false.
    82     saveComments := false.
    57     ignoreErrors := false.
    83     ignoreErrors := false.
    58     ignoreWarnings := false.
    84     ignoreWarnings := false.
       
    85     warnSTXSpecialComment := true.
    59 
    86 
    60     ActionArray isNil ifTrue:[
    87     ActionArray isNil ifTrue:[
    61         ActionArray := Array new:256.
    88         ActionArray := Array new:256.
    62         TypeArray := Array new:256.
    89         TypeArray := Array new:256.
    63 
    90 
   103     tokenLineNr := 1.
   130     tokenLineNr := 1.
   104     currentComments := nil.
   131     currentComments := nil.
   105     saveComments := false.
   132     saveComments := false.
   106     ignoreErrors := false.
   133     ignoreErrors := false.
   107     ignoreWarnings := false.
   134     ignoreWarnings := false.
       
   135     warnSTXSpecialComment := true
   108 !
   136 !
   109 
   137 
   110 notifying:anObject
   138 notifying:anObject
   111     "set the requestor to be notified"
   139     "set the requestor to be notified"
   112 
   140 
   130      one token too many"
   158      one token too many"
   131 
   159 
   132     (tokenType == #EOF) ifFalse:[
   160     (tokenType == #EOF) ifFalse:[
   133         source position:tokenPosition
   161         source position:tokenPosition
   134     ]
   162     ]
       
   163 !
       
   164 
       
   165 beginComment
       
   166     ^ self
       
   167 !
       
   168 
       
   169 endComment:comment
       
   170     saveComments ifTrue:[
       
   171         currentComments isNil ifTrue:[
       
   172             currentComments := OrderedCollection with:comment
       
   173         ] ifFalse:[
       
   174             currentComments add:comment
       
   175         ]
       
   176     ].
   135 ! !
   177 ! !
   136 
   178 
   137 !Scanner methodsFor:'error handling'!
   179 !Scanner methodsFor:'error handling'!
   138 
   180 
   139 showErrorMessage:aMessage position:pos
   181 showErrorMessage:aMessage position:pos
   259 skipComment
   301 skipComment
   260     |comment startPos|
   302     |comment startPos|
   261 
   303 
   262     comment := ''.
   304     comment := ''.
   263 
   305 
       
   306     self beginComment.
       
   307     outStream notNil ifTrue:[
       
   308         outStream nextPut:Character doubleQuote.
       
   309         outCol := outCol + 1
       
   310     ].
       
   311 
   264     startPos := source position.
   312     startPos := source position.
   265     source next.
   313     source next.
   266     thisChar := source peek.
   314     thisChar := source peek.
   267 
   315 
   268     "special ST/X addition:
   316     "special ST/X addition:
   275     thisChar == $/ ifTrue:[
   323     thisChar == $/ ifTrue:[
   276         [thisChar notNil and:[thisChar ~~ Character cr]] whileTrue:[
   324         [thisChar notNil and:[thisChar ~~ Character cr]] whileTrue:[
   277             saveComments ifTrue:[
   325             saveComments ifTrue:[
   278                 comment := comment copyWith:thisChar
   326                 comment := comment copyWith:thisChar
   279             ].
   327             ].
       
   328             outStream notNil ifTrue:[
       
   329                 outStream nextPut:thisChar.
       
   330                 outCol := outCol + 1
       
   331             ].
   280             thisChar := source nextPeek.
   332             thisChar := source nextPeek.
   281         ].
   333         ].
   282         tokenLineNr := tokenLineNr + 1.
   334         tokenLineNr := tokenLineNr + 1.
   283         self warning:'end-of-line comments are a nonstandard feature of ST/X' 
   335         warnSTXSpecialComment ifTrue:[
   284             position:startPos
   336             self warning:'end-of-line comments are a nonstandard feature of ST/X' 
   285                   to:(source position)
   337                  position:startPos to:(source position).
       
   338             "
       
   339              only warn once
       
   340             "
       
   341             warnSTXSpecialComment := false
       
   342         ].
       
   343         outStream notNil ifTrue:[
       
   344             outStream cr.
       
   345             outCol := 1
       
   346         ].
   286     ] ifFalse:[
   347     ] ifFalse:[
   287         [thisChar notNil and:[thisChar ~~ (Character doubleQuote)]] whileTrue:[
   348         [thisChar notNil and:[thisChar ~~ (Character doubleQuote)]] whileTrue:[
   288             thisChar == (Character cr) ifTrue:[
   349             thisChar == (Character cr) ifTrue:[
   289                 tokenLineNr := tokenLineNr + 1.
   350                 tokenLineNr := tokenLineNr + 1.
   290             ].
   351             ].
   291             saveComments ifTrue:[
   352             saveComments ifTrue:[
   292                 comment := comment copyWith:thisChar
   353                 comment := comment copyWith:thisChar
   293             ].
   354             ].
       
   355             outStream notNil ifTrue:[
       
   356                 outStream nextPut:thisChar.
       
   357                 outCol := outCol + 1
       
   358             ].
   294             thisChar := source nextPeek
   359             thisChar := source nextPeek
   295         ].
   360         ].
   296         thisChar isNil ifTrue:[
   361         thisChar isNil ifTrue:[
   297             self warning:'unclosed comment' position:startPos to:(source position)
   362             self warning:'unclosed comment' position:startPos to:(source position)
   298         ].
   363         ] ifFalse:[
       
   364             outStream notNil ifTrue:[
       
   365                 outStream nextPut:(Character doubleQuote).
       
   366                 outCol := outCol + 1
       
   367             ].
       
   368         ]
   299     ].
   369     ].
   300 
   370 
   301     saveComments ifTrue:[
   371     saveComments ifTrue:[
   302         currentComments isNil ifTrue:[
   372         currentComments isNil ifTrue:[
   303             currentComments := OrderedCollection with:comment
   373             currentComments := OrderedCollection with:comment
   304         ] ifFalse:[
   374         ] ifFalse:[
   305             currentComments add:comment
   375             currentComments add:comment
   306         ]
   376         ]
   307     ].
   377     ].
   308 
   378 
   309     "skip final dQuote"
   379     "skip final dQuote or cr"
   310     source next.
   380     source next.
       
   381 
       
   382     self endComment:comment.
   311 !
   383 !
   312 
   384 
   313 nextToken
   385 nextToken
   314     "return the next token from the source-stream"
   386     "return the next token from the source-stream"
   315 
   387 
   319         thisChar := peekChar.
   391         thisChar := peekChar.
   320         peekChar := nil
   392         peekChar := nil
   321     ] ifFalse:[
   393     ] ifFalse:[
   322         skipping := true.
   394         skipping := true.
   323         [skipping] whileTrue:[
   395         [skipping] whileTrue:[
   324             thisChar := source skipSeparatorsExceptCR.
   396             outStream notNil ifTrue:[
       
   397                 [(thisChar := source peek) == Character space] whileTrue:[
       
   398                     source next.
       
   399                     outStream space. 
       
   400                     outCol := outCol + 1.
       
   401                 ]
       
   402             ] ifFalse:[
       
   403                 thisChar := source skipSeparatorsExceptCR.
       
   404             ].
   325             thisChar == (Character cr) ifTrue:[
   405             thisChar == (Character cr) ifTrue:[
   326                 tokenLineNr := tokenLineNr + 1.
   406                 tokenLineNr := tokenLineNr + 1.
   327                 source next
   407                 source next.
       
   408                 outStream notNil ifTrue:[
       
   409                     outStream cr.
       
   410                     outCol := 1
       
   411                 ]
   328             ] ifFalse:[
   412             ] ifFalse:[
   329                 thisChar == (Character doubleQuote) ifTrue:[
   413                 thisChar == (Character doubleQuote) ifTrue:[
   330                     "start of a comment"
   414                     "start of a comment"
   331 
   415 
   332                     self skipComment.
   416                     self skipComment.
   387             tokenValue := tokenValue negated.
   471             tokenValue := tokenValue negated.
   388             ^ tokenType
   472             ^ tokenType
   389         ]
   473         ]
   390     ].
   474     ].
   391     string := firstChar asString.
   475     string := firstChar asString.
   392     ((TypeArray at:(secondChar asciiValue)) == #special) ifTrue:[
   476     secondChar notNil ifTrue:[
   393         (secondChar == $-) ifTrue:[
   477         ((TypeArray at:(secondChar asciiValue)) == #special) ifTrue:[
   394             "special- look if minus belongs to number following"
   478             (secondChar == $-) ifTrue:[
   395             p := source position.
   479                 "special- look if minus belongs to number following"
       
   480                 p := source position.
       
   481                 source next.
       
   482                 thirdChar := source peek.
       
   483                 source position:p.
       
   484                 thirdChar isDigit ifTrue:[
       
   485                     tokenName := string.
       
   486                     tokenType := #BinaryOperator.
       
   487                     ^ tokenType
       
   488                 ]
       
   489             ].
   396             source next.
   490             source next.
   397             thirdChar := source peek.
   491             string := string copyWith:secondChar
   398             source position:p.
   492         ].
   399             thirdChar isDigit ifTrue:[
       
   400                 tokenName := string.
       
   401                 tokenType := #BinaryOperator.
       
   402                 ^ tokenType
       
   403             ]
       
   404         ].
       
   405         source next.
       
   406         string := string copyWith:secondChar
       
   407     ].
   493     ].
   408     tokenName := string.
   494     tokenName := string.
   409     tokenType := #BinaryOperator.
   495     tokenType := #BinaryOperator.
   410     ^ tokenType
   496     ^ tokenType
   411 !
   497 !
   532             tokenType := #Keyword.
   618             tokenType := #Keyword.
   533             ^ self
   619             ^ self
   534         ].
   620         ].
   535         peekChar := $_
   621         peekChar := $_
   536     ].
   622     ].
       
   623     tokenName := string.
   537     firstChar := string at:1.
   624     firstChar := string at:1.
   538     (firstChar == $s) ifTrue:[
   625     (firstChar == $s) ifTrue:[
   539         (string = 'self') ifTrue:[tokenType := #Self. ^self].
   626         (string = 'self') ifTrue:[tokenType := #Self. ^self].
   540         (string = 'super') ifTrue:[tokenType := #Super. ^self]
   627         (string = 'super') ifTrue:[tokenType := #Super. ^self]
   541     ].
   628     ].
   547         (string = 'thisContext') ifTrue:[tokenType := #ThisContext. ^self]
   634         (string = 'thisContext') ifTrue:[tokenType := #ThisContext. ^self]
   548     ].
   635     ].
   549     (firstChar == $f) ifTrue:[
   636     (firstChar == $f) ifTrue:[
   550         (string = 'false') ifTrue:[tokenType := #False. ^self]
   637         (string = 'false') ifTrue:[tokenType := #False. ^self]
   551     ].
   638     ].
   552     tokenName := string.
       
   553     tokenType := #Identifier.
   639     tokenType := #Identifier.
   554     ^ tokenType
   640     ^ tokenType
   555 !
   641 !
   556 
   642 
   557 nextPrimitive
   643 nextPrimitive