• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   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
12 
13 #include <boost/type_traits/is_base_and_derived.hpp>
14 #include <boost/static_assert.hpp>
15 
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>
19 
20 ///////////////////////////////////////////////////////////////////////////////
21 namespace boost { namespace spirit {
22 
23 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
24 
25 ///////////////////////////////////////////////////////////////////////////////
26 //
27 //  Parser traits templates
28 //
29 //      Used to determine the type and several other characteristics of a given
30 //      parser type.
31 //
32 ///////////////////////////////////////////////////////////////////////////////
33 
34 ///////////////////////////////////////////////////////////////////////////////
35 //
36 // The is_parser traits template can be used to tell wether a given
37 // class is a parser.
38 //
39 ///////////////////////////////////////////////////////////////////////////////
40 template <typename T>
41 struct is_parser
42 {
43     BOOST_STATIC_CONSTANT(bool, value =
44         (::boost::is_base_and_derived<parser<T>, T>::value));
45 
46 //  [JDG 2/3/03] simplified implementation by
47 //  using boost::is_base_and_derived
48 };
49 
50 ///////////////////////////////////////////////////////////////////////////////
51 //
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.
54 //
55 ///////////////////////////////////////////////////////////////////////////////
56 template <typename UnaryT>
57 struct is_unary_composite {
58 
59     BOOST_STATIC_CONSTANT(bool, value = (::boost::is_convertible<
60         typename UnaryT::parser_category_t, unary_parser_category>::value));
61 };
62 
63 ///////////////////////////////////////////////////////////////////////////////
64 //
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 //  auxiliary parser and an attached semantic action.
68 //
69 ///////////////////////////////////////////////////////////////////////////////
70 template <typename ActionT>
71 struct is_action_parser {
72 
73     BOOST_STATIC_CONSTANT(bool, value = (::boost::is_convertible<
74         typename ActionT::parser_category_t, action_parser_category>::value));
75 };
76 
77 ///////////////////////////////////////////////////////////////////////////////
78 //
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.
81 //
82 ///////////////////////////////////////////////////////////////////////////////
83 template <typename BinaryT>
84 struct is_binary_composite {
85 
86     BOOST_STATIC_CONSTANT(bool, value = (::boost::is_convertible<
87         typename BinaryT::parser_category_t, binary_parser_category>::value));
88 };
89 
90 ///////////////////////////////////////////////////////////////////////////////
91 //
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.
94 //
95 ///////////////////////////////////////////////////////////////////////////////
96 template <typename CompositeT>
97 struct is_composite_parser {
98 
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));
102 };
103 
104 ///////////////////////////////////////////////////////////////////////////////
105 template <typename ParserT>
106 struct is_alternative {
107 
108     BOOST_STATIC_CONSTANT(bool, value = (
109         ::BOOST_SPIRIT_CLASSIC_NS::impl::parser_type_traits<ParserT>::is_alternative));
110 };
111 
112 template <typename ParserT>
113 struct is_sequence {
114 
115     BOOST_STATIC_CONSTANT(bool, value = (
116         ::BOOST_SPIRIT_CLASSIC_NS::impl::parser_type_traits<ParserT>::is_sequence));
117 };
118 
119 template <typename ParserT>
120 struct is_sequential_or {
121 
122     BOOST_STATIC_CONSTANT(bool, value = (
123         ::BOOST_SPIRIT_CLASSIC_NS::impl::parser_type_traits<ParserT>::is_sequential_or));
124 };
125 
126 template <typename ParserT>
127 struct is_intersection {
128 
129     BOOST_STATIC_CONSTANT(bool, value = (
130         ::BOOST_SPIRIT_CLASSIC_NS::impl::parser_type_traits<ParserT>::is_intersection));
131 };
132 
133 template <typename ParserT>
134 struct is_difference {
135 
136     BOOST_STATIC_CONSTANT(bool, value = (
137         ::BOOST_SPIRIT_CLASSIC_NS::impl::parser_type_traits<ParserT>::is_difference));
138 };
139 
140 template <typename ParserT>
141 struct is_exclusive_or {
142 
143     BOOST_STATIC_CONSTANT(bool, value = (
144         ::BOOST_SPIRIT_CLASSIC_NS::impl::parser_type_traits<ParserT>::is_exclusive_or));
145 };
146 
147 template <typename ParserT>
148 struct is_optional {
149 
150     BOOST_STATIC_CONSTANT(bool, value = (
151         ::BOOST_SPIRIT_CLASSIC_NS::impl::parser_type_traits<ParserT>::is_optional));
152 };
153 
154 template <typename ParserT>
155 struct is_kleene_star {
156 
157     BOOST_STATIC_CONSTANT(bool, value = (
158         ::BOOST_SPIRIT_CLASSIC_NS::impl::parser_type_traits<ParserT>::is_kleene_star));
159 };
160 
161 template <typename ParserT>
162 struct is_positive {
163 
164     BOOST_STATIC_CONSTANT(bool, value = (
165         ::BOOST_SPIRIT_CLASSIC_NS::impl::parser_type_traits<ParserT>::is_positive));
166 };
167 
168 ///////////////////////////////////////////////////////////////////////////////
169 //
170 //  Parser extraction templates
171 //
172 ///////////////////////////////////////////////////////////////////////////////
173 
174 ///////////////////////////////////////////////////////////////////////////////
175 //
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
179 //  will fail.
180 //
181 ///////////////////////////////////////////////////////////////////////////////
182 template <typename UnaryT>
183 struct unary_subject {
184 
185     BOOST_STATIC_ASSERT(BOOST_SPIRIT_CLASSIC_NS::is_unary_composite<UnaryT>::value);
186     typedef typename UnaryT::subject_t type;
187 };
188 
189 ///////////////////////////////////////////////////////////////////////////////
190 //
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
194 //  will fail.
195 //
196 ///////////////////////////////////////////////////////////////////////////////
197 template <typename UnaryT>
198 inline typename unary_subject<UnaryT>::type const &
get_unary_subject(UnaryT const & unary_)199 get_unary_subject(UnaryT const &unary_)
200 {
201     BOOST_STATIC_ASSERT(::BOOST_SPIRIT_CLASSIC_NS::is_unary_composite<UnaryT>::value);
202     return unary_.subject();
203 }
204 
205 ///////////////////////////////////////////////////////////////////////////////
206 //
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
209 //  binary parser.
210 //  If the parser under inspection is not a binary type parser the compilation
211 //  will fail.
212 //
213 ///////////////////////////////////////////////////////////////////////////////
214 template <typename BinaryT>
215 struct binary_left_subject {
216 
217     BOOST_STATIC_ASSERT(::BOOST_SPIRIT_CLASSIC_NS::is_binary_composite<BinaryT>::value);
218     typedef typename BinaryT::left_t type;
219 };
220 
221 template <typename BinaryT>
222 struct binary_right_subject {
223 
224     BOOST_STATIC_ASSERT(::BOOST_SPIRIT_CLASSIC_NS::is_binary_composite<BinaryT>::value);
225     typedef typename BinaryT::right_t type;
226 };
227 
228 ///////////////////////////////////////////////////////////////////////////////
229 //
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
232 //  binary parser.
233 //  If the parser under inspection is not a binary type parser the compilation
234 //  will fail.
235 //
236 ///////////////////////////////////////////////////////////////////////////////
237 template <typename BinaryT>
238 inline typename binary_left_subject<BinaryT>::type const &
get_binary_left_subject(BinaryT const & binary_)239 get_binary_left_subject(BinaryT const &binary_)
240 {
241     BOOST_STATIC_ASSERT(::BOOST_SPIRIT_CLASSIC_NS::is_binary_composite<BinaryT>::value);
242     return binary_.left();
243 }
244 
245 template <typename BinaryT>
246 inline typename binary_right_subject<BinaryT>::type const &
get_binary_right_subject(BinaryT const & binary_)247 get_binary_right_subject(BinaryT const &binary_)
248 {
249     BOOST_STATIC_ASSERT(::BOOST_SPIRIT_CLASSIC_NS::is_binary_composite<BinaryT>::value);
250     return binary_.right();
251 }
252 
253 ///////////////////////////////////////////////////////////////////////////////
254 //
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
258 //  will fail.
259 //
260 ///////////////////////////////////////////////////////////////////////////////
261 template <typename ActionT>
262 struct action_subject {
263 
264     BOOST_STATIC_ASSERT(::BOOST_SPIRIT_CLASSIC_NS::is_action_parser<ActionT>::value);
265     typedef typename ActionT::subject_t type;
266 };
267 
268 ///////////////////////////////////////////////////////////////////////////////
269 //
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
273 //  will fail.
274 //
275 ///////////////////////////////////////////////////////////////////////////////
276 template <typename ActionT>
277 inline typename action_subject<ActionT>::type const &
get_action_subject(ActionT const & action_)278 get_action_subject(ActionT const &action_)
279 {
280     BOOST_STATIC_ASSERT(::BOOST_SPIRIT_CLASSIC_NS::is_action_parser<ActionT>::value);
281     return action_.subject();
282 }
283 
284 ///////////////////////////////////////////////////////////////////////////////
285 //
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
289 //  will fail.
290 //
291 ///////////////////////////////////////////////////////////////////////////////
292 template <typename ActionT>
293 struct semantic_action {
294 
295     BOOST_STATIC_ASSERT(::BOOST_SPIRIT_CLASSIC_NS::is_action_parser<ActionT>::value);
296     typedef typename ActionT::predicate_t type;
297 };
298 
299 ///////////////////////////////////////////////////////////////////////////////
300 //
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
304 //  will fail.
305 //
306 ///////////////////////////////////////////////////////////////////////////////
307 template <typename ActionT>
308 inline typename semantic_action<ActionT>::type const &
get_semantic_action(ActionT const & action_)309 get_semantic_action(ActionT const &action_)
310 {
311     BOOST_STATIC_ASSERT(::BOOST_SPIRIT_CLASSIC_NS::is_action_parser<ActionT>::value);
312     return action_.predicate();
313 }
314 
315 ///////////////////////////////////////////////////////////////////////////////
316 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
317 
318 }} // namespace BOOST_SPIRIT_CLASSIC_NS
319 
320 #endif // !defined(BOOST_SPIRIT_PARSER_TRAITS_HPP)
321