Token.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. namespace JS {
  30. const char* Token::name(TokenType type)
  31. {
  32. switch (type) {
  33. #define __ENUMERATE_JS_TOKEN(x) \
  34. case TokenType::x: \
  35. return #x;
  36. ENUMERATE_JS_TOKENS
  37. #undef __ENUMERATE_JS_TOKEN
  38. default:
  39. ASSERT_NOT_REACHED();
  40. return "<Unknown>";
  41. }
  42. }
  43. const char* Token::name() const
  44. {
  45. return name(m_type);
  46. }
  47. double Token::double_value() const
  48. {
  49. ASSERT(type() == TokenType::NumericLiteral);
  50. // FIXME: need to parse double instead of int
  51. bool ok;
  52. return m_value.to_int(ok);
  53. }
  54. String Token::string_value() const
  55. {
  56. ASSERT(type() == TokenType::StringLiteral);
  57. StringBuilder builder;
  58. for (size_t i = 1; i < m_value.length() - 1; ++i) {
  59. if (m_value[i] == '\\' && i + 1 < m_value.length() - 1) {
  60. i++;
  61. switch (m_value[i]) {
  62. case 'b':
  63. builder.append('\b');
  64. break;
  65. case 'f':
  66. builder.append('\f');
  67. break;
  68. case 'n':
  69. builder.append('\n');
  70. break;
  71. case 'r':
  72. builder.append('\r');
  73. break;
  74. case 't':
  75. builder.append('\t');
  76. break;
  77. case 'v':
  78. builder.append('\v');
  79. break;
  80. case '0':
  81. builder.append((char)0);
  82. break;
  83. case '\'':
  84. builder.append('\'');
  85. break;
  86. case '"':
  87. builder.append('"');
  88. break;
  89. case '\\':
  90. builder.append('\\');
  91. break;
  92. default:
  93. // FIXME: Also parse octal, hex and unicode sequences
  94. // should anything else generate a syntax error?
  95. builder.append(m_value[i]);
  96. }
  97. } else {
  98. builder.append(m_value[i]);
  99. }
  100. }
  101. return builder.to_string();
  102. }
  103. bool Token::bool_value() const
  104. {
  105. ASSERT(type() == TokenType::BoolLiteral);
  106. return m_value == "true";
  107. }
  108. }