From 3a5879eb4d95b0742da7d0d23aa3077997eb9f3d Mon Sep 17 00:00:00 2001 From: 3lswear Date: Sat, 12 Feb 2022 18:54:45 +0300 Subject: [PATCH] feat(tokenizer): add exceptions --- src/config/TOMLParser.cpp | 8 ++++---- src/config/Tokenizer.cpp | 16 ++++++++++++---- src/config/Tokenizer.hpp | 31 +++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/config/TOMLParser.cpp b/src/config/TOMLParser.cpp index 42630fe..182777e 100644 --- a/src/config/TOMLParser.cpp +++ b/src/config/TOMLParser.cpp @@ -35,7 +35,7 @@ namespace config s_token current; try { current = tokenizer.getToken(); } - catch (std::logic_error e) + catch (std::exception &e) { std::cerr << e.what() << std::endl; return; @@ -71,7 +71,7 @@ namespace config { s_token nextToken; try { nextToken = tokenizer.getToken(); } - catch (std::logic_error e) + catch (std::exception &e) { std::cerr << e.what() << std::endl; break; @@ -158,7 +158,7 @@ namespace config s_token current; try { current = tokenizer.getToken(); } - catch (std::logic_error e) + catch (std::exception &e) { std::cerr << e.what() << std::endl; return; @@ -313,7 +313,7 @@ namespace config { s_token current; try { current = tokenizer.getToken(); } - catch (std::logic_error e) + catch (std::exception &e) { std::cerr << e.what() << std::endl; break; diff --git a/src/config/Tokenizer.cpp b/src/config/Tokenizer.cpp index ed9455b..23c25f3 100644 --- a/src/config/Tokenizer.cpp +++ b/src/config/Tokenizer.cpp @@ -178,6 +178,8 @@ namespace config token.value += c; file.get(c); } + if (token.value != "false") + throw InvalidToken(token.value); file.seekg(-1, std::ios_base::cur); } @@ -189,12 +191,20 @@ namespace config token.value += c; file.get(c); } + if (token.value != "true") + throw InvalidToken(token.value); file.seekg(-1, std::ios_base::cur); } else if (c == 'n') { 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 == ',') token.type = COMMA; @@ -237,9 +247,7 @@ namespace config { file.get(c); if ((c == ' ') && !file.good()) - { - throw std::logic_error("No more tokens!"); - } + throw NoMoreTokens(); else if (!file.good()) return (c); } diff --git a/src/config/Tokenizer.hpp b/src/config/Tokenizer.hpp index 2de6808..dcddc8d 100644 --- a/src/config/Tokenizer.hpp +++ b/src/config/Tokenizer.hpp @@ -59,6 +59,37 @@ namespace config void rollBackToken(); 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) */