#pragma once #include "treeifier/constr.hh" #include "treeifier/parsers/helper.hh" #ifdef PROFILE_debug #include #endif namespace ppc::tree::parse { struct named_t { virtual std::string name() = 0; virtual ~named_t() = default; #ifdef PROFILE_debug virtual void print() { std::cout << "(" << name() << ")"; } #endif }; template class inspoint_t { private: std::map> parsers; public: inspoint_t &add(const ParserT &parser, bool replace = false) { const std::string &name = parser.name(); auto it = parsers.find(name); if (it != parsers.end()) { if (!replace) throw "The parser '" + name + "' is already in the group."; it->second = std::make_unique(parser); } else { parsers.emplace(name, std::make_unique(parser)); } return *this; } bool operator()(ppc::tree::parse_ctx_t &ctx, size_t &i, std::unique_ptr &out) const override { helper_t h(ctx, i); if (h.ended()) return false; for (std::pair> &pair : parsers) { if (pair.second(ctx, i, out)) return true; } return false; } }; }