ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/selection/SelectionCompiler.hpp
Revision: 1965
Committed: Wed Feb 2 23:13:11 2005 UTC (19 years, 5 months ago) by tim
File size: 7868 byte(s)
Log Message:
selection library in progress, except SelectionEvaluator, other files are compiled

File Contents

# Content
1 /*
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 #include <iostream>
45 #include <string>
46 #include <vector>
47
48 #include "selection/Token.hpp"
49 #include "selection/TokenMap.hpp"
50 namespace oopse {
51
52
53 /**
54 * @class SelectionCompiler SelectionCompiler.hpp "selection/SelectionCompiler.hpp"
55 * @brief compile a selection script to tokens
56 * @todo document
57 * <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 clauseChemObject |
70 none | all |
71 ( clauseOr )
72
73 clauseComparator ::= atomproperty comparatorop integer
74
75 clauseWithin ::= WITHIN ( clauseDistance , expression )
76
77 clauseDistance ::= integer | decimal
78
79 clauseChemObject::= {clauseMolecule} | {clauseStuntDouble}
80
81 clauseMolecule ::= {clauseMolName} | {clauseMolIndex}
82
83 clauseMolName ::= molname clauseName
84
85 clauseName::= *|string
86
87 clauseMolIndex ::= molindex clauseIndex
88
89 clauseIndex ::= integer {- integer }
90
91 clauseStuntDouble ::= {clauseStuntDoubleName} | {clauseStuntDoubleIndex}
92
93 clauseStuntDoubleName ::= name clauseName
94
95 clauseStuntDoubleIndex ::= index clauseIndex
96
97 * </pre>
98 */
99 class SelectionCompiler{
100 public:
101 bool compile(const std::string& filename, const std::string& script );
102
103
104 std::vector<int> getLineNumbers() {
105 return lineNumbers;
106 }
107
108 std::vector<int> getLineIndices() {
109 return lineIndices;
110 }
111
112 std::vector<std::vector<Token> > getAatokenCompiled() {
113 return aatokenCompiled;
114 }
115
116 std::string getErrorMessage() {
117 std::string strError = errorMessage;
118 strError += " : " + errorLine + "\n";
119
120 if (!filename.empty()) {
121 strError += filename;
122 }
123
124 strError += " line#" + lineCurrent;
125 return strError;
126 }
127
128
129 private:
130
131 bool internalCompile();
132
133
134 bool lookingAtLeadingWhitespace();
135 bool lookingAtComment();
136 bool lookingAtEndOfLine();
137 bool lookingAtEndOfStatement();
138 bool lookingAtString();
139 bool lookingAtDecimal(bool allowNegative);
140 bool lookingAtInteger(bool allowNegative);
141 bool lookingAtLookupToken();
142 bool lookingAtSpecialString();
143
144 std::string getUnescapedStringLiteral();
145 int getHexitValue(char ch);
146
147 bool compileCommand(const std::vector<Token>& ltoken);
148 bool compileExpression();
149 bool compileExpression(int itoken);
150
151 bool clauseOr();
152 bool clauseAnd();
153 bool clauseNot();
154 bool clausePrimitive();
155 bool clauseWithin();
156 bool clauseComparator();
157 bool clauseName(int tok);
158 bool clauseIndex(int tok);
159
160 Token tokenNext();
161 boost::any valuePeek();
162 int tokPeek();
163
164 bool addTokenToPostfix(const Token& token);
165
166
167 bool compileError(const std::string& errorMessage) {
168 std::cerr << "SelectionCompiler Error: " << errorMessage << std::endl;
169 error = true;
170 this->errorMessage = errorMessage;
171 return false;
172 }
173
174 bool commandExpected() {
175 return compileError("command expected");
176 }
177
178 bool invalidExpressionToken(const std::string& ident) {
179 return compileError("invalid expression token:" + ident);
180 }
181
182 bool unrecognizedToken() {
183 return compileError("unrecognized token");
184 }
185
186 bool badArgumentCount() {
187 return compileError("bad argument count");
188 }
189
190 bool endOfExpressionExpected() {
191 return compileError("end of expression expected");
192 }
193
194 bool leftParenthesisExpected() {
195 return compileError("left parenthesis expected");
196 }
197
198 bool rightParenthesisExpected() {
199 return compileError("right parenthesis expected");
200 }
201
202 bool commaExpected() {
203 return compileError("comma expected");
204 }
205
206 bool unrecognizedExpressionToken() {
207 boost::any tmp = valuePeek();
208 std::string tokenStr;
209
210 try {
211 tokenStr = boost::any_cast<std::string>(tmp);
212 } catch(const boost::bad_any_cast &) {
213 return compileError("any_cast error");
214 }
215
216 return compileError("unrecognized expression token:" + tokenStr);
217 }
218
219 bool comparisonOperatorExpected() {
220 return compileError("comparison operator expected");
221 }
222
223 bool integerExpected() {
224 return compileError("integer expected");
225 }
226
227 bool numberOrKeywordExpected() {
228 return compileError("number or keyword expected");
229 }
230
231 std::string filename;
232 std::string script;
233
234 std::vector<int> lineNumbers;
235 std::vector<int> lineIndices;
236 std::vector<std::vector<Token> >aatokenCompiled;
237
238 bool error;
239 std::string errorMessage;
240 std::string errorLine;
241
242 int cchScript;
243 short lineCurrent;
244
245 int ichToken;
246 int cchToken;
247 std::vector<Token> atokenCommand;
248
249 int ichCurrentCommand;
250
251 std::vector<Token> ltokenPostfix;
252 std::vector<Token> atokenInfix;
253 int itokenInfix;
254
255 //std::vector<Token> compiledTokens_;
256 };
257
258 }
259 #endif
260