Token.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #include "Token.h"
  27. #include <AK/Assertions.h>
  28. #include <AK/StringBuilder.h>
  29. #include <ctype.h>
  30. namespace JS {
  31. const char* Token::name(TokenType type)
  32. {
  33. switch (type) {
  34. #define __ENUMERATE_JS_TOKEN(x) \
  35. case TokenType::x: \
  36. return #x;
  37. ENUMERATE_JS_TOKENS
  38. #undef __ENUMERATE_JS_TOKEN
  39. default:
  40. ASSERT_NOT_REACHED();
  41. return "<Unknown>";
  42. }
  43. }
  44. const char* Token::name() const
  45. {
  46. return name(m_type);
  47. }
  48. double Token::double_value() const
  49. {
  50. ASSERT(type() == TokenType::NumericLiteral);
  51. String value_string(m_value);
  52. if (value_string[0] == '0' && value_string.length() >= 2) {
  53. if (value_string[1] == 'x' || value_string[1] == 'X') {
  54. // hexadecimal
  55. return static_cast<double>(strtoul(value_string.characters() + 2, nullptr, 16));
  56. } else if (value_string[1] == 'o' || value_string[1] == 'O') {
  57. // octal
  58. return static_cast<double>(strtoul(value_string.characters() + 2, nullptr, 8));
  59. } else if (value_string[1] == 'b' || value_string[1] == 'B') {
  60. // binary
  61. return static_cast<double>(strtoul(value_string.characters() + 2, nullptr, 2));
  62. } else if (isdigit(value_string[1])) {
  63. // also octal, but syntax error in strict mode
  64. return static_cast<double>(strtoul(value_string.characters() + 1, nullptr, 8));
  65. }
  66. }
  67. return strtod(value_string.characters(), nullptr);
  68. }
  69. String Token::string_value() const
  70. {
  71. ASSERT(type() == TokenType::StringLiteral || type() == TokenType::TemplateLiteral);
  72. StringBuilder builder;
  73. for (size_t i = 1; i < m_value.length() - 1; ++i) {
  74. if (m_value[i] == '\\' && i + 1 < m_value.length() - 1) {
  75. i++;
  76. switch (m_value[i]) {
  77. case 'b':
  78. builder.append('\b');
  79. break;
  80. case 'f':
  81. builder.append('\f');
  82. break;
  83. case 'n':
  84. builder.append('\n');
  85. break;
  86. case 'r':
  87. builder.append('\r');
  88. break;
  89. case 't':
  90. builder.append('\t');
  91. break;
  92. case 'v':
  93. builder.append('\v');
  94. break;
  95. case '0':
  96. builder.append((char)0);
  97. break;
  98. case '\'':
  99. builder.append('\'');
  100. break;
  101. case '"':
  102. builder.append('"');
  103. break;
  104. case '`':
  105. builder.append('`');
  106. break;
  107. case '\\':
  108. builder.append('\\');
  109. break;
  110. default:
  111. // FIXME: Also parse octal, hex and unicode sequences
  112. // should anything else generate a syntax error?
  113. builder.append(m_value[i]);
  114. }
  115. } else {
  116. builder.append(m_value[i]);
  117. }
  118. }
  119. return builder.to_string();
  120. }
  121. bool Token::bool_value() const
  122. {
  123. ASSERT(type() == TokenType::BoolLiteral);
  124. return m_value == "true";
  125. }
  126. bool Token::is_identifier_name() const
  127. {
  128. // IdentifierNames are Identifiers + ReservedWords
  129. // The standard defines this reversed: Identifiers are IdentifierNames except reserved words
  130. // https://www.ecma-international.org/ecma-262/5.1/#sec-7.6
  131. return m_type == TokenType::Identifier
  132. || m_type == TokenType::Await
  133. || m_type == TokenType::BoolLiteral
  134. || m_type == TokenType::Break
  135. || m_type == TokenType::Case
  136. || m_type == TokenType::Catch
  137. || m_type == TokenType::Class
  138. || m_type == TokenType::Const
  139. || m_type == TokenType::Continue
  140. || m_type == TokenType::Default
  141. || m_type == TokenType::Delete
  142. || m_type == TokenType::Do
  143. || m_type == TokenType::Else
  144. || m_type == TokenType::Finally
  145. || m_type == TokenType::For
  146. || m_type == TokenType::Function
  147. || m_type == TokenType::If
  148. || m_type == TokenType::In
  149. || m_type == TokenType::Instanceof
  150. || m_type == TokenType::Interface
  151. || m_type == TokenType::Let
  152. || m_type == TokenType::New
  153. || m_type == TokenType::NullLiteral
  154. || m_type == TokenType::Return
  155. || m_type == TokenType::Switch
  156. || m_type == TokenType::This
  157. || m_type == TokenType::Throw
  158. || m_type == TokenType::Try
  159. || m_type == TokenType::Typeof
  160. || m_type == TokenType::Var
  161. || m_type == TokenType::Void
  162. || m_type == TokenType::While
  163. || m_type == TokenType::Yield;
  164. }
  165. }