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

Comparing trunk/OOPSE-4/src/selection/SelectionCompiler.cpp (file contents):
Revision 1963 by tim, Wed Feb 2 12:51:25 2005 UTC vs.
Revision 1965 by tim, Wed Feb 2 23:13:11 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();
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);
134 >            std::string ident = script.substr(ichToken, ichToken + cchToken);
135  
136 <            /**@todo */
137 <            Token token = (Token) Token.map.get(ident);
138 <            if (token == NULL) {
139 <
136 >            Token token;            
137 >            Token* pToken = TokenMap::getInstance()->getToken(ident);
138 >            if (pToken != NULL) {
139 >                token = *pToken;
140 >            } else {
141 >                token = Token(Token::identifier, ident);
142              }
141            Token token(Token.identifier, ident);
142
143              
144              int tok = token.tok;
145              
146              switch (tokCommand) {
147 <                case Token.nada:
147 >                case Token::nada:
148                      ichCurrentCommand = ichToken;
149                      //tokenCommand = token;
150                      tokCommand = tok;
151 <                    if ((tokCommand & Token.command) == 0)
151 >                    if ((tokCommand & Token::command) == 0)
152                      return commandExpected();
153                      break;
154  
155 <                case Token.define:
155 >                case Token::define:
156                      if (ltoken.size() == 1) {
157                          // we are looking at the variable name
158 <                        if (tok != Token.identifier &&
159 <                        (tok & Token.predefinedset) != Token.predefinedset)
158 >                        if (tok != Token::identifier &&
159 >                        (tok & Token::predefinedset) != Token::predefinedset)
160                          return invalidExpressionToken(ident);
161                      } else {
162                      // we are looking at the expression
163 <                    if (tok != Token.identifier && tok != Token.set &&
164 <                        (tok & (Token.expression | Token.predefinedset)) == 0)
163 >                    if (tok != Token::identifier &&
164 >                        (tok & (Token::expression | Token::predefinedset)) == 0)
165                          return invalidExpressionToken(ident);
166                      }
167                      
168                      break;
169  
170 <                case Token.select:
171 <                    if (tok != Token.identifier && (tok & Token.expression) == 0)
170 >                case Token::select:
171 >                    if (tok != Token::identifier && (tok & Token::expression) == 0)
172                          return invalidExpressionToken(ident);
173                  break;
174              }
# Line 183 | Line 183 | bool SelectionCompiler::internalCompile(){
183          return unrecognizedToken();
184      }
185  
186    aatokenCompiled.push_back(lltoken);
186      return true;
187    }
188  
# Line 236 | Line 235 | bool SelectionCompiler::internalCompile(){
235      int ichT = ichToken + 1;
236      //    while (ichT < cchScript && script.charAt(ichT++) != chFirst)
237      char ch;
238 <    boolean previousCharBackslash = false;
238 >    bool previousCharBackslash = false;
239      while (ichT < cchScript) {
240 <      ch = script.[ichT++];
240 >      ch = script[ichT++];
241        if (ch == '"' && !previousCharBackslash)
242          break;
243        previousCharBackslash = ch == '\\' ? !previousCharBackslash : false;
# Line 249 | Line 248 | std::string SelectionCompiler::getUnescapedStringLiter
248  
249    
250   std::string SelectionCompiler::getUnescapedStringLiteral() {
251 <    StringBuffer sb = new StringBuffer(cchToken - 2);
251 >    /** @todo */
252 >    std::string sb(cchToken - 2, ' ');
253 >    
254      int ichMax = ichToken + cchToken - 1;
255      int ich = ichToken + 1;
256  
# Line 292 | Line 293 | std::string SelectionCompiler::getUnescapedStringLiter
293                      }
294              }
295          }
296 <        sb.append(ch);
296 >        sb.append(1, ch);
297      }
298  
299 <    return "" + sb;
299 >    return sb;
300   }
301  
302 < static int SelectionCompiler::getHexitValue(char ch) {
302 > int SelectionCompiler::getHexitValue(char ch) {
303      if (ch >= '0' && ch <= '9')
304          return ch - '0';
305      else if (ch >= 'a' && ch <= 'f')
# Line 319 | Line 320 | bool SelectionCompiler::lookingAtDecimal(boolean allow
320      return cchToken > 0;
321   }
322  
323 < bool SelectionCompiler::lookingAtDecimal(boolean allowNegative) {
323 > bool SelectionCompiler::lookingAtDecimal(bool allowNegative) {
324      if (ichToken == cchScript) {
325          return false;
326      }
# Line 328 | Line 329 | bool SelectionCompiler::lookingAtDecimal(boolean allow
329      if (script[ichT] == '-') {
330          ++ichT;
331      }
332 <    boolean digitSeen = false;
332 >    bool digitSeen = false;
333      char ch = 'X';
334      while (ichT < cchScript && std::isdigit(ch = script[ichT])) {
335          ++ichT;
# Line 354 | Line 355 | bool SelectionCompiler::lookingAtInteger(boolean allow
355      return digitSeen;
356   }
357  
358 < bool SelectionCompiler::lookingAtInteger(boolean allowNegative) {
358 > bool SelectionCompiler::lookingAtInteger(bool allowNegative) {
359      if (ichToken == cchScript) {
360          return false;
361      }
# Line 430 | Line 431 | bool SelectionCompiler::compileCommand(Vector ltoken)
431      return true;
432   }
433  
434 < bool SelectionCompiler::compileCommand(Vector ltoken) {
435 <    /** todo */
435 <    Token tokenCommand = (Token)ltoken.firstElement();
434 > bool SelectionCompiler::compileCommand(const std::vector<Token>& ltoken) {
435 >    const Token& tokenCommand = ltoken[0];
436      int tokCommand = tokenCommand.tok;
437 <        
438 <    atokenCommand = new Token[ltoken.size()];
439 <    ltoken.copyInto(atokenCommand);
440 <    if ((tokCommand & Token.expressionCommand) != 0 && !compileExpression()) {
437 >
438 >    atokenCommand = ltoken;
439 >    if ((tokCommand & Token::expressionCommand) != 0 && !compileExpression()) {
440          return false;
441      }
442 +    
443      return true;
444   }
445  
# Line 447 | Line 447 | bool SelectionCompiler::compileExpression() {
447      /** todo */
448      int i = 1;
449      int tokCommand = atokenCommand[0].tok;
450 <    if (tokCommand == Token.define)
451 <      i = 2;
452 <    else if ((tokCommand & Token.embeddedExpression) != 0) {
453 <      // look for the open parenthesis
454 <      while (i < atokenCommand.length &&
455 <             atokenCommand[i].tok != Token.leftparen)
450 >    if (tokCommand == Token::define) {
451 >        i = 2;
452 >    } else if ((tokCommand & Token::embeddedExpression) != 0) {
453 >        // look for the open parenthesis
454 >        while (i < atokenCommand.size() &&
455 >         atokenCommand[i].tok != Token::leftparen)
456          ++i;
457      }
458 <    if (i >= atokenCommand.length)
459 <      return true;
458 >
459 >    if (i >= atokenCommand.size()) {
460 >        return true;
461 >    }
462      return compileExpression(i);
463    }
464  
465                    
466 < bool SelectionCompiler::addTokenToPostfix(Token token) {
466 > bool SelectionCompiler::addTokenToPostfix(const Token& token) {
467      ltokenPostfix.push_back(token);
468      return true;
469   }
470  
471   bool SelectionCompiler::compileExpression(int itoken) {
472 <    ltokenPostfix = new Vector();
473 <    for (int i = 0; i < itoken; ++i)
472 >    ltokenPostfix.clear();
473 >    for (int i = 0; i < itoken; ++i) {
474          addTokenToPostfix(atokenCommand[i]);
475 <
475 >    }
476 >    
477      atokenInfix = atokenCommand;
478      itokenInfix = itoken;
479  
480 <    addTokenToPostfix(Token.tokenExpressionBegin);
480 >    addTokenToPostfix(Token::tokenExpressionBegin);
481      if (!clauseOr()) {
482          return false;
483      }
484      
485 <    addTokenToPostfix(Token.tokenExpressionEnd);
486 <    if (itokenInfix != atokenInfix.length) {
485 >    addTokenToPostfix(Token::tokenExpressionEnd);
486 >    if (itokenInfix != atokenInfix.size()) {
487          return endOfExpressionExpected();
488      }
489  
# Line 489 | Line 492 | if (itokenInfix == atokenInfix.length)
492   }
493  
494   Token SelectionCompiler::tokenNext() {
495 < if (itokenInfix == atokenInfix.length)
496 < return null;
497 < return atokenInfix[itokenInfix++];
495 >    if (itokenInfix == atokenInfix.size()) {
496 >        return Token();
497 >    }
498 >    return atokenInfix[itokenInfix++];
499   }
500  
501 < Object SelectionCompiler::valuePeek() {
502 <    if (itokenInfix == atokenInfix.length) {
503 <        return null;
501 > boost::any SelectionCompiler::valuePeek() {
502 >    if (itokenInfix == atokenInfix.size()) {
503 >        return boost::any();
504      } else {
505          return atokenInfix[itokenInfix].value;
506      }
507   }
508  
509   int SelectionCompiler::tokPeek() {
510 <    if (itokenInfix == atokenInfix.length) {
510 >    if (itokenInfix == atokenInfix.size()) {
511          return 0;
512      }else {
513          return atokenInfix[itokenInfix].tok;
# Line 515 | Line 519 | bool SelectionCompiler::clauseOr() {
519          return false;
520      }
521      
522 <    while (tokPeek() == Token.opOr) {
522 >    while (tokPeek() == Token::opOr) {
523          Token tokenOr = tokenNext();
524          if (!clauseAnd()) {
525              return false;
# Line 530 | Line 534 | bool SelectionCompiler::clauseAnd() {
534          return false;
535      }
536  
537 <    while (tokPeek() == Token.opAnd) {
537 >    while (tokPeek() == Token::opAnd) {
538          Token tokenAnd = tokenNext();
539          if (!clauseNot()) {
540              return false;
# Line 541 | Line 545 | bool SelectionCompiler::clauseNot() {
545   }
546  
547   bool SelectionCompiler::clauseNot() {
548 <    if (tokPeek() == Token.opNot) {
548 >    if (tokPeek() == Token::opNot) {
549          Token tokenNot = tokenNext();
550          if (!clauseNot()) {
551              return false;
# Line 554 | Line 558 | bool SelectionCompiler::clausePrimitive() {
558   bool SelectionCompiler::clausePrimitive() {
559      int tok = tokPeek();
560      switch (tok) {
561 <        case Token.within:
561 >        case Token::within:
562              return clauseWithin();
563 <        case Token.hyphen: // selecting a negative residue spec
564 <        case Token.integer:
565 <        case Token.seqcode:
566 <        case Token.asterisk:
567 <        case Token.leftsquare:
568 <        case Token.identifier:
569 <        case Token.x:
570 <        case Token.y:
571 <        case Token.z:
568 <        case Token.colon:
569 <            return clauseResidueSpec();
563 >        case Token::name :
564 >            return clauseName(Token::name);
565 >        case Token::index :
566 >            return clauseIndex(Token::index);
567 >        case Token::molname :
568 >            return clauseName(Token::molname);
569 >        case Token::molindex :
570 >            return clauseIndex(Token::molindex);
571 >            
572          default:
573 <            if ((tok & Token.atomproperty) == Token.atomproperty) {
573 >            if ((tok & Token::atomproperty) == Token::atomproperty) {
574                  return clauseComparator();
575              }
576 <            if ((tok & Token.predefinedset) != Token.predefinedset) {
576 >            if ((tok & Token::predefinedset) != Token::predefinedset) {
577                  break;
578              }
579              // fall into the code and below and just add the token
580 <        case Token.all:
581 <        case Token.none:
580 >        case Token::all:
581 >        case Token::none:
582              return addTokenToPostfix(tokenNext());
583 <        case Token.leftparen:
583 >        case Token::leftparen:
584              tokenNext();
585              if (!clauseOr()) {
586                  return false;
587              }
588 <            if (tokenNext().tok != Token.rightparen) {
588 >            if (tokenNext().tok != Token::rightparen) {
589                  return rightParenthesisExpected();
590              }
591              return true;
# Line 594 | Line 596 | bool SelectionCompiler::clauseComparator() {
596   bool SelectionCompiler::clauseComparator() {
597      Token tokenAtomProperty = tokenNext();
598      Token tokenComparator = tokenNext();
599 <    if ((tokenComparator.tok & Token.comparator) == 0) {
599 >    if ((tokenComparator.tok & Token::comparator) == 0) {
600          return comparisonOperatorExpected();
601      }
602  
603      Token tokenValue = tokenNext();
604 <    if (tokenValue.tok != Token.integer) {
604 >    if (tokenValue.tok != Token::integer) {
605          return integerExpected();
606      }
607      int val = tokenValue.intValue;
# Line 607 | Line 609 | bool SelectionCompiler::clauseComparator() {
609      // int intValue is the tok of the property you are comparing
610      // the value against which you are comparing is stored as an Integer
611      // in the object value
612 <    return addTokenToPostfix(new Token(tokenComparator.tok,
613 <                       tokenAtomProperty.tok,
612 <                       new Integer(val)));
612 >    return addTokenToPostfix(Token(tokenComparator.tok,
613 >                       tokenAtomProperty.tok, boost::any(val)));
614   }
615  
616   bool SelectionCompiler::clauseWithin() {
617      tokenNext();                             // WITHIN
618 <    if (tokenNext().tok != Token.leftparen) {  // (
618 >    if (tokenNext().tok != Token::leftparen) {  // (
619          return leftParenthesisExpected();
620      }
621      
622 <    Object distance;
622 >    boost::any distance;
623      Token tokenDistance = tokenNext();       // distance
624      switch(tokenDistance.tok) {
625 <        case Token.integer:
626 <            distance = new Float((tokenDistance.intValue * 4) / 1000f);
625 >        case Token::integer:
626 >            distance = float(tokenDistance.intValue);
627              break;
628 <        case Token.decimal:
628 >        case Token::decimal:
629              distance = tokenDistance.value;
630              break;
631          default:
632              return numberOrKeywordExpected();
633      }
634  
635 <    if (tokenNext().tok != Token.opOr) {       // ,
635 >    if (tokenNext().tok != Token::opOr) {       // ,
636          return commaExpected();
637      }
638      
# Line 639 | Line 640 | bool SelectionCompiler::clauseWithin() {
640          return false;
641      }
642      
643 <    if (tokenNext().tok != Token.rightparen) { // )T
643 >    if (tokenNext().tok != Token::rightparen) { // )T
644          return rightParenthesisExpected();
645      }
646      
647 <    return addTokenToPostfix(new Token(Token.within, distance));
647 >    return addTokenToPostfix(Token(Token::within, distance));
648   }
649  
649 bool SelectionCompiler:: clauseChemObject() {
650 }
650  
651 < bool SelectionCompiler:: clauseMolecule() {
652 < }
651 > bool SelectionCompiler:: clauseName(int tok) {
652 >    Token tokenName = tokenNext();
653 >    std::string name = boost::any_cast<std::string>(tokenName.value);
654 >    return addTokenToPostfix(Token(tok, name)); /**@todo */
655  
655 bool SelectionCompiler:: clauseMolName() {
656   }
657  
658 < bool SelectionCompiler:: clauseMolIndex() {
658 > bool SelectionCompiler:: clauseIndex(int tok) {
659 >    //return addTokenToPostfix(Token(tok, )); /**@todo*/
660 >    return true;
661   }
662  
661 bool SelectionCompiler:: clauseName() {
663   }
663
664 bool SelectionCompiler:: clauseIndex() {
665 }
666
667 bool SelectionCompiler:: clauseStuntDoubleName() {
668 }
669
670 bool SelectionCompiler:: clauseStuntDoubleIndex() {
671 }
672
673 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines