ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/selection/SelectionCompiler.cpp
(Generate patch)

Comparing trunk/src/selection/SelectionCompiler.cpp (file contents):
Revision 279 by tim, Wed Feb 2 12:51:25 2005 UTC vs.
Revision 288 by tim, Fri Feb 4 22:39:26 2005 UTC

# Line 40 | Line 40
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, ichToken + cchToken));          
122 >                ltoken.push_back(Token(Token::decimal, value));/**@todo*/
123                  continue;
124              }
125 <            if (lookingAtDecimal((tokCommand & Token.negnums) != 0)) {
121 <                float value = lexi_cast<float>((script.substr(ichToken, ichToken + cchToken));          
122 <                ltoken.push_back(Token(Token.decimal, new Float(value)));/**@todo*/
123 <                continue;
124 <            }
125 <            if (lookingAtInteger((tokCommand & Token.negnums) != 0)) {
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*/
128 >                ltoken.push_back(Token(Token::integer, val, intString));/**@todo*/
129                  continue;
130              }
131          }
132        
133          if (lookingAtLookupToken()) {
134 <            std::string ident = script.subst(ichToken, ichToken + cchToken);
135 <
136 <            /**@todo */
137 <            Token token = (Token) Token.map.get(ident);
138 <            if (token == NULL) {
139 <
134 >            std::string ident = script.substr(ichToken, cchToken);
135 >            Token token;            
136 >            Token* pToken = TokenMap::getInstance()->getToken(ident);
137 >            if (pToken != NULL) {
138 >                token = *pToken;
139 >            } else {
140 >                token = Token(Token::identifier, ident);
141              }
141            Token token(Token.identifier, ident);
142
142              
143              int tok = token.tok;
144              
145              switch (tokCommand) {
146 <                case Token.nada:
146 >                case Token::nada:
147                      ichCurrentCommand = ichToken;
148                      //tokenCommand = token;
149                      tokCommand = tok;
150 <                    if ((tokCommand & Token.command) == 0)
150 >                    if ((tokCommand & Token::command) == 0)
151                      return commandExpected();
152                      break;
153  
154 <                case Token.define:
154 >                case Token::define:
155                      if (ltoken.size() == 1) {
156                          // we are looking at the variable name
157 <                        if (tok != Token.identifier &&
158 <                        (tok & Token.predefinedset) != Token.predefinedset)
157 >                        if (tok != Token::identifier &&
158 >                        (tok & Token::predefinedset) != Token::predefinedset)
159                          return invalidExpressionToken(ident);
160                      } else {
161                      // we are looking at the expression
162 <                    if (tok != Token.identifier && tok != Token.set &&
163 <                        (tok & (Token.expression | Token.predefinedset)) == 0)
162 >                    if (tok != Token::identifier &&
163 >                        (tok & (Token::expression | Token::predefinedset)) == 0)
164                          return invalidExpressionToken(ident);
165                      }
166                      
167                      break;
168  
169 <                case Token.select:
170 <                    if (tok != Token.identifier && (tok & Token.expression) == 0)
169 >                case Token::select:
170 >                    if (tok != Token::identifier && (tok & Token::expression) == 0)
171                          return invalidExpressionToken(ident);
172                  break;
173              }
# Line 183 | Line 182 | bool SelectionCompiler::internalCompile(){
182          return unrecognizedToken();
183      }
184  
186    aatokenCompiled.push_back(lltoken);
185      return true;
186    }
187  
# Line 236 | Line 234 | bool SelectionCompiler::internalCompile(){
234      int ichT = ichToken + 1;
235      //    while (ichT < cchScript && script.charAt(ichT++) != chFirst)
236      char ch;
237 <    boolean previousCharBackslash = false;
237 >    bool previousCharBackslash = false;
238      while (ichT < cchScript) {
239 <      ch = script.[ichT++];
239 >      ch = script[ichT++];
240        if (ch == '"' && !previousCharBackslash)
241          break;
242        previousCharBackslash = ch == '\\' ? !previousCharBackslash : false;
# Line 249 | Line 247 | std::string SelectionCompiler::getUnescapedStringLiter
247  
248    
249   std::string SelectionCompiler::getUnescapedStringLiteral() {
250 <    StringBuffer sb = new StringBuffer(cchToken - 2);
250 >    /** @todo */
251 >    std::string sb(cchToken - 2, ' ');
252 >    
253      int ichMax = ichToken + cchToken - 1;
254      int ich = ichToken + 1;
255  
# Line 292 | Line 292 | std::string SelectionCompiler::getUnescapedStringLiter
292                      }
293              }
294          }
295 <        sb.append(ch);
295 >        sb.append(1, ch);
296      }
297  
298 <    return "" + sb;
298 >    return sb;
299   }
300  
301 < static int SelectionCompiler::getHexitValue(char ch) {
301 > int SelectionCompiler::getHexitValue(char ch) {
302      if (ch >= '0' && ch <= '9')
303          return ch - '0';
304      else if (ch >= 'a' && ch <= 'f')
# Line 319 | Line 319 | bool SelectionCompiler::lookingAtSpecialString() {
319      return cchToken > 0;
320   }
321  
322 < bool SelectionCompiler::lookingAtDecimal(boolean allowNegative) {
322 > bool SelectionCompiler::lookingAtDecimal(bool allowNegative) {
323      if (ichToken == cchScript) {
324          return false;
325      }
# Line 328 | Line 328 | bool SelectionCompiler::lookingAtDecimal(boolean allow
328      if (script[ichT] == '-') {
329          ++ichT;
330      }
331 <    boolean digitSeen = false;
331 >    bool digitSeen = false;
332      char ch = 'X';
333      while (ichT < cchScript && std::isdigit(ch = script[ichT])) {
334          ++ichT;
# Line 354 | Line 354 | bool SelectionCompiler::lookingAtDecimal(boolean allow
354      return digitSeen;
355   }
356  
357 < bool SelectionCompiler::lookingAtInteger(boolean allowNegative) {
357 > bool SelectionCompiler::lookingAtInteger(bool allowNegative) {
358      if (ichToken == cchScript) {
359          return false;
360      }
# Line 418 | Line 418 | bool SelectionCompiler::lookingAtLookupToken() {
418                  return false;
419              }
420          case '?': // include question marks in identifier for atom expressions
421 <            while (ichT < cchScript && (std::isalpha(ch = script[ichT]) ||std::isdigit(ch) ||
422 <                ch == '_' || ch == '?') ||(ch == '^' && ichT > ichToken && std::isdigit(script[ichT - 1]))) {
423 <                // hack for insertion codes embedded in an atom expression :-(
424 <                // select c3^a
421 >            while (ichT < cchScript && !std::isspace(ch = script[ichT]) && (std::isalpha(ch) ||std::isdigit(ch) ||
422 >                ch == '_' || ch == '?') ) {
423 >
424                  ++ichT;
425              }
426          break;
# Line 430 | Line 429 | bool SelectionCompiler::lookingAtLookupToken() {
429      return true;
430   }
431  
432 < bool SelectionCompiler::compileCommand(Vector ltoken) {
433 <    /** todo */
435 <    Token tokenCommand = (Token)ltoken.firstElement();
432 > bool SelectionCompiler::compileCommand(const std::vector<Token>& ltoken) {
433 >    const Token& tokenCommand = ltoken[0];
434      int tokCommand = tokenCommand.tok;
435 <        
436 <    atokenCommand = new Token[ltoken.size()];
437 <    ltoken.copyInto(atokenCommand);
440 <    if ((tokCommand & Token.expressionCommand) != 0 && !compileExpression()) {
435 >
436 >    atokenCommand = ltoken;
437 >    if ((tokCommand & Token::expressionCommand) != 0 && !compileExpression()) {
438          return false;
439      }
440 +    
441      return true;
442   }
443  
# Line 447 | Line 445 | bool SelectionCompiler::compileExpression() {
445      /** todo */
446      int i = 1;
447      int tokCommand = atokenCommand[0].tok;
448 <    if (tokCommand == Token.define)
449 <      i = 2;
450 <    else if ((tokCommand & Token.embeddedExpression) != 0) {
451 <      // look for the open parenthesis
452 <      while (i < atokenCommand.length &&
453 <             atokenCommand[i].tok != Token.leftparen)
448 >    if (tokCommand == Token::define) {
449 >        i = 2;
450 >    } else if ((tokCommand & Token::embeddedExpression) != 0) {
451 >        // look for the open parenthesis
452 >        while (i < atokenCommand.size() &&
453 >         atokenCommand[i].tok != Token::leftparen)
454          ++i;
455      }
456 <    if (i >= atokenCommand.length)
457 <      return true;
456 >
457 >    if (i >= atokenCommand.size()) {
458 >        return true;
459 >    }
460      return compileExpression(i);
461    }
462  
463                    
464 < bool SelectionCompiler::addTokenToPostfix(Token token) {
464 > bool SelectionCompiler::addTokenToPostfix(const Token& token) {
465      ltokenPostfix.push_back(token);
466      return true;
467   }
468  
469   bool SelectionCompiler::compileExpression(int itoken) {
470 <    ltokenPostfix = new Vector();
471 <    for (int i = 0; i < itoken; ++i)
470 >    ltokenPostfix.clear();
471 >    for (int i = 0; i < itoken; ++i) {
472          addTokenToPostfix(atokenCommand[i]);
473 <
473 >    }
474 >    
475      atokenInfix = atokenCommand;
476      itokenInfix = itoken;
477  
478 <    addTokenToPostfix(Token.tokenExpressionBegin);
478 >    addTokenToPostfix(Token::tokenExpressionBegin);
479      if (!clauseOr()) {
480          return false;
481      }
482      
483 <    addTokenToPostfix(Token.tokenExpressionEnd);
484 <    if (itokenInfix != atokenInfix.length) {
483 >    addTokenToPostfix(Token::tokenExpressionEnd);
484 >    if (itokenInfix != atokenInfix.size()) {
485          return endOfExpressionExpected();
486      }
487  
# Line 489 | Line 490 | Token SelectionCompiler::tokenNext() {
490   }
491  
492   Token SelectionCompiler::tokenNext() {
493 < if (itokenInfix == atokenInfix.length)
494 < return null;
495 < return atokenInfix[itokenInfix++];
493 >    if (itokenInfix == atokenInfix.size()) {
494 >        return Token();
495 >    }
496 >    return atokenInfix[itokenInfix++];
497   }
498  
499 < Object SelectionCompiler::valuePeek() {
500 <    if (itokenInfix == atokenInfix.length) {
501 <        return null;
499 > boost::any SelectionCompiler::valuePeek() {
500 >    if (itokenInfix == atokenInfix.size()) {
501 >        return boost::any();
502      } else {
503          return atokenInfix[itokenInfix].value;
504      }
505   }
506  
507   int SelectionCompiler::tokPeek() {
508 <    if (itokenInfix == atokenInfix.length) {
508 >    if (itokenInfix == atokenInfix.size()) {
509          return 0;
510      }else {
511          return atokenInfix[itokenInfix].tok;
# Line 515 | Line 517 | bool SelectionCompiler::clauseOr() {
517          return false;
518      }
519      
520 <    while (tokPeek() == Token.opOr) {
520 >    while (tokPeek() == Token::opOr) {
521          Token tokenOr = tokenNext();
522          if (!clauseAnd()) {
523              return false;
# Line 530 | Line 532 | bool SelectionCompiler::clauseAnd() {
532          return false;
533      }
534  
535 <    while (tokPeek() == Token.opAnd) {
535 >    while (tokPeek() == Token::opAnd) {
536          Token tokenAnd = tokenNext();
537          if (!clauseNot()) {
538              return false;
# Line 541 | Line 543 | bool SelectionCompiler::clauseNot() {
543   }
544  
545   bool SelectionCompiler::clauseNot() {
546 <    if (tokPeek() == Token.opNot) {
546 >    if (tokPeek() == Token::opNot) {
547          Token tokenNot = tokenNext();
548          if (!clauseNot()) {
549              return false;
# Line 554 | Line 556 | bool SelectionCompiler::clausePrimitive() {
556   bool SelectionCompiler::clausePrimitive() {
557      int tok = tokPeek();
558      switch (tok) {
559 <        case Token.within:
559 >        case Token::within:
560              return clauseWithin();
561 <        case Token.hyphen: // selecting a negative residue spec
562 <        case Token.integer:
563 <        case Token.seqcode:
564 <        case Token.asterisk:
565 <        case Token.leftsquare:
564 <        case Token.identifier:
565 <        case Token.x:
566 <        case Token.y:
567 <        case Token.z:
568 <        case Token.colon:
569 <            return clauseResidueSpec();
561 >
562 >        case Token::asterisk:
563 >        case Token::identifier:
564 >            return clauseChemObjName();
565 >      
566          default:
567 <            if ((tok & Token.atomproperty) == Token.atomproperty) {
567 >            if ((tok & Token::atomproperty) == Token::atomproperty) {
568                  return clauseComparator();
569              }
570 <            if ((tok & Token.predefinedset) != Token.predefinedset) {
570 >            if ((tok & Token::predefinedset) != Token::predefinedset) {
571                  break;
572              }
573              // fall into the code and below and just add the token
574 <        case Token.all:
575 <        case Token.none:
574 >        case Token::all:
575 >        case Token::none:
576              return addTokenToPostfix(tokenNext());
577 <        case Token.leftparen:
577 >        case Token::leftparen:
578              tokenNext();
579              if (!clauseOr()) {
580                  return false;
581              }
582 <            if (tokenNext().tok != Token.rightparen) {
582 >            if (tokenNext().tok != Token::rightparen) {
583                  return rightParenthesisExpected();
584              }
585              return true;
# Line 594 | Line 590 | bool SelectionCompiler::clauseComparator() {
590   bool SelectionCompiler::clauseComparator() {
591      Token tokenAtomProperty = tokenNext();
592      Token tokenComparator = tokenNext();
593 <    if ((tokenComparator.tok & Token.comparator) == 0) {
593 >    if ((tokenComparator.tok & Token::comparator) == 0) {
594          return comparisonOperatorExpected();
595      }
596  
597      Token tokenValue = tokenNext();
598 <    if (tokenValue.tok != Token.integer) {
599 <        return integerExpected();
598 >    if (tokenValue.tok != Token::integer && tokenValue.tok != Token::decimal) {
599 >        return numberExpected();
600      }
601 <    int val = tokenValue.intValue;
602 <    // note that a comparator instruction is a complicated instruction
603 <    // int intValue is the tok of the property you are comparing
604 <    // the value against which you are comparing is stored as an Integer
605 <    // in the object value
606 <    return addTokenToPostfix(new Token(tokenComparator.tok,
607 <                       tokenAtomProperty.tok,
608 <                       new Integer(val)));
601 >    
602 >    float val;
603 >    if (tokenValue.value.type() == typeid(int)) {
604 >        val = boost::any_cast<int>(tokenValue.value);
605 >    } else if (tokenValue.value.type() == typeid(float)) {
606 >        val = boost::any_cast<float>(tokenValue.value);
607 >    } else {
608 >        return false;
609 >    }
610 >
611 >    return addTokenToPostfix(Token(tokenComparator.tok,
612 >                       tokenAtomProperty.tok, boost::any(val)));
613   }
614  
615   bool SelectionCompiler::clauseWithin() {
616      tokenNext();                             // WITHIN
617 <    if (tokenNext().tok != Token.leftparen) {  // (
617 >    if (tokenNext().tok != Token::leftparen) {  // (
618          return leftParenthesisExpected();
619      }
620      
621 <    Object distance;
621 >    boost::any distance;
622      Token tokenDistance = tokenNext();       // distance
623      switch(tokenDistance.tok) {
624 <        case Token.integer:
625 <            distance = new Float((tokenDistance.intValue * 4) / 1000f);
624 >        case Token::integer:
625 >            distance = float(tokenDistance.intValue);
626              break;
627 <        case Token.decimal:
627 >        case Token::decimal:
628              distance = tokenDistance.value;
629              break;
630          default:
631              return numberOrKeywordExpected();
632      }
633  
634 <    if (tokenNext().tok != Token.opOr) {       // ,
634 >    if (tokenNext().tok != Token::opOr) {       // ,
635          return commaExpected();
636      }
637      
# Line 639 | Line 639 | bool SelectionCompiler::clauseWithin() {
639          return false;
640      }
641      
642 <    if (tokenNext().tok != Token.rightparen) { // )T
642 >    if (tokenNext().tok != Token::rightparen) { // )T
643          return rightParenthesisExpected();
644      }
645      
646 <    return addTokenToPostfix(new Token(Token.within, distance));
646 >    return addTokenToPostfix(Token(Token::within, distance));
647   }
648  
649 < bool SelectionCompiler:: clauseChemObject() {
650 < }
649 > bool SelectionCompiler::clauseChemObjName() {
650 >    std::string chemObjName;
651 >    int tok = tokPeek();
652 >    if (!clauseName(chemObjName)){
653 >        return false;
654 >    }
655  
652 bool SelectionCompiler:: clauseMolecule() {
653 }
656  
657 < bool SelectionCompiler:: clauseMolName() {
658 < }
657 >    tok = tokPeek();
658 >    //allow two dot at most
659 >    if (tok == Token::dot) {
660 >        tokenNext();
661 >        chemObjName += ".";
662 >        if (!clauseName(chemObjName)) {
663 >            return false;
664 >        }
665 >        tok = tokPeek();
666 >        if (tok == Token::dot) {
667 >            tokenNext();
668 >            chemObjName += ".";
669  
670 < bool SelectionCompiler:: clauseMolIndex() {
671 < }
670 >            if (!clauseName(chemObjName)) {
671 >                return false;
672 >            }
673 >        }        
674 >    }
675  
676 < bool SelectionCompiler:: clauseName() {
676 >    return addTokenToPostfix(Token(Token::name, chemObjName));
677   }
678  
679 < bool SelectionCompiler:: clauseIndex() {
665 < }
679 > bool SelectionCompiler:: clauseName(std::string& name) {
680  
681 < bool SelectionCompiler:: clauseStuntDoubleName() {
668 < }
681 >    int tok = tokPeek();
682  
683 < bool SelectionCompiler:: clauseStuntDoubleIndex() {
683 >    if (tok == Token::asterisk || tok == Token::identifier) {
684 >        name += boost::any_cast<std::string>(tokenNext().value);
685 >        
686 >        while(true){
687 >            tok = tokPeek();
688 >            switch (tok) {
689 >                case Token::asterisk :
690 >                    name += "*";
691 >                    tokenNext();
692 >                    break;
693 >                case Token::identifier :
694 >                    name += boost::any_cast<std::string>(tokenNext().value);
695 >                    break;
696 >                case Token::integer :
697 >                    name += toString(boost::any_cast<int>(tokenNext().value));
698 >                    break;
699 >                case Token::dot :
700 >                    return true;
701 >                default :
702 >                    return true;
703 >            }
704 >        }
705 >        
706 >    }else {
707 >        return false;
708 >    }
709 >
710   }
711  
712 +
713   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines