This repository has been archived by the owner on Nov 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
CalcTokenizer.cpp
436 lines (383 loc) · 11.9 KB
/
CalcTokenizer.cpp
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#ifndef CALCULATOR_CALCTOKENIZER_CPP
#define CALCULATOR_CALCTOKENIZER_CPP
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include "CalcTokenizer.hpp"
#include "Tokenizer.cpp"
#include "CalcTkEnum.hpp"
#include "CalcASTException.hpp"
#include "CalcOperators.hpp"
template <class Num>
std::wstring CalcTokenizer<Num>::var_chars = L"abcdefghijklmnopqrstuvwxyz"
L"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
// greek alphabet
L"αβγδεζηθικλμνξοπρσςτυφχψω"
L"ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ"
L"_"; // lonely underscore
template <class Num>
std::wstring CalcTokenizer<Num>::digits = L"0.123456789";
template <class Num>
std::vector<CalcOperator<Num>> CalcTokenizer<Num>::binary_ops
= CalcOperators<Num>::binary_ops;
template <class Num>
std::vector<CalcUOperator<Num>> CalcTokenizer<Num>::unary_ops
= CalcOperators<Num>::unary_ops;
template <class Num>
std::wstring CalcTokenizer<Num>::parenthesis = L"()";
template <class Num>
template <class Op>
bool CalcTokenizer<Num>::ops_start_with(std::vector<Op> & ops,
std::wstring prefix) {
return std::find_if(ops.begin(), ops.end(), [&prefix](Op op) {
return op.op.length() >= prefix.length() &&
op.op.substr(0, prefix.length()) == prefix;
}) != ops.end();
}
template <class Num>
template <class Op>
bool CalcTokenizer<Num>::ops_start_with(std::vector<Op> & ops,
wchar_t prefix) {
return std::find_if(ops.begin(), ops.end(), [&prefix](Op op) {
return op.op[0] == prefix;
}) != ops.end();
}
/**
* Consumes all characters from string `str`. See
* [[CalcTokenizer<Num>::consume_push]] for a function that creates a new token.
*
* @param str The string to consume characters from.
*/
template <class Num>
void CalcTokenizer<Num>::consume(const std::wstring & str) {
while (STRING_CONTAINS(str, peek())) {
move();
}
}
/**
* Does all the main work, honestly. Uses the various [[Tokenizer]]
* functions to chop up a string into little itty bitty bits and label them
* based on an extremely strict set of rules.
*/
template <class Num>
void CalcTokenizer<Num>::_tokenize() {
while (true) {
/**
* Remove whitespace before getting the token
*/
chomp_whitespace();
wchar_t peeked = peek();
if (peeked == -1) {
/**
* If we are at the end of the input, exit
*/
break;
} else if (STRING_CONTAINS(digits + var_chars, peeked)) {
bool is_var = STRING_CONTAINS(var_chars, peeked);
/**
* implied multiplication, e.g. 5x
*/
if (is_var && after_value()) {
push_implied_token(TK_OPERATOR, L"*");
}
/**
* maybe check this _before_ we push a value to the stack
*/
bool throw_err = after_value();
start_token();
consume(is_var ? var_chars + digits : digits);
push_token(is_var ? TK_VARIABLE : TK_NUMBER);
/**
* Throw an error if the last token was a value
*/
if (throw_err) {
throw CalcASTException(tokens.back(),
L"Two values with no operation in"
L" between");
}
/**
* There's some stuff going on here.
*
* First of all, the peeked character has to be at the start of
* an operator. Once that happens, one of the following
* conditions must be true:
*
* - The last token must not be an opening parenthesis (that
* would imply a unary operator)
* - This operator cannot possibly be a unary operator
* - There is a space after this operator
*
* The next case handles unary operators where these conditions
* are not fulfilled.
*/
} else if (ops_start_with(binary_ops, peeked) && (
!ops_start_with(unary_ops, peeked) || (
tokens.empty() &&
!immediately_before_value()
) || !after_operator()
)) {
/**
* To be unary:
* - this has to be an actual unary operator
* - if it's not after a value, it must be after a unary
* operator
*/
bool unary = ops_start_with(unary_ops, peeked) &&
(after_uop() || after_operator());
/**
* Throw error if this is not unary but is after an operator
*
* @formatter:off
*/
bool throw_err = !unary && after_operator();
// @formatter:on
start_token();
/**
* we know this is valid, we checked
*/
move();
/**
* Consume all characters of the operator
*/
if (unary) {
while (ops_start_with(unary_ops, get_token_data() +
peek())) {
move();
}
} else {
while (ops_start_with(binary_ops, get_token_data() +
peek())) {
move();
}
}
push_token(unary ? TK_UOPERATOR : TK_OPERATOR);
/**
* This only happens if there are 2 operators in a row
*/
if (throw_err) {
throw CalcASTException(tokens.back(),
L"Too many operators");
}
} else if (ops_start_with(unary_ops, peeked)) {
/**
* Anything that didn't pass as a binary operator but is in
* unary_ops should be counted as a unary operator, of course
*
* Not all unary operators are also binary operators
*/
start_token();
/**
* Consume the first character, we know it's valid, we checked
*/
move();
/**
* Consume all extra characters of the unary operator (for
* operators that span more than one character) until consuming
* any more characters would make it invalid
*/
while (ops_start_with(unary_ops, get_token_data() + peek())) {
move();
}
bool after = uassociativity(get_token_data())
== UASSOCIATE_AFT;
/**
* Throw an error if this is a trailing token after an operator,
* e.g. +! (factorial of addition operator)
*
* Also throws an error if we're after a leading unary operator,
* for example 2 + -! (2 plus negative factorial)
*
* Does not throw an error if this is the first token and it's
* a trailing unary operator, since implicit insertion would get
* it there. If not, AST will get it anyway
*/
bool throw_err = after ? !tokens.empty() && !after_value()
: !after_operator();
push_token(TK_UOPERATOR);
if (throw_err) {
throw CalcASTException(tokens.back(),
after ? L"Missing operand for unary"
L" operator"
: L"Too many operators");
}
} else if (STRING_CONTAINS(parenthesis, peeked)) {
bool opening_paren = peeked == parenthesis[0];
Token matching_paren =
get_matching_token(TK_OPAREN, TK_CPAREN, 1);
Token matching_func_paren =
get_matching_token(TK_FOPAREN, TK_FCPAREN, 1);
/**
* to be a function parenthesis, this needs to either:
* - be a closing parenthesis with a matching function opening
* parenthesis
* or:
* - be an opening parenthesis coming immediately after a
* variable name (in which case that variable name will be
* upgraded to a function name)
*/
bool function_paren =
(
opening_paren && !tokens.empty() &&
tokens.back().type == TK_VARIABLE &&
STRING_CONTAINS(var_chars, peek(-1))
) ||
(
!opening_paren &&
matching_func_paren.type == TK_FOPAREN &&
matching_func_paren.type != TK_UNKNOWN &&
(
matching_paren.type == TK_UNKNOWN ||
matching_func_paren.pos > matching_paren.pos
)
);
if (opening_paren) {
if (!function_paren && after_value()) {
/**
* imply multiplication since 5(2) or something like that
*
* 5(2)
* (2 + 3)(2 + 3)
*
* etc...
*/
start_token();
move();
push_token(TK_OPERATOR);
tokens.back().data = L"*";
move(-1);
} else if (function_paren) {
tokens.back().type = TK_FUNCTION;
}
}
start_token();
std::wstring paren = get();
/**
* if this is an opening parenthesis, push TK_OPAREN
*
* If it's not, it can only be one other type of parenthesis:
* TK_CPAREN
*/
push_token(function_paren
? (opening_paren ? TK_FOPAREN : TK_FCPAREN)
: (opening_paren ? TK_OPAREN : TK_CPAREN));
} else if (peeked == ',' &&
get_matching_token(TK_FOPAREN, TK_FCPAREN, 1).type ==
TK_FOPAREN) {
start_token();
move();
push_token(TK_COMMA);
} else {
/**
* just grab the next character and push it, I guess?
*/
start_token();
get();
push_token(TK_UNKNOWN);
/**
* throw an exception accordingly, the : thing can be confusing,
* I guess?
*
* There's unfortunately a difference between:
* one; :help
* one;:help
*
* as well as
* :help
* :help
*/
if (peeked == ':') {
throw CalcASTException(tokens.back(),
L"Commands must start at the"
L" beginning of a line");
} else {
throw CalcASTException(tokens.back(),
L"Unknown character");
}
}
}
}
template <class Num>
CalcTokenizer<Num>::CalcTokenizer(
const std::wstring & str)
: Tokenizer(str) {}
template <class Num>
bool CalcTokenizer<Num>::after_value() {
if (tokens.empty()) {
return false;
}
Token & tk = tokens.back();
return (tk.type & (TK_VARIABLE | TK_NUMBER | TK_CPAREN)) != 0 ||
(
tk.type == TK_UOPERATOR &&
uassociativity(tk.data) == UASSOCIATE_AFT
);
}
template <class Num>
bool CalcTokenizer<Num>::after_operator() {
if (tokens.empty()) {
return false;
}
return !after_value() && (
tokens.back().type &
(TK_OPERATOR | TK_OPAREN | TK_FOPAREN)
) > 0;
}
template <class Num>
bool CalcTokenizer<Num>::after_uop() {
if (tokens.empty()) {
/**
* might as well return true if we're immediately before a number
* and the token stream is empty, the user clearly intends to imply
* a unary operator
*
* but if that's not the case, don't return true, if there's a space
* there's a likely chance the user wants an implied operation
* instead
*/
return immediately_before_value();
}
Token & tk = tokens.back();
return tk.type == TK_UOPERATOR &&
uassociativity(tk.data) == UASSOCIATE_BEF;
}
template <class Num>
void CalcTokenizer<Num>::push_implied_token(TokenType type,
std::wstring op) {
start_token();
move();
push_token(type);
move(-1);
tokens.back().data = op;
}
template <class Num>
UnaryAssociativity
CalcTokenizer<Num>::uassociativity(std::wstring op) {
return CalcOperators<Num>::unary_op_lookup.at(op).associativity;
}
template <class Num>
bool CalcTokenizer<Num>::immediately_before_value() {
return STRING_CONTAINS(digits + var_chars + parenthesis[0],
peek(1));
}
template <class Num>
Token CalcTokenizer<Num>::get_matching_token(TokenType opening,
TokenType closing,
int scope) {
if (tokens.empty()) {
return Token();
}
for (auto begin = tokens.begin(), current = --tokens.end();
current != begin; current--) {
if (current->type == opening) {
scope--;
} else if (current->type == closing) {
scope++;
}
if (scope == 0) {
return * current;
}
}
return Token();
}
#endif //CALCULATOR_CALCTOKENIZER_CPP