author | Claus Gittinger <cg@exept.de> |
Thu, 25 Apr 1996 19:09:53 +0200 | |
changeset 263 | 3b21d0991eff |
parent 254 | edfcf93d821f |
child 264 | f10298cba622 |
permissions | -rw-r--r-- |
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 |
|
241
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
14 |
instanceVariableNames:'source collectedSource token tokenType tokenPosition tokenValue |
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
15 |
tokenName tokenLineNr tokenRadix hereChar peekChar peekChar2 |
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
16 |
requestor exitBlock errorFlag ignoreErrors ignoreWarnings |
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
17 |
saveComments currentComments warnSTXSpecialComment |
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
18 |
warnUnderscoreInIdentifier warnOldStyleAssignment outStream |
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
19 |
outCol' |
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
20 |
classVariableNames:'TypeArray ActionArray AllowUnderscoreInIdentifier Warnings |
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
21 |
WarnSTXSpecials WarnOldStyleAssignment WarnUnderscoreInIdentifier' |
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
22 |
poolDictionaries:'' |
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
23 |
category:'System-Compiler' |
0 | 24 |
! |
25 |
||
20 | 26 |
!Scanner class methodsFor:'documentation'! |
27 |
||
28 |
copyright |
|
29 |
" |
|
30 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
45 | 31 |
All Rights Reserved |
20 | 32 |
|
33 |
This software is furnished under a license and may be used |
|
34 |
only in accordance with the terms of that license and with the |
|
35 |
inclusion of the above copyright notice. This software may not |
|
36 |
be provided or otherwise made available to, or used by, any |
|
37 |
other person. No title to or ownership of the software is |
|
38 |
hereby transferred. |
|
39 |
" |
|
40 |
! |
|
41 |
||
42 |
documentation |
|
43 |
" |
|
44 |
Scanner reads from a stream and returns individual smalltalk tokens |
|
75 | 45 |
|
46 |
TODO: some testers noticed, that ST-80's scanner methods are called |
|
47 |
xLetter, xDigit etc. For code using these (internals), the nextNumber, |
|
48 |
nextIdentifier etc. methods should be renamed. |
|
120 | 49 |
(to me, these seem to be internal private methods; their public use |
50 |
is not a good idea ..) |
|
263 | 51 |
|
52 |
[author:] |
|
53 |
Claus Gittinger |
|
20 | 54 |
" |
41 | 55 |
! ! |
56 |
||
33 | 57 |
!Scanner class methodsFor:'initialization'! |
58 |
||
60 | 59 |
initialize |
60 |
Warnings := true. |
|
61 |
WarnSTXSpecials := true. |
|
62 |
WarnUnderscoreInIdentifier := true. |
|
87 | 63 |
WarnOldStyleAssignment := true. |
60 | 64 |
AllowUnderscoreInIdentifier := false. |
65 |
! |
|
66 |
||
33 | 67 |
setupActions |
68 |
|block| |
|
69 |
||
70 |
ActionArray := Array new:256. |
|
71 |
TypeArray := Array new:256. |
|
72 |
||
73 |
block := [:s :char | s nextNumber]. |
|
74 |
($0 asciiValue) to:($9 asciiValue) do:[:index | |
|
45 | 75 |
ActionArray at:index put:block |
33 | 76 |
]. |
77 |
||
87 | 78 |
block := [:s :char | s nextSpecial]. |
89 | 79 |
#( $& $- $+ $= $* $/ $\ $< $> $~ $@ $, $? "new:" $!! ) do:[:binop | |
87 | 80 |
TypeArray at:(binop asciiValue) put:#special. |
81 |
ActionArray at:(binop asciiValue) put:block |
|
82 |
]. |
|
83 |
||
84 |
block := [:s :char | s nextToken:char]. |
|
89 | 85 |
#( $; $. $( $) $[ $] "$!!" $^ $| $_ ) do:[:ch | |
87 | 86 |
ActionArray at:(ch asciiValue) put:block |
87 |
]. |
|
88 |
||
33 | 89 |
block := [:s :char | s nextIdentifier]. |
90 |
($a asciiValue) to:($z asciiValue) do:[:index | |
|
45 | 91 |
ActionArray at:index put:block |
33 | 92 |
]. |
93 |
($A asciiValue) to:($Z asciiValue) do:[:index | |
|
45 | 94 |
ActionArray at:index put:block |
33 | 95 |
]. |
71 | 96 |
AllowUnderscoreInIdentifier ifTrue:[ |
97 |
ActionArray at:$_ asciiValue put:block |
|
98 |
]. |
|
33 | 99 |
|
100 |
"kludge: action is characterToken, but type is special" |
|
101 |
TypeArray at:($| asciiValue) put:#special. |
|
102 |
||
80 | 103 |
"kludge: action is nextColonOrAssign, but type is special" |
104 |
TypeArray at:($: asciiValue) put:#special. |
|
105 |
||
33 | 106 |
ActionArray at:($' asciiValue) put:[:s :char | s nextString]. |
107 |
ActionArray at:($$ asciiValue) put:[:s :char | s nextCharacter]. |
|
108 |
ActionArray at:($# asciiValue) put:[:s :char | s nextHash]. |
|
109 |
ActionArray at:($% asciiValue) put:[:s :char | s nextPrimitive]. |
|
110 |
ActionArray at:($: asciiValue) put:[:s :char | s nextColonOrAssign] |
|
111 |
||
112 |
" |
|
113 |
Scanner setupActions |
|
114 |
" |
|
115 |
! ! |
|
116 |
||
141 | 117 |
!Scanner class methodsFor:'instance creation'! |
118 |
||
119 |
for:aStringOrStream |
|
120 |
"return a new scanner reading from aStringOrStream" |
|
121 |
||
122 |
^ (super new) initializeFor:aStringOrStream |
|
123 |
! ! |
|
124 |
||
125 |
!Scanner class methodsFor:'defaults'! |
|
126 |
||
127 |
allowUnderscoreInIdentifier |
|
128 |
"return true, if underscores are allowed in identifiers" |
|
129 |
||
130 |
^ AllowUnderscoreInIdentifier |
|
131 |
! |
|
132 |
||
133 |
allowUnderscoreInIdentifier:aBoolean |
|
134 |
"this allows turning on/off underscores in identifiers. |
|
135 |
If turned off (the default), underscores are not allowed in identifiers, |
|
136 |
but instead scanned as assignment character (old ST/80 syntax). |
|
137 |
If turned on, underscores are in identifiers are allowed, while extra |
|
138 |
underscores are still scanned as assignment. |
|
139 |
If you have to fileIn VV-Vsn2 classes, |
|
140 |
add a line such as: |
|
141 |
Compiler allowUnderscoreInIdentifiers:false |
|
142 |
in your 'private.rc'/'smalltalk.rc' file" |
|
143 |
||
144 |
AllowUnderscoreInIdentifier := aBoolean. |
|
145 |
self setupActions |
|
146 |
! |
|
147 |
||
148 |
warnOldStyleAssignment |
|
149 |
"return true, if underscore-assignment (pre ST-80v4 syntax) are to be warned about" |
|
150 |
||
151 |
^ WarnOldStyleAssignment |
|
152 |
! |
|
153 |
||
154 |
warnOldStyleAssignment:aBoolean |
|
155 |
"this allows turning on/off warnings about underscore-assignment (pre ST-80v4 syntax). |
|
156 |
If you get bored by those warnings, turn them off by adding |
|
157 |
a line as: |
|
158 |
Compiler warnOldStyleAssignment:false |
|
159 |
in your 'private.rc' file" |
|
160 |
||
161 |
WarnOldStyleAssignment := aBoolean |
|
162 |
! |
|
163 |
||
164 |
warnSTXSpecials |
|
165 |
"return true, if ST/X specials are to be warned about" |
|
166 |
||
167 |
^ WarnSTXSpecials |
|
168 |
! |
|
169 |
||
170 |
warnSTXSpecials:aBoolean |
|
171 |
"this allows turning on/off warnings about stx specials. |
|
172 |
If you get bored by those warnings, turn them off by adding |
|
173 |
a line as: |
|
174 |
Compiler warnSTXSpecials:false |
|
175 |
in your 'private.rc' file" |
|
176 |
||
177 |
WarnSTXSpecials := aBoolean |
|
178 |
! |
|
179 |
||
180 |
warnUnderscoreInIdentifier |
|
181 |
"return true, if underscores in identifiers are to be warned about" |
|
182 |
||
183 |
^ WarnUnderscoreInIdentifier |
|
184 |
! |
|
185 |
||
186 |
warnUnderscoreInIdentifier:aBoolean |
|
187 |
"this allows turning on/off warnings about underscores in identifiers. |
|
188 |
If you get bored by those warnings, turn them off by adding |
|
189 |
a line as: |
|
190 |
Compiler warnUnderscoreInIdentifier:false |
|
191 |
in your 'private.rc' file" |
|
192 |
||
193 |
WarnUnderscoreInIdentifier := aBoolean |
|
194 |
! |
|
195 |
||
196 |
warnings |
|
197 |
"return true, if any warnings are to be shown" |
|
198 |
||
199 |
^ Warnings |
|
200 |
! |
|
201 |
||
202 |
warnings:aBoolean |
|
203 |
"this allows turning on/off warnings; the default is on. |
|
204 |
You can turn off warnings in your 'private.rc' file with |
|
205 |
Compiler warnings:false |
|
206 |
" |
|
207 |
||
208 |
Warnings := aBoolean |
|
209 |
! ! |
|
210 |
||
98 | 211 |
!Scanner methodsFor:'ST-80 compatibility'! |
212 |
||
213 |
endOfLastToken |
|
214 |
^ source position |
|
215 |
! ! |
|
216 |
||
254
edfcf93d821f
changes to allow parsing for comments
Claus Gittinger <cg@exept.de>
parents:
241
diff
changeset
|
217 |
!Scanner methodsFor:'accessing'! |
edfcf93d821f
changes to allow parsing for comments
Claus Gittinger <cg@exept.de>
parents:
241
diff
changeset
|
218 |
|
edfcf93d821f
changes to allow parsing for comments
Claus Gittinger <cg@exept.de>
parents:
241
diff
changeset
|
219 |
comments |
edfcf93d821f
changes to allow parsing for comments
Claus Gittinger <cg@exept.de>
parents:
241
diff
changeset
|
220 |
^ currentComments |
edfcf93d821f
changes to allow parsing for comments
Claus Gittinger <cg@exept.de>
parents:
241
diff
changeset
|
221 |
|
edfcf93d821f
changes to allow parsing for comments
Claus Gittinger <cg@exept.de>
parents:
241
diff
changeset
|
222 |
"Created: 20.4.1996 / 20:07:01 / cg" |
edfcf93d821f
changes to allow parsing for comments
Claus Gittinger <cg@exept.de>
parents:
241
diff
changeset
|
223 |
! |
edfcf93d821f
changes to allow parsing for comments
Claus Gittinger <cg@exept.de>
parents:
241
diff
changeset
|
224 |
|
edfcf93d821f
changes to allow parsing for comments
Claus Gittinger <cg@exept.de>
parents:
241
diff
changeset
|
225 |
saveComments:aBoolean |
edfcf93d821f
changes to allow parsing for comments
Claus Gittinger <cg@exept.de>
parents:
241
diff
changeset
|
226 |
saveComments := aBoolean |
edfcf93d821f
changes to allow parsing for comments
Claus Gittinger <cg@exept.de>
parents:
241
diff
changeset
|
227 |
|
edfcf93d821f
changes to allow parsing for comments
Claus Gittinger <cg@exept.de>
parents:
241
diff
changeset
|
228 |
"Created: 20.4.1996 / 20:03:56 / cg" |
edfcf93d821f
changes to allow parsing for comments
Claus Gittinger <cg@exept.de>
parents:
241
diff
changeset
|
229 |
! |
edfcf93d821f
changes to allow parsing for comments
Claus Gittinger <cg@exept.de>
parents:
241
diff
changeset
|
230 |
|
edfcf93d821f
changes to allow parsing for comments
Claus Gittinger <cg@exept.de>
parents:
241
diff
changeset
|
231 |
sourceStream |
edfcf93d821f
changes to allow parsing for comments
Claus Gittinger <cg@exept.de>
parents:
241
diff
changeset
|
232 |
^ source |
edfcf93d821f
changes to allow parsing for comments
Claus Gittinger <cg@exept.de>
parents:
241
diff
changeset
|
233 |
|
edfcf93d821f
changes to allow parsing for comments
Claus Gittinger <cg@exept.de>
parents:
241
diff
changeset
|
234 |
"Created: 20.4.1996 / 19:59:58 / cg" |
edfcf93d821f
changes to allow parsing for comments
Claus Gittinger <cg@exept.de>
parents:
241
diff
changeset
|
235 |
! ! |
edfcf93d821f
changes to allow parsing for comments
Claus Gittinger <cg@exept.de>
parents:
241
diff
changeset
|
236 |
|
81 | 237 |
!Scanner methodsFor:'directives'! |
238 |
||
74 | 239 |
parseDirective |
81 | 240 |
|directive packageName namespace list| |
74 | 241 |
|
242 |
source next. |
|
243 |
source skipSeparatorsExceptCR. |
|
244 |
hereChar := source peek. |
|
245 |
hereChar isLetter ifTrue:[ |
|
246 |
directive := source nextWord. |
|
247 |
source peek == $: ifTrue:[ |
|
248 |
source next. |
|
249 |
source skipSeparatorsExceptCR. |
|
250 |
hereChar := source peek. |
|
251 |
||
252 |
" |
|
253 |
Package: 'name-of-package' |
|
254 |
" |
|
255 |
directive = 'Package' ifTrue:[ |
|
81 | 256 |
packageName := self parseDirectiveStringArg. |
257 |
packageName notNil ifTrue:[ |
|
258 |
(requestor respondsTo:#setPackage:) ifTrue:[ |
|
259 |
requestor setPackage:packageName |
|
260 |
]. |
|
74 | 261 |
]. |
262 |
]. |
|
263 |
||
264 |
" |
|
81 | 265 |
Namespace: 'name-of-package' |
266 |
" |
|
267 |
directive = 'Namespace' ifTrue:[ |
|
268 |
namespace := self parseDirectiveStringArg. |
|
269 |
namespace notNil ifTrue:[ |
|
270 |
(requestor respondsTo:#setNameSpace:) ifTrue:[ |
|
271 |
requestor setNameSpace:namespace |
|
272 |
]. |
|
273 |
]. |
|
274 |
]. |
|
275 |
||
276 |
" |
|
277 |
Uses: 'nameSpace1', ... , 'nameSpace2' |
|
278 |
" |
|
279 |
directive = 'Uses' ifTrue:[ |
|
280 |
list := self parseDirectiveStringListArg. |
|
281 |
(requestor respondsTo:#useNameSpaces:) ifTrue:[ |
|
282 |
requestor useNameSpaces:list |
|
283 |
]. |
|
284 |
]. |
|
285 |
" |
|
74 | 286 |
Prerequisites: 'name-of-package', ... , 'name-of-package' |
287 |
" |
|
288 |
directive = 'Prerequisites' ifTrue:[ |
|
81 | 289 |
list := self parseDirectiveStringListArg. |
290 |
(requestor respondsTo:#requirePackages:) ifTrue:[ |
|
291 |
requestor requirePackages:list |
|
74 | 292 |
]. |
293 |
]. |
|
294 |
] |
|
295 |
]. |
|
296 |
hereChar := source peek. |
|
141 | 297 |
! |
298 |
||
299 |
parseDirectiveStringArg |
|
300 |
hereChar == $' ifTrue:[ |
|
301 |
self nextString. |
|
302 |
tokenType == #String ifTrue:[ |
|
303 |
^ tokenValue |
|
304 |
] |
|
305 |
]. |
|
306 |
^ nil |
|
307 |
! |
|
308 |
||
309 |
parseDirectiveStringListArg |
|
310 |
|list| |
|
311 |
||
312 |
list := OrderedCollection new. |
|
313 |
||
314 |
[hereChar == $'] whileTrue:[ |
|
315 |
list addLast:self parseDirectiveStringArg. |
|
316 |
source skipSeparatorsExceptCR. |
|
317 |
hereChar := source peek. |
|
318 |
(hereChar == $,) ifTrue:[ |
|
319 |
source next. |
|
320 |
source skipSeparatorsExceptCR. |
|
321 |
hereChar := source peek. |
|
322 |
]. |
|
323 |
]. |
|
324 |
^ list |
|
0 | 325 |
! ! |
326 |
||
327 |
!Scanner methodsFor:'error handling'! |
|
328 |
||
329 |
notifyError:aMessage position:position to:endPos |
|
330 |
"notify requestor of an error - if there is no requestor |
|
53 | 331 |
put it on the transcript. Requestor is typically the CodeView |
332 |
in which the accept/doIt was triggered, or the PositionableStream |
|
333 |
which does the fileIn. The requestor may decide how to highlight the |
|
334 |
error (and/or to abort the compile). |
|
0 | 335 |
Return the result passed back by the requestor." |
336 |
||
53 | 337 |
ignoreErrors ifFalse:[ |
338 |
requestor isNil ifTrue:[ |
|
339 |
self showErrorMessage:aMessage position:position. |
|
340 |
^ false |
|
341 |
]. |
|
83 | 342 |
^ requestor error:aMessage position:position to:endPos from:self |
0 | 343 |
]. |
53 | 344 |
^ false |
0 | 345 |
! |
346 |
||
347 |
notifyWarning:aMessage position:position to:endPos |
|
348 |
"notify requestor of an warning - if there is no requestor |
|
349 |
put it on the transcript. |
|
350 |
Return the result passed back by the requestor." |
|
351 |
||
53 | 352 |
ignoreWarnings ifFalse:[ |
353 |
requestor isNil ifTrue:[ |
|
45 | 354 |
self showErrorMessage:aMessage position:position. |
53 | 355 |
^ false |
45 | 356 |
]. |
83 | 357 |
^ requestor warning:aMessage position:position to:endPos from:self |
0 | 358 |
]. |
53 | 359 |
^ false |
0 | 360 |
! |
361 |
||
141 | 362 |
showErrorMessage:aMessage position:pos |
363 |
"show an errormessage on the Transcript" |
|
0 | 364 |
|
141 | 365 |
ignoreErrors ifFalse:[ |
366 |
Smalltalk silentLoading == true ifFalse:[ |
|
367 |
Transcript showCr:(pos printString , ' [line: ' , tokenLineNr printString , '] ' , aMessage) |
|
368 |
] |
|
369 |
] |
|
370 |
! |
|
371 |
||
372 |
syntaxError:aMessage |
|
373 |
"a syntax error happened - position is not known" |
|
374 |
||
375 |
^ self syntaxError:aMessage position:tokenPosition |
|
0 | 376 |
! |
377 |
||
378 |
syntaxError:aMessage position:position |
|
379 |
"a syntax error happened - only start position is known" |
|
380 |
||
381 |
^ self syntaxError:aMessage position:position to:nil |
|
382 |
! |
|
383 |
||
141 | 384 |
syntaxError:aMessage position:position to:endPos |
385 |
"a syntax error happened" |
|
0 | 386 |
|
141 | 387 |
self notifyError:('Error:' , aMessage) position:position to:endPos. |
388 |
exitBlock notNil ifTrue:[exitBlock value]. |
|
389 |
^ false |
|
87 | 390 |
! |
391 |
||
392 |
warnOldStyleAssignmentAt:position |
|
393 |
ignoreWarnings ifFalse:[ |
|
394 |
warnOldStyleAssignment ifTrue:[ |
|
395 |
self |
|
396 |
warning:'old style assignment - please change to use '':=''' |
|
397 |
position:position to:position. |
|
398 |
" |
|
399 |
only warn once (per method) |
|
400 |
" |
|
401 |
warnOldStyleAssignment := false |
|
402 |
] |
|
403 |
] |
|
404 |
! |
|
405 |
||
406 |
warnUnderscoreAt:position |
|
407 |
ignoreWarnings ifFalse:[ |
|
408 |
warnUnderscoreInIdentifier ifTrue:[ |
|
409 |
self |
|
410 |
warning:'underscores in identifiers/symbols are nonportable' |
|
411 |
position:position to:position. |
|
412 |
" |
|
413 |
only warn once (per method) |
|
414 |
" |
|
415 |
warnUnderscoreInIdentifier := false |
|
416 |
] |
|
417 |
] |
|
141 | 418 |
! |
419 |
||
420 |
warning:aMessage |
|
421 |
"a warning - position is not known" |
|
422 |
||
423 |
^ self warning:aMessage position:tokenPosition |
|
424 |
! |
|
425 |
||
426 |
warning:aMessage position:position |
|
427 |
"a warning - only start position is known" |
|
428 |
||
429 |
^ self warning:aMessage position:position to:nil |
|
430 |
! |
|
431 |
||
432 |
warning:aMessage position:position to:endPos |
|
433 |
"a warning" |
|
434 |
||
435 |
^ self notifyWarning:('Warning: ' , aMessage) position:position to:endPos |
|
0 | 436 |
! ! |
437 |
||
17 | 438 |
!Scanner methodsFor:'general scanning'! |
439 |
||
440 |
scanPositionsFor:aTokenString inString:aSourceString |
|
441 |
"scan aSourceString for occurrances of aTokenString. |
|
442 |
Return a collection of start positions. |
|
443 |
Added for VW compatibility (to support syntax-highlight)." |
|
444 |
||
445 |
|searchType searchName searchValue positions t| |
|
446 |
||
447 |
" |
|
448 |
first, look what kind of token we have to search for |
|
449 |
" |
|
450 |
self initializeFor:(ReadStream on:aTokenString). |
|
451 |
self nextToken. |
|
452 |
searchType := tokenType. |
|
453 |
searchName := tokenName. |
|
454 |
searchValue := tokenValue. |
|
455 |
||
456 |
" |
|
457 |
start the real work ... |
|
458 |
" |
|
459 |
self initializeFor:(ReadStream on:aSourceString). |
|
460 |
positions := OrderedCollection new. |
|
461 |
||
462 |
[(t := self nextToken) ~~ #EOF] whileTrue:[ |
|
45 | 463 |
searchType == t ifTrue:[ |
464 |
(searchName isNil or:[tokenName = searchName]) ifTrue:[ |
|
465 |
(searchValue isNil or:[tokenValue = searchValue]) ifTrue:[ |
|
466 |
positions add:tokenPosition. |
|
467 |
] |
|
468 |
] |
|
469 |
] |
|
17 | 470 |
]. |
471 |
||
472 |
^ positions |
|
473 |
||
474 |
" |
|
30 | 475 |
Scanner new scanPositionsFor:'hello' inString:'foo bar hello baz hello' |
476 |
Scanner new scanPositionsFor:'3.14' inString:'foo 3.145 bar hello 3.14 baz hello 3.14' |
|
477 |
Scanner new scanPositionsFor:'16' inString:'foo 16 bar hello 16r10 baz hello 2r10000' |
|
17 | 478 |
" |
479 |
! ! |
|
480 |
||
141 | 481 |
!Scanner methodsFor:'private'! |
3 | 482 |
|
141 | 483 |
backupPosition |
484 |
"if reading from a stream, at the end we might have read |
|
485 |
one token too many" |
|
20 | 486 |
|
141 | 487 |
(tokenType == #EOF) ifFalse:[ |
488 |
source position:tokenPosition |
|
489 |
] |
|
490 |
! |
|
15 | 491 |
|
141 | 492 |
beginComment |
493 |
^ self |
|
494 |
! |
|
495 |
||
496 |
collectedSource |
|
497 |
^ collectedSource |
|
498 |
! |
|
74 | 499 |
|
141 | 500 |
endComment:comment |
501 |
saveComments ifTrue:[ |
|
502 |
currentComments isNil ifTrue:[ |
|
503 |
currentComments := OrderedCollection with:comment |
|
45 | 504 |
] ifFalse:[ |
141 | 505 |
currentComments add:comment |
45 | 506 |
] |
3 | 507 |
]. |
508 |
! |
|
509 |
||
141 | 510 |
ignoreErrors |
511 |
"turn off notification of errors" |
|
512 |
||
513 |
ignoreErrors := true |
|
514 |
! |
|
0 | 515 |
|
141 | 516 |
ignoreWarnings |
517 |
"turn off notification of warnings" |
|
518 |
||
519 |
ignoreWarnings := true |
|
520 |
! |
|
0 | 521 |
|
141 | 522 |
initialize |
523 |
"prepare a scan" |
|
3 | 524 |
|
141 | 525 |
errorFlag := false. |
526 |
tokenLineNr := 1. |
|
527 |
currentComments := nil. |
|
528 |
saveComments := false. |
|
529 |
ignoreErrors := false. |
|
530 |
ignoreWarnings := Warnings not. |
|
531 |
warnSTXSpecialComment := WarnSTXSpecials. |
|
532 |
warnUnderscoreInIdentifier := WarnUnderscoreInIdentifier. |
|
533 |
warnOldStyleAssignment := WarnOldStyleAssignment. |
|
534 |
ActionArray isNil ifTrue:[ |
|
535 |
self class setupActions |
|
536 |
] |
|
0 | 537 |
! |
538 |
||
141 | 539 |
initializeFor:aStringOrStream |
540 |
"initialize - |
|
541 |
if this is the first time, setup character- and action tables" |
|
542 |
||
543 |
self initialize. |
|
544 |
||
545 |
aStringOrStream isStream ifFalse:[ |
|
546 |
source := ReadStream on:aStringOrStream |
|
547 |
] ifTrue:[ |
|
548 |
source := aStringOrStream. |
|
549 |
]. |
|
550 |
! |
|
551 |
||
552 |
notifying:anObject |
|
553 |
"set the requestor to be notified" |
|
554 |
||
555 |
requestor := anObject |
|
556 |
! ! |
|
557 |
||
558 |
!Scanner methodsFor:'reading next token'! |
|
559 |
||
560 |
nextCharacter |
|
561 |
"a $ has been read - return a character token" |
|
562 |
||
563 |
|nextChar| |
|
564 |
||
0 | 565 |
source next. |
141 | 566 |
nextChar := source next. |
567 |
nextChar notNil ifTrue:[ |
|
568 |
tokenValue := nextChar. |
|
569 |
tokenType := #Character |
|
570 |
] ifFalse:[ |
|
571 |
tokenType := #EOF |
|
572 |
]. |
|
0 | 573 |
^ tokenType |
574 |
! |
|
575 |
||
576 |
nextColonOrAssign |
|
577 |
"colon has been read - look for = to make it an assign" |
|
578 |
||
120 | 579 |
"/ special kludge for identifier:= (without spaces inbetween) |
580 |
"/ here we needed two characters lookahead after the identifier ... |
|
581 |
||
582 |
peekChar == $= ifTrue:[ |
|
583 |
source next. |
|
584 |
peekChar := nil. |
|
585 |
tokenType := #':='. |
|
586 |
^ tokenType |
|
587 |
]. |
|
588 |
||
0 | 589 |
(source nextPeek == $=) ifTrue:[ |
45 | 590 |
source next. |
591 |
tokenType := $_ |
|
0 | 592 |
] ifFalse:[ |
45 | 593 |
tokenType := $: |
0 | 594 |
]. |
595 |
^ tokenType |
|
596 |
! |
|
597 |
||
141 | 598 |
nextHash |
599 |
|nextChar string| |
|
0 | 600 |
|
141 | 601 |
nextChar := source nextPeek. |
602 |
nextChar notNil ifTrue:[ |
|
603 |
nextChar isLetterOrDigit ifTrue:[ |
|
604 |
string := ''. |
|
605 |
[nextChar notNil and:[nextChar isLetterOrDigit]] whileTrue:[ |
|
606 |
string := string , (source nextAlphaNumericWord "self nextId"). |
|
607 |
nextChar := source peek. |
|
608 |
AllowUnderscoreInIdentifier == true ifTrue:[ |
|
609 |
nextChar == $_ ifTrue:[ |
|
610 |
self warnUnderscoreAt:source position. |
|
611 |
]. |
|
612 |
[nextChar == $_] whileTrue:[ |
|
613 |
string := string copyWith:nextChar. |
|
614 |
nextChar := source nextPeek. |
|
615 |
(nextChar isAlphaNumeric) ifTrue:[ |
|
616 |
string := string , source nextAlphaNumericWord. |
|
617 |
nextChar := source peek. |
|
618 |
] |
|
619 |
]. |
|
620 |
]. |
|
621 |
(nextChar == $:) ifFalse:[ |
|
622 |
tokenValue := string asSymbol. |
|
623 |
tokenType := #Symbol. |
|
624 |
^ tokenType |
|
625 |
]. |
|
626 |
string := string copyWith:nextChar. |
|
627 |
nextChar := source nextPeek |
|
628 |
]. |
|
629 |
tokenValue := string asSymbol. |
|
630 |
tokenType := #Symbol. |
|
631 |
^ tokenType |
|
632 |
]. |
|
633 |
(nextChar == $( ) ifTrue:[ |
|
634 |
source next. |
|
635 |
tokenType := #HashLeftParen. |
|
636 |
^ tokenType |
|
637 |
]. |
|
638 |
(nextChar == $[ ) ifTrue:[ |
|
639 |
"it seems that ST-80 supports Constant ByteArrays as #[...] |
|
640 |
(seen in a PD program)" |
|
641 |
source next. |
|
642 |
tokenType := #HashLeftBrack. |
|
643 |
^ tokenType |
|
644 |
]. |
|
645 |
(nextChar == $' ) ifTrue:[ |
|
646 |
"it seems that ST-80 supports arbitrary symbols as #'...' |
|
647 |
(seen in a PD program)" |
|
648 |
self nextString. |
|
649 |
tokenValue := tokenValue asSymbol. |
|
650 |
tokenType := #Symbol. |
|
651 |
^ tokenType |
|
652 |
]. |
|
653 |
((TypeArray at:(nextChar asciiValue)) == #special) ifTrue:[ |
|
654 |
string := source next asString. |
|
655 |
nextChar := source peek. |
|
656 |
nextChar notNil ifTrue:[ |
|
657 |
((TypeArray at:(nextChar asciiValue)) == #special) ifTrue:[ |
|
658 |
source next. |
|
659 |
string := string copyWith:nextChar |
|
660 |
] |
|
661 |
]. |
|
662 |
tokenValue := string asSymbol. |
|
663 |
tokenType := #Symbol. |
|
45 | 664 |
^ tokenType |
665 |
] |
|
0 | 666 |
]. |
141 | 667 |
"this allows hash to be used as binop - |
668 |
I dont know, if this is correct ..." |
|
669 |
||
670 |
tokenName := '#'. |
|
0 | 671 |
tokenType := #BinaryOperator. |
672 |
^ tokenType |
|
141 | 673 |
" |
674 |
self syntaxError:'unexpected end-of-input in Symbol' |
|
675 |
position:tokenPosition to:(tokenPosition + 1). |
|
676 |
^ #Error |
|
677 |
" |
|
0 | 678 |
! |
679 |
||
680 |
nextId |
|
681 |
|nextChar string oldString |
|
682 |
index "{ Class: SmallInteger }" |
|
683 |
max "{ Class: SmallInteger }" | |
|
684 |
||
685 |
nextChar := source peek. |
|
96 | 686 |
string := String basicNew:20. |
0 | 687 |
index := 0. |
688 |
max := 10. |
|
689 |
[true] whileTrue:[ |
|
45 | 690 |
(nextChar notNil and:[nextChar isLetterOrDigit]) ifFalse:[ |
691 |
^ string copyTo:index |
|
692 |
]. |
|
693 |
(index == max) ifTrue:[ |
|
694 |
oldString := string. |
|
96 | 695 |
string := String basicNew:(max * 2). |
45 | 696 |
string replaceFrom:1 to:max with:oldString. |
697 |
max := max * 2 |
|
698 |
]. |
|
699 |
index := index + 1. |
|
700 |
string at:index put:nextChar. |
|
701 |
nextChar := source nextPeek |
|
0 | 702 |
] |
703 |
! |
|
704 |
||
705 |
nextIdentifier |
|
97 | 706 |
|nextChar string firstChar| |
0 | 707 |
|
87 | 708 |
hereChar == $_ ifTrue:[ |
709 |
"/ |
|
710 |
"/ no need to check for AllowUnderscoreInIdentifier here; |
|
711 |
"/ could not arrive here if it was off |
|
712 |
"/ |
|
713 |
nextChar := source nextPeek. |
|
714 |
(nextChar isAlphaNumeric or:[nextChar == $_]) ifFalse:[ |
|
715 |
"oops: a single underscore is an old-style assignement" |
|
716 |
self warnOldStyleAssignmentAt:tokenPosition. |
|
717 |
source next. |
|
718 |
tokenType := $_. |
|
719 |
^ tokenType |
|
720 |
]. |
|
721 |
string := '_'. |
|
722 |
self warnUnderscoreAt:tokenPosition. |
|
723 |
[nextChar == '_'] whileTrue:[ |
|
724 |
string := string copyWith:$_. |
|
725 |
nextChar := source nextPeek. |
|
726 |
]. |
|
727 |
string := string , source nextAlphaNumericWord. |
|
728 |
] ifFalse:[ |
|
729 |
string := source nextAlphaNumericWord "self nextId". |
|
730 |
]. |
|
0 | 731 |
nextChar := source peek. |
87 | 732 |
|
60 | 733 |
AllowUnderscoreInIdentifier ifTrue:[ |
734 |
nextChar == $_ ifTrue:[ |
|
87 | 735 |
self warnUnderscoreAt:(source position). |
60 | 736 |
]. |
737 |
[nextChar == $_] whileTrue:[ |
|
738 |
string := string copyWith:nextChar. |
|
739 |
nextChar := source nextPeek. |
|
740 |
(nextChar isAlphaNumeric) ifTrue:[ |
|
741 |
string := string , source nextAlphaNumericWord. |
|
742 |
nextChar := source peek. |
|
743 |
] |
|
744 |
]. |
|
745 |
]. |
|
746 |
||
0 | 747 |
(nextChar == $:) ifTrue:[ |
45 | 748 |
source next. |
749 |
(source peek == $=) ifFalse:[ |
|
750 |
tokenName := string copyWith:nextChar. |
|
751 |
tokenType := #Keyword. |
|
752 |
^ self |
|
753 |
]. |
|
120 | 754 |
peekChar := $:. |
755 |
peekChar2 := $=. |
|
0 | 756 |
]. |
20 | 757 |
tokenName := string. |
0 | 758 |
firstChar := string at:1. |
759 |
(firstChar == $s) ifTrue:[ |
|
45 | 760 |
(string = 'self') ifTrue:[tokenType := #Self. ^self]. |
761 |
(string = 'super') ifTrue:[tokenType := #Super. ^self] |
|
0 | 762 |
]. |
763 |
(firstChar == $n) ifTrue:[ |
|
45 | 764 |
(string = 'nil') ifTrue:[tokenType := #Nil. ^self] |
0 | 765 |
]. |
766 |
(firstChar == $t) ifTrue:[ |
|
45 | 767 |
(string = 'true') ifTrue:[tokenType := #True. ^self]. |
768 |
(string = 'thisContext') ifTrue:[tokenType := #ThisContext. ^self] |
|
0 | 769 |
]. |
770 |
(firstChar == $f) ifTrue:[ |
|
45 | 771 |
(string = 'false') ifTrue:[tokenType := #False. ^self] |
0 | 772 |
]. |
773 |
tokenType := #Identifier. |
|
774 |
^ tokenType |
|
120 | 775 |
|
776 |
"Created: 13.9.1995 / 12:56:42 / claus" |
|
0 | 777 |
! |
778 |
||
141 | 779 |
nextMantissa:radix |
780 |
|nextChar value factor| |
|
781 |
||
782 |
value := 0. |
|
783 |
factor := 1.0 / radix. |
|
784 |
nextChar := source peek. |
|
785 |
[(nextChar notNil and:[nextChar isDigitRadix:radix])] whileTrue:[ |
|
786 |
value := value + (nextChar digitValue * factor). |
|
787 |
factor := factor / radix. |
|
788 |
nextChar := source nextPeek |
|
789 |
]. |
|
790 |
^ value |
|
791 |
! |
|
792 |
||
793 |
nextNumber |
|
794 |
|nextChar value s| |
|
795 |
||
796 |
tokenRadix := 10. |
|
797 |
value := Integer readFrom:source radix:tokenRadix. |
|
798 |
nextChar := source peek. |
|
799 |
(nextChar == $r) ifTrue:[ |
|
800 |
tokenRadix := value. |
|
801 |
source next. |
|
802 |
s := 1. |
|
803 |
source peek == $- ifTrue:[ |
|
804 |
source next. |
|
805 |
s := -1 |
|
806 |
]. |
|
807 |
value := Integer readFrom:source radix:tokenRadix. |
|
808 |
value := value * s. |
|
809 |
nextChar := source peek |
|
810 |
]. |
|
811 |
(nextChar == $.) ifTrue:[ |
|
812 |
nextChar := source nextPeek. |
|
813 |
(nextChar notNil and:[nextChar isDigitRadix:tokenRadix]) ifTrue:[ |
|
814 |
value := value asFloat + (self nextMantissa:tokenRadix). |
|
815 |
nextChar := source peek |
|
816 |
] ifFalse:[ |
|
817 |
nextChar == (Character cr) ifTrue:[ |
|
818 |
tokenLineNr := tokenLineNr + 1. |
|
819 |
]. |
|
820 |
peekChar := $. |
|
821 |
] |
|
822 |
]. |
|
823 |
((nextChar == $e) or:[nextChar == $E]) ifTrue:[ |
|
824 |
nextChar := source nextPeek. |
|
825 |
(nextChar notNil and:[(nextChar isDigitRadix:tokenRadix) or:['+-' includes:nextChar]]) ifTrue:[ |
|
826 |
s := 1. |
|
827 |
(nextChar == $+) ifTrue:[ |
|
828 |
nextChar := source nextPeek |
|
829 |
] ifFalse:[ |
|
830 |
(nextChar == $-) ifTrue:[ |
|
831 |
nextChar := source nextPeek. |
|
832 |
s := s negated |
|
833 |
] |
|
834 |
]. |
|
835 |
value := value asFloat |
|
836 |
* (10.0 raisedToInteger:((Integer readFrom:source radix:tokenRadix) * s)) |
|
837 |
] |
|
838 |
]. |
|
839 |
tokenValue := value. |
|
840 |
(value isMemberOf:Float) ifTrue:[ |
|
841 |
tokenType := #Float |
|
842 |
] ifFalse:[ |
|
843 |
tokenType := #Integer |
|
844 |
]. |
|
845 |
^ tokenType |
|
846 |
! |
|
847 |
||
0 | 848 |
nextPrimitive |
849 |
|nextChar inPrimitive string |
|
850 |
index "{ Class: SmallInteger }" |
|
851 |
len "{ Class: SmallInteger }" | |
|
852 |
||
853 |
nextChar := source nextPeek. |
|
854 |
string := String new:500. |
|
855 |
len := 500. |
|
856 |
index := 1. |
|
857 |
(nextChar == ${) ifTrue:[ |
|
45 | 858 |
nextChar := source nextPeek. |
859 |
inPrimitive := true. |
|
860 |
[inPrimitive] whileTrue:[ |
|
861 |
[nextChar == $%] whileFalse:[ |
|
862 |
string at:index put:nextChar. |
|
863 |
(index == len) ifTrue:[ |
|
864 |
string := string , (String new:len). |
|
865 |
len := len * 2 |
|
866 |
]. |
|
867 |
index := index + 1. |
|
868 |
nextChar := source next |
|
869 |
]. |
|
870 |
(source peek == $}) ifTrue:[ |
|
871 |
inPrimitive := false |
|
872 |
] ifFalse:[ |
|
873 |
string at:index put:nextChar. |
|
874 |
(index == len) ifTrue:[ |
|
875 |
string := string , (String new:len). |
|
876 |
len := len * 2 |
|
877 |
]. |
|
878 |
index := index + 1. |
|
879 |
nextChar := source next |
|
880 |
] |
|
881 |
]. |
|
882 |
source next. |
|
883 |
tokenValue := string copyTo:(index - 1). |
|
884 |
tokenType := #Primitive. |
|
885 |
tokenLineNr := tokenLineNr + (tokenValue occurrencesOf:(Character cr)). |
|
886 |
^ tokenType |
|
0 | 887 |
]. |
888 |
||
889 |
"a % alone is a binary operator" |
|
890 |
tokenName := '%'. |
|
891 |
tokenType := #BinaryOperator. |
|
892 |
^ tokenType. |
|
893 |
" |
|
894 |
self syntaxError:('invalid character: ''' , nextChar asString , '''') |
|
45 | 895 |
position:tokenPosition to:(tokenPosition + 1). |
0 | 896 |
^ #Error |
897 |
" |
|
898 |
! |
|
899 |
||
141 | 900 |
nextSpecial |
901 |
"a special character has been read, look for another one. |
|
902 |
also -number is handled here" |
|
903 |
||
904 |
|firstChar secondChar thirdChar string p| |
|
0 | 905 |
|
141 | 906 |
firstChar := source next. |
907 |
secondChar := source peek. |
|
241
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
908 |
((firstChar == $-) and:[secondChar notNil]) ifTrue:[ |
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
909 |
secondChar isDigit ifTrue:[ |
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
910 |
self nextNumber. |
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
911 |
tokenValue := tokenValue negated. |
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
912 |
^ tokenType |
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
913 |
] |
0 | 914 |
]. |
141 | 915 |
string := firstChar asString. |
916 |
secondChar notNil ifTrue:[ |
|
241
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
917 |
((TypeArray at:(secondChar asciiValue)) == #special) ifTrue:[ |
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
918 |
(secondChar == $-) ifTrue:[ |
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
919 |
"special- look if minus belongs to number following" |
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
920 |
p := source position. |
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
921 |
source next. |
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
922 |
thirdChar := source peek. |
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
923 |
source position:p. |
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
924 |
thirdChar isDigit ifTrue:[ |
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
925 |
tokenName := string. |
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
926 |
tokenType := #BinaryOperator. |
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
927 |
^ tokenType |
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
928 |
] |
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
929 |
]. |
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
930 |
source next. |
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
931 |
string := string copyWith:secondChar |
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
932 |
]. |
141 | 933 |
]. |
934 |
tokenName := string. |
|
45 | 935 |
tokenType := #BinaryOperator. |
3 | 936 |
^ tokenType |
241
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
937 |
|
418eb41350d3
no debugger when parsing '-' (which is wrong anyway ...)
Claus Gittinger <cg@exept.de>
parents:
148
diff
changeset
|
938 |
"Modified: 12.4.1996 / 15:05:19 / cg" |
0 | 939 |
! |
940 |
||
263 | 941 |
pos |
0 | 942 |
index "{ Class: SmallInteger }" |
943 |
len "{ Class: SmallInteger }" |
|
944 |
inString| |
|
945 |
||
96 | 946 |
string := String basicNew:20. |
0 | 947 |
len := 20. |
948 |
index := 1. |
|
949 |
pos := source position. |
|
950 |
source next. |
|
951 |
nextChar := source next. |
|
952 |
inString := true. |
|
953 |
||
954 |
[inString] whileTrue:[ |
|
45 | 955 |
nextChar isNil ifTrue:[ |
956 |
self syntaxError:'unexpected end-of-input in String' |
|
957 |
position:pos to:(source position - 1). |
|
958 |
tokenType := #EOF. |
|
959 |
^ tokenType |
|
960 |
]. |
|
961 |
(nextChar == Character cr) ifTrue:[ |
|
962 |
tokenLineNr := tokenLineNr + 1 |
|
963 |
]. |
|
964 |
(nextChar == Character quote) ifTrue:[ |
|
965 |
(source peek == Character quote) ifTrue:[ |
|
966 |
source next |
|
967 |
] ifFalse:[ |
|
968 |
inString := false |
|
969 |
] |
|
970 |
]. |
|
971 |
inString ifTrue:[ |
|
972 |
string at:index put:nextChar. |
|
973 |
(index == len) ifTrue:[ |
|
974 |
string := string , (String new:len). |
|
975 |
len := len * 2 |
|
976 |
]. |
|
977 |
index := index + 1. |
|
978 |
nextChar := source next |
|
979 |
] |
|
0 | 980 |
]. |
15 | 981 |
tokenValue := string copyTo:(index - 1). |
0 | 982 |
tokenType := #String. |
983 |
^ tokenType |
|
141 | 984 |
! |
985 |
||
263 | 986 |
oken from the source-stream" |
141 | 987 |
|
988 |
|skipping actionBlock| |
|
989 |
||
990 |
peekChar notNil ifTrue:[ |
|
991 |
hereChar := peekChar. |
|
992 |
peekChar := peekChar2. |
|
993 |
peekChar2 := nil |
|
994 |
] ifFalse:[ |
|
995 |
skipping := true. |
|
996 |
[skipping] whileTrue:[ |
|
997 |
outStream notNil ifTrue:[ |
|
998 |
[(hereChar := source peek) == Character space] whileTrue:[ |
|
999 |
source next. |
|
1000 |
outStream space. |
|
1001 |
outCol := outCol + 1. |
|
1002 |
] |
|
1003 |
] ifFalse:[ |
|
1004 |
hereChar := source skipSeparatorsExceptCR. |
|
1005 |
]. |
|
1006 |
hereChar == (Character cr) ifTrue:[ |
|
1007 |
tokenLineNr := tokenLineNr + 1. |
|
1008 |
source next. |
|
1009 |
outStream notNil ifTrue:[ |
|
1010 |
outStream cr. |
|
1011 |
outCol := 1 |
|
1012 |
] |
|
1013 |
] ifFalse:[ |
|
1014 |
hereChar == (Character doubleQuote) ifTrue:[ |
|
1015 |
"start of a comment" |
|
1016 |
||
1017 |
self skipComment. |
|
1018 |
hereChar := source peek. |
|
1019 |
] ifFalse:[ |
|
1020 |
skipping := false |
|
1021 |
] |
|
1022 |
] |
|
1023 |
]. |
|
1024 |
hereChar isNil ifTrue:[ |
|
1025 |
tokenType := #EOF. |
|
1026 |
^ tokenType |
|
1027 |
] |
|
1028 |
]. |
|
1029 |
tokenPosition := source position. |
|
1030 |
||
1031 |
actionBlock := ActionArray at:(hereChar asciiValue). |
|
1032 |
actionBlock notNil ifTrue:[ |
|
1033 |
^ actionBlock value:self value:hereChar |
|
1034 |
]. |
|
1035 |
||
1036 |
self syntaxError:('invalid character: ''' , hereChar asString , ''' ', |
|
1037 |
'(' , hereChar asciiValue printString , ')') |
|
1038 |
position:tokenPosition to:tokenPosition. |
|
1039 |
tokenType := #Error. |
|
1040 |
^ #Error |
|
1041 |
||
1042 |
"Modified: 13.9.1995 / 12:56:14 / claus" |
|
1043 |
! |
|
1044 |
||
263 | 1045 |
pe := aCharacter. |
141 | 1046 |
source next. |
1047 |
^ tokenType |
|
1048 |
! |
|
1049 |
||
263 | 1050 |
tartPos| |
141 | 1051 |
|
1052 |
saveComments ifTrue:[ |
|
1053 |
commentStream := WriteStream on:''. |
|
1054 |
self beginComment. |
|
1055 |
]. |
|
1056 |
||
1057 |
outStream notNil ifTrue:[ |
|
1058 |
outStream nextPut:Character doubleQuote. |
|
1059 |
outCol := outCol + 1 |
|
1060 |
]. |
|
1061 |
||
1062 |
startPos := source position. |
|
1063 |
source next. |
|
1064 |
hereChar := source peek. |
|
1065 |
||
1066 |
" |
|
1067 |
special ST/X addition: |
|
1068 |
a $/ right after the initial double quote makes it an up-to-end-of-line comment, |
|
1069 |
which is very useful to comment out parts of filed-in source code. |
|
1070 |
Since this is non-standard, use it in very rare cases only. |
|
1071 |
(maybe the upcoming ansi-standard adds something similar - in this case, I will |
|
1072 |
change it without notice)" |
|
1073 |
||
1074 |
hereChar == $/ ifTrue:[ |
|
1075 |
[hereChar notNil and:[hereChar ~~ Character cr]] whileTrue:[ |
|
1076 |
saveComments ifTrue:[ |
|
1077 |
commentStream nextPut:hereChar |
|
1078 |
]. |
|
1079 |
outStream notNil ifTrue:[ |
|
1080 |
outStream nextPut:hereChar. |
|
1081 |
outCol := outCol + 1 |
|
1082 |
]. |
|
1083 |
hereChar := source nextPeek. |
|
1084 |
]. |
|
1085 |
tokenLineNr := tokenLineNr + 1. |
|
1086 |
ignoreWarnings ifFalse:[ |
|
1087 |
warnSTXSpecialComment ifTrue:[ |
|
1088 |
self warning:'end-of-line comments are a nonstandard feature of ST/X' |
|
1089 |
position:startPos to:(source position). |
|
1090 |
" |
|
1091 |
only warn once |
|
1092 |
" |
|
1093 |
warnSTXSpecialComment := false |
|
1094 |
] |
|
1095 |
]. |
|
1096 |
outStream notNil ifTrue:[ |
|
1097 |
outStream cr. |
|
1098 |
outCol := 1 |
|
1099 |
]. |
|
1100 |
] ifFalse:[ |
|
1101 |
hereChar == ${ ifTrue:[ |
|
1102 |
" |
|
1103 |
special ST/X addition: |
|
1104 |
a ${ right after the initial double quote starts a directive |
|
1105 |
" |
|
1106 |
self parseDirective |
|
1107 |
]. |
|
1108 |
||
1109 |
[hereChar notNil and:[hereChar ~~ (Character doubleQuote)]] whileTrue:[ |
|
1110 |
hereChar == (Character cr) ifTrue:[ |
|
1111 |
tokenLineNr := tokenLineNr + 1. |
|
1112 |
]. |
|
1113 |
saveComments ifTrue:[ |
|
1114 |
commentStream nextPut:hereChar |
|
1115 |
]. |
|
1116 |
outStream notNil ifTrue:[ |
|
1117 |
outStream nextPut:hereChar. |
|
1118 |
outCol := outCol + 1 |
|
1119 |
]. |
|
1120 |
hereChar := source nextPeek |
|
1121 |
]. |
|
1122 |
hereChar isNil ifTrue:[ |
|
1123 |
self warning:'unclosed comment' position:startPos to:(source position) |
|
1124 |
] ifFalse:[ |
|
1125 |
outStream notNil ifTrue:[ |
|
1126 |
outStream nextPut:(Character doubleQuote). |
|
1127 |
outCol := outCol + 1 |
|
1128 |
]. |
|
1129 |
] |
|
1130 |
]. |
|
1131 |
||
1132 |
"skip final dQuote or cr" |
|
1133 |
source next. |
|
1134 |
||
1135 |
saveComments ifTrue:[ |
|
1136 |
self endComment:commentStream contents. |
|
1137 |
]. |
|
0 | 1138 |
! ! |
141 | 1139 |
|
148 | 1140 |
!Scanner class methodsFor:'documentation'! |
1141 |
||
263 | 1142 |
VS/stx/libcomp/Scanner.st,v 1.40 1996/04/20 18:08:54 cg Exp $' |
148 | 1143 |
! ! |
141 | 1144 |
Scanner initialize! |