QSS Solver GUI  4.5.3
mmohighlight.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2 
3  This file is part of QSS Solver.
4 
5  QSS Solver is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  QSS Solver is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QSS Solver. If not, see <http://www.gnu.org/licenses/>.
17 
18  ******************************************************************************/
19 
20 #include <QtGui>
21 
22 #include "mmohighlight.hpp"
23 
24 // Initialize static const QStringLists
25 const QStringList MmoHighlighter::_model_keywords = {"AbsTolerance",
26  "algorithm",
27  "and",
28  "bin",
29  "Boolean",
30  "constant",
31  "derivative",
32  "discrete",
33  "each",
34  "else",
35  "elseif",
36  "elsewhen",
37  "end",
38  "equation",
39  "external",
40  "for",
41  "function",
42  "if",
43  "import",
44  "Include",
45  "IncludeDirectory",
46  "initial",
47  "input",
48  "Integer",
49  "Jacobian",
50  "Library",
51  "LibraryDirectory",
52  "loop",
53  "MMO_BDF_Max_Step",
54  "MMO_BDF_Part",
55  "MMO_BDF_PDepth",
56  "MMO_CVODEMaxOrder",
57  "MMO_DebugGraph",
58  "MMO_DerDelta",
59  "MMO_Description",
60  "MMO_DH",
61  "MMO_DT_Min",
62  "MMO_DT_Synch",
63  "MMO_Event_Id",
64  "MMO_GenerateArch",
65  "MMO_HD",
66  "MMO_HH",
67  "MMO_HZ",
68  "MMO_Imbalance",
69  "MMO_LHS_DSC",
70  "MMO_LHS_ST",
71  "MMO_LPS",
72  "MMO_MetisSettings",
73  "MMO_MinStep",
74  "MMO_NodeSize",
75  "MMO_Output",
76  "MMO_OutputType",
77  "MMO_Parallel",
78  "MMO_PartitionMethod",
79  "MMO_PatohSettings",
80  "MMO_Period",
81  "MMO_RandomSeed",
82  "MMO_ReorderPartition",
83  "MMO_RHS_ST",
84  "MMO_Scheduler",
85  "MMO_ScotchSettings",
86  "MMO_SD",
87  "MMO_SH",
88  "MMO_Solver",
89  "MMO_StoreData",
90  "MMO_SymDiff",
91  "MMO_SZ",
92  "MMO_Weight",
93  "MMO_XOutput",
94  "MMO_ZCHyst",
95  "model",
96  "not",
97  "or",
98  "output",
99  "package",
100  "parameter",
101  "protected",
102  "Real",
103  "return",
104  "start",
105  "StartTime",
106  "StopTime",
107  "then",
108  "time",
109  "Tolerance",
110  "when"};
111 
112 const QStringList MmoHighlighter::_model_functions = {"abs", "acos", "asin", "atan", "atan2", "ceil", "conh", "cos", "der",
113  "div", "exp", "floor", "log", "log10", "max", "min", "mod", "pre",
114  "product", "reinit", "sin", "sinh", "sum", "tan", "tanh", "terminate"};
115 
116 const QStringList MmoHighlighter::_model_annotations = {"annotation", "experiment"};
117 
118 const QStringList MmoHighlighter::_model_qss_tags = {
119  "CI_Dense", "CI_Sampled", "CI_Step", "CQSS", "CVODE_AM", "CVODE_BDF", "DASSL", "Dense", "DOPRI", "false",
120  "HMetis", "IDA", "KaHIP", "LIQSS", "LIQSS2", "LIQSS3", "LIQSS_BDF", "Manual", "Metis", "mLIQSS",
121  "mLIQSS2", "MTPL", "MTPL_IT", "Patoh", "QSS", "QSS2", "QSS3", "QSS4", "Scotch", "SD_DT_Asynchronous",
122  "SD_DT_Fixed", "Sparse", "ST_Binary", "ST_Linear", "ST_Random", "true"};
123 
124 const QStringList MmoHighlighter::_log_keywords = {"-", "Additional", "Allocated", "allocated", "Average",
125  "Begin", "by", "changes", "cost", "CPU",
126  "crossing", "Crossings", "DASSL", "data", "dependencies",
127  "derivative", "derivatives", "detected", "done", "Dt",
128  "dt", "efficiency", "Estimated", "evaluations", "Event",
129  "events", "Events", "execution", "external", "External",
130  "for", "Function", "function", "Handler", "Individual",
131  "Init", "Initial", "Initialization", "Initialize", "input",
132  "iterations", "Jacobian", "load", "LP", "LPS",
133  "max", "memory", "messages", "Messages", "Miliseconds",
134  "min", "ms", "Newton", "next", "output",
135  "Output", "Parallel", "Partitioning", "past", "per",
136  "performed", "Processed", "reinits", "reported", "Results",
137  "Save", "Scalar", "Seconds", "sent", "sequential",
138  "Simulation", "simulation", "solver", "speedup", "State",
139  "state", "Statistics", "step", "steps", "sum",
140  "time", "Time", "Total", "transition", "transitions",
141  "value", "values", "Variable", "variable", "variables",
142  "Variables", "Zero"};
143 
144 MmoHighlighter::MmoHighlighter(QTextDocument *parent, h_type file_type) : QSyntaxHighlighter(parent) { initialize(file_type); }
145 
147 {
148  switch (file) {
149  case MMO_MODEL:
151  break;
152  case MMO_LOG:
154  break;
155  }
156  _comment_start_expression = QRegularExpression("/\\*");
157  _comment_end_expression = QRegularExpression("\\*/");
158 }
159 
160 void MmoHighlighter::addHighlightingRule(const QString &pattern, const QTextCharFormat &format, bool bold)
161 {
162  MmoHighlightingRule rule;
163  QString add_pattern = bold ? "\\b" + pattern + "\\b" : pattern;
164  rule.pattern = QRegularExpression(add_pattern);
165  rule.format = format;
166  _highlighting_rules.append(rule);
167 }
168 
170 {
171  QTextCharFormat keyword_format;
172  keyword_format.setForeground(Qt::darkBlue);
173 
174  for (const auto &pattern : _model_keywords) {
175  addHighlightingRule(pattern, keyword_format);
176  }
177 
178  QTextCharFormat function_format;
179  function_format.setForeground(Qt::blue);
180  function_format.setFontItalic(true);
181 
182  for (const auto &_pattern : _model_functions) {
183  addHighlightingRule(_pattern, function_format);
184  }
185 
186  QTextCharFormat annotation_format;
187  annotation_format.setForeground(Qt::darkGreen);
188  annotation_format.setFontItalic(true);
189 
190  for (const auto &_pattern : _model_annotations) {
191  addHighlightingRule(_pattern, annotation_format);
192  }
193 
194  QTextCharFormat qss_tags_format;
195  qss_tags_format.setForeground(Qt::darkRed);
196 
197  for (const auto &_pattern : _model_qss_tags) {
198  addHighlightingRule(_pattern, qss_tags_format);
199  }
200 
201  _single_line_comment_format.setForeground(Qt::darkGray);
202  const bool DONT_USE_BOLD = false;
203  addHighlightingRule("//.*", _single_line_comment_format, DONT_USE_BOLD);
204 
205  QTextCharFormat quotation_format;
206  quotation_format.setForeground(Qt::darkMagenta);
207  addHighlightingRule("\".*\"", quotation_format);
208 }
209 
211 {
212  QTextCharFormat keyword_format;
213  keyword_format.setForeground(Qt::darkBlue);
214  keyword_format.setFontWeight(QFont::Bold);
215 
216  for (const auto &_pattern : _log_keywords) {
217  addHighlightingRule(_pattern, keyword_format);
218  }
219 }
220 
221 void MmoHighlighter::highlightBlock(const QString &text)
222 {
223  highlightSyntax(text);
224  highlightComments(text);
225 }
226 
227 void MmoHighlighter::highlightSyntax(const QString &text)
228 {
229  for (const MmoHighlightingRule &rule : _highlighting_rules) {
230  QRegularExpression expression(rule.pattern);
231  QRegularExpressionMatchIterator match_iterator = expression.globalMatch(text);
232 
233  while (match_iterator.hasNext()) {
234  QRegularExpressionMatch match = match_iterator.next();
235  int start_index = match.capturedStart();
236  int length = match.capturedLength();
237  setFormat(start_index, length, rule.format);
238  }
239  }
240 }
241 
242 void MmoHighlighter::highlightComments(const QString &text)
243 {
244  const int IN_COMMENT_BLOCK = 1;
245  setCurrentBlockState(0);
246  QTextCharFormat multi_line_comment_format;
247  multi_line_comment_format.setForeground(Qt::darkGray);
248 
249  // Determine the starting index for comment highlighting
250  int start_index = (previousBlockState() != IN_COMMENT_BLOCK) ? _comment_start_expression.match(text).capturedStart() : 0;
251 
252  while (start_index >= 0) {
253  // Find the end index of the comment
254  QRegularExpressionMatch end_match = _comment_end_expression.match(text, start_index);
255  int length_of_comment;
256 
257  if (!end_match.hasMatch()) {
258  // No end index found, means the comment continues to the end of the text
259  setCurrentBlockState(IN_COMMENT_BLOCK);
260  length_of_comment = text.length() - start_index;
261  } else {
262  // Calculate the length of the comment
263  length_of_comment = end_match.capturedStart() - start_index + end_match.capturedLength();
264  }
265 
266  // Apply formatting for the identified comment
267  setFormat(start_index, length_of_comment, multi_line_comment_format);
268 
269  // Find the next comment start index
270  QRegularExpressionMatch startMatch = _comment_start_expression.match(text, start_index + length_of_comment);
271  start_index = startMatch.hasMatch() ? startMatch.capturedStart() : -1; // Set start_index to -1 if no more comments are found
272  }
273 }
MmoHighlighter::setupLogHighlighting
void setupLogHighlighting()
Definition: mmohighlight.cpp:210
MmoHighlighter::_highlighting_rules
QVector< MmoHighlightingRule > _highlighting_rules
Definition: mmohighlight.hpp:73
MmoHighlighter::h_type
enum { MMO_MODEL, MMO_LOG } h_type
Definition: mmohighlight.hpp:47
MmoHighlighter::addHighlightingRule
void addHighlightingRule(const QString &pattern, const QTextCharFormat &format, bool bold=true)
Definition: mmohighlight.cpp:160
MmoHighlighter::MmoHighlightingRule::pattern
QRegularExpression pattern
Definition: mmohighlight.hpp:61
mmohighlight.hpp
MmoHighlighter::_comment_start_expression
QRegularExpression _comment_start_expression
Definition: mmohighlight.hpp:74
MmoHighlighter::_single_line_comment_format
QTextCharFormat _single_line_comment_format
Definition: mmohighlight.hpp:76
MmoHighlighter::setupModelHighlighting
void setupModelHighlighting()
Definition: mmohighlight.cpp:169
MmoHighlighter::initialize
void initialize(h_type file)
Definition: mmohighlight.cpp:146
MmoHighlighter::MmoHighlightingRule
Definition: mmohighlight.hpp:60
MmoHighlighter::highlightBlock
void highlightBlock(const QString &text) override
Definition: mmohighlight.cpp:221
MmoHighlighter::_model_qss_tags
static const QStringList _model_qss_tags
Definition: mmohighlight.hpp:69
MmoHighlighter::_log_keywords
static const QStringList _log_keywords
Definition: mmohighlight.hpp:70
MmoHighlighter::MmoHighlighter
MmoHighlighter(QTextDocument *parent=nullptr, h_type file=MMO_MODEL)
Definition: mmohighlight.cpp:144
MmoHighlighter::_comment_end_expression
QRegularExpression _comment_end_expression
Definition: mmohighlight.hpp:75
MmoHighlighter::highlightComments
void highlightComments(const QString &text)
Definition: mmohighlight.cpp:242
MmoHighlighter::_model_annotations
static const QStringList _model_annotations
Definition: mmohighlight.hpp:68
MmoHighlighter::highlightSyntax
void highlightSyntax(const QString &text)
Definition: mmohighlight.cpp:227
MmoHighlighter::_model_functions
static const QStringList _model_functions
Definition: mmohighlight.hpp:67
MmoHighlighter::_model_keywords
static const QStringList _model_keywords
Definition: mmohighlight.hpp:66
MmoHighlighter::MmoHighlightingRule::format
QTextCharFormat format
Definition: mmohighlight.hpp:62