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>
23 #if !defined(BOOST_SPIRIT_X3_NO_RTTI)
27 namespace boost { namespace spirit { namespace x3
29 template <typename Subject, typename Action>
32 template <typename Subject, typename Handler>
35 struct parser_base {};
38 template <typename Derived>
39 struct parser : parser_base
41 typedef Derived derived_type;
42 static bool const handles_container = false;
43 static bool const is_pass_through_unary = false;
44 static bool const has_action = false;
46 Derived const& derived() const
48 return *static_cast<Derived const*>(this);
51 template <typename Action>
52 action<Derived, Action>
53 operator[](Action f) const
55 return action<Derived, Action>(this->derived(), f);
58 template <typename Handler>
59 guard<Derived, Handler>
60 on_error(Handler f) const
62 return guard<Derived, Handler>(this->derived(), f);
66 struct unary_category;
67 struct binary_category;
69 template <typename Subject, typename Derived>
70 struct unary_parser : parser<Derived>
72 typedef unary_category category;
73 typedef Subject subject_type;
74 static bool const has_action = Subject::has_action;
76 unary_parser(Subject const& subject)
79 unary_parser const& get_unary() const { return *this; }
84 template <typename Left, typename Right, typename Derived>
85 struct binary_parser : parser<Derived>
87 typedef binary_category category;
88 typedef Left left_type;
89 typedef Right right_type;
90 static bool const has_action =
91 left_type::has_action || right_type::has_action;
93 binary_parser(Left const& left, Right const& right)
94 : left(left), right(right) {}
96 binary_parser const& get_binary() const { return *this; }
102 ///////////////////////////////////////////////////////////////////////////
103 // as_parser: convert a type, T, into a parser.
104 ///////////////////////////////////////////////////////////////////////////
109 namespace as_parser_guard
111 void as_spirit_parser(...);
113 template<typename T, typename R =
114 decltype(as_spirit_parser(boost::declval<T const&>()))>
115 struct deduce_as_parser
120 typename boost::remove_reference<R>::type
124 static type call(T const& v)
126 return as_spirit_parser(v);
130 struct deduce_as_parser<T, void>
133 using as_parser_guard::deduce_as_parser;
136 template <typename T, typename Enable = void>
137 struct as_parser : detail::deduce_as_parser<T> {};
140 struct as_parser<unused_type>
142 typedef unused_type type;
143 typedef unused_type value_type;
144 static type call(unused_type)
150 template <typename Derived>
151 struct as_parser<Derived
152 , typename enable_if<is_base_of<parser_base, Derived>>::type>
154 typedef Derived const& type;
155 typedef Derived value_type;
156 static type call(Derived const& p)
162 template <typename Derived>
163 struct as_parser<parser<Derived>>
165 typedef Derived const& type;
166 typedef Derived value_type;
167 static type call(parser<Derived> const& p)
174 template <typename T>
175 inline typename extension::as_parser<T>::type
176 as_parser(T const& x)
178 return extension::as_parser<T>::call(x);
181 template <typename Derived>
182 inline Derived const&
183 as_parser(parser<Derived> const& p)
188 ///////////////////////////////////////////////////////////////////////////
189 // The main what function
191 // Note: unlike Spirit2, spirit parsers are no longer required to have a
192 // "what" member function. In X3, we specialize the get_info struct
193 // below where needed. If a specialization is not provided, the default
194 // below will be used. The default "what" result will be the typeid
195 // name of the parser if BOOST_SPIRIT_X3_NO_RTTI is not defined, otherwise
197 ///////////////////////////////////////////////////////////////////////////
198 template <typename Parser, typename Enable = void>
201 typedef std::string result_type;
202 std::string operator()(Parser const&) const
204 #if !defined(BOOST_SPIRIT_X3_NO_RTTI)
205 return typeid(Parser).name();
212 template <typename Parser>
213 std::string what(Parser const& p)
215 return get_info<Parser>()(p);
219 namespace boost { namespace spirit { namespace x3 { namespace traits
221 template <typename Subject, typename Derived, typename Context>
222 struct has_attribute<x3::unary_parser<Subject, Derived>, Context>
223 : has_attribute<Subject, Context> {};
225 template <typename Left, typename Right, typename Derived, typename Context>
226 struct has_attribute<x3::binary_parser<Left, Right, Derived>, Context>
227 : mpl::bool_<has_attribute<Left, Context>::value ||
228 has_attribute<Right, Context>::value> {};