]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*============================================================================= |
2 | Copyright (c) 2001-2014 Joel de Guzman | |
3 | Copyright (c) 2013-2014 Agustin Berge | |
4 | ||
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 | |
10 | ||
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> | |
19 | #include <memory> | |
20 | #include <string> | |
21 | ||
22 | namespace boost { namespace spirit { namespace x3 | |
23 | { | |
24 | template < | |
25 | typename Iterator | |
26 | , typename Attribute = unused_type | |
27 | , typename Context = subcontext<>> | |
28 | struct any_parser : parser<any_parser<Iterator, Attribute, Context>> | |
29 | { | |
30 | typedef Attribute attribute_type; | |
31 | ||
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; | |
36 | ||
37 | public: | |
38 | any_parser() | |
39 | : _content(nullptr) {} | |
40 | ||
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)) {} | |
45 | ||
46 | any_parser(any_parser const& other) | |
47 | : _content(other._content ? other._content->clone() : nullptr) {} | |
48 | ||
49 | any_parser(any_parser&& other) = default; | |
50 | ||
51 | any_parser& operator=(any_parser const& other) | |
52 | { | |
53 | _content.reset(other._content ? other._content->clone() : nullptr); | |
54 | return *this; | |
55 | } | |
56 | ||
57 | any_parser& operator=(any_parser&& other) = default; | |
58 | ||
59 | template <typename Iterator_, typename Context_> | |
60 | bool parse(Iterator_& first, Iterator_ const& last | |
61 | , Context_ const& context, unused_type, Attribute& attr) const | |
62 | { | |
63 | BOOST_STATIC_ASSERT_MSG( | |
64 | (is_same<Iterator, Iterator_>::value) | |
65 | , "Incompatible iterator used" | |
66 | ); | |
67 | ||
68 | BOOST_ASSERT_MSG( | |
69 | (_content != nullptr) | |
70 | , "Invalid use of uninitialized any_parser" | |
71 | ); | |
72 | ||
73 | return _content->parse(first, last, context, attr); | |
74 | } | |
75 | ||
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 | |
79 | { | |
80 | Attribute attr; | |
81 | if (parse(first, last, context, unused, attr)) | |
82 | { | |
83 | traits::move_to(attr, attr_); | |
84 | return true; | |
85 | } | |
86 | return false; | |
87 | } | |
88 | ||
89 | std::string get_info() const | |
90 | { | |
91 | return _content ? _content->get_info() : ""; | |
92 | } | |
93 | ||
94 | private: | |
95 | ||
96 | struct placeholder | |
97 | { | |
98 | virtual placeholder* clone() const = 0; | |
99 | ||
100 | virtual bool parse(Iterator& first, Iterator const& last | |
101 | , Context const& context, Attribute& attr) const = 0; | |
102 | ||
103 | virtual std::string get_info() const = 0; | |
104 | ||
105 | virtual ~placeholder() {} | |
106 | }; | |
107 | ||
108 | template <typename Expr> | |
109 | struct holder : placeholder | |
110 | { | |
111 | typedef typename extension::as_parser<Expr>::value_type parser_type; | |
112 | ||
113 | explicit holder(Expr const& p) | |
114 | : _parser(as_parser(p)) {} | |
115 | ||
116 | holder* clone() const override | |
117 | { | |
118 | return new holder(*this); | |
119 | } | |
120 | ||
121 | bool parse(Iterator& first, Iterator const& last | |
122 | , Context const& context, Attribute& attr) const override | |
123 | { | |
124 | return _parser.parse(first, last, context, unused, attr); | |
125 | } | |
126 | ||
127 | std::string get_info() const override | |
128 | { | |
129 | return x3::what(_parser); | |
130 | } | |
131 | ||
132 | parser_type _parser; | |
133 | }; | |
134 | ||
135 | private: | |
136 | std::unique_ptr<placeholder> _content; | |
137 | }; | |
138 | ||
139 | template <typename Iterator, typename Attribute, typename Context> | |
140 | struct get_info<any_parser<Iterator, Attribute, Context>> | |
141 | { | |
142 | typedef std::string result_type; | |
143 | std::string operator()( | |
144 | any_parser<Iterator, Attribute, Context> const& p) const | |
145 | { | |
146 | return p.get_info(); | |
147 | } | |
148 | }; | |
149 | }}} | |
150 | ||
151 | #endif |