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