Skip to content

Commit

Permalink
Highlighter bugs have been fixed.
Browse files Browse the repository at this point in the history
Bugs with quotes and comments in highlighter have been fixed.
  • Loading branch information
Dman95 committed Mar 23, 2015
1 parent d8eba6d commit 1b9cd53
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 43 deletions.
3 changes: 3 additions & 0 deletions assembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@ class Assembler : public QObject //Abstract class. Need to implement to add new
};
struct HighlightingRule
{
HighlightingRule() : isComment(false) {
}
QRegExp pattern; //pattern to highlight
QTextCharFormat format; //highlighting format
bool isComment;
};
bool x86;
explicit Assembler(bool x86, QObject *parent = 0);
Expand Down
20 changes: 12 additions & 8 deletions fasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,22 +473,26 @@ void FASM::fillHighligherRules(QVector<Assembler::HighlightingRule> &highlightin
highlightingRules.append(rule);
}

//comments
rule.pattern = QRegExp(";[^\n]*");
rule.format = commentFormat;
highlightingRules.append(rule);
multiLineComments = false;
commentStartExpression = QRegExp();
commentEndExpression = QRegExp();

//quotations
rule.pattern = QRegExp("\".*\"");
rule.format = quotationFormat;
rule.pattern.setMinimal(true);
highlightingRules.append(rule);

rule.pattern = QRegExp("'.*'");
rule.pattern.setMinimal(true);
highlightingRules.append(rule);

rule.pattern = QRegExp("`.*`");
rule.pattern.setMinimal(true);
highlightingRules.append(rule);

//comments
rule.pattern = QRegExp(";[^\n]*");
rule.format = commentFormat;
rule.isComment = true;
highlightingRules.append(rule);
multiLineComments = false;
commentStartExpression = QRegExp();
commentEndExpression = QRegExp();
}
20 changes: 12 additions & 8 deletions gas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,22 +445,26 @@ void GAS::fillHighligherRules(QVector<Assembler::HighlightingRule> &highlighting
highlightingRules.append(rule);
}

//comments
commentStartExpression = QRegExp("/\\*");
commentEndExpression = QRegExp("\\*/");
rule.format = commentFormat;
rule.pattern = QRegExp("#[^\n]*");
highlightingRules.append(rule);
multiLineComments = true;

//quotations
rule.pattern = QRegExp("\".*\"");
rule.pattern.setMinimal(true);
rule.format = quotationFormat;
highlightingRules.append(rule);

rule.pattern = QRegExp("'.*'");
rule.pattern.setMinimal(true);
highlightingRules.append(rule);

rule.pattern = QRegExp("`.*`");
rule.pattern.setMinimal(true);
highlightingRules.append(rule);

//comments
commentStartExpression = QRegExp("/\\*");
commentEndExpression = QRegExp("[^\\\\]\\*/");
rule.format = commentFormat;
rule.pattern = QRegExp("#[^\n]*");
rule.isComment = true;
highlightingRules.append(rule);
multiLineComments = true;
}
55 changes: 44 additions & 11 deletions highlighter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,35 @@
assembler->fillHighligherRules(highlightingRules, formats, multiLineComments, commentStartExpression, commentEndExpression);
}

void Highlighter::highlightBlock(const QString &text)
{
foreach (const HighlightingRule &rule, highlightingRules) {
QRegExp expression(rule.pattern);
int index = expression.indexIn(text);
while (index >= 0) {
int length = expression.matchedLength();
setFormat(index, length, rule.format);
index = expression.indexIn(text, index + length);
void Highlighter::highlightBlock(const QString &text)
{
foreach (const HighlightingRule &rule, highlightingRules) {
QRegExp expression(rule.pattern);
int index = expression.indexIn(text);
while (index >= 0) {
int length = expression.matchedLength();
if (rule.isComment) {
if (!isCommentInQuote(text, index)) {
setFormat(index, length, rule.format);
index = expression.indexIn(text, index + length);
} else {
index = expression.indexIn(text, index + 1);
}
} else {
setFormat(index, length, rule.format);
index = expression.indexIn(text, index + length);
}
}
}

if (multiLineComments) {
setCurrentBlockState(0);
int startIndex = 0;
if (previousBlockState() != 1)
if (previousBlockState() != 1) {
startIndex = commentStartExpression.indexIn(text);
if (startIndex != -1 && isCommentInQuote(text, startIndex)) {
startIndex = -1;
}
}
while (startIndex >= 0) {
int endIndex = commentEndExpression.indexIn(text, startIndex);
int commentLength;
Expand All @@ -113,6 +125,27 @@
}
}

bool Highlighter::isCommentInQuote(const QString &text, int index)
{
bool inQuote = false;
char quote = 0;
for (int i = 0; i < index; ++i) {
char c = text[i].toLatin1();
switch (c) {
case '"':
case '\'':
case '`':
if (!inQuote) {
inQuote = true;
quote = c;
} else if (quote == c) {
inQuote = false;
}
}
}
return inQuote;
}

Highlighter::~Highlighter()
{
highlightingRules.clear();
Expand Down
2 changes: 2 additions & 0 deletions highlighter.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class Highlighter : public QSyntaxHighlighter
typedef Assembler::HighlightingRule HighlightingRule;
QVector<HighlightingRule> highlightingRules;

bool isCommentInQuote(const QString &text, int index);

QRegExp commentStartExpression;
QRegExp commentEndExpression;
bool multiLineComments;
Expand Down
19 changes: 11 additions & 8 deletions masm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,19 +511,22 @@ void MASM::fillHighligherRules(QVector<Assembler::HighlightingRule> &highlightin
highlightingRules.append(rule);
}

//comments
rule.pattern = QRegExp(";[^\n]*");
rule.format = commentFormat;
highlightingRules.append(rule);
multiLineComments = false;
commentStartExpression = QRegExp();
commentEndExpression = QRegExp();

//quotations
rule.pattern = QRegExp("\".*\"");
rule.format = quotationFormat;
rule.pattern.setMinimal(true);
highlightingRules.append(rule);

rule.pattern = QRegExp("'.*'");
rule.pattern.setMinimal(true);
highlightingRules.append(rule);

//comments
rule.pattern = QRegExp(";[^\n]*");
rule.isComment = true;
rule.format = commentFormat;
highlightingRules.append(rule);
multiLineComments = false;
commentStartExpression = QRegExp();
commentEndExpression = QRegExp();
}
20 changes: 12 additions & 8 deletions nasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,22 +468,26 @@ void NASM::fillHighligherRules(QVector<Assembler::HighlightingRule> &highlightin
highlightingRules.append(rule);
}

//comments
rule.pattern = QRegExp(";[^\n]*");
rule.format = commentFormat;
highlightingRules.append(rule);
multiLineComments = false;
commentStartExpression = QRegExp();
commentEndExpression = QRegExp();

//quotations
rule.pattern = QRegExp("\".*\"");
rule.format = quotationFormat;
rule.pattern.setMinimal(true);
highlightingRules.append(rule);

rule.pattern = QRegExp("'.*'");
rule.pattern.setMinimal(true);
highlightingRules.append(rule);

rule.pattern = QRegExp("`.*`");
rule.pattern.setMinimal(true);
highlightingRules.append(rule);

//comments
rule.pattern = QRegExp(";[^\n]*");
rule.format = commentFormat;
rule.isComment = true;
highlightingRules.append(rule);
multiLineComments = false;
commentStartExpression = QRegExp();
commentEndExpression = QRegExp();
}

0 comments on commit 1b9cd53

Please sign in to comment.