forked from bonzini/gst-visualgst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SyntaxHighlighter.st
164 lines (126 loc) · 4.63 KB
/
SyntaxHighlighter.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
STInST.STInST.RBProgramNodeVisitor subclass: SyntaxHighlighter [
| textBuffer variable |
<category: 'Graphics-Browser'>
<comment: nil>
SyntaxHighlighter class >> highlight: node in: aGtkTextBuffer [
<category: 'instance creation'>
(self new)
initialize;
textBuffer: aGtkTextBuffer;
visitNode: node;
acceptComments: node comments
]
initialize [
<category: 'initialize-release'>
variable := Dictionary new.
variable
at: 'self' put: #specialId;
at: 'super' put: #specialId;
at: 'thisContext' put: #specialId
]
textBuffer: aGtkTextBuffer [
<category: 'initialize-release'>
textBuffer := aGtkTextBuffer
]
acceptComments: anArray [
<category: 'visitor-double dispatching'>
anArray ifNil: [ ^ self ].
anArray do: [ :each |
textBuffer applyTagByName: #comment startOffset: (each first - 1) endOffset: each last ]
]
acceptArrayNode: anArrayNode [
<category: 'visitor-double dispatching'>
self visitNode: anArrayNode body
]
acceptAssignmentNode: anAssignmentNode [
<category: 'visitor-double dispatching'>
self acceptVariableNode: anAssignmentNode variable.
self visitNode: anAssignmentNode value
]
acceptBlockNode: aBlockNode [
<category: 'visitor-double dispatching'>
aBlockNode colons with: aBlockNode arguments
do: [ :colonPos :argument |
self highlightNewVariable: argument as: #arguments ].
self visitNode: aBlockNode body
]
acceptCascadeNode: aCascadeNode [
<category: 'visitor-double dispatching'>
| n |
n := 0.
self visitNode: aCascadeNode messages first receiver.
aCascadeNode messages do: [ :each |
self highlightMessageSend: each ]
]
acceptLiteralNode: aLiteralNode [
<category: 'visitor-double dispatching'>
textBuffer applyTagByName: #literal startOffset: (aLiteralNode start - 1) endOffset: aLiteralNode stop
]
acceptMessageNode: aMessageNode [
<category: 'visitor-double dispatching'>
self visitNode: aMessageNode receiver.
self highlightMessageSend: aMessageNode
]
acceptMethodNode: aMethodNode [
"A pity we cannot share this code with highlightMessageSend: ..."
<category: 'visitor-double dispatching'>
aMethodNode isUnary
ifTrue:
[ textBuffer applyTagByName: #unaryMsg startOffset: (aMethodNode selectorParts first start - 1) endOffset: aMethodNode selectorParts first stop ].
aMethodNode isBinary
ifTrue:
[ textBuffer applyTagByName: #binaryMsg startOffset: (aMethodNode selectorParts first start - 1) endOffset: aMethodNode selectorParts first stop.
self highlightNewVariable: aMethodNode arguments first as: #arguments ].
aMethodNode isKeyword
ifTrue:
[ aMethodNode selectorParts with: aMethodNode arguments
do: [ :sel :arg |
textBuffer applyTagByName: #binaryMsg startOffset: (sel start - 1) endOffset: sel stop.
self highlightNewVariable: arg as: #arguments ] ].
self visitNode: aMethodNode body
]
acceptOptimizedNode: aBlockNode [
<category: 'visitor-double dispatching'>
self visitNode: aBlockNode body
]
acceptReturnNode: aReturnNode [
<category: 'visitor-double dispatching'>
self visitNode: aReturnNode value
]
acceptSequenceNode: aSequenceNode [
<category: 'visitor-double dispatching'>
| n |
n := 0.
aSequenceNode temporaries do: [ :temporary |
self highlightNewVariable: temporary as: #temporary].
aSequenceNode statements do: [ :each |
self visitNode: each ]
]
acceptVariableNode: aVariableNode [
<category: 'visitor-double dispatching'>
| tag |
tag := variable at: aVariableNode name ifAbsentPut: [ #undeclaredVar ].
textBuffer applyTagByName: tag startOffset: (aVariableNode start - 1) endOffset: aVariableNode stop
]
highlightMessageSend: aMessageNode [
<category: 'visitor-double dispatching'>
aMessageNode isUnary
ifTrue:
[ textBuffer applyTagByName: #unaryMsg startOffset: (aMessageNode selectorParts first start - 1) endOffset: aMessageNode selectorParts first stop ].
aMessageNode isBinary
ifTrue:
[ textBuffer applyTagByName: #binaryMsg startOffset: (aMessageNode selectorParts first start - 1) endOffset: aMessageNode selectorParts first stop.
self visitNode: aMessageNode arguments first ].
aMessageNode isKeyword
ifTrue: [
aMessageNode selectorParts with: aMessageNode arguments
do: [ :sel :arg |
textBuffer applyTagByName: #binaryMsg startOffset: (sel start - 1) endOffset: sel stop.
self visitNode: arg ] ]
]
highlightNewVariable: node as: kind [
<category: 'visitor-double dispatching'>
variable at: node name ifAbsentPut: [ kind ].
textBuffer applyTagByName: kind startOffset: (node start - 1) endOffset: node stop
]
]