expect.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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_EXPECT_MARCH_16_2012_1024PM)
  7. #define BOOST_SPIRIT_X3_EXPECT_MARCH_16_2012_1024PM
  8. #include <boost/spirit/home/x3/support/context.hpp>
  9. #include <boost/spirit/home/x3/core/parser.hpp>
  10. #include <boost/spirit/home/x3/core/detail/parse_into_container.hpp>
  11. #include <boost/config.hpp> // for BOOST_SYMBOL_VISIBLE
  12. #include <boost/throw_exception.hpp>
  13. #include <stdexcept>
  14. namespace boost { namespace spirit { namespace x3
  15. {
  16. template <typename Iterator>
  17. struct BOOST_SYMBOL_VISIBLE expectation_failure : std::runtime_error
  18. {
  19. public:
  20. expectation_failure(Iterator where, std::string const& which)
  21. : std::runtime_error("boost::spirit::x3::expectation_failure")
  22. , where_(where), which_(which)
  23. {}
  24. ~expectation_failure() {}
  25. std::string which() const { return which_; }
  26. Iterator const& where() const { return where_; }
  27. private:
  28. Iterator where_;
  29. std::string which_;
  30. };
  31. template <typename Subject>
  32. struct expect_directive : unary_parser<Subject, expect_directive<Subject>>
  33. {
  34. typedef unary_parser<Subject, expect_directive<Subject> > base_type;
  35. static bool const is_pass_through_unary = true;
  36. expect_directive(Subject const& subject)
  37. : base_type(subject) {}
  38. template <typename Iterator, typename Context
  39. , typename RContext, typename Attribute>
  40. bool parse(Iterator& first, Iterator const& last
  41. , Context const& context, RContext& rcontext, Attribute& attr) const
  42. {
  43. bool r = this->subject.parse(first, last, context, rcontext, attr);
  44. if (!r)
  45. {
  46. boost::throw_exception(
  47. expectation_failure<Iterator>(
  48. first, what(this->subject)));
  49. }
  50. return r;
  51. }
  52. };
  53. struct expect_gen
  54. {
  55. template <typename Subject>
  56. expect_directive<typename extension::as_parser<Subject>::value_type>
  57. operator[](Subject const& subject) const
  58. {
  59. return { as_parser(subject) };
  60. }
  61. };
  62. auto const expect = expect_gen{};
  63. }}}
  64. namespace boost { namespace spirit { namespace x3 { namespace detail
  65. {
  66. // Special case handling for expect expressions.
  67. template <typename Subject, typename Context, typename RContext>
  68. struct parse_into_container_impl<expect_directive<Subject>, Context, RContext>
  69. {
  70. template <typename Iterator, typename Attribute>
  71. static bool call(
  72. expect_directive<Subject> const& parser
  73. , Iterator& first, Iterator const& last
  74. , Context const& context, RContext& rcontext, Attribute& attr)
  75. {
  76. bool r = parse_into_container(
  77. parser.subject, first, last, context, rcontext, attr);
  78. if (!r)
  79. {
  80. boost::throw_exception(
  81. expectation_failure<Iterator>(
  82. first, what(parser.subject)));
  83. }
  84. return r;
  85. }
  86. };
  87. }}}}
  88. #endif