author | Claus Gittinger <cg@exept.de> |
Thu, 23 Nov 1995 03:23:14 +0100 | |
changeset 141 | d378d997aab0 |
parent 139 | 65eaf1a009f5 |
child 143 | 1008a91b7bc1 |
permissions | -rw-r--r-- |
0 | 1 |
" |
4 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
44 | 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 |
Scanner subclass:#Parser |
|
141 | 14 |
instanceVariableNames:'classToCompileFor selfValue contextToEvaluateIn selector |
15 |
methodArgs methodArgNames methodVars methodVarNames tree |
|
16 |
currentBlock parseForCode usedInstVars usedClassVars usedVars |
|
17 |
modifiedInstVars modifiedClassVars modifiedGlobals usesSuper |
|
18 |
usedGlobals usedSymbols usedMessages localVarDefPosition |
|
19 |
evalExitBlock selfNode superNode hasPrimitiveCode |
|
20 |
hasNonOptionalPrimitiveCode primitiveNr primitiveResource logged |
|
21 |
warnedUndefVars warnSTXHereExtensionUsed correctedSource' |
|
22 |
classVariableNames:'PrevClass PrevInstVarNames PrevClassVarNames |
|
23 |
PrevClassInstVarNames LazyCompilation ArraysAreImmutable |
|
24 |
ImplicitSelfSends' |
|
25 |
poolDictionaries:'' |
|
26 |
category:'System-Compiler' |
|
0 | 27 |
! |
28 |
||
19 | 29 |
!Parser class methodsFor:'documentation'! |
30 |
||
20 | 31 |
copyright |
32 |
" |
|
33 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
44 | 34 |
All Rights Reserved |
20 | 35 |
|
36 |
This software is furnished under a license and may be used |
|
37 |
only in accordance with the terms of that license and with the |
|
38 |
inclusion of the above copyright notice. This software may not |
|
39 |
be provided or otherwise made available to, or used by, any |
|
40 |
other person. No title to or ownership of the software is |
|
41 |
hereby transferred. |
|
42 |
" |
|
43 |
! |
|
44 |
||
19 | 45 |
documentation |
46 |
" |
|
47 |
Parser is used for both parsing and evaluating smalltalk expressions; |
|
48 |
it first builds a parseTree which is then interpreted (evaluate) or |
|
49 |
compiled. Compilation is done in the subclass ByteCodeCompiler and/or |
|
50 |
the (planned) MachineCodeCompiler. |
|
51 |
||
52 |
methods of main interrest are: |
|
44 | 53 |
Parser evaluateExpression:... |
19 | 54 |
|
55 |
and: |
|
44 | 56 |
Parser parseExpression:... |
57 |
Parser parseMethod:... |
|
19 | 58 |
|
59 |
there is protocol to parse complete methods, selector specs, body only etc. |
|
60 |
||
61 |
Parser is also used to find the referenced/modified inst/classvars of |
|
62 |
a method - this is done by sending parseXXX message to a parser and asking |
|
63 |
the parser for referencedXVars or modifiedXVars (see SystemBrowser). |
|
64 |
||
65 |
You can also use parsers for all kinds of other things (ChangesBrowser for |
|
66 |
example analyzes the expressions in the changelist ...) by looking at the |
|
67 |
parsers tree. (Although this is somewhat dangerous, since it exports the |
|
20 | 68 |
compilers internals ... better style is to add specialized query methods here, |
69 |
which will be done incrementally.) |
|
19 | 70 |
|
71 |
One instance of Parser is created to parse one method or expression - i.e. |
|
72 |
its not suggested to reuse parsers. |
|
73 |
||
74 |
Instance variables: |
|
75 |
||
44 | 76 |
classToCompileFor <Class> the class (or nil) we are compiling for |
19 | 77 |
|
44 | 78 |
selfValue <any> value to use as self when interpreting |
19 | 79 |
|
44 | 80 |
contextToEvaluateIn <Context> the context (or nil) when interpreting |
19 | 81 |
|
44 | 82 |
selector <Symbol> the selector of the parsed method |
83 |
(valid after parseMethodSpecification) |
|
84 |
methodArgs internal |
|
19 | 85 |
|
44 | 86 |
methodArgNames <Collection> the names of the arguments |
87 |
(valid after parseMethodSpecification) |
|
19 | 88 |
|
44 | 89 |
methodVars internal |
0 | 90 |
|
44 | 91 |
methodVarNames <Collection> the names of the method locals |
92 |
(valid after parseMethodBodyVarSpec) |
|
19 | 93 |
|
44 | 94 |
tree <ParseTree> the parse tree - valid after parsing |
19 | 95 |
|
44 | 96 |
currentBlock if currently parsing for a block |
19 | 97 |
|
44 | 98 |
usedInstVars set of all accessed instances variables |
99 |
(valid after parsing) |
|
19 | 100 |
|
44 | 101 |
usedClassVars same for classVars |
19 | 102 |
|
44 | 103 |
usedVars all used variables (inst, class & globals) |
19 | 104 |
|
44 | 105 |
modifiedInstVars set of all modified instance variables |
19 | 106 |
|
44 | 107 |
modifiedClassVars same for clasVars |
19 | 108 |
|
44 | 109 |
localVarDefPosition <Integer> the character offset of the local variable |
110 |
def. (i.e. the first '|' if any) |
|
111 |
Not yet used - prepared for automatic add of |
|
112 |
undefined variables |
|
19 | 113 |
|
44 | 114 |
evalExitBlock internal for interpretation |
0 | 115 |
|
44 | 116 |
selfNode <Node> cached one-and-only 'self' node |
117 |
superNode <Node> cached one-and-only 'super' node |
|
19 | 118 |
|
44 | 119 |
hasPrimitiveCode <Boolean> true, if it contains ST/X style primitive code |
126 | 120 |
hasNonOptionalPrimitiveCode |
121 |
<Boolean> true, if it contains ST/X style primitive code |
|
122 |
which is NOT flagged by the OPTIONAL directive. |
|
19 | 123 |
|
44 | 124 |
primitiveNr <Integer> the parsed ST-80 type primitive number (or nil) |
19 | 125 |
|
44 | 126 |
logged |
19 | 127 |
|
44 | 128 |
warnedUndefVars <Set> set of all variables which the parser has |
129 |
already output a warning (to avoid multiple |
|
130 |
warnings about the same variable) |
|
19 | 131 |
|
132 |
Class variables: |
|
133 |
||
44 | 134 |
PrevClass <Class> class, of which properties are |
135 |
cached in: |
|
19 | 136 |
|
44 | 137 |
PrevInstVarNames <Collection> instance variablenames of cached class |
138 |
PrevClassVarNames <Collection> class variablenames of cached class |
|
139 |
PrevClassInstVarNames <Collection> class instance variablenames of cached class |
|
19 | 140 |
|
66 | 141 |
LazyCompilation <Boolean> EXPERIMENTAL: lazy compilation |
142 |
||
143 |
ArraysAreImmutable <Boolean> if true, create array literals |
|
144 |
as instances of ImmutableArray, |
|
145 |
which cannot be stored into. |
|
146 |
Default is false, for compatibility. |
|
147 |
Can be turned on while developping |
|
148 |
new code to make certain that side |
|
149 |
effects are avoided. |
|
19 | 150 |
" |
141 | 151 |
! |
152 |
||
153 |
version |
|
154 |
^ '$Header: /cvs/stx/stx/libcomp/Parser.st,v 1.57 1995-11-23 02:22:24 cg Exp $' |
|
155 |
! ! |
|
156 |
||
157 |
!Parser class methodsFor:'instance creation'! |
|
158 |
||
159 |
for:aStringOrStream in:aClass |
|
160 |
"return a new parser, reading code for aClass from aStringOrStream" |
|
161 |
||
162 |
|parser| |
|
163 |
||
164 |
parser := self for:aStringOrStream. |
|
165 |
parser setClassToCompileFor:aClass. |
|
166 |
^ parser |
|
167 |
! ! |
|
168 |
||
169 |
!Parser class methodsFor:'changes'! |
|
170 |
||
171 |
flushNameCache |
|
172 |
"unconditional flush name caches" |
|
173 |
||
174 |
PrevClass notNil ifTrue:[ |
|
175 |
PrevClass removeDependent:Parser |
|
176 |
]. |
|
177 |
PrevClass := nil. |
|
178 |
PrevInstVarNames := nil. |
|
179 |
PrevClassVarNames := nil. |
|
180 |
PrevClassInstVarNames := nil. |
|
181 |
||
182 |
"Parser flushNameCache" |
|
183 |
! |
|
184 |
||
185 |
update:something with:someArgument from:changedObject |
|
186 |
"aClass has changed its definition - flush name caches if we have to" |
|
187 |
||
188 |
(changedObject == PrevClass) ifTrue:[ |
|
189 |
something == #definition ifTrue:[ |
|
190 |
self flushNameCache |
|
191 |
] |
|
192 |
] |
|
193 |
! ! |
|
194 |
||
195 |
!Parser class methodsFor:'controlling compilation'! |
|
196 |
||
197 |
arraysAreImmutable |
|
198 |
"return true if arrays are immutable literals" |
|
199 |
||
200 |
^ ArraysAreImmutable |
|
201 |
! |
|
202 |
||
203 |
arraysAreImmutable:aBoolean |
|
204 |
"turn on/off immutable array literals - default is false for ST-80 compatibilty." |
|
205 |
||
206 |
ArraysAreImmutable := aBoolean. |
|
207 |
||
208 |
" |
|
209 |
can be added to your private.rc file: |
|
210 |
||
211 |
Compiler arraysAreImmutable:true |
|
212 |
Compiler arraysAreImmutable:false |
|
213 |
" |
|
214 |
! |
|
215 |
||
216 |
compileLazy |
|
217 |
"return true if compiling lazy" |
|
218 |
||
219 |
^ LazyCompilation. |
|
220 |
! |
|
221 |
||
222 |
compileLazy:aBoolean |
|
223 |
"turn on/off lazy compilation - return previous setting. |
|
224 |
Actually this flag belongs into the ByteCodeCompiler subclass, |
|
225 |
but it also controls the reporting of some errors here; therefore |
|
226 |
its located here" |
|
227 |
||
228 |
|oldLazy| |
|
229 |
||
230 |
oldLazy := LazyCompilation. |
|
231 |
LazyCompilation := aBoolean. |
|
232 |
^ oldLazy |
|
233 |
||
234 |
" |
|
235 |
usually set in your .rc file |
|
236 |
||
237 |
Compiler compileLazy:false |
|
238 |
Compiler compileLazy:true |
|
239 |
" |
|
240 |
! |
|
241 |
||
242 |
implicitSelfSends |
|
243 |
"return true if undefined variables with |
|
244 |
lowercase first character are to be turned |
|
245 |
into implicit self sends" |
|
246 |
||
247 |
^ ImplicitSelfSends |
|
248 |
! |
|
249 |
||
250 |
implicitSelfSends:aBoolean |
|
251 |
"turn on/off implicit self sends" |
|
252 |
||
253 |
ImplicitSelfSends := aBoolean |
|
254 |
||
255 |
" |
|
256 |
Compiler implicitSelfSends:true |
|
257 |
Compiler implicitSelfSends:false |
|
258 |
" |
|
19 | 259 |
! ! |
0 | 260 |
|
261 |
!Parser class methodsFor:'evaluating expressions'! |
|
262 |
||
19 | 263 |
evaluate:aStringOrStream |
71 | 264 |
"return the result of evaluating an expression in aStringOrStream. |
265 |
No doit-entry is added to the changeLog." |
|
0 | 266 |
|
19 | 267 |
^ self |
44 | 268 |
evaluate:aStringOrStream |
269 |
in:nil |
|
270 |
receiver:nil |
|
271 |
notifying:nil |
|
272 |
logged:false |
|
273 |
ifFail:nil |
|
71 | 274 |
compile:true |
19 | 275 |
|
276 |
" |
|
277 |
Compiler evaluate:'1 + 2' |
|
278 |
Compiler evaluate:'''hello world'' asSortedCollection displayString printNL' |
|
279 |
Compiler evaluate:'''hello world'' asSortedCollection printNL' |
|
280 |
" |
|
0 | 281 |
! |
282 |
||
71 | 283 |
evaluate:aStringOrStream compile:compile |
284 |
"return the result of evaluating aString, |
|
285 |
The compile argument specifies if the string should be compiled down to |
|
286 |
bytecode or instead be interpreted from the parseTree. |
|
287 |
The first should be done for doIts etc, where a readable walkback is |
|
288 |
required. |
|
289 |
The latter is better done for constants, styleSheet and resource |
|
290 |
reading and simple sends, where the overhead of compilation is bigger |
|
291 |
than the interpretation overhead." |
|
292 |
||
293 |
^ self |
|
294 |
evaluate:aStringOrStream |
|
295 |
in:nil |
|
296 |
receiver:nil |
|
297 |
notifying:nil |
|
298 |
logged:false |
|
299 |
ifFail:nil |
|
300 |
compile:compile |
|
301 |
! |
|
302 |
||
20 | 303 |
evaluate:aStringOrStream ifFail:failBlock |
304 |
"return the result of evaluating an expression in aStringOrStream. |
|
71 | 305 |
In case of any syntax errors, return the value of failBlock. |
306 |
No doit-entry is added to the changeLog." |
|
20 | 307 |
|
308 |
^ self |
|
44 | 309 |
evaluate:aStringOrStream |
310 |
in:nil |
|
311 |
receiver:nil |
|
312 |
notifying:nil |
|
313 |
logged:false |
|
314 |
ifFail:failBlock |
|
71 | 315 |
compile:true |
20 | 316 |
" |
317 |
Compiler evaluate:'1 +' ifFail:['oops'] |
|
318 |
" |
|
319 |
! |
|
320 |
||
71 | 321 |
evaluate:aStringOrStream in:aContext receiver:anObject notifying:requestor ifFail:failBlock |
322 |
"return the result of evaluating aStringOrStream, errors are reported to requestor. |
|
323 |
Allow access to anObject as self and to its instVars (used in the inspector). |
|
324 |
No doIt entry is added to the change-file. |
|
325 |
If the failBlock argument is non-nil, it is evaluated if an error occurs." |
|
326 |
||
327 |
^ self |
|
328 |
evaluate:aStringOrStream |
|
329 |
in:nil |
|
330 |
receiver:anObject |
|
331 |
notifying:requestor |
|
332 |
logged:false |
|
333 |
ifFail:nil |
|
334 |
compile:true |
|
20 | 335 |
! |
336 |
||
337 |
evaluate:aStringOrStream in:aContext receiver:anObject notifying:requestor logged:logged ifFail:failBlock |
|
338 |
"return the result of evaluating aStringOrStream, errors are reported to requestor. |
|
339 |
Allow access to anObject as self and to its instVars (used in the inspector). |
|
340 |
If logged is true, an entry is added to the change-file. If the failBlock argument |
|
341 |
is non-nil, it is evaluated if an error occurs." |
|
342 |
||
71 | 343 |
^ self |
344 |
evaluate:aStringOrStream |
|
345 |
in:aContext |
|
346 |
receiver:anObject |
|
347 |
notifying:requestor |
|
348 |
logged:logged |
|
349 |
ifFail:failBlock |
|
350 |
compile:true |
|
351 |
! |
|
352 |
||
353 |
evaluate:aStringOrStream in:aContext receiver:anObject notifying:requestor logged:logged ifFail:failBlock compile:compile |
|
354 |
"return the result of evaluating aStringOrStream, errors are reported to requestor. |
|
355 |
Allow access to anObject as self and to its instVars (used in the inspector). |
|
356 |
If logged is true, an entry is added to the change-file. If the failBlock argument |
|
357 |
is non-nil, it is evaluated if an error occurs. |
|
358 |
Finally, compile specifies if the string should be compiled down to |
|
359 |
bytecode or instead be interpreted from the parseTree. |
|
360 |
The first should be done for doIts etc, where a readable walkback is |
|
361 |
required. |
|
362 |
The latter is better done for constants, styleSheet and resource |
|
363 |
reading and simple sends, where the overhead of compilation is bigger |
|
364 |
than the interpretation overhead." |
|
365 |
||
52 | 366 |
|parser tree mustBackup loggedString chgStream value s sReal m| |
0 | 367 |
|
368 |
aStringOrStream isNil ifTrue:[^ nil]. |
|
369 |
aStringOrStream isStream ifTrue:[ |
|
44 | 370 |
parser := self for:aStringOrStream. |
371 |
mustBackup := true |
|
0 | 372 |
] ifFalse:[ |
44 | 373 |
loggedString := aStringOrStream. |
374 |
parser := self for:(ReadStream on:aStringOrStream). |
|
375 |
mustBackup := false |
|
0 | 376 |
]. |
84 | 377 |
parser parseForCode. |
0 | 378 |
parser setSelf:anObject. |
379 |
parser setContext:aContext. |
|
380 |
parser notifying:requestor. |
|
381 |
parser nextToken. |
|
79 | 382 |
tree := parser parseMethodBodyOrEmpty. |
0 | 383 |
|
384 |
"if reading from a stream, backup for next expression" |
|
385 |
mustBackup ifTrue:[ |
|
44 | 386 |
parser backupPosition |
0 | 387 |
]. |
388 |
||
389 |
(parser errorFlag or:[tree == #Error]) ifTrue:[ |
|
44 | 390 |
failBlock notNil ifTrue:[ |
391 |
^ failBlock value |
|
392 |
]. |
|
393 |
^ #Error |
|
0 | 394 |
]. |
395 |
tree notNil ifTrue:[ |
|
44 | 396 |
(logged |
397 |
and:[loggedString notNil |
|
398 |
and:[Smalltalk logDoits]]) ifTrue:[ |
|
399 |
chgStream := Class changesStream. |
|
400 |
chgStream notNil ifTrue:[ |
|
401 |
chgStream nextChunkPut:loggedString. |
|
402 |
chgStream cr. |
|
403 |
chgStream close |
|
404 |
]. |
|
405 |
]. |
|
35 | 406 |
|
49 | 407 |
" |
71 | 408 |
if compile is false, or the parse tree is that of a constant, |
409 |
quickly return its value. |
|
410 |
This is used for example, when reading simple objects |
|
411 |
via #readFrom:. |
|
412 |
The overhead of compiling a method is avoided in this case. |
|
49 | 413 |
" |
76 | 414 |
(compile not |
415 |
or:[tree isConstant |
|
416 |
or:[aStringOrStream isStream]]) ifTrue:[ |
|
49 | 417 |
^ tree evaluate |
418 |
] ifFalse:[ |
|
419 |
" |
|
420 |
if I am the ByteCodeCompiler, |
|
421 |
generate a dummy method, execute it and return the value. |
|
52 | 422 |
otherwise, just evaluate the tree; slower, but not too bad ... |
423 |
This allows systems to be delivered without the ByteCodeCompiler, |
|
424 |
and still evaluate expressions (neede for example, to read resource |
|
53 | 425 |
files or to process .rc files). |
49 | 426 |
" |
52 | 427 |
self == Parser ifTrue:[ |
49 | 428 |
parser evalExitBlock:[:value | parser release. ^ value]. |
51 | 429 |
value := tree evaluate. |
430 |
parser evalExitBlock:nil. |
|
52 | 431 |
] ifFalse:[ |
432 |
aStringOrStream isStream ifTrue:[ |
|
76 | 433 |
s := parser collectedSource. "/ does not work yet ... |
52 | 434 |
] ifFalse:[ |
435 |
s := aStringOrStream |
|
436 |
]. |
|
437 |
sReal := 'doIt ^[\' withCRs , s , '\] value' withCRs. |
|
87 | 438 |
m := self |
439 |
compile:sReal |
|
440 |
forClass:anObject class |
|
441 |
inCategory:'_temporary_' |
|
442 |
notifying:requestor |
|
443 |
install:false |
|
444 |
skipIfSame:false |
|
445 |
silent:true. |
|
52 | 446 |
m notNil ifTrue:[ |
53 | 447 |
m ~~ #Error ifTrue:[ |
448 |
" |
|
449 |
fake: patch the source string, to what the user expects |
|
450 |
in the browser |
|
451 |
" |
|
452 |
m source:' \' withCRs , s . |
|
453 |
value := m valueWithReceiver:anObject |
|
454 |
arguments:#() |
|
455 |
selector:#doIt |
|
61 | 456 |
search:nil |
457 |
sender:nil. |
|
53 | 458 |
] ifFalse:[ |
459 |
parser evalExitBlock:[:value | parser release. ^ value]. |
|
460 |
value := tree evaluate. |
|
461 |
parser evalExitBlock:nil. |
|
462 |
] |
|
52 | 463 |
]. |
464 |
] |
|
49 | 465 |
] |
0 | 466 |
]. |
35 | 467 |
parser release. |
468 |
^ value |
|
141 | 469 |
! |
470 |
||
471 |
evaluate:aStringOrStream logged:logged |
|
472 |
"return the result of evaluating an expression in aStringOrStream. |
|
473 |
The argument log controls if an entry is added to the changeLog." |
|
474 |
||
475 |
^ self |
|
476 |
evaluate:aStringOrStream |
|
477 |
in:nil |
|
478 |
receiver:nil |
|
479 |
notifying:nil |
|
480 |
logged:logged |
|
481 |
ifFail:nil |
|
482 |
compile:true |
|
483 |
" |
|
484 |
Compiler evaluate:'''some string''' logged:false |
|
485 |
Compiler evaluate:'''some string''' logged:true |
|
486 |
" |
|
487 |
! |
|
488 |
||
489 |
evaluate:aStringOrStream notifying:requestor |
|
490 |
"return the result of evaluating aString, |
|
491 |
errors are reported to requestor" |
|
492 |
||
493 |
^ self |
|
494 |
evaluate:aStringOrStream |
|
495 |
in:nil |
|
496 |
receiver:nil |
|
497 |
notifying:requestor |
|
498 |
logged:false |
|
499 |
ifFail:nil |
|
500 |
compile:true |
|
501 |
! |
|
502 |
||
503 |
evaluate:aStringOrStream notifying:requestor compile:compile |
|
504 |
"return the result of evaluating aString, |
|
505 |
errors are reported to requestor. |
|
506 |
The compile argument specifies if the string should be compiled down to |
|
507 |
bytecode or instead be interpreted from the parseTree. |
|
508 |
The first should be done for doIts etc, where a readable walkback is |
|
509 |
required. |
|
510 |
The latter is better done for constants, styleSheet and resource |
|
511 |
reading and simple sends, where the overhead of compilation is bigger |
|
512 |
than the interpretation overhead." |
|
513 |
||
514 |
^ self |
|
515 |
evaluate:aStringOrStream |
|
516 |
in:nil |
|
517 |
receiver:nil |
|
518 |
notifying:requestor |
|
519 |
logged:false |
|
520 |
ifFail:nil |
|
521 |
compile:compile |
|
522 |
! |
|
523 |
||
524 |
evaluate:aStringOrStream receiver:anObject notifying:requestor |
|
525 |
"return the result of evaluating aString, |
|
526 |
errors are reported to requestor. Allow access to |
|
527 |
anObject as self and to its instVars (used in the inspector)" |
|
528 |
||
529 |
^ self |
|
530 |
evaluate:aStringOrStream |
|
531 |
in:nil |
|
532 |
receiver:anObject |
|
533 |
notifying:requestor |
|
534 |
logged:false |
|
535 |
ifFail:nil |
|
536 |
compile:true |
|
537 |
||
538 |
" |
|
539 |
Compiler evaluate:'self x' receiver:(1 @ 2) notifying:nil |
|
540 |
" |
|
541 |
! |
|
542 |
||
543 |
evaluate:aStringOrStream receiver:anObject notifying:requestor compile:compile |
|
544 |
"return the result of evaluating aString, |
|
545 |
errors are reported to requestor. Allow access to |
|
546 |
anObject as self and to its instVars (used in the inspector). |
|
547 |
The compile argument specifies if the string should be compiled down to |
|
548 |
bytecode or instead be interpreted from the parseTree. |
|
549 |
The first should be done for doIts etc, where a readable walkback is |
|
550 |
required. |
|
551 |
The latter is better done for constants, styleSheet and resource |
|
552 |
reading and simple sends, where the overhead of compilation is bigger |
|
553 |
than the interpretation overhead." |
|
554 |
||
555 |
^ self |
|
556 |
evaluate:aStringOrStream |
|
557 |
in:nil |
|
558 |
receiver:anObject |
|
559 |
notifying:requestor |
|
560 |
logged:false |
|
561 |
ifFail:nil |
|
562 |
compile:compile |
|
0 | 563 |
! ! |
564 |
||
66 | 565 |
!Parser class methodsFor:'initialization '! |
566 |
||
567 |
initialize |
|
98 | 568 |
LazyCompilation := false. "/ usually set to true in your .rc file |
569 |
ArraysAreImmutable := false. "/ usually left true for ST-80 compatibility |
|
570 |
ImplicitSelfSends := false |
|
66 | 571 |
! ! |
572 |
||
141 | 573 |
!Parser class methodsFor:'parsing'! |
574 |
||
575 |
parseExpression:aString |
|
576 |
"parse aString as an expression; |
|
577 |
Return the parseTree (if ok), nil (for an empty string |
|
578 |
or comment only) or #Error (syntactic error)." |
|
579 |
||
580 |
^ self withSelf:nil |
|
581 |
parseExpression:aString |
|
582 |
notifying:nil |
|
583 |
ignoreErrors:true "silence on Transcript" |
|
584 |
ignoreWarnings:true |
|
585 |
! |
|
586 |
||
587 |
parseMethod:aString |
|
588 |
"parse a method. |
|
589 |
Return a parser (if ok), nil (empty) or #Error (syntax). |
|
590 |
The parser can be queried for selector, receiver, args, locals, |
|
591 |
used selectors etc." |
|
592 |
||
593 |
^ self parseMethod:aString in:nil |
|
594 |
||
595 |
" |
|
596 |
|p| |
|
597 |
||
598 |
p := Parser |
|
599 |
parseMethod:' |
|
600 |
foo:arg1 bar:arg2 baz:arg3 |
|
601 |
|l1 l2| |
|
602 |
l1 := 0. |
|
603 |
l2 := arg1. |
|
604 |
^ self'. |
|
605 |
||
606 |
'nArgs: ' print. p numberOfMethodArgs printNL. |
|
607 |
'args: ' print. p methodArgs printNL. |
|
608 |
'sel: ' print. p selector printNL. |
|
609 |
'nLocal: ' print. p numberOfMethodVars printNL. |
|
610 |
'locals: ' print. p methodVars printNL. |
|
611 |
'tree: ' printNL. p tree printAllOn:Stdout. Stdout cr. |
|
612 |
" |
|
613 |
! |
|
614 |
||
615 |
parseMethod:aString in:aClass |
|
616 |
"parse a method in a given class. |
|
617 |
Return a parser (if ok), nil (empty) or #Error (syntax). |
|
618 |
The parser can be queried for selector, receiver, args, locals, |
|
619 |
used selectors, modified instvars, referenced classvars etc." |
|
620 |
||
621 |
^ self parseMethod:aString in:aClass |
|
622 |
ignoreErrors:false |
|
623 |
ignoreWarnings:false |
|
624 |
! |
|
625 |
||
626 |
parseMethod:aString in:aClass ignoreErrors:noErrors ignoreWarnings:noWarnings |
|
627 |
"parse a method in a given class. |
|
628 |
Return a parser (if ok), nil (empty) or #Error (syntax). |
|
629 |
The parser can be queried for selector, receiver, args, locals, |
|
630 |
used selectors, modified instvars, referenced classvars etc." |
|
631 |
||
632 |
|parser tree| |
|
633 |
||
634 |
aString isNil ifTrue:[^ nil]. |
|
635 |
parser := self for:(ReadStream on:aString) in:aClass. |
|
636 |
noErrors ifTrue:[ |
|
637 |
parser ignoreErrors |
|
638 |
]. |
|
639 |
noWarnings ifTrue:[ |
|
640 |
parser ignoreWarnings |
|
641 |
]. |
|
642 |
tree := parser parseMethod. |
|
643 |
(parser errorFlag or:[tree == #Error]) ifTrue:[^ nil]. |
|
644 |
^ parser |
|
645 |
! |
|
646 |
||
647 |
parseMethod:aString in:aClass warnings:warnBoolean |
|
648 |
"parse a method in a given class. |
|
649 |
Return a parser (if ok), nil (empty) or #Error (syntax). |
|
650 |
The parser can be queried for selector, receiver, args, locals, |
|
651 |
used selectors, modified instvars, referenced classvars etc. |
|
652 |
This method is obsolete, and left in for backward compatibility." |
|
653 |
||
654 |
^ self |
|
655 |
parseMethod:aString in:aClass |
|
656 |
ignoreErrors:false |
|
657 |
ignoreWarnings:warnBoolean not |
|
658 |
! |
|
659 |
||
660 |
parseMethodArgAndVarSpecification:aString |
|
661 |
"parse a methods selector, arg and var spec (i.e. locals); |
|
662 |
Return a parser (if ok), nil (empty) or #Error (syntax). |
|
663 |
The parser can be queried for selector, receiver etc." |
|
664 |
||
665 |
^ self parseMethodArgAndVarSpecification:aString in:nil |
|
666 |
||
667 |
" |
|
668 |
|p| |
|
669 |
||
670 |
p := Parser |
|
671 |
parseMethodArgAndVarSpecification:' |
|
672 |
foo:arg1 bar:arg2 baz:arg3 |
|
673 |
|l1 l2|'. |
|
674 |
||
675 |
'nArgs: ' print. p numberOfMethodArgs printNL. |
|
676 |
'args: ' print. p methodArgs printNL. |
|
677 |
'sel: ' print. p selector printNL. |
|
678 |
'nLocal: ' print. p numberOfMethodVars printNL. |
|
679 |
'locals: ' print. p methodVars printNL. |
|
680 |
" |
|
681 |
! |
|
682 |
||
683 |
parseMethodArgAndVarSpecification:aString in:aClass |
|
684 |
"parse a methods selector, arg and var spec in a given class; |
|
685 |
Return a parser (if ok), nil (empty) or #Error (syntax). |
|
686 |
The parser can be queried for selector, receiver, args and locals" |
|
687 |
||
688 |
^ self parseMethodArgAndVarSpecification:aString |
|
689 |
in:aClass |
|
690 |
ignoreErrors:false |
|
691 |
ignoreWarnings:false |
|
692 |
! |
|
693 |
||
694 |
parseMethodArgAndVarSpecification:aString in:aClass ignoreErrors:noErrors ignoreWarnings:noWarnings |
|
695 |
"parse a methods selector, arg and var spec in a given class; |
|
696 |
Return a parser (if ok), nil (empty) or #Error (syntax). |
|
697 |
The parser can be queried for selector, receiver, args and locals" |
|
19 | 698 |
|
0 | 699 |
|parser| |
700 |
||
141 | 701 |
aString isNil ifTrue:[^ nil]. |
702 |
parser := self for:(ReadStream on:aString) in:aClass. |
|
703 |
noErrors ifTrue:[ |
|
704 |
parser ignoreErrors |
|
705 |
]. |
|
706 |
noWarnings ifTrue:[ |
|
707 |
parser ignoreWarnings |
|
708 |
]. |
|
709 |
parser nextToken. |
|
710 |
(parser parseMethodSpec == #Error) ifTrue:[^ nil]. |
|
711 |
(parser parseMethodBodyVarSpec == #Error) ifTrue:[^ nil]. |
|
712 |
parser errorFlag ifTrue:[^ nil]. |
|
0 | 713 |
^ parser |
141 | 714 |
! |
715 |
||
716 |
parseMethodArgAndVarSpecificationSilent:aString |
|
717 |
"parse a methods selector, arg and var spec (i.e. locals); |
|
718 |
Return a parser (if ok), nil (empty) or #Error (syntax). |
|
719 |
The parser can be queried for selector, receiver etc. |
|
720 |
Like #parseMethodArgAndVarSpecification:, but does not |
|
721 |
display error/warning messages on the transcript." |
|
722 |
||
723 |
^ self parseMethodArgAndVarSpecificationSilent:aString in:nil |
|
724 |
! |
|
725 |
||
726 |
parseMethodArgAndVarSpecificationSilent:aString in:aClass |
|
727 |
"parse a methods selector, arg and var spec in a given class; |
|
728 |
Return a parser (if ok), nil (empty) or #Error (syntax). |
|
729 |
The parser can be queried for selector, receiver, args and locals. |
|
730 |
Like #parseMethodArgAndVarSpecification:in:, but does not |
|
731 |
display error/warning messages on the transcript." |
|
732 |
||
733 |
^ self parseMethodArgAndVarSpecification:aString |
|
734 |
in:aClass |
|
735 |
ignoreErrors:true |
|
736 |
ignoreWarnings:true |
|
737 |
! |
|
738 |
||
739 |
parseMethodSilent:aString |
|
740 |
"parse a method. |
|
741 |
Return a parser (if ok), nil (empty) or #Error (syntax). |
|
742 |
The parser can be queried for selector, receiver, args, locals, |
|
743 |
used selectors etc. |
|
744 |
Like #parseMethod:, but does not display warning/error messages on the |
|
745 |
transcript." |
|
746 |
||
747 |
^ self parseMethodSilent:aString in:nil |
|
748 |
! |
|
749 |
||
750 |
parseMethodSilent:aString in:aClass |
|
751 |
"parse a method in a given class. |
|
752 |
Return a parser (if ok), nil (empty) or #Error (syntax). |
|
753 |
The parser can be queried for selector, receiver, args, locals, |
|
754 |
used selectors, modified instvars, referenced classvars etc. |
|
755 |
Like #parseMethod:in:, but does not display any error/warning |
|
756 |
messages on the transcript." |
|
757 |
||
758 |
^ self parseMethod:aString in:aClass |
|
759 |
ignoreErrors:true |
|
760 |
ignoreWarnings:true |
|
761 |
! |
|
762 |
||
763 |
parseMethodSpecification:aString |
|
764 |
"parse a methods selector & arg specification; |
|
765 |
Return a parser (if ok), nil (empty) or #Error (syntax). |
|
766 |
The parser can be queried for selector, receiver etc." |
|
767 |
||
768 |
^ self parseMethodSpecification:aString in:nil |
|
769 |
||
770 |
" |
|
771 |
|p| |
|
772 |
||
773 |
p := Parser parseMethodSpecification:'foo:arg1 bar:arg2 baz:arg3'. |
|
774 |
'nArgs: ' print. p numberOfMethodArgs printNL. |
|
775 |
'args: ' print. p methodArgs printNL. |
|
776 |
'sel: ' print. p selector printNL |
|
777 |
" |
|
778 |
! |
|
779 |
||
780 |
parseMethodSpecification:aString in:aClass |
|
781 |
"parse a methods selector & arg spec for a given class; |
|
782 |
Return a parser (if ok), nil (empty) or #Error (syntax). |
|
783 |
The parser can be queried for selector, receiver etc." |
|
784 |
||
785 |
^ self parseMethodSpecification:aString |
|
786 |
in:aClass |
|
787 |
ignoreErrors:false |
|
788 |
ignoreWarnings:false |
|
789 |
! |
|
790 |
||
791 |
parseMethodSpecification:aString in:aClass ignoreErrors:noErrors ignoreWarnings:noWarnings |
|
792 |
"parse a methods selector & arg spec for a given class; |
|
793 |
Return a parser (if ok), nil (empty) or #Error (syntax). |
|
794 |
The parser can be queried for selector, receiver etc. |
|
795 |
noErrors and noWarnings specify if error- and warningMessages are |
|
796 |
to be output onto the Transcript." |
|
797 |
||
798 |
|parser tree| |
|
799 |
||
800 |
aString isNil ifTrue:[^ nil]. |
|
801 |
parser := self for:(ReadStream on:aString) in:aClass. |
|
802 |
noErrors ifTrue:[ |
|
803 |
parser ignoreErrors |
|
804 |
]. |
|
805 |
noWarnings ifTrue:[ |
|
806 |
parser ignoreWarnings |
|
807 |
]. |
|
808 |
parser nextToken. |
|
809 |
tree := parser parseMethodSpec. |
|
810 |
(parser errorFlag or:[tree == #Error]) ifTrue:[^ #Error]. |
|
811 |
^ parser |
|
812 |
! |
|
813 |
||
814 |
parseMethodSpecificationSilent:aString |
|
815 |
"parse a methods selector & arg specification; |
|
816 |
Return a parser (if ok), nil (empty) or #Error (syntax). |
|
817 |
The parser can be queried for selector, receiver etc. |
|
818 |
Like #parseMethodSpecification:, but does not display any error/warning Messages on the transcript." |
|
819 |
||
820 |
^ self parseMethodSpecificationSilent:aString in:nil |
|
821 |
! |
|
822 |
||
823 |
parseMethodSpecificationSilent:aString in:aClass |
|
824 |
"parse a methods selector & arg spec for a given class; |
|
825 |
Return a parser (if ok), nil (empty) or #Error (syntax). |
|
826 |
The parser can be queried for selector, receiver etc. |
|
827 |
Like #parseMethodSpecification:in:, but does not display any error/warning Messages on the transcript." |
|
828 |
||
829 |
^ self parseMethodSpecification:aString |
|
830 |
in:aClass |
|
831 |
ignoreErrors:true |
|
832 |
ignoreWarnings:true |
|
833 |
||
834 |
"Created: 31.10.1995 / 14:37:49 / cg" |
|
835 |
! |
|
0 | 836 |
|
7 | 837 |
selectorInExpression:aString |
47 | 838 |
"parse an expression - return the selector. Even malformed expressions |
839 |
(such as missing receiver or missing arg are parsed. |
|
840 |
Used for the SystemBrowsers implementors/senders query-box initial text. |
|
21 | 841 |
Returns nil if unparsable." |
7 | 842 |
|
843 |
|tree parser| |
|
844 |
||
845 |
(aString isNil or:[aString isEmpty]) ifTrue:[^ nil]. |
|
846 |
||
21 | 847 |
tree := self withSelf:nil |
44 | 848 |
parseExpression:aString |
849 |
notifying:nil |
|
850 |
ignoreErrors:true |
|
851 |
ignoreWarnings:true. |
|
21 | 852 |
|
853 |
" |
|
854 |
special: take the expression of the right side, if its an |
|
855 |
assignment or return |
|
856 |
" |
|
7 | 857 |
(tree notNil and:[tree ~~ #Error]) ifTrue:[ |
44 | 858 |
tree isAssignment ifTrue:[ |
859 |
tree expression isMessage ifTrue:[ |
|
860 |
tree := tree expression |
|
861 |
] |
|
862 |
]. |
|
863 |
tree isReturnNode ifTrue:[ |
|
864 |
tree expression isMessage ifTrue:[ |
|
865 |
tree := tree expression |
|
866 |
] |
|
867 |
]. |
|
19 | 868 |
|
44 | 869 |
tree isMessage ifTrue:[ |
870 |
^ tree selector |
|
871 |
]. |
|
7 | 872 |
]. |
873 |
||
21 | 874 |
" |
875 |
mhmh, try expression without receiver |
|
876 |
" |
|
7 | 877 |
parser := self for:(ReadStream on:aString). |
878 |
parser ignoreErrors. |
|
879 |
parser nextToken. |
|
880 |
^ parser degeneratedKeywordExpressionForSelector |
|
881 |
||
882 |
" |
|
20 | 883 |
Parser selectorInExpression:'foo at:1 put:(5 * bar)' |
884 |
Parser selectorInExpression:'(foo at:1) at:1' |
|
47 | 885 |
Parser selectorInExpression:'a + 4' |
886 |
Parser selectorInExpression:'a negated' |
|
20 | 887 |
Parser selectorInExpression:'at:1 put:5' |
47 | 888 |
Parser selectorInExpression:'at:1 put:' |
889 |
Parser selectorInExpression:'a at:1 put:5' |
|
890 |
Parser selectorInExpression:'a at:1 put:' |
|
20 | 891 |
Parser selectorInExpression:'a := foo at:1 put:5' |
7 | 892 |
" |
893 |
! |
|
894 |
||
895 |
withSelf:anObject parseExpression:aString notifying:someOne |
|
896 |
"parse aString as an expression with self set to anObject; |
|
21 | 897 |
Return the parseTree (if ok), nil (for an empty string |
898 |
or comment only ) or #Error (syntactic error). |
|
899 |
||
900 |
Errors and warnings are forwarded to someOne (usually some |
|
901 |
codeView) which can highlight it and show a popup box." |
|
7 | 902 |
|
21 | 903 |
^ self withSelf:anObject |
44 | 904 |
parseExpression:aString |
905 |
notifying:someOne |
|
906 |
ignoreErrors:false |
|
907 |
ignoreWarnings:false |
|
7 | 908 |
! |
909 |
||
910 |
withSelf:anObject parseExpression:aString notifying:someOne ignoreErrors:ignore |
|
0 | 911 |
"parse aString as an expression with self set to anObject; |
21 | 912 |
Return the parseTree (if ok), nil (for an empty string |
913 |
or comment only ) or #Error (syntactic error). |
|
914 |
||
915 |
Errors and warnings are forwarded to someOne (usually some |
|
916 |
codeView) which can highlight it and show a popup box." |
|
917 |
||
918 |
^ self withSelf:anObject |
|
44 | 919 |
parseExpression:aString |
920 |
notifying:someOne |
|
921 |
ignoreErrors:ignore |
|
922 |
ignoreWarnings:ignore |
|
21 | 923 |
! |
924 |
||
925 |
withSelf:anObject parseExpression:aString notifying:someOne ignoreErrors:ignoreErrors ignoreWarnings:ignoreWarnings |
|
926 |
"parse aString as an expression with self set to anObject; |
|
927 |
Return the parseTree (if ok), nil (for an empty string |
|
928 |
or comment only ) or #Error (syntactic error). |
|
929 |
||
930 |
Errors and warnings are forwarded to someOne (usually some |
|
931 |
codeView) which can highlight it and show a popup box, |
|
932 |
iff ignoreErrors/ignoreWarnings is true respectively." |
|
0 | 933 |
|
35 | 934 |
|parser tree token| |
0 | 935 |
|
936 |
aString isNil ifTrue:[^ nil]. |
|
937 |
parser := self for:(ReadStream on:aString). |
|
938 |
parser setSelf:anObject. |
|
939 |
parser notifying:someOne. |
|
21 | 940 |
ignoreErrors ifTrue:[parser ignoreErrors]. |
941 |
ignoreWarnings ifTrue:[parser ignoreWarnings]. |
|
35 | 942 |
token := parser nextToken. |
943 |
(token == $^) ifTrue:[ |
|
44 | 944 |
parser nextToken. |
35 | 945 |
]. |
0 | 946 |
tree := parser expression. |
947 |
(parser errorFlag or:[tree == #Error]) ifTrue:[^ #Error]. |
|
948 |
^ tree |
|
949 |
! ! |
|
950 |
||
20 | 951 |
!Parser class methodsFor:'unparsing'! |
952 |
||
953 |
methodSpecificationForSelector:aSelector |
|
954 |
"given a selector such as #foo:bar:, return a string that could |
|
955 |
serve as a methods specification source code. |
|
956 |
To be used for code generators" |
|
957 |
||
958 |
^ self methodSpecificationForSelector:aSelector |
|
44 | 959 |
argNames:#('arg1' 'arg2' 'arg3' 'arg4' 'arg5' 'arg6' |
960 |
'arg7' 'arg8' 'arg9' 'arg10' 'arg11' 'arg12' |
|
961 |
'arg13' 'arg14' 'arg15') |
|
20 | 962 |
" |
963 |
Parser methodSpecificationForSelector:#foo:bar: |
|
21 | 964 |
Parser methodSpecificationForSelector:#+ |
20 | 965 |
Parser methodSpecificationForSelector:#negated |
966 |
" |
|
967 |
! |
|
968 |
||
969 |
methodSpecificationForSelector:aSelector argNames:argNames |
|
970 |
"given a selector such as #foo:bar:, return a string that could |
|
971 |
serve as a methods specification source code. |
|
972 |
To be used for code generators" |
|
973 |
||
974 |
|s nargs parts| |
|
975 |
||
976 |
s := WriteStream on:String new. |
|
44 | 977 |
nargs := aSelector numArgs. |
20 | 978 |
nargs == 0 ifTrue:[ |
44 | 979 |
s nextPutAll:aSelector |
20 | 980 |
] ifFalse:[ |
44 | 981 |
parts := aSelector partsIfSelector. |
982 |
1 to:nargs do:[:i | |
|
983 |
s nextPutAll:(parts at:i); space; |
|
984 |
nextPutAll:(argNames at:i); space. |
|
985 |
] |
|
20 | 986 |
]. |
987 |
^ s contents |
|
988 |
||
989 |
" |
|
990 |
Parser methodSpecificationForSelector:#foo:bar: argNames:#('one' 'two' 'three') |
|
991 |
Parser methodSpecificationForSelector:#+ argNames:#('one') |
|
992 |
Parser methodSpecificationForSelector:#negated |
|
993 |
" |
|
994 |
! ! |
|
995 |
||
0 | 996 |
!Parser methodsFor:'ST-80 compatibility'! |
997 |
||
998 |
evaluate:aString in:aClass to:to notifying:aRequestor ifFail:failBlock |
|
35 | 999 |
|parseTree value| |
0 | 1000 |
|
1001 |
aString isNil ifTrue:[^ nil]. |
|
1002 |
self initializeFor:(ReadStream on:aString). |
|
1003 |
self setClassToCompileFor:aClass. |
|
1004 |
selfValue := nil. |
|
1005 |
requestor := aRequestor. |
|
1006 |
||
1007 |
self nextToken. |
|
1008 |
parseTree := self parseMethodBody. |
|
1009 |
(errorFlag or:[tree == #Error]) ifTrue:[^ #Error]. |
|
1010 |
parseTree notNil ifTrue:[ |
|
44 | 1011 |
self evalExitBlock:[:value | ^ failBlock value]. |
1012 |
value := parseTree evaluate |
|
0 | 1013 |
]. |
35 | 1014 |
self release. |
1015 |
^ value |
|
0 | 1016 |
! ! |
1017 |
||
44 | 1018 |
!Parser methodsFor:'accessing'! |
1019 |
||
1020 |
correctedSource |
|
1021 |
^ correctedSource |
|
1022 |
! |
|
1023 |
||
141 | 1024 |
errorFlag |
1025 |
"return true if there where any errors (valid after parsing)" |
|
1026 |
||
1027 |
^ errorFlag |
|
1028 |
! |
|
1029 |
||
1030 |
evalExitBlock:aBlock |
|
1031 |
"when evaluating a return expression, this block is evaluated" |
|
1032 |
||
1033 |
evalExitBlock := aBlock |
|
1034 |
! |
|
1035 |
||
19 | 1036 |
primitiveNumber |
1037 |
"return the ST-80 style primitiveNumber or nil (valid after parsing)" |
|
1038 |
||
1039 |
^ primitiveNr |
|
1040 |
! |
|
1041 |
||
97 | 1042 |
primitiveResource |
1043 |
"return the ST-80 style resource info or nil (valid after parsing)." |
|
1044 |
||
1045 |
^ primitiveResource |
|
1046 |
! |
|
1047 |
||
35 | 1048 |
release |
1049 |
methodArgs := methodVars := tree := selfNode := superNode := nil. |
|
1050 |
super release. |
|
90 | 1051 |
! |
1052 |
||
141 | 1053 |
targetClass |
1054 |
^ classToCompileFor |
|
1055 |
! |
|
1056 |
||
90 | 1057 |
targetClass:aClass |
117 | 1058 |
classToCompileFor := aClass |
1059 |
! |
|
1060 |
||
141 | 1061 |
tree |
1062 |
"return the parsetree" |
|
1063 |
||
1064 |
^tree |
|
1065 |
! |
|
1066 |
||
1067 |
tree:aTree |
|
1068 |
"private: set the tree - for internal use only" |
|
1069 |
||
1070 |
tree := aTree |
|
1071 |
! ! |
|
1072 |
||
1073 |
!Parser methodsFor:'error correction'! |
|
1074 |
||
1075 |
askForCorrection:aString fromList:aList |
|
1076 |
"launch a selection box, which allows user to enter correction. |
|
1077 |
return newString or nil (for abort)" |
|
1078 |
||
1079 |
|box| |
|
1080 |
||
1081 |
"in systems without widgets ..." |
|
1082 |
ListSelectionBox isNil ifTrue:[ |
|
1083 |
^ self confirm:aString |
|
1084 |
]. |
|
1085 |
box := ListSelectionBox title:aString. |
|
1086 |
box initialText:(aList at:1). |
|
1087 |
box list:aList. |
|
1088 |
box okText:'correct'. |
|
1089 |
box action:[:aString | ^ aString]. |
|
1090 |
box showAtPointer. |
|
1091 |
^ nil |
|
1092 |
! |
|
1093 |
||
1094 |
correctByDeleting |
|
1095 |
"correct (by deleting token) if user wants to; |
|
1096 |
return #Error if there was no correction; |
|
1097 |
nil if there was one." |
|
1098 |
||
1099 |
(self confirm:'confirm deleting') ifFalse:[^ #Error]. |
|
1100 |
||
1101 |
" |
|
1102 |
tell requestor (i.e. CodeView) about the change |
|
1103 |
this will update what the requestor shows. |
|
1104 |
" |
|
1105 |
requestor deleteSelection. |
|
1106 |
||
1107 |
" |
|
1108 |
get the updated source-string |
|
1109 |
which is needed, when we eventually install the new method |
|
1110 |
" |
|
1111 |
correctedSource := requestor currentSourceCode. |
|
1112 |
^ nil |
|
1113 |
! |
|
1114 |
||
1115 |
correctSelector:aSelectorString message:msg position:pos1 to:pos2 |
|
1116 |
"notify error and correct if user wants to; |
|
1117 |
return #Error if there was no correction |
|
1118 |
or a ParseNode as returned by variable" |
|
1119 |
||
1120 |
|correctIt suggestedNames newSelector| |
|
1121 |
||
1122 |
" |
|
1123 |
sorry, but I cannot handle keywords with more than one-part |
|
1124 |
currently (too much work - maybe Ill do it later when everything else works :-) |
|
1125 |
" |
|
1126 |
(aSelectorString occurrencesOf:$:) > 1 ifTrue:[ |
|
1127 |
self warning:msg position:pos1 to:pos2. |
|
1128 |
^ aSelectorString |
|
1129 |
]. |
|
1130 |
||
1131 |
correctIt := self correctableError:msg position:pos1 to:pos2. |
|
1132 |
correctIt ifFalse:[^ aSelectorString]. |
|
1133 |
||
1134 |
suggestedNames := self findBestSelectorsFor:aSelectorString. |
|
1135 |
suggestedNames notNil ifTrue:[ |
|
1136 |
newSelector := self askForCorrection:'correct selector to: ' fromList:suggestedNames. |
|
1137 |
newSelector isNil ifTrue:[^ aSelectorString]. |
|
1138 |
] ifFalse:[ |
|
1139 |
self information:'no good correction found'. |
|
1140 |
^ aSelectorString |
|
1141 |
]. |
|
1142 |
||
1143 |
" |
|
1144 |
tell requestor (i.e. CodeView) about the change |
|
1145 |
this will update what the requestor shows. |
|
1146 |
" |
|
1147 |
||
1148 |
requestor replaceSelectionBy:newSelector keepCursor:false. |
|
1149 |
" |
|
1150 |
get the updated source-string |
|
1151 |
which is needed, when we eventually install the new method |
|
1152 |
" |
|
1153 |
correctedSource := requestor currentSourceCode. |
|
1154 |
||
1155 |
^ newSelector |
|
1156 |
! |
|
1157 |
||
1158 |
correctVariable |
|
1159 |
"notify error and correct if user wants to; |
|
1160 |
return #Error if there was no correction |
|
1161 |
or a ParseNode as returned by variable" |
|
1162 |
||
1163 |
|correctIt varName suggestedNames newName pos1 pos2| |
|
1164 |
||
1165 |
pos1 := tokenPosition. |
|
1166 |
varName := tokenName. |
|
1167 |
pos2 := pos1 + varName size - 1. |
|
1168 |
||
1169 |
"OLD: |
|
1170 |
(varName at:1) isLowercase ifTrue:[ |
|
1171 |
correctIt := self undefError:varName position:pos1 to:pos2. |
|
1172 |
correctIt ifFalse:[^ #Error] |
|
1173 |
] ifFalse:[ |
|
1174 |
correctIt := self warning:(varName , ' is undefined') position:pos1 to:pos2. |
|
1175 |
correctIt ifFalse:[ |
|
1176 |
^ VariableNode type:#GlobalVariable name:(varName asSymbol) |
|
1177 |
] |
|
1178 |
]. |
|
1179 |
" |
|
1180 |
||
1181 |
correctIt := self undefError:varName position:pos1 to:pos2. |
|
1182 |
correctIt ifFalse:[ |
|
1183 |
(varName at:1) isLowercase ifTrue:[ |
|
1184 |
^ #Error |
|
1185 |
] ifFalse:[ |
|
1186 |
^ VariableNode type:#GlobalVariable name:(varName asSymbol) |
|
1187 |
] |
|
1188 |
]. |
|
1189 |
||
1190 |
suggestedNames := self findBestVariablesFor:varName. |
|
1191 |
suggestedNames notNil ifTrue:[ |
|
1192 |
newName := self askForCorrection:'correct variable to: ' fromList:suggestedNames. |
|
1193 |
newName isNil ifTrue:[^ #Error]. |
|
1194 |
" |
|
1195 |
newName := suggestedNames at:1. |
|
1196 |
(self confirm:('confirm correction to: ' , newName)) ifFalse:[^ #Error] |
|
1197 |
" |
|
1198 |
] ifFalse:[ |
|
1199 |
self information:'no good correction found'. |
|
1200 |
^ #Error |
|
1201 |
]. |
|
1202 |
||
1203 |
" |
|
1204 |
tell requestor (i.e. CodeView) about the change |
|
1205 |
this will update what the requestor shows. |
|
1206 |
" |
|
1207 |
requestor replaceSelectionBy:newName. |
|
1208 |
" |
|
1209 |
get the updated source-string |
|
1210 |
which is needed, when we eventually install the new method |
|
1211 |
" |
|
1212 |
correctedSource := requestor currentSourceCode. |
|
1213 |
||
1214 |
"redo parse with new value" |
|
1215 |
tokenName := newName. |
|
1216 |
^ self variableOrError |
|
1217 |
! |
|
1218 |
||
1219 |
findBestSelectorsFor:aString |
|
1220 |
"collect known selectors with their spelling distances to aString; |
|
1221 |
return the 10 best suggestions" |
|
1222 |
||
1223 |
|info n| |
|
1224 |
||
1225 |
info := SortedCollection new. |
|
1226 |
info sortBlock:[:a :b | a value > b value]. |
|
1227 |
||
1228 |
n := 0. |
|
1229 |
||
1230 |
Symbol allInstancesDo:[:sym | |
|
1231 |
|dist| |
|
1232 |
||
1233 |
dist := aString spellAgainst:sym. |
|
1234 |
dist > 20 ifTrue:[ |
|
1235 |
info add:(sym -> dist). |
|
1236 |
n := n + 1. |
|
1237 |
n > 10 ifTrue:[ |
|
1238 |
info removeLast. |
|
1239 |
] |
|
1240 |
] |
|
1241 |
]. |
|
1242 |
||
1243 |
^ info asOrderedCollection collect:[:a | a key] |
|
1244 |
||
1245 |
"Time millisecondsToRun:[Parser new findBestSelectorsFor:'foo']" |
|
1246 |
"Parser new findBestSelectorsFor:'findBestSel'" |
|
1247 |
"Parser new findBestSelectorsFor:'fildBestSelectrFr'" |
|
1248 |
! |
|
1249 |
||
1250 |
findBestVariablesFor:aString |
|
1251 |
"collect known variables with their spelling distances to aString; |
|
1252 |
return the 10 best suggestions" |
|
1253 |
||
1254 |
|names dists searchBlock args vars globalVarName aClass className baseClass n| |
|
1255 |
||
1256 |
names := OrderedCollection new. |
|
1257 |
dists := OrderedCollection new. |
|
1258 |
||
1259 |
"block arguments" |
|
1260 |
searchBlock := currentBlock. |
|
1261 |
[searchBlock notNil] whileTrue:[ |
|
1262 |
args := searchBlock arguments. |
|
1263 |
args notNil ifTrue:[ |
|
1264 |
args do:[:aBlockArg | |
|
1265 |
names add:(aBlockArg name). |
|
1266 |
dists add:(aString spellAgainst: "levenshteinTo:"(aBlockArg name)) |
|
1267 |
] |
|
1268 |
]. |
|
1269 |
||
1270 |
vars := searchBlock variables. |
|
1271 |
vars notNil ifTrue:[ |
|
1272 |
vars do:[:aBlockVar | |
|
1273 |
names add:(aBlockVar name). |
|
1274 |
dists add:(aString spellAgainst: "levenshteinTo:"(aBlockVar name)) |
|
1275 |
] |
|
1276 |
]. |
|
1277 |
searchBlock := searchBlock home |
|
1278 |
]. |
|
1279 |
||
1280 |
"method-variables" |
|
1281 |
methodVars notNil ifTrue:[ |
|
1282 |
methodVarNames do:[:methodVarName | |
|
1283 |
names add:methodVarName. |
|
1284 |
dists add:(aString spellAgainst: "levenshteinTo:"methodVarName) |
|
1285 |
] |
|
1286 |
]. |
|
1287 |
||
1288 |
"method-arguments" |
|
1289 |
methodArgs notNil ifTrue:[ |
|
1290 |
methodArgNames do:[:methodArgName | |
|
1291 |
names add:methodArgName. |
|
1292 |
dists add:(aString spellAgainst: "levenshteinTo:"methodArgName) |
|
1293 |
] |
|
1294 |
]. |
|
1295 |
||
1296 |
"instance-variables" |
|
1297 |
classToCompileFor notNil ifTrue:[ |
|
1298 |
PrevInstVarNames do:[:instVarName | |
|
1299 |
names add:instVarName. |
|
1300 |
dists add:(aString spellAgainst: "levenshteinTo:"instVarName) |
|
1301 |
] |
|
1302 |
]. |
|
1303 |
||
1304 |
"class-variables" |
|
1305 |
classToCompileFor notNil ifTrue:[ |
|
1306 |
PrevClassVarNames do:[:classVarName | |
|
1307 |
names add:classVarName. |
|
1308 |
dists add:(aString spellAgainst: "levenshteinTo:"classVarName) |
|
1309 |
]. |
|
1310 |
||
1311 |
"/ aClass := classToCompileFor. |
|
1312 |
"/ aClass isMeta ifTrue:[ |
|
1313 |
"/ className := aClass name. |
|
1314 |
"/ className := className copyWithoutLast:5. |
|
1315 |
"/ baseClass := Smalltalk at:(className asSymbol). |
|
1316 |
"/ baseClass notNil ifTrue:[ |
|
1317 |
"/ aClass := baseClass |
|
1318 |
"/ ] |
|
1319 |
"/ ]. |
|
1320 |
"/ [aClass notNil] whileTrue:[ |
|
1321 |
"/ (aClass classVarNames) do:[:classVarName | |
|
1322 |
"/ names add:classVarName. |
|
1323 |
"/ dists add:(aString spellAgainst: "levenshteinTo:"classVarName) |
|
1324 |
"/ ]. |
|
1325 |
"/ aClass := aClass superclass |
|
1326 |
"/ ] |
|
1327 |
||
1328 |
]. |
|
1329 |
||
1330 |
"globals" |
|
1331 |
Smalltalk keysDo:[:aKey | |
|
1332 |
globalVarName := aKey asString. |
|
1333 |
"only compare strings where length is about right" |
|
1334 |
((globalVarName size - aString size) abs < 3) ifTrue:[ |
|
1335 |
names add:globalVarName. |
|
1336 |
dists add:(aString spellAgainst: "levenshteinTo:"globalVarName) |
|
1337 |
] |
|
1338 |
]. |
|
1339 |
||
1340 |
"misc" |
|
1341 |
#('self' 'super' 'nil' 'thisContext') do:[:name | |
|
1342 |
names add:name. |
|
1343 |
dists add:(aString spellAgainst: "levenshteinTo:"name) |
|
1344 |
]. |
|
1345 |
||
1346 |
(dists size ~~ 0) ifTrue:[ |
|
1347 |
dists sortWith:names. |
|
1348 |
dists := dists reverse. |
|
1349 |
names := names reverse. |
|
1350 |
n := names size min:10. |
|
1351 |
names := names copyTo:n. |
|
1352 |
||
1353 |
"if it starts with a lower case character, add all local & instvar names" |
|
1354 |
(aString at:1) isLowercase ifTrue:[ |
|
1355 |
methodVarNames size > 0 ifTrue:[ |
|
1356 |
names add:'---- method locals ----'. |
|
1357 |
methodVarNames asSortedCollection do:[:methodVarName | |
|
1358 |
names add:methodVarName. |
|
1359 |
]. |
|
1360 |
]. |
|
1361 |
||
1362 |
||
1363 |
methodArgs notNil ifTrue:[ |
|
1364 |
names add:'---- method arguments ----'. |
|
1365 |
methodArgNames asSortedCollection do:[:methodArgName | |
|
1366 |
names add:methodArgName. |
|
1367 |
] |
|
1368 |
]. |
|
1369 |
||
1370 |
names add:'---- instance variables ----'. |
|
1371 |
PrevInstVarNames asSortedCollection do:[:instVarName | |
|
1372 |
(names includes:instVarName) ifFalse:[ |
|
1373 |
names add:instVarName. |
|
1374 |
] |
|
1375 |
] |
|
1376 |
]. |
|
1377 |
||
1378 |
^ names |
|
1379 |
]. |
|
1380 |
^ nil |
|
1381 |
! |
|
1382 |
||
1383 |
selectorCheck:aSelectorString for:receiver position:pos1 to:pos2 |
|
1384 |
"just a quick check: if a selector is totally unknown as a symbol, |
|
1385 |
or has the same name as a variable or cannot be understood. |
|
1386 |
Simple, but catches many typos" |
|
1387 |
||
1388 |
|ok err sym rec superCls| |
|
1389 |
||
1390 |
" |
|
1391 |
if compiling lazy, or errors are to be ignored, or there |
|
1392 |
is no requestor, do not check |
|
1393 |
" |
|
1394 |
(LazyCompilation == true) ifTrue:[^ aSelectorString]. |
|
1395 |
(ignoreErrors or:[ignoreWarnings]) ifTrue:[^ aSelectorString]. |
|
1396 |
(requestor isNil or:[requestor isStream]) ifTrue:[^ aSelectorString]. |
|
1397 |
||
1398 |
err := ' is currently nowhere implemented'. |
|
1399 |
" |
|
1400 |
if the selector has the name of a variable, use another message |
|
1401 |
" |
|
1402 |
((methodVarNames notNil and:[methodVarNames includes:aSelectorString]) |
|
1403 |
or:[(methodArgNames notNil and:[methodArgNames includes:aSelectorString]) |
|
1404 |
or:[(self instVarNames notNil and:[self instVarNames includes:aSelectorString]) |
|
1405 |
or:[(self classInstVarNames notNil and:[self classInstVarNames includes:aSelectorString]) |
|
1406 |
or:[(self classVarNames notNil and:[self classVarNames includes:aSelectorString])]]]]) ifTrue:[ |
|
1407 |
err := ' is currently nowhere implemented .. |
|
1408 |
.. but a variable with that name is defined. |
|
1409 |
||
1410 |
Missing ''.'' after the previous expression ?'. |
|
1411 |
]. |
|
1412 |
||
1413 |
" |
|
1414 |
check if the selector is known at all |
|
1415 |
- if not, it cannot be understood |
|
1416 |
" |
|
1417 |
ok := false. |
|
1418 |
sym := aSelectorString asSymbolIfInterned. |
|
1419 |
sym notNil ifTrue:[ |
|
1420 |
ok := true. |
|
1421 |
receiver notNil ifTrue:[ |
|
1422 |
" |
|
1423 |
if the receiver is a constant, we can check if it responds |
|
1424 |
to this selector |
|
1425 |
" |
|
1426 |
receiver isConstant ifTrue:[ |
|
1427 |
ok := receiver evaluate respondsTo:sym. |
|
1428 |
err := ' will not be understood here'. |
|
1429 |
] ifFalse:[ |
|
1430 |
receiver isBlock ifTrue:[ |
|
1431 |
"/ this should help with typos, sending #ifTrue to blocks ... |
|
1432 |
ok := [] respondsTo:sym. |
|
1433 |
err := ' will not be understood here'. |
|
1434 |
] ifFalse:[ |
|
1435 |
" |
|
1436 |
if the receiver is a global, we check it too ... |
|
1437 |
" |
|
1438 |
receiver type == #GlobalVariable ifTrue:[ |
|
1439 |
"dont check autoloaded classes - it may work after |
|
1440 |
loading" |
|
1441 |
||
1442 |
rec := receiver evaluate. |
|
1443 |
(rec notNil |
|
1444 |
and:[rec isBehavior |
|
1445 |
and:[rec isLoaded not]]) ifTrue:[^ aSelectorString]. |
|
1446 |
||
1447 |
ok := rec respondsTo:sym. |
|
1448 |
err := ' may not be understood here'. |
|
1449 |
] ifFalse:[ |
|
1450 |
"if its a super send, we can do more checking" |
|
1451 |
receiver isSuper ifTrue:[ |
|
1452 |
receiver isHere ifFalse:[ |
|
1453 |
((superCls := classToCompileFor superclass) notNil |
|
1454 |
and:[(superCls whichClassIncludesSelector:sym) isNil]) ifTrue:[ |
|
1455 |
err := ' is currently not implemented in any superclass'. |
|
1456 |
ok := false |
|
1457 |
] |
|
1458 |
] ifTrue:[ |
|
1459 |
(classToCompileFor whichClassIncludesSelector:sym) isNil ifTrue:[ |
|
1460 |
err := ' is currently not implemented in this class'. |
|
1461 |
ok := false |
|
1462 |
] |
|
1463 |
] |
|
1464 |
]. |
|
1465 |
||
1466 |
(receiver isUnaryMessage |
|
1467 |
and:[receiver selector == #class |
|
1468 |
and:[receiver receiver type == #Self]]) ifTrue:[ |
|
1469 |
"its a message to self class - can check this too ..." |
|
1470 |
(classToCompileFor class whichClassIncludesSelector:sym) isNil ifTrue:[ |
|
1471 |
ok := false. |
|
1472 |
classToCompileFor allSubclasses do:[:subclass | |
|
1473 |
(subclass class implements:sym) ifTrue:[ |
|
1474 |
ok := true |
|
1475 |
] |
|
1476 |
]. |
|
1477 |
err := ' is currently not implemented in the class'. |
|
1478 |
] |
|
1479 |
] |
|
1480 |
] |
|
1481 |
] |
|
1482 |
] |
|
1483 |
] |
|
1484 |
]. |
|
1485 |
||
1486 |
ok ifFalse:[ |
|
1487 |
||
1488 |
"OLD: " |
|
1489 |
self warning:('#' , aSelectorString , err) position:pos1 to:pos2 |
|
1490 |
" " |
|
1491 |
||
1492 |
"NEW: - not finished - need more interfaces |
|
1493 |
(currently produces warning output on Transcript while filing in |
|
1494 |
||
1495 |
||
1496 |
^ self correctSelector:aSelectorString message:('#' , aSelectorString , err) position:pos1 to:pos2 |
|
1497 |
" |
|
1498 |
]. |
|
1499 |
^ aSelectorString |
|
1500 |
||
1501 |
"Modified: 5.9.1995 / 17:02:11 / claus" |
|
0 | 1502 |
! ! |
1503 |
||
1504 |
!Parser methodsFor:'error handling'! |
|
1505 |
||
141 | 1506 |
correctableError:message position:pos1 to:pos2 |
1507 |
"report an error which can be corrected by compiler - |
|
1508 |
return true if correction is wanted" |
|
1509 |
||
1510 |
|correctIt| |
|
1511 |
||
1512 |
requestor isNil ifTrue:[ |
|
1513 |
self showErrorMessage:message position:pos1. |
|
1514 |
correctIt := false |
|
1515 |
] ifFalse:[ |
|
1516 |
correctIt := requestor correctableError:message position:pos1 to:pos2 from:self |
|
1517 |
]. |
|
1518 |
correctIt ifFalse:[ |
|
1519 |
exitBlock notNil ifTrue:[exitBlock value] |
|
1520 |
]. |
|
1521 |
^ correctIt |
|
1522 |
! |
|
1523 |
||
1524 |
exitWith:something |
|
1525 |
"this is the longjump out of evaluation via a return expression" |
|
1526 |
||
1527 |
evalExitBlock value:something |
|
1528 |
! |
|
1529 |
||
1530 |
identifierExpectedIn:what |
|
1531 |
|msg| |
|
1532 |
||
1533 |
(#(True False Self Nil Super ThisContext) includes:tokenType) ifTrue:[ |
|
1534 |
msg := 'Reserved keyword in ' |
|
1535 |
] ifFalse:[ |
|
1536 |
msg := 'Identifier expected in ' |
|
1537 |
]. |
|
1538 |
self syntaxError:msg , what position:tokenPosition to:source position - 1. |
|
1539 |
^ #Error |
|
1540 |
! |
|
1541 |
||
1542 |
parseError:aMessage |
|
1543 |
"report an error" |
|
1544 |
||
1545 |
^ self parseError:aMessage position:tokenPosition to:nil |
|
1546 |
! |
|
1547 |
||
1548 |
parseError:aMessage position:position |
|
1549 |
"report an error" |
|
1550 |
||
1551 |
^ self parseError:aMessage position:position to:nil |
|
1552 |
! |
|
1553 |
||
1554 |
parseError:aMessage position:position to:endPos |
|
1555 |
"report an error" |
|
1556 |
||
1557 |
|m| |
|
1558 |
||
1559 |
errorFlag := true. |
|
1560 |
m := 'Error: ' , aMessage. |
|
1561 |
self notifyError:m position:position to:endPos. |
|
1562 |
exitBlock notNil ifTrue:[exitBlock value]. |
|
1563 |
^ false |
|
1564 |
! |
|
1565 |
||
0 | 1566 |
showErrorMessage:aMessage position:pos |
19 | 1567 |
"redefined since parser can give more detailed info about |
1568 |
the class & selector where the error occured." |
|
1569 |
||
71 | 1570 |
|text| |
1571 |
||
19 | 1572 |
ignoreErrors ifFalse:[ |
44 | 1573 |
Smalltalk silentLoading == true ifFalse:[ |
1574 |
Transcript show:(pos printString). |
|
1575 |
Transcript show:' '. |
|
1576 |
selector notNil ifTrue:[ |
|
1577 |
Transcript show:aMessage. |
|
1578 |
classToCompileFor notNil ifTrue:[ |
|
71 | 1579 |
text := ' in ' , classToCompileFor name , '>>' , selector |
44 | 1580 |
] ifFalse:[ |
71 | 1581 |
text := ' in ' , selector |
44 | 1582 |
] |
1583 |
] ifFalse:[ |
|
1584 |
classToCompileFor notNil ifTrue:[ |
|
71 | 1585 |
text := aMessage , ' (' , classToCompileFor name , ')' |
44 | 1586 |
] ifFalse:[ |
71 | 1587 |
text := aMessage |
44 | 1588 |
] |
71 | 1589 |
]. |
1590 |
Transcript showCr:text. |
|
44 | 1591 |
] |
0 | 1592 |
] |
1593 |
! |
|
1594 |
||
1595 |
undefError:aName position:pos1 to:pos2 |
|
3 | 1596 |
"report an undefined variable error - return true, if it should be |
86 | 1597 |
corrected. If not corrected, only one warning is made per undefined |
1598 |
variable." |
|
1599 |
||
131 | 1600 |
|doCorrect msg idx hasSourceInfoStripped| |
86 | 1601 |
|
1602 |
" |
|
1603 |
alredy warned about this one ? |
|
1604 |
" |
|
1605 |
warnedUndefVars notNil ifTrue:[ |
|
1606 |
(warnedUndefVars includes:aName) ifTrue:[ |
|
1607 |
"already warned about this one" |
|
1608 |
^ false |
|
44 | 1609 |
]. |
86 | 1610 |
]. |
1611 |
||
131 | 1612 |
"/ (classToCompileFor notNil |
1613 |
"/ and:[classToCompileFor superclass notNil |
|
1614 |
"/ and:[classToCompileFor superclass instanceVariableString isNil]]) ifTrue:[ |
|
132
5be56a5e90b7
added methods for silent parsing (parseMethod / parseMethodSpec)
Claus Gittinger <cg@exept.de>
parents:
131
diff
changeset
|
1615 |
"/ self showErrorMessage:'Error: no source information (instvar names)' position:pos1. |
5be56a5e90b7
added methods for silent parsing (parseMethod / parseMethodSpec)
Claus Gittinger <cg@exept.de>
parents:
131
diff
changeset
|
1616 |
"/ ^ false |
131 | 1617 |
"/ ]. |
1618 |
||
1619 |
(requestor isNil or:[requestor isStream]) ifTrue:[ |
|
56 | 1620 |
aName first isUppercase ifFalse:[ |
1621 |
self showErrorMessage:('Error: ' , aName , ' is undefined') position:pos1. |
|
1622 |
]. |
|
86 | 1623 |
doCorrect := false. |
1624 |
] ifFalse:[ |
|
1625 |
" |
|
1626 |
ask requestor for correct/continue/abort ... |
|
1627 |
it is supposed to raise abort or return true/false. |
|
1628 |
True return means that correction is wanted. |
|
1629 |
" |
|
95 | 1630 |
msg := 'Warning: ' , aName , ' is undefined'. |
1631 |
classToCompileFor notNil ifTrue:[ |
|
1632 |
"is it an instance-variable marked inaccessable ?" |
|
1633 |
||
1634 |
idx := (self instVarNames) indexOf:(aName , '*') startingAt:1. |
|
1635 |
idx ~~ 0 ifTrue:[ |
|
1636 |
msg := 'Warning: ' , aName , ' is a hidden instvar (not accessable from ST-code)'. |
|
1637 |
] |
|
1638 |
]. |
|
1639 |
||
1640 |
doCorrect := self correctableError:msg position:pos1 to:pos2 |
|
86 | 1641 |
]. |
1642 |
||
1643 |
doCorrect ifFalse:[ |
|
44 | 1644 |
warnedUndefVars isNil ifTrue:[ |
1645 |
warnedUndefVars := Set new. |
|
1646 |
]. |
|
1647 |
warnedUndefVars add:aName. |
|
3 | 1648 |
]. |
0 | 1649 |
|
86 | 1650 |
^ doCorrect |
0 | 1651 |
! ! |
1652 |
||
1653 |
!Parser methodsFor:'parsing'! |
|
1654 |
||
141 | 1655 |
array |
1656 |
|arr elements elem pos1| |
|
1657 |
||
1658 |
pos1 := tokenPosition. |
|
1659 |
elements := OrderedCollection new:20. |
|
1660 |
[tokenType ~~ $) ] whileTrue:[ |
|
1661 |
elem := self arrayConstant. |
|
1662 |
(elem == #Error) ifTrue:[ |
|
1663 |
(tokenType == #EOF) ifTrue:[ |
|
1664 |
self syntaxError:'unterminated array-constant; '')'' expected' |
|
1665 |
position:pos1 to:tokenPosition |
|
1666 |
]. |
|
1667 |
^ #Error |
|
1668 |
]. |
|
1669 |
elements add:elem. |
|
1670 |
self nextToken |
|
1671 |
]. |
|
1672 |
arr := Array withAll:elements. |
|
1673 |
||
1674 |
(ArraysAreImmutable and:[ImmutableArray notNil]) ifTrue:[ |
|
1675 |
arr changeClassTo:ImmutableArray. |
|
1676 |
]. |
|
1677 |
^ arr |
|
1678 |
! |
|
1679 |
||
1680 |
arrayConstant |
|
1681 |
(tokenType == #String) ifTrue:[ |
|
1682 |
^ tokenValue |
|
1683 |
]. |
|
1684 |
(tokenType == #Nil) ifTrue:[ |
|
1685 |
^ nil |
|
1686 |
]. |
|
1687 |
(tokenType == #Integer) ifTrue:[ |
|
1688 |
^ tokenValue |
|
1689 |
]. |
|
1690 |
(tokenType == #Character) ifTrue:[ |
|
1691 |
^ tokenValue |
|
1692 |
]. |
|
1693 |
(tokenType == #Float) ifTrue:[ |
|
1694 |
^ tokenValue |
|
1695 |
]. |
|
1696 |
(tokenType == #True) ifTrue:[ |
|
1697 |
^ true |
|
1698 |
]. |
|
1699 |
(tokenType == #False) ifTrue:[ |
|
1700 |
^ false |
|
1701 |
]. |
|
1702 |
(tokenType == #Error) ifTrue:[ |
|
1703 |
^ #Error |
|
1704 |
]. |
|
1705 |
(tokenType == #BinaryOperator) ifTrue:[ |
|
1706 |
^ tokenName asSymbol |
|
1707 |
]. |
|
1708 |
(tokenType == #Keyword) ifTrue:[ |
|
1709 |
^ tokenName asSymbol |
|
1710 |
]. |
|
1711 |
(tokenType == #Identifier) ifTrue:[ |
|
1712 |
^ tokenName asSymbol |
|
1713 |
]. |
|
1714 |
(tokenType == $() ifTrue:[ |
|
1715 |
self nextToken. |
|
1716 |
^ self array |
|
1717 |
]. |
|
1718 |
(tokenType == $[) ifTrue:[ |
|
1719 |
self nextToken. |
|
1720 |
^ self byteArray |
|
1721 |
]. |
|
1722 |
(tokenType == #Symbol) ifTrue:[ |
|
1723 |
" |
|
1724 |
self warning:'no # for symbols within array-constants'. |
|
1725 |
" |
|
1726 |
^ tokenValue |
|
1727 |
]. |
|
1728 |
(tokenType == #HashLeftParen) ifTrue:[ |
|
1729 |
" |
|
1730 |
self warning:'no # for arrays within array-constants'. |
|
1731 |
" |
|
1732 |
self nextToken. |
|
1733 |
^ self array |
|
1734 |
]. |
|
1735 |
(tokenType == #HashLeftBrack) ifTrue:[ |
|
1736 |
" |
|
1737 |
self warning:'no # for arrays within array-constants'. |
|
1738 |
" |
|
1739 |
self nextToken. |
|
1740 |
^ self byteArray |
|
1741 |
]. |
|
1742 |
(tokenType == #EOF) ifTrue:[ |
|
1743 |
"just for the better error-hilight; let caller handle error" |
|
1744 |
^ #Error |
|
1745 |
]. |
|
1746 |
self syntaxError:('error in array-constant; ' |
|
1747 |
, tokenType printString |
|
1748 |
, ' unexpected'). |
|
1749 |
^ #Error |
|
1750 |
! |
|
1751 |
||
1752 |
binaryExpression |
|
1753 |
"parse a binary-expression; return a node-tree, nil or #Error" |
|
1754 |
||
1755 |
|receiver arg sel pos try lno note| |
|
1756 |
||
1757 |
receiver := self unaryExpression. |
|
1758 |
(receiver == #Error) ifTrue:[^ #Error]. |
|
1759 |
||
1760 |
"special kludge: since Scanner cannot know if -digit is a binary |
|
1761 |
expression or a negative constant, handle cases here" |
|
1762 |
||
1763 |
[(tokenType == #BinaryOperator) or:[(tokenType == $|) |
|
1764 |
or:[(tokenType == #Integer) and:[tokenValue < 0]]]] whileTrue:[ |
|
1765 |
pos := tokenPosition. |
|
1766 |
||
1767 |
lno := tokenLineNr. |
|
1768 |
||
1769 |
"kludge here: bar and minus are not scanned as binop " |
|
1770 |
(tokenType == $|) ifTrue:[ |
|
1771 |
sel := '|'. |
|
1772 |
self nextToken |
|
1773 |
] ifFalse:[ |
|
1774 |
(tokenType == #BinaryOperator) ifTrue:[ |
|
1775 |
sel := self selectorCheck:tokenName for:receiver position:tokenPosition to:(tokenPosition + tokenName size - 1). |
|
1776 |
self nextToken |
|
1777 |
] ifFalse:[ |
|
1778 |
sel := '-'. |
|
1779 |
tokenValue := tokenValue negated |
|
1780 |
] |
|
1781 |
]. |
|
1782 |
arg := self unaryExpression. |
|
1783 |
(arg == #Error) ifTrue:[^ #Error]. |
|
1784 |
try := BinaryNode receiver:receiver selector:sel arg:arg. |
|
1785 |
(try isMemberOf:String) ifTrue:[ |
|
1786 |
self parseError:try position:pos to:tokenPosition. |
|
1787 |
errorFlag := false. "ok, user wants it - so he'll get it" |
|
1788 |
receiver := BinaryNode receiver:receiver selector:sel arg:arg fold:false. |
|
1789 |
note := receiver plausibilityCheck. |
|
1790 |
note notNil ifTrue:[ |
|
1791 |
self warning:note position:pos to:tokenPosition |
|
1792 |
]. |
|
1793 |
] ifFalse:[ |
|
1794 |
receiver := try |
|
1795 |
]. |
|
1796 |
receiver lineNumber:lno. |
|
1797 |
parseForCode ifFalse:[self rememberSelectorUsed:sel]. |
|
1798 |
]. |
|
1799 |
^ receiver |
|
1800 |
! |
|
1801 |
||
1802 |
block |
|
1803 |
"parse a block; return a node-tree, nil or #Error" |
|
1804 |
||
1805 |
|node args argNames arg pos lno| |
|
1806 |
||
1807 |
lno := tokenLineNr. |
|
1808 |
self nextToken. |
|
1809 |
(tokenType == $: ) ifTrue:[ |
|
1810 |
[tokenType == $:] whileTrue:[ |
|
1811 |
pos := tokenPosition. |
|
1812 |
self nextToken. |
|
1813 |
(tokenType == #Identifier) ifFalse:[ |
|
1814 |
^ self identifierExpectedIn:'block-arg declaration' |
|
1815 |
]. |
|
1816 |
arg := Variable name:tokenName. |
|
1817 |
args isNil ifTrue:[ |
|
1818 |
args := Array with:arg. |
|
1819 |
argNames := Array with:tokenName. |
|
1820 |
] ifFalse:[ |
|
1821 |
(argNames includes:tokenName) ifTrue:[ |
|
1822 |
self syntaxError:'redefinition of ''' , tokenName , ''' in argument list.' |
|
1823 |
position:tokenPosition |
|
1824 |
to:(tokenPosition + tokenName size - 1) |
|
1825 |
]. |
|
1826 |
args := args copyWith:arg. |
|
1827 |
argNames := argNames copyWith:tokenName. |
|
1828 |
]. |
|
1829 |
self nextToken |
|
1830 |
]. |
|
1831 |
(tokenType ~~ $| ) ifTrue:[ |
|
1832 |
"ST-80 allows [:arg ]" |
|
1833 |
(tokenType == $] ) ifTrue:[ |
|
1834 |
node := BlockNode arguments:args home:currentBlock variables:nil. |
|
1835 |
node lineNumber:lno. |
|
1836 |
^ node |
|
1837 |
]. |
|
1838 |
self syntaxError:'| expected after block-arg declaration'. |
|
1839 |
^ #Error |
|
1840 |
]. |
|
1841 |
self nextToken |
|
1842 |
]. |
|
1843 |
node := self blockBody:args. |
|
1844 |
(node notNil and:[node ~~ #Error]) ifTrue:[ |
|
1845 |
node lineNumber:lno. |
|
1846 |
]. |
|
1847 |
^ node |
|
1848 |
! |
|
1849 |
||
1850 |
blockBody:args |
|
1851 |
"parse a blocks body; return a node-tree, nil or #Error" |
|
1852 |
||
1853 |
|stats node var vars lno names| |
|
1854 |
||
1855 |
lno := tokenLineNr. |
|
1856 |
(tokenType == $| ) ifTrue:[ |
|
1857 |
self nextToken. |
|
1858 |
[tokenType == $|] whileFalse:[ |
|
1859 |
(tokenType == #Identifier) ifFalse:[ |
|
1860 |
^ self identifierExpectedIn:'block-var declaration' |
|
1861 |
]. |
|
1862 |
var := Variable name:tokenName. |
|
1863 |
vars isNil ifTrue:[ |
|
1864 |
vars := Array with:var. |
|
1865 |
names := Array with:tokenName |
|
1866 |
] ifFalse:[ |
|
1867 |
(names includes:tokenName) ifTrue:[ |
|
1868 |
self parseError:'redefinition of ''' , tokenName , ''' in local variables' |
|
1869 |
position:tokenPosition to:tokenPosition + tokenName size -1. |
|
1870 |
] ifFalse:[ |
|
1871 |
vars := vars copyWith:var. |
|
1872 |
names := names copyWith:tokenName |
|
1873 |
] |
|
1874 |
]. |
|
1875 |
self nextToken. |
|
1876 |
]. |
|
1877 |
self nextToken |
|
1878 |
]. |
|
1879 |
node := BlockNode arguments:args home:currentBlock variables:vars. |
|
1880 |
node lineNumber:lno. |
|
1881 |
currentBlock := node. |
|
1882 |
stats := self blockStatementList. |
|
1883 |
node statements:stats. |
|
1884 |
currentBlock := node home. |
|
1885 |
(stats == #Error) ifTrue:[^ #Error]. |
|
1886 |
^ node |
|
1887 |
! |
|
1888 |
||
1889 |
blockExpression |
|
1890 |
"parse a blockExpression; return a node-tree, nil or #Error. |
|
1891 |
Not used by ST/X's parser, but added for ST-80 compatibility." |
|
1892 |
||
1893 |
tokenType ~~ $[ ifTrue:[ |
|
1894 |
self syntaxError:'[ expected'. |
|
1895 |
^ #Error. |
|
1896 |
]. |
|
1897 |
^ self block |
|
1898 |
! |
|
1899 |
||
1900 |
blockStatementList |
|
1901 |
"parse a blocks statementlist; return a node-tree, nil or #Error" |
|
1902 |
||
1903 |
|thisStatement prevStatement firstStatement| |
|
1904 |
||
1905 |
(tokenType == $] ) ifTrue:[^ nil]. |
|
1906 |
thisStatement := self statement. |
|
1907 |
(thisStatement == #Error) ifTrue:[^ #Error]. |
|
1908 |
firstStatement := thisStatement. |
|
1909 |
[tokenType == $] ] whileFalse:[ |
|
1910 |
(tokenType == $.) ifFalse:[ |
|
1911 |
((tokenType == #EOF) or:[tokenType == $)]) ifTrue:[ |
|
1912 |
self syntaxError:'missing '']'' in block' |
|
1913 |
] ifFalse:[ |
|
1914 |
self syntaxError:'missing ''.'' in block' |
|
1915 |
]. |
|
1916 |
^ #Error |
|
1917 |
] ifTrue:[ |
|
1918 |
prevStatement := thisStatement. |
|
1919 |
self nextToken. |
|
1920 |
tokenType == $] ifTrue:[ |
|
1921 |
" |
|
1922 |
*** I had a warning here (since it was not defined |
|
1923 |
*** in the blue-book; but PD-code contains a lot of |
|
1924 |
*** code with periods at the end so that the warnings |
|
1925 |
*** became annoying |
|
1926 |
||
1927 |
self warning:'period after last statement in block'. |
|
1928 |
" |
|
1929 |
^ firstStatement |
|
1930 |
]. |
|
1931 |
thisStatement := self statement. |
|
1932 |
(thisStatement == #Error) ifTrue:[^ #Error]. |
|
1933 |
prevStatement nextStatement:thisStatement |
|
1934 |
] |
|
1935 |
]. |
|
1936 |
^ firstStatement |
|
1937 |
! |
|
1938 |
||
1939 |
byteArray |
|
1940 |
"started with ST-80 R4 - allow byteArray constants as #[ ... ]" |
|
1941 |
||
1942 |
|bytes index limit newArray elem pos1 pos2| |
|
1943 |
||
1944 |
pos1 := tokenPosition. |
|
1945 |
bytes := ByteArray uninitializedNew:5000. |
|
1946 |
index := 0. limit := 5000. |
|
1947 |
[tokenType ~~ $] ] whileTrue:[ |
|
1948 |
pos2 := tokenPosition. |
|
1949 |
" |
|
1950 |
this is not good programming style, but speeds up |
|
1951 |
reading of huge byte arrays (i.e. stored Images ...) |
|
1952 |
" |
|
1953 |
(tokenType == #Integer) ifTrue:[ |
|
1954 |
elem := tokenValue |
|
1955 |
] ifFalse:[ |
|
1956 |
elem := self arrayConstant. |
|
1957 |
(elem == #Error) ifTrue:[ |
|
1958 |
(tokenType == #EOF) ifTrue:[ |
|
1959 |
self syntaxError:'unterminated bytearray-constant; '']'' expected' |
|
1960 |
position:pos1 to:tokenPosition |
|
1961 |
]. |
|
1962 |
^ #Error |
|
1963 |
]. |
|
1964 |
]. |
|
1965 |
((elem isMemberOf:SmallInteger) and:[elem between:0 and:255]) ifTrue:[ |
|
1966 |
index := index + 1. |
|
1967 |
bytes at:index put:elem. |
|
1968 |
index == limit ifTrue:[ |
|
1969 |
newArray := ByteArray uninitializedNew:(limit * 2). |
|
1970 |
newArray replaceFrom:1 to:limit with:bytes startingAt:1. |
|
1971 |
limit := limit * 2. |
|
1972 |
bytes := newArray |
|
1973 |
]. |
|
1974 |
] ifFalse:[ |
|
1975 |
self parseError:'invalid ByteArray element' position:pos2 to:tokenPosition - 1 |
|
1976 |
]. |
|
1977 |
self nextToken. |
|
1978 |
]. |
|
1979 |
newArray := ByteArray uninitializedNew:index. |
|
1980 |
newArray replaceFrom:1 to:index with:bytes startingAt:1. |
|
1981 |
^ newArray |
|
1982 |
! |
|
1983 |
||
1984 |
degeneratedKeywordExpressionForSelector |
|
1985 |
"parse a keyword-expression without receiver - for the selector |
|
1986 |
only. return the selector or nil. This is not used in normal parsing, |
|
1987 |
but instead to extract the selector from a code fragment. |
|
1988 |
(for example, the system browsers implementors-function uses this)" |
|
1989 |
||
1990 |
|sel arg rec| |
|
1991 |
||
1992 |
(tokenType == #Keyword) ifTrue:[ |
|
1993 |
sel := tokenName. |
|
1994 |
self nextToken. |
|
1995 |
arg := self binaryExpression. |
|
1996 |
(arg == #Error) ifTrue:[^ sel]. |
|
1997 |
[tokenType == #Keyword] whileTrue:[ |
|
1998 |
sel := sel , tokenName. |
|
1999 |
self nextToken. |
|
2000 |
arg := self binaryExpression. |
|
2001 |
(arg == #Error) ifTrue:[^ sel]. |
|
2002 |
]. |
|
2003 |
^ sel |
|
2004 |
]. |
|
2005 |
||
2006 |
(rec := self primary) ~~ #Error ifTrue:[ |
|
2007 |
sel := self degeneratedKeywordExpressionForSelector. |
|
2008 |
sel isNil ifTrue:[ |
|
2009 |
rec isMessage ifTrue:[ |
|
2010 |
sel := rec selector |
|
2011 |
] ifFalse:[ |
|
2012 |
rec isAssignment ifTrue:[ |
|
2013 |
rec expression isMessage ifTrue:[ |
|
2014 |
sel := rec expression selector |
|
2015 |
] |
|
2016 |
] |
|
2017 |
] |
|
2018 |
] |
|
2019 |
]. |
|
2020 |
^ sel |
|
2021 |
! |
|
2022 |
||
2023 |
expression |
|
2024 |
"parse a cascade-expression; return a node-tree, nil or #Error. |
|
2025 |
||
2026 |
expression ::= keywordExpression |
|
2027 |
| keywordExpression cascade |
|
2028 |
||
2029 |
cascade ::= ';' expressionSendPart |
|
2030 |
| cascade ';' expressionSendPart |
|
2031 |
||
2032 |
expressionSendPart ::= { KEYWORD binaryExpression } |
|
2033 |
| BINARYOPERATOR unaryExpression |
|
2034 |
| IDENTIFIER |
|
2035 |
" |
|
2036 |
||
2037 |
|receiver arg sel args pos pos2 lno| |
|
2038 |
||
2039 |
pos := tokenPosition. |
|
2040 |
receiver := self keywordExpression. |
|
2041 |
(receiver == #Error) ifTrue:[^ #Error]. |
|
2042 |
(tokenType == $;) ifTrue:[ |
|
2043 |
[tokenType == $;] whileTrue:[ |
|
2044 |
receiver isMessage ifFalse:[ |
|
2045 |
self syntaxError:'left side of cascade must be a message expression' |
|
2046 |
position:pos to:tokenPosition |
|
2047 |
]. |
|
2048 |
self nextToken. |
|
2049 |
(tokenType == #Identifier) ifTrue:[ |
|
2050 |
sel := self selectorCheck:tokenName for:receiver position:tokenPosition to:(tokenPosition + tokenName size - 1). |
|
2051 |
receiver := CascadeNode receiver:receiver selector:sel. |
|
2052 |
receiver lineNumber:tokenLineNr. |
|
2053 |
parseForCode ifFalse:[self rememberSelectorUsed:sel]. |
|
2054 |
self nextToken. |
|
2055 |
] ifFalse:[ |
|
2056 |
(tokenType == #BinaryOperator) ifTrue:[ |
|
2057 |
sel := self selectorCheck:tokenName for:receiver position:tokenPosition to:(tokenPosition + tokenName size - 1). |
|
2058 |
lno := tokenLineNr. |
|
2059 |
self nextToken. |
|
2060 |
arg := self unaryExpression. |
|
2061 |
(arg == #Error) ifTrue:[^ #Error]. |
|
2062 |
receiver := CascadeNode receiver:receiver selector:sel arg:arg. |
|
2063 |
receiver lineNumber:lno. |
|
2064 |
parseForCode ifFalse:[self rememberSelectorUsed:sel]. |
|
2065 |
] ifFalse:[ |
|
2066 |
(tokenType == #Keyword) ifTrue:[ |
|
2067 |
pos := tokenPosition. |
|
2068 |
lno := tokenLineNr. |
|
2069 |
sel := tokenName. |
|
2070 |
self nextToken. |
|
2071 |
arg := self binaryExpression. |
|
2072 |
(arg == #Error) ifTrue:[^ #Error]. |
|
2073 |
args := Array with:arg. |
|
2074 |
[tokenType == #Keyword] whileTrue:[ |
|
2075 |
sel := sel , tokenName. |
|
2076 |
self nextToken. |
|
2077 |
arg := self binaryExpression. |
|
2078 |
(arg == #Error) ifTrue:[^ #Error]. |
|
2079 |
args := args copyWith:arg. |
|
2080 |
pos2 := tokenPosition |
|
2081 |
]. |
|
2082 |
sel := self selectorCheck:sel for:receiver position:pos to:pos2. |
|
2083 |
receiver := CascadeNode receiver:receiver selector:sel args:args. |
|
2084 |
receiver lineNumber:lno. |
|
2085 |
parseForCode ifFalse:[self rememberSelectorUsed:sel]. |
|
2086 |
] ifFalse:[ |
|
2087 |
(tokenType == #Error) ifTrue:[^ #Error]. |
|
2088 |
self syntaxError:('invalid cascade; ' , tokenType printString , ' unexpected') |
|
2089 |
position:tokenPosition to:source position - 1. |
|
2090 |
^ #Error |
|
2091 |
] |
|
2092 |
] |
|
2093 |
] |
|
2094 |
]. |
|
2095 |
||
2096 |
"obscure (uspecified ?) if selector follows; Question: |
|
2097 |
||
2098 |
is |
|
2099 |
'expr sel1; sel2 sel3' |
|
2100 |
||
2101 |
to be parsed as: |
|
2102 |
(t := expr. |
|
2103 |
t sel1. |
|
2104 |
t sel2) sel3 |
|
2105 |
||
2106 |
or: |
|
2107 |
(t := expr. |
|
2108 |
t sel1. |
|
2109 |
t sel2 sel3) |
|
2110 |
" |
|
2111 |
((tokenType == #Identifier) |
|
2112 |
or:[(tokenType == #BinaryOperator) |
|
2113 |
or:[tokenType == #Keyword]]) ifTrue:[ |
|
2114 |
self syntaxError:'ambigous cascade - please group using ( ...)' |
|
2115 |
position:tokenPosition to:source position - 1. |
|
2116 |
^ #Error |
|
2117 |
] |
|
2118 |
]. |
|
2119 |
^ receiver |
|
2120 |
! |
|
2121 |
||
2122 |
inWhichClassIsClassInstVar:aString |
|
2123 |
"search class-chain for the class-instance variable named aString |
|
2124 |
- return the class or nil if not found" |
|
2125 |
||
2126 |
|aClass| |
|
2127 |
||
2128 |
aClass := classToCompileFor. |
|
2129 |
[aClass notNil] whileTrue:[ |
|
2130 |
(aClass class instVarNames includes:aString) ifTrue:[ ^ aClass]. |
|
2131 |
aClass := aClass superclass |
|
2132 |
]. |
|
2133 |
^ nil |
|
2134 |
! |
|
2135 |
||
2136 |
inWhichClassIsClassVar:aString |
|
2137 |
"search class-chain for the classvariable named aString |
|
2138 |
- return the class or nil if not found" |
|
2139 |
||
2140 |
|aClass className baseClass| |
|
2141 |
||
2142 |
aClass := classToCompileFor. |
|
2143 |
aClass isMeta ifTrue:[ |
|
2144 |
className := aClass name. |
|
2145 |
className := className copyWithoutLast:5. |
|
2146 |
baseClass := Smalltalk at:(className asSymbol). |
|
2147 |
baseClass notNil ifTrue:[ |
|
2148 |
aClass := baseClass |
|
2149 |
] |
|
2150 |
]. |
|
2151 |
^ aClass whichClassDefinesClassVar:aString |
|
2152 |
"/ [aClass notNil] whileTrue:[ |
|
2153 |
"/ (aClass classVarNames includes:aString) ifTrue:[ ^ aClass]. |
|
2154 |
"/ aClass := aClass superclass |
|
2155 |
"/ ]. |
|
2156 |
"/ ^ nil |
|
2157 |
! |
|
2158 |
||
2159 |
keywordExpression |
|
2160 |
"parse a keyword-expression; return a node-tree, nil or #Error. |
|
2161 |
||
2162 |
keywordExpression ::= binaryexpression |
|
2163 |
| { KEYWORD-PART binaryExpression } |
|
2164 |
" |
|
2165 |
||
2166 |
|receiver sel arg args pos1 pos2 try lno note| |
|
2167 |
||
2168 |
receiver := self binaryExpression. |
|
2169 |
(receiver == #Error) ifTrue:[^ #Error]. |
|
2170 |
(tokenType == #Keyword) ifTrue:[ |
|
2171 |
pos1 := tokenPosition. |
|
2172 |
pos2 := tokenPosition + tokenName size - 1. |
|
2173 |
sel := tokenName. |
|
2174 |
lno := tokenLineNr. |
|
2175 |
self nextToken. |
|
2176 |
arg := self binaryExpression. |
|
2177 |
(arg == #Error) ifTrue:[^ #Error]. |
|
2178 |
args := Array with:arg. |
|
2179 |
[tokenType == #Keyword] whileTrue:[ |
|
2180 |
sel := sel , tokenName. |
|
2181 |
pos2 := tokenPosition + tokenName size - 1. |
|
2182 |
self nextToken. |
|
2183 |
arg := self binaryExpression. |
|
2184 |
(arg == #Error) ifTrue:[^ #Error]. |
|
2185 |
args := args copyWith:arg. |
|
2186 |
]. |
|
2187 |
sel := self selectorCheck:sel for:receiver position:pos1 to:pos2. |
|
2188 |
try := MessageNode receiver:receiver selector:sel args:args. |
|
2189 |
(try isMemberOf:String) ifTrue:[ |
|
2190 |
self parseError:try position:pos1 to:pos2. |
|
2191 |
errorFlag := false. "ok, user wants it - so he'll get it" |
|
2192 |
receiver := MessageNode receiver:receiver selector:sel args:args fold:false. |
|
2193 |
note := receiver plausibilityCheck. |
|
2194 |
note notNil ifTrue:[ |
|
2195 |
self warning:note position:pos1 to:pos2 |
|
2196 |
]. |
|
2197 |
] ifFalse:[ |
|
2198 |
receiver := try |
|
2199 |
]. |
|
2200 |
receiver lineNumber:lno. |
|
2201 |
parseForCode ifFalse:[self rememberSelectorUsed:sel]. |
|
2202 |
]. |
|
2203 |
^ receiver |
|
2204 |
! |
|
2205 |
||
0 | 2206 |
parseMethod |
19 | 2207 |
"parse a method. |
2208 |
Return the parseTree or #Error. |
|
2209 |
||
2210 |
method ::= methodSpec methodBody |
|
2211 |
" |
|
0 | 2212 |
|
2213 |
|parseTree| |
|
2214 |
||
2215 |
self nextToken. |
|
2216 |
(self parseMethodSpec == #Error) ifTrue:[^ #Error]. |
|
2217 |
parseTree := self parseMethodBody. |
|
2218 |
(parseTree == #Error) ifFalse:[ |
|
44 | 2219 |
self tree:parseTree |
0 | 2220 |
]. |
2221 |
^ parseTree |
|
2222 |
! |
|
2223 |
||
19 | 2224 |
parseMethodBody |
2225 |
"parse a methods body (locals & statements). |
|
2226 |
No more tokens may follow. |
|
2227 |
Return a node-tree, or #Error |
|
2228 |
||
2229 |
methodBody ::= '<' st80Primitive '>' #EOF |
|
44 | 2230 |
| '<' st80Primitive '>' methodBodyVarSpec statementList #EOF |
19 | 2231 |
|
2232 |
" |
|
2233 |
|stats| |
|
2234 |
||
79 | 2235 |
stats := self parseMethodBodyOrEmpty. |
19 | 2236 |
(stats == #Error) ifFalse:[ |
44 | 2237 |
(tokenType ~~ #EOF) ifTrue:[ |
2238 |
"/ just for the nicer error message |
|
49 | 2239 |
(#(Self Nil True False Super Here) includes:tokenType) ifTrue:[ |
44 | 2240 |
self parseError:tokenName , ' unexpected (missing ''.'' before ' , tokenName , ' ?)' |
2241 |
position:tokenPosition to:(tokenPosition + tokenName size - 1) |
|
2242 |
] ifFalse:[ |
|
2243 |
self parseError:(tokenType printString , ' unexpected'). |
|
2244 |
]. |
|
2245 |
^#Error |
|
2246 |
] |
|
19 | 2247 |
]. |
2248 |
^ stats |
|
2249 |
! |
|
2250 |
||
79 | 2251 |
parseMethodBodyOrEmpty |
0 | 2252 |
"parse a methods body (locals & statements); |
19 | 2253 |
return a node-tree, nil or #Error. |
2254 |
empty (or comment only) input is accepted and returns nil. |
|
2255 |
||
2256 |
methodBodyOrNil ::= '<' st80Primitive '>' |
|
44 | 2257 |
| '<' st80Primitive '>' methodBodyVarSpec statementList |
2258 |
| <empty> |
|
19 | 2259 |
" |
0 | 2260 |
|
71 | 2261 |
|stats pos wmsg| |
0 | 2262 |
|
2263 |
((tokenType == #BinaryOperator) and:[tokenName = '<']) ifTrue:[ |
|
44 | 2264 |
"an ST-80 primitive - parsed but ignored" |
71 | 2265 |
pos := tokenPosition. |
44 | 2266 |
self nextToken. |
2267 |
primitiveNr := self parseST80Primitive. |
|
2268 |
(primitiveNr == #Error) ifTrue:[^ #Error]. |
|
60 | 2269 |
primitiveNr < 0 ifTrue:[ |
79 | 2270 |
wmsg := 'ST-80 directive ignored'. |
2271 |
primitiveNr := nil. |
|
60 | 2272 |
] ifFalse:[ |
78 | 2273 |
wmsg := 'ST-80 primitive may not work' |
71 | 2274 |
]. |
2275 |
self warning:wmsg position:pos |
|
0 | 2276 |
]. |
2277 |
||
2278 |
(self parseMethodBodyVarSpec == #Error) ifTrue:[^ #Error]. |
|
2279 |
||
2280 |
(tokenType ~~ #EOF) ifTrue:[ |
|
44 | 2281 |
stats := self statementList |
0 | 2282 |
]. |
2283 |
^ stats |
|
2284 |
! |
|
2285 |
||
19 | 2286 |
parseMethodBodyVarSpec |
2287 |
"parse a methods local variable specification. |
|
2288 |
Leave spec of locals in methodLocals as a side effect. |
|
2289 |
Return #Error or nil. |
|
0 | 2290 |
|
19 | 2291 |
methodBodyVarSpec ::= '|' { IDENTIFIER } '|' |
44 | 2292 |
| <empty> |
19 | 2293 |
" |
0 | 2294 |
|
53 | 2295 |
|var pos msg| |
0 | 2296 |
|
2297 |
(tokenType == $|) ifTrue:[ |
|
44 | 2298 |
"memorize position for declaration in correction" |
2299 |
localVarDefPosition := tokenPosition. |
|
2300 |
self nextToken. |
|
53 | 2301 |
pos := tokenPosition. |
44 | 2302 |
[tokenType == #Identifier] whileTrue:[ |
2303 |
var := Variable name:tokenName. |
|
2304 |
methodVars isNil ifTrue:[ |
|
2305 |
methodVars := Array with:var. |
|
2306 |
methodVarNames := Array with:tokenName |
|
2307 |
] ifFalse:[ |
|
53 | 2308 |
(methodVarNames includes:tokenName) ifTrue:[ |
2309 |
self parseError:'redefinition of ''' , tokenName , ''' in local variables' |
|
2310 |
position:tokenPosition to:tokenPosition + tokenName size -1. |
|
2311 |
] ifFalse:[ |
|
2312 |
methodVars := methodVars copyWith:var. |
|
2313 |
methodVarNames := methodVarNames copyWith:tokenName |
|
2314 |
] |
|
44 | 2315 |
]. |
139
65eaf1a009f5
warn if method local hides method arg
Claus Gittinger <cg@exept.de>
parents:
135
diff
changeset
|
2316 |
methodArgNames notNil ifTrue:[ |
65eaf1a009f5
warn if method local hides method arg
Claus Gittinger <cg@exept.de>
parents:
135
diff
changeset
|
2317 |
(methodArgNames includes:tokenName) ifTrue:[ |
65eaf1a009f5
warn if method local hides method arg
Claus Gittinger <cg@exept.de>
parents:
135
diff
changeset
|
2318 |
self warning:'local variable ''' , tokenName , ''' hides argument.' |
65eaf1a009f5
warn if method local hides method arg
Claus Gittinger <cg@exept.de>
parents:
135
diff
changeset
|
2319 |
position:tokenPosition |
65eaf1a009f5
warn if method local hides method arg
Claus Gittinger <cg@exept.de>
parents:
135
diff
changeset
|
2320 |
to:(tokenPosition + tokenName size - 1) |
65eaf1a009f5
warn if method local hides method arg
Claus Gittinger <cg@exept.de>
parents:
135
diff
changeset
|
2321 |
] |
65eaf1a009f5
warn if method local hides method arg
Claus Gittinger <cg@exept.de>
parents:
135
diff
changeset
|
2322 |
]. |
53 | 2323 |
self nextToken. |
2324 |
pos := tokenPosition |
|
44 | 2325 |
]. |
2326 |
(tokenType ~~ $|) ifTrue:[ |
|
53 | 2327 |
(#(True False Self Nil Super ThisContext) includes:tokenType) ifTrue:[ |
2328 |
msg := 'Reserved keyword in local var declaration' |
|
2329 |
] ifFalse:[ |
|
2330 |
msg := 'Identifier or | expected in local var declaration' |
|
2331 |
]. |
|
2332 |
self syntaxError:msg position:tokenPosition to:source position-1. |
|
44 | 2333 |
^ #Error |
2334 |
]. |
|
2335 |
self nextToken |
|
0 | 2336 |
]. |
2337 |
^ nil |
|
139
65eaf1a009f5
warn if method local hides method arg
Claus Gittinger <cg@exept.de>
parents:
135
diff
changeset
|
2338 |
|
65eaf1a009f5
warn if method local hides method arg
Claus Gittinger <cg@exept.de>
parents:
135
diff
changeset
|
2339 |
"Modified: 18.11.1995 / 16:32:51 / cg" |
0 | 2340 |
! |
2341 |
||
141 | 2342 |
parseMethodSpec |
2343 |
"parse a methods selector & arg specification; |
|
2344 |
Set selector and methodArgs in the receiver as a side effect. |
|
2345 |
Return the receiver or #Error. |
|
2346 |
||
2347 |
methodSpec ::= { KEYWORD IDENTIFIER } |
|
2348 |
| binaryOperator IDENTIFIER |
|
2349 |
| IDENTIFIER |
|
2350 |
" |
|
2351 |
||
2352 |
|var| |
|
2353 |
||
2354 |
(tokenType == #Keyword) ifTrue:[ |
|
2355 |
selector := ''. |
|
2356 |
[tokenType == #Keyword] whileTrue:[ |
|
2357 |
selector := selector , tokenName. |
|
2358 |
self nextToken. |
|
2359 |
(tokenType ~~ #Identifier) ifTrue:[^ #Error]. |
|
2360 |
var := Variable name:tokenName. |
|
2361 |
methodArgs isNil ifTrue:[ |
|
2362 |
methodArgs := Array with:var. |
|
2363 |
methodArgNames := Array with:tokenName |
|
2364 |
] ifFalse:[ |
|
2365 |
(methodArgNames includes:tokenName) ifTrue:[ |
|
2366 |
self syntaxError:'redefinition of ''' , tokenName , ''' in argument list.' |
|
2367 |
position:tokenPosition |
|
2368 |
to:(tokenPosition + tokenName size - 1) |
|
2369 |
]. |
|
2370 |
methodArgs := methodArgs copyWith:var. |
|
2371 |
methodArgNames := methodArgNames copyWith:tokenName |
|
2372 |
]. |
|
2373 |
self nextToken |
|
2374 |
]. |
|
2375 |
selector := selector asSymbol. |
|
2376 |
^ self |
|
2377 |
]. |
|
2378 |
(tokenType == #Identifier) ifTrue:[ |
|
2379 |
selector := tokenName asSymbol. |
|
2380 |
self nextToken. |
|
2381 |
^ self |
|
2382 |
]. |
|
2383 |
(tokenType == #BinaryOperator) ifTrue:[ |
|
2384 |
selector := tokenName asSymbol. |
|
2385 |
self nextToken. |
|
2386 |
(tokenType ~~ #Identifier) ifTrue:[^ #Error]. |
|
2387 |
var := Variable name:tokenName. |
|
2388 |
"/ methodArgs isNil ifTrue:[ |
|
2389 |
methodArgs := Array with:var. |
|
2390 |
methodArgNames := Array with:tokenName. |
|
2391 |
"/ ] ifFalse:[ |
|
2392 |
"/ methodArgs := methodArgs copyWith:var. |
|
2393 |
"/ methodArgNames := methodArgNames copyWith:tokenName |
|
2394 |
"/ ]. |
|
2395 |
self nextToken. |
|
2396 |
^ self |
|
2397 |
]. |
|
2398 |
^ #Error |
|
2399 |
! |
|
2400 |
||
7 | 2401 |
parseST80Primitive |
2402 |
"parse an ST-80 type primitive as '< primitive: nr >'; |
|
19 | 2403 |
return primitive number or #Error. |
97 | 2404 |
Also, ST-80 style resource specs are parsed; the result is |
2405 |
left (as side effect) in primitiveResource. |
|
2406 |
(maybe someone else knows what to do with it ...) |
|
2407 |
||
2408 |
Well, as we now have this mechanism, I'll use it to mark methods which |
|
2409 |
do keyboard processing ... <resource: keyboard ( keys )> |
|
2410 |
For faster finding of used keyboard accelerators. |
|
19 | 2411 |
|
2412 |
st80Primitive ::= 'primitive:' INTEGER |
|
97 | 2413 |
st80Primitive ::= 'resource:' SYMBOL - ignored; leave SYMBOL in primitiveResource |
2414 |
st80Primitive ::= 'resource:' SYMBOL (...) - ignored; leave (SYMBOL (...)) in primitiveResource |
|
19 | 2415 |
" |
0 | 2416 |
|
97 | 2417 |
|primNumber keys| |
0 | 2418 |
|
60 | 2419 |
(tokenType ~~ #Keyword) ifTrue:[ |
2420 |
self parseError:'bad primitive definition (keyword expected)'. |
|
44 | 2421 |
^ #Error |
0 | 2422 |
]. |
60 | 2423 |
(tokenName = 'primitive:') ifTrue:[ |
2424 |
self nextToken. |
|
2425 |
(tokenType == #Integer) ifFalse:[ |
|
2426 |
self parseError:'primitive number expected'. |
|
2427 |
^ #Error |
|
2428 |
]. |
|
2429 |
primNumber := tokenValue. |
|
97 | 2430 |
self nextToken. |
60 | 2431 |
] ifFalse:[ |
2432 |
(tokenName = 'resource:') ifTrue:[ |
|
2433 |
self nextToken. |
|
97 | 2434 |
(tokenType ~~ #Symbol) ifTrue:[ |
60 | 2435 |
self parseError:'symbol expected'. |
2436 |
^ #Error |
|
2437 |
]. |
|
2438 |
primNumber := -1. |
|
97 | 2439 |
primitiveResource := tokenValue. |
2440 |
||
2441 |
primitiveResource == #keyboard ifTrue:[ |
|
2442 |
self nextToken. |
|
2443 |
tokenType == $( ifTrue:[ |
|
2444 |
self nextToken. |
|
2445 |
keys := OrderedCollection new. |
|
2446 |
[tokenType == $) ] whileFalse:[ |
|
2447 |
keys add:tokenValue. |
|
2448 |
self nextToken. |
|
2449 |
]. |
|
2450 |
primitiveResource := Array with:primitiveResource |
|
2451 |
with:keys. |
|
2452 |
self nextToken. |
|
2453 |
] |
|
2454 |
] ifFalse:[ |
|
2455 |
self nextToken. |
|
2456 |
]. |
|
60 | 2457 |
] ifFalse:[ |
97 | 2458 |
self parseError:'unrecognized primitive'. |
60 | 2459 |
^ #Error |
2460 |
]. |
|
0 | 2461 |
]. |
97 | 2462 |
|
0 | 2463 |
((tokenType == #BinaryOperator) and:[tokenName = '>']) ifFalse:[ |
44 | 2464 |
self parseError:'bad primitive definition (> expected)'. |
2465 |
^ #Error |
|
0 | 2466 |
]. |
2467 |
self nextToken. |
|
2468 |
^ primNumber |
|
2469 |
! |
|
2470 |
||
2471 |
primary |
|
2472 |
"parse a primary-expression; return a node-tree, nil or #Error" |
|
2473 |
||
84 | 2474 |
|val var expr pos name t cls| |
0 | 2475 |
|
2476 |
pos := tokenPosition. |
|
44 | 2477 |
(tokenType == #Self) ifTrue:[ |
2478 |
self nextToken. |
|
120 | 2479 |
((tokenType == $_) or:[tokenType == #':=']) ifTrue:[ |
44 | 2480 |
self parseError:'assignment to self' position:pos to:tokenPosition. |
2481 |
^ #Error |
|
2482 |
]. |
|
2483 |
selfNode isNil ifTrue:[ |
|
2484 |
selfNode := SelfNode value:selfValue |
|
2485 |
]. |
|
2486 |
^ selfNode |
|
2487 |
]. |
|
0 | 2488 |
(tokenType == #Identifier) ifTrue:[ |
49 | 2489 |
" |
2490 |
must check for variable first, to be backward compatible |
|
2491 |
with other smalltalks. |
|
2492 |
" |
|
101 | 2493 |
tokenName = 'here' ifTrue:[ |
2494 |
self variableOrError == #Error ifTrue:[ |
|
49 | 2495 |
tokenType := #Here. |
2496 |
warnSTXHereExtensionUsed ifTrue:[ |
|
50 | 2497 |
self warning:'here-sends are a nonstandard feature of ST/X' |
2498 |
position:pos to:pos+3. |
|
2499 |
" |
|
2500 |
only warn once |
|
2501 |
" |
|
49 | 2502 |
warnSTXHereExtensionUsed := false |
2503 |
] |
|
2504 |
] |
|
2505 |
] |
|
2506 |
]. |
|
2507 |
||
2508 |
(tokenType == #Identifier) ifTrue:[ |
|
101 | 2509 |
name := tokenName. |
2510 |
||
44 | 2511 |
var := self variable. |
2512 |
(var == #Error) ifTrue:[ |
|
2513 |
errorFlag := true |
|
2514 |
]. |
|
2515 |
self nextToken. |
|
120 | 2516 |
((tokenType == $_) or:[tokenType == #':=']) ifFalse:[ |
44 | 2517 |
^ var |
2518 |
]. |
|
101 | 2519 |
"/ careful: it could already be an implicit self send |
2520 |
ImplicitSelfSends ifTrue:[ |
|
2521 |
var isMessage ifTrue:[ |
|
2522 |
self nextToken. |
|
2523 |
expr := self expression. |
|
2524 |
(errorFlag or:[expr == #Error]) ifTrue:[^ #Error]. |
|
2525 |
selfNode isNil ifTrue:[ |
|
2526 |
selfNode := SelfNode value:selfValue |
|
2527 |
]. |
|
2528 |
^ MessageNode receiver:selfNode selector:('implicit_' , name , ':') asSymbol arg:expr. |
|
2529 |
]. |
|
2530 |
]. |
|
2531 |
||
44 | 2532 |
(var ~~ #Error) ifTrue:[ |
2533 |
t := var type. |
|
120 | 2534 |
(t ~~ #MethodVar) ifTrue:[ |
2535 |
(t == #MethodArg) ifTrue:[ |
|
2536 |
self parseError:'assignment to method argument' position:pos to:tokenPosition. |
|
44 | 2537 |
errorFlag := true |
2538 |
] ifFalse:[ |
|
120 | 2539 |
(t == #BlockArg) ifTrue:[ |
2540 |
self parseError:'assignment to block argument' position:pos to:tokenPosition. |
|
2541 |
errorFlag := true |
|
44 | 2542 |
] ifFalse:[ |
120 | 2543 |
(t == #InstanceVariable) ifTrue:[ |
2544 |
name := PrevInstVarNames at:(var index). |
|
84 | 2545 |
parseForCode ifFalse:[ |
120 | 2546 |
modifiedInstVars isNil ifTrue:[ |
2547 |
modifiedInstVars := Set new |
|
84 | 2548 |
]. |
120 | 2549 |
modifiedInstVars add:name |
44 | 2550 |
] |
2551 |
] ifFalse:[ |
|
120 | 2552 |
(t == #ClassVariable) ifTrue:[ |
2553 |
name := var name. |
|
2554 |
name := name copyFrom:((name indexOf:$:) + 1). |
|
2555 |
parseForCode ifFalse:[ |
|
2556 |
modifiedClassVars isNil ifTrue:[ |
|
2557 |
modifiedClassVars := Set new |
|
2558 |
]. |
|
2559 |
modifiedClassVars add:name |
|
2560 |
] |
|
2561 |
] ifFalse:[ |
|
2562 |
(t == #GlobalVariable) ifTrue:[ |
|
2563 |
(cls := Smalltalk classNamed:var name) notNil ifTrue:[ |
|
2564 |
cls name = var name ifTrue:[ |
|
2565 |
self warning:'assignment to global which contains class' position:pos to:tokenPosition. |
|
2566 |
] |
|
2567 |
]. |
|
2568 |
parseForCode ifFalse:[ |
|
2569 |
modifiedGlobals isNil ifTrue:[ |
|
2570 |
modifiedGlobals := Set new |
|
2571 |
]. |
|
2572 |
modifiedGlobals add:var name |
|
84 | 2573 |
] |
44 | 2574 |
] |
2575 |
] |
|
2576 |
] |
|
2577 |
] |
|
2578 |
] |
|
120 | 2579 |
]. |
44 | 2580 |
]. |
0 | 2581 |
|
44 | 2582 |
self nextToken. |
2583 |
expr := self expression. |
|
2584 |
(errorFlag or:[expr == #Error]) ifTrue:[^ #Error]. |
|
2585 |
^ AssignmentNode variable:var expression:expr |
|
0 | 2586 |
]. |
49 | 2587 |
|
44 | 2588 |
((tokenType == #Integer) |
120 | 2589 |
or:[(tokenType == #String) |
44 | 2590 |
or:[(tokenType == #Character) |
2591 |
or:[(tokenType == #Float) |
|
2592 |
or:[(tokenType == #Symbol)]]]]) ifTrue:[ |
|
2593 |
val := ConstantNode type:tokenType value:tokenValue. |
|
2594 |
self nextToken. |
|
120 | 2595 |
((tokenType == $_) or:[tokenType == #':=']) ifTrue:[ |
44 | 2596 |
self parseError:'assignment to a constant' position:pos to:tokenPosition. |
2597 |
^ #Error |
|
2598 |
]. |
|
2599 |
^ val |
|
0 | 2600 |
]. |
2601 |
(tokenType == #Nil) ifTrue:[ |
|
44 | 2602 |
self nextToken. |
120 | 2603 |
((tokenType == $_) or:[tokenType == #':=']) ifTrue:[ |
44 | 2604 |
self parseError:'assignment to nil' position:pos to:tokenPosition. |
2605 |
^ #Error |
|
2606 |
]. |
|
2607 |
^ ConstantNode type:#Nil value:nil |
|
0 | 2608 |
]. |
2609 |
(tokenType == #True) ifTrue:[ |
|
44 | 2610 |
self nextToken. |
120 | 2611 |
((tokenType == $_) or:[tokenType == #':=']) ifTrue:[ |
44 | 2612 |
self parseError:'assignment to true' position:pos to:tokenPosition. |
2613 |
^ #Error |
|
2614 |
]. |
|
2615 |
^ ConstantNode type:#True value:true |
|
0 | 2616 |
]. |
2617 |
(tokenType == #False) ifTrue:[ |
|
44 | 2618 |
self nextToken. |
120 | 2619 |
((tokenType == $_) or:[tokenType == #':=']) ifTrue:[ |
44 | 2620 |
self parseError:'assignment to false' position:pos to:tokenPosition. |
2621 |
^ #Error |
|
2622 |
]. |
|
2623 |
^ ConstantNode type:#False value:false |
|
0 | 2624 |
]. |
2625 |
(tokenType == #Super) ifTrue:[ |
|
44 | 2626 |
usesSuper := true. |
2627 |
self nextToken. |
|
120 | 2628 |
((tokenType == $_) or:[tokenType == #':=']) ifTrue:[ |
44 | 2629 |
self parseError:'assignment to super' position:pos to:tokenPosition. |
2630 |
^ #Error |
|
2631 |
]. |
|
2632 |
(classToCompileFor isNil or:[classToCompileFor superclass isNil]) ifTrue:[ |
|
49 | 2633 |
self warning:'superclass is (currently ?) nil' position:pos to:(pos + 4). |
44 | 2634 |
]. |
2635 |
superNode isNil ifTrue:[ |
|
2636 |
superNode := SuperNode value:selfValue inClass:classToCompileFor |
|
2637 |
]. |
|
2638 |
^ superNode |
|
0 | 2639 |
]. |
49 | 2640 |
(tokenType == #Here) ifTrue:[ |
2641 |
self nextToken. |
|
120 | 2642 |
((tokenType == $_) or:[tokenType == #':=']) ifTrue:[ |
49 | 2643 |
self parseError:'assignment to here' position:pos to:tokenPosition. |
2644 |
^ #Error |
|
2645 |
]. |
|
2646 |
classToCompileFor isNil ifTrue:[ |
|
2647 |
self warning:'in which class are you ?' position:pos to:(pos + 3). |
|
2648 |
]. |
|
2649 |
^ SuperNode value:selfValue inClass:classToCompileFor here:true |
|
2650 |
]. |
|
0 | 2651 |
(tokenType == #ThisContext) ifTrue:[ |
44 | 2652 |
self nextToken. |
120 | 2653 |
((tokenType == $_) or:[tokenType == #':=']) ifTrue:[ |
44 | 2654 |
self parseError:'assignment to thisContext' position:pos to:tokenPosition. |
2655 |
^ #Error |
|
2656 |
]. |
|
2657 |
^ VariableNode type:#ThisContext |
|
0 | 2658 |
]. |
2659 |
(tokenType == #HashLeftParen) ifTrue:[ |
|
44 | 2660 |
self nextToken. |
2661 |
val := self array. |
|
2662 |
self nextToken. |
|
120 | 2663 |
((tokenType == $_) or:[tokenType == #':=']) ifTrue:[ |
44 | 2664 |
self parseError:'assignment to a constant' position:pos to:tokenPosition. |
2665 |
^ #Error |
|
2666 |
]. |
|
2667 |
^ ConstantNode type:#Array value:val |
|
0 | 2668 |
]. |
2669 |
(tokenType == #HashLeftBrack) ifTrue:[ |
|
44 | 2670 |
self nextToken. |
2671 |
val := self byteArray. |
|
2672 |
self nextToken. |
|
120 | 2673 |
((tokenType == $_) or:[tokenType == #':=']) ifTrue:[ |
44 | 2674 |
self parseError:'assignment to a constant' position:pos to:tokenPosition. |
2675 |
^ #Error |
|
2676 |
]. |
|
2677 |
^ ConstantNode type:#Array value:val |
|
0 | 2678 |
]. |
2679 |
(tokenType == $() ifTrue:[ |
|
44 | 2680 |
self nextToken. |
2681 |
val := self expression. |
|
2682 |
(val == #Error) ifTrue:[^ #Error]. |
|
2683 |
(tokenType ~~ $) ) ifTrue:[ |
|
2684 |
tokenType isCharacter ifTrue:[ |
|
2685 |
self syntaxError:'missing '')'' (i.e. ''' , tokenType asString , ''' unexpected)' withCRs position:pos to:tokenPosition. |
|
2686 |
] ifFalse:[ |
|
2687 |
self syntaxError:'missing '')''' position:pos to:tokenPosition. |
|
2688 |
]. |
|
2689 |
^ #Error |
|
2690 |
]. |
|
2691 |
self nextToken. |
|
120 | 2692 |
((tokenType == $_) or:[tokenType == #':=']) ifTrue:[ |
44 | 2693 |
self parseError:'invalid assignment' position:pos to:tokenPosition. |
2694 |
^ #Error |
|
2695 |
]. |
|
2696 |
val parenthized:true. |
|
2697 |
^ val |
|
0 | 2698 |
]. |
2699 |
(tokenType == $[ ) ifTrue:[ |
|
44 | 2700 |
val := self block. |
2701 |
self nextToken. |
|
120 | 2702 |
((tokenType == $_) or:[tokenType == #':=']) ifTrue:[ |
44 | 2703 |
self parseError:'invalid assignment' position:pos to:tokenPosition. |
2704 |
^ #Error |
|
2705 |
]. |
|
2706 |
^ val |
|
0 | 2707 |
]. |
19 | 2708 |
|
0 | 2709 |
(tokenType == #Error) ifTrue:[^ #Error]. |
44 | 2710 |
tokenType isCharacter ifTrue:[ |
2711 |
self syntaxError:('error in primary; ' |
|
2712 |
, tokenType printString , |
|
2713 |
' unexpected') position:tokenPosition to:tokenPosition |
|
0 | 2714 |
] ifFalse:[ |
44 | 2715 |
(#(BinaryOperator Keyword) includes:tokenType) ifTrue:[ |
2716 |
self syntaxError:('error in primary; ' |
|
2717 |
, tokenType printString , '(' , tokenName , ') ' , |
|
2718 |
' unexpected') |
|
2719 |
] ifFalse:[ |
|
2720 |
self syntaxError:('error in primary; ' |
|
2721 |
, tokenType printString , |
|
2722 |
' unexpected') |
|
2723 |
] |
|
0 | 2724 |
]. |
2725 |
^ #Error |
|
120 | 2726 |
|
2727 |
"Created: 13.9.1995 / 12:50:50 / claus" |
|
0 | 2728 |
! |
2729 |
||
141 | 2730 |
statement |
2731 |
"parse a statement; return a node-tree or #Error. |
|
2732 |
||
2733 |
statement ::= '^' expression |
|
2734 |
| PRIMITIVECODE |
|
2735 |
| expression |
|
2736 |
" |
|
2737 |
||
2738 |
|expr node| |
|
2739 |
||
2740 |
(tokenType == $^) ifTrue:[ |
|
2741 |
self nextToken. |
|
2742 |
expr := self expression. |
|
2743 |
(expr == #Error) ifTrue:[^ #Error]. |
|
2744 |
node := ReturnNode expression:expr. |
|
2745 |
node home:self blockHome:currentBlock. |
|
2746 |
^ node |
|
2747 |
]. |
|
2748 |
(tokenType == #Primitive) ifTrue:[ |
|
2749 |
self nextToken. |
|
2750 |
node := PrimitiveNode code:tokenValue. |
|
2751 |
node isOptional ifFalse:[ |
|
2752 |
hasNonOptionalPrimitiveCode := true |
|
2753 |
]. |
|
2754 |
hasPrimitiveCode := true. |
|
2755 |
^ node |
|
2756 |
]. |
|
2757 |
(tokenType == #EOF) ifTrue:[ |
|
2758 |
self syntaxError:'period after last statement'. |
|
2759 |
^ #Error |
|
2760 |
]. |
|
2761 |
expr := self expression. |
|
2762 |
(expr == #Error) ifTrue:[^ #Error]. |
|
2763 |
" |
|
2764 |
classToCompileFor notNil ifTrue:[ |
|
2765 |
currentBlock isNil ifTrue:[ |
|
2766 |
expr isPrimary ifTrue:[ |
|
2767 |
self warning:'useless computation - missing ^ ?' |
|
2768 |
] |
|
2769 |
] |
|
2770 |
]. |
|
2771 |
" |
|
2772 |
^ StatementNode expression:expr |
|
2773 |
! |
|
2774 |
||
2775 |
statementList |
|
2776 |
"parse a statementlist; return a node-tree, nil or #Error. |
|
2777 |
Statements must be separated by periods. |
|
2778 |
||
2779 |
statementList ::= <statement> |
|
2780 |
| <statementList> . <statement> |
|
2781 |
" |
|
2782 |
||
2783 |
|thisStatement prevStatement firstStatement correctIt periodPos| |
|
2784 |
||
2785 |
thisStatement := self statement. |
|
2786 |
(thisStatement == #Error) ifTrue:[^ #Error]. |
|
2787 |
firstStatement := thisStatement. |
|
2788 |
[tokenType == $.] whileTrue:[ |
|
2789 |
periodPos := tokenPosition. |
|
2790 |
self nextToken. |
|
2791 |
(tokenType == $]) ifTrue:[ |
|
2792 |
currentBlock isNil ifTrue:[ |
|
2793 |
self parseError:'block nesting error'. |
|
2794 |
errorFlag := true |
|
2795 |
" |
|
2796 |
*** I had a warning here (since it was not defined |
|
2797 |
*** in the blue-book; but PD-code contains a lot of |
|
2798 |
*** code with periods at the end so that the warnings |
|
2799 |
*** became annoying |
|
2800 |
||
2801 |
] ifFalse:[ |
|
2802 |
self warning:'period after last statement' position:periodPos |
|
2803 |
" |
|
2804 |
]. |
|
2805 |
^ firstStatement |
|
2806 |
]. |
|
2807 |
(tokenType == #EOF) ifTrue:[ |
|
2808 |
currentBlock notNil ifTrue:[ |
|
2809 |
self parseError:'block nesting error (expected '']'')'. |
|
2810 |
errorFlag := true |
|
2811 |
" |
|
2812 |
*** I had a warning here (since it was not defined |
|
2813 |
*** in the blue-book; but PD-code contains a lot of |
|
2814 |
*** code with periods at the end so that the warnings |
|
2815 |
*** became annoying |
|
2816 |
||
2817 |
] ifFalse:[ |
|
2818 |
self warning:'period after last statement' position:periodPos |
|
2819 |
" |
|
2820 |
]. |
|
2821 |
^ firstStatement |
|
2822 |
]. |
|
2823 |
||
2824 |
prevStatement := thisStatement. |
|
2825 |
prevStatement isReturnNode ifTrue:[ |
|
2826 |
self warning:'statements after return' position:tokenPosition |
|
2827 |
]. |
|
2828 |
" |
|
2829 |
periodPos := tokenPosition. |
|
2830 |
self nextToken. |
|
2831 |
" |
|
2832 |
||
2833 |
((tokenType == $]) or:[tokenType == #EOF]) ifTrue:[ |
|
2834 |
(currentBlock isNil and:[tokenType == $]]) ifTrue:[ |
|
2835 |
self parseError:'block nesting error'. |
|
2836 |
errorFlag := true |
|
2837 |
] ifFalse:[ |
|
2838 |
correctIt := self correctableError:'period after last statement in block' |
|
2839 |
position:periodPos to:(periodPos + 1). |
|
2840 |
correctIt ifTrue:[ |
|
2841 |
(self correctByDeleting == #Error) ifTrue:[ |
|
2842 |
errorFlag := true |
|
2843 |
] |
|
2844 |
] |
|
2845 |
]. |
|
2846 |
^ firstStatement |
|
2847 |
]. |
|
2848 |
thisStatement := self statement. |
|
2849 |
(thisStatement == #Error) ifTrue:[^ #Error]. |
|
2850 |
prevStatement nextStatement:thisStatement |
|
2851 |
]. |
|
2852 |
^ firstStatement |
|
2853 |
! |
|
2854 |
||
2855 |
unaryExpression |
|
2856 |
"parse a unary-expression; return a node-tree, nil or #Error" |
|
2857 |
||
2858 |
|receiver sel pos pos2 try| |
|
2859 |
||
2860 |
receiver := self primary. |
|
2861 |
(receiver == #Error) ifTrue:[^ #Error]. |
|
2862 |
[tokenType == #Identifier] whileTrue:[ |
|
2863 |
pos := tokenPosition. |
|
2864 |
pos2 := pos + tokenName size - 1. |
|
2865 |
sel := self selectorCheck:tokenName for:receiver position:pos to:pos2. |
|
2866 |
try := UnaryNode receiver:receiver selector:sel. |
|
2867 |
(try isMemberOf:String) ifTrue:[ |
|
2868 |
self parseError:try position:pos to:pos2. |
|
2869 |
errorFlag := false. "ok, user wants it - so he'll get it" |
|
2870 |
receiver := UnaryNode receiver:receiver selector:sel fold:false. |
|
2871 |
] ifFalse:[ |
|
2872 |
receiver := try |
|
2873 |
]. |
|
2874 |
receiver lineNumber:tokenLineNr. |
|
2875 |
parseForCode ifFalse:[self rememberSelectorUsed:sel]. |
|
2876 |
self nextToken. |
|
2877 |
]. |
|
2878 |
^ receiver |
|
2879 |
! |
|
2880 |
||
2881 |
variable |
|
2882 |
"parse a variable; if undefined, notify error and correct if user wants to" |
|
2883 |
||
2884 |
|v| |
|
2885 |
||
2886 |
v := self variableOrError. |
|
2887 |
(v == #Error) ifFalse:[^ v]. |
|
2888 |
v := self correctVariable. |
|
2889 |
(v == #Error) ifFalse:[^ v]. |
|
2890 |
parseForCode ifFalse:[ |
|
2891 |
self rememberGlobalUsed:tokenName |
|
2892 |
] ifTrue:[ |
|
2893 |
tokenName first isLowercase ifTrue:[ |
|
2894 |
ImplicitSelfSends ifTrue:[ |
|
2895 |
selfNode isNil ifTrue:[ |
|
2896 |
selfNode := SelfNode value:selfValue |
|
2897 |
]. |
|
2898 |
^ UnaryNode receiver:selfNode selector:('implicit_' , tokenName) asSymbol. |
|
2899 |
]. |
|
2900 |
^ #Error |
|
2901 |
] |
|
2902 |
]. |
|
2903 |
^ VariableNode type:#GlobalVariable name:tokenName asSymbol |
|
2904 |
! |
|
2905 |
||
0 | 2906 |
variableOrError |
2907 |
"parse a variable; return a node-tree, nil or #Error" |
|
2908 |
||
21 | 2909 |
^ self variableOrError:tokenName |
2910 |
! |
|
2911 |
||
2912 |
variableOrError:varName |
|
2913 |
"parse a variable; return a node-tree, nil or #Error" |
|
2914 |
||
19 | 2915 |
|var instIndex aClass searchBlock args vars |
53 | 2916 |
tokenSymbol| |
0 | 2917 |
|
2918 |
"is it a block-arg or block-var ?" |
|
2919 |
searchBlock := currentBlock. |
|
2920 |
[searchBlock notNil] whileTrue:[ |
|
47 | 2921 |
vars := searchBlock variables. |
2922 |
vars notNil ifTrue:[ |
|
2923 |
instIndex := vars findFirst:[:aBlockVar | aBlockVar name = varName]. |
|
2924 |
instIndex ~~ 0 ifTrue:[ |
|
2925 |
^ VariableNode type:#BlockVariable |
|
2926 |
name:varName |
|
2927 |
token:(vars at:instIndex) |
|
2928 |
index:instIndex |
|
2929 |
block:searchBlock |
|
2930 |
]. |
|
2931 |
]. |
|
2932 |
||
44 | 2933 |
args := searchBlock arguments. |
2934 |
args notNil ifTrue:[ |
|
2935 |
instIndex := args findFirst:[:aBlockArg | aBlockArg name = varName]. |
|
2936 |
instIndex ~~ 0 ifTrue:[ |
|
2937 |
^ VariableNode type:#BlockArg |
|
2938 |
name:varName |
|
2939 |
token:(args at:instIndex) |
|
2940 |
index:instIndex |
|
2941 |
block:searchBlock |
|
2942 |
]. |
|
19 | 2943 |
|
44 | 2944 |
]. |
0 | 2945 |
|
44 | 2946 |
searchBlock := searchBlock home |
0 | 2947 |
]. |
2948 |
||
2949 |
"is it a method-variable ?" |
|
2950 |
methodVars notNil ifTrue:[ |
|
44 | 2951 |
instIndex := methodVarNames indexOf:varName. |
2952 |
instIndex ~~ 0 ifTrue:[ |
|
2953 |
var := methodVars at:instIndex. |
|
2954 |
var used:true. |
|
2955 |
^ VariableNode type:#MethodVariable |
|
2956 |
name:varName |
|
2957 |
token:var |
|
2958 |
index:instIndex |
|
2959 |
] |
|
0 | 2960 |
]. |
2961 |
||
2962 |
"is it a method-argument ?" |
|
2963 |
methodArgs notNil ifTrue:[ |
|
44 | 2964 |
instIndex := methodArgNames indexOf:varName. |
2965 |
instIndex ~~ 0 ifTrue:[ |
|
2966 |
^ VariableNode type:#MethodArg |
|
2967 |
name:varName |
|
2968 |
token:(methodArgs at:instIndex) |
|
2969 |
index:instIndex |
|
2970 |
] |
|
0 | 2971 |
]. |
2972 |
||
2973 |
classToCompileFor notNil ifTrue:[ |
|
80 | 2974 |
"is it an instance-variable ?" |
2975 |
||
98 | 2976 |
instIndex := (self instVarNames) lastIndexOf:varName. |
44 | 2977 |
instIndex ~~ 0 ifTrue:[ |
84 | 2978 |
parseForCode ifFalse:[self rememberInstVarUsed:varName]. |
44 | 2979 |
^ VariableNode type:#InstanceVariable |
2980 |
name:varName |
|
2981 |
index:instIndex |
|
2982 |
selfValue:selfValue |
|
80 | 2983 |
]. |
2984 |
||
2985 |
"is it a class-instance-variable ?" |
|
2986 |
||
98 | 2987 |
instIndex := (self classInstVarNames) lastIndexOf:varName. |
44 | 2988 |
instIndex ~~ 0 ifTrue:[ |
2989 |
aClass := self inWhichClassIsClassInstVar:varName. |
|
2990 |
aClass notNil ifTrue:[ |
|
84 | 2991 |
parseForCode ifFalse:[self rememberClassVarUsed:varName]. |
44 | 2992 |
^ VariableNode type:#ClassInstanceVariable |
2993 |
name:varName |
|
2994 |
index:instIndex |
|
2995 |
selfClass:aClass |
|
2996 |
] |
|
80 | 2997 |
]. |
2998 |
||
2999 |
"is it a class-variable ?" |
|
3000 |
||
98 | 3001 |
instIndex := (self classVarNames) lastIndexOf:varName. |
44 | 3002 |
instIndex ~~ 0 ifTrue:[ |
3003 |
aClass := self inWhichClassIsClassVar:varName. |
|
3004 |
aClass notNil ifTrue:[ |
|
84 | 3005 |
parseForCode ifFalse:[self rememberClassVarUsed:varName]. |
98 | 3006 |
^ VariableNode type:#ClassVariable class:aClass name:varName |
44 | 3007 |
] |
3008 |
] |
|
0 | 3009 |
]. |
3010 |
||
3011 |
"is it a global-variable ?" |
|
63 | 3012 |
tokenSymbol := varName asSymbolIfInterned. |
3013 |
tokenSymbol notNil ifTrue:[ |
|
47 | 3014 |
(Smalltalk includesKey:tokenSymbol) ifTrue:[ |
84 | 3015 |
parseForCode ifFalse:[self rememberGlobalUsed:varName]. |
47 | 3016 |
^ VariableNode type:#GlobalVariable name:tokenSymbol |
3017 |
] |
|
0 | 3018 |
]. |
3019 |
^ #Error |
|
141 | 3020 |
! ! |
3021 |
||
3022 |
!Parser methodsFor:'queries'! |
|
3023 |
||
3024 |
classInstVarNames |
|
3025 |
"caching allInstVarNames for next compilation saves time ..." |
|
3026 |
||
3027 |
PrevClassInstVarNames isNil ifTrue:[ |
|
3028 |
PrevClassInstVarNames := classToCompileFor class allInstVarNames |
|
98 | 3029 |
]. |
141 | 3030 |
^ PrevClassInstVarNames |
0 | 3031 |
! |
3032 |
||
141 | 3033 |
classVarNames |
3034 |
"caching allClassVarNames for next compilation saves time ..." |
|
3035 |
||
3036 |
|aClass className| |
|
3037 |
||
3038 |
PrevClassVarNames isNil ifTrue:[ |
|
3039 |
aClass := classToCompileFor. |
|
3040 |
classToCompileFor isMeta ifTrue:[ |
|
3041 |
className := aClass name. |
|
3042 |
className := className copyWithoutLast:5. |
|
3043 |
aClass := Smalltalk at:(className asSymbol). |
|
3044 |
aClass isNil ifTrue:[ |
|
3045 |
aClass := classToCompileFor |
|
3046 |
] |
|
3047 |
]. |
|
3048 |
PrevClassVarNames := aClass allClassVarNames |
|
0 | 3049 |
]. |
141 | 3050 |
^ PrevClassVarNames |
0 | 3051 |
! |
3052 |
||
141 | 3053 |
hasNonOptionalPrimitiveCode |
3054 |
"return true if there was any ST/X style primitive code (valid after parsing)" |
|
3055 |
||
3056 |
^ hasNonOptionalPrimitiveCode |
|
0 | 3057 |
! |
3058 |
||
141 | 3059 |
hasPrimitiveCode |
3060 |
"return true if there was any ST/X style primitive code (valid after parsing)" |
|
3061 |
||
3062 |
^ hasPrimitiveCode |
|
98 | 3063 |
! |
3064 |
||
141 | 3065 |
instVarNames |
3066 |
"caching allInstVarNames for next compilation saves time ..." |
|
3067 |
||
3068 |
(PrevInstVarNames isNil or:[PrevClass ~~ classToCompileFor]) ifTrue:[ |
|
3069 |
PrevClass notNil ifTrue:[ |
|
3070 |
PrevClass removeDependent:Parser |
|
44 | 3071 |
]. |
141 | 3072 |
PrevClass := classToCompileFor. |
3073 |
PrevInstVarNames := classToCompileFor allInstVarNames. |
|
3074 |
PrevClassInstVarNames := nil. |
|
3075 |
PrevClassVarNames := nil. |
|
3076 |
PrevClass addDependent:Parser |
|
0 | 3077 |
]. |
141 | 3078 |
|
3079 |
^ PrevInstVarNames |
|
3080 |
! |
|
3081 |
||
3082 |
methodArgs |
|
3083 |
"return an array with methodarg names (valid after parsing spec)" |
|
3084 |
||
3085 |
^ methodArgNames |
|
3086 |
! |
|
3087 |
||
3088 |
methodVars |
|
3089 |
"return a collection with method variablenames (valid after parsing)" |
|
3090 |
||
3091 |
^ methodVarNames |
|
23 | 3092 |
! |
3093 |
||
141 | 3094 |
modifiedClassVars |
3095 |
"return a collection with classvariablenames modified by method (valid after parsing)" |
|
3096 |
||
3097 |
^ modifiedClassVars |
|
3098 |
! |
|
3099 |
||
3100 |
modifiedGlobals |
|
3101 |
"return a collection with globalnames modified by method (valid after parsing)" |
|
3102 |
||
3103 |
^ modifiedGlobals |
|
3104 |
! |
|
3105 |
||
3106 |
modifiedInstVars |
|
3107 |
"return a collection with instvariablenames modified by method (valid after parsing)" |
|
3108 |
||
3109 |
^ modifiedInstVars |
|
0 | 3110 |
! |
3111 |
||
141 | 3112 |
numberOfMethodArgs |
3113 |
"return the number of methodargs (valid after parsing spec)" |
|
3114 |
||
3115 |
^ methodArgs size |
|
3116 |
! |
|
3117 |
||
3118 |
numberOfMethodVars |
|
3119 |
"return the number of method variables (valid after parsing)" |
|
3120 |
||
3121 |
^ methodVars size |
|
3122 |
! |
|
3123 |
||
3124 |
selector |
|
3125 |
"return the selector (valid after parsing spec)" |
|
3126 |
||
3127 |
^ selector |
|
0 | 3128 |
! |
3129 |
||
141 | 3130 |
usedClassVars |
3131 |
"return a collection with classvariablenames refd by method (valid after parsing)" |
|
3132 |
||
3133 |
^ usedClassVars |
|
3134 |
! |
|
3135 |
||
3136 |
usedGlobals |
|
3137 |
"return a collection with globalnames refd by method (valid after parsing)" |
|
3138 |
||
3139 |
^ usedGlobals |
|
3140 |
! |
|
3141 |
||
3142 |
usedInstVars |
|
3143 |
"return a collection with instvariablenames refd by method (valid after parsing)" |
|
3144 |
||
3145 |
^ usedInstVars |
|
3146 |
! |
|
3147 |
||
3148 |
usedVars |
|
3149 |
"return a collection with variablenames refd by method (valid after parsing)" |
|
3150 |
||
3151 |
^ usedVars |
|
0 | 3152 |
! |
3153 |
||
141 | 3154 |
usesSuper |
3155 |
"return true if the parsed method uses super (valid after parsing)" |
|
3156 |
||
3157 |
^ usesSuper |
|
3158 |
! ! |
|
3159 |
||
3160 |
!Parser methodsFor:'setup'! |
|
3161 |
||
3162 |
initialize |
|
3163 |
super initialize. |
|
3164 |
||
3165 |
hasPrimitiveCode := hasNonOptionalPrimitiveCode := false. |
|
3166 |
warnSTXHereExtensionUsed := WarnSTXSpecials. |
|
3167 |
usesSuper := false. |
|
3168 |
parseForCode := false. |
|
3169 |
! |
|
3170 |
||
3171 |
parseForCode |
|
3172 |
"turns off certain statistics (keeping referenced variables, modified vars etc.) |
|
3173 |
Use this when parsing for compilation or evaluation" |
|
3174 |
||
3175 |
parseForCode := true |
|
0 | 3176 |
! |
3177 |
||
141 | 3178 |
setClassToCompileFor:aClass |
3179 |
"set the class to be used for parsing/evaluating" |
|
3180 |
||
3181 |
classToCompileFor := aClass. |
|
3182 |
(classToCompileFor ~~ PrevClass) ifTrue:[ |
|
3183 |
PrevClass notNil ifTrue:[ |
|
3184 |
Parser update:PrevClass |
|
3185 |
] |
|
3186 |
] |
|
3187 |
! |
|
3188 |
||
3189 |
setContext:aContext |
|
3190 |
"set the context used while evaluating" |
|
3191 |
||
3192 |
contextToEvaluateIn := aContext |
|
3193 |
! |
|
3194 |
||
3195 |
setSelf:anObject |
|
3196 |
"set the value to be used for self while evaluating" |
|
3197 |
||
3198 |
selfValue := anObject. |
|
3199 |
classToCompileFor := anObject class. |
|
3200 |
(classToCompileFor ~~ PrevClass) ifTrue:[ |
|
3201 |
PrevClass notNil ifTrue:[ |
|
3202 |
Parser update:PrevClass |
|
3203 |
] |
|
3204 |
] |
|
0 | 3205 |
! ! |
3206 |
||
84 | 3207 |
!Parser methodsFor:'statistic'! |
3208 |
||
141 | 3209 |
rememberClassVarUsed:name |
3210 |
usedClassVars isNil ifTrue:[ |
|
3211 |
usedClassVars := Set new |
|
3212 |
]. |
|
3213 |
usedClassVars add:name. |
|
3214 |
self rememberVariableUsed:name |
|
3215 |
! |
|
3216 |
||
3217 |
rememberGlobalUsed:name |
|
3218 |
usedGlobals isNil ifTrue:[ |
|
3219 |
usedGlobals := Set new |
|
3220 |
]. |
|
3221 |
usedGlobals add:name. |
|
3222 |
self rememberVariableUsed:name |
|
3223 |
! |
|
3224 |
||
3225 |
rememberInstVarUsed:name |
|
3226 |
usedInstVars isNil ifTrue:[ |
|
3227 |
usedInstVars := Set new |
|
3228 |
]. |
|
3229 |
usedInstVars add:name. |
|
3230 |
self rememberVariableUsed:name |
|
3231 |
! |
|
3232 |
||
84 | 3233 |
rememberSelectorUsed:sel |
3234 |
usedMessages isNil ifTrue:[ |
|
3235 |
usedMessages := IdentitySet new. |
|
3236 |
]. |
|
3237 |
usedMessages add:sel |
|
3238 |
! |
|
3239 |
||
3240 |
rememberVariableUsed:name |
|
3241 |
usedVars isNil ifTrue:[ |
|
3242 |
usedVars := Set new |
|
3243 |
]. |
|
3244 |
usedVars add:name |
|
3245 |
! ! |
|
3246 |
||
141 | 3247 |
Parser initialize! |