1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
3 Copyright (c) 2011 Bryce Lelbach
4 Copyright (c) 2011 Jan Frederick Eick
6 Distributed under the Boost Software License, Version 1.0. (See accompanying
7 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 ==============================================================================*/
9 #if !defined(SPIRIT_UINT_APR_17_2006_0901AM)
10 #define SPIRIT_UINT_APR_17_2006_0901AM
16 #include <boost/spirit/home/qi/skip_over.hpp>
17 #include <boost/spirit/home/qi/detail/enable_lit.hpp>
18 #include <boost/spirit/home/qi/numeric/numeric_utils.hpp>
19 #include <boost/spirit/home/qi/meta_compiler.hpp>
20 #include <boost/spirit/home/qi/parser.hpp>
21 #include <boost/spirit/home/support/common_terminals.hpp>
22 #include <boost/spirit/home/support/info.hpp>
23 #include <boost/spirit/home/support/detail/is_spirit_tag.hpp>
24 #include <boost/mpl/assert.hpp>
25 #include <boost/type_traits/is_same.hpp>
27 namespace boost { namespace spirit
31 template <typename T, unsigned Radix, unsigned MinDigits
41 ///////////////////////////////////////////////////////////////////////
42 // This one is the class that the user can instantiate directly in
43 // order to create a customized int parser
44 template <typename T = int, unsigned Radix = 10, unsigned MinDigits = 1
47 : spirit::terminal<tag::uint_parser<T, Radix, MinDigits, MaxDigits> >
51 ///////////////////////////////////////////////////////////////////////////
53 ///////////////////////////////////////////////////////////////////////////
54 template <> // enables ushort_
55 struct use_terminal<qi::domain, tag::ushort_> : mpl::true_ {};
57 template <typename A0> // enables lit(n)
58 struct use_terminal<qi::domain
59 , terminal_ex<tag::lit, fusion::vector1<A0> >
60 , typename enable_if<is_same<A0, unsigned short> >::type>
63 template <typename A0> // enables ushort_(n)
64 struct use_terminal<qi::domain
65 , terminal_ex<tag::ushort_, fusion::vector1<A0> > >
66 : is_arithmetic<A0> {};
68 template <> // enables *lazy* ushort_(n)
69 struct use_lazy_terminal<qi::domain, tag::ushort_, 1> : mpl::true_ {};
71 ///////////////////////////////////////////////////////////////////////////
72 template <> // enables uint_
73 struct use_terminal<qi::domain, tag::uint_> : mpl::true_ {};
75 template <typename A0> // enables lit(n)
76 struct use_terminal<qi::domain
77 , terminal_ex<tag::lit, fusion::vector1<A0> >
78 , typename enable_if<is_same<A0, unsigned> >::type>
81 template <typename A0> // enables uint_(n)
82 struct use_terminal<qi::domain
83 , terminal_ex<tag::uint_, fusion::vector1<A0> > >
84 : is_arithmetic<A0> {};
86 template <> // enables *lazy* uint_(n)
87 struct use_lazy_terminal<qi::domain, tag::uint_, 1> : mpl::true_ {};
89 ///////////////////////////////////////////////////////////////////////////
90 template <> // enables ulong_
91 struct use_terminal<qi::domain, tag::ulong_> : mpl::true_ {};
93 template <typename A0> // enables lit(n)
94 struct use_terminal<qi::domain
95 , terminal_ex<tag::lit, fusion::vector1<A0> >
96 , typename enable_if<is_same<A0, unsigned long> >::type>
99 template <typename A0> // enables ulong_(n)
100 struct use_terminal<qi::domain
101 , terminal_ex<tag::ulong_, fusion::vector1<A0> > >
102 : is_arithmetic<A0> {};
104 template <> // enables *lazy* ulong_(n)
105 struct use_lazy_terminal<qi::domain, tag::ulong_, 1> : mpl::true_ {};
107 ///////////////////////////////////////////////////////////////////////////
108 #ifdef BOOST_HAS_LONG_LONG
109 template <> // enables ulong_long
110 struct use_terminal<qi::domain, tag::ulong_long> : mpl::true_ {};
112 template <typename A0> // enables lit(n)
113 struct use_terminal<qi::domain
114 , terminal_ex<tag::lit, fusion::vector1<A0> >
115 , typename enable_if<is_same<A0, boost::ulong_long_type> >::type>
118 template <typename A0> // enables ulong_long(n)
119 struct use_terminal<qi::domain
120 , terminal_ex<tag::ulong_long, fusion::vector1<A0> > >
121 : is_arithmetic<A0> {};
123 template <> // enables *lazy* ulong_long(n)
124 struct use_lazy_terminal<qi::domain, tag::ulong_long, 1> : mpl::true_ {};
127 ///////////////////////////////////////////////////////////////////////////
128 template <> // enables bin
129 struct use_terminal<qi::domain, tag::bin> : mpl::true_ {};
131 template <typename A0> // enables bin(n)
132 struct use_terminal<qi::domain
133 , terminal_ex<tag::bin, fusion::vector1<A0> > >
134 : is_arithmetic<A0> {};
136 template <> // enables *lazy* bin(n)
137 struct use_lazy_terminal<qi::domain, tag::bin, 1> : mpl::true_ {};
139 ///////////////////////////////////////////////////////////////////////////
140 template <> // enables oct
141 struct use_terminal<qi::domain, tag::oct> : mpl::true_ {};
143 template <typename A0> // enables oct(n)
144 struct use_terminal<qi::domain
145 , terminal_ex<tag::oct, fusion::vector1<A0> > >
146 : is_arithmetic<A0> {};
148 template <> // enables *lazy* oct(n)
149 struct use_lazy_terminal<qi::domain, tag::oct, 1> : mpl::true_ {};
151 ///////////////////////////////////////////////////////////////////////////
152 template <> // enables hex
153 struct use_terminal<qi::domain, tag::hex> : mpl::true_ {};
155 template <typename A0> // enables hex(n)
156 struct use_terminal<qi::domain
157 , terminal_ex<tag::hex, fusion::vector1<A0> > >
158 : is_arithmetic<A0> {};
160 template <> // enables *lazy* hex(n)
161 struct use_lazy_terminal<qi::domain, tag::hex, 1> : mpl::true_ {};
163 ///////////////////////////////////////////////////////////////////////////
164 // enables any custom uint_parser
165 template <typename T, unsigned Radix, unsigned MinDigits
167 struct use_terminal<qi::domain
168 , tag::uint_parser<T, Radix, MinDigits, MaxDigits> >
171 // enables any custom uint_parser(n)
172 template <typename T, unsigned Radix, unsigned MinDigits
173 , int MaxDigits, typename A0>
174 struct use_terminal<qi::domain
175 , terminal_ex<tag::uint_parser<T, Radix, MinDigits, MaxDigits>
176 , fusion::vector1<A0> >
179 // enables *lazy* custom uint_parser(n)
180 template <typename T, unsigned Radix, unsigned MinDigits
182 struct use_lazy_terminal<qi::domain
183 , tag::uint_parser<T, Radix, MinDigits, MaxDigits>, 1
187 namespace boost { namespace spirit { namespace qi
189 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
194 using spirit::ushort_;
196 using spirit::ulong_;
197 #ifdef BOOST_HAS_LONG_LONG
198 using spirit::ulong_long;
200 using spirit::lit; // lit(1) is equivalent to 1
203 using spirit::bin_type;
204 using spirit::oct_type;
205 using spirit::hex_type;
207 using spirit::ushort_type;
208 using spirit::uint_type;
209 using spirit::ulong_type;
210 #ifdef BOOST_HAS_LONG_LONG
211 using spirit::ulong_long_type;
213 using spirit::lit_type;
215 ///////////////////////////////////////////////////////////////////////////
216 // This is the actual uint parser
217 ///////////////////////////////////////////////////////////////////////////
218 template <typename T, unsigned Radix = 10, unsigned MinDigits = 1
219 , int MaxDigits = -1>
220 struct any_uint_parser
221 : primitive_parser<any_uint_parser<T, Radix, MinDigits, MaxDigits> >
223 // check template parameter 'Radix' for validity
224 BOOST_SPIRIT_ASSERT_MSG(
225 Radix >= 2 && Radix <= 36,
226 not_supported_radix, ());
228 template <typename Context, typename Iterator>
234 template <typename Iterator, typename Context
235 , typename Skipper, typename Attribute>
236 bool parse(Iterator& first, Iterator const& last
237 , Context& /*context*/, Skipper const& skipper
238 , Attribute& attr_) const
240 typedef extract_uint<T, Radix, MinDigits, MaxDigits> extract;
241 qi::skip_over(first, last, skipper);
242 return extract::call(first, last, attr_);
245 template <typename Context>
246 info what(Context& /*context*/) const
248 return info("unsigned-integer");
253 template <typename T, unsigned Radix = 10, unsigned MinDigits = 1
254 , int MaxDigits = -1, bool no_attribute = true>
255 struct literal_uint_parser
256 : primitive_parser<literal_uint_parser<T, Radix, MinDigits, MaxDigits
259 // check template parameter 'Radix' for validity
260 BOOST_SPIRIT_ASSERT_MSG(
261 Radix == 2 || Radix == 8 || Radix == 10 || Radix == 16,
262 not_supported_radix, ());
264 template <typename Value>
265 literal_uint_parser(Value const& n) : n_(n) {}
267 template <typename Context, typename Iterator>
269 : mpl::if_c<no_attribute, unused_type, T>
272 template <typename Iterator, typename Context
273 , typename Skipper, typename Attribute>
274 bool parse(Iterator& first, Iterator const& last
275 , Context& /*context*/, Skipper const& skipper
276 , Attribute& attr_param) const
278 typedef extract_uint<T, Radix, MinDigits, MaxDigits> extract;
279 qi::skip_over(first, last, skipper);
281 Iterator save = first;
284 if (extract::call(first, last, attr_) && (attr_ == n_))
286 traits::assign_to(attr_, attr_param);
294 template <typename Context>
295 info what(Context& /*context*/) const
297 return info("unsigned-integer");
303 ///////////////////////////////////////////////////////////////////////////
304 // Parser generators: make_xxx function (objects)
305 ///////////////////////////////////////////////////////////////////////////
306 template <typename T, unsigned Radix = 10, unsigned MinDigits = 1
307 , int MaxDigits = -1>
310 typedef any_uint_parser<T, Radix, MinDigits, MaxDigits> result_type;
311 result_type operator()(unused_type, unused_type) const
313 return result_type();
317 template <typename T, unsigned Radix = 10, unsigned MinDigits = 1
318 , int MaxDigits = -1>
319 struct make_direct_uint
321 typedef literal_uint_parser<T, Radix, MinDigits, MaxDigits, false>
323 template <typename Terminal>
324 result_type operator()(Terminal const& term, unused_type) const
326 return result_type(fusion::at_c<0>(term.args));
330 template <typename T, unsigned Radix = 10, unsigned MinDigits = 1
331 , int MaxDigits = -1>
332 struct make_literal_uint
334 typedef literal_uint_parser<T, Radix, MinDigits, MaxDigits> result_type;
335 template <typename Terminal>
336 result_type operator()(Terminal const& term, unused_type) const
338 return result_type(fusion::at_c<0>(term.args));
342 ///////////////////////////////////////////////////////////////////////////
343 template <typename Modifiers, typename A0>
344 struct make_primitive<
345 terminal_ex<tag::lit, fusion::vector1<A0> >
346 , Modifiers, typename enable_if<is_same<A0, unsigned short> >::type>
347 : make_literal_uint<unsigned short> {};
349 template <typename Modifiers, typename A0>
350 struct make_primitive<
351 terminal_ex<tag::lit, fusion::vector1<A0> >
352 , Modifiers, typename enable_if<is_same<A0, unsigned> >::type>
353 : make_literal_uint<unsigned> {};
355 template <typename Modifiers, typename A0>
356 struct make_primitive<
357 terminal_ex<tag::lit, fusion::vector1<A0> >
358 , Modifiers, typename enable_if<is_same<A0, unsigned long> >::type>
359 : make_literal_uint<unsigned long> {};
361 #ifdef BOOST_HAS_LONG_LONG
362 template <typename Modifiers, typename A0>
363 struct make_primitive<
364 terminal_ex<tag::lit, fusion::vector1<A0> >
365 , Modifiers, typename enable_if<is_same<A0, boost::ulong_long_type> >::type>
366 : make_literal_uint<boost::ulong_long_type> {};
369 ///////////////////////////////////////////////////////////////////////////
370 template <typename T, unsigned Radix, unsigned MinDigits, int MaxDigits
371 , typename Modifiers>
372 struct make_primitive<
373 tag::uint_parser<T, Radix, MinDigits, MaxDigits>
375 : make_uint<T, Radix, MinDigits, MaxDigits> {};
377 template <typename T, unsigned Radix, unsigned MinDigits, int MaxDigits
378 , typename A0, typename Modifiers>
379 struct make_primitive<
380 terminal_ex<tag::uint_parser<T, Radix, MinDigits, MaxDigits>
381 , fusion::vector1<A0> >, Modifiers>
382 : make_direct_uint<T, Radix, MinDigits, MaxDigits> {};
384 ///////////////////////////////////////////////////////////////////////////
385 template <typename Modifiers>
386 struct make_primitive<tag::bin, Modifiers>
387 : make_uint<unsigned, 2> {};
389 template <typename Modifiers, typename A0>
390 struct make_primitive<
392 , fusion::vector1<A0> > , Modifiers>
393 : make_direct_uint<unsigned, 2> {};
395 ///////////////////////////////////////////////////////////////////////////
396 template <typename Modifiers>
397 struct make_primitive<tag::oct, Modifiers>
398 : make_uint<unsigned, 8> {};
400 template <typename Modifiers, typename A0>
401 struct make_primitive<
403 , fusion::vector1<A0> > , Modifiers>
404 : make_direct_uint<unsigned, 8> {};
406 ///////////////////////////////////////////////////////////////////////////
407 template <typename Modifiers>
408 struct make_primitive<tag::hex, Modifiers>
409 : make_uint<unsigned, 16> {};
411 template <typename Modifiers, typename A0>
412 struct make_primitive<
414 , fusion::vector1<A0> > , Modifiers>
415 : make_direct_uint<unsigned, 16> {};
417 ///////////////////////////////////////////////////////////////////////////
418 template <typename Modifiers>
419 struct make_primitive<tag::ushort_, Modifiers>
420 : make_uint<unsigned short> {};
422 template <typename Modifiers, typename A0>
423 struct make_primitive<
424 terminal_ex<tag::ushort_
425 , fusion::vector1<A0> > , Modifiers>
426 : make_direct_uint<unsigned short> {};
428 ///////////////////////////////////////////////////////////////////////////
429 template <typename Modifiers>
430 struct make_primitive<tag::uint_, Modifiers>
431 : make_uint<unsigned> {};
433 template <typename Modifiers, typename A0>
434 struct make_primitive<
435 terminal_ex<tag::uint_
436 , fusion::vector1<A0> > , Modifiers>
437 : make_direct_uint<unsigned> {};
439 ///////////////////////////////////////////////////////////////////////////
440 template <typename Modifiers>
441 struct make_primitive<tag::ulong_, Modifiers>
442 : make_uint<unsigned long> {};
444 template <typename Modifiers, typename A0>
445 struct make_primitive<
446 terminal_ex<tag::ulong_
447 , fusion::vector1<A0> > , Modifiers>
448 : make_direct_uint<unsigned long> {};
450 ///////////////////////////////////////////////////////////////////////////
451 #ifdef BOOST_HAS_LONG_LONG
452 template <typename Modifiers>
453 struct make_primitive<tag::ulong_long, Modifiers>
454 : make_uint<boost::ulong_long_type> {};
456 template <typename Modifiers, typename A0>
457 struct make_primitive<
458 terminal_ex<tag::ulong_long
459 , fusion::vector1<A0> > , Modifiers>
460 : make_direct_uint<boost::ulong_long_type> {};