Token.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2023, 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 GLSL {
  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(Comma) \
  31. __TOKEN(Plus) \
  32. __TOKEN(PlusPlus) \
  33. __TOKEN(PlusEquals) \
  34. __TOKEN(Minus) \
  35. __TOKEN(MinusMinus) \
  36. __TOKEN(MinusEquals) \
  37. __TOKEN(Asterisk) \
  38. __TOKEN(AsteriskEquals) \
  39. __TOKEN(Slash) \
  40. __TOKEN(SlashEquals) \
  41. __TOKEN(Percent) \
  42. __TOKEN(PercentEquals) \
  43. __TOKEN(Caret) \
  44. __TOKEN(CaretCaret) \
  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(Semicolon) \
  60. __TOKEN(Dot) \
  61. __TOKEN(DoubleQuotedString) \
  62. __TOKEN(SingleQuotedString) \
  63. __TOKEN(RawString) \
  64. __TOKEN(EscapeSequence) \
  65. __TOKEN(Integer) \
  66. __TOKEN(Float) \
  67. __TOKEN(Keyword) \
  68. __TOKEN(KnownType) \
  69. __TOKEN(Identifier) \
  70. __TOKEN(EOF_TOKEN)
  71. struct Position {
  72. size_t line { 0 };
  73. size_t column { 0 };
  74. bool operator<(Position const&) const;
  75. bool operator<=(Position const&) const;
  76. bool operator>(Position const&) const;
  77. bool operator==(Position const&) const;
  78. };
  79. struct Token {
  80. enum class Type {
  81. #define __TOKEN(x) x,
  82. FOR_EACH_TOKEN_TYPE
  83. #undef __TOKEN
  84. };
  85. Token(Type type, Position const& start, Position const& end, StringView text)
  86. : m_type(type)
  87. , m_start(start)
  88. , m_end(end)
  89. , m_text(text)
  90. {
  91. }
  92. static char const* type_to_string(Type t)
  93. {
  94. switch (t) {
  95. #define __TOKEN(x) \
  96. case Type::x: \
  97. return #x;
  98. FOR_EACH_TOKEN_TYPE
  99. #undef __TOKEN
  100. }
  101. VERIFY_NOT_REACHED();
  102. }
  103. ErrorOr<String> to_string() const;
  104. ErrorOr<String> type_as_string() const;
  105. Position const& start() const { return m_start; }
  106. Position const& end() const { return m_end; }
  107. void set_start(Position const& other) { m_start = other; }
  108. void set_end(Position const& other) { m_end = other; }
  109. Type type() const { return m_type; }
  110. StringView text() const { return m_text; }
  111. private:
  112. Type m_type { Type::Unknown };
  113. Position m_start;
  114. Position m_end;
  115. StringView m_text;
  116. };
  117. }
  118. #undef FOR_EACH_TOKEN_TYPE