--- trunk/src/selection/SelectionCompiler.hpp 2005/02/01 06:55:00 277 +++ trunk/src/selection/SelectionCompiler.hpp 2005/02/07 19:13:18 295 @@ -41,14 +41,19 @@ #ifndef SELECTION_SELECTIONCOMPILER_HPP #define SELECTION_SELECTIONCOMPILER_HPP - +#include +#include #include + +#include "selection/Token.hpp" +#include "selection/TokenMap.hpp" namespace oopse { /** * @class SelectionCompiler SelectionCompiler.hpp "selection/SelectionCompiler.hpp" * @brief compile a selection script to tokens + * @todo document *
 
     expression       :: = clauseOr
@@ -61,7 +66,7 @@ namespace oopse {
 
     clausePrimitive  ::= clauseComparator |
                          clauseWithin |
-                         clauseResidueSpec |
+                         clauseName |
                          none | all |
                          ( clauseOr )
 
@@ -70,58 +75,171 @@ namespace oopse {
     clauseWithin     ::= WITHIN ( clauseDistance , expression )
 
     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 } + strError += " line#" + lineCurrent; + 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 compileCommand(const std::vector& ltoken); + bool compileExpression(); + bool compileExpression(int itoken); + bool clauseOr(); bool clauseAnd(); bool clauseNot(); bool clausePrimitive(); bool clauseWithin(); bool clauseComparator(); + bool clauseChemObjName(); + bool clauseName(std::string& name); + bool clauseIndex(); + Token tokenNext(); + boost::any valuePeek(); + int tokPeek(); + + bool addTokenToPostfix(const Token& token); + + + bool compileError(const std::string& errorMessage) { + std::cerr << "SelectionCompiler Error: " << errorMessage << std::endl; + error = true; + this->errorMessage = errorMessage; + return false; + } - internalCompile(); + bool commandExpected() { + return compileError("command expected"); + } - std::vector compiledTokens_; + 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 +