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