| 1 | //File: pephighlighter.cpp |
| 2 | /* |
| 3 | Pep8-1 is a virtual machine for writing machine language and assembly |
| 4 | language programs. |
| 5 | |
| 6 | Copyright (C) 2009 J. Stanley Warford, Pepperdine University |
| 7 | |
| 8 | This program is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | */ |
| 21 | #include "pephighlighter.h" |
| 22 | |
| 23 | PepHighlighter::PepHighlighter(QTextDocument *parent) |
| 24 | : QSyntaxHighlighter(parent) |
| 25 | { |
| 26 | HighlightingRule rule; |
| 27 | |
| 28 | oprndFormat.setForeground(Qt::darkBlue); |
| 29 | oprndFormat.setFontWeight(QFont::Bold); |
| 30 | QStringList oprndPatterns; |
| 31 | oprndPatterns << "\\bADD(A|SP|X)\\b" << "\\bAND(A|X)\\b" << "\\bASL(A|X)\\b" |
| 32 | << "\\bASR(A|X)\\b" << "\\bBR\\b" << "\\bBR(C|EQ|GE|GT|LE|LT|NE|V)\\b" |
| 33 | << "\\bCALL\\b" << "\\bCHAR(I|O)\\b" << "\\bCP(A|X)\\b" |
| 34 | << "\\bDEC(I|O)\\b" << "\\bLD(A|X|BYTEA|BYTEX)\\b" << "\\bMOV(FLGA|SPA)\\b" |
| 35 | << "\\bNEG(A|X)\\b" << "\\bNOP\\b" << "\\bNOP(0|1|2|3)\\b" |
| 36 | << "\\bNOT(A|X)\\b" << "\\bOR(A|X)\\b" << "\\bRET(0|1|2|3|4|5|6|7|TR)\\b" |
| 37 | << "\\bROL(A|X)\\b" << "\\bROR(A|X)\\b" << "\\bST(A|X|BYTEA|BYTEX)\\b" << "\\bSTOP\\b" |
| 38 | << "\\bSTRO\\b" << "\\bSTX\\b" << "\\bSUB(A|X|SP)\\b"; |
| 39 | foreach (const QString &pattern, oprndPatterns) { |
| 40 | rule.pattern = QRegExp(pattern); |
| 41 | rule.format = oprndFormat; |
| 42 | highlightingRules.append(rule); |
| 43 | } |
| 44 | |
| 45 | dotFormat.setForeground(Qt::darkBlue); |
| 46 | dotFormat.setFontItalic(true); |
| 47 | QStringList dotPatterns; |
| 48 | dotPatterns << "[\\.]\\bEQUATE\\b" << "[\\.]\\bASCII\\b" << "[\\.]\\bBLOCK\\b" |
| 49 | << "[\\.]\\bBURN\\b" << "[\\.]\\bBYTE\\b" << "[\\.]\\bEND\\b" |
| 50 | << "[\\.]\\bEQUATE\\b" << "[\\.]\\bWORD\\b" << "[\\.]\\bADDRSS\\b"; |
| 51 | foreach (const QString &pattern, dotPatterns) { |
| 52 | rule.pattern = QRegExp(pattern); |
| 53 | rule.format = dotFormat; |
| 54 | highlightingRules.append(rule); |
| 55 | } |
| 56 | |
| 57 | symbolFormat.setFontWeight(QFont::Bold); |
| 58 | symbolFormat.setForeground(Qt::darkMagenta); |
| 59 | rule.pattern = QRegExp("[A-Za-z0-9_]+(?=:)"); |
| 60 | rule.format = symbolFormat; |
| 61 | highlightingRules.append(rule); |
| 62 | |
| 63 | singleLineCommentFormat.setForeground(Qt::darkGreen); |
| 64 | rule.pattern = QRegExp(";.*"); |
| 65 | rule.format = singleLineCommentFormat; |
| 66 | highlightingRules.append(rule); |
| 67 | |
| 68 | multiLineCommentFormat.setForeground(Qt::white); |
| 69 | multiLineCommentFormat.setBackground(Qt::red); |
| 70 | |
| 71 | singleQuotationFormat.setForeground(Qt::red); |
| 72 | rule.pattern = QRegExp("((\')(?![\'])(([^\'|\\\\]){1}|((\\\\)([\'|b|f|n|r|t|v|\"|\\\\]))|((\\\\)(([x|X])([0-9|A-F|a-f]{2}))))(\'))"); |
| 73 | rule.format = singleQuotationFormat; |
| 74 | highlightingRules.append(rule); |
| 75 | |
| 76 | doubleQuotationFormat.setForeground(Qt::red); |
| 77 | rule.pattern = QRegExp("((\")((([^\"|\\\\])|((\\\\)([\'|b|f|n|r|t|v|\"|\\\\]))|((\\\\)(([x|X])([0-9|A-F|a-f]{2}))))*)(\"))"); |
| 78 | rule.format = doubleQuotationFormat; |
| 79 | highlightingRules.append(rule); |
| 80 | |
| 81 | warningFormat.setForeground(Qt::white); |
| 82 | warningFormat.setBackground(Qt::blue); |
| 83 | rule.pattern = QRegExp(";WARNING:[\\s].*$"); |
| 84 | rule.format = warningFormat; |
| 85 | highlightingRules.append(rule); |
| 86 | |
| 87 | commentStartExpression = QRegExp(";ERROR:[\\s]"); |
| 88 | commentEndExpression = QRegExp("$"); |
| 89 | } |
| 90 | |
| 91 | void PepHighlighter::highlightBlock(const QString &text) |
| 92 | { |
| 93 | foreach (const HighlightingRule &rule, highlightingRules) { |
| 94 | QRegExp expression(rule.pattern); |
| 95 | expression.setCaseSensitivity(Qt::CaseInsensitive); |
| 96 | int index = expression.indexIn(text); |
| 97 | while (index >= 0) { |
| 98 | int length = expression.matchedLength(); |
| 99 | setFormat(index, length, rule.format); |
| 100 | index = expression.indexIn(text, index + length); |
| 101 | } |
| 102 | } |
| 103 | setCurrentBlockState(0); |
| 104 | |
| 105 | int startIndex = 0; |
| 106 | if (previousBlockState() != 1) |
| 107 | startIndex = commentStartExpression.indexIn(text); |
| 108 | |
| 109 | while (startIndex >= 0) { |
| 110 | int endIndex = commentEndExpression.indexIn(text, startIndex); |
| 111 | int commentLength; |
| 112 | if (endIndex == -1) { |
| 113 | setCurrentBlockState(1); |
| 114 | commentLength = text.length() - startIndex; |
| 115 | } else { |
| 116 | commentLength = endIndex - startIndex |
| 117 | + commentEndExpression.matchedLength(); |
| 118 | } |
| 119 | setFormat(startIndex, commentLength, multiLineCommentFormat); |
| 120 | startIndex = commentStartExpression.indexIn(text, startIndex + commentLength); |
| 121 | } |
| 122 | } |