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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
/***************************************************************************
kxesyntaxhighlighter.cpp - XML Syntax highlighter
-------------------
begin : Ne pro 14 2003
copyright : (C) 2003 by The KXMLEditor Team
email : lvanek.sourceforge.net
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "kxesyntaxhighlighter.h"
#include <iostream>
#include <tqcolor.h>
#include <tqregexp.h>
#include <tqstring.h>
#include <tqstringlist.h>
#include <ktextedit.h>
// Regular expressions for parsing XML borrowed from:
// http://www.cs.sfu.ca/~cameron/REX.html
KXESyntaxHighlighter::KXESyntaxHighlighter(TQTextEdit *textEdit)
: TQSyntaxHighlighter(textEdit)
{
m_clrDefaultText.setRgb(0, 0, 0);
m_clrElementName.setRgb(128, 0, 0);
m_clrAttributeName.setRgb(0, 255, 255);
m_clrAttributeValue.setRgb(0, 255, 0);
m_clrXmlSyntaxChar.setRgb(0, 0, 128);
m_clrComment.setRgb(128, 128, 128);
m_clrSyntaxError.setRgb(255, 0, 0);
}
KXESyntaxHighlighter::~KXESyntaxHighlighter()
{
}
int KXESyntaxHighlighter::highlightParagraph(const TQString& text, int endStateOfLastPara)
{
//first I format the given line to default so any remaining highlighting is removed (TQt does not do it by itself)
setFormat(0 , text.length(), TQColor(0, 0, 0));
int iBracketNesting = 0;
m_eParserState = parsingNone;
int pos;
unsigned int i = 0;
if(endStateOfLastPara == 1)
{
TQRegExp patternComment("[^-]*-([^-][^-]*-)*->"); // search end of comment
pos=patternComment.search(text, i);
if(pos >= 0) // end comment found ?
{
int l = patternComment.matchedLength();
setFormat(0, l - 3, m_clrComment);
setFormat(l - 3, 3, m_clrXmlSyntaxChar );
i += l; // skip comment
}
else
{
setFormat(0, text.length(), m_clrComment);
return 1; // return 1 to signify "in comment"
}
}
for(; i < text.length() - 1; i++)
{
switch(text[i])
{
case '<':
iBracketNesting++;
if(iBracketNesting == 1)
{ setFormat( i, 1, m_clrXmlSyntaxChar );
m_eParserState = expectElementNameOrSlash;
}
else
setFormat( i, 1, m_clrSyntaxError ); // wrong bracket nesting
break;
case '>':
iBracketNesting--;
if(iBracketNesting == 0)
setFormat( i, 1, m_clrXmlSyntaxChar );
else
setFormat( i, 1, m_clrSyntaxError ); // wrong bracket nesting
m_eParserState = parsingNone;
break;
case '/':
if(m_eParserState == expectElementNameOrSlash)
{
m_eParserState = expectElementName;
setFormat( i, 1, m_clrXmlSyntaxChar );
}
else
{
if(m_eParserState == expectAtttributeOrEndOfElement)
setFormat( i, 1, m_clrXmlSyntaxChar );
else
processDefaultText(i, text);
}
break;
case '=':
if(m_eParserState == expectEqual)
{
m_eParserState = expectAttributeValue;
setFormat( i, 1, m_clrXmlSyntaxChar );
}
else
{
processDefaultText(i, text);
}
break;
case '\"':
if(m_eParserState == expectAttributeValue)
{
TQRegExp patternAttribute("\"[^<\"]*\"|'[^<']*'"); // search attribute value
pos=patternAttribute.search(text, i);
if(pos == (int) i) // attribute value found ?
{
int l = patternAttribute.matchedLength();
setFormat(i, 1, m_clrXmlSyntaxChar );
setFormat(i+1, l - 2, m_clrAttributeValue);
setFormat(i+l-1, 1, m_clrXmlSyntaxChar );
i += l - 1; // skip attribute value
m_eParserState = expectAtttributeOrEndOfElement;
}
else
processDefaultText(i, text);
}
else
processDefaultText(i, text);
break;
case '!':
if(m_eParserState == expectElementNameOrSlash)
{
TQRegExp patternComment("<!--[^-]*-([^-][^-]*-)*->"); // search comment
pos=patternComment.search(text, i-1);
if(pos == (int) i-1) // comment found ?
{
int l = patternComment.matchedLength();
setFormat(pos, 4, m_clrXmlSyntaxChar);
setFormat(pos + 4, l - 7, m_clrComment);
setFormat(l - 3, 3, m_clrXmlSyntaxChar);
i += l - 2; // skip comment
m_eParserState = parsingNone;
iBracketNesting--;
}
else
{ // Try find multiline comment
TQRegExp patternCommentStart("<!--"); // search comment start
pos=patternCommentStart.search(text, i-1);
if(pos == (int)i-1) // comment found ?
{
setFormat(i, 3, m_clrXmlSyntaxChar );
setFormat(i + 3, text.length() - i - 3, m_clrComment);
return 1; // return 1 to signify "in comment"
}
else
processDefaultText(i, text);
}
}
else
processDefaultText(i, text);
break;
default:
int iLenght = processDefaultText(i, text);
if(iLenght > 0)
i += iLenght - 1;
break;
}
}
return 0;
}
int KXESyntaxHighlighter::processDefaultText(int i, const TQString& text)
{
int l = 0; // length of matched text
switch(m_eParserState)
{
case expectElementNameOrSlash:
case expectElementName:
{
TQRegExp patternName("([A-Za-z_:]|[^\\x00-\\x7F])([A-Za-z0-9_:.-]|[^\\x00-\\x7F])*"); // search element name
int pos=patternName.search(text, i);
if(pos == i) // found ?
{
l = patternName.matchedLength();
setFormat(pos, l, m_clrElementName);
m_eParserState = expectAtttributeOrEndOfElement;
}
else
setFormat( i, 1, m_clrDefaultText );
}
break;
case expectAtttributeOrEndOfElement:
{
TQRegExp patternName("([A-Za-z_:]|[^\\x00-\\x7F])([A-Za-z0-9_:.-]|[^\\x00-\\x7F])*"); // search attribute name
int pos=patternName.search(text, i);
if(pos == i) // found ?
{
l = patternName.matchedLength();
setFormat(pos, l, m_clrAttributeName);
m_eParserState = expectEqual;
}
else
setFormat( i, 1, m_clrDefaultText );
}
break;
default:
setFormat( i, 1, m_clrDefaultText );
break;
}
return l;
}
|