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