Scanner.st
changeset 30 26e2f849916a
parent 20 f8dd8ba75205
child 33 8985ec2f9e82
equal deleted inserted replaced
29:5884a68a6226 30:26e2f849916a
    11 "
    11 "
    12 
    12 
    13 Object subclass:#Scanner
    13 Object subclass:#Scanner
    14        instanceVariableNames:'source 
    14        instanceVariableNames:'source 
    15                               token tokenType tokenPosition tokenValue
    15                               token tokenType tokenPosition tokenValue
    16                               tokenName tokenLineNr
    16                               tokenName tokenLineNr tokenRadix
    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
    27 !
    27 !
    28 
    28 
    29 Scanner comment:'
    29 Scanner comment:'
    30 COPYRIGHT (c) 1989 by Claus Gittinger
    30 COPYRIGHT (c) 1989 by Claus Gittinger
    31              All Rights Reserved
    31              All Rights Reserved
       
    32 
       
    33 $Header: /cvs/stx/stx/libcomp/Scanner.st,v 1.9 1994-08-11 23:27:27 claus Exp $
    32 '!
    34 '!
    33 
    35 
    34 !Scanner class methodsFor:'documentation'!
    36 !Scanner class methodsFor:'documentation'!
    35 
    37 
    36 copyright
    38 copyright
    47 "
    49 "
    48 !
    50 !
    49 
    51 
    50 version
    52 version
    51 "
    53 "
    52 $Header: /cvs/stx/stx/libcomp/Scanner.st,v 1.8 1994-06-02 20:26:16 claus Exp $
    54 $Header: /cvs/stx/stx/libcomp/Scanner.st,v 1.9 1994-08-11 23:27:27 claus Exp $
    53 "
    55 "
    54 !
    56 !
    55 
    57 
    56 documentation
    58 documentation
    57 "
    59 "
    59 "
    61 "
    60 ! !
    62 ! !
    61 
    63 
    62 !Scanner class methodsFor:'instance creation'!
    64 !Scanner class methodsFor:'instance creation'!
    63 
    65 
    64 for:aStream
    66 for:aStringOrStream
    65     "return a new scanner reading from aStream"
    67     "return a new scanner reading from aStringOrStream"
    66 
    68 
    67     ^ (super new) initializeFor:aStream
    69     ^ (super new) initializeFor:aStringOrStream
    68 ! !
    70 ! !
    69 
    71 
    70 !Scanner methodsFor:'private'!
    72 !Scanner methodsFor:'private'!
    71 
    73 
    72 initializeFor:aStream
    74 initializeFor:aStringOrStream
    73     "initialize -
    75     "initialize -
    74      if this is the first time, setup character- and action tables"
    76      if this is the first time, setup character- and action tables"
    75 
    77 
    76     |block|
    78     |block|
    77 
    79 
    78     errorFlag := false.
    80     errorFlag := false.
    79     tokenLineNr := 1.
    81     tokenLineNr := 1.
    80     source := aStream.
    82     aStringOrStream isStream ifFalse:[
       
    83         source := ReadStream on:aStringOrStream
       
    84     ] ifTrue:[
       
    85         source := aStringOrStream.
       
    86     ].
    81     currentComments := nil.
    87     currentComments := nil.
    82     saveComments := false.
    88     saveComments := false.
    83     ignoreErrors := false.
    89     ignoreErrors := false.
    84     ignoreWarnings := false.
    90     ignoreWarnings := false.
    85     warnSTXSpecialComment := true.
    91     warnSTXSpecialComment := true.
   236 !
   242 !
   237 
   243 
   238 warning:aMessage position:position to:endPos
   244 warning:aMessage position:position to:endPos
   239     "a warning"
   245     "a warning"
   240 
   246 
   241     ^ self notifyWarning:(' Warning:' , aMessage) position:position to:endPos
   247     ^ self notifyWarning:('Warning: ' , aMessage) position:position to:endPos
   242 !
   248 !
   243 
   249 
   244 warning:aMessage position:position
   250 warning:aMessage position:position
   245     "a warning - only start position is known"
   251     "a warning - only start position is known"
   246 
   252 
   288     ].
   294     ].
   289 
   295 
   290     ^ positions
   296     ^ positions
   291 
   297 
   292     "
   298     "
   293      Scanner new scanPositionsFor:'hello' inString:'foo bar hello baz hello'
   299      Scanner new scanPositionsFor:'hello' inString:'foo bar hello baz hello' 
   294      Scanner new scanPositionsFor:'3.14' inString:'foo 3.145 bar hello 3.14 baz hello 3.14'
   300      Scanner new scanPositionsFor:'3.14' inString:'foo 3.145 bar hello 3.14 baz hello 3.14' 
   295      Scanner new scanPositionsFor:'16' inString:'foo 16 bar hello 16r10 baz hello 2r10000'
   301      Scanner new scanPositionsFor:'16' inString:'foo 16 bar hello 16r10 baz hello 2r10000' 
   296     "
   302     "
   297 ! !
   303 ! !
   298 
   304 
   299 !Scanner methodsFor:'reading next token'!
   305 !Scanner methodsFor:'reading next token'!
   300 
   306 
   330                 outCol := outCol + 1
   336                 outCol := outCol + 1
   331             ].
   337             ].
   332             thisChar := source nextPeek.
   338             thisChar := source nextPeek.
   333         ].
   339         ].
   334         tokenLineNr := tokenLineNr + 1.
   340         tokenLineNr := tokenLineNr + 1.
   335         warnSTXSpecialComment ifTrue:[
   341         ignoreWarnings ifFalse:[
   336             self warning:'end-of-line comments are a nonstandard feature of ST/X' 
   342             warnSTXSpecialComment ifTrue:[
   337                  position:startPos to:(source position).
   343                 self warning:'end-of-line comments are a nonstandard feature of ST/X' 
   338             "
   344                      position:startPos to:(source position).
   339              only warn once
   345                 "
   340             "
   346                  only warn once
   341             warnSTXSpecialComment := false
   347                 "
       
   348                 warnSTXSpecialComment := false
       
   349             ]
   342         ].
   350         ].
   343         outStream notNil ifTrue:[
   351         outStream notNil ifTrue:[
   344             outStream cr.
   352             outStream cr.
   345             outCol := 1
   353             outCol := 1
   346         ].
   354         ].
   525     ].
   533     ].
   526     ^ value
   534     ^ value
   527 !
   535 !
   528 
   536 
   529 nextNumber
   537 nextNumber
   530     |nextChar value radix s|
   538     |nextChar value s|
   531 
   539 
   532     radix := 10.
   540     tokenRadix := 10.
   533     value := Integer readFrom:source radix:radix.
   541     value := Integer readFrom:source radix:tokenRadix.
   534     nextChar := source peek.
   542     nextChar := source peek.
   535     (nextChar == $r) ifTrue:[
   543     (nextChar == $r) ifTrue:[
   536         radix := value.
   544         tokenRadix := value.
   537         source next.
   545         source next.
   538         s := 1.
   546         s := 1.
   539         source peek == $- ifTrue:[
   547         source peek == $- ifTrue:[
   540             source next.
   548             source next.
   541             s := -1
   549             s := -1
   542         ].
   550         ].
   543         value := Integer readFrom:source radix:radix.
   551         value := Integer readFrom:source radix:tokenRadix.
   544         value := value * s.
   552         value := value * s.
   545         nextChar := source peek
   553         nextChar := source peek
   546     ].
   554     ].
   547     (nextChar == $.) ifTrue:[
   555     (nextChar == $.) ifTrue:[
   548         nextChar := source nextPeek.
   556         nextChar := source nextPeek.
   549         (nextChar notNil and:[nextChar isDigitRadix:radix]) ifTrue:[
   557         (nextChar notNil and:[nextChar isDigitRadix:tokenRadix]) ifTrue:[
   550             value := value asFloat + (self nextMantissa:radix).
   558             value := value asFloat + (self nextMantissa:tokenRadix).
   551             nextChar := source peek
   559             nextChar := source peek
   552         ] ifFalse:[
   560         ] ifFalse:[
   553             nextChar == (Character cr) ifTrue:[
   561             nextChar == (Character cr) ifTrue:[
   554                 tokenLineNr := tokenLineNr + 1.
   562                 tokenLineNr := tokenLineNr + 1.
   555             ].
   563             ].
   556             peekChar := $.
   564             peekChar := $.
   557         ]
   565         ]
   558     ].
   566     ].
   559     (nextChar == $e) ifTrue:[
   567     ((nextChar == $e) or:[nextChar == $E]) ifTrue:[
   560         nextChar := source nextPeek.
   568         nextChar := source nextPeek.
   561         (nextChar notNil and:[(nextChar isDigitRadix:radix) or:['+-' includes:nextChar]]) ifTrue:[
   569         (nextChar notNil and:[(nextChar isDigitRadix:tokenRadix) or:['+-' includes:nextChar]]) ifTrue:[
   562             s := 1.
   570             s := 1.
   563             (nextChar == $+) ifTrue:[
   571             (nextChar == $+) ifTrue:[
   564                 nextChar := source nextPeek
   572                 nextChar := source nextPeek
   565             ] ifFalse:[
   573             ] ifFalse:[
   566                 (nextChar == $-) ifTrue:[
   574                 (nextChar == $-) ifTrue:[
   567                     nextChar := source nextPeek.
   575                     nextChar := source nextPeek.
   568                     s := s negated
   576                     s := s negated
   569                 ]
   577                 ]
   570             ].
   578             ].
   571             value := value asFloat
   579             value := value asFloat
   572                      * (10.0 raisedToInteger:((Integer readFrom:source radix:radix) * s))
   580                      * (10.0 raisedToInteger:((Integer readFrom:source radix:tokenRadix) * s))
   573         ]
   581         ]
   574     ].
   582     ].
   575     tokenValue := value.
   583     tokenValue := value.
   576     (value isMemberOf:Float) ifTrue:[
   584     (value isMemberOf:Float) ifTrue:[
   577         tokenType := #Float
   585         tokenType := #Float