| 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. 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 | #include <string> | 
| 45 | #include <vector> | 
| 46 | namespace oopse { | 
| 47 |  | 
| 48 |  | 
| 49 | /** | 
| 50 | * @class SelectionCompiler SelectionCompiler.hpp "selection/SelectionCompiler.hpp" | 
| 51 | * @brief compile a selection script to tokens | 
| 52 | * @todo document | 
| 53 | * <pre> | 
| 54 |  | 
| 55 | expression       :: = clauseOr | 
| 56 |  | 
| 57 | clauseOr         ::= clauseAnd {OR clauseAnd}* | 
| 58 |  | 
| 59 | clauseAnd        ::= clauseNot {AND clauseNot}* | 
| 60 |  | 
| 61 | clauseNot        ::= NOT clauseNot | clausePrimitive | 
| 62 |  | 
| 63 | clausePrimitive  ::= clauseComparator | | 
| 64 | clauseWithin | | 
| 65 | clauseChemObject | | 
| 66 | none | all | | 
| 67 | ( clauseOr ) | 
| 68 |  | 
| 69 | clauseComparator ::= atomproperty comparatorop integer | 
| 70 |  | 
| 71 | clauseWithin     ::= WITHIN ( clauseDistance , expression ) | 
| 72 |  | 
| 73 | clauseDistance   ::= integer | decimal | 
| 74 |  | 
| 75 | clauseChemObject::= {clauseMolecule} | {clauseStuntDouble} | 
| 76 |  | 
| 77 | clauseMolecule ::= {clauseMolName} | {clauseMolIndex} | 
| 78 |  | 
| 79 | clauseMolName ::= molname clauseName | 
| 80 |  | 
| 81 | clauseName::= *|string | 
| 82 |  | 
| 83 | clauseMolIndex ::= molindex clauseIndex | 
| 84 |  | 
| 85 | clauseIndex ::= integer {- integer } | 
| 86 |  | 
| 87 | clauseStuntDouble ::= {clauseStuntDoubleName} | {clauseStuntDoubleIndex} | 
| 88 |  | 
| 89 | clauseStuntDoubleName ::= name clauseName | 
| 90 |  | 
| 91 | clauseStuntDoubleIndex ::= index clauseIndex | 
| 92 |  | 
| 93 | * </pre> | 
| 94 | */ | 
| 95 | class SelectionCompiler{ | 
| 96 | public: | 
| 97 | bool compile(const std::string& filename, const std::string& script ); | 
| 98 |  | 
| 99 |  | 
| 100 | std::vector<int> getLineNumbers() { | 
| 101 | return lineNumbers; | 
| 102 | } | 
| 103 |  | 
| 104 | std::vector<int> getLineIndices() { | 
| 105 | return lineIndices; | 
| 106 | } | 
| 107 |  | 
| 108 | std::vector<std::vector<Token> > getAatokenCompiled() { | 
| 109 | return aatokenCompiled; | 
| 110 | } | 
| 111 |  | 
| 112 | std::string getErrorMessage() { | 
| 113 | std::string strError = errorMessage; | 
| 114 | strError += " : " + errorLine + "\n"; | 
| 115 |  | 
| 116 | if (!filename.empty()) { | 
| 117 | strError += filename; | 
| 118 | } | 
| 119 |  | 
| 120 | strError += " line#" + lineCurrent; | 
| 121 | return strError; | 
| 122 | } | 
| 123 |  | 
| 124 |  | 
| 125 | private: | 
| 126 |  | 
| 127 | bool internalcompile(); | 
| 128 |  | 
| 129 |  | 
| 130 | bool lookingAtLeadingWhitespace(); | 
| 131 | bool lookingAtComment(); | 
| 132 | bool lookingAtEndOfLine(); | 
| 133 | bool lookingAtEndOfStatement(); | 
| 134 | bool lookingAtString(); | 
| 135 |  | 
| 136 |  | 
| 137 | bool compileCommand(const std::vector<vector>&); | 
| 138 |  | 
| 139 | bool clauseOr(); | 
| 140 | bool clauseAnd(); | 
| 141 | bool clauseNot(); | 
| 142 | bool clausePrimitive(); | 
| 143 | bool clauseWithin(); | 
| 144 | bool clauseComparator(); | 
| 145 | bool clauseChemObject(); | 
| 146 | bool clauseMolecule(); | 
| 147 | bool clauseMolName(); | 
| 148 | bool clauseMolIndex(); | 
| 149 | bool clauseName(); | 
| 150 | bool clauseIndex(); | 
| 151 | bool clauseStuntDoubleName(); | 
| 152 | bool clauseStuntDoubleIndex(); | 
| 153 |  | 
| 154 | bool compileError(const std::string& errorMessage) { | 
| 155 | std::cerr << "SelectionCompiler Error: " << errorMessage <<  << std::endl; | 
| 156 | error = true; | 
| 157 | this.errorMessage = errorMessage; | 
| 158 | return false; | 
| 159 | } | 
| 160 |  | 
| 161 | bool commandExpected() { | 
| 162 | return compileError("command expected"); | 
| 163 | } | 
| 164 |  | 
| 165 | bool invalidExpressionToken(const std::string& ident) { | 
| 166 | return compileError("invalid expression token:" + ident); | 
| 167 | } | 
| 168 |  | 
| 169 | bool unrecognizedToken() { | 
| 170 | return compileError("unrecognized token"); | 
| 171 | } | 
| 172 |  | 
| 173 | bool badArgumentCount() { | 
| 174 | return compileError("bad argument count"); | 
| 175 | } | 
| 176 |  | 
| 177 | bool endOfExpressionExpected() { | 
| 178 | return compileError("end of expression expected"); | 
| 179 | } | 
| 180 |  | 
| 181 | bool leftParenthesisExpected() { | 
| 182 | return compileError("left parenthesis expected"); | 
| 183 | } | 
| 184 |  | 
| 185 | bool rightParenthesisExpected() { | 
| 186 | return compileError("right parenthesis expected"); | 
| 187 | } | 
| 188 |  | 
| 189 | bool commaExpected() { | 
| 190 | return compileError("comma expected"); | 
| 191 | } | 
| 192 |  | 
| 193 | bool unrecognizedExpressionToken() { | 
| 194 | return compileError("unrecognized expression token:" + valuePeek()); | 
| 195 | } | 
| 196 |  | 
| 197 | bool comparisonOperatorExpected() { | 
| 198 | return compileError("comparison operator expected"); | 
| 199 | } | 
| 200 |  | 
| 201 | bool integerExpected() { | 
| 202 | return compileError("integer expected"); | 
| 203 | } | 
| 204 |  | 
| 205 |  | 
| 206 | std::string filename; | 
| 207 | std::string script; | 
| 208 |  | 
| 209 | std::vector<int> lineNumbers; | 
| 210 | std::vector<int> lineIndices; | 
| 211 | std::vector<std::vector<Token> >aatokenCompiled; | 
| 212 |  | 
| 213 | bool error; | 
| 214 | std::string errorMessage; | 
| 215 | std::string errorLine; | 
| 216 |  | 
| 217 | int cchScript; | 
| 218 | short lineCurrent; | 
| 219 |  | 
| 220 | int ichToken; | 
| 221 | int cchToken; | 
| 222 | std::vector<Token> atokenCommand; | 
| 223 |  | 
| 224 | int ichCurrentCommand; | 
| 225 |  | 
| 226 | std::vector<Token> ltokenPostfix; | 
| 227 | std::vector<Token> atokenInfix; | 
| 228 | int itokenInfix; | 
| 229 |  | 
| 230 | //std::vector<Token> compiledTokens_; | 
| 231 | }; | 
| 232 |  | 
| 233 | } | 
| 234 | #endif | 
| 235 |  |