kleene.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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_KLEENE_JANUARY_07_2007_0818AM)
  8. #define SPIRIT_KLEENE_JANUARY_07_2007_0818AM
  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. //[composite_parsers_kleene_enable_
  27. template <>
  28. struct use_operator<qi::domain, proto::tag::dereference> // enables *p
  29. : mpl::true_ {};
  30. //]
  31. }}
  32. namespace boost { namespace spirit { namespace qi
  33. {
  34. //[composite_parsers_kleene
  35. template <typename Subject>
  36. struct kleene : unary_parser<kleene<Subject> >
  37. {
  38. typedef Subject subject_type;
  39. template <typename Context, typename Iterator>
  40. struct attribute
  41. {
  42. // Build a std::vector from the subject's attribute. Note
  43. // that build_std_vector may return unused_type if the
  44. // subject's attribute is an unused_type.
  45. typedef typename
  46. traits::build_std_vector<
  47. typename traits::
  48. attribute_of<Subject, Context, Iterator>::type
  49. >::type
  50. type;
  51. };
  52. kleene(Subject const& subject_)
  53. : subject(subject_) {}
  54. template <typename F>
  55. bool parse_container(F f) const
  56. {
  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. // ensure the attribute is actually a container type
  68. traits::make_container(attr_);
  69. typedef detail::fail_function<Iterator, Context, Skipper>
  70. fail_function;
  71. Iterator iter = first;
  72. fail_function f(iter, last, context, skipper);
  73. parse_container(detail::make_pass_container(f, attr_));
  74. first = f.first;
  75. return true;
  76. }
  77. template <typename Context>
  78. info what(Context& context) const
  79. {
  80. return info("kleene", subject.what(context));
  81. }
  82. Subject subject;
  83. };
  84. //]
  85. ///////////////////////////////////////////////////////////////////////////
  86. // Parser generators: make_xxx function (objects)
  87. ///////////////////////////////////////////////////////////////////////////
  88. //[composite_parsers_kleene_generator
  89. template <typename Elements, typename Modifiers>
  90. struct make_composite<proto::tag::dereference, Elements, Modifiers>
  91. : make_unary_composite<Elements, kleene>
  92. {};
  93. //]
  94. // ///////////////////////////////////////////////////////////////////////////
  95. // // Define what attributes are compatible with a kleene
  96. // template <typename Attribute, typename Subject, typename Context, typename Iterator>
  97. // struct is_attribute_compatible<Attribute, kleene<Subject>, Context, Iterator>
  98. // : traits::is_container_compatible<qi::domain, Attribute
  99. // , kleene<Subject>, Context, Iterator>
  100. // {};
  101. }}}
  102. namespace boost { namespace spirit { namespace traits
  103. {
  104. ///////////////////////////////////////////////////////////////////////////
  105. template <typename Subject>
  106. struct has_semantic_action<qi::kleene<Subject> >
  107. : unary_has_semantic_action<Subject> {};
  108. ///////////////////////////////////////////////////////////////////////////
  109. template <typename Subject, typename Attribute, typename Context
  110. , typename Iterator>
  111. struct handles_container<qi::kleene<Subject>, Attribute
  112. , Context, Iterator>
  113. : mpl::true_ {};
  114. }}}
  115. #endif