• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 1998-2003 Joel de Guzman
3     http://spirit.sourceforge.net/
4 
5     Use, modification and distribution is subject to the Boost Software
6     License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7     http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 #include <iostream>
10 #include <boost/detail/lightweight_test.hpp>
11 
12 #include <boost/spirit/include/classic_core.hpp>
13 #include <boost/spirit/include/classic_loops.hpp>
14 using namespace BOOST_SPIRIT_CLASSIC_NS;
15 
16 ///////////////////////////////////////////////////////////////////////////////
17 //
18 //  Loops tests
19 //
20 ///////////////////////////////////////////////////////////////////////////////
21 void
loops_tests()22 loops_tests()
23 {
24     parse_info<char const*> pi;
25 
26     pi = parse("\"Hello World\"", "\"" >> *(anychar_p - "\"") >> "\"");
27     BOOST_TEST(pi.hit);
28     BOOST_TEST(pi.full);
29     BOOST_TEST(pi.length == 13);
30     BOOST_TEST(*pi.stop == 0);
31 
32     pi = parse("\"Hello World\"", "\"" >> repeat_p(0, more)[anychar_p - "\""] >> "\"");
33     BOOST_TEST(pi.hit);
34     BOOST_TEST(pi.full);
35     BOOST_TEST(pi.length == 13);
36     BOOST_TEST(*pi.stop == 0);
37 
38     pi = parse("xx", +ch_p('x'));
39     BOOST_TEST(pi.hit);
40     BOOST_TEST(pi.full);
41     BOOST_TEST(pi.length == 2);
42     BOOST_TEST(*pi.stop == 0);
43 
44     pi = parse("xx", repeat_p(1, more)[ch_p('x')]);
45     BOOST_TEST(pi.hit);
46     BOOST_TEST(pi.full);
47     BOOST_TEST(pi.length == 2);
48     BOOST_TEST(*pi.stop == 0);
49 
50     pi = parse("", +ch_p('x'));
51     BOOST_TEST(!pi.hit);
52 
53     pi = parse("", repeat_p(1, more)[ch_p('x')]);
54     BOOST_TEST(!pi.hit);
55 
56     pi = parse("", *ch_p('x'));
57     BOOST_TEST(pi.hit);
58     BOOST_TEST(pi.full);
59     BOOST_TEST(pi.length == 0);
60     BOOST_TEST(*pi.stop == 0);
61 
62     pi = parse("", repeat_p(0, more)[ch_p('x')]);
63     BOOST_TEST(pi.hit);
64     BOOST_TEST(pi.full);
65     BOOST_TEST(pi.length == 0);
66     BOOST_TEST(*pi.stop == 0);
67 
68     //  repeat exact 8
69     rule<>  rep8 = repeat_p(8)[alpha_p] >> 'X';
70     BOOST_TEST(!parse("abcdefgX", rep8).hit);
71     BOOST_TEST(parse("abcdefghX", rep8).full);
72     BOOST_TEST(!parse("abcdefghiX", rep8).hit);
73     BOOST_TEST(!parse("abcdefgX", rep8).hit);
74     BOOST_TEST(!parse("aX", rep8).hit);
75 
76     //  repeat 2 to 8
77     rule<>  rep28 = repeat_p(2, 8)[alpha_p] >> '*';
78     BOOST_TEST(parse("abcdefg*", rep28).full);
79     BOOST_TEST(parse("abcdefgh*", rep28).full);
80     BOOST_TEST(!parse("abcdefghi*", rep28).hit);
81     BOOST_TEST(!parse("a*", rep28).hit);
82 
83     //  repeat 2 or more
84     rule<>  rep2_ = repeat_p(2, more)[alpha_p] >> '+';
85     BOOST_TEST(parse("abcdefg+", rep2_).full);
86     BOOST_TEST(parse("abcdefgh+", rep2_).full);
87     BOOST_TEST(parse("abcdefghi+", rep2_).full);
88     BOOST_TEST(parse("abcdefg+", rep2_).full);
89     BOOST_TEST(!parse("a+", rep2_).hit);
90 
91     //  repeat 0
92     rule<>  rep0 = repeat_p(0)[alpha_p] >> '/';
93     BOOST_TEST(parse("/", rep0).full);
94     BOOST_TEST(!parse("a/", rep0).hit);
95 
96     //  repeat 0 or 1
97     rule<>  rep01 = repeat_p(0, 1)[alpha_p >> digit_p] >> '?';
98     BOOST_TEST(!parse("abcdefg?", rep01).hit);
99     BOOST_TEST(!parse("a?", rep01).hit);
100     BOOST_TEST(!parse("1?", rep01).hit);
101     BOOST_TEST(!parse("11?", rep01).hit);
102     BOOST_TEST(!parse("aa?", rep01).hit);
103     BOOST_TEST(parse("?", rep01).full);
104     BOOST_TEST(parse("a1?", rep01).full);
105 }
106 
107 ///////////////////////////////////////////////////////////////////////////////
108 //
109 //  Main
110 //
111 ///////////////////////////////////////////////////////////////////////////////
112 int
main()113 main()
114 {
115     loops_tests();
116     return boost::report_errors();
117 }
118 
119