feat: rewrite more to own exceptions + handle put_to_subtable error

This commit is contained in:
3lswear
2022-02-20 20:03:27 +03:00
parent be0d34b8d5
commit 41b2cdb90e
2 changed files with 83 additions and 135 deletions

View File

@@ -33,8 +33,7 @@ namespace config
void TOMLParser::processMap(void) void TOMLParser::processMap(void)
{ {
/* std::cerr << "Processing map" << std::endl; */ /* std::cerr << "Processing map" << std::endl; */
// toml_node *map_node; toml_node *map_node;
std::auto_ptr<toml_node> map_node(new toml_node);
s_token current; s_token current;
try { current = tokenizer.getToken(); } try { current = tokenizer.getToken(); }
@@ -46,13 +45,11 @@ namespace config
if (current.type == MAP_DECL) if (current.type == MAP_DECL)
{ {
if (tokenizer.getToken().type != NEWLINE) if (tokenizer.getToken().type != NEWLINE)
throw ExpectedToken("newline", current.value); throw ExpectedToken("newline", "after " + current.value);
// throw std::logic_error("no newline after MAP_DECL"); map_node = parseMap();
map_node.reset(parseMap());
} }
else else
// throw std::logic_error("unexpected token in processMap"); throw UnexpectedToken("", "near " + current.value);
throw UnexpectedToken("", current.value);
/* std::cout << current.value << std::endl; */ /* std::cout << current.value << std::endl; */
@@ -60,14 +57,22 @@ namespace config
full_name = split_name(current.value); full_name = split_name(current.value);
put_to_subtable(root, full_name, map_node.release(), toml_node::MAP);; try
{
put_to_subtable(root, full_name, map_node, toml_node::MAP);;
}
catch (std::domain_error &e)
{
config::display(root);
DBOUT << "CAUGHT in process Map" <<ENDL;
config::clean_generic(map_node);;
throw;
}
} }
toml_node *TOMLParser::parseMap(void) toml_node *TOMLParser::parseMap(void)
{ {
/* std::cerr << "Parsing map" << std::endl; */ /* std::cerr << "Parsing map" << std::endl; */
// std::auto_ptr<TOMLMap> mapObject(new TOMLMap);
toml_node *node = new toml_node; toml_node *node = new toml_node;
TOMLMap *mapObject = new TOMLMap; TOMLMap *mapObject = new TOMLMap;
bool completed = false; bool completed = false;
@@ -103,7 +108,6 @@ namespace config
/* std::cerr << key << std::endl; */ /* std::cerr << key << std::endl; */
if (tokenizer.getToken().type != ASSIGN) if (tokenizer.getToken().type != ASSIGN)
throw ExpectedToken("assign", "after " + key); throw ExpectedToken("assign", "after " + key);
// throw std::logic_error("EXPECTED assign! 1");
nextToken = tokenizer.getToken(); nextToken = tokenizer.getToken();
switch (nextToken.type) switch (nextToken.type)
@@ -146,8 +150,7 @@ namespace config
break; break;
if (nextToken.type != NEWLINE) if (nextToken.type != NEWLINE)
{ {
// throw std::logic_error("EXPECTED newline"); throw ExpectedToken("newline", "at parsing Hash Table");
throw ExpectedToken("newline", "parsing Hash Table");
} }
} }
} }
@@ -168,8 +171,7 @@ namespace config
{ {
/* std::cerr << "Parsing MapArray" << std::endl; */ /* std::cerr << "Parsing MapArray" << std::endl; */
// toml_node *map_node; toml_node *map_node;
std::auto_ptr<toml_node> map_node(new toml_node);
s_token current; s_token current;
try { current = tokenizer.getToken(); } try { current = tokenizer.getToken(); }
@@ -181,21 +183,11 @@ namespace config
if (current.type == MAPARRAY_DECL) if (current.type == MAPARRAY_DECL)
{ {
if (tokenizer.getToken().type != NEWLINE) if (tokenizer.getToken().type != NEWLINE)
throw std::logic_error("no newline after map_array declaration"); throw ExpectedToken("newline", "after " + current.value);
try map_node = parseMap();
{
map_node.reset(parseMap());
}
catch (std::exception &e)
{
// DBOUT << "map_node is " << map_node->getMap() << ENDL;
// for (TOMLMap::iterator it = map->begin();)
throw;
}
} }
else else
throw std::logic_error("unexpected token in process Map Array"); throw UnexpectedToken(current.value, "when expected array of map declaration");
/* std::cout << current.value << std::endl; */ /* std::cout << current.value << std::endl; */
@@ -203,7 +195,15 @@ namespace config
full_name = split_name(current.value); full_name = split_name(current.value);
put_to_subtable(root, full_name, map_node.release(), toml_node::MAPARRAY); try
{
put_to_subtable(root, full_name, map_node, toml_node::MAPARRAY);
}
catch (std::domain_error &e)
{
config::clean_generic(map_node);
throw;
}
} }
@@ -246,51 +246,39 @@ namespace config
try try
{ {
while (!completed)
while (!completed) {
{
// if (!tokenizer.hasMoreTokens())
// throw std::logic_error("No more tokens");
// else
// {
current = tokenizer.getToken(); current = tokenizer.getToken();
switch (current.type) switch (current.type)
{ {
// case OPEN_BRACKET:
// {
// node = parseArray();
// break;
// }
case STRING: case STRING:
{ {
tokenizer.rollBackToken(); tokenizer.rollBackToken();
node = parseString(); node = parseString();
break; break;
} }
case NUMBER: case NUMBER:
{ {
tokenizer.rollBackToken(); tokenizer.rollBackToken();
node = parseNumber(); node = parseNumber();
break; break;
} }
case BOOL: case BOOL:
{ {
tokenizer.rollBackToken(); tokenizer.rollBackToken();
node = parseBool(); node = parseBool();
break; break;
} }
case NIL: case NIL:
{ {
node = parseNil(); node = parseNil();
break; break;
} }
default: default:
{ {
// throw std::logic_error("unkown token in parseArray"); throw UnexpectedToken("entry " + current.value,
throw UnexpectedToken("entry " + current.value, "in array");
"in Array"); }
}
} }
array->push_back(node); array->push_back(node);
current = tokenizer.getToken(); current = tokenizer.getToken();
@@ -300,9 +288,7 @@ namespace config
completed = true; completed = true;
else else
throw UnexpectedToken(current.value, ", when expected COMMA, or CLOSE_BRACKET"); throw UnexpectedToken(current.value, ", when expected COMMA, or CLOSE_BRACKET");
// throw std::invalid_argument("Unexpected token in array!"); }
// }
}
} }
catch (std::domain_error &e) catch (std::domain_error &e)
{ {
@@ -323,10 +309,8 @@ namespace config
s_token token = tokenizer.getToken(); s_token token = tokenizer.getToken();
if (token.value == "true") if (token.value == "true")
value = true; value = true;
else if (token.value == "false")
value = false;
else else
throw std::invalid_argument("Unexpected bool value"); value = false;
toml_node *node = new toml_node; toml_node *node = new toml_node;
node->setBool(value); node->setBool(value);
@@ -346,7 +330,6 @@ namespace config
void TOMLParser::parse(void) void TOMLParser::parse(void)
{ {
/* std::cerr << "Parsing ROOT" << std::endl; */ /* std::cerr << "Parsing ROOT" << std::endl; */
root = new TOMLMap;
bool completed = false; bool completed = false;
while (!completed) while (!completed)
{ {
@@ -371,62 +354,11 @@ namespace config
tokenizer.set_last(NEWLINE); tokenizer.set_last(NEWLINE);
tokenizer.rollBackToken(); tokenizer.rollBackToken();
processMap(); processMap();
continue; // continue;
}
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:
{
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 else
{
completed = true; completed = true;
break;
}
} }
} }
@@ -445,10 +377,6 @@ namespace config
} }
full_name.push_back(name); full_name.push_back(name);
/* for (size_t i = 0; i < full_name.size(); i++) */
/* std::cout << full_name[i] << ", "; */
/* std::cout << std::endl; */
return (full_name); return (full_name);
} }
@@ -488,7 +416,7 @@ namespace config
(*local_root)[*subname] = map_node; (*local_root)[*subname] = map_node;
} }
else else
throw std::logic_error("map already in subtable!"); throw std::domain_error("Config file: Map " + *subname + " already exists in subtable!");
} }
break; break;
} }

