Debian Codegraph @

avogadro/qtplugins/gamessinput/gamesshighlighter.cpp

raw
1 /******************************************************************************
2
3 This source file is part of the Avogadro project.
4
5 Copyright (C) 2009 Marcus D. Hanwell
6
7 This source code is released under the New BSD License, (the "License").
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14
15 ******************************************************************************/
16
17 #include "gamesshighlighter.h"
18
19 namespace Avogadro::QtPlugins {
20
21 GamessHighlighter::GamessHighlighter(QTextDocument* parent_)
22 : QSyntaxHighlighter(parent_)
23 {
24 HighlightingRule rule;
25
26 m_keywordFormat.setForeground(Qt::darkBlue);
27 m_keywordFormat.setFontWeight(QFont::Bold);
28 m_keywords << R"(\s\$BASIS\b)"
29 << R"(\s\$CONTRL\b)"
30 << R"(\s\$SYSTEM\b)"
31 << R"(\s\$ZMAT\b)"
32 << R"(\s\$LIBE\b)"
33 << R"(\s\$SCF\b)"
34 << R"(\s\$SCFMI\b)"
35 << R"(\s\$DFT\b)"
36 << R"(\s\$TDDFT\b)"
37 << R"(\s\$CIS\b)"
38 << R"(\s\$CISVEC\b)"
39 << R"(\s\$MP2\b)"
40 << R"(\s\$CCINP\b)"
41 << R"(\s\$EOMINP\b)"
42 << R"(\s\$MOPAC\b)"
43 << R"(\s\$GUESS\b)"
44 << R"(\s\$VEC\b)"
45 << R"(\s\$MOFRZ\b)"
46 << R"(\s\$STATPT\b)"
47 << R"(\s\$TRUDGE\b)"
48 << R"(\s\$TRURST\b)"
49 << R"(\s\$FORCE\b)"
50 << R"(\s\$CPHF\b)"
51 << R"(\s\$MASS\b)"
52 << R"(\s\$HESS\b)"
53 << R"(\s\$GRAD\b)"
54 << R"(\s\$DIPDR\b)"
55 << R"(\s\$VIB\b)"
56 << R"(\s\$VIB2\b)"
57 << R"(\s\$VSCF\b)"
58 << R"(\s\$VIBSCF\b)"
59 << R"(\s\$GAMMA\b)"
60 << R"(\s\$EQGEOM\b)"
61 << R"(\s\$HLOWT\b)"
62 << R"(\s\$GLOWT\b)"
63 << R"(\s\$IRC\b)"
64 << R"(\s\$DRC\b)"
65 << R"(\s\$MEX\b)"
66 << R"(\s\$MD\b)"
67 << R"(\s\$RDF\b)"
68 << R"(\s\$GLOBOP\b)"
69 << R"(\s\$GRADEX\b)"
70 << R"(\s\$SURF\b)"
71 << R"(\s\$LOCAL\b)"
72 << R"(\s\$TWOEI\b)"
73 << R"(\s\$TRUNCN\b)"
74 << R"(\s\$ELMOM\b)"
75 << R"(\s\$ELPOT\b)"
76 << R"(\s\$ELDENS\b)"
77 << R"(\s\$ELFLDG\b)"
78 << R"(\s\$POINTS\b)"
79 << R"(\s\$GRID\b)"
80 << R"(\s\$PDC\b)"
81 << R"(\s\$MOLGRF\b)"
82 << R"(\s\$STONE\b)"
83 << R"(\s\$RAMAN\b)"
84 << R"(\s\$ALPDR\b)"
85 << R"(\s\$NMR\b)"
86 << R"(\s\$MOROKM\b)"
87 << R"(\s\$FFCALC\b)"
88 << R"(\s\$TDHF\b)"
89 << R"(\s\$TDHFX\b)"
90 << R"(\s\$EFRAG\b)"
91 << R"(\s\$FRAGNAME\b)"
92 << R"(\s\$FRGRPL\b)"
93 << R"(\s\$EWALD\b)"
94 << R"(\s\$MAKEFP\b)"
95 << R"(\s\$PRTEFP\b)"
96 << R"(\s\$DAMP\b)"
97 << R"(\s\$DAMPGS\b)"
98 << R"(\s\$PCM\b)"
99 << R"(\s\$PCMGRD\b)"
100 << R"(\s\$PCMCAV\b)"
101 << R"(\s\$TESCAV\b)"
102 << R"(\s\$NEWCAV\b)"
103 << R"(\s\$IEFPCM\b)"
104 << R"(\s\$PCMITR\b)"
105 << R"(\s\$DISBS\b)"
106 << R"(\s\$DISREP\b)"
107 << R"(\s\$SVP\b)"
108 << R"(\s\$SVPIRF\b)"
109 << R"(\s\$COSGMS\b)"
110 << R"(\s\$SCRF\b)"
111 << R"(\s\$ECP\b)"
112 << R"(\s\$MCP\b)"
113 << R"(\s\$RELWFN\b)"
114 << R"(\s\$EFIELD\b)"
115 << R"(\s\$INTGRL\b)"
116 << R"(\s\$FMM\b)"
117 << R"(\s\$TRANS\b)"
118 << R"(\s\$FMO\b)"
119 << R"(\s\$FMOPRP\b)"
120 << R"(\s\$FMOXYZ\b)"
121 << R"(\s\$OPTFMO\b)"
122 << R"(\s\$FMOHYB\b)"
123 << R"(\s\$FMOBND\b)"
124 << R"(\s\$FMOENM\b)"
125 << R"(\s\$FMOEND\b)"
126 << R"(\s\$OPTRST\b)"
127 << R"(\s\$GDDI\b)"
128 << R"(\s\$ELG\b)"
129 << R"(\s\$DANDC\b)"
130 << R"(\s\$DCCORR\b)"
131 << R"(\s\$SUBSCF\b)"
132 << R"(\s\$SUBCOR\b)"
133 << R"(\s\$MP2RES\b)"
134 << R"(\s\$CCRES\b)"
135 << R"(\s\$CIINP\b)"
136 << R"(\s\$DET\b)"
137 << R"(\s\$CIDET\b)"
138 << R"(\s\$GEN\b)"
139 << R"(\s\$CIGEN\b)"
140 << R"(\s\$ORMAS\b)"
141 << R"(\s\$CEEIS\b)"
142 << R"(\s\$CEDATA\b)"
143 << R"(\s\$GCILST\b)"
144 << R"(\s\$GMCPT\b)"
145 << R"(\s\$PDET\b)"
146 << R"(\s\$ADDDET\b)"
147 << R"(\s\$REMDET\b)"
148 << R"(\s\$SODET\b)"
149 << R"(\s\$DRT\b)"
150 << R"(\s\$CIDRT\b)"
151 << R"(\s\$MCSCF\b)"
152 << R"(\s\$MRMP\b)"
153 << R"(\s\$DETPT\b)"
154 << R"(\s\$MCQDPT\b)"
155 << R"(\s\$CASCI\b)"
156 << R"(\s\$IVOORB\b)"
157 << R"(\s\$CISORT\b)"
158 << R"(\s\$GUGEM\b)"
159 << R"(\s\$GUGDIA\b)"
160 << R"(\s\$GUGDM\b)"
161 << R"(\s\$GUGDM2\b)"
162 << R"(\s\$LAGRAN\b)"
163 << R"(\s\$TRFDM2\b)"
164 << R"(\s\$TRANST\b)"
165 << R"(\s\$DATA\b)";
166 rule.format = m_keywordFormat;
167 foreach (const QString& pattern, m_keywords) {
168 rule.pattern = QRegularExpression(pattern);
169 m_highlightingRules.append(rule);
170 }
171 rule.pattern = QRegularExpression(R"(\s\$END\b)");
172 m_highlightingRules.append(rule);
173
174 m_singleLineCommentFormat.setForeground(Qt::green);
175 rule.pattern = QRegularExpression("![^\n]*");
176 rule.format = m_singleLineCommentFormat;
177 m_highlightingRules.append(rule);
178
179 m_numberFormat.setForeground(Qt::blue);
180 rule.pattern =
181 QRegularExpression(R"((\b|[\s-])[0-9]+\.([0-9]+\b)?|\.[0-9]+\b)");
182 rule.format = m_numberFormat;
183 m_highlightingRules.append(rule);
184
185 m_numberFormat.setForeground(Qt::blue);
186 rule.pattern =
187 QRegularExpression(R"((\b|[\s-])[0-9]+\.([0-9]+\b)?|\.[0-9]+\b)");
188 rule.format = m_numberFormat;
189 m_highlightingRules.append(rule);
190 rule.pattern =
191 QRegularExpression(R"((\b|[\s-])[0-9]+([0-9]+\b)?|\.[0-9]+\b)");
192 rule.format = m_numberFormat;
193 m_highlightingRules.append(rule);
194
195 m_inDataBlockFormat.setForeground(Qt::gray);
196
197 m_errorFormat.setForeground(Qt::red);
198 m_errorFormat.setBackground(Qt::yellow);
199 }
200
201 void GamessHighlighter::highlightBlock(const QString& text)
202 {
203 // Single line comments
204 QRegularExpression pattern("![^\n]*");
205 QRegularExpressionMatch commentMatch = pattern.match(text);
206 if (commentMatch.hasMatch())
207 setFormat(commentMatch.capturedStart(), commentMatch.capturedLength(),
208 m_singleLineCommentFormat);
209
210 setCurrentBlockState(0);
211
212 int startIndex = 0;
213 int keywordLength = 0;
214 if (previousBlockState() != 1) {
215 foreach (const QString& regexString, m_keywords) {
216 QRegularExpression startExpression(
217 regexString, QRegularExpression::CaseInsensitiveOption);
218 QRegularExpressionMatch startMatch = startExpression.match(text);
219 startIndex = startMatch.capturedStart();
220 keywordLength = startMatch.capturedLength();
221 if (startMatch.hasMatch()) {
222 setFormat(startIndex, keywordLength, m_keywordFormat);
223 break;
224 }
225 }
226 }
227
228 while (startIndex >= 0) {
229 QRegularExpression endExpression(R"(\s\$END\b)",
230 QRegularExpression::CaseInsensitiveOption);
231 QRegularExpressionMatch match = endExpression.match(text, startIndex);
232 int endIndex = match.capturedStart();
233 int blockLength;
234 if (endIndex == -1) {
235 setCurrentBlockState(1);
236 blockLength = text.length() - startIndex - keywordLength;
237 } else {
238 setFormat(endIndex, match.capturedLength(), m_keywordFormat);
239 blockLength = endIndex - startIndex - keywordLength;
240 }
241 setFormat(startIndex + keywordLength, blockLength, m_inDataBlockFormat);
242 bool found = false;
243 foreach (const QString& regexString, m_keywords) {
244 QRegularExpression newExpression(regexString);
245 QRegularExpressionMatch newMatch =
246 newExpression.match(text, startIndex + blockLength);
247 int index = newMatch.capturedStart();
248 if (index > startIndex) {
249 found = true;
250 startIndex = index;
251 keywordLength = newMatch.capturedLength();
252 setFormat(startIndex, keywordLength, m_keywordFormat);
253 break;
254 }
255 }
256 if (!found)
257 break;
258 }
259
260 if (previousBlockState() ==
261 1) { // Anything outside of data blocks is a comment
262 foreach (const HighlightingRule& rule, m_highlightingRules) {
263 QRegularExpression otherExpression(rule.pattern);
264 otherExpression.setPatternOptions(
265 QRegularExpression::CaseInsensitiveOption);
266 QRegularExpressionMatchIterator iterator =
267 otherExpression.globalMatch(text);
268 while (iterator.hasNext()) {
269 QRegularExpressionMatch otherMatch = iterator.next();
270 int index = otherMatch.capturedStart();
271 int length = otherMatch.capturedLength();
272 setFormat(index, length, rule.format);
273 }
274 }
275 }
276
277 // Anything over 80 columns will not be read
278 if (text.length() > 80)
279 setFormat(80, text.length(), m_errorFormat);
280 }
281
282 } // namespace Avogadro::QtPlugins