ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-2.0/src/selection/SelectionCompiler.cpp
(Generate patch)

Comparing trunk/OOPSE-2.0/src/selection/SelectionCompiler.cpp (file contents):
Revision 1963 by tim, Wed Feb 2 12:51:25 2005 UTC vs.
Revision 1979 by tim, Mon Feb 7 19:13:18 2005 UTC

# Line 40 | Line 40 | namespace oopse {
40   */
41  
42   #include "selection/SelectionCompiler.hpp"
43 + #include "utils/StringUtils.hpp"
44   namespace oopse {
45  
46   bool SelectionCompiler::compile(const std::string& filename, const std::string& script) {
# Line 50 | Line 51 | bool SelectionCompiler::compile(const std::string& fil
51      lineIndices.clear();
52      aatokenCompiled.clear();
53          
54 <    if (internalcompile()) {
54 >    if (internalCompile()) {
55          return true;
56      }
57      
58      int icharEnd;
59 <    if ((icharEnd = script.find('\r', ichCurrentCommand)) == std::string:npos &&
60 <        (icharEnd = script.find('\n', ichCurrentCommand)) == std::string:npos) {
59 >    if ((icharEnd = script.find('\r', ichCurrentCommand)) == std::string::npos &&
60 >        (icharEnd = script.find('\n', ichCurrentCommand)) == std::string::npos) {
61          icharEnd = script.size();
62      }
63      errorLine = script.substr(ichCurrentCommand, icharEnd);
# Line 71 | Line 72 | bool SelectionCompiler::internalCompile(){
72  
73      error = false;
74  
75 <    std::vector<Token> lltoken;
75 >    //std::vector<Token> lltoken;
76 >    aatokenCompiled.clear();
77      std::vector<Token> ltoken;
78  
79 <    //Token tokenCommand = null;
80 <    int tokCommand = Token.nada;
79 >    Token tokenCommand;
80 >    int tokCommand = Token::nada;
81  
82      for ( ; true; ichToken += cchToken) {
83          if (lookingAtLeadingWhitespace())
84              continue;
85 <        if (lookingAtComment())
86 <            continue;
87 <        boolean endOfLine = lookingAtEndOfLine();
85 >        //if (lookingAtComment())
86 >        //    continue;
87 >        bool endOfLine = lookingAtEndOfLine();
88          if (endOfLine || lookingAtEndOfStatement()) {
89 <            if (tokCommand != Token.nada) {
89 >            if (tokCommand != Token::nada) {
90                  if (! compileCommand(ltoken)) {
91                      return false;
92                  }
93 <            lltoken.push_back(atokenCommand);
94 <            /** @todo*/
95 <            int iCommand = lltoken.size();
96 <            lineNumbers[iCommand] = lineCurrent;
97 <            lineIndices[iCommand] = (short) ichCurrentCommand;
96 <            ltoken.clear();
97 <            tokCommand = Token.nada;
93 >                aatokenCompiled.push_back(atokenCommand);
94 >                lineNumbers.push_back(lineCurrent);
95 >                lineIndices.push_back(ichCurrentCommand);
96 >                ltoken.clear();
97 >                tokCommand = Token::nada;
98              }
99              
100              if (ichToken < cchScript) {
# Line 105 | Line 105 | bool SelectionCompiler::internalCompile(){
105              break;
106          }
107  
108 <        if (tokCommand != Token.nada) {
108 >        if (tokCommand != Token::nada) {
109              if (lookingAtString()) {
110                  std::string str = getUnescapedStringLiteral();
111 <                ltoken.push_back(Token(Token.string, str));
111 >                ltoken.push_back(Token(Token::string, str));
112                  continue;
113              }
114 <            if ((tokCommand & Token.specialstring) != 0 &&
115 <                lookingAtSpecialString()) {
116 <                std::string str = script.substr(ichToken, ichToken + cchToken);
117 <                ltoken.push_back(Token(Token.string, str));
114 >            //if ((tokCommand & Token::specialstring) != 0 &&
115 >            //    lookingAtSpecialString()) {
116 >            //    std::string str = script.substr(ichToken, ichToken + cchToken);
117 >            //    ltoken.push_back(Token(Token::string, str));
118 >            //    continue;
119 >            //}
120 >            if (lookingAtDecimal((tokCommand & Token::negnums) != 0)) {
121 >                float value = lexi_cast<float>(script.substr(ichToken, cchToken));        
122 >                std::cout << "encount an decimal: " << value << std::endl;
123 >                ltoken.push_back(Token(Token::decimal, boost::any(value)));
124                  continue;
125              }
126 <            if (lookingAtDecimal((tokCommand & Token.negnums) != 0)) {
127 <                float value = lexi_cast<float>((script.substr(ichToken, ichToken + cchToken));          
128 <                ltoken.push_back(Token(Token.decimal, new Float(value)));/**@todo*/
126 >            if (lookingAtInteger((tokCommand & Token::negnums) != 0)) {
127 >
128 >                int val = lexi_cast<int>(script.substr(ichToken, cchToken));
129 >                std::cout << "encount an integer: " << val << std::endl;
130 >                ltoken.push_back(Token(Token::integer,   boost::any(val)));
131                  continue;
132              }
125            if (lookingAtInteger((tokCommand & Token.negnums) != 0)) {
126                std::string intString = script.substr(ichToken, ichToken + cchToken);
127                int val = lexi_cast<int>(intString);
128                ltoken.push_back(new Token(Token.integer, val, intString));/**@todo*/
129                continue;
130            }
133          }
134        
135          if (lookingAtLookupToken()) {
136 <            std::string ident = script.subst(ichToken, ichToken + cchToken);
137 <
138 <            /**@todo */
139 <            Token token = (Token) Token.map.get(ident);
140 <            if (token == NULL) {
141 <
136 >            std::string ident = script.substr(ichToken, cchToken);
137 >            Token token;            
138 >            Token* pToken = TokenMap::getInstance()->getToken(ident);
139 >            if (pToken != NULL) {
140 >                token = *pToken;
141 >            } else {
142 >                token = Token(Token::identifier, ident);
143              }
141            Token token(Token.identifier, ident);
142
144              
145              int tok = token.tok;
146              
147              switch (tokCommand) {
148 <                case Token.nada:
148 >                case Token::nada:
149                      ichCurrentCommand = ichToken;
150                      //tokenCommand = token;
151                      tokCommand = tok;
152 <                    if ((tokCommand & Token.command) == 0)
152 >                    if ((tokCommand & Token::command) == 0)
153                      return commandExpected();
154                      break;
155  
156 <                case Token.define:
156 >                case Token::define:
157                      if (ltoken.size() == 1) {
158                          // we are looking at the variable name
159 <                        if (tok != Token.identifier &&
160 <                        (tok & Token.predefinedset) != Token.predefinedset)
159 >                        if (tok != Token::identifier &&
160 >                        (tok & Token::predefinedset) != Token::predefinedset)
161                          return invalidExpressionToken(ident);
162                      } else {
163                      // we are looking at the expression
164 <                    if (tok != Token.identifier && tok != Token.set &&
165 <                        (tok & (Token.expression | Token.predefinedset)) == 0)
164 >                    if (tok != Token::identifier &&
165 >                        (tok & (Token::expression | Token::predefinedset)) == 0)
166                          return invalidExpressionToken(ident);
167                      }
168                      
169                      break;
170  
171 <                case Token.select:
172 <                    if (tok != Token.identifier && (tok & Token.expression) == 0)
171 >                case Token::select:
172 >                    if (tok != Token::identifier && (tok & Token::expression) == 0)
173                          return invalidExpressionToken(ident);
174                  break;
175              }
# Line 183 | Line 184 | bool SelectionCompiler::internalCompile(){
184          return unrecognizedToken();
185      }
186  
186    aatokenCompiled.push_back(lltoken);
187      return true;
188    }
189  
# Line 236 | Line 236 | bool SelectionCompiler::internalCompile(){
236      int ichT = ichToken + 1;
237      //    while (ichT < cchScript && script.charAt(ichT++) != chFirst)
238      char ch;
239 <    boolean previousCharBackslash = false;
239 >    bool previousCharBackslash = false;
240      while (ichT < cchScript) {
241 <      ch = script.[ichT++];
241 >      ch = script[ichT++];
242        if (ch == '"' && !previousCharBackslash)
243          break;
244        previousCharBackslash = ch == '\\' ? !previousCharBackslash : false;
245      }
246      cchToken = ichT - ichToken;
247 +
248 +
249 +    std::cout << "lookingAtString: encount " << script.substr(ichToken, cchToken) << std::endl;
250      return true;
251    }
252  
253    
254   std::string SelectionCompiler::getUnescapedStringLiteral() {
255 <    StringBuffer sb = new StringBuffer(cchToken - 2);
255 >    /** @todo */
256 >    std::string sb(cchToken - 2, ' ');
257 >    
258      int ichMax = ichToken + cchToken - 1;
259      int ich = ichToken + 1;
260  
# Line 292 | Line 297 | std::string SelectionCompiler::getUnescapedStringLiter
297                      }
298              }
299          }
300 <        sb.append(ch);
300 >        sb.append(1, ch);
301      }
302  
303 <    return "" + sb;
303 >    return sb;
304   }
305  
306 < static int SelectionCompiler::getHexitValue(char ch) {
306 > int SelectionCompiler::getHexitValue(char ch) {
307      if (ch >= '0' && ch <= '9')
308          return ch - '0';
309      else if (ch >= 'a' && ch <= 'f')
# Line 319 | Line 324 | bool SelectionCompiler::lookingAtDecimal(boolean allow
324      return cchToken > 0;
325   }
326  
327 < bool SelectionCompiler::lookingAtDecimal(boolean allowNegative) {
327 > bool SelectionCompiler::lookingAtDecimal(bool allowNegative) {
328      if (ichToken == cchScript) {
329          return false;
330      }
# Line 328 | Line 333 | bool SelectionCompiler::lookingAtDecimal(boolean allow
333      if (script[ichT] == '-') {
334          ++ichT;
335      }
336 <    boolean digitSeen = false;
336 >    bool digitSeen = false;
337      char ch = 'X';
338      while (ichT < cchScript && std::isdigit(ch = script[ichT])) {
339          ++ichT;
# Line 354 | Line 359 | bool SelectionCompiler::lookingAtInteger(boolean allow
359      return digitSeen;
360   }
361  
362 < bool SelectionCompiler::lookingAtInteger(boolean allowNegative) {
362 > bool SelectionCompiler::lookingAtInteger(bool allowNegative) {
363      if (ichToken == cchScript) {
364          return false;
365      }
# Line 418 | Line 423 | bool SelectionCompiler::lookingAtLookupToken() {
423                  return false;
424              }
425          case '?': // include question marks in identifier for atom expressions
426 <            while (ichT < cchScript && (std::isalpha(ch = script[ichT]) ||std::isdigit(ch) ||
427 <                ch == '_' || ch == '?') ||(ch == '^' && ichT > ichToken && std::isdigit(script[ichT - 1]))) {
428 <                // hack for insertion codes embedded in an atom expression :-(
424 <                // select c3^a
426 >            while (ichT < cchScript && !std::isspace(ch = script[ichT]) && (std::isalpha(ch) ||std::isdigit(ch) ||
427 >                ch == '_' || ch == '?') ) {
428 >
429                  ++ichT;
430              }
431          break;
432      }
433 +
434      cchToken = ichT - ichToken;
435 +
436 +    std::cout << "lookingAtLookupToken: encount " << script.substr(ichToken, cchToken) << std::endl;
437      return true;
438   }
439  
440 < bool SelectionCompiler::compileCommand(Vector ltoken) {
441 <    /** todo */
435 <    Token tokenCommand = (Token)ltoken.firstElement();
440 > bool SelectionCompiler::compileCommand(const std::vector<Token>& ltoken) {
441 >    const Token& tokenCommand = ltoken[0];
442      int tokCommand = tokenCommand.tok;
443 <        
444 <    atokenCommand = new Token[ltoken.size()];
445 <    ltoken.copyInto(atokenCommand);
440 <    if ((tokCommand & Token.expressionCommand) != 0 && !compileExpression()) {
443 >
444 >    atokenCommand = ltoken;
445 >    if ((tokCommand & Token::expressionCommand) != 0 && !compileExpression()) {
446          return false;
447      }
448 +    
449      return true;
450   }
451  
# Line 447 | Line 453 | bool SelectionCompiler::compileExpression() {
453      /** todo */
454      int i = 1;
455      int tokCommand = atokenCommand[0].tok;
456 <    if (tokCommand == Token.define)
457 <      i = 2;
458 <    else if ((tokCommand & Token.embeddedExpression) != 0) {
459 <      // look for the open parenthesis
460 <      while (i < atokenCommand.length &&
461 <             atokenCommand[i].tok != Token.leftparen)
456 >    if (tokCommand == Token::define) {
457 >        i = 2;
458 >    } else if ((tokCommand & Token::embeddedExpression) != 0) {
459 >        // look for the open parenthesis
460 >        while (i < atokenCommand.size() &&
461 >         atokenCommand[i].tok != Token::leftparen)
462          ++i;
463      }
464 <    if (i >= atokenCommand.length)
465 <      return true;
464 >
465 >    if (i >= atokenCommand.size()) {
466 >        return true;
467 >    }
468      return compileExpression(i);
469    }
470  
471                    
472 < bool SelectionCompiler::addTokenToPostfix(Token token) {
472 > bool SelectionCompiler::addTokenToPostfix(const Token& token) {
473      ltokenPostfix.push_back(token);
474      return true;
475   }
476  
477   bool SelectionCompiler::compileExpression(int itoken) {
478 <    ltokenPostfix = new Vector();
479 <    for (int i = 0; i < itoken; ++i)
478 >    ltokenPostfix.clear();
479 >    for (int i = 0; i < itoken; ++i) {
480          addTokenToPostfix(atokenCommand[i]);
481 <
481 >    }
482 >    
483      atokenInfix = atokenCommand;
484      itokenInfix = itoken;
485  
486 <    addTokenToPostfix(Token.tokenExpressionBegin);
486 >    addTokenToPostfix(Token::tokenExpressionBegin);
487      if (!clauseOr()) {
488          return false;
489      }
490      
491 <    addTokenToPostfix(Token.tokenExpressionEnd);
492 <    if (itokenInfix != atokenInfix.length) {
491 >    addTokenToPostfix(Token::tokenExpressionEnd);
492 >    if (itokenInfix != atokenInfix.size()) {
493          return endOfExpressionExpected();
494      }
495  
# Line 489 | Line 498 | if (itokenInfix == atokenInfix.length)
498   }
499  
500   Token SelectionCompiler::tokenNext() {
501 < if (itokenInfix == atokenInfix.length)
502 < return null;
503 < return atokenInfix[itokenInfix++];
501 >    if (itokenInfix == atokenInfix.size()) {
502 >        return Token();
503 >    }
504 >    return atokenInfix[itokenInfix++];
505   }
506  
507 < Object SelectionCompiler::valuePeek() {
508 <    if (itokenInfix == atokenInfix.length) {
509 <        return null;
507 > boost::any SelectionCompiler::valuePeek() {
508 >    if (itokenInfix == atokenInfix.size()) {
509 >        return boost::any();
510      } else {
511          return atokenInfix[itokenInfix].value;
512      }
513   }
514  
515   int SelectionCompiler::tokPeek() {
516 <    if (itokenInfix == atokenInfix.length) {
516 >    if (itokenInfix == atokenInfix.size()) {
517          return 0;
518      }else {
519          return atokenInfix[itokenInfix].tok;
# Line 515 | Line 525 | bool SelectionCompiler::clauseOr() {
525          return false;
526      }
527      
528 <    while (tokPeek() == Token.opOr) {
528 >    while (tokPeek() == Token::opOr) {
529          Token tokenOr = tokenNext();
530          if (!clauseAnd()) {
531              return false;
# Line 530 | Line 540 | bool SelectionCompiler::clauseAnd() {
540          return false;
541      }
542  
543 <    while (tokPeek() == Token.opAnd) {
543 >    while (tokPeek() == Token::opAnd) {
544          Token tokenAnd = tokenNext();
545          if (!clauseNot()) {
546              return false;
# Line 541 | Line 551 | bool SelectionCompiler::clauseNot() {
551   }
552  
553   bool SelectionCompiler::clauseNot() {
554 <    if (tokPeek() == Token.opNot) {
554 >    if (tokPeek() == Token::opNot) {
555          Token tokenNot = tokenNext();
556          if (!clauseNot()) {
557              return false;
# Line 554 | Line 564 | bool SelectionCompiler::clausePrimitive() {
564   bool SelectionCompiler::clausePrimitive() {
565      int tok = tokPeek();
566      switch (tok) {
567 <        case Token.within:
567 >        case Token::within:
568              return clauseWithin();
569 <        case Token.hyphen: // selecting a negative residue spec
570 <        case Token.integer:
571 <        case Token.seqcode:
572 <        case Token.asterisk:
573 <        case Token.leftsquare:
574 <        case Token.identifier:
575 <        case Token.x:
566 <        case Token.y:
567 <        case Token.z:
568 <        case Token.colon:
569 <            return clauseResidueSpec();
569 >
570 >        case Token::asterisk:
571 >        case Token::identifier:
572 >            return clauseChemObjName();
573 >
574 >        case Token::integer :
575 >            return clauseIndex();
576          default:
577 <            if ((tok & Token.atomproperty) == Token.atomproperty) {
577 >            if ((tok & Token::atomproperty) == Token::atomproperty) {
578                  return clauseComparator();
579              }
580 <            if ((tok & Token.predefinedset) != Token.predefinedset) {
580 >            if ((tok & Token::predefinedset) != Token::predefinedset) {
581                  break;
582              }
583              // fall into the code and below and just add the token
584 <        case Token.all:
585 <        case Token.none:
584 >        case Token::all:
585 >        case Token::none:
586              return addTokenToPostfix(tokenNext());
587 <        case Token.leftparen:
587 >        case Token::leftparen:
588              tokenNext();
589              if (!clauseOr()) {
590                  return false;
591              }
592 <            if (tokenNext().tok != Token.rightparen) {
592 >            if (tokenNext().tok != Token::rightparen) {
593                  return rightParenthesisExpected();
594              }
595              return true;
# Line 594 | Line 600 | bool SelectionCompiler::clauseComparator() {
600   bool SelectionCompiler::clauseComparator() {
601      Token tokenAtomProperty = tokenNext();
602      Token tokenComparator = tokenNext();
603 <    if ((tokenComparator.tok & Token.comparator) == 0) {
603 >    if ((tokenComparator.tok & Token::comparator) == 0) {
604          return comparisonOperatorExpected();
605      }
606  
607      Token tokenValue = tokenNext();
608 <    if (tokenValue.tok != Token.integer) {
609 <        return integerExpected();
608 >    if (tokenValue.tok != Token::integer && tokenValue.tok != Token::decimal) {
609 >        return numberExpected();
610      }
611 <    int val = tokenValue.intValue;
612 <    // note that a comparator instruction is a complicated instruction
613 <    // int intValue is the tok of the property you are comparing
614 <    // the value against which you are comparing is stored as an Integer
615 <    // in the object value
616 <    return addTokenToPostfix(new Token(tokenComparator.tok,
617 <                       tokenAtomProperty.tok,
618 <                       new Integer(val)));
611 >    
612 >    float val;
613 >    if (tokenValue.value.type() == typeid(int)) {
614 >        val = boost::any_cast<int>(tokenValue.value);
615 >    } else if (tokenValue.value.type() == typeid(float)) {
616 >        val = boost::any_cast<float>(tokenValue.value);
617 >    } else {
618 >        return false;
619 >    }
620 >
621 >    boost::any floatVal;
622 >    floatVal = val;
623 >    return addTokenToPostfix(Token(tokenComparator.tok,
624 >                       tokenAtomProperty.tok, floatVal));
625   }
626  
627   bool SelectionCompiler::clauseWithin() {
628      tokenNext();                             // WITHIN
629 <    if (tokenNext().tok != Token.leftparen) {  // (
629 >    if (tokenNext().tok != Token::leftparen) {  // (
630          return leftParenthesisExpected();
631      }
632      
633 <    Object distance;
633 >    boost::any distance;
634      Token tokenDistance = tokenNext();       // distance
635      switch(tokenDistance.tok) {
636 <        case Token.integer:
637 <            distance = new Float((tokenDistance.intValue * 4) / 1000f);
626 <            break;
627 <        case Token.decimal:
636 >        case Token::integer:
637 >        case Token::decimal:
638              distance = tokenDistance.value;
639              break;
640          default:
641              return numberOrKeywordExpected();
642      }
643  
644 <    if (tokenNext().tok != Token.opOr) {       // ,
644 >    if (tokenNext().tok != Token::opOr) {       // ,
645          return commaExpected();
646      }
647      
# Line 639 | Line 649 | bool SelectionCompiler::clauseWithin() {
649          return false;
650      }
651      
652 <    if (tokenNext().tok != Token.rightparen) { // )T
652 >    if (tokenNext().tok != Token::rightparen) { // )T
653          return rightParenthesisExpected();
654      }
655      
656 <    return addTokenToPostfix(new Token(Token.within, distance));
656 >    return addTokenToPostfix(Token(Token::within, distance));
657   }
658  
659 < bool SelectionCompiler:: clauseChemObject() {
660 < }
659 > bool SelectionCompiler::clauseChemObjName() {
660 >    std::string chemObjName;
661 >    int tok = tokPeek();
662 >    if (!clauseName(chemObjName)){
663 >        return false;
664 >    }
665  
652 bool SelectionCompiler:: clauseMolecule() {
653 }
666  
667 < bool SelectionCompiler:: clauseMolName() {
668 < }
667 >    tok = tokPeek();
668 >    //allow two dot at most
669 >    if (tok == Token::dot) {
670 >        tokenNext();
671 >        chemObjName += ".";
672 >        if (!clauseName(chemObjName)) {
673 >            return false;
674 >        }
675 >        tok = tokPeek();
676 >        if (tok == Token::dot) {
677 >            tokenNext();
678 >            chemObjName += ".";
679  
680 < bool SelectionCompiler:: clauseMolIndex() {
681 < }
680 >            if (!clauseName(chemObjName)) {
681 >                return false;
682 >            }
683 >        }        
684 >    }
685  
686 < bool SelectionCompiler:: clauseName() {
686 >    return addTokenToPostfix(Token(Token::name, chemObjName));
687   }
688  
689 < bool SelectionCompiler:: clauseIndex() {
665 < }
689 > bool SelectionCompiler:: clauseName(std::string& name) {
690  
691 < bool SelectionCompiler:: clauseStuntDoubleName() {
691 >    int tok = tokPeek();
692 >
693 >    if (tok == Token::asterisk || tok == Token::identifier) {
694 >        name += boost::any_cast<std::string>(tokenNext().value);
695 >        
696 >        while(true){
697 >            tok = tokPeek();
698 >            switch (tok) {
699 >                case Token::asterisk :
700 >                    name += "*";
701 >                    tokenNext();
702 >                    break;
703 >                case Token::identifier :
704 >                    name += boost::any_cast<std::string>(tokenNext().value);
705 >                    break;
706 >                case Token::integer :
707 >                    name += toString(boost::any_cast<int>(tokenNext().value));
708 >                    break;
709 >                case Token::dot :
710 >                    return true;
711 >                default :
712 >                    return true;
713 >            }
714 >        }
715 >        
716 >    }else {
717 >        return false;
718 >    }
719 >
720   }
721  
722 < bool SelectionCompiler:: clauseStuntDoubleIndex() {
722 > bool SelectionCompiler::clauseIndex(){
723 >    Token token = tokenNext();
724 >    if (token.tok == Token::integer) {
725 >        int index = boost::any_cast<int>(token.value);
726 >        int tok = tokPeek();
727 >        std::cout << "Token::to is " << Token::to << ", tok = " << tok << std::endl;
728 >        if (tok == Token::to) {
729 >            tokenNext();
730 >            tok = tokPeek();
731 >            if (tok != Token::integer) {
732 >                return numberExpected();
733 >            }
734 >            
735 >            boost::any intVal = tokenNext().value;
736 >            int first = index;
737 >            if (intVal.type() != typeid(int)){
738 >                return false;
739 >            }
740 >            int second = boost::any_cast<int>(intVal);
741 >
742 >            return addTokenToPostfix(Token(Token::index, boost::any(std::make_pair(first, second))));
743 >            
744 >        }else {
745 >            return addTokenToPostfix(Token(Token::index, boost::any(index)));
746 >        }
747 >    } else {
748 >        return numberExpected();
749 >    }
750   }
751  
752   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines