1 /*=============================================================================
2 Copyright (c) 2002-2003 Hartmut Kaiser
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 ///////////////////////////////////////////////////////////////////////////////
10 //
11 // Parser traits tests
12 //
13 ///////////////////////////////////////////////////////////////////////////////
14
15 #include <boost/detail/lightweight_test.hpp>
16 #include <iostream>
17 #include <boost/spirit/include/classic_core.hpp>
18 #include <boost/spirit/include/classic_assign_actor.hpp>
19
20 using namespace BOOST_SPIRIT_CLASSIC_NS;
21
22 typedef ref_value_actor<char, assign_action> assign_actor;
23
24 // Test parser_traits templates
25 void
parser_traits_tests()26 parser_traits_tests()
27 {
28 // is_parser_category_tests
29 typedef chlit<char> plain_t;
30 typedef optional<chlit<char> > unary_t;
31 typedef action<chlit<char>, assign_actor> action_t;
32 typedef sequence<chlit<char>, anychar_parser> binary_t;
33
34 // is_parser
35 BOOST_STATIC_ASSERT(is_parser<plain_t>::value);
36 BOOST_STATIC_ASSERT(is_parser<unary_t>::value);
37 BOOST_STATIC_ASSERT(is_parser<action_t>::value);
38 BOOST_STATIC_ASSERT(is_parser<binary_t>::value);
39
40 // is_action_parser
41 BOOST_STATIC_ASSERT(!is_action_parser<plain_t>::value);
42 BOOST_STATIC_ASSERT(!is_action_parser<unary_t>::value);
43 BOOST_STATIC_ASSERT(is_action_parser<action_t>::value);
44 BOOST_STATIC_ASSERT(!is_action_parser<binary_t>::value);
45
46 // is_unary_composite
47 BOOST_STATIC_ASSERT(!is_unary_composite<plain_t>::value);
48 BOOST_STATIC_ASSERT(is_unary_composite<unary_t>::value);
49 BOOST_STATIC_ASSERT(is_unary_composite<action_t>::value); // action_t _is_ an unary_t!
50 BOOST_STATIC_ASSERT(!is_unary_composite<binary_t>::value);
51
52 // is_binary_composite
53 BOOST_STATIC_ASSERT(!is_binary_composite<plain_t>::value);
54 BOOST_STATIC_ASSERT(!is_binary_composite<unary_t>::value);
55 BOOST_STATIC_ASSERT(!is_binary_composite<action_t>::value);
56 BOOST_STATIC_ASSERT(is_binary_composite<binary_t>::value);
57
58 // is_composite_parser
59 BOOST_STATIC_ASSERT(!is_composite_parser<plain_t>::value);
60 BOOST_STATIC_ASSERT(is_composite_parser<unary_t>::value);
61 BOOST_STATIC_ASSERT(is_composite_parser<action_t>::value); // action_t _is_ an unary_t!
62 BOOST_STATIC_ASSERT(is_composite_parser<binary_t>::value);
63
64 // is_composite_type_tests
65 typedef alternative<chlit<char>, anychar_parser> alternative_t;
66 typedef sequence<chlit<char>, anychar_parser> sequence_t;
67 typedef sequential_or<chlit<char>, anychar_parser> sequential_or_t;
68 typedef intersection<chlit<char>, anychar_parser> intersection_t;
69 typedef difference<chlit<char>, anychar_parser> difference_t;
70 typedef exclusive_or<chlit<char>, anychar_parser> exclusive_or_t;
71
72 typedef optional<chlit<char> > optional_t;
73 typedef kleene_star<chlit<char> > kleene_star_t;
74 typedef positive<chlit<char> > positive_t;
75
76 // is_parser
77 BOOST_STATIC_ASSERT(is_parser<alternative_t>::value);
78 BOOST_STATIC_ASSERT(is_parser<sequence_t>::value);
79 BOOST_STATIC_ASSERT(is_parser<sequential_or_t>::value);
80 BOOST_STATIC_ASSERT(is_parser<intersection_t>::value);
81 BOOST_STATIC_ASSERT(is_parser<difference_t>::value);
82 BOOST_STATIC_ASSERT(is_parser<exclusive_or_t>::value);
83 BOOST_STATIC_ASSERT(is_parser<optional_t>::value);
84 BOOST_STATIC_ASSERT(is_parser<positive_t>::value);
85
86 // is_alternative
87 BOOST_STATIC_ASSERT(!is_alternative<plain_t>::value);
88 BOOST_STATIC_ASSERT(!is_alternative<action_t>::value);
89
90 BOOST_STATIC_ASSERT(is_alternative<alternative_t>::value);
91 BOOST_STATIC_ASSERT(!is_alternative<sequence_t>::value);
92 BOOST_STATIC_ASSERT(!is_alternative<sequential_or_t>::value);
93 BOOST_STATIC_ASSERT(!is_alternative<intersection_t>::value);
94 BOOST_STATIC_ASSERT(!is_alternative<difference_t>::value);
95 BOOST_STATIC_ASSERT(!is_alternative<exclusive_or_t>::value);
96
97 BOOST_STATIC_ASSERT(!is_alternative<optional_t>::value);
98 BOOST_STATIC_ASSERT(!is_alternative<kleene_star_t>::value);
99 BOOST_STATIC_ASSERT(!is_alternative<positive_t>::value);
100
101 // is_sequence
102 BOOST_STATIC_ASSERT(!is_sequence<plain_t>::value);
103 BOOST_STATIC_ASSERT(!is_sequence<action_t>::value);
104
105 BOOST_STATIC_ASSERT(!is_sequence<alternative_t>::value);
106 BOOST_STATIC_ASSERT(is_sequence<sequence_t>::value);
107 BOOST_STATIC_ASSERT(!is_sequence<sequential_or_t>::value);
108 BOOST_STATIC_ASSERT(!is_sequence<intersection_t>::value);
109 BOOST_STATIC_ASSERT(!is_sequence<difference_t>::value);
110 BOOST_STATIC_ASSERT(!is_sequence<exclusive_or_t>::value);
111
112 BOOST_STATIC_ASSERT(!is_sequence<optional_t>::value);
113 BOOST_STATIC_ASSERT(!is_sequence<kleene_star_t>::value);
114 BOOST_STATIC_ASSERT(!is_sequence<positive_t>::value);
115
116 // is_sequential_or
117 BOOST_STATIC_ASSERT(!is_sequential_or<plain_t>::value);
118 BOOST_STATIC_ASSERT(!is_sequential_or<action_t>::value);
119
120 BOOST_STATIC_ASSERT(!is_sequential_or<alternative_t>::value);
121 BOOST_STATIC_ASSERT(!is_sequential_or<sequence_t>::value);
122 BOOST_STATIC_ASSERT(is_sequential_or<sequential_or_t>::value);
123 BOOST_STATIC_ASSERT(!is_sequential_or<intersection_t>::value);
124 BOOST_STATIC_ASSERT(!is_sequential_or<difference_t>::value);
125 BOOST_STATIC_ASSERT(!is_sequential_or<exclusive_or_t>::value);
126
127 BOOST_STATIC_ASSERT(!is_sequential_or<optional_t>::value);
128 BOOST_STATIC_ASSERT(!is_sequential_or<kleene_star_t>::value);
129 BOOST_STATIC_ASSERT(!is_sequential_or<positive_t>::value);
130
131 // is_intersection
132 BOOST_STATIC_ASSERT(!is_intersection<plain_t>::value);
133 BOOST_STATIC_ASSERT(!is_intersection<action_t>::value);
134
135 BOOST_STATIC_ASSERT(!is_intersection<alternative_t>::value);
136 BOOST_STATIC_ASSERT(!is_intersection<sequence_t>::value);
137 BOOST_STATIC_ASSERT(!is_intersection<sequential_or_t>::value);
138 BOOST_STATIC_ASSERT(is_intersection<intersection_t>::value);
139 BOOST_STATIC_ASSERT(!is_intersection<difference_t>::value);
140 BOOST_STATIC_ASSERT(!is_intersection<exclusive_or_t>::value);
141
142 BOOST_STATIC_ASSERT(!is_intersection<optional_t>::value);
143 BOOST_STATIC_ASSERT(!is_intersection<kleene_star_t>::value);
144 BOOST_STATIC_ASSERT(!is_intersection<positive_t>::value);
145
146 // is_difference
147 BOOST_STATIC_ASSERT(!is_difference<plain_t>::value);
148 BOOST_STATIC_ASSERT(!is_difference<action_t>::value);
149
150 BOOST_STATIC_ASSERT(!is_difference<alternative_t>::value);
151 BOOST_STATIC_ASSERT(!is_difference<sequence_t>::value);
152 BOOST_STATIC_ASSERT(!is_difference<sequential_or_t>::value);
153 BOOST_STATIC_ASSERT(!is_difference<intersection_t>::value);
154 BOOST_STATIC_ASSERT(is_difference<difference_t>::value);
155 BOOST_STATIC_ASSERT(!is_difference<exclusive_or_t>::value);
156
157 BOOST_STATIC_ASSERT(!is_difference<optional_t>::value);
158 BOOST_STATIC_ASSERT(!is_difference<kleene_star_t>::value);
159 BOOST_STATIC_ASSERT(!is_difference<positive_t>::value);
160
161 // is_exclusive_or
162 BOOST_STATIC_ASSERT(!is_exclusive_or<plain_t>::value);
163 BOOST_STATIC_ASSERT(!is_exclusive_or<action_t>::value);
164
165 BOOST_STATIC_ASSERT(!is_exclusive_or<alternative_t>::value);
166 BOOST_STATIC_ASSERT(!is_exclusive_or<sequence_t>::value);
167 BOOST_STATIC_ASSERT(!is_exclusive_or<sequential_or_t>::value);
168 BOOST_STATIC_ASSERT(!is_exclusive_or<intersection_t>::value);
169 BOOST_STATIC_ASSERT(!is_exclusive_or<difference_t>::value);
170 BOOST_STATIC_ASSERT(is_exclusive_or<exclusive_or_t>::value);
171
172 BOOST_STATIC_ASSERT(!is_exclusive_or<optional_t>::value);
173 BOOST_STATIC_ASSERT(!is_exclusive_or<kleene_star_t>::value);
174 BOOST_STATIC_ASSERT(!is_exclusive_or<positive_t>::value);
175
176 // is_optional
177 BOOST_STATIC_ASSERT(!is_optional<plain_t>::value);
178 BOOST_STATIC_ASSERT(!is_optional<action_t>::value);
179
180 BOOST_STATIC_ASSERT(!is_optional<alternative_t>::value);
181 BOOST_STATIC_ASSERT(!is_optional<sequence_t>::value);
182 BOOST_STATIC_ASSERT(!is_optional<sequential_or_t>::value);
183 BOOST_STATIC_ASSERT(!is_optional<intersection_t>::value);
184 BOOST_STATIC_ASSERT(!is_optional<difference_t>::value);
185 BOOST_STATIC_ASSERT(!is_optional<exclusive_or_t>::value);
186
187 BOOST_STATIC_ASSERT(is_optional<optional_t>::value);
188 BOOST_STATIC_ASSERT(!is_optional<kleene_star_t>::value);
189 BOOST_STATIC_ASSERT(!is_optional<positive_t>::value);
190
191 // is_kleene_star
192 BOOST_STATIC_ASSERT(!is_kleene_star<plain_t>::value);
193 BOOST_STATIC_ASSERT(!is_kleene_star<action_t>::value);
194
195 BOOST_STATIC_ASSERT(!is_kleene_star<alternative_t>::value);
196 BOOST_STATIC_ASSERT(!is_kleene_star<sequence_t>::value);
197 BOOST_STATIC_ASSERT(!is_kleene_star<sequential_or_t>::value);
198 BOOST_STATIC_ASSERT(!is_kleene_star<intersection_t>::value);
199 BOOST_STATIC_ASSERT(!is_kleene_star<difference_t>::value);
200 BOOST_STATIC_ASSERT(!is_kleene_star<exclusive_or_t>::value);
201
202 BOOST_STATIC_ASSERT(!is_kleene_star<optional_t>::value);
203 BOOST_STATIC_ASSERT(is_kleene_star<kleene_star_t>::value);
204 BOOST_STATIC_ASSERT(!is_kleene_star<positive_t>::value);
205
206 // is_positive
207 BOOST_STATIC_ASSERT(!is_positive<plain_t>::value);
208 BOOST_STATIC_ASSERT(!is_positive<action_t>::value);
209
210 BOOST_STATIC_ASSERT(!is_positive<alternative_t>::value);
211 BOOST_STATIC_ASSERT(!is_positive<sequence_t>::value);
212 BOOST_STATIC_ASSERT(!is_positive<sequential_or_t>::value);
213 BOOST_STATIC_ASSERT(!is_positive<intersection_t>::value);
214 BOOST_STATIC_ASSERT(!is_positive<difference_t>::value);
215 BOOST_STATIC_ASSERT(!is_positive<exclusive_or_t>::value);
216
217 BOOST_STATIC_ASSERT(!is_positive<optional_t>::value);
218 BOOST_STATIC_ASSERT(!is_positive<kleene_star_t>::value);
219 BOOST_STATIC_ASSERT(is_positive<positive_t>::value);
220 }
221
222 ///////////////////////////////////////////////////////////////////////////////
223 // Test parser extraction templates
224 void
parser_extraction_tests()225 parser_extraction_tests()
226 {
227 typedef chlit<char> plain_t;
228 typedef optional<plain_t> unary_t;
229 typedef action<plain_t, assign_actor> action_t;
230 typedef sequence<plain_t, anychar_parser> binary_t;
231
232 // parser type extraction templates
233 BOOST_STATIC_ASSERT((
234 ::boost::is_same<
235 chlit<char>, unary_subject<unary_t>::type
236 >::value));
237 BOOST_STATIC_ASSERT((
238 ::boost::is_same<
239 chlit<char>, action_subject<action_t>::type
240 >::value));
241 BOOST_STATIC_ASSERT((
242 ::boost::is_same<
243 assign_actor, semantic_action<action_t>::type
244 >::value));
245 BOOST_STATIC_ASSERT((
246 ::boost::is_same<
247 chlit<char>, binary_left_subject<binary_t>::type
248 >::value));
249 BOOST_STATIC_ASSERT((
250 ::boost::is_same<
251 anychar_parser, binary_right_subject<binary_t>::type
252 >::value));
253
254 // parser object extraction functions
255 BOOST_TEST(1 == parse("aaaa", get_unary_subject(*ch_p('a'))).length);
256
257 char c = 'b';
258
259 BOOST_TEST(1 == parse("aaaa", get_action_subject(ch_p('a')[assign(c)])).length);
260 BOOST_TEST(c == 'b');
261
262 BOOST_TEST(1 == parse("aaaa",
263 ch_p('a')[ get_semantic_action(ch_p('b')[assign(c)]) ]).length);
264 BOOST_TEST(c == 'a');
265
266 BOOST_TEST(1 == parse("abab",
267 get_binary_left_subject(ch_p('a') >> ch_p('b'))).length);
268 BOOST_TEST(1 == parse("baba",
269 get_binary_right_subject(ch_p('a') >> ch_p('b'))).length);
270 }
271
272 ///////////////////////////////////////////////////////////////////////////////
273 //
274 // Main
275 //
276 ///////////////////////////////////////////////////////////////////////////////
277 int
main()278 main()
279 {
280 parser_traits_tests();
281 parser_extraction_tests();
282
283 return boost::report_errors();
284 }
285
286