1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00
Sprout/sprout/weed/parser/lim.hpp

102 lines
3.5 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2015-01-10 10:13:57 +00:00
Copyright (c) 2011-2015 Bolero MURAKAMI
2013-08-08 09:54:33 +00:00
https://github.com/bolero-MURAKAMI/Sprout
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
2011-11-13 08:54:38 +00:00
#ifndef SPROUT_WEED_PARSER_LIM_HPP
#define SPROUT_WEED_PARSER_LIM_HPP
#include <sprout/config.hpp>
#include <sprout/workaround/std/cstddef.hpp>
#include <sprout/type_traits/integral_constant.hpp>
2013-03-25 03:43:47 +00:00
#include <sprout/type_traits/identity.hpp>
2011-11-13 08:54:38 +00:00
#include <sprout/weed/parser_result.hpp>
#include <sprout/weed/limited.hpp>
#include <sprout/weed/expr/make_terminal_or_expr.hpp>
#include <sprout/weed/expr/eval.hpp>
#include <sprout/weed/parser/parser_base.hpp>
#include <sprout/weed/traits/expr/terminal_or_expr_of.hpp>
#include <sprout/weed/traits/parser/attribute_of.hpp>
#include <sprout/weed/traits/parser/limit_of.hpp>
namespace sprout {
namespace weed {
//
// limit_p
//
template<typename Parser, std::size_t Limit>
struct limit_p
: public sprout::weed::parser_base
{
public:
SPROUT_STATIC_CONSTEXPR std::size_t limit = Limit;
public:
template<typename Context, typename Iterator>
2013-03-25 03:43:47 +00:00
struct attribute
: public sprout::weed::traits::attribute_of<Parser, Iterator, Context>
{};
2011-11-13 08:54:38 +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-13 08:54:38 +00:00
private:
typedef typename sprout::weed::traits::terminal_or_expr_of<Parser>::type expr_type;
private:
expr_type expr_;
sprout::weed::limited::category limited_category_;
public:
2014-01-13 15:18:06 +00:00
SPROUT_CONSTEXPR limit_p() SPROUT_DEFAULTED_DEFAULT_CONSTRUCTOR_DECL
2012-04-11 14:28:29 +00:00
explicit SPROUT_CONSTEXPR limit_p(
2011-11-13 08:54:38 +00:00
Parser const& p,
sprout::weed::limited::category limited_category = sprout::weed::limited::discard
)
: expr_(sprout::weed::make_terminal_or_expr(p))
, limited_category_(limited_category)
{}
SPROUT_CONSTEXPR sprout::weed::limited::category limited_category() const {
return limited_category_;
}
template<typename Context, typename Iterator>
SPROUT_CONSTEXPR typename result<Context, Iterator>::type operator()(
2013-07-22 13:00:09 +00:00
Iterator, Iterator,
2011-11-13 08:54:38 +00:00
Context const& ctx
) const
{
return sprout::weed::eval(expr_, ctx).presult();
}
};
template<typename Parser, std::size_t Limit>
SPROUT_CONSTEXPR_OR_CONST std::size_t limit_p<Parser, Limit>::limit;
2011-11-13 08:54:38 +00:00
//
// lim
//
template<std::size_t Limit, typename Parser>
SPROUT_CONSTEXPR sprout::weed::limit_p<Parser, Limit> lim(Parser const& p) {
return sprout::weed::limit_p<Parser, Limit>(p);
}
template<std::size_t Limit, typename Parser>
SPROUT_CONSTEXPR sprout::weed::limit_p<Parser, Limit> lim(
Parser const& p,
sprout::weed::limited::category limited_category
)
{
return sprout::weed::limit_p<Parser, Limit>(p, limited_category);
}
namespace traits {
//
// limit_of
//
template<typename Parser, std::size_t Limit, typename Iterator, typename Context>
struct limit_of<sprout::weed::limit_p<Parser, Limit>, Iterator, Context>
: public sprout::integral_constant<std::size_t, Limit>
2011-11-13 08:54:38 +00:00
{};
} // namespace traits
} // namespace weed
} // namespace sprout
#endif // #ifndef SPROUT_WEED_PARSER_LIM_HPP