mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2024-12-23 21:25:49 +00:00
sprout/weed/parser/numeric/int.hpp 追加
This commit is contained in:
parent
ad94c0b247
commit
005f8a9a87
5 changed files with 329 additions and 23 deletions
|
@ -3,7 +3,9 @@
|
|||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/weed/parser/numeric/uint_p.hpp>
|
||||
#include <sprout/weed/parser/numeric/int_p.hpp>
|
||||
#include <sprout/weed/parser/numeric/uint.hpp>
|
||||
#include <sprout/weed/parser/numeric/int.hpp>
|
||||
#include <sprout/weed/parser/numeric/bin.hpp>
|
||||
#include <sprout/weed/parser/numeric/oct.hpp>
|
||||
#include <sprout/weed/parser/numeric/hex.hpp>
|
||||
|
|
29
sprout/weed/parser/numeric/int.hpp
Normal file
29
sprout/weed/parser/numeric/int.hpp
Normal file
|
@ -0,0 +1,29 @@
|
|||
#ifndef SPROUT_WEED_PARSER_NUMERIC_INT_HPP
|
||||
#define SPROUT_WEED_PARSER_NUMERIC_INT_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/integer/integer_digits.hpp>
|
||||
#include <sprout/weed/parser/numeric/int_p.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace weed {
|
||||
//
|
||||
// int_
|
||||
//
|
||||
SPROUT_STATIC_CONSTEXPR auto int_ = sprout::weed::int_p<std::intmax_t, 10, 1, -1>();
|
||||
|
||||
//
|
||||
// int8
|
||||
// int16
|
||||
// int32
|
||||
// int64
|
||||
//
|
||||
SPROUT_STATIC_CONSTEXPR auto int8 = sprout::weed::int_p<std::int8_t, 10>();
|
||||
SPROUT_STATIC_CONSTEXPR auto int16 = sprout::weed::int_p<std::int16_t, 10>();
|
||||
SPROUT_STATIC_CONSTEXPR auto int32 = sprout::weed::int_p<std::int32_t, 10>();
|
||||
SPROUT_STATIC_CONSTEXPR auto int64 = sprout::weed::int_p<std::int64_t, 10>();
|
||||
} // namespace weed
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_WEED_PARSER_NUMERIC_INT_HPP
|
228
sprout/weed/parser/numeric/int_p.hpp
Normal file
228
sprout/weed/parser/numeric/int_p.hpp
Normal file
|
@ -0,0 +1,228 @@
|
|||
#ifndef SPROUT_WEED_PARSER_NUMERIC_INT_P_HPP
|
||||
#define SPROUT_WEED_PARSER_NUMERIC_INT_P_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/iterator/next.hpp>
|
||||
#include <sprout/tuple/tuple.hpp>
|
||||
#include <sprout/integer/integer_digits.hpp>
|
||||
#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 {
|
||||
//
|
||||
// int_p
|
||||
//
|
||||
template<
|
||||
typename IntType,
|
||||
std::size_t Radix = 10,
|
||||
std::size_t MinDigits = 1,
|
||||
std::size_t MaxDigits = sprout::integer_digits<IntType, Radix>::value
|
||||
>
|
||||
struct int_p
|
||||
: public sprout::weed::parser_base
|
||||
{
|
||||
private:
|
||||
SPROUT_STATIC_CONSTEXPR std::size_t limit_digits = MaxDigits <= sprout::integer_digits<IntType, Radix>::value
|
||||
? MaxDigits
|
||||
: sprout::integer_digits<IntType, Radix>::value
|
||||
;
|
||||
public:
|
||||
template<typename Context, typename Iterator>
|
||||
struct attribute {
|
||||
public:
|
||||
typedef IntType type;
|
||||
};
|
||||
template<typename Context, typename Iterator>
|
||||
struct result {
|
||||
public:
|
||||
typedef sprout::weed::parser_result<Iterator, typename attribute<Context, Iterator>::type> type;
|
||||
};
|
||||
private:
|
||||
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;
|
||||
typedef typename attribute<Context, Iterator>::type attribute_type;
|
||||
return res.success() && sign < 0
|
||||
? result_type(true, res.current(), -res.attr())
|
||||
: res
|
||||
;
|
||||
}
|
||||
template<std::size_t N, typename Context, typename Iterator, typename PResult>
|
||||
SPROUT_CONSTEXPR typename std::enable_if<
|
||||
N == limit_digits,
|
||||
typename result<Context, Iterator>::type
|
||||
>::type call_1(
|
||||
Iterator first,
|
||||
Iterator last,
|
||||
Iterator temp_first,
|
||||
IntType t,
|
||||
std::size_t n,
|
||||
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)
|
||||
? n < MaxDigits
|
||||
? call_0<N - 1, Context>(
|
||||
sprout::next(first),
|
||||
last,
|
||||
temp_first,
|
||||
static_cast<IntType>(t * Radix + sprout::tuples::get<0>(res)),
|
||||
n
|
||||
)
|
||||
: result_type(
|
||||
true,
|
||||
sprout::next(first),
|
||||
static_cast<IntType>(t * Radix + sprout::tuples::get<0>(res))
|
||||
)
|
||||
: 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<
|
||||
N != limit_digits,
|
||||
typename result<Context, Iterator>::type
|
||||
>::type call_1(
|
||||
Iterator first,
|
||||
Iterator last,
|
||||
Iterator temp_first,
|
||||
IntType t,
|
||||
std::size_t n,
|
||||
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),
|
||||
last,
|
||||
temp_first,
|
||||
static_cast<IntType>(t * Radix + sprout::tuples::get<0>(res)),
|
||||
n
|
||||
)
|
||||
: 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,
|
||||
IntType t,
|
||||
std::size_t n
|
||||
) 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,
|
||||
n + 1,
|
||||
sprout::weed::detail::from_ndigit<Radix, IntType>(*first)
|
||||
)
|
||||
: N < MinDigits
|
||||
? result_type(false, temp_first, attribute_type())
|
||||
: result_type(true, first, t)
|
||||
;
|
||||
}
|
||||
template<typename Context, typename Iterator, typename PResult>
|
||||
SPROUT_CONSTEXPR typename result<Context, Iterator>::type call_i(
|
||||
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,
|
||||
sprout::tuples::get<0>(res),
|
||||
1
|
||||
)
|
||||
: result_type(false, temp_first, attribute_type())
|
||||
;
|
||||
}
|
||||
template<typename Context, typename Iterator>
|
||||
SPROUT_CONSTEXPR typename result<Context, Iterator>::type call(
|
||||
Iterator first,
|
||||
Iterator last,
|
||||
Iterator temp_first,
|
||||
int sign = 0
|
||||
) const
|
||||
{
|
||||
typedef typename result<Context, Iterator>::type result_type;
|
||||
typedef typename attribute<Context, Iterator>::type attribute_type;
|
||||
return first != last
|
||||
? make_result<Context, Iterator>(
|
||||
call_i<Context>(
|
||||
first,
|
||||
last,
|
||||
temp_first,
|
||||
sprout::weed::detail::from_ndigit<Radix, IntType>(*first)
|
||||
),
|
||||
sign
|
||||
)
|
||||
: result_type(false, temp_first, attribute_type())
|
||||
;
|
||||
}
|
||||
public:
|
||||
template<typename Context, typename Iterator>
|
||||
SPROUT_CONSTEXPR typename result<Context, Iterator>::type operator()(
|
||||
Iterator first,
|
||||
Iterator last,
|
||||
Context const&
|
||||
) const
|
||||
{
|
||||
typedef typename std::iterator_traits<Iterator>::value_type elem_type;
|
||||
typedef typename result<Context, Iterator>::type result_type;
|
||||
typedef typename attribute<Context, Iterator>::type attribute_type;
|
||||
return first != last
|
||||
? *first == elem_type('+')
|
||||
? call<Context>(
|
||||
sprout::next(first),
|
||||
last,
|
||||
first,
|
||||
1
|
||||
)
|
||||
: *first == elem_type('-')
|
||||
? call<Context>(
|
||||
sprout::next(first),
|
||||
last,
|
||||
first,
|
||||
-1
|
||||
)
|
||||
: call<Context>(
|
||||
first,
|
||||
last,
|
||||
first
|
||||
)
|
||||
: result_type(false, first, attribute_type())
|
||||
;
|
||||
}
|
||||
};
|
||||
} // namespace weed
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_WEED_PARSER_NUMERIC_INT_P_HPP
|
||||
|
|
@ -9,9 +9,9 @@
|
|||
namespace sprout {
|
||||
namespace weed {
|
||||
//
|
||||
// uint
|
||||
// uint_
|
||||
//
|
||||
SPROUT_STATIC_CONSTEXPR auto uint = sprout::weed::uint_p<std::uintmax_t, 10, -1>();
|
||||
SPROUT_STATIC_CONSTEXPR auto uint_ = sprout::weed::uint_p<std::uintmax_t, 10, 1, -1>();
|
||||
|
||||
//
|
||||
// uint8
|
||||
|
|
|
@ -17,19 +17,24 @@ namespace sprout {
|
|||
// uint_p
|
||||
//
|
||||
template<
|
||||
typename IntType,
|
||||
std::size_t Radix,
|
||||
typename UIntType,
|
||||
std::size_t Radix = 10,
|
||||
std::size_t MinDigits = 1,
|
||||
std::size_t MaxDigits = sprout::integer_digits<IntType, Radix>::value
|
||||
std::size_t MaxDigits = sprout::integer_digits<UIntType, Radix>::value
|
||||
>
|
||||
struct uint_p
|
||||
: public sprout::weed::parser_base
|
||||
{
|
||||
private:
|
||||
SPROUT_STATIC_CONSTEXPR std::size_t limit_digits = MaxDigits <= sprout::integer_digits<UIntType, Radix>::value
|
||||
? MaxDigits
|
||||
: sprout::integer_digits<UIntType, Radix>::value
|
||||
;
|
||||
public:
|
||||
template<typename Context, typename Iterator>
|
||||
struct attribute {
|
||||
public:
|
||||
typedef IntType type;
|
||||
typedef UIntType type;
|
||||
};
|
||||
template<typename Context, typename Iterator>
|
||||
struct result {
|
||||
|
@ -37,26 +42,48 @@ namespace sprout {
|
|||
typedef sprout::weed::parser_result<Iterator, typename attribute<Context, Iterator>::type> type;
|
||||
};
|
||||
private:
|
||||
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;
|
||||
typedef typename attribute<Context, Iterator>::type attribute_type;
|
||||
return res.success() && sign < 0
|
||||
? result_type(true, res.current(), -res.attr())
|
||||
: res
|
||||
;
|
||||
}
|
||||
template<std::size_t N, typename Context, typename Iterator, typename PResult>
|
||||
SPROUT_CONSTEXPR typename std::enable_if<
|
||||
N == MaxDigits,
|
||||
N == limit_digits,
|
||||
typename result<Context, Iterator>::type
|
||||
>::type call_1(
|
||||
Iterator first,
|
||||
Iterator last,
|
||||
Iterator temp_first,
|
||||
IntType t,
|
||||
UIntType t,
|
||||
std::size_t n,
|
||||
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)
|
||||
? result_type(
|
||||
true,
|
||||
sprout::next(first),
|
||||
static_cast<IntType>(t * Radix | sprout::tuples::get<0>(res))
|
||||
)
|
||||
? 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))
|
||||
)
|
||||
: N < MinDigits
|
||||
? result_type(false, temp_first, attribute_type())
|
||||
: result_type(true, first, t)
|
||||
|
@ -64,13 +91,14 @@ namespace sprout {
|
|||
}
|
||||
template<std::size_t N, typename Context, typename Iterator, typename PResult>
|
||||
SPROUT_CONSTEXPR typename std::enable_if<
|
||||
N != MaxDigits,
|
||||
N != limit_digits,
|
||||
typename result<Context, Iterator>::type
|
||||
>::type call_1(
|
||||
Iterator first,
|
||||
Iterator last,
|
||||
Iterator temp_first,
|
||||
IntType t,
|
||||
UIntType t,
|
||||
std::size_t n,
|
||||
PResult const& res
|
||||
) const
|
||||
{
|
||||
|
@ -79,8 +107,10 @@ namespace sprout {
|
|||
return sprout::tuples::get<1>(res)
|
||||
? call_0<N, Context>(
|
||||
sprout::next(first),
|
||||
last, temp_first,
|
||||
static_cast<IntType>(t * Radix | sprout::tuples::get<0>(res))
|
||||
last,
|
||||
temp_first,
|
||||
static_cast<UIntType>(t * Radix + sprout::tuples::get<0>(res)),
|
||||
n
|
||||
)
|
||||
: N < MinDigits
|
||||
? result_type(false, temp_first, attribute_type())
|
||||
|
@ -92,7 +122,8 @@ namespace sprout {
|
|||
Iterator first,
|
||||
Iterator last,
|
||||
Iterator temp_first,
|
||||
IntType t
|
||||
UIntType t,
|
||||
std::size_t n
|
||||
) const
|
||||
{
|
||||
typedef typename result<Context, Iterator>::type result_type;
|
||||
|
@ -103,7 +134,8 @@ namespace sprout {
|
|||
last,
|
||||
temp_first,
|
||||
t,
|
||||
sprout::weed::detail::from_ndigit<Radix, IntType>(*first)
|
||||
n + 1,
|
||||
sprout::weed::detail::from_ndigit<Radix, UIntType>(*first)
|
||||
)
|
||||
: N < MinDigits
|
||||
? result_type(false, temp_first, attribute_type())
|
||||
|
@ -111,7 +143,7 @@ namespace sprout {
|
|||
;
|
||||
}
|
||||
template<typename Context, typename Iterator, typename PResult>
|
||||
SPROUT_CONSTEXPR typename result<Context, Iterator>::type call(
|
||||
SPROUT_CONSTEXPR typename result<Context, Iterator>::type call_i(
|
||||
Iterator first,
|
||||
Iterator last,
|
||||
Iterator temp_first,
|
||||
|
@ -125,11 +157,26 @@ namespace sprout {
|
|||
sprout::next(first),
|
||||
last,
|
||||
temp_first,
|
||||
sprout::tuples::get<0>(res)
|
||||
sprout::tuples::get<0>(res),
|
||||
1
|
||||
)
|
||||
: result_type(false, temp_first, attribute_type())
|
||||
;
|
||||
}
|
||||
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)
|
||||
);
|
||||
}
|
||||
public:
|
||||
template<typename Context, typename Iterator>
|
||||
SPROUT_CONSTEXPR typename result<Context, Iterator>::type operator()(
|
||||
|
@ -144,8 +191,7 @@ namespace sprout {
|
|||
? call<Context>(
|
||||
first,
|
||||
last,
|
||||
first,
|
||||
sprout::weed::detail::from_ndigit<Radix, IntType>(*first)
|
||||
first
|
||||
)
|
||||
: result_type(false, first, attribute_type())
|
||||
;
|
||||
|
@ -155,3 +201,4 @@ namespace sprout {
|
|||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_WEED_PARSER_NUMERIC_UINT_P_HPP
|
||||
|
||||
|
|
Loading…
Reference in a new issue