Repository navigation
Expand file tree
/
Copy pathtinyrulechecker.cc
More file actions
889 lines (782 loc) · 25.8 KB
/
Copy pathtinyrulechecker.cc
File metadata and controls
889 lines (782 loc) · 25.8 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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
// -----------------------------------------------------------------------------
// Copyright (C) 2024 Pau Sanchez
// MIT License
// -----------------------------------------------------------------------------
#include <stdio.h>
#include <math.h>
#include <string>
#include <cstring>
#include <stdint.h>
#include <charconv>
#include "tinyrulechecker.h"
// -----------------------------------------------------------------------------
// TinyRuleChecker constructor
// -----------------------------------------------------------------------------
TinyRuleChecker::TinyRuleChecker(bool defaultMethods) {
clearVars();
clearMethods();
if (defaultMethods) {
initMethods();
}
}
// -----------------------------------------------------------------------------
// TinyRuleChecker destructor
// -----------------------------------------------------------------------------
TinyRuleChecker::~TinyRuleChecker() {
// free variables and methods
clearVars();
}
// -----------------------------------------------------------------------------
// Clear internal variables
// -----------------------------------------------------------------------------
void TinyRuleChecker::clearVars() {
_variables.clear();
}
// -----------------------------------------------------------------------------
// setVarInt
// -----------------------------------------------------------------------------
void TinyRuleChecker::setVarInt(const char *name, int32_t value) {
VarValue v;
v.type = V_TYPE_INT;
v.intval = value;
_variables.set(name, v);
}
// -----------------------------------------------------------------------------
// setVarFloat
// -----------------------------------------------------------------------------
void TinyRuleChecker::setVarFloat(const char *name, float value) {
VarValue v;
v.type = V_TYPE_FLOAT;
v.floatval = value;
_variables.set(name, v);
}
// -----------------------------------------------------------------------------
// setVarString
// -----------------------------------------------------------------------------
void TinyRuleChecker::setVarString(const char *name, const char *value) {
VarValue v;
v.type = V_TYPE_STRING;
v.strval = value;
_variables.set(name, v);
}
// -----------------------------------------------------------------------------
// Clear internal methods
// -----------------------------------------------------------------------------
void TinyRuleChecker::clearMethods() {
_methods.clear();
}
// -----------------------------------------------------------------------------
// setMethod
// -----------------------------------------------------------------------------
void TinyRuleChecker::setMethod(const char *name, TinyRuleChecker::MethodOperator method) {
_methods.set(name, method);
}
// -----------------------------------------------------------------------------
// initMethods
//
// Initialize all standard methods
// -----------------------------------------------------------------------------
void TinyRuleChecker::initMethods() {
#define ENSURE_SAME_TYPE(v1, v2) \
if (v1.type != v2.type) { \
eval.error = "type mismatch: type " + std::string(1, v1.type) + " vs " + std::string(1, v2.type); \
return false; \
} \
setMethod("eq", [](const VarValue &v1, const VarValue &v2, EvalResult &eval) {
ENSURE_SAME_TYPE(v1, v2);
switch (v1.type) {
case V_TYPE_INT:
eval.result = v1.intval == v2.intval;
break;
case V_TYPE_FLOAT:
eval.result = v1.floatval == v2.floatval;
break;
case V_TYPE_STRING:
eval.result = v1.strval == v2.strval;
break;
default:
eval.error = "unsupported operation 'eq' with type '" + std::string(1, v1.type) + "'";
return false;
}
return true;
});
setMethod("neq", [](const VarValue &v1, const VarValue &v2, EvalResult &eval) {
ENSURE_SAME_TYPE(v1, v2);
switch (v1.type) {
case V_TYPE_INT:
eval.result = v1.intval != v2.intval;
break;
case V_TYPE_FLOAT:
eval.result = v1.floatval != v2.floatval;
break;
case V_TYPE_STRING:
eval.result = v1.strval != v2.strval;
break;
default:
eval.error = "unsupported operation 'neq' with type '" + std::string(1, v1.type) + "'";
return false;
}
return true;
});
setMethod("gt", [](const VarValue &v1, const VarValue &v2, EvalResult &eval) {
ENSURE_SAME_TYPE(v1, v2);
switch (v1.type) {
case V_TYPE_INT:
eval.result = v1.intval > v2.intval;
break;
case V_TYPE_FLOAT:
eval.result = v1.floatval > v2.floatval;
break;
case V_TYPE_STRING:
eval.result = v1.strval > v2.strval;
break;
default:
eval.error = "unsupported operation 'gt' with type '" + std::string(1, v1.type) + "'";
return false;
}
return true;
});
setMethod("gte", [](const VarValue &v1, const VarValue &v2, EvalResult &eval) {
ENSURE_SAME_TYPE(v1, v2);
switch(v1.type) {
case V_TYPE_INT:
eval.result = v1.intval >= v2.intval;
break;
case V_TYPE_FLOAT:
eval.result = v1.floatval >= v2.floatval;
break;
case V_TYPE_STRING:
eval.result = v1.strval >= v2.strval;
break;
default:
eval.error = "unsupported operation 'gte' with type '" + std::string(1, v1.type) + "'";
return false;
}
return true;
});
setMethod("lt", [](const VarValue &v1, const VarValue &v2, EvalResult &eval) {
ENSURE_SAME_TYPE(v1, v2);
switch (v1.type) {
case V_TYPE_INT:
eval.result = v1.intval < v2.intval;
break;
case V_TYPE_FLOAT:
eval.result = v1.floatval < v2.floatval;
break;
case V_TYPE_STRING:
eval.result = v1.strval < v2.strval;
break;
default:
eval.error = "unsupported operation 'lt' with type '" + std::string(1, v1.type) + "'";
return false;
}
return true;
});
setMethod("lte", [](const VarValue &v1, const VarValue &v2, EvalResult &eval) {
ENSURE_SAME_TYPE(v1, v2);
switch (v1.type) {
case V_TYPE_INT:
eval.result = v1.intval <= v2.intval;
break;
case V_TYPE_FLOAT:
eval.result = v1.floatval <= v2.floatval;
break;
case V_TYPE_STRING:
eval.result = v1.strval <= v2.strval;
break;
default:
eval.error = "unsupported operation 'lte' with type '" + std::string(1, v1.type) + "'";
return false;
}
return true;
});
setMethod("contains", [](const VarValue &v1, const VarValue &v2, EvalResult &eval) {
if (v1.type == V_TYPE_STRING) {
eval.result = v1.strval.find(v2.strval) != std::string::npos;
}
else {
eval.error = "unsupported operation 'contains' with type '" + std::string(1, v1.type) + "'";
return false;
}
return true;
});
setMethod("in", [](const VarValue &v1, const VarValue &v2, EvalResult &eval) {
if (v2.type == V_TYPE_STRING) {
eval.result = v2.strval.find(v1.strval) != std::string::npos;
}
else if (v2.type == V_TYPE_ARRAY) {
for (const VarValue &v : v2.array) {
if (v1.type == v.type) {
switch (v1.type) {
case V_TYPE_INT:
if (v1.intval == v.intval) {
eval.result = true;
return true;
}
break;
case V_TYPE_FLOAT:
if (v1.floatval == v.floatval) {
eval.result = true;
return true;
}
break;
case V_TYPE_STRING:
if (v1.strval == v.strval) {
eval.result = true;
return true;
}
break;
default:
break;
}
}
}
eval.result = false;
}
else {
eval.error = "unsupported operation 'in' with type '" + std::string(1, v2.type) + "'";
return false;
}
return true;
});
}
// -----------------------------------------------------------------------------
// eval
//
// Returns TRUE if the expression is valid and the result is stored in the
// 'result' parameter. Returns FALSE otherwise.
// -----------------------------------------------------------------------------
TinyRuleChecker::EvalResult
TinyRuleChecker::eval(const char *expr) {
ParseState ps { expr };
EvalResult er;
// no matter if error or not, we are assigning both vars
_parseExpr(ps);
// we should have consumed everything, otherwise there's an error
if (ps.error.empty() && _peekToken(ps.next, ps.token)) {
er.error = "unexpected token \'" + std::string(ps.token.value) + "\'";
return er;
}
er.result = ps.result;
er.error = ps.error;
return er;
}
// -----------------------------------------------------------------------------
// _parseExpr
//
// parse an expression and returns the result in the 'result' parameter.
//
// expr -> '(' expr ')'
// -> statement
// -> statement boolop expr
// -----------------------------------------------------------------------------
bool TinyRuleChecker::_parseExpr(ParseState &ps) {
const char *peekNext = _nextToken(ps.next, ps.token);
if (peekNext == NULL) {
ps.error = "expecting expression";
return false;
}
if (ps.token.type == TK_LPAR) {
// consume LPAR
ps.next = peekNext;
// then expression
if (!_parseExpr(ps)) {
// preserve error by parseExpr
return false;
}
// then RPAR
ps.next = _nextToken(ps.next, ps.token);
if (ps.token.type != TK_RPAR) {
ps.error = "expecting ')'";
return false;
}
}
else if (!_parseStatement(ps)) {
// preserve error by parseStatement
return false;
}
// let's see what's ahead (we might need to process some extra things or
// we might just want to return to higher level, e.g still ")..." to be
// processed)
peekNext = _nextToken(ps.next, ps.token);
switch(ps.token.type) {
case TK_AND:
{
// consume AND
ps.next = peekNext;
bool result = ps.result;
if (!_parseExpr(ps))
return false;
ps.result &= result;
return true;
}
break;
case TK_OR:
{
// consume OR
ps.next = peekNext;
bool result = ps.result;
if (!_parseExpr(ps))
return false;
ps.result |= result;
}
return true;
default:
// nothing else to manage on purpose
break;
}
// let's allow high-level function to continue processing if needed
return true;
}
// -----------------------------------------------------------------------------
// _parseStatement
//
// parse a statement and returns the result in the 'result' parameter.
//
// statement -> id '.' id '(' value ')'
// -> 'not' statement
// -----------------------------------------------------------------------------
bool
TinyRuleChecker::_parseStatement(ParseState &ps) {
ps.next = _nextToken(ps.next, ps.token);
if (ps.next == NULL) {
ps.error = "expecting statement";
return false;
}
// optional 'not' operator
if (ps.token.type == TK_NOT) {
if (!_parseStatement(ps))
return false;
ps.result = !ps.result;
return true;
}
// expecting an identifier
if (ps.token.type != TK_ID) {
ps.error = "expecting identifier";
return false;
}
std::string_view id = ps.token.value;
// then expecting a dot
ps.next = _nextToken(ps.next, ps.token);
if (ps.token.type != TK_DOT) {
ps.error = "expecting '.'";
return false;
}
// then another identifier
ps.next = _nextToken(ps.next, ps.token);
if (ps.token.type != TK_ID) {
ps.error = "expecting identifier";
return false;
}
std::string_view method = ps.token.value;
// then a '('
ps.next = _nextToken(ps.next, ps.token);
if (ps.token.type != TK_LPAR) {
ps.error = "expecting '('";
return false;
}
// then should parse a value
VarValue value;
if (!_parseValue(ps, value)) {
// preserve error by parseValue
return false;
}
// then a ')'
ps.next = _nextToken(ps.next, ps.token);
if (ps.token.type != TK_RPAR) {
ps.error = "expecting ')'";
return false;
}
// evaluate the statement inline
const VarValue *pVar = _variables.get(id);
if (pVar == NULL) {
ps.error = "variable '" + std::string(id) + "' not found";
return false;
}
return _evalStatement(ps, *pVar, method, value);
}
// -----------------------------------------------------------------------------
// _parseValue
// -----------------------------------------------------------------------------
bool
TinyRuleChecker::_parseValue(ParseState &ps, VarValue &v) {
ps.next = _nextToken(ps.next, ps.token);
switch (ps.token.type) {
case TK_INT:
// we know is a valid int value
v.type = V_TYPE_INT;
v.intval = ps.token.intval;
return true;
case TK_FLOAT:
v.type = V_TYPE_FLOAT;
v.floatval = ps.token.floatval;
return true;
case TK_RAW_STRING:
case TK_RAW_STRING_NO_ESCAPE:
{
v.type = V_TYPE_STRING;
v.strval = ps.token.value;
// no escape sequences, thus, we are done!
if (ps.token.type == TK_RAW_STRING_NO_ESCAPE)
return true;
// TK_RAW_STRING requires unescaping the string
size_t pos = 0;
for (size_t i = 0; i < v.strval.size(); i++) {
if (v.strval[i] == '\\') {
if (i + 1 < v.strval.size()) {
switch (v.strval[i+1]) {
case 'n': v.strval[i] = '\n'; break;
case 'r': v.strval[i] = '\r'; break;
case 't': v.strval[i] = '\t'; break;
case '0': v.strval[i] = '\0'; break;
case '\\': v.strval[i] = '\\'; break;
case '\'': v.strval[i] = '\''; break;
case '"': v.strval[i] = '"'; break;
default: break;
}
v.strval[pos++] = v.strval[i];
}
}
else {
v.strval[pos++] = v.strval[i];
}
}
v.strval.resize(pos);
}
return true;
case TK_UNTERMINATED_STRING:
ps.error = "unterminated string";
return false;
case TK_ID:
{
const VarValue *pVar = _variables.get(ps.token.value);
if (pVar == NULL) {
ps.error = "variable '" + std::string(ps.token.value) + "' not found";
return false;
}
v = *pVar;
}
return true;
case TK_LBRACE:
{
v.type = V_TYPE_ARRAY;
v.array.clear();
_peekToken(ps.next, ps.token);
while (ps.token.type != TK_RBRACE) {
VarValue vtmp;
if (!_parseValue(ps, vtmp)) {
// preserve error by parseValue
return false;
}
v.array.push_back(vtmp);
// then a ',' or end of array
ps.next = _nextToken(ps.next, ps.token);
if (ps.token.type == TK_RBRACE) {
break;
}
else if (ps.token.type != TK_COMMA) {
ps.error = "expecting ','";
return false;
}
}
// end of array found, we are done!
if (ps.token.type != TK_RBRACE) {
ps.error = "expecting ']'";
return false;
}
}
return true;
default:
ps.error = "expecting value, got " + _stringifyToken(ps.token);
}
return false;
}
// -----------------------------------------------------------------------------
// _evalStatement
// -----------------------------------------------------------------------------
bool TinyRuleChecker::_evalStatement(
ParseState &ps,
const VarValue &v1,
const std::string_view &method,
const VarValue &v2
) {
// NOTE: type compatibility is left to the method implementation
// if (v1.type != v2.type) {
// ps.error = "type mismatch: type " + std::string(1, v1.type) + " vs " + std::string(1, v2.type);
// return false;
// }
const MethodOperator *pMethod = _methods.get(method);
if (pMethod == NULL) {
ps.error = "unknown method '" + std::string(method) + "'";
return false;
}
EvalResult evalResult;
if (!(*pMethod)(v1, v2, evalResult)) {
ps.error = evalResult.error;
return false;
}
ps.result = evalResult.result;
return true;
}
// -----------------------------------------------------------------------------
// _peekToken
//
// peek next token without consuming it, returns TRUE if there is a token and
// FALSE otherwise
// -----------------------------------------------------------------------------
bool TinyRuleChecker::_peekToken(const char *expr, Token &t) {
return _nextToken(expr, t) != NULL;
}
// -----------------------------------------------------------------------------
// __generateLookupTable
//
// generate a lookup table for token types where the index is the character
// (only considering that the character is the first one of a token)
// -----------------------------------------------------------------------------
void TinyRuleChecker::__generateLookupTable() {
printf ("static const char _TOKEN_LOOKUP_TABLE[256] = {\n ");
for (int i = 0; i < 256; i++) {
if (i == 0) printf("'%c'", TK_EOF);
else if (isspace(i)) printf("'%c'", TK_SPACE);
else if (isalpha(i) || i == '_') printf("'%c'", TK_ID);
else if (i == '"' || i == '\'') printf("'%c'", TK_RAW_STRING);
else if (isdigit(i) || i == '-' || i == '+') printf("'%c'", TK_INT);
else if (isspace(i)) printf("'%c'", TK_UNKNOWN);
else if (i == '(') printf("'%c'", TK_LPAR);
else if (i == ')') printf("'%c'", TK_RPAR);
else if (i == '[') printf("'%c'", TK_LBRACE);
else if (i == ']') printf("'%c'", TK_RBRACE);
else if (i == '&') printf("'%c'", TK_AND);
else if (i == '|') printf("'%c'", TK_OR);
else if (i == '!') printf("'%c'", TK_NOT);
else if (i == '.') printf("'%c'", TK_DOT);
else if (i == ',') printf("'%c'", TK_COMMA);
else printf("'%c'", TK_UNKNOWN);
if (i < 255) printf(",");
if (i % 16 == 15) printf("\n ");
}
printf ("};\n");
// alphanumeric and '_' accepted as second character onwards
// for an identifier
printf ("static const char _TOKEN_LOOKUP_ID[256] = {\n ");
for (int i = 0; i < 256; i++) {
if (i == 0) printf("'%c'", TK_EOF);
else if (isalnum(i) || i == '_') printf("'%c'", TK_ID);
else if (isspace(i)) printf("'%c'", ' ');
else printf("'%c'", TK_UNKNOWN);
if (i < 255) printf(",");
if (i % 16 == 15) printf("\n ");
}
printf ("};\n");
}
// -----------------------------------------------------------------------------
// lookup table for token types where the index is the character
// NOTE: generated with __generateLookupTable()
// -----------------------------------------------------------------------------
static const char _TOKEN_LOOKUP_TABLE[256] = {
'e','u','u','u','u','u','u','u','u',' ',' ',' ',' ',' ','u','u',
'u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u',
' ','!','s','u','u','u','&','s','(',')','u','n',',','n','.','u',
'n','n','n','n','n','n','n','n','n','n','u','u','u','u','u','u',
'u','i','i','i','i','i','i','i','i','i','i','i','i','i','i','i',
'i','i','i','i','i','i','i','i','i','i','i','[','u',']','u','i',
'u','i','i','i','i','i','i','i','i','i','i','i','i','i','i','i',
'i','i','i','i','i','i','i','i','i','i','i','u','|','u','u','u',
'u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u',
'u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u',
'u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u',
'u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u',
'u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u',
'u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u',
'u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u',
'u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u'
};
// -----------------------------------------------------------------------------
// lookup table with characters accepted as identifier after first character
// (alphanum + '_')
// NOTE: generated with __generateLookupTable()
// -----------------------------------------------------------------------------
static const char _TOKEN_LOOKUP_ID[256] = {
'e','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u',
'u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u',
'u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u',
'i','i','i','i','i','i','i','i','i','i','u','u','u','u','u','u',
'u','i','i','i','i','i','i','i','i','i','i','i','i','i','i','i',
'i','i','i','i','i','i','i','i','i','i','i','u','u','u','u','i',
'u','i','i','i','i','i','i','i','i','i','i','i','i','i','i','i',
'i','i','i','i','i','i','i','i','i','i','i','u','u','u','u','u',
'u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u',
'u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u',
'u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u',
'u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u',
'u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u',
'u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u',
'u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u',
'u','u','u','u','u','u','u','u','u','u','u','u','u','u','u','u'
};
// -----------------------------------------------------------------------------
// _nextToken
//
// get next token evaluating given expression and returning a pointer to the
// next character after the token (or NULL if no more tokens)
//
// this function will eat all spaces first
// -----------------------------------------------------------------------------
const char *TinyRuleChecker::_nextToken(const char *expr, Token &t) {
t.type = TK_UNKNOWN;
// while (isspace(*expr)) {
// while (*expr == ' ' || *expr == '\n' || *expr == '\r' || *expr == '\t') {
while (_TOKEN_LOOKUP_TABLE[*expr] == TK_SPACE) {
expr++;
}
if (expr == NULL || *expr == '\0') {
t.type = TK_EOF;
t.value = std::string_view{};
return NULL;
}
const char *start_expr = expr;
t.type = (TokenType)_TOKEN_LOOKUP_TABLE[*expr];
switch(t.type) {
case TK_ID:
expr++; // letter or underscore
// while (isalnum(*expr) || *expr == '_') {
while(_TOKEN_LOOKUP_ID[*expr] == TK_ID) {
expr++;
}
t.value = std::string_view(start_expr, expr - start_expr);
return expr;
case TK_RAW_STRING:
{
bool escapeCharFound = false;
char quoteChar = *expr;
expr++;
start_expr = expr;
while (*expr && *expr != quoteChar) {
// skip escaped characters for now, will be processed later
escapeCharFound |= (*expr == '\\');
expr += (*expr == '\\');
expr++;
}
t.value = std::string_view(start_expr, expr - start_expr);
if (*expr == quoteChar) {
t.type = escapeCharFound ? TK_RAW_STRING : TK_RAW_STRING_NO_ESCAPE;
expr++;
}
else {
// EOF reached without closing quote
t.type = TK_UNTERMINATED_STRING;
}
}
break;
case TK_INT:
{
bool negative = *expr == '-';
t.intval = (*expr >= '0' && *expr <= '9') ? *expr - '0' : 0;
// skip first digit or sign
expr++;
//while(isdigit(*expr)) {
while (*expr >= '0' && *expr <= '9') {
t.intval = t.intval * 10 + (*expr - '0');
expr++;
}
if (negative) {
t.intval = -t.intval;
}
if (*expr == '.') {
float fraction = 0;
float divisor = 1.0;
t.type = TK_FLOAT;
expr++;
//while (isdigit(*expr)) {
while (*expr >= '0' && *expr <= '9') {
fraction = fraction * 10 + (*expr - '0');
divisor *= 10;
expr++;
}
t.floatval = (float)t.intval;
t.floatval += fraction / divisor;
}
t.value = std::string_view(start_expr, expr - start_expr);
}
break;
case TK_LPAR:
t.value = std::string_view{expr, 1};
expr++;
break;
case TK_RPAR:
t.value = std::string_view{expr, 1};
expr++;
break;
case TK_LBRACE:
t.value = std::string_view{expr, 1};
expr++;
break;
case TK_RBRACE:
t.value = std::string_view{expr, 1};
expr++;
break;
case TK_AND:
if (*(expr+1) == '&') {
t.value = std::string_view{expr, 2};
expr+=2;
}
else {
t.type = TK_UNKNOWN;
t.value = std::string_view{expr, 1};
return expr;
}
break;
case TK_OR:
if (*(expr+1) == '|') {
t.value = std::string_view{expr, 2};
expr+=2;
}
else {
t.type = TK_UNKNOWN;
t.value = std::string_view{expr, 1};
return expr;
}
break;
case TK_NOT:
t.value = std::string_view{expr, 1};
expr++;
break;
case TK_DOT:
t.value = std::string_view{expr, 1};
expr++;
break;
case TK_COMMA:
t.value = std::string_view{expr, 1};
expr++;
break;
case TK_EOF:
// can happen if expression ends with spaces
t.value = std::string_view{};
return NULL;
case TK_SPACE:
// spaces should not appear here
default:
t.type = TK_UNKNOWN;
t.value = std::string_view{expr, 1};
expr++;
break;
}
return expr;
}
// -----------------------------------------------------------------------------
// _stringifyToken
// -----------------------------------------------------------------------------
std::string TinyRuleChecker::_stringifyToken(const Token &t) {
switch (t.type) {
case TK_INT: return std::to_string(t.intval);
case TK_FLOAT: return std::to_string(t.floatval);
case TK_RAW_STRING:
case TK_RAW_STRING_NO_ESCAPE:
return "string '" + std::string(t.value) + "'";
case TK_UNTERMINATED_STRING:
return "unterminated string (" + std::string(t.value) + ")";
case TK_EOF: return "EOF";
default:
return "'" + std::string(1, t.type) + "'";
}
}