1 | /*=============================================================================
|
---|
2 | Copyright (c) 2001-2011 Joel de Guzman
|
---|
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 <string>
|
---|
8 | #include <vector>
|
---|
9 |
|
---|
10 | #include <boost/detail/lightweight_test.hpp>
|
---|
11 | #include <boost/utility/enable_if.hpp>
|
---|
12 |
|
---|
13 | #include <boost/spirit/include/qi_operator.hpp>
|
---|
14 | #include <boost/spirit/include/qi_char.hpp>
|
---|
15 | #include <boost/spirit/include/qi_string.hpp>
|
---|
16 | #include <boost/spirit/include/qi_numeric.hpp>
|
---|
17 | #include <boost/spirit/include/qi_directive.hpp>
|
---|
18 | #include <boost/spirit/include/qi_action.hpp>
|
---|
19 | #include <boost/spirit/include/qi_eps.hpp>
|
---|
20 | #include <boost/spirit/include/support_argument.hpp>
|
---|
21 | #include <boost/spirit/include/phoenix_core.hpp>
|
---|
22 | #include <boost/spirit/include/phoenix_operator.hpp>
|
---|
23 | #include <boost/phoenix/core/reference.hpp>
|
---|
24 |
|
---|
25 | #include <string>
|
---|
26 | #include <iostream>
|
---|
27 | #include "test.hpp"
|
---|
28 |
|
---|
29 | struct x_attr
|
---|
30 | {
|
---|
31 | };
|
---|
32 |
|
---|
33 | namespace boost { namespace spirit { namespace traits
|
---|
34 | {
|
---|
35 | template <>
|
---|
36 | struct container_value<x_attr>
|
---|
37 | {
|
---|
38 | typedef char type; // value type of container
|
---|
39 | };
|
---|
40 |
|
---|
41 | template <>
|
---|
42 | struct push_back_container<x_attr, char>
|
---|
43 | {
|
---|
44 | static bool call(x_attr& /*c*/, char /*val*/)
|
---|
45 | {
|
---|
46 | // push back value type into container
|
---|
47 | return true;
|
---|
48 | }
|
---|
49 | };
|
---|
50 | }}}
|
---|
51 |
|
---|
52 | int
|
---|
53 | main()
|
---|
54 | {
|
---|
55 | using spirit_test::test_attr;
|
---|
56 | using spirit_test::test;
|
---|
57 |
|
---|
58 | using namespace boost::spirit::ascii;
|
---|
59 | using boost::spirit::qi::repeat;
|
---|
60 | using boost::spirit::qi::inf;
|
---|
61 | using boost::spirit::qi::omit;
|
---|
62 | using boost::spirit::qi::int_;
|
---|
63 | using boost::spirit::qi::_1;
|
---|
64 | using boost::spirit::qi::lexeme;
|
---|
65 |
|
---|
66 | {
|
---|
67 | BOOST_TEST(test("aaaaaaaa", repeat[char_])); // kleene synonym
|
---|
68 | BOOST_TEST(test("aaaaaaaa", repeat(8)[char_]));
|
---|
69 | BOOST_TEST(!test("aa", repeat(3)[char_]));
|
---|
70 |
|
---|
71 | BOOST_TEST(test("aaa", repeat(3, 5)[char_]));
|
---|
72 | BOOST_TEST(test("aaaaa", repeat(3, 5)[char_]));
|
---|
73 | BOOST_TEST(!test("aaaaaa", repeat(3, 5)[char_]));
|
---|
74 | BOOST_TEST(!test("aa", repeat(3, 5)[char_]));
|
---|
75 |
|
---|
76 | BOOST_TEST(test("aaa", repeat(3, inf)[char_]));
|
---|
77 | BOOST_TEST(test("aaaaa", repeat(3, inf)[char_]));
|
---|
78 | BOOST_TEST(test("aaaaaa", repeat(3, inf)[char_]));
|
---|
79 | BOOST_TEST(!test("aa", repeat(3, inf)[char_]));
|
---|
80 | }
|
---|
81 |
|
---|
82 | {
|
---|
83 | std::string s;
|
---|
84 | BOOST_TEST(test_attr("aaaaaaaa", repeat[char_ >> char_], s)); // kleene synonym
|
---|
85 | BOOST_TEST(s == "aaaaaaaa");
|
---|
86 |
|
---|
87 | s.clear();
|
---|
88 | BOOST_TEST(test_attr("aaaaaaaa", repeat(4)[char_ >> char_], s));
|
---|
89 | BOOST_TEST(s == "aaaaaaaa");
|
---|
90 |
|
---|
91 | BOOST_TEST(!test("aa", repeat(3)[char_ >> char_]));
|
---|
92 | BOOST_TEST(!test("a", repeat(1)[char_ >> char_]));
|
---|
93 |
|
---|
94 | s.clear();
|
---|
95 | BOOST_TEST(test_attr("aa", repeat(1, 3)[char_ >> char_], s));
|
---|
96 | BOOST_TEST(s == "aa");
|
---|
97 |
|
---|
98 | s.clear();
|
---|
99 | BOOST_TEST(test_attr("aaaaaa", repeat(1, 3)[char_ >> char_], s));
|
---|
100 | BOOST_TEST(s == "aaaaaa");
|
---|
101 |
|
---|
102 | BOOST_TEST(!test("aaaaaaa", repeat(1, 3)[char_ >> char_]));
|
---|
103 | BOOST_TEST(!test("a", repeat(1, 3)[char_ >> char_]));
|
---|
104 |
|
---|
105 | s.clear();
|
---|
106 | BOOST_TEST(test_attr("aaaa", repeat(2, inf)[char_ >> char_], s));
|
---|
107 | BOOST_TEST(s == "aaaa");
|
---|
108 |
|
---|
109 | s.clear();
|
---|
110 | BOOST_TEST(test_attr("aaaaaa", repeat(2, inf)[char_ >> char_], s));
|
---|
111 | BOOST_TEST(s == "aaaaaa");
|
---|
112 |
|
---|
113 | BOOST_TEST(!test("aa", repeat(2, inf)[char_ >> char_]));
|
---|
114 | }
|
---|
115 |
|
---|
116 | { // from classic spirit tests
|
---|
117 | BOOST_TEST(test("", repeat(0, inf)['x']));
|
---|
118 |
|
---|
119 | // repeat exact 8
|
---|
120 | #define rep8 repeat(8)[alpha] >> 'X'
|
---|
121 | BOOST_TEST(!test("abcdefgX", rep8, false));
|
---|
122 | BOOST_TEST(test("abcdefghX", rep8));
|
---|
123 | BOOST_TEST(!test("abcdefghiX", rep8, false));
|
---|
124 | BOOST_TEST(!test("abcdefgX", rep8, false));
|
---|
125 | BOOST_TEST(!test("aX", rep8, false));
|
---|
126 |
|
---|
127 | // repeat 2 to 8
|
---|
128 | #define rep28 repeat(2, 8)[alpha] >> '*'
|
---|
129 | BOOST_TEST(test("abcdefg*", rep28));
|
---|
130 | BOOST_TEST(test("abcdefgh*", rep28));
|
---|
131 | BOOST_TEST(!test("abcdefghi*", rep28, false));
|
---|
132 | BOOST_TEST(!test("a*", rep28, false));
|
---|
133 |
|
---|
134 | // repeat 2 or more
|
---|
135 | #define rep2_ repeat(2, inf)[alpha] >> '+'
|
---|
136 | BOOST_TEST(test("abcdefg+", rep2_));
|
---|
137 | BOOST_TEST(test("abcdefgh+", rep2_));
|
---|
138 | BOOST_TEST(test("abcdefghi+", rep2_));
|
---|
139 | BOOST_TEST(test("abcdefg+", rep2_));
|
---|
140 | BOOST_TEST(!test("a+", rep2_, false));
|
---|
141 |
|
---|
142 | // repeat 0
|
---|
143 | #define rep0 repeat(0)[alpha] >> '/'
|
---|
144 | BOOST_TEST(test("/", rep0));
|
---|
145 | BOOST_TEST(!test("a/", rep0, false));
|
---|
146 |
|
---|
147 | // repeat 0 or 1
|
---|
148 | #define rep01 repeat(0, 1)[alpha >> digit] >> '?'
|
---|
149 | BOOST_TEST(!test("abcdefg?", rep01, false));
|
---|
150 | BOOST_TEST(!test("a?", rep01, false));
|
---|
151 | BOOST_TEST(!test("1?", rep01, false));
|
---|
152 | BOOST_TEST(!test("11?", rep01, false));
|
---|
153 | BOOST_TEST(!test("aa?", rep01, false));
|
---|
154 | BOOST_TEST(test("?", rep01));
|
---|
155 | BOOST_TEST(test("a1?", rep01));
|
---|
156 | }
|
---|
157 |
|
---|
158 | {
|
---|
159 | BOOST_TEST(test(" a a aaa aa", repeat(7)[char_], space));
|
---|
160 | BOOST_TEST(test("12345 678 9", repeat(9)[digit], space));
|
---|
161 | }
|
---|
162 |
|
---|
163 | {
|
---|
164 | BOOST_TEST(test("aBcdeFGH", no_case[repeat(8)[lower]]));
|
---|
165 | BOOST_TEST(test("a B cde FGH", no_case[repeat(8)[lower]], space));
|
---|
166 | }
|
---|
167 |
|
---|
168 | {
|
---|
169 | std::vector<std::string> v;
|
---|
170 | BOOST_TEST(test_attr("a b c d", repeat(4)[lexeme[+alpha]], v, space) && 4 == v.size() &&
|
---|
171 | v[0] == "a" && v[1] == "b" && v[2] == "c" && v[3] == "d");
|
---|
172 | }
|
---|
173 |
|
---|
174 | {
|
---|
175 | std::string s;
|
---|
176 | BOOST_TEST(test_attr("bbbb", repeat(4)[char_], s) && s == "bbbb");
|
---|
177 |
|
---|
178 | s.clear();
|
---|
179 | BOOST_TEST(test_attr("b b b b", repeat(4)[char_], s, space) && s == "bbbb");
|
---|
180 |
|
---|
181 | // The following 2 tests show that omit does not inhibit explicit attributes
|
---|
182 | s.clear();
|
---|
183 | BOOST_TEST(test_attr("bbbb", omit[repeat(4)[char_('b')]], s) && s == "bbbb");
|
---|
184 |
|
---|
185 | s.clear();
|
---|
186 | BOOST_TEST(test_attr("b b b b", omit[repeat(4)[char_('b')]], s, space) && s == "bbbb");
|
---|
187 | }
|
---|
188 |
|
---|
189 | {
|
---|
190 | BOOST_TEST(test("1 2 3", int_ >> repeat(2)[int_], space));
|
---|
191 | BOOST_TEST(!test("1 2", int_ >> repeat(2)[int_], space));
|
---|
192 | }
|
---|
193 |
|
---|
194 | {
|
---|
195 | std::vector<char> v;
|
---|
196 | BOOST_TEST(test_attr("1 2 3", int_ >> repeat(2)[int_], v, space));
|
---|
197 | BOOST_TEST(v.size() == 3 && v[0] == 1 && v[1] == 2 && v[2] == 3);
|
---|
198 |
|
---|
199 | BOOST_TEST(!test("1 2", int_ >> repeat(2)[int_], space));
|
---|
200 | }
|
---|
201 |
|
---|
202 | { // actions
|
---|
203 | namespace phx = boost::phoenix;
|
---|
204 |
|
---|
205 | std::vector<char> v;
|
---|
206 | BOOST_TEST(test("bbbb", repeat(4)[char_][phx::ref(v) = _1]) && 4 == v.size() &&
|
---|
207 | v[0] == 'b' && v[1] == 'b' && v[2] == 'b' && v[3] == 'b');
|
---|
208 | }
|
---|
209 |
|
---|
210 | { // more actions
|
---|
211 | namespace phx = boost::phoenix;
|
---|
212 |
|
---|
213 | std::vector<int> v;
|
---|
214 | BOOST_TEST(test("123 456 789", repeat(3)[int_][phx::ref(v) = _1], space) && 3 == v.size() &&
|
---|
215 | v[0] == 123 && v[1] == 456 && v[2] == 789);
|
---|
216 | }
|
---|
217 |
|
---|
218 | { // lazy repeats
|
---|
219 | using boost::phoenix::val;
|
---|
220 |
|
---|
221 | BOOST_TEST(test("aaaaaaaa", repeat(val(8))[char_]));
|
---|
222 | BOOST_TEST(!test("aa", repeat(val(3))[char_]));
|
---|
223 |
|
---|
224 | BOOST_TEST(test("aaa", repeat(val(3), val(5))[char_]));
|
---|
225 | BOOST_TEST(test("aaaaa", repeat(val(3), val(5))[char_]));
|
---|
226 | BOOST_TEST(!test("aaaaaa", repeat(val(3), val(5))[char_]));
|
---|
227 | BOOST_TEST(!test("aa", repeat(val(3), val(5))[char_]));
|
---|
228 |
|
---|
229 | BOOST_TEST(test("aaa", repeat(val(3), val(inf))[char_]));
|
---|
230 | BOOST_TEST(test("aaaaa", repeat(val(3), val(inf))[char_]));
|
---|
231 | BOOST_TEST(test("aaaaaa", repeat(val(3), val(inf))[char_]));
|
---|
232 | BOOST_TEST(!test("aa", repeat(val(3), val(inf))[char_]));
|
---|
233 | }
|
---|
234 |
|
---|
235 | { // more lazy repeats
|
---|
236 | using boost::phoenix::val;
|
---|
237 |
|
---|
238 | BOOST_TEST(test("aaa", repeat(3, val(5))[char_]));
|
---|
239 | BOOST_TEST(test("aaaaa", repeat(val(3), 5)[char_]));
|
---|
240 | BOOST_TEST(!test("aaaaaa", repeat(3, val(5))[char_]));
|
---|
241 | BOOST_TEST(!test("aa", repeat(val(3), 5)[char_]));
|
---|
242 |
|
---|
243 | //#warning "testcase commented out"
|
---|
244 | BOOST_TEST(test("aaa", repeat(val(3), inf)[char_]));
|
---|
245 | BOOST_TEST(test("aaaaa", repeat(3, val(inf))[char_]));
|
---|
246 | BOOST_TEST(test("aaaaaa", repeat(val(3), inf)[char_]));
|
---|
247 | BOOST_TEST(!test("aa", repeat(3, val(inf))[char_]));
|
---|
248 | }
|
---|
249 |
|
---|
250 | {
|
---|
251 | using boost::spirit::qi::eps;
|
---|
252 | using boost::phoenix::val;
|
---|
253 | using boost::phoenix::ref;
|
---|
254 |
|
---|
255 | std::string s;
|
---|
256 | size_t c = 0;
|
---|
257 |
|
---|
258 | s.clear();
|
---|
259 | BOOST_TEST(test_attr("aaaaaaaa", repeat[char_ >> eps], s) && s == "aaaaaaaa");
|
---|
260 |
|
---|
261 | s.clear();
|
---|
262 | BOOST_TEST(test_attr("aaaaaaaa", repeat(8)[char_ >> eps], s) && s == "aaaaaaaa");
|
---|
263 |
|
---|
264 | s.clear();
|
---|
265 | BOOST_TEST(test_attr("aaaaaaaa", repeat(val(8))[char_ >> eps], s) && s == "aaaaaaaa");
|
---|
266 |
|
---|
267 | s.clear();
|
---|
268 | c = 0;
|
---|
269 | BOOST_TEST(test_attr("aaaaaaaa", repeat[char_ >> eps[ref(c)++]], s) && s == "aaaaaaaa" && c == 8);
|
---|
270 |
|
---|
271 | s.clear();
|
---|
272 | c = 0;
|
---|
273 | BOOST_TEST(test_attr("aaaaaaaa", repeat(8)[char_ >> eps[ref(c)++]], s) && s == "aaaaaaaa" && c == 8);
|
---|
274 |
|
---|
275 | s.clear();
|
---|
276 | c = 0;
|
---|
277 | BOOST_TEST(test_attr("aaaaaaaa", repeat(val(8))[char_ >> eps[ref(c)++]], s) && s == "aaaaaaaa" && c == 8);
|
---|
278 | }
|
---|
279 |
|
---|
280 | { // attribute customization
|
---|
281 |
|
---|
282 | x_attr x;
|
---|
283 | test_attr("abcde", repeat[char_], x);
|
---|
284 | test_attr("abcde", repeat(5)[char_], x);
|
---|
285 | test_attr("abcde", repeat(1, 5)[char_], x);
|
---|
286 | test_attr("abcde", repeat(1, inf)[char_], x);
|
---|
287 | }
|
---|
288 |
|
---|
289 | return boost::report_errors();
|
---|
290 | }
|
---|
291 |
|
---|