Argentum_02

token.cpp

Apr 9th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include "token.h"
  2.  
  3. #include <stdexcept>
  4.  
  5. using namespace std;
  6.  
  7. vector<Token> Tokenize(istream& cl) {
  8.   vector<Token> tokens;
  9.  
  10.   char c;
  11.   while (cl >> c) {
  12.     if (isdigit(c)) {
  13.       string date(1, c);
  14.       for (int i = 0; i < 3; ++i) {
  15.         while (isdigit(cl.peek())) {
  16.           date += cl.get();
  17.         }
  18.         if (i < 2) {
  19.           date += cl.get(); // Consume '-'
  20.         }
  21.       }
  22.       tokens.push_back({date, TokenType::DATE});
  23.     } else if (c == '"') {
  24.       string event;
  25.       getline(cl, event, '"');
  26.       tokens.push_back({event, TokenType::EVENT});
  27.     } else if (c == 'd') {
  28.       if (cl.get() == 'a' && cl.get() == 't' && cl.get() == 'e') {
  29.         tokens.push_back({"date", TokenType::COLUMN});
  30.       } else {
  31.         throw logic_error("Unknown token");
  32.       }
  33.     } else if (c == 'e') {
  34.       if (cl.get() == 'v' && cl.get() == 'e' && cl.get() == 'n' &&
  35.           cl.get() == 't') {
  36.         tokens.push_back({"event", TokenType::COLUMN});
  37.       } else {
  38.         throw logic_error("Unknown token");
  39.       }
  40.     } else if (c == 'A') {
  41.       if (cl.get() == 'N' && cl.get() == 'D') {
  42.         tokens.push_back({"AND", TokenType::LOGICAL_OP});
  43.       } else {
  44.         throw logic_error("Unknown token");
  45.       }
  46.     } else if (c == 'O') {
  47.       if (cl.get() == 'R') {
  48.         tokens.push_back({"OR", TokenType::LOGICAL_OP});
  49.       } else {
  50.         throw logic_error("Unknown token");
  51.       }
  52.     } else if (c == '(') {
  53.       tokens.push_back({"(", TokenType::PAREN_LEFT});
  54.     } else if (c == ')') {
  55.       tokens.push_back({")", TokenType::PAREN_RIGHT});
  56.     } else if (c == '<') {
  57.       if (cl.peek() == '=') {
  58.         cl.get();
  59.         tokens.push_back({"<=", TokenType::COMPARE_OP});
  60.       } else {
  61.         tokens.push_back({"<", TokenType::COMPARE_OP});
  62.       }
  63.     } else if (c == '>') {
  64.       if (cl.peek() == '=') {
  65.         cl.get();
  66.         tokens.push_back({">=", TokenType::COMPARE_OP});
  67.       } else {
  68.         tokens.push_back({">", TokenType::COMPARE_OP});
  69.       }
  70.     } else if (c == '=') {
  71.       if (cl.get() == '=') {
  72.         tokens.push_back({"==", TokenType::COMPARE_OP});
  73.       } else {
  74.         throw logic_error("Unknown token");
  75.       }
  76.     } else if (c == '!') {
  77.       if (cl.get() == '=') {
  78.         tokens.push_back({"!=", TokenType::COMPARE_OP});
  79.       } else {
  80.         throw logic_error("Unknown token");
  81.       }
  82.     }
  83.   }
  84.  
  85.   return tokens;
  86. }
Add Comment
Please, Sign In to add comment