Debian Codegraph @

src/internal/QLuaHighlighter.cpp

raw symbols SCIP
1 // QCodeEditor
2 #include <QLuaHighlighter>
3 #include <QSyntaxStyle>
4 #include <QLanguage>
5
6 // Qt
7 #include <QFile>
8
9
10 QLuaHighlighter::QLuaHighlighter(QTextDocument* document) :
11 QStyleSyntaxHighlighter(document),
12 m_highlightRules(),
13 m_highlightBlockRules(),
14 m_requirePattern(QRegularExpression(R"(require\s*([("'][a-zA-Z0-9*._]+['")]))")),
15 m_functionPattern(QRegularExpression(R"(\b([A-Za-z0-9_]+(?:\s+|::))*([A-Za-z0-9_]+)(?=\())")),
16 m_defTypePattern(QRegularExpression(R"(\b([A-Za-z0-9_]+)\s+[A-Za-z]{1}[A-Za-z0-9_]+\s*[=])"))
17 {
18 Q_INIT_RESOURCE(qcodeeditor_resources);
19 QFile fl(":/languages/lua.xml");
20
21 if (!fl.open(QIODevice::ReadOnly))
22 {
23 return;
24 }
25
26 QLanguage language(&fl);
27
28 if (!language.isLoaded())
29 {
30 return;
31 }
32
33 auto keys = language.keys();
34 for (auto&& key : keys)
35 {
36 auto names = language.names(key);
37 for (auto&& name : names)
38 {
39 m_highlightRules.append({
40 QRegularExpression(QString(R"(\b\s{0,1}%1\s{0,1}\b)").arg(name)),
41 key
42 });
43 }
44 }
45
46 // Numbers
47 m_highlightRules.append({
48 QRegularExpression(R"(\b(0b|0x){0,1}[\d.']+\b)"),
49 "Number"
50 });
51
52 // Strings
53 m_highlightRules.append({
54 QRegularExpression(R"(["'][^\n"]*["'])"),
55 "String"
56 });
57
58 // Preprocessor
59 m_highlightRules.append({
60 QRegularExpression(R"(#\![a-zA-Z_]+)"),
61 "Preprocessor"
62 });
63
64 // Single line
65 m_highlightRules.append({
66 QRegularExpression(R"(--[^\n]*)"),
67 "Comment"
68 });
69
70 // Multiline comments
71 m_highlightBlockRules.append({
72 QRegularExpression(R"(--\[\[)"),
73 QRegularExpression(R"(--\]\])"),
74 "Comment"
75 });
76
77 // Multiline string
78 m_highlightBlockRules.append({
79 QRegularExpression(R"(\[\[)"),
80 QRegularExpression(R"(\]\])"),
81 "String"
82 });
83 }
84
85 void QLuaHighlighter::highlightBlock(const QString& text)
86 {
87 { // Checking for require
88 auto matchIterator = m_requirePattern.globalMatch(text);
89
90 while (matchIterator.hasNext())
91 {
92 auto match = matchIterator.next();
93
94 setFormat(
95 match.capturedStart(),
96 match.capturedLength(),
97 syntaxStyle()->getFormat("Preprocessor")
98 );
99
100 setFormat(
101 match.capturedStart(1),
102 match.capturedLength(1),
103 syntaxStyle()->getFormat("String")
104 );
105 }
106 }
107 { // Checking for function
108 auto matchIterator = m_functionPattern.globalMatch(text);
109
110 while (matchIterator.hasNext())
111 {
112 auto match = matchIterator.next();
113
114 setFormat(
115 match.capturedStart(),
116 match.capturedLength(),
117 syntaxStyle()->getFormat("Type")
118 );
119
120 setFormat(
121 match.capturedStart(2),
122 match.capturedLength(2),
123 syntaxStyle()->getFormat("Function")
124 );
125 }
126 }
127 { // checking for type
128 auto matchIterator = m_defTypePattern.globalMatch(text);
129
130 while (matchIterator.hasNext())
131 {
132 auto match = matchIterator.next();
133
134 setFormat(
135 match.capturedStart(1),
136 match.capturedLength(1),
137 syntaxStyle()->getFormat("Type")
138 );
139 }
140 }
141
142 for (auto& rule : m_highlightRules)
143 {
144 auto matchIterator = rule.pattern.globalMatch(text);
145
146 while (matchIterator.hasNext())
147 {
148 auto match = matchIterator.next();
149
150 setFormat(
151 match.capturedStart(),
152 match.capturedLength(),
153 syntaxStyle()->getFormat(rule.formatName)
154 );
155 }
156 }
157
158 setCurrentBlockState(0);
159 int startIndex = 0;
160 int highlightRuleId = previousBlockState();
161 if (highlightRuleId < 1 || highlightRuleId > m_highlightBlockRules.size()) {
162 for(int i = 0; i < m_highlightBlockRules.size(); ++i) {
163 startIndex = text.indexOf(m_highlightBlockRules.at(i).startPattern);
164 if (startIndex >= 0) {
165 highlightRuleId = i + 1;
166 break;
167 }
168 }
169 }
170
171 while (startIndex >= 0)
172 {
173 const auto &blockRules = m_highlightBlockRules.at(highlightRuleId - 1);
174 auto match = blockRules.endPattern.match(text, startIndex);
175
176 int endIndex = match.capturedStart();
177 int matchLength = 0;
178
179 if (endIndex == -1)
180 {
181 setCurrentBlockState(highlightRuleId);
182 matchLength = text.length() - startIndex;
183 }
184 else
185 {
186 matchLength = endIndex - startIndex + match.capturedLength();
187 }
188
189 setFormat(
190 startIndex,
191 matchLength,
192 syntaxStyle()->getFormat(blockRules.formatName)
193 );
194 startIndex = text.indexOf(blockRules.startPattern, startIndex + matchLength);
195 }
196 }