Token.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
  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/String.h>
  29. #include <stdlib.h>
  30. namespace SQL {
  31. StringView Token::name(TokenType type)
  32. {
  33. switch (type) {
  34. #define __ENUMERATE_SQL_TOKEN(value, type, category) \
  35. case TokenType::type: \
  36. return #type;
  37. ENUMERATE_SQL_TOKENS
  38. #undef __ENUMERATE_SQL_TOKEN
  39. default:
  40. VERIFY_NOT_REACHED();
  41. }
  42. }
  43. TokenCategory Token::category(TokenType type)
  44. {
  45. switch (type) {
  46. #define __ENUMERATE_SQL_TOKEN(value, type, category) \
  47. case TokenType::type: \
  48. return TokenCategory::category;
  49. ENUMERATE_SQL_TOKENS
  50. #undef __ENUMERATE_SQL_TOKEN
  51. default:
  52. VERIFY_NOT_REACHED();
  53. }
  54. }
  55. double Token::double_value() const
  56. {
  57. VERIFY(type() == TokenType::NumericLiteral);
  58. String value(m_value);
  59. if (value[0] == '0' && value.length() >= 2) {
  60. if (value[1] == 'x' || value[1] == 'X')
  61. return static_cast<double>(strtoul(value.characters() + 2, nullptr, 16));
  62. }
  63. return strtod(value.characters(), nullptr);
  64. }
  65. }