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