# | Line 41 | Line 41 | |
---|---|---|
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 | |
# | Line 61 | Line 66 | namespace oopse { | |
66 | ||
67 | clausePrimitive ::= clauseComparator | | |
68 | clauseWithin | | |
69 | < | clauseResidueSpec | |
69 | > | clauseChemObject | |
70 | none | all | | |
71 | ( clauseOr ) | |
72 | ||
# | Line 70 | Line 75 | namespace oopse { | |
75 | clauseWithin ::= WITHIN ( clauseDistance , expression ) | |
76 | ||
77 | clauseDistance ::= integer | decimal | |
73 | – | |
78 | ||
79 | + | clauseChemObject::= {clauseMolecule} | {clauseStuntDouble} |
80 | ||
81 | + | clauseMolecule ::= {clauseMolName} | {clauseMolIndex} |
82 | ||
83 | < | clauseResidueSpec::= { clauseResNameSpec } |
84 | < | { clauseResNumSpec } |
85 | < | { chainSpec } |
80 | < | { clauseAtomSpec } |
81 | < | { modelSpec } |
83 | > | clauseMolName ::= molname clauseName |
84 | > | |
85 | > | clauseName::= *|string |
86 | ||
87 | < | clauseResNameSpec::= * | [ resNamePattern ] | resNamePattern |
87 | > | clauseMolIndex ::= molindex clauseIndex |
88 | > | |
89 | > | clauseIndex ::= integer {- integer } |
90 | > | |
91 | > | clauseStuntDouble ::= {clauseStuntDoubleName} | {clauseStuntDoubleIndex} |
92 | ||
93 | < | // question marks are part of identifiers |
86 | < | // they get split up and dealt with as wildcards at runtime |
87 | < | // 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 |
93 | > | clauseStuntDoubleName ::= name clauseName |
94 | ||
95 | < | resNamePattern ::= up to 3 alphanumeric chars with * and ? |
95 | > | clauseStuntDoubleIndex ::= index clauseIndex |
96 | ||
97 | < | clauseResNumSpec ::= * | clauseSequenceRange |
97 | > | * </pre> |
98 | > | */ |
99 | > | class SelectionCompiler{ |
100 | > | public: |
101 | > | bool compile(const std::string& filename, const std::string& script ); |
102 | > | |
103 | ||
104 | < | clauseSequenceRange ::= clauseSequenceCode { - clauseSequenceCode } |
104 | > | std::vector<int> getLineNumbers() { |
105 | > | return lineNumbers; |
106 | > | } |
107 | ||
108 | < | clauseSequenceCode ::= seqcode | {-} integer |
108 | > | std::vector<int> getLineIndices() { |
109 | > | return lineIndices; |
110 | > | } |
111 | ||
112 | < | clauseChainSpec ::= {:} * | identifier | integer |
112 | > | std::vector<std::vector<Token> > getAatokenCompiled() { |
113 | > | return aatokenCompiled; |
114 | > | } |
115 | ||
116 | < | clauseAtomSpec ::= . * | . identifier {*} // note that this * is *not* a wildcard |
116 | > | std::string getErrorMessage() { |
117 | > | std::string strError = errorMessage; |
118 | > | strError += " : " + errorLine + "\n"; |
119 | ||
120 | < | clauseModelSpec ::= {:|/} * | integer |
121 | < | |
122 | < | * </pre> |
106 | < | */ |
107 | < | class SelectionCompiler{ |
108 | < | public: |
109 | < | bool compile(); |
120 | > | if (!filename.empty()) { |
121 | > | strError += filename; |
122 | > | } |
123 | ||
124 | < | std::vector<Token> getCompiledTokens(); |
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 | < | internalCompile(); |
174 | > | bool commandExpected() { |
175 | > | return compileError("command expected"); |
176 | > | } |
177 | ||
178 | < | std::vector<Token> compiledTokens_; |
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 | + |
– | Removed lines |
+ | Added lines |
< | Changed lines |
> | Changed lines |