Token.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. namespace JS {
  28. const char* Token::name(TokenType type)
  29. {
  30. switch (type) {
  31. case TokenType::Ampersand:
  32. return "Ampersand";
  33. case TokenType::AmpersandEquals:
  34. return "AmpersandEquals";
  35. case TokenType::Asterisk:
  36. return "Asterisk";
  37. case TokenType::AsteriskEquals:
  38. return "AsteriskEquals";
  39. case TokenType::BoolLiteral:
  40. return "BoolLiteral";
  41. case TokenType::BracketOpen:
  42. return "BracketOpen";
  43. case TokenType::BracketClose:
  44. return "BracketClose";
  45. case TokenType::Catch:
  46. return "Catch";
  47. case TokenType::Class:
  48. return "Class";
  49. case TokenType::Comma:
  50. return "Comma";
  51. case TokenType::Const:
  52. return "Const";
  53. case TokenType::CurlyClose:
  54. return "CurlyClose";
  55. case TokenType::CurlyOpen:
  56. return "CurlyOpen";
  57. case TokenType::Delete:
  58. return "Delete";
  59. case TokenType::Do:
  60. return "Do";
  61. case TokenType::DoubleAmpersand:
  62. return "DoubleAmpersand";
  63. case TokenType::DoublePipe:
  64. return "DoublePipe";
  65. case TokenType::Else:
  66. return "Else";
  67. case TokenType::Eof:
  68. return "Eof";
  69. case TokenType::Equals:
  70. return "Equals";
  71. case TokenType::EqualsEquals:
  72. return "EqualsEquals";
  73. case TokenType::ExclamationMark:
  74. return "ExclamationMark";
  75. case TokenType::ExclamationMarkEquals:
  76. return "ExclamationMarkEquals";
  77. case TokenType::Finally:
  78. return "Finally";
  79. case TokenType::Function:
  80. return "Function";
  81. case TokenType::GreaterThan:
  82. return "GreaterThan";
  83. case TokenType::Identifier:
  84. return "Identifier";
  85. case TokenType::If:
  86. return "If";
  87. case TokenType::Interface:
  88. return "Interface";
  89. case TokenType::Invalid:
  90. return "Invalid";
  91. case TokenType::LessThan:
  92. return "LessThan";
  93. case TokenType::Let:
  94. return "Let";
  95. case TokenType::Minus:
  96. return "Minus";
  97. case TokenType::MinusEquals:
  98. return "MinusEquals";
  99. case TokenType::MinusMinus:
  100. return "MinusMinus";
  101. case TokenType::New:
  102. return "New";
  103. case TokenType::NullLiteral:
  104. return "NullLiteral";
  105. case TokenType::NumericLiteral:
  106. return "NumericLiteral";
  107. case TokenType::ParenClose:
  108. return "ParenClose";
  109. case TokenType::ParenOpen:
  110. return "ParenOpen";
  111. case TokenType::Percent:
  112. return "Percent";
  113. case TokenType::PercentEquals:
  114. return "PercentEquals";
  115. case TokenType::Period:
  116. return "Period";
  117. case TokenType::Pipe:
  118. return "Pipe";
  119. case TokenType::PipeEquals:
  120. return "PipeEquals";
  121. case TokenType::Plus:
  122. return "Plus";
  123. case TokenType::PlusEquals:
  124. return "PlusEquals";
  125. case TokenType::PlusPlus:
  126. return "PlusPlus";
  127. case TokenType::QuestionMark:
  128. return "QuestionMark";
  129. case TokenType::RegexLiteral:
  130. return "RegexLiteral";
  131. case TokenType::Return:
  132. return "Return";
  133. case TokenType::Semicolon:
  134. return "Semicolon";
  135. case TokenType::ShiftLeft:
  136. return "ShiftLeft";
  137. case TokenType::ShiftRight:
  138. return "ShiftRight";
  139. case TokenType::Slash:
  140. return "Slash";
  141. case TokenType::SlashEquals:
  142. return "SlashEquals";
  143. case TokenType::StringLiteral:
  144. return "StringLiteral";
  145. case TokenType::Try:
  146. return "Try";
  147. case TokenType::Var:
  148. return "Var";
  149. case TokenType::While:
  150. return "While";
  151. default:
  152. return "<Unknown>";
  153. }
  154. }
  155. const char* Token::name() const
  156. {
  157. return name(m_type);
  158. }
  159. double Token::double_value() const
  160. {
  161. // FIXME: need to parse double instead of int
  162. bool ok;
  163. return m_value.to_int(ok);
  164. }
  165. String Token::string_value() const
  166. {
  167. // FIXME: unescape the string and remove quotes
  168. return m_value;
  169. }
  170. bool Token::bool_value() const
  171. {
  172. return m_value == "true";
  173. }
  174. }