ChaiScript / ChaiScript
1
// This file is distributed under the BSD License.
2
// See "license.txt" for details.
3
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
4
// Copyright 2009-2018, Jason Turner (jason@emptycrate.com)
5
// http://www.chaiscript.com
6

7
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
8
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
9

10

11
#ifndef CHAISCRIPT_PARSER_HPP_
12
#define CHAISCRIPT_PARSER_HPP_
13

14
#include <exception>
15
#include <iostream>
16
#include <memory>
17
#include <sstream>
18
#include <string>
19
#include <vector>
20
#include <cctype>
21
#include <cstring>
22

23

24

25

26
#include "../dispatchkit/boxed_value.hpp"
27
#include "chaiscript_common.hpp"
28
#include "chaiscript_optimizer.hpp"
29
#include "chaiscript_tracer.hpp"
30
#include "../utility/hash.hpp"
31
#include "../utility/static_string.hpp"
32

33
#if defined(CHAISCRIPT_UTF16_UTF32)
34
#include <locale>
35
#include <codecvt>
36
#endif
37

38
#if defined(CHAISCRIPT_MSVC) && defined(max) && defined(min)
39
#define CHAISCRIPT_PUSHED_MIN_MAX
40
#pragma push_macro("max") // Why Microsoft? why? This is worse than bad
41
#undef max
42
#pragma push_macro("min")
43
#undef min
44
#endif
45

46

