1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
/*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman
Copyright (c) 2001-2011 Hartmut Kaiser
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#include <boost/detail/lightweight_test.hpp>
#include <boost/mpl/print.hpp>
#include <boost/spirit/include/qi_operator.hpp>
#include <boost/spirit/include/qi_char.hpp>
#include <boost/spirit/include/qi_string.hpp>
#include <boost/spirit/include/qi_numeric.hpp>
#include <boost/spirit/include/qi_directive.hpp>
#include <boost/spirit/include/qi_action.hpp>
#include <boost/spirit/include/qi_nonterminal.hpp>
#include <boost/spirit/include/qi_auxiliary.hpp>
#include <boost/spirit/include/qi_rule.hpp>
#include <boost/spirit/include/support_argument.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/variant.hpp>
#include <boost/assert.hpp>
#include <string>
#include <iostream>
#include <vector>
#include "test.hpp"
struct test_action
{
test_action(char last)
: last_(last) {}
template<typename Context>
void operator()(std::vector<char> const& v, Context const&, bool&) const
{
BOOST_TEST(v.size() == 4 &&
v[0] == 'a' && v[1] == 'b' && v[2] == '1' && v[3] == last_);
}
char last_;
};
struct test_action_2
{
typedef std::vector<boost::optional<char> > result_type;
template<typename Context>
void operator()(result_type const& v, Context const&, bool&) const
{
BOOST_TEST(v.size() == 5 &&
!v[0] && v[1] == 'a' && v[2] == 'b' && v[3] == '1' && v[4] == '2');
}
};
struct DIgnore
{
std::string text;
};
struct DInclude
{
std::string FileName;
};
BOOST_FUSION_ADAPT_STRUCT(
DIgnore,
(std::string, text)
)
BOOST_FUSION_ADAPT_STRUCT(
DInclude,
(std::string, FileName)
)
int
main()
{
using spirit_test::test;
using spirit_test::test_attr;
using boost::spirit::ascii::char_;
using boost::spirit::qi::int_;
using boost::spirit::qi::lit;
using boost::spirit::qi::unused_type;
using boost::spirit::qi::_1;
using boost::spirit::qi::omit;
{
BOOST_TEST((test("a", char_ | char_)));
BOOST_TEST((test("x", lit('x') | lit('i'))));
BOOST_TEST((test("i", lit('x') | lit('i'))));
BOOST_TEST((!test("z", lit('x') | lit('o'))));
BOOST_TEST((test("rock", lit("rock") | lit("roll"))));
BOOST_TEST((test("roll", lit("rock") | lit("roll"))));
BOOST_TEST((test("rock", lit("rock") | int_)));
BOOST_TEST((test("12345", lit("rock") | int_)));
}
{
boost::variant<unused_type, int, char> v;
BOOST_TEST((test_attr("12345", lit("rock") | int_ | char_, v)));
BOOST_TEST(boost::get<int>(v) == 12345);
BOOST_TEST((test_attr("rock", lit("rock") | int_ | char_, v)));
BOOST_TEST(v.which() == 1);
BOOST_TEST((test_attr("x", lit("rock") | int_ | char_, v)));
BOOST_TEST(boost::get<char>(v) == 'x');
}
{ // Make sure that we are using the actual supplied attribute types
// from the variant and not the expected type.
// $$$ Fixed: <2/19/2011> JDG $$$
boost::variant<int, std::string> v;
BOOST_TEST((test_attr("12345", int_ | +char_, v)));
BOOST_TEST(boost::get<int>(v) == 12345);
BOOST_TEST((test_attr("abc", int_ | +char_, v)));
BOOST_TEST(boost::get<std::string>(v) == "abc");
BOOST_TEST((test_attr("12345", +char_ | int_, v)));
BOOST_TEST(boost::get<std::string>(v) == "12345");
}
{ // test action
namespace phx = boost::phoenix;
boost::optional<boost::variant<int, char> > v;
BOOST_TEST((test("12345", (lit("rock") | int_ | char_)[phx::ref(v) = _1])));
BOOST_TEST(boost::get<int>(boost::get(v)) == 12345);
BOOST_TEST((test("rock", (lit("rock") | int_ | char_)[phx::ref(v) = _1])));
BOOST_TEST(!v);
}
{
unused_type x;
BOOST_TEST((test_attr("rock", lit("rock") | lit('x'), x)));
}
{
// test if alternatives with all components having unused
// attributes have an unused attribute
using boost::fusion::vector;
using boost::fusion::at_c;
vector<char, char> v;
BOOST_TEST((test_attr("abc",
char_ >> (omit[char_] | omit[char_]) >> char_, v)));
BOOST_TEST((at_c<0>(v) == 'a'));
BOOST_TEST((at_c<1>(v) == 'c'));
}
{
// Test that we can still pass a "compatible" attribute to
// an alternate even if its "expected" attribute is unused type.
std::string s;
BOOST_TEST((test_attr("...", *(char_('.') | char_(',')), s)));
BOOST_TEST(s == "...");
}
{ // make sure collapsing eps works as expected
// (compile check only)
using boost::spirit::qi::rule;
using boost::spirit::qi::_val;
using boost::spirit::qi::_1;
using boost::spirit::eps;
rule<const wchar_t*, wchar_t()> r1, r2, r3;
r3 = ((eps >> r1))[_val += _1];
r3 = ((r1 ) | r2)[_val += _1];
r3 %= ((eps >> r1) | r2);
r3 = ((eps >> r1) | r2)[_val += _1];
}
// make sure the attribute of an alternative gets properly collapsed
{
using boost::spirit::qi::lexeme;
using boost::spirit::ascii::alnum;
using boost::spirit::ascii::alpha;
using boost::spirit::ascii::digit;
using boost::spirit::ascii::string;
namespace phx = boost::phoenix;
BOOST_TEST( (test("ab1_", (*(alnum | char_('_')))[test_action('_')])) );
BOOST_TEST( (test("ab12", (*(alpha | digit))[test_action('2')])) );
BOOST_TEST( (test("abcab12", (*("abc" | alnum))[test_action_2()])) );
std::vector<boost::optional<char> > v;
BOOST_TEST( (test("x,y,z", (*(',' | char_))[phx::ref(v) = _1])) );
BOOST_ASSERT(v[0] == 'x');
BOOST_ASSERT(!v[1]);
BOOST_ASSERT(v[2] == 'y');
BOOST_ASSERT(!v[3]);
BOOST_ASSERT(v[4] == 'z');
}
{
using boost::spirit::qi::eps;
// testing a sequence taking a container as attribute
std::string s;
BOOST_TEST( (test_attr("abc,a,b,c",
char_ >> char_ >> (char_ % ','), s )) );
BOOST_TEST(s == "abcabc");
// test having an optional<container> inside a sequence
s.erase();
BOOST_TEST( (test_attr("ab",
char_ >> char_ >> -(char_ % ','), s )) );
BOOST_TEST(s == "ab");
// test having a variant<container, ...> inside a sequence
s.erase();
BOOST_TEST( (test_attr("ab",
char_ >> char_ >> ((char_ % ',') | eps), s )) );
BOOST_TEST(s == "ab");
s.erase();
BOOST_TEST( (test_attr("abc",
char_ >> char_ >> ((char_ % ',') | eps), s )) );
BOOST_TEST(s == "abc");
}
{
using boost::spirit::qi::int_;
int i = 0;
BOOST_TEST( (test_attr("10", int_(5) | int_(10), i)) );
BOOST_TEST(i == 10);
}
{
#ifdef SPIRIT_NO_COMPILE_CHECK
//compile test only (bug_march_10_2011_8_35_am)
// TODO: does not work as intended with std <= c++03
typedef boost::variant<double, std::string> value_type;
using boost::spirit::qi::rule;
using boost::spirit::qi::eps;
rule<std::string::const_iterator, value_type()> r1 = r1 | eps;
#endif
}
{
using boost::spirit::qi::rule;
typedef boost::variant<DIgnore, DInclude> DLine;
rule<char*, DIgnore()> ignore;
rule<char*, DInclude()> include;
rule<char*, DLine()> line = include | ignore;
}
return boost::report_errors();
}
|