plus.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Copyright (c) 2001-2011 Hartmut Kaiser
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. =============================================================================*/
  7. #ifndef BOOST_SPIRIT_QI_OPERATOR_PLUS_HPP
  8. #define BOOST_SPIRIT_QI_OPERATOR_PLUS_HPP
  9. #if defined(_MSC_VER)
  10. #pragma once
  11. #endif
  12. #include <boost/spirit/home/qi/meta_compiler.hpp>
  13. #include <boost/spirit/home/qi/parser.hpp>
  14. #include <boost/spirit/home/support/container.hpp>
  15. #include <boost/spirit/home/qi/detail/attributes.hpp>
  16. #include <boost/spirit/home/qi/detail/fail_function.hpp>
  17. #include <boost/spirit/home/qi/detail/pass_container.hpp>
  18. #include <boost/spirit/home/support/has_semantic_action.hpp>
  19. #include <boost/spirit/home/support/handles_container.hpp>
  20. #include <boost/spirit/home/support/info.hpp>
  21. #include <boost/proto/operators.hpp>
  22. #include <boost/proto/tags.hpp>
  23. namespace boost { namespace spirit
  24. {
  25. ///////////////////////////////////////////////////////////////////////////
  26. // Enablers
  27. ///////////////////////////////////////////////////////////////////////////
  28. template <>
  29. struct use_operator<qi::domain, proto::tag::unary_plus> // enables +p
  30. : mpl::true_ {};
  31. }}
  32. namespace boost { namespace spirit { namespace qi
  33. {
  34. template <typename Subject>
  35. struct plus : unary_parser<plus<Subject> >
  36. {
  37. typedef Subject subject_type;
  38. template <typename Context, typename Iterator>
  39. struct attribute
  40. {
  41. // Build a std::vector from the subject's attribute. Note
  42. // that build_std_vector may return unused_type if the
  43. // subject's attribute is an unused_type.
  44. typedef typename
  45. traits::build_std_vector<
  46. typename traits::attribute_of<
  47. Subject, Context, Iterator>::type
  48. >::type
  49. type;
  50. };
  51. plus(Subject const& subject_)
  52. : subject(subject_) {}
  53. template <typename F>
  54. bool parse_container(F f) const
  55. {
  56. // in order to succeed we need to match at least one element
  57. if (f (subject))
  58. return false;
  59. while (!f (subject))
  60. ;
  61. return true;
  62. }
  63. template <typename Iterator, typename Context
  64. , typename Skipper, typename Attribute>
  65. bool parse(Iterator& first, Iterator const& last
  66. , Context& context, Skipper const& skipper
  67. , Attribute& attr_) const
  68. {
  69. typedef detail::fail_function<Iterator, Context, Skipper>
  70. fail_function;
  71. // ensure the attribute is actually a container type
  72. traits::make_container(attr_);
  73. Iterator iter = first;
  74. fail_function f(iter, last, context, skipper);
  75. if (!parse_container(detail::make_pass_container(f, attr_)))
  76. return false;
  77. first = f.first;
  78. return true;
  79. }
  80. template <typename Context>
  81. info what(Context& context) const
  82. {
  83. return info("plus", subject.what(context));
  84. }
  85. Subject subject;
  86. };
  87. ///////////////////////////////////////////////////////////////////////////
  88. // Parser generators: make_xxx function (objects)
  89. ///////////////////////////////////////////////////////////////////////////
  90. template <typename Elements, typename Modifiers>
  91. struct make_composite<proto::tag::unary_plus, Elements, Modifiers>
  92. : make_unary_composite<Elements, plus>
  93. {};
  94. }}}
  95. namespace boost { namespace spirit { namespace traits
  96. {
  97. ///////////////////////////////////////////////////////////////////////////
  98. template <typename Subject>
  99. struct has_semantic_action<qi::plus<Subject> >
  100. : unary_has_semantic_action<Subject> {};
  101. ///////////////////////////////////////////////////////////////////////////
  102. template <typename Subject, typename Attribute, typename Context
  103. , typename Iterator>
  104. struct handles_container<qi::plus<Subject>, Attribute, Context
  105. , Iterator>
  106. : mpl::true_ {};
  107. }}}
  108. #endif