mirror of
https://github.com/3lswear/webserv.git
synced 2025-10-29 13:27:59 +03:00
working map_array parser??
This commit is contained in:
@@ -103,7 +103,8 @@ class toml_node
|
|||||||
<< std::endl;
|
<< std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
ss >> *result;
|
/* ss >> *result; */
|
||||||
|
*result = ss.str();
|
||||||
return (result);
|
return (result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,12 +150,14 @@ class toml_node
|
|||||||
ss << std::endl;
|
ss << std::endl;
|
||||||
for (it = map_array->begin(); it != map_array->end(); ++it)
|
for (it = map_array->begin(); it != map_array->end(); ++it)
|
||||||
{
|
{
|
||||||
ss << (TOMLMap_to_string(*it));
|
ss << (*TOMLMap_to_string(*it));
|
||||||
|
ss << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
ss << "-------" << std::endl;
|
ss << "-------" << std::endl;
|
||||||
|
|
||||||
ss >> *result;
|
/* ss >> *result; */
|
||||||
|
*result = ss.str();
|
||||||
return (result);
|
return (result);
|
||||||
}
|
}
|
||||||
case BOOL:
|
case BOOL:
|
||||||
|
|||||||
@@ -11,13 +11,19 @@ namespace config
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
std::fstream file;
|
std::fstream file;
|
||||||
toml_node *root; //root of TOML tree
|
TOMLMap *root; //root of TOML tree
|
||||||
/* toml_node *current; //node currently being parsed */
|
/* toml_node *current; //node currently being parsed */
|
||||||
Tokenizer tokenizer;
|
Tokenizer tokenizer;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
TOMLParser(const std::string filename) : tokenizer(filename) {}
|
TOMLParser(const std::string filename) : tokenizer(filename) {}
|
||||||
toml_node *parse(void);
|
TOMLMap *parse(void);
|
||||||
|
|
||||||
|
/* get a name possibly with dots and add new map to corresponding map_array*/
|
||||||
|
/* void add_to_map_array(std::string name, TOMLMap *new) */
|
||||||
|
/* { */
|
||||||
|
|
||||||
|
/* } */
|
||||||
|
|
||||||
toml_node *parseMap(void)
|
toml_node *parseMap(void)
|
||||||
{
|
{
|
||||||
@@ -38,12 +44,8 @@ namespace config
|
|||||||
}
|
}
|
||||||
if (nextToken.type == MAPARRAY_DECL)
|
if (nextToken.type == MAPARRAY_DECL)
|
||||||
{
|
{
|
||||||
parseMapArray();
|
tokenizer.rollBackToken();
|
||||||
continue;
|
break;
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
/* take key make decision */
|
|
||||||
}
|
}
|
||||||
std::string key = nextToken.value;
|
std::string key = nextToken.value;
|
||||||
std::cerr << key << std::endl;
|
std::cerr << key << std::endl;
|
||||||
@@ -75,6 +77,12 @@ namespace config
|
|||||||
(*mapObject)[key] = parseBool();
|
(*mapObject)[key] = parseBool();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case MAPARRAY_DECL:
|
||||||
|
{
|
||||||
|
std::cerr << "reached MAPARRAY_DECL in parseMap" << std::endl;
|
||||||
|
completed = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
/* throw std::logic_error("jopa in parseMap"); */
|
/* throw std::logic_error("jopa in parseMap"); */
|
||||||
@@ -105,41 +113,9 @@ namespace config
|
|||||||
return (node);
|
return (node);
|
||||||
}
|
}
|
||||||
|
|
||||||
toml_node *parseMapArray(void)
|
/* toml_node *parseMapArray(void) */
|
||||||
{
|
/* { */
|
||||||
std::cerr << "Parsing MapArray" << std::endl;
|
/* } */
|
||||||
toml_node *node = new toml_node;
|
|
||||||
TOMLMapArray *servers = new TOMLMapArray;
|
|
||||||
s_token current;
|
|
||||||
|
|
||||||
while (tokenizer.hasMoreTokens())
|
|
||||||
{
|
|
||||||
try { current = tokenizer.getToken(); }
|
|
||||||
catch (std::logic_error e)
|
|
||||||
{
|
|
||||||
std::cerr << e.what() << std::endl;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (current.type == MAPARRAY_DECL)
|
|
||||||
{
|
|
||||||
if (tokenizer.getToken().type != NEWLINE)
|
|
||||||
throw std::logic_error("no newline after map_array declaration");
|
|
||||||
servers->second->push_back(parseMap()->getMap());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
throw std::logic_error("unexpected token in parseMapArray");
|
|
||||||
if (tokenizer.hasMoreTokens())
|
|
||||||
current = tokenizer.getToken();
|
|
||||||
else
|
|
||||||
break;
|
|
||||||
if (current.type != NEWLINE)
|
|
||||||
{
|
|
||||||
throw std::logic_error("EXPECTED newline");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
node->setMapArray(servers);
|
|
||||||
return (node);
|
|
||||||
}
|
|
||||||
|
|
||||||
toml_node *parseString(void)
|
toml_node *parseString(void)
|
||||||
{
|
{
|
||||||
@@ -261,12 +237,145 @@ namespace config
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
toml_node *TOMLParser::parse(void)
|
/* parse tha root ! */
|
||||||
|
TOMLMap *TOMLParser::parse(void)
|
||||||
{
|
{
|
||||||
std::string key;
|
/* root = parseMap()->getMap(); */
|
||||||
key = "";
|
std::cerr << "Parsing ROOT" << std::endl;
|
||||||
|
/* TOMLMap *mapObject = new TOMLMap; */
|
||||||
|
root = new TOMLMap;
|
||||||
|
/* toml_node *node = new toml_node; */
|
||||||
|
bool completed = false;
|
||||||
|
while (!completed)
|
||||||
|
{
|
||||||
|
if (tokenizer.hasMoreTokens())
|
||||||
|
{
|
||||||
|
s_token current;
|
||||||
|
try { current = tokenizer.getToken(); }
|
||||||
|
catch (std::logic_error e)
|
||||||
|
{
|
||||||
|
std::cerr << e.what() << std::endl;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (current.type == MAPARRAY_DECL)
|
||||||
|
{
|
||||||
|
/* parseMapArray(); */
|
||||||
|
tokenizer.rollBackToken();
|
||||||
|
std::cerr << "Parsing MapArray" << std::endl;
|
||||||
|
toml_node *map_node;
|
||||||
|
toml_node *maparr_node;
|
||||||
|
s_token current;
|
||||||
|
|
||||||
root = parseMap();
|
try { current = tokenizer.getToken(); }
|
||||||
|
catch (std::logic_error e)
|
||||||
|
{
|
||||||
|
std::cerr << e.what() << std::endl;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (current.type == MAPARRAY_DECL)
|
||||||
|
{
|
||||||
|
if (tokenizer.getToken().type != NEWLINE)
|
||||||
|
throw std::logic_error("no newline after map_array declaration");
|
||||||
|
map_node = parseMap();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
throw std::logic_error("unexpected token in parseMapArray");
|
||||||
|
/* if (current.type != NEWLINE) */
|
||||||
|
/* { */
|
||||||
|
/* throw std::logic_error("EXPECTED newline"); */
|
||||||
|
/* } */
|
||||||
|
|
||||||
|
TOMLMap::iterator it;
|
||||||
|
std::string name = current.value;
|
||||||
|
|
||||||
|
it = root->find(name);
|
||||||
|
if (it == root->end())
|
||||||
|
{
|
||||||
|
maparr_node = new toml_node;
|
||||||
|
TOMLMapArray *map_array = new TOMLMapArray;
|
||||||
|
map_array->push_back(map_node->getMap());
|
||||||
|
maparr_node->setMapArray(map_array);
|
||||||
|
(*root)[name] = maparr_node;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
(it->second)->getMapArray()->push_back(map_node->getMap());
|
||||||
|
}
|
||||||
|
|
||||||
|
/* if (tokenizer.hasMoreTokens()) */
|
||||||
|
/* nextToken = tokenizer.getToken(); */
|
||||||
|
/* else */
|
||||||
|
/* break; */
|
||||||
|
/* if (nextToken.type != NEWLINE) */
|
||||||
|
/* { */
|
||||||
|
/* throw std::logic_error("EXPECTED newline"); */
|
||||||
|
/* } */
|
||||||
|
|
||||||
|
/* continue; */
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* take key make decision */
|
||||||
|
std::string key = current.value;
|
||||||
|
std::cerr << key << std::endl;
|
||||||
|
if (tokenizer.getToken().type != ASSIGN)
|
||||||
|
throw std::logic_error("EXPECTED assign!");
|
||||||
|
current = tokenizer.getToken();
|
||||||
|
switch (current.type)
|
||||||
|
{
|
||||||
|
case STRING:
|
||||||
|
{
|
||||||
|
tokenizer.rollBackToken();
|
||||||
|
(*root)[key] = parseString();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ARR_OPEN:
|
||||||
|
{
|
||||||
|
(*root)[key] = parseArray();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case NUMBER:
|
||||||
|
{
|
||||||
|
tokenizer.rollBackToken();
|
||||||
|
(*root)[key] = parseNumber();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case BOOL:
|
||||||
|
{
|
||||||
|
tokenizer.rollBackToken();
|
||||||
|
(*root)[key] = parseBool();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
/* throw std::logic_error("jopa in parseMap"); */
|
||||||
|
std::cerr << "Unknown token, marking as complete" << std::endl;
|
||||||
|
completed = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (tokenizer.hasMoreTokens())
|
||||||
|
current = tokenizer.getToken();
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
if (current.type != NEWLINE)
|
||||||
|
{
|
||||||
|
throw std::logic_error("EXPECTED newline");
|
||||||
|
}
|
||||||
|
/* if (tokenizer.getToken().type == NEWLINE) */
|
||||||
|
/* completed = true; */
|
||||||
|
/* else */
|
||||||
|
/* throw std::logic_error("EXPECTED newline"); */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* throw std::logic_error("parseMap: no more tokens"); */
|
||||||
|
completed = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
/* node->setObject(root); */
|
||||||
|
}
|
||||||
/* root = parseMapArray(); */
|
/* root = parseMapArray(); */
|
||||||
return (root);
|
return (root);
|
||||||
|
|
||||||
|
|||||||
@@ -24,27 +24,20 @@
|
|||||||
namespace config
|
namespace config
|
||||||
{
|
{
|
||||||
|
|
||||||
void display(toml_node *root_node)
|
void display(TOMLMap *root_map)
|
||||||
{
|
{
|
||||||
std::cout << ">>> printing config: <<<" << std::endl;
|
std::cout << ">>> printing config: <<<" << std::endl;
|
||||||
if (config->get_type() != toml_node::MAPARRAY)
|
|
||||||
throw std::logic_error("Attempting to display not map_array");
|
|
||||||
|
|
||||||
TOMLMapArray *root_map_array = config->getMapArray();
|
|
||||||
TOMLMapArray::iterator root_it;
|
|
||||||
TOMLMap::iterator it;
|
TOMLMap::iterator it;
|
||||||
|
|
||||||
for (root_it = root_map_array->begin(); root_it != root_map_array->end(); ++root_it)
|
for (it = root_map->begin(); it != root_map->end(); ++it)
|
||||||
{
|
{
|
||||||
for (it = (*root_it)->begin(); it != (*root_it)->end(); ++it)
|
std::cout << it->first
|
||||||
{
|
<< ": "
|
||||||
std::cout << it->first
|
<< *(it->second->toString())
|
||||||
<< ": "
|
<< std::endl;
|
||||||
<< *(it->second->toString())
|
|
||||||
<< std::endl;
|
|
||||||
}
|
|
||||||
std::cout << "-------" << std::endl;
|
|
||||||
}
|
}
|
||||||
|
std::cout << "-------" << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,6 +45,6 @@ void parse(void)
|
|||||||
{
|
{
|
||||||
std::string filename = "config/simple.toml";
|
std::string filename = "config/simple.toml";
|
||||||
config::TOMLParser parser(filename);
|
config::TOMLParser parser(filename);
|
||||||
toml_node *root = parser.parse();
|
TOMLMap *root = parser.parse();
|
||||||
config::display(root);
|
config::display(root);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user