2011-11-14 04:22:04 +00:00
|
|
|
#ifndef SPROUT_WEED_PARSER_NUMERIC_UINT_P_HPP
|
|
|
|
#define SPROUT_WEED_PARSER_NUMERIC_UINT_P_HPP
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <sprout/config.hpp>
|
|
|
|
#include <sprout/iterator/next.hpp>
|
|
|
|
#include <sprout/tuple/tuple.hpp>
|
|
|
|
#include <sprout/integer/integer_digits.hpp>
|
2013-03-25 03:43:47 +00:00
|
|
|
#include <sprout/type_traits/identity.hpp>
|
2011-11-14 04:22:04 +00:00
|
|
|
#include <sprout/weed/unused.hpp>
|
|
|
|
#include <sprout/weed/parser_result.hpp>
|
|
|
|
#include <sprout/weed/parser/parser_base.hpp>
|
|
|
|
#include <sprout/weed/detail/ndigits.hpp>
|
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace weed {
|
|
|
|
//
|
|
|
|
// uint_p
|
|
|
|
//
|
|
|
|
template<
|
2011-11-14 06:34:28 +00:00
|
|
|
typename UIntType,
|
|
|
|
std::size_t Radix = 10,
|
2011-11-14 04:22:04 +00:00
|
|
|
std::size_t MinDigits = 1,
|
2011-11-14 06:34:28 +00:00
|
|
|
std::size_t MaxDigits = sprout::integer_digits<UIntType, Radix>::value
|
2011-11-14 04:22:04 +00:00
|
|
|
>
|
|
|
|
struct uint_p
|
|
|
|
: public sprout::weed::parser_base
|
|
|
|
{
|
2011-11-14 06:34:28 +00:00
|
|
|
private:
|
|
|
|
SPROUT_STATIC_CONSTEXPR std::size_t limit_digits = MaxDigits <= sprout::integer_digits<UIntType, Radix>::value
|
|
|
|
? MaxDigits
|
|
|
|
: sprout::integer_digits<UIntType, Radix>::value
|
|
|
|
;
|
2011-11-14 04:22:04 +00:00
|
|
|
public:
|
|
|
|
template<typename Context, typename Iterator>
|
2013-03-25 03:43:47 +00:00
|
|
|
struct attribute
|
|
|
|
: public sprout::identity<UIntType>
|
|
|
|
{};
|
2011-11-14 04:22:04 +00:00
|
|
|
template<typename Context, typename Iterator>
|
2013-03-25 03:43:47 +00:00
|
|
|
struct result
|
|
|
|
: public sprout::identity<sprout::weed::parser_result<Iterator, typename attribute<Context, Iterator>::type> >
|
|
|
|
{};
|
2011-11-14 04:22:04 +00:00
|
|
|
private:
|
2011-11-14 06:34:28 +00:00
|
|
|
template<typename Context, typename Iterator>
|
|
|
|
SPROUT_CONSTEXPR typename result<Context, Iterator>::type make_result(
|
|
|
|
typename result<Context, Iterator>::type const& res,
|
|
|
|
int sign = 0
|
|
|
|
) const
|
|
|
|
{
|
|
|
|
typedef typename result<Context, Iterator>::type result_type;
|
|
|
|
return res.success() && sign < 0
|
|
|
|
? result_type(true, res.current(), -res.attr())
|
|
|
|
: res
|
|
|
|
;
|
|
|
|
}
|
2011-11-14 04:22:04 +00:00
|
|
|
template<std::size_t N, typename Context, typename Iterator, typename PResult>
|
|
|
|
SPROUT_CONSTEXPR typename std::enable_if<
|
2011-11-14 06:34:28 +00:00
|
|
|
N == limit_digits,
|
2011-11-14 04:22:04 +00:00
|
|
|
typename result<Context, Iterator>::type
|
|
|
|
>::type call_1(
|
|
|
|
Iterator first,
|
|
|
|
Iterator last,
|
|
|
|
Iterator temp_first,
|
2011-11-14 06:34:28 +00:00
|
|
|
UIntType t,
|
|
|
|
std::size_t n,
|
2011-11-14 04:22:04 +00:00
|
|
|
PResult const& res
|
|
|
|
) const
|
|
|
|
{
|
|
|
|
typedef typename result<Context, Iterator>::type result_type;
|
|
|
|
typedef typename attribute<Context, Iterator>::type attribute_type;
|
|
|
|
return sprout::tuples::get<1>(res)
|
2011-11-14 06:34:28 +00:00
|
|
|
? n < MaxDigits
|
|
|
|
? call_0<N - 1, Context>(
|
|
|
|
sprout::next(first),
|
|
|
|
last,
|
|
|
|
temp_first,
|
|
|
|
static_cast<UIntType>(t * Radix + sprout::tuples::get<0>(res)),
|
|
|
|
n
|
|
|
|
)
|
|
|
|
: result_type(
|
|
|
|
true,
|
|
|
|
sprout::next(first),
|
|
|
|
static_cast<UIntType>(t * Radix + sprout::tuples::get<0>(res))
|
|
|
|
)
|
2011-11-14 04:22:04 +00:00
|
|
|
: N < MinDigits
|
|
|
|
? result_type(false, temp_first, attribute_type())
|
|
|
|
: result_type(true, first, t)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<std::size_t N, typename Context, typename Iterator, typename PResult>
|
|
|
|
SPROUT_CONSTEXPR typename std::enable_if<
|
2011-11-14 06:34:28 +00:00
|
|
|
N != limit_digits,
|
2011-11-14 04:22:04 +00:00
|
|
|
typename result<Context, Iterator>::type
|
|
|
|
>::type call_1(
|
|
|
|
Iterator first,
|
|
|
|
Iterator last,
|
|
|
|
Iterator temp_first,
|
2011-11-14 06:34:28 +00:00
|
|
|
UIntType t,
|
|
|
|
std::size_t n,
|
2011-11-14 04:22:04 +00:00
|
|
|
PResult const& res
|
|
|
|
) const
|
|
|
|
{
|
|
|
|
typedef typename result<Context, Iterator>::type result_type;
|
|
|
|
typedef typename attribute<Context, Iterator>::type attribute_type;
|
|
|
|
return sprout::tuples::get<1>(res)
|
|
|
|
? call_0<N, Context>(
|
|
|
|
sprout::next(first),
|
2011-11-14 06:34:28 +00:00
|
|
|
last,
|
|
|
|
temp_first,
|
|
|
|
static_cast<UIntType>(t * Radix + sprout::tuples::get<0>(res)),
|
|
|
|
n
|
2011-11-14 04:22:04 +00:00
|
|
|
)
|
|
|
|
: N < MinDigits
|
|
|
|
? result_type(false, temp_first, attribute_type())
|
|
|
|
: result_type(true, first, t)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<std::size_t N, typename Context, typename Iterator>
|
|
|
|
SPROUT_CONSTEXPR typename result<Context, Iterator>::type call_0(
|
|
|
|
Iterator first,
|
|
|
|
Iterator last,
|
|
|
|
Iterator temp_first,
|
2011-11-14 06:34:28 +00:00
|
|
|
UIntType t,
|
|
|
|
std::size_t n
|
2011-11-14 04:22:04 +00:00
|
|
|
) const
|
|
|
|
{
|
|
|
|
typedef typename result<Context, Iterator>::type result_type;
|
|
|
|
typedef typename attribute<Context, Iterator>::type attribute_type;
|
|
|
|
return first != last
|
|
|
|
? call_1<N + 1, Context>(
|
|
|
|
first,
|
|
|
|
last,
|
|
|
|
temp_first,
|
|
|
|
t,
|
2011-11-14 06:34:28 +00:00
|
|
|
n + 1,
|
|
|
|
sprout::weed::detail::from_ndigit<Radix, UIntType>(*first)
|
2011-11-14 04:22:04 +00:00
|
|
|
)
|
|
|
|
: N < MinDigits
|
|
|
|
? result_type(false, temp_first, attribute_type())
|
|
|
|
: result_type(true, first, t)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename Context, typename Iterator, typename PResult>
|
2011-11-14 06:34:28 +00:00
|
|
|
SPROUT_CONSTEXPR typename result<Context, Iterator>::type call_i(
|
2011-11-14 04:22:04 +00:00
|
|
|
Iterator first,
|
|
|
|
Iterator last,
|
|
|
|
Iterator temp_first,
|
|
|
|
PResult const& res
|
|
|
|
) const
|
|
|
|
{
|
|
|
|
typedef typename result<Context, Iterator>::type result_type;
|
|
|
|
typedef typename attribute<Context, Iterator>::type attribute_type;
|
|
|
|
return sprout::tuples::get<1>(res)
|
|
|
|
? call_0<1, Context>(
|
|
|
|
sprout::next(first),
|
|
|
|
last,
|
|
|
|
temp_first,
|
2011-11-14 06:34:28 +00:00
|
|
|
sprout::tuples::get<0>(res),
|
|
|
|
1
|
2011-11-14 04:22:04 +00:00
|
|
|
)
|
|
|
|
: result_type(false, temp_first, attribute_type())
|
|
|
|
;
|
|
|
|
}
|
2011-11-14 06:34:28 +00:00
|
|
|
template<typename Context, typename Iterator>
|
|
|
|
SPROUT_CONSTEXPR typename result<Context, Iterator>::type call(
|
|
|
|
Iterator first,
|
|
|
|
Iterator last,
|
|
|
|
Iterator temp_first
|
|
|
|
) const
|
|
|
|
{
|
|
|
|
return call_i<Context>(
|
|
|
|
first,
|
|
|
|
last,
|
|
|
|
temp_first,
|
|
|
|
sprout::weed::detail::from_ndigit<Radix, UIntType>(*first)
|
|
|
|
);
|
|
|
|
}
|
2011-11-14 04:22:04 +00:00
|
|
|
public:
|
|
|
|
template<typename Context, typename Iterator>
|
|
|
|
SPROUT_CONSTEXPR typename result<Context, Iterator>::type operator()(
|
|
|
|
Iterator first,
|
|
|
|
Iterator last,
|
|
|
|
Context const&
|
|
|
|
) const
|
|
|
|
{
|
|
|
|
typedef typename result<Context, Iterator>::type result_type;
|
|
|
|
typedef typename attribute<Context, Iterator>::type attribute_type;
|
|
|
|
return first != last
|
|
|
|
? call<Context>(
|
|
|
|
first,
|
|
|
|
last,
|
2011-11-14 06:34:28 +00:00
|
|
|
first
|
2011-11-14 04:22:04 +00:00
|
|
|
)
|
|
|
|
: result_type(false, first, attribute_type())
|
|
|
|
;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace weed
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_WEED_PARSER_NUMERIC_UINT_P_HPP
|