matches.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #ifndef BOOST_SPIRIT_QI_DIRECTIVE_MATCHES_HPP
  7. #define BOOST_SPIRIT_QI_DIRECTIVE_MATCHES_HPP
  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. };
  64. ///////////////////////////////////////////////////////////////////////////
  65. // Parser generators: make_xxx function (objects)
  66. ///////////////////////////////////////////////////////////////////////////
  67. template <typename Subject, typename Modifiers>
  68. struct make_directive<tag::matches, Subject, Modifiers>
  69. {
  70. typedef matches_directive<Subject> result_type;
  71. result_type operator()(unused_type, Subject const& subject, unused_type) const
  72. {
  73. return result_type(subject);
  74. }
  75. };
  76. }}}
  77. namespace boost { namespace spirit { namespace traits
  78. {
  79. ///////////////////////////////////////////////////////////////////////////
  80. template <typename Subject>
  81. struct has_semantic_action<qi::matches_directive<Subject> >
  82. : unary_has_semantic_action<Subject> {};
  83. ///////////////////////////////////////////////////////////////////////////
  84. template <typename Subject, typename Attribute, typename Context
  85. , typename Iterator>
  86. struct handles_container<qi::matches_directive<Subject>, Attribute
  87. , Context, Iterator>
  88. : unary_handles_container<Subject, Attribute, Context, Iterator> {};
  89. }}}
  90. #endif