-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLuaHighlighter.cpp
More file actions
189 lines (160 loc) · 4.63 KB
/
LuaHighlighter.cpp
File metadata and controls
189 lines (160 loc) · 4.63 KB
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
#include "LuaHighlighter.h"
#include <QDebug>
namespace
{
enum
{
bs_none = -1,
bs_quote = 1,
bs_comment = 2
};
}
LuaHighlighter::LuaHighlighter( QTextDocument *parent )
: QSyntaxHighlighter( parent )
{
HighlightingRule rule;
// function calls
functionFormat.setForeground( Qt::blue );
rule.pattern = QRegExp( QLatin1String( "\\b[A-Za-z0-9_]+(?=\\()" ) );
rule.format = functionFormat;
highlightingRules.append( rule );
// keywords (from grammar)
QStringList keywordPatterns;
keywordPatterns << QLatin1String( "\\bfunction\\b" )
<< QLatin1String( "\\bbreak\\b" )
<< QLatin1String( "\\bgoto\\b" )
<< QLatin1String( "\\bdo\\b" )
<< QLatin1String( "\\bend\\b" )
<< QLatin1String( "\\bwhile\\b" )
<< QLatin1String( "\\brepeat\\b" )
<< QLatin1String( "\\buntil\\b" )
<< QLatin1String( "\\bif\\b" )
<< QLatin1String( "\\bthen\\b" )
<< QLatin1String( "\\belseif\\b" )
<< QLatin1String( "\\belse\\b" )
<< QLatin1String( "\\bfor\\b" )
<< QLatin1String( "\\bin\\b" )
<< QLatin1String( "\\blocal\\b" )
<< QLatin1String( "\\bor\\b" )
<< QLatin1String( "\\band\\b" )
<< QLatin1String( "\\bnot\\b" )
<< QLatin1String( "\\breturn\\b" );
keywordFormat.setForeground( Qt::darkBlue );
keywordFormat.setFontWeight( QFont::Bold );
for( auto const& pattern : keywordPatterns )
{
rule.pattern = QRegExp( pattern );
rule.format = keywordFormat;
highlightingRules.append( rule );
}
// numbers, boolean, nil
QStringList valuePatterns;
valuePatterns << QLatin1String( "\\bnil\\b" )
<< QLatin1String( "\\btrue\\b" )
<< QLatin1String( "\\bfalse\\b" )
<< QLatin1String( "\\b\\d+\\b" )
<< QLatin1String( "\\b\\d+.\\b" )
<< QLatin1String( "\\b\\d+e\\b" )
<< QLatin1String( "\\b\\[\\dA-Fa-F]+\\b" );
valueFormat.setForeground( Qt::red );
valueFormat.setFontWeight( QFont::Normal );
for( auto const& pattern : valuePatterns )
{
rule.pattern = QRegExp( pattern );
rule.format = valueFormat;
highlightingRules.append( rule );
}
// double quote "
quotationFormat.setForeground( Qt::darkGreen );
rule.pattern = QRegExp( QLatin1String( "\"[^\"]*\"" ) );
rule.format = quotationFormat;
highlightingRules.append( rule );
// single quote '
rule.pattern = QRegExp( QLatin1String( "\'[^\']*\'" ) );
rule.format = quotationFormat;
highlightingRules.append( rule );
// multi line string [[ ]]
quoteStartExpression = QRegExp( QLatin1String( "\\[\\[" ) ); // --[[
quoteEndExpression = QRegExp( QLatin1String( "\\]\\]" ) ); // ]]
// single line comments
singleLineCommentFormat.setForeground( QColor( Qt::darkGray ).darker( 120 ) );
rule.pattern = QRegExp( QLatin1String( "--[^\n]*") );
rule.format = singleLineCommentFormat;
highlightingRules.append( rule );
//Multi Line Comment --[[ ]]
commentStartExpression = QRegExp( QLatin1String( "--\\[\\[" ) ); // --[[
commentEndExpression = QRegExp( QLatin1String( "\\]\\]" ) ); // ]]
rule.pattern.setMinimal(false);
}
void LuaHighlighter::highlightBlock(const QString &text)
{
for( auto const& rule : highlightingRules )
{
QRegExp expression( rule.pattern );
int index = expression.indexIn( text );
while( index >= 0 )
{
int length = expression.matchedLength();
setFormat( index, length, rule.format );
index = expression.indexIn( text, index + length );
}
}
setCurrentBlockState( bs_none );
//
// multi-line strings
//
int start = -1;
int prev = previousBlockState();
if( prev == bs_quote )
{
start = 0;
}
if( prev == bs_none )
{
start = quoteStartExpression.indexIn( text );
}
while( start >= 0 )
{
int end = quoteEndExpression.indexIn( text, start );
int length;
if( end == -1 )
{
setCurrentBlockState( bs_quote );
length = text.length() - start;
}
else
{
length = end - start + quoteEndExpression.matchedLength();
}
setFormat( start, length, quotationFormat );
start = quoteStartExpression.indexIn( text, start + length );
}
//
// multi-line comments
//
start = -1;
if( prev == bs_comment )
{
start = 0;
}
if( prev == bs_none )
{
start = commentStartExpression.indexIn( text );
}
while( start >= 0 )
{
int end = commentEndExpression.indexIn( text, start );
int length;
if( end == -1 )
{
setCurrentBlockState( bs_comment );
length = text.length() - start;
}
else
{
length = end - start + commentEndExpression.matchedLength();
}
setFormat( start, length, singleLineCommentFormat );
start = commentStartExpression.indexIn( text, start + length );
}
}