OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
SelectionCompiler.hpp
1/*
2 * Copyright (c) 2004-present, The University of Notre Dame. All rights
3 * reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
32 * research, please cite the following paper when you publish your work:
33 *
34 * [1] Drisko et al., J. Open Source Softw. 9, 7004 (2024).
35 *
36 * Good starting points for code and simulation methodology are:
37 *
38 * [2] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
39 * [3] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
40 * [4] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
41 * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
42 * [6] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
43 * [7] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
44 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
45 * [9] Drisko & Gezelter, J. Chem. Theory Comput. 20, 4986-4997 (2024).
46 */
47
48#ifndef SELECTION_SELECTIONCOMPILER_HPP
49#define SELECTION_SELECTIONCOMPILER_HPP
50
51#include <any>
52#include <iostream>
53#include <string>
54#include <vector>
55
56#include "brains/SimInfo.hpp"
57#include "selection/SelectionToken.hpp"
58#include "selection/TokenMap.hpp"
59
60namespace OpenMD {
61
62 /**
63 * @class SelectionCompiler SelectionCompiler.hpp
64 "selection/SelectionCompiler.hpp"
65 * @brief compile a selection script to tokens
66 * @todo document
67 * <pre>
68
69 expression :: = clauseOr
70
71 clauseOr ::= clauseAnd {OR clauseAnd}*
72
73 clauseAnd ::= clauseNot {AND clauseNot}*
74
75 clauseNot ::= NOT clauseNot | clausePrimitive
76
77 clausePrimitive ::= clauseComparator |
78 clauseWithin |
79 clauseAlphaHull |
80 clauseName |
81 none | all |
82 ( clauseOr )
83
84 clauseComparator ::= atomproperty comparatorop integer
85
86 clauseWithin ::= WITHIN ( clauseDistance , expression )
87
88 clauseDistance ::= integer | decimal
89
90 clauseName::= *|string{.string{.string}}
91
92
93 * </pre>
94 */
96 public:
97 bool compile(const std::string& filename, const std::string& script);
98
99 std::vector<int> getLineNumbers() { return lineNumbers; }
100
101 std::vector<int> getLineIndices() { return lineIndices; }
102
103 std::vector<std::vector<Token>> getAatokenCompiled() {
104 return aatokenCompiled;
105 }
106
107 std::string getErrorMessage() {
108 std::string strError = errorMessage;
109 strError += " : " + errorLine + "\n";
110
111 if (!filename.empty()) { strError += filename; }
112
113 return strError;
114 }
115
116 private:
117 bool internalCompile();
118
119 bool lookingAtLeadingWhitespace();
120 // bool lookingAtComment();
121 bool lookingAtEndOfLine();
122 bool lookingAtEndOfStatement();
123 bool lookingAtString();
124 bool lookingAtDecimal(bool allowNegative);
125 bool lookingAtInteger(bool allowNegative);
126 bool lookingAtLookupToken();
127 bool lookingAtSpecialString();
128
129 std::string getUnescapedStringLiteral();
130 int getHexitValue(char ch);
131
132 bool compileCommand(const std::vector<Token>& ltoken);
133 bool compileExpression();
134 bool compileExpression(int itoken);
135
136 bool clauseOr();
137 bool clauseAnd();
138 bool clauseNot();
139 bool clausePrimitive();
140 bool clauseWithin();
141 bool clauseAlphaHull();
142 bool clauseComparator();
143 bool clauseChemObjName();
144 bool clauseIndex();
145 Token tokenNext();
146 std::any valuePeek();
147 int tokPeek();
148
149 bool addTokenToPostfix(const Token& token);
150 bool isNameValid(const std::string& name);
151
152 bool compileError(const std::string& errorMsg) {
153 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
154 "SelectionCompiler Error: %s\n", errorMsg.c_str());
155 painCave.severity = OPENMD_ERROR;
156 painCave.isFatal = 1;
157 simError();
158
159 error = true;
160 this->errorMessage = errorMsg;
161 return false;
162 }
163
164 bool commandExpected() { return compileError("command expected"); }
165
166 bool invalidExpressionToken(const std::string& ident) {
167 return compileError("invalid expression token:" + ident);
168 }
169
170 bool unrecognizedToken() { return compileError("unrecognized token"); }
171
172 bool badArgumentCount() { return compileError("bad argument count"); }
173
174 bool endOfExpressionExpected() {
175 return compileError("end of expression expected");
176 }
177
178 bool leftParenthesisExpected() {
179 return compileError("left parenthesis expected");
180 }
181
182 bool rightParenthesisExpected() {
183 return compileError("right parenthesis expected");
184 }
185
186 bool commaExpected() { return compileError("comma expected"); }
187
188 bool unrecognizedExpressionToken() {
189 std::any tmp = valuePeek();
190 std::string tokenStr;
191
192 try {
193 tokenStr = std::any_cast<std::string>(tmp);
194 } catch (const std::bad_any_cast&) {
195 return compileError("any_cast error");
196 }
197
198 return compileError("unrecognized expression token:" + tokenStr);
199 }
200
201 bool comparisonOperatorExpected() {
202 return compileError("comparison operator expected");
203 }
204
205 bool numberExpected() { return compileError("number expected"); }
206
207 bool numberOrKeywordExpected() {
208 return compileError("number or keyword expected");
209 }
210
211 std::string filename;
212 std::string script;
213
214 std::vector<int> lineNumbers;
215 std::vector<int> lineIndices;
216 std::vector<std::vector<Token>> aatokenCompiled;
217
218 bool error;
219 std::string errorMessage;
220 std::string errorLine;
221
222 int cchScript;
223 short lineCurrent;
224
225 int ichToken;
226 int cchToken;
227 std::vector<Token> atokenCommand;
228
229 int ichCurrentCommand;
230
231 std::vector<Token> ltokenPostfix;
232 std::vector<Token> atokenInfix;
233 std::size_t itokenInfix;
234
235 // std::vector<Token> compiledTokens_;
236 };
237} // namespace OpenMD
238
239#endif
"selection/SelectionCompiler.hpp"
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.