/src/contrib/boost/spirit/home/qi/operator/sequence_base.hpp

http://pythonocc.googlecode.com/ · C++ Header · 134 lines · 98 code · 15 blank · 21 comment · 2 complexity · 63f52cd2dcab475476dbbe7e84aaed6d MD5 · raw file

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