--- trunk/src/selection/SelectionCompiler.hpp 2005/02/01 06:55:00 277 +++ trunk/src/selection/SelectionCompiler.hpp 2009/11/25 20:02:06 1390 @@ -6,19 +6,10 @@ * redistribute this software in source and binary code form, provided * that the following conditions are met: * - * 1. Acknowledgement of the program authors must be made in any - * publication of scientific results based in part on use of the - * program. An acceptable form of acknowledgement is citation of - * the article in which the program was described (Matthew - * A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher - * J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented - * Parallel Simulation Engine for Molecular Dynamics," - * J. Comput. Chem. 26, pp. 252-271 (2005)) - * - * 2. Redistributions of source code must retain the above copyright + * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - * 3. Redistributions in binary form must reproduce the above copyright + * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the * distribution. @@ -37,91 +28,224 @@ * arising out of the use of or inability to use software, even if the * University of Notre Dame has been advised of the possibility of * such damages. + * + * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your + * research, please cite the appropriate papers when you publish your + * work. Good starting points are: + * + * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). + * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). + * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). + * [4] Vardeman & Gezelter, in progress (2009). */ #ifndef SELECTION_SELECTIONCOMPILER_HPP #define SELECTION_SELECTIONCOMPILER_HPP - +#include +#include #include -namespace oopse { +#include "selection/SelectionToken.hpp" +#include "selection/TokenMap.hpp" +#include "brains/SimInfo.hpp" -/** - * @class SelectionCompiler SelectionCompiler.hpp "selection/SelectionCompiler.hpp" - * @brief compile a selection script to tokens - *
+namespace OpenMD {
 
-    expression       :: = clauseOr
 
-    clauseOr         ::= clauseAnd {OR clauseAnd}*
+  /**
+   * @class SelectionCompiler SelectionCompiler.hpp "selection/SelectionCompiler.hpp"
+   * @brief compile a selection script to tokens
+   * @todo document
+   * 
 
-    clauseAnd        ::= clauseNot {AND clauseNot}*
+   expression       :: = clauseOr
 
-    clauseNot        ::= NOT clauseNot | clausePrimitive
+   clauseOr         ::= clauseAnd {OR clauseAnd}*
 
-    clausePrimitive  ::= clauseComparator |
-                         clauseWithin |
-                         clauseResidueSpec |
-                         none | all |
-                         ( clauseOr )
+   clauseAnd        ::= clauseNot {AND clauseNot}*
 
-    clauseComparator ::= atomproperty comparatorop integer
+   clauseNot        ::= NOT clauseNot | clausePrimitive
 
-    clauseWithin     ::= WITHIN ( clauseDistance , expression )
+   clausePrimitive  ::= clauseComparator |
+   clauseWithin |
+   clauseName |
+   none | all |
+   ( clauseOr )
 
-    clauseDistance   ::= integer | decimal
+   clauseComparator ::= atomproperty comparatorop integer
 
-    
+   clauseWithin     ::= WITHIN ( clauseDistance , expression )
 
+   clauseDistance   ::= integer | decimal
+        
+   clauseName::= *|string{.string{.string}}
 
-    clauseResidueSpec::= { clauseResNameSpec }
-                         { clauseResNumSpec }
-                         { chainSpec }
-                         { clauseAtomSpec }
-                         { modelSpec }
 
-    clauseResNameSpec::= * | [ resNamePattern ] | resNamePattern
+   * 
+ */ + class SelectionCompiler{ + public: + bool compile(const std::string& filename, const std::string& script ); + - // question marks are part of identifiers - // they get split up and dealt with as wildcards at runtime - // and the integers which are residue number chains get bundled - // in with the identifier and also split out at runtime - // iff a variable of that name does not exist + std::vector getLineNumbers() { + return lineNumbers; + } - resNamePattern ::= up to 3 alphanumeric chars with * and ? + std::vector getLineIndices() { + return lineIndices; + } - clauseResNumSpec ::= * | clauseSequenceRange + std::vector > getAatokenCompiled() { + return aatokenCompiled; + } - clauseSequenceRange ::= clauseSequenceCode { - clauseSequenceCode } + std::string getErrorMessage() { + std::string strError = errorMessage; + strError += " : " + errorLine + "\n"; - clauseSequenceCode ::= seqcode | {-} integer + if (!filename.empty()) { + strError += filename; + } - clauseChainSpec ::= {:} * | identifier | integer + return strError; + } - clauseAtomSpec ::= . * | . identifier {*} // note that this * is *not* a wildcard + + private: - clauseModelSpec ::= {:|/} * | integer - - *
- */ -class SelectionCompiler{ - public: - bool compile(); + bool internalCompile(); - std::vector getCompiledTokens(); - private: - bool clauseOr(); - bool clauseAnd(); - bool clauseNot(); - bool clausePrimitive(); - bool clauseWithin(); - bool clauseComparator(); + bool lookingAtLeadingWhitespace(); + //bool lookingAtComment(); + bool lookingAtEndOfLine(); + bool lookingAtEndOfStatement(); + bool lookingAtString(); + bool lookingAtDecimal(bool allowNegative); + bool lookingAtInteger(bool allowNegative); + bool lookingAtLookupToken(); + bool lookingAtSpecialString(); + + std::string getUnescapedStringLiteral(); + int getHexitValue(char ch); + + bool compileCommand(const std::vector& ltoken); + bool compileExpression(); + bool compileExpression(int itoken); - internalCompile(); + bool clauseOr(); + bool clauseAnd(); + bool clauseNot(); + bool clausePrimitive(); + bool clauseWithin(); + bool clauseComparator(); + bool clauseChemObjName(); + bool clauseIndex(); + Token tokenNext(); + boost::any valuePeek(); + int tokPeek(); - std::vector compiledTokens_; -}; + bool addTokenToPostfix(const Token& token); + bool isNameValid(const std::string& name); + bool compileError(const std::string& errorMsg) { + + sprintf( painCave.errMsg, + "SelectionCompiler Error: %s\n", errorMsg.c_str()); + painCave.severity = OPENMD_ERROR; + painCave.isFatal = 1; + simError(); + + error = true; + this->errorMessage = errorMsg; + return false; + } + + bool commandExpected() { + return compileError("command expected"); + } + + bool invalidExpressionToken(const std::string& ident) { + return compileError("invalid expression token:" + ident); + } + + bool unrecognizedToken() { + return compileError("unrecognized token"); + } + + bool badArgumentCount() { + return compileError("bad argument count"); + } + + bool endOfExpressionExpected() { + return compileError("end of expression expected"); + } + + bool leftParenthesisExpected() { + return compileError("left parenthesis expected"); + } + + bool rightParenthesisExpected() { + return compileError("right parenthesis expected"); + } + + bool commaExpected() { + return compileError("comma expected"); + } + + bool unrecognizedExpressionToken() { + boost::any tmp = valuePeek(); + std::string tokenStr; + + try { + tokenStr = boost::any_cast(tmp); + } catch(const boost::bad_any_cast &) { + return compileError("any_cast error"); + } + + return compileError("unrecognized expression token:" + tokenStr); + } + + bool comparisonOperatorExpected() { + return compileError("comparison operator expected"); + } + + bool numberExpected() { + return compileError("number expected"); + } + + bool numberOrKeywordExpected() { + return compileError("number or keyword expected"); + } + + std::string filename; + std::string script; + + std::vector lineNumbers; + std::vector lineIndices; + std::vector >aatokenCompiled; + + bool error; + std::string errorMessage; + std::string errorLine; + + int cchScript; + short lineCurrent; + + int ichToken; + int cchToken; + std::vector atokenCommand; + + int ichCurrentCommand; + + std::vector ltokenPostfix; + std::vector atokenInfix; + int itokenInfix; + + //std::vector compiledTokens_; + }; + } #endif +