File indexing completed on 2023-05-30 09:03:10
0001 /* 0002 SPDX-License-Identifier: GPL-2.0-or-later 0003 SPDX-FileCopyrightText: 2009-2012 Alexander Rieder <alexanderrieder@gmail.com> 0004 */ 0005 0006 #include "maximahighlighter.h" 0007 #include "maximakeywords.h" 0008 #include "maximasession.h" 0009 #include "maximavariablemodel.h" 0010 0011 MaximaHighlighter::MaximaHighlighter(QObject* parent, MaximaSession* session) 0012 : Cantor::DefaultHighlighter(parent, session) 0013 { 0014 //addRule(QRegExp("\\b[A-Za-z0-9_]+(?=\\()"), functionFormat()); 0015 0016 //Code highlighting the different keywords 0017 addKeywords(MaximaKeywords::instance()->keywords()); 0018 0019 addRule(QLatin1String("FIXME"), commentFormat()); 0020 addRule(QLatin1String("TODO"), commentFormat()); 0021 0022 addFunctions(MaximaKeywords::instance()->functions()); 0023 addVariables(MaximaKeywords::instance()->variables()); 0024 0025 //addRule(QRegExp("\".*\""), stringFormat()); 0026 //addRule(QRegExp("'.*'"), stringFormat()); 0027 0028 commentStartExpression = QRegularExpression(QStringLiteral("/\\*")); 0029 commentEndExpression = QRegularExpression(QStringLiteral("\\*/")); 0030 } 0031 0032 void MaximaHighlighter::highlightBlock(const QString& text) 0033 { 0034 if (skipHighlighting(text)) 0035 return; 0036 0037 //Do some backend independent highlighting (brackets etc.) 0038 DefaultHighlighter::highlightBlock(text); 0039 0040 setCurrentBlockState(-1); 0041 0042 int commentLevel = 0; 0043 bool inString = false; 0044 int startIndex = -1; 0045 0046 if (previousBlockState() > 0) { 0047 commentLevel = previousBlockState(); 0048 startIndex = 0; 0049 } else if (previousBlockState() < -1) { 0050 inString = true; 0051 startIndex = 0; 0052 } 0053 0054 for (int i = 0; i < text.size(); ++i) { 0055 if (text[i] == QLatin1Char('\\')) { 0056 ++i; // skip the next character 0057 } else if (text[i] == QLatin1Char('"') && commentLevel == 0) { 0058 if (!inString) 0059 startIndex = i; 0060 else 0061 setFormat(startIndex, i - startIndex + 1, stringFormat()); 0062 inString = !inString; 0063 } else if (text.mid(i,2) == QLatin1String("/*") && !inString) { 0064 if (commentLevel == 0) 0065 startIndex = i; 0066 ++commentLevel; 0067 ++i; 0068 } else if (text.mid(i,2) == QLatin1String("*/") && !inString) { 0069 if (commentLevel == 0) { 0070 setFormat(i, 2, errorFormat()); 0071 // undo the --commentLevel below, so we stay at 0 0072 ++commentLevel; 0073 } else if (commentLevel == 1) { 0074 setFormat(startIndex, i - startIndex + 2, commentFormat()); 0075 } 0076 ++i; 0077 --commentLevel; 0078 } 0079 } 0080 0081 if (inString) { 0082 setCurrentBlockState(-2); 0083 setFormat(startIndex, text.size() - startIndex, stringFormat()); 0084 } else if (commentLevel > 0) { 0085 setCurrentBlockState(commentLevel); 0086 setFormat(startIndex, text.size() - startIndex, commentFormat()); 0087 } 0088 } 0089 0090 QString MaximaHighlighter::nonSeparatingCharacters() const 0091 { 0092 return QLatin1String("%"); 0093 }