diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2019-12-07 03:36:21 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2019-12-07 03:36:21 +0300 |
commit | 00863566ec4601c65c435b74e575d49546a1c707 (patch) | |
tree | 479a0a6e96aba8191c7a65ea9bee2f4d5e3a4aba /server/lexer/token.hpp | |
parent | add stress_test.py (diff) | |
download | math-server-00863566ec4601c65c435b74e575d49546a1c707.tar.gz math-server-00863566ec4601c65c435b74e575d49546a1c707.zip |
split server into multiple components
In a vague attempt to make header files more readable, split server/
into a number of components.
Also, refactor the unit tests to use the "Data-driven test cases" of
Boost.Test.
Diffstat (limited to 'server/lexer/token.hpp')
-rw-r--r-- | server/lexer/token.hpp | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/server/lexer/token.hpp b/server/lexer/token.hpp new file mode 100644 index 0000000..6f98383 --- /dev/null +++ b/server/lexer/token.hpp @@ -0,0 +1,49 @@ +#pragma once + +#include "token_type.hpp" + +#include <cstddef> + +#include <string_view> +#include <utility> +#include <variant> + +namespace math::server::lexer { + +class Token { +public: + using Type = token::Type; + + explicit Token(token::Type type); + explicit Token(double value); + + bool operator==(const Token& other) const; + bool operator!=(const Token& other) const { return !(*this == other); } + + Type get_type() const { return m_type; } + + double as_number() const; + +private: + token::Type m_type; + std::variant<double> m_value; + + friend std::ostream& operator<<(std::ostream&, const Token&); +}; + +class ParsedToken : public Token { +public: + ParsedToken(Token&& token, std::size_t pos, const std::string_view& view) + : Token{std::move(token)}, m_pos{pos}, m_view{view} { + } + + std::size_t get_pos() const { return m_pos; } + + std::size_t get_length() const { return m_view.length(); } + +private: + std::size_t m_pos; + std::string_view m_view; +}; + +} |