skip.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 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_SKIP_JANUARY_26_2008_0422PM)
  7. #define SPIRIT_SKIP_JANUARY_26_2008_0422PM
  8. #if defined(_MSC_VER)
  9. #pragma once
  10. #endif
  11. #include <boost/spirit/home/qi/meta_compiler.hpp>
  12. #include <boost/spirit/home/qi/parser.hpp>
  13. #include <boost/spirit/home/qi/auxiliary/lazy.hpp>
  14. #include <boost/spirit/home/qi/operator/kleene.hpp>
  15. #include <boost/spirit/home/qi/directive/lexeme.hpp>
  16. #include <boost/spirit/home/qi/skip_over.hpp>
  17. #include <boost/spirit/home/qi/detail/unused_skipper.hpp>
  18. #include <boost/spirit/home/support/container.hpp>
  19. #include <boost/spirit/home/support/common_terminals.hpp>
  20. #include <boost/spirit/home/qi/detail/attributes.hpp>
  21. #include <boost/spirit/home/support/info.hpp>
  22. #include <boost/spirit/home/support/has_semantic_action.hpp>
  23. #include <boost/spirit/home/support/handles_container.hpp>
  24. #include <boost/fusion/include/at.hpp>
  25. #include <boost/fusion/include/vector.hpp>
  26. #include <boost/utility/enable_if.hpp>
  27. namespace boost { namespace spirit
  28. {
  29. ///////////////////////////////////////////////////////////////////////////
  30. // Enablers
  31. ///////////////////////////////////////////////////////////////////////////
  32. template <>
  33. struct use_directive<qi::domain, tag::skip> // enables skip[p]
  34. : mpl::true_ {};
  35. template <typename T>
  36. struct use_directive<qi::domain
  37. , terminal_ex<tag::skip // enables skip(s)[p]
  38. , fusion::vector1<T> >
  39. > : boost::spirit::traits::matches<qi::domain, T> {};
  40. template <> // enables *lazy* skip(s)[p]
  41. struct use_lazy_directive<
  42. qi::domain
  43. , tag::skip
  44. , 1 // arity
  45. > : mpl::true_ {};
  46. }}
  47. namespace boost { namespace spirit { namespace qi
  48. {
  49. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  50. using spirit::skip;
  51. #endif
  52. using spirit::skip_type;
  53. template <typename Subject>
  54. struct reskip_parser : unary_parser<reskip_parser<Subject> >
  55. {
  56. typedef Subject subject_type;
  57. template <typename Context, typename Iterator>
  58. struct attribute
  59. {
  60. typedef typename
  61. traits::attribute_of<Subject, Context, Iterator>::type
  62. type;
  63. };
  64. reskip_parser(Subject const& subject_)
  65. : subject(subject_) {}
  66. template <typename Iterator, typename Context
  67. , typename Skipper, typename Attribute>
  68. typename enable_if<detail::is_unused_skipper<Skipper>, bool>::type
  69. parse(Iterator& first, Iterator const& last
  70. , Context& context, Skipper const& u // --> The skipper is reintroduced
  71. , Attribute& attr_) const
  72. {
  73. return subject.parse(first, last, context
  74. , detail::get_skipper(u), attr_);
  75. }
  76. template <typename Iterator, typename Context
  77. , typename Skipper, typename Attribute>
  78. typename disable_if<detail::is_unused_skipper<Skipper>, bool>::type
  79. parse(Iterator& first, Iterator const& last
  80. , Context& context, Skipper const& skipper
  81. , Attribute& attr_) const
  82. {
  83. return subject.parse(first, last, context
  84. , skipper, attr_);
  85. }
  86. template <typename Context>
  87. info what(Context& context) const
  88. {
  89. return info("skip", subject.what(context));
  90. }
  91. Subject subject;
  92. };
  93. template <typename Subject, typename Skipper>
  94. struct skip_parser : unary_parser<skip_parser<Subject, Skipper> >
  95. {
  96. typedef Subject subject_type;
  97. typedef Skipper skipper_type;
  98. template <typename Context, typename Iterator>
  99. struct attribute
  100. {
  101. typedef typename
  102. traits::attribute_of<Subject, Context, Iterator>::type
  103. type;
  104. };
  105. skip_parser(Subject const& subject_, Skipper const& skipper_)
  106. : subject(subject_), skipper(skipper_) {}
  107. template <typename Iterator, typename Context
  108. , typename Skipper_, typename Attribute>
  109. bool parse(Iterator& first, Iterator const& last
  110. , Context& context, Skipper_ const& //skipper --> bypass the supplied skipper
  111. , Attribute& attr_) const
  112. {
  113. return subject.parse(first, last, context, skipper, attr_);
  114. }
  115. template <typename Context>
  116. info what(Context& context) const
  117. {
  118. return info("skip", subject.what(context));
  119. }
  120. Subject subject;
  121. Skipper skipper;
  122. };
  123. ///////////////////////////////////////////////////////////////////////////
  124. // Parser generators: make_xxx function (objects)
  125. ///////////////////////////////////////////////////////////////////////////
  126. template <typename Subject, typename Modifiers>
  127. struct make_directive<tag::skip, Subject, Modifiers>
  128. {
  129. typedef reskip_parser<Subject> result_type;
  130. result_type operator()(unused_type, Subject const& subject, unused_type) const
  131. {
  132. return result_type(subject);
  133. }
  134. };
  135. template <typename Skipper, typename Subject, typename Modifiers>
  136. struct make_directive<
  137. terminal_ex<tag::skip, fusion::vector1<Skipper> >, Subject, Modifiers>
  138. {
  139. typedef typename
  140. result_of::compile<qi::domain, Skipper, Modifiers>::type
  141. skipper_type;
  142. typedef skip_parser<Subject, skipper_type> result_type;
  143. template <typename Terminal>
  144. result_type operator()(Terminal const& term, Subject const& subject
  145. , Modifiers const& modifiers) const
  146. {
  147. return result_type(subject
  148. , compile<qi::domain>(fusion::at_c<0>(term.args), modifiers));
  149. }
  150. };
  151. }}}
  152. namespace boost { namespace spirit { namespace traits
  153. {
  154. ///////////////////////////////////////////////////////////////////////////
  155. template <typename Subject>
  156. struct has_semantic_action<qi::reskip_parser<Subject> >
  157. : unary_has_semantic_action<Subject> {};
  158. template <typename Subject, typename Skipper>
  159. struct has_semantic_action<qi::skip_parser<Subject, Skipper> >
  160. : unary_has_semantic_action<Subject> {};
  161. ///////////////////////////////////////////////////////////////////////////
  162. template <typename Subject, typename Attribute, typename Context
  163. , typename Iterator>
  164. struct handles_container<qi::reskip_parser<Subject>, Attribute
  165. , Context, Iterator>
  166. : unary_handles_container<Subject, Attribute, Context, Iterator> {};
  167. template <typename Subject, typename Skipper, typename Attribute
  168. , typename Context, typename Iterator>
  169. struct handles_container<qi::skip_parser<Subject, Skipper>, Attribute
  170. , Context, Iterator>
  171. : unary_handles_container<Subject, Attribute, Context, Iterator> {};
  172. }}}
  173. #endif