1 ///////////////////////////////////////////////////////////////////////////////
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)
8 #ifndef BOOST_XPRESSIVE_DETAIL_STATIC_WIDTH_OF_HPP_EAN_10_04_2005
9 #define BOOST_XPRESSIVE_DETAIL_STATIC_WIDTH_OF_HPP_EAN_10_04_2005
11 // MS compatible compilers support #pragma once
16 #include <boost/ref.hpp>
17 #include <boost/mpl/if.hpp>
18 #include <boost/mpl/or.hpp>
19 #include <boost/mpl/plus.hpp>
20 #include <boost/mpl/times.hpp>
21 #include <boost/mpl/assert.hpp>
22 #include <boost/mpl/size_t.hpp>
23 #include <boost/mpl/equal_to.hpp>
24 #include <boost/type_traits/is_same.hpp>
25 #include <boost/xpressive/detail/detail_fwd.hpp>
26 #include <boost/xpressive/detail/static/type_traits.hpp>
27 #include <boost/proto/traits.hpp>
29 namespace boost { namespace xpressive { namespace detail
31 template<typename Expr, typename Char, typename Tag = typename Expr::proto_tag>
34 ///////////////////////////////////////////////////////////////////////////////
37 template<std::size_t N, std::size_t M>
42 template<std::size_t M>
43 struct add_widths<unknown_width::value, M>
47 template<std::size_t N>
48 struct add_widths<N, unknown_width::value>
53 struct add_widths<unknown_width::value, unknown_width::value>
57 ///////////////////////////////////////////////////////////////////////////////
60 template<std::size_t N, std::size_t M>
65 template<std::size_t N>
66 struct or_widths<N, N>
70 ///////////////////////////////////////////////////////////////////////////////
73 template<typename Expr, typename Char, bool IsXpr = is_xpr<Expr>::value>
74 struct width_of_terminal
75 : mpl::size_t<Expr::width> // xpressive literals
78 template<typename Expr, typename Char>
79 struct width_of_terminal<Expr, Char, false>
80 : unknown_width // unknown literals (eg, basic_string, basic_regex, etc.)
83 template<typename Char>
84 struct width_of_terminal<Char, Char, false>
85 : mpl::size_t<1> // char literals
88 template<typename Char>
89 struct width_of_terminal<char, Char, false>
90 : mpl::size_t<1> // char literals
94 struct width_of_terminal<char, char, false>
95 : mpl::size_t<1> // char literals
98 template<typename Elem, std::size_t N, typename Char>
99 struct width_of_terminal<Elem (&) [N], Char, false>
100 : mpl::size_t<N-is_char<Elem>::value> // string literals
103 template<typename Elem, std::size_t N, typename Char>
104 struct width_of_terminal<Elem const (&) [N], Char, false>
105 : mpl::size_t<N-is_char<Elem>::value> // string literals
108 ///////////////////////////////////////////////////////////////////////////////
111 template<typename Expr, typename Char, typename Tag>
115 template<typename Expr, typename Char>
116 struct width_of<Expr, Char, proto::tag::terminal>
117 : width_of_terminal<typename proto::result_of::value<Expr>::type, Char>
120 template<typename Expr, typename Char>
121 struct width_of<Expr, Char, proto::tag::shift_right>
123 width_of<typename remove_reference<typename Expr::proto_child0>::type::proto_base_expr, Char>::value
124 , width_of<typename remove_reference<typename Expr::proto_child1>::type::proto_base_expr, Char>::value
128 template<typename Expr, typename Char>
129 struct width_of<Expr, Char, proto::tag::bitwise_or>
131 width_of<typename remove_reference<typename Expr::proto_child0>::type::proto_base_expr, Char>::value
132 , width_of<typename remove_reference<typename Expr::proto_child1>::type::proto_base_expr, Char>::value
136 template<typename Expr, typename Char, typename Left>
137 struct width_of_assign
140 template<typename Expr, typename Char>
141 struct width_of_assign<Expr, Char, mark_placeholder>
142 : width_of<typename remove_reference<typename Expr::proto_child1>::type::proto_base_expr, Char>
145 template<typename Expr, typename Char>
146 struct width_of_assign<Expr, Char, set_initializer>
150 template<typename Expr, typename Char, typename Nbr>
151 struct width_of_assign<Expr, Char, attribute_placeholder<Nbr> >
155 // either (s1 = ...) or (a1 = ...) or (set = ...)
156 template<typename Expr, typename Char>
157 struct width_of<Expr, Char, proto::tag::assign>
161 , typename proto::result_of::value<
162 typename remove_reference<typename Expr::proto_child0>::type::proto_base_expr
167 template<typename Expr, typename Char>
168 struct width_of<Expr, Char, modifier_tag>
169 : width_of<typename remove_reference<typename Expr::proto_child1>::type::proto_base_expr, Char>
172 template<typename Expr, typename Char>
173 struct width_of<Expr, Char, lookahead_tag>
177 template<typename Expr, typename Char>
178 struct width_of<Expr, Char, lookbehind_tag>
182 // keep() is used to turn off backtracking, so they should only be used
183 // for things that are variable-width (eg. quantified)
184 template<typename Expr, typename Char>
185 struct width_of<Expr, Char, keeper_tag>
188 // TODO: keep() now has a second meaning: execute actions immediately.
189 // In that sense, it is perfectly reasonable to put a fixed-width
190 // sub-expression in a keep. Can fixed-width keep() sub-expressions
191 // use the simple_repeat_matcher?
194 template<typename Expr, typename Char>
195 struct width_of<Expr, Char, proto::tag::unary_plus>
199 template<typename Expr, typename Char>
200 struct width_of<Expr, Char, proto::tag::dereference>
204 template<typename Expr, typename Char>
205 struct width_of<Expr, Char, proto::tag::logical_not>
209 template<typename Expr, typename Char, uint_t Min, uint_t Max>
210 struct width_of<Expr, Char, generic_quant_tag<Min, Max> >
214 template<typename Expr, typename Char, uint_t Count>
215 struct width_of<Expr, Char, generic_quant_tag<Count, Count> >
217 mpl::equal_to<unknown_width, width_of<typename remove_reference<typename Expr::proto_child0>::type::proto_base_expr, Char> >::value
220 width_of<typename remove_reference<typename Expr::proto_child0>::type::proto_base_expr, Char>
226 template<typename Expr, typename Char>
227 struct width_of<Expr, Char, proto::tag::negate>
228 : width_of<typename remove_reference<typename Expr::proto_child0>::type::proto_base_expr, Char>
231 // when complementing a set or an assertion, the width is that of the set (1) or the assertion (0)
232 template<typename Expr, typename Char>
233 struct width_of<Expr, Char, proto::tag::complement>
234 : width_of<typename remove_reference<typename Expr::proto_child0>::type::proto_base_expr, Char>
237 // The comma is used in list-initialized sets, and the width of sets are 1
238 template<typename Expr, typename Char>
239 struct width_of<Expr, Char, proto::tag::comma>
243 // The subscript operator[] is used for sets, as in set['a' | range('b','h')],
244 // or for actions as in (any >> expr)[ action ]
245 template<typename Expr, typename Char, typename Left>
246 struct width_of_subscript
247 : width_of<Left, Char>
250 template<typename Expr, typename Char>
251 struct width_of_subscript<Expr, Char, set_initializer_type>
254 // If Left is "set" then make sure that Right has a width_of 1
255 BOOST_MPL_ASSERT_RELATION(
258 , (width_of<typename remove_reference<typename Expr::proto_child1>::type::proto_base_expr, Char>::value));
261 template<typename Expr, typename Char>
262 struct width_of<Expr, Char, proto::tag::subscript>
263 : width_of_subscript<Expr, Char, typename remove_reference<typename Expr::proto_child0>::type::proto_base_expr>
266 }}} // namespace boost::xpressive::detail