mirror of
https://github.com/3lswear/webserv.git
synced 2025-10-29 13:27:59 +03:00
feat(tokenizer): add exceptions
This commit is contained in:
@@ -35,7 +35,7 @@ namespace config
|
|||||||
s_token current;
|
s_token current;
|
||||||
|
|
||||||
try { current = tokenizer.getToken(); }
|
try { current = tokenizer.getToken(); }
|
||||||
catch (std::logic_error e)
|
catch (std::exception &e)
|
||||||
{
|
{
|
||||||
std::cerr << e.what() << std::endl;
|
std::cerr << e.what() << std::endl;
|
||||||
return;
|
return;
|
||||||
@@ -71,7 +71,7 @@ namespace config
|
|||||||
{
|
{
|
||||||
s_token nextToken;
|
s_token nextToken;
|
||||||
try { nextToken = tokenizer.getToken(); }
|
try { nextToken = tokenizer.getToken(); }
|
||||||
catch (std::logic_error e)
|
catch (std::exception &e)
|
||||||
{
|
{
|
||||||
std::cerr << e.what() << std::endl;
|
std::cerr << e.what() << std::endl;
|
||||||
break;
|
break;
|
||||||
@@ -158,7 +158,7 @@ namespace config
|
|||||||
s_token current;
|
s_token current;
|
||||||
|
|
||||||
try { current = tokenizer.getToken(); }
|
try { current = tokenizer.getToken(); }
|
||||||
catch (std::logic_error e)
|
catch (std::exception &e)
|
||||||
{
|
{
|
||||||
std::cerr << e.what() << std::endl;
|
std::cerr << e.what() << std::endl;
|
||||||
return;
|
return;
|
||||||
@@ -313,7 +313,7 @@ namespace config
|
|||||||
{
|
{
|
||||||
s_token current;
|
s_token current;
|
||||||
try { current = tokenizer.getToken(); }
|
try { current = tokenizer.getToken(); }
|
||||||
catch (std::logic_error e)
|
catch (std::exception &e)
|
||||||
{
|
{
|
||||||
std::cerr << e.what() << std::endl;
|
std::cerr << e.what() << std::endl;
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -178,6 +178,8 @@ namespace config
|
|||||||
token.value += c;
|
token.value += c;
|
||||||
file.get(c);
|
file.get(c);
|
||||||
}
|
}
|
||||||
|
if (token.value != "false")
|
||||||
|
throw InvalidToken(token.value);
|
||||||
file.seekg(-1, std::ios_base::cur);
|
file.seekg(-1, std::ios_base::cur);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -189,12 +191,20 @@ namespace config
|
|||||||
token.value += c;
|
token.value += c;
|
||||||
file.get(c);
|
file.get(c);
|
||||||
}
|
}
|
||||||
|
if (token.value != "true")
|
||||||
|
throw InvalidToken(token.value);
|
||||||
file.seekg(-1, std::ios_base::cur);
|
file.seekg(-1, std::ios_base::cur);
|
||||||
}
|
}
|
||||||
else if (c == 'n')
|
else if (c == 'n')
|
||||||
{
|
{
|
||||||
token.type = NIL;
|
token.type = NIL;
|
||||||
file.seekg(3, std::ios_base::cur);
|
while (std::isalpha(c))
|
||||||
|
{
|
||||||
|
token.value += c;
|
||||||
|
file.get(c);
|
||||||
|
}
|
||||||
|
if (token.value != "null")
|
||||||
|
throw InvalidToken(token.value);
|
||||||
}
|
}
|
||||||
else if (c == ',')
|
else if (c == ',')
|
||||||
token.type = COMMA;
|
token.type = COMMA;
|
||||||
@@ -237,9 +247,7 @@ namespace config
|
|||||||
{
|
{
|
||||||
file.get(c);
|
file.get(c);
|
||||||
if ((c == ' ') && !file.good())
|
if ((c == ' ') && !file.good())
|
||||||
{
|
throw NoMoreTokens();
|
||||||
throw std::logic_error("No more tokens!");
|
|
||||||
}
|
|
||||||
else if (!file.good())
|
else if (!file.good())
|
||||||
return (c);
|
return (c);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,6 +59,37 @@ namespace config
|
|||||||
void rollBackToken();
|
void rollBackToken();
|
||||||
void set_last(e_token type);
|
void set_last(e_token type);
|
||||||
|
|
||||||
|
class NoMoreTokens: public std::exception
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual const char *what() const throw()
|
||||||
|
{
|
||||||
|
return ("No more tokens!");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
class InvalidToken: public std::exception
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
std::string *msg;
|
||||||
|
|
||||||
|
public:
|
||||||
|
InvalidToken(const std::string &token)
|
||||||
|
{
|
||||||
|
|
||||||
|
msg = new std::string("Invalid token: '" + token + "'");
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual const char *what() const throw()
|
||||||
|
{
|
||||||
|
return (msg->c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~InvalidToken() throw()
|
||||||
|
{
|
||||||
|
delete msg;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* struct s_token Tokenizer::getKey(void) */
|
/* struct s_token Tokenizer::getKey(void) */
|
||||||
|
|||||||
Reference in New Issue
Block a user