plus.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #if !defined(SPIRIT_PLUS_MARCH_13_2007_0127PM)
  8. #define SPIRIT_PLUS_MARCH_13_2007_0127PM
  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. namespace boost { namespace spirit
  22. {
  23. ///////////////////////////////////////////////////////////////////////////
  24. // Enablers
  25. ///////////////////////////////////////////////////////////////////////////
  26. template <>
  27. struct use_operator<qi::domain, proto::tag::unary_plus> // enables +p
  28. : mpl::true_ {};
  29. }}
  30. namespace boost { namespace spirit { namespace qi
  31. {
  32. template <typename Subject>
  33. struct plus : unary_parser<plus<Subject> >
  34. {
  35. typedef Subject subject_type;
  36. template <typename Context, typename Iterator>
  37. struct attribute
  38. {
  39. // Build a std::vector from the subject's attribute. Note
  40. // that build_std_vector may return unused_type if the
  41. // subject's attribute is an unused_type.
  42. typedef typename
  43. traits::build_std_vector<
  44. typename traits::attribute_of<
  45. Subject, Context, Iterator>::type
  46. >::type
  47. type;
  48. };
  49. plus(Subject const& subject_)
  50. : subject(subject_) {}
  51. template <typename F>
  52. bool parse_container(F f) const
  53. {
  54. // in order to succeed we need to match at least one element
  55. if (f (subject))
  56. return false;
  57. while (!f (subject))
  58. ;
  59. return true;
  60. }
  61. template <typename Iterator, typename Context
  62. , typename Skipper, typename Attribute>
  63. bool parse(Iterator& first, Iterator const& last
  64. , Context& context, Skipper const& skipper
  65. , Attribute& attr_) const
  66. {
  67. typedef detail::fail_function<Iterator, Context, Skipper>
  68. fail_function;
  69. // ensure the attribute is actually a container type
  70. traits::make_container(attr_);
  71. Iterator iter = first;
  72. fail_function f(iter, last, context, skipper);
  73. if (!parse_container(detail::make_pass_container(f, attr_)))
  74. return false;
  75. first = f.first;
  76. return true;
  77. }
  78. template <typename Context>
  79. info what(Context& context) const
  80. {
  81. return info("plus", subject.what(context));
  82. }
  83. Subject subject;
  84. };
  85. ///////////////////////////////////////////////////////////////////////////
  86. // Parser generators: make_xxx function (objects)
  87. ///////////////////////////////////////////////////////////////////////////
  88. template <typename Elements, typename Modifiers>
  89. struct make_composite<proto::tag::unary_plus, Elements, Modifiers>
  90. : make_unary_composite<Elements, plus>
  91. {};
  92. }}}
  93. namespace boost { namespace spirit { namespace traits
  94. {
  95. ///////////////////////////////////////////////////////////////////////////
  96. template <typename Subject>
  97. struct has_semantic_action<qi::plus<Subject> >
  98. : unary_has_semantic_action<Subject> {};
  99. ///////////////////////////////////////////////////////////////////////////
  100. template <typename Subject, typename Attribute, typename Context
  101. , typename Iterator>
  102. struct handles_container<qi::plus<Subject>, Attribute, Context
  103. , Iterator>
  104. : mpl::true_ {};
  105. }}}
  106. #endif