Skip to content

Commit 5a71fd9

Browse files
committed
[Update] Added more regex pattern
1 parent 8a74fe9 commit 5a71fd9

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

include/Syntax.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ class Syntax : public QSyntaxHighlighter
2626

2727
QTextCharFormat keywordFormat;
2828
QTextCharFormat singleLineCommentFormat;
29+
QTextCharFormat quotationMark;
30+
QTextCharFormat functionFormat;
31+
QTextCharFormat parenthesisFormat;
32+
QTextCharFormat charFormat;
2933
};
3034

3135
#endif // SYNTAX_H

src/Syntax.cpp

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Syntax::Syntax(QTextDocument *parent) : QSyntaxHighlighter(parent)
44
{
55
SyntaxRule rule;
66

7-
keywordFormat.setForeground(Qt::darkBlue);
7+
keywordFormat.setForeground(Qt::blue);
88
keywordFormat.setFontWeight(QFont::Bold);
99
QStringList keywordPatterns;
1010
keywordPatterns << "\\bchar\\b" << "\\bclass\\b" << "\\bconst\\b"
@@ -16,7 +16,7 @@ Syntax::Syntax(QTextDocument *parent) : QSyntaxHighlighter(parent)
1616
<< "\\bslots\\b" << "\\bstatic\\b" << "\\bstruct\\b"
1717
<< "\\btemplate\\b" << "\\btypedef\\b" << "\\btypename\\b"
1818
<< "\\bunion\\b" << "\\bunsigned\\b" << "\\bvirtual\\b"
19-
<< "\\bvoid\\b" << "\\bvolatile\\b";
19+
<< "\\bvoid\\b" << "\\bvolatile\\b" << "\\bforeach\\b";
2020
foreach (const QString &pattern, keywordPatterns)
2121
{
2222
rule.pattern = QRegularExpression(pattern);
@@ -25,10 +25,36 @@ Syntax::Syntax(QTextDocument *parent) : QSyntaxHighlighter(parent)
2525
}
2626

2727
// Single line comment format expression
28-
singleLineCommentFormat.setForeground(Qt::red);
28+
singleLineCommentFormat.setForeground(Qt::darkGray);
2929
rule.pattern = QRegularExpression(QStringLiteral("//[^\n]*"));
3030
rule.format = singleLineCommentFormat;
3131
syntaxRules.append(rule);
32+
33+
// Double quotation mark for string
34+
quotationMark.setForeground(Qt::darkGreen);
35+
rule.pattern = QRegularExpression(QStringLiteral("\".*\""));
36+
rule.format = quotationMark;
37+
syntaxRules.append(rule);
38+
39+
// Function format expression
40+
functionFormat.setFontItalic(true);
41+
functionFormat.setForeground(Qt::darkYellow);
42+
rule.pattern = QRegularExpression(QStringLiteral("\\b[a-zA-Z_][a-zA-Z0-9_]*(?=\\s*\\()"));
43+
rule.format = functionFormat;
44+
syntaxRules.append(rule);
45+
46+
// Color pattern for parenthesis
47+
QColor parenthesisColor("#6495ED");
48+
parenthesisFormat.setForeground(parenthesisColor);
49+
rule.pattern = QRegularExpression(QStringLiteral("[()]"));
50+
rule.format = parenthesisFormat;
51+
syntaxRules.append(rule);
52+
53+
// Regex for single character format 'a', '\n', etc
54+
charFormat.setForeground(Qt::darkCyan);
55+
rule.pattern = QRegularExpression(QStringLiteral("'(\\\\.|[^'])'"));
56+
rule.format = charFormat;
57+
syntaxRules.append(rule);
3258
}
3359

3460
void Syntax::highlightBlock(const QString &text)

0 commit comments

Comments
 (0)