47
namespace chaiscript
48
{
49
  /// \brief Classes and functions used during the parsing process.
50
  namespace parser
51
  {
52
    /// \brief Classes and functions internal to the parsing process. Not supported for the end user.
53
    namespace detail 
54
    {
55
      enum Alphabet
56
      {   symbol_alphabet = 0
57
        ,   keyword_alphabet
58
          ,   int_alphabet
59
          ,   float_alphabet
60
          ,   x_alphabet
61
          ,   hex_alphabet
62
          ,   b_alphabet
63
          ,   bin_alphabet
64
          ,   id_alphabet
65
          ,   white_alphabet
66
          ,   int_suffix_alphabet
67
          ,   float_suffix_alphabet
68
          ,   max_alphabet
69
          ,   lengthof_alphabet = 256
70
      };
71

72
      // Generic for u16, u32 and wchar
73
      template<typename string_type>
74
      struct Char_Parser_Helper
75
      {
76
        // common for all implementations
77
        static std::string u8str_from_ll(long long val)
78
        {
79
          using char_type = std::string::value_type;
80

81
          char_type c[2];
82
          c[1] = char_type(val);
83
          c[0] = char_type(val >> 8);
84

85
          if (c[0] == 0)
86
          {
87
            return std::string(1, c[1]); // size, character
88
          }
89

90
          return std::string(c, 2); // char buffer, size
91
        }
92

93
        static string_type str_from_ll(long long val)
94
        {
95
          using target_char_type = typename string_type::value_type;
96
#if defined (CHAISCRIPT_UTF16_UTF32)
97
          // prepare converter
98
          std::wstring_convert<std::codecvt_utf8<target_char_type>, target_char_type> converter;
99
          // convert
100
          return converter.from_bytes(u8str_from_ll(val));
101
#else
102
          // no conversion available, just put value as character
103
          return string_type(1, target_char_type(val)); // size, character
104
#endif
105
        }
106
      };
107

108
      // Specialization for char AKA UTF-8
109
      template<>
110
      struct Char_Parser_Helper<std::string>
111
      {
112
        static std::string str_from_ll(long long val)
113
        {
114
          // little SFINAE trick to avoid base class
115
          return Char_Parser_Helper<std::true_type>::u8str_from_ll(val);
116
        }
117
      };
118
    }
119

120

121
    template<typename Tracer, typename Optimizer, std::size_t Parse_Depth=512>
122
    class ChaiScript_Parser final : public ChaiScript_Parser_Base {
123 1
      void *get_tracer_ptr() noexcept override {
124 1
        return &m_tracer;
125
      }
126

127
      std::size_t m_current_parse_depth = 0;
128

129
      struct Depth_Counter
130
      {
131
        static const auto max_depth = Parse_Depth;
132 1
        Depth_Counter(ChaiScript_Parser *t_parser) : parser(t_parser)
133
        {
134 1
          ++parser->m_current_parse_depth;
135 1
          if (parser->m_current_parse_depth > max_depth) {
136
            throw exception::eval_error("Maximum parse depth exceeded", 
137 1
                File_Position(parser->m_position.line, parser->m_position.col), *(parser->m_filename));
138
          }
139
        }
140

141 1
        ~Depth_Counter() noexcept
142
        {
143 1
          --parser->m_current_parse_depth;
144
        }
145

146
        ChaiScript_Parser *parser;
147
      };
148

149
      template<typename Array2D, typename First, typename Second>
150
      constexpr static void set_alphabet(Array2D &array, const First first, const Second second) noexcept
151
      {
152
        auto *first_ptr = &std::get<0>(array) + static_cast<std::size_t>(first);
153
        auto *second_ptr = &std::get<0>(*first_ptr) + static_cast<std::size_t>(second);
154
        *second_ptr = true;
155
      }
156

157
      constexpr static std::array<std::array<bool, detail::lengthof_alphabet>, detail::max_alphabet> build_alphabet() noexcept
158
      {
159
        std::array<std::array<bool, detail::lengthof_alphabet>, detail::max_alphabet> alphabet{};
160

161
        set_alphabet(alphabet, detail::symbol_alphabet, '?');
162

163
        set_alphabet(alphabet, detail::symbol_alphabet, '?');
164
        set_alphabet(alphabet, detail::symbol_alphabet, '+');
165
        set_alphabet(alphabet, detail::symbol_alphabet, '-');
166
        set_alphabet(alphabet, detail::symbol_alphabet, '*');
167
        set_alphabet(alphabet, detail::symbol_alphabet, '/');
168
        set_alphabet(alphabet, detail::symbol_alphabet, '|');
169
        set_alphabet(alphabet, detail::symbol_alphabet, '&');
170
        set_alphabet(alphabet, detail::symbol_alphabet, '^');
171
        set_alphabet(alphabet, detail::symbol_alphabet, '=');
172
        set_alphabet(alphabet, detail::symbol_alphabet, '.');
173
        set_alphabet(alphabet, detail::symbol_alphabet, '<');
174
        set_alphabet(alphabet, detail::symbol_alphabet, '>');
175

176
        for ( size_t c = 'a' ; c <= 'z' ; ++c ) { set_alphabet(alphabet, detail::keyword_alphabet, c); }
177
        for ( size_t c = 'A' ; c <= 'Z' ; ++c ) { set_alphabet(alphabet, detail::keyword_alphabet, c); }
178
        for ( size_t c = '0' ; c <= '9' ; ++c ) { set_alphabet(alphabet, detail::keyword_alphabet, c); }
179
        set_alphabet(alphabet, detail::keyword_alphabet, '_');
180

181
        for ( size_t c = '0' ; c <= '9' ; ++c ) { set_alphabet(alphabet, detail::int_alphabet, c); }
182
        for ( size_t c = '0' ; c <= '9' ; ++c ) { set_alphabet(alphabet, detail::float_alphabet, c); }
183
        set_alphabet(alphabet, detail::float_alphabet, '.');
184

185
        for ( size_t c = '0' ; c <= '9' ; ++c ) { set_alphabet(alphabet, detail::hex_alphabet, c); }
186
        for ( size_t c = 'a' ; c <= 'f' ; ++c ) { set_alphabet(alphabet, detail::hex_alphabet, c); }
187
        for ( size_t c = 'A' ; c <= 'F' ; ++c ) { set_alphabet(alphabet, detail::hex_alphabet, c); }
188

189
        set_alphabet(alphabet, detail::x_alphabet, 'x');
190
        set_alphabet(alphabet, detail::x_alphabet, 'X');
191

192
        for ( size_t c = '0' ; c <= '1' ; ++c ) { set_alphabet(alphabet, detail::bin_alphabet, c); }
193
        set_alphabet(alphabet, detail::b_alphabet, 'b');
194
        set_alphabet(alphabet, detail::b_alphabet, 'B');
195

196
        for ( size_t c = 'a' ; c <= 'z' ; ++c ) { set_alphabet(alphabet, detail::id_alphabet, c); }
197
        for ( size_t c = 'A' ; c <= 'Z' ; ++c ) { set_alphabet(alphabet, detail::id_alphabet, c); }
198
        set_alphabet(alphabet, detail::id_alphabet, '_');
199

200
        set_alphabet(alphabet, detail::white_alphabet, ' ');
201
        set_alphabet(alphabet, detail::white_alphabet, '\t');
202

203
        set_alphabet(alphabet, detail::int_suffix_alphabet, 'l');
204
        set_alphabet(alphabet, detail::int_suffix_alphabet, 'L');
205
        set_alphabet(alphabet, detail::int_suffix_alphabet, 'u');
206
        set_alphabet(alphabet, detail::int_suffix_alphabet, 'U');
207

208
        set_alphabet(alphabet, detail::float_suffix_alphabet, 'l');
209
        set_alphabet(alphabet, detail::float_suffix_alphabet, 'L');
210
        set_alphabet(alphabet, detail::float_suffix_alphabet, 'f');
211
        set_alphabet(alphabet, detail::float_suffix_alphabet, 'F');
212

213
        return alphabet;
214
      }
215

216

217
      struct Operator_Matches
218
      {
219
        using SS = utility::Static_String;
220

221
        std::array<utility::Static_String, 1> m_0  {{SS("?")}};
222
        std::array<utility::Static_String, 1> m_1  {{SS("||")}};
223
        std::array<utility::Static_String, 1> m_2  {{SS("&&")}};
224
        std::array<utility::Static_String, 1> m_3  {{SS("|")}};
225
        std::array<utility::Static_String, 1> m_4  {{SS("^")}};
226
        std::array<utility::Static_String, 1> m_5  {{SS("&")}};
227
        std::array<utility::Static_String, 2> m_6  {{SS("=="), SS("!=")}};
228
        std::array<utility::Static_String, 4> m_7  {{SS("<"), SS("<="), SS(">"), SS(">=")}};
229
        std::array<utility::Static_String, 2> m_8  {{SS("<<"), SS(">>")}};
230
          //We share precedence here but then separate them later
231
        std::array<utility::Static_String, 2> m_9  {{SS("+"), SS("-")}};
232
        std::array<utility::Static_String, 3> m_10 {{SS("*"), SS("/"), SS("%")}};
233
        std::array<utility::Static_String, 6> m_11 {{SS("++"), SS("--"), SS("-"), SS("+"), SS("!"), SS("~")}};
234

235 1
        bool is_match(const std::string_view &t_str) const noexcept {
236 1
          constexpr std::array<std::size_t, 12> groups{{0,1,2,3,4,5,6,7,8,9,10,11}};
237 1
          return std::any_of(groups.begin(), groups.end(), [&t_str, this](const std::size_t group){ return is_match(group, t_str); });
238
        }
239

240
        template<typename Predicate>
241 1
        bool any_of(const std::size_t t_group, Predicate &&predicate) const
242
        {
243 1
          auto match = [&predicate](const auto &array) {
244
            return std::any_of(array.begin(), array.end(), predicate);
245
          };
246

247 1
          switch (t_group) {
248 1
            case 0: return match(m_0);
249 1
            case 1: return match(m_1);
250 1
            case 2: return match(m_2);
251 1
            case 3: return match(m_3);
252 1
            case 4: return match(m_4);
253 1
            case 5: return match(m_5);
254 1
            case 6: return match(m_6);
255 1
            case 7: return match(m_7);
256 1
            case 8: return match(m_8);
257 1
            case 9: return match(m_9);
258 1
            case 10: return match(m_10);
259 0
            case 11: return match(m_11);
260 0
            default: return false;
261
          }
262
        }
263

264 1
        constexpr bool is_match(const std::size_t t_group, const std::string_view &t_str) const noexcept {
265 1
          auto match = [&t_str](const auto &array) {
266
            return std::any_of(array.begin(), array.end(), [&t_str](const auto &v){ return v == t_str; });
267
          };
268

269 1
          switch (t_group) {
270 1
            case 0: return match(m_0);
271 1
            case 1: return match(m_1);
272 1
            case 2: return match(m_2);
273 1
            case 3: return match(m_3);
274 1
            case 4: return match(m_4);
275 1
            case 5: return match(m_5);
276 1
            case 6: return match(m_6);
277 1
            case 7: return match(m_7);
278 1
            case 8: return match(m_8);
279 1
            case 9: return match(m_9);
280 1
            case 10: return match(m_10);
281 1
            case 11: return match(m_11);
282 0
            default: return false;
283
          }
284
        }
285

286
      };
287

288
      constexpr static std::array<Operator_Precedence, 12> create_operators() noexcept {
289
        std::array<Operator_Precedence, 12> operators = { {
290
          Operator_Precedence::Ternary_Cond,
291
          Operator_Precedence::Logical_Or,
292
          Operator_Precedence::Logical_And,
293
          Operator_Precedence::Bitwise_Or,
294
          Operator_Precedence::Bitwise_Xor,
295
          Operator_Precedence::Bitwise_And,
296
          Operator_Precedence::Equality,
297
          Operator_Precedence::Comparison,
298
          Operator_Precedence::Shift,
299
          Operator_Precedence::Addition,
300
          Operator_Precedence::Multiplication,
301
          Operator_Precedence::Prefix
302
        } };
303
        return operators;
304
      }
305

306
      constexpr static utility::Static_String m_multiline_comment_end{"*/"};
307
      constexpr static utility::Static_String m_multiline_comment_begin{"/*"};
308
      constexpr static utility::Static_String m_singleline_comment{"//"};
309
      constexpr static utility::Static_String m_annotation{"#"};
310
      constexpr static utility::Static_String m_cr_lf{"\r\n"};
311
      constexpr static auto m_operators = create_operators();
312

313
      std::shared_ptr<std::string> m_filename;
314
      std::vector<eval::AST_Node_Impl_Ptr<Tracer>> m_match_stack;
315

316

317
      struct Position
318
      {
319 1
        constexpr Position() = default;
320

321 1
        constexpr Position(const char * t_pos, const char * t_end) noexcept
322 1
          : line(1), col(1), m_pos(t_pos), m_end(t_end), m_last_col(1)
323
        {
324
        }
325

326 1
        static std::string_view str(const Position &t_begin, const Position &t_end) noexcept {
327 1
          if (t_begin.m_pos != nullptr && t_end.m_pos != nullptr) {
328 1
            return std::string_view(t_begin.m_pos, std::distance(t_begin.m_pos, t_end.m_pos));
329
          } else {
330 0
            return {};
331
          }
332
        }
333

334 1
        constexpr Position &operator++() noexcept {
335 1
          if (m_pos != m_end) {
336 1
            if (*m_pos == '\n') {
337 1
              ++line;
338 1
              m_last_col = col;
339 1
              col = 1;
340
            } else {
341 1
              ++col;
342
            }
343

344 1
            ++m_pos;
345
          }
346 1
          return *this;
347
        }
348

349 1
        constexpr Position &operator--() noexcept {
350 1
          --m_pos;
351 1
          if (*m_pos == '\n') {
352 1
            --line;
353 1
            col = m_last_col;
354
          } else {
355 1
            --col;
356
          }
357 1
          return *this;
358
        }
359

360 1
        constexpr Position &operator+=(size_t t_distance) noexcept {
361 1
          *this = (*this) + t_distance;
362 1
          return *this;
363
        }
364

365 1
        constexpr Position operator+(size_t t_distance) const noexcept {
366 1
          Position ret(*this);
367 1
          for (size_t i = 0; i < t_distance; ++i) {
368 1
            ++ret;
369
          }
370 1
          return ret;
371
        }
372

373 1
        constexpr Position &operator-=(size_t t_distance) noexcept {
374 1
          *this = (*this) - t_distance;
375 1
          return *this;
376
        }
377

378 1
        constexpr Position operator-(size_t t_distance) const noexcept {
379 1
          Position ret(*this);
380 1
          for (size_t i = 0; i < t_distance; ++i) {
381 1
            --ret;
382
          }
383 1
          return ret;
384
        }
385

386 1
        constexpr bool operator==(const Position &t_rhs) const noexcept {
387 1
          return m_pos == t_rhs.m_pos;
388
        }
389

390 1
        constexpr bool operator!=(const Position &t_rhs) const noexcept {
391 1
          return m_pos != t_rhs.m_pos;
392
        }
393

394 1
        constexpr bool has_more() const noexcept {
395 1
          return m_pos != m_end;
396
        }
397

398 1
        constexpr size_t remaining() const noexcept {
399 1
          return static_cast<size_t>(m_end - m_pos);
400
        }
401

402 1
        constexpr const char& operator*() const noexcept {
403 1
          if (m_pos == m_end) {
404 1
            return ""[0];
405
          } else {
406 1
            return *m_pos;
407
          }
408
        }
409

410
        int line = -1;
411
        int col = -1;
412

413
        private:
414
          const char *m_pos = nullptr;
415
          const char *m_end = nullptr;
416
          int m_last_col = -1;
417
      };
418

419
      Position m_position;
420

421
      Tracer m_tracer;
422
      Optimizer m_optimizer;
423

424 1
      void validate_object_name(const std::string_view &name) const
425
      {
426 1
        if (!Name_Validator::valid_object_name(name)) {
427 1
          throw exception::eval_error("Invalid Object Name: " + std::string(name), File_Position(m_position.line, m_position.col), *m_filename);
428
        }
429
      }
430

431
    public:
432 1
      explicit ChaiScript_Parser(Tracer tracer = Tracer(), Optimizer optimizer=Optimizer())
433 1
        : m_tracer(std::move(tracer)),
434 1
          m_optimizer(std::move(optimizer))
435
      {
436 1
        m_match_stack.reserve(2);
437
      }
438

439 1
      Tracer &get_tracer() noexcept
440
      {
441 1
        return m_tracer;
442
      }
443

444
      Optimizer &get_optimizer() noexcept
445
      {
446
        return m_optimizer;
447
      }
448

449
      ChaiScript_Parser(const ChaiScript_Parser &) = delete;
450
      ChaiScript_Parser &operator=(const ChaiScript_Parser &) = delete;
451
      ChaiScript_Parser(ChaiScript_Parser &&) = default;
452
      ChaiScript_Parser &operator=(ChaiScript_Parser &&) = delete;
453

454
      constexpr static auto m_alphabet = build_alphabet();
455
      constexpr static Operator_Matches m_operator_matches{};
456

457
      /// test a char in an m_alphabet
458 1
      constexpr bool char_in_alphabet(char c, detail::Alphabet a) const noexcept { 
459 1
        return m_alphabet[a][static_cast<uint8_t>(c)]; 
460
      }
461

462
      /// Prints the parsed ast_nodes as a tree
463 0
      void debug_print(const AST_Node &t, std::string prepend = "") const override {
464 0
        std::cout << prepend << "(" << ast_node_type_to_string(t.identifier) << ") " << t.text << " : " << t.start().line << ", " << t.start().column << '\n';
465 0
        for (const auto &node : t.get_children()) {
466 0
          debug_print(node.get(), prepend + "  ");
467
        }
468
      }
469

470

471
      /// Helper function that collects ast_nodes from a starting position to the top of the stack into a new AST node
472
      template<typename NodeType>
473 1
      void build_match(size_t t_match_start, std::string t_text = "") {
474 1
        bool is_deep = false;
475

476 1
        Parse_Location filepos = [&]()->Parse_Location{ 
477
          //so we want to take everything to the right of this and make them children
478
          if (t_match_start != m_match_stack.size()) {
479
            is_deep = true;
480
            return Parse_Location(
481
                m_filename,
482
                m_match_stack[t_match_start]->location.start.line,
483
                m_match_stack[t_match_start]->location.start.column,
484
                m_position.line,
485
                m_position.col 
486
              );
487
          } else {
488
            return Parse_Location(
489
                m_filename,
490
                m_position.line,
491
                m_position.col,
492
                m_position.line,
493
                m_position.col
494
              );
495
          }
496
        }();
497

498 1
        std::vector<eval::AST_Node_Impl_Ptr<Tracer>> new_children;
499

500 1
        if (is_deep) {
501 1
          new_children.assign(std::make_move_iterator(m_match_stack.begin() + static_cast<int>(t_match_start)), 
502
                              std::make_move_iterator(m_match_stack.end()));
503 1
          m_match_stack.erase(m_match_stack.begin() + static_cast<int>(t_match_start), m_match_stack.end());
504
        }
505

506
        /// \todo fix the fact that a successful match that captured no ast_nodes doesn't have any real start position
507 1
        m_match_stack.push_back(
508
            m_optimizer.optimize(
509
              chaiscript::make_unique<chaiscript::eval::AST_Node_Impl<Tracer>, NodeType>(
510 1
                std::move(t_text),
511 1
                std::move(filepos),
512 1
                std::move(new_children)))
513
            );
514
      }
515

516

517
      /// Reads a symbol group from input if it matches the parameter, without skipping initial whitespace
518 1
      inline auto Symbol_(const utility::Static_String &sym) noexcept
519
      {
520 1
        const auto len = sym.size();
521 1
        if (m_position.remaining() >= len) {
522 1
          const char *file_pos = &(*m_position);
523 1
          for (size_t pos = 0; pos < len; ++pos)
524
          {
525 1
            if (sym.c_str()[pos] != file_pos[pos]) { return false; }
526
          }
527 1
          m_position += len;
528 1
          return true;
529
        }
530 1
        return false;
531
      }
532

533
      /// Skips any multi-line or single-line comment
534 1
      bool SkipComment() {
535 1
        if (Symbol_(m_multiline_comment_begin)) {
536 1
          while (m_position.has_more()) {
537 1
            if (Symbol_(m_multiline_comment_end)) {
538 1
              break;
539 1
            } else if (!Eol_()) {
540 1
              ++m_position;
541
            }
542
          }
543 1
          return true;
544 1
        } else if (Symbol_(m_singleline_comment)) {
545 1
          while (m_position.has_more()) {
546 1
            if (Symbol_(m_cr_lf)) {
547 1
              m_position -= 2;
548 1
              break;
549 1
            } else if (Char_('\n')) {
550 1
              --m_position;
551 1
              break;
552
            } else {
553 1
              ++m_position;
554
            }
555
          }
556 1
          return true;
557 1
        } else if (Symbol_(m_annotation)) {
558 1
          while (m_position.has_more()) {
559 1
            if (Symbol_(m_cr_lf)) {
560 1
              m_position -= 2;
561 1
              break;
562 1
            } else if (Char_('\n')) {
563 1
              --m_position;
564 1
              break;
565
            } else {
566 1
              ++m_position;
567
            }
568
          }
569 1
          return true;
570
        }
571 1
        return false;
572
      }
573

574

575
      /// Skips ChaiScript whitespace, which means space and tab, but not cr/lf
576
      /// jespada: Modified SkipWS to skip optionally CR ('\n') and/or LF+CR ("\r\n")
577
      /// AlekMosingiewicz: Added exception when illegal character detected
578 1
      bool SkipWS(bool skip_cr=false) {
579 1
        bool retval = false;
580

581 1
        while (m_position.has_more()) {
582 1
          if(static_cast<unsigned char>(*m_position) > 0x7e) {
583 1
            throw exception::eval_error("Illegal character", File_Position(m_position.line, m_position.col), *m_filename);
584
          }
585 1
          auto end_line = (*m_position != 0) && ((*m_position == '\n') || (*m_position == '\r' && *(m_position+1) == '\n'));
586

587 1
          if ( char_in_alphabet(*m_position,detail::white_alphabet) || (skip_cr && end_line)) {
588

589 1
            if(end_line) {
590 1
              if(*m_position == '\r') {
591
                // discards lf
592 1
                ++m_position;
593
              }
594
            }
595

596 1
            ++m_position;
597

598 1
            retval = true;
599
          }
600 1
          else if (SkipComment()) {
601 1
            retval = true;
602
          } else {
603 1
            break;
604
          }
605
        }
606 1
        return retval;
607
      }
608

609
      /// Reads the optional exponent (scientific notation) and suffix for a Float
610 1
      bool read_exponent_and_suffix() noexcept {
611
        // Support a form of scientific notation: 1e-5, 35.5E+8, 0.01e19
612 1
        if (m_position.has_more() && (std::tolower(*m_position) == 'e')) {
613 1
          ++m_position;
614 1
          if (m_position.has_more() && ((*m_position == '-') || (*m_position == '+'))) {
615 1
            ++m_position;
616
          }
617 1
          auto exponent_pos = m_position;
618 1
          while (m_position.has_more() && char_in_alphabet(*m_position,detail::int_alphabet) ) {
619 1
            ++m_position;
620
          }
621 1
          if (m_position == exponent_pos) {
622
            // Require at least one digit after the exponent
623 1
            return false;
624
          }
625
        }
626

627
        // Parse optional float suffix
628 1
        while (m_position.has_more() && char_in_alphabet(*m_position, detail::float_suffix_alphabet))
629
        {
630 1
          ++m_position;
631
        }
632

633 1
        return true;
634
      }
635

636

637
      /// Reads a floating point value from input, without skipping initial whitespace
638 1
      bool Float_() noexcept {
639 1
        if (m_position.has_more() && char_in_alphabet(*m_position,detail::float_alphabet) ) {
640 1
          while (m_position.has_more() && char_in_alphabet(*m_position,detail::int_alphabet) ) {
641 1
            ++m_position;
642
          }
643

644 1
          if (m_position.has_more() && (std::tolower(*m_position) == 'e')) {
645
            // The exponent is valid even without any decimal in the Float (1e8, 3e-15)
646 1
            return read_exponent_and_suffix();
647
          }
648 1
          else if (m_position.has_more() && (*m_position == '.')) {
649 1
            ++m_position;
650 1
            if (m_position.has_more() && char_in_alphabet(*m_position,detail::int_alphabet)) {
651 1
              while (m_position.has_more() && char_in_alphabet(*m_position,detail::int_alphabet) ) {
652 1
                ++m_position;
653
              }
654

655
              // After any decimal digits, support an optional exponent (3.7e3)
656 1
              return read_exponent_and_suffix();
657
            } else {
658 1
              --m_position;
659
            }
660
          }
661
        }
662 1
        return false;
663
      }
664

665
      /// Reads a hex value from input, without skipping initial whitespace
666 1
      bool Hex_() noexcept {
667 1
        if (m_position.has_more() && (*m_position == '0')) {
668 1
          ++m_position;
669

670 1
          if (m_position.has_more() && char_in_alphabet(*m_position, detail::x_alphabet) ) {
671 1
            ++m_position;
672 1
            if (m_position.has_more() && char_in_alphabet(*m_position, detail::hex_alphabet)) {
673 1
              while (m_position.has_more() && char_in_alphabet(*m_position, detail::hex_alphabet) ) {
674 1
                ++m_position;
675
              }
676 1
              while (m_position.has_more() && char_in_alphabet(*m_position, detail::int_suffix_alphabet))
677
              {
678 1
                ++m_position;
679
              }
680

681 1
              return true;
682
            }
683
            else {
684 1
              --m_position;
685
            }
686
          }
687
          else {
688 1
            --m_position;
689
          }
690
        }
691

692 1
        return false;
693
      }
694

695
      /// Reads an integer suffix
696 1
      void IntSuffix_() {
697 1
        while (m_position.has_more() && char_in_alphabet(*m_position, detail::int_suffix_alphabet))
698
        {
699 1
          ++m_position;
700
        }
701
      }
702

703
      /// Reads a binary value from input, without skipping initial whitespace
704 1
      bool Binary_() {
705 1
        if (m_position.has_more() && (*m_position == '0')) {
706 1
          ++m_position;
707

708 1
          if (m_position.has_more() && char_in_alphabet(*m_position, detail::b_alphabet) ) {
709 1
            ++m_position;
710 1
            if (m_position.has_more() && char_in_alphabet(*m_position, detail::bin_alphabet) ) {
711 1
              while (m_position.has_more() && char_in_alphabet(*m_position, detail::bin_alphabet) ) {
712 1
                ++m_position;
713
              }
714 1
              return true;
715
            } else {
716 1
              --m_position;
717
            }
718
          } else {
719 1
            --m_position;
720
          }
721
        }
722

723 1
        return false;
724
      }
725

726
      /// Parses a floating point value and returns a Boxed_Value representation of it
727 1
      static Boxed_Value buildFloat(const std::string_view &t_val)
728
      {
729 1
        bool float_ = false;
730 1
        bool long_ = false;
731

732 1
        auto i = t_val.size();
733

734 1
        for (; i > 0; --i)
735
        {
736 1
          char val = t_val[i-1];
737

738 1
          if (val == 'f' || val == 'F')
739
          {
740 1
            float_ = true;
741 1
          } else if (val == 'l' || val == 'L') {
742 1
            long_ = true;
743
          } else {
744
            break;
745
          }
746
        }
747

748 1
        if (float_)
749
        {
750 1
          return const_var(parse_num<float>(t_val.substr(0,i)));
751 1
        } else if (long_) {
752 1
          return const_var(parse_num<long double>(t_val.substr(0,i)));
753
        } else {
754 1
          return const_var(parse_num<double>(t_val.substr(0,i)));
755
        }
756
      }
757

758

759

760 1
      static Boxed_Value buildInt(const int base, std::string_view t_val, const bool prefixed)
761
      {
762 1
        bool unsigned_ = false;
763 1
        bool long_ = false;
764 1
        bool longlong_ = false;
765

766 1
        auto i = t_val.size();
767

768 1
        for (; i > 0; --i)
769
        {
770 1
          const char val = t_val[i-1];
771

772 1
          if (val == 'u' || val == 'U')
773
          {
774 1
            unsigned_ = true;
775 1
          } else if (val == 'l' || val == 'L') {
776 1
            if (long_)
777
            {
778 1
              longlong_ = true;
779
            }
780

781 1
            long_ = true;
782
          } else {
783
            break;
784
          }
785
        }
786

787 1
        if (prefixed) { t_val.remove_prefix(2); };
788

789
#ifdef __GNUC__
790
#pragma GCC diagnostic push
791
#pragma GCC diagnostic ignored "-Wsign-compare"
792

793
#ifdef CHAISCRIPT_CLANG
794
#pragma GCC diagnostic ignored "-Wtautological-compare"
795
#pragma GCC diagnostic ignored "-Wsign-conversion"
796
#endif
797

798
#endif
799

800
        try {
801
          /// TODO fix this to use from_chars
802 1
          auto u = std::stoll(std::string(t_val),nullptr,base);
803

804

805 1
          if (!unsigned_ && !long_ && u >= std::numeric_limits<int>::min() && u <= std::numeric_limits<int>::max()) {
806 1
            return const_var(static_cast<int>(u));
807 1
          } else if ((unsigned_ || base != 10) && !long_ && u >= std::numeric_limits<unsigned int>::min() && u <= std::numeric_limits<unsigned int>::max()) {
808 1
            return const_var(static_cast<unsigned int>(u));
809 1
          } else if (!unsigned_ && !longlong_ && u >= std::numeric_limits<long>::min() && u <= std::numeric_limits<long>::max()) {
810 1
            return const_var(static_cast<long>(u));
811 1
          } else if ((unsigned_ || base != 10) && !longlong_
812

813 1
                     && u >= std::numeric_limits<unsigned long>::min()
814 1
                     && u <= std::numeric_limits<unsigned long>::max()) {
815 1
            return const_var(static_cast<unsigned long>(u));
816 1
          } else if (!unsigned_ && u >= std::numeric_limits<long long>::min() && u <= std::numeric_limits<long long>::max()) {
817 1
            return const_var(static_cast<long long>(u));
818
          } else {
819 1
            return const_var(static_cast<unsigned long long>(u));
820
          }
821

822 1
        } catch (const std::out_of_range &) {
823
          // too big to be signed
824
          try {
825
            /// TODO fix this to use from_chars
826 1
            auto u = std::stoull(std::string(t_val),nullptr,base);
827

828 1
            if (!longlong_ && u >= std::numeric_limits<unsigned long>::min() && u <= std::numeric_limits<unsigned long>::max()) {
829 1
              return const_var(static_cast<unsigned long>(u));
830
            } else {
831 0
              return const_var(static_cast<unsigned long long>(u));
832
            }
833 1
          } catch (const std::out_of_range &) {
834
            // it's just simply too big
835 1
            return const_var(std::numeric_limits<long long>::max());
836
          }
837
        }
838

839
#ifdef __GNUC__
840
#pragma GCC diagnostic pop
841
#endif
842

843
      }
844

845
      template<typename T, typename ... Param>
846 1
      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)
847
      {
848 1
        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)...);
849
      }
