Chris@102
|
1 /*=============================================================================
|
Chris@102
|
2 Copyright (c) 2001-2014 Joel de Guzman
|
Chris@102
|
3 Copyright (c) 2013-2014 Agustin Berge
|
Chris@102
|
4
|
Chris@102
|
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@102
|
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@102
|
7 ==============================================================================*/
|
Chris@102
|
8 #if !defined(BOOST_SPIRIT_X3_AUXILIARY_ANY_PARSER_APR_09_2014_1145PM)
|
Chris@102
|
9 #define BOOST_SPIRIT_X3_AUXILIARY_ANY_PARSER_APR_09_2014_1145PM
|
Chris@102
|
10
|
Chris@102
|
11 #if defined(_MSC_VER)
|
Chris@102
|
12 #pragma once
|
Chris@102
|
13 #endif
|
Chris@102
|
14
|
Chris@102
|
15 #include <boost/spirit/home/x3/core/parser.hpp>
|
Chris@102
|
16 #include <boost/spirit/home/x3/support/context.hpp>
|
Chris@102
|
17 #include <boost/spirit/home/x3/support/subcontext.hpp>
|
Chris@102
|
18 #include <boost/spirit/home/x3/support/unused.hpp>
|
Chris@102
|
19 #include <boost/spirit/home/x3/support/traits/container_traits.hpp>
|
Chris@102
|
20 #include <boost/spirit/home/x3/support/traits/has_attribute.hpp>
|
Chris@102
|
21 #include <boost/spirit/home/x3/support/traits/move_to.hpp>
|
Chris@102
|
22 #include <boost/spirit/home/x3/support/traits/is_parser.hpp>
|
Chris@102
|
23 #include <memory>
|
Chris@102
|
24 #include <string>
|
Chris@102
|
25
|
Chris@102
|
26 namespace boost { namespace spirit { namespace x3
|
Chris@102
|
27 {
|
Chris@102
|
28 template <
|
Chris@102
|
29 typename Iterator
|
Chris@102
|
30 , typename Attribute = unused_type
|
Chris@102
|
31 , typename Context = subcontext<>>
|
Chris@102
|
32 struct any_parser : parser<any_parser<Iterator, Attribute, Context>>
|
Chris@102
|
33 {
|
Chris@102
|
34 typedef Attribute attribute_type;
|
Chris@102
|
35
|
Chris@102
|
36 static bool const has_attribute =
|
Chris@102
|
37 !is_same<unused_type, attribute_type>::value;
|
Chris@102
|
38 static bool const handles_container =
|
Chris@102
|
39 traits::is_container<Attribute>::value;
|
Chris@102
|
40
|
Chris@102
|
41 public:
|
Chris@102
|
42 any_parser()
|
Chris@102
|
43 : _content(nullptr) {}
|
Chris@102
|
44
|
Chris@102
|
45 template <typename Expr,
|
Chris@102
|
46 typename Enable = typename enable_if<traits::is_parser<Expr>>::type>
|
Chris@102
|
47 any_parser(Expr const& expr)
|
Chris@102
|
48 : _content(new holder<Expr>(expr)) {}
|
Chris@102
|
49
|
Chris@102
|
50 any_parser(any_parser const& other)
|
Chris@102
|
51 : _content(other._content ? other._content->clone() : nullptr) {}
|
Chris@102
|
52
|
Chris@102
|
53 any_parser(any_parser&& other) = default;
|
Chris@102
|
54
|
Chris@102
|
55 any_parser& operator=(any_parser const& other)
|
Chris@102
|
56 {
|
Chris@102
|
57 _content.reset(other._content ? other._content->clone() : nullptr);
|
Chris@102
|
58 return *this;
|
Chris@102
|
59 }
|
Chris@102
|
60
|
Chris@102
|
61 any_parser& operator=(any_parser&& other) = default;
|
Chris@102
|
62
|
Chris@102
|
63 template <typename Iterator_, typename Context_>
|
Chris@102
|
64 bool parse(Iterator_& first, Iterator_ const& last
|
Chris@102
|
65 , Context_ const& context, unused_type, Attribute& attr) const
|
Chris@102
|
66 {
|
Chris@102
|
67 BOOST_STATIC_ASSERT_MSG(
|
Chris@102
|
68 (is_same<Iterator, Iterator_>::value)
|
Chris@102
|
69 , "Incompatible iterator used"
|
Chris@102
|
70 );
|
Chris@102
|
71
|
Chris@102
|
72 BOOST_ASSERT_MSG(
|
Chris@102
|
73 (_content != nullptr)
|
Chris@102
|
74 , "Invalid use of uninitialized any_parser"
|
Chris@102
|
75 );
|
Chris@102
|
76
|
Chris@102
|
77 return _content->parse(first, last, context, attr);
|
Chris@102
|
78 }
|
Chris@102
|
79
|
Chris@102
|
80 template <typename Iterator_, typename Context_, typename Attribute_>
|
Chris@102
|
81 bool parse(Iterator_& first, Iterator_ const& last
|
Chris@102
|
82 , Context_ const& context, unused_type, Attribute_& attr_) const
|
Chris@102
|
83 {
|
Chris@102
|
84 Attribute attr;
|
Chris@102
|
85 if (parse(first, last, context, unused, attr))
|
Chris@102
|
86 {
|
Chris@102
|
87 traits::move_to(attr, attr_);
|
Chris@102
|
88 return true;
|
Chris@102
|
89 }
|
Chris@102
|
90 return false;
|
Chris@102
|
91 }
|
Chris@102
|
92
|
Chris@102
|
93 std::string get_info() const
|
Chris@102
|
94 {
|
Chris@102
|
95 return _content ? _content->get_info() : "";
|
Chris@102
|
96 }
|
Chris@102
|
97
|
Chris@102
|
98 private:
|
Chris@102
|
99
|
Chris@102
|
100 struct placeholder
|
Chris@102
|
101 {
|
Chris@102
|
102 virtual placeholder* clone() const = 0;
|
Chris@102
|
103
|
Chris@102
|
104 virtual bool parse(Iterator& first, Iterator const& last
|
Chris@102
|
105 , Context const& context, Attribute& attr) const = 0;
|
Chris@102
|
106
|
Chris@102
|
107 virtual std::string get_info() const = 0;
|
Chris@102
|
108
|
Chris@102
|
109 virtual ~placeholder() {}
|
Chris@102
|
110 };
|
Chris@102
|
111
|
Chris@102
|
112 template <typename Expr>
|
Chris@102
|
113 struct holder : placeholder
|
Chris@102
|
114 {
|
Chris@102
|
115 typedef typename extension::as_parser<Expr>::value_type parser_type;
|
Chris@102
|
116
|
Chris@102
|
117 explicit holder(Expr const& p)
|
Chris@102
|
118 : _parser(as_parser(p)) {}
|
Chris@102
|
119
|
Chris@102
|
120 holder* clone() const override
|
Chris@102
|
121 {
|
Chris@102
|
122 return new holder(*this);
|
Chris@102
|
123 }
|
Chris@102
|
124
|
Chris@102
|
125 bool parse(Iterator& first, Iterator const& last
|
Chris@102
|
126 , Context const& context, Attribute& attr) const override
|
Chris@102
|
127 {
|
Chris@102
|
128 return _parser.parse(first, last, context, unused, attr);
|
Chris@102
|
129 }
|
Chris@102
|
130
|
Chris@102
|
131 std::string get_info() const override
|
Chris@102
|
132 {
|
Chris@102
|
133 return x3::what(_parser);
|
Chris@102
|
134 }
|
Chris@102
|
135
|
Chris@102
|
136 parser_type _parser;
|
Chris@102
|
137 };
|
Chris@102
|
138
|
Chris@102
|
139 private:
|
Chris@102
|
140 std::unique_ptr<placeholder> _content;
|
Chris@102
|
141 };
|
Chris@102
|
142
|
Chris@102
|
143 template <typename Iterator, typename Attribute, typename Context>
|
Chris@102
|
144 struct get_info<any_parser<Iterator, Attribute, Context>>
|
Chris@102
|
145 {
|
Chris@102
|
146 typedef std::string result_type;
|
Chris@102
|
147 std::string operator()(
|
Chris@102
|
148 any_parser<Iterator, Attribute, Context> const& p) const
|
Chris@102
|
149 {
|
Chris@102
|
150 return p.get_info();
|
Chris@102
|
151 }
|
Chris@102
|
152 };
|
Chris@102
|
153 }}}
|
Chris@102
|
154
|
Chris@102
|
155 #endif
|