OpenMD 3.0
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 appropriate papers when you publish your
33 * work. Good starting points are:
34 *
35 * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
36 * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
37 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
38 * [4] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
39 * [5] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
40 * [6] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
41 * [7] Lamichhane, Newman & Gezelter, J. Chem. Phys. 141, 134110 (2014).
42 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
43 */
44
45#ifndef SELECTION_SELECTIONCOMPILER_HPP
46#define SELECTION_SELECTIONCOMPILER_HPP
47
48#include <any>
49#include <iostream>
50#include <string>
51#include <vector>
52
53#include "brains/SimInfo.hpp"
54#include "selection/SelectionToken.hpp"
55#include "selection/TokenMap.hpp"
56
57namespace OpenMD {
58
59 /**
60 * @class SelectionCompiler SelectionCompiler.hpp
61 "selection/SelectionCompiler.hpp"
62 * @brief compile a selection script to tokens
63 * @todo document
64 * <pre>
65
66 expression :: = clauseOr
67
68 clauseOr ::= clauseAnd {OR clauseAnd}*
69
70 clauseAnd ::= clauseNot {AND clauseNot}*
71
72 clauseNot ::= NOT clauseNot | clausePrimitive
73
74 clausePrimitive ::= clauseComparator |
75 clauseWithin |
76 clauseAlphaHull |
77 clauseName |
78 none | all |
79 ( clauseOr )
80
81 clauseComparator ::= atomproperty comparatorop integer
82
83 clauseWithin ::= WITHIN ( clauseDistance , expression )
84
85 clauseDistance ::= integer | decimal
86
87 clauseName::= *|string{.string{.string}}
88
89
90 * </pre>
91 */
93 public:
94 bool compile(const std::string& filename, const std::string& script);
95
96 std::vector<int> getLineNumbers() { return lineNumbers; }
97
98 std::vector<int> getLineIndices() { return lineIndices; }
99
100 std::vector<std::vector<Token>> getAatokenCompiled() {
101 return aatokenCompiled;
102 }
103
104 std::string getErrorMessage() {
105 std::string strError = errorMessage;
106 strError += " : " + errorLine + "\n";
107
108 if (!filename.empty()) { strError += filename; }
109
110 return strError;
111 }
112
113 private:
114 bool internalCompile();
115
116 bool lookingAtLeadingWhitespace();
117 // bool lookingAtComment();
118 bool lookingAtEndOfLine();
119 bool lookingAtEndOfStatement();
120 bool lookingAtString();
121 bool lookingAtDecimal(bool allowNegative);
122 bool lookingAtInteger(bool allowNegative);
123 bool lookingAtLookupToken();
124 bool lookingAtSpecialString();
125
126 std::string getUnescapedStringLiteral();
127 int getHexitValue(char ch);
128
129 bool compileCommand(const std::vector<Token>& ltoken);
130 bool compileExpression();
131 bool compileExpression(int itoken);
132
133 bool clauseOr();
134 bool clauseAnd();
135 bool clauseNot();
136 bool clausePrimitive();
137 bool clauseWithin();
138 bool clauseAlphaHull();
139 bool clauseComparator();
140 bool clauseChemObjName();
141 bool clauseIndex();
142 Token tokenNext();
143 std::any valuePeek();
144 int tokPeek();
145
146 bool addTokenToPostfix(const Token& token);
147 bool isNameValid(const std::string& name);
148
149 bool compileError(const std::string& errorMsg) {
150 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
151 "SelectionCompiler Error: %s\n", errorMsg.c_str());
152 painCave.severity = OPENMD_ERROR;
153 painCave.isFatal = 1;
154 simError();
155
156 error = true;
157 this->errorMessage = errorMsg;
158 return false;
159 }
160
161 bool commandExpected() { return compileError("command expected"); }
162
163 bool invalidExpressionToken(const std::string& ident) {
164 return compileError("invalid expression token:" + ident);
165 }
166
167 bool unrecognizedToken() { return compileError("unrecognized token"); }
168
169 bool badArgumentCount() { return compileError("bad argument count"); }
170
171 bool endOfExpressionExpected() {
172 return compileError("end of expression expected");
173 }
174
175 bool leftParenthesisExpected() {
176 return compileError("left parenthesis expected");
177 }
178
179 bool rightParenthesisExpected() {
180 return compileError("right parenthesis expected");
181 }
182
183 bool commaExpected() { return compileError("comma expected"); }
184
185 bool unrecognizedExpressionToken() {
186 std::any tmp = valuePeek();
187 std::string tokenStr;
188
189 try {
190 tokenStr = std::any_cast<std::string>(tmp);
191 } catch (const std::bad_any_cast&) {
192 return compileError("any_cast error");
193 }
194
195 return compileError("unrecognized expression token:" + tokenStr);
196 }
197
198 bool comparisonOperatorExpected() {
199 return compileError("comparison operator expected");
200 }
201
202 bool numberExpected() { return compileError("number expected"); }
203
204 bool numberOrKeywordExpected() {
205 return compileError("number or keyword expected");
206 }
207
208 std::string filename;
209 std::string script;
210
211 std::vector<int> lineNumbers;
212 std::vector<int> lineIndices;
213 std::vector<std::vector<Token>> aatokenCompiled;
214
215 bool error;
216 std::string errorMessage;
217 std::string errorLine;
218
219 int cchScript;
220 short lineCurrent;
221
222 int ichToken;
223 int cchToken;
224 std::vector<Token> atokenCommand;
225
226 int ichCurrentCommand;
227
228 std::vector<Token> ltokenPostfix;
229 std::vector<Token> atokenInfix;
230 std::size_t itokenInfix;
231
232 // std::vector<Token> compiledTokens_;
233 };
234} // namespace OpenMD
235
236#endif
"selection/SelectionCompiler.hpp"
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.