hold.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 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(SPIRIT_HOLD_FEBRUARY_6_2010_0917AM)
  7. #define SPIRIT_HOLD_FEBRUARY_6_2010_0917AM
  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/skip_over.hpp>
  13. #include <boost/spirit/home/qi/parser.hpp>
  14. #include <boost/spirit/home/support/attributes.hpp>
  15. #include <boost/spirit/home/support/info.hpp>
  16. #include <boost/spirit/home/support/common_terminals.hpp>
  17. #include <boost/spirit/home/support/unused.hpp>
  18. #include <boost/spirit/home/support/has_semantic_action.hpp>
  19. #include <boost/spirit/home/support/handles_container.hpp>
  20. namespace boost { namespace spirit
  21. {
  22. ///////////////////////////////////////////////////////////////////////////
  23. // Enablers
  24. ///////////////////////////////////////////////////////////////////////////
  25. template <>
  26. struct use_directive<qi::domain, tag::hold> // enables hold
  27. : mpl::true_ {};
  28. }}
  29. namespace boost { namespace spirit { namespace qi
  30. {
  31. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  32. using spirit::hold;
  33. #endif
  34. using spirit::hold_type;
  35. template <typename Subject>
  36. struct hold_directive : unary_parser<hold_directive<Subject> >
  37. {
  38. typedef Subject subject_type;
  39. hold_directive(Subject const& subject_)
  40. : subject(subject_) {}
  41. template <typename Context, typename Iterator>
  42. struct attribute
  43. {
  44. typedef typename
  45. traits::attribute_of<subject_type, Context, Iterator>::type
  46. 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. Attribute copy(attr_);
  54. if (subject.parse(first, last, context, skipper, copy))
  55. {
  56. traits::swap_impl(copy, attr_);
  57. return true;
  58. }
  59. return false;
  60. }
  61. template <typename Context>
  62. info what(Context& context) const
  63. {
  64. return info("hold", subject.what(context));
  65. }
  66. Subject subject;
  67. };
  68. ///////////////////////////////////////////////////////////////////////////
  69. // Parser generators: make_xxx function (objects)
  70. ///////////////////////////////////////////////////////////////////////////
  71. template <typename Subject, typename Modifiers>
  72. struct make_directive<tag::hold, Subject, Modifiers>
  73. {
  74. typedef hold_directive<Subject> result_type;
  75. result_type operator()(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. ///////////////////////////////////////////////////////////////////////////
  84. template <typename Subject>
  85. struct has_semantic_action<qi::hold_directive<Subject> >
  86. : unary_has_semantic_action<Subject> {};
  87. ///////////////////////////////////////////////////////////////////////////
  88. template <typename Subject, typename Attribute, typename Context
  89. , typename Iterator>
  90. struct handles_container<qi::hold_directive<Subject>, Attribute
  91. , Context, Iterator>
  92. : unary_handles_container<Subject, Attribute, Context, Iterator> {};
  93. }}}
  94. #endif