View File

@@ -45,6 +45,7 @@ namespace config
toml_node *parseNil(void); toml_node *parseNil(void);
// Expected + missing + context
class ExpectedToken: public std::domain_error class ExpectedToken: public std::domain_error
{ {
protected: protected:
@@ -54,7 +55,7 @@ namespace config
ExpectedToken(const std::string &missing, const std::string context): std::domain_error("ExpectedToken") ExpectedToken(const std::string &missing, const std::string context): std::domain_error("ExpectedToken")
{ {
msg = std::string("Config file: "); msg = std::string("Config file: ");
msg += ("Expected " + missing + " at " + context); msg += ("Expected " + missing + " " + context);
} }
virtual const char *what() const throw() virtual const char *what() const throw()
@@ -63,10 +64,10 @@ namespace config
} }
virtual ~ExpectedToken() throw() virtual ~ExpectedToken() throw()
{ {}
// delete msg;
}
}; };
// Unexpected + unexpected + context
class UnexpectedToken: public std::domain_error class UnexpectedToken: public std::domain_error
{ {
protected: protected:
@@ -85,10 +86,29 @@ namespace config
} }
virtual ~UnexpectedToken() throw() virtual ~UnexpectedToken() throw()
{}
};
class AlreadyExists: public std::domain_error
{
protected:
std::string msg;
public:
AlreadyExists(const std::string &token, const std::string context): std::domain_error("ExpectedToken")
{ {
// delete msg; msg = std::string("Config file: ");
msg += (token + "already exists " + context);
} }
virtual const char *what() const throw()
{
return (msg.c_str());
}
virtual ~AlreadyExists() throw()
{}
}; };
}; };
/* parse tha root ! */ /* parse tha root ! */