123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- #ifndef FORTH_KATA_TOKEN_H
- #define FORTH_KATA_TOKEN_H
- #include <cstdint>
- #include <iosfwd>
- #include <stdexcept>
- #include <string>
- #include <gsl/assert>
- namespace forth {
- namespace token {
- struct Number final
- {
- std::int16_t value;
- bool operator==(Number const &rhs) const noexcept;
- bool operator!=(Number const &rhs) const noexcept;
- };
- std::ostream &operator<<(std::ostream &os, Number const &number);
- struct Keyword final
- {
-
- std::string value;
- bool operator==(Keyword const &rhs) const noexcept;
- bool operator!=(Keyword const &rhs) const noexcept;
- };
- std::ostream &operator<<(std::ostream &os, Keyword const &keyword);
- struct Word final
- {
- std::string value;
- bool operator==(Word const &rhs) const noexcept;
- bool operator!=(Word const &rhs) const noexcept;
- };
- std::ostream &operator<<(std::ostream &os, Word const &word);
- }
- struct WrongTokenException : std::runtime_error
- {
- using std::runtime_error::runtime_error;
- using std::runtime_error::operator=;
- };
- class Token final
- {
- enum class Type
- {
- Number,
- Keyword,
- Word,
- };
- public:
- Token(token::Number value) noexcept;
- Token(token::Keyword value) noexcept;
- Token(token::Word value) noexcept;
- Token(Token const &src);
- Token(Token &&src) noexcept;
- Token &operator=(Token const &src);
- Token &operator=(Token &&src) noexcept;
- ~Token() noexcept;
- bool IsNumber() const noexcept;
- bool IsKeyword() const noexcept;
- bool IsWord() const noexcept;
- token::Number const &GetNumber() const;
- token::Keyword const &GetKeyword() const;
- token::Word const &GetWord() const;
- bool operator==(Token const &rhs) const noexcept;
- bool operator!=(Token const &rhs) const noexcept;
- private:
- void destroy() noexcept;
- template<typename TType>
- void destroy(TType *p)
- {
- Expects(p != nullptr);
- p->~TType();
- }
- Type type;
- union
- {
- token::Number number;
- token::Keyword keyword;
- token::Word word;
- };
- };
- std::ostream &operator<<(std::ostream &os, Token const &token);
- }
- #endif
|