Token.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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::TemplateLiteralString);
  72. auto is_template = type() == TokenType::TemplateLiteralString;
  73. auto offset = type() == TokenType::TemplateLiteralString ? 0 : 1;
  74. StringBuilder builder;
  75. for (size_t i = offset; i < m_value.length() - offset; ++i) {
  76. if (m_value[i] == '\\' && i + 1 < m_value.length() - offset) {
  77. i++;
  78. switch (m_value[i]) {
  79. case 'b':
  80. builder.append('\b');
  81. break;
  82. case 'f':
  83. builder.append('\f');
  84. break;
  85. case 'n':
  86. builder.append('\n');
  87. break;
  88. case 'r':
  89. builder.append('\r');
  90. break;
  91. case 't':
  92. builder.append('\t');
  93. break;
  94. case 'v':
  95. builder.append('\v');
  96. break;
  97. case '0':
  98. builder.append((char)0);
  99. break;
  100. case '\'':
  101. builder.append('\'');
  102. break;
  103. case '"':
  104. builder.append('"');
  105. break;
  106. case '\\':
  107. builder.append('\\');
  108. break;
  109. default:
  110. if (is_template && (m_value[i] == '$' || m_value[i] == '`')) {
  111. builder.append(m_value[i]);
  112. } else {
  113. // FIXME: Also parse octal, hex and unicode sequences
  114. // should anything else generate a syntax error?
  115. builder.append(m_value[i]);
  116. }
  117. }
  118. } else {
  119. builder.append(m_value[i]);
  120. }
  121. }
  122. return builder.to_string();
  123. }
  124. bool Token::bool_value() const
  125. {
  126. ASSERT(type() == TokenType::BoolLiteral);
  127. return m_value == "true";
  128. }
  129. bool Token::is_identifier_name() const
  130. {
  131. // IdentifierNames are Identifiers + ReservedWords
  132. // The standard defines this reversed: Identifiers are IdentifierNames except reserved words
  133. // https://www.ecma-international.org/ecma-262/5.1/#sec-7.6
  134. return m_type == TokenType::Identifier
  135. || m_type == TokenType::Await
  136. || m_type == TokenType::BoolLiteral
  137. || m_type == TokenType::Break
  138. || m_type == TokenType::Case
  139. || m_type == TokenType::Catch
  140. || m_type == TokenType::Class
  141. || m_type == TokenType::Const
  142. || m_type == TokenType::Continue
  143. || m_type == TokenType::Default
  144. || m_type == TokenType::Delete
  145. || m_type == TokenType::Do
  146. || m_type == TokenType::Else
  147. || m_type == TokenType::Finally
  148. || m_type == TokenType::For
  149. || m_type == TokenType::Function
  150. || m_type == TokenType::If
  151. || m_type == TokenType::In
  152. || m_type == TokenType::Instanceof
  153. || m_type == TokenType::Interface
  154. || m_type == TokenType::Let
  155. || m_type == TokenType::New
  156. || m_type == TokenType::NullLiteral
  157. || m_type == TokenType::Return
  158. || m_type == TokenType::Switch
  159. || m_type == TokenType::This
  160. || m_type == TokenType::Throw
  161. || m_type == TokenType::Try
  162. || m_type == TokenType::Typeof
  163. || m_type == TokenType::Var
  164. || m_type == TokenType::Void
  165. || m_type == TokenType::While
  166. || m_type == TokenType::Yield;
  167. }
  168. }