1 /*=============================================================================
2 Copyright (c) 2003 Hartmut Kaiser
3 http://spirit.sourceforge.net/
5 Use, modification and distribution is subject to the Boost Software
6 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 #ifndef BOOST_SPIRIT_SWITCH_IPP
10 #define BOOST_SPIRIT_SWITCH_IPP
12 #include <boost/mpl/if.hpp>
13 #include <boost/type_traits/is_same.hpp>
14 #include <boost/static_assert.hpp>
16 #include <boost/preprocessor/cat.hpp>
17 #include <boost/preprocessor/inc.hpp>
18 #include <boost/preprocessor/repeat.hpp>
19 #include <boost/preprocessor/repeat_from_to.hpp>
21 #include <boost/spirit/home/classic/core/parser.hpp>
22 #include <boost/spirit/home/classic/core/primitives/primitives.hpp>
23 #include <boost/spirit/home/classic/core/composite/composite.hpp>
24 #include <boost/spirit/home/classic/meta/as_parser.hpp>
26 #include <boost/spirit/home/classic/phoenix/actor.hpp>
27 #include <boost/spirit/home/classic/phoenix/tuples.hpp>
29 ///////////////////////////////////////////////////////////////////////////////
30 namespace boost { namespace spirit {
32 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
34 // forward declaration
35 template <int N, typename ParserT, bool IsDefault> struct case_parser;
37 ///////////////////////////////////////////////////////////////////////////////
40 ///////////////////////////////////////////////////////////////////////////////
41 // parse helper functions
42 template <typename ParserT, typename ScannerT>
43 inline typename parser_result<ParserT, ScannerT>::type
44 delegate_parse(ParserT const &p, ScannerT const &scan,
45 typename ScannerT::iterator_t const save)
47 typedef typename parser_result<ParserT, ScannerT>::type result_t;
49 result_t result (p.subject().parse(scan));
55 ///////////////////////////////////////////////////////////////////////////////
56 // General default case handling (no default_p case branch given).
57 // First try to match the current parser node (if the condition value is
58 // matched) and, if that fails, return a no_match
59 template <int N, bool IsDefault, bool HasDefault>
60 struct default_delegate_parse {
63 typename ParserT, typename DefaultT,
64 typename ValueT, typename ScannerT
66 static typename parser_result<ParserT, ScannerT>::type
67 parse (ValueT const &value, ParserT const &p, DefaultT const &,
68 ScannerT const &scan, typename ScannerT::iterator_t const save)
71 return delegate_parse(p, scan, save);
72 return scan.no_match();
76 // The current case parser node is the default parser.
77 // Ignore the given case value and try to match the given default parser.
78 template <int N, bool HasDefault>
79 struct default_delegate_parse<N, true, HasDefault> {
82 typename ParserT, typename DefaultT,
83 typename ValueT, typename ScannerT
85 static typename parser_result<ParserT, ScannerT>::type
86 parse (ValueT const& /*value*/, ParserT const &, DefaultT const &d,
87 ScannerT const &scan, typename ScannerT::iterator_t const save)
89 // Since there is a default_p case branch defined, the corresponding
90 // parser shouldn't be the nothing_parser
91 BOOST_STATIC_ASSERT((!boost::is_same<DefaultT, nothing_parser>::value));
92 return delegate_parse(d, scan, save);
96 // The current case parser node is not the default parser, but there is a
97 // default_p branch given inside the switch_p parser.
98 // First try to match the current parser node (if the condition value is
99 // matched) and, if that fails, match the given default_p parser.
101 struct default_delegate_parse<N, false, true> {
104 typename ParserT, typename DefaultT,
105 typename ValueT, typename ScannerT
107 static typename parser_result<ParserT, ScannerT>::type
108 parse (ValueT const &value, ParserT const &p, DefaultT const &d,
109 ScannerT const &scan, typename ScannerT::iterator_t const save)
111 // Since there is a default_p case branch defined, the corresponding
112 // parser shouldn't be the nothing_parser
113 BOOST_STATIC_ASSERT((!boost::is_same<DefaultT, nothing_parser>::value));
115 return delegate_parse(p, scan, save);
117 return delegate_parse(d, scan, save);
121 ///////////////////////////////////////////////////////////////////////////////
122 // Look through the case parser chain to test, if there is a default case
123 // branch defined (returned by 'value').
124 template <typename CaseT, bool IsSimple = CaseT::is_simple>
127 ////////////////////////////////////////
128 template <typename ResultT, bool IsDefault>
129 struct get_default_parser {
131 template <typename ParserT>
133 get(parser<ParserT> const &p)
135 return default_case<typename ParserT::derived_t::left_t>::
136 get(p.derived().left());
140 template <typename ResultT>
141 struct get_default_parser<ResultT, true> {
143 template <typename ParserT>
145 get(parser<ParserT> const &p) { return p.derived().right(); }
148 ////////////////////////////////////////
149 template <typename CaseT, bool IsSimple>
150 struct default_case {
152 // The 'value' constant is true, if the current case_parser or one of its
153 // left siblings is a default_p generated case_parser.
154 BOOST_STATIC_CONSTANT(bool, value =
155 (CaseT::is_default || default_case<typename CaseT::left_t>::value));
157 // The 'is_epsilon' constant is true, if the current case_parser or one of
158 // its left siblings is a default_p generated parser with an attached
159 // epsilon_p (this is generated by the plain default_p).
160 BOOST_STATIC_CONSTANT(bool, is_epsilon = (
161 (CaseT::is_default && CaseT::is_epsilon) ||
162 default_case<typename CaseT::left_t>::is_epsilon
165 // The computed 'type' represents the type of the default case branch
166 // parser (if there is one) or nothing_parser (if there isn't any default
168 typedef typename boost::mpl::if_c<
169 CaseT::is_default, typename CaseT::right_embed_t,
170 typename default_case<typename CaseT::left_t>::type
173 // The get function returns the parser attached to the default case branch
174 // (if there is one) or an instance of a nothing_parser (if there isn't
175 // any default case branch).
176 template <typename ParserT>
178 get(parser<ParserT> const &p)
179 { return get_default_parser<type, CaseT::is_default>::get(p); }
182 ////////////////////////////////////////
183 template <typename ResultT, bool IsDefault>
184 struct get_default_parser_simple {
186 template <typename ParserT>
188 get(parser<ParserT> const &p) { return p.derived(); }
191 template <typename ResultT>
192 struct get_default_parser_simple<ResultT, false> {
194 template <typename ParserT>
195 static nothing_parser
196 get(parser<ParserT> const &) { return nothing_p; }
199 ////////////////////////////////////////
200 // Specialization of the default_case template for the last (leftmost) element
201 // of the case parser chain.
202 template <typename CaseT>
203 struct default_case<CaseT, true> {
205 // The 'value' and 'is_epsilon' constant, the 'type' type and the function
206 // 'get' are described above.
208 BOOST_STATIC_CONSTANT(bool, value = CaseT::is_default);
209 BOOST_STATIC_CONSTANT(bool, is_epsilon = (
210 CaseT::is_default && CaseT::is_epsilon
213 typedef typename boost::mpl::if_c<
214 CaseT::is_default, CaseT, nothing_parser
217 template <typename ParserT>
219 get(parser<ParserT> const &p)
220 { return get_default_parser_simple<type, value>::get(p); }
223 ///////////////////////////////////////////////////////////////////////////////
224 // The case_chain template calculates recursivly the depth of the left
225 // subchain of the given case branch node.
226 template <typename CaseT, bool IsSimple = CaseT::is_simple>
229 BOOST_STATIC_CONSTANT(int, depth = (
230 case_chain<typename CaseT::left_t>::depth + 1
234 template <typename CaseT>
235 struct case_chain<CaseT, true> {
237 BOOST_STATIC_CONSTANT(int, depth = 0);
240 ///////////////////////////////////////////////////////////////////////////////
241 // The chain_parser template is used to extract the type and the instance of
242 // a left or a right parser, burried arbitrary deep inside the case parser
244 template <int Depth, typename CaseT>
245 struct chain_parser {
247 typedef typename CaseT::left_t our_left_t;
249 typedef typename chain_parser<Depth-1, our_left_t>::left_t left_t;
250 typedef typename chain_parser<Depth-1, our_left_t>::right_t right_t;
254 { return chain_parser<Depth-1, our_left_t>::left(p.left()); }
257 right(CaseT const &p)
258 { return chain_parser<Depth-1, our_left_t>::right(p.left()); }
261 template <typename CaseT>
262 struct chain_parser<1, CaseT> {
264 typedef typename CaseT::left_t left_t;
265 typedef typename CaseT::right_t right_t;
267 static left_t left(CaseT const &p) { return p.left(); }
268 static right_t right(CaseT const &p) { return p.right(); }
271 template <typename CaseT>
272 struct chain_parser<0, CaseT>; // shouldn't be instantiated
274 ///////////////////////////////////////////////////////////////////////////////
275 // Type computing meta function for calculating the type of the return value
276 // of the used conditional switch expression
277 template <typename TargetT, typename ScannerT>
278 struct condition_result {
280 typedef typename TargetT::template result<ScannerT>::type type;
283 ///////////////////////////////////////////////////////////////////////////////
284 template <typename LeftT, typename RightT, bool IsDefault>
285 struct compound_case_parser
286 : public binary<LeftT, RightT,
287 parser<compound_case_parser<LeftT, RightT, IsDefault> > >
289 typedef compound_case_parser<LeftT, RightT, IsDefault> self_t;
290 typedef binary_parser_category parser_category_t;
291 typedef binary<LeftT, RightT, parser<self_t> > base_t;
293 BOOST_STATIC_CONSTANT(int, value = RightT::value);
294 BOOST_STATIC_CONSTANT(bool, is_default = IsDefault);
295 BOOST_STATIC_CONSTANT(bool, is_simple = false);
296 BOOST_STATIC_CONSTANT(bool, is_epsilon = (
298 boost::is_same<typename RightT::subject_t, epsilon_parser>::value
301 compound_case_parser(parser<LeftT> const &lhs, parser<RightT> const &rhs)
302 : base_t(lhs.derived(), rhs.derived())
305 template <typename ScannerT>
308 typedef typename match_result<ScannerT, nil_t>::type type;
311 template <typename ScannerT, typename CondT>
312 typename parser_result<self_t, ScannerT>::type
313 parse(ScannerT const& scan, CondT const &cond) const;
315 template <int N1, typename ParserT1, bool IsDefault1>
316 compound_case_parser<
317 self_t, case_parser<N1, ParserT1, IsDefault1>, IsDefault1
319 operator, (case_parser<N1, ParserT1, IsDefault1> const &p) const
321 // If the following compile time assertion fires, you've probably used
322 // more than one default_p case inside the switch_p parser construct.
323 BOOST_STATIC_ASSERT(!default_case<self_t>::value || !IsDefault1);
325 // If this compile time assertion fires, you've probably want to use
326 // more case_p/default_p case branches, than possible.
328 case_chain<self_t>::depth < BOOST_SPIRIT_SWITCH_CASE_LIMIT
331 typedef case_parser<N1, ParserT1, IsDefault1> right_t;
332 return compound_case_parser<self_t, right_t, IsDefault1>(*this, p);
336 ///////////////////////////////////////////////////////////////////////////////
337 // The parse_switch::do_ functions dispatch to the correct parser, which is
338 // selected through the given conditional switch value.
339 template <int Value, int Depth, bool IsDefault>
342 ///////////////////////////////////////////////////////////////////////////////
344 // The following generates a couple of parse_switch template specializations
345 // with an increasing number of handled case branches (for 1..N).
347 // template <int Value, bool IsDefault>
348 // struct parse_switch<Value, N, IsDefault> {
350 // template <typename ParserT, typename ScannerT>
351 // static typename parser_result<ParserT, ScannerT>::type
352 // do_(ParserT const &p, ScannerT const &scan, long cond_value,
353 // typename ScannerT::iterator_t const &save)
355 // typedef ParserT left_t0;
356 // typedef typename left_t0::left left_t1;
359 // switch (cond_value) {
360 // case left_tN::value:
361 // return delegate_parse(chain_parser<
362 // case_chain<ParserT>::depth, ParserT
363 // >::left(p), scan, save);
365 // case left_t1::value:
366 // return delegate_parse(chain_parser<
368 // >::right(p.left()), scan, save);
370 // case left_t0::value:
372 // typedef default_case<ParserT> default_t;
373 // typedef default_delegate_parse<
374 // Value, IsDefault, default_t::value>
377 // return default_parse_t::parse(cond_value, p.right(),
378 // default_t::get(p), scan, save);
383 ///////////////////////////////////////////////////////////////////////////////
384 #define BOOST_SPIRIT_PARSE_SWITCH_TYPEDEFS(z, N, _) \
385 typedef typename BOOST_PP_CAT(left_t, N)::left_t \
386 BOOST_PP_CAT(left_t, BOOST_PP_INC(N)); \
389 #define BOOST_SPIRIT_PARSE_SWITCH_CASES(z, N, _) \
390 case (long)(BOOST_PP_CAT(left_t, N)::value): \
391 return delegate_parse(chain_parser<N, left_t1>::right(p.left()), \
395 #define BOOST_SPIRIT_PARSE_SWITCHES(z, N, _) \
396 template <int Value, bool IsDefault> \
397 struct parse_switch<Value, BOOST_PP_INC(N), IsDefault> { \
399 template <typename ParserT, typename ScannerT> \
400 static typename parser_result<ParserT, ScannerT>::type \
401 do_(ParserT const &p, ScannerT const &scan, long cond_value, \
402 typename ScannerT::iterator_t const &save) \
404 typedef ParserT left_t0; \
405 BOOST_PP_REPEAT_FROM_TO_ ## z(0, BOOST_PP_INC(N), \
406 BOOST_SPIRIT_PARSE_SWITCH_TYPEDEFS, _) \
408 switch (cond_value) { \
409 case (long)(BOOST_PP_CAT(left_t, BOOST_PP_INC(N))::value): \
410 return delegate_parse( \
412 case_chain<ParserT>::depth, ParserT \
413 >::left(p), scan, save); \
415 BOOST_PP_REPEAT_FROM_TO_ ## z(1, BOOST_PP_INC(N), \
416 BOOST_SPIRIT_PARSE_SWITCH_CASES, _) \
418 case (long)(left_t0::value): \
420 typedef default_case<ParserT> default_t; \
422 default_delegate_parse<Value, IsDefault, default_t::value> \
425 return default_parse_t::parse(cond_value, p.right(), \
426 default_t::get(p), scan, save); \
432 BOOST_PP_REPEAT(BOOST_PP_DEC(BOOST_SPIRIT_SWITCH_CASE_LIMIT),
433 BOOST_SPIRIT_PARSE_SWITCHES, _)
435 #undef BOOST_SPIRIT_PARSE_SWITCH_TYPEDEFS
436 #undef BOOST_SPIRIT_PARSE_SWITCH_CASES
437 #undef BOOST_SPIRIT_PARSE_SWITCHES
438 ///////////////////////////////////////////////////////////////////////////////
440 template <typename LeftT, typename RightT, bool IsDefault>
441 template <typename ScannerT, typename CondT>
442 inline typename parser_result<
443 compound_case_parser<LeftT, RightT, IsDefault>, ScannerT
445 compound_case_parser<LeftT, RightT, IsDefault>::
446 parse(ScannerT const& scan, CondT const &cond) const
448 scan.at_end(); // allow skipper to take effect
449 return parse_switch<value, case_chain<self_t>::depth, is_default>::
450 do_(*this, scan, cond(scan), scan.first);
453 ///////////////////////////////////////////////////////////////////////////////
454 // The switch condition is to be evaluated from a parser result value.
455 template <typename ParserT>
456 struct cond_functor {
458 typedef cond_functor<ParserT> self_t;
460 cond_functor(ParserT const &p_)
464 template <typename ScannerT>
467 typedef typename parser_result<ParserT, ScannerT>::type::attr_t type;
470 template <typename ScannerT>
471 typename condition_result<self_t, ScannerT>::type
472 operator()(ScannerT const &scan) const
474 typedef typename parser_result<ParserT, ScannerT>::type result_t;
475 typedef typename result_t::attr_t attr_t;
477 result_t result(p.parse(scan));
478 return !result ? attr_t() : result.value();
481 typename ParserT::embed_t p;
484 template <typename ParserT>
485 struct make_cond_functor {
487 typedef as_parser<ParserT> as_parser_t;
489 static cond_functor<typename as_parser_t::type>
490 do_(ParserT const &cond)
492 return cond_functor<typename as_parser_t::type>(
493 as_parser_t::convert(cond));
497 ///////////////////////////////////////////////////////////////////////////////
498 // The switch condition is to be evaluated from a phoenix actor
499 template <typename ActorT>
502 typedef cond_actor<ActorT> self_t;
504 cond_actor(ActorT const &actor_)
508 template <typename ScannerT>
511 typedef typename ::phoenix::actor_result<ActorT, ::phoenix::tuple<> >::type
515 template <typename ScannerT>
516 typename condition_result<self_t, ScannerT>::type
517 operator()(ScannerT const& /*scan*/) const
525 template <typename ActorT>
526 struct make_cond_functor< ::phoenix::actor<ActorT> > {
528 static cond_actor< ::phoenix::actor<ActorT> >
529 do_(::phoenix::actor<ActorT> const &actor)
531 return cond_actor< ::phoenix::actor<ActorT> >(actor);
535 ///////////////////////////////////////////////////////////////////////////////
536 // The switch condition is to be taken directly from the input stream
537 struct get_next_token_cond {
539 typedef get_next_token_cond self_t;
541 template <typename ScannerT>
544 typedef typename ScannerT::value_t type;
547 template <typename ScannerT>
548 typename condition_result<self_t, ScannerT>::type
549 operator()(ScannerT const &scan) const
551 typename ScannerT::value_t val(*scan);
558 struct make_cond_functor<get_next_token_cond> {
560 static get_next_token_cond
561 do_(get_next_token_cond const &cond)
567 ///////////////////////////////////////////////////////////////////////////////
570 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
572 }} // namespace boost::spirit
574 #endif // BOOST_SPIRIT_SWITCH_IPP