-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.h
244 lines (197 loc) · 6.59 KB
/
token.h
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/****************************************************************************
** Copyright (c) 2023, Adel Kara Slimane <[email protected]>
**
** This file is part of ZeCalculator.
**
** ZeCalculators is free software: you may copy, redistribute and/or modify it
** under the terms of the GNU Affero General Public License as published by the
** Free Software Foundation, either version 3 of the License, or (at your
** option) any later version.
**
** This file is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/
#pragma once
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <optional>
#include <ranges>
#include <string>
#include <string_view>
#include <variant>
#include <zecalculator/external/expected.h>
#include <zecalculator/utils/tuple.h>
#include <zecalculator/utils/utils.h>
namespace zc {
namespace parsing {
namespace tokens {
struct Text
{
static Text from_views(std::string_view substr, std::string_view full_str)
{
return Text{.substr = std::string(substr), .begin = utils::begin_index(substr, full_str)};
};
/// @brief name of the token, can different from what appears in the expressions
/// @example '+' is replaced with 'internal::plus' (a valid function name)
std::string substr = {};
///@brief begin position of 'substr' in the original string
size_t begin = 0;
bool operator == (const Text& other) const = default;
};
enum Type: size_t
{
UNKNOWN,
NUMBER,
VARIABLE,
FUNCTION,
OP_ASSIGN,
OP_ADD,
OP_SUBTRACT,
OP_MULTIPLY,
OP_DIVIDE,
OP_POWER,
OP_UNARY_PLUS,
OP_UNARY_MINUS,
OPENING_PARENTHESIS,
CLOSING_PARENTHESIS,
FUNCTION_CALL_START,
FUNCTION_CALL_END,
SEPARATOR,
END_OF_EXPRESSION,
};
struct Operator
{
enum Desc {BINARY_INFIX, UNARY_PREFIX};
char token;
uint8_t priority;
Type type;
Desc desc;
};
inline constexpr std::array operators = {
Operator{'=', 0, OP_ASSIGN, Operator::BINARY_INFIX},
Operator{',', 1, SEPARATOR, Operator::BINARY_INFIX},
Operator{';', 1, SEPARATOR, Operator::BINARY_INFIX},
Operator{'+', 2, OP_ADD, Operator::BINARY_INFIX},
Operator{'-', 2, OP_SUBTRACT, Operator::BINARY_INFIX},
Operator{'*', 3, OP_MULTIPLY, Operator::BINARY_INFIX},
Operator{'/', 3, OP_DIVIDE, Operator::BINARY_INFIX},
Operator{'-', 4, OP_UNARY_MINUS, Operator::UNARY_PREFIX},
Operator{'+', 4, OP_UNARY_PLUS, Operator::UNARY_PREFIX},
Operator{'^', 5, OP_POWER, Operator::BINARY_INFIX},
};
inline constexpr uint8_t max_priority = std::ranges::max(operators | std::views::transform(&Operator::priority));
consteval size_t operator_number(size_t priority)
{
size_t num = 0;
for (const auto& op: operators)
if (op.priority == priority)
num++;
return num;
}
template <uint8_t priority>
consteval auto get_operators()
{
std::array<Operator, operator_number(priority)> ops;
size_t i = 0;
for (const auto& op: operators)
if (op.priority == priority)
{
ops[i] = op;
i++;
}
// just a "constexpr" check
if (i != ops.size())
throw 0;
return ops;
}
inline constexpr std::optional<Operator> as_binary_infix_operator(const char ch)
{
for(const auto& op: operators)
if (op.desc == Operator::BINARY_INFIX and op.token == ch)
return op;
return {};
}
inline constexpr std::optional<Operator> as_unary_prefix_operator(const char ch)
{
for(const auto& op: operators)
if (op.desc == Operator::UNARY_PREFIX and op.token == ch)
return op;
return {};
}
}
struct Token: tokens::Text
{
Token(tokens::Type type, tokens::Text text)
: tokens::Text{std::move(text)}, type(type)
{}
Token(double value, tokens::Text text)
: tokens::Text{std::move(text)}, type(tokens::NUMBER), value(value)
{}
static Token OpeningParenthesis(std::string_view name, size_t start) {
return Token(tokens::OPENING_PARENTHESIS, Text{std::string(name), start});
}
static Token Function(std::string_view name, size_t start) {
return Token(tokens::FUNCTION, Text{std::string(name), start});
}
static Token FunctionCallStart(std::string_view name, size_t start) {
return Token(tokens::FUNCTION_CALL_START, Text{std::string(name), start});
}
static Token Variable(std::string_view name, size_t start) {
return Token(tokens::VARIABLE, Text{std::string(name), start});
}
static Token FunctionCallEnd(std::string_view name, size_t start) {
return Token(tokens::FUNCTION_CALL_END, Text{std::string(name), start});
}
static Token Number(double value, std::string_view name, size_t start) {
return Token(value, Text{std::string(name), start});
}
static Token Number(double value, tokens::Text token) {
return Token(value, std::move(token));
}
static Token Separator(std::string_view name, size_t start) {
return Token(tokens::SEPARATOR, Text{std::string(name), start});
}
static Token ClosingParenthesis(std::string_view name, size_t start) {
return Token(tokens::CLOSING_PARENTHESIS, Text{std::string(name), start});
}
static Token Assign(std::string_view name, size_t start) {
return Token(tokens::OP_ASSIGN, Text{std::string(name), start});
}
static Token Add(std::string_view name, size_t start) {
return Token(tokens::OP_ADD, Text{std::string(name), start});
}
static Token Subtract(std::string_view name, size_t start) {
return Token(tokens::OP_SUBTRACT, Text{std::string(name), start});
}
static Token Multiply(std::string_view name, size_t start) {
return Token(tokens::OP_MULTIPLY, Text{std::string(name), start});
}
static Token Divide(std::string_view name, size_t start) {
return Token(tokens::OP_DIVIDE, Text{std::string(name), start});
}
static Token UnaryMinus(std::string_view name, size_t start) {
return Token(tokens::OP_UNARY_MINUS, Text{std::string(name), start});
}
static Token UnaryPlus(std::string_view name, size_t start) {
return Token(tokens::OP_UNARY_MINUS, Text{std::string(name), start});
}
static Token Power(std::string_view name, size_t start) {
return Token(tokens::OP_POWER, Text{std::string(name), start});
}
tokens::Type type = tokens::UNKNOWN;
double value = std::nan("");
};
template <class... U>
inline tokens::Text text_token(const std::variant<U...>& token)
{
return std::visit([](const auto& tk) -> tokens::Text { return tk; }, token);
}
}
}