1
1
#include " Syntax.h"
2
+ #include < QColor>
3
+ #include < QFont>
2
4
3
- Syntax::Syntax (QTextDocument *parent) : QSyntaxHighlighter(parent)
5
+ Syntax::Syntax (QTextDocument *parent)
6
+ : QSyntaxHighlighter(parent)
4
7
{
5
- keywordFormat.setForeground (Qt::blue);
6
- keywordFormat.setFontWeight (QFont::Bold);
7
- QStringList keywordPatterns;
8
- keywordPatterns << " \\ bchar\\ b" << " \\ bclass\\ b" << " \\ bconst\\ b"
9
- << " \\ bdouble\\ b" << " \\ benum\\ b" << " \\ bexplicit\\ b"
10
- << " \\ bfriend\\ b" << " \\ binline\\ b" << " \\ bint\\ b"
11
- << " \\ blong\\ b" << " \\ bnamespace\\ b" << " \\ boperator\\ b"
12
- << " \\ bprivate\\ b" << " \\ bprotected\\ b" << " \\ bpublic\\ b"
13
- << " \\ bshort\\ b" << " \\ bsignals\\ b" << " \\ bsigned\\ b"
14
- << " \\ bslots\\ b" << " \\ bstatic\\ b" << " \\ bstruct\\ b"
15
- << " \\ btemplate\\ b" << " \\ btypedef\\ b" << " \\ btypename\\ b"
16
- << " \\ bunion\\ b" << " \\ bunsigned\\ b" << " \\ bvirtual\\ b"
17
- << " \\ bvoid\\ b" << " \\ bvolatile\\ b" << " \\ bforeach\\ b" ;
18
- foreach (const QString &pattern, keywordPatterns)
19
- {
20
- addPattern (pattern, keywordFormat);
21
- }
8
+ initKeywordRules ();
9
+ initFunctionRules ();
10
+ initParenthesisRules ();
11
+ initCharRules ();
22
12
23
- // Single line comment format expression
24
- singleLineCommentFormat.setForeground (Qt::darkGray);
25
- addPattern (" //[^\n ]*" , singleLineCommentFormat);
13
+ // Keep these two calls are at the end
14
+ // to correctly apply the rules for strings and comments
15
+ initCommentRules ();
16
+ initQuotationRules ();
17
+ }
18
+
19
+ void Syntax::initKeywordRules ()
20
+ {
21
+ // Keyword format
22
+ keywordFormat.setForeground (Qt::blue);
23
+ keywordFormat.setFontWeight (QFont::Bold);
24
+
25
+ QStringList keywordPatterns = {
26
+ " \\ bchar\\ b" , " \\ bclass\\ b" , " \\ bconst\\ b" ,
27
+ " \\ bdouble\\ b" , " \\ benum\\ b" , " \\ bexplicit\\ b" ,
28
+ " \\ bfriend\\ b" , " \\ binline\\ b" , " \\ bint\\ b" ,
29
+ " \\ blong\\ b" , " \\ bnamespace\\ b" , " \\ boperator\\ b" ,
30
+ " \\ bprivate\\ b" , " \\ bprotected\\ b" , " \\ bpublic\\ b" ,
31
+ " \\ bshort\\ b" , " \\ bsignals\\ b" , " \\ bsigned\\ b" ,
32
+ " \\ bslots\\ b" , " \\ bstatic\\ b" , " \\ bstruct\\ b" ,
33
+ " \\ btemplate\\ b" , " \\ btypedef\\ b" , " \\ btypename\\ b" ,
34
+ " \\ bunion\\ b" , " \\ bunsigned\\ b" , " \\ bvirtual\\ b" ,
35
+ " \\ bvoid\\ b" , " \\ bvolatile\\ b" , " \\ bforeach\\ b" };
36
+
37
+ for (const QString &pattern : keywordPatterns)
38
+ {
39
+ addPattern (pattern, keywordFormat);
40
+ }
26
41
27
- // Double quotation mark for string
28
- quotationMark.setForeground (Qt::darkGreen);
29
- addPattern (" \" .*\" " , quotationMark);
42
+ iterationFormat.setForeground (Qt::darkMagenta);
43
+ iterationFormat.setFontWeight (QFont::Bold);
44
+ QStringList iterationPatterns = {
45
+ " \\ bfor\\ b" , " \\ bwhile\\ b" , " \\ bdo\\ b" , " \\ bif\\ b" , " \\ belse\\ b" };
46
+ for (const QString &pattern : iterationPatterns)
47
+ {
48
+ addPattern (pattern, iterationFormat);
49
+ }
50
+ }
30
51
31
- // Function format expression
32
- functionFormat.setFontItalic (true );
33
- functionFormat.setForeground (Qt::darkYellow);
34
- addPattern (" \\ b[a-zA-Z_][a-zA-Z0-9_]*(?=\\ s*\\ ()" , functionFormat);
52
+ void Syntax::initCommentRules ()
53
+ {
54
+ // Single line comment format expression
55
+ singleLineCommentFormat.setForeground (Qt::darkGray);
56
+ addPattern (" //[^\n ]*" , singleLineCommentFormat);
35
57
36
- // Color pattern for parenthesis
37
- QColor parenthesisColor (" #6495ED" );
38
- parenthesisFormat.setForeground (parenthesisColor);
39
- addPattern (" [()]" , parenthesisFormat);
58
+ // TO-DO: Add multi-line comment support
59
+ }
40
60
41
- // Regex for single character format 'a', '\n', etc
42
- charFormat.setForeground (Qt::darkCyan);
43
- addPattern (" '(\\\\ .|[^'])'" , charFormat);
61
+ void Syntax::initQuotationRules ()
62
+ {
63
+ // Double quotation mark for string
64
+ quotationMark.setForeground (Qt::darkGreen);
65
+ addPattern (" \" (\\\\ .|[^\"\\\\ ])*\" " , quotationMark);
66
+
67
+ // TO-DO: Add single quotation mark for character
68
+ }
69
+
70
+ void Syntax::initFunctionRules ()
71
+ {
72
+ // Function format expression
73
+ functionFormat.setFontItalic (true );
74
+ functionFormat.setForeground (Qt::darkYellow);
75
+ addPattern (" \\ b(?!for\\ b|if\\ b|else\\ b|while\\ b|do\\ b)[a-zA-Z_][a-zA-Z0-9_]*(?=\\ s*\\ ()" , functionFormat);
76
+ }
77
+
78
+ void Syntax::initParenthesisRules ()
79
+ {
80
+ // Color pattern for parenthesis
81
+ QColor parenthesisColor (" #6495ED" );
82
+ parenthesisFormat.setForeground (parenthesisColor);
83
+ addPattern (" [()]" , parenthesisFormat);
84
+ }
85
+
86
+ // Regex for single character format 'a', '\n', etc.
87
+ void Syntax::initCharRules ()
88
+ {
89
+ charFormat.setForeground (Qt::darkCyan);
90
+ addPattern (" '(\\\\ .|[^'])'" , charFormat);
44
91
}
45
92
46
- // Add syntax highlighting patterns
47
93
void Syntax::addPattern (const QString &pattern, const QTextCharFormat &format)
48
94
{
49
95
SyntaxRule rule;
@@ -54,13 +100,13 @@ void Syntax::addPattern(const QString &pattern, const QTextCharFormat &format)
54
100
55
101
void Syntax::highlightBlock (const QString &text)
56
102
{
57
- foreach (const SyntaxRule &rule, syntaxRules)
58
- {
59
- QRegularExpressionMatchIterator matchIterator = rule.pattern .globalMatch (text);
60
- while (matchIterator.hasNext ())
103
+ for (const SyntaxRule &rule : syntaxRules)
61
104
{
62
- QRegularExpressionMatch match = matchIterator.next ();
63
- setFormat (match.capturedStart (), match.capturedLength (), rule.format );
105
+ QRegularExpressionMatchIterator matchIterator = rule.pattern .globalMatch (text);
106
+ while (matchIterator.hasNext ())
107
+ {
108
+ QRegularExpressionMatch match = matchIterator.next ();
109
+ setFormat (match.capturedStart (), match.capturedLength (), rule.format );
110
+ }
64
111
}
65
- }
66
112
}
0 commit comments