| 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 | 
| 9 | > | * 1. Redistributions of source code must retain the above copyright | 
| 10 |  | *    notice, this list of conditions and the following disclaimer. | 
| 11 |  | * | 
| 12 | < | * 3. Redistributions in binary form must reproduce the above copyright | 
| 12 | > | * 2. Redistributions in binary form must reproduce the above copyright | 
| 13 |  | *    notice, this list of conditions and the following disclaimer in the | 
| 14 |  | *    documentation and/or other materials provided with the | 
| 15 |  | *    distribution. | 
| 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 | + | * | 
| 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, 234107 (2008). | 
| 39 | + | * [4]  Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010). | 
| 40 | + | * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). | 
| 41 |  | */ | 
| 42 |  |  | 
| 43 |  | #ifndef SELECTION_SELECTIONCOMPILER_HPP | 
| 44 |  | #define SELECTION_SELECTIONCOMPILER_HPP | 
| 45 | < |  | 
| 45 | > | #include <iostream> | 
| 46 | > | #include <string> | 
| 47 |  | #include <vector> | 
| 46 | – | namespace oopse { | 
| 48 |  |  | 
| 49 | + | #include "selection/SelectionToken.hpp" | 
| 50 | + | #include "selection/TokenMap.hpp" | 
| 51 | + | #include "brains/SimInfo.hpp" | 
| 52 |  |  | 
| 53 | < | /** | 
| 50 | < | * @class SelectionCompiler SelectionCompiler.hpp "selection/SelectionCompiler.hpp" | 
| 51 | < | * @brief compile a selection script to tokens | 
| 52 | < | * <pre> | 
| 53 | > | namespace OpenMD { | 
| 54 |  |  | 
| 54 | – | expression       :: = clauseOr | 
| 55 |  |  | 
| 56 | < | clauseOr         ::= clauseAnd {OR clauseAnd}* | 
| 56 | > | /** | 
| 57 | > | * @class SelectionCompiler SelectionCompiler.hpp "selection/SelectionCompiler.hpp" | 
| 58 | > | * @brief compile a selection script to tokens | 
| 59 | > | * @todo document | 
| 60 | > | * <pre> | 
| 61 |  |  | 
| 62 | < | clauseAnd        ::= clauseNot {AND clauseNot}* | 
| 62 | > | expression       :: = clauseOr | 
| 63 |  |  | 
| 64 | < | clauseNot        ::= NOT clauseNot | clausePrimitive | 
| 64 | > | clauseOr         ::= clauseAnd {OR clauseAnd}* | 
| 65 |  |  | 
| 66 | < | clausePrimitive  ::= clauseComparator | | 
| 63 | < | clauseWithin | | 
| 64 | < | clauseResidueSpec | | 
| 65 | < | none | all | | 
| 66 | < | ( clauseOr ) | 
| 66 | > | clauseAnd        ::= clauseNot {AND clauseNot}* | 
| 67 |  |  | 
| 68 | < | clauseComparator ::= atomproperty comparatorop integer | 
| 68 | > | clauseNot        ::= NOT clauseNot | clausePrimitive | 
| 69 |  |  | 
| 70 | < | clauseWithin     ::= WITHIN ( clauseDistance , expression ) | 
| 70 | > | clausePrimitive  ::= clauseComparator | | 
| 71 | > | clauseWithin | | 
| 72 | > | clauseName | | 
| 73 | > | none | all | | 
| 74 | > | ( clauseOr ) | 
| 75 |  |  | 
| 76 | < | clauseDistance   ::= integer | decimal | 
| 76 | > | clauseComparator ::= atomproperty comparatorop integer | 
| 77 |  |  | 
| 78 | < |  | 
| 78 | > | clauseWithin     ::= WITHIN ( clauseDistance , expression ) | 
| 79 |  |  | 
| 80 | + | clauseDistance   ::= integer | decimal | 
| 81 | + |  | 
| 82 | + | clauseName::= *|string{.string{.string}} | 
| 83 |  |  | 
| 77 | – | clauseResidueSpec::= { clauseResNameSpec } | 
| 78 | – | { clauseResNumSpec } | 
| 79 | – | { chainSpec } | 
| 80 | – | { clauseAtomSpec } | 
| 81 | – | { modelSpec } | 
| 84 |  |  | 
| 85 | < | clauseResNameSpec::= * | [ resNamePattern ] | resNamePattern | 
| 85 | > | * </pre> | 
| 86 | > | */ | 
| 87 | > | class SelectionCompiler{ | 
| 88 | > | public: | 
| 89 | > | bool compile(const std::string& filename, const std::string& script ); | 
| 90 | > |  | 
| 91 |  |  | 
| 92 | < | // question marks are part of identifiers | 
| 93 | < | // they get split up and dealt with as wildcards at runtime | 
| 94 | < | // 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 | 
| 92 | > | std::vector<int> getLineNumbers() { | 
| 93 | > | return lineNumbers; | 
| 94 | > | } | 
| 95 |  |  | 
| 96 | < | resNamePattern   ::= up to 3 alphanumeric chars with * and ? | 
| 96 | > | std::vector<int> getLineIndices() { | 
| 97 | > | return lineIndices; | 
| 98 | > | } | 
| 99 |  |  | 
| 100 | < | clauseResNumSpec ::= * | clauseSequenceRange | 
| 100 | > | std::vector<std::vector<Token> > getAatokenCompiled() { | 
| 101 | > | return aatokenCompiled; | 
| 102 | > | } | 
| 103 |  |  | 
| 104 | < | clauseSequenceRange ::= clauseSequenceCode { - clauseSequenceCode } | 
| 104 | > | std::string getErrorMessage() { | 
| 105 | > | std::string strError = errorMessage; | 
| 106 | > | strError += " : " + errorLine + "\n"; | 
| 107 |  |  | 
| 108 | < | clauseSequenceCode ::= seqcode | {-} integer | 
| 108 | > | if (!filename.empty()) { | 
| 109 | > | strError += filename; | 
| 110 | > | } | 
| 111 |  |  | 
| 112 | < | clauseChainSpec  ::= {:} * | identifier | integer | 
| 112 | > | return strError; | 
| 113 | > | } | 
| 114 |  |  | 
| 115 | < | clauseAtomSpec   ::= . * | . identifier {*} // note that this * is *not* a wildcard | 
| 115 | > |  | 
| 116 | > | private: | 
| 117 |  |  | 
| 118 | < | clauseModelSpec  ::= {:|/} * | integer | 
| 104 | < |  | 
| 105 | < | * </pre> | 
| 106 | < | */ | 
| 107 | < | class SelectionCompiler{ | 
| 108 | < | public: | 
| 109 | < | bool compile(); | 
| 118 | > | bool internalCompile(); | 
| 119 |  |  | 
| 111 | – | std::vector<Token> getCompiledTokens(); | 
| 112 | – | private: | 
| 120 |  |  | 
| 121 | < | bool clauseOr(); | 
| 122 | < | bool clauseAnd(); | 
| 123 | < | bool clauseNot(); | 
| 124 | < | bool clausePrimitive(); | 
| 125 | < | bool clauseWithin(); | 
| 126 | < | bool clauseComparator(); | 
| 121 | > | 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 | > |  | 
| 131 | > | std::string getUnescapedStringLiteral(); | 
| 132 | > | int getHexitValue(char ch); | 
| 133 | > |  | 
| 134 | > | bool compileCommand(const std::vector<Token>& ltoken); | 
| 135 | > | bool compileExpression(); | 
| 136 | > | bool compileExpression(int itoken); | 
| 137 |  |  | 
| 138 | < | internalCompile(); | 
| 138 | > | 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 |  |  | 
| 150 | < | std::vector<Token> compiledTokens_; | 
| 151 | < | }; | 
| 150 | > | bool addTokenToPostfix(const Token& token); | 
| 151 | > | bool isNameValid(const std::string& name); | 
| 152 |  |  | 
| 153 | + | bool compileError(const std::string& errorMsg) { | 
| 154 | + |  | 
| 155 | + | sprintf( painCave.errMsg, | 
| 156 | + | "SelectionCompiler Error: %s\n", errorMsg.c_str()); | 
| 157 | + | painCave.severity = OPENMD_ERROR; | 
| 158 | + | painCave.isFatal = 1; | 
| 159 | + | simError(); | 
| 160 | + |  | 
| 161 | + | error = true; | 
| 162 | + | this->errorMessage = errorMsg; | 
| 163 | + | return false; | 
| 164 | + | } | 
| 165 | + |  | 
| 166 | + | bool commandExpected() { | 
| 167 | + | return compileError("command expected"); | 
| 168 | + | } | 
| 169 | + |  | 
| 170 | + | bool invalidExpressionToken(const std::string& ident) { | 
| 171 | + | return compileError("invalid expression token:" + ident); | 
| 172 | + | } | 
| 173 | + |  | 
| 174 | + | bool unrecognizedToken() { | 
| 175 | + | return compileError("unrecognized token"); | 
| 176 | + | } | 
| 177 | + |  | 
| 178 | + | bool badArgumentCount() { | 
| 179 | + | return compileError("bad argument count"); | 
| 180 | + | } | 
| 181 | + |  | 
| 182 | + | bool endOfExpressionExpected() { | 
| 183 | + | return compileError("end of expression expected"); | 
| 184 | + | } | 
| 185 | + |  | 
| 186 | + | bool leftParenthesisExpected() { | 
| 187 | + | return compileError("left parenthesis expected"); | 
| 188 | + | } | 
| 189 | + |  | 
| 190 | + | bool rightParenthesisExpected() { | 
| 191 | + | return compileError("right parenthesis expected"); | 
| 192 | + | } | 
| 193 | + |  | 
| 194 | + | bool commaExpected() { | 
| 195 | + | return compileError("comma expected"); | 
| 196 | + | } | 
| 197 | + |  | 
| 198 | + | bool unrecognizedExpressionToken() { | 
| 199 | + | boost::any tmp = valuePeek(); | 
| 200 | + | std::string tokenStr; | 
| 201 | + |  | 
| 202 | + | try { | 
| 203 | + | tokenStr = boost::any_cast<std::string>(tmp); | 
| 204 | + | } catch(const boost::bad_any_cast &) { | 
| 205 | + | return compileError("any_cast error"); | 
| 206 | + | } | 
| 207 | + |  | 
| 208 | + | return compileError("unrecognized expression token:" + tokenStr); | 
| 209 | + | } | 
| 210 | + |  | 
| 211 | + | bool comparisonOperatorExpected() { | 
| 212 | + | return compileError("comparison operator expected"); | 
| 213 | + | } | 
| 214 | + |  | 
| 215 | + | bool numberExpected() { | 
| 216 | + | return compileError("number expected"); | 
| 217 | + | } | 
| 218 | + |  | 
| 219 | + | bool numberOrKeywordExpected() { | 
| 220 | + | return compileError("number or keyword expected"); | 
| 221 | + | } | 
| 222 | + |  | 
| 223 | + | std::string filename; | 
| 224 | + | std::string script; | 
| 225 | + |  | 
| 226 | + | std::vector<int> lineNumbers; | 
| 227 | + | std::vector<int> lineIndices; | 
| 228 | + | std::vector<std::vector<Token> >aatokenCompiled; | 
| 229 | + |  | 
| 230 | + | bool error; | 
| 231 | + | std::string errorMessage; | 
| 232 | + | std::string errorLine; | 
| 233 | + |  | 
| 234 | + | int cchScript; | 
| 235 | + | short lineCurrent; | 
| 236 | + |  | 
| 237 | + | int ichToken; | 
| 238 | + | int cchToken; | 
| 239 | + | std::vector<Token> atokenCommand; | 
| 240 | + |  | 
| 241 | + | int ichCurrentCommand; | 
| 242 | + |  | 
| 243 | + | std::vector<Token> ltokenPostfix; | 
| 244 | + | std::vector<Token> atokenInfix; | 
| 245 | + | int itokenInfix; | 
| 246 | + |  | 
| 247 | + | //std::vector<Token> compiledTokens_; | 
| 248 | + | }; | 
| 249 | + |  | 
| 250 |  | } | 
| 251 |  | #endif | 
| 252 | + |  |