41 |
|
|
42 |
|
#ifndef SELECTION_SELECTIONCOMPILER_HPP |
43 |
|
#define SELECTION_SELECTIONCOMPILER_HPP |
44 |
< |
|
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 |
66 |
|
|
67 |
|
clausePrimitive ::= clauseComparator | |
68 |
|
clauseWithin | |
69 |
< |
clauseResidueSpec | |
69 |
> |
clauseName | |
70 |
|
none | all | |
71 |
|
( clauseOr ) |
72 |
|
|
75 |
|
clauseWithin ::= WITHIN ( clauseDistance , expression ) |
76 |
|
|
77 |
|
clauseDistance ::= integer | decimal |
78 |
+ |
|
79 |
+ |
clauseName::= *|string{.string{.string}} |
80 |
|
|
74 |
– |
|
81 |
|
|
82 |
+ |
* </pre> |
83 |
+ |
*/ |
84 |
+ |
class SelectionCompiler{ |
85 |
+ |
public: |
86 |
+ |
bool compile(const std::string& filename, const std::string& script ); |
87 |
+ |
|
88 |
|
|
89 |
< |
clauseResidueSpec::= { clauseResNameSpec } |
90 |
< |
{ clauseResNumSpec } |
91 |
< |
{ chainSpec } |
80 |
< |
{ clauseAtomSpec } |
81 |
< |
{ modelSpec } |
89 |
> |
std::vector<int> getLineNumbers() { |
90 |
> |
return lineNumbers; |
91 |
> |
} |
92 |
|
|
93 |
< |
clauseResNameSpec::= * | [ resNamePattern ] | resNamePattern |
93 |
> |
std::vector<int> getLineIndices() { |
94 |
> |
return lineIndices; |
95 |
> |
} |
96 |
|
|
97 |
< |
// question marks are part of identifiers |
98 |
< |
// they get split up and dealt with as wildcards at runtime |
99 |
< |
// and the integers which are residue number chains get bundled |
88 |
< |
// in with the identifier and also split out at runtime |
89 |
< |
// iff a variable of that name does not exist |
97 |
> |
std::vector<std::vector<Token> > getAatokenCompiled() { |
98 |
> |
return aatokenCompiled; |
99 |
> |
} |
100 |
|
|
101 |
< |
resNamePattern ::= up to 3 alphanumeric chars with * and ? |
101 |
> |
std::string getErrorMessage() { |
102 |
> |
std::string strError = errorMessage; |
103 |
> |
strError += " : " + errorLine + "\n"; |
104 |
|
|
105 |
< |
clauseResNumSpec ::= * | clauseSequenceRange |
105 |
> |
if (!filename.empty()) { |
106 |
> |
strError += filename; |
107 |
> |
} |
108 |
|
|
109 |
< |
clauseSequenceRange ::= clauseSequenceCode { - clauseSequenceCode } |
109 |
> |
strError += " line#" + lineCurrent; |
110 |
> |
return strError; |
111 |
> |
} |
112 |
|
|
113 |
< |
clauseSequenceCode ::= seqcode | {-} integer |
113 |
> |
|
114 |
> |
private: |
115 |
|
|
116 |
< |
clauseChainSpec ::= {:} * | identifier | integer |
116 |
> |
bool internalCompile(); |
117 |
|
|
101 |
– |
clauseAtomSpec ::= . * | . identifier {*} // note that this * is *not* a wildcard |
118 |
|
|
119 |
< |
clauseModelSpec ::= {:|/} * | integer |
120 |
< |
|
121 |
< |
* </pre> |
122 |
< |
*/ |
123 |
< |
class SelectionCompiler{ |
124 |
< |
public: |
125 |
< |
bool compile(); |
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::vector<Token> getCompiledTokens(); |
130 |
< |
private: |
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 clauseComparator(); |
142 |
+ |
bool clauseChemObjName(); |
143 |
+ |
bool clauseName(std::string& name); |
144 |
+ |
bool clauseIndex(); |
145 |
+ |
Token tokenNext(); |
146 |
+ |
boost::any valuePeek(); |
147 |
+ |
int tokPeek(); |
148 |
+ |
|
149 |
+ |
bool addTokenToPostfix(const Token& token); |
150 |
+ |
|
151 |
+ |
|
152 |
+ |
bool compileError(const std::string& errorMessage) { |
153 |
+ |
std::cerr << "SelectionCompiler Error: " << errorMessage << std::endl; |
154 |
+ |
error = true; |
155 |
+ |
this->errorMessage = errorMessage; |
156 |
+ |
return false; |
157 |
+ |
} |
158 |
|
|
159 |
< |
internalCompile(); |
159 |
> |
bool commandExpected() { |
160 |
> |
return compileError("command expected"); |
161 |
> |
} |
162 |
|
|
163 |
< |
std::vector<Token> compiledTokens_; |
163 |
> |
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 |
> |
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 |
> |
} |
203 |
> |
|
204 |
> |
bool comparisonOperatorExpected() { |
205 |
> |
return compileError("comparison operator expected"); |
206 |
> |
} |
207 |
> |
|
208 |
> |
bool numberExpected() { |
209 |
> |
return compileError("number expected"); |
210 |
> |
} |
211 |
> |
|
212 |
> |
bool numberOrKeywordExpected() { |
213 |
> |
return compileError("number or keyword expected"); |
214 |
> |
} |
215 |
> |
|
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 |
|
}; |
242 |
|
|
243 |
|
} |
244 |
|
#endif |
245 |
+ |
|