]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Copyright (c) 2001-2011 Hartmut Kaiser |
2 | // | |
3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
5 | ||
1e59de90 | 6 | #include <boost/spirit/include/karma_repeat.hpp> |
7c673cae FG |
7 | |
8 | #include <boost/spirit/include/karma_operator.hpp> | |
9 | #include <boost/spirit/include/karma_char.hpp> | |
10 | #include <boost/spirit/include/karma_string.hpp> | |
11 | #include <boost/spirit/include/karma_numeric.hpp> | |
12 | #include <boost/spirit/include/karma_directive.hpp> | |
13 | #include <boost/spirit/include/karma_operator.hpp> | |
14 | #include <boost/spirit/include/karma_action.hpp> | |
15 | #include <boost/spirit/include/karma_nonterminal.hpp> | |
16 | #include <boost/spirit/include/karma_auxiliary.hpp> | |
17 | #include <boost/spirit/include/karma_directive.hpp> | |
18 | #include <boost/spirit/include/karma_phoenix_attributes.hpp> | |
19 | #include <boost/spirit/include/support_argument.hpp> | |
1e59de90 TL |
20 | |
21 | #include <boost/assign/std/vector.hpp> | |
22 | #include <boost/phoenix/core.hpp> | |
23 | #include <boost/phoenix/operator.hpp> | |
7c673cae FG |
24 | #include <boost/fusion/include/std_pair.hpp> |
25 | ||
26 | #include <string> | |
27 | #include <iostream> | |
28 | #include <vector> | |
29 | ||
30 | #include "test.hpp" | |
31 | ||
32 | using namespace spirit_test; | |
33 | ||
34 | /////////////////////////////////////////////////////////////////////////////// | |
35 | struct action | |
36 | { | |
37 | action (std::vector<char>& vec) | |
38 | : vec(vec), it(vec.begin()) | |
39 | {} | |
40 | ||
41 | void operator()(unsigned& value, boost::spirit::unused_type, bool& pass) const | |
42 | { | |
43 | pass = (it != vec.end()); | |
44 | if (pass) | |
45 | value = *it++; | |
46 | } | |
47 | ||
48 | std::vector<char>& vec; | |
49 | mutable std::vector<char>::iterator it; | |
50 | }; | |
51 | ||
52 | /////////////////////////////////////////////////////////////////////////////// | |
53 | int main() | |
54 | { | |
55 | using namespace boost::spirit::ascii; | |
56 | using boost::spirit::karma::repeat; | |
57 | using boost::spirit::karma::inf; | |
58 | using boost::spirit::karma::int_; | |
59 | using boost::spirit::karma::hex; | |
60 | using boost::spirit::karma::_1; | |
61 | ||
62 | { // lazy repeats | |
63 | using boost::phoenix::val; | |
64 | ||
65 | std::string str8("aaaaaaaa"); | |
66 | BOOST_TEST(test("aaaaaaaa", repeat[char_], str8)); // kleene synonym | |
67 | BOOST_TEST(test("aaaaaaaa", repeat(val(8))[char_], str8)); | |
68 | BOOST_TEST(test("aaa", repeat(val(3))[char_], str8)); | |
69 | BOOST_TEST(!test("aaaaaaaa", repeat(val(9))[char_], str8)); | |
70 | ||
71 | std::string str3("aaa"); | |
72 | BOOST_TEST(test("aaaaa", repeat(val(3), val(5))[char_], str8)); | |
73 | BOOST_TEST(test("aaa", repeat(val(3), val(5))[char_], str3)); | |
74 | BOOST_TEST(!test("aaa", repeat(val(4), val(5))[char_], str3)); | |
75 | ||
76 | BOOST_TEST(test("aaa", repeat(val(3), val(inf))[char_], str3)); | |
77 | BOOST_TEST(test("aaaaaaaa", repeat(val(3), val(inf))[char_], str8)); | |
78 | BOOST_TEST(!test("aaa", repeat(val(4), val(inf))[char_], str3)); | |
79 | } | |
80 | ||
81 | ||
82 | { | |
83 | std::string str8("aaaaaaaa"); | |
84 | BOOST_TEST(test("aaaaaaaa", repeat[char_], str8)); // kleene synonym | |
85 | BOOST_TEST(test("aaaaaaaa", repeat(8)[char_], str8)); | |
86 | BOOST_TEST(test("aaa", repeat(3)[char_], str8)); | |
87 | BOOST_TEST(!test("aaaaaaaa", repeat(9)[char_], str8)); | |
88 | ||
89 | std::string str3("aaa"); | |
90 | BOOST_TEST(test("aaaaa", repeat(3, 5)[char_], str8)); | |
91 | BOOST_TEST(test("aaa", repeat(3, 5)[char_], str3)); | |
92 | BOOST_TEST(!test("aaa", repeat(4, 5)[char_], str3)); | |
93 | ||
94 | BOOST_TEST(test("aaa", repeat(3, inf)[char_], str3)); | |
95 | BOOST_TEST(test("aaaaaaaa", repeat(3, inf)[char_], str8)); | |
96 | BOOST_TEST(!test("aaa", repeat(4, inf)[char_], str3)); | |
97 | } | |
98 | ||
99 | { | |
100 | std::string str8("aaaaaaaa"); | |
101 | BOOST_TEST(test_delimited("a a a a a a a a ", repeat[char_], str8, space)); | |
102 | BOOST_TEST(test_delimited("a a a a a a a a ", repeat(8)[char_], str8, space)); | |
103 | BOOST_TEST(test_delimited("a a a ", repeat(3)[char_], str8, space)); | |
104 | BOOST_TEST(!test_delimited("a a a a a a a a ", repeat(9)[char_], str8, space)); | |
105 | ||
106 | std::string str3("aaa"); | |
107 | BOOST_TEST(test_delimited("a a a a a ", repeat(3, 5)[char_], str8, space)); | |
108 | BOOST_TEST(test_delimited("a a a ", repeat(3, 5)[char_], str3, space)); | |
109 | BOOST_TEST(!test_delimited("a a a ", repeat(4, 5)[char_], str3, space)); | |
110 | ||
111 | BOOST_TEST(test_delimited("a a a ", repeat(3, inf)[char_], str3, space)); | |
112 | BOOST_TEST(test_delimited("a a a a a a a a ", repeat(3, inf)[char_], str8, space)); | |
113 | BOOST_TEST(!test_delimited("a a a ", repeat(4, inf)[char_], str3, space)); | |
114 | } | |
115 | ||
116 | { | |
117 | // make sure user defined end condition is applied if no attribute | |
118 | // is passed in | |
119 | using namespace boost::assign; | |
120 | ||
121 | std::vector<char> v; | |
122 | v += 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'; | |
123 | BOOST_TEST(test("[6162636465666768]", | |
124 | '[' << repeat[hex[action(v)]] << ']')); | |
125 | } | |
126 | ||
7c673cae FG |
127 | { |
128 | namespace ascii = boost::spirit::ascii; | |
129 | namespace phoenix = boost::phoenix; | |
130 | ||
131 | char c = 'a'; | |
132 | BOOST_TEST(test("bcd", repeat(3)[ascii::char_[_1 = ++phoenix::ref(c)]])); | |
133 | ||
134 | c = 'a'; | |
135 | BOOST_TEST(test("bcd", repeat(3)[ascii::char_], ++phoenix::ref(c))); | |
136 | } | |
7c673cae FG |
137 | |
138 | return boost::report_errors(); | |
139 | } | |
140 |