| 1 | tim | 277 | /* | 
| 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 | tim | 281 | #include <iostream> | 
| 45 | tim | 278 | #include <string> | 
| 46 | tim | 277 | #include <vector> | 
| 47 | tim | 281 |  | 
| 48 |  |  | #include "selection/Token.hpp" | 
| 49 |  |  | #include "selection/TokenMap.hpp" | 
| 50 | tim | 277 | namespace oopse { | 
| 51 |  |  |  | 
| 52 |  |  |  | 
| 53 |  |  | /** | 
| 54 |  |  | * @class SelectionCompiler SelectionCompiler.hpp "selection/SelectionCompiler.hpp" | 
| 55 |  |  | * @brief compile a selection script to tokens | 
| 56 | tim | 278 | * @todo document | 
| 57 | tim | 277 | * <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 | tim | 278 | clauseChemObject | | 
| 70 | tim | 277 | none | all | | 
| 71 |  |  | ( clauseOr ) | 
| 72 |  |  |  | 
| 73 |  |  | clauseComparator ::= atomproperty comparatorop integer | 
| 74 |  |  |  | 
| 75 |  |  | clauseWithin     ::= WITHIN ( clauseDistance , expression ) | 
| 76 |  |  |  | 
| 77 |  |  | clauseDistance   ::= integer | decimal | 
| 78 | tim | 278 |  | 
| 79 |  |  | clauseChemObject::= {clauseMolecule} | {clauseStuntDouble} | 
| 80 | tim | 277 |  | 
| 81 | tim | 278 | clauseMolecule ::= {clauseMolName} | {clauseMolIndex} | 
| 82 |  |  |  | 
| 83 |  |  | clauseMolName ::= molname clauseName | 
| 84 | tim | 277 |  | 
| 85 | tim | 278 | clauseName::= *|string | 
| 86 | tim | 277 |  | 
| 87 | tim | 278 | clauseMolIndex ::= molindex clauseIndex | 
| 88 |  |  |  | 
| 89 |  |  | clauseIndex ::= integer {- integer } | 
| 90 |  |  |  | 
| 91 |  |  | clauseStuntDouble ::= {clauseStuntDoubleName} | {clauseStuntDoubleIndex} | 
| 92 | tim | 277 |  | 
| 93 | tim | 278 | clauseStuntDoubleName ::= name clauseName | 
| 94 | tim | 277 |  | 
| 95 | tim | 278 | clauseStuntDoubleIndex ::= index clauseIndex | 
| 96 | tim | 277 |  | 
| 97 | tim | 278 | * </pre> | 
| 98 |  |  | */ | 
| 99 |  |  | class SelectionCompiler{ | 
| 100 |  |  | public: | 
| 101 |  |  | bool compile(const std::string& filename, const std::string& script ); | 
| 102 |  |  |  | 
| 103 | tim | 277 |  | 
| 104 | tim | 278 | std::vector<int> getLineNumbers() { | 
| 105 |  |  | return lineNumbers; | 
| 106 |  |  | } | 
| 107 | tim | 277 |  | 
| 108 | tim | 278 | std::vector<int> getLineIndices() { | 
| 109 |  |  | return lineIndices; | 
| 110 |  |  | } | 
| 111 | tim | 277 |  | 
| 112 | tim | 278 | std::vector<std::vector<Token> > getAatokenCompiled() { | 
| 113 |  |  | return aatokenCompiled; | 
| 114 |  |  | } | 
| 115 | tim | 277 |  | 
| 116 | tim | 278 | std::string getErrorMessage() { | 
| 117 |  |  | std::string strError = errorMessage; | 
| 118 |  |  | strError += " : " + errorLine + "\n"; | 
| 119 | tim | 277 |  | 
| 120 | tim | 278 | if (!filename.empty()) { | 
| 121 |  |  | strError += filename; | 
| 122 |  |  | } | 
| 123 | tim | 277 |  | 
| 124 | tim | 278 | strError += " line#" + lineCurrent; | 
| 125 |  |  | return strError; | 
| 126 |  |  | } | 
| 127 | tim | 277 |  | 
| 128 | tim | 278 |  | 
| 129 | tim | 277 | private: | 
| 130 |  |  |  | 
| 131 | tim | 281 | bool internalCompile(); | 
| 132 | tim | 278 |  | 
| 133 |  |  |  | 
| 134 |  |  | bool lookingAtLeadingWhitespace(); | 
| 135 |  |  | bool lookingAtComment(); | 
| 136 |  |  | bool lookingAtEndOfLine(); | 
| 137 |  |  | bool lookingAtEndOfStatement(); | 
| 138 |  |  | bool lookingAtString(); | 
| 139 | tim | 281 | bool lookingAtDecimal(bool allowNegative); | 
| 140 |  |  | bool lookingAtInteger(bool allowNegative); | 
| 141 |  |  | bool lookingAtLookupToken(); | 
| 142 |  |  | bool lookingAtSpecialString(); | 
| 143 | tim | 278 |  | 
| 144 | tim | 281 | std::string getUnescapedStringLiteral(); | 
| 145 |  |  | int getHexitValue(char ch); | 
| 146 | tim | 278 |  | 
| 147 | tim | 281 | bool compileCommand(const std::vector<Token>& ltoken); | 
| 148 |  |  | bool compileExpression(); | 
| 149 |  |  | bool compileExpression(int itoken); | 
| 150 | tim | 278 |  | 
| 151 | tim | 277 | bool clauseOr(); | 
| 152 |  |  | bool clauseAnd(); | 
| 153 |  |  | bool clauseNot(); | 
| 154 |  |  | bool clausePrimitive(); | 
| 155 |  |  | bool clauseWithin(); | 
| 156 |  |  | bool clauseComparator(); | 
| 157 | tim | 281 | bool clauseName(int tok); | 
| 158 |  |  | bool clauseIndex(int tok); | 
| 159 | tim | 278 |  | 
| 160 | tim | 281 | Token tokenNext(); | 
| 161 |  |  | boost::any valuePeek(); | 
| 162 |  |  | int tokPeek(); | 
| 163 |  |  |  | 
| 164 |  |  | bool addTokenToPostfix(const Token& token); | 
| 165 |  |  |  | 
| 166 |  |  |  | 
| 167 | tim | 278 | bool compileError(const std::string& errorMessage) { | 
| 168 | tim | 281 | std::cerr << "SelectionCompiler Error: " << errorMessage << std::endl; | 
| 169 | tim | 278 | error = true; | 
| 170 | tim | 281 | this->errorMessage = errorMessage; | 
| 171 | tim | 278 | return false; | 
| 172 |  |  | } | 
| 173 | tim | 277 |  | 
| 174 | tim | 278 | bool commandExpected() { | 
| 175 |  |  | return compileError("command expected"); | 
| 176 |  |  | } | 
| 177 | tim | 277 |  | 
| 178 | tim | 278 | 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 | tim | 281 | 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 | tim | 278 | } | 
| 218 |  |  |  | 
| 219 |  |  | bool comparisonOperatorExpected() { | 
| 220 |  |  | return compileError("comparison operator expected"); | 
| 221 |  |  | } | 
| 222 |  |  |  | 
| 223 |  |  | bool integerExpected() { | 
| 224 |  |  | return compileError("integer expected"); | 
| 225 |  |  | } | 
| 226 |  |  |  | 
| 227 | tim | 281 | bool numberOrKeywordExpected() { | 
| 228 |  |  | return compileError("number or keyword expected"); | 
| 229 |  |  | } | 
| 230 | tim | 278 |  | 
| 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 | tim | 277 | }; | 
| 257 |  |  |  | 
| 258 |  |  | } | 
| 259 |  |  | #endif | 
| 260 | tim | 278 |  |