#ifndef CHILON_PARSER_MANY_HPP
#define CHILON_PARSER_MANY_HPP
#include <chilon/parser/detail/many.hpp>
#include <chilon/parser/parse.hpp>
#include <chilon/parser/skipped.hpp>
#include <chilon/parser/stored.hpp>
namespace chilon { namespace parser {
template <class H, bool SkipWhitespace, bool AtLeastOne = false>
struct many_base {
template <class Stream>
inline static bool skip(Stream& stream) {
if (AtLeastOne) {
if (! H::skip(stream)) return false;
if (SkipWhitespace) stream.skip_whitespace();
if (stream.empty()) return true;
}
while (H::skip(stream)) {
if (SkipWhitespace) stream.skip_whitespace();
if (stream.empty()) return true;
};
return true;
}
template <class Stream>
inline static bool match(Stream& stream) {
return ! AtLeastOne || H::match(stream);
}
};
/**
* H^*
* Equivalent to H* where each H cannot be separated by whitespace.
* Stores a range always.
*/
template <class... H>
struct many_range : many_base<sequence<H...>, false> {};
template <class... H>
struct many_plus_range : many_base<sequence<H...>, false, true> {};
/**
* H~*
* Equivalent to H* where each H can be separated by whitespace.
*/
template <class... H>
struct many_list : many_base<sequence<H...>, true> {};
template <class... H>
struct many_plus_list : many_base<sequence<H...>, true, true> {};
/**
* (...)*
* many<T...> -> many< sequence<T...> >
*/
template <class... T>
struct many : many< sequence<T...> > {};
template <class... T>
struct many_plus : many_plus< sequence<T...> > {};
/**
* Redirect character matches to no-whitespace version of many which stores
* a string, otherwise redirect to whitespace version which stores a vector
* of its results.
*/
template <class H>
struct many<H> :
detail::make_many<typename skipped_store<H>::type, H> {};
template <class H>
struct many_plus<H> :
detail::make_many_plus<typename skipped_store<H>::type, H> {};
template <class H>
struct parse<many_range<H>> : detail::parse_many_range<H, false> {};
template <class H>
struct parse<many_plus_range<H>> : detail::parse_many_range<H, true> {};
template <class H>
struct parse<many_list<H>> : detail::parse_many_list<H, false> {};
template <class H>
struct parse<many_plus_list<H>> : detail::parse_many_list<H, true> {};
} }
#endif