--- trunk/src/selection/SelectionCompiler.cpp 2005/02/02 23:13:11 281 +++ trunk/src/selection/SelectionCompiler.cpp 2005/03/09 18:46:16 415 @@ -82,8 +82,8 @@ bool SelectionCompiler::internalCompile(){ for ( ; true; ichToken += cchToken) { if (lookingAtLeadingWhitespace()) continue; - if (lookingAtComment()) - continue; + //if (lookingAtComment()) + // continue; bool endOfLine = lookingAtEndOfLine(); if (endOfLine || lookingAtEndOfStatement()) { if (tokCommand != Token::nada) { @@ -118,21 +118,20 @@ bool SelectionCompiler::internalCompile(){ // continue; //} if (lookingAtDecimal((tokCommand & Token::negnums) != 0)) { - float value = lexi_cast(script.substr(ichToken, ichToken + cchToken)); - ltoken.push_back(Token(Token::decimal, value));/**@todo*/ + float value = lexi_cast(script.substr(ichToken, cchToken)); + ltoken.push_back(Token(Token::decimal, boost::any(value))); continue; } if (lookingAtInteger((tokCommand & Token::negnums) != 0)) { - std::string intString = script.substr(ichToken, ichToken + cchToken); - int val = lexi_cast(intString); - ltoken.push_back(Token(Token::integer, val, intString));/**@todo*/ + + int val = lexi_cast(script.substr(ichToken, cchToken)); + ltoken.push_back(Token(Token::integer, boost::any(val))); continue; } } if (lookingAtLookupToken()) { - std::string ident = script.substr(ichToken, ichToken + cchToken); - + std::string ident = script.substr(ichToken, cchToken); Token token; Token* pToken = TokenMap::getInstance()->getToken(ident); if (pToken != NULL) { @@ -243,6 +242,7 @@ bool SelectionCompiler::internalCompile(){ previousCharBackslash = ch == '\\' ? !previousCharBackslash : false; } cchToken = ichT - ichToken; + return true; } @@ -340,9 +340,8 @@ bool SelectionCompiler::lookingAtDecimal(bool allowNeg return false; } - // to support 1.ca, let's check the character after the dot - // to determine if it is an alpha - if (ch == '.' && (ichT + 1 < cchScript) && std::isalpha(script[ichT + 1])) { + // to support DMPC.1, let's check the character before the dot + if (ch == '.' && (ichT > 0) && std::isalpha(script[ichT - 1])) { return false; } @@ -419,15 +418,16 @@ bool SelectionCompiler::lookingAtLookupToken() { return false; } case '?': // include question marks in identifier for atom expressions - while (ichT < cchScript && (std::isalpha(ch = script[ichT]) ||std::isdigit(ch) || - ch == '_' || ch == '?') ||(ch == '^' && ichT > ichToken && std::isdigit(script[ichT - 1]))) { - // hack for insertion codes embedded in an atom expression :-( - // select c3^a + while (ichT < cchScript && !std::isspace(ch = script[ichT]) && (std::isalpha(ch) ||std::isdigit(ch) || + ch == '_' || ch == '?') ) { + ++ichT; } break; } + cchToken = ichT - ichToken; + return true; } @@ -560,15 +560,13 @@ bool SelectionCompiler::clausePrimitive() { switch (tok) { case Token::within: return clauseWithin(); - case Token::name : - return clauseName(Token::name); - case Token::index : - return clauseIndex(Token::index); - case Token::molname : - return clauseName(Token::molname); - case Token::molindex : - return clauseIndex(Token::molindex); - + + case Token::asterisk: + case Token::identifier: + return clauseChemObjName(); + + case Token::integer : + return clauseIndex(); default: if ((tok & Token::atomproperty) == Token::atomproperty) { return clauseComparator(); @@ -601,16 +599,23 @@ bool SelectionCompiler::clauseComparator() { } Token tokenValue = tokenNext(); - if (tokenValue.tok != Token::integer) { - return integerExpected(); + if (tokenValue.tok != Token::integer && tokenValue.tok != Token::decimal) { + return numberExpected(); } - int val = tokenValue.intValue; - // note that a comparator instruction is a complicated instruction - // int intValue is the tok of the property you are comparing - // the value against which you are comparing is stored as an Integer - // in the object value + + float val; + if (tokenValue.value.type() == typeid(int)) { + val = boost::any_cast(tokenValue.value); + } else if (tokenValue.value.type() == typeid(float)) { + val = boost::any_cast(tokenValue.value); + } else { + return false; + } + + boost::any floatVal; + floatVal = val; return addTokenToPostfix(Token(tokenComparator.tok, - tokenAtomProperty.tok, boost::any(val))); + tokenAtomProperty.tok, floatVal)); } bool SelectionCompiler::clauseWithin() { @@ -623,8 +628,6 @@ bool SelectionCompiler::clauseWithin() { Token tokenDistance = tokenNext(); // distance switch(tokenDistance.tok) { case Token::integer: - distance = float(tokenDistance.intValue); - break; case Token::decimal: distance = tokenDistance.value; break; @@ -647,17 +650,106 @@ bool SelectionCompiler::clauseWithin() { return addTokenToPostfix(Token(Token::within, distance)); } +bool SelectionCompiler::clauseChemObjName() { + std::string chemObjName; + int tok = tokPeek(); + if (!clauseName(chemObjName)){ + return false; + } -bool SelectionCompiler:: clauseName(int tok) { - Token tokenName = tokenNext(); - std::string name = boost::any_cast(tokenName.value); - return addTokenToPostfix(Token(tok, name)); /**@todo */ + + tok = tokPeek(); + //allow two dot at most + if (tok == Token::dot) { + tokenNext(); + chemObjName += "."; + if (!clauseName(chemObjName)) { + return false; + } + tok = tokPeek(); + if (tok == Token::dot) { + tokenNext(); + chemObjName += "."; + if (!clauseName(chemObjName)) { + return false; + } + } + } + + return addTokenToPostfix(Token(Token::name, chemObjName)); } -bool SelectionCompiler:: clauseIndex(int tok) { - //return addTokenToPostfix(Token(tok, )); /**@todo*/ - return true; +bool SelectionCompiler:: clauseName(std::string& name) { + + int tok = tokPeek(); + + if (tok == Token::asterisk || tok == Token::identifier || tok == Token::integer) { + + Token token = tokenNext(); + if (token.value.type() == typeid(std::string)) { + name += boost::any_cast(token.value); + } else if (token.value.type() == typeid(int)){ + int intVal = boost::any_cast(token.value); + char buffer[255]; + sprintf(buffer,"%d", intVal); + name += buffer; /** @todo */ + //name += toString(intVal); + } + while(true){ + tok = tokPeek(); + switch (tok) { + case Token::asterisk : + name += "*"; + tokenNext(); + break; + case Token::identifier : + name += boost::any_cast(tokenNext().value); + break; + case Token::integer : + name += toString(boost::any_cast(tokenNext().value)); + break; + case Token::dot : + return true; + default : + return true; + } + } + + }else { + return false; + } + } +bool SelectionCompiler::clauseIndex(){ + Token token = tokenNext(); + if (token.tok == Token::integer) { + int index = boost::any_cast(token.value); + int tok = tokPeek(); + std::cout << "Token::to is " << Token::to << ", tok = " << tok << std::endl; + if (tok == Token::to) { + tokenNext(); + tok = tokPeek(); + if (tok != Token::integer) { + return numberExpected(); + } + + boost::any intVal = tokenNext().value; + int first = index; + if (intVal.type() != typeid(int)){ + return false; + } + int second = boost::any_cast(intVal); + + return addTokenToPostfix(Token(Token::index, boost::any(std::make_pair(first, second)))); + + }else { + return addTokenToPostfix(Token(Token::index, boost::any(index))); + } + } else { + return numberExpected(); + } } + +}