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