sequence.hpp 3.2 KB

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