mirror of
https://github.com/3lswear/webserv.git
synced 2025-12-17 00:34:09 +03:00
refactor: separate parsing classes
This commit is contained in:
58
includes/TOMLNode.hpp
Normal file
58
includes/TOMLNode.hpp
Normal file
@@ -0,0 +1,58 @@
|
||||
#ifndef TOMLNODE_HPP
|
||||
#define TOMLNODE_HPP
|
||||
|
||||
#include "webserv.hpp"
|
||||
#include "tomlstuff.hpp"
|
||||
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
class toml_node
|
||||
{
|
||||
|
||||
union u_value
|
||||
{
|
||||
std::string *str;
|
||||
int integer;
|
||||
bool boolean;
|
||||
/* std::vector<toml_node *> *array; */
|
||||
TOMLArray *array;
|
||||
/* std::map<std::string, toml_node *> *map; */
|
||||
TOMLMap *map;
|
||||
/* std::vector<std::map<std::string, toml_node> > *map_array; */
|
||||
TOMLMapArray *map_array;
|
||||
} value;
|
||||
|
||||
public:
|
||||
|
||||
enum e_type
|
||||
{
|
||||
STRING, NUM, BOOL, ARRAY, MAP, MAPARRAY, NIL
|
||||
} type;
|
||||
|
||||
enum e_type get_type(void);
|
||||
|
||||
TOMLMap *getMap(void);
|
||||
TOMLMapArray *getMapArray(void);
|
||||
|
||||
void setString(std::string *str);
|
||||
void setNumber(int num);
|
||||
|
||||
void setArr(TOMLArray *toml_array);
|
||||
void setBool(bool b);
|
||||
void setNil(void);
|
||||
void setObject(TOMLMap *obj);
|
||||
void setMapArray(TOMLMapArray *map_array);
|
||||
|
||||
std::string *toString(void) const;
|
||||
static std::string *TOMLMap_to_string(TOMLMap *map);
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
43
includes/TOMLParser.hpp
Normal file
43
includes/TOMLParser.hpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#ifndef TOMLPARSER_HPP
|
||||
#define TOMLPARSER_HPP
|
||||
|
||||
#include "webserv.hpp"
|
||||
#include "tomlstuff.hpp"
|
||||
#include "Tokenizer.hpp"
|
||||
#include "TOMLNode.hpp"
|
||||
#include <string>
|
||||
|
||||
namespace config
|
||||
{
|
||||
class TOMLParser
|
||||
{
|
||||
private:
|
||||
std::fstream file;
|
||||
TOMLMap *root; //root of TOML tree
|
||||
/* toml_node *current; //node currently being parsed */
|
||||
Tokenizer tokenizer;
|
||||
|
||||
public:
|
||||
TOMLParser(const std::string filename);
|
||||
TOMLMap *parse(void);
|
||||
|
||||
toml_node *parseMap(void);
|
||||
|
||||
void parseMapArray(void);
|
||||
|
||||
toml_node *parseString(void);
|
||||
|
||||
toml_node *parseNumber(void);
|
||||
|
||||
toml_node *parseArray(void);
|
||||
|
||||
toml_node *parseBool(void);
|
||||
|
||||
toml_node *parseNil(void);
|
||||
|
||||
};
|
||||
|
||||
/* parse tha root ! */
|
||||
/* TOMLMap *TOMLParser::parse(void); */
|
||||
}
|
||||
#endif
|
||||
74
includes/Tokenizer.hpp
Normal file
74
includes/Tokenizer.hpp
Normal file
@@ -0,0 +1,74 @@
|
||||
#ifndef TOKENIZER_HPP
|
||||
#define TOKENIZER_HPP
|
||||
|
||||
#include "webserv.hpp"
|
||||
#include "tomlstuff.hpp"
|
||||
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <exception>
|
||||
|
||||
|
||||
namespace config
|
||||
{
|
||||
enum e_token
|
||||
{
|
||||
KEY,
|
||||
NEWLINE,
|
||||
ASSIGN,
|
||||
STRING,
|
||||
NUMBER,
|
||||
COMMA,
|
||||
BOOL,
|
||||
NIL,
|
||||
ARR_OPEN,
|
||||
ARR_CLOSE,
|
||||
MAP_OPEN,
|
||||
MAP_CLOSE,
|
||||
MAPARRAY_DECL
|
||||
};
|
||||
|
||||
struct s_token
|
||||
{
|
||||
std::string value;
|
||||
e_token type;
|
||||
/* std::string to_string(void); */
|
||||
};
|
||||
|
||||
bool isspace(char c);
|
||||
|
||||
bool istomlkey(char c);
|
||||
|
||||
class Tokenizer
|
||||
{
|
||||
private:
|
||||
std::fstream file;
|
||||
size_t prev_pos;
|
||||
e_token last_token;
|
||||
|
||||
public:
|
||||
Tokenizer(std::string filename);
|
||||
char getWithoutWhiteSpace();
|
||||
struct s_token getToken();
|
||||
bool hasMoreTokens();
|
||||
bool firstToken();
|
||||
void rollBackToken();
|
||||
|
||||
};
|
||||
|
||||
/* struct s_token Tokenizer::getKey(void) */
|
||||
/* { */
|
||||
/* char c; */
|
||||
/* struct s_token token; */
|
||||
/* if (file.eof()) */
|
||||
/* { */
|
||||
/* std::cout << "Tokens exhausted" << std::endl; */
|
||||
/* } */
|
||||
/* } */
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
13
includes/parse.hpp
Normal file
13
includes/parse.hpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef PARSE_HPP
|
||||
#define PARSE_HPP
|
||||
|
||||
#include "tomlstuff.hpp"
|
||||
|
||||
#include "Tokenizer.hpp"
|
||||
#include "TOMLNode.hpp"
|
||||
#include "TOMLParser.hpp"
|
||||
|
||||
void parse(void);
|
||||
void display(TOMLMap *config);
|
||||
|
||||
#endif
|
||||
11
includes/tomlstuff.hpp
Normal file
11
includes/tomlstuff.hpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef TOMLSTUFF_HPP
|
||||
#define TOMLSTUFF_HPP
|
||||
|
||||
class toml_node;
|
||||
|
||||
typedef std::map<std::string, toml_node *> TOMLMap; // = JSONObject
|
||||
typedef std::vector<TOMLMap *> TOMLMapArray;
|
||||
typedef std::vector<toml_node *> TOMLArray;
|
||||
|
||||
#endif
|
||||
|
||||
@@ -19,16 +19,4 @@
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
void parse(void);
|
||||
|
||||
class toml_node;
|
||||
|
||||
/* typedef std::vector<TOMLMap *> TOMLArray; */
|
||||
/* typedef std::vector<TOMLArray *> TOMLArrayOfMap; */
|
||||
typedef std::map<std::string, toml_node *> TOMLMap; // = JSONObject
|
||||
typedef std::vector<TOMLMap *> TOMLMapArray;
|
||||
typedef std::vector<toml_node *> TOMLArray;
|
||||
|
||||
void display(TOMLMap *config);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user