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