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_TRANSFORMS_AS_SET_HPP_EAN_04_05_2007
9 #define BOOST_XPRESSIVE_DETAIL_STATIC_TRANSFORMS_AS_SET_HPP_EAN_04_05_2007
11 // MS compatible compilers support #pragma once
16 #include <boost/mpl/assert.hpp>
17 #include <boost/proto/core.hpp>
18 #include <boost/xpressive/detail/detail_fwd.hpp>
19 #include <boost/xpressive/detail/static/static.hpp>
20 #include <boost/xpressive/detail/utility/chset/chset.hpp>
21 #include <boost/xpressive/detail/utility/traits_utils.hpp>
23 namespace boost { namespace xpressive { namespace grammar_detail
26 ///////////////////////////////////////////////////////////////////////////
28 template<typename Char>
37 struct CharLiteral<char>
41 ///////////////////////////////////////////////////////////////////////////
43 // matches expressions like (set= 'a','b','c')
44 // calculates the size of the set
45 template<typename Char>
49 comma<ListSet<Char>, CharLiteral<Char> >
50 , make<mpl::next<call<ListSet<Char>(_left)> > > // TODO make a custom transform for this...
53 assign<detail::set_initializer_type, CharLiteral<Char> >
59 template<typename Char, typename Traits>
60 void fill_list_set(Char *&, detail::set_initializer_type, Traits const &)
63 template<typename Char, typename Expr, typename Traits>
64 void fill_list_set(Char *&buffer, Expr const &expr, Traits const &traits)
66 fill_list_set(buffer, proto::left(expr), traits);
67 *buffer++ = traits.translate(detail::char_cast<Char>(proto::value(proto::right(expr)), traits));
70 ///////////////////////////////////////////////////////////////////////////////
71 // as_list_set_matcher
72 template<typename Char, typename Callable = proto::callable>
73 struct as_list_set_matcher : proto::transform<as_list_set_matcher<Char, Callable> >
75 template<typename Expr, typename State, typename Data>
76 struct impl : proto::transform_impl<Expr, State, Data>
78 typedef typename impl::data data_type;
81 typename data_type::traits_type
82 , typename ListSet<Char>::template impl<Expr, State, Data>::result_type
86 result_type operator ()(
87 typename impl::expr_param expr
88 , typename impl::state_param
89 , typename impl::data_param data
93 typedef typename impl::data data_type;
94 typename data_type::char_type *buffer = set.set_;
95 fill_list_set(buffer, expr, data.traits());
101 ///////////////////////////////////////////////////////////////////////////////
104 template<typename Grammar, typename CharSet, typename Data>
107 typedef typename Data::traits_type traits_type;
108 typedef typename CharSet::char_type char_type;
109 typedef typename CharSet::icase_type icase_type;
111 merge_charset(CharSet &charset, Data &data)
116 template<typename Expr>
117 void operator ()(Expr const &expr) const
119 this->call_(expr, typename Expr::proto_tag());
123 merge_charset &operator =(merge_charset const &);
125 template<typename Expr, typename Tag>
126 void call_(Expr const &expr, Tag) const
129 typename Grammar::template impl<Expr const &, detail::end_xpression, Data &>()(
131 , detail::end_xpression()
137 template<typename Expr>
138 void call_(Expr const &expr, tag::bitwise_or) const
140 (*this)(proto::left(expr));
141 (*this)(proto::right(expr));
144 template<typename Not>
145 void set_(detail::literal_matcher<traits_type, icase_type, Not> const &ch) const
148 BOOST_MPL_ASSERT_NOT((Not));
149 set_char(this->charset_.charset_, ch.ch_, this->visitor_.traits(), icase_type());
152 void set_(detail::range_matcher<traits_type, icase_type> const &rg) const
155 BOOST_ASSERT(!rg.not_);
156 set_range(this->charset_.charset_, rg.ch_min_, rg.ch_max_, this->visitor_.traits(), icase_type());
159 template<typename Size>
160 void set_(detail::set_matcher<traits_type, Size> const &set_) const
163 BOOST_ASSERT(!set_.not_);
164 for(int i = 0; i < Size::value; ++i)
166 set_char(this->charset_.charset_, set_.set_[i], this->visitor_.traits(), icase_type());
170 void set_(detail::posix_charset_matcher<traits_type> const &posix) const
172 set_class(this->charset_.charset_, posix.mask_, posix.not_, this->visitor_.traits());
179 ///////////////////////////////////////////////////////////////////////////////
181 template<typename Grammar, typename Callable = proto::callable>
182 struct as_set_matcher : proto::transform<as_set_matcher<Grammar, Callable> >
184 template<typename Expr, typename State, typename Data>
185 struct impl : proto::transform_impl<Expr, State, Data>
187 typedef typename impl::data data_type;
188 typedef typename data_type::char_type char_type;
190 // if sizeof(char_type)==1, merge everything into a basic_chset
191 // BUGBUG this is not optimal.
194 detail::is_narrow_char<char_type>::value
195 , detail::basic_chset<char_type>
196 , detail::compound_charset<typename data_type::traits_type>
201 detail::charset_matcher<
202 typename data_type::traits_type
203 , typename data_type::icase_type
208 result_type operator ()(
209 typename impl::expr_param expr
210 , typename impl::state_param
211 , typename impl::data_param data
215 merge_charset<Grammar, result_type, typename impl::data> merge(matcher, data);
216 merge(expr); // Walks the tree and fills in the charset