1 /*=============================================================================
2 Copyright (c) 2001-2014 Joel de Guzman
3 Copyright (c) 2013-2014 Agustin Berge
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 ==============================================================================*/
8 #if !defined(BOOST_SPIRIT_X3_AUXILIARY_ANY_PARSER_APR_09_2014_1145PM)
9 #define BOOST_SPIRIT_X3_AUXILIARY_ANY_PARSER_APR_09_2014_1145PM
11 #include <boost/spirit/home/x3/core/parser.hpp>
12 #include <boost/spirit/home/x3/support/context.hpp>
13 #include <boost/spirit/home/x3/support/subcontext.hpp>
14 #include <boost/spirit/home/x3/support/unused.hpp>
15 #include <boost/spirit/home/x3/support/traits/container_traits.hpp>
16 #include <boost/spirit/home/x3/support/traits/has_attribute.hpp>
17 #include <boost/spirit/home/x3/support/traits/move_to.hpp>
18 #include <boost/spirit/home/x3/support/traits/is_parser.hpp>
22 namespace boost { namespace spirit { namespace x3
26 , typename Attribute = unused_type
27 , typename Context = subcontext<>>
28 struct any_parser : parser<any_parser<Iterator, Attribute, Context>>
30 typedef Attribute attribute_type;
32 static bool const has_attribute =
33 !is_same<unused_type, attribute_type>::value;
34 static bool const handles_container =
35 traits::is_container<Attribute>::value;
39 : _content(nullptr) {}
41 template <typename Expr,
42 typename Enable = typename enable_if<traits::is_parser<Expr>>::type>
43 any_parser(Expr const& expr)
44 : _content(new holder<Expr>(expr)) {}
46 any_parser(any_parser const& other)
47 : _content(other._content ? other._content->clone() : nullptr) {}
49 any_parser(any_parser&& other) = default;
51 any_parser& operator=(any_parser const& other)
53 _content.reset(other._content ? other._content->clone() : nullptr);
57 any_parser& operator=(any_parser&& other) = default;
59 template <typename Iterator_, typename Context_>
60 bool parse(Iterator_& first, Iterator_ const& last
61 , Context_ const& context, unused_type, Attribute& attr) const
63 BOOST_STATIC_ASSERT_MSG(
64 (is_same<Iterator, Iterator_>::value)
65 , "Incompatible iterator used"
70 , "Invalid use of uninitialized any_parser"
73 return _content->parse(first, last, context, attr);
76 template <typename Iterator_, typename Context_, typename Attribute_>
77 bool parse(Iterator_& first, Iterator_ const& last
78 , Context_ const& context, unused_type, Attribute_& attr_) const
81 if (parse(first, last, context, unused, attr))
83 traits::move_to(attr, attr_);
89 std::string get_info() const
91 return _content ? _content->get_info() : "";
98 virtual placeholder* clone() const = 0;
100 virtual bool parse(Iterator& first, Iterator const& last
101 , Context const& context, Attribute& attr) const = 0;
103 virtual std::string get_info() const = 0;
105 virtual ~placeholder() {}
108 template <typename Expr>
109 struct holder : placeholder
111 typedef typename extension::as_parser<Expr>::value_type parser_type;
113 explicit holder(Expr const& p)
114 : _parser(as_parser(p)) {}
116 holder* clone() const override
118 return new holder(*this);
121 bool parse(Iterator& first, Iterator const& last
122 , Context const& context, Attribute& attr) const override
124 return _parser.parse(first, last, context, unused, attr);
127 std::string get_info() const override
129 return x3::what(_parser);
136 std::unique_ptr<placeholder> _content;
139 template <typename Iterator, typename Attribute, typename Context>
140 struct get_info<any_parser<Iterator, Attribute, Context>>
142 typedef std::string result_type;
143 std::string operator()(
144 any_parser<Iterator, Attribute, Context> const& p) const