list.hpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. static bool const has_attribute = true;
  21. list(Left const& left, Right const& right)
  22. : base_type(left, right) {}
  23. template <typename Iterator, typename Context
  24. , typename RContext, typename Attribute>
  25. bool parse(Iterator& first, Iterator const& last
  26. , Context const& context, RContext& rcontext, Attribute& attr) const
  27. {
  28. // in order to succeed we need to match at least one element
  29. if (!detail::parse_into_container(
  30. this->left, first, last, context, rcontext, attr))
  31. return false;
  32. Iterator iter = first;
  33. while (this->right.parse(iter, last, context, rcontext, unused)
  34. && detail::parse_into_container(
  35. this->left, iter, last, context, rcontext, attr))
  36. {
  37. first = iter;
  38. }
  39. return true;
  40. }
  41. };
  42. template <typename Left, typename Right>
  43. inline list<
  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. }}}
  51. namespace boost { namespace spirit { namespace x3 { namespace traits
  52. {
  53. template <typename Left, typename Right, typename Context>
  54. struct attribute_of<x3::list<Left, Right>, Context>
  55. : traits::build_container<
  56. typename attribute_of<Left, Context>::type> {};
  57. }}}}
  58. #endif