-
Notifications
You must be signed in to change notification settings - Fork 4
/
highlighter.cpp
executable file
·130 lines (117 loc) · 4.89 KB
/
highlighter.cpp
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
#include "highlighter.h"
Highlighter::Highlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
HighlightingRule rule;
keywordFormat.setForeground(Qt::darkBlue);
keywordFormat.setFontWeight(QFont::Bold);
const QString keywordPatterns[] = {
QStringLiteral("\\bmdp\\b"),
QStringLiteral("\\bnondeterministic\\b"),
QStringLiteral("\\bdtmc\\b"),
QStringLiteral("\\bprobabilistic\\b"),
QStringLiteral("\\bqmc\\b"),
QStringLiteral("\\bctmc\\b"),
QStringLiteral("\\bstochastic\\b"),
QStringLiteral("\\bpta\\b"),
QStringLiteral("\\bconst\\b"),
QStringLiteral("\\bint\\b"),
QStringLiteral("\\bdouble\\b"),
QStringLiteral("\\bbool\\b"),
QStringLiteral("\\brate\\b"),
QStringLiteral("\\bprob\\b"),
QStringLiteral("\\bvector\\b"),
QStringLiteral("\\bmatrix\\b"),
QStringLiteral("\\bsuperoperator\\b"),
QStringLiteral("\\blabel\\b"),
QStringLiteral("\\bglobal\\b"),
QStringLiteral("\\bformula\\b"),
QStringLiteral("\\bmodule\\b"),
QStringLiteral("\\bendmodule\\b"),
QStringLiteral("\\binit\\b"),
QStringLiteral("\\bclock\\b"),
QStringLiteral("\\binvariant\\b"),
QStringLiteral("\\bendinvariant\\b"),
QStringLiteral("\\btrue\\b"),
QStringLiteral("\\brewards\\b"),
QStringLiteral("\\bfalse\\b"),
QStringLiteral("\\bendrewards\\b"),
QStringLiteral("\\bendinit\\b"),
QStringLiteral("\\bsystem\\b"),
QStringLiteral("\\bendsystem\\b")
};
for (const QString &pattern : keywordPatterns) {
rule.pattern = QRegularExpression(pattern);
rule.format = keywordFormat;
highlightingRules.append(rule);
}
/*assertFormat.setFontItalic(true);
assertFormat.setForeground(Qt::blue);
const QString assertPatterns[] = {
QStringLiteral("\\bbvAssert\\b"), QStringLiteral("\\bbvAssume\\b")
};
for (const QString &pattern : assertPatterns) {
rule.pattern = QRegularExpression(pattern);
rule.format = assertFormat;
highlightingRules.append(rule);
}*/
/*compareFormat.setFontWeight(QFont::Bold);
compareFormat.setForeground(Qt::green);
const QString comparePatterns[] = {
QStringLiteral("\\bbveands\\b"), QStringLiteral("\\bbveEqMod\\b"), QStringLiteral("\\bbvrands\\b"), QStringLiteral("\\bbveEq\\b"), QStringLiteral("\\bbvrEq\\b")
, QStringLiteral("\\bbveTrue\\b"), QStringLiteral("\\bbvrTrue\\b"), QStringLiteral("\\bbvTrue\\b")
};
for (const QString &pattern : comparePatterns) {
rule.pattern = QRegularExpression(pattern);
rule.format = compareFormat;
highlightingRules.append(rule);
}*/
/*otherFormat.setForeground(Qt::darkMagenta);
const QString otherPatterns[] = {
QStringLiteral("\\blimbs\\b"), QStringLiteral("\\bbvconst\\b"), QStringLiteral("\\bbvvar\\b"), QStringLiteral("proc"), QStringLiteral("\\bbvVar\\b")
};
for (const QString &pattern : otherPatterns) {
rule.pattern = QRegularExpression(pattern);
rule.format = otherFormat;
highlightingRules.append(rule);
}*/
quotationFormat.setForeground(Qt::darkGreen);//string
rule.pattern = QRegularExpression("\".*\"");
rule.format = quotationFormat;
highlightingRules.append(rule);
singleLineCommentFormat.setForeground(Qt::gray);//single line comment
rule.pattern = QRegularExpression("//[^\n]*");
rule.format = singleLineCommentFormat;
highlightingRules.append(rule);
multiLineCommentFormat.setForeground(Qt::gray);
commentStartExpression = QRegularExpression(QStringLiteral("/\\*"));
commentEndExpression = QRegularExpression(QStringLiteral("\\*/"));
}
void Highlighter::highlightBlock(const QString &text)
{
for (const HighlightingRule &rule : qAsConst(highlightingRules)) {
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
while (matchIterator.hasNext()) {
QRegularExpressionMatch match = matchIterator.next();
setFormat(match.capturedStart(), match.capturedLength(), rule.format);
}
}
setCurrentBlockState(0);
int startIndex = 0;
if (previousBlockState() != 1)
startIndex = text.indexOf(commentStartExpression);
while (startIndex >= 0) {
QRegularExpressionMatch match = commentEndExpression.match(text, startIndex);
int endIndex = match.capturedStart();
int commentLength = 0;
if (endIndex == -1) {
setCurrentBlockState(1);
commentLength = text.length() - startIndex;
} else {
commentLength = endIndex - startIndex
+ match.capturedLength();
}
setFormat(startIndex, commentLength, multiLineCommentFormat);
startIndex = text.indexOf(commentStartExpression, startIndex + commentLength);
}
}