source: NonGTP/Boost/boost/spirit/meta/parser_traits.hpp @ 857

Revision 857, 11.2 KB checked in by igarcia, 19 years ago (diff)
RevLine 
[857]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/
6
7    Use, modification and distribution is subject to the Boost Software
8    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
9    http://www.boost.org/LICENSE_1_0.txt)
10=============================================================================*/
11#if !defined(BOOST_SPIRIT_PARSER_TRAITS_HPP)
12#define BOOST_SPIRIT_PARSER_TRAITS_HPP
13
14#include <boost/type_traits/is_base_and_derived.hpp>
15#include <boost/static_assert.hpp>
16
17#include <boost/spirit/core/parser.hpp>
18#include <boost/spirit/meta/impl/parser_traits.ipp>
19
20///////////////////////////////////////////////////////////////////////////////
21namespace boost { namespace spirit {
22
23///////////////////////////////////////////////////////////////////////////////
24//
25//  Parser traits templates
26//
27//      Used to determine the type and several other characteristics of a given
28//      parser type.
29//
30///////////////////////////////////////////////////////////////////////////////
31
32///////////////////////////////////////////////////////////////////////////////
33//
34// The is_parser traits template can be used to tell wether a given
35// class is a parser.
36//
37///////////////////////////////////////////////////////////////////////////////
38template <typename T>
39struct is_parser
40{
41    BOOST_STATIC_CONSTANT(bool, value =
42        (::boost::is_base_and_derived<parser<T>, T>::value));
43
44//  [JDG 2/3/03] simplified implementation by
45//  using boost::is_base_and_derived
46};
47
48///////////////////////////////////////////////////////////////////////////////
49//
50//  The is_unary_composite traits template can be used to tell if a given
51//  parser is a unary parser as for instance kleene_star or optional.
52//
53///////////////////////////////////////////////////////////////////////////////
54template <typename UnaryT>
55struct is_unary_composite {
56
57    BOOST_STATIC_CONSTANT(bool, value = (::boost::is_convertible<
58        typename UnaryT::parser_category_t, unary_parser_category>::value));
59};
60
61///////////////////////////////////////////////////////////////////////////////
62//
63//  The is_acction_parser traits template can be used to tell if a given
64//  parser is a action parser, i.e. it is a composite consisting of a
65//  auxilliary parser and an attached semantic action.
66//
67///////////////////////////////////////////////////////////////////////////////
68template <typename ActionT>
69struct is_action_parser {
70
71    BOOST_STATIC_CONSTANT(bool, value = (::boost::is_convertible<
72        typename ActionT::parser_category_t, action_parser_category>::value));
73};
74
75///////////////////////////////////////////////////////////////////////////////
76//
77//  The is_binary_composite traits template can be used to tell if a given
78//  parser is a binary parser as for instance sequence or difference.
79//
80///////////////////////////////////////////////////////////////////////////////
81template <typename BinaryT>
82struct is_binary_composite {
83
84    BOOST_STATIC_CONSTANT(bool, value = (::boost::is_convertible<
85        typename BinaryT::parser_category_t, binary_parser_category>::value));
86};
87
88///////////////////////////////////////////////////////////////////////////////
89//
90//  The is_composite_parser traits template can be used to tell if a given
91//  parser is a unary or a binary parser composite type.
92//
93///////////////////////////////////////////////////////////////////////////////
94template <typename CompositeT>
95struct is_composite_parser {
96
97    BOOST_STATIC_CONSTANT(bool, value = (
98        ::boost::spirit::is_unary_composite<CompositeT>::value ||
99        ::boost::spirit::is_binary_composite<CompositeT>::value));
100};
101
102///////////////////////////////////////////////////////////////////////////////
103template <typename ParserT>
104struct is_alternative {
105
106    BOOST_STATIC_CONSTANT(bool, value = (
107        ::boost::spirit::impl::parser_type_traits<ParserT>::is_alternative));
108};
109
110template <typename ParserT>
111struct is_sequence {
112
113    BOOST_STATIC_CONSTANT(bool, value = (
114        ::boost::spirit::impl::parser_type_traits<ParserT>::is_sequence));
115};
116
117template <typename ParserT>
118struct is_sequential_or {
119
120    BOOST_STATIC_CONSTANT(bool, value = (
121        ::boost::spirit::impl::parser_type_traits<ParserT>::is_sequential_or));
122};
123
124template <typename ParserT>
125struct is_intersection {
126
127    BOOST_STATIC_CONSTANT(bool, value = (
128        ::boost::spirit::impl::parser_type_traits<ParserT>::is_intersection));
129};
130
131template <typename ParserT>
132struct is_difference {
133
134    BOOST_STATIC_CONSTANT(bool, value = (
135        ::boost::spirit::impl::parser_type_traits<ParserT>::is_difference));
136};
137
138template <typename ParserT>
139struct is_exclusive_or {
140
141    BOOST_STATIC_CONSTANT(bool, value = (
142        ::boost::spirit::impl::parser_type_traits<ParserT>::is_exclusive_or));
143};
144
145template <typename ParserT>
146struct is_optional {
147
148    BOOST_STATIC_CONSTANT(bool, value = (
149        ::boost::spirit::impl::parser_type_traits<ParserT>::is_optional));
150};
151
152template <typename ParserT>
153struct is_kleene_star {
154
155    BOOST_STATIC_CONSTANT(bool, value = (
156        ::boost::spirit::impl::parser_type_traits<ParserT>::is_kleene_star));
157};
158
159template <typename ParserT>
160struct is_positive {
161
162    BOOST_STATIC_CONSTANT(bool, value = (
163        ::boost::spirit::impl::parser_type_traits<ParserT>::is_positive));
164};
165
166///////////////////////////////////////////////////////////////////////////////
167//
168//  Parser extraction templates
169//
170///////////////////////////////////////////////////////////////////////////////
171
172///////////////////////////////////////////////////////////////////////////////
173//
174//  The unary_subject template can be used to return the type of the
175//  parser used as the subject of an unary parser.
176//  If the parser under inspection is not an unary type parser the compilation
177//  will fail.
178//
179///////////////////////////////////////////////////////////////////////////////
180template <typename UnaryT>
181struct unary_subject {
182
183    BOOST_STATIC_ASSERT(::boost::spirit::is_unary_composite<UnaryT>::value);
184    typedef typename UnaryT::subject_t type;
185};
186
187///////////////////////////////////////////////////////////////////////////////
188//
189//  The get_unary_subject template function returns the parser object, which
190//  is used as the subject of an unary parser.
191//  If the parser under inspection is not an unary type parser the compilation
192//  will fail.
193//
194///////////////////////////////////////////////////////////////////////////////
195template <typename UnaryT>
196inline typename unary_subject<UnaryT>::type const &
197get_unary_subject(UnaryT const &unary_)
198{
199    BOOST_STATIC_ASSERT(::boost::spirit::is_unary_composite<UnaryT>::value);
200    return unary_.subject();
201}
202
203///////////////////////////////////////////////////////////////////////////////
204//
205//  The binary_left_subject and binary_right_subject templates can be used to
206//  return the types of the parsers used as the left and right subject of an
207//  binary parser.
208//  If the parser under inspection is not a binary type parser the compilation
209//  will fail.
210//
211///////////////////////////////////////////////////////////////////////////////
212template <typename BinaryT>
213struct binary_left_subject {
214
215    BOOST_STATIC_ASSERT(::boost::spirit::is_binary_composite<BinaryT>::value);
216    typedef typename BinaryT::left_t type;
217};
218
219template <typename BinaryT>
220struct binary_right_subject {
221
222    BOOST_STATIC_ASSERT(::boost::spirit::is_binary_composite<BinaryT>::value);
223    typedef typename BinaryT::right_t type;
224};
225
226///////////////////////////////////////////////////////////////////////////////
227//
228//  The get_binary_left_subject and get_binary_right_subject template functions
229//  return the parser object, which is used as the left or right subject of a
230//  binary parser.
231//  If the parser under inspection is not a binary type parser the compilation
232//  will fail.
233//
234///////////////////////////////////////////////////////////////////////////////
235template <typename BinaryT>
236inline typename binary_left_subject<BinaryT>::type const &
237get_binary_left_subject(BinaryT const &binary_)
238{
239    BOOST_STATIC_ASSERT(::boost::spirit::is_binary_composite<BinaryT>::value);
240    return binary_.left();
241}
242
243template <typename BinaryT>
244inline typename binary_right_subject<BinaryT>::type const &
245get_binary_right_subject(BinaryT const &binary_)
246{
247    BOOST_STATIC_ASSERT(::boost::spirit::is_binary_composite<BinaryT>::value);
248    return binary_.right();
249}
250
251///////////////////////////////////////////////////////////////////////////////
252//
253//  The action_subject template can be used to return the type of the
254//  parser used as the subject of an action parser.
255//  If the parser under inspection is not an action type parser the compilation
256//  will fail.
257//
258///////////////////////////////////////////////////////////////////////////////
259template <typename ActionT>
260struct action_subject {
261
262    BOOST_STATIC_ASSERT(::boost::spirit::is_action_parser<ActionT>::value);
263    typedef typename ActionT::subject_t type;
264};
265
266///////////////////////////////////////////////////////////////////////////////
267//
268//  The get_action_subject template function returns the parser object, which
269//  is used as the subject of an action parser.
270//  If the parser under inspection is not an action type parser the compilation
271//  will fail.
272//
273///////////////////////////////////////////////////////////////////////////////
274template <typename ActionT>
275inline typename action_subject<ActionT>::type const &
276get_action_subject(ActionT const &action_)
277{
278    BOOST_STATIC_ASSERT(::boost::spirit::is_action_parser<ActionT>::value);
279    return action_.subject();
280}
281
282///////////////////////////////////////////////////////////////////////////////
283//
284//  The semantic_action template can be used to return the type of the
285//  attached semantic action of an action parser.
286//  If the parser under inspection is not an action type parser the compilation
287//  will fail.
288//
289///////////////////////////////////////////////////////////////////////////////
290template <typename ActionT>
291struct semantic_action {
292
293    BOOST_STATIC_ASSERT(::boost::spirit::is_action_parser<ActionT>::value);
294    typedef typename ActionT::predicate_t type;
295};
296
297///////////////////////////////////////////////////////////////////////////////
298//
299//  The get_semantic_action template function returns the attached semantic
300//  action of an action parser.
301//  If the parser under inspection is not an action type parser the compilation
302//  will fail.
303//
304///////////////////////////////////////////////////////////////////////////////
305template <typename ActionT>
306inline typename semantic_action<ActionT>::type const &
307get_semantic_action(ActionT const &action_)
308{
309    BOOST_STATIC_ASSERT(::boost::spirit::is_action_parser<ActionT>::value);
310    return action_.predicate();
311}
312
313///////////////////////////////////////////////////////////////////////////////
314}} // namespace boost::spirit
315
316#endif // !defined(BOOST_SPIRIT_PARSER_TRAITS_HPP)
Note: See TracBrowser for help on using the repository browser.