1 |
tim |
277 |
/* |
2 |
|
|
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
3 |
|
|
* |
4 |
|
|
* The University of Notre Dame grants you ("Licensee") a |
5 |
|
|
* non-exclusive, royalty free, license to use, modify and |
6 |
|
|
* redistribute this software in source and binary code form, provided |
7 |
|
|
* that the following conditions are met: |
8 |
|
|
* |
9 |
gezelter |
1390 |
* 1. Redistributions of source code must retain the above copyright |
10 |
tim |
277 |
* notice, this list of conditions and the following disclaimer. |
11 |
|
|
* |
12 |
gezelter |
1390 |
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
tim |
277 |
* notice, this list of conditions and the following disclaimer in the |
14 |
|
|
* documentation and/or other materials provided with the |
15 |
|
|
* distribution. |
16 |
|
|
* |
17 |
|
|
* This software is provided "AS IS," without a warranty of any |
18 |
|
|
* kind. All express or implied conditions, representations and |
19 |
|
|
* warranties, including any implied warranty of merchantability, |
20 |
|
|
* fitness for a particular purpose or non-infringement, are hereby |
21 |
|
|
* excluded. The University of Notre Dame and its licensors shall not |
22 |
|
|
* be liable for any damages suffered by licensee as a result of |
23 |
|
|
* using, modifying or distributing the software or its |
24 |
|
|
* derivatives. In no event will the University of Notre Dame or its |
25 |
|
|
* licensors be liable for any lost revenue, profit or data, or for |
26 |
|
|
* direct, indirect, special, consequential, incidental or punitive |
27 |
|
|
* damages, however caused and regardless of the theory of liability, |
28 |
|
|
* arising out of the use of or inability to use software, even if the |
29 |
|
|
* University of Notre Dame has been advised of the possibility of |
30 |
|
|
* such damages. |
31 |
gezelter |
1390 |
* |
32 |
|
|
* SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your |
33 |
|
|
* research, please cite the appropriate papers when you publish your |
34 |
|
|
* work. Good starting points are: |
35 |
|
|
* |
36 |
|
|
* [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). |
37 |
|
|
* [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). |
38 |
|
|
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). |
39 |
gezelter |
1782 |
* [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). |
40 |
|
|
* [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). |
41 |
tim |
277 |
*/ |
42 |
|
|
|
43 |
|
|
#ifndef SELECTION_SELECTIONCOMPILER_HPP |
44 |
|
|
#define SELECTION_SELECTIONCOMPILER_HPP |
45 |
tim |
281 |
#include <iostream> |
46 |
tim |
278 |
#include <string> |
47 |
tim |
277 |
#include <vector> |
48 |
tim |
281 |
|
49 |
tim |
770 |
#include "selection/SelectionToken.hpp" |
50 |
tim |
281 |
#include "selection/TokenMap.hpp" |
51 |
cli2 |
1364 |
#include "brains/SimInfo.hpp" |
52 |
|
|
|
53 |
gezelter |
1390 |
namespace OpenMD { |
54 |
tim |
277 |
|
55 |
|
|
|
56 |
gezelter |
507 |
/** |
57 |
|
|
* @class SelectionCompiler SelectionCompiler.hpp "selection/SelectionCompiler.hpp" |
58 |
|
|
* @brief compile a selection script to tokens |
59 |
|
|
* @todo document |
60 |
|
|
* <pre> |
61 |
tim |
277 |
|
62 |
gezelter |
507 |
expression :: = clauseOr |
63 |
tim |
277 |
|
64 |
gezelter |
507 |
clauseOr ::= clauseAnd {OR clauseAnd}* |
65 |
tim |
277 |
|
66 |
gezelter |
507 |
clauseAnd ::= clauseNot {AND clauseNot}* |
67 |
tim |
277 |
|
68 |
gezelter |
507 |
clauseNot ::= NOT clauseNot | clausePrimitive |
69 |
tim |
277 |
|
70 |
gezelter |
507 |
clausePrimitive ::= clauseComparator | |
71 |
|
|
clauseWithin | |
72 |
|
|
clauseName | |
73 |
|
|
none | all | |
74 |
|
|
( clauseOr ) |
75 |
tim |
277 |
|
76 |
gezelter |
507 |
clauseComparator ::= atomproperty comparatorop integer |
77 |
tim |
277 |
|
78 |
gezelter |
507 |
clauseWithin ::= WITHIN ( clauseDistance , expression ) |
79 |
tim |
277 |
|
80 |
gezelter |
507 |
clauseDistance ::= integer | decimal |
81 |
tim |
283 |
|
82 |
gezelter |
507 |
clauseName::= *|string{.string{.string}} |
83 |
tim |
277 |
|
84 |
tim |
278 |
|
85 |
gezelter |
507 |
* </pre> |
86 |
|
|
*/ |
87 |
|
|
class SelectionCompiler{ |
88 |
|
|
public: |
89 |
|
|
bool compile(const std::string& filename, const std::string& script ); |
90 |
tim |
278 |
|
91 |
tim |
277 |
|
92 |
gezelter |
507 |
std::vector<int> getLineNumbers() { |
93 |
|
|
return lineNumbers; |
94 |
|
|
} |
95 |
tim |
277 |
|
96 |
gezelter |
507 |
std::vector<int> getLineIndices() { |
97 |
|
|
return lineIndices; |
98 |
|
|
} |
99 |
tim |
277 |
|
100 |
gezelter |
507 |
std::vector<std::vector<Token> > getAatokenCompiled() { |
101 |
|
|
return aatokenCompiled; |
102 |
|
|
} |
103 |
tim |
277 |
|
104 |
gezelter |
507 |
std::string getErrorMessage() { |
105 |
|
|
std::string strError = errorMessage; |
106 |
|
|
strError += " : " + errorLine + "\n"; |
107 |
tim |
277 |
|
108 |
gezelter |
507 |
if (!filename.empty()) { |
109 |
|
|
strError += filename; |
110 |
|
|
} |
111 |
tim |
277 |
|
112 |
gezelter |
507 |
return strError; |
113 |
|
|
} |
114 |
tim |
277 |
|
115 |
tim |
278 |
|
116 |
gezelter |
507 |
private: |
117 |
tim |
277 |
|
118 |
gezelter |
507 |
bool internalCompile(); |
119 |
tim |
278 |
|
120 |
|
|
|
121 |
gezelter |
507 |
bool lookingAtLeadingWhitespace(); |
122 |
|
|
//bool lookingAtComment(); |
123 |
|
|
bool lookingAtEndOfLine(); |
124 |
|
|
bool lookingAtEndOfStatement(); |
125 |
|
|
bool lookingAtString(); |
126 |
|
|
bool lookingAtDecimal(bool allowNegative); |
127 |
|
|
bool lookingAtInteger(bool allowNegative); |
128 |
|
|
bool lookingAtLookupToken(); |
129 |
|
|
bool lookingAtSpecialString(); |
130 |
tim |
278 |
|
131 |
gezelter |
507 |
std::string getUnescapedStringLiteral(); |
132 |
|
|
int getHexitValue(char ch); |
133 |
tim |
278 |
|
134 |
gezelter |
507 |
bool compileCommand(const std::vector<Token>& ltoken); |
135 |
|
|
bool compileExpression(); |
136 |
|
|
bool compileExpression(int itoken); |
137 |
tim |
278 |
|
138 |
gezelter |
507 |
bool clauseOr(); |
139 |
|
|
bool clauseAnd(); |
140 |
|
|
bool clauseNot(); |
141 |
|
|
bool clausePrimitive(); |
142 |
|
|
bool clauseWithin(); |
143 |
|
|
bool clauseComparator(); |
144 |
|
|
bool clauseChemObjName(); |
145 |
|
|
bool clauseIndex(); |
146 |
|
|
Token tokenNext(); |
147 |
|
|
boost::any valuePeek(); |
148 |
|
|
int tokPeek(); |
149 |
tim |
281 |
|
150 |
gezelter |
507 |
bool addTokenToPostfix(const Token& token); |
151 |
|
|
bool isNameValid(const std::string& name); |
152 |
tim |
281 |
|
153 |
gezelter |
507 |
bool compileError(const std::string& errorMsg) { |
154 |
cli2 |
1364 |
|
155 |
|
|
sprintf( painCave.errMsg, |
156 |
|
|
"SelectionCompiler Error: %s\n", errorMsg.c_str()); |
157 |
gezelter |
1390 |
painCave.severity = OPENMD_ERROR; |
158 |
cli2 |
1364 |
painCave.isFatal = 1; |
159 |
|
|
simError(); |
160 |
|
|
|
161 |
gezelter |
507 |
error = true; |
162 |
|
|
this->errorMessage = errorMsg; |
163 |
|
|
return false; |
164 |
|
|
} |
165 |
tim |
277 |
|
166 |
gezelter |
507 |
bool commandExpected() { |
167 |
|
|
return compileError("command expected"); |
168 |
|
|
} |
169 |
tim |
277 |
|
170 |
gezelter |
507 |
bool invalidExpressionToken(const std::string& ident) { |
171 |
|
|
return compileError("invalid expression token:" + ident); |
172 |
|
|
} |
173 |
tim |
278 |
|
174 |
gezelter |
507 |
bool unrecognizedToken() { |
175 |
|
|
return compileError("unrecognized token"); |
176 |
|
|
} |
177 |
tim |
278 |
|
178 |
gezelter |
507 |
bool badArgumentCount() { |
179 |
|
|
return compileError("bad argument count"); |
180 |
|
|
} |
181 |
tim |
278 |
|
182 |
gezelter |
507 |
bool endOfExpressionExpected() { |
183 |
|
|
return compileError("end of expression expected"); |
184 |
|
|
} |
185 |
tim |
278 |
|
186 |
gezelter |
507 |
bool leftParenthesisExpected() { |
187 |
|
|
return compileError("left parenthesis expected"); |
188 |
|
|
} |
189 |
tim |
278 |
|
190 |
gezelter |
507 |
bool rightParenthesisExpected() { |
191 |
|
|
return compileError("right parenthesis expected"); |
192 |
|
|
} |
193 |
tim |
278 |
|
194 |
gezelter |
507 |
bool commaExpected() { |
195 |
|
|
return compileError("comma expected"); |
196 |
|
|
} |
197 |
tim |
278 |
|
198 |
gezelter |
507 |
bool unrecognizedExpressionToken() { |
199 |
|
|
boost::any tmp = valuePeek(); |
200 |
|
|
std::string tokenStr; |
201 |
tim |
281 |
|
202 |
gezelter |
507 |
try { |
203 |
|
|
tokenStr = boost::any_cast<std::string>(tmp); |
204 |
|
|
} catch(const boost::bad_any_cast &) { |
205 |
|
|
return compileError("any_cast error"); |
206 |
|
|
} |
207 |
tim |
281 |
|
208 |
gezelter |
507 |
return compileError("unrecognized expression token:" + tokenStr); |
209 |
|
|
} |
210 |
tim |
278 |
|
211 |
gezelter |
507 |
bool comparisonOperatorExpected() { |
212 |
|
|
return compileError("comparison operator expected"); |
213 |
|
|
} |
214 |
tim |
278 |
|
215 |
gezelter |
507 |
bool numberExpected() { |
216 |
|
|
return compileError("number expected"); |
217 |
|
|
} |
218 |
tim |
278 |
|
219 |
gezelter |
507 |
bool numberOrKeywordExpected() { |
220 |
|
|
return compileError("number or keyword expected"); |
221 |
|
|
} |
222 |
tim |
278 |
|
223 |
gezelter |
507 |
std::string filename; |
224 |
|
|
std::string script; |
225 |
tim |
278 |
|
226 |
gezelter |
507 |
std::vector<int> lineNumbers; |
227 |
|
|
std::vector<int> lineIndices; |
228 |
|
|
std::vector<std::vector<Token> >aatokenCompiled; |
229 |
tim |
278 |
|
230 |
gezelter |
507 |
bool error; |
231 |
|
|
std::string errorMessage; |
232 |
|
|
std::string errorLine; |
233 |
tim |
278 |
|
234 |
gezelter |
507 |
int cchScript; |
235 |
|
|
short lineCurrent; |
236 |
tim |
278 |
|
237 |
gezelter |
507 |
int ichToken; |
238 |
|
|
int cchToken; |
239 |
|
|
std::vector<Token> atokenCommand; |
240 |
tim |
278 |
|
241 |
gezelter |
507 |
int ichCurrentCommand; |
242 |
tim |
278 |
|
243 |
gezelter |
507 |
std::vector<Token> ltokenPostfix; |
244 |
|
|
std::vector<Token> atokenInfix; |
245 |
|
|
int itokenInfix; |
246 |
tim |
278 |
|
247 |
gezelter |
507 |
//std::vector<Token> compiledTokens_; |
248 |
|
|
}; |
249 |
tim |
277 |
|
250 |
|
|
} |
251 |
|
|
#endif |
252 |
tim |
278 |
|