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
7#include <iostream>
8#include <boost/spirit/include/qi.hpp>
9
10#include <boost/core/lightweight_test.hpp>
11
12namespace qi = boost::spirit::qi;
13using qi::omit;
14using qi::repeat;
15using std::cout;
16using std::endl;
17
18typedef qi::rule<std::string::const_iterator, std::string()> strrule_type;
19
20void test(const std::string input, strrule_type rule, std::string result)
21{
22 std::string target;
23 std::string::const_iterator i = input.begin(), ie = input.end();
24
25 BOOST_TEST(qi::parse(i, ie, rule, target) && target == result);
26}
27
28int main()
29{
30 strrule_type obsolete_year =
31 omit[-qi::char_(" \t")] >>
32 repeat(2)[qi::digit] >>
33 omit[-qi::char_(" \t")];
34 strrule_type correct_year = repeat(4)[qi::digit];
35
36 test(input: "1776", rule: qi::hold[correct_year] | repeat(2)[qi::digit], result: "1776");
37 test(input: "76", rule: obsolete_year, result: "76");
38 test(input: "76", rule: qi::hold[obsolete_year] | correct_year, result: "76");
39 test(input: " 76", rule: qi::hold[correct_year] | obsolete_year, result: "76");
40 test(input: "76", rule: qi::hold[correct_year] | obsolete_year, result: "76");
41 test(input: "76", rule: qi::hold[correct_year] | repeat(2)[qi::digit], result: "76");
42
43 return boost::report_errors();
44}
45
46

source code of boost/libs/spirit/test/qi/regression_repeat.cpp