| 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. Redistributions of source code must retain the above copyright | 
| 10 | *    notice, this list of conditions and the following disclaimer. | 
| 11 | * | 
| 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. | 
| 16 | * | 
| 17 | * This software is provided "AS IS," without a warranty of any | 
| 18 | * kind. All express or implied conditions, representations and | 
| 19 | * warranties, including any implied warranty of merchantability, | 
| 20 | * fitness for a particular purpose or non-infringement, are hereby | 
| 21 | * excluded.  The University of Notre Dame and its licensors shall not | 
| 22 | * be liable for any damages suffered by licensee as a result of | 
| 23 | * using, modifying or distributing the software or its | 
| 24 | * derivatives. In no event will the University of Notre Dame or its | 
| 25 | * licensors be liable for any lost revenue, profit or data, or for | 
| 26 | * direct, indirect, special, consequential, incidental or punitive | 
| 27 | * damages, however caused and regardless of the theory of liability, | 
| 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 | #include <iostream> | 
| 46 | #include <string> | 
| 47 | #include <vector> | 
| 48 |  | 
| 49 | #include "selection/SelectionToken.hpp" | 
| 50 | #include "selection/TokenMap.hpp" | 
| 51 | #include "brains/SimInfo.hpp" | 
| 52 |  | 
| 53 | namespace OpenMD { | 
| 54 |  | 
| 55 |  | 
| 56 | /** | 
| 57 | * @class SelectionCompiler SelectionCompiler.hpp "selection/SelectionCompiler.hpp" | 
| 58 | * @brief compile a selection script to tokens | 
| 59 | * @todo document | 
| 60 | * <pre> | 
| 61 |  | 
| 62 | expression       :: = clauseOr | 
| 63 |  | 
| 64 | clauseOr         ::= clauseAnd {OR clauseAnd}* | 
| 65 |  | 
| 66 | clauseAnd        ::= clauseNot {AND clauseNot}* | 
| 67 |  | 
| 68 | clauseNot        ::= NOT clauseNot | clausePrimitive | 
| 69 |  | 
| 70 | clausePrimitive  ::= clauseComparator | | 
| 71 | clauseWithin | | 
| 72 | clauseName | | 
| 73 | none | all | | 
| 74 | ( clauseOr ) | 
| 75 |  | 
| 76 | clauseComparator ::= atomproperty comparatorop integer | 
| 77 |  | 
| 78 | clauseWithin     ::= WITHIN ( clauseDistance , expression ) | 
| 79 |  | 
| 80 | clauseDistance   ::= integer | decimal | 
| 81 |  | 
| 82 | clauseName::= *|string{.string{.string}} | 
| 83 |  | 
| 84 |  | 
| 85 | * </pre> | 
| 86 | */ | 
| 87 | class SelectionCompiler{ | 
| 88 | public: | 
| 89 | bool compile(const std::string& filename, const std::string& script ); | 
| 90 |  | 
| 91 |  | 
| 92 | std::vector<int> getLineNumbers() { | 
| 93 | return lineNumbers; | 
| 94 | } | 
| 95 |  | 
| 96 | std::vector<int> getLineIndices() { | 
| 97 | return lineIndices; | 
| 98 | } | 
| 99 |  | 
| 100 | std::vector<std::vector<Token> > getAatokenCompiled() { | 
| 101 | return aatokenCompiled; | 
| 102 | } | 
| 103 |  | 
| 104 | std::string getErrorMessage() { | 
| 105 | std::string strError = errorMessage; | 
| 106 | strError += " : " + errorLine + "\n"; | 
| 107 |  | 
| 108 | if (!filename.empty()) { | 
| 109 | strError += filename; | 
| 110 | } | 
| 111 |  | 
| 112 | return strError; | 
| 113 | } | 
| 114 |  | 
| 115 |  | 
| 116 | private: | 
| 117 |  | 
| 118 | bool internalCompile(); | 
| 119 |  | 
| 120 |  | 
| 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 | 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 | 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 | std::size_t itokenInfix; | 
| 246 |  | 
| 247 | //std::vector<Token> compiledTokens_; | 
| 248 | }; | 
| 249 |  | 
| 250 | } | 
| 251 | #endif | 
| 252 |  |