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 | #if !defined(BOOST_SPIRIT_FUNDAMENTAL_IPP) |
---|
10 | #define BOOST_SPIRIT_FUNDAMENTAL_IPP |
---|
11 | |
---|
12 | #include <boost/mpl/int.hpp> |
---|
13 | |
---|
14 | namespace boost { namespace spirit { |
---|
15 | |
---|
16 | #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300) |
---|
17 | BOOST_SPIRIT_DEPENDENT_TEMPLATE_WRAPPER2(count_wrapper, count); |
---|
18 | #endif // defined(BOOST_MSVC) && (BOOST_MSVC <= 1300) |
---|
19 | |
---|
20 | namespace impl |
---|
21 | { |
---|
22 | /////////////////////////////////////////////////////////////////////////// |
---|
23 | // |
---|
24 | // Helper template for counting the number of nodes contained in a |
---|
25 | // given parser type. |
---|
26 | // All parser_category type parsers are counted as nodes. |
---|
27 | // |
---|
28 | /////////////////////////////////////////////////////////////////////////// |
---|
29 | template <typename CategoryT> |
---|
30 | struct nodes; |
---|
31 | |
---|
32 | template <> |
---|
33 | struct nodes<plain_parser_category> { |
---|
34 | |
---|
35 | template <typename ParserT, typename LeafCountT> |
---|
36 | struct count { |
---|
37 | |
---|
38 | // __BORLANDC__ == 0x0561 isn't happy with BOOST_STATIC_CONSTANT |
---|
39 | enum { value = (LeafCountT::value + 1) }; |
---|
40 | }; |
---|
41 | }; |
---|
42 | |
---|
43 | #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300) |
---|
44 | |
---|
45 | template <> |
---|
46 | struct nodes<unary_parser_category> { |
---|
47 | |
---|
48 | template <typename ParserT, typename LeafCountT> |
---|
49 | struct count { |
---|
50 | |
---|
51 | typedef typename ParserT::subject_t subject_t; |
---|
52 | typedef typename subject_t::parser_category_t subject_category_t; |
---|
53 | |
---|
54 | typedef nodes<subject_category_t> nodes_t; |
---|
55 | typedef typename count_wrapper<nodes_t> |
---|
56 | ::template result_<subject_t, LeafCountT> count_t; |
---|
57 | |
---|
58 | BOOST_STATIC_CONSTANT(int, value = count_t::value + 1); |
---|
59 | }; |
---|
60 | }; |
---|
61 | |
---|
62 | template <> |
---|
63 | struct nodes<action_parser_category> { |
---|
64 | |
---|
65 | template <typename ParserT, typename LeafCountT> |
---|
66 | struct count { |
---|
67 | |
---|
68 | typedef typename ParserT::subject_t subject_t; |
---|
69 | typedef typename subject_t::parser_category_t subject_category_t; |
---|
70 | |
---|
71 | typedef nodes<subject_category_t> nodes_t; |
---|
72 | typedef typename count_wrapper<nodes_t> |
---|
73 | ::template result_<subject_t, LeafCountT> count_t; |
---|
74 | |
---|
75 | BOOST_STATIC_CONSTANT(int, value = count_t::value + 1); |
---|
76 | }; |
---|
77 | }; |
---|
78 | |
---|
79 | template <> |
---|
80 | struct nodes<binary_parser_category> { |
---|
81 | |
---|
82 | template <typename ParserT, typename LeafCountT> |
---|
83 | struct count { |
---|
84 | |
---|
85 | typedef typename ParserT::left_t left_t; |
---|
86 | typedef typename ParserT::right_t right_t; |
---|
87 | typedef typename left_t::parser_category_t left_category_t; |
---|
88 | typedef typename right_t::parser_category_t right_category_t; |
---|
89 | |
---|
90 | typedef nodes<left_category_t> left_nodes_t; |
---|
91 | typedef typename count_wrapper<left_nodes_t> |
---|
92 | ::template result_<left_t, LeafCountT> left_count_t; |
---|
93 | |
---|
94 | typedef nodes<right_category_t> right_nodes_t; |
---|
95 | typedef typename count_wrapper<right_nodes_t> |
---|
96 | ::template result_<right_t, LeafCountT> right_count_t; |
---|
97 | |
---|
98 | BOOST_STATIC_CONSTANT(int, |
---|
99 | value = (left_count_t::value + right_count_t::value + 1)); |
---|
100 | }; |
---|
101 | }; |
---|
102 | |
---|
103 | #else |
---|
104 | |
---|
105 | template <> |
---|
106 | struct nodes<unary_parser_category> { |
---|
107 | |
---|
108 | template <typename ParserT, typename LeafCountT> |
---|
109 | struct count { |
---|
110 | |
---|
111 | typedef typename ParserT::subject_t subject_t; |
---|
112 | typedef typename subject_t::parser_category_t subject_category_t; |
---|
113 | |
---|
114 | // __BORLANDC__ == 0x0561 isn't happy with BOOST_STATIC_CONSTANT |
---|
115 | enum { value = (nodes<subject_category_t> |
---|
116 | ::template count<subject_t, LeafCountT>::value + 1) }; |
---|
117 | }; |
---|
118 | }; |
---|
119 | |
---|
120 | template <> |
---|
121 | struct nodes<action_parser_category> { |
---|
122 | |
---|
123 | template <typename ParserT, typename LeafCountT> |
---|
124 | struct count { |
---|
125 | |
---|
126 | typedef typename ParserT::subject_t subject_t; |
---|
127 | typedef typename subject_t::parser_category_t subject_category_t; |
---|
128 | |
---|
129 | // __BORLANDC__ == 0x0561 isn't happy with BOOST_STATIC_CONSTANT |
---|
130 | enum { value = (nodes<subject_category_t> |
---|
131 | ::template count<subject_t, LeafCountT>::value + 1) }; |
---|
132 | }; |
---|
133 | }; |
---|
134 | |
---|
135 | template <> |
---|
136 | struct nodes<binary_parser_category> { |
---|
137 | |
---|
138 | template <typename ParserT, typename LeafCountT> |
---|
139 | struct count { |
---|
140 | |
---|
141 | typedef typename ParserT::left_t left_t; |
---|
142 | typedef typename ParserT::right_t right_t; |
---|
143 | typedef typename left_t::parser_category_t left_category_t; |
---|
144 | typedef typename right_t::parser_category_t right_category_t; |
---|
145 | |
---|
146 | typedef count self_t; |
---|
147 | |
---|
148 | // __BORLANDC__ == 0x0561 isn't happy with BOOST_STATIC_CONSTANT |
---|
149 | enum { |
---|
150 | leftcount = (nodes<left_category_t> |
---|
151 | ::template count<left_t, LeafCountT>::value), |
---|
152 | rightcount = (nodes<right_category_t> |
---|
153 | ::template count<right_t, LeafCountT>::value), |
---|
154 | value = ((self_t::leftcount) + (self_t::rightcount) + 1) |
---|
155 | }; |
---|
156 | }; |
---|
157 | }; |
---|
158 | |
---|
159 | #endif |
---|
160 | |
---|
161 | /////////////////////////////////////////////////////////////////////////// |
---|
162 | // |
---|
163 | // Helper template for counting the number of leaf nodes contained in a |
---|
164 | // given parser type. |
---|
165 | // Only plain_parser_category type parsers are counted as leaf nodes. |
---|
166 | // |
---|
167 | /////////////////////////////////////////////////////////////////////////// |
---|
168 | template <typename CategoryT> |
---|
169 | struct leafs; |
---|
170 | |
---|
171 | template <> |
---|
172 | struct leafs<plain_parser_category> { |
---|
173 | |
---|
174 | template <typename ParserT, typename LeafCountT> |
---|
175 | struct count { |
---|
176 | |
---|
177 | // __BORLANDC__ == 0x0561 isn't happy with BOOST_STATIC_CONSTANT |
---|
178 | enum { value = (LeafCountT::value + 1) }; |
---|
179 | }; |
---|
180 | }; |
---|
181 | |
---|
182 | #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300) |
---|
183 | |
---|
184 | template <> |
---|
185 | struct leafs<unary_parser_category> { |
---|
186 | |
---|
187 | template <typename ParserT, typename LeafCountT> |
---|
188 | struct count { |
---|
189 | |
---|
190 | typedef typename ParserT::subject_t subject_t; |
---|
191 | typedef typename subject_t::parser_category_t subject_category_t; |
---|
192 | |
---|
193 | typedef leafs<subject_category_t> nodes_t; |
---|
194 | typedef typename count_wrapper<nodes_t> |
---|
195 | ::template result_<subject_t, LeafCountT> count_t; |
---|
196 | |
---|
197 | BOOST_STATIC_CONSTANT(int, value = count_t::value); |
---|
198 | }; |
---|
199 | }; |
---|
200 | |
---|
201 | template <> |
---|
202 | struct leafs<action_parser_category> { |
---|
203 | |
---|
204 | template <typename ParserT, typename LeafCountT> |
---|
205 | struct count { |
---|
206 | |
---|
207 | typedef typename ParserT::subject_t subject_t; |
---|
208 | typedef typename subject_t::parser_category_t subject_category_t; |
---|
209 | |
---|
210 | typedef leafs<subject_category_t> nodes_t; |
---|
211 | typedef typename count_wrapper<nodes_t> |
---|
212 | ::template result_<subject_t, LeafCountT> count_t; |
---|
213 | |
---|
214 | BOOST_STATIC_CONSTANT(int, value = count_t::value); |
---|
215 | }; |
---|
216 | }; |
---|
217 | |
---|
218 | template <> |
---|
219 | struct leafs<binary_parser_category> { |
---|
220 | |
---|
221 | template <typename ParserT, typename LeafCountT> |
---|
222 | struct count { |
---|
223 | |
---|
224 | typedef typename ParserT::left_t left_t; |
---|
225 | typedef typename ParserT::right_t right_t; |
---|
226 | typedef typename left_t::parser_category_t left_category_t; |
---|
227 | typedef typename right_t::parser_category_t right_category_t; |
---|
228 | |
---|
229 | typedef leafs<left_category_t> left_nodes_t; |
---|
230 | typedef typename count_wrapper<left_nodes_t> |
---|
231 | ::template result_<left_t, LeafCountT> left_count_t; |
---|
232 | |
---|
233 | typedef leafs<right_category_t> right_nodes_t; |
---|
234 | typedef typename count_wrapper<right_nodes_t> |
---|
235 | ::template result_<right_t, LeafCountT> right_count_t; |
---|
236 | |
---|
237 | BOOST_STATIC_CONSTANT(int, |
---|
238 | value = (left_count_t::value + right_count_t::value)); |
---|
239 | }; |
---|
240 | }; |
---|
241 | |
---|
242 | #else |
---|
243 | |
---|
244 | template <> |
---|
245 | struct leafs<unary_parser_category> { |
---|
246 | |
---|
247 | template <typename ParserT, typename LeafCountT> |
---|
248 | struct count { |
---|
249 | |
---|
250 | typedef typename ParserT::subject_t subject_t; |
---|
251 | typedef typename subject_t::parser_category_t subject_category_t; |
---|
252 | |
---|
253 | // __BORLANDC__ == 0x0561 isn't happy with BOOST_STATIC_CONSTANT |
---|
254 | enum { value = (leafs<subject_category_t> |
---|
255 | ::template count<subject_t, LeafCountT>::value) }; |
---|
256 | }; |
---|
257 | }; |
---|
258 | |
---|
259 | template <> |
---|
260 | struct leafs<action_parser_category> { |
---|
261 | |
---|
262 | template <typename ParserT, typename LeafCountT> |
---|
263 | struct count { |
---|
264 | |
---|
265 | typedef typename ParserT::subject_t subject_t; |
---|
266 | typedef typename subject_t::parser_category_t subject_category_t; |
---|
267 | |
---|
268 | // __BORLANDC__ == 0x0561 isn't happy with BOOST_STATIC_CONSTANT |
---|
269 | enum { value = (leafs<subject_category_t> |
---|
270 | ::template count<subject_t, LeafCountT>::value) }; |
---|
271 | }; |
---|
272 | }; |
---|
273 | |
---|
274 | template <> |
---|
275 | struct leafs<binary_parser_category> { |
---|
276 | |
---|
277 | template <typename ParserT, typename LeafCountT> |
---|
278 | struct count { |
---|
279 | |
---|
280 | typedef typename ParserT::left_t left_t; |
---|
281 | typedef typename ParserT::right_t right_t; |
---|
282 | typedef typename left_t::parser_category_t left_category_t; |
---|
283 | typedef typename right_t::parser_category_t right_category_t; |
---|
284 | |
---|
285 | typedef count self_t; |
---|
286 | |
---|
287 | // __BORLANDC__ == 0x0561 isn't happy with BOOST_STATIC_CONSTANT |
---|
288 | enum { |
---|
289 | leftcount = (leafs<left_category_t> |
---|
290 | ::template count<left_t, LeafCountT>::value), |
---|
291 | rightcount = (leafs<right_category_t> |
---|
292 | ::template count<right_t, LeafCountT>::value), |
---|
293 | value = (self_t::leftcount + self_t::rightcount) |
---|
294 | }; |
---|
295 | }; |
---|
296 | }; |
---|
297 | |
---|
298 | #endif |
---|
299 | |
---|
300 | } // namespace impl |
---|
301 | |
---|
302 | /////////////////////////////////////////////////////////////////////////////// |
---|
303 | }} // namespace boost::spirit |
---|
304 | |
---|
305 | #endif // !defined(BOOST_SPIRIT_FUNDAMENTAL_IPP) |
---|