]> git.sesse.net Git - casparcg/blob - dependencies/boost/boost/xpressive/detail/static/transforms/as_alternate.hpp
Manually merged pull request #222
[casparcg] / dependencies / boost / boost / xpressive / detail / static / transforms / as_alternate.hpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // as_alternate.hpp
3 //
4 //  Copyright 2008 Eric Niebler. Distributed under the Boost
5 //  Software License, Version 1.0. (See accompanying file
6 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7
8 #ifndef BOOST_XPRESSIVE_DETAIL_STATIC_TRANSFORMS_AS_ALTERNATE_HPP_EAN_04_01_2007
9 #define BOOST_XPRESSIVE_DETAIL_STATIC_TRANSFORMS_AS_ALTERNATE_HPP_EAN_04_01_2007
10
11 // MS compatible compilers support #pragma once
12 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
13 # pragma once
14 #endif
15
16 #include <boost/config.hpp>
17 #include <boost/mpl/assert.hpp>
18 #include <boost/type_traits/is_same.hpp>
19 #include <boost/proto/core.hpp>
20 #include <boost/xpressive/detail/detail_fwd.hpp>
21 #include <boost/xpressive/detail/static/static.hpp>
22 #include <boost/xpressive/detail/core/matcher/alternate_matcher.hpp>
23 #include <boost/xpressive/detail/utility/cons.hpp>
24
25 namespace boost { namespace xpressive
26 {
27     namespace detail
28     {
29         ///////////////////////////////////////////////////////////////////////////////
30         // alternates_list
31         //   a fusion-compatible sequence of alternate expressions, that also keeps
32         //   track of the list's width and purity.
33         template<typename Head, typename Tail>
34         struct alternates_list
35           : fusion::cons<Head, Tail>
36         {
37             BOOST_STATIC_CONSTANT(std::size_t, width = Head::width == Tail::width ? Head::width : detail::unknown_width::value);
38             BOOST_STATIC_CONSTANT(bool, pure = Head::pure && Tail::pure);
39
40             alternates_list(Head const &head, Tail const &tail)
41               : fusion::cons<Head, Tail>(head, tail)
42             {
43             }
44         };
45
46         template<typename Head>
47         struct alternates_list<Head, fusion::nil>
48           : fusion::cons<Head, fusion::nil>
49         {
50             BOOST_STATIC_CONSTANT(std::size_t, width = Head::width);
51             BOOST_STATIC_CONSTANT(bool, pure = Head::pure);
52
53             alternates_list(Head const &head, fusion::nil const &tail)
54               : fusion::cons<Head, fusion::nil>(head, tail)
55             {
56             }
57         };
58     }
59
60     namespace grammar_detail
61     {
62         ///////////////////////////////////////////////////////////////////////////////
63         // in_alternate_list
64         template<typename Grammar, typename Callable = proto::callable>
65         struct in_alternate_list : proto::transform<in_alternate_list<Grammar, Callable> >
66         {
67             template<typename Expr, typename State, typename Data>
68             struct impl : proto::transform_impl<Expr, State, Data>
69             {
70                 typedef
71                     detail::alternates_list<
72                         typename Grammar::template impl<
73                             Expr
74                           , detail::alternate_end_xpression
75                           , Data
76                         >::result_type
77                       , State
78                     >
79                 result_type;
80
81                 result_type operator ()(
82                     typename impl::expr_param expr
83                   , typename impl::state_param state
84                   , typename impl::data_param data
85                 ) const
86                 {
87                     return result_type(
88                         typename Grammar::template impl<Expr, detail::alternate_end_xpression, Data>()(
89                             expr
90                           , detail::alternate_end_xpression()
91                           , data
92                         )
93                       , state
94                     );
95                 }
96             };
97         };
98
99         ///////////////////////////////////////////////////////////////////////////////
100         // as_alternate_matcher
101         template<typename Grammar, typename Callable = proto::callable>
102         struct as_alternate_matcher : proto::transform<as_alternate_matcher<Grammar, Callable> >
103         {
104             template<typename Expr, typename State, typename Data>
105             struct impl : proto::transform_impl<Expr, State, Data>
106             {
107                 typedef typename impl::data data_type;
108                 typedef
109                     detail::alternate_matcher<
110                         typename Grammar::template impl<Expr, State, Data>::result_type
111                       , typename data_type::traits_type
112                     >
113                 result_type;
114
115                 result_type operator ()(
116                     typename impl::expr_param expr
117                   , typename impl::state_param state
118                   , typename impl::data_param data
119                 ) const
120                 {
121                     return result_type(
122                         typename Grammar::template impl<Expr, State, Data>()(expr, state, data)
123                     );
124                 }
125             };
126         };
127     }
128
129 }}
130
131 #endif