1

Update Token class with subtoken and int conversion

This commit is contained in:
2023-03-21 14:32:30 +01:00
parent 6179ab13be
commit 0cae53a3d1
2 changed files with 28 additions and 5 deletions

View File

@ -4,6 +4,7 @@
#include <array>
#include <string>
#include <charconv>
#include "Token.h"
Token::Token(Token::Type type) : type(type) {}
@ -23,6 +24,25 @@ auto Token::getTypeName() const -> std::string {
"UNEXPECTED"}[type];
}
auto Token::subtoken(uint8_t pos, uint8_t len) const -> Token {
return std::move(Token(type, lexeme.substr(pos, len)));
}
Token::operator std::string_view() const {
return lexeme;
}
Token::operator std::string() const {
return lexeme;
}
// https://stackoverflow.com/questions/56634507/safely-convert-stdstring-view-to-int-like-stoi-or-atoi#answer-65675200
Token::operator uint8_t() const {
uint8_t out;
const std::from_chars_result result = std::from_chars(lexeme.data(), lexeme.data() + lexeme.size(), out);
if (result.ec == std::errc::invalid_argument || result.ec == std::errc::result_out_of_range)
{
throw "Conversion Error: Can't convert Token to uint8_t!";
}
return out;
}

View File

@ -35,9 +35,9 @@ public:
auto operator=(const Token &copy) -> Token & = default;
Token(Token &&move) = default;
Token(Token &&move) noexcept = default;
auto operator=(Token &&move) -> Token & = default;
auto operator=(Token &&move) noexcept -> Token & = default;
~Token() = default;
@ -45,13 +45,16 @@ public:
[[nodiscard]] auto getTypeName() const -> std::string;
[[nodiscard]] auto subtoken(uint8_t pos, uint8_t len) const -> Token;
explicit operator std::string_view() const;
explicit operator std::string() const;
explicit operator uint8_t() const;
private:
Type type;
// ! The Token only contains a reference to the string kept inside the Lexer.
// ! If the Lexer is gone, all Tokens will be invalid!
std::string lexeme;
};