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