Token.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@gmx.de>
  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/String.h>
  28. #include <AK/StringView.h>
  29. namespace JS {
  30. enum class TokenType {
  31. Ampersand,
  32. AmpersandEquals,
  33. Asterisk,
  34. AsteriskAsteriskEquals,
  35. AsteriskEquals,
  36. Await,
  37. BoolLiteral,
  38. BracketClose,
  39. BracketOpen,
  40. Caret,
  41. Catch,
  42. Class,
  43. Comma,
  44. Const,
  45. CurlyClose,
  46. CurlyOpen,
  47. Delete,
  48. Do,
  49. DoubleAmpersand,
  50. DoubleAsterisk,
  51. DoublePipe,
  52. DoubleQuestionMark,
  53. Else,
  54. Eof,
  55. Equals,
  56. EqualsEquals,
  57. EqualsEqualsEquals,
  58. ExclamationMark,
  59. ExclamationMarkEquals,
  60. ExclamationMarkEqualsEquals,
  61. Finally,
  62. For,
  63. Function,
  64. GreaterThan,
  65. GreaterThanEquals,
  66. Identifier,
  67. If,
  68. In,
  69. Instanceof,
  70. Interface,
  71. Invalid,
  72. LessThan,
  73. LessThanEquals,
  74. Let,
  75. Minus,
  76. MinusEquals,
  77. MinusMinus,
  78. New,
  79. NullLiteral,
  80. NumericLiteral,
  81. ParenClose,
  82. ParenOpen,
  83. Percent,
  84. PercentEquals,
  85. Period,
  86. Pipe,
  87. PipeEquals,
  88. Plus,
  89. PlusEquals,
  90. PlusPlus,
  91. QuestionMark,
  92. QuestionMarkPeriod,
  93. RegexLiteral,
  94. Return,
  95. Semicolon,
  96. ShiftLeft,
  97. ShiftLeftEquals,
  98. ShiftRight,
  99. ShiftRightEquals,
  100. Slash,
  101. SlashEquals,
  102. StringLiteral,
  103. Tilde,
  104. Try,
  105. Typeof,
  106. UndefinedLiteral,
  107. UnsignedShiftRight,
  108. UnsignedShiftRightEquals,
  109. UnterminatedStringLiteral,
  110. Var,
  111. Void,
  112. While,
  113. Yield,
  114. };
  115. class Token {
  116. public:
  117. Token(TokenType type, StringView trivia, StringView value)
  118. : m_type(type)
  119. , m_trivia(trivia)
  120. , m_value(value)
  121. {
  122. }
  123. TokenType type() const { return m_type; }
  124. const char* name() const;
  125. static const char* name(TokenType);
  126. const StringView& trivia() const { return m_trivia; }
  127. const StringView& value() const { return m_value; }
  128. double double_value() const;
  129. String string_value() const;
  130. bool bool_value() const;
  131. private:
  132. TokenType m_type;
  133. StringView m_trivia;
  134. StringView m_value;
  135. };
  136. }
  137. namespace AK {
  138. template<>
  139. struct Traits<JS::TokenType> : public GenericTraits<JS::TokenType> {
  140. static constexpr bool is_trivial() { return true; }
  141. static unsigned hash(JS::TokenType t) { return int_hash((int)t); }
  142. };
  143. }