1 /*=============================================================================
2 Copyright (c) 2002-2003 Joel de Guzman
3 Copyright (c) 2002-2003 Hartmut Kaiser
4 Copyright (c) 2003 Martin Wille
5 http://spirit.sourceforge.net/
7 Distributed under the Boost Software License, Version 1.0. (See accompanying
8 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 =============================================================================*/
10 #if !defined(BOOST_SPIRIT_PARSER_TRAITS_HPP)
11 #define BOOST_SPIRIT_PARSER_TRAITS_HPP
13 #include <boost/type_traits/is_base_and_derived.hpp>
14 #include <boost/static_assert.hpp>
16 #include <boost/spirit/home/classic/namespace.hpp>
17 #include <boost/spirit/home/classic/core/parser.hpp>
18 #include <boost/spirit/home/classic/meta/impl/parser_traits.ipp>
20 ///////////////////////////////////////////////////////////////////////////////
21 namespace boost { namespace spirit {
23 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
25 ///////////////////////////////////////////////////////////////////////////////
27 // Parser traits templates
29 // Used to determine the type and several other characteristics of a given
32 ///////////////////////////////////////////////////////////////////////////////
34 ///////////////////////////////////////////////////////////////////////////////
36 // The is_parser traits template can be used to tell wether a given
39 ///////////////////////////////////////////////////////////////////////////////
43 BOOST_STATIC_CONSTANT(bool, value =
44 (::boost::is_base_and_derived<parser<T>, T>::value));
46 // [JDG 2/3/03] simplified implementation by
47 // using boost::is_base_and_derived
50 ///////////////////////////////////////////////////////////////////////////////
52 // The is_unary_composite traits template can be used to tell if a given
53 // parser is a unary parser as for instance kleene_star or optional.
55 ///////////////////////////////////////////////////////////////////////////////
56 template <typename UnaryT>
57 struct is_unary_composite {
59 BOOST_STATIC_CONSTANT(bool, value = (::boost::is_convertible<
60 typename UnaryT::parser_category_t, unary_parser_category>::value));
63 ///////////////////////////////////////////////////////////////////////////////
65 // The is_acction_parser traits template can be used to tell if a given
66 // parser is a action parser, i.e. it is a composite consisting of a
67 // auxilliary parser and an attached semantic action.
69 ///////////////////////////////////////////////////////////////////////////////
70 template <typename ActionT>
71 struct is_action_parser {
73 BOOST_STATIC_CONSTANT(bool, value = (::boost::is_convertible<
74 typename ActionT::parser_category_t, action_parser_category>::value));
77 ///////////////////////////////////////////////////////////////////////////////
79 // The is_binary_composite traits template can be used to tell if a given
80 // parser is a binary parser as for instance sequence or difference.
82 ///////////////////////////////////////////////////////////////////////////////
83 template <typename BinaryT>
84 struct is_binary_composite {
86 BOOST_STATIC_CONSTANT(bool, value = (::boost::is_convertible<
87 typename BinaryT::parser_category_t, binary_parser_category>::value));
90 ///////////////////////////////////////////////////////////////////////////////
92 // The is_composite_parser traits template can be used to tell if a given
93 // parser is a unary or a binary parser composite type.
95 ///////////////////////////////////////////////////////////////////////////////
96 template <typename CompositeT>
97 struct is_composite_parser {
99 BOOST_STATIC_CONSTANT(bool, value = (
100 ::BOOST_SPIRIT_CLASSIC_NS::is_unary_composite<CompositeT>::value ||
101 ::BOOST_SPIRIT_CLASSIC_NS::is_binary_composite<CompositeT>::value));
104 ///////////////////////////////////////////////////////////////////////////////
105 template <typename ParserT>
106 struct is_alternative {
108 BOOST_STATIC_CONSTANT(bool, value = (
109 ::BOOST_SPIRIT_CLASSIC_NS::impl::parser_type_traits<ParserT>::is_alternative));
112 template <typename ParserT>
115 BOOST_STATIC_CONSTANT(bool, value = (
116 ::BOOST_SPIRIT_CLASSIC_NS::impl::parser_type_traits<ParserT>::is_sequence));
119 template <typename ParserT>
120 struct is_sequential_or {
122 BOOST_STATIC_CONSTANT(bool, value = (
123 ::BOOST_SPIRIT_CLASSIC_NS::impl::parser_type_traits<ParserT>::is_sequential_or));
126 template <typename ParserT>
127 struct is_intersection {
129 BOOST_STATIC_CONSTANT(bool, value = (
130 ::BOOST_SPIRIT_CLASSIC_NS::impl::parser_type_traits<ParserT>::is_intersection));
133 template <typename ParserT>
134 struct is_difference {
136 BOOST_STATIC_CONSTANT(bool, value = (
137 ::BOOST_SPIRIT_CLASSIC_NS::impl::parser_type_traits<ParserT>::is_difference));
140 template <typename ParserT>
141 struct is_exclusive_or {
143 BOOST_STATIC_CONSTANT(bool, value = (
144 ::BOOST_SPIRIT_CLASSIC_NS::impl::parser_type_traits<ParserT>::is_exclusive_or));
147 template <typename ParserT>
150 BOOST_STATIC_CONSTANT(bool, value = (
151 ::BOOST_SPIRIT_CLASSIC_NS::impl::parser_type_traits<ParserT>::is_optional));
154 template <typename ParserT>
155 struct is_kleene_star {
157 BOOST_STATIC_CONSTANT(bool, value = (
158 ::BOOST_SPIRIT_CLASSIC_NS::impl::parser_type_traits<ParserT>::is_kleene_star));
161 template <typename ParserT>
164 BOOST_STATIC_CONSTANT(bool, value = (
165 ::BOOST_SPIRIT_CLASSIC_NS::impl::parser_type_traits<ParserT>::is_positive));
168 ///////////////////////////////////////////////////////////////////////////////
170 // Parser extraction templates
172 ///////////////////////////////////////////////////////////////////////////////
174 ///////////////////////////////////////////////////////////////////////////////
176 // The unary_subject template can be used to return the type of the
177 // parser used as the subject of an unary parser.
178 // If the parser under inspection is not an unary type parser the compilation
181 ///////////////////////////////////////////////////////////////////////////////
182 template <typename UnaryT>
183 struct unary_subject {
185 BOOST_STATIC_ASSERT(BOOST_SPIRIT_CLASSIC_NS::is_unary_composite<UnaryT>::value);
186 typedef typename UnaryT::subject_t type;
189 ///////////////////////////////////////////////////////////////////////////////
191 // The get_unary_subject template function returns the parser object, which
192 // is used as the subject of an unary parser.
193 // If the parser under inspection is not an unary type parser the compilation
196 ///////////////////////////////////////////////////////////////////////////////
197 template <typename UnaryT>
198 inline typename unary_subject<UnaryT>::type const &
199 get_unary_subject(UnaryT const &unary_)
201 BOOST_STATIC_ASSERT(::BOOST_SPIRIT_CLASSIC_NS::is_unary_composite<UnaryT>::value);
202 return unary_.subject();
205 ///////////////////////////////////////////////////////////////////////////////
207 // The binary_left_subject and binary_right_subject templates can be used to
208 // return the types of the parsers used as the left and right subject of an
210 // If the parser under inspection is not a binary type parser the compilation
213 ///////////////////////////////////////////////////////////////////////////////
214 template <typename BinaryT>
215 struct binary_left_subject {
217 BOOST_STATIC_ASSERT(::BOOST_SPIRIT_CLASSIC_NS::is_binary_composite<BinaryT>::value);
218 typedef typename BinaryT::left_t type;
221 template <typename BinaryT>
222 struct binary_right_subject {
224 BOOST_STATIC_ASSERT(::BOOST_SPIRIT_CLASSIC_NS::is_binary_composite<BinaryT>::value);
225 typedef typename BinaryT::right_t type;
228 ///////////////////////////////////////////////////////////////////////////////
230 // The get_binary_left_subject and get_binary_right_subject template functions
231 // return the parser object, which is used as the left or right subject of a
233 // If the parser under inspection is not a binary type parser the compilation
236 ///////////////////////////////////////////////////////////////////////////////
237 template <typename BinaryT>
238 inline typename binary_left_subject<BinaryT>::type const &
239 get_binary_left_subject(BinaryT const &binary_)
241 BOOST_STATIC_ASSERT(::BOOST_SPIRIT_CLASSIC_NS::is_binary_composite<BinaryT>::value);
242 return binary_.left();
245 template <typename BinaryT>
246 inline typename binary_right_subject<BinaryT>::type const &
247 get_binary_right_subject(BinaryT const &binary_)
249 BOOST_STATIC_ASSERT(::BOOST_SPIRIT_CLASSIC_NS::is_binary_composite<BinaryT>::value);
250 return binary_.right();
253 ///////////////////////////////////////////////////////////////////////////////
255 // The action_subject template can be used to return the type of the
256 // parser used as the subject of an action parser.
257 // If the parser under inspection is not an action type parser the compilation
260 ///////////////////////////////////////////////////////////////////////////////
261 template <typename ActionT>
262 struct action_subject {
264 BOOST_STATIC_ASSERT(::BOOST_SPIRIT_CLASSIC_NS::is_action_parser<ActionT>::value);
265 typedef typename ActionT::subject_t type;
268 ///////////////////////////////////////////////////////////////////////////////
270 // The get_action_subject template function returns the parser object, which
271 // is used as the subject of an action parser.
272 // If the parser under inspection is not an action type parser the compilation
275 ///////////////////////////////////////////////////////////////////////////////
276 template <typename ActionT>
277 inline typename action_subject<ActionT>::type const &
278 get_action_subject(ActionT const &action_)
280 BOOST_STATIC_ASSERT(::BOOST_SPIRIT_CLASSIC_NS::is_action_parser<ActionT>::value);
281 return action_.subject();
284 ///////////////////////////////////////////////////////////////////////////////
286 // The semantic_action template can be used to return the type of the
287 // attached semantic action of an action parser.
288 // If the parser under inspection is not an action type parser the compilation
291 ///////////////////////////////////////////////////////////////////////////////
292 template <typename ActionT>
293 struct semantic_action {
295 BOOST_STATIC_ASSERT(::BOOST_SPIRIT_CLASSIC_NS::is_action_parser<ActionT>::value);
296 typedef typename ActionT::predicate_t type;
299 ///////////////////////////////////////////////////////////////////////////////
301 // The get_semantic_action template function returns the attached semantic
302 // action of an action parser.
303 // If the parser under inspection is not an action type parser the compilation
306 ///////////////////////////////////////////////////////////////////////////////
307 template <typename ActionT>
308 inline typename semantic_action<ActionT>::type const &
309 get_semantic_action(ActionT const &action_)
311 BOOST_STATIC_ASSERT(::BOOST_SPIRIT_CLASSIC_NS::is_action_parser<ActionT>::value);
312 return action_.predicate();
315 ///////////////////////////////////////////////////////////////////////////////
316 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
318 }} // namespace BOOST_SPIRIT_CLASSIC_NS
320 #endif // !defined(BOOST_SPIRIT_PARSER_TRAITS_HPP)