2021-04-18 17:35:40 -04:00
|
|
|
/*
|
2022-01-31 13:07:22 -05:00
|
|
|
* Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
|
2021-04-18 17:35:40 -04:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-04-18 17:35:40 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Token.h"
|
|
|
|
|
#include <AK/Assertions.h>
|
2023-12-16 17:49:34 +03:30
|
|
|
#include <AK/ByteString.h>
|
2021-04-18 17:35:40 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
2021-06-21 10:57:44 -04:00
|
|
|
namespace SQL::AST {
|
2021-04-18 17:35:40 -04:00
|
|
|
|
|
|
|
|
StringView Token::name(TokenType type)
|
|
|
|
|
{
|
|
|
|
|
switch (type) {
|
|
|
|
|
#define __ENUMERATE_SQL_TOKEN(value, type, category) \
|
|
|
|
|
case TokenType::type: \
|
2022-07-11 17:32:29 +00:00
|
|
|
return #type##sv;
|
2021-04-18 17:35:40 -04:00
|
|
|
ENUMERATE_SQL_TOKENS
|
|
|
|
|
#undef __ENUMERATE_SQL_TOKEN
|
|
|
|
|
default:
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TokenCategory Token::category(TokenType type)
|
|
|
|
|
{
|
|
|
|
|
switch (type) {
|
|
|
|
|
#define __ENUMERATE_SQL_TOKEN(value, type, category) \
|
|
|
|
|
case TokenType::type: \
|
|
|
|
|
return TokenCategory::category;
|
|
|
|
|
ENUMERATE_SQL_TOKENS
|
|
|
|
|
#undef __ENUMERATE_SQL_TOKEN
|
|
|
|
|
default:
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double Token::double_value() const
|
|
|
|
|
{
|
|
|
|
|
VERIFY(type() == TokenType::NumericLiteral);
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString value(m_value);
|
2021-04-18 17:35:40 -04:00
|
|
|
|
|
|
|
|
if (value[0] == '0' && value.length() >= 2) {
|
|
|
|
|
if (value[1] == 'x' || value[1] == 'X')
|
|
|
|
|
return static_cast<double>(strtoul(value.characters() + 2, nullptr, 16));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return strtod(value.characters(), nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|