Token.h 3.6 KB

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