850

851
      /// Reads a number from the input, detecting if it's an integer or floating point
852 1
      bool Num() {
853 1
        SkipWS();
854

855 1
        const auto start = m_position;
856 1
        if (m_position.has_more() && char_in_alphabet(*m_position, detail::float_alphabet) ) {
857
          try {
858 1
            if (Hex_()) {
859 1
              auto match = Position::str(start, m_position);
860 1
              auto bv = buildInt(16, match, true);
861 1
              m_match_stack.emplace_back(make_node<eval::Constant_AST_Node<Tracer>>(match, start.line, start.col, std::move(bv)));
862 1
              return true;
863
            }
864

865 1
            if (Binary_()) {
866 1
              auto match = Position::str(start, m_position);
867 1
              auto bv = buildInt(2, match, true);
868 1
              m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(match, start.line, start.col, std::move(bv)));
869 1
              return true;
870
            }
871 1
            if (Float_()) {
872 1
              auto match = Position::str(start, m_position);
873 1
              auto bv = buildFloat(match);
874 1
              m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(match, start.line, start.col, std::move(bv)));
875 1
              return true;
876
            }
877
            else {
878 1
              IntSuffix_();
879 1
              auto match = Position::str(start, m_position);
880 1
              if (!match.empty() && (match[0] == '0')) {
881 1
                auto bv = buildInt(8, match, false);
882 1
                m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(match, start.line, start.col, std::move(bv)));
883
              }
884 1
              else if (!match.empty()) {
885 1
                auto bv = buildInt(10, match, false);
886 1
                m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(match, start.line, start.col, std::move(bv)));
887
              } else {
888 1
                return false;
889
              }
890 1
              return true;
891
            }
892 1
          } catch (const std::invalid_argument &) {
893
            // error parsing number passed in to buildFloat/buildInt
894 1
            return false;
895
          }
896
        }
897
        else {
898 1
          return false;
899
        }
900
      }
901

902
      /// Reads an identifier from input which conforms to C's identifier naming conventions, without skipping initial whitespace
903 1
      bool Id_() {
904 1
        if (m_position.has_more() && char_in_alphabet(*m_position, detail::id_alphabet)) {
905 1
          while (m_position.has_more() && char_in_alphabet(*m_position, detail::keyword_alphabet) ) {
906 1
            ++m_position;
907
          }
908

909 1
          return true;
910 1
        } else if (m_position.has_more() && (*m_position == '`')) {
911 1
          ++m_position;
912 1
          const auto start = m_position;
913

914 1
          while (m_position.has_more() && (*m_position != '`')) {
915 1
            if (Eol()) {
916 1
              throw exception::eval_error("Carriage return in identifier literal", File_Position(m_position.line, m_position.col), *m_filename);
917
            }
918
            else {
919 1
              ++m_position;
920
            }
921
          }
922

923 1
          if (start == m_position) {
924 1
            throw exception::eval_error("Missing contents of identifier literal", File_Position(m_position.line, m_position.col), *m_filename);
925
          }
926 1
          else if (!m_position.has_more()) {
927 1
            throw exception::eval_error("Incomplete identifier literal", File_Position(m_position.line, m_position.col), *m_filename);
928
          }
929

930 1
          ++m_position;
931

932 1
          return true;
933
        }
934 1
        return false;
935
      }
936

937
      /// Reads (and potentially captures) an identifier from input
938 1
      bool Id(const bool validate) {
939 1
        SkipWS();
940

941 1
        const auto start = m_position;
942 1
        if (Id_()) {
943

944 1
          auto text = Position::str(start, m_position);
945 1
          const auto text_hash = utility::hash(text);
946

947 1
          if (validate) {
948 1
            validate_object_name(text);
949
          }
950

951
#ifdef CHAISCRIPT_MSVC
952
#pragma warning(push)
953
#pragma warning(disable : 4307)
954
#endif
955

956 1
          switch (text_hash) {
957 1
            case utility::hash("true"): {
958 1
              m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(text, start.line, start.col, const_var(true)));
959 1
            } break;
960 1
            case utility::hash("false"): {
961 1
              m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(text, start.line, start.col, const_var(false)));
962 1
            } break;
963 1
            case utility::hash("Infinity"): {
964 1
              m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(text, start.line, start.col,
965 1
                const_var(std::numeric_limits<double>::infinity())));
966 1
            } break;
967 0
            case utility::hash("NaN"): {
968 0
              m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(text, start.line, start.col,
969 0
                const_var(std::numeric_limits<double>::quiet_NaN())));
970 0
            } break;
971 1
            case utility::hash("__LINE__"): {
972 1
              m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(text, start.line, start.col,
973
                const_var(start.line)));
974 1
            } break;
975 1
            case utility::hash("__FILE__"): {
976 1
              m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(text, start.line, start.col,
977 1
                const_var(m_filename)));
978 1
            } break;
979 1
            case utility::hash("__FUNC__"): {
980 1
              std::string fun_name = "NOT_IN_FUNCTION";
981 1
              for (size_t idx = m_match_stack.empty() ? 0 : m_match_stack.size() - 1; idx > 0; --idx)
982
              {
983 1
                if (m_match_stack[idx-1]->identifier == AST_Node_Type::Id
984 1
                    && m_match_stack[idx-0]->identifier == AST_Node_Type::Arg_List) {
985 1
                  fun_name = m_match_stack[idx-1]->text;
986
                }
987
              }
988

989 1
              m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(text, start.line, start.col,
990 0
                const_var(fun_name)));
991 1
            } break;
992 1
            case utility::hash("__CLASS__"): {
993 1
              std::string fun_name = "NOT_IN_CLASS";
994 1
              for (size_t idx = m_match_stack.empty() ? 0 : m_match_stack.size() - 1; idx > 1; --idx)
995
              {
996 1
                if (m_match_stack[idx-2]->identifier == AST_Node_Type::Id
997 1
                    && m_match_stack[idx-1]->identifier == AST_Node_Type::Id
998 1
                    && m_match_stack[idx-0]->identifier == AST_Node_Type::Arg_List) {
999 1
                  fun_name = m_match_stack[idx-2]->text;
1000
                }
1001
              }
1002

1003 1
              m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(std::move(text), start.line, start.col,
1004 0
                const_var(fun_name)));
1005 1
            } break;
1006 1
            case utility::hash("_"): {
1007 1
              m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(std::move(text), start.line, start.col,
1008
                Boxed_Value(std::make_shared<dispatch::Placeholder_Object>())));
1009 1
            } break;
