regression_repeat.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright (c) 2001-2010 Hartmut Kaiser
  2. // Copyright (c) 2010 Head Geek
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #include <iostream>
  7. #include <boost/detail/lightweight_test.hpp>
  8. #include <boost/spirit/include/qi.hpp>
  9. namespace qi = boost::spirit::qi;
  10. using qi::omit;
  11. using qi::repeat;
  12. using std::cout;
  13. using std::endl;
  14. typedef qi::rule<std::string::const_iterator, std::string()> strrule_type;
  15. void test(const std::string input, strrule_type rule, std::string result)
  16. {
  17. std::string target;
  18. std::string::const_iterator i = input.begin(), ie = input.end();
  19. BOOST_TEST(qi::parse(i, ie, rule, target) && target == result);
  20. }
  21. int main()
  22. {
  23. strrule_type obsolete_year =
  24. omit[-qi::char_(" \t")] >>
  25. repeat(2)[qi::digit] >>
  26. omit[-qi::char_(" \t")];
  27. strrule_type correct_year = repeat(4)[qi::digit];
  28. test("1776", qi::hold[correct_year] | repeat(2)[qi::digit], "1776");
  29. test("76", obsolete_year, "76");
  30. test("76", qi::hold[obsolete_year] | correct_year, "76");
  31. test(" 76", qi::hold[correct_year] | obsolete_year, "76");
  32. test("76", qi::hold[correct_year] | obsolete_year, "76");
  33. test("76", qi::hold[correct_year] | repeat(2)[qi::digit], "76");
  34. return boost::report_errors();
  35. }