From 013e3c1ddb3e827afb54d6747f42839fe9d2df4c Mon Sep 17 00:00:00 2001 From: 3lswear Date: Tue, 4 Jan 2022 14:48:28 +0300 Subject: [PATCH] intermid commit --- src/config/TOMLNode.hpp | 66 +++++++++++++++++ src/config/TOMLParser.hpp | 93 ++++++++++++++++++++++++ src/config/Tokenizer.hpp | 145 +++++++++++++++++++++++++++++++++++++ src/config/parse.cpp | 21 +++++- src/config/parse_types.cpp | 6 ++ 5 files changed, 330 insertions(+), 1 deletion(-) create mode 100644 src/config/TOMLNode.hpp create mode 100644 src/config/TOMLParser.hpp create mode 100644 src/config/Tokenizer.hpp create mode 100644 src/config/parse_types.cpp diff --git a/src/config/TOMLNode.hpp b/src/config/TOMLNode.hpp new file mode 100644 index 0000000..ad644c2 --- /dev/null +++ b/src/config/TOMLNode.hpp @@ -0,0 +1,66 @@ +#include "webserv.hpp" +#include +#include +#include +#include +#include +#include + + +class toml_node; + +/* typedef std::vector TOMLArray; */ +/* typedef std::vector TOMLArrayOfMap; */ +typedef std::map TOMLMap; // = JSONObject +typedef std::vector TOMLMapArray; +typedef std::vector TOMLArray; + + +class toml_node +{ + enum e_type + { + STRING, NUM, BOOL, ARRAY, MAP, MAPARRAY, NIL + } type; + + union u_value + { + std::string *str; + int integer; + bool boolean; + /* std::vector *array; */ + TOMLArray *array; + /* std::map *map; */ + TOMLMap *map; + /* std::vector > *map_array; */ + TOMLMapArray *map_array; + } value; + + public: + void setString(std::string *str) + { + value.str = str; + type = STRING; + } + + void setNumber(int num) + { + value.integer = num; + type = NUM; + } + + void setArr(TOMLArray *toml_array) + { + value.array = toml_array; + type = ARRAY; + } + void setBool(bool b) + { + value.boolean = b; + type = BOOL; + } + void setNil(void) + { + type = NIL; + } +}; diff --git a/src/config/TOMLParser.hpp b/src/config/TOMLParser.hpp new file mode 100644 index 0000000..8f74694 --- /dev/null +++ b/src/config/TOMLParser.hpp @@ -0,0 +1,93 @@ +#include "TOMLNode.hpp" +#include "Tokenizer.hpp" + +namespace config +{ + class TOMLParser + { + private: + std::fstream file; + toml_node *root; //root of TOML tree + toml_node *current; //node currently being parsed + Tokenizer tokenizer; + + public: + TOMLParser(const std::string filename) : tokenizer(filename) {} + void parse(void); + + toml_node *parseObject(void); + toml_node *parseString(void); + toml_node *parseNumber(void); + toml_node *parseArray(void); + toml_node *parseBool(void); + toml_node *parseNil(void); + }; + + void TOMLParser::parse(void) + { + std::string key; + key = ""; + + while (tokenizer.hasMoreTokens()) + { + s_token token; + try + { + token = tokenizer.getToken(); + std::cout << token.to_string() << std::endl; + switch (token.type) + { + case ARR_OPEN: + { + toml_node *parsedObject = parseObject(); + /* parsedObject->printNode(0); */ + if (!root) + root = parsedObject; + } + break; + case NUMBER: + { + tokenizer.rollBackToken(); + toml_node *parsedNumber = parseNumber(); + if (!root) + root = parsedNumber; + } + case STRING: + { + tokenizer.rollBackToken(); + toml_node *parsedString = parseString(); + if (!root) + root = parsedString; + } + case BOOL: + { + tokenizer.rollBackToken(); + toml_node *parsedBool = parseBool(); + if (!root) + root = parsedBool; + } + default: + { + throw std::logic_error("JOPA :("); + } + } + } + catch (std::logic_error err) + { + break; + } + } + } + + toml_node *TOMLParser::parseString(void) + { + toml_node *node; + std::string *sValue; + + std::cout << "Parsing string" << std::endl; + s_token token = tokenizer.getToken(); + sValue = new std::string(token.value); + node->setString(sValue); + + } +} diff --git a/src/config/Tokenizer.hpp b/src/config/Tokenizer.hpp new file mode 100644 index 0000000..d467ad7 --- /dev/null +++ b/src/config/Tokenizer.hpp @@ -0,0 +1,145 @@ +#include "webserv.hpp" +#include +#include +#include +#include +#include +#include + + +namespace config +{ + enum e_token + { + ASSIGN, + STRING, + NUMBER, + COMMA, + BOOL, + NIL, + ARR_OPEN, + ARR_CLOSE, + MAP_OPEN, + MAP_CLOSE, + MAPARRAY_OPEN, + MAPARRAY_CLOSE + }; + + struct s_token + { + std::string value; + e_token type; + std::string to_string(void); + }; + + class Tokenizer + { + private: + std::fstream file; + size_t prev_pos; + public: + Tokenizer(std::string filename); + char getWithoutWhiteSpace(); + struct s_token getToken(); + void *hasMoreTokens(); + void rollBackToken(); + }; + + char Tokenizer::getWithoutWhiteSpace(void) + { + char c = ' '; + while ((c == ' ' || c == '\n')) + { + file.get(c); + if ((c == ' ' || c == '\n') && !file.good()) + { + throw std::logic_error("No more tokens!"); + } + else if (!file.good()) + return (c); + } + return (c); + } + + void Tokenizer::rollBackToken(void) + { + if (file.eof()) + file.clear(); + file.seekg(prev_pos); + } + + struct s_token Tokenizer::getToken(void) + { + char c; + struct s_token token; + + if (file.eof()) + { + std::cout << "Tokens exhausted" << std::endl; + } + prev_pos = file.tellg(); + c = getWithoutWhiteSpace(); + + 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 == '[') + token.type = ARR_OPEN; + else if (c == ']') + token.type = ARR_CLOSE; + else if (c == '=') + token.type = ASSIGN; + else if (c == '-' || isdigit(c)) + { + std::streampos prevCharPos; + token.type = NUMBER; + token.value = ""; + token.value += c; + /* prevCharPos = file.tellg(); */ + while (c == '-' || c == '.' || isdigit(c)) + { + prevCharPos = file.tellg(); + file.get(c); + if (file.eof()) + break; + else + { + if (c == '-' || c == '.' || isdigit(c)) + token.value += c; + else + file.seekg(prevCharPos); + + } + } + } + else if (c == 'f') + { + token.type = BOOL; + token.value = "false"; + file.seekg(4, std::ios_base::cur); + } + else if (c == 't') + { + token.type = BOOL; + token.value = "true"; + file.seekg(3, std::ios_base::cur); + } + else if (c == 'n') + { + token.type = NIL; + file.seekg(3, std::ios_base::cur); + } + else if (c == ',') + token.type = COMMA; + return (token); + } +} diff --git a/src/config/parse.cpp b/src/config/parse.cpp index 39a8abe..ea94994 100644 --- a/src/config/parse.cpp +++ b/src/config/parse.cpp @@ -1,9 +1,28 @@ #include "webserv.hpp" +#include +#include +#include +#include #include +#include + +#include "TOMLNode.cpp" +#include "TOMLParser.cpp" + +/* struct location */ +/* { */ +/* std::string location; */ +/* std::string root; */ + +/* } */ + +/* class config */ +/* { */ +/* std::string name; */ +/* } */ namespace config { - void parse( } diff --git a/src/config/parse_types.cpp b/src/config/parse_types.cpp new file mode 100644 index 0000000..a2a398d --- /dev/null +++ b/src/config/parse_types.cpp @@ -0,0 +1,6 @@ +#include "webserv.hpp" +#include "parse.cpp" + +namespace config +{ +}