Token.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #ifndef FORTH_KATA_TOKEN_H
  2. #define FORTH_KATA_TOKEN_H
  3. #include <cstdint>
  4. #include <iosfwd>
  5. #include <stdexcept>
  6. #include <string>
  7. #include <gsl/assert>
  8. namespace forth {
  9. namespace token {
  10. /**
  11. * Structure representing a number token (An integer literal).
  12. */
  13. struct Number final
  14. {
  15. std::int16_t value; ///< The numeric value of the token.
  16. bool operator==(Number const &rhs) const noexcept;
  17. bool operator!=(Number const &rhs) const noexcept;
  18. };
  19. /**
  20. * Overloaded output operator for Number tokens.
  21. * @param os The output stream.
  22. * @param number The number.
  23. * @return The output stream.
  24. */
  25. std::ostream &operator<<(std::ostream &os, Number const &number);
  26. /**
  27. * Structure representing a keyword token (Special words with fixed meaning).
  28. */
  29. struct Keyword final
  30. {
  31. /*
  32. * This value holds the value of the key word.
  33. */
  34. std::string value;
  35. bool operator==(Keyword const &rhs) const noexcept;
  36. bool operator!=(Keyword const &rhs) const noexcept;
  37. };
  38. /**
  39. * Overloaded output operator for Keyword tokens.
  40. * @param os The output stream.
  41. * @param keyword The keyword.
  42. * @return The output stream.
  43. */
  44. std::ostream &operator<<(std::ostream &os, Keyword const &keyword);
  45. /**
  46. * Structure representing a word token (Words that are neither keywords nor numbers).
  47. */
  48. struct Word final
  49. {
  50. std::string value;
  51. bool operator==(Word const &rhs) const noexcept;
  52. bool operator!=(Word const &rhs) const noexcept;
  53. };
  54. /**
  55. * Overloaded output operator for Word tokens.
  56. * @param os The output stream.
  57. * @param word The word.
  58. * @return The output stream.
  59. */
  60. std::ostream &operator<<(std::ostream &os, Word const &word);
  61. } // namespace token
  62. struct WrongTokenException : std::runtime_error
  63. {
  64. using std::runtime_error::runtime_error;
  65. using std::runtime_error::operator=;
  66. };
  67. class Token final
  68. {
  69. enum class Type
  70. {
  71. Number,
  72. Keyword,
  73. Word,
  74. };
  75. public:
  76. Token(token::Number value) noexcept;
  77. Token(token::Keyword value) noexcept;
  78. Token(token::Word value) noexcept;
  79. Token(Token const &src);
  80. Token(Token &&src) noexcept;
  81. Token &operator=(Token const &src);
  82. Token &operator=(Token &&src) noexcept;
  83. ~Token() noexcept;
  84. bool IsNumber() const noexcept;
  85. bool IsKeyword() const noexcept;
  86. bool IsWord() const noexcept;
  87. token::Number const &GetNumber() const;
  88. token::Keyword const &GetKeyword() const;
  89. token::Word const &GetWord() const;
  90. bool operator==(Token const &rhs) const noexcept;
  91. bool operator!=(Token const &rhs) const noexcept;
  92. private:
  93. void destroy() noexcept;
  94. template<typename TType>
  95. void destroy(TType *p)
  96. {
  97. Expects(p != nullptr);
  98. p->~TType();
  99. }
  100. Type type;
  101. union
  102. {
  103. token::Number number;
  104. token::Keyword keyword;
  105. token::Word word;
  106. };
  107. };
  108. /**
  109. * Overloaded output operator for Tokens.
  110. * @param os The output stream.
  111. * @param token The token.
  112. * @return The output stream.
  113. */
  114. std::ostream &operator<<(std::ostream &os, Token const &token);
  115. } // namespace forth
  116. #endif // !FORTH_KATA_TOKEN_H