feat(parser): add exceptions

This commit is contained in:
3lswear
2022-02-13 22:39:58 +03:00
parent 4be3ba3f9d
commit 9d5c0f79e8
2 changed files with 66 additions and 12 deletions

View File

@@ -43,11 +43,13 @@ namespace config
if (current.type == MAP_DECL) if (current.type == MAP_DECL)
{ {
if (tokenizer.getToken().type != NEWLINE) if (tokenizer.getToken().type != NEWLINE)
throw std::logic_error("no newline after MAP_DECL"); throw ExpectedToken("newline", current.value);
// throw std::logic_error("no newline after MAP_DECL");
map_node = parseMap(); map_node = parseMap();
} }
else else
throw std::logic_error("unexpected token in processMap"); // throw std::logic_error("unexpected token in processMap");
throw UnexpectedToken("", current.value);
/* std::cout << current.value << std::endl; */ /* std::cout << current.value << std::endl; */
@@ -90,8 +92,11 @@ namespace config
} }
std::string key = nextToken.value; std::string key = nextToken.value;
/* std::cerr << key << std::endl; */ /* std::cerr << key << std::endl; */
DBOUT << RED << "key is " << key << ENDL;
if (tokenizer.getToken().type != ASSIGN) if (tokenizer.getToken().type != ASSIGN)
throw std::logic_error("EXPECTED assign!"); throw ExpectedToken("assign", "after " + key);
// throw std::logic_error("EXPECTED assign! 1");
nextToken = tokenizer.getToken(); nextToken = tokenizer.getToken();
switch (nextToken.type) switch (nextToken.type)
{ {
@@ -126,8 +131,9 @@ namespace config
} }
default: default:
{ {
throw UnexpectedToken(nextToken.value, key);
/* throw std::logic_error("jopa in parseMap"); */ /* throw std::logic_error("jopa in parseMap"); */
std::cerr << "Unknown token, marking as complete" << std::endl; // std::cerr << "Unknown token, marking as complete" << std::endl;
completed = true; completed = true;
break; break;
} }
@@ -138,13 +144,14 @@ namespace config
break; break;
if (nextToken.type != NEWLINE) if (nextToken.type != NEWLINE)
{ {
throw std::logic_error("EXPECTED newline"); // throw std::logic_error("EXPECTED newline");
throw ExpectedToken("newline", "parsing Hash Table");
} }
} }
else // else
{ // {
throw std::logic_error("parseMap: no more tokens"); // throw std::logic_error("parseMap: no more tokens");
} // }
} }
node->setObject(mapObject); node->setObject(mapObject);
return (node); return (node);
@@ -257,7 +264,9 @@ namespace config
} }
default: default:
{ {
throw std::logic_error("unkown token in parseArray"); // throw std::logic_error("unkown token in parseArray");
throw UnexpectedToken("entry " + current.value,
"in Array");
} }
} }
@@ -268,7 +277,8 @@ namespace config
else if (current.type == CLOSE_BRACKET) else if (current.type == CLOSE_BRACKET)
completed = true; completed = true;
else else
throw std::invalid_argument("Unexpected token in array!"); throw UnexpectedToken(current.value, ", when expected COMMA, or CLOSE_BRACKET");
// throw std::invalid_argument("Unexpected token in array!");
} }
} }
result->setArr(array); result->setArr(array);

View File

@@ -41,8 +41,52 @@ namespace config
toml_node *parseNil(void); toml_node *parseNil(void);
}; class ExpectedToken: public std::exception
{
protected:
std::string *msg;
public:
ExpectedToken(const std::string &missing, const std::string context)
{
msg = new std::string("Config file: ");
*msg += ("Expected " + missing + " at " + context);
}
virtual const char *what() const throw()
{
return (msg->c_str());
}
virtual ~ExpectedToken() throw()
{
delete msg;
}
};
class UnexpectedToken: public std::exception
{
protected:
std::string *msg;
public:
UnexpectedToken(const std::string &unexpected, const std::string context)
{
msg = new std::string("Config file: ");
*msg += ("Unexpected " + unexpected + " " + context);
}
virtual const char *what() const throw()
{
return (msg->c_str());
}
virtual ~UnexpectedToken() throw()
{
delete msg;
}
};
};
/* parse tha root ! */ /* parse tha root ! */
/* TOMLMap *TOMLParser::parse(void); */ /* TOMLMap *TOMLParser::parse(void); */
} }