author | Claus Gittinger <cg@exept.de> |
Thu, 19 Jan 2012 10:48:54 +0100 | |
changeset 11152 | c9a386e4f1e0 |
parent 10882 | fed2030884dc |
child 11458 | 8fdf55d6d1c8 |
child 12123 | 4bde08cebd48 |
permissions | -rw-r--r-- |
10022 | 1 |
" |
10067 | 2 |
COPYRIGHT (c) 2010 by Jan Vrany, SWING Research Group. CTU in Prague |
3 |
All Rights Reserved |
|
10022 | 4 |
|
10067 | 5 |
Permission is hereby granted, free of charge, to any person |
6 |
obtaining a copy of this software and associated documentation |
|
7 |
files (the 'Software'), to deal in the Software without |
|
8 |
restriction, including without limitation the rights to use, |
|
9 |
copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10 |
copies of the Software, and to permit persons to whom the |
|
11 |
Software is furnished to do so, subject to the following |
|
12 |
conditions: |
|
13 |
||
14 |
The above copyright notice and this permission notice shall be |
|
15 |
included in all copies or substantial portions of the Software. |
|
16 |
||
17 |
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, |
|
18 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|
19 |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
20 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
21 |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|
22 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
23 |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
24 |
OTHER DEALINGS IN THE SOFTWARE. |
|
10022 | 25 |
" |
26 |
"{ Package: 'stx:libtool' }" |
|
27 |
||
28 |
SyntaxHighlighter subclass:#SyntaxHighlighter2 |
|
10633 | 29 |
instanceVariableNames:'elements lastVariableElements lastSelectorElement' |
10022 | 30 |
classVariableNames:'' |
31 |
poolDictionaries:'' |
|
32 |
category:'Interface-CodeView-Syntax' |
|
33 |
! |
|
34 |
||
35 |
!SyntaxHighlighter2 class methodsFor:'documentation'! |
|
36 |
||
37 |
copyright |
|
38 |
" |
|
10067 | 39 |
COPYRIGHT (c) 2010 by Jan Vrany, SWING Research Group. CTU in Prague |
40 |
All Rights Reserved |
|
41 |
||
42 |
Permission is hereby granted, free of charge, to any person |
|
43 |
obtaining a copy of this software and associated documentation |
|
44 |
files (the 'Software'), to deal in the Software without |
|
45 |
restriction, including without limitation the rights to use, |
|
46 |
copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
47 |
copies of the Software, and to permit persons to whom the |
|
48 |
Software is furnished to do so, subject to the following |
|
49 |
conditions: |
|
10022 | 50 |
|
10067 | 51 |
The above copyright notice and this permission notice shall be |
52 |
included in all copies or substantial portions of the Software. |
|
53 |
||
54 |
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, |
|
55 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|
56 |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
57 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
58 |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|
59 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
60 |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
61 |
OTHER DEALINGS IN THE SOFTWARE. |
|
10022 | 62 |
" |
63 |
! ! |
|
64 |
||
65 |
!SyntaxHighlighter2 class methodsFor:'highlighting'! |
|
66 |
||
67 |
formatClassDefinition:aString in:aClass elementsInto: elements |
|
68 |
"format (recolor) a class definition expression in a given class. |
|
69 |
Return the text containing font changes and color information." |
|
70 |
||
71 |
^ self formatExpression:aString in:aClass elementsInto: elements |
|
72 |
||
73 |
"Created: / 10-04-2011 / 18:18:00 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
74 |
! |
|
75 |
||
76 |
formatExpression:aString in:aClass elementsInto: elements |
|
77 |
"format (recolor) an expression in a given class. |
|
78 |
Return the text containing font changes and color information." |
|
79 |
||
80 |
|parser tree text endPos| |
|
81 |
||
82 |
aString isNil ifTrue:[^ nil]. |
|
83 |
||
84 |
parser := self for:(ReadStream on:aString string) in:aClass. |
|
85 |
parser elements: elements. |
|
86 |
parser ignoreErrors:true. |
|
87 |
parser ignoreWarnings:true. |
|
88 |
parser sourceText:(text := aString string asText). |
|
89 |
"/ use an array here - this can be changed much faster using #at:put: |
|
90 |
text emphasisCollection:(Array new:aString size). |
|
91 |
||
92 |
parser nextToken. |
|
93 |
tree := parser "expression"statementList. |
|
94 |
"/ now, convert the emphasis-array to a runArray |
|
95 |
text emphasisCollection:(text emphasis asRunArray). |
|
96 |
||
97 |
tree == #Error ifTrue:[ |
|
98 |
"/ mhmh - which is better ... |
|
99 |
"/ alternative1: color rest after error in red |
|
100 |
"/ text |
|
101 |
"/ emphasizeFrom:(parser sourceStream position) |
|
102 |
"/ to:text size |
|
103 |
"/ with:(#color->Color red). |
|
104 |
||
105 |
||
106 |
"/ alternative2: take original emphasis for rest |
|
107 |
||
108 |
endPos := parser sourceStream position1Based. |
|
109 |
endPos >= text size ifTrue:[ |
|
110 |
^ text |
|
111 |
]. |
|
112 |
^ ((text copyTo:endPos) , (aString copyFrom:(endPos+1))). |
|
113 |
||
114 |
"/ alternative3: no emphasis for rest. |
|
115 |
||
116 |
"/ ^ text "/ aString |
|
117 |
]. |
|
118 |
^ text |
|
119 |
||
120 |
" |
|
121 |
self |
|
122 |
formatExpression:'(1 + 2) max:5' |
|
123 |
in:UndefinedObject |
|
124 |
" |
|
125 |
||
126 |
"Created: / 25-07-2010 / 08:56:05 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
127 |
"Modified: / 25-07-2010 / 10:57:00 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
128 |
! |
|
129 |
||
130 |
formatMethod:aString in:aClass using:preferencesOrNil elementsInto: elements |
|
131 |
"format (recolor) a method in a given class. |
|
132 |
Return the text containing font changes and color information." |
|
133 |
||
10152
561883853228
comment/format in: #formatMethod:in:using:elementsInto:
Claus Gittinger <cg@exept.de>
parents:
10150
diff
changeset
|
134 |
"/ obsolete interface |
10150
3792ee8840ea
added: #formatMethod:source:in:using:elementsInto:
Claus Gittinger <cg@exept.de>
parents:
10067
diff
changeset
|
135 |
^ self |
3792ee8840ea
added: #formatMethod:source:in:using:elementsInto:
Claus Gittinger <cg@exept.de>
parents:
10067
diff
changeset
|
136 |
formatMethod:nil |
3792ee8840ea
added: #formatMethod:source:in:using:elementsInto:
Claus Gittinger <cg@exept.de>
parents:
10067
diff
changeset
|
137 |
source:aString |
3792ee8840ea
added: #formatMethod:source:in:using:elementsInto:
Claus Gittinger <cg@exept.de>
parents:
10067
diff
changeset
|
138 |
in:aClass |
3792ee8840ea
added: #formatMethod:source:in:using:elementsInto:
Claus Gittinger <cg@exept.de>
parents:
10067
diff
changeset
|
139 |
using:preferencesOrNil elementsInto:elements |
3792ee8840ea
added: #formatMethod:source:in:using:elementsInto:
Claus Gittinger <cg@exept.de>
parents:
10067
diff
changeset
|
140 |
|
3792ee8840ea
added: #formatMethod:source:in:using:elementsInto:
Claus Gittinger <cg@exept.de>
parents:
10067
diff
changeset
|
141 |
"Created: / 25-07-2010 / 08:56:29 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
10152
561883853228
comment/format in: #formatMethod:in:using:elementsInto:
Claus Gittinger <cg@exept.de>
parents:
10150
diff
changeset
|
142 |
"Modified (comment): / 05-07-2011 / 11:07:50 / cg" |
10150
3792ee8840ea
added: #formatMethod:source:in:using:elementsInto:
Claus Gittinger <cg@exept.de>
parents:
10067
diff
changeset
|
143 |
! |
3792ee8840ea
added: #formatMethod:source:in:using:elementsInto:
Claus Gittinger <cg@exept.de>
parents:
10067
diff
changeset
|
144 |
|
3792ee8840ea
added: #formatMethod:source:in:using:elementsInto:
Claus Gittinger <cg@exept.de>
parents:
10067
diff
changeset
|
145 |
formatMethod:aMethodOrNil source:aString in:aClass using:preferencesOrNil elementsInto: elements |
3792ee8840ea
added: #formatMethod:source:in:using:elementsInto:
Claus Gittinger <cg@exept.de>
parents:
10067
diff
changeset
|
146 |
"format (recolor) a method in a given class. |
3792ee8840ea
added: #formatMethod:source:in:using:elementsInto:
Claus Gittinger <cg@exept.de>
parents:
10067
diff
changeset
|
147 |
Return the text containing font changes and color information." |
3792ee8840ea
added: #formatMethod:source:in:using:elementsInto:
Claus Gittinger <cg@exept.de>
parents:
10067
diff
changeset
|
148 |
|
10022 | 149 |
|highlighter tree text endPos eColor| |
150 |
||
151 |
aString isNil ifTrue:[^ nil]. |
|
152 |
||
153 |
Error handle:[:ex | |
|
154 |
ex signal isHandled ifTrue:[ |
|
155 |
ex reject. |
|
156 |
]. |
|
157 |
(self parseErrorSignal handles:ex) ifFalse:[ |
|
158 |
"Parse error may happen when re-formatting incomplete code while editing" |
|
159 |
('SyntaxHighlighter [info]: error during highlight: ' , ex description) infoPrintCR. |
|
160 |
"/ ex suspendedContext fullPrintAll. |
|
161 |
]. |
|
162 |
^ aString |
|
163 |
] do:[ |
|
164 |
highlighter := self for:(ReadStream on:aString string) in:aClass. |
|
165 |
highlighter elements: elements. |
|
166 |
preferencesOrNil notNil ifTrue:[highlighter preferences:preferencesOrNil]. |
|
167 |
"/ highlighter ignoreErrors:true. |
|
168 |
highlighter ignoreWarnings:true. |
|
169 |
highlighter sourceText:(text := aString string asText). |
|
170 |
"/ use an array here - this can be changed much faster using #at:put: |
|
171 |
text emphasisCollection:(Array new:aString size). |
|
172 |
||
173 |
tree := highlighter parseMethod. |
|
174 |
"/ now, convert the emphasis-array to a runArray |
|
175 |
text emphasisCollection:(text emphasis asRunArray). |
|
176 |
||
177 |
tree == #Error ifTrue:[ |
|
178 |
eColor := UserPreferences current errorColor. |
|
179 |
eColor notNil ifTrue:[ |
|
180 |
"/ mhmh - which is better ... |
|
181 |
"/ alternative1: color rest after error in red |
|
182 |
text |
|
183 |
emphasizeFrom:(highlighter sourceStream position1Based) |
|
184 |
to:text size |
|
185 |
with:(#color->eColor). |
|
186 |
] ifFalse:[ |
|
187 |
"/ alternative2: take original emphasis for rest |
|
188 |
||
189 |
endPos := highlighter sourceStream position1Based. |
|
190 |
endPos >= text size ifTrue:[ |
|
191 |
^ Array with: text with: highlighter elements |
|
192 |
]. |
|
193 |
^ ((text copyTo:endPos) , (aString copyFrom:(endPos+1))) |
|
194 |
]. |
|
195 |
"/ alternative3: no emphasis for rest. |
|
196 |
]. |
|
197 |
^text |
|
198 |
] |
|
199 |
" |
|
200 |
self |
|
201 |
formatMethod:'foo |
|
202 |
^ self bar:''hello''. |
|
203 |
||
204 |
' , (Character doubleQuote asString) , 'some comment' , (Character doubleQuote asString) , ' |
|
205 |
' |
|
206 |
in:UndefinedObject |
|
207 |
" |
|
208 |
||
209 |
"Modified: / 22-08-2006 / 13:32:04 / cg" |
|
10150
3792ee8840ea
added: #formatMethod:source:in:using:elementsInto:
Claus Gittinger <cg@exept.de>
parents:
10067
diff
changeset
|
210 |
"Created: / 05-07-2011 / 10:39:21 / cg" |
10022 | 211 |
! ! |
212 |
||
213 |
!SyntaxHighlighter2 methodsFor:'accessing'! |
|
214 |
||
215 |
elements |
|
216 |
^ elements |
|
217 |
! |
|
218 |
||
219 |
elements:something |
|
10633 | 220 |
"the element collection, to collect variables, selectors etc. into" |
221 |
||
10022 | 222 |
elements := something. |
10633 | 223 |
|
224 |
"Modified (comment): / 21-08-2011 / 09:13:31 / cg" |
|
10022 | 225 |
! ! |
226 |
||
227 |
!SyntaxHighlighter2 methodsFor:'initialization'! |
|
228 |
||
229 |
initialize |
|
230 |
||
231 |
super initialize. |
|
232 |
elements := SortedCollection new. |
|
10633 | 233 |
lastVariableElements := Dictionary new. |
10022 | 234 |
|
235 |
"Created: / 14-02-2010 / 13:08:27 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
236 |
"Modified: / 25-06-2010 / 13:04:37 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
10633 | 237 |
"Modified: / 21-08-2011 / 09:37:35 / cg" |
10022 | 238 |
! ! |
239 |
||
240 |
!SyntaxHighlighter2 methodsFor:'parsing-expressions'! |
|
241 |
||
242 |
binaryExpressionFor:receiverArg |
|
243 |
"parse a binary-expression; return a node-tree, nil or #Error" |
|
244 |
||
11152
c9a386e4f1e0
refactored plausibility checks
Claus Gittinger <cg@exept.de>
parents:
10882
diff
changeset
|
245 |
|receiver expr arg sel pos1 pos2 lno| |
10022 | 246 |
|
247 |
receiver := receiverArg. |
|
248 |
(receiver == #Error) ifTrue:[^ #Error]. |
|
249 |
||
250 |
"special kludge: since Scanner cannot know if -digit is a binary |
|
251 |
expression or a negative constant, handle cases here" |
|
252 |
||
253 |
[(tokenType == #BinaryOperator) |
|
254 |
or:[(tokenType == $|) |
|
255 |
or:[(tokenType == $^ and:[parserFlags allowCaretAsBinop]) |
|
256 |
or:[((tokenType == #Integer) or:[tokenType == #Float]) |
|
257 |
and:[tokenValue < 0]]]] |
|
258 |
] whileTrue:[ |
|
259 |
"/ kludge alarm: in a function-call argList, #, is not a binarySelector |
|
260 |
inFunctionCallArgument == true ifTrue:[ |
|
261 |
((tokenType == #BinaryOperator) and:[tokenName = ',']) ifTrue:[ |
|
262 |
^ receiver |
|
263 |
]. |
|
264 |
]. |
|
265 |
||
266 |
pos1 := tokenPosition. |
|
267 |
lno := tokenLineNr. |
|
268 |
||
269 |
"/ kludge alarm: bar, caret and minus are not scanned as binop |
|
270 |
(tokenType == $|) ifTrue:[ |
|
271 |
sel := '|'. |
|
272 |
sel := self selectorCheck:sel for:receiver position:tokenPosition to:tokenPosition. |
|
273 |
self nextToken. |
|
274 |
] ifFalse:[ |
|
275 |
(tokenType == $^) ifTrue:[ |
|
276 |
sel := '^'. |
|
277 |
sel := self selectorCheck:sel for:receiver position:tokenPosition to:tokenPosition. |
|
278 |
self nextToken. |
|
279 |
] ifFalse:[ |
|
280 |
(tokenType == #BinaryOperator) ifTrue:[ |
|
281 |
sel := tokenName. |
|
282 |
sel := self selectorCheck:sel for:receiver position:tokenPosition to:(tokenPosition + tokenName size - 1). |
|
283 |
self nextToken |
|
284 |
] ifFalse:[ |
|
285 |
sel := '-'. |
|
286 |
token := tokenValue := tokenValue negated. |
|
287 |
tokenPosition := tokenPosition + 1. "/ to skip the sign |
|
288 |
] |
|
289 |
]. |
|
290 |
]. |
|
291 |
||
292 |
pos2 := pos1 + sel size - 1. |
|
293 |
self markSelector:sel from:pos1 to:pos2 receiverNode:receiver. |
|
294 |
lastSelectorElement := nil. |
|
295 |
||
296 |
arg := self unaryExpression. |
|
297 |
(arg == #Error) ifTrue:[^ #Error]. |
|
298 |
||
299 |
expr := BinaryNode receiver:receiver selector:sel arg:arg fold:foldConstants. |
|
300 |
expr isErrorNode ifTrue:[ |
|
301 |
self parseError:(expr errorString) position:pos1 to:tokenPosition. |
|
302 |
errorFlag := false. "ok, user wants it - so he'll get it" |
|
303 |
expr := BinaryNode receiver:receiver selector:sel arg:arg fold:nil. |
|
304 |
]. |
|
305 |
expr lineNumber:lno. |
|
306 |
expr selectorPosition:pos1. |
|
307 |
||
11152
c9a386e4f1e0
refactored plausibility checks
Claus Gittinger <cg@exept.de>
parents:
10882
diff
changeset
|
308 |
self checkPlausibilityOf:expr from:pos1 to:pos2. |
10022 | 309 |
parseForCode ifFalse:[ |
310 |
self rememberSelectorUsed:sel receiver:receiver |
|
311 |
]. |
|
312 |
receiver := expr. "/ for next message |
|
313 |
]. |
|
314 |
^ receiver |
|
315 |
||
316 |
"Modified: / 09-01-1998 / 19:05:18 / stefan" |
|
317 |
"Modified: / 14-02-2010 / 17:54:53 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
11152
c9a386e4f1e0
refactored plausibility checks
Claus Gittinger <cg@exept.de>
parents:
10882
diff
changeset
|
318 |
"Modified: / 19-01-2012 / 10:46:49 / cg" |
10022 | 319 |
! |
320 |
||
321 |
expression |
|
322 |
"parse a cascade-expression; return a node-tree, nil or #Error. |
|
323 |
||
324 |
expression ::= keywordExpression |
|
325 |
| keywordExpression cascade |
|
326 |
||
327 |
cascade ::= ';' expressionSendPart |
|
328 |
| cascade ';' expressionSendPart |
|
329 |
||
330 |
expressionSendPart ::= { KEYWORD binaryExpression } |
|
331 |
| BINARYOPERATOR unaryExpression |
|
332 |
| IDENTIFIER |
|
333 |
" |
|
334 |
||
335 |
|receiver arg sel args pos pos2 lno tokenEnd realReceiver positions| |
|
336 |
||
337 |
pos := tokenPosition. |
|
338 |
receiver := self keywordExpression. |
|
339 |
(receiver == #Error) ifTrue:[^ #Error]. |
|
340 |
(tokenType == $;) ifTrue:[ |
|
341 |
receiver isMessage ifFalse:[ |
|
342 |
self syntaxError:'left side of cascade must be a message expression' |
|
343 |
position:pos to:tokenPosition. |
|
344 |
realReceiver := receiver. "/ only to allow continuing. |
|
345 |
] ifTrue:[ |
|
346 |
realReceiver := receiver receiver. |
|
347 |
]. |
|
348 |
[tokenType == $;] whileTrue:[ |
|
349 |
self nextToken. |
|
350 |
(tokenType == #Identifier) ifTrue:[ |
|
351 |
tokenEnd := tokenPosition + tokenName size - 1. |
|
352 |
self markSelector:tokenName from:tokenPosition to:tokenEnd receiverNode:realReceiver. |
|
353 |
sel := tokenName. |
|
354 |
sel := self selectorCheck:tokenName for:realReceiver position:tokenPosition to:tokenEnd. |
|
355 |
receiver := CascadeNode receiver:receiver selector:sel. |
|
356 |
receiver lineNumber:tokenLineNr. |
|
357 |
parseForCode ifFalse:[ |
|
358 |
self rememberSelectorUsed:sel |
|
359 |
]. |
|
360 |
self nextToken. |
|
361 |
] ifFalse:[ |
|
362 |
(tokenType == #BinaryOperator) ifTrue:[ |
|
363 |
tokenEnd := tokenPosition + tokenName size - 1. |
|
364 |
self markSelector:tokenName from:tokenPosition to:tokenEnd receiverNode:realReceiver. |
|
365 |
lastSelectorElement := nil. |
|
366 |
sel := tokenName. |
|
367 |
sel := self selectorCheck:tokenName for:realReceiver position:tokenPosition to:tokenEnd. |
|
368 |
lno := tokenLineNr. |
|
369 |
self nextToken. |
|
370 |
arg := self unaryExpression. |
|
371 |
(arg == #Error) ifTrue:[^ #Error]. |
|
372 |
receiver := CascadeNode receiver:receiver selector:sel arg:arg. |
|
373 |
receiver lineNumber:lno. |
|
374 |
parseForCode ifFalse:[ |
|
375 |
self rememberSelectorUsed:sel |
|
376 |
]. |
|
377 |
] ifFalse:[ |
|
378 |
(tokenType == #Keyword) ifTrue:[ |
|
379 |
tokenEnd := tokenPosition + tokenName size - 1. |
|
380 |
positions := OrderedCollection with:(tokenPosition to:tokenEnd). |
|
381 |
pos := tokenPosition. |
|
382 |
pos2 := tokenEnd. |
|
383 |
lno := tokenLineNr. |
|
384 |
sel := tokenName. |
|
385 |
self nextToken. |
|
386 |
arg := self binaryExpression. |
|
387 |
(arg == #Error) ifTrue:[^ #Error]. |
|
388 |
args := Array with:arg. |
|
389 |
[tokenType == #Keyword] whileTrue:[ |
|
390 |
tokenEnd := tokenPosition + tokenName size - 1. |
|
391 |
positions add:(tokenPosition to:tokenEnd). |
|
392 |
sel := sel , tokenName. |
|
393 |
self nextToken. |
|
394 |
arg := self binaryExpression. |
|
395 |
(arg == #Error) ifTrue:[^ #Error]. |
|
396 |
args := args copyWith:arg. |
|
397 |
pos2 := tokenEnd |
|
398 |
]. |
|
399 |
positions do:[:p | |
|
400 |
self markSelector:sel from:p start to:p stop receiverNode:realReceiver. |
|
401 |
]. |
|
402 |
lastSelectorElement := nil. |
|
403 |
||
404 |
sel := self selectorCheck:sel for:realReceiver position:pos to:pos2. |
|
405 |
||
406 |
receiver := CascadeNode receiver:receiver selector:sel args:args. |
|
407 |
receiver lineNumber:lno. |
|
408 |
parseForCode ifFalse:[ |
|
409 |
self rememberSelectorUsed:sel |
|
410 |
]. |
|
411 |
] ifFalse:[ |
|
412 |
(tokenType == #Error) ifTrue:[^ #Error]. |
|
413 |
self syntaxError:('invalid cascade; ' , tokenType printString , ' unexpected') |
|
414 |
position:tokenPosition to:source position1Based - 1. |
|
415 |
^ #Error |
|
416 |
] |
|
417 |
] |
|
418 |
] |
|
419 |
]. |
|
420 |
||
421 |
"obscure (unspecified ?) if selector follows; Question: |
|
422 |
||
423 |
is |
|
424 |
'expr sel1; sel2 sel3' |
|
425 |
||
426 |
to be parsed as: |
|
427 |
(t := expr. |
|
428 |
t sel1. |
|
429 |
t sel2) sel3 |
|
430 |
||
431 |
or: |
|
432 |
(t := expr. |
|
433 |
t sel1. |
|
434 |
t sel2 sel3) |
|
435 |
" |
|
436 |
((tokenType == #Identifier) |
|
437 |
or:[(tokenType == #BinaryOperator) |
|
438 |
or:[tokenType == #Keyword]]) ifTrue:[ |
|
439 |
self syntaxError:'ambigous cascade - please group using (...)' |
|
440 |
position:tokenPosition to:source position1Based - 1. |
|
441 |
^ #Error |
|
442 |
"/ self warning: "syntaxError:" 'possibly ambigous cascade - please group using (...)' |
|
443 |
"/ position:tokenPosition to:source position - 1. |
|
444 |
"/ tokenType == #Identifier ifTrue:[ |
|
445 |
"/ ^ self unaryExpressionFor:receiver |
|
446 |
"/ ]. |
|
447 |
"/ tokenType == #BinaryOperator ifTrue:[ |
|
448 |
"/ ^ self binaryExpressionFor:receiver |
|
449 |
"/ ]. |
|
450 |
"/ ^ self keywordExpressionFor:receiver |
|
451 |
] |
|
452 |
]. |
|
453 |
^ receiver |
|
454 |
||
455 |
"Modified: / 19-01-2000 / 16:22:16 / cg" |
|
456 |
"Modified: / 14-02-2010 / 17:58:57 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
457 |
! |
|
458 |
||
459 |
keywordExpressionFor:receiverArg |
|
460 |
"parse a keyword-expression; return a node-tree, nil or #Error. |
|
461 |
||
462 |
keywordExpression ::= binaryexpression |
|
463 |
| { KEYWORD-PART binaryExpression } |
|
464 |
" |
|
465 |
||
11152
c9a386e4f1e0
refactored plausibility checks
Claus Gittinger <cg@exept.de>
parents:
10882
diff
changeset
|
466 |
|expr receiver sel arg args posR1 posR2 pos1 pos2 lno positions constVal| |
10022 | 467 |
|
468 |
receiver := receiverArg. |
|
469 |
posR1 := tokenPosition. |
|
470 |
(tokenType == #Keyword) ifFalse:[^ receiver]. |
|
471 |
||
472 |
pos1 := posR2 := tokenPosition. |
|
473 |
pos2 := tokenPosition + tokenName size - 1. |
|
474 |
positions := OrderedCollection with:(pos1 to:pos2). |
|
475 |
sel := tokenName. |
|
476 |
lno := tokenLineNr. |
|
477 |
self nextToken. |
|
478 |
arg := self binaryExpression. |
|
479 |
(arg == #Error) ifTrue:[^ #Error]. |
|
480 |
args := Array with:arg. |
|
481 |
[tokenType == #Keyword] whileTrue:[ |
|
482 |
sel := sel , tokenName. |
|
483 |
pos2 := tokenPosition + tokenName size - 1. |
|
484 |
positions add:(tokenPosition to:pos2). |
|
485 |
self nextToken. |
|
486 |
arg := self binaryExpression. |
|
487 |
(arg == #Error) ifTrue:[^ #Error]. |
|
488 |
args := args copyWith:arg. |
|
489 |
]. |
|
490 |
||
491 |
positions do:[:p | |
|
492 |
self markSelector:sel from:p start to:p stop receiverNode:receiver. |
|
493 |
]. |
|
494 |
lastSelectorElement := nil. |
|
495 |
sel := self selectorCheck:sel for:receiver positions:positions. |
|
496 |
||
497 |
ignoreWarnings ifFalse:[ |
|
498 |
(Class definitionSelectors includes:sel) ifTrue:[ |
|
499 |
(receiver isVariable and:[receiver isUndeclared]) ifTrue:[ |
|
500 |
"this is not an error - the undefined class may be loaded after this code!!" |
|
501 |
self warning:('as yet undefined superclass: ' , receiver name) position:pos1 to:pos2. |
|
502 |
]. |
|
503 |
]. |
|
504 |
]. |
|
505 |
||
506 |
expr := MessageNode receiver:receiver selector:sel args:args fold:foldConstants. |
|
507 |
expr isErrorNode ifTrue:[ |
|
508 |
self parseError:(expr errorString) position:pos1 to:pos2. |
|
509 |
errorFlag := false. "ok, user wants it - so he'll get it" |
|
510 |
expr := MessageNode receiver:receiver selector:sel args:args fold:nil. |
|
511 |
]. |
|
512 |
expr lineNumber:lno. |
|
11152
c9a386e4f1e0
refactored plausibility checks
Claus Gittinger <cg@exept.de>
parents:
10882
diff
changeset
|
513 |
self checkPlausibilityOf:expr from:pos1 to:pos2. |
10022 | 514 |
parseForCode ifFalse:[ |
515 |
self rememberSelectorUsed:sel receiver:receiver |
|
516 |
]. |
|
517 |
||
518 |
"/ (contextToEvaluateIn isNil and:[selfValue isNil]) ifTrue:[ "/ do not check this for doits |
|
519 |
"/ receiver isSuper ifTrue:[ |
|
520 |
"/ sel ~= selector ifTrue:[ |
|
521 |
"/ self warnCommonMistake:'possible bad super message (selector should be same as in current method) ?' |
|
522 |
"/ position:posR1 to:posR2-1 |
|
523 |
"/ ]. |
|
524 |
"/ ]. |
|
525 |
"/ ]. |
|
526 |
"/ |
|
527 |
||
528 |
(sel = #ifTrue: or:[sel = #ifFalse: or:[sel = #ifTrue:ifFalse: or:[sel = #ifFalse:ifTrue:]]]) ifTrue:[ |
|
529 |
(expr receiver withConstantValueDo:[:val | constVal := val]) ifTrue:[ |
|
530 |
|indexOfArgNotExecuted| |
|
531 |
||
532 |
"/ receiver evaluates to a constant |
|
533 |
constVal == true ifTrue:[ |
|
534 |
(sel startsWith: #ifFalse:) ifTrue:[ |
|
535 |
indexOfArgNotExecuted := 1. |
|
536 |
] ifFalse:[ |
|
537 |
indexOfArgNotExecuted := 2. |
|
538 |
] |
|
539 |
]. |
|
540 |
constVal == false ifTrue:[ |
|
541 |
(sel startsWith: #ifTrue:) ifTrue:[ |
|
542 |
indexOfArgNotExecuted := 1. |
|
543 |
] ifFalse:[ |
|
544 |
indexOfArgNotExecuted := 2. |
|
545 |
] |
|
546 |
]. |
|
547 |
indexOfArgNotExecuted == 2 ifTrue:[ |
|
548 |
args size == 1 ifTrue:[ indexOfArgNotExecuted := nil] |
|
549 |
]. |
|
550 |
||
551 |
indexOfArgNotExecuted notNil ifTrue:[ |
|
552 |
|argIsNotExecuted| |
|
553 |
||
554 |
"/ self warning:'receiver is constant; arg',indexOfArgNotExecuted printString,' is never executed' position:pos1 to:tokenPosition. |
|
555 |
argIsNotExecuted := expr args at:indexOfArgNotExecuted. |
|
556 |
argIsNotExecuted isBlockNode ifTrue:[ |
|
557 |
self markCommentFrom:argIsNotExecuted startPosition to:argIsNotExecuted endPosition. |
|
558 |
]. |
|
559 |
]. |
|
560 |
]. |
|
561 |
]. |
|
562 |
||
563 |
(ignoreErrors or:[ignoreWarnings]) ifFalse:[ |
|
564 |
(sel = #and: or:[sel = #or:]) ifTrue:[ |
|
565 |
expr arg1 isBlock ifFalse:[ |
|
566 |
(expr arg1 isVariable |
|
567 |
and:[ (expr arg1 name asLowercase includesString:'block')]) ifFalse:[ |
|
568 |
self warnCommonMistake:'(possible common mistake) missing block brackets ?' |
|
569 |
position:pos2+1 to:tokenPosition-1 |
|
570 |
] |
|
571 |
]. |
|
572 |
^ expr. |
|
573 |
]. |
|
574 |
||
575 |
(sel = #whileTrue: or:[sel = #whileFalse:]) ifTrue:[ |
|
576 |
expr receiver isBlock ifFalse:[ |
|
577 |
(expr receiver isVariable |
|
578 |
and:[ (expr receiver name asLowercase includesString:'block')]) ifFalse:[ |
|
579 |
self warnCommonMistake:'(possible common mistake) missing block brackets ?' |
|
580 |
position:pos1 to:pos2 |
|
581 |
] |
|
582 |
]. |
|
583 |
^ expr. |
|
584 |
]. |
|
585 |
||
586 |
(sel = #ifTrue: or:[sel = #ifFalse:]) ifTrue:[ |
|
587 |
expr receiver isMessage ifTrue:[ |
|
588 |
(expr receiver selector = #whileTrue or:[expr receiver selector = #whileFalse]) ifTrue:[ |
|
589 |
self warnCommonMistake:'strange receiver expression' |
|
590 |
position:pos1 to:pos2 |
|
591 |
]. |
|
592 |
]. |
|
593 |
^ expr |
|
594 |
]. |
|
595 |
]. |
|
596 |
||
597 |
^ expr. |
|
598 |
||
599 |
"Modified: / 14-02-2010 / 17:58:40 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
11152
c9a386e4f1e0
refactored plausibility checks
Claus Gittinger <cg@exept.de>
parents:
10882
diff
changeset
|
600 |
"Modified: / 19-01-2012 / 10:47:01 / cg" |
10022 | 601 |
! |
602 |
||
603 |
unaryExpressionFor:receiverArg |
|
604 |
"parse a unary-expression; return a node-tree, nil or #Error" |
|
605 |
||
11152
c9a386e4f1e0
refactored plausibility checks
Claus Gittinger <cg@exept.de>
parents:
10882
diff
changeset
|
606 |
|receiver expr sel pos pos2 lNr arguments| |
10022 | 607 |
|
608 |
receiver := receiverArg. |
|
609 |
(receiver == #Error) ifTrue:[^ #Error]. |
|
610 |
||
611 |
[ self isValidUnarySelector:tokenType ] whileTrue:[ |
|
612 |
pos := tokenPosition. |
|
613 |
pos2 := pos + tokenName size - 1. |
|
614 |
lNr := tokenLineNr. |
|
615 |
sel := tokenName. |
|
616 |
||
617 |
self markSelector:sel from:pos to:pos2 receiverNode:receiver. |
|
618 |
lastSelectorElement := nil. |
|
619 |
||
620 |
self nextToken. |
|
621 |
tokenType == $( ifTrue:[ |
|
622 |
parserFlags allowSqueakExtensions == true ifTrue:[ |
|
623 |
"/ croquet/squeak extension - c/java-style arguments |
|
624 |
arguments := self functionCallArgList. |
|
625 |
"/ synthetic selector: foo[:[with:[with:[...]]]] |
|
626 |
arguments notEmpty ifTrue:[ |
|
627 |
sel := sel , ':'. |
|
628 |
arguments size - 1 timesRepeat:[ sel := sel , 'with:' ]. |
|
629 |
]. |
|
630 |
sel := self selectorCheck:sel for:receiver position:pos to:pos2. |
|
631 |
expr := MessageNode receiver:receiver selector:sel args:arguments fold:foldConstants. |
|
632 |
expr isErrorNode ifTrue:[ |
|
633 |
self parseError:(expr errorString) position:pos to:pos2. |
|
634 |
errorFlag := false. "ok, user wants it - so he'll get it" |
|
635 |
expr := MessageNode receiver:receiver selector:sel args:arguments fold:nil. |
|
636 |
]. |
|
637 |
expr lineNumber:lNr. |
|
11152
c9a386e4f1e0
refactored plausibility checks
Claus Gittinger <cg@exept.de>
parents:
10882
diff
changeset
|
638 |
self checkPlausibilityOf:expr from:pos to:pos2. |
10022 | 639 |
parseForCode ifFalse:[ |
640 |
self rememberSelectorUsed:sel receiver:receiver |
|
641 |
]. |
|
642 |
^ expr. |
|
643 |
]. |
|
644 |
]. |
|
645 |
||
646 |
sel := self selectorCheck:sel for:receiver position:pos to:pos2. |
|
647 |
expr := UnaryNode receiver:receiver selector:sel fold:foldConstants. |
|
648 |
expr isErrorNode ifTrue:[ |
|
649 |
self warning:(expr errorString , '.\\If you proceed, that error will happen at runtime.') withCRs position:pos to:pos2. |
|
650 |
errorFlag := false. "ok, user wants it - so he'll get it" |
|
651 |
expr := UnaryNode receiver:receiver selector:sel fold:nil. |
|
652 |
]. |
|
653 |
expr lineNumber:lNr. |
|
654 |
||
11152
c9a386e4f1e0
refactored plausibility checks
Claus Gittinger <cg@exept.de>
parents:
10882
diff
changeset
|
655 |
self checkPlausibilityOf:expr from:pos to:pos2. |
10022 | 656 |
parseForCode ifFalse:[ |
657 |
self rememberSelectorUsed:sel receiver:receiver |
|
658 |
]. |
|
659 |
||
660 |
receiver := expr. "/ for next message |
|
661 |
]. |
|
662 |
^ receiver |
|
663 |
||
664 |
"Modified: / 14-02-2010 / 17:56:21 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
11152
c9a386e4f1e0
refactored plausibility checks
Claus Gittinger <cg@exept.de>
parents:
10882
diff
changeset
|
665 |
"Modified: / 19-01-2012 / 10:47:37 / cg" |
10022 | 666 |
! ! |
667 |
||
668 |
!SyntaxHighlighter2 methodsFor:'syntax detection'! |
|
669 |
||
670 |
markArgumentIdentifierFrom:pos1 to:pos2 |
|
671 |
super markArgumentIdentifierFrom:pos1 to:pos2. |
|
10633 | 672 |
self rememberVariableElementFor:tokenName type:#variable from:pos1 to:pos2 |
10022 | 673 |
|
674 |
"Created: / 24-07-2010 / 09:25:12 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
10633 | 675 |
"Modified: / 21-08-2011 / 09:27:26 / cg" |
10022 | 676 |
! |
677 |
||
678 |
markGlobalClassIdentifierFrom:pos1 to:pos2 |
|
679 |
||
680 |
| name env cls | |
|
681 |
||
682 |
super markGlobalClassIdentifierFrom:pos1 to:pos2. |
|
683 |
||
684 |
name := token isString ifTrue:[token asSymbolIfInterned] ifFalse:[nil]. |
|
10882
fed2030884dc
changed: #markGlobalClassIdentifierFrom:to:
Claus Gittinger <cg@exept.de>
parents:
10633
diff
changeset
|
685 |
name notNil ifTrue:[ |
fed2030884dc
changed: #markGlobalClassIdentifierFrom:to:
Claus Gittinger <cg@exept.de>
parents:
10633
diff
changeset
|
686 |
env := (classToCompileFor ? UndefinedObject) theNonMetaclass environment. |
10022 | 687 |
cls := env isNameSpace |
688 |
ifTrue:[env at: name] |
|
689 |
ifFalse:[nil]. |
|
10882
fed2030884dc
changed: #markGlobalClassIdentifierFrom:to:
Claus Gittinger <cg@exept.de>
parents:
10633
diff
changeset
|
690 |
cls isNil ifTrue:[ |
fed2030884dc
changed: #markGlobalClassIdentifierFrom:to:
Claus Gittinger <cg@exept.de>
parents:
10633
diff
changeset
|
691 |
cls := Smalltalk at: name |
fed2030884dc
changed: #markGlobalClassIdentifierFrom:to:
Claus Gittinger <cg@exept.de>
parents:
10633
diff
changeset
|
692 |
] |
fed2030884dc
changed: #markGlobalClassIdentifierFrom:to:
Claus Gittinger <cg@exept.de>
parents:
10633
diff
changeset
|
693 |
]. |
10022 | 694 |
|
695 |
elements add: |
|
696 |
(SyntaxElement from: pos1 to: pos2 type: #class value: cls) |
|
697 |
||
698 |
"Created: / 14-02-2010 / 14:08:44 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
699 |
"Modified: / 15-02-2010 / 10:53:40 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
10882
fed2030884dc
changed: #markGlobalClassIdentifierFrom:to:
Claus Gittinger <cg@exept.de>
parents:
10633
diff
changeset
|
700 |
"Modified: / 18-11-2011 / 14:54:53 / cg" |
10022 | 701 |
! |
702 |
||
703 |
markLocalIdentifierFrom:pos1 to:pos2 |
|
10633 | 704 |
super markLocalIdentifierFrom:pos1 to:pos2. |
10022 | 705 |
|
10633 | 706 |
self rememberVariableElementFor:tokenName type:#variable from:pos1 to:pos2 |
10022 | 707 |
|
708 |
"Created: / 24-07-2010 / 09:28:55 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
10633 | 709 |
"Modified (format): / 21-08-2011 / 09:27:12 / cg" |
10022 | 710 |
! |
711 |
||
712 |
markSelector:selectorString from:pos1 to:pos2 receiverNode:aReceiverNode |
|
713 |
||
714 |
| element | |
|
715 |
||
716 |
super markSelector:selectorString from:pos1 to:pos2 receiverNode:aReceiverNode. |
|
717 |
||
718 |
element := SyntaxElement from: pos1 to: pos2 type: #selector value: selectorString asSymbol. |
|
10633 | 719 |
lastSelectorElement ifNotNil:[lastSelectorElement next: element]. |
10022 | 720 |
elements add: element. |
721 |
lastSelectorElement := element. |
|
722 |
||
723 |
"Created: / 14-02-2010 / 17:40:09 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
724 |
"Modified: / 14-02-2010 / 19:24:17 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
10633 | 725 |
"Modified (format): / 21-08-2011 / 09:18:21 / cg" |
726 |
! |
|
727 |
||
728 |
markSelfFrom:pos1 to:pos2 |
|
729 |
super markSelfFrom:pos1 to:pos2. |
|
730 |
self rememberVariableElementFor:'self' type:#self from:pos1 to:pos2 |
|
731 |
||
732 |
"Created: / 21-08-2011 / 09:15:45 / cg" |
|
10022 | 733 |
! |
734 |
||
735 |
markUnknownIdentifierFrom:pos1 to:pos2 |
|
736 |
||
10428
777230dbf7ab
changed: #markUnknownIdentifierFrom:to:
Claus Gittinger <cg@exept.de>
parents:
10152
diff
changeset
|
737 |
classToCompileFor notNil |
777230dbf7ab
changed: #markUnknownIdentifierFrom:to:
Claus Gittinger <cg@exept.de>
parents:
10152
diff
changeset
|
738 |
ifTrue:[super markUnknownIdentifierFrom:pos1 to:pos2] |
777230dbf7ab
changed: #markUnknownIdentifierFrom:to:
Claus Gittinger <cg@exept.de>
parents:
10152
diff
changeset
|
739 |
ifFalse:[self markLocalIdentifierFrom: pos1 to: pos2]. |
10022 | 740 |
|
741 |
"Created: / 24-07-2010 / 09:51:38 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
10428
777230dbf7ab
changed: #markUnknownIdentifierFrom:to:
Claus Gittinger <cg@exept.de>
parents:
10152
diff
changeset
|
742 |
"Modified: / 02-08-2011 / 09:35:02 / cg" |
10022 | 743 |
! |
744 |
||
745 |
markVariable:v from:pos1 to:pos2 |
|
746 |
super markVariable:v from:pos1 to:pos2. |
|
747 |
(v type == #GlobalVariable) ifTrue:[^self]. |
|
748 |
||
10633 | 749 |
self rememberVariableElementFor:v name type:#variable from:pos1 to:pos2 |
10022 | 750 |
|
751 |
"Created: / 25-06-2010 / 13:03:37 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
752 |
"Modified: / 25-06-2010 / 14:23:03 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
10633 | 753 |
"Modified: / 21-08-2011 / 09:26:30 / cg" |
754 |
! |
|
755 |
||
756 |
rememberVariableElementFor:name type:typeSymbol from:pos1 to:pos2 |
|
757 |
|element prev| |
|
758 |
||
759 |
element := SyntaxElement from: pos1 to: pos2 type:typeSymbol value: name. |
|
760 |
||
761 |
prev := lastVariableElements at:name ifAbsent:[nil]. |
|
762 |
prev notNil ifTrue:[prev next:element]. |
|
763 |
lastVariableElements at:name put:element. |
|
764 |
||
765 |
elements add: element. |
|
766 |
||
767 |
"Created: / 21-08-2011 / 09:26:24 / cg" |
|
10022 | 768 |
! ! |
769 |
||
770 |
!SyntaxHighlighter2 class methodsFor:'documentation'! |
|
771 |
||
772 |
version_CVS |
|
11152
c9a386e4f1e0
refactored plausibility checks
Claus Gittinger <cg@exept.de>
parents:
10882
diff
changeset
|
773 |
^ '$Header: /cvs/stx/stx/libtool/SyntaxHighlighter2.st,v 1.8 2012-01-19 09:48:54 cg Exp $' |
10022 | 774 |
! |
775 |
||
776 |
version_SVN |
|
777 |
^ '§Id: SyntaxHighlighter2.st 7715 2011-04-10 16:32:58Z vranyj1 §' |
|
778 |
! ! |