Debian Codegraph @

cpphighlighter.cpp

raw symbols SCIP
1 // File: cpphighlighter.cpp
2 /*
3 Pep8-1 is a virtual machine for writing machine language and assembly
4 language programs.
5
6 Copyright (C) 2009 J. Stanley Warford, Pepperdine University
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include "cpphighlighter.h"
22
23 CppHighlighter::CppHighlighter(QTextDocument *parent)
24 : QSyntaxHighlighter(parent)
25 {
26 HighlightingRule rule;
27
28
29 // functionFormat.setFontItalic(true);
30 functionFormat.setFontWeight(QFont::Bold);
31 functionFormat.setForeground(Qt::darkMagenta);
32 rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=[\\s]*\\()");
33 rule.format = functionFormat;
34 highlightingRules.append(rule);
35
36 declarationFormat.setFontItalic(true);
37 declarationFormat.setForeground(Qt::darkBlue);
38 QStringList declarationPatterns;
39 declarationPatterns << "\\bbool\\b" << "\\bchar\\b" << "\\bconst\\b" << "\\bcase\\b"
40 << "\\benum\\b" << "\\bint\\b" << "\\bnamespace\\b" << "\\bstruct\\b"
41 << "\\busing\\b" << "\\#include\\b" << "\\bvoid\\b";
42 foreach (const QString &pattern, declarationPatterns) {
43 rule.pattern = QRegExp(pattern);
44 rule.format = declarationFormat;
45 highlightingRules.append(rule);
46 }
47
48 keywordFormat.setForeground(Qt::darkBlue);
49 keywordFormat.setFontWeight(QFont::Bold);
50 QStringList keywordPatterns;
51 keywordPatterns << "\\bwhile\\b" << "\\bfor\\b" << "\\bswitch\\b"
52 << "\\bif\\b" << "\\bdo\\b" << "\\bnew\\b"
53 << "\\breturn\\b" << "\\belse\\b";
54 foreach (const QString &pattern, keywordPatterns) {
55 rule.pattern = QRegExp(pattern);
56 rule.format = keywordFormat;
57 highlightingRules.append(rule);
58 }
59
60 classFormat.setFontWeight(QFont::Bold);
61 classFormat.setForeground(Qt::darkMagenta);
62 rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
63 rule.format = classFormat;
64 highlightingRules.append(rule);
65
66 singleLineCommentFormat.setForeground(Qt::darkGreen);
67 rule.pattern = QRegExp("//[^\n]*");
68 rule.format = singleLineCommentFormat;
69 highlightingRules.append(rule);
70
71 multiLineCommentFormat.setForeground(Qt::darkGreen);
72
73 singleQuotationFormat.setForeground(Qt::red);
74 rule.pattern = QRegExp("((\')(?![\'])(([^\'|\\\\]){1}|((\\\\)([\'|b|f|n|r|t|v|\"|\\\\]))|((\\\\)(([x|X])([0-9|A-F|a-f]{2}))))(\'))");
75 rule.format = singleQuotationFormat;
76 highlightingRules.append(rule);
77
78 doubleQuotationFormat.setForeground(Qt::red);
79 rule.pattern = QRegExp("((\")((([^\"|\\\\])|((\\\\)([\'|b|f|n|r|t|v|\"|\\\\]))|((\\\\)(([x|X])([0-9|A-F|a-f]{2}))))*)(\"))");
80 rule.format = doubleQuotationFormat;
81 highlightingRules.append(rule);
82
83 commentStartExpression = QRegExp("/\\*");
84 commentEndExpression = QRegExp("\\*/");
85 }
86
87 void CppHighlighter::highlightBlock(const QString &text)
88 {
89 foreach (const HighlightingRule &rule, highlightingRules) {
90 QRegExp expression(rule.pattern);
91 int index = expression.indexIn(text);
92 while (index >= 0) {
93 int length = expression.matchedLength();
94 setFormat(index, length, rule.format);
95 index = expression.indexIn(text, index + length);
96 }
97 }
98 setCurrentBlockState(0);
99
100 int startIndex = 0;
101 if (previousBlockState() != 1)
102 startIndex = commentStartExpression.indexIn(text);
103
104 while (startIndex >= 0) {
105 int endIndex = commentEndExpression.indexIn(text, startIndex);
106 int commentLength;
107 if (endIndex == -1) {
108 setCurrentBlockState(1);
109 commentLength = text.length() - startIndex;
110 } else {
111 commentLength = endIndex - startIndex
112 + commentEndExpression.matchedLength();
113 }
114 setFormat(startIndex, commentLength, multiLineCommentFormat);
115 startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
116 }
117 }