--- trunk/src/selection/SelectionCompiler.hpp 2005/02/01 06:55:00 277 +++ trunk/src/selection/SelectionCompiler.hpp 2009/10/07 20:49:50 1364 @@ -41,87 +41,211 @@ #ifndef SELECTION_SELECTIONCOMPILER_HPP #define SELECTION_SELECTIONCOMPILER_HPP - +#include +#include #include + +#include "selection/SelectionToken.hpp" +#include "selection/TokenMap.hpp" +#include "brains/SimInfo.hpp" + namespace oopse { -/** - * @class SelectionCompiler SelectionCompiler.hpp "selection/SelectionCompiler.hpp" - * @brief compile a selection script to tokens - *
+  /**
+   * @class SelectionCompiler SelectionCompiler.hpp "selection/SelectionCompiler.hpp"
+   * @brief compile a selection script to tokens
+   * @todo document
+   * 
 
-    expression       :: = clauseOr
+   expression       :: = clauseOr
 
-    clauseOr         ::= clauseAnd {OR clauseAnd}*
+   clauseOr         ::= clauseAnd {OR clauseAnd}*
 
-    clauseAnd        ::= clauseNot {AND clauseNot}*
+   clauseAnd        ::= clauseNot {AND clauseNot}*
 
-    clauseNot        ::= NOT clauseNot | clausePrimitive
+   clauseNot        ::= NOT clauseNot | clausePrimitive
 
-    clausePrimitive  ::= clauseComparator |
-                         clauseWithin |
-                         clauseResidueSpec |
-                         none | all |
-                         ( clauseOr )
+   clausePrimitive  ::= clauseComparator |
+   clauseWithin |
+   clauseName |
+   none | all |
+   ( clauseOr )
 
-    clauseComparator ::= atomproperty comparatorop integer
+   clauseComparator ::= atomproperty comparatorop integer
 
-    clauseWithin     ::= WITHIN ( clauseDistance , expression )
+   clauseWithin     ::= WITHIN ( clauseDistance , expression )
 
-    clauseDistance   ::= integer | decimal
+   clauseDistance   ::= integer | decimal
+        
+   clauseName::= *|string{.string{.string}}
 
-    
 
+   * 
+ */ + class SelectionCompiler{ + public: + bool compile(const std::string& filename, const std::string& script ); + - clauseResidueSpec::= { clauseResNameSpec } - { clauseResNumSpec } - { chainSpec } - { clauseAtomSpec } - { modelSpec } + std::vector getLineNumbers() { + return lineNumbers; + } - clauseResNameSpec::= * | [ resNamePattern ] | resNamePattern + std::vector getLineIndices() { + return lineIndices; + } - // 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 > getAatokenCompiled() { + return aatokenCompiled; + } - resNamePattern ::= up to 3 alphanumeric chars with * and ? + std::string getErrorMessage() { + std::string strError = errorMessage; + strError += " : " + errorLine + "\n"; - clauseResNumSpec ::= * | clauseSequenceRange + if (!filename.empty()) { + strError += filename; + } - clauseSequenceRange ::= clauseSequenceCode { - clauseSequenceCode } + return strError; + } - clauseSequenceCode ::= seqcode | {-} integer + + private: - clauseChainSpec ::= {:} * | identifier | integer + bool internalCompile(); - clauseAtomSpec ::= . * | . identifier {*} // note that this * is *not* a wildcard - clauseModelSpec ::= {:|/} * | integer - - *
- */ -class SelectionCompiler{ - public: - bool compile(); + bool lookingAtLeadingWhitespace(); + //bool lookingAtComment(); + bool lookingAtEndOfLine(); + bool lookingAtEndOfStatement(); + bool lookingAtString(); + bool lookingAtDecimal(bool allowNegative); + bool lookingAtInteger(bool allowNegative); + bool lookingAtLookupToken(); + bool lookingAtSpecialString(); - std::vector getCompiledTokens(); - private: + std::string getUnescapedStringLiteral(); + int getHexitValue(char ch); - bool clauseOr(); - bool clauseAnd(); - bool clauseNot(); - bool clausePrimitive(); - bool clauseWithin(); - bool clauseComparator(); + 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 = OOPSE_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 +