seek.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*=============================================================================
  2. Copyright (c) 2011 Jamboree
  3. Copyright (c) 2014 Lee Clagett
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #if !defined(BOOST_SPIRIT_X3_SEEK_APRIL_13_2014_1920PM)
  8. #define BOOST_SPIRIT_X3_SEEK_APRIL_13_2014_1920PM
  9. #include <boost/spirit/home/x3/core/parser.hpp>
  10. namespace boost { namespace spirit { namespace x3
  11. {
  12. template<typename Subject>
  13. struct seek_directive : unary_parser<Subject, seek_directive<Subject>>
  14. {
  15. typedef unary_parser<Subject, seek_directive<Subject>> base_type;
  16. static bool const is_pass_through_unary = true;
  17. static bool const handles_container = Subject::handles_container;
  18. constexpr seek_directive(Subject const& subject) :
  19. base_type(subject) {}
  20. template<typename Iterator, typename Context
  21. , typename RContext, typename Attribute>
  22. bool parse(
  23. Iterator& first, Iterator const& last
  24. , Context const& context, RContext& rcontext, Attribute& attr) const
  25. {
  26. for (Iterator current(first);; ++current)
  27. {
  28. if (this->subject.parse(current, last, context, rcontext, attr))
  29. {
  30. first = current;
  31. return true;
  32. }
  33. // fail only after subject fails & no input
  34. if (current == last)
  35. return false;
  36. }
  37. }
  38. };
  39. struct seek_gen
  40. {
  41. template<typename Subject>
  42. constexpr seek_directive<typename extension::as_parser<Subject>::value_type>
  43. operator[](Subject const& subject) const
  44. {
  45. return { as_parser(subject) };
  46. }
  47. };
  48. constexpr auto seek = seek_gen{};
  49. }}}
  50. #endif