File: codehighlighter.cpp

package info (click to toggle)
actionaz 3.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,608 kB
  • ctags: 6,390
  • sloc: cpp: 44,713; ansic: 82; xml: 17; makefile: 14; sh: 10
file content (236 lines) | stat: -rw-r--r-- 5,132 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
	Actionaz
	Copyright (C) 2008-2014 Jonathan Mercier-Ganady

	Actionaz is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Actionaz is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program. If not, see <http://www.gnu.org/licenses/>.

	Contact : jmgr@jmgr.info
*/
// This file uses some code lines from the Ofi Labs X2 project
// Copyright (C) 2010 Ariya Hidayat <ariya.hidayat@gmail.com>
// Licensed under GNU/GPLv3

#include "codehighlighter.h"
#include "keywords.h"

#include <QFile>
#include <QDebug>

namespace ActionTools
{
	CodeHighlighter::CodeHighlighter(QTextDocument *parent)
		: QSyntaxHighlighter(parent)
	{
		mFormats[KeywordFormat].setForeground(Qt::darkBlue);
		mFormats[KeywordFormat].setFontWeight(QFont::Bold);

		mFormats[ReservedFormat].setForeground(Qt::red);
		mFormats[ReservedFormat].setFontWeight(QFont::Bold);
		mFormats[ReservedFormat].setFontStrikeOut(true);

		mFormats[CodeObjectsFormat].setForeground(Qt::darkBlue);
		mFormats[CodeObjectsFormat].setFontWeight(QFont::Bold);

		mFormats[OperatorFormat].setForeground(Qt::red);

		mFormats[NumberFormat].setForeground(Qt::darkMagenta);

		mFormats[CommentFormat].setForeground(Qt::darkGreen);

		mFormats[StringFormat].setForeground(Qt::darkRed);

		foreach(const QString &keyword, usedKeywords)
			mUsedKeywords.insert(keyword);

		foreach(const QString &keyword, reservedKeywords)
			mReservedKeywords.insert(keyword);
	}

	void CodeHighlighter::highlightBlock(const QString &text)
	{
		// parsing state
		enum
		{
			Start = 0,
			Number = 1,
			Identifier = 2,
			String = 3,
			Comment = 4,
			Regex = 5
		};

		QList<int> bracketPositions;

		int blockState = previousBlockState();
		int bracketLevel = blockState >> 4;
		int state = blockState & 15;
		if (blockState < 0)
		{
			bracketLevel = 0;
			state = Start;
		}

		int start = 0;
		int i = 0;
		while (i <= text.length())
		{
			QChar ch = (i < text.length()) ? text.at(i) : QChar();
			QChar next = (i < text.length() - 1) ? text.at(i + 1) : QChar();

			switch (state)
			{

			case Start:
				start = i;
				if (ch.isSpace())
				{
					++i;
				}
				else if (ch.isDigit())
				{
					++i;
					state = Number;
				}
				else if (ch.isLetter() || ch == '_')
				{
					++i;
					state = Identifier;
				}
				else if (ch == '\'' || ch == '\"')
				{
					++i;
					state = String;
				}
				else if (ch == '/' && next == '*')
				{
					++i;
					++i;
					state = Comment;
				}
				else if (ch == '/' && next == '/')
				{
					i = text.length();
					setFormat(start, text.length(), mFormats[CommentFormat]);
				}
				else if (ch == '/' && next != '*')
				{
					++i;
					state = Regex;
				}
				else
				{
					if (!QString("(){}[]").contains(ch))
						setFormat(start, 1, mFormats[OperatorFormat]);
					if (ch =='{' || ch == '}')
					{
						bracketPositions += i;
						if (ch == '{')
							bracketLevel++;
						else
							bracketLevel--;
					}
					++i;
					state = Start;
				}
				break;

			case Number:
				if (ch.isSpace() || !ch.isDigit())
				{
					setFormat(start, i - start, mFormats[NumberFormat]);
					state = Start;
				}
				else
					++i;
				break;

			case Identifier:
				if (ch.isSpace() || !(ch.isDigit() || ch.isLetter() || ch == '_'))
				{
					QString token = text.mid(start, i - start).trimmed();
					if (mUsedKeywords.contains(token))
						setFormat(start, i - start, mFormats[KeywordFormat]);
					else if (mReservedKeywords.contains(token))
						setFormat(start, i - start, mFormats[ReservedFormat]);
					else if (mCodeObjects.contains(token))
						setFormat(start, i - start, mFormats[CodeObjectsFormat]);
					state = Start;
				}
				else
					++i;
				break;

			case String:
				if (ch == text.at(start))
				{
					QChar prev = (i > 0) ? text.at(i - 1) : QChar();
					if (prev != '\\')
					{
						++i;
						setFormat(start, i - start, mFormats[StringFormat]);
						state = Start;
					}
					else
						++i;
				}
				else
					++i;
				break;

			case Comment:
				if (ch == '*' && next == '/')
				{
					++i;
					++i;
					setFormat(start, i - start, mFormats[CommentFormat]);
					state = Start;
				}
				else
					++i;
				break;

			case Regex:
				if (ch == '/')
				{
					QChar prev = (i > 0) ? text.at(i - 1) : QChar();
					if (prev != '\\')
					{
						++i;
						setFormat(start, i - start, mFormats[StringFormat]);
						state = Start;
					}
					else
						++i;
				}
				else
					++i;
				break;

			default:
				state = Start;
				break;
			}
		}

		if (state == Comment)
			setFormat(start, text.length(), mFormats[CommentFormat]);
		else
			state = Start;
	}

	void CodeHighlighter::addCodeObject(const QString &name)
	{
		mCodeObjects.insert(name);
	}
}