2
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
49
|
3 |
All Rights Reserved
|
2
|
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 |
Parser subclass:#Explainer
|
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'System-Compiler'
|
|
18 |
!
|
|
19 |
|
|
20 |
Explainer comment:'
|
|
21 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
49
|
22 |
All Rights Reserved
|
24
|
23 |
|
102
|
24 |
$Header: /cvs/stx/stx/libcomp/Explainer.st,v 1.12 1995-08-10 18:11:23 claus Exp $
|
2
|
25 |
'!
|
|
26 |
|
|
27 |
!Explainer class methodsFor:'documentation'!
|
|
28 |
|
20
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
49
|
32 |
All Rights Reserved
|
20
|
33 |
|
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
|
42 |
|
|
43 |
version
|
|
44 |
"
|
102
|
45 |
$Header: /cvs/stx/stx/libcomp/Explainer.st,v 1.12 1995-08-10 18:11:23 claus Exp $
|
|
46 |
$Revision: 1.12 $
|
20
|
47 |
"
|
|
48 |
!
|
|
49 |
|
2
|
50 |
documentation
|
|
51 |
"
|
20
|
52 |
a very simple explainer - much more should be added ...
|
|
53 |
This one is involved, when the 'explain' function is used in a
|
84
|
54 |
codeView (popup or via CMD-E).
|
2
|
55 |
"
|
|
56 |
! !
|
|
57 |
|
|
58 |
!Explainer class methodsFor:'explaining'!
|
|
59 |
|
|
60 |
explain:someText in:source forClass:aClass
|
6
|
61 |
"Given a source and a substring of it, return a string containing
|
|
62 |
an explanation.
|
49
|
63 |
This is just a q&d implementation - to be correct, it should use the parser,
|
|
64 |
and explain from the parsetree (instead of doing string matches).
|
|
65 |
This leads to some wrong explanations, for example if some string is
|
|
66 |
used as selector within a string.
|
|
67 |
Also, there could be much more detailed explanations."
|
2
|
68 |
|
|
69 |
|parser variables v c string sym list count tmp
|
|
70 |
commonSuperClass|
|
|
71 |
|
|
72 |
string := someText withoutSeparators.
|
49
|
73 |
|
|
74 |
"
|
|
75 |
ask parser for variable names
|
|
76 |
"
|
2
|
77 |
parser := self parseMethod:source in:aClass.
|
|
78 |
parser notNil ifTrue:[
|
49
|
79 |
"look for variables"
|
2
|
80 |
|
49
|
81 |
variables := parser methodVars.
|
|
82 |
(variables notNil and:[variables includes:string]) ifTrue:[
|
|
83 |
^ string , ' is a method variable'
|
|
84 |
].
|
|
85 |
variables := parser methodArgs.
|
|
86 |
(variables notNil and:[variables includes:string]) ifTrue:[
|
|
87 |
^ string , ' is a method argument'
|
|
88 |
]
|
2
|
89 |
].
|
49
|
90 |
|
2
|
91 |
parser isNil ifTrue:[
|
49
|
92 |
parser := self for:(ReadStream on:source) in:aClass
|
2
|
93 |
].
|
|
94 |
|
|
95 |
"instvars"
|
84
|
96 |
c := aClass whichClassDefinesInstVar:string.
|
|
97 |
c notNil ifTrue:[
|
|
98 |
^ string , ' is an instance variable in ' , c name
|
2
|
99 |
].
|
|
100 |
|
84
|
101 |
"/ variables := aClass allInstVarNames.
|
|
102 |
"/ (variables notNil and:[variables includes:string]) ifTrue:[
|
|
103 |
"/ "where is it"
|
|
104 |
"/ c := aClass.
|
|
105 |
"/ [c notNil] whileTrue:[
|
|
106 |
"/ v := c instVarNames.
|
|
107 |
"/ (v notNil and:[v includes:string]) ifTrue:[
|
|
108 |
"/ ^ string , ' is an instance variable in ' , c name
|
|
109 |
"/ ].
|
|
110 |
"/ c := c superclass
|
|
111 |
"/ ].
|
|
112 |
"/ self error:'oops'
|
|
113 |
"/ ].
|
|
114 |
|
2
|
115 |
"class instvars"
|
|
116 |
variables := aClass class allInstVarNames.
|
|
117 |
(variables notNil and:[variables includes:string]) ifTrue:[
|
49
|
118 |
"where is it"
|
|
119 |
c := aClass.
|
|
120 |
[c notNil] whileTrue:[
|
|
121 |
v := c class instVarNames.
|
|
122 |
(v notNil and:[v includes:string]) ifTrue:[
|
|
123 |
^ string , ' is a class instance variable in ' , c name
|
|
124 |
].
|
|
125 |
c := c superclass
|
|
126 |
].
|
|
127 |
self error:'oops'
|
2
|
128 |
].
|
|
129 |
|
|
130 |
"classvars"
|
|
131 |
c := parser inWhichClassIsClassVar:string.
|
|
132 |
c notNil ifTrue:[
|
49
|
133 |
^ string , ' is a class variable in ' , c name
|
2
|
134 |
].
|
|
135 |
|
|
136 |
string knownAsSymbol ifTrue:[
|
49
|
137 |
"globals & symbols"
|
2
|
138 |
|
49
|
139 |
tmp := self explainKnownSymbol:string.
|
|
140 |
tmp notNil ifTrue:[ ^ tmp].
|
2
|
141 |
].
|
|
142 |
|
|
143 |
"try for some obvious things"
|
|
144 |
tmp := self explainPseudoVariable:string in:aClass.
|
|
145 |
tmp notNil ifTrue:[ ^ tmp].
|
|
146 |
|
|
147 |
"try syntax ..."
|
|
148 |
|
49
|
149 |
tmp := self explainSyntax:string.
|
|
150 |
tmp notNil ifTrue:[ ^ tmp].
|
20
|
151 |
|
2
|
152 |
parser isNil ifTrue:[
|
49
|
153 |
^ 'parse error - no explanation'
|
2
|
154 |
].
|
|
155 |
^ 'cannot explain this - select individual tokens for an explanation.'
|
|
156 |
!
|
|
157 |
|
|
158 |
explainPseudoVariable:string in:aClass
|
|
159 |
"return explanation for the pseudoVariables self, super etc."
|
|
160 |
|
|
161 |
(string = 'self') ifTrue:[
|
49
|
162 |
^ self explainSelfIn:aClass
|
2
|
163 |
].
|
|
164 |
|
|
165 |
(string = 'super') ifTrue:[
|
49
|
166 |
^ self explainSuperIn:aClass
|
2
|
167 |
].
|
|
168 |
|
84
|
169 |
(string = 'here') ifTrue:[
|
|
170 |
^ self explainHereIn:aClass
|
|
171 |
].
|
|
172 |
|
2
|
173 |
(string = 'thisContext') ifTrue:[
|
49
|
174 |
^ 'thisContext is a pseudo variable (i.e. it is built in).
|
2
|
175 |
|
49
|
176 |
ThisContext always refers to the context object for the currently executed method or
|
|
177 |
block (an instance of Context or BlockContext respectively). The calling chain and calling
|
6
|
178 |
receivers/selectors can be accessed via thisContext.'
|
|
179 |
].
|
|
180 |
|
|
181 |
(string = 'true') ifTrue:[
|
49
|
182 |
^ 'true is a pseudo variable (i.e. it is built in).
|
6
|
183 |
|
|
184 |
True represents logical truth. It is the one and only instance of class True.'
|
2
|
185 |
].
|
|
186 |
|
|
187 |
(string = 'false') ifTrue:[
|
49
|
188 |
^ 'false is a pseudo variable (i.e. it is built in).
|
2
|
189 |
|
|
190 |
False represents logical falseness. It is the one and only instance of class False.'
|
|
191 |
].
|
|
192 |
|
|
193 |
(string = 'nil') ifTrue:[
|
49
|
194 |
^ 'nil is a pseudo variable (i.e. it is built in).
|
2
|
195 |
|
|
196 |
Nil is used for unitialized variables (among other uses).
|
|
197 |
Nil is the one and only instance of class UndefinedObject.'
|
|
198 |
].
|
|
199 |
^ nil
|
|
200 |
!
|
|
201 |
|
6
|
202 |
explainSelfIn:aClass
|
|
203 |
|sub|
|
|
204 |
|
|
205 |
sub := aClass allSubclasses collect:[:c | c name].
|
|
206 |
sub size == 0 ifTrue:[
|
49
|
207 |
^ 'self refers to the object which received the message.
|
6
|
208 |
|
|
209 |
In this case, it will be an instance of ' , aClass name , '.'
|
|
210 |
].
|
|
211 |
|
|
212 |
sub size <= 5 ifTrue:[
|
49
|
213 |
^ 'self refers to the object which received the message.
|
6
|
214 |
|
|
215 |
In this case, it will be an instance of ' , aClass name , '
|
|
216 |
or one of its subclasses:
|
|
217 |
|
58
|
218 |
' , sub asStringCollection asString
|
6
|
219 |
].
|
|
220 |
|
|
221 |
^ 'self refers to the object which received the message.
|
|
222 |
|
|
223 |
In this case, it will be an instance of ' , aClass name , '
|
|
224 |
or one of its ' , sub size printString , ' subclasses.'
|
|
225 |
!
|
|
226 |
|
|
227 |
explainSuperIn:aClass
|
|
228 |
^ 'like self, super refers to the object which received the message.
|
|
229 |
|
|
230 |
However, when sending a message to super the search for methods
|
|
231 |
implementing this message will start in the superclass (' , aClass superclass name , ')
|
84
|
232 |
instead of the receivers class (' , aClass name , ' or subclass).
|
49
|
233 |
Thus, using super, a redefined method can call the original method in its superclass.'
|
6
|
234 |
!
|
2
|
235 |
|
84
|
236 |
explainHereIn:aClass
|
|
237 |
^ 'like self, here refers to the object which received the message.
|
|
238 |
|
|
239 |
However, when sending a message to here the search for methods
|
|
240 |
implementing this message will start in the defining class (' , aClass name , ')
|
|
241 |
instead of the receivers class (' , aClass name , ' or subclass).
|
|
242 |
Thus, using here, redefined methods will NOT be reached with a here-send.'
|
|
243 |
!
|
|
244 |
|
2
|
245 |
commonSuperClassOf:listOfClassNames
|
|
246 |
|common found|
|
|
247 |
|
|
248 |
listOfClassNames do:[:className |
|
49
|
249 |
|class|
|
2
|
250 |
|
49
|
251 |
((className endsWith:'class') and:[className ~= 'Metaclass']) ifTrue:[
|
96
|
252 |
class := (Smalltalk at:(className copyWithoutLast:5 "copyTo:(className size - 5)") asSymbol) class
|
49
|
253 |
] ifFalse:[
|
|
254 |
class := Smalltalk at:(className asSymbol).
|
|
255 |
].
|
|
256 |
common isNil ifTrue:[
|
|
257 |
common := class
|
|
258 |
] ifFalse:[
|
|
259 |
(class isSubclassOf:common) ifTrue:[
|
|
260 |
"keep common"
|
|
261 |
] ifFalse:[
|
|
262 |
(common isSubclassOf:class) ifTrue:[
|
|
263 |
common := class
|
|
264 |
] ifFalse:[
|
|
265 |
"walk up, checking"
|
|
266 |
found := false.
|
|
267 |
common allSuperclassesDo:[:sup |
|
|
268 |
(class isSubclassOf:sup) ifTrue:[
|
|
269 |
common := sup
|
|
270 |
]
|
|
271 |
].
|
|
272 |
found ifFalse:[
|
|
273 |
class allSuperclassesDo:[:sup |
|
|
274 |
(common isSubclassOf:sup) ifTrue:[
|
|
275 |
common := sup
|
|
276 |
]
|
|
277 |
].
|
|
278 |
].
|
|
279 |
]
|
|
280 |
].
|
|
281 |
].
|
|
282 |
common == Object ifTrue:[^ common]
|
2
|
283 |
].
|
|
284 |
^ common
|
49
|
285 |
!
|
|
286 |
|
|
287 |
explainSyntax:string
|
|
288 |
"try syntax ...; return explanation or nil"
|
|
289 |
|
|
290 |
((string = ':=') or:[string = '_']) ifTrue:[
|
|
291 |
^ '<variable> := <expression>
|
|
292 |
|
|
293 |
:= and _ (which is left-arrow in some fonts) mean assignment.
|
|
294 |
The variable is bound to (i.e. points to) the value of <expression>.'
|
|
295 |
].
|
|
296 |
|
|
297 |
(string = '^') ifTrue:[
|
|
298 |
^ '^ <expression>
|
|
299 |
|
|
300 |
return the value of <expression> as value from the method.
|
|
301 |
A return from within a block exits the method where the block is defined.'
|
|
302 |
].
|
|
303 |
|
|
304 |
(string = ';') ifTrue:[
|
|
305 |
^ '<expression> ; selector1 ; .... ; selectorN
|
|
306 |
|
|
307 |
a cascade expression; evaluate expression, and send messages
|
|
308 |
<selector1> ... <selectorN> to the first expressions receiver.
|
|
309 |
Returns the value of the last send. The cascade sends may also have arguments.'
|
|
310 |
].
|
|
311 |
|
|
312 |
(string = '|') ifTrue:[
|
|
313 |
^ '| locals | or: [:arg | statements]
|
|
314 |
|
|
315 |
| is used to mark a local variable declaration or separates arguments
|
|
316 |
from the statements in a block. Notice, that in a block-argument declaration
|
|
317 |
these must be prefixed by a colon character.
|
|
318 |
| is also a selector understood by Booleans.'
|
|
319 |
].
|
|
320 |
|
|
321 |
((string startsWith:'(') or:[string endsWith:')']) ifTrue:[
|
|
322 |
^ '(<expression>)
|
|
323 |
|
|
324 |
expression grouping.'
|
|
325 |
].
|
|
326 |
|
|
327 |
((string startsWith:'[') or:[string endsWith:']']) ifTrue:[
|
|
328 |
^ '[arguments | statements]
|
|
329 |
|
|
330 |
defines a block.
|
|
331 |
Blocks represent pieces of executable code. Definition of a block does
|
|
332 |
not evaluate it. The block is evaluated by sending it a value/value:
|
|
333 |
message.
|
|
334 |
Blocks are often passed as arguments to Booleans (i.e. ifTrue:[...]) or
|
|
335 |
collections (i.e. do:[...]).'
|
|
336 |
].
|
|
337 |
|
|
338 |
(string = ':') ifTrue:[
|
|
339 |
^ 'colons have different meaning depending on context:
|
|
340 |
|
|
341 |
1) they separate keyword-parts in symbols and keyword-messages as in:
|
|
342 |
|
|
343 |
#at:put: a constant keyword symbol
|
|
344 |
|
|
345 |
rec at:index put:value sends the #at:put: message to rec,
|
|
346 |
passing index and value as arguments.
|
|
347 |
|
|
348 |
2) within block-argument declarations as in:
|
|
349 |
|
|
350 |
[:arg1 :arg2 | statements]
|
|
351 |
'
|
|
352 |
].
|
|
353 |
|
|
354 |
(string = '.') ifTrue:[
|
|
355 |
^ 'statement. "<- period here"
|
|
356 |
statement
|
|
357 |
|
|
358 |
within a method or block, individual statements are separated by periods.
|
|
359 |
'
|
|
360 |
].
|
|
361 |
|
|
362 |
(string startsWith:'#' ) ifTrue:[
|
|
363 |
(string startsWith:'#(' ) ifTrue:[
|
|
364 |
^ 'is a constant Array.
|
|
365 |
|
|
366 |
The elements of a constant Array must be Number-constants, nil, true or false.
|
|
367 |
(notice, that not all Smalltalk implementations allow true, false and nil as
|
|
368 |
constant-Array elements).'
|
|
369 |
].
|
|
370 |
|
|
371 |
(string startsWith:'#[') ifTrue:[
|
|
372 |
^ 'is a constant ByteArray.
|
|
373 |
|
|
374 |
The elements of a constant ByteArray must be Integer constants in the range
|
|
375 |
0 .. 255.
|
|
376 |
(notice, that not all Smalltalk implementations support constant ByteArrays).'
|
|
377 |
].
|
|
378 |
|
|
379 |
(string startsWith:'#''') ifTrue:[
|
|
380 |
^ 'is a constant symbol containing non-alphanumeric characters.
|
|
381 |
|
|
382 |
Symbols are unique strings, meaning that there exists
|
|
383 |
exactly one instance of a given symbol. Therefore symbols can
|
|
384 |
be compared using == (identity compare) in addition to = (contents compare).
|
|
385 |
Beside this, Symbols behave mostly like Strings.
|
|
386 |
|
|
387 |
Notice, that not all Smalltalk implementations support this kind of symbols.'
|
|
388 |
].
|
|
389 |
|
|
390 |
^ 'is a symbol.
|
|
391 |
|
|
392 |
Symbols are unique strings, meaning that there exists
|
|
393 |
exactly one instance of a given symbol. Therefore symbols can
|
|
394 |
be compared using == (identity compare) in addition to = (contents compare).
|
|
395 |
Beside this, Symbols behave mostly like Strings.'
|
|
396 |
].
|
|
397 |
|
|
398 |
"/ is it a symbol without hash-character ?
|
|
399 |
"/
|
|
400 |
string knownAsSymbol ifTrue:[
|
|
401 |
^ 'is nothing, but #' , string , ' is known as a symbol.
|
|
402 |
|
|
403 |
Symbols are unique strings, meaning that there exists
|
|
404 |
exactly one instance of a given symbol. Therefore symbols can
|
|
405 |
be compared using == (identity compare) in addition to = (contents compare).
|
|
406 |
Beside this, Symbols behave mostly like Strings.'
|
|
407 |
].
|
|
408 |
|
|
409 |
^ nil
|
|
410 |
!
|
|
411 |
|
|
412 |
explainKnownSymbol:string
|
|
413 |
"return explanation or nil"
|
|
414 |
|
84
|
415 |
|sym list count tmp commonSuperClass val|
|
49
|
416 |
|
|
417 |
sym := string asSymbol.
|
|
418 |
|
|
419 |
"try globals"
|
|
420 |
|
|
421 |
(Smalltalk includesKey:sym) ifTrue:[
|
54
|
422 |
tmp := string , ' is a global variable.'.
|
84
|
423 |
val := Smalltalk at:sym.
|
|
424 |
val isBehavior ifTrue:[
|
|
425 |
val name = string ifTrue:[
|
54
|
426 |
tmp := tmp , '
|
49
|
427 |
|
84
|
428 |
' , string , ' is a class categorized as ' , val category , '
|
|
429 |
in the ''' , val package , ''' package.'
|
54
|
430 |
] ifFalse:[
|
|
431 |
tmp := tmp , '
|
|
432 |
|
84
|
433 |
' , string , ' is bound to the class ' , val name ,
|
|
434 |
' in category ' , val category , '.'
|
54
|
435 |
]
|
49
|
436 |
] ifFalse:[
|
54
|
437 |
tmp := tmp , '
|
49
|
438 |
|
84
|
439 |
Its current value is ' , val classNameWithArticle , '.'
|
54
|
440 |
].
|
|
441 |
^ tmp.
|
49
|
442 |
].
|
|
443 |
|
54
|
444 |
"
|
|
445 |
try selectors
|
|
446 |
look who implements it
|
|
447 |
"
|
49
|
448 |
list := Set new.
|
|
449 |
Smalltalk allBehaviorsDo:[:c|
|
|
450 |
(c implements:sym) ifTrue:[
|
|
451 |
list add:(c name)
|
|
452 |
].
|
|
453 |
(c class implements:sym) ifTrue:[
|
|
454 |
list add:(c name , 'class')
|
|
455 |
]
|
|
456 |
].
|
|
457 |
|
|
458 |
count := list size.
|
|
459 |
(count ~~ 0) ifTrue:[
|
54
|
460 |
"
|
|
461 |
for up-to 4 implementing classes,
|
|
462 |
list them
|
|
463 |
"
|
49
|
464 |
list := list asOrderedCollection sort.
|
|
465 |
tmp := ' is a selector implemented in '.
|
|
466 |
(count == 1) ifTrue:[
|
|
467 |
^ string , tmp , (list at:1) , '.'
|
|
468 |
].
|
84
|
469 |
|
49
|
470 |
(count == 2) ifTrue:[
|
|
471 |
^ string , tmp , (list at:1) , ' and ' , (list at:2) , '.'
|
|
472 |
].
|
|
473 |
(count == 3) ifTrue:[
|
|
474 |
^ string , tmp , '
|
|
475 |
' , (list at:1) , ', ' , (list at:2) , ' and ' , (list at:3) , '.'
|
|
476 |
].
|
|
477 |
(count == 4) ifTrue:[
|
|
478 |
^ string , tmp , '
|
|
479 |
' , (list at:1) , ', ' , (list at:2) , ', ' , (list at:3), ' and ' , (list at:4) , '.'
|
|
480 |
].
|
|
481 |
|
54
|
482 |
"
|
|
483 |
if there are more, look for a common
|
|
484 |
superclass and show it ...
|
|
485 |
"
|
49
|
486 |
commonSuperClass := self commonSuperClassOf:list.
|
|
487 |
commonSuperClass ~~ Object ifTrue:[
|
84
|
488 |
(list includes:commonSuperClass) ifTrue:[
|
|
489 |
^ string , tmp, count printString , commonSuperClass name
|
|
490 |
, ' and redefined in ' , (count - 1) printString
|
|
491 |
, ' subclasses'
|
|
492 |
].
|
49
|
493 |
^ string , tmp, count printString , ' subclasses of ' , commonSuperClass name
|
|
494 |
].
|
|
495 |
|
54
|
496 |
"
|
|
497 |
otherwise just give the number.
|
|
498 |
"
|
49
|
499 |
^ string , tmp , count printString , ' classes.'
|
|
500 |
].
|
|
501 |
|
|
502 |
^ nil
|
2
|
503 |
! !
|