matches.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*=============================================================================
  2. Copyright (c) 2015 Mario Lang
  3. Copyright (c) 2001-2011 Hartmut Kaiser
  4. Copyright (c) 2017 wanghan02
  5. Copyright (c) 2024 Nana Sakisaka
  6. Distributed under the Boost Software License, Version 1.0. (See accompanying
  7. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. =============================================================================*/
  9. #if !defined(BOOST_SPIRIT_HOME_X3_EXTENSIONS_MATCHES_HPP)
  10. #define BOOST_SPIRIT_HOME_X3_EXTENSIONS_MATCHES_HPP
  11. #include <boost/spirit/home/x3/core/parser.hpp>
  12. #include <boost/spirit/home/x3/support/traits/move_to.hpp>
  13. #include <boost/spirit/home/x3/support/expectation.hpp>
  14. #include <boost/spirit/home/x3/support/unused.hpp>
  15. namespace boost { namespace spirit { namespace x3
  16. {
  17. template <typename Subject>
  18. struct matches_directive : unary_parser<Subject, matches_directive<Subject>>
  19. {
  20. using base_type = unary_parser<Subject, matches_directive<Subject>>;
  21. static bool const has_attribute = true;
  22. using attribute_type = bool;
  23. constexpr matches_directive(Subject const& subject) : base_type(subject) {}
  24. template <typename Iterator, typename Context
  25. , typename RContext, typename Attribute>
  26. bool parse(Iterator& first, Iterator const& last
  27. , Context const& context, RContext& rcontext, Attribute& attr) const
  28. {
  29. bool const result = this->subject.parse(
  30. first, last, context, rcontext, unused);
  31. #if !BOOST_SPIRIT_X3_THROW_EXPECTATION_FAILURE
  32. if (has_expectation_failure(context)) return false;
  33. #endif
  34. traits::move_to(result, attr);
  35. return true;
  36. }
  37. };
  38. struct matches_gen
  39. {
  40. template <typename Subject>
  41. constexpr matches_directive<typename extension::as_parser<Subject>::value_type>
  42. operator[](Subject const& subject) const
  43. {
  44. return { as_parser(subject) };
  45. }
  46. };
  47. constexpr auto matches = matches_gen{};
  48. }}}
  49. #endif