CppHighlighter.cpp
Go to the documentation of this file.
1/*
2* This file is part of ArmarX.
3*
4* ArmarX is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License version 2 as
6* published by the Free Software Foundation.
7*
8* ArmarX is distributed in the hope that it will be useful, but
9* WITHOUT ANY WARRANTY; without even the implied warranty of
10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11* GNU General Public License for more details.
12*
13* You should have received a copy of the GNU General Public License
14* along with this program. If not, see <http://www.gnu.org/licenses/>.
15*
16* @package ArmarX::
17* @author Mirko Waechter ( mirko.waechter at kit dot edu)
18* @date 2014
19* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20* GNU General Public License
21*/
22
23#include "CppHighlighter.h"
24
25namespace armarx
26{
27
28
29 CppHighlighter::CppHighlighter(QTextDocument* parent) : QSyntaxHighlighter(parent)
30 {
31 HighlightingRule rule;
32
33 keywordFormat.setForeground(Qt::darkBlue);
34 keywordFormat.setFontWeight(QFont::Bold);
35 QStringList keywordPatterns;
36 keywordPatterns << "\\bfor\\b"
37 << "\\bwhile\\b"
38 << "\\bif\\b"
39 << "\\bchar\\b"
40 << "\\bclass\\b"
41 << "\\bconst\\b"
42 << "\\breturn\\b"
43 << "\\bdouble\\b"
44 << "\\benum\\b"
45 << "\\bexplicit\\b"
46 << "\\bfriend\\b"
47 << "\\binline\\b"
48 << "\\bint\\b"
49 << "\\blong\\b"
50 << "\\bnamespace\\b"
51 << "\\boperator\\b"
52 << "\\bprivate\\b"
53 << "\\bprotected\\b"
54 << "\\bpublic\\b"
55 << "\\bshort\\b"
56 << "\\bsignals\\b"
57 << "\\bsigned\\b"
58 << "\\bslots\\b"
59 << "\\bstatic\\b"
60 << "\\bstruct\\b"
61 << "\\btemplate\\b"
62 << "\\btypedef\\b"
63 << "\\btypename\\b"
64 << "\\bunion\\b"
65 << "\\bunsigned\\b"
66 << "\\bvirtual\\b"
67 << "\\bvoid\\b"
68 << "\\bvolatile\\b";
69 foreach (const QString& pattern, keywordPatterns)
70 {
71 rule.pattern = QRegExp(pattern);
72 rule.format = keywordFormat;
73 highlightingRules.append(rule);
74 }
75
76 classFormat.setFontWeight(QFont::Bold);
77 classFormat.setForeground(Qt::darkMagenta);
78 rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
79 rule.format = classFormat;
80 highlightingRules.append(rule);
81
82 singleLineCommentFormat.setForeground(Qt::red);
83 rule.pattern = QRegExp("//[^\n]*");
84 rule.format = singleLineCommentFormat;
85 highlightingRules.append(rule);
86
87 multiLineCommentFormat.setForeground(Qt::red);
88
89 quotationFormat.setForeground(Qt::darkGreen);
90 rule.pattern = QRegExp("\".*\"");
91 rule.format = quotationFormat;
92 highlightingRules.append(rule);
93
94 functionFormat.setFontItalic(true);
95 functionFormat.setForeground(Qt::blue);
96 rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
97 rule.format = functionFormat;
98 highlightingRules.append(rule);
99
100 commentStartExpression = QRegExp("/\\*");
101 commentEndExpression = QRegExp("\\*/");
102 }
103
104 void
106 {
107 foreach (const HighlightingRule& rule, highlightingRules)
108 {
109 QRegExp expression(rule.pattern);
110 int index = expression.indexIn(text);
111
112 while (index >= 0)
113 {
114 int length = expression.matchedLength();
115 setFormat(index, length, rule.format);
116 index = expression.indexIn(text, index + length);
117 }
118 }
119 setCurrentBlockState(0);
120
121 int startIndex = 0;
122
123 if (previousBlockState() != 1)
124 {
125 startIndex = commentStartExpression.indexIn(text);
126 }
127
128 while (startIndex >= 0)
129 {
130 int endIndex = commentEndExpression.indexIn(text, startIndex);
131 int commentLength;
132
133 if (endIndex == -1)
134 {
135 setCurrentBlockState(1);
136 commentLength = text.length() - startIndex;
137 }
138 else
139 {
140 commentLength = endIndex - startIndex + commentEndExpression.matchedLength();
141 }
142
143 setFormat(startIndex, commentLength, multiLineCommentFormat);
144 startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
145 }
146 }
147} // namespace armarx
uint8_t index
void highlightBlock(const QString &text) override
CppHighlighter(QTextDocument *parent)
This file offers overloads of toIce() and fromIce() functions for STL container types.