summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibSQL/AST/Token.cpp
blob: 0e33bd5dd01d8abca1d687b34a3c0988756b2f4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
 * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "Token.h"
#include <AK/Assertions.h>
#include <AK/String.h>
#include <stdlib.h>

namespace SQL::AST {

StringView Token::name(TokenType type)
{
    switch (type) {
#define __ENUMERATE_SQL_TOKEN(value, type, category) \
    case TokenType::type:                            \
        return #type##sv;
        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);
    String value(m_value);

    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);
}

}