list.hpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*=============================================================================
  2. Copyright (c) 2001-2014 Joel de Guzman
  3. Copyright (c) 2001-2011 Hartmut Kaiser
  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_LIST_MARCH_24_2007_1031AM)
  8. #define BOOST_SPIRIT_X3_LIST_MARCH_24_2007_1031AM
  9. #include <boost/spirit/home/x3/core/parser.hpp>
  10. #include <boost/spirit/home/x3/support/traits/container_traits.hpp>
  11. #include <boost/spirit/home/x3/support/traits/attribute_of.hpp>
  12. #include <boost/spirit/home/x3/core/detail/parse_into_container.hpp>
  13. namespace boost { namespace spirit { namespace x3
  14. {
  15. template <typename Left, typename Right>
  16. struct list : binary_parser<Left, Right, list<Left, Right>>
  17. {
  18. typedef binary_parser<Left, Right, list<Left, Right>> base_type;
  19. static bool const handles_container = true;
  20. constexpr list(Left const& left, Right const& right)
  21. : base_type(left, right) {}
  22. template <typename Iterator, typename Context
  23. , typename RContext, typename Attribute>
  24. bool parse(Iterator& first, Iterator const& last
  25. , Context const& context, RContext& rcontext, Attribute& attr) const
  26. {
  27. // in order to succeed we need to match at least one element
  28. if (!detail::parse_into_container(
  29. this->left, first, last, context, rcontext, attr))
  30. return false;
  31. Iterator iter = first;
  32. while (this->right.parse(iter, last, context, rcontext, unused)
  33. && detail::parse_into_container(
  34. this->left, iter, last, context, rcontext, attr))
  35. {
  36. first = iter;
  37. }
  38. return true;
  39. }
  40. };
  41. template <typename Left, typename Right>
  42. constexpr list<
  43. typename extension::as_parser<Left>::value_type
  44. , typename extension::as_parser<Right>::value_type>
  45. operator%(Left const& left, Right const& right)
  46. {
  47. return { as_parser(left), as_parser(right) };
  48. }
  49. }}}
  50. namespace boost { namespace spirit { namespace x3 { namespace traits
  51. {
  52. template <typename Left, typename Right, typename Context>
  53. struct attribute_of<x3::list<Left, Right>, Context>
  54. : traits::build_container<
  55. typename attribute_of<Left, Context>::type> {};
  56. template <typename Left, typename Right, typename Context>
  57. struct has_attribute<x3::list<Left, Right>, Context>
  58. : has_attribute<Left, Context> {};
  59. }}}}
  60. #endif