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_GRAMMAR_HPP_EAN_11_12_2006
9 #define BOOST_XPRESSIVE_DETAIL_STATIC_GRAMMAR_HPP_EAN_11_12_2006
11 // MS compatible compilers support #pragma once
16 #include <boost/mpl/if.hpp>
17 #include <boost/mpl/bool.hpp>
18 #include <boost/mpl/assert.hpp>
19 #include <boost/proto/core.hpp>
20 #include <boost/xpressive/detail/static/is_pure.hpp>
21 #include <boost/xpressive/detail/static/transforms/as_matcher.hpp>
22 #include <boost/xpressive/detail/static/transforms/as_alternate.hpp>
23 #include <boost/xpressive/detail/static/transforms/as_sequence.hpp>
24 #include <boost/xpressive/detail/static/transforms/as_quantifier.hpp>
25 #include <boost/xpressive/detail/static/transforms/as_marker.hpp>
26 #include <boost/xpressive/detail/static/transforms/as_set.hpp>
27 #include <boost/xpressive/detail/static/transforms/as_independent.hpp>
28 #include <boost/xpressive/detail/static/transforms/as_modifier.hpp>
29 #include <boost/xpressive/detail/static/transforms/as_inverse.hpp>
30 #include <boost/xpressive/detail/static/transforms/as_action.hpp>
31 #include <boost/xpressive/detail/detail_fwd.hpp>
33 #define BOOST_XPRESSIVE_CHECK_REGEX(Expr, Char)\
36 typename boost::mpl::if_c<\
37 boost::xpressive::is_valid_regex<Expr, Char>::value\
39 , boost::xpressive::INVALID_REGULAR_EXPRESSION\
43 //////////////////////////////////////////////////////////////////////////
44 //**********************************************************************//
47 //* Whenever you change this grammar, you MUST also make corresponding *//
48 //* changes to width_of.hpp and is_pure.hpp. *//
50 //**********************************************************************//
51 //////////////////////////////////////////////////////////////////////////
53 namespace boost { namespace xpressive
55 template<typename Char>
58 template<typename Char>
59 struct ActionableGrammar;
61 namespace grammar_detail
63 ///////////////////////////////////////////////////////////////////////////
65 template<typename Char>
68 ///////////////////////////////////////////////////////////////////////////
70 template<typename Char>
73 ///////////////////////////////////////////////////////////////////////////
75 template<typename Char, typename Gram, typename Greedy>
78 make<detail::use_simple_repeat<_child, Char> >
79 , as_simple_quantifier<Gram, Greedy>
80 , as_default_quantifier<Greedy>
84 ///////////////////////////////////////////////////////////////////////////
85 // NonGreedyRepeatCases
86 template<typename Gram>
87 struct NonGreedyRepeatCases
89 template<typename Tag, typename Dummy = void>
94 template<typename Dummy>
95 struct case_<tag::dereference, Dummy>
99 template<typename Dummy>
100 struct case_<tag::unary_plus, Dummy>
104 template<typename Dummy>
105 struct case_<tag::logical_not, Dummy>
109 template<uint_t Min, uint_t Max, typename Dummy>
110 struct case_<detail::generic_quant_tag<Min, Max>, Dummy>
111 : unary_expr<detail::generic_quant_tag<Min, Max>, Gram>
115 ///////////////////////////////////////////////////////////////////////////
117 template<typename Char, typename Gram>
118 struct InvertibleCases
120 template<typename Tag, typename Dummy = void>
125 template<typename Dummy>
126 struct case_<tag::comma, Dummy>
127 : when<ListSet<Char>, as_list_set_matcher<Char> >
130 template<typename Dummy>
131 struct case_<tag::assign, Dummy>
132 : when<ListSet<Char>, as_list_set_matcher<Char> >
135 template<typename Dummy>
136 struct case_<tag::subscript, Dummy>
137 : when<subscript<detail::set_initializer_type, Gram>, call<as_set_matcher<Gram>(_right)> >
140 template<typename Dummy>
141 struct case_<detail::lookahead_tag, Dummy>
143 unary_expr<detail::lookahead_tag, Gram>
148 template<typename Dummy>
149 struct case_<detail::lookbehind_tag, Dummy>
151 unary_expr<detail::lookbehind_tag, Gram>
152 , as_lookbehind<Gram>
156 template<typename Dummy>
157 struct case_<tag::terminal, Dummy>
161 , terminal<detail::posix_charset_placeholder>
162 , terminal<detail::range_placeholder<_> >
163 , terminal<detail::logical_newline_placeholder>
164 , terminal<detail::assert_word_placeholder<detail::word_boundary<mpl::true_> > >
171 ///////////////////////////////////////////////////////////////////////////
173 template<typename Char, typename Gram>
176 template<typename Tag, typename Dummy = void>
181 template<typename Dummy>
182 struct case_<tag::terminal, Dummy>
185 , in_sequence<as_matcher>
189 template<typename Dummy>
190 struct case_<tag::shift_right, Dummy>
192 shift_right<Gram, Gram>
193 , reverse_fold<_, _state, Gram>
197 template<typename Dummy>
198 struct case_<tag::bitwise_or, Dummy>
200 bitwise_or<Gram, Gram>
202 as_alternate_matcher<
203 reverse_fold_tree<_, make<fusion::nil>, in_alternate_list<Gram> >
209 template<typename Dummy, typename Greedy>
210 struct case_<optional_tag<Greedy> , Dummy>
212 unary_expr<optional_tag<Greedy>, Gram>
213 , in_sequence<call<as_optional<Gram, Greedy>(_child)> >
217 template<typename Dummy>
218 struct case_<tag::dereference, Dummy>
221 , call<Gram(as_repeat<Char, Gram, mpl::true_>)>
225 template<typename Dummy>
226 struct case_<tag::unary_plus, Dummy>
229 , call<Gram(as_repeat<Char, Gram, mpl::true_>)>
233 template<typename Dummy>
234 struct case_<tag::logical_not, Dummy>
237 , call<Gram(as_repeat<Char, Gram, mpl::true_>)>
241 template<uint_t Min, uint_t Max, typename Dummy>
242 struct case_<detail::generic_quant_tag<Min, Max>, Dummy>
244 unary_expr<detail::generic_quant_tag<Min, Max>, Gram>
245 , call<Gram(as_repeat<Char, Gram, mpl::true_>)>
249 template<typename Dummy>
250 struct case_<tag::negate, Dummy>
252 negate<switch_<NonGreedyRepeatCases<Gram> > >
253 , call<Gram(call<as_repeat<Char, Gram, mpl::false_>(_child)>)>
257 template<typename Dummy>
258 struct case_<tag::complement, Dummy>
260 complement<switch_<InvertibleCases<Char, Gram> > >
261 , in_sequence<call<as_inverse(call<switch_<InvertibleCases<Char, Gram> >(_child)>)> >
265 template<typename Dummy>
266 struct case_<detail::modifier_tag, Dummy>
267 : when<binary_expr<detail::modifier_tag, _, Gram>, as_modifier<Gram> >
270 template<typename Dummy>
271 struct case_<detail::lookahead_tag, Dummy>
273 unary_expr<detail::lookahead_tag, Gram>
274 , in_sequence<as_lookahead<Gram> >
278 template<typename Dummy>
279 struct case_<detail::lookbehind_tag, Dummy>
281 unary_expr<detail::lookbehind_tag, Gram>
282 , in_sequence<as_lookbehind<Gram> >
286 template<typename Dummy>
287 struct case_<detail::keeper_tag, Dummy>
289 unary_expr<detail::keeper_tag, Gram>
290 , in_sequence<as_keeper<Gram> >
294 template<typename Dummy>
295 struct case_<tag::comma, Dummy>
296 : when<ListSet<Char>, in_sequence<as_list_set_matcher<Char> > >
299 template<typename Dummy>
300 struct case_<tag::assign, Dummy>
302 when<assign<detail::basic_mark_tag, Gram>, call<Gram(as_marker)> >
303 , when<ListSet<Char>, in_sequence<as_list_set_matcher<Char> > >
307 template<typename Dummy>
308 struct case_<tag::subscript, Dummy>
310 when<subscript<detail::set_initializer_type, Gram>, in_sequence<call<as_set_matcher<Gram>(_right)> > >
311 , when<subscript<ActionableGrammar<Char>, _>, call<ActionableGrammar<Char>(as_action)> >
316 ///////////////////////////////////////////////////////////////////////////
318 template<typename Char, typename Gram>
319 struct ActionableCases
321 template<typename Tag, typename Dummy = void>
323 : Cases<Char, Gram>::template case_<Tag>
326 // Only in sub-expressions with actions attached do we allow attribute assignements
327 template<typename Dummy>
328 struct case_<proto::tag::assign, Dummy>
330 typename Cases<Char, Gram>::template case_<proto::tag::assign>
331 , when<proto::assign<terminal<detail::attribute_placeholder<_> >, _>, in_sequence<as_attr_matcher> >
336 } // namespace detail
338 ///////////////////////////////////////////////////////////////////////////
340 template<typename Char>
342 : proto::switch_<grammar_detail::Cases<Char, Grammar<Char> > >
345 template<typename Char>
346 struct ActionableGrammar
347 : proto::switch_<grammar_detail::ActionableCases<Char, ActionableGrammar<Char> > >
350 ///////////////////////////////////////////////////////////////////////////
351 // INVALID_REGULAR_EXPRESSION
352 struct INVALID_REGULAR_EXPRESSION
356 ///////////////////////////////////////////////////////////////////////////
358 template<typename Expr, typename Char>
359 struct is_valid_regex
360 : proto::matches<Expr, Grammar<Char> >
363 }} // namespace boost::xpressive