-
Notifications
You must be signed in to change notification settings - Fork 644
Expand file tree
/
Copy pathtoken.cc
More file actions
149 lines (125 loc) · 3.7 KB
/
Copy pathtoken.cc
File metadata and controls
149 lines (125 loc) · 3.7 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <thrift/compiler/parse/token.h>
#include <fmt/format.h>
#include <algorithm>
#include <cassert>
#include <stdexcept>
namespace apache::thrift::compiler {
namespace {
struct token_kind_info {
tok kind;
const char* name;
};
#define THRIFT_KEYWORD(kw) {tok::kw_##kw, #kw}
constexpr token_kind_info info[] = {
{tok::eof, "EOF"},
{tok::error, "error"},
{tok::bool_literal, "bool literal"},
{tok::int_literal, "int literal"},
{tok::float_literal, "float literal"},
{tok::string_literal, "string literal"},
{tok::identifier, "identifier"},
{tok::inline_doc, "inline doc"},
// Operators and punctuation:
{tok::comma, ","},
{tok::semi, ";"},
{tok::colon, ":"},
{tok::l_paren, "("},
{tok::r_paren, ")"},
{tok::l_brace, "{"},
{tok::r_brace, "}"},
{tok::l_square, "["},
{tok::r_square, "]"},
{tok::less, "<"},
{tok::greater, ">"},
{tok::equal, "="},
{tok::plus, "+"},
{tok::minus, "-"},
{tok::at, "@"},
// Basic types:
THRIFT_KEYWORD(void),
THRIFT_KEYWORD(bool),
THRIFT_KEYWORD(byte),
THRIFT_KEYWORD(i16),
THRIFT_KEYWORD(i32),
THRIFT_KEYWORD(i64),
THRIFT_KEYWORD(float),
THRIFT_KEYWORD(double),
THRIFT_KEYWORD(string),
THRIFT_KEYWORD(binary),
// Complex types:
THRIFT_KEYWORD(list),
THRIFT_KEYWORD(set),
THRIFT_KEYWORD(map),
THRIFT_KEYWORD(stream),
THRIFT_KEYWORD(sink),
// Function qualifiers:
THRIFT_KEYWORD(oneway),
THRIFT_KEYWORD(idempotent),
THRIFT_KEYWORD(readonly),
// Exception qualifiers:
THRIFT_KEYWORD(safe),
THRIFT_KEYWORD(transient),
THRIFT_KEYWORD(stateful),
THRIFT_KEYWORD(permanent),
THRIFT_KEYWORD(client),
THRIFT_KEYWORD(server),
// Header keywords:
THRIFT_KEYWORD(include),
THRIFT_KEYWORD(cpp_include),
THRIFT_KEYWORD(hs_include),
THRIFT_KEYWORD(package),
THRIFT_KEYWORD(namespace),
// Other keywords:
THRIFT_KEYWORD(const),
THRIFT_KEYWORD(enum),
THRIFT_KEYWORD(exception),
THRIFT_KEYWORD(extends),
THRIFT_KEYWORD(interaction),
THRIFT_KEYWORD(optional),
THRIFT_KEYWORD(performs),
THRIFT_KEYWORD(required),
THRIFT_KEYWORD(service),
THRIFT_KEYWORD(struct),
THRIFT_KEYWORD(throws),
THRIFT_KEYWORD(typedef),
THRIFT_KEYWORD(union),
THRIFT_KEYWORD(as)};
constexpr int num_token_kinds = sizeof(info) / sizeof(*info);
struct token_kind_names {
const char* data[num_token_kinds] = {};
constexpr token_kind_names() {
int max_kind = 0;
for (auto i : info) {
int int_kind = static_cast<int>(i.kind);
data[int_kind] = i.name;
max_kind = (std::max)(int_kind, max_kind);
}
assert(num_token_kinds == max_kind + 1);
}
};
constexpr token_kind_names names;
} // namespace
std::string_view to_string(token_kind kind) {
int int_kind = static_cast<int>(kind.value);
assert(int_kind < num_token_kinds);
return names.data[int_kind];
}
void token::throw_invalid_kind(const char* expected) {
throw std::runtime_error(fmt::format("token value is not {}", expected));
}
} // namespace apache::thrift::compiler