1010 1
            default: {
1011 1
              auto val = text;
1012 1
              if (*start == '`') {
1013
                // 'escaped' literal, like an operator name
1014 1
                val = Position::str(start+1, m_position-1);
1015
                // val.remove_prefix(1); val.remove_suffix(1);
1016
              }
1017 1
              m_match_stack.push_back(make_node<eval::Id_AST_Node<Tracer>>(val, start.line, start.col));
1018 1
            } break;
1019
          }
1020

1021
#ifdef CHAISCRIPT_MSVC
1022
#pragma warning(pop)
1023
#endif
1024

1025

1026 1
          return true;
1027
        } else {
1028 1
          return false;
1029
        }
1030
      }
1031

1032
      /// Reads an argument from input
1033 1
      bool Arg(const bool t_type_allowed = true) {
1034 1
        const auto prev_stack_top = m_match_stack.size();
1035 1
        SkipWS();
1036

1037 1
        if (!Id(true)) {
1038 1
          return false;
1039
        }
1040

1041 1
        SkipWS();
1042

1043 1
        if (t_type_allowed) {
1044 1
          Id(true);
1045
        }
1046

1047 1
        build_match<eval::Arg_AST_Node<Tracer>>(prev_stack_top);
1048

1049 1
        return true;
1050
      }
1051

1052

1053

1054
      /// Reads a quoted string from input, without skipping initial whitespace
1055 1
      bool Quoted_String_() {
1056 1
        if (m_position.has_more() && (*m_position == '\"')) {
1057 1
          char prev_char = *m_position;
1058 1
          ++m_position;
1059

1060 1
          int in_interpolation = 0;
1061 1
          bool in_quote = false;
1062

1063 1
          while (m_position.has_more() && ((*m_position != '\"') || (in_interpolation > 0) || (prev_char == '\\'))) {
1064

1065 1
            if (!Eol_()) {
1066 1
              if (prev_char == '$' && *m_position == '{') {
1067 1
                ++in_interpolation;
1068 1
              } else if (prev_char != '\\' && *m_position == '"') {
1069 1
                in_quote = !in_quote;
1070 1
              } else if (*m_position == '}' && !in_quote) {
1071 1
                --in_interpolation;
1072
              }
1073

1074 1
              if (prev_char == '\\') {
1075 1
                prev_char = 0;
1076
              } else {
1077 1
                prev_char = *m_position;
1078
              }
1079 1
              ++m_position;
1080
            }
1081
          }
1082

1083 1
          if (m_position.has_more()) {
1084 1
            ++m_position;
1085
          } else {
1086 1
            throw exception::eval_error("Unclosed quoted string", File_Position(m_position.line, m_position.col), *m_filename);
1087
          }
1088

1089 1
          return true;
1090
        }
1091 1
        return false;
1092
      }
1093

1094
      template<typename string_type>
1095
      struct Char_Parser
1096
      {
1097
        string_type &match;
1098
        using char_type = typename string_type::value_type;
1099
        bool is_escaped = false;
1100
        bool is_interpolated = false;
1101
        bool saw_interpolation_marker = false;
1102
        bool is_octal = false;
1103
        bool is_hex = false;
1104
        std::size_t unicode_size = 0;
1105
        const bool interpolation_allowed;
1106

1107
        string_type octal_matches;
1108
        string_type hex_matches;
1109

1110 1
        Char_Parser(string_type &t_match, const bool t_interpolation_allowed)
1111
          : match(t_match),
1112 1
            interpolation_allowed(t_interpolation_allowed)
1113
        {
1114
        }
1115

1116
        Char_Parser &operator=(const Char_Parser &) = delete;
1117

1118 1
        ~Char_Parser(){
1119
          try {
1120 1
            if (is_octal) {
1121 1
              process_octal();
1122
            }
1123

1124 1
            if (is_hex) {
1125 1
              process_hex();
1126
            }
1127

1128 1
            if (unicode_size > 0) {
1129 1
              process_unicode();
1130
            }
1131 1
          } catch (const std::invalid_argument &) {
1132 1
          } catch (const exception::eval_error &) {
1133
            // Something happened with parsing, we'll catch it later?
1134
          }
1135
        }
1136

1137 1
        void process_hex()
1138
        {
1139 1
          if (!hex_matches.empty()) {
1140 1
            auto val = stoll(hex_matches, nullptr, 16);
1141 1
            match.push_back(char_type(val));
1142
          }
1143 1
          hex_matches.clear();
1144 1
          is_escaped = false;
1145 1
          is_hex = false;
1146
        }
1147

1148

1149 1
        void process_octal()
1150
        {
1151 1
          if (!octal_matches.empty()) {
1152 1
            auto val = stoll(octal_matches, nullptr, 8);
1153 1
            match.push_back(char_type(val));
1154
          }
1155 1
          octal_matches.clear();
1156 1
          is_escaped = false;
1157 1
          is_octal = false;
1158
        }
1159

1160

1161 1
        void process_unicode()
1162
        {
1163 1
          const auto ch = static_cast<uint32_t>(std::stoi(hex_matches, nullptr, 16));
1164 1
          const auto match_size = hex_matches.size();
1165 1
          hex_matches.clear();
1166 1
          is_escaped = false;
1167 1
          const auto u_size = unicode_size;
1168 1
          unicode_size = 0;
1169

1170
          char buf[4];
1171 1
          if (u_size != match_size) {
1172 1
            throw exception::eval_error("Incomplete unicode escape sequence");
1173
          }
1174 1
          if (u_size == 4 && ch >= 0xD800 && ch <= 0xDFFF) {
1175 1
            throw exception::eval_error("Invalid 16 bit universal character");
1176
          }
1177

1178

1179 1
          if (ch < 0x80) {
1180 1
            match += static_cast<char>(ch);
1181 1
          } else if (ch < 0x800) {
1182 1
            buf[0] = static_cast<char>(0xC0 | (ch >> 6));
1183 1
            buf[1] = static_cast<char>(0x80 | (ch & 0x3F));
1184 1
            match.append(buf, 2);
1185 1
          } else if (ch < 0x10000) {
1186 1
            buf[0] = static_cast<char>(0xE0 |  (ch >> 12));
1187 1
            buf[1] = static_cast<char>(0x80 | ((ch >>  6) & 0x3F));
1188 1
            buf[2] = static_cast<char>(0x80 |  (ch        & 0x3F));
1189 1
            match.append(buf, 3);
1190 1
          } else if (ch < 0x200000) {
1191 1
            buf[0] = static_cast<char>(0xF0 |  (ch >> 18));
1192 1
            buf[1] = static_cast<char>(0x80 | ((ch >> 12) & 0x3F));
1193 1
            buf[2] = static_cast<char>(0x80 | ((ch >>  6) & 0x3F));
1194 1
            buf[3] = static_cast<char>(0x80 |  (ch        & 0x3F));
1195 1
            match.append(buf, 4);
1196
          } else {
1197
            // this must be an invalid escape sequence?
1198 0
            throw exception::eval_error("Invalid 32 bit universal character");
1199
          }
1200
        }
1201

1202 1
        void parse(const char_type t_char, const int line, const int col, const std::string &filename) {
1203 1
          const bool is_octal_char = t_char >= '0' && t_char <= '7';
1204

1205 1
          const bool is_hex_char  = (t_char >= '0' && t_char <= '9')
1206 1
                                 || (t_char >= 'a' && t_char <= 'f')
1207 1
                                 || (t_char >= 'A' && t_char <= 'F');
1208

1209 1
          if (is_octal) {
1210 1
            if (is_octal_char) {
1211 1
              octal_matches.push_back(t_char);
1212

1213 1
              if (octal_matches.size() == 3) {
1214 1
                process_octal();
1215
              }
1216 1
              return;
1217
            } else {
1218 1
              process_octal();
1219
            }
1220 1
          } else if (is_hex) {
1221 1
            if (is_hex_char) {
1222 1
              hex_matches.push_back(t_char);
1223

1224 1
              if (hex_matches.size() == 2*sizeof(char_type)) {
1225
                // This rule differs from the C/C++ standard, but ChaiScript
1226
                // does not offer the same workaround options, and having
1227
                // hexadecimal sequences longer than can fit into the char
1228
                // type is undefined behavior anyway.
1229 1
                process_hex();
1230
              }
1231 1
              return;
1232
            } else {
1233 1
              process_hex();
1234
            }
1235 1
          } else if (unicode_size > 0) {
1236 1
            if (is_hex_char) {
1237 1
              hex_matches.push_back(t_char);
1238

1239 1
              if(hex_matches.size() == unicode_size) {
1240
                // Format is specified to be 'slash'uABCD
1241
                // on collecting from A to D do parsing
1242 1
                process_unicode();
1243
              }
1244 1
              return;
1245
            } else {
1246
              // Not a unicode anymore, try parsing any way
1247
              // May be someone used 'slash'uAA only
1248 1
              process_unicode();
1249
            }
1250
          }
1251

1252 1
          if (t_char == '\\') {
1253 1
            if (is_escaped) {
1254 1
              match.push_back('\\');
1255 1
              is_escaped = false;
1256
            } else {
1257 1
              is_escaped = true;
1258
            }
1259
          } else {
1260 1
            if (is_escaped) {
1261 1
              if (is_octal_char) {
1262 1
                is_octal = true;
1263 1
                octal_matches.push_back(t_char);
1264 1
              } else if (t_char == 'x') {
1265 1
                is_hex = true;
1266 1
              } else if (t_char == 'u') {
1267 1
                unicode_size = 4;
1268 1
              } else if (t_char == 'U') {
1269 1
                unicode_size = 8;
1270
              } else {
1271 1
                switch (t_char) {
1272 1
                  case ('\'') : match.push_back('\''); break;
1273 1
                  case ('\"') : match.push_back('\"'); break;
1274 0
                  case ('?') : match.push_back('?'); break;
1275 1
                  case ('a') : match.push_back('\a'); break;
1276 1
                  case ('b') : match.push_back('\b'); break;
1277 1
                  case ('f') : match.push_back('\f'); break;
1278 1
                  case ('n') : match.push_back('\n'); break;
1279 1
                  case ('r') : match.push_back('\r'); break;
1280 1
                  case ('t') : match.push_back('\t'); break;
1281 1
                  case ('v') : match.push_back('\v'); break;
1282 1
                  case ('$') : match.push_back('$'); break;
1283 1
                  default: throw exception::eval_error("Unknown escaped sequence in string", File_Position(line, col), filename);
1284
                }
1285 1
                is_escaped = false;
1286
              }
1287 1
            } else if (interpolation_allowed && t_char == '$') {
1288 1
              saw_interpolation_marker = true;
1289
            } else {
1290 1
              match.push_back(t_char);
1291
            }
1292
          }
1293
        }
1294

1295
      };
1296

1297

1298
      /// Reads (and potentially captures) a quoted string from input.  Translates escaped sequences.
1299 1
      bool Quoted_String() {
1300 1
        Depth_Counter dc{this};
1301 1
        SkipWS();
1302

1303 1
        const auto start = m_position;
1304

1305 1
        if (Quoted_String_()) {
1306 1
          std::string match;
1307 1
          const auto prev_stack_top = m_match_stack.size();
1308

1309 1
          bool is_interpolated = [&]()->bool {
1310 1
            Char_Parser<std::string> cparser(match, true);
1311

1312

1313
            auto s = start + 1, end = m_position - 1;
1314

1315 1
            while (s != end) {
1316 1
              if (cparser.saw_interpolation_marker) {
1317
                if (*s == '{') {
1318
                  //We've found an interpolation point
1319

1320
                  m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(match, start.line, start.col, const_var(match)));
1321

1322
                  if (cparser.is_interpolated) {
1323
                    //If we've seen previous interpolation, add on instead of making a new one
1324
                    build_match<eval::Binary_Operator_AST_Node<Tracer>>(prev_stack_top, "+");
1325
                  }
1326

1327
                  //We've finished with the part of the string up to this point, so clear it
1328
                  match.clear();
1329

1330
                  std::string eval_match;
1331

1332
                  ++s;
1333
                  while ((s != end) && (*s != '}')) {
1334
                    eval_match.push_back(*s);
1335
                    ++s;
1336
                  }
1337

1338
                  if (*s == '}') {
1339
                    cparser.is_interpolated = true;
1340
                    ++s;
1341

1342
                    const auto tostr_stack_top = m_match_stack.size();
1343

1344
                    m_match_stack.push_back(make_node<eval::Id_AST_Node<Tracer>>("to_string", start.line, start.col));
1345

1346
                    const auto ev_stack_top = m_match_stack.size();
1347

1348
                    try {
1349
                      m_match_stack.push_back(parse_instr_eval(eval_match));
1350 1
                    } catch (const exception::eval_error &e) {
1351 1
                      throw exception::eval_error(e.what(), File_Position(start.line, start.col), *m_filename);
1352
                    }
1353

1354
                    build_match<eval::Arg_List_AST_Node<Tracer>>(ev_stack_top);
1355
                    build_match<eval::Fun_Call_AST_Node<Tracer>>(tostr_stack_top);
1356
                    build_match<eval::Binary_Operator_AST_Node<Tracer>>(prev_stack_top, "+");
1357
                  } else {
1358
                    throw exception::eval_error("Unclosed in-string eval", File_Position(start.line, start.col), *m_filename);
1359
                  }
1360
                } else {
1361
                  match.push_back('$');
1362
                }
1363
                cparser.saw_interpolation_marker = false;
1364
              } else {
1365 1
                cparser.parse(*s, start.line, start.col, *m_filename);
1366

1367 1
                ++s;
1368
              }
1369
            }
1370

1371 1
            return cparser.is_interpolated;
1372 1
          }();
1373

1374 1
          m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(match, start.line, start.col, const_var(match)));
