sequence.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*=============================================================================
  2. Copyright (c) 2001-2014 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. =============================================================================*/
  6. #if !defined(BOOST_SPIRIT_X3_SEQUENCE_JAN_06_2013_1015AM)
  7. #define BOOST_SPIRIT_X3_SEQUENCE_JAN_06_2013_1015AM
  8. #include <boost/spirit/home/x3/support/traits/attribute_of.hpp>
  9. #include <boost/spirit/home/x3/core/parser.hpp>
  10. #include <boost/spirit/home/x3/operator/detail/sequence.hpp>
  11. #include <boost/spirit/home/x3/directive/expect.hpp>
  12. namespace boost { namespace spirit { namespace x3
  13. {
  14. template <typename Left, typename Right>
  15. struct sequence : binary_parser<Left, Right, sequence<Left, Right>>
  16. {
  17. typedef binary_parser<Left, Right, sequence<Left, Right>> base_type;
  18. sequence(Left const& left, Right const& right)
  19. : base_type(left, right) {}
  20. template <typename Iterator, typename Context, typename RContext>
  21. bool parse(
  22. Iterator& first, Iterator const& last
  23. , Context const& context, RContext& rcontext, unused_type) const
  24. {
  25. Iterator save = first;
  26. if (this->left.parse(first, last, context, rcontext, unused)
  27. && this->right.parse(first, last, context, rcontext, unused))
  28. return true;
  29. first = save;
  30. return false;
  31. }
  32. template <typename Iterator, typename Context
  33. , typename RContext, typename Attribute>
  34. bool parse(
  35. Iterator& first, Iterator const& last
  36. , Context const& context, RContext& rcontext, Attribute& attr) const
  37. {
  38. return detail::parse_sequence(*this, first, last, context, rcontext, attr
  39. , typename traits::attribute_category<Attribute>::type());
  40. }
  41. };
  42. template <typename Left, typename Right>
  43. inline sequence<
  44. typename extension::as_parser<Left>::value_type
  45. , typename extension::as_parser<Right>::value_type>
  46. operator>>(Left const& left, Right const& right)
  47. {
  48. return { as_parser(left), as_parser(right) };
  49. }
  50. template <typename Left, typename Right>
  51. auto operator>(Left const& left, Right const& right)
  52. {
  53. return left >> expect[right];
  54. }
  55. }}}
  56. namespace boost { namespace spirit { namespace x3 { namespace traits
  57. {
  58. template <typename Left, typename Right, typename Context>
  59. struct attribute_of<x3::sequence<Left, Right>, Context>
  60. : x3::detail::attribute_of_sequence<Left, Right, Context> {};
  61. }}}}
  62. #endif