Token.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/StringView.h>
  28. #include <AK/Types.h>
  29. namespace Cpp {
  30. #define FOR_EACH_TOKEN_TYPE \
  31. __TOKEN(Unknown) \
  32. __TOKEN(Whitespace) \
  33. __TOKEN(PreprocessorStatement) \
  34. __TOKEN(IncludeStatement) \
  35. __TOKEN(IncludePath) \
  36. __TOKEN(LeftParen) \
  37. __TOKEN(RightParen) \
  38. __TOKEN(LeftCurly) \
  39. __TOKEN(RightCurly) \
  40. __TOKEN(LeftBracket) \
  41. __TOKEN(RightBracket) \
  42. __TOKEN(Less) \
  43. __TOKEN(Greater) \
  44. __TOKEN(LessEquals) \
  45. __TOKEN(GreaterEquals) \
  46. __TOKEN(LessLess) \
  47. __TOKEN(GreaterGreater) \
  48. __TOKEN(LessLessEquals) \
  49. __TOKEN(GreaterGreaterEquals) \
  50. __TOKEN(LessGreater) \
  51. __TOKEN(Comma) \
  52. __TOKEN(Plus) \
  53. __TOKEN(PlusPlus) \
  54. __TOKEN(PlusEquals) \
  55. __TOKEN(Minus) \
  56. __TOKEN(MinusMinus) \
  57. __TOKEN(MinusEquals) \
  58. __TOKEN(Asterisk) \
  59. __TOKEN(AsteriskEquals) \
  60. __TOKEN(Slash) \
  61. __TOKEN(SlashEquals) \
  62. __TOKEN(Percent) \
  63. __TOKEN(PercentEquals) \
  64. __TOKEN(Caret) \
  65. __TOKEN(CaretEquals) \
  66. __TOKEN(ExclamationMark) \
  67. __TOKEN(ExclamationMarkEquals) \
  68. __TOKEN(Equals) \
  69. __TOKEN(EqualsEquals) \
  70. __TOKEN(And) \
  71. __TOKEN(AndAnd) \
  72. __TOKEN(AndEquals) \
  73. __TOKEN(Pipe) \
  74. __TOKEN(PipePipe) \
  75. __TOKEN(PipeEquals) \
  76. __TOKEN(Tilde) \
  77. __TOKEN(QuestionMark) \
  78. __TOKEN(Colon) \
  79. __TOKEN(ColonColon) \
  80. __TOKEN(ColonColonAsterisk) \
  81. __TOKEN(Semicolon) \
  82. __TOKEN(Dot) \
  83. __TOKEN(DotAsterisk) \
  84. __TOKEN(Arrow) \
  85. __TOKEN(ArrowAsterisk) \
  86. __TOKEN(DoubleQuotedString) \
  87. __TOKEN(SingleQuotedString) \
  88. __TOKEN(RawString) \
  89. __TOKEN(EscapeSequence) \
  90. __TOKEN(Comment) \
  91. __TOKEN(Integer) \
  92. __TOKEN(Float) \
  93. __TOKEN(Keyword) \
  94. __TOKEN(KnownType) \
  95. __TOKEN(Identifier) \
  96. __TOKEN(EOF_TOKEN)
  97. struct Position {
  98. size_t line { 0 };
  99. size_t column { 0 };
  100. bool operator<(const Position&) const;
  101. bool operator>(const Position&) const;
  102. bool operator==(const Position&) const;
  103. };
  104. struct Token {
  105. enum class Type {
  106. #define __TOKEN(x) x,
  107. FOR_EACH_TOKEN_TYPE
  108. #undef __TOKEN
  109. };
  110. Token(Type type, const Position& start, const Position& end, const StringView& text)
  111. : m_type(type)
  112. , m_start(start)
  113. , m_end(end)
  114. , m_text(text)
  115. {
  116. }
  117. static const char* type_to_string(Type t)
  118. {
  119. switch (t) {
  120. #define __TOKEN(x) \
  121. case Type::x: \
  122. return #x;
  123. FOR_EACH_TOKEN_TYPE
  124. #undef __TOKEN
  125. }
  126. VERIFY_NOT_REACHED();
  127. }
  128. const char* to_string() const
  129. {
  130. return type_to_string(m_type);
  131. }
  132. const Position& start() const { return m_start; }
  133. const Position& end() const { return m_end; }
  134. void set_start(const Position& other) { m_start = other; }
  135. void set_end(const Position& other) { m_end = other; }
  136. Type type() const { return m_type; }
  137. const StringView& text() const { return m_text; }
  138. private:
  139. Type m_type { Type::Unknown };
  140. Position m_start;
  141. Position m_end;
  142. StringView m_text;
  143. };
  144. }