1375

1376 1
          if (is_interpolated) {
1377 1
            build_match<eval::Binary_Operator_AST_Node<Tracer>>(prev_stack_top, "+");
1378
          }
1379

1380 1
          return true;
1381
        } else {
1382 1
          return false;
1383
        }
1384
      }
1385

1386
      /// Reads a character group from input, without skipping initial whitespace
1387 1
      bool Single_Quoted_String_() {
1388 1
        bool retval = false;
1389 1
        if (m_position.has_more() && (*m_position == '\'')) {
1390 1
          retval = true;
1391 1
          char prev_char = *m_position;
1392 1
          ++m_position;
1393

1394 1
          while (m_position.has_more() && ((*m_position != '\'') || (prev_char == '\\'))) {
1395 1
            if (!Eol_()) {
1396 1
              if (prev_char == '\\') {
1397 1
                prev_char = 0;
1398
              } else {
1399 1
                prev_char = *m_position;
1400
              }
1401 1
              ++m_position;
1402
            }
1403
          }
1404

1405 1
          if (m_position.has_more()) {
1406 1
            ++m_position;
1407
          } else {
1408 1
            throw exception::eval_error("Unclosed single-quoted string", File_Position(m_position.line, m_position.col), *m_filename);
1409
          }
1410
        }
1411 1
        return retval;
1412
      }
1413

1414
      /// Reads (and potentially captures) a char group from input.  Translates escaped sequences.
1415 1
      bool Single_Quoted_String() {
1416 1
        Depth_Counter dc{this};
1417 1
        SkipWS();
1418

1419 1
        const auto start = m_position;
1420 1
        if (Single_Quoted_String_()) {
1421 1
          std::string match;
1422

1423
          {
1424
            // scope for cparser destructor
1425 1
            Char_Parser<std::string> cparser(match, false);
1426

1427 1
            for (auto s = start + 1, end = m_position - 1; s != end; ++s) {
1428 1
              cparser.parse(*s, start.line, start.col, *m_filename);
1429
            }
1430
          }
1431

1432 1
          if (match.size() != 1) {
1433 1
            throw exception::eval_error("Single-quoted strings must be 1 character long", File_Position(m_position.line, m_position.col), *m_filename);
1434
          }
1435

1436 1
          m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(match, start.line, start.col, const_var(char(match.at(0)))));
1437 1
          return true;
1438
        }
1439
        else {
1440 1
          return false;
1441
        }
1442
      }
1443

1444
      /// Reads a char from input if it matches the parameter, without skipping initial whitespace
1445 1
      bool Char_(const char c) {
1446 1
        if (m_position.has_more() && (*m_position == c)) {
1447 1
          ++m_position;
1448 1
          return true;
1449
        } else {
1450 1
          return false;
1451
        }
1452
      }
1453

1454
      /// Reads (and potentially captures) a char from input if it matches the parameter
1455 1
      bool Char(const char t_c) {
1456 1
        Depth_Counter dc{this};
1457 1
        SkipWS();
1458 1
        return Char_(t_c);
1459
      }
1460

1461
      /// Reads a string from input if it matches the parameter, without skipping initial whitespace
1462 1
      bool Keyword_(const utility::Static_String &t_s) {
1463 1
        const auto len = t_s.size();
1464 1
        if (m_position.remaining() >= len) {
1465 1
          auto tmp = m_position;
1466 1
          for (size_t i = 0; tmp.has_more() && i < len; ++i) {
1467 1
            if (*tmp != t_s.c_str()[i]) {
1468 1
              return false;
1469
            }
1470 1
            ++tmp;
1471
          }
1472 1
          m_position = tmp;
1473 1
          return true;
1474
        }
1475

1476 1
        return false;
1477
      }
1478

1479
      /// Reads (and potentially captures) a string from input if it matches the parameter
1480 1
      bool Keyword(const utility::Static_String &t_s) {
1481 1
        Depth_Counter dc{this};
1482 1
        SkipWS();
1483 1
        const auto start = m_position;
1484 1
        bool retval = Keyword_(t_s);
1485
        // ignore substring matches
1486 1
        if ( retval && m_position.has_more() && char_in_alphabet(*m_position, detail::keyword_alphabet) ) {
1487 1
          m_position = start;
1488 1
          retval = false;
1489
        }
1490

1491 1
        return retval;
1492
      }
1493

1494 1
      bool is_operator(const std::string_view &t_s) const noexcept {
1495 1
        return m_operator_matches.is_match(t_s);
1496
      }
1497

1498
      /// Reads (and potentially captures) a symbol group from input if it matches the parameter
1499 1
      bool Symbol(const utility::Static_String &t_s, const bool t_disallow_prevention=false) {
1500 1
        Depth_Counter dc{this};
1501 1
        SkipWS();
1502 1
        const auto start = m_position;
1503 1
        bool retval = Symbol_(t_s);
1504

1505
        // ignore substring matches
1506 1
        if (retval && m_position.has_more() && (t_disallow_prevention == false) && char_in_alphabet(*m_position,detail::symbol_alphabet)) {
1507 1
          if (*m_position != '=' && is_operator(Position::str(start, m_position)) && !is_operator(Position::str(start, m_position+1))) {
1508
            // don't throw this away, it's a good match and the next is not
1509
          } else {
1510 1
            m_position = start;
1511 1
            retval = false;
1512
          }
1513
        }
1514

1515 1
        return retval;
1516
      }
1517

1518
      /// Reads an end-of-line group from input, without skipping initial whitespace
1519 1
      bool Eol_(const bool t_eos = false) {
1520 1
        bool retval = false;
1521

1522 1
        if (m_position.has_more() && (Symbol_(m_cr_lf) || Char_('\n'))) {
1523 1
          retval = true;
1524
          //++m_position.line;
1525 1
          m_position.col = 1;
1526 1
        } else if (m_position.has_more() && !t_eos && Char_(';')) {
1527 1
          retval = true;
1528
        }
1529

1530 1
        return retval;
1531
      }
1532

1533
      /// Reads until the end of the current statement
1534 1
      bool Eos() {
1535 1
        Depth_Counter dc{this};
1536 1
        SkipWS();
1537

1538 1
        return Eol_(true);
1539
      }
1540

1541
      /// Reads (and potentially captures) an end-of-line group from input
1542 1
      bool Eol() {
1543 1
        Depth_Counter dc{this};
1544 1
        SkipWS();
1545

1546 1
        return Eol_();
1547
      }
1548

1549
      /// Reads a comma-separated list of values from input. Id's only, no types allowed
1550 1
      bool Id_Arg_List() {
1551 1
        Depth_Counter dc{this};
1552 1
        SkipWS(true);
1553 1
        bool retval = false;
1554

1555 1
        const auto prev_stack_top = m_match_stack.size();
1556

1557 1
        if (Arg(false)) {
1558 1
          retval = true;
1559 1
          while (Eol()) {}
1560

1561
          while (Char(',')) {
1562 0
            while (Eol()) {}
1563 0
            if (!Arg(false)) {
1564 0
              throw exception::eval_error("Unexpected value in parameter list", File_Position(m_position.line, m_position.col), *m_filename);
1565
            }
1566
          } 
1567
        }
1568 1
        build_match<eval::Arg_List_AST_Node<Tracer>>(prev_stack_top);
1569

1570 1
        SkipWS(true);
1571

1572 1
        return retval;
1573
      }
1574

1575
      /// Reads a comma-separated list of values from input, for function declarations
1576 1
      bool Decl_Arg_List() {
1577 1
        Depth_Counter dc{this};
1578 1
        SkipWS(true);
1579 1
        bool retval = false;
1580

1581 1
        const auto prev_stack_top = m_match_stack.size();
1582

1583 1
        if (Arg()) {
1584 1
          retval = true;
1585 1
          while (Eol()) {}
1586

1587 1
          while (Char(',')) {
1588 1
            while (Eol()) {}
1589 1
            if (!Arg()) {
1590 1
              throw exception::eval_error("Unexpected value in parameter list", File_Position(m_position.line, m_position.col), *m_filename);
1591
            }
1592
          }
1593
        }
1594 1
        build_match<eval::Arg_List_AST_Node<Tracer>>(prev_stack_top);
1595

1596 1
        SkipWS(true);
1597

1598 1
        return retval;
1599
      }
1600

1601

1602
      /// Reads a comma-separated list of values from input
1603 1
      bool Arg_List() {
1604 1
        Depth_Counter dc{this};
1605 1
        SkipWS(true);
1606 1
        bool retval = false;
1607

1608 1
        const auto prev_stack_top = m_match_stack.size();
1609

1610 1
        if (Equation()) {
1611 1
          retval = true;
1612 1
          while (Eol()) {}
1613 1
          while (Char(',')) {
1614 1
            while (Eol()) {}
1615 1
            if (!Equation()) {
1616 1
              throw exception::eval_error("Unexpected value in parameter list", File_Position(m_position.line, m_position.col), *m_filename);
1617
            }
1618
          }
1619
        }
1620

1621 1
        build_match<eval::Arg_List_AST_Node<Tracer>>(prev_stack_top);
1622

1623 1
        SkipWS(true);
1624

1625 1
        return retval;
1626
      }
1627

1628
      /// Reads possible special container values, including ranges and map_pairs
1629 1
      bool Container_Arg_List() {
1630 1
        Depth_Counter dc{this};
1631 1
        bool retval = false;
1632 1
        SkipWS(true);
1633

1634 1
        const auto prev_stack_top = m_match_stack.size();
1635

1636 1
        if (Value_Range()) {
1637 1
          retval = true;
1638 1
          build_match<eval::Arg_List_AST_Node<Tracer>>(prev_stack_top);
1639 1
        } else if (Map_Pair()) {
1640 1
          retval = true;
1641 1
          while (Eol()) {}
1642 1
          while (Char(',')) {
1643 1
            while (Eol()) {}
1644 1
            if (!Map_Pair()) {
1645 1
              throw exception::eval_error("Unexpected value in container", File_Position(m_position.line, m_position.col), *m_filename);
1646
            }
1647
          }
1648 1
          build_match<eval::Arg_List_AST_Node<Tracer>>(prev_stack_top);
1649 1
        } else if (Operator()) {
1650 1
          retval = true;
1651 1
          while (Eol()) {}
1652 1
          while (Char(',')) {
1653 1
            while (Eol()) {}
1654 1
            if (!Operator()) {
1655 1
              throw exception::eval_error("Unexpected value in container", File_Position(m_position.line, m_position.col), *m_filename);
1656
            }
1657
          }
1658 1
          build_match<eval::Arg_List_AST_Node<Tracer>>(prev_stack_top);
1659
        }
1660

1661 1
        SkipWS(true);
1662

1663 1
        return retval;
1664
      }
1665

1666
      /// Reads a lambda (anonymous function) from input
1667 1
      bool Lambda() {
1668 1
        Depth_Counter dc{this};
1669 1
        bool retval = false;
1670

1671 1
        const auto prev_stack_top = m_match_stack.size();
1672

1673 1
        if (Keyword("fun")) {
1674 1
          retval = true;
1675

1676 1
          if (Char('[')) {
1677 1
            Id_Arg_List();
1678 1
            if (!Char(']')) {
1679 1
              throw exception::eval_error("Incomplete anonymous function bind", File_Position(m_position.line, m_position.col), *m_filename);
1680
            }
1681
          } else {
1682
            // make sure we always have the same number of nodes
1683 1
            build_match<eval::Arg_List_AST_Node<Tracer>>(prev_stack_top);
1684
          }
1685

1686 1
          if (Char('(')) {
1687 1
            Decl_Arg_List();
1688 1
            if (!Char(')')) {
1689 1
              throw exception::eval_error("Incomplete anonymous function", File_Position(m_position.line, m_position.col), *m_filename);
1690
            }
1691
          } else {
1692 1
            throw exception::eval_error("Incomplete anonymous function", File_Position(m_position.line, m_position.col), *m_filename);
1693
          }
1694

1695

1696 1
          while (Eol()) {}
1697

1698 1
          if (!Block()) {
1699 1
            throw exception::eval_error("Incomplete anonymous function", File_Position(m_position.line, m_position.col), *m_filename);
1700
          }
1701

1702 1
          build_match<eval::Lambda_AST_Node<Tracer>>(prev_stack_top);
1703
        }
1704

1705 1
        return retval;
1706
      }
1707

1708
      /// Reads a function definition from input
