#include "Tokenizer.hpp" /* -Template::-Template(void) */ /* { */ /* return; */ /* } */ /* -Template::-Template(const -Template &src) */ /* { */ /* *this = src; */ /* return; */ /* } */ /* -Template::~-Template(void) */ /* { */ /* return; */ /* } */ /* -Template &-Template::operator=(const -Template &rhs) */ /* { */ /* //code */ /* return (*this); */ /* } */ namespace config { bool isspace(char c) { if (c == ' ' || c == '\t') return (true); else return (false); } bool istomlkey(char c) { if (isalnum(c) || c == '-' || c == '_') return (true); else return (false); } Tokenizer::Tokenizer(char *filename) { last_token = NO_TOK; file.open(filename, std::ios::in); if (!file.good()) { std::cerr << "file didn't open" << std::endl; throw std::logic_error("file didnt open"); } } bool Tokenizer::firstToken() { // doesn't account for indent! if (file.tellg() == 0 || file.tellg() == 1 || (last_token == NEWLINE || last_token == NO_TOK)) return (true); else return (false); } struct s_token Tokenizer::getToken(void) { char c; struct s_token token; if (file.eof()) { std::cout << "Tokens exhausted" << std::endl; throw std::logic_error("Tokens exhausted"); } prev_pos = file.tellg(); c = getWithoutWhiteSpace(); if (firstToken() && config::istomlkey(c)) { token.type = KEY; while (config::istomlkey(c)) { token.value += c; file.get(c); } } else if (c == '"') { token.type = STRING; token.value = ""; /* TODO: maybe do-while? */ file.get(c); while (c != '"') { token.value += c; file.get(c); } } else if (c == '[' && firstToken()) { /* std::streampos prev_pos = file.tellg(); */ file.get(c); if (c == '[') { token.type = MAPARRAY_DECL; file.get(c); while (c != ']') { token.value += c; file.get(c); } if (c == ']') file.get(c); if (c != ']') throw std::logic_error("error in MAPARRAY_DECL"); } else { token.type = MAP_DECL; token.value += c; file.get(c); while (c != ']') { token.value += c; file.get(c); } if (c != ']') throw std::logic_error("malformed MAP_DECL"); } } else if (c == '[') { token.type = OPEN_BRACKET; } else if (c == ']') token.type = CLOSE_BRACKET; else if (c == '=') token.type = ASSIGN; else if (c == '\n') { token.type = NEWLINE; do file.get(c); while (c == '\n' && !file.eof()); if (file.eof()) { file.clear(); DBOUT << "cleared" <