Boost logo

Boost-Commit :

From: eric_at_[hidden]
Date: 2007-10-10 17:20:49


Author: eric_niebler
Date: 2007-10-10 17:20:49 EDT (Wed, 10 Oct 2007)
New Revision: 39909
URL: http://svn.boost.org/trac/boost/changeset/39909

Log:
extra test for custom assertions, fix nasty bug in custom assertion handling
Text files modified:
   trunk/boost/xpressive/detail/core/matcher/predicate_matcher.hpp | 23 +++++++++++++----
   trunk/boost/xpressive/detail/detail_fwd.hpp | 5 +--
   trunk/boost/xpressive/detail/static/placeholders.hpp | 12 ---------
   trunk/boost/xpressive/detail/static/transforms/as_action.hpp | 11 +++++++
   trunk/boost/xpressive/regex_actions.hpp | 13 ++++-----
   trunk/libs/xpressive/test/test_assert.cpp | 51 ++++++++++++++++++++++++++++++++++++---
   6 files changed, 81 insertions(+), 34 deletions(-)

Modified: trunk/boost/xpressive/detail/core/matcher/predicate_matcher.hpp
==============================================================================
--- trunk/boost/xpressive/detail/core/matcher/predicate_matcher.hpp (original)
+++ trunk/boost/xpressive/detail/core/matcher/predicate_matcher.hpp 2007-10-10 17:20:49 EDT (Wed, 10 Oct 2007)
@@ -93,6 +93,16 @@
     };
 
     ///////////////////////////////////////////////////////////////////////////////
+ // AssertionFunctor
+ //
+ struct AssertionFunctor
+ : proto::function<
+ proto::terminal<check_tag>
+ , proto::terminal<proto::_>
+ >
+ {};
+
+ ///////////////////////////////////////////////////////////////////////////////
     // predicate_matcher
     //
     template<typename Predicate>
@@ -111,23 +121,24 @@
         template<typename BidiIter, typename Next>
         bool match(match_state<BidiIter> &state, Next const &next) const
         {
- typedef typename Predicate::proto_arg0::predicate_type predicate_type;
- return this->match_(state, next, proto::is_expr<predicate_type>());
+ // Predicate is check(assertion), where assertion can be
+ // a lambda or a function object.
+ return this->match_(state, next, proto::matches<Predicate, AssertionFunctor>());
         }
 
     private:
         template<typename BidiIter, typename Next>
- bool match_(match_state<BidiIter> &state, Next const &next, mpl::false_) const
+ bool match_(match_state<BidiIter> &state, Next const &next, mpl::true_) const
         {
             sub_match<BidiIter> const &sub = state.sub_match(this->sub_);
- return proto::arg(this->predicate_).pred(sub) && next.match(state);
+ return proto::arg(proto::arg_c<1>(this->predicate_))(sub) && next.match(state);
         }
 
         template<typename BidiIter, typename Next>
- bool match_(match_state<BidiIter> &state, Next const &next, mpl::true_) const
+ bool match_(match_state<BidiIter> &state, Next const &next, mpl::false_) const
         {
             predicate_context<BidiIter> ctx(this->sub_, state.sub_matches_);
- return proto::eval(proto::arg(this->predicate_).pred, ctx) && next.match(state);
+ return proto::eval(proto::arg_c<1>(this->predicate_), ctx) && next.match(state);
         }
     };
 

Modified: trunk/boost/xpressive/detail/detail_fwd.hpp
==============================================================================
--- trunk/boost/xpressive/detail/detail_fwd.hpp (original)
+++ trunk/boost/xpressive/detail/detail_fwd.hpp 2007-10-10 17:20:49 EDT (Wed, 10 Oct 2007)
@@ -32,6 +32,8 @@
 
     struct modifier_tag;
 
+ struct check_tag;
+
     typedef mpl::size_t<INT_MAX / 2 - 1> unknown_width;
 
     struct type_info_less;
@@ -59,9 +61,6 @@
 
     struct self_placeholder;
 
- template<typename Predicate>
- struct predicate_placeholder;
-
     template<typename Nbr>
     struct attribute_placeholder;
 

Modified: trunk/boost/xpressive/detail/static/placeholders.hpp
==============================================================================
--- trunk/boost/xpressive/detail/static/placeholders.hpp (original)
+++ trunk/boost/xpressive/detail/static/placeholders.hpp 2007-10-10 17:20:49 EDT (Wed, 10 Oct 2007)
@@ -100,18 +100,6 @@
 };
 
 ///////////////////////////////////////////////////////////////////////////////
-// predicate_placeholder
-//
-template<typename Predicate>
-struct predicate_placeholder
-{
- BOOST_XPR_QUANT_STYLE(quant_variable_width, unknown_width::value, false)
-
- typedef Predicate predicate_type;
- Predicate pred;
-};
-
-///////////////////////////////////////////////////////////////////////////////
 // attribute_placeholder
 //
 template<typename Nbr>