1709 1
      bool Def(const bool t_class_context = false, const std::string &t_class_name = "") {
1710 1
        Depth_Counter dc{this};
1711 1
        bool retval = false;
1712

1713 1
        const auto prev_stack_top = m_match_stack.size();
1714

1715 1
        if (Keyword("def")) {
1716 1
          retval = true;
1717

1718 1
          if (t_class_context) {
1719 1
            m_match_stack.push_back(make_node<eval::Id_AST_Node<Tracer>>(t_class_name, m_position.line, m_position.col));
1720
          }
1721

1722 1
          if (!Id(true)) {
1723 1
            throw exception::eval_error("Missing function name in definition", File_Position(m_position.line, m_position.col), *m_filename);
1724
          }
1725

1726 1
          bool is_method = false;
1727

1728 1
          if (Symbol("::")) {
1729
            //We're now a method
1730 1
            is_method = true;
1731

1732 1
            if (!Id(true)) {
1733 0
              throw exception::eval_error("Missing method name in definition", File_Position(m_position.line, m_position.col), *m_filename);
1734
            }
1735
          }
1736

1737 1
          if (Char('(')) {
1738 1
            Decl_Arg_List();
1739 1
            if (!Char(')')) {
1740 1
              throw exception::eval_error("Incomplete function definition", File_Position(m_position.line, m_position.col), *m_filename);
1741
            }
1742
          }
1743

1744 1
          while (Eos()) {}
1745

1746 1
          if (Char(':')) {
1747 1
            if (!Operator()) {
1748 1
              throw exception::eval_error("Missing guard expression for function", File_Position(m_position.line, m_position.col), *m_filename);
1749
            }
1750
          }
1751

1752 1
          while (Eol()) {}
1753 1
          if (!Block()) {
1754 1
            throw exception::eval_error("Incomplete function definition", File_Position(m_position.line, m_position.col), *m_filename);
1755
          }
1756

1757 1
          if (is_method || t_class_context) {
1758 1
            build_match<eval::Method_AST_Node<Tracer>>(prev_stack_top);
1759
          } else {
1760 1
            build_match<eval::Def_AST_Node<Tracer>>(prev_stack_top);
1761
          }
1762

1763
        }
1764

1765 1
        return retval;
1766
      }
1767

1768
      /// Reads a function definition from input
1769 1
      bool Try() {
1770 1
        Depth_Counter dc{this};
1771 1
        bool retval = false;
1772

1773 1
        const auto prev_stack_top = m_match_stack.size();
1774

1775 1
        if (Keyword("try")) {
1776 1
          retval = true;
1777

1778 1
          while (Eol()) {}
1779

1780 1
          if (!Block()) {
1781 1
            throw exception::eval_error("Incomplete 'try' block", File_Position(m_position.line, m_position.col), *m_filename);
1782
          }
1783

1784 1
          bool has_matches = true;
1785 1
          while (has_matches) {
1786 1
            while (Eol()) {}
1787 1
            has_matches = false;
1788 1
            if (Keyword("catch")) {
1789 1
              const auto catch_stack_top = m_match_stack.size();
1790 1
              if (Char('(')) {
1791 1
                if (!(Arg() && Char(')'))) {
1792 1
                  throw exception::eval_error("Incomplete 'catch' expression", File_Position(m_position.line, m_position.col), *m_filename);
1793
                }
1794
              }
1795

1796 1
              while (Eol()) {}
1797

1798 1
              if (!Block()) {
1799 1
                throw exception::eval_error("Incomplete 'catch' block", File_Position(m_position.line, m_position.col), *m_filename);
1800
              }
1801 1
              build_match<eval::Catch_AST_Node<Tracer>>(catch_stack_top);
1802 1
              has_matches = true;
1803
            }
1804
          }
1805 0
          while (Eol()) {}
1806 1
          if (Keyword("finally")) {
1807 1
            const auto finally_stack_top = m_match_stack.size();
1808

1809 1
            while (Eol()) {}
1810

1811 1
            if (!Block()) {
1812 1
              throw exception::eval_error("Incomplete 'finally' block", File_Position(m_position.line, m_position.col), *m_filename);
1813
            }
1814 1
            build_match<eval::Finally_AST_Node<Tracer>>(finally_stack_top);
1815
          }
1816

1817 1
          build_match<eval::Try_AST_Node<Tracer>>(prev_stack_top);
1818
        }
1819

1820 1
        return retval;
1821
      }
1822

1823
      /// Reads an if/else if/else block from input
1824 1
      bool If() {
1825 1
        Depth_Counter dc{this};
1826 1
        bool retval = false;
1827

1828 1
        const auto prev_stack_top = m_match_stack.size();
1829

1830 1
        if (Keyword("if")) {
1831 1
          retval = true;
1832

1833 1
          if (!Char('(')) {
1834 1
            throw exception::eval_error("Incomplete 'if' expression", File_Position(m_position.line, m_position.col), *m_filename);
1835
          }
1836

1837 1
          if (!Equation()) {
1838 1
            throw exception::eval_error("Incomplete 'if' expression", File_Position(m_position.line, m_position.col), *m_filename);
1839
          }
1840

1841 1
          const bool is_if_init = Eol() && Equation();
1842

1843 1
          if (!Char(')')) {
1844 1
            throw exception::eval_error("Incomplete 'if' expression", File_Position(m_position.line, m_position.col), *m_filename);
1845
          }
1846

1847 1
          while (Eol()) {}
1848

1849 1
          if (!Block()) {
1850 1
            throw exception::eval_error("Incomplete 'if' block", File_Position(m_position.line, m_position.col), *m_filename);
1851
          }
1852

1853 1
          bool has_matches = true;
1854 1
          while (has_matches) {
1855 1
            while (Eol()) {}
1856 1
            has_matches = false;
1857 1
            if (Keyword("else")) {
1858 1
              if (If()) {
1859 1
                has_matches = true;
1860
              } else {
1861 1
                while (Eol()) {}
1862

1863 1
                if (!Block()) {
1864 1
                  throw exception::eval_error("Incomplete 'else' block", File_Position(m_position.line, m_position.col), *m_filename);
1865
                }
1866 1
                has_matches = true;
1867
              }
1868
            }
1869
          }
1870

1871 1
          const auto num_children = m_match_stack.size() - prev_stack_top;
1872

1873 1
          if ((is_if_init && num_children == 3)
1874 1
              || (!is_if_init && num_children == 2)) {
1875 1
            m_match_stack.push_back(chaiscript::make_unique<eval::AST_Node_Impl<Tracer>, eval::Noop_AST_Node<Tracer>>());
1876
          }
1877

1878 1
          if (!is_if_init) {
1879 1
            build_match<eval::If_AST_Node<Tracer>>(prev_stack_top);
1880
          } else {
1881 1
            build_match<eval::If_AST_Node<Tracer>>(prev_stack_top+1);
1882 1
            build_match<eval::Block_AST_Node<Tracer>>(prev_stack_top);
1883
          }
1884
        }
1885

1886 1
        return retval;
1887
      }
1888

1889
      /// Reads a class block from input
1890 1
      bool Class(const bool t_class_allowed) {
1891 1
        Depth_Counter dc{this};
1892 1
        bool retval = false;
1893

1894 1
        size_t prev_stack_top = m_match_stack.size();
1895

1896 1
        if (Keyword("class")) {
1897 1
          if (!t_class_allowed) {
1898 1
            throw exception::eval_error("Class definitions only allowed at top scope", File_Position(m_position.line, m_position.col), *m_filename);
1899
          }
1900

1901 1
          retval = true;
1902

1903 1
          if (!Id(true)) {
1904 1
            throw exception::eval_error("Missing class name in definition", File_Position(m_position.line, m_position.col), *m_filename);
1905
          }
1906

1907 1
          const auto class_name = m_match_stack.back()->text;
1908

1909 1
          while (Eol()) {}
1910

1911 1
          if (!Class_Block(class_name)) {
1912 1
            throw exception::eval_error("Incomplete 'class' block", File_Position(m_position.line, m_position.col), *m_filename);
1913
          }
1914

1915 1
          build_match<eval::Class_AST_Node<Tracer>>(prev_stack_top);
1916
        }
1917

1918 1
        return retval;
1919
      }
1920

1921

1922
      /// Reads a while block from input
1923 1
      bool While() {
1924 1
        Depth_Counter dc{this};
1925 1
        bool retval = false;
1926

1927 1
        const auto prev_stack_top = m_match_stack.size();
1928

1929 1
        if (Keyword("while")) {
1930 1
          retval = true;
1931

1932 1
          if (!Char('(')) {
1933 1
            throw exception::eval_error("Incomplete 'while' expression", File_Position(m_position.line, m_position.col), *m_filename);
1934
          }
1935

1936 1
          if (!(Operator() && Char(')'))) {
1937 1
            throw exception::eval_error("Incomplete 'while' expression", File_Position(m_position.line, m_position.col), *m_filename);
1938
          }
1939

1940 1
          while (Eol()) {}
1941

1942 1
          if (!Block()) {
1943 1
            throw exception::eval_error("Incomplete 'while' block", File_Position(m_position.line, m_position.col), *m_filename);
1944
          }
1945

1946 1
          build_match<eval::While_AST_Node<Tracer>>(prev_stack_top);
1947
        }
1948

1949 1
        return retval;
1950
      }
1951

1952
      /// Reads the ranged `for` conditions from input
1953 1
      bool Range_Expression() {
1954 1
        Depth_Counter dc{this};
1955
        // the first element will have already been captured by the For_Guards() call that preceeds it
1956 1
        return Char(':') && Equation();
1957
      }
1958

1959

1960
      /// Reads the C-style `for` conditions from input
1961 1
      bool For_Guards() {
1962 1
        Depth_Counter dc{this};
1963 1
        if (!(Equation() && Eol()))
1964
        {
1965 1
          if (!Eol())
1966
          {
1967 1
            return false;
1968
          } else {
1969 1
            m_match_stack.push_back(chaiscript::make_unique<eval::AST_Node_Impl<Tracer>, eval::Noop_AST_Node<Tracer>>());
1970
          }
1971
        }
1972

1973 1
        if (!(Equation() && Eol()))
1974
        {
1975 1
          if (!Eol())
1976
          {
1977 1
            return false;
1978
          } else {
1979 1
            m_match_stack.push_back(chaiscript::make_unique<eval::AST_Node_Impl<Tracer>, eval::Constant_AST_Node<Tracer>>(Boxed_Value(true)));
1980
          }
1981
        }
1982

1983 1
        if (!Equation())
1984
        {
1985 1
          m_match_stack.push_back(chaiscript::make_unique<eval::AST_Node_Impl<Tracer>, eval::Noop_AST_Node<Tracer>>());
1986
        }
1987

1988 1
        return true; 
1989
      }
1990

1991

1992
      /// Reads a for block from input
1993 1
      bool For() {
1994 1
        Depth_Counter dc{this};
1995 1
        bool retval = false;
1996

1997 1
        const auto prev_stack_top = m_match_stack.size();
1998

1999 1
        if (Keyword("for")) {
2000 1
          retval = true;
2001

2002 1
          if (!Char('(')) {
2003 1
            throw exception::eval_error("Incomplete 'for' expression", File_Position(m_position.line, m_position.col), *m_filename);
2004
          }
2005

2006 1
          const bool classic_for = For_Guards() && Char(')');
2007 1
          if (!classic_for && !(Range_Expression() && Char(')'))) {
2008 1
            throw exception::eval_error("Incomplete 'for' expression", File_Position(m_position.line, m_position.col), *m_filename);
2009
          }
2010

2011 1
          while (Eol()) {}
2012

2013 1
          if (!Block()) {
2014 1
            throw exception::eval_error("Incomplete 'for' block", File_Position(m_position.line, m_position.col), *m_filename);
2015
          }
2016

2017 1
          const auto num_children = m_match_stack.size() - prev_stack_top;
2018

2019 1
          if (classic_for) {
2020 1
            if (num_children != 4) {
2021 0
              throw exception::eval_error("Incomplete 'for' expression", File_Position(m_position.line, m_position.col), *m_filename);
2022
            }
2023 1
            build_match<eval::For_AST_Node<Tracer>>(prev_stack_top);
2024
          } else {
2025 1
            if (num_children != 3) {
2026 0
              throw exception::eval_error("Incomplete ranged-for expression", File_Position(m_position.line, m_position.col), *m_filename);
2027
            }
2028 1
            build_match<eval::Ranged_For_AST_Node<Tracer>>(prev_stack_top);
2029
          }
2030
        }
2031

2032 1
        return retval;
2033
      }
2034

2035

2036
      /// Reads a case block from input
2037 1
      bool Case() {
2038 1
        Depth_Counter dc{this};
2039 1
        bool retval = false;
2040

2041 1
        const auto prev_stack_top = m_match_stack.size();
2042

2043 1
        if (Keyword("case")) {
2044 1
          retval = true;
2045

2046 1
          if (!Char('(')) {
2047 1
            throw exception::eval_error("Incomplete 'case' expression", File_Position(m_position.line, m_position.col), *m_filename);
2048
          }
2049

2050 1
          if (!(Operator() && Char(')'))) {
2051 1
            throw exception::eval_error("Incomplete 'case' expression", File_Position(m_position.line, m_position.col), *m_filename);
2052
          }
2053

2054 1
          while (Eol()) {}
2055

2056 1
          if (!Block()) {
2057 1
            throw exception::eval_error("Incomplete 'case' block", File_Position(m_position.line, m_position.col), *m_filename);
2058
          }
2059

2060 1
          build_match<eval::Case_AST_Node<Tracer>>(prev_stack_top);
2061 1
        } else if (Keyword("default")) {
2062 1
          retval = true;
2063

2064 1
          while (Eol()) {}
2065

2066 1
          if (!Block()) {
2067 1
            throw exception::eval_error("Incomplete 'default' block", File_Position(m_position.line, m_position.col), *m_filename);
2068
          }
2069

2070 1
          build_match<eval::Default_AST_Node<Tracer>>(prev_stack_top);
2071
        }
2072

2073 1
        return retval;
2074
      }
