feat: add comment handling

This commit is contained in:
3lswear
2022-02-12 17:53:10 +03:00
parent 3f3d25d9c7
commit 7df10265e0
2 changed files with 33 additions and 3 deletions

View File

@@ -40,6 +40,7 @@ namespace config
} }
Tokenizer::Tokenizer(char *filename) Tokenizer::Tokenizer(char *filename)
{ {
last_token = NO_TOK;
file.open(filename, std::ios::in); file.open(filename, std::ios::in);
if (!file.good()) if (!file.good())
{ {
@@ -50,7 +51,7 @@ namespace config
bool Tokenizer::firstToken() bool Tokenizer::firstToken()
{ {
// doesn't account for indent! // doesn't account for indent!
if (file.tellg() == 0 || file.tellg() == 1 || (last_token == NEWLINE)) if (file.tellg() == 0 || file.tellg() == 1 || (last_token == NEWLINE || last_token == NO_TOK))
return (true); return (true);
else else
return (false); return (false);
@@ -198,10 +199,37 @@ namespace config
else if (c == ',') else if (c == ',')
token.type = COMMA; token.type = COMMA;
else if (c == '#') else if (c == '#')
token.type = COMMENT; {
// consume all comments
do
file.get(c);
while (c != '\n' || file.eof());
DBOUT << "getting comment token" << ENDL;
if (last_token == NO_TOK || last_token == NEWLINE)
{
DBOUT << "getting first token instead of comment" << ENDL;
struct s_token actual;
actual.type = NEWLINE;
while (actual.type == NEWLINE)
actual = getToken();
DBOUT
<< "actual token: '"
<< actual.value << "', type: "
<< actual.type
<< ENDL;
token = actual;
}
else
token.type = NEWLINE;
}
last_token = token.type; last_token = token.type;
DBOUT << YELLO << "GOT " << token.value << ", type: " << token.type << ENDL;
return (token); return (token);
} }
char Tokenizer::getWithoutWhiteSpace(void) char Tokenizer::getWithoutWhiteSpace(void)
{ {
char c = ' '; char c = ' ';

View File

@@ -27,7 +27,9 @@ namespace config
OPEN_BRACKET, OPEN_BRACKET,
CLOSE_BRACKET, CLOSE_BRACKET,
MAP_DECL, MAP_DECL,
MAPARRAY_DECL MAPARRAY_DECL,
COMMENT,
NO_TOK
}; };
struct s_token struct s_token