Modified: trunk/boost/xpressive/detail/static/transforms/as_action.hpp
==============================================================================
--- trunk/boost/xpressive/detail/static/transforms/as_action.hpp (original)
+++ trunk/boost/xpressive/detail/static/transforms/as_action.hpp 2007-10-10 17:20:49 EDT (Wed, 10 Oct 2007)
@@ -237,6 +237,15 @@
     {};
 
     ///////////////////////////////////////////////////////////////////////////////
+ // CheckAssertion
+ struct CheckAssertion
+ : proto::function<
+ proto::terminal<check_tag>
+ , proto::_
+ >
+ {};
+
+ ///////////////////////////////////////////////////////////////////////////////
     // action_transform
     // Turn A[B] into (mark_begin(n) >> A >> mark_end(n) >> action_matcher<B>(n))
     // If A and B use attributes, wrap the above expression in
@@ -261,7 +270,7 @@
 
             typedef
                 typename mpl::if_<
- proto::matches<action_type, proto::terminal<predicate_placeholder<proto::_> > >
+ proto::matches<action_type, CheckAssertion>
                   , predicate_matcher<action_copy_type>
                   , action_matcher<action_copy_type>
>::type

Modified: trunk/boost/xpressive/regex_actions.hpp
==============================================================================
--- trunk/boost/xpressive/regex_actions.hpp (original)
+++ trunk/boost/xpressive/regex_actions.hpp 2007-10-10 17:20:49 EDT (Wed, 10 Oct 2007)
@@ -77,6 +77,9 @@
 
             T value;
         };
+
+ struct check_tag
+ {};
     }
 
     namespace op
@@ -687,13 +690,9 @@
         return reference<T const>(t);
     }
 
- template<typename Predicate>
- typename proto::terminal<detail::predicate_placeholder<Predicate> >::type const
- check(Predicate const &pred)
- {
- detail::predicate_placeholder<Predicate> p = {pred};
- return proto::as_expr(p);
- }
+ /// check(), for testing custom assertions
+ ///
+ proto::terminal<detail::check_tag>::type const check = {{}};
 
     template<typename T, int I = 0, typename Dummy = proto::is_proto_expr>
     struct placeholder

Modified: trunk/libs/xpressive/test/test_assert.cpp
==============================================================================
--- trunk/libs/xpressive/test/test_assert.cpp (original)
+++ trunk/libs/xpressive/test/test_assert.cpp 2007-10-10 17:20:49 EDT (Wed, 10 Oct 2007)
@@ -5,11 +5,7 @@
 // Software License, Version 1.0. (See accompanying file
 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 
-#include <map>
-#include <list>
-#include <stack>
-#include <numeric>
-#include <boost/version.hpp>
+#include <iostream>
 #include <boost/xpressive/xpressive_static.hpp>
 #include <boost/xpressive/regex_actions.hpp>
 #include <boost/test/unit_test.hpp>
@@ -50,6 +46,50 @@
     BOOST_CHECK_EQUAL(std::distance(first, last), 2);
 }
 
+struct days_per_month_type
+{
+ int operator[](int i) const
+ {
+ std::cout << "HERE " << i << std::endl;
+ return 29;
+ }
+};
+
+///////////////////////////////////////////////////////////////////////////////
+// test3
+// more complicated use of custom assertions to validate a date
+void test3()
+{
+ int const days_per_month[] =
+ {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 31, 31};
+
+ mark_tag month(1), day(2);
+ // find a valid date of the form month/day/year.
+ sregex date =
+ (
+ // Month must be between 1 and 12 inclusive
+ (month= _d >> !_d) [ check(as<int>(_) >= 1
+ && as<int>(_) <= 12) ]
+ >> '/'
+ // Day must be between 1 and 31 inclusive
+ >> (day= _d >> !_d) [ check(as<int>(_) >= 1
+ && as<int>(_) <= 31) ]
+ >> '/'
+ // Only consider years between 1970 and 2038
+ >> (_d >> _d >> _d >> _d) [ check(as<int>(_) >= 1970
+ && as<int>(_) <= 2038) ]
+ )
+ // Ensure the month actually has that many days.
+ [ check( ref(days_per_month)[as<int>(month)-1] >= as<int>(day) ) ]
+ ;
+
+ smatch what;
+ std::string str("99/99/9999 2/30/2006 2/28/2006");
+
+ BOOST_REQUIRE(regex_search(str, what, date));
+ BOOST_CHECK_EQUAL(what[0], "2/28/2006");
+}
+
 using namespace boost::unit_test;
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -60,6 +100,7 @@
     test_suite *test = BOOST_TEST_SUITE("test_assert");
     test->add(BOOST_TEST_CASE(&test1));
     test->add(BOOST_TEST_CASE(&test2));
+ test->add(BOOST_TEST_CASE(&test3));
     return test;
 }
 


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk