refactor: token names and misc

This commit is contained in:
3lswear
2022-01-22 14:07:54 +03:00
parent 51a3050beb
commit d9f3330440
3 changed files with 342 additions and 334 deletions

View File

@@ -24,8 +24,8 @@ namespace config
COMMA,
BOOL,
NIL,
ARR_OPEN,
ARR_CLOSE,
OPEN_BRACKET,
CLOSE_BRACKET,
MAP_OPEN,
MAP_CLOSE,
MAPARRAY_DECL

View File

@@ -22,357 +22,72 @@
/* return (*this); */
/* } */
config::TOMLParser::TOMLParser(const std::string filename) : tokenizer(filename)
{};
toml_node *config::TOMLParser::parseMap(void)
namespace config
{
std::cerr << "Parsing map" << std::endl;
toml_node *node = new toml_node;
TOMLMap *mapObject = new TOMLMap;
bool completed = false;
while (!completed)
TOMLParser::TOMLParser(const std::string filename) : tokenizer(filename)
{};
toml_node *TOMLParser::parseMap(void)
{
if (tokenizer.hasMoreTokens())
std::cerr << "Parsing map" << std::endl;
toml_node *node = new toml_node;
TOMLMap *mapObject = new TOMLMap;
bool completed = false;
while (!completed)
{
s_token nextToken;
try { nextToken = tokenizer.getToken(); }
catch (std::logic_error e)
{
std::cerr << e.what() << std::endl;
break;
}
if (nextToken.type == MAPARRAY_DECL)
{
tokenizer.rollBackToken();
break;
}
std::string key = nextToken.value;
std::cerr << key << std::endl;
if (tokenizer.getToken().type != ASSIGN)
throw std::logic_error("EXPECTED assign!");
nextToken = tokenizer.getToken();
switch (nextToken.type)
{
case STRING:
{
tokenizer.rollBackToken();
(*mapObject)[key] = parseString();
break;
}
case ARR_OPEN:
{
(*mapObject)[key] = parseArray();
break;
}
case NUMBER:
{
tokenizer.rollBackToken();
(*mapObject)[key] = parseNumber();
break;
}
case BOOL:
{
tokenizer.rollBackToken();
(*mapObject)[key] = parseBool();
break;
}
case MAPARRAY_DECL:
{
std::cerr << "reached MAPARRAY_DECL in parseMap" << std::endl;
completed = true;
break;
}
default:
{
/* throw std::logic_error("jopa in parseMap"); */
std::cerr << "Unknown token, marking as complete" << std::endl;
completed = true;
break;
}
}
if (tokenizer.hasMoreTokens())
nextToken = tokenizer.getToken();
else
break;
if (nextToken.type != NEWLINE)
{
throw std::logic_error("EXPECTED newline");
}
}
else
{
throw std::logic_error("parseMap: no more tokens");
}
}
node->setObject(mapObject);
return (node);
}
s_token nextToken;
try { nextToken = tokenizer.getToken(); }
catch (std::logic_error e)
{
std::cerr << e.what() << std::endl;
break;
}
if (nextToken.type == MAPARRAY_DECL)
{
tokenizer.rollBackToken();
break;
}
else if (nextToken.type == OPEN_BRACKET)
{
void config::TOMLParser::parseMapArray(void)
{
std::cerr << "Parsing MapArray" << std::endl;
toml_node *map_node;
toml_node *maparr_node;
s_token current;
try { current = tokenizer.getToken(); }
catch (std::logic_error e)
{
std::cerr << e.what() << std::endl;
return;
}
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");
TOMLMap::iterator it;
std::cout << current.value << std::endl;
std::string name = current.value;
std::vector<std::string> full_name;
size_t dot;
while (1)
{
dot = name.find('.');
if (dot == std::string::npos)
break;
/* std::cout << dot << std::endl; */
full_name.push_back(name.substr(0, dot));
name.erase(0, dot + 1);
}
full_name.push_back(name);
for (size_t i = 0; i < full_name.size(); i++)
std::cout << full_name[i] << ", ";
std::cout << std::endl;
/* throw std::logic_error("tha end"); */
TOMLMap *local_root;
std::vector<std::string>::iterator subname = full_name.begin();
local_root = root;
while (1)
{
if (subname + 1 == full_name.end())
{
it = local_root->find(*subname);
if (it == local_root->end())
{
maparr_node = new toml_node;
TOMLMapArray *map_array = new TOMLMapArray;
map_array->push_back(map_node->getMap());
maparr_node->setMapArray(map_array);
(*local_root)[*subname] = maparr_node;
}
else
(it->second)->getMapArray()->push_back(map_node->getMap());
break;
}
else
{
it = local_root->find(*subname);
toml_node *map_node2;
map_node2 = new toml_node;
TOMLMap *map = new TOMLMap;
map_node2->setObject(map);
/* subname not found in local_root */
if (it == local_root->end())
{
(*local_root)[*subname] = map_node2;
local_root = map;
}
/* subname found in local_root */
else
{
local_root = *((it->second)->getMapArray()->end() - 1);
}
}
++subname;
}
}
toml_node *config::TOMLParser::parseString(void)
{
/* toml_node *node; */
toml_node *node = new toml_node;
std::string *sValue;
std::cerr << "Parsing string" << std::endl;
s_token token = tokenizer.getToken();
sValue = new std::string(token.value);
node->setString(sValue);
return (node);
}
toml_node *config::TOMLParser::parseNumber(void)
{
toml_node *node = new toml_node;
int value;
std::cerr << "Parsing number" << std::endl;
s_token token = tokenizer.getToken();
value = std::atoi(token.value.c_str());
node->setNumber(value);
return (node);
}
toml_node *config::TOMLParser::parseArray(void)
{
std::cerr << "Parsing array" << std::endl;
toml_node *node;
toml_node *result = new toml_node;
TOMLArray *array = new TOMLArray;
bool completed = false;
s_token current;
while (!completed)
{
if (!tokenizer.hasMoreTokens())
throw std::logic_error("No more tokens");
else
{
current = tokenizer.getToken();
switch (current.type)
{
case ARR_OPEN:
{
node = parseArray();
break;
}
case STRING:
{
tokenizer.rollBackToken();
node = parseString();
break;
}
case NUMBER:
{
tokenizer.rollBackToken();
node = parseNumber();
break;
}
case BOOL:
{
tokenizer.rollBackToken();
node = parseBool();
break;
}
case NIL:
{
node = parseNil();
break;
}
default:
{
throw std::logic_error("unkown token in parseArray");
}
}
array->push_back(node);
current = tokenizer.getToken();
if (current.type == COMMA)
continue;
else if (current.type == ARR_CLOSE)
completed = true;
else
throw std::invalid_argument("Unexpected token in array!");
}
}
result->setArr(array);
return (result);
}
toml_node *config::TOMLParser::parseBool(void)
{
toml_node *node = new toml_node;
bool value;
std::cerr << "Parsing bool" << std::endl;
s_token token = tokenizer.getToken();
if (token.value == "true")
value = true;
else if (token.value == "false")
value = false;
else
throw std::invalid_argument("Unexpected bool value");
node->setBool(value);
return (node);
}
toml_node *config::TOMLParser::parseNil(void)
{
toml_node *node = new toml_node;
std::cerr << "Parsing NIL" << std::endl;
node->setNil();
return (node);
}
/* parse tha root ! */
TOMLMap *config::TOMLParser::parse(void)
{
std::cerr << "Parsing ROOT" << std::endl;
root = new TOMLMap;
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();
parseMapArray();
}
else
{
std::string key = current.value;
}
std::string key = nextToken.value;
std::cerr << key << std::endl;
if (tokenizer.getToken().type != ASSIGN)
throw std::logic_error("EXPECTED assign!");
current = tokenizer.getToken();
switch (current.type)
nextToken = tokenizer.getToken();
switch (nextToken.type)
{
case STRING:
{
tokenizer.rollBackToken();
(*root)[key] = parseString();
(*mapObject)[key] = parseString();
break;
}
case ARR_OPEN:
case OPEN_BRACKET:
{
(*root)[key] = parseArray();
(*mapObject)[key] = parseArray();
break;
}
case NUMBER:
{
tokenizer.rollBackToken();
(*root)[key] = parseNumber();
(*mapObject)[key] = parseNumber();
break;
}
case BOOL:
{
tokenizer.rollBackToken();
(*root)[key] = parseBool();
(*mapObject)[key] = parseBool();
break;
}
case MAPARRAY_DECL:
{
std::cerr << "reached MAPARRAY_DECL in parseMap" << std::endl;
completed = true;
break;
}
default:
@@ -384,20 +99,313 @@ TOMLMap *config::TOMLParser::parse(void)
}
}
if (tokenizer.hasMoreTokens())
current = tokenizer.getToken();
nextToken = tokenizer.getToken();
else
break;
if (current.type != NEWLINE)
if (nextToken.type != NEWLINE)
{
throw std::logic_error("EXPECTED newline");
}
}
else
{
throw std::logic_error("parseMap: no more tokens");
}
}
node->setObject(mapObject);
return (node);
}
void TOMLParser::parseMapArray(void)
{
std::cerr << "Parsing MapArray" << std::endl;
toml_node *map_node;
toml_node *maparr_node;
s_token current;
try { current = tokenizer.getToken(); }
catch (std::logic_error e)
{
std::cerr << e.what() << std::endl;
return;
}
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");
TOMLMap::iterator it;
std::cout << current.value << std::endl;
std::string name = current.value;
std::vector<std::string> full_name;
size_t dot;
while (1)
{
completed = true;
break;
dot = name.find('.');
if (dot == std::string::npos)
break;
/* std::cout << dot << std::endl; */
full_name.push_back(name.substr(0, dot));
name.erase(0, dot + 1);
}
full_name.push_back(name);
for (size_t i = 0; i < full_name.size(); i++)
std::cout << full_name[i] << ", ";
std::cout << std::endl;
/* throw std::logic_error("tha end"); */
TOMLMap *local_root;
std::vector<std::string>::iterator subname = full_name.begin();
local_root = root;
while (1)
{
if (subname + 1 == full_name.end())
{
it = local_root->find(*subname);
if (it == local_root->end())
{
maparr_node = new toml_node;
TOMLMapArray *map_array = new TOMLMapArray;
map_array->push_back(map_node->getMap());
maparr_node->setMapArray(map_array);
(*local_root)[*subname] = maparr_node;
}
else
(it->second)->getMapArray()->push_back(map_node->getMap());
break;
}
else
{
it = local_root->find(*subname);
toml_node *map_node2;
map_node2 = new toml_node;
TOMLMap *map = new TOMLMap;
map_node2->setObject(map);
/* subname not found in local_root */
if (it == local_root->end())
{
(*local_root)[*subname] = map_node2;
local_root = map;
}
/* subname found in local_root */
else
{
local_root = *((it->second)->getMapArray()->end() - 1);
}
}
++subname;
}
}
toml_node *TOMLParser::parseString(void)
{
/* toml_node *node; */
toml_node *node = new toml_node;
std::string *sValue;
std::cerr << "Parsing string" << std::endl;
s_token token = tokenizer.getToken();
sValue = new std::string(token.value);
node->setString(sValue);
return (node);
}
toml_node *TOMLParser::parseNumber(void)
{
toml_node *node = new toml_node;
int value;
std::cerr << "Parsing number" << std::endl;
s_token token = tokenizer.getToken();
value = std::atoi(token.value.c_str());
node->setNumber(value);
return (node);
}
toml_node *TOMLParser::parseArray(void)
{
std::cerr << "Parsing array" << std::endl;
toml_node *node;
toml_node *result = new toml_node;
TOMLArray *array = new TOMLArray;
bool completed = false;
s_token current;
while (!completed)
{
if (!tokenizer.hasMoreTokens())
throw std::logic_error("No more tokens");
else
{
current = tokenizer.getToken();
switch (current.type)
{
case OPEN_BRACKET:
{
node = parseArray();
break;
}
case STRING:
{
tokenizer.rollBackToken();
node = parseString();
break;
}
case NUMBER:
{
tokenizer.rollBackToken();
node = parseNumber();
break;
}
case BOOL:
{
tokenizer.rollBackToken();
node = parseBool();
break;
}
case NIL:
{
node = parseNil();
break;
}
default:
{
throw std::logic_error("unkown token in parseArray");
}
}
array->push_back(node);
current = tokenizer.getToken();
if (current.type == COMMA)
continue;
else if (current.type == CLOSE_BRACKET)
completed = true;
else
throw std::invalid_argument("Unexpected token in array!");
}
}
result->setArr(array);
return (result);
}
toml_node *TOMLParser::parseBool(void)
{
toml_node *node = new toml_node;
bool value;
std::cerr << "Parsing bool" << std::endl;
s_token token = tokenizer.getToken();
if (token.value == "true")
value = true;
else if (token.value == "false")
value = false;
else
throw std::invalid_argument("Unexpected bool value");
node->setBool(value);
return (node);
}
toml_node *TOMLParser::parseNil(void)
{
toml_node *node = new toml_node;
std::cerr << "Parsing NIL" << std::endl;
node->setNil();
return (node);
}
/* parse tha root ! */
TOMLMap *TOMLParser::parse(void)
{
std::cerr << "Parsing ROOT" << std::endl;
root = new TOMLMap;
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();
parseMapArray();
}
else
{
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 OPEN_BRACKET:
{
(*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");
}
}
}
else
{
completed = true;
break;
}
}
return (root);
}
return (root);
}

View File

@@ -108,13 +108,13 @@ namespace config
}
else
{
token.type = ARR_OPEN;
token.type = OPEN_BRACKET;
file.seekg(prev_pos);
}
}
else if (c == ']')
token.type = ARR_CLOSE;
token.type = CLOSE_BRACKET;
else if (c == '=')
token.type = ASSIGN;
else if (c == '\n')