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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
// Copyright (c) 2019 Egor Tensin <Egor.Tensin@gmail.com>
// This file is part of the "math-server" project.
// For details, see https://github.com/egor-tensin/math-server.
// Distributed under the MIT License.
#include "error.hpp"
#include "token_type.hpp"
#include <functional>
#include <map>
#include <ostream>
#include <stdexcept>
#include <string>
#include <unordered_map>
namespace math::server::lexer::token {
namespace {
using ToStringMap = std::unordered_map<Type, std::string>;
using FromStringMap = std::map<std::string, Type, std::greater<std::string>>;
class ToStringConverter {
public:
ToStringConverter() : m_map{to_string_map()} {
validate();
}
const ToStringMap& map() const { return m_map; }
private:
static const ToStringMap& to_string_map() {
static const ToStringMap map{
{Type::WHITESPACE, "whitespace"},
{Type::PLUS, "+"},
{Type::MINUS, "-"},
{Type::ASTERISK, "*"},
{Type::SLASH, "/"},
{Type::CARET, "^"},
{Type::LEFT_PAREN, "("},
{Type::RIGHT_PAREN, ")"},
{Type::NUMBER, "number"},
};
return map;
}
void validate() const {
check_for_duplicates();
}
void check_for_duplicates() const {
std::unordered_set<std::string> strings;
for (const auto& [type, str] : m_map) {
const auto [_, inserted] = strings.emplace(str);
if (!inserted) {
throw std::logic_error{"multiple tokens have the same string representation: " + str};
}
}
}
const ToStringMap& m_map;
};
const ToStringMap& to_string_map() {
static const ToStringConverter converter;
return converter.map();
}
class FromStringConverter {
public:
FromStringConverter(const ToStringMap& to_string)
: m_map{build_map(to_string)} {
}
const FromStringMap& map() const { return m_map; }
private:
static FromStringMap build_map(const ToStringMap& to_string) {
FromStringMap from_string;
for (const auto& [type, str] : to_string) {
const auto [_, inserted] = from_string.emplace(str, type);
if (!inserted) {
throw std::logic_error{"multiple tokens have the same string representation: " + str};
}
}
return from_string;
}
FromStringMap m_map;
};
const FromStringMap& from_string_map() {
static const FromStringConverter converter{to_string_map()};
return converter.map();
}
class ConstTokens {
public:
ConstTokens() {
const auto& map = to_string_map();
for (const auto& [type, _] : map) {
if (is_const_token(type)) {
m_set.emplace(type);
}
}
}
const TypeSet& set() const { return m_set; }
private:
TypeSet m_set;
};
}
TypeInt type_to_int(Type type) {
return static_cast<TypeInt>(type);
}
std::string type_to_int_string(Type type) {
return std::to_string(type_to_int(type));
}
bool is_const_token(Type type) {
switch (type) {
case Type::WHITESPACE:
case Type::NUMBER:
return false;
default:
return true;
}
}
const TypeSet& const_tokens() {
static const ConstTokens tokens;
return tokens.set();
}
bool token_has_value(Type type) {
switch (type) {
case Type::NUMBER:
return true;
default:
return false;
}
}
std::string type_to_string(Type type) {
const auto& map = to_string_map();
const auto it = map.find(type);
if (it == map.cend()) {
throw LexerError{"type_to_string: unsupported token type: " + type_to_int_string(type)};
}
return it->second;
}
Type type_from_string(const std::string& src) {
const auto& map = from_string_map();
const auto it = map.find(src);
if (it == map.cend()) {
throw LexerError{"type_from_string: unsupported token: " + std::string{src}};
}
return it->second;
}
std::ostream& operator<<(std::ostream& os, const Type& type) {
os << type_to_int(type);
return os;
}
}
|