lazix

lexer

Sep 8th, 2021 (edited)
903
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.29 KB | None | 0 0
  1. #include "include/lexer.h"
  2. #include <fstream>
  3. #include "include/main.h"
  4. #include <cassert>
  5. #include <strings.h>
  6. #include "include/sym.h"
  7. #include "states.h"
  8. std::ifstream get_file(std::string_view& file)
  9. {
  10.     static std::ifstream buffer = {file};
  11.     Main::File = buffer;
  12.     if(!Main::File.is_open())
  13.     {
  14.         std::cerr << "Error: File " << file << "did not open correctly" << std::endl;
  15.         assert(0);
  16.     }
  17.     else if(!Main::File)
  18.     {
  19.         std::cerr << "Error: File " << file << "does not exist" << std::endl;
  20.         assert(0);
  21.     }
  22.     file = {std::istream::iterator<char>(file), std::istream::iterator<char>{}};
  23.     return file;
  24. }
  25.  
  26. enum OP
  27. {
  28.     GE, LE, NE, LT, GT, EQ
  29. };
  30.  
  31. static uint8_t states = 0; // 0 = start, 1 = EQ, 2 = LE, ...
  32.  
  33. std::vector<lexer::Kind> lexer::tokenize(lexer::Token& file)
  34. {
  35.     // will rewrite lexer one day
  36.     std::cout << "Another day, another day you haven't rewritten the tokenizer" << std::endl;
  37.     std::ifstream file = get_file(file.to_string());
  38.     std::vector<lexer::Kind> tokenkind;
  39.     if(file.empty()) return;
  40.     auto It = file.begin();
  41.     auto eof = file.end();
  42.     while(It!=eof)
  43.     {
  44.         const char current = *It; // *it represents the literal
  45.         while(It!=eof && isspace(current))
  46.         {
  47.             ++It;
  48.         }
  49.         if(It == eof)
  50.         {
  51.             return lexer::Kind::EndOfFile;
  52.         }
  53.         switch(current)
  54.         {
  55.             using namespace lexer;
  56.             case '.':
  57.                 tokenkind.push(Kind::COMMA);
  58.                 break;
  59.             case ',':
  60.                 tokenkind.push(Kind::PERIOD);
  61.                 break;
  62.             case ';':
  63.                 tokenkind.push(Kind::SEMICOLON);
  64.                 break;
  65.             case '(':
  66.                 tokenkind.push(Kind::L_BRACKET);
  67.                 break;
  68.             case ')':
  69.                 tokenkind.push(Kind::R_BRACKET);
  70.                 break;
  71.             case '{':
  72.                 tokenkind.push(Kind::L_CURLY);
  73.                 break;
  74.             case '}':
  75.                 tokenkind.push(Kind::R_CURLY);
  76.             case ':':
  77.                 tokenkind.push(Kind::COLON);
  78.                 break;
  79.             case '=':
  80.                 if(--current == '<') states = 2; tokenkind.push(Kind::LE); break;
  81.                 if(--current == '>') states = 3; tokenkind.push(Kind::GE); break;
  82.                 else states = 1;
  83.                 tokenkind.push(Kind::EQ);
  84.                 break;
  85.             default:
  86.                 if(isalpha(current))
  87.                 {
  88.                 std::string_view str = current;
  89.                 while (++It != eof && isalnum(*It)) str += *It;
  90.                 if(str == "of") // S -> ID | 'of' | TYPE
  91.                 {
  92.                     ++str;
  93.                     for(auto& i : SymbolTable::types)
  94.                     {
  95.                         if(strcasecmp(str, i) == 0)
  96.                         {
  97.                             tokenkind.push();  
  98.                         }
  99.                     }
  100.                 }
  101.                 for(int i = 0; i <= reserved.size(); i++)
  102.                 {
  103.                     if(str == reserved[i] && str != eof) tokenkind.push(Kind::RESERVED);
  104.                     else if (str != eof)tokenkind.push(Kind::IDENTIFER);
  105.                 }
  106.                 else if(isalnum(str) && str != eof) tokenkind.push(Kind::ALNUM);
  107.                 else if(isdigit(current) && str != eof) tokenkind.push(Kind::NUM);
  108.                 else if (str != eof) tokenkind.push(Kind::UNKOWN);
  109.                 break;
  110.                 }
  111.         }
  112.     }
  113.     return tokenkind;
  114. }
  115.  
  116. std::vector<std::string> lexer::delimiter(lexer::Token& str) noexcept // converts a file to a vector of strings with the individual words
  117. {
  118.     //std::ifstream file = get_file(str.to_string());
  119.     str.insert(sizeof(str), " ");
  120.     std::string delim = " ";
  121.     std::string token;
  122.     std::vector<std::string> vec;
  123.     //auto It = file.begin();
  124.     //auto eof = file.end();
  125.     std::string source = str.to_string();
  126.     size_t pos = 0;
  127.     while((pos = source.find(delim)) != std::string::npos)
  128.     {
  129.        token = source.substr(0, source.find(delim));
  130.        vec.push(token);
  131.        source.erase(0, pos + delim.length());
  132.     }
  133.     return vec;
  134. }
  135.  
  136. int main()
  137. {
  138.     return 0;
  139. }
  140.  
Add Comment
Please, Sign In to add comment