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_SELECT_IPP
10 #define BOOST_SPIRIT_SELECT_IPP
12 #include <boost/spirit/home/classic/core/parser.hpp>
13 #include <boost/spirit/home/classic/core/composite/composite.hpp>
14 #include <boost/spirit/home/classic/meta/as_parser.hpp>
16 ///////////////////////////////////////////////////////////////////////////////
17 namespace boost { namespace spirit {
19 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
21 ///////////////////////////////////////////////////////////////////////////////
24 ///////////////////////////////////////////////////////////////////////////////
25 template <typename ParserT>
26 struct as_embedded_parser : public as_parser<ParserT>
28 typedef typename as_parser<ParserT>::type::derived_t::embed_t type;
31 ///////////////////////////////////////////////////////////////////////////////
33 // no implementation here to catch unknown BehaviourT template arguments
34 template <typename ResultT, typename BehaviourT>
35 struct select_match_gen;
37 // implementation for the select_default_no_fail behaviour
38 template <typename ResultT>
39 struct select_match_gen<ResultT, select_default_no_fail> {
41 template <typename ScannerT>
43 do_ (ScannerT const &scan)
45 return scan.create_match(0, -1, scan.first, scan.first);
49 // implementation for the select_default_fail behaviour
50 template <typename ResultT>
51 struct select_match_gen<ResultT, select_default_fail> {
53 template <typename ScannerT>
55 do_ (ScannerT const &scan)
57 return scan.no_match();
61 ///////////////////////////////////////////////////////////////////////////////
62 template <int N, typename ResultT, typename TupleT, typename BehaviourT>
63 struct parse_tuple_element {
65 BOOST_STATIC_CONSTANT(int, index = (TupleT::length - N));
67 template <typename ScannerT>
69 do_(TupleT const &t, ScannerT const &scan)
71 typedef typename ::phoenix::tuple_element<index, TupleT>::type parser_t;
72 typedef typename ScannerT::iterator_t iterator_t;
73 typedef typename parser_result<parser_t, ScannerT>::type result_t;
75 iterator_t save(scan.first);
76 result_t result(t[::phoenix::tuple_index<index>()].parse(scan));
79 return scan.create_match(result.length(), TupleT::length - N,
82 scan.first = save; // reset the input stream
83 return parse_tuple_element<N-1, ResultT, TupleT, BehaviourT>::
88 template <typename ResultT, typename TupleT, typename BehaviourT>
89 struct parse_tuple_element<1, ResultT, TupleT, BehaviourT> {
91 BOOST_STATIC_CONSTANT(int, index = (TupleT::length - 1));
93 template <typename ScannerT>
95 do_(TupleT const &t, ScannerT const &scan)
97 typedef typename ::phoenix::tuple_element<index, TupleT>::type parser_t;
98 typedef typename ScannerT::iterator_t iterator_t;
99 typedef typename parser_result<parser_t, ScannerT>::type result_t;
101 iterator_t save(scan.first);
102 result_t result(t[::phoenix::tuple_index<index>()].parse(scan));
105 return scan.create_match(result.length(), TupleT::length - 1,
108 scan.first = save; // reset the input stream
109 return select_match_gen<ResultT, BehaviourT>::do_(scan);
113 ///////////////////////////////////////////////////////////////////////////////
116 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
118 }} // namespace boost::spirit
120 #endif // BOOST_SPIRIT_SELECT_IPP