2075

2076

2077
      /// Reads a switch statement from input
2078 1
      bool Switch() {
2079 1
        Depth_Counter dc{this};
2080 1
        const auto prev_stack_top = m_match_stack.size();
2081

2082 1
        if (Keyword("switch")) {
2083

2084 1
          if (!Char('(')) {
2085 0
            throw exception::eval_error("Incomplete 'switch' expression", File_Position(m_position.line, m_position.col), *m_filename);
2086
          }
2087

2088 1
          if (!(Operator() && Char(')'))) {
2089 1
            throw exception::eval_error("Incomplete 'switch' expression", File_Position(m_position.line, m_position.col), *m_filename);
2090
          }
2091

2092 1
          while (Eol()) {}
2093

2094 1
          if (Char('{')) {
2095 1
            while (Eol()) {}
2096

2097 1
            while (Case()) {
2098 1
              while (Eol()) { } // eat
2099
            }
2100

2101 0
            while (Eol()) { } // eat
2102

2103 1
            if (!Char('}')) {
2104 1
              throw exception::eval_error("Incomplete block", File_Position(m_position.line, m_position.col), *m_filename);
2105
            }
2106
          }
2107
          else {
2108 1
            throw exception::eval_error("Incomplete block", File_Position(m_position.line, m_position.col), *m_filename);
2109
          }
2110

2111 1
          build_match<eval::Switch_AST_Node<Tracer>>(prev_stack_top);
2112 1
          return true;
2113

2114
        } else {
2115 1
          return false;
2116
        }
2117

2118
      }
2119

2120

2121
      /// Reads a curly-brace C-style class block from input
2122 1
      bool Class_Block(const std::string &t_class_name) {
2123 1
        Depth_Counter dc{this};
2124 1
        bool retval = false;
2125

2126 1
        const auto prev_stack_top = m_match_stack.size();
2127

2128 1
        if (Char('{')) {
2129 1
          retval = true;
2130

2131 1
          Class_Statements(t_class_name);
2132 1
          if (!Char('}')) {
2133 1
            throw exception::eval_error("Incomplete class block", File_Position(m_position.line, m_position.col), *m_filename);
2134
          }
2135

2136 1
          if (m_match_stack.size() == prev_stack_top) {
2137 1
            m_match_stack.push_back(chaiscript::make_unique<eval::AST_Node_Impl<Tracer>, eval::Noop_AST_Node<Tracer>>());
2138
          }
2139

2140 1
          build_match<eval::Block_AST_Node<Tracer>>(prev_stack_top);
2141
        }
2142

2143 1
        return retval;
2144
      }
2145

2146
      /// Reads a curly-brace C-style block from input
2147 1
      bool Block() {
2148 1
        Depth_Counter dc{this};
2149 1
        bool retval = false;
2150

2151 1
        const auto prev_stack_top = m_match_stack.size();
2152

2153 1
        if (Char('{')) {
2154 1
          retval = true;
2155

2156 1
          Statements();
2157 1
          if (!Char('}')) {
2158 1
            throw exception::eval_error("Incomplete block", File_Position(m_position.line, m_position.col), *m_filename);
2159
          }
2160

2161 1
          if (m_match_stack.size() == prev_stack_top) {
2162 1
            m_match_stack.push_back(chaiscript::make_unique<eval::AST_Node_Impl<Tracer>, eval::Noop_AST_Node<Tracer>>());
2163
          }
2164

2165 1
          build_match<eval::Block_AST_Node<Tracer>>(prev_stack_top);
2166
        }
2167

2168 1
        return retval;
2169
      }
2170

2171
      /// Reads a return statement from input
2172 1
      bool Return() {
2173 1
        Depth_Counter dc{this};
2174 1
        const auto prev_stack_top = m_match_stack.size();
2175

2176 1
        if (Keyword("return")) {
2177 1
          Operator();
2178 1
          build_match<eval::Return_AST_Node<Tracer>>(prev_stack_top);
2179 1
          return true;
2180
        } else {
2181 1
          return false;
2182
        }
2183
      }
2184

2185
      /// Reads a break statement from input
2186 1
      bool Break() {
2187 1
        Depth_Counter dc{this};
2188 1
        const auto prev_stack_top = m_match_stack.size();
2189

2190 1
        if (Keyword("break")) {
2191 1
          build_match<eval::Break_AST_Node<Tracer>>(prev_stack_top);
2192 1
          return true;
2193
        } else {
2194 1
          return false;
2195
        }
2196
      }
2197

2198
      /// Reads a continue statement from input
2199 1
      bool Continue() {
2200 1
        Depth_Counter dc{this};
2201 1
        const auto prev_stack_top = m_match_stack.size();
2202

2203 1
        if (Keyword("continue")) {
2204 1
          build_match<eval::Continue_AST_Node<Tracer>>(prev_stack_top);
2205 1
          return true;
2206
        } else {
2207 1
          return false;
2208
        }
2209
      }
2210

2211
      /// Reads a dot expression(member access), then proceeds to check if it's a function or array call
2212 1
      bool Dot_Fun_Array() {
2213 1
        Depth_Counter dc{this};
2214 1
        bool retval = false;
2215

2216 1
        const auto prev_stack_top = m_match_stack.size();
2217 1
        if (Lambda() || Num() || Quoted_String() || Single_Quoted_String() ||
2218 1
            Paren_Expression() || Inline_Container() || Id(false))
2219
        {
2220 1
          retval = true;
2221 1
          bool has_more = true;
2222

2223 1
          while (has_more) {
2224 1
            has_more = false;
2225

2226 1
            if (Char('(')) {
2227 1
              has_more = true;
2228

2229 1
              Arg_List();
2230 1
              if (!Char(')')) {
2231 1
                throw exception::eval_error("Incomplete function call", File_Position(m_position.line, m_position.col), *m_filename);
2232
              }
2233

2234 1
              build_match<eval::Fun_Call_AST_Node<Tracer>>(prev_stack_top);
2235
              /// \todo Work around for method calls until we have a better solution
2236 1
              if (!m_match_stack.back()->children.empty()) {
2237 1
                if (m_match_stack.back()->children[0]->identifier == AST_Node_Type::Dot_Access) {
2238
                  if (m_match_stack.empty()) { throw exception::eval_error("Incomplete dot access fun call", File_Position(m_position.line, m_position.col), *m_filename);
2239
}
2240
                  if (m_match_stack.back()->children.empty()) { throw exception::eval_error("Incomplete dot access fun call", File_Position(m_position.line, m_position.col), *m_filename);
2241
}
2242 1
                  auto dot_access = std::move(m_match_stack.back()->children[0]);
2243 1
                  auto func_call = std::move(m_match_stack.back());
2244 1
                  m_match_stack.pop_back();
2245 1
                  func_call->children.erase(func_call->children.begin());
2246
                  if (dot_access->children.empty()) { throw exception::eval_error("Incomplete dot access fun call", File_Position(m_position.line, m_position.col), *m_filename);
2247
}
2248 1
                  func_call->children.insert(func_call->children.begin(), std::move(dot_access->children.back()));
2249 1
                  dot_access->children.pop_back();
2250 1
                  dot_access->children.push_back(std::move(func_call));
2251
                  if (dot_access->children.size() != 2) { throw exception::eval_error("Incomplete dot access fun call", File_Position(m_position.line, m_position.col), *m_filename);
2252
}
2253 1
                  m_match_stack.push_back(std::move(dot_access));
2254
                }
2255
              }
2256 1
            } else if (Char('[')) {
2257 1
              has_more = true;
2258

2259 1
              if (!(Operator() && Char(']'))) {
2260 1
                throw exception::eval_error("Incomplete array access", File_Position(m_position.line, m_position.col), *m_filename);
2261
              }
2262

2263 1
              build_match<eval::Array_Call_AST_Node<Tracer>>(prev_stack_top);
2264
            }
2265 1
            else if (Symbol(".")) {
2266 1
              has_more = true;
2267 1
              if (!(Id(true))) {
2268 1
                throw exception::eval_error("Incomplete dot access fun call", File_Position(m_position.line, m_position.col), *m_filename);
2269
              }
2270

2271 1
              if ( std::distance(m_match_stack.begin() + static_cast<int>(prev_stack_top), m_match_stack.end()) != 2) {
2272 0
                throw exception::eval_error("Incomplete dot access fun call", File_Position(m_position.line, m_position.col), *m_filename);
2273
              }
2274 1
              build_match<eval::Dot_Access_AST_Node<Tracer>>(prev_stack_top);
2275
            }
2276 1
            else if (Eol()) {
2277 1
              auto start = --m_position;
2278 1
              while (Eol()) {}
2279 1
              if (Symbol(".")) {
2280 1
                has_more = true;
2281 1
                --m_position;
2282
              } else {
2283 1
                m_position = start;
2284
              }
2285
            }
2286
          }
2287
        }
2288

2289 1
        return retval;
2290
      }
2291

2292
      /// Reads a variable declaration from input
2293 1
      bool Var_Decl(const bool t_class_context = false, const std::string &t_class_name = "") {
2294 1
        Depth_Counter dc{this};
2295 1
        bool retval = false;
2296

2297 1
        const auto prev_stack_top = m_match_stack.size();
2298

2299 1
        if (t_class_context && (Keyword("attr") || Keyword("auto") || Keyword("var"))) {
2300 1
          retval = true;
2301

2302 1
          m_match_stack.push_back(make_node<eval::Id_AST_Node<Tracer>>(t_class_name, m_position.line, m_position.col));
2303

2304 1
          if (!Id(true)) {
2305 0
            throw exception::eval_error("Incomplete attribute declaration", File_Position(m_position.line, m_position.col), *m_filename);
2306
          }
2307

2308 1
          build_match<eval::Attr_Decl_AST_Node<Tracer>>(prev_stack_top);
2309 1
        } else if (Keyword("auto") || Keyword("var") ) {
2310 1
          retval = true;
2311

2312 1
          if (Reference()) {
2313
            // we built a reference node - continue
2314 1
          } else if (Id(true)) {
2315 1
            build_match<eval::Var_Decl_AST_Node<Tracer>>(prev_stack_top);
2316
          } else {
2317 1
            throw exception::eval_error("Incomplete variable declaration", File_Position(m_position.line, m_position.col), *m_filename);
2318
          }
2319

2320 1
        } else if (Keyword("global")) {
2321 1
          retval = true;
2322

2323 1
          if (!(Reference() || Id(true))) {
2324 1
            throw exception::eval_error("Incomplete global declaration", File_Position(m_position.line, m_position.col), *m_filename);
2325
          }
2326

2327 1
          build_match<eval::Global_Decl_AST_Node<Tracer>>(prev_stack_top);
2328 1
        } else if (Keyword("attr")) {
2329 1
          retval = true;
2330

2331 1
          if (!Id(true)) {
2332 1
            throw exception::eval_error("Incomplete attribute declaration", File_Position(m_position.line, m_position.col), *m_filename);
2333
          }
2334 1
          if (!Symbol("::")) {
2335 1
            throw exception::eval_error("Incomplete attribute declaration", File_Position(m_position.line, m_position.col), *m_filename);
2336
          }
2337 1
          if (!Id(true)) {
2338 1
            throw exception::eval_error("Missing attribute name in definition", File_Position(m_position.line, m_position.col), *m_filename);
2339
          }
2340

2341

2342 1
          build_match<eval::Attr_Decl_AST_Node<Tracer>>(prev_stack_top);
2343
        }
2344

2345 1
        return retval;
2346
      }
2347

2348
      /// Reads an expression surrounded by parentheses from input
2349 1
      bool Paren_Expression() {
2350 1
        Depth_Counter dc{this};
2351 1
        if (Char('(')) {
2352 1
          if (!Operator()) {
2353 1
            throw exception::eval_error("Incomplete expression", File_Position(m_position.line, m_position.col), *m_filename);
2354
          }
2355 1
          if (!Char(')')) {
2356 1
            throw exception::eval_error("Missing closing parenthesis ')'", File_Position(m_position.line, m_position.col), *m_filename);
2357
          }
2358 1
          return true;
2359
        } else {
2360 1
          return false;
2361
        }
2362
      }
2363

2364
      /// Reads, and identifies, a short-form container initialization from input
