1 ///////////////////////////////////////////////////////////////////////////////
2 /// \file regex_actions.hpp
3 /// Defines the syntax elements of xpressive's action expressions.
5 // Copyright 2008 Eric Niebler. Distributed under the Boost
6 // Software License, Version 1.0. (See accompanying file
7 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 #ifndef BOOST_XPRESSIVE_ACTIONS_HPP_EAN_03_22_2007
10 #define BOOST_XPRESSIVE_ACTIONS_HPP_EAN_03_22_2007
12 // MS compatible compilers support #pragma once
17 #include <boost/config.hpp>
18 #include <boost/preprocessor/punctuation/comma_if.hpp>
19 #include <boost/ref.hpp>
20 #include <boost/mpl/if.hpp>
21 #include <boost/mpl/or.hpp>
22 #include <boost/mpl/int.hpp>
23 #include <boost/mpl/assert.hpp>
24 #include <boost/noncopyable.hpp>
25 #include <boost/lexical_cast.hpp>
26 #include <boost/throw_exception.hpp>
27 #include <boost/utility/enable_if.hpp>
28 #include <boost/type_traits/is_same.hpp>
29 #include <boost/type_traits/is_const.hpp>
30 #include <boost/type_traits/is_integral.hpp>
31 #include <boost/type_traits/decay.hpp>
32 #include <boost/type_traits/remove_cv.hpp>
33 #include <boost/type_traits/remove_reference.hpp>
34 #include <boost/range/iterator_range.hpp>
35 #include <boost/xpressive/detail/detail_fwd.hpp>
36 #include <boost/xpressive/detail/core/state.hpp>
37 #include <boost/xpressive/detail/core/matcher/attr_matcher.hpp>
38 #include <boost/xpressive/detail/core/matcher/attr_end_matcher.hpp>
39 #include <boost/xpressive/detail/core/matcher/attr_begin_matcher.hpp>
40 #include <boost/xpressive/detail/core/matcher/predicate_matcher.hpp>
41 #include <boost/xpressive/detail/utility/ignore_unused.hpp>
42 #include <boost/xpressive/detail/static/type_traits.hpp>
44 // These are very often needed by client code.
45 #include <boost/typeof/std/map.hpp>
46 #include <boost/typeof/std/string.hpp>
48 // Doxygen can't handle proto :-(
49 #ifndef BOOST_XPRESSIVE_DOXYGEN_INVOKED
50 # include <boost/proto/core.hpp>
51 # include <boost/proto/transform.hpp>
52 # include <boost/xpressive/detail/core/matcher/action_matcher.hpp>
57 #pragma warning(disable : 4510) // default constructor could not be generated
58 #pragma warning(disable : 4512) // assignment operator could not be generated
59 #pragma warning(disable : 4610) // can never be instantiated - user defined constructor required
62 namespace boost { namespace xpressive
67 template<typename T, typename U>
71 typedef typename add_reference<T>::type reference;
73 reference cast(void *pv) const
75 return *static_cast<typename remove_reference<T>::type *>(pv);
87 value_wrapper(T const &t)
99 BOOST_PROTO_CALLABLE()
100 template<typename Sig>
103 template<typename This, typename MatchResults, typename Expr>
104 struct result<This(MatchResults, Expr)>
109 template<typename MatchResults, typename Expr>
110 Expr const & operator ()(MatchResults &what, Expr const &expr) const
120 // let(_a = b, _c = d)
123 proto::terminal<let_tag>
126 proto::assign<proto::_, proto::_>
127 , proto::call<BindArg(proto::_data, proto::_)>
134 : boost::proto::domain<boost::proto::pod_generator<let_> >
137 template<typename Expr>
140 BOOST_PROTO_BASIC_EXTENDS(Expr, let_<Expr>, let_domain)
141 BOOST_PROTO_EXTENDS_FUNCTION()
144 template<typename Args, typename BidiIter>
145 void bind_args(let_<Args> const &args, match_results<BidiIter> &what)
147 BindArgs()(args, 0, what);
150 typedef boost::proto::functional::make_expr<proto::tag::function, proto::default_domain> make_function;
155 /// \brief \c at is a PolymorphicFunctionObject for indexing into a sequence
158 BOOST_PROTO_CALLABLE()
159 template<typename Sig>
162 template<typename This, typename Cont, typename Idx>
163 struct result<This(Cont &, Idx)>
165 typedef typename Cont::reference type;
168 template<typename This, typename Cont, typename Idx>
169 struct result<This(Cont const &, Idx)>
171 typedef typename Cont::const_reference type;
174 template<typename This, typename Cont, typename Idx>
175 struct result<This(Cont, Idx)>
177 typedef typename Cont::const_reference type;
180 /// \pre \c Cont is a model of RandomAccessSequence
181 /// \param c The RandomAccessSequence to index into
182 /// \param idx The index
183 /// \return <tt>c[idx]</tt>
184 template<typename Cont, typename Idx>
185 typename Cont::reference operator()(Cont &c, Idx idx BOOST_PROTO_DISABLE_IF_IS_CONST(Cont)) const
192 template<typename Cont, typename Idx>
193 typename Cont::const_reference operator()(Cont const &c, Idx idx) const
199 /// \brief \c push is a PolymorphicFunctionObject for pushing an element into a container.
202 BOOST_PROTO_CALLABLE()
203 typedef void result_type;
205 /// \param seq The sequence into which the value should be pushed.
206 /// \param val The value to push into the sequence.
207 /// \brief Equivalent to <tt>seq.push(val)</tt>.
209 template<typename Sequence, typename Value>
210 void operator()(Sequence &seq, Value const &val) const
216 /// \brief \c push_back is a PolymorphicFunctionObject for pushing an element into the back of a container.
219 BOOST_PROTO_CALLABLE()
220 typedef void result_type;
222 /// \param seq The sequence into which the value should be pushed.
223 /// \param val The value to push into the sequence.
224 /// \brief Equivalent to <tt>seq.push_back(val)</tt>.
226 template<typename Sequence, typename Value>
227 void operator()(Sequence &seq, Value const &val) const
233 /// \brief \c push_front is a PolymorphicFunctionObject for pushing an element into the front of a container.
236 BOOST_PROTO_CALLABLE()
237 typedef void result_type;
239 /// \param seq The sequence into which the value should be pushed.
240 /// \param val The value to push into the sequence.
241 /// \brief Equivalent to <tt>seq.push_front(val)</tt>.
243 template<typename Sequence, typename Value>
244 void operator()(Sequence &seq, Value const &val) const
250 /// \brief \c pop is a PolymorphicFunctionObject for popping an element from a container.
253 BOOST_PROTO_CALLABLE()
254 typedef void result_type;
256 /// \param seq The sequence from which to pop.
257 /// \brief Equivalent to <tt>seq.pop()</tt>.
259 template<typename Sequence>
260 void operator()(Sequence &seq) const
266 /// \brief \c pop_back is a PolymorphicFunctionObject for popping an element from the back of a container.
269 BOOST_PROTO_CALLABLE()
270 typedef void result_type;
272 /// \param seq The sequence from which to pop.
273 /// \brief Equivalent to <tt>seq.pop_back()</tt>.
275 template<typename Sequence>
276 void operator()(Sequence &seq) const
282 /// \brief \c pop_front is a PolymorphicFunctionObject for popping an element from the front of a container.
285 BOOST_PROTO_CALLABLE()
286 typedef void result_type;
288 /// \param seq The sequence from which to pop.
289 /// \brief Equivalent to <tt>seq.pop_front()</tt>.
291 template<typename Sequence>
292 void operator()(Sequence &seq) const
298 /// \brief \c front is a PolymorphicFunctionObject for fetching the front element of a container.
301 BOOST_PROTO_CALLABLE()
302 template<typename Sig>
305 template<typename This, typename Sequence>
306 struct result<This(Sequence)>
308 typedef typename remove_reference<Sequence>::type sequence_type;
311 is_const<sequence_type>::value
312 , typename sequence_type::const_reference
313 , typename sequence_type::reference
318 /// \param seq The sequence from which to fetch the front.
319 /// \return <tt>seq.front()</tt>
320 template<typename Sequence>
321 typename result<front(Sequence &)>::type operator()(Sequence &seq) const
327 /// \brief \c back is a PolymorphicFunctionObject for fetching the back element of a container.
330 BOOST_PROTO_CALLABLE()
331 template<typename Sig>
334 template<typename This, typename Sequence>
335 struct result<This(Sequence)>
337 typedef typename remove_reference<Sequence>::type sequence_type;
340 is_const<sequence_type>::value
341 , typename sequence_type::const_reference
342 , typename sequence_type::reference
347 /// \param seq The sequence from which to fetch the back.
348 /// \return <tt>seq.back()</tt>
349 template<typename Sequence>
350 typename result<back(Sequence &)>::type operator()(Sequence &seq) const
356 /// \brief \c top is a PolymorphicFunctionObject for fetching the top element of a stack.
359 BOOST_PROTO_CALLABLE()
360 template<typename Sig>
363 template<typename This, typename Sequence>
364 struct result<This(Sequence)>
366 typedef typename remove_reference<Sequence>::type sequence_type;
369 is_const<sequence_type>::value
370 , typename sequence_type::value_type const &
371 , typename sequence_type::value_type &
376 /// \param seq The sequence from which to fetch the top.
377 /// \return <tt>seq.top()</tt>
378 template<typename Sequence>
379 typename result<top(Sequence &)>::type operator()(Sequence &seq) const
385 /// \brief \c first is a PolymorphicFunctionObject for fetching the first element of a pair.
388 BOOST_PROTO_CALLABLE()
389 template<typename Sig>
392 template<typename This, typename Pair>
393 struct result<This(Pair)>
395 typedef typename remove_reference<Pair>::type::first_type type;
398 /// \param p The pair from which to fetch the first element.
399 /// \return <tt>p.first</tt>
400 template<typename Pair>
401 typename Pair::first_type operator()(Pair const &p) const
407 /// \brief \c second is a PolymorphicFunctionObject for fetching the second element of a pair.
410 BOOST_PROTO_CALLABLE()
411 template<typename Sig>
414 template<typename This, typename Pair>
415 struct result<This(Pair)>
417 typedef typename remove_reference<Pair>::type::second_type type;
420 /// \param p The pair from which to fetch the second element.
421 /// \return <tt>p.second</tt>
422 template<typename Pair>
423 typename Pair::second_type operator()(Pair const &p) const
429 /// \brief \c matched is a PolymorphicFunctionObject for assessing whether a \c sub_match object
433 BOOST_PROTO_CALLABLE()
434 typedef bool result_type;
436 /// \param sub The \c sub_match object.
437 /// \return <tt>sub.matched</tt>
438 template<typename Sub>
439 bool operator()(Sub const &sub) const
445 /// \brief \c length is a PolymorphicFunctionObject for fetching the length of \c sub_match.
448 BOOST_PROTO_CALLABLE()
449 template<typename Sig>
452 template<typename This, typename Sub>
453 struct result<This(Sub)>
455 typedef typename remove_reference<Sub>::type::difference_type type;
458 /// \param sub The \c sub_match object.
459 /// \return <tt>sub.length()</tt>
460 template<typename Sub>
461 typename Sub::difference_type operator()(Sub const &sub) const
467 /// \brief \c str is a PolymorphicFunctionObject for turning a \c sub_match into an
468 /// equivalent \c std::string.
471 BOOST_PROTO_CALLABLE()
472 template<typename Sig>
475 template<typename This, typename Sub>
476 struct result<This(Sub)>
478 typedef typename remove_reference<Sub>::type::string_type type;
481 /// \param sub The \c sub_match object.
482 /// \return <tt>sub.str()</tt>
483 template<typename Sub>
484 typename Sub::string_type operator()(Sub const &sub) const
490 // This codifies the return types of the various insert member
491 // functions found in sequence containers, the 2 flavors of
492 // associative containers, and strings.
494 /// \brief \c insert is a PolymorphicFunctionObject for inserting a value or a
495 /// sequence of values into a sequence container, an associative
496 /// container, or a string.
499 BOOST_PROTO_CALLABLE()
505 template<typename Sig, typename EnableIf = void>
510 template<typename This, typename Cont, typename Value>
511 struct result_detail<This(Cont, Value), void>
513 typedef typename remove_reference<Cont>::type cont_type;
514 typedef typename remove_reference<Value>::type value_type;
515 static cont_type &scont_;
516 static value_type &svalue_;
517 typedef char yes_type;
518 typedef char (&no_type)[2];
519 static yes_type check_insert_return(typename cont_type::iterator);
520 static no_type check_insert_return(std::pair<typename cont_type::iterator, bool>);
521 BOOST_STATIC_CONSTANT(bool, is_iterator = (sizeof(yes_type) == sizeof(check_insert_return(scont_.insert(svalue_)))));
525 , typename cont_type::iterator
526 , std::pair<typename cont_type::iterator, bool>
531 // sequence containers, assoc containers, strings
532 template<typename This, typename Cont, typename It, typename Value>
533 struct result_detail<This(Cont, It, Value),
536 is_integral<typename remove_cv<typename remove_reference<It>::type>::type>
538 typename remove_cv<typename remove_reference<It>::type>::type
539 , typename remove_cv<typename remove_reference<Value>::type>::type
545 typedef typename remove_reference<Cont>::type::iterator type;
549 template<typename This, typename Cont, typename Size, typename T>
550 struct result_detail<This(Cont, Size, T),
552 is_integral<typename remove_cv<typename remove_reference<Size>::type>::type>
556 typedef typename remove_reference<Cont>::type &type;
560 template<typename This, typename Cont, typename It>
561 struct result_detail<This(Cont, It, It), void>
566 // sequence containers, strings
567 template<typename This, typename Cont, typename It, typename Size, typename Value>
568 struct result_detail<This(Cont, It, Size, Value),
570 is_integral<typename remove_cv<typename remove_reference<It>::type>::type>
578 template<typename This, typename Cont, typename Size, typename A0, typename A1>
579 struct result_detail<This(Cont, Size, A0, A1),
581 is_integral<typename remove_cv<typename remove_reference<Size>::type>::type>
585 typedef typename remove_reference<Cont>::type &type;
589 template<typename This, typename Cont, typename Pos0, typename String, typename Pos1, typename Length>
590 struct result_detail<This(Cont, Pos0, String, Pos1, Length)>
592 typedef typename remove_reference<Cont>::type &type;
596 template<typename Sig>
599 typedef typename detail::result_detail<Sig>::type type;
604 template<typename Cont, typename A0>
605 typename result<insert(Cont &, A0 const &)>::type
606 operator()(Cont &cont, A0 const &a0) const
608 return cont.insert(a0);
613 template<typename Cont, typename A0, typename A1>
614 typename result<insert(Cont &, A0 const &, A1 const &)>::type
615 operator()(Cont &cont, A0 const &a0, A1 const &a1) const
617 return cont.insert(a0, a1);
622 template<typename Cont, typename A0, typename A1, typename A2>
623 typename result<insert(Cont &, A0 const &, A1 const &, A2 const &)>::type
624 operator()(Cont &cont, A0 const &a0, A1 const &a1, A2 const &a2) const
626 return cont.insert(a0, a1, a2);
629 /// \param cont The container into which to insert the element(s)
630 /// \param a0 A value, iterator, or count
631 /// \param a1 A value, iterator, string, count, or character
632 /// \param a2 A value, iterator, or count
633 /// \param a3 A count
634 /// \return \li For the form <tt>insert()(cont, a0)</tt>, return <tt>cont.insert(a0)</tt>.
635 /// \li For the form <tt>insert()(cont, a0, a1)</tt>, return <tt>cont.insert(a0, a1)</tt>.
636 /// \li For the form <tt>insert()(cont, a0, a1, a2)</tt>, return <tt>cont.insert(a0, a1, a2)</tt>.
637 /// \li For the form <tt>insert()(cont, a0, a1, a2, a3)</tt>, return <tt>cont.insert(a0, a1, a2, a3)</tt>.
638 template<typename Cont, typename A0, typename A1, typename A2, typename A3>
639 typename result<insert(Cont &, A0 const &, A1 const &, A2 const &, A3 const &)>::type
640 operator()(Cont &cont, A0 const &a0, A1 const &a1, A2 const &a2, A3 const &a3) const
642 return cont.insert(a0, a1, a2, a3);
646 /// \brief \c make_pair is a PolymorphicFunctionObject for building a \c std::pair out of two parameters
649 BOOST_PROTO_CALLABLE()
650 template<typename Sig>
653 template<typename This, typename First, typename Second>
654 struct result<This(First, Second)>
656 /// \brief For exposition only
657 typedef typename decay<First>::type first_type;
658 /// \brief For exposition only
659 typedef typename decay<Second>::type second_type;
660 typedef std::pair<first_type, second_type> type;
663 /// \param first The first element of the pair
664 /// \param second The second element of the pair
665 /// \return <tt>std::make_pair(first, second)</tt>
666 template<typename First, typename Second>
667 std::pair<First, Second> operator()(First const &first, Second const &second) const
669 return std::make_pair(first, second);
673 /// \brief \c as\<\> is a PolymorphicFunctionObject for lexically casting a parameter to a different type.
674 /// \tparam T The type to which to lexically cast the parameter.
678 BOOST_PROTO_CALLABLE()
679 typedef T result_type;
681 /// \param val The value to lexically cast.
682 /// \return <tt>boost::lexical_cast\<T\>(val)</tt>
683 template<typename Value>
684 T operator()(Value const &val) const
686 return boost::lexical_cast<T>(val);
689 // Hack around some limitations in boost::lexical_cast
691 T operator()(csub_match const &val) const
694 ? boost::lexical_cast<T>(boost::make_iterator_range(val.first, val.second))
695 : boost::lexical_cast<T>("");
698 #ifndef BOOST_XPRESSIVE_NO_WREGEX
700 T operator()(wcsub_match const &val) const
703 ? boost::lexical_cast<T>(boost::make_iterator_range(val.first, val.second))
704 : boost::lexical_cast<T>("");
709 template<typename BidiIter>
710 T operator()(sub_match<BidiIter> const &val) const
712 // If this assert fires, you're trying to coerce a sequences of non-characters
713 // to some other type. Xpressive doesn't know how to do that.
714 typedef typename iterator_value<BidiIter>::type char_type;
715 BOOST_MPL_ASSERT_MSG(
716 (xpressive::detail::is_char<char_type>::value)
717 , CAN_ONLY_CONVERT_FROM_CHARACTER_SEQUENCES
720 return this->impl(val, xpressive::detail::is_string_iterator<BidiIter>());
725 template<typename RandIter>
726 T impl(sub_match<RandIter> const &val, mpl::true_) const
729 ? boost::lexical_cast<T>(boost::make_iterator_range(&*val.first, &*val.first + (val.second - val.first)))
730 : boost::lexical_cast<T>("");
734 template<typename BidiIter>
735 T impl(sub_match<BidiIter> const &val, mpl::false_) const
737 return boost::lexical_cast<T>(val.str());
741 /// \brief \c static_cast_\<\> is a PolymorphicFunctionObject for statically casting a parameter to a different type.
742 /// \tparam T The type to which to statically cast the parameter.
746 BOOST_PROTO_CALLABLE()
747 typedef T result_type;
749 /// \param val The value to statically cast.
750 /// \return <tt>static_cast\<T\>(val)</tt>
751 template<typename Value>
752 T operator()(Value const &val) const
754 return static_cast<T>(val);
758 /// \brief \c dynamic_cast_\<\> is a PolymorphicFunctionObject for dynamically casting a parameter to a different type.
759 /// \tparam T The type to which to dynamically cast the parameter.
763 BOOST_PROTO_CALLABLE()
764 typedef T result_type;
766 /// \param val The value to dynamically cast.
767 /// \return <tt>dynamic_cast\<T\>(val)</tt>
768 template<typename Value>
769 T operator()(Value const &val) const
771 return dynamic_cast<T>(val);
775 /// \brief \c const_cast_\<\> is a PolymorphicFunctionObject for const-casting a parameter to a cv qualification.
776 /// \tparam T The type to which to const-cast the parameter.
780 BOOST_PROTO_CALLABLE()
781 typedef T result_type;
783 /// \param val The value to const-cast.
784 /// \pre Types \c T and \c Value differ only in cv-qualification.
785 /// \return <tt>const_cast\<T\>(val)</tt>
786 template<typename Value>
787 T operator()(Value const &val) const
789 return const_cast<T>(val);
793 /// \brief \c construct\<\> is a PolymorphicFunctionObject for constructing a new object.
794 /// \tparam T The type of the object to construct.
798 BOOST_PROTO_CALLABLE()
799 typedef T result_type;
808 template<typename A0>
809 T operator()(A0 const &a0) const
815 template<typename A0, typename A1>
816 T operator()(A0 const &a0, A1 const &a1) const
821 /// \param a0 The first argument to the constructor
822 /// \param a1 The second argument to the constructor
823 /// \param a2 The third argument to the constructor
824 /// \return <tt>T(a0,a1,...)</tt>
825 template<typename A0, typename A1, typename A2>
826 T operator()(A0 const &a0, A1 const &a1, A2 const &a2) const
828 return T(a0, a1, a2);
832 /// \brief \c throw_\<\> is a PolymorphicFunctionObject for throwing an exception.
833 /// \tparam Except The type of the object to throw.
834 template<typename Except>
837 BOOST_PROTO_CALLABLE()
838 typedef void result_type;
841 void operator()() const
843 BOOST_THROW_EXCEPTION(Except());
847 template<typename A0>
848 void operator()(A0 const &a0) const
850 BOOST_THROW_EXCEPTION(Except(a0));
854 template<typename A0, typename A1>
855 void operator()(A0 const &a0, A1 const &a1) const
857 BOOST_THROW_EXCEPTION(Except(a0, a1));
860 /// \param a0 The first argument to the constructor
861 /// \param a1 The second argument to the constructor
862 /// \param a2 The third argument to the constructor
863 /// \throw <tt>Except(a0,a1,...)</tt>
864 /// \note This function makes use of the \c BOOST_THROW_EXCEPTION macro
865 /// to actually throw the exception. See the documentation for the
866 /// Boost.Exception library.
867 template<typename A0, typename A1, typename A2>
868 void operator()(A0 const &a0, A1 const &a1, A2 const &a2) const
870 BOOST_THROW_EXCEPTION(Except(a0, a1, a2));
874 /// \brief \c unwrap_reference is a PolymorphicFunctionObject for unwrapping a <tt>boost::reference_wrapper\<\></tt>.
875 struct unwrap_reference
877 BOOST_PROTO_CALLABLE()
878 template<typename Sig>
881 template<typename This, typename Ref>
882 struct result<This(Ref)>
884 typedef typename boost::unwrap_reference<Ref>::type &type;
887 template<typename This, typename Ref>
888 struct result<This(Ref &)>
890 typedef typename boost::unwrap_reference<Ref>::type &type;
893 /// \param r The <tt>boost::reference_wrapper\<T\></tt> to unwrap.
894 /// \return <tt>static_cast\<T &\>(r)</tt>
896 T &operator()(boost::reference_wrapper<T> r) const
898 return static_cast<T &>(r);
903 /// \brief A unary metafunction that turns an ordinary function object type into the type of
904 /// a deferred function object for use in xpressive semantic actions.
906 /// Use \c xpressive::function\<\> to turn an ordinary polymorphic function object type
907 /// into a type that can be used to declare an object for use in xpressive semantic actions.
909 /// For example, the global object \c xpressive::push_back can be used to create deferred actions
910 /// that have the effect of pushing a value into a container. It is defined with
911 /// \c xpressive::function\<\> as follows:
914 xpressive::function<xpressive::op::push_back>::type const push_back = {};
918 /// where \c op::push_back is an ordinary function object that pushes its second argument into
919 /// its first. Thus defined, \c xpressive::push_back can be used in semantic actions as follows:
922 namespace xp = boost::xpressive;
924 std::list<int> result;
925 std::string str("1 23 456 7890");
926 xp::sregex rx = (+_d)[ xp::push_back(xp::ref(result), xp::as<int>(_) ]
927 >> *(' ' >> (+_d)[ xp::push_back(xp::ref(result), xp::as<int>(_) ) ]);
930 template<typename PolymorphicFunctionObject>
933 typedef typename proto::terminal<PolymorphicFunctionObject>::type type;
936 /// \brief \c at is a lazy PolymorphicFunctionObject for indexing into a sequence in an
937 /// xpressive semantic action.
938 function<op::at>::type const at = {{}};
940 /// \brief \c push is a lazy PolymorphicFunctionObject for pushing a value into a container in an
941 /// xpressive semantic action.
942 function<op::push>::type const push = {{}};
944 /// \brief \c push_back is a lazy PolymorphicFunctionObject for pushing a value into a container in an
945 /// xpressive semantic action.
946 function<op::push_back>::type const push_back = {{}};
948 /// \brief \c push_front is a lazy PolymorphicFunctionObject for pushing a value into a container in an
949 /// xpressive semantic action.
950 function<op::push_front>::type const push_front = {{}};
952 /// \brief \c pop is a lazy PolymorphicFunctionObject for popping the top element from a sequence in an
953 /// xpressive semantic action.
954 function<op::pop>::type const pop = {{}};
956 /// \brief \c pop_back is a lazy PolymorphicFunctionObject for popping the back element from a sequence in an
957 /// xpressive semantic action.
958 function<op::pop_back>::type const pop_back = {{}};
960 /// \brief \c pop_front is a lazy PolymorphicFunctionObject for popping the front element from a sequence in an
961 /// xpressive semantic action.
962 function<op::pop_front>::type const pop_front = {{}};
964 /// \brief \c top is a lazy PolymorphicFunctionObject for accessing the top element from a stack in an
965 /// xpressive semantic action.
966 function<op::top>::type const top = {{}};
968 /// \brief \c back is a lazy PolymorphicFunctionObject for fetching the back element of a sequence in an
969 /// xpressive semantic action.
970 function<op::back>::type const back = {{}};
972 /// \brief \c front is a lazy PolymorphicFunctionObject for fetching the front element of a sequence in an
973 /// xpressive semantic action.
974 function<op::front>::type const front = {{}};
976 /// \brief \c first is a lazy PolymorphicFunctionObject for accessing the first element of a \c std::pair\<\> in an
977 /// xpressive semantic action.
978 function<op::first>::type const first = {{}};
980 /// \brief \c second is a lazy PolymorphicFunctionObject for accessing the second element of a \c std::pair\<\> in an
981 /// xpressive semantic action.
982 function<op::second>::type const second = {{}};
984 /// \brief \c matched is a lazy PolymorphicFunctionObject for accessing the \c matched member of a \c xpressive::sub_match\<\> in an
985 /// xpressive semantic action.
986 function<op::matched>::type const matched = {{}};
988 /// \brief \c length is a lazy PolymorphicFunctionObject for computing the length of a \c xpressive::sub_match\<\> in an
989 /// xpressive semantic action.
990 function<op::length>::type const length = {{}};
992 /// \brief \c str is a lazy PolymorphicFunctionObject for converting a \c xpressive::sub_match\<\> to a \c std::basic_string\<\> in an
993 /// xpressive semantic action.
994 function<op::str>::type const str = {{}};
996 /// \brief \c insert is a lazy PolymorphicFunctionObject for inserting a value or a range of values into a sequence in an
997 /// xpressive semantic action.
998 function<op::insert>::type const insert = {{}};
1000 /// \brief \c make_pair is a lazy PolymorphicFunctionObject for making a \c std::pair\<\> in an
1001 /// xpressive semantic action.
1002 function<op::make_pair>::type const make_pair = {{}};
1004 /// \brief \c unwrap_reference is a lazy PolymorphicFunctionObject for unwrapping a \c boost::reference_wrapper\<\> in an
1005 /// xpressive semantic action.
1006 function<op::unwrap_reference>::type const unwrap_reference = {{}};
1008 /// \brief \c value\<\> is a lazy wrapper for a value that can be used in xpressive semantic actions.
1009 /// \tparam T The type of the value to store.
1011 /// Below is an example that shows where \c <tt>value\<\></tt> is useful.
1014 sregex good_voodoo(boost::shared_ptr<int> pi)
1016 using namespace boost::xpressive;
1017 // Use val() to hold the shared_ptr by value:
1018 sregex rex = +( _d [ ++*val(pi) ] >> '!' );
1019 // OK, rex holds a reference count to the integer.
1025 /// In the above code, \c xpressive::val() is a function that returns a \c value\<\> object. Had
1026 /// \c val() not been used here, the operation <tt>++*pi</tt> would have been evaluated eagerly
1027 /// once, instead of lazily when the regex match happens.
1028 template<typename T>
1030 : proto::extends<typename proto::terminal<T>::type, value<T> >
1033 typedef proto::extends<typename proto::terminal<T>::type, value<T> > base_type;
1035 /// \brief Store a default-constructed \c T
1040 /// \param t The initial value.
1041 /// \brief Store a copy of \c t.
1042 explicit value(T const &t)
1043 : base_type(base_type::proto_base_expr::make(t))
1046 using base_type::operator=;
1051 return proto::value(*this);
1054 /// \brief Fetch the stored value
1055 T const &get() const
1057 return proto::value(*this);
1061 /// \brief \c reference\<\> is a lazy wrapper for a reference that can be used in
1062 /// xpressive semantic actions.
1064 /// \tparam T The type of the referent.
1066 /// Here is an example of how to use \c reference\<\> to create a lazy reference to
1067 /// an existing object so it can be read and written in an xpressive semantic action.
1070 using namespace boost::xpressive;
1071 std::map<std::string, int> result;
1072 reference<std::map<std::string, int> > result_ref(result);
1074 // Match a word and an integer, separated by =>,
1075 // and then stuff the result into a std::map<>
1076 sregex pair = ( (s1= +_w) >> "=>" >> (s2= +_d) )
1077 [ result_ref[s1] = as<int>(s2) ];
1080 template<typename T>
1082 : proto::extends<typename proto::terminal<reference_wrapper<T> >::type, reference<T> >
1085 typedef proto::extends<typename proto::terminal<reference_wrapper<T> >::type, reference<T> > base_type;
1087 /// \param t Reference to object
1088 /// \brief Store a reference to \c t
1089 explicit reference(T &t)
1090 : base_type(base_type::proto_base_expr::make(boost::ref(t)))
1093 using base_type::operator=;
1095 /// \brief Fetch the stored value
1098 return proto::value(*this).get();
1102 /// \brief \c local\<\> is a lazy wrapper for a reference to a value that is stored within the local itself.
1103 /// It is for use within xpressive semantic actions.
1105 /// \tparam T The type of the local variable.
1107 /// Below is an example of how to use \c local\<\> in semantic actions.
1110 using namespace boost::xpressive;
1112 std::string str("1!2!3?");
1113 // count the exciting digits, but not the
1114 // questionable ones.
1115 sregex rex = +( _d [ ++i ] >> '!' );
1116 regex_search(str, rex);
1117 assert( i.get() == 2 );
1121 /// \note As the name "local" suggests, \c local\<\> objects and the regexes
1122 /// that refer to them should never leave the local scope. The value stored
1123 /// within the local object will be destroyed at the end of the \c local\<\>'s
1124 /// lifetime, and any regex objects still holding the \c local\<\> will be
1125 /// left with a dangling reference.
1126 template<typename T>
1128 : detail::value_wrapper<T>
1129 , proto::terminal<reference_wrapper<T> >::type
1132 typedef typename proto::terminal<reference_wrapper<T> >::type base_type;
1134 /// \brief Store a default-constructed value of type \c T
1136 : detail::value_wrapper<T>()
1137 , base_type(base_type::make(boost::ref(detail::value_wrapper<T>::value)))
1140 /// \param t The initial value.
1141 /// \brief Store a default-constructed value of type \c T
1142 explicit local(T const &t)
1143 : detail::value_wrapper<T>(t)
1144 , base_type(base_type::make(boost::ref(detail::value_wrapper<T>::value)))
1147 using base_type::operator=;
1149 /// Fetch the wrapped value.
1152 return proto::value(*this);
1156 T const &get() const
1158 return proto::value(*this);
1162 /// \brief \c as() is a lazy funtion for lexically casting a parameter to a different type.
1163 /// \tparam T The type to which to lexically cast the parameter.
1164 /// \param a The lazy value to lexically cast.
1165 /// \return A lazy object that, when evaluated, lexically casts its argument to the desired type.
1166 template<typename T, typename A>
1167 typename detail::make_function::impl<op::as<T> const, A const &>::result_type const
1170 return detail::make_function::impl<op::as<T> const, A const &>()((op::as<T>()), a);
1173 /// \brief \c static_cast_ is a lazy funtion for statically casting a parameter to a different type.
1174 /// \tparam T The type to which to statically cast the parameter.
1175 /// \param a The lazy value to statically cast.
1176 /// \return A lazy object that, when evaluated, statically casts its argument to the desired type.
1177 template<typename T, typename A>
1178 typename detail::make_function::impl<op::static_cast_<T> const, A const &>::result_type const
1179 static_cast_(A const &a)
1181 return detail::make_function::impl<op::static_cast_<T> const, A const &>()((op::static_cast_<T>()), a);
1184 /// \brief \c dynamic_cast_ is a lazy funtion for dynamically casting a parameter to a different type.
1185 /// \tparam T The type to which to dynamically cast the parameter.
1186 /// \param a The lazy value to dynamically cast.
1187 /// \return A lazy object that, when evaluated, dynamically casts its argument to the desired type.
1188 template<typename T, typename A>
1189 typename detail::make_function::impl<op::dynamic_cast_<T> const, A const &>::result_type const
1190 dynamic_cast_(A const &a)
1192 return detail::make_function::impl<op::dynamic_cast_<T> const, A const &>()((op::dynamic_cast_<T>()), a);
1195 /// \brief \c dynamic_cast_ is a lazy funtion for const-casting a parameter to a different type.
1196 /// \tparam T The type to which to const-cast the parameter.
1197 /// \param a The lazy value to const-cast.
1198 /// \return A lazy object that, when evaluated, const-casts its argument to the desired type.
1199 template<typename T, typename A>
1200 typename detail::make_function::impl<op::const_cast_<T> const, A const &>::result_type const
1201 const_cast_(A const &a)
1203 return detail::make_function::impl<op::const_cast_<T> const, A const &>()((op::const_cast_<T>()), a);
1206 /// \brief Helper for constructing \c value\<\> objects.
1207 /// \return <tt>value\<T\>(t)</tt>
1208 template<typename T>
1209 value<T> const val(T const &t)
1214 /// \brief Helper for constructing \c reference\<\> objects.
1215 /// \return <tt>reference\<T\>(t)</tt>
1216 template<typename T>
1217 reference<T> const ref(T &t)
1219 return reference<T>(t);
1222 /// \brief Helper for constructing \c reference\<\> objects that
1223 /// store a reference to const.
1224 /// \return <tt>reference\<T const\>(t)</tt>
1225 template<typename T>
1226 reference<T const> const cref(T const &t)
1228 return reference<T const>(t);
1231 /// \brief For adding user-defined assertions to your regular expressions.
1233 /// \param t The UnaryPredicate object or Boolean semantic action.
1235 /// A \RefSect{user_s_guide.semantic_actions_and_user_defined_assertions.user_defined_assertions,user-defined assertion}
1236 /// is a kind of semantic action that evaluates
1237 /// a Boolean lambda and, if it evaluates to false, causes the match to
1238 /// fail at that location in the string. This will cause backtracking,
1239 /// so the match may ultimately succeed.
1241 /// To use \c check() to specify a user-defined assertion in a regex, use the
1242 /// following syntax:
1245 sregex s = (_d >> _d)[check( XXX )]; // XXX is a custom assertion
1249 /// The assertion is evaluated with a \c sub_match\<\> object that delineates
1250 /// what part of the string matched the sub-expression to which the assertion
1253 /// \c check() can be used with an ordinary predicate that takes a
1254 /// \c sub_match\<\> object as follows:
1257 // A predicate that is true IFF a sub-match is
1258 // either 3 or 6 characters long.
1261 bool operator()(ssub_match const &sub) const
1263 return sub.length() == 3 || sub.length() == 6;
1267 // match words of 3 characters or 6 characters.
1268 sregex rx = (bow >> +_w >> eow)[ check(three_or_six()) ] ;
1272 /// Alternately, \c check() can be used to define inline custom
1273 /// assertions with the same syntax as is used to define semantic
1274 /// actions. The following code is equivalent to above:
1277 // match words of 3 characters or 6 characters.
1278 sregex rx = (bow >> +_w >> eow)[ check(length(_)==3 || length(_)==6) ] ;
1282 /// Within a custom assertion, \c _ is a placeholder for the \c sub_match\<\>
1283 /// That delineates the part of the string matched by the sub-expression to
1284 /// which the custom assertion was attached.
1285 #ifdef BOOST_XPRESSIVE_DOXYGEN_INVOKED // A hack so Doxygen emits something more meaningful.
1286 template<typename T>
1287 detail::unspecified check(T const &t);
1289 proto::terminal<detail::check_tag>::type const check = {{}};
1292 /// \brief For binding local variables to placeholders in semantic actions when
1293 /// constructing a \c regex_iterator or a \c regex_token_iterator.
1295 /// \param args A set of argument bindings, where each argument binding is an assignment
1296 /// expression, the left hand side of which must be an instance of \c placeholder\<X\>
1297 /// for some \c X, and the right hand side is an lvalue of type \c X.
1299 /// \c xpressive::let() serves the same purpose as <tt>match_results::let()</tt>;
1300 /// that is, it binds a placeholder to a local value. The purpose is to allow a
1301 /// regex with semantic actions to be defined that refers to objects that do not yet exist.
1302 /// Rather than referring directly to an object, a semantic action can refer to a placeholder,
1303 /// and the value of the placeholder can be specified later with a <em>let expression</em>.
1304 /// The <em>let expression</em> created with \c let() is passed to the constructor of either
1305 /// \c regex_iterator or \c regex_token_iterator.
1307 /// See the section \RefSect{user_s_guide.semantic_actions_and_user_defined_assertions.referring_to_non_local_variables, "Referring to Non-Local Variables"}
1308 /// in the Users' Guide for more discussion.
1314 // Define a placeholder for a map object:
1315 placeholder<std::map<std::string, int> > _map;
1317 // Match a word and an integer, separated by =>,
1318 // and then stuff the result into a std::map<>
1319 sregex pair = ( (s1= +_w) >> "=>" >> (s2= +_d) )
1320 [ _map[s1] = as<int>(s2) ];
1322 // The string to parse
1323 std::string str("aaa=>1 bbb=>23 ccc=>456");
1325 // Here is the actual map to fill in:
1326 std::map<std::string, int> result;
1328 // Create a regex_iterator to find all the matches
1329 sregex_iterator it(str.begin(), str.end(), pair, let(_map=result));
1330 sregex_iterator end;
1332 // step through all the matches, and fill in
1337 std::cout << result["aaa"] << '\n';
1338 std::cout << result["bbb"] << '\n';
1339 std::cout << result["ccc"] << '\n';
1343 /// The above code displays:
1351 #ifdef BOOST_XPRESSIVE_DOXYGEN_INVOKED // A hack so Doxygen emits something more meaningful.
1352 template<typename...ArgBindings>
1353 detail::unspecified let(ArgBindings const &...args);
1355 detail::let_<proto::terminal<detail::let_tag>::type> const let = {{{}}};
1358 /// \brief For defining a placeholder to stand in for a variable a semantic action.
1360 /// Use \c placeholder\<\> to define a placeholder for use in semantic actions to stand
1361 /// in for real objects. The use of placeholders allows regular expressions with actions
1362 /// to be defined once and reused in many contexts to read and write from objects which
1363 /// were not available when the regex was defined.
1365 /// \tparam T The type of the object for which this placeholder stands in.
1366 /// \tparam I An optional identifier that can be used to distinguish this placeholder
1367 /// from others that may be used in the same semantic action that happen
1368 /// to have the same type.
1370 /// You can use \c placeholder\<\> by creating an object of type \c placeholder\<T\>
1371 /// and using that object in a semantic action exactly as you intend an object of
1372 /// type \c T to be used.
1376 placeholder<int> _i;
1377 placeholder<double> _d;
1379 sregex rex = ( some >> regex >> here )
1384 /// Then, when doing a pattern match with either \c regex_search(),
1385 /// \c regex_match() or \c regex_replace(), pass a \c match_results\<\> object that
1386 /// contains bindings for the placeholders used in the regex object's semantic actions.
1387 /// You can create the bindings by calling \c match_results::let as follows:
1398 if(regex_match("some string", rex, what))
1399 // i and d mutated here
1403 /// If a semantic action executes that contains an unbound placeholder, a exception of
1404 /// type \c regex_error is thrown.
1406 /// See the discussion for \c xpressive::let() and the
1407 /// \RefSect{user_s_guide.semantic_actions_and_user_defined_assertions.referring_to_non_local_variables, "Referring to Non-Local Variables"}
1408 /// section in the Users' Guide for more information.
1410 /// <em>Example:</em>
1414 // Define a placeholder for a map object:
1415 placeholder<std::map<std::string, int> > _map;
1417 // Match a word and an integer, separated by =>,
1418 // and then stuff the result into a std::map<>
1419 sregex pair = ( (s1= +_w) >> "=>" >> (s2= +_d) )
1420 [ _map[s1] = as<int>(s2) ];
1422 // Match one or more word/integer pairs, separated
1424 sregex rx = pair >> *(+_s >> pair);
1426 // The string to parse
1427 std::string str("aaa=>1 bbb=>23 ccc=>456");
1429 // Here is the actual map to fill in:
1430 std::map<std::string, int> result;
1432 // Bind the _map placeholder to the actual map
1434 what.let( _map = result );
1436 // Execute the match and fill in result map
1437 if(regex_match(str, what, rx))
1439 std::cout << result["aaa"] << '\n';
1440 std::cout << result["bbb"] << '\n';
1441 std::cout << result["ccc"] << '\n';
1445 #ifdef BOOST_XPRESSIVE_DOXYGEN_INVOKED // A hack so Doxygen emits something more meaningful.
1446 template<typename T, int I = 0>
1449 /// \param t The object to associate with this placeholder
1450 /// \return An object of unspecified type that records the association of \c t
1452 detail::unspecified operator=(T &t) const;
1454 detail::unspecified operator=(T const &t) const;
1457 template<typename T, int I, typename Dummy>
1460 typedef placeholder<T, I, Dummy> this_type;
1462 typename proto::terminal<detail::action_arg<T, mpl::int_<I> > >::type
1465 BOOST_PROTO_EXTENDS(action_arg_type, this_type, proto::default_domain)
1469 /// \brief A lazy funtion for constructing objects objects of the specified type.
1470 /// \tparam T The type of object to construct.
1471 /// \param args The arguments to the constructor.
1472 /// \return A lazy object that, when evaluated, returns <tt>T(xs...)</tt>, where
1473 /// <tt>xs...</tt> is the result of evaluating the lazy arguments
1474 /// <tt>args...</tt>.
1475 #ifdef BOOST_XPRESSIVE_DOXYGEN_INVOKED // A hack so Doxygen emits something more meaningful.
1476 template<typename T, typename ...Args>
1477 detail::unspecified construct(Args const &...args);
1480 #define BOOST_PROTO_LOCAL_MACRO(N, typename_A, A_const_ref, A_const_ref_a, a) \
1481 template<typename X2_0 BOOST_PP_COMMA_IF(N) typename_A(N)> \
1482 typename detail::make_function::impl< \
1483 op::construct<X2_0> const \
1484 BOOST_PP_COMMA_IF(N) A_const_ref(N) \
1485 >::result_type const \
1486 construct(A_const_ref_a(N)) \
1488 return detail::make_function::impl< \
1489 op::construct<X2_0> const \
1490 BOOST_PP_COMMA_IF(N) A_const_ref(N) \
1491 >()((op::construct<X2_0>()) BOOST_PP_COMMA_IF(N) a(N)); \
1494 template<typename X2_0 BOOST_PP_COMMA_IF(N) typename_A(N)> \
1495 typename detail::make_function::impl< \
1496 op::throw_<X2_0> const \
1497 BOOST_PP_COMMA_IF(N) A_const_ref(N) \
1498 >::result_type const \
1499 throw_(A_const_ref_a(N)) \
1501 return detail::make_function::impl< \
1502 op::throw_<X2_0> const \
1503 BOOST_PP_COMMA_IF(N) A_const_ref(N) \
1504 >()((op::throw_<X2_0>()) BOOST_PP_COMMA_IF(N) a(N)); \
1508 #define BOOST_PROTO_LOCAL_a BOOST_PROTO_a ///< INTERNAL ONLY
1509 #define BOOST_PROTO_LOCAL_LIMITS (0, BOOST_PP_DEC(BOOST_PROTO_MAX_ARITY)) ///< INTERNAL ONLY
1510 #include BOOST_PROTO_LOCAL_ITERATE()
1515 inline void ignore_unused_regex_actions()
1517 detail::ignore_unused(xpressive::at);
1518 detail::ignore_unused(xpressive::push);
1519 detail::ignore_unused(xpressive::push_back);
1520 detail::ignore_unused(xpressive::push_front);
1521 detail::ignore_unused(xpressive::pop);
1522 detail::ignore_unused(xpressive::pop_back);
1523 detail::ignore_unused(xpressive::pop_front);
1524 detail::ignore_unused(xpressive::top);
1525 detail::ignore_unused(xpressive::back);
1526 detail::ignore_unused(xpressive::front);
1527 detail::ignore_unused(xpressive::first);
1528 detail::ignore_unused(xpressive::second);
1529 detail::ignore_unused(xpressive::matched);
1530 detail::ignore_unused(xpressive::length);
1531 detail::ignore_unused(xpressive::str);
1532 detail::ignore_unused(xpressive::insert);
1533 detail::ignore_unused(xpressive::make_pair);
1534 detail::ignore_unused(xpressive::unwrap_reference);
1535 detail::ignore_unused(xpressive::check);
1536 detail::ignore_unused(xpressive::let);
1541 BOOST_PROTO_CALLABLE()
1542 typedef int result_type;
1544 int operator()(mark_placeholder m) const
1546 return m.mark_number_;
1553 proto::terminal<mark_placeholder>
1554 , op::at(proto::_data, proto::call<mark_nbr(proto::_value)>)
1557 proto::terminal<any_matcher>
1558 , op::at(proto::_data, proto::size_t<0>)
1561 proto::terminal<reference_wrapper<proto::_> >
1562 , op::unwrap_reference(proto::_value)
1564 , proto::_default<ReplaceAlgo>
1571 #pragma warning(pop)
1574 #endif // BOOST_XPRESSIVE_ACTIONS_HPP_EAN_03_22_2007