0
|
1 |
"
|
4
|
2 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
45
|
3 |
All Rights Reserved
|
0
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
Object subclass:#Scanner
|
|
14 |
instanceVariableNames:'source
|
45
|
15 |
token tokenType tokenPosition tokenValue
|
|
16 |
tokenName tokenLineNr tokenRadix
|
|
17 |
thisChar peekChar
|
|
18 |
requestor exitBlock
|
|
19 |
errorFlag
|
|
20 |
ignoreErrors ignoreWarnings
|
|
21 |
saveComments currentComments
|
|
22 |
warnSTXSpecialComment
|
|
23 |
outStream outCol'
|
|
24 |
classVariableNames:'TypeArray ActionArray Warnings WarnSTXSpecials'
|
|
25 |
poolDictionaries:''
|
|
26 |
category:'System-Compiler'
|
0
|
27 |
!
|
|
28 |
|
|
29 |
Scanner comment:'
|
4
|
30 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
45
|
31 |
All Rights Reserved
|
30
|
32 |
|
45
|
33 |
$Header: /cvs/stx/stx/libcomp/Scanner.st,v 1.13 1994-10-10 00:57:45 claus Exp $
|
20
|
34 |
'!
|
0
|
35 |
|
20
|
36 |
!Scanner class methodsFor:'documentation'!
|
|
37 |
|
|
38 |
copyright
|
|
39 |
"
|
|
40 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
45
|
41 |
All Rights Reserved
|
20
|
42 |
|
|
43 |
This software is furnished under a license and may be used
|
|
44 |
only in accordance with the terms of that license and with the
|
|
45 |
inclusion of the above copyright notice. This software may not
|
|
46 |
be provided or otherwise made available to, or used by, any
|
|
47 |
other person. No title to or ownership of the software is
|
|
48 |
hereby transferred.
|
|
49 |
"
|
|
50 |
!
|
|
51 |
|
|
52 |
version
|
|
53 |
"
|
45
|
54 |
$Header: /cvs/stx/stx/libcomp/Scanner.st,v 1.13 1994-10-10 00:57:45 claus Exp $
|
20
|
55 |
"
|
|
56 |
!
|
|
57 |
|
|
58 |
documentation
|
|
59 |
"
|
|
60 |
Scanner reads from a stream and returns individual smalltalk tokens
|
|
61 |
"
|
|
62 |
! !
|
0
|
63 |
|
|
64 |
!Scanner class methodsFor:'instance creation'!
|
|
65 |
|
30
|
66 |
for:aStringOrStream
|
|
67 |
"return a new scanner reading from aStringOrStream"
|
0
|
68 |
|
30
|
69 |
^ (super new) initializeFor:aStringOrStream
|
0
|
70 |
! !
|
|
71 |
|
41
|
72 |
!Scanner class methodsFor:'defaults'!
|
|
73 |
|
|
74 |
warnings:aBoolean
|
|
75 |
"this allows turning on/off warnings; the default is on.
|
|
76 |
You can turn off warnings in your 'private.rc' file with
|
45
|
77 |
Compiler warnings:false
|
41
|
78 |
"
|
|
79 |
|
|
80 |
Warnings := aBoolean
|
|
81 |
!
|
|
82 |
|
|
83 |
warnSTXSpecials:aBoolean
|
|
84 |
"this allows turning on/off warnings about stx specials.
|
|
85 |
If you get bored by those warnings, turn them off by adding
|
|
86 |
a line as:
|
45
|
87 |
Compiler warnSTXSpecials:false
|
41
|
88 |
in your 'private.rc' file"
|
|
89 |
|
|
90 |
WarnSTXSpecials := aBoolean
|
|
91 |
! !
|
|
92 |
|
33
|
93 |
!Scanner class methodsFor:'initialization'!
|
|
94 |
|
|
95 |
setupActions
|
|
96 |
|block|
|
|
97 |
|
|
98 |
ActionArray := Array new:256.
|
|
99 |
TypeArray := Array new:256.
|
|
100 |
|
|
101 |
block := [:s :char | s nextNumber].
|
|
102 |
($0 asciiValue) to:($9 asciiValue) do:[:index |
|
45
|
103 |
ActionArray at:index put:block
|
33
|
104 |
].
|
|
105 |
|
|
106 |
block := [:s :char | s nextIdentifier].
|
|
107 |
($a asciiValue) to:($z asciiValue) do:[:index |
|
45
|
108 |
ActionArray at:index put:block
|
33
|
109 |
].
|
|
110 |
($A asciiValue) to:($Z asciiValue) do:[:index |
|
45
|
111 |
ActionArray at:index put:block
|
33
|
112 |
].
|
|
113 |
|
|
114 |
block := [:s :char | s nextSpecial].
|
|
115 |
#( $& $- $+ $= $* $/ $\ $< $> $~ $@ $, $? ) do:[:binop |
|
45
|
116 |
TypeArray at:(binop asciiValue) put:#special.
|
|
117 |
ActionArray at:(binop asciiValue) put:block
|
33
|
118 |
].
|
|
119 |
|
|
120 |
block := [:s :char | s nextToken:char].
|
38
|
121 |
#( $; $. $( $) $[ $] $!! $^ $| $_ ) do:[:ch |
|
45
|
122 |
ActionArray at:(ch asciiValue) put:block
|
33
|
123 |
].
|
|
124 |
|
|
125 |
"kludge: action is characterToken, but type is special"
|
|
126 |
TypeArray at:($| asciiValue) put:#special.
|
|
127 |
|
|
128 |
ActionArray at:($' asciiValue) put:[:s :char | s nextString].
|
|
129 |
ActionArray at:($$ asciiValue) put:[:s :char | s nextCharacter].
|
|
130 |
ActionArray at:($# asciiValue) put:[:s :char | s nextHash].
|
|
131 |
ActionArray at:($% asciiValue) put:[:s :char | s nextPrimitive].
|
|
132 |
ActionArray at:($: asciiValue) put:[:s :char | s nextColonOrAssign]
|
|
133 |
|
|
134 |
"
|
|
135 |
Scanner setupActions
|
|
136 |
"
|
|
137 |
! !
|
|
138 |
|
0
|
139 |
!Scanner methodsFor:'private'!
|
|
140 |
|
30
|
141 |
initializeFor:aStringOrStream
|
0
|
142 |
"initialize -
|
|
143 |
if this is the first time, setup character- and action tables"
|
|
144 |
|
|
145 |
errorFlag := false.
|
|
146 |
tokenLineNr := 1.
|
30
|
147 |
aStringOrStream isStream ifFalse:[
|
45
|
148 |
source := ReadStream on:aStringOrStream
|
30
|
149 |
] ifTrue:[
|
45
|
150 |
source := aStringOrStream.
|
30
|
151 |
].
|
3
|
152 |
currentComments := nil.
|
|
153 |
saveComments := false.
|
7
|
154 |
ignoreErrors := false.
|
41
|
155 |
ignoreWarnings := Warnings not.
|
|
156 |
warnSTXSpecialComment := WarnSTXSpecials.
|
0
|
157 |
|
10
|
158 |
ActionArray isNil ifTrue:[
|
45
|
159 |
self class setupActions
|
0
|
160 |
]
|
|
161 |
!
|
|
162 |
|
3
|
163 |
initialize
|
|
164 |
"prepare a scan"
|
|
165 |
|
|
166 |
errorFlag := false.
|
|
167 |
tokenLineNr := 1.
|
|
168 |
currentComments := nil.
|
|
169 |
saveComments := false.
|
7
|
170 |
ignoreErrors := false.
|
41
|
171 |
ignoreWarnings := Warnings not.
|
|
172 |
warnSTXSpecialComment := WarnSTXSpecials
|
3
|
173 |
!
|
|
174 |
|
0
|
175 |
notifying:anObject
|
|
176 |
"set the requestor to be notified"
|
|
177 |
|
|
178 |
requestor := anObject
|
|
179 |
!
|
|
180 |
|
7
|
181 |
ignoreErrors
|
|
182 |
"turn off notification of errors"
|
|
183 |
|
|
184 |
ignoreErrors := true
|
|
185 |
!
|
|
186 |
|
17
|
187 |
ignoreWarnings
|
|
188 |
"turn off notification of warnings"
|
|
189 |
|
|
190 |
ignoreWarnings := true
|
|
191 |
!
|
|
192 |
|
0
|
193 |
backupPosition
|
|
194 |
"if reading from a stream, at the end we might have read
|
|
195 |
one token too many"
|
|
196 |
|
|
197 |
(tokenType == #EOF) ifFalse:[
|
45
|
198 |
source position:tokenPosition
|
0
|
199 |
]
|
20
|
200 |
!
|
|
201 |
|
|
202 |
beginComment
|
|
203 |
^ self
|
|
204 |
!
|
|
205 |
|
|
206 |
endComment:comment
|
|
207 |
saveComments ifTrue:[
|
45
|
208 |
currentComments isNil ifTrue:[
|
|
209 |
currentComments := OrderedCollection with:comment
|
|
210 |
] ifFalse:[
|
|
211 |
currentComments add:comment
|
|
212 |
]
|
20
|
213 |
].
|
0
|
214 |
! !
|
|
215 |
|
|
216 |
!Scanner methodsFor:'error handling'!
|
|
217 |
|
|
218 |
showErrorMessage:aMessage position:pos
|
|
219 |
"show an errormessage on the Transcript"
|
|
220 |
|
7
|
221 |
ignoreErrors ifFalse:[
|
45
|
222 |
Smalltalk silentLoading == true ifFalse:[
|
|
223 |
Transcript showCr:(pos printString , ' ' , aMessage)
|
|
224 |
]
|
7
|
225 |
]
|
0
|
226 |
!
|
|
227 |
|
|
228 |
notifyError:aMessage position:position to:endPos
|
|
229 |
"notify requestor of an error - if there is no requestor
|
|
230 |
put it on the transcript.
|
|
231 |
Return the result passed back by the requestor."
|
|
232 |
|
|
233 |
requestor isNil ifTrue:[
|
45
|
234 |
self showErrorMessage:aMessage position:position.
|
|
235 |
^ false
|
0
|
236 |
].
|
7
|
237 |
|
0
|
238 |
^ requestor error:aMessage position:position to:endPos
|
|
239 |
!
|
|
240 |
|
|
241 |
notifyWarning:aMessage position:position to:endPos
|
|
242 |
"notify requestor of an warning - if there is no requestor
|
|
243 |
put it on the transcript.
|
|
244 |
Return the result passed back by the requestor."
|
|
245 |
|
|
246 |
requestor isNil ifTrue:[
|
45
|
247 |
ignoreWarnings ifFalse:[
|
|
248 |
self showErrorMessage:aMessage position:position.
|
|
249 |
].
|
|
250 |
^ false
|
0
|
251 |
].
|
|
252 |
^ requestor warning:aMessage position:position to:endPos
|
|
253 |
!
|
|
254 |
|
|
255 |
syntaxError:aMessage position:position to:endPos
|
|
256 |
"a syntax error happened"
|
|
257 |
|
|
258 |
self notifyError:(' Error:' , aMessage) position:position to:endPos.
|
|
259 |
exitBlock notNil ifTrue:[exitBlock value].
|
|
260 |
^ false
|
|
261 |
!
|
|
262 |
|
|
263 |
syntaxError:aMessage position:position
|
|
264 |
"a syntax error happened - only start position is known"
|
|
265 |
|
|
266 |
^ self syntaxError:aMessage position:position to:nil
|
|
267 |
!
|
|
268 |
|
|
269 |
syntaxError:aMessage
|
|
270 |
"a syntax error happened - position is not known"
|
|
271 |
|
|
272 |
^ self syntaxError:aMessage position:tokenPosition
|
|
273 |
!
|
|
274 |
|
|
275 |
warning:aMessage position:position to:endPos
|
|
276 |
"a warning"
|
|
277 |
|
30
|
278 |
^ self notifyWarning:('Warning: ' , aMessage) position:position to:endPos
|
0
|
279 |
!
|
|
280 |
|
|
281 |
warning:aMessage position:position
|
|
282 |
"a warning - only start position is known"
|
|
283 |
|
|
284 |
^ self warning:aMessage position:position to:nil
|
|
285 |
!
|
|
286 |
|
|
287 |
warning:aMessage
|
|
288 |
"a warning - position is not known"
|
|
289 |
|
|
290 |
^ self warning:aMessage position:tokenPosition
|
|
291 |
! !
|
|
292 |
|
17
|
293 |
!Scanner methodsFor:'general scanning'!
|
|
294 |
|
|
295 |
scanPositionsFor:aTokenString inString:aSourceString
|
|
296 |
"scan aSourceString for occurrances of aTokenString.
|
|
297 |
Return a collection of start positions.
|
|
298 |
Added for VW compatibility (to support syntax-highlight)."
|
|
299 |
|
|
300 |
|searchType searchName searchValue positions t|
|
|
301 |
|
|
302 |
"
|
|
303 |
first, look what kind of token we have to search for
|
|
304 |
"
|
|
305 |
self initializeFor:(ReadStream on:aTokenString).
|
|
306 |
self nextToken.
|
|
307 |
searchType := tokenType.
|
|
308 |
searchName := tokenName.
|
|
309 |
searchValue := tokenValue.
|
|
310 |
|
|
311 |
"
|
|
312 |
start the real work ...
|
|
313 |
"
|
|
314 |
self initializeFor:(ReadStream on:aSourceString).
|
|
315 |
positions := OrderedCollection new.
|
|
316 |
|
|
317 |
[(t := self nextToken) ~~ #EOF] whileTrue:[
|
45
|
318 |
searchType == t ifTrue:[
|
|
319 |
(searchName isNil or:[tokenName = searchName]) ifTrue:[
|
|
320 |
(searchValue isNil or:[tokenValue = searchValue]) ifTrue:[
|
|
321 |
positions add:tokenPosition.
|
|
322 |
]
|
|
323 |
]
|
|
324 |
]
|
17
|
325 |
].
|
|
326 |
|
|
327 |
^ positions
|
|
328 |
|
|
329 |
"
|
30
|
330 |
Scanner new scanPositionsFor:'hello' inString:'foo bar hello baz hello'
|
|
331 |
Scanner new scanPositionsFor:'3.14' inString:'foo 3.145 bar hello 3.14 baz hello 3.14'
|
|
332 |
Scanner new scanPositionsFor:'16' inString:'foo 16 bar hello 16r10 baz hello 2r10000'
|
17
|
333 |
"
|
|
334 |
! !
|
|
335 |
|
0
|
336 |
!Scanner methodsFor:'reading next token'!
|
|
337 |
|
3
|
338 |
skipComment
|
7
|
339 |
|comment startPos|
|
3
|
340 |
|
|
341 |
comment := ''.
|
|
342 |
|
20
|
343 |
self beginComment.
|
|
344 |
outStream notNil ifTrue:[
|
45
|
345 |
outStream nextPut:Character doubleQuote.
|
|
346 |
outCol := outCol + 1
|
20
|
347 |
].
|
|
348 |
|
7
|
349 |
startPos := source position.
|
3
|
350 |
source next.
|
|
351 |
thisChar := source peek.
|
15
|
352 |
|
|
353 |
"special ST/X addition:
|
17
|
354 |
a $/ right after the initial double quote makes it an up-to-end-of-line comment,
|
15
|
355 |
which is very useful to comment out parts of filed-in source code.
|
17
|
356 |
Since this is non-standard, use it in very rare cases only.
|
|
357 |
(maybe the upcoming ansi-standard adds something similar - in this case, I will
|
|
358 |
change it without notice)"
|
15
|
359 |
|
|
360 |
thisChar == $/ ifTrue:[
|
45
|
361 |
[thisChar notNil and:[thisChar ~~ Character cr]] whileTrue:[
|
|
362 |
saveComments ifTrue:[
|
|
363 |
comment := comment copyWith:thisChar
|
|
364 |
].
|
|
365 |
outStream notNil ifTrue:[
|
|
366 |
outStream nextPut:thisChar.
|
|
367 |
outCol := outCol + 1
|
|
368 |
].
|
|
369 |
thisChar := source nextPeek.
|
|
370 |
].
|
|
371 |
tokenLineNr := tokenLineNr + 1.
|
|
372 |
ignoreWarnings ifFalse:[
|
|
373 |
warnSTXSpecialComment ifTrue:[
|
|
374 |
self warning:'end-of-line comments are a nonstandard feature of ST/X'
|
|
375 |
position:startPos to:(source position).
|
|
376 |
"
|
|
377 |
only warn once
|
|
378 |
"
|
|
379 |
warnSTXSpecialComment := false
|
|
380 |
]
|
|
381 |
].
|
|
382 |
outStream notNil ifTrue:[
|
|
383 |
outStream cr.
|
|
384 |
outCol := 1
|
|
385 |
].
|
15
|
386 |
] ifFalse:[
|
45
|
387 |
[thisChar notNil and:[thisChar ~~ (Character doubleQuote)]] whileTrue:[
|
|
388 |
thisChar == (Character cr) ifTrue:[
|
|
389 |
tokenLineNr := tokenLineNr + 1.
|
|
390 |
].
|
|
391 |
saveComments ifTrue:[
|
|
392 |
comment := comment copyWith:thisChar
|
|
393 |
].
|
|
394 |
outStream notNil ifTrue:[
|
|
395 |
outStream nextPut:thisChar.
|
|
396 |
outCol := outCol + 1
|
|
397 |
].
|
|
398 |
thisChar := source nextPeek
|
|
399 |
].
|
|
400 |
thisChar isNil ifTrue:[
|
|
401 |
self warning:'unclosed comment' position:startPos to:(source position)
|
|
402 |
] ifFalse:[
|
|
403 |
outStream notNil ifTrue:[
|
|
404 |
outStream nextPut:(Character doubleQuote).
|
|
405 |
outCol := outCol + 1
|
|
406 |
].
|
|
407 |
]
|
3
|
408 |
].
|
15
|
409 |
|
3
|
410 |
saveComments ifTrue:[
|
45
|
411 |
currentComments isNil ifTrue:[
|
|
412 |
currentComments := OrderedCollection with:comment
|
|
413 |
] ifFalse:[
|
|
414 |
currentComments add:comment
|
|
415 |
]
|
3
|
416 |
].
|
7
|
417 |
|
20
|
418 |
"skip final dQuote or cr"
|
3
|
419 |
source next.
|
20
|
420 |
|
|
421 |
self endComment:comment.
|
3
|
422 |
!
|
|
423 |
|
0
|
424 |
nextToken
|
|
425 |
"return the next token from the source-stream"
|
|
426 |
|
41
|
427 |
|skipping actionBlock|
|
0
|
428 |
|
|
429 |
peekChar notNil ifTrue:[
|
45
|
430 |
thisChar := peekChar.
|
|
431 |
peekChar := nil
|
0
|
432 |
] ifFalse:[
|
45
|
433 |
skipping := true.
|
|
434 |
[skipping] whileTrue:[
|
|
435 |
outStream notNil ifTrue:[
|
|
436 |
[(thisChar := source peek) == Character space] whileTrue:[
|
|
437 |
source next.
|
|
438 |
outStream space.
|
|
439 |
outCol := outCol + 1.
|
|
440 |
]
|
|
441 |
] ifFalse:[
|
|
442 |
thisChar := source skipSeparatorsExceptCR.
|
|
443 |
].
|
|
444 |
thisChar == (Character cr) ifTrue:[
|
|
445 |
tokenLineNr := tokenLineNr + 1.
|
|
446 |
source next.
|
|
447 |
outStream notNil ifTrue:[
|
|
448 |
outStream cr.
|
|
449 |
outCol := 1
|
|
450 |
]
|
|
451 |
] ifFalse:[
|
|
452 |
thisChar == (Character doubleQuote) ifTrue:[
|
|
453 |
"start of a comment"
|
3
|
454 |
|
45
|
455 |
self skipComment.
|
|
456 |
thisChar := source peek.
|
|
457 |
] ifFalse:[
|
|
458 |
skipping := false
|
|
459 |
]
|
|
460 |
]
|
|
461 |
].
|
|
462 |
thisChar isNil ifTrue:[
|
|
463 |
tokenType := #EOF.
|
|
464 |
^ tokenType
|
|
465 |
]
|
0
|
466 |
].
|
|
467 |
tokenPosition := source position.
|
|
468 |
|
10
|
469 |
actionBlock := ActionArray at:(thisChar asciiValue).
|
0
|
470 |
actionBlock notNil ifTrue:[
|
45
|
471 |
^ actionBlock value:self value:thisChar
|
0
|
472 |
].
|
|
473 |
|
|
474 |
self syntaxError:('invalid character: ''' , thisChar asString , ''' ',
|
45
|
475 |
'(' , thisChar asciiValue printString , ')')
|
|
476 |
position:tokenPosition to:tokenPosition.
|
0
|
477 |
tokenType := #Error.
|
|
478 |
^ #Error
|
|
479 |
!
|
|
480 |
|
|
481 |
nextToken:aCharacter
|
|
482 |
tokenType := aCharacter.
|
|
483 |
source next.
|
|
484 |
^ tokenType
|
|
485 |
!
|
|
486 |
|
|
487 |
nextColonOrAssign
|
|
488 |
"colon has been read - look for = to make it an assign"
|
|
489 |
|
|
490 |
(source nextPeek == $=) ifTrue:[
|
45
|
491 |
source next.
|
|
492 |
tokenType := $_
|
0
|
493 |
] ifFalse:[
|
45
|
494 |
tokenType := $:
|
0
|
495 |
].
|
|
496 |
^ tokenType
|
|
497 |
!
|
|
498 |
|
|
499 |
nextSpecial
|
|
500 |
"a special character has been read, look for another one.
|
|
501 |
also -number is handled here"
|
|
502 |
|
|
503 |
|firstChar secondChar thirdChar string p|
|
|
504 |
|
|
505 |
firstChar := source next.
|
|
506 |
secondChar := source peek.
|
|
507 |
(firstChar == $-) ifTrue:[
|
45
|
508 |
secondChar isDigit ifTrue:[
|
|
509 |
self nextNumber.
|
|
510 |
tokenValue := tokenValue negated.
|
|
511 |
^ tokenType
|
|
512 |
]
|
0
|
513 |
].
|
|
514 |
string := firstChar asString.
|
20
|
515 |
secondChar notNil ifTrue:[
|
45
|
516 |
((TypeArray at:(secondChar asciiValue)) == #special) ifTrue:[
|
|
517 |
(secondChar == $-) ifTrue:[
|
|
518 |
"special- look if minus belongs to number following"
|
|
519 |
p := source position.
|
|
520 |
source next.
|
|
521 |
thirdChar := source peek.
|
|
522 |
source position:p.
|
|
523 |
thirdChar isDigit ifTrue:[
|
|
524 |
tokenName := string.
|
|
525 |
tokenType := #BinaryOperator.
|
|
526 |
^ tokenType
|
|
527 |
]
|
|
528 |
].
|
|
529 |
source next.
|
|
530 |
string := string copyWith:secondChar
|
|
531 |
].
|
0
|
532 |
].
|
|
533 |
tokenName := string.
|
|
534 |
tokenType := #BinaryOperator.
|
|
535 |
^ tokenType
|
|
536 |
!
|
|
537 |
|
|
538 |
nextCharacter
|
|
539 |
"a $ has been read - return a character token"
|
|
540 |
|
|
541 |
|nextChar|
|
|
542 |
|
|
543 |
source next.
|
|
544 |
nextChar := source next.
|
|
545 |
nextChar notNil ifTrue:[
|
45
|
546 |
tokenValue := nextChar.
|
|
547 |
tokenType := #Character
|
0
|
548 |
] ifFalse:[
|
45
|
549 |
tokenType := #EOF
|
0
|
550 |
].
|
|
551 |
^ tokenType
|
|
552 |
!
|
|
553 |
|
|
554 |
nextMantissa:radix
|
|
555 |
|nextChar value factor|
|
|
556 |
|
|
557 |
value := 0.
|
|
558 |
factor := 1.0 / radix.
|
|
559 |
nextChar := source peek.
|
|
560 |
[(nextChar notNil and:[nextChar isDigitRadix:radix])] whileTrue:[
|
45
|
561 |
value := value + (nextChar digitValue * factor).
|
|
562 |
factor := factor / radix.
|
|
563 |
nextChar := source nextPeek
|
0
|
564 |
].
|
|
565 |
^ value
|
|
566 |
!
|
|
567 |
|
|
568 |
nextNumber
|
30
|
569 |
|nextChar value s|
|
0
|
570 |
|
30
|
571 |
tokenRadix := 10.
|
|
572 |
value := Integer readFrom:source radix:tokenRadix.
|
0
|
573 |
nextChar := source peek.
|
|
574 |
(nextChar == $r) ifTrue:[
|
45
|
575 |
tokenRadix := value.
|
|
576 |
source next.
|
|
577 |
s := 1.
|
|
578 |
source peek == $- ifTrue:[
|
|
579 |
source next.
|
|
580 |
s := -1
|
|
581 |
].
|
|
582 |
value := Integer readFrom:source radix:tokenRadix.
|
|
583 |
value := value * s.
|
|
584 |
nextChar := source peek
|
0
|
585 |
].
|
|
586 |
(nextChar == $.) ifTrue:[
|
45
|
587 |
nextChar := source nextPeek.
|
|
588 |
(nextChar notNil and:[nextChar isDigitRadix:tokenRadix]) ifTrue:[
|
|
589 |
value := value asFloat + (self nextMantissa:tokenRadix).
|
|
590 |
nextChar := source peek
|
|
591 |
] ifFalse:[
|
|
592 |
nextChar == (Character cr) ifTrue:[
|
|
593 |
tokenLineNr := tokenLineNr + 1.
|
|
594 |
].
|
|
595 |
peekChar := $.
|
|
596 |
]
|
0
|
597 |
].
|
30
|
598 |
((nextChar == $e) or:[nextChar == $E]) ifTrue:[
|
45
|
599 |
nextChar := source nextPeek.
|
|
600 |
(nextChar notNil and:[(nextChar isDigitRadix:tokenRadix) or:['+-' includes:nextChar]]) ifTrue:[
|
|
601 |
s := 1.
|
|
602 |
(nextChar == $+) ifTrue:[
|
|
603 |
nextChar := source nextPeek
|
|
604 |
] ifFalse:[
|
|
605 |
(nextChar == $-) ifTrue:[
|
|
606 |
nextChar := source nextPeek.
|
|
607 |
s := s negated
|
|
608 |
]
|
|
609 |
].
|
|
610 |
value := value asFloat
|
|
611 |
* (10.0 raisedToInteger:((Integer readFrom:source radix:tokenRadix) * s))
|
|
612 |
]
|
0
|
613 |
].
|
|
614 |
tokenValue := value.
|
|
615 |
(value isMemberOf:Float) ifTrue:[
|
45
|
616 |
tokenType := #Float
|
0
|
617 |
] ifFalse:[
|
45
|
618 |
tokenType := #Integer
|
0
|
619 |
].
|
|
620 |
^ tokenType
|
|
621 |
!
|
|
622 |
|
|
623 |
nextId
|
|
624 |
|nextChar string oldString
|
|
625 |
index "{ Class: SmallInteger }"
|
|
626 |
max "{ Class: SmallInteger }" |
|
|
627 |
|
|
628 |
nextChar := source peek.
|
|
629 |
string := String new:10.
|
|
630 |
index := 0.
|
|
631 |
max := 10.
|
|
632 |
[true] whileTrue:[
|
45
|
633 |
(nextChar notNil and:[nextChar isLetterOrDigit]) ifFalse:[
|
|
634 |
^ string copyTo:index
|
|
635 |
].
|
|
636 |
(index == max) ifTrue:[
|
|
637 |
oldString := string.
|
|
638 |
string := String new:(max * 2).
|
|
639 |
string replaceFrom:1 to:max with:oldString.
|
|
640 |
max := max * 2
|
|
641 |
].
|
|
642 |
index := index + 1.
|
|
643 |
string at:index put:nextChar.
|
|
644 |
nextChar := source nextPeek
|
0
|
645 |
]
|
|
646 |
!
|
|
647 |
|
|
648 |
nextIdentifier
|
|
649 |
|nextChar string firstChar|
|
|
650 |
|
|
651 |
string := source nextWord "self nextId".
|
|
652 |
nextChar := source peek.
|
|
653 |
(nextChar == $:) ifTrue:[
|
45
|
654 |
source next.
|
|
655 |
(source peek == $=) ifFalse:[
|
|
656 |
tokenName := string copyWith:nextChar.
|
|
657 |
tokenType := #Keyword.
|
|
658 |
^ self
|
|
659 |
].
|
|
660 |
peekChar := $_
|
0
|
661 |
].
|
20
|
662 |
tokenName := string.
|
0
|
663 |
firstChar := string at:1.
|
|
664 |
(firstChar == $s) ifTrue:[
|
45
|
665 |
(string = 'self') ifTrue:[tokenType := #Self. ^self].
|
|
666 |
(string = 'super') ifTrue:[tokenType := #Super. ^self]
|
0
|
667 |
].
|
|
668 |
(firstChar == $n) ifTrue:[
|
45
|
669 |
(string = 'nil') ifTrue:[tokenType := #Nil. ^self]
|
0
|
670 |
].
|
|
671 |
(firstChar == $t) ifTrue:[
|
45
|
672 |
(string = 'true') ifTrue:[tokenType := #True. ^self].
|
|
673 |
(string = 'thisContext') ifTrue:[tokenType := #ThisContext. ^self]
|
0
|
674 |
].
|
|
675 |
(firstChar == $f) ifTrue:[
|
45
|
676 |
(string = 'false') ifTrue:[tokenType := #False. ^self]
|
0
|
677 |
].
|
|
678 |
tokenType := #Identifier.
|
|
679 |
^ tokenType
|
|
680 |
!
|
|
681 |
|
|
682 |
nextPrimitive
|
|
683 |
|nextChar inPrimitive string
|
|
684 |
index "{ Class: SmallInteger }"
|
|
685 |
len "{ Class: SmallInteger }" |
|
|
686 |
|
|
687 |
nextChar := source nextPeek.
|
|
688 |
string := String new:500.
|
|
689 |
len := 500.
|
|
690 |
index := 1.
|
|
691 |
(nextChar == ${) ifTrue:[
|
45
|
692 |
nextChar := source nextPeek.
|
|
693 |
inPrimitive := true.
|
|
694 |
[inPrimitive] whileTrue:[
|
|
695 |
[nextChar == $%] whileFalse:[
|
|
696 |
string at:index put:nextChar.
|
|
697 |
(index == len) ifTrue:[
|
|
698 |
string := string , (String new:len).
|
|
699 |
len := len * 2
|
|
700 |
].
|
|
701 |
index := index + 1.
|
|
702 |
nextChar := source next
|
|
703 |
].
|
|
704 |
(source peek == $}) ifTrue:[
|
|
705 |
inPrimitive := false
|
|
706 |
] ifFalse:[
|
|
707 |
string at:index put:nextChar.
|
|
708 |
(index == len) ifTrue:[
|
|
709 |
string := string , (String new:len).
|
|
710 |
len := len * 2
|
|
711 |
].
|
|
712 |
index := index + 1.
|
|
713 |
nextChar := source next
|
|
714 |
]
|
|
715 |
].
|
|
716 |
source next.
|
|
717 |
tokenValue := string copyTo:(index - 1).
|
|
718 |
tokenType := #Primitive.
|
|
719 |
tokenLineNr := tokenLineNr + (tokenValue occurrencesOf:(Character cr)).
|
|
720 |
^ tokenType
|
0
|
721 |
].
|
|
722 |
|
|
723 |
"a % alone is a binary operator"
|
|
724 |
tokenName := '%'.
|
|
725 |
tokenType := #BinaryOperator.
|
|
726 |
^ tokenType.
|
|
727 |
"
|
|
728 |
self syntaxError:('invalid character: ''' , nextChar asString , '''')
|
45
|
729 |
position:tokenPosition to:(tokenPosition + 1).
|
0
|
730 |
^ #Error
|
|
731 |
"
|
|
732 |
!
|
|
733 |
|
|
734 |
nextHash
|
|
735 |
|nextChar string|
|
|
736 |
|
|
737 |
nextChar := source nextPeek.
|
|
738 |
nextChar notNil ifTrue:[
|
45
|
739 |
nextChar isLetterOrDigit ifTrue:[
|
|
740 |
string := ''.
|
|
741 |
[nextChar notNil and:[nextChar isLetterOrDigit]] whileTrue:[
|
|
742 |
string := string , (source nextWord "self nextId").
|
|
743 |
nextChar := source peek.
|
|
744 |
(nextChar == $:) ifFalse:[
|
|
745 |
tokenValue := string asSymbol.
|
|
746 |
tokenType := #Symbol.
|
|
747 |
^ tokenType
|
|
748 |
].
|
|
749 |
string := string copyWith:nextChar.
|
|
750 |
nextChar := source nextPeek
|
|
751 |
].
|
|
752 |
tokenValue := string asSymbol.
|
|
753 |
tokenType := #Symbol.
|
|
754 |
^ tokenType
|
|
755 |
].
|
|
756 |
(nextChar == $( ) ifTrue:[
|
|
757 |
source next.
|
|
758 |
tokenType := #HashLeftParen.
|
|
759 |
^ tokenType
|
|
760 |
].
|
|
761 |
(nextChar == $[ ) ifTrue:[
|
|
762 |
"it seems that ST-80 supports Constant ByteArrays as #[...]
|
|
763 |
(seen in a PD program)"
|
|
764 |
source next.
|
|
765 |
tokenType := #HashLeftBrack.
|
|
766 |
^ tokenType
|
|
767 |
].
|
|
768 |
(nextChar == $' ) ifTrue:[
|
|
769 |
"it seems that ST-80 supports arbitrary symbols as #'...'
|
|
770 |
(seen in a PD program)"
|
|
771 |
self nextString.
|
|
772 |
tokenValue := tokenValue asSymbol.
|
|
773 |
tokenType := #Symbol.
|
|
774 |
^ tokenType
|
|
775 |
].
|
|
776 |
((TypeArray at:(nextChar asciiValue)) == #special) ifTrue:[
|
|
777 |
string := source next asString.
|
|
778 |
nextChar := source peek.
|
|
779 |
nextChar notNil ifTrue:[
|
|
780 |
((TypeArray at:(nextChar asciiValue)) == #special) ifTrue:[
|
|
781 |
source next.
|
|
782 |
string := string copyWith:nextChar
|
|
783 |
]
|
|
784 |
].
|
|
785 |
tokenValue := string asSymbol.
|
|
786 |
tokenType := #Symbol.
|
|
787 |
^ tokenType
|
|
788 |
]
|
0
|
789 |
].
|
3
|
790 |
"this allows hash to be used as binop -
|
|
791 |
I dont know, if this is correct ..."
|
|
792 |
|
|
793 |
tokenName := '#'.
|
45
|
794 |
tokenType := #BinaryOperator.
|
3
|
795 |
^ tokenType
|
|
796 |
"
|
0
|
797 |
self syntaxError:'unexpected end-of-input in Symbol'
|
45
|
798 |
position:tokenPosition to:(tokenPosition + 1).
|
0
|
799 |
^ #Error
|
3
|
800 |
"
|
0
|
801 |
!
|
|
802 |
|
|
803 |
nextString
|
|
804 |
|nextChar string pos
|
|
805 |
index "{ Class: SmallInteger }"
|
|
806 |
len "{ Class: SmallInteger }"
|
|
807 |
inString|
|
|
808 |
|
|
809 |
string := String new:20.
|
|
810 |
len := 20.
|
|
811 |
index := 1.
|
|
812 |
pos := source position.
|
|
813 |
source next.
|
|
814 |
nextChar := source next.
|
|
815 |
inString := true.
|
|
816 |
|
|
817 |
[inString] whileTrue:[
|
45
|
818 |
nextChar isNil ifTrue:[
|
|
819 |
self syntaxError:'unexpected end-of-input in String'
|
|
820 |
position:pos to:(source position - 1).
|
|
821 |
tokenType := #EOF.
|
|
822 |
^ tokenType
|
|
823 |
].
|
|
824 |
(nextChar == Character cr) ifTrue:[
|
|
825 |
tokenLineNr := tokenLineNr + 1
|
|
826 |
].
|
|
827 |
(nextChar == Character quote) ifTrue:[
|
|
828 |
(source peek == Character quote) ifTrue:[
|
|
829 |
source next
|
|
830 |
] ifFalse:[
|
|
831 |
inString := false
|
|
832 |
]
|
|
833 |
].
|
|
834 |
inString ifTrue:[
|
|
835 |
string at:index put:nextChar.
|
|
836 |
(index == len) ifTrue:[
|
|
837 |
string := string , (String new:len).
|
|
838 |
len := len * 2
|
|
839 |
].
|
|
840 |
index := index + 1.
|
|
841 |
nextChar := source next
|
|
842 |
]
|
0
|
843 |
].
|
15
|
844 |
tokenValue := string copyTo:(index - 1).
|
0
|
845 |
tokenType := #String.
|
|
846 |
^ tokenType
|
|
847 |
! !
|