1 /*=============================================================================
2 Copyright (c) 2001-2014 Joel de Guzman
3 Copyright (c) 2013 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_PARSER_OCTOBER_16_2008_0254PM)
9 #define BOOST_SPIRIT_X3_PARSER_OCTOBER_16_2008_0254PM
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>
25 #if !defined(BOOST_SPIRIT_X3_NO_RTTI)
29 namespace boost { namespace spirit { namespace x3
31 template <typename Subject, typename Action>
34 template <typename Subject, typename Handler>
37 struct parser_base {};
40 template <typename Derived>
41 struct parser : parser_base
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;
48 Derived const& derived() const
50 return *static_cast<Derived const*>(this);
53 template <typename Action>
54 action<Derived, Action>
55 operator[](Action f) const
57 return action<Derived, Action>(this->derived(), f);
60 template <typename Handler>
61 guard<Derived, Handler>
62 on_error(Handler f) const
64 return guard<Derived, Handler>(this->derived(), f);
69 template <typename Parser>
70 static void assert_initialized_rule(Parser const& p) {
71 boost::ignore_unused(p);
73 // Assert that we are not copying an unitialized static rule. If
74 // the static is in another TU, it may be initialized after we copy
75 // it. If so, its name member will be nullptr.
77 // Rather than hardcoding behaviour for rule-type subject parsers,
78 // we simply allow get_info<> to do the check in debug builds.
80 what(p); // note: allows get_info<> to diagnose the issue
85 struct unary_category;
86 struct binary_category;
88 template <typename Subject, typename Derived>
89 struct unary_parser : parser<Derived>
91 typedef unary_category category;
92 typedef Subject subject_type;
93 static bool const has_action = Subject::has_action;
95 unary_parser(Subject const& subject)
96 : subject(subject) { detail::assert_initialized_rule(subject); }
98 unary_parser const& get_unary() const { return *this; }
103 template <typename Left, typename Right, typename Derived>
104 struct binary_parser : parser<Derived>
106 typedef binary_category category;
107 typedef Left left_type;
108 typedef Right right_type;
109 static bool const has_action =
110 left_type::has_action || right_type::has_action;
112 binary_parser(Left const& left, Right const& right)
113 : left(left), right(right)
115 detail::assert_initialized_rule(left);
116 detail::assert_initialized_rule(right);
119 binary_parser const& get_binary() const { return *this; }
125 ///////////////////////////////////////////////////////////////////////////
126 // as_parser: convert a type, T, into a parser.
127 ///////////////////////////////////////////////////////////////////////////
132 namespace as_parser_guard
134 void as_spirit_parser(...);
136 template<typename T, typename R =
137 decltype(as_spirit_parser(boost::declval<T const&>()))>
138 struct deduce_as_parser
143 typename boost::remove_reference<R>::type
147 static type call(T const& v)
149 return as_spirit_parser(v);
153 struct deduce_as_parser<T, void>
156 using as_parser_guard::deduce_as_parser;
159 template <typename T, typename Enable = void>
160 struct as_parser : detail::deduce_as_parser<T> {};
163 struct as_parser<unused_type>
165 typedef unused_type type;
166 typedef unused_type value_type;
167 static type call(unused_type)
173 template <typename Derived>
174 struct as_parser<Derived
175 , typename enable_if<is_base_of<parser_base, Derived>>::type>
177 typedef Derived const& type;
178 typedef Derived value_type;
179 static type call(Derived const& p)
185 template <typename Derived>
186 struct as_parser<parser<Derived>>
188 typedef Derived const& type;
189 typedef Derived value_type;
190 static type call(parser<Derived> const& p)
197 template <typename T>
198 inline typename extension::as_parser<T>::type
199 as_parser(T const& x)
201 return extension::as_parser<T>::call(x);
204 template <typename Derived>
205 inline Derived const&
206 as_parser(parser<Derived> const& p)
211 ///////////////////////////////////////////////////////////////////////////
212 // The main what function
214 // Note: unlike Spirit2, spirit parsers are no longer required to have a
215 // "what" member function. In X3, we specialize the get_info struct
216 // below where needed. If a specialization is not provided, the default
217 // below will be used. The default "what" result will be the typeid
218 // name of the parser if BOOST_SPIRIT_X3_NO_RTTI is not defined, otherwise
220 ///////////////////////////////////////////////////////////////////////////
221 template <typename Parser, typename Enable = void>
224 typedef std::string result_type;
225 std::string operator()(Parser const&) const
227 #if !defined(BOOST_SPIRIT_X3_NO_RTTI)
228 return typeid(Parser).name();
235 template <typename Parser>
236 std::string what(Parser const& p)
238 return get_info<Parser>()(p);
242 namespace boost { namespace spirit { namespace x3 { namespace traits
244 template <typename Subject, typename Derived, typename Context>
245 struct has_attribute<x3::unary_parser<Subject, Derived>, Context>
246 : has_attribute<Subject, Context> {};
248 template <typename Left, typename Right, typename Derived, typename Context>
249 struct has_attribute<x3::binary_parser<Left, Right, Derived>, Context>
250 : mpl::bool_<has_attribute<Left, Context>::value ||
251 has_attribute<Right, Context>::value> {};