2365 1
      bool Inline_Container() {
2366 1
        Depth_Counter dc{this};
2367 1
        const auto prev_stack_top = m_match_stack.size();
2368

2369 1
        if (Char('[')) {
2370 1
          Container_Arg_List();
2371

2372 1
          if (!Char(']')) {
2373 1
            throw exception::eval_error("Missing closing square bracket ']' in container initializer", File_Position(m_position.line, m_position.col), *m_filename);
2374
          }
2375 1
          if ((prev_stack_top != m_match_stack.size()) && (!m_match_stack.back()->children.empty())) {
2376 1
            if (m_match_stack.back()->children[0]->identifier == AST_Node_Type::Value_Range) {
2377 1
              build_match<eval::Inline_Range_AST_Node<Tracer>>(prev_stack_top);
2378
            }
2379 1
            else if (m_match_stack.back()->children[0]->identifier == AST_Node_Type::Map_Pair) {
2380 1
              build_match<eval::Inline_Map_AST_Node<Tracer>>(prev_stack_top);
2381
            }
2382
            else {
2383 1
              build_match<eval::Inline_Array_AST_Node<Tracer>>(prev_stack_top);
2384
            }
2385
          }
2386
          else {
2387 1
            build_match<eval::Inline_Array_AST_Node<Tracer>>(prev_stack_top);
2388
          }
2389

2390 1
          return true;
2391
        } else {
2392 1
          return false;
2393
        }
2394
      }
2395

2396
      /// Parses a variable specified with a & aka reference
2397 1
      bool Reference() {
2398 1
        Depth_Counter dc{this};
2399 1
        const auto prev_stack_top = m_match_stack.size();
2400

2401 1
        if (Symbol("&")) {
2402 1
          if (!Id(true)) {
2403 1
            throw exception::eval_error("Incomplete '&' expression", File_Position(m_position.line, m_position.col), *m_filename);
2404
          }
2405

2406 1
          build_match<eval::Reference_AST_Node<Tracer>>(prev_stack_top);
2407 1
          return true;
2408
        } else {
2409 1
          return false;
2410
        }
2411
      }
2412

2413
      /// Reads a unary prefixed expression from input
2414 1
      bool Prefix() {
2415 1
        Depth_Counter dc{this};
2416 1
        const auto prev_stack_top = m_match_stack.size();
2417
        using SS = utility::Static_String;
2418 1
        const std::array<utility::Static_String, 6> prefix_opers{{
2419
            SS{"++"}, 
2420
            SS{"--"}, 
2421
            SS{"-"}, 
2422
            SS{"+"}, 
2423
            SS{"!"}, 
2424
            SS{"~"}
2425
        }};
2426

2427 1
        for (const auto &oper : prefix_opers)
2428
        {
2429 1
          const bool is_char = oper.size() == 1;
2430 1
          if ((is_char && Char(oper.c_str()[0])) || (!is_char && Symbol(oper)))
2431
          {
2432 1
            if (!Operator(m_operators.size()-1)) {
2433 1
              throw exception::eval_error("Incomplete prefix '" + std::string(oper.c_str()) + "' expression", File_Position(m_position.line, m_position.col), *m_filename);
2434
            }
2435

2436 1
            build_match<eval::Prefix_AST_Node<Tracer>>(prev_stack_top, oper.c_str());
2437 1
            return true;
2438
          }
2439
        }
2440

2441 1
        return false;
2442
      }
2443

2444
      /// Parses any of a group of 'value' style ast_node groups from input
2445 1
      bool Value() {
2446 1
        Depth_Counter dc{this};
2447 1
        return Var_Decl() || Dot_Fun_Array() || Prefix();
2448
      }
2449

2450 1
      bool Operator_Helper(const size_t t_precedence, std::string &oper) {
2451
        return m_operator_matches.any_of(t_precedence,
2452 1
            [&oper, this](const auto &elem){ 
2453 1
              if (Symbol(elem)) {
2454 1
                oper = elem.c_str();
2455 1
                return true;
2456
              } else {
2457 1
                return false;
2458
              }
2459
            }
2460 1
          );
2461
      }
2462

2463

2464 1
      bool Operator(const size_t t_precedence = 0) {
2465 1
        Depth_Counter dc{this};
2466 1
        bool retval = false;
2467 1
        const auto prev_stack_top = m_match_stack.size();
2468

2469 1
        if (m_operators[t_precedence] != Operator_Precedence::Prefix) {
2470 1
          if (Operator(t_precedence+1)) {
2471 1
            retval = true;
2472 1
            std::string oper;
2473 1
            while (Operator_Helper(t_precedence, oper)) {
2474 1
              while (Eol()) {}
2475 1
              if (!Operator(t_precedence+1)) {
2476
                throw exception::eval_error("Incomplete '" + oper + "' expression",
2477 1
                    File_Position(m_position.line, m_position.col), *m_filename);
2478
              }
2479

2480 1
              switch (m_operators[t_precedence]) {
2481 1
                case(Operator_Precedence::Ternary_Cond) :
2482 1
                  if (Symbol(":")) {
2483 1
                    if (!Operator(t_precedence+1)) {
2484
                      throw exception::eval_error("Incomplete '" + oper + "' expression",
2485 1
                          File_Position(m_position.line, m_position.col), *m_filename);
2486
                    }
2487 1
                    build_match<eval::If_AST_Node<Tracer>>(prev_stack_top);
2488
                  }
2489
                  else {
2490
                    throw exception::eval_error("Incomplete '" + oper + "' expression",
2491 1
                        File_Position(m_position.line, m_position.col), *m_filename);
2492
                  }
2493 1
                  break;
2494

2495 1
                case(Operator_Precedence::Addition) :
2496
                case(Operator_Precedence::Multiplication) :
2497
                case(Operator_Precedence::Shift) :
2498
                case(Operator_Precedence::Equality) :
2499
                case(Operator_Precedence::Bitwise_And) :
2500
                case(Operator_Precedence::Bitwise_Xor) :
2501
                case(Operator_Precedence::Bitwise_Or) :
2502
                case(Operator_Precedence::Comparison) :
2503 1
                  build_match<eval::Binary_Operator_AST_Node<Tracer>>(prev_stack_top, oper);
2504 1
                  break;
2505

2506 1
                case(Operator_Precedence::Logical_And) :
2507 1
                  build_match<eval::Logical_And_AST_Node<Tracer>>(prev_stack_top, oper);
2508 1
                  break;
2509 1
                case(Operator_Precedence::Logical_Or) :
2510 1
                  build_match<eval::Logical_Or_AST_Node<Tracer>>(prev_stack_top, oper);
2511 1
                  break;
2512 0
                case(Operator_Precedence::Prefix) :
2513 0
                  assert(false); // cannot reach here because of if() statement at the top
2514
                  break;
2515

2516
//                default:
2517
//                  throw exception::eval_error("Internal error: unhandled ast_node", File_Position(m_position.line, m_position.col), *m_filename);
2518
              }
2519
            }
2520
          }
2521
        } else {
2522 1
          return Value();
2523
        }
2524

2525 1
        return retval;
2526
      }
2527

2528
      /// Reads a pair of values used to create a map initialization from input
2529 1
      bool Map_Pair() {
2530 1
        Depth_Counter dc{this};
2531 1
        bool retval = false;
2532

2533 1
        const auto prev_stack_top = m_match_stack.size();
2534 1
        const auto prev_pos = m_position;
2535

2536 1
        if (Operator()) {
2537 1
          if (Symbol(":")) {
2538 1
            retval = true;
2539 1
            if (!Operator()) {
2540 1
              throw exception::eval_error("Incomplete map pair", File_Position(m_position.line, m_position.col), *m_filename);
2541
            }
2542

2543 1
            build_match<eval::Map_Pair_AST_Node<Tracer>>(prev_stack_top);
2544
          }
2545
          else {
2546 1
            m_position = prev_pos;
2547 1
            while (prev_stack_top != m_match_stack.size()) {
2548 1
              m_match_stack.pop_back();
2549
            }
2550
          }
2551
        }
2552

2553 1
        return retval;
2554
      }
2555

2556
      /// Reads a pair of values used to create a range initialization from input
2557 1
      bool Value_Range() {
2558 1
        Depth_Counter dc{this};
2559 1
        bool retval = false;
2560

2561 1
        const auto prev_stack_top = m_match_stack.size();
2562 1
        const auto prev_pos = m_position;
2563

2564 1
        if (Operator()) {
2565 1
          if (Symbol("..")) {
2566 1
            retval = true;
2567 1
            if (!Operator()) {
2568 1
              throw exception::eval_error("Incomplete value range", File_Position(m_position.line, m_position.col), *m_filename);
2569
            }
2570

2571 1
            build_match<eval::Value_Range_AST_Node<Tracer>>(prev_stack_top);
2572
          }
2573
          else {
2574 1
            m_position = prev_pos;
2575 1
            while (prev_stack_top != m_match_stack.size()) {
2576 1
              m_match_stack.pop_back();
2577
            }
2578
          }
2579
        }
2580

2581 1
        return retval;
2582
      }
2583

2584
      /// Parses a string of binary equation operators
2585 1
      bool Equation() {
2586 1
        Depth_Counter dc{this};
2587 1
        const auto prev_stack_top = m_match_stack.size();
2588

2589
        using SS = utility::Static_String;
2590

2591 1
        if (Operator()) {
2592 1
          for (const auto &sym : {SS{"="}, SS{":="}, SS{"+="}, SS{"-="}, SS{"*="}, SS{"/="}, SS{"%="}, SS{"<<="}, SS{">>="}, SS{"&="}, SS{"^="}, SS{"|="}}) 
2593
          {
2594 1
            if (Symbol(sym, true)) {
2595 1
              SkipWS(true);
2596 1
              if (!Equation()) {
2597 1
                throw exception::eval_error("Incomplete equation", File_Position(m_position.line, m_position.col), *m_filename);
2598
              }
2599

2600 1
              build_match<eval::Equation_AST_Node<Tracer>>(prev_stack_top, sym.c_str());
2601 1
              return true;
2602
            }
2603
          }
2604 1
          return true;
2605
        }
2606

2607 1
        return false;
2608
      }
2609

2610
      /// Parses statements allowed inside of a class block
2611 1
      bool Class_Statements(const std::string &t_class_name) {
2612 1
        Depth_Counter dc{this};
2613 1
        bool retval = false;
2614

2615 1
        bool has_more = true;
2616 1
        bool saw_eol = true;
2617

2618 1
        while (has_more) {
2619 1
          const auto start = m_position;
2620 1
          if (Def(true, t_class_name) || Var_Decl(true, t_class_name)) {
2621 1
            if (!saw_eol) {
2622 0
              throw exception::eval_error("Two function definitions missing line separator", File_Position(start.line, start.col), *m_filename);
2623
            }
2624 1
            has_more = true;
2625 1
            retval = true;
2626 1
            saw_eol = true;
2627 1
          } else if (Eol()) {
2628 1
            has_more = true;
2629 1
            retval = true;
2630 1
            saw_eol = true;
2631
          } else {
2632 1
            has_more = false;
2633
          }
2634
        }
2635

2636 1
        return retval;
2637
      }
2638

2639
      /// Top level parser, starts parsing of all known parses
2640 1
      bool Statements(const bool t_class_allowed = false) {
2641 1
        Depth_Counter dc{this};
2642 1
        bool retval = false;
2643

2644 1
        bool has_more = true;
2645 1
        bool saw_eol = true;
2646

2647 1
        while (has_more) {
2648 1
          const auto start = m_position;
2649 1
          if (Def() || Try() || If() || While() || Class(t_class_allowed) || For() || Switch()) {
2650 1
            if (!saw_eol) {
2651 1
              throw exception::eval_error("Two function definitions missing line separator", File_Position(start.line, start.col), *m_filename);
2652
            }
2653 1
            has_more = true;
2654 1
            retval = true;
2655 1
            saw_eol = true;
2656
          }
2657 1
          else if (Return() || Break() || Continue() || Equation()) {
2658 1
            if (!saw_eol) {
2659 1
              throw exception::eval_error("Two expressions missing line separator", File_Position(start.line, start.col), *m_filename);
2660
            }
2661 1
            has_more = true;
2662 1
            retval = true;
2663 1
            saw_eol = false;
2664
          }
2665 1
          else if (Block() || Eol()) {
2666 1
            has_more = true;
2667 1
            retval = true;
2668 1
            saw_eol = true;
2669
          }
2670
          else {
2671 1
            has_more = false;
2672
          }
2673
        }
2674

2675 1
        return retval;
2676
      }
2677

2678 1
      AST_NodePtr parse(const std::string &t_input, const std::string &t_fname) override
2679
      {
2680 1
        ChaiScript_Parser<Tracer, Optimizer> parser(m_tracer, m_optimizer);
2681 1
        return parser.parse_internal(t_input, t_fname);
2682
      }
2683

2684 1
      eval::AST_Node_Impl_Ptr<Tracer> parse_instr_eval(const std::string &t_input)
2685
      {
2686 1
        auto last_position    = m_position;
2687 1
        auto last_filename    = m_filename;
2688 1
        auto last_match_stack = std::exchange(m_match_stack, decltype(m_match_stack){});
2689

2690 1
        auto retval = parse_internal(t_input, "instr eval");
2691

2692 1
        m_position = std::move(last_position);
2693 1
        m_filename = std::move(last_filename);
2694 1
        m_match_stack = std::move(last_match_stack);
2695

2696 0
        return eval::AST_Node_Impl_Ptr<Tracer>(dynamic_cast<eval::AST_Node_Impl<Tracer>*>(retval.release()));
2697
      }
2698

2699
      /// Parses the given input string, tagging parsed ast_nodes with the given m_filename.
2700 1
      AST_NodePtr parse_internal(const std::string &t_input, std::string t