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
|
/*=============================================================================
Copyright (c) 1998-2003 Joel de Guzman
http://spirit.sourceforge.net/
Use, modification and distribution is subject to 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 <iostream>
#include <boost/detail/lightweight_test.hpp>
//#define BOOST_SPIRIT_DEBUG
#define BOOST_SPIRIT_RULE_SCANNERTYPE_LIMIT 3
#include <boost/spirit/include/classic_core.hpp>
#include <boost/spirit/include/classic_stored_rule.hpp>
using namespace BOOST_SPIRIT_CLASSIC_NS;
///////////////////////////////////////////////////////////////////////////////
//
// Rule tests
//
///////////////////////////////////////////////////////////////////////////////
void
aliasing_tests()
{
rule<> a = ch_p('a');
rule<> b = ch_p('b');
rule<> c = ch_p('c');
std::cout << "sizeof(rule<>): " << sizeof(rule<>) << std::endl;
BOOST_SPIRIT_DEBUG_RULE(a);
BOOST_SPIRIT_DEBUG_RULE(b);
BOOST_SPIRIT_DEBUG_RULE(c);
rule<> start;
BOOST_SPIRIT_DEBUG_RULE(start);
rule<> d;
d = start; // aliasing
parse_info<char const*> pi;
start = *(a | b | c);
pi = parse("abcabcacb", d);
BOOST_TEST(pi.hit);
BOOST_TEST(pi.full);
BOOST_TEST(pi.length == 9);
BOOST_TEST(*pi.stop == 0);
start = (a | b) >> (start | b);
pi = parse("aaaabababaaabbb", d);
BOOST_TEST(pi.hit);
BOOST_TEST(pi.full);
BOOST_TEST(pi.length == 15);
BOOST_TEST(*pi.stop == 0);
}
void
rule_template_param_tests()
{
// test that rules can be issued its template params in any order:
rule<> rx1;
rule<scanner<> > rx2;
rule<scanner<>, parser_context<> > rx3;
rule<scanner<>, parser_context<>, parser_address_tag> rx4;
rule<parser_context<> > rx5;
rule<parser_context<>, parser_address_tag> rx6;
rule<parser_context<>, parser_address_tag, scanner<> > rx7;
rule<parser_address_tag> rx8;
rule<parser_address_tag, scanner<> > rx9;
rule<parser_address_tag, scanner<>, parser_context<> > rx10;
rule<parser_address_tag, parser_context<> > rx11;
rule<parser_address_tag, parser_context<>, scanner<> > rx12;
rule<parser_context<>, scanner<> > rx13;
rule<parser_context<>, scanner<>, parser_address_tag> rx14;
}
struct my_grammar : public grammar<my_grammar>
{
template <typename ScannerT>
struct definition
{
definition(my_grammar const& /*self*/)
{
r = lower_p;
rr = +(lexeme_d[r] >> as_lower_d[r] >> r);
}
typedef scanner_list<
ScannerT
, typename lexeme_scanner<ScannerT>::type
, typename as_lower_scanner<ScannerT>::type
> scanners;
rule<scanners> r;
rule<ScannerT> rr;
rule<ScannerT> const& start() const { return rr; }
};
};
void
rule_2_or_more_scanners_tests()
{
{ // 2 scanners
typedef scanner_list<scanner<>, phrase_scanner_t> scanners;
rule<scanners> r = +anychar_p;
BOOST_TEST(parse("abcdefghijk", r).full);
BOOST_TEST(parse("a b c d e f g h i j k", r, space_p).full);
}
{ // 3 scanners
my_grammar g;
BOOST_TEST(parse("abcdef aBc d e f aBc d E f", g, space_p).full);
}
}
void
rule_basic_tests()
{
rule<> a = ch_p('a');
rule<> b = ch_p('b');
rule<> c = ch_p('c');
BOOST_SPIRIT_DEBUG_RULE(a);
BOOST_SPIRIT_DEBUG_RULE(b);
BOOST_SPIRIT_DEBUG_RULE(c);
parse_info<char const*> pi;
rule<> start = *(a | b | c);
BOOST_SPIRIT_DEBUG_RULE(start);
pi = parse("abcabcacb", start);
BOOST_TEST(pi.hit);
BOOST_TEST(pi.full);
BOOST_TEST(pi.length == 9);
BOOST_TEST(*pi.stop == 0);
start = (a | b) >> (start | b);
pi = parse("aaaabababaaabbb", start);
BOOST_TEST(pi.hit);
BOOST_TEST(pi.full);
BOOST_TEST(pi.length == 15);
BOOST_TEST(*pi.stop == 0);
pi = parse("aaaabababaaabba", start);
BOOST_TEST(pi.hit);
BOOST_TEST(!pi.full);
BOOST_TEST(pi.length == 14);
rule<> r = anychar_p;
r.copy(); // copy test (compile only)
}
void
stored_rule_basic_tests()
{
stored_rule<> a = ch_p('a');
stored_rule<> b = ch_p('b');
stored_rule<> c = ch_p('c');
BOOST_SPIRIT_DEBUG_RULE(a);
BOOST_SPIRIT_DEBUG_RULE(b);
BOOST_SPIRIT_DEBUG_RULE(c);
parse_info<char const*> pi;
stored_rule<> start = *(a | b | c);
BOOST_SPIRIT_DEBUG_RULE(start);
pi = parse("abcabcacb", start);
BOOST_TEST(pi.hit);
BOOST_TEST(pi.full);
BOOST_TEST(pi.length == 9);
BOOST_TEST(*pi.stop == 0);
start = (a | b) >> (start | b);
pi = parse("aaaabababaaabbb", start);
BOOST_TEST(pi.hit);
BOOST_TEST(pi.full);
BOOST_TEST(pi.length == 15);
BOOST_TEST(*pi.stop == 0);
pi = parse("aaaabababaaabba", start);
BOOST_TEST(pi.hit);
BOOST_TEST(!pi.full);
BOOST_TEST(pi.length == 14);
}
void
stored_rule_dynamic_tests()
{
rule<> a = ch_p('a');
rule<> b = ch_p('b');
rule<> c = ch_p('c');
BOOST_SPIRIT_DEBUG_RULE(a);
BOOST_SPIRIT_DEBUG_RULE(b);
BOOST_SPIRIT_DEBUG_RULE(c);
parse_info<char const*> pi;
// The FF is the dynamic equivalent of start = *(a | b | c);
stored_rule<> start = a;
start = start.copy() | b;
start = start.copy() | c;
start = *(start.copy());
std::cout << "sizeof(stored_rule<>): " << sizeof(stored_rule<>) << std::endl;
BOOST_SPIRIT_DEBUG_RULE(start);
pi = parse("abcabcacb", start);
BOOST_TEST(pi.hit);
BOOST_TEST(pi.full);
BOOST_TEST(pi.length == 9);
BOOST_TEST(*pi.stop == 0);
// The FF is the dynamic equivalent of start = (a | b) >> (start | b);
start = b;
start = a | start.copy();
start = start.copy() >> (start | b);
pi = parse("aaaabababaaabbb", start);
BOOST_TEST(pi.hit);
BOOST_TEST(pi.full);
BOOST_TEST(pi.length == 15);
BOOST_TEST(*pi.stop == 0);
pi = parse("aaaabababaaabba", start);
BOOST_TEST(pi.hit);
BOOST_TEST(!pi.full);
BOOST_TEST(pi.length == 14);
}
///////////////////////////////////////////////////////////////////////////////
//
// Main
//
///////////////////////////////////////////////////////////////////////////////
int
main()
{
rule_basic_tests();
aliasing_tests();
rule_template_param_tests();
rule_2_or_more_scanners_tests();
stored_rule_basic_tests();
stored_rule_dynamic_tests();
return boost::report_errors();
}
|