Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "include/lexer.h"
- #include <fstream>
- #include "include/main.h"
- #include <cassert>
- #include <strings.h>
- #include "include/sym.h"
- #include "states.h"
- std::ifstream get_file(std::string_view& file)
- {
- static std::ifstream buffer = {file};
- Main::File = buffer;
- if(!Main::File.is_open())
- {
- std::cerr << "Error: File " << file << "did not open correctly" << std::endl;
- assert(0);
- }
- else if(!Main::File)
- {
- std::cerr << "Error: File " << file << "does not exist" << std::endl;
- assert(0);
- }
- file = {std::istream::iterator<char>(file), std::istream::iterator<char>{}};
- return file;
- }
- enum OP
- {
- GE, LE, NE, LT, GT, EQ
- };
- static uint8_t states = 0; // 0 = start, 1 = EQ, 2 = LE, ...
- std::vector<lexer::Kind> lexer::tokenize(lexer::Token& file)
- {
- // will rewrite lexer one day
- std::cout << "Another day, another day you haven't rewritten the tokenizer" << std::endl;
- std::ifstream file = get_file(file.to_string());
- std::vector<lexer::Kind> tokenkind;
- if(file.empty()) return;
- auto It = file.begin();
- auto eof = file.end();
- while(It!=eof)
- {
- const char current = *It; // *it represents the literal
- while(It!=eof && isspace(current))
- {
- ++It;
- }
- if(It == eof)
- {
- return lexer::Kind::EndOfFile;
- }
- switch(current)
- {
- using namespace lexer;
- case '.':
- tokenkind.push(Kind::COMMA);
- break;
- case ',':
- tokenkind.push(Kind::PERIOD);
- break;
- case ';':
- tokenkind.push(Kind::SEMICOLON);
- break;
- case '(':
- tokenkind.push(Kind::L_BRACKET);
- break;
- case ')':
- tokenkind.push(Kind::R_BRACKET);
- break;
- case '{':
- tokenkind.push(Kind::L_CURLY);
- break;
- case '}':
- tokenkind.push(Kind::R_CURLY);
- case ':':
- tokenkind.push(Kind::COLON);
- break;
- case '=':
- if(--current == '<') states = 2; tokenkind.push(Kind::LE); break;
- if(--current == '>') states = 3; tokenkind.push(Kind::GE); break;
- else states = 1;
- tokenkind.push(Kind::EQ);
- break;
- default:
- if(isalpha(current))
- {
- std::string_view str = current;
- while (++It != eof && isalnum(*It)) str += *It;
- if(str == "of") // S -> ID | 'of' | TYPE
- {
- ++str;
- for(auto& i : SymbolTable::types)
- {
- if(strcasecmp(str, i) == 0)
- {
- tokenkind.push();
- }
- }
- }
- for(int i = 0; i <= reserved.size(); i++)
- {
- if(str == reserved[i] && str != eof) tokenkind.push(Kind::RESERVED);
- else if (str != eof)tokenkind.push(Kind::IDENTIFER);
- }
- else if(isalnum(str) && str != eof) tokenkind.push(Kind::ALNUM);
- else if(isdigit(current) && str != eof) tokenkind.push(Kind::NUM);
- else if (str != eof) tokenkind.push(Kind::UNKOWN);
- break;
- }
- }
- }
- return tokenkind;
- }
- std::vector<std::string> lexer::delimiter(lexer::Token& str) noexcept // converts a file to a vector of strings with the individual words
- {
- //std::ifstream file = get_file(str.to_string());
- str.insert(sizeof(str), " ");
- std::string delim = " ";
- std::string token;
- std::vector<std::string> vec;
- //auto It = file.begin();
- //auto eof = file.end();
- std::string source = str.to_string();
- size_t pos = 0;
- while((pos = source.find(delim)) != std::string::npos)
- {
- token = source.substr(0, source.find(delim));
- vec.push(token);
- source.erase(0, pos + delim.length());
- }
- return vec;
- }
- int main()
- {
- return 0;
- }
Add Comment
Please, Sign In to add comment