forked from ChaiScript/ChaiScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchaiscript_parser.hpp
More file actions
2738 lines (2254 loc) · 92 KB
/
Copy pathchaiscript_parser.hpp
File metadata and controls
2738 lines (2254 loc) · 92 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
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is distributed under the BSD License.
// See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// Copyright 2009-2018, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
#ifndef CHAISCRIPT_PARSER_HPP_
#define CHAISCRIPT_PARSER_HPP_
#include <exception>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include <vector>
#include <cctype>
#include <cstring>
#include "../dispatchkit/boxed_value.hpp"
#include "chaiscript_common.hpp"
#include "chaiscript_optimizer.hpp"
#include "chaiscript_tracer.hpp"
#include "../utility/hash.hpp"
#include "../utility/static_string.hpp"
#if defined(CHAISCRIPT_UTF16_UTF32)
#include <locale>
#include <codecvt>
#endif
#if defined(CHAISCRIPT_MSVC) && defined(max) && defined(min)
#define CHAISCRIPT_PUSHED_MIN_MAX
#pragma push_macro("max") // Why Microsoft? why? This is worse than bad
#undef max
#pragma push_macro("min")
#undef min
#endif
namespace chaiscript
{
/// \brief Classes and functions used during the parsing process.
namespace parser
{
/// \brief Classes and functions internal to the parsing process. Not supported for the end user.
namespace detail
{
enum Alphabet
{ symbol_alphabet = 0
, keyword_alphabet
, int_alphabet
, float_alphabet
, x_alphabet
, hex_alphabet
, b_alphabet
, bin_alphabet
, id_alphabet
, white_alphabet
, int_suffix_alphabet
, float_suffix_alphabet
, max_alphabet
, lengthof_alphabet = 256
};
// Generic for u16, u32 and wchar
template<typename string_type>
struct Char_Parser_Helper
{
// common for all implementations
static std::string u8str_from_ll(long long val)
{
using char_type = std::string::value_type;
char_type c[2];
c[1] = char_type(val);
c[0] = char_type(val >> 8);
if (c[0] == 0)
{
return std::string(1, c[1]); // size, character
}
return std::string(c, 2); // char buffer, size
}
static string_type str_from_ll(long long val)
{
using target_char_type = typename string_type::value_type;
#if defined (CHAISCRIPT_UTF16_UTF32)
// prepare converter
std::wstring_convert<std::codecvt_utf8<target_char_type>, target_char_type> converter;
// convert
return converter.from_bytes(u8str_from_ll(val));
#else
// no conversion available, just put value as character
return string_type(1, target_char_type(val)); // size, character
#endif
}
};
// Specialization for char AKA UTF-8
template<>
struct Char_Parser_Helper<std::string>
{
static std::string str_from_ll(long long val)
{
// little SFINAE trick to avoid base class
return Char_Parser_Helper<std::true_type>::u8str_from_ll(val);
}
};
}
template<typename Tracer, typename Optimizer, std::size_t Parse_Depth=512>
class ChaiScript_Parser final : public ChaiScript_Parser_Base {
void *get_tracer_ptr() noexcept override {
return &m_tracer;
}
std::size_t m_current_parse_depth = 0;
struct Depth_Counter
{
static const auto max_depth = Parse_Depth;
Depth_Counter(ChaiScript_Parser *t_parser) : parser(t_parser)
{
++parser->m_current_parse_depth;
if (parser->m_current_parse_depth > max_depth) {
throw exception::eval_error("Maximum parse depth exceeded",
File_Position(parser->m_position.line, parser->m_position.col), *(parser->m_filename));
}
}
~Depth_Counter() noexcept
{
--parser->m_current_parse_depth;
}
ChaiScript_Parser *parser;
};
template<typename Array2D, typename First, typename Second>
constexpr static void set_alphabet(Array2D &array, const First first, const Second second) noexcept
{
auto *first_ptr = &std::get<0>(array) + static_cast<std::size_t>(first);
auto *second_ptr = &std::get<0>(*first_ptr) + static_cast<std::size_t>(second);
*second_ptr = true;
}
constexpr static std::array<std::array<bool, detail::lengthof_alphabet>, detail::max_alphabet> build_alphabet() noexcept
{
std::array<std::array<bool, detail::lengthof_alphabet>, detail::max_alphabet> alphabet{};
set_alphabet(alphabet, detail::symbol_alphabet, '?');
set_alphabet(alphabet, detail::symbol_alphabet, '?');
set_alphabet(alphabet, detail::symbol_alphabet, '+');
set_alphabet(alphabet, detail::symbol_alphabet, '-');
set_alphabet(alphabet, detail::symbol_alphabet, '*');
set_alphabet(alphabet, detail::symbol_alphabet, '/');
set_alphabet(alphabet, detail::symbol_alphabet, '|');
set_alphabet(alphabet, detail::symbol_alphabet, '&');
set_alphabet(alphabet, detail::symbol_alphabet, '^');
set_alphabet(alphabet, detail::symbol_alphabet, '=');
set_alphabet(alphabet, detail::symbol_alphabet, '.');
set_alphabet(alphabet, detail::symbol_alphabet, '<');
set_alphabet(alphabet, detail::symbol_alphabet, '>');
for ( size_t c = 'a' ; c <= 'z' ; ++c ) { set_alphabet(alphabet, detail::keyword_alphabet, c); }
for ( size_t c = 'A' ; c <= 'Z' ; ++c ) { set_alphabet(alphabet, detail::keyword_alphabet, c); }
for ( size_t c = '0' ; c <= '9' ; ++c ) { set_alphabet(alphabet, detail::keyword_alphabet, c); }
set_alphabet(alphabet, detail::keyword_alphabet, '_');
for ( size_t c = '0' ; c <= '9' ; ++c ) { set_alphabet(alphabet, detail::int_alphabet, c); }
for ( size_t c = '0' ; c <= '9' ; ++c ) { set_alphabet(alphabet, detail::float_alphabet, c); }
set_alphabet(alphabet, detail::float_alphabet, '.');
for ( size_t c = '0' ; c <= '9' ; ++c ) { set_alphabet(alphabet, detail::hex_alphabet, c); }
for ( size_t c = 'a' ; c <= 'f' ; ++c ) { set_alphabet(alphabet, detail::hex_alphabet, c); }
for ( size_t c = 'A' ; c <= 'F' ; ++c ) { set_alphabet(alphabet, detail::hex_alphabet, c); }
set_alphabet(alphabet, detail::x_alphabet, 'x');
set_alphabet(alphabet, detail::x_alphabet, 'X');
for ( size_t c = '0' ; c <= '1' ; ++c ) { set_alphabet(alphabet, detail::bin_alphabet, c); }
set_alphabet(alphabet, detail::b_alphabet, 'b');
set_alphabet(alphabet, detail::b_alphabet, 'B');
for ( size_t c = 'a' ; c <= 'z' ; ++c ) { set_alphabet(alphabet, detail::id_alphabet, c); }
for ( size_t c = 'A' ; c <= 'Z' ; ++c ) { set_alphabet(alphabet, detail::id_alphabet, c); }
set_alphabet(alphabet, detail::id_alphabet, '_');
set_alphabet(alphabet, detail::white_alphabet, ' ');
set_alphabet(alphabet, detail::white_alphabet, '\t');
set_alphabet(alphabet, detail::int_suffix_alphabet, 'l');
set_alphabet(alphabet, detail::int_suffix_alphabet, 'L');
set_alphabet(alphabet, detail::int_suffix_alphabet, 'u');
set_alphabet(alphabet, detail::int_suffix_alphabet, 'U');
set_alphabet(alphabet, detail::float_suffix_alphabet, 'l');
set_alphabet(alphabet, detail::float_suffix_alphabet, 'L');
set_alphabet(alphabet, detail::float_suffix_alphabet, 'f');
set_alphabet(alphabet, detail::float_suffix_alphabet, 'F');
return alphabet;
}
struct Operator_Matches
{
using SS = utility::Static_String;
std::array<utility::Static_String, 1> m_0 {{SS("?")}};
std::array<utility::Static_String, 1> m_1 {{SS("||")}};
std::array<utility::Static_String, 1> m_2 {{SS("&&")}};
std::array<utility::Static_String, 1> m_3 {{SS("|")}};
std::array<utility::Static_String, 1> m_4 {{SS("^")}};
std::array<utility::Static_String, 1> m_5 {{SS("&")}};
std::array<utility::Static_String, 2> m_6 {{SS("=="), SS("!=")}};
std::array<utility::Static_String, 4> m_7 {{SS("<"), SS("<="), SS(">"), SS(">=")}};
std::array<utility::Static_String, 2> m_8 {{SS("<<"), SS(">>")}};
//We share precedence here but then separate them later
std::array<utility::Static_String, 2> m_9 {{SS("+"), SS("-")}};
std::array<utility::Static_String, 3> m_10 {{SS("*"), SS("/"), SS("%")}};
std::array<utility::Static_String, 6> m_11 {{SS("++"), SS("--"), SS("-"), SS("+"), SS("!"), SS("~")}};
bool is_match(const std::string_view &t_str) const noexcept {
constexpr std::array<std::size_t, 12> groups{{0,1,2,3,4,5,6,7,8,9,10,11}};
return std::any_of(groups.begin(), groups.end(), [&t_str, this](const std::size_t group){ return is_match(group, t_str); });
}
template<typename Predicate>
bool any_of(const std::size_t t_group, Predicate &&predicate) const
{
auto match = [&predicate](const auto &array) {
return std::any_of(array.begin(), array.end(), predicate);
};
switch (t_group) {
case 0: return match(m_0);
case 1: return match(m_1);
case 2: return match(m_2);
case 3: return match(m_3);
case 4: return match(m_4);
case 5: return match(m_5);
case 6: return match(m_6);
case 7: return match(m_7);
case 8: return match(m_8);
case 9: return match(m_9);
case 10: return match(m_10);
case 11: return match(m_11);
default: return false;
}
}
constexpr bool is_match(const std::size_t t_group, const std::string_view &t_str) const noexcept {
auto match = [&t_str](const auto &array) {
return std::any_of(array.begin(), array.end(), [&t_str](const auto &v){ return v == t_str; });
};
switch (t_group) {
case 0: return match(m_0);
case 1: return match(m_1);
case 2: return match(m_2);
case 3: return match(m_3);
case 4: return match(m_4);
case 5: return match(m_5);
case 6: return match(m_6);
case 7: return match(m_7);
case 8: return match(m_8);
case 9: return match(m_9);
case 10: return match(m_10);
case 11: return match(m_11);
default: return false;
}
}
};
constexpr static std::array<Operator_Precedence, 12> create_operators() noexcept {
std::array<Operator_Precedence, 12> operators = { {
Operator_Precedence::Ternary_Cond,
Operator_Precedence::Logical_Or,
Operator_Precedence::Logical_And,
Operator_Precedence::Bitwise_Or,
Operator_Precedence::Bitwise_Xor,
Operator_Precedence::Bitwise_And,
Operator_Precedence::Equality,
Operator_Precedence::Comparison,
Operator_Precedence::Shift,
Operator_Precedence::Addition,
Operator_Precedence::Multiplication,
Operator_Precedence::Prefix
} };
return operators;
}
constexpr static utility::Static_String m_multiline_comment_end{"*/"};
constexpr static utility::Static_String m_multiline_comment_begin{"/*"};
constexpr static utility::Static_String m_singleline_comment{"//"};
constexpr static utility::Static_String m_annotation{"#"};
constexpr static utility::Static_String m_cr_lf{"\r\n"};
constexpr static auto m_operators = create_operators();
std::shared_ptr<std::string> m_filename;
std::vector<eval::AST_Node_Impl_Ptr<Tracer>> m_match_stack;
struct Position
{
constexpr Position() = default;
constexpr Position(const char * t_pos, const char * t_end) noexcept
: line(1), col(1), m_pos(t_pos), m_end(t_end), m_last_col(1)
{
}
static std::string_view str(const Position &t_begin, const Position &t_end) noexcept {
if (t_begin.m_pos != nullptr && t_end.m_pos != nullptr) {
return std::string_view(t_begin.m_pos, std::distance(t_begin.m_pos, t_end.m_pos));
} else {
return {};
}
}
constexpr Position &operator++() noexcept {
if (m_pos != m_end) {
if (*m_pos == '\n') {
++line;
m_last_col = col;
col = 1;
} else {
++col;
}
++m_pos;
}
return *this;
}
constexpr Position &operator--() noexcept {
--m_pos;
if (*m_pos == '\n') {
--line;
col = m_last_col;
} else {
--col;
}
return *this;
}
constexpr Position &operator+=(size_t t_distance) noexcept {
*this = (*this) + t_distance;
return *this;
}
constexpr Position operator+(size_t t_distance) const noexcept {
Position ret(*this);
for (size_t i = 0; i < t_distance; ++i) {
++ret;
}
return ret;
}
constexpr Position &operator-=(size_t t_distance) noexcept {
*this = (*this) - t_distance;
return *this;
}
constexpr Position operator-(size_t t_distance) const noexcept {
Position ret(*this);
for (size_t i = 0; i < t_distance; ++i) {
--ret;
}
return ret;
}
constexpr bool operator==(const Position &t_rhs) const noexcept {
return m_pos == t_rhs.m_pos;
}
constexpr bool operator!=(const Position &t_rhs) const noexcept {
return m_pos != t_rhs.m_pos;
}
constexpr bool has_more() const noexcept {
return m_pos != m_end;
}
constexpr size_t remaining() const noexcept {
return static_cast<size_t>(m_end - m_pos);
}
constexpr const char& operator*() const noexcept {
if (m_pos == m_end) {
return ""[0];
} else {
return *m_pos;
}
}
int line = -1;
int col = -1;
private:
const char *m_pos = nullptr;
const char *m_end = nullptr;
int m_last_col = -1;
};
Position m_position;
Tracer m_tracer;
Optimizer m_optimizer;
void validate_object_name(const std::string_view &name) const
{
if (!Name_Validator::valid_object_name(name)) {
throw exception::eval_error("Invalid Object Name: " + std::string(name), File_Position(m_position.line, m_position.col), *m_filename);
}
}
public:
explicit ChaiScript_Parser(Tracer tracer = Tracer(), Optimizer optimizer=Optimizer())
: m_tracer(std::move(tracer)),
m_optimizer(std::move(optimizer))
{
m_match_stack.reserve(2);
}
Tracer &get_tracer() noexcept
{
return m_tracer;
}
Optimizer &get_optimizer() noexcept
{
return m_optimizer;
}
ChaiScript_Parser(const ChaiScript_Parser &) = delete;
ChaiScript_Parser &operator=(const ChaiScript_Parser &) = delete;
ChaiScript_Parser(ChaiScript_Parser &&) = default;
ChaiScript_Parser &operator=(ChaiScript_Parser &&) = delete;
constexpr static auto m_alphabet = build_alphabet();
constexpr static Operator_Matches m_operator_matches{};
/// test a char in an m_alphabet
constexpr bool char_in_alphabet(char c, detail::Alphabet a) const noexcept {
return m_alphabet[a][static_cast<uint8_t>(c)];
}
/// Prints the parsed ast_nodes as a tree
void debug_print(const AST_Node &t, std::string prepend = "") const override {
std::cout << prepend << "(" << ast_node_type_to_string(t.identifier) << ") " << t.text << " : " << t.start().line << ", " << t.start().column << '\n';
for (const auto &node : t.get_children()) {
debug_print(node.get(), prepend + " ");
}
}
/// Helper function that collects ast_nodes from a starting position to the top of the stack into a new AST node
template<typename NodeType>
void build_match(size_t t_match_start, std::string t_text = "") {
bool is_deep = false;
Parse_Location filepos = [&]()->Parse_Location{
//so we want to take everything to the right of this and make them children
if (t_match_start != m_match_stack.size()) {
is_deep = true;
return Parse_Location(
m_filename,
m_match_stack[t_match_start]->location.start.line,
m_match_stack[t_match_start]->location.start.column,
m_position.line,
m_position.col
);
} else {
return Parse_Location(
m_filename,
m_position.line,
m_position.col,
m_position.line,
m_position.col
);
}
}();
std::vector<eval::AST_Node_Impl_Ptr<Tracer>> new_children;
if (is_deep) {
new_children.assign(std::make_move_iterator(m_match_stack.begin() + static_cast<int>(t_match_start)),
std::make_move_iterator(m_match_stack.end()));
m_match_stack.erase(m_match_stack.begin() + static_cast<int>(t_match_start), m_match_stack.end());
}
/// \todo fix the fact that a successful match that captured no ast_nodes doesn't have any real start position
m_match_stack.push_back(
m_optimizer.optimize(
chaiscript::make_unique<chaiscript::eval::AST_Node_Impl<Tracer>, NodeType>(
std::move(t_text),
std::move(filepos),
std::move(new_children)))
);
}
/// Reads a symbol group from input if it matches the parameter, without skipping initial whitespace
inline auto Symbol_(const utility::Static_String &sym) noexcept
{
const auto len = sym.size();
if (m_position.remaining() >= len) {
const char *file_pos = &(*m_position);
for (size_t pos = 0; pos < len; ++pos)
{
if (sym.c_str()[pos] != file_pos[pos]) { return false; }
}
m_position += len;
return true;
}
return false;
}
/// Skips any multi-line or single-line comment
bool SkipComment() {
if (Symbol_(m_multiline_comment_begin)) {
while (m_position.has_more()) {
if (Symbol_(m_multiline_comment_end)) {
break;
} else if (!Eol_()) {
++m_position;
}
}
return true;
} else if (Symbol_(m_singleline_comment)) {
while (m_position.has_more()) {
if (Symbol_(m_cr_lf)) {
m_position -= 2;
break;
} else if (Char_('\n')) {
--m_position;
break;
} else {
++m_position;
}
}
return true;
} else if (Symbol_(m_annotation)) {
while (m_position.has_more()) {
if (Symbol_(m_cr_lf)) {
m_position -= 2;
break;
} else if (Char_('\n')) {
--m_position;
break;
} else {
++m_position;
}
}
return true;
}
return false;
}
/// Skips ChaiScript whitespace, which means space and tab, but not cr/lf
/// jespada: Modified SkipWS to skip optionally CR ('\n') and/or LF+CR ("\r\n")
/// AlekMosingiewicz: Added exception when illegal character detected
bool SkipWS(bool skip_cr=false) {
bool retval = false;
while (m_position.has_more()) {
if(static_cast<unsigned char>(*m_position) > 0x7e) {
throw exception::eval_error("Illegal character", File_Position(m_position.line, m_position.col), *m_filename);
}
auto end_line = (*m_position != 0) && ((*m_position == '\n') || (*m_position == '\r' && *(m_position+1) == '\n'));
if ( char_in_alphabet(*m_position,detail::white_alphabet) || (skip_cr && end_line)) {
if(end_line) {
if(*m_position == '\r') {
// discards lf
++m_position;
}
}
++m_position;
retval = true;
}
else if (SkipComment()) {
retval = true;
} else {
break;
}
}
return retval;
}
/// Reads the optional exponent (scientific notation) and suffix for a Float
bool read_exponent_and_suffix() noexcept {
// Support a form of scientific notation: 1e-5, 35.5E+8, 0.01e19
if (m_position.has_more() && (std::tolower(*m_position) == 'e')) {
++m_position;
if (m_position.has_more() && ((*m_position == '-') || (*m_position == '+'))) {
++m_position;
}
auto exponent_pos = m_position;
while (m_position.has_more() && char_in_alphabet(*m_position,detail::int_alphabet) ) {
++m_position;
}
if (m_position == exponent_pos) {
// Require at least one digit after the exponent
return false;
}
}
// Parse optional float suffix
while (m_position.has_more() && char_in_alphabet(*m_position, detail::float_suffix_alphabet))
{
++m_position;
}
return true;
}
/// Reads a floating point value from input, without skipping initial whitespace
bool Float_() noexcept {
if (m_position.has_more() && char_in_alphabet(*m_position,detail::float_alphabet) ) {
while (m_position.has_more() && char_in_alphabet(*m_position,detail::int_alphabet) ) {
++m_position;
}
if (m_position.has_more() && (std::tolower(*m_position) == 'e')) {
// The exponent is valid even without any decimal in the Float (1e8, 3e-15)
return read_exponent_and_suffix();
}
else if (m_position.has_more() && (*m_position == '.')) {
++m_position;
if (m_position.has_more() && char_in_alphabet(*m_position,detail::int_alphabet)) {
while (m_position.has_more() && char_in_alphabet(*m_position,detail::int_alphabet) ) {
++m_position;
}
// After any decimal digits, support an optional exponent (3.7e3)
return read_exponent_and_suffix();
} else {
--m_position;
}
}
}
return false;
}
/// Reads a hex value from input, without skipping initial whitespace
bool Hex_() noexcept {
if (m_position.has_more() && (*m_position == '0')) {
++m_position;
if (m_position.has_more() && char_in_alphabet(*m_position, detail::x_alphabet) ) {
++m_position;
if (m_position.has_more() && char_in_alphabet(*m_position, detail::hex_alphabet)) {
while (m_position.has_more() && char_in_alphabet(*m_position, detail::hex_alphabet) ) {
++m_position;
}
while (m_position.has_more() && char_in_alphabet(*m_position, detail::int_suffix_alphabet))
{
++m_position;
}
return true;
}
else {
--m_position;
}
}
else {
--m_position;
}
}
return false;
}
/// Reads an integer suffix
void IntSuffix_() {
while (m_position.has_more() && char_in_alphabet(*m_position, detail::int_suffix_alphabet))
{
++m_position;
}
}
/// Reads a binary value from input, without skipping initial whitespace
bool Binary_() {
if (m_position.has_more() && (*m_position == '0')) {
++m_position;
if (m_position.has_more() && char_in_alphabet(*m_position, detail::b_alphabet) ) {
++m_position;
if (m_position.has_more() && char_in_alphabet(*m_position, detail::bin_alphabet) ) {
while (m_position.has_more() && char_in_alphabet(*m_position, detail::bin_alphabet) ) {
++m_position;
}
return true;
} else {
--m_position;
}
} else {
--m_position;
}
}
return false;
}
/// Parses a floating point value and returns a Boxed_Value representation of it
static Boxed_Value buildFloat(const std::string_view &t_val)
{
bool float_ = false;
bool long_ = false;
auto i = t_val.size();
for (; i > 0; --i)
{
char val = t_val[i-1];
if (val == 'f' || val == 'F')
{
float_ = true;
} else if (val == 'l' || val == 'L') {
long_ = true;
} else {
break;
}
}
if (float_)
{
return const_var(parse_num<float>(t_val.substr(0,i)));
} else if (long_) {
return const_var(parse_num<long double>(t_val.substr(0,i)));
} else {
return const_var(parse_num<double>(t_val.substr(0,i)));
}
}
static Boxed_Value buildInt(const int base, std::string_view t_val, const bool prefixed)
{
bool unsigned_ = false;
bool long_ = false;
bool longlong_ = false;
auto i = t_val.size();
for (; i > 0; --i)
{
const char val = t_val[i-1];
if (val == 'u' || val == 'U')
{
unsigned_ = true;
} else if (val == 'l' || val == 'L') {
if (long_)
{
longlong_ = true;
}
long_ = true;
} else {
break;
}
}
if (prefixed) { t_val.remove_prefix(2); };
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-compare"
#ifdef CHAISCRIPT_CLANG
#pragma GCC diagnostic ignored "-Wtautological-compare"
#pragma GCC diagnostic ignored "-Wsign-conversion"
#endif
#endif
try {
/// TODO fix this to use from_chars
auto u = std::stoll(std::string(t_val),nullptr,base);
if (!unsigned_ && !long_ && u >= std::numeric_limits<int>::min() && u <= std::numeric_limits<int>::max()) {
return const_var(static_cast<int>(u));
} else if ((unsigned_ || base != 10) && !long_ && u >= std::numeric_limits<unsigned int>::min() && u <= std::numeric_limits<unsigned int>::max()) {
return const_var(static_cast<unsigned int>(u));
} else if (!unsigned_ && !longlong_ && u >= std::numeric_limits<long>::min() && u <= std::numeric_limits<long>::max()) {
return const_var(static_cast<long>(u));
} else if ((unsigned_ || base != 10) && !longlong_
&& u >= std::numeric_limits<unsigned long>::min()
&& u <= std::numeric_limits<unsigned long>::max()) {
return const_var(static_cast<unsigned long>(u));
} else if (!unsigned_ && u >= std::numeric_limits<long long>::min() && u <= std::numeric_limits<long long>::max()) {
return const_var(static_cast<long long>(u));
} else {
return const_var(static_cast<unsigned long long>(u));
}
} catch (const std::out_of_range &) {
// too big to be signed
try {
/// TODO fix this to use from_chars
auto u = std::stoull(std::string(t_val),nullptr,base);
if (!longlong_ && u >= std::numeric_limits<unsigned long>::min() && u <= std::numeric_limits<unsigned long>::max()) {
return const_var(static_cast<unsigned long>(u));
} else {
return const_var(static_cast<unsigned long long>(u));
}
} catch (const std::out_of_range &) {
// it's just simply too big
return const_var(std::numeric_limits<long long>::max());
}
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
}
template<typename T, typename ... Param>
std::unique_ptr<eval::AST_Node_Impl<Tracer>> make_node(std::string_view t_match, const int t_prev_line, const int t_prev_col, Param && ...param)
{
return chaiscript::make_unique<eval::AST_Node_Impl<Tracer>, T>(std::string(t_match), Parse_Location(m_filename, t_prev_line, t_prev_col, m_position.line, m_position.col), std::forward<Param>(param)...);
}
/// Reads a number from the input, detecting if it's an integer or floating point
bool Num() {
SkipWS();
const auto start = m_position;
if (m_position.has_more() && char_in_alphabet(*m_position, detail::float_alphabet) ) {
try {
if (Hex_()) {
auto match = Position::str(start, m_position);
auto bv = buildInt(16, match, true);
m_match_stack.emplace_back(make_node<eval::Constant_AST_Node<Tracer>>(match, start.line, start.col, std::move(bv)));
return true;
}
if (Binary_()) {
auto match = Position::str(start, m_position);
auto bv = buildInt(2, match, true);
m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(match, start.line, start.col, std::move(bv)));
return true;
}
if (Float_()) {
auto match = Position::str(start, m_position);
auto bv = buildFloat(match);
m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(match, start.line, start.col, std::move(bv)));
return true;
}
else {
IntSuffix_();
auto match = Position::str(start, m_position);
if (!match.empty() && (match[0] == '0')) {
auto bv = buildInt(8, match, false);
m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(match, start.line, start.col, std::move(bv)));
}
else if (!match.empty()) {
auto bv = buildInt(10, match, false);
m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(match, start.line, start.col, std::move(bv)));
} else {
return false;
}
return true;
}
} catch (const std::invalid_argument &) {
// error parsing number passed in to buildFloat/buildInt
return false;
}
}
else {
return false;
}
}
/// Reads an identifier from input which conforms to C's identifier naming conventions, without skipping initial whitespace
bool Id_() {
if (m_position.has_more() && char_in_alphabet(*m_position, detail::id_alphabet)) {
while (m_position.has_more() && char_in_alphabet(*m_position, detail::keyword_alphabet) ) {
++m_position;
}
return true;
} else if (m_position.has_more() && (*m_position == '`')) {
++m_position;
const auto start = m_position;
while (m_position.has_more() && (*m_position != '`')) {
if (Eol()) {
throw exception::eval_error("Carriage return in identifier literal", File_Position(m_position.line, m_position.col), *m_filename);
}
else {
++m_position;
}
}
if (start == m_position) {
throw exception::eval_error("Missing contents of identifier literal", File_Position(m_position.line, m_position.col), *m_filename);
}
else if (!m_position.has_more()) {
throw exception::eval_error("Incomplete identifier literal", File_Position(m_position.line, m_position.col), *m_filename);
}
++m_position;
return true;
}
return false;
}
/// Reads (and potentially captures) an identifier from input
bool Id(const bool validate) {
SkipWS();
const auto start = m_position;
if (Id_()) {
auto text = Position::str(start, m_position);
const auto text_hash = utility::hash(text);
if (validate) {
validate_object_name(text);
}
#ifdef CHAISCRIPT_MSVC
#pragma warning(push)
#pragma warning(disable : 4307)
#endif
switch (text_hash) {
case utility::hash("true"): {
m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(text, start.line, start.col, const_var(true)));
} break;
case utility::hash("false"): {
m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(text, start.line, start.col, const_var(false)));
} break;
case utility::hash("Infinity"): {
m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(text, start.line, start.col,
const_var(std::numeric_limits<double>::infinity())));
} break;
case utility::hash("NaN"): {
m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(text, start.line, start.col,
const_var(std::numeric_limits<double>::quiet_NaN())));
} break;
case utility::hash("__LINE__"): {
m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(text, start.line, start.col,
const_var(start.line)));
} break;
case utility::hash("__FILE__"): {
m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(text, start.line, start.col,
const_var(m_filename)));
} break;
case utility::hash("__FUNC__"): {
std::string fun_name = "NOT_IN_FUNCTION";
for (size_t idx = m_match_stack.empty() ? 0 : m_match_stack.size() - 1; idx > 0; --idx)
{
if (m_match_stack[idx-1]->identifier == AST_Node_Type::Id
&& m_match_stack[idx-0]->identifier == AST_Node_Type::Arg_List) {
fun_name = m_match_stack[idx-1]->text;
}
}
m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(text, start.line, start.col,
const_var(fun_name)));
} break;
case utility::hash("__CLASS__"): {
std::string fun_name = "NOT_IN_CLASS";
for (size_t idx = m_match_stack.empty() ? 0 : m_match_stack.size() - 1; idx > 1; --idx)
{
if (m_match_stack[idx-2]->identifier == AST_Node_Type::Id
&& m_match_stack[idx-1]->identifier == AST_Node_Type::Id
&& m_match_stack[idx-0]->identifier == AST_Node_Type::Arg_List) {
fun_name = m_match_stack[idx-2]->text;
}