Debian Codegraph @

highlighter.cpp

raw symbols SCIP
1 /****************************************************************************
2 ** SASM - simple IDE for assembler development
3 ** Copyright (C) 2013 Dmitriy Manushin
4 ** Contact: site: http://dman95.github.io/SASM/
5 ** e-mail: [email protected]
6 **
7 ** This file is part of SASM.
8 **
9 ** SASM is free software: you can redistribute it and/or modify
10 ** it under the terms of the GNU General Public License as published by
11 ** the Free Software Foundation, either version 3 of the License, or
12 ** (at your option) any later version.
13 **
14 ** SASM is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ** GNU General Public License for more details.
18 **
19 ** You should have received a copy of the GNU General Public License
20 ** along with SASM. If not, see <http://www.gnu.org/licenses/>.
21 **
22 ** Этот файл — часть SASM.
23 **
24 ** SASM - свободная программа: вы можете перераспространять ее и/или
25 ** изменять ее на условиях Стандартной общественной лицензии GNU в том виде,
26 ** в каком она была опубликована Фондом свободного программного обеспечения;
27 ** либо версии 3 лицензии, либо (по вашему выбору) любой более поздней
28 ** версии.
29 **
30 ** SASM распространяется в надежде, что она будет полезной,
31 ** но БЕЗО ВСЯКИХ ГАРАНТИЙ; даже без неявной гарантии ТОВАРНОГО ВИДА
32 ** или ПРИГОДНОСТИ ДЛЯ ОПРЕДЕЛЕННЫХ ЦЕЛЕЙ. Подробнее см. в Стандартной
33 ** общественной лицензии GNU.
34 **
35 ** Вы должны были получить копию Стандартной общественной лицензии GNU
36 ** вместе с этой программой. Если это не так, см.
37 ** <http://www.gnu.org/licenses/>.)
38 **
39 ****************************************************************************/
40
41 #include "highlighter.h"
42
43 /**
44 * @file highlighter.cpp
45 * The code highlighting features are defined here.
46 */
47
48 Highlighter::Highlighter(Assembler *assembler, QTextDocument *parent)
49 : QSyntaxHighlighter(parent)
50 {
51 QTextCharFormat keywordFormat;
52 QTextCharFormat quotationFormat;
53 QTextCharFormat registerFormat;
54 QTextCharFormat labelFormat;
55 QTextCharFormat labelWithDotFormat;
56 QTextCharFormat memoryFormat;
57 QTextCharFormat systemFormat;
58 QTextCharFormat numberFormat;
59 QTextCharFormat iomacrosFormat;
60 QList<QColor> defaultColors;
61 QList<QString> names;
62 QList<QTextCharFormat *> formats;
63 //setting up text formats
64 names << "keywords" << "registers" << "numbers" << "memory" <<
65 "labels" << "comments" << "system" << "iomacro" <<
66 "quotation";
67 defaultColors << QColor(Qt::blue) << QColor(153, 0, 204) << //according to colorNames
68 QColor(255, 122, 0) << QColor(0, 128, 255) <<
69 QColor(128, 0, 0) << QColor(Qt::darkGreen) <<
70 QColor(Qt::darkCyan) << QColor(Qt::blue) <<
71 QColor(128, 128, 128);
72 formats << &keywordFormat << &registerFormat << &numberFormat << &memoryFormat <<
73 &labelFormat << &commentFormat << &systemFormat << &iomacrosFormat <<
74 &quotationFormat;
75 QSettings settings("SASM Project", "SASM");
76 for (int i = 0; i < formats.size(); i++) {
77 formats[i]->setForeground(settings.value(names[i] + "color", defaultColors[i]).value<QColor>());
78 formats[i]->setBackground(settings.value(names[i] + "colorbg", QColor(255, 255, 255)).value<QColor>());
79 if (settings.value(names[i] + "bold", (i == 0) ? true : false).toBool())
80 formats[i]->setFontWeight(QFont::Bold);
81 formats[i]->setFontItalic(settings.value(names[i] + "italic", false).toBool());
82 }
83 labelWithDotFormat.setForeground(settings.value("fontcolor", QColor(30, 30, 30)).value<QColor>());
84 formats << &labelWithDotFormat;
85 assembler->fillHighligherRules(highlightingRules, formats, multiLineComments, commentStartExpression, commentEndExpression);
86 }
87
88 void Highlighter::highlightBlock(const QString &text)
89 {
90 foreach (const HighlightingRule &rule, highlightingRules) {
91 QRegExp expression(rule.pattern);
92 int index = expression.indexIn(text);
93 while (index >= 0) {
94 int length = expression.matchedLength();
95 if (rule.isComment) {
96 if (!isCommentInQuote(text, index)) {
97 setFormat(index, length, rule.format);
98 index = expression.indexIn(text, index + length);
99 } else {
100 index = expression.indexIn(text, index + 1);
101 }
102 } else {
103 setFormat(index, length, rule.format);
104 index = expression.indexIn(text, index + length);
105 }
106 }
107 }
108 if (multiLineComments) {
109 setCurrentBlockState(0);
110 int startIndex = 0;
111 if (previousBlockState() != 1) {
112 startIndex = commentStartExpression.indexIn(text);
113 if (startIndex != -1 && isCommentInQuote(text, startIndex)) {
114 startIndex = -1;
115 }
116 }
117 while (startIndex >= 0) {
118 int endIndex = commentEndExpression.indexIn(text, startIndex);
119 int commentLength;
120 if (endIndex == -1) {
121 setCurrentBlockState(1);
122 commentLength = text.length() - startIndex;
123 } else {
124 commentLength = endIndex - startIndex
125 + commentEndExpression.matchedLength();
126 }
127 setFormat(startIndex, commentLength, commentFormat);
128 startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
129 }
130 }
131 }
132
133 bool Highlighter::isCommentInQuote(const QString &text, int index)
134 {
135 bool inQuote = false;
136 char quote = 0;
137 for (int i = 0; i < index; ++i) {
138 char c = text[i].toLatin1();
139 switch (c) {
140 case '"':
141 case '\'':
142 case '`':
143 if (!inQuote) {
144 inQuote = true;
145 quote = c;
146 } else if (quote == c) {
147 inQuote = false;
148 }
149 }
150 }
151 return inQuote;
152 }
153
154 Highlighter::~Highlighter()
155 {
156 highlightingRules.clear();
157 }