expect.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*=============================================================================
  2. Copyright (c) 2016 Frank Hein, maxence business consulting gmbh
  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 (SPIRIT_EXPECT_JULY_13_2016)
  7. #define SPIRIT_EXPECT_JULY_13_2016
  8. #if defined(_MSC_VER)
  9. #pragma once
  10. #endif
  11. #include <boost/spirit/home/qi/meta_compiler.hpp>
  12. #include <boost/spirit/home/support/has_semantic_action.hpp>
  13. #include <boost/spirit/home/qi/detail/attributes.hpp>
  14. #include <boost/spirit/home/qi/detail/expectation_failure.hpp>
  15. #include <boost/spirit/home/support/common_terminals.hpp>
  16. #include <boost/spirit/home/support/handles_container.hpp>
  17. #include <boost/spirit/home/support/unused.hpp>
  18. #include <boost/spirit/home/support/info.hpp>
  19. namespace boost { namespace spirit {
  20. ///////////////////////////////////////////////////////////////////////////
  21. // Enablers
  22. ///////////////////////////////////////////////////////////////////////////
  23. template <>
  24. struct use_directive<qi::domain, tag::expect> // enables expect[p]
  25. : mpl::true_ {};
  26. }}
  27. namespace boost { namespace spirit { namespace qi {
  28. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  29. using spirit::expect;
  30. #endif
  31. using spirit::expect_type;
  32. template <typename Subject>
  33. struct expect_directive : unary_parser<expect_directive<Subject> >
  34. {
  35. typedef result_of::compile<domain, Subject> subject_type;
  36. template <typename Context, typename Iterator>
  37. struct attribute
  38. {
  39. typedef traits::attribute_of<subject_type, Context, Iterator>
  40. type;
  41. };
  42. expect_directive(Subject const& subject_) : subject(subject_) {}
  43. template <typename Iterator, typename Context
  44. , typename Skipper, typename Attribute>
  45. bool parse(Iterator& first, Iterator const& last
  46. , Context& context, Skipper const& skipper
  47. , Attribute& attr_) const
  48. {
  49. typedef expectation_failure<Iterator> exception;
  50. if (!subject.parse(first, last, context, skipper, attr_))
  51. {
  52. boost::throw_exception(
  53. exception(first, last, subject.what(context)));
  54. #if defined(BOOST_NO_EXCEPTIONS)
  55. return false; // for systems not supporting exceptions
  56. #endif
  57. }
  58. return true;
  59. }
  60. template <typename Context>
  61. info what(Context& context) const
  62. {
  63. return info("expect", subject.what(context));
  64. }
  65. Subject subject;
  66. };
  67. ///////////////////////////////////////////////////////////////////////////
  68. // Parser generators: make_xxx function (objects)
  69. ///////////////////////////////////////////////////////////////////////////
  70. template <typename Subject, typename Modifiers>
  71. struct make_directive<tag::expect, Subject, Modifiers>
  72. {
  73. typedef expect_directive<Subject> result_type;
  74. result_type operator()
  75. (unused_type, Subject const& subject, unused_type) const
  76. {
  77. return result_type(subject);
  78. }
  79. };
  80. }}}
  81. namespace boost { namespace spirit { namespace traits {
  82. ///////////////////////////////////////////////////////////////////////////
  83. template <typename Subject>
  84. struct has_semantic_action<qi::expect_directive<Subject> >
  85. : unary_has_semantic_action<Subject> {};
  86. ///////////////////////////////////////////////////////////////////////////
  87. template <typename Subject, typename Attribute
  88. , typename Context, typename Iterator>
  89. struct handles_container<
  90. qi::expect_directive<Subject>, Attribute, Context, Iterator
  91. >
  92. : unary_handles_container<Subject, Attribute, Context, Iterator> {};
  93. }}}
  94. #endif