File indexing completed on 2025-01-31 10:02:34
0001
0002
0003
0004
0005
0006
0007
0008 #if !defined(BOOST_SPIRIT_X3_AUXILIARY_ANY_PARSER_APR_09_2014_1145PM)
0009 #define BOOST_SPIRIT_X3_AUXILIARY_ANY_PARSER_APR_09_2014_1145PM
0010
0011 #include <boost/spirit/home/x3/core/parser.hpp>
0012 #include <boost/spirit/home/x3/support/context.hpp>
0013 #include <boost/spirit/home/x3/support/subcontext.hpp>
0014 #include <boost/spirit/home/x3/support/unused.hpp>
0015 #include <boost/spirit/home/x3/support/traits/container_traits.hpp>
0016 #include <boost/spirit/home/x3/support/traits/has_attribute.hpp>
0017 #include <boost/spirit/home/x3/support/traits/move_to.hpp>
0018 #include <boost/spirit/home/x3/support/traits/is_parser.hpp>
0019 #include <memory>
0020 #include <string>
0021
0022 namespace boost { namespace spirit { namespace x3
0023 {
0024 template <
0025 typename Iterator
0026 , typename Attribute = unused_type
0027 , typename Context = subcontext<>>
0028 struct any_parser : parser<any_parser<Iterator, Attribute, Context>>
0029 {
0030 typedef Attribute attribute_type;
0031
0032 static bool const has_attribute =
0033 !is_same<unused_type, attribute_type>::value;
0034 static bool const handles_container =
0035 traits::is_container<Attribute>::value;
0036
0037 public:
0038 any_parser() = default;
0039
0040 template <typename Expr,
0041 typename Enable = typename enable_if<traits::is_parser<Expr>>::type>
0042 any_parser(Expr const& expr)
0043 : _content(new holder<Expr>(expr)) {}
0044
0045 any_parser(any_parser const& other)
0046 : _content(other._content ? other._content->clone() : nullptr) {}
0047
0048 any_parser(any_parser&& other) = default;
0049
0050 any_parser& operator=(any_parser const& other)
0051 {
0052 _content.reset(other._content ? other._content->clone() : nullptr);
0053 return *this;
0054 }
0055
0056 any_parser& operator=(any_parser&& other) = default;
0057
0058 template <typename Iterator_, typename Context_>
0059 bool parse(Iterator_& first, Iterator_ const& last
0060 , Context_ const& context, unused_type, Attribute& attr) const
0061 {
0062 BOOST_STATIC_ASSERT_MSG(
0063 (is_same<Iterator, Iterator_>::value)
0064 , "Incompatible iterator used"
0065 );
0066
0067 BOOST_ASSERT_MSG(
0068 (_content != nullptr)
0069 , "Invalid use of uninitialized any_parser"
0070 );
0071
0072 return _content->parse(first, last, context, attr);
0073 }
0074
0075 template <typename Iterator_, typename Context_, typename Attribute_>
0076 bool parse(Iterator_& first, Iterator_ const& last
0077 , Context_ const& context, unused_type, Attribute_& attr_) const
0078 {
0079 Attribute attr;
0080 if (parse(first, last, context, unused, attr))
0081 {
0082 traits::move_to(attr, attr_);
0083 return true;
0084 }
0085 return false;
0086 }
0087
0088 std::string get_info() const
0089 {
0090 return _content ? _content->get_info() : "";
0091 }
0092
0093 private:
0094
0095 struct placeholder
0096 {
0097 virtual placeholder* clone() const = 0;
0098
0099 virtual bool parse(Iterator& first, Iterator const& last
0100 , Context const& context, Attribute& attr) const = 0;
0101
0102 virtual std::string get_info() const = 0;
0103
0104 virtual ~placeholder() {}
0105 };
0106
0107 template <typename Expr>
0108 struct holder : placeholder
0109 {
0110 typedef typename extension::as_parser<Expr>::value_type parser_type;
0111
0112 explicit holder(Expr const& p)
0113 : _parser(as_parser(p)) {}
0114
0115 holder* clone() const override
0116 {
0117 return new holder(*this);
0118 }
0119
0120 bool parse(Iterator& first, Iterator const& last
0121 , Context const& context, Attribute& attr) const override
0122 {
0123 return _parser.parse(first, last, context, unused, attr);
0124 }
0125
0126 std::string get_info() const override
0127 {
0128 return x3::what(_parser);
0129 }
0130
0131 parser_type _parser;
0132 };
0133
0134 private:
0135 std::unique_ptr<placeholder> _content;
0136 };
0137
0138 template <typename Iterator, typename Attribute, typename Context>
0139 struct get_info<any_parser<Iterator, Attribute, Context>>
0140 {
0141 typedef std::string result_type;
0142 std::string operator()(
0143 any_parser<Iterator, Attribute, Context> const& p) const
0144 {
0145 return p.get_info();
0146 }
0147 };
0148 }}}
0149
0150 #endif