Token.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/StringView.h>
  8. #include <AK/Types.h>
  9. namespace Cpp {
  10. #define FOR_EACH_TOKEN_TYPE \
  11. __TOKEN(Unknown) \
  12. __TOKEN(Whitespace) \
  13. __TOKEN(PreprocessorStatement) \
  14. __TOKEN(IncludeStatement) \
  15. __TOKEN(IncludePath) \
  16. __TOKEN(LeftParen) \
  17. __TOKEN(RightParen) \
  18. __TOKEN(LeftCurly) \
  19. __TOKEN(RightCurly) \
  20. __TOKEN(LeftBracket) \
  21. __TOKEN(RightBracket) \
  22. __TOKEN(Less) \
  23. __TOKEN(Greater) \
  24. __TOKEN(LessEquals) \
  25. __TOKEN(GreaterEquals) \
  26. __TOKEN(LessLess) \
  27. __TOKEN(GreaterGreater) \
  28. __TOKEN(LessLessEquals) \
  29. __TOKEN(GreaterGreaterEquals) \
  30. __TOKEN(LessGreater) \
  31. __TOKEN(Comma) \
  32. __TOKEN(Plus) \
  33. __TOKEN(PlusPlus) \
  34. __TOKEN(PlusEquals) \
  35. __TOKEN(Minus) \
  36. __TOKEN(MinusMinus) \
  37. __TOKEN(MinusEquals) \
  38. __TOKEN(Asterisk) \
  39. __TOKEN(AsteriskEquals) \
  40. __TOKEN(Slash) \
  41. __TOKEN(SlashEquals) \
  42. __TOKEN(Percent) \
  43. __TOKEN(PercentEquals) \
  44. __TOKEN(Caret) \
  45. __TOKEN(CaretEquals) \
  46. __TOKEN(ExclamationMark) \
  47. __TOKEN(ExclamationMarkEquals) \
  48. __TOKEN(Equals) \
  49. __TOKEN(EqualsEquals) \
  50. __TOKEN(And) \
  51. __TOKEN(AndAnd) \
  52. __TOKEN(AndEquals) \
  53. __TOKEN(Pipe) \
  54. __TOKEN(PipePipe) \
  55. __TOKEN(PipeEquals) \
  56. __TOKEN(Tilde) \
  57. __TOKEN(QuestionMark) \
  58. __TOKEN(Colon) \
  59. __TOKEN(ColonColon) \
  60. __TOKEN(ColonColonAsterisk) \
  61. __TOKEN(Semicolon) \
  62. __TOKEN(Dot) \
  63. __TOKEN(DotAsterisk) \
  64. __TOKEN(Arrow) \
  65. __TOKEN(ArrowAsterisk) \
  66. __TOKEN(DoubleQuotedString) \
  67. __TOKEN(SingleQuotedString) \
  68. __TOKEN(RawString) \
  69. __TOKEN(EscapeSequence) \
  70. __TOKEN(Comment) \
  71. __TOKEN(Integer) \
  72. __TOKEN(Float) \
  73. __TOKEN(Keyword) \
  74. __TOKEN(KnownType) \
  75. __TOKEN(Identifier) \
  76. __TOKEN(EOF_TOKEN)
  77. struct Position {
  78. size_t line { 0 };
  79. size_t column { 0 };
  80. bool operator<(Position const&) const;
  81. bool operator<=(Position const&) const;
  82. bool operator>(Position const&) const;
  83. bool operator==(Position const&) const;
  84. };
  85. struct Token {
  86. enum class Type {
  87. #define __TOKEN(x) x,
  88. FOR_EACH_TOKEN_TYPE
  89. #undef __TOKEN
  90. };
  91. Token(Type type, Position const& start, Position const& end, StringView text)
  92. : m_type(type)
  93. , m_start(start)
  94. , m_end(end)
  95. , m_text(text)
  96. {
  97. }
  98. static char const* type_to_string(Type t)
  99. {
  100. switch (t) {
  101. #define __TOKEN(x) \
  102. case Type::x: \
  103. return #x;
  104. FOR_EACH_TOKEN_TYPE
  105. #undef __TOKEN
  106. }
  107. VERIFY_NOT_REACHED();
  108. }
  109. ByteString to_byte_string() const;
  110. ByteString type_as_byte_string() const;
  111. Position const& start() const { return m_start; }
  112. Position const& end() const { return m_end; }
  113. void set_start(Position const& other) { m_start = other; }
  114. void set_end(Position const& other) { m_end = other; }
  115. Type type() const { return m_type; }
  116. StringView text() const { return m_text; }
  117. private:
  118. Type m_type { Type::Unknown };
  119. Position m_start;
  120. Position m_end;
  121. StringView m_text;
  122. };
  123. }
  124. #undef FOR_EACH_TOKEN_TYPE