feat: detect invalid tokens

This commit is contained in:
3lswear
2022-02-12 20:20:09 +03:00
parent 6b244dc57f
commit 15ae2b131f
2 changed files with 25 additions and 3 deletions

View File

@@ -38,6 +38,15 @@ namespace config
else else
return (false); return (false);
} }
bool istomlmapdecl(char c)
{
if (isalnum(c) || c == '-' || c == '_' || c == '.')
return (true);
else
return (false);
}
Tokenizer::Tokenizer(char *filename) Tokenizer::Tokenizer(char *filename)
{ {
last_token = NO_TOK; last_token = NO_TOK;
@@ -99,7 +108,7 @@ namespace config
{ {
token.type = MAPARRAY_DECL; token.type = MAPARRAY_DECL;
file.get(c); file.get(c);
while (c != ']') while (c != ']' && config::istomlmapdecl(c))
{ {
token.value += c; token.value += c;
file.get(c); file.get(c);
@@ -107,7 +116,7 @@ namespace config
if (c == ']') if (c == ']')
file.get(c); file.get(c);
if (c != ']') if (c != ']')
throw std::logic_error("error in MAPARRAY_DECL"); throw InvalidToken("[[" + token.value);
} }
else else
{ {
@@ -120,7 +129,8 @@ namespace config
file.get(c); file.get(c);
} }
if (c != ']') if (c != ']')
throw std::logic_error("malformed MAP_DECL"); // throw std::logic_error("malformed MAP_DECL");
throw InvalidToken(token.value);
} }
} }
else if (c == '[') else if (c == '[')
@@ -233,6 +243,16 @@ namespace config
token.type = NEWLINE; token.type = NEWLINE;
} }
else
{
while (!config::isspace(c) && (c != '\n'))
{
DBOUT << RED << "[" << c << "]" <<ENDL;
token.value += c;
file.get(c);
}
throw InvalidToken(token.value);
}
last_token = token.type; last_token = token.type;
DBOUT << YELLO << "GOT " << token.value << ", type: " << token.type << ENDL; DBOUT << YELLO << "GOT " << token.value << ", type: " << token.type << ENDL;

View File

@@ -43,6 +43,8 @@ namespace config
bool istomlkey(char c); bool istomlkey(char c);
bool istomlmapdecl(char c);
class Tokenizer class Tokenizer
{ {
private: private: