Sprout/sprout/weed/parser/string/string.hpp

167 lines
5 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
Copyright (c) 2011-2013 Bolero MURAKAMI
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_STRING_STRING_HPP
#define SPROUT_WEED_PARSER_STRING_STRING_HPP
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/string.hpp>
#include <sprout/container/begin.hpp>
#include <sprout/container/end.hpp>
#include <sprout/container/size.hpp>
2013-01-11 19:08:44 +00:00
#include <sprout/iterator/operation.hpp>
#include <sprout/algorithm/equal.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/unused.hpp>
#include <sprout/weed/parser_result.hpp>
#include <sprout/weed/parser/parser_base.hpp>
#include <sprout/weed/parser/lit.hpp>
#include <sprout/weed/traits/type/is_c_str.hpp>
#include <sprout/weed/traits/type/is_string.hpp>
#include <sprout/weed/detail/c_str_as_string.hpp>
2011-11-13 08:54:38 +00:00
namespace sprout {
namespace weed {
//
// lit_str_p
//
template<typename T>
struct lit_str_p
: public sprout::weed::parser_base
{
public:
template<typename Context, typename Iterator>
2013-03-25 03:43:47 +00:00
struct attribute
: public sprout::identity<sprout::weed::unused>
{};
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:
T t_;
public:
lit_str_p() = default;
2012-04-11 14:28:29 +00:00
explicit SPROUT_CONSTEXPR lit_str_p(T const& t)
2011-11-13 08:54:38 +00:00
: t_(t)
{}
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 sprout::distance(first, last) >= sprout::size(t_)
2013-01-11 19:08:44 +00:00
&& sprout::equal(sprout::begin(t_), sprout::end(t_), first)
2011-11-13 08:54:38 +00:00
? result_type(true, sprout::next(first, sprout::size(t_)), attribute_type())
: result_type(false, first, typename attribute<Context, Iterator>::type())
;
}
};
//
// lit_g
//
template<typename T>
struct lit_g::eval<
T,
typename std::enable_if<
sprout::weed::traits::is_c_str<T>::value
>::type
> {
public:
typedef sprout::weed::lit_str_p<
typename sprout::weed::detail::c_str_as_string<T const>::type
> result_type;
public:
SPROUT_CONSTEXPR result_type operator()(T const& t) const {
return result_type(sprout::to_string(t));
}
};
template<typename T>
struct lit_g::eval<
T,
typename std::enable_if<
sprout::weed::traits::is_string<T>::value
>::type
> {
public:
typedef sprout::weed::lit_str_p<T> result_type;
public:
SPROUT_CONSTEXPR result_type operator()(T const& t) const {
return result_type(t);
}
};
//
// str_p
//
template<typename T>
struct str_p
: public sprout::weed::parser_base
{
public:
template<typename Context, typename Iterator>
2013-03-25 03:43:47 +00:00
struct attribute
: public sprout::identity<T>
{};
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:
T t_;
public:
str_p() = default;
2012-04-11 14:28:29 +00:00
explicit SPROUT_CONSTEXPR str_p(T const& t)
2011-11-13 08:54:38 +00:00
: t_(t)
{}
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 sprout::distance(first, last) >= sprout::size(t_)
2013-01-11 19:08:44 +00:00
&& sprout::equal(sprout::begin(t_), sprout::end(t_), first)
2011-11-13 08:54:38 +00:00
? result_type(true, sprout::next(first, sprout::size(t_)), attribute_type())
: result_type(false, first, typename attribute<Context, Iterator>::type())
;
}
};
//
// string
//
template<typename T>
inline SPROUT_CONSTEXPR typename std::enable_if<
2011-11-13 08:54:38 +00:00
sprout::weed::traits::is_c_str<T const>::value,
sprout::weed::str_p<
typename sprout::weed::detail::c_str_as_string<T const>::type
>
>::type string(T const& t) {
return sprout::weed::str_p<
typename sprout::weed::detail::c_str_as_string<T const>::type
>(sprout::to_string(t));
}
template<typename T>
inline SPROUT_CONSTEXPR typename std::enable_if<
2011-11-13 08:54:38 +00:00
sprout::weed::traits::is_string<T>::value,
sprout::weed::str_p<T>
>::type string(T const& t) {
return sprout::weed::str_p<T>(t);
}
} // namespace weed
} // namespace sprout
#endif // #ifndef SPROUT_WEED_PARSER_STRING_STRING_HPP