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 |
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. |
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 |