]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/spirit/home/x3/core/parser.hpp
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / boost / boost / spirit / home / x3 / core / parser.hpp
1 /*=============================================================================
2 Copyright (c) 2001-2014 Joel de Guzman
3 Copyright (c) 2013 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_PARSER_OCTOBER_16_2008_0254PM)
9 #define BOOST_SPIRIT_X3_PARSER_OCTOBER_16_2008_0254PM
10
11 #include <boost/mpl/bool.hpp>
12 #include <boost/type_traits/is_base_of.hpp>
13 #include <boost/type_traits/remove_cv.hpp>
14 #include <boost/type_traits/remove_reference.hpp>
15 #include <boost/utility/declval.hpp>
16 #include <boost/utility/enable_if.hpp>
17 #include <boost/spirit/home/x3/support/unused.hpp>
18 #include <boost/spirit/home/x3/support/context.hpp>
19 #include <boost/spirit/home/x3/support/traits/has_attribute.hpp>
20 #include <boost/spirit/home/x3/support/utility/sfinae.hpp>
21 #include <boost/core/ignore_unused.hpp>
22 #include <boost/assert.hpp>
23 #include <string>
24
25 #if !defined(BOOST_SPIRIT_X3_NO_RTTI)
26 #include <typeinfo>
27 #endif
28
29 namespace boost { namespace spirit { namespace x3
30 {
31 template <typename Subject, typename Action>
32 struct action;
33
34 template <typename Subject, typename Handler>
35 struct guard;
36
37 struct parser_base {};
38 struct parser_id;
39
40 template <typename Derived>
41 struct parser : parser_base
42 {
43 typedef Derived derived_type;
44 static bool const handles_container = false;
45 static bool const is_pass_through_unary = false;
46 static bool const has_action = false;
47
48 Derived const& derived() const
49 {
50 return *static_cast<Derived const*>(this);
51 }
52
53 template <typename Action>
54 action<Derived, Action> operator[](Action f) const
55 {
56 return { this->derived(), f };
57 }
58
59 template <typename Handler>
60 guard<Derived, Handler> on_error(Handler f) const
61 {
62 return { this->derived(), f };
63 }
64 };
65
66 namespace detail {
67 template <typename Parser>
68 static void assert_initialized_rule(Parser const& p) {
69 boost::ignore_unused(p);
70
71 // Assert that we are not copying an unitialized static rule. If
72 // the static is in another TU, it may be initialized after we copy
73 // it. If so, its name member will be nullptr.
74 //
75 // Rather than hardcoding behaviour for rule-type subject parsers,
76 // we simply allow get_info<> to do the check in debug builds.
77 #ifndef NDEBUG
78 what(p); // note: allows get_info<> to diagnose the issue
79 #endif
80 }
81 }
82
83 struct unary_category;
84 struct binary_category;
85
86 template <typename Subject, typename Derived>
87 struct unary_parser : parser<Derived>
88 {
89 typedef unary_category category;
90 typedef Subject subject_type;
91 static bool const has_action = Subject::has_action;
92
93 unary_parser(Subject const& subject)
94 : subject(subject) { detail::assert_initialized_rule(subject); }
95
96 unary_parser const& get_unary() const { return *this; }
97
98 Subject subject;
99 };
100
101 template <typename Left, typename Right, typename Derived>
102 struct binary_parser : parser<Derived>
103 {
104 typedef binary_category category;
105 typedef Left left_type;
106 typedef Right right_type;
107 static bool const has_action =
108 left_type::has_action || right_type::has_action;
109
110 binary_parser(Left const& left, Right const& right)
111 : left(left), right(right)
112 {
113 detail::assert_initialized_rule(left);
114 detail::assert_initialized_rule(right);
115 }
116
117 binary_parser const& get_binary() const { return *this; }
118
119 Left left;
120 Right right;
121 };
122
123 ///////////////////////////////////////////////////////////////////////////
124 // as_parser: convert a type, T, into a parser.
125 ///////////////////////////////////////////////////////////////////////////
126 namespace extension
127 {
128 namespace detail
129 {
130 namespace as_parser_guard
131 {
132 void as_spirit_parser(...);
133
134 template<typename T, typename R =
135 decltype(as_spirit_parser(boost::declval<T const&>()))>
136 struct deduce_as_parser
137 {
138 typedef R type;
139 typedef typename
140 boost::remove_cv<
141 typename boost::remove_reference<R>::type
142 >::type
143 value_type;
144
145 static type call(T const& v)
146 {
147 return as_spirit_parser(v);
148 }
149 };
150 template<typename T>
151 struct deduce_as_parser<T, void>
152 {};
153 }
154 using as_parser_guard::deduce_as_parser;
155 }
156
157 template <typename T, typename Enable = void>
158 struct as_parser : detail::deduce_as_parser<T> {};
159
160 template <>
161 struct as_parser<unused_type>
162 {
163 typedef unused_type type;
164 typedef unused_type value_type;
165 static type call(unused_type)
166 {
167 return unused;
168 }
169 };
170
171 template <typename Derived>
172 struct as_parser<Derived
173 , typename enable_if<is_base_of<parser_base, Derived>>::type>
174 {
175 typedef Derived const& type;
176 typedef Derived value_type;
177 static type call(Derived const& p)
178 {
179 return p;
180 }
181 };
182
183 template <typename Derived>
184 struct as_parser<parser<Derived>>
185 {
186 typedef Derived const& type;
187 typedef Derived value_type;
188 static type call(parser<Derived> const& p)
189 {
190 return p.derived();
191 }
192 };
193 }
194
195 template <typename T>
196 inline typename extension::as_parser<T>::type
197 as_parser(T const& x)
198 {
199 return extension::as_parser<T>::call(x);
200 }
201
202 template <typename Derived>
203 inline Derived const&
204 as_parser(parser<Derived> const& p)
205 {
206 return p.derived();
207 }
208
209 ///////////////////////////////////////////////////////////////////////////
210 // The main what function
211 //
212 // Note: unlike Spirit2, spirit parsers are no longer required to have a
213 // "what" member function. In X3, we specialize the get_info struct
214 // below where needed. If a specialization is not provided, the default
215 // below will be used. The default "what" result will be the typeid
216 // name of the parser if BOOST_SPIRIT_X3_NO_RTTI is not defined, otherwise
217 // "undefined"
218 ///////////////////////////////////////////////////////////////////////////
219 template <typename Parser, typename Enable = void>
220 struct get_info
221 {
222 typedef std::string result_type;
223 std::string operator()(Parser const&) const
224 {
225 #if !defined(BOOST_SPIRIT_X3_NO_RTTI)
226 return typeid(Parser).name();
227 #else
228 return "undefined";
229 #endif
230 }
231 };
232
233 template <typename Parser>
234 std::string what(Parser const& p)
235 {
236 return get_info<Parser>()(p);
237 }
238 }}}
239
240 namespace boost { namespace spirit { namespace x3 { namespace traits
241 {
242 template <typename Subject, typename Derived, typename Context>
243 struct has_attribute<x3::unary_parser<Subject, Derived>, Context>
244 : has_attribute<Subject, Context> {};
245
246 template <typename Left, typename Right, typename Derived, typename Context>
247 struct has_attribute<x3::binary_parser<Left, Right, Derived>, Context>
248 : mpl::bool_<has_attribute<Left, Context>::value ||
249 has_attribute<Right, Context>::value> {};
250 }}}}
251
252 #endif