matches.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Hartmut Kaiser
  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_MATCHES_JAN_07_2010_0745PM)
  7. #define SPIRIT_MATCHES_JAN_07_2010_0745PM
  8. #if defined(_MSC_VER)
  9. #pragma once
  10. #endif
  11. #include <boost/spirit/home/qi/meta_compiler.hpp>
  12. #include <boost/spirit/home/qi/parser.hpp>
  13. #include <boost/spirit/home/qi/detail/assign_to.hpp>
  14. #include <boost/spirit/home/support/unused.hpp>
  15. #include <boost/spirit/home/support/info.hpp>
  16. #include <boost/spirit/home/support/common_terminals.hpp>
  17. #include <boost/spirit/home/support/has_semantic_action.hpp>
  18. #include <boost/spirit/home/support/handles_container.hpp>
  19. namespace boost { namespace spirit
  20. {
  21. ///////////////////////////////////////////////////////////////////////////
  22. // Enablers
  23. ///////////////////////////////////////////////////////////////////////////
  24. template <>
  25. struct use_directive<qi::domain, tag::matches> // enables matches
  26. : mpl::true_ {};
  27. }}
  28. namespace boost { namespace spirit { namespace qi
  29. {
  30. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  31. using spirit::matches;
  32. #endif
  33. using spirit::matches_type;
  34. ///////////////////////////////////////////////////////////////////////////
  35. // matches_directive returns whether the embedded parser matched
  36. ///////////////////////////////////////////////////////////////////////////
  37. template <typename Subject>
  38. struct matches_directive : unary_parser<matches_directive<Subject> >
  39. {
  40. typedef Subject subject_type;
  41. matches_directive(Subject const& subject_)
  42. : subject(subject_) {}
  43. template <typename Context, typename Iterator>
  44. struct attribute
  45. {
  46. typedef bool type;
  47. };
  48. template <typename Iterator, typename Context
  49. , typename Skipper, typename Attribute>
  50. bool parse(Iterator& first, Iterator const& last
  51. , Context& context, Skipper const& skipper, Attribute& attr_) const
  52. {
  53. bool result = subject.parse(first, last, context, skipper, unused);
  54. spirit::traits::assign_to(result, attr_);
  55. return true;
  56. }
  57. template <typename Context>
  58. info what(Context& context) const
  59. {
  60. return info("matches", subject.what(context));
  61. }
  62. Subject subject;
  63. private:
  64. // silence MSVC warning C4512: assignment operator could not be generated
  65. matches_directive& operator= (matches_directive const&);
  66. };
  67. ///////////////////////////////////////////////////////////////////////////
  68. // Parser generators: make_xxx function (objects)
  69. ///////////////////////////////////////////////////////////////////////////
  70. template <typename Subject, typename Modifiers>
  71. struct make_directive<tag::matches, Subject, Modifiers>
  72. {
  73. typedef matches_directive<Subject> result_type;
  74. result_type operator()(unused_type, Subject const& subject, unused_type) const
  75. {
  76. return result_type(subject);
  77. }
  78. };
  79. }}}
  80. namespace boost { namespace spirit { namespace traits
  81. {
  82. ///////////////////////////////////////////////////////////////////////////
  83. template <typename Subject>
  84. struct has_semantic_action<qi::matches_directive<Subject> >
  85. : unary_has_semantic_action<Subject> {};
  86. ///////////////////////////////////////////////////////////////////////////
  87. template <typename Subject, typename Attribute, typename Context
  88. , typename Iterator>
  89. struct handles_container<qi::matches_directive<Subject>, Attribute
  90. , Context, Iterator>
  91. : unary_handles_container<Subject, Attribute, Context, Iterator> {};
  92. }}}
  93. #endif