sequence_base.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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_SEQUENCE_BASE_HPP
  8. #define BOOST_SPIRIT_QI_OPERATOR_SEQUENCE_BASE_HPP
  9. #if defined(_MSC_VER)
  10. #pragma once
  11. #endif
  12. #include <boost/spirit/home/qi/domain.hpp>
  13. #include <boost/spirit/home/qi/detail/pass_container.hpp>
  14. #include <boost/spirit/home/qi/detail/attributes.hpp>
  15. #include <boost/spirit/home/support/algorithm/any_if.hpp>
  16. #include <boost/spirit/home/support/detail/what_function.hpp>
  17. #include <boost/spirit/home/support/unused.hpp>
  18. #include <boost/spirit/home/support/info.hpp>
  19. #include <boost/spirit/home/support/sequence_base_id.hpp>
  20. #include <boost/spirit/home/support/has_semantic_action.hpp>
  21. #include <boost/spirit/home/qi/parser.hpp>
  22. #include <boost/fusion/include/as_vector.hpp>
  23. #include <boost/fusion/include/vector.hpp>
  24. #include <boost/fusion/include/for_each.hpp>
  25. #include <boost/mpl/identity.hpp>
  26. namespace boost { namespace spirit { namespace qi
  27. {
  28. template <typename Derived, typename Elements>
  29. struct sequence_base // this class is shared by sequence and expect
  30. : nary_parser<Derived>
  31. {
  32. typedef Elements elements_type;
  33. struct sequence_base_id;
  34. template <typename Context, typename Iterator>
  35. struct attribute
  36. {
  37. // Put all the element attributes in a tuple
  38. typedef typename traits::build_attribute_sequence<
  39. Elements, Context, traits::sequence_attribute_transform
  40. , Iterator, qi::domain
  41. >::type all_attributes;
  42. // Now, build a fusion vector over the attributes. Note
  43. // that build_fusion_vector 1) removes all unused attributes
  44. // and 2) may return unused_type if all elements have
  45. // unused_type(s).
  46. typedef typename
  47. traits::build_fusion_vector<all_attributes>::type
  48. type_;
  49. // Finally, strip single element vectors into its
  50. // naked form: vector1<T> --> T
  51. typedef typename
  52. traits::strip_single_element_vector<type_>::type
  53. type;
  54. };
  55. sequence_base(Elements const& elements_)
  56. : elements(elements_) {}
  57. // standard case. Attribute is a fusion tuple
  58. template <typename Iterator, typename Context
  59. , typename Skipper, typename Attribute>
  60. bool parse_impl(Iterator& first, Iterator const& last
  61. , Context& context, Skipper const& skipper
  62. , Attribute& attr_, mpl::false_) const
  63. {
  64. Iterator iter = first;
  65. typedef traits::attribute_not_unused<Context, Iterator> predicate;
  66. // wrap the attribute in a tuple if it is not a tuple or if the
  67. // attribute of this sequence is a single element tuple
  68. typedef typename attribute<Context, Iterator>::type_ attr_type_;
  69. typename traits::wrap_if_not_tuple<Attribute
  70. , typename mpl::and_<
  71. traits::one_element_sequence<attr_type_>
  72. , mpl::not_<traits::one_element_sequence<Attribute> >
  73. >::type
  74. >::type attr_local(attr_);
  75. // return false if *any* of the parsers fail
  76. if (spirit::any_if(elements, attr_local
  77. , Derived::fail_function(iter, last, context, skipper), predicate()))
  78. return false;
  79. first = iter;
  80. return true;
  81. }
  82. // Special case when Attribute is an stl container
  83. template <typename Iterator, typename Context
  84. , typename Skipper, typename Attribute>
  85. bool parse_impl(Iterator& first, Iterator const& last
  86. , Context& context, Skipper const& skipper
  87. , Attribute& attr_, mpl::true_) const
  88. {
  89. // ensure the attribute is actually a container type
  90. traits::make_container(attr_);
  91. Iterator iter = first;
  92. // return false if *any* of the parsers fail
  93. if (fusion::any(elements
  94. , detail::make_sequence_pass_container(
  95. Derived::fail_function(iter, last, context, skipper), attr_))
  96. )
  97. return false;
  98. first = iter;
  99. return true;
  100. }
  101. // main parse function. Dispatches to parse_impl depending
  102. // on the Attribute type.
  103. template <typename Iterator, typename Context
  104. , typename Skipper, typename Attribute>
  105. bool parse(Iterator& first, Iterator const& last
  106. , Context& context, Skipper const& skipper
  107. , Attribute& attr_) const
  108. {
  109. return parse_impl(first, last, context, skipper, attr_
  110. , traits::is_container<Attribute>());
  111. }
  112. template <typename Context>
  113. info what(Context& context) const
  114. {
  115. info result(this->derived().id());
  116. fusion::for_each(elements,
  117. spirit::detail::what_function<Context>(result, context));
  118. return result;
  119. }
  120. Elements elements;
  121. };
  122. }}}
  123. #endif