-
Notifications
You must be signed in to change notification settings - Fork 1
/
highlighter.cpp
121 lines (101 loc) · 3.18 KB
/
highlighter.cpp
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
#include "highlighter.h"
/*
* Constructor
*/
Highlighter::Highlighter(QTextDocument *parent) : QSyntaxHighlighter(parent)
{
// Initializes a dark theme with no error line
setTheme(THEME_DARK);
lineError=-1;
}
/*
* Sets the theme
*/
void Highlighter::setTheme(int th) {
theme=th;
if(th==THEME_LIGHT) {
commentFormat.setForeground(Qt::darkGreen);
labelFormat.setFontWeight(QFont::Bold);
labelFormat.setForeground(Qt::black);
instructionFormat.setForeground(Qt::darkRed);
}
else if(th==THEME_DARK) {
commentFormat.setForeground(Qt::green);
labelFormat.setFontWeight(QFont::Bold);
labelFormat.setForeground(Qt::white);
instructionFormat.setForeground(Qt::red);
}
}
/*
* Sets an error on a certain line
*/
void Highlighter::setLineError(int lError) {
lineError=lError;
}
/*
* Highlights a QString
* Usually a single row
*/
void Highlighter::highlightBlock(const QString &text) {
int index=0;
int begin;
begin=index;
// Label name (read until space, tab, newline, semicolon or end of string)
while(index<text.length() && (text[index]!=' ' && text[index]!='\t' && text[index]!='\n' && text[index]!=';')) index++;
setFormat(begin,index-begin,labelFormat);
// We have reached the end of the text
if(index==text.length())
return;
// We have reached a new line
if(text[index]=='\n') {
index++;
return;
}
// We have reached a comment
if(text[index]==';') {
begin=index;
// Highlight until we reach end of string or a new line
while(index<text.length() && (text[index]!='\n')) index++;
setFormat(begin,index-begin,commentFormat);
return;
}
// Instruction name (read until space, tab or end of string)
while(index<text.length() && (text[index]==' ' || text[index]=='\t')) index++;
// Instruction arguments (read until space, tab, new line, semicolon or end of string)
begin=index;
while(index<text.length() && (text[index]!=' ' && text[index]!='\t' && text[index]!='\n' && text[index]!=';')) index++;
setFormat(begin,index-begin,instructionFormat);
// We have reached end of string
if(index==text.length())
return;
// We have reached end of line
if(text[index]=='\n') {
return;
}
// We have reached a comment
if(text[index]==';') {
begin=index;
// Highlight until we reach end of string or new line
while(index<text.length() && text[index]!='\n') index++;
setFormat(begin,index-begin,commentFormat);
return;
}
// If there is a comment separated by empty spaces
while(index<text.length() && (text[index]!=';' && text[index]!='\n')) index++;
// We have reached end of string
if(index==text.length())
return;
// We have reached end of line
if(text[index]=='\n') {
index++;
return;
}
// We have reached a comment
if(text[index]==';') {
begin=index;
// Highlight until end of line or end of string
while(index<text.length() && text[index]!='\n') index++;
setFormat(begin,index-begin,commentFormat);
return;
}
}