#include "syntaxhighlighter.h" SyntaxHighlighter::SyntaxHighlighter(QTextDocument *parent) : QSyntaxHighlighter(parent) { HighlightingRule rule; rule.pattern.setCaseSensitivity(Qt::CaseInsensitive); QStringList instructionPatterns; instructionPatterns << "\\bCREATE\\b" << "\\bMOVE\\b" << "\\bDIE\\b" << "\\bTRANS\\b" << "\\bRTRANS\\b" << "\\bTURN\\b" << "\\bJUMP\\b" << "\\bAJUMP\\b" << "\\bBJUMP\\b" << "\\bSCAN\\b" << "\\bFARSCAN\\b" << "\\bSET\\b" << "\\bADD\\b" << "\\bSUB\\b" << "\\bMUL\\b" << "\\bDIV\\b" << "\\bMOD\\b" << "\\bMIN\\b" << "\\bMAX\\b" << "\\bRANDOM\\b" << "\\bIF\\b" << "\\bIFN\\b" << "\\bIFG\\b" << "\\bIFL\\b" << "\\bIFGE\\b" << "\\bIFLE\\b" << "\\bINIT\\b" << "\\bBREAK\\b" << "\\bRESUME\\b" << "\\bSEIZE\\b" << "\\bSLEEP\\b" << "\\bQUIT\\b" ; instructionFormat.setForeground(Qt::darkBlue); instructionFormat.setFontWeight(QFont::Bold); foreach (const QString &pattern, instructionPatterns) { rule.pattern = QRegExp(pattern); rule.pattern.setCaseSensitivity(Qt::CaseInsensitive); rule.format = instructionFormat; highlightingRules.append(rule); } QStringList variablePatterns; variablePatterns << "#1\\b" << "#2\\b" << "#3\\b" << "#4\\b" << "#5\\b" << "#6\\b" << "#7\\b" << "#8\\b" << "#9\\b" << "#10\\b" << "#11\\b" << "#12\\b" << "#13\\b" << "#14\\b" << "#15\\b" << "#16\\b" << "#17\\b" << "#18\\b" << "#19\\b" << "#20\\b" << "#Active\\b" << "\\\\$Banks\\b" << "\\\\$InstrSet\\b" << "\\\\$Mobile\\b" << "\\\\$Age\\b" << "\\\\$Tasks\\b" << "\\\\$Generation\\b" << "\\\\$ID\\b" << "%Active\\b" << "%Banks\\b" << "%InstrSet\\b" << "%Mobile\\b" << "%Age\\b" << "%Tasks\\b" << "%Generation\\b" << "#Pub\\b" << "#Team\\b" << "\\\\$Own\\b" << "\\\\$Others\\b" << "\\\\$Fields\\b" << "\\\\$Time\\b" << "\\\\$Timeout\\b" ; variableFormat.setForeground(Qt::darkRed); variableFormat.setFontWeight(QFont::Normal); variableFormat.setFontItalic(true); foreach (const QString &pattern, variablePatterns) { rule.pattern = QRegExp(pattern); rule.pattern.setCaseSensitivity(Qt::CaseInsensitive); rule.format = variableFormat; highlightingRules.append(rule); } bankFormat.setFontWeight(QFont::Bold); bankFormat.setForeground(Qt::white); bankFormat.setBackground(Qt::darkGray); rule.pattern = QRegExp("\\s*BANK.*"); rule.format = bankFormat; highlightingRules.append(rule); singleLineCommentFormat.setForeground(Qt::darkGreen); rule.pattern = QRegExp("//[^\n]*"); rule.format = singleLineCommentFormat; highlightingRules.append(rule); multiLineCommentFormat.setForeground(Qt::darkGreen); commentStartExpression = QRegExp("/\\*"); commentEndExpression = QRegExp("\\*/"); } void SyntaxHighlighter::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); } } setCurrentBlockState(0); int startIndex = 0; if (previousBlockState() != 1) startIndex = commentStartExpression.indexIn(text); while (startIndex >= 0) { int endIndex = commentEndExpression.indexIn(text, startIndex); int commentLength; if (endIndex == -1) { setCurrentBlockState(1); commentLength = text.length() - startIndex; } else { commentLength = endIndex - startIndex + commentEndExpression.matchedLength(); } setFormat(startIndex, commentLength, multiLineCommentFormat); startIndex = commentStartExpression.indexIn(text, startIndex + commentLength); } }