author | Claus Gittinger <cg@exept.de> |
Tue, 05 Jul 2011 11:05:08 +0200 | |
changeset 2525 | f08c56018d9b |
parent 2506 | 3a8b4a11428e |
child 2526 | ecd1ee8f03bc |
permissions | -rw-r--r-- |
2506 | 1 |
" |
2 |
COPYRIGHT (c) 2006 by eXept Software AG |
|
3 |
All Rights Reserved |
|
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 |
" |
|
2371 | 12 |
"{ Package: 'stx:libcomp' }" |
13 |
||
14 |
Parser subclass:#AbstractSyntaxHighlighter |
|
2525 | 15 |
instanceVariableNames:'method sourceText preferences fullSelectorCheck' |
2371 | 16 |
classVariableNames:'' |
17 |
poolDictionaries:'' |
|
18 |
category:'System-Compiler' |
|
19 |
! |
|
20 |
||
2506 | 21 |
!AbstractSyntaxHighlighter class methodsFor:'documentation'! |
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 2006 by eXept Software AG |
|
26 |
All Rights Reserved |
|
27 |
||
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! ! |
|
2371 | 36 |
|
37 |
!AbstractSyntaxHighlighter class methodsFor:'api highlighting'! |
|
38 |
||
39 |
formatClassDefinition:aString in:aClass |
|
40 |
"format (recolor) a class definition expression in a given class. |
|
41 |
Return the text containing font changes and color information." |
|
42 |
||
43 |
^ self formatExpression:aString in:aClass |
|
44 |
! |
|
45 |
||
2506 | 46 |
formatClassDefinition:aString in:aClass elementsInto: elements |
47 |
"format (recolor) a class definition expression in a given class. |
|
48 |
Return the text containing font changes and color information." |
|
49 |
||
50 |
^ self formatExpression:aString in:aClass |
|
51 |
||
52 |
"Created: / 10-04-2011 / 18:18:28 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
53 |
! |
|
54 |
||
2371 | 55 |
formatExpression:aString in:aClass |
56 |
"format (recolor) an expression in a given class. |
|
57 |
Return the text containing font changes and color information." |
|
58 |
||
59 |
|parser tree text endPos| |
|
60 |
||
61 |
aString isNil ifTrue:[^ nil]. |
|
62 |
||
63 |
parser := self for:(ReadStream on:aString string) in:aClass. |
|
64 |
parser ignoreErrors:true. |
|
65 |
parser ignoreWarnings:true. |
|
66 |
parser sourceText:(text := aString string asText). |
|
67 |
"/ use an array here - this can be changed much faster using #at:put: |
|
68 |
text emphasisCollection:(Array new:aString size). |
|
69 |
||
70 |
parser nextToken. |
|
71 |
tree := parser expression. |
|
72 |
"/ now, convert the emphasis-array to a runArray |
|
73 |
text emphasisCollection:(text emphasis asRunArray). |
|
74 |
||
75 |
tree == #Error ifTrue:[ |
|
76 |
"/ mhmh - which is better ... |
|
77 |
"/ alternative1: color rest after error in red |
|
78 |
"/ text |
|
79 |
"/ emphasizeFrom:(parser sourceStream position) |
|
80 |
"/ to:text size |
|
81 |
"/ with:(#color->Color red). |
|
82 |
||
83 |
||
84 |
"/ alternative2: take original emphasis for rest |
|
85 |
||
86 |
endPos := parser sourceStream position1Based. |
|
87 |
endPos >= text size ifTrue:[ |
|
88 |
^ text |
|
89 |
]. |
|
90 |
^ (text copyTo:endPos) , (aString copyFrom:(endPos+1)) |
|
91 |
||
92 |
"/ alternative3: no emphasis for rest. |
|
93 |
||
94 |
"/ ^ text "/ aString |
|
95 |
]. |
|
96 |
^ text |
|
97 |
||
98 |
" |
|
99 |
self |
|
100 |
formatExpression:'(1 + 2) max:5' |
|
101 |
in:UndefinedObject |
|
102 |
" |
|
103 |
||
104 |
"Modified: / 7.4.1998 / 09:57:19 / cg" |
|
105 |
"Created: / 9.4.1998 / 16:57:16 / cg" |
|
106 |
! |
|
107 |
||
108 |
formatMethod:aString in:aClass |
|
109 |
"format (recolor) a method in a given class. |
|
110 |
Return the text containing font changes and color information." |
|
111 |
||
112 |
self obsoleteMethodWarning:'use #formatMethodSource:in:'. |
|
113 |
^ self formatMethod:nil source:aString in:aClass using:nil |
|
114 |
||
115 |
" |
|
116 |
self |
|
117 |
formatMethod:'foo |
|
118 |
^ self bar:''hello''. |
|
119 |
||
120 |
' , (Character doubleQuote asString) , 'some comment' , (Character doubleQuote asString) , ' |
|
121 |
' |
|
122 |
in:UndefinedObject |
|
123 |
" |
|
124 |
||
125 |
"Modified: / 28-04-2010 / 13:03:04 / cg" |
|
126 |
! |
|
127 |
||
128 |
formatMethod:aString in:aClass using:preferencesOrNil |
|
129 |
"format (recolor) a method in a given class. |
|
130 |
Return the text containing font changes and color information." |
|
131 |
||
132 |
self obsoleteMethodWarning:'use #formatMethodSource:in:using:'. |
|
133 |
^ self formatMethod:nil source:aString in:aClass using:preferencesOrNil |
|
134 |
||
135 |
"Modified: / 28-04-2010 / 13:03:15 / cg" |
|
136 |
! |
|
137 |
||
138 |
formatMethod:methodOrNil source:aString in:aClass |
|
139 |
"format (recolor) a method in a given class. |
|
140 |
Return the text containing font changes and color information." |
|
141 |
||
142 |
^ self formatMethod:methodOrNil source:aString in:aClass using:nil |
|
143 |
||
144 |
" |
|
145 |
self |
|
146 |
formatMethod:'foo |
|
147 |
^ self bar:''hello''. |
|
148 |
||
149 |
' , (Character doubleQuote asString) , 'some comment' , (Character doubleQuote asString) , ' |
|
150 |
' |
|
151 |
in:UndefinedObject |
|
152 |
" |
|
153 |
||
154 |
"Created: / 28-04-2010 / 13:44:24 / cg" |
|
155 |
! |
|
156 |
||
157 |
formatMethod:methodOrNil source:aString in:aClass using:preferencesOrNil |
|
158 |
"format (recolor) a method in a given class. |
|
159 |
Return the text containing font changes and color information." |
|
160 |
||
161 |
|highlighter tree text endPos eColor| |
|
162 |
||
163 |
aString isNil ifTrue:[^ nil]. |
|
164 |
||
165 |
Error handle:[:ex | |
|
2392
c26d2d1dfffe
changed: #formatMethod:source:in:using:
Claus Gittinger <cg@exept.de>
parents:
2371
diff
changeset
|
166 |
"/ Transcript showCR:ex description. |
2371 | 167 |
"/ self halt. |
168 |
ex signal isHandled ifTrue:[ |
|
169 |
ex reject. |
|
170 |
]. |
|
171 |
(self parseErrorSignal handles:ex) ifFalse:[ |
|
172 |
"Parse error may happen when re-formatting incomplete code while editing" |
|
173 |
('SyntaxHighlighter [info]: error during highlight: ' , ex description) infoPrintCR. |
|
174 |
"/ ex suspendedContext fullPrintAll. |
|
175 |
]. |
|
176 |
^ aString |
|
177 |
] do:[ |
|
178 |
highlighter := self for:(ReadStream on:aString string) in:aClass. |
|
179 |
preferencesOrNil notNil ifTrue:[highlighter preferences:preferencesOrNil]. |
|
180 |
"/ highlighter ignoreErrors:true. |
|
181 |
highlighter ignoreWarnings:true. |
|
182 |
highlighter sourceText:(text := aString string asText). |
|
183 |
highlighter method:methodOrNil. |
|
184 |
||
185 |
"/ use an array here - this can be changed much faster using #at:put: |
|
186 |
text emphasisCollection:(Array new:aString size). |
|
187 |
||
188 |
tree := highlighter parseMethod. |
|
189 |
"/ now, convert the emphasis-array to a runArray |
|
190 |
text emphasisCollection:(text emphasis asRunArray). |
|
191 |
||
192 |
tree == #Error ifTrue:[ |
|
193 |
eColor := UserPreferences current errorColor. |
|
194 |
eColor notNil ifTrue:[ |
|
195 |
"/ mhmh - which is better ... |
|
196 |
"/ alternative1: color rest after error in red |
|
197 |
text |
|
198 |
emphasizeFrom:(highlighter sourceStream position1Based) |
|
199 |
to:text size |
|
200 |
with:(#color->eColor). |
|
201 |
] ifFalse:[ |
|
202 |
"/ alternative2: take original emphasis for rest |
|
203 |
||
204 |
endPos := highlighter sourceStream position1Based. |
|
205 |
endPos >= text size ifTrue:[ |
|
206 |
^ text |
|
207 |
]. |
|
208 |
^ (text copyTo:endPos) , (aString copyFrom:(endPos+1)) |
|
209 |
]. |
|
210 |
"/ alternative3: no emphasis for rest. |
|
211 |
]. |
|
212 |
^ text |
|
213 |
] |
|
214 |
" |
|
215 |
self |
|
216 |
formatMethod:'foo |
|
217 |
^ self bar:''hello''. |
|
218 |
||
219 |
' , (Character doubleQuote asString) , 'some comment' , (Character doubleQuote asString) , ' |
|
220 |
' |
|
221 |
in:UndefinedObject |
|
222 |
" |
|
223 |
||
224 |
"Created: / 28-04-2010 / 13:01:42 / cg" |
|
2392
c26d2d1dfffe
changed: #formatMethod:source:in:using:
Claus Gittinger <cg@exept.de>
parents:
2371
diff
changeset
|
225 |
"Modified: / 07-05-2010 / 12:24:52 / cg" |
2371 | 226 |
! |
227 |
||
228 |
formatMethodSource:aString in:aClass |
|
229 |
"format (recolor) a method in a given class. |
|
230 |
Return the text containing font changes and color information." |
|
231 |
||
232 |
^ self formatMethod:nil source:aString in:aClass using:nil |
|
233 |
||
234 |
" |
|
235 |
self |
|
236 |
formatMethod:'foo |
|
237 |
^ self bar:''hello''. |
|
238 |
||
239 |
' , (Character doubleQuote asString) , 'some comment' , (Character doubleQuote asString) , ' |
|
240 |
' |
|
241 |
in:UndefinedObject |
|
242 |
" |
|
243 |
||
244 |
"Created: / 28-04-2010 / 12:58:13 / cg" |
|
245 |
! |
|
246 |
||
247 |
formatMethodSource:aString in:aClass using:preferencesOrNil |
|
248 |
"format (recolor) a method in a given class. |
|
249 |
Return the text containing font changes and color information." |
|
250 |
||
251 |
^ self formatMethod:nil source:aString in:aClass using:preferencesOrNil |
|
252 |
||
253 |
"Modified: / 28-04-2010 / 13:02:11 / cg" |
|
254 |
! ! |
|
255 |
||
2525 | 256 |
!AbstractSyntaxHighlighter class methodsFor:'highlighting'! |
257 |
||
258 |
formatMethod:aMethodOrNil source:aString in:aClass using:preferencesOrNil elementsInto: elements |
|
259 |
^ self formatMethod:aString in:aClass using:preferencesOrNil |
|
260 |
||
261 |
"Created: / 05-07-2011 / 10:41:14 / cg" |
|
262 |
! ! |
|
263 |
||
2371 | 264 |
!AbstractSyntaxHighlighter methodsFor:'accessing'! |
265 |
||
266 |
method:aMethod |
|
2525 | 267 |
"the original method, if known (for subclasses which can make use of it)" |
268 |
||
269 |
method := aMethod. |
|
2371 | 270 |
|
271 |
"Created: / 28-04-2010 / 13:15:33 / cg" |
|
272 |
! |
|
273 |
||
274 |
preferences:something |
|
275 |
preferences := something. |
|
276 |
! |
|
277 |
||
278 |
sourceText |
|
279 |
^ sourceText |
|
280 |
||
281 |
"Created: / 31.3.1998 / 11:49:05 / cg" |
|
282 |
! |
|
283 |
||
284 |
sourceText:aString |
|
285 |
sourceText := aString. |
|
286 |
||
287 |
"Created: / 31-03-1998 / 11:49:05 / cg" |
|
288 |
"Modified: / 28-04-2010 / 13:22:27 / cg" |
|
289 |
! ! |
|
290 |
||
291 |
!AbstractSyntaxHighlighter methodsFor:'error handling'! |
|
292 |
||
293 |
parseError:aMessage position:position to:endPos |
|
294 |
"/ Transcript showCR:aMessage. |
|
295 |
super parseError:aMessage position:position to:endPos. |
|
296 |
||
297 |
self |
|
298 |
markFrom:position to:endPos |
|
299 |
withEmphasis:nil color:UserPreferences current errorColor |
|
300 |
! |
|
301 |
||
302 |
showErrorMessage:aMessage position:pos |
|
303 |
"/ Transcript showCR:aMessage. |
|
304 |
super showErrorMessage:aMessage position:pos. |
|
305 |
||
306 |
self |
|
307 |
markFrom:pos to:nil |
|
308 |
withEmphasis:nil color:UserPreferences current errorColor |
|
309 |
! |
|
310 |
||
311 |
syntaxError:aMessage position:position to:endPos |
|
312 |
"/ Transcript showCR:aMessage. |
|
313 |
super syntaxError:aMessage position:position to:endPos. |
|
314 |
||
315 |
self |
|
316 |
markFrom:position to:endPos |
|
317 |
withEmphasis:nil color:UserPreferences current errorColor |
|
318 |
! |
|
319 |
||
320 |
warning:msg position:pos1 to:pos2 |
|
321 |
"/ self markUnknownIdentifierFrom:pos1 to:pos2 |
|
322 |
||
323 |
"/ self |
|
324 |
"/ markFrom:pos1 to:pos2 |
|
325 |
"/ withEmphasis:nil color:UserPreferences current errorColor |
|
326 |
||
327 |
"Modified: / 25.9.1999 / 18:42:30 / cg" |
|
328 |
! ! |
|
329 |
||
330 |
!AbstractSyntaxHighlighter methodsFor:'initialization'! |
|
331 |
||
332 |
initialize |
|
333 |
super initialize. |
|
334 |
||
335 |
foldConstants := false. |
|
336 |
preferences := UserPreferences current. |
|
337 |
fullSelectorCheck := preferences fullSelectorCheck. |
|
338 |
||
339 |
"Created: / 31-03-1998 / 15:12:55 / cg" |
|
340 |
"Modified: / 28-04-2010 / 13:17:45 / cg" |
|
341 |
! ! |
|
342 |
||
343 |
!AbstractSyntaxHighlighter methodsFor:'misc'! |
|
344 |
||
345 |
controlFlowSelectors |
|
346 |
^ #( |
|
347 |
ifTrue: |
|
348 |
ifFalse: |
|
349 |
ifTrue:ifFalse: |
|
350 |
ifFalse:ifTrue: |
|
351 |
||
352 |
whileTrue: |
|
353 |
whileFalse: |
|
354 |
||
355 |
to:do: |
|
356 |
downTo:do: |
|
357 |
to:by:do: |
|
358 |
||
359 |
loop |
|
360 |
whileTrue |
|
361 |
whileFalse |
|
362 |
doWhile: |
|
363 |
doUntil: |
|
364 |
||
365 |
do: |
|
366 |
keysAndValuesDo: |
|
367 |
) |
|
368 |
||
369 |
"Created: / 08-09-2006 / 15:56:47 / cg" |
|
370 |
! |
|
371 |
||
372 |
defineAsUndeclaredVariable:aName |
|
373 |
"redefined to NOT declare undefined vars" |
|
374 |
||
375 |
^ VariableNode globalNamed:aName |
|
376 |
||
377 |
"Modified: / 19.10.1998 / 19:38:12 / cg" |
|
378 |
! |
|
379 |
||
380 |
isSyntaxHighlighter |
|
381 |
^ true |
|
382 |
||
383 |
! |
|
384 |
||
385 |
plausibilityCheck:aNode |
|
386 |
"redefined to NOT do checks" |
|
387 |
||
388 |
^ nil |
|
389 |
||
390 |
"Modified: / 19.10.1998 / 19:38:12 / cg" |
|
391 |
"Created: / 19.10.1998 / 19:57:18 / cg" |
|
392 |
! ! |
|
393 |
||
394 |
!AbstractSyntaxHighlighter methodsFor:'syntax detection'! |
|
395 |
||
396 |
markFrom:pos1 to:pos2 withAddedEmphasis:addedEmphasis |
|
397 |
sourceText emphasisFrom:pos1 to:pos2 add:addedEmphasis |
|
398 |
||
399 |
"Created: / 15-01-2008 / 11:48:18 / cg" |
|
400 |
! |
|
401 |
||
402 |
markFrom:pos1 to:pos2 withEmphasis:fontEmp color:clrIn |
|
403 |
|e p2 clr| |
|
404 |
||
405 |
(clrIn isNil or:[clrIn = Color black]) ifTrue:[ |
|
406 |
e := fontEmp |
|
407 |
] ifFalse:[ |
|
408 |
clr := clrIn onDevice:Screen current. |
|
409 |
fontEmp isNil ifTrue:[ |
|
410 |
e := (#color->clr) |
|
411 |
] ifFalse:[ |
|
412 |
e := Text addEmphasis:fontEmp to:(#color->clr). |
|
413 |
] |
|
414 |
]. |
|
415 |
(p2 := pos2) isNil ifTrue:[ |
|
416 |
p2 := sourceText size |
|
417 |
] ifFalse:[ |
|
418 |
p2 := p2 min:sourceText size |
|
419 |
]. |
|
420 |
sourceText emphasizeFrom:pos1 to:p2 with:e |
|
421 |
||
422 |
"Created: / 31-03-1998 / 13:26:53 / cg" |
|
423 |
"Modified: / 28-04-2010 / 14:12:31 / cg" |
|
424 |
! |
|
425 |
||
426 |
markVariable:v |
|
427 |
|pos endPos| |
|
428 |
||
429 |
pos := tokenPosition. |
|
430 |
endPos := pos+tokenName size-1. |
|
431 |
self markVariable:v from:pos to:endPos |
|
432 |
||
2464
595ec0d9efa9
comment/format in: #markVariable:
Claus Gittinger <cg@exept.de>
parents:
2392
diff
changeset
|
433 |
"Modified: / 30-11-2010 / 14:44:28 / cg" |
2371 | 434 |
! ! |
435 |
||
436 |
!AbstractSyntaxHighlighter class methodsFor:'documentation'! |
|
437 |
||
438 |
version_CVS |
|
2525 | 439 |
^ '$Header: /cvs/stx/stx/libcomp/AbstractSyntaxHighlighter.st,v 1.5 2011-07-05 09:05:08 cg Exp $' |
2506 | 440 |
! |
441 |
||
442 |
version_SVN |
|
2525 | 443 |
^ '§ Id §' |
2371 | 444 |
! ! |