-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaxsyntaxhighlighter.cpp
More file actions
executable file
·125 lines (106 loc) · 3.9 KB
/
axsyntaxhighlighter.cpp
File metadata and controls
executable file
·125 lines (106 loc) · 3.9 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
#include <QSyntaxHighlighter>
#include <QString>
#include "axsyntaxhighlighter.h"
AXSyntaxHighlighter::AXSyntaxHighlighter(QTextDocument *parent) :
QSyntaxHighlighter(parent)
{
HighlightingRule rule;
// Setting up regex recognition for language keywords
keywordFormat.setForeground(Qt::darkBlue);
keywordFormat.setFontWeight(QFont::Bold);
const QString keywordPatterns[] = {
QStringLiteral("\\bvariable\\b"),
QStringLiteral("\\bparameter\\b"),
QStringLiteral("\\bdeviation\\b"),
QStringLiteral("\\bdefault\\b"),
QStringLiteral("\\bwarning\\b"),
QStringLiteral("\\balarm\\b"),
QStringLiteral("\\buntil\\b"),
QStringLiteral("\\bin\\b"),
QStringLiteral("\\bto\\b"),
QStringLiteral("\\bwith\\b"),
QStringLiteral("\\bfollow\\b"),
QStringLiteral("\\bbegin\\b"),
QStringLiteral("\\bend\\b"),
QStringLiteral("\\bstat\\b"),
QStringLiteral("\\bloop\\b"),
QStringLiteral("\\bopen\\b"),
QStringLiteral("\\bclose\\b"),
QStringLiteral("\\bon\\b"),
QStringLiteral("\\boff\\b"),
// Python keywords
QStringLiteral("\\btry\\b"),
QStringLiteral("\\bexcept\\b"),
QStringLiteral("\\bif\\b"),
QStringLiteral("\\belse\\b"),
QStringLiteral("\\belif\\b"),
QStringLiteral("\\bwhile\\b"),
QStringLiteral("\\bclass\\b")
};
for (const QString &pattern : keywordPatterns) {
rule.pattern = QRegularExpression(pattern);
rule.format = keywordFormat;
highlightingRules.append(rule);
}
// Setting up regex recognition for macro definitions. Example:
//
// MacroName { ... }
//
macroFormat.setForeground(Qt::darkYellow);
macroFormat.setFontWeight(QFont::Bold);
const QString macroPatterns[] = {
QStringLiteral("\\w+\\s*{"),
QStringLiteral("}")
};
for (const QString &pattern : macroPatterns) {
rule.pattern = QRegularExpression(pattern);
rule.format = macroFormat;
highlightingRules.append(rule);
}
// Setting up regex recognition for wait calls. Example:
//
// 10
//
// [Waiting_Period ]
//
waitFormat.setFontWeight(QFont::Bold);
rule.pattern = QRegularExpression(QStringLiteral("\\A\\s*\\d+"));
rule.format = waitFormat;
highlightingRules.append(rule);
rule.pattern = QRegularExpression(QStringLiteral("[[]\\s*\\w*\\s*[]]"));
highlightingRules.append(rule);
// Setting up regex recognition for print calls. Example:
//
// "Setting the temperature to 150 degrees"
//
quotationFormat.setForeground(Qt::darkGreen);
rule.pattern = QRegularExpression(QStringLiteral("\".*\""));
rule.format = quotationFormat;
highlightingRules.append(rule);
// Setting up regex recognition for code comments. Example:
//
// # This is a comment
//
singleLineCommentFormat.setForeground(Qt::red);
rule.pattern = QRegularExpression(QStringLiteral("#[^\n]*"));
rule.format = singleLineCommentFormat;
highlightingRules.append(rule);
// Setting up regex recognition for read calls. Example:
//
// read Macros.txt
//
readFormat.setForeground(Qt::darkMagenta);
rule.pattern = QRegularExpression("\\b[rR][eE][aA][dD]\\s*\\w*");
rule.format = readFormat;
highlightingRules.append(rule);
}
void AXSyntaxHighlighter::highlightBlock(const QString &text)
{
for (const HighlightingRule &rule : highlightingRules) {
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
while (matchIterator.hasNext()) {
QRegularExpressionMatch match = matchIterator.next();
setFormat(match.capturedStart(), match.capturedLength(), rule.format);
}
}
}