skip.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. namespace boost { namespace spirit
  27. {
  28. ///////////////////////////////////////////////////////////////////////////
  29. // Enablers
  30. ///////////////////////////////////////////////////////////////////////////
  31. template <>
  32. struct use_directive<qi::domain, tag::skip> // enables skip[p]
  33. : mpl::true_ {};
  34. template <typename T>
  35. struct use_directive<qi::domain
  36. , terminal_ex<tag::skip // enables skip(s)[p]
  37. , fusion::vector1<T> >
  38. > : boost::spirit::traits::matches<qi::domain, T> {};
  39. template <> // enables *lazy* skip(s)[p]
  40. struct use_lazy_directive<
  41. qi::domain
  42. , tag::skip
  43. , 1 // arity
  44. > : mpl::true_ {};
  45. }}
  46. namespace boost { namespace spirit { namespace qi
  47. {
  48. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  49. using spirit::skip;
  50. #endif
  51. using spirit::skip_type;
  52. template <typename Subject>
  53. struct reskip_parser : unary_parser<reskip_parser<Subject> >
  54. {
  55. typedef Subject subject_type;
  56. template <typename Context, typename Iterator>
  57. struct attribute
  58. {
  59. typedef typename
  60. traits::attribute_of<Subject, Context, Iterator>::type
  61. type;
  62. };
  63. reskip_parser(Subject const& subject_)
  64. : subject(subject_) {}
  65. template <typename Iterator, typename Context
  66. , typename Skipper, typename Attribute>
  67. bool parse(Iterator& first, Iterator const& last
  68. , Context& context, Skipper const& u // --> The skipper is reintroduced
  69. , Attribute& attr_) const
  70. {
  71. return subject.parse(first, last, context
  72. , detail::get_skipper(u), attr_);
  73. }
  74. template <typename Context>
  75. info what(Context& context) const
  76. {
  77. return info("skip", subject.what(context));
  78. }
  79. Subject subject;
  80. };
  81. template <typename Subject, typename Skipper>
  82. struct skip_parser : unary_parser<skip_parser<Subject, Skipper> >
  83. {
  84. typedef Subject subject_type;
  85. typedef Skipper skipper_type;
  86. template <typename Context, typename Iterator>
  87. struct attribute
  88. {
  89. typedef typename
  90. traits::attribute_of<Subject, Context, Iterator>::type
  91. type;
  92. };
  93. skip_parser(Subject const& subject_, Skipper const& skipper_)
  94. : subject(subject_), skipper(skipper_) {}
  95. template <typename Iterator, typename Context
  96. , typename Skipper_, typename Attribute>
  97. bool parse(Iterator& first, Iterator const& last
  98. , Context& context, Skipper_ const& //skipper --> bypass the supplied skipper
  99. , Attribute& attr_) const
  100. {
  101. return subject.parse(first, last, context, skipper, attr_);
  102. }
  103. template <typename Context>
  104. info what(Context& context) const
  105. {
  106. return info("skip", subject.what(context));
  107. }
  108. Subject subject;
  109. Skipper skipper;
  110. };
  111. ///////////////////////////////////////////////////////////////////////////
  112. // Parser generators: make_xxx function (objects)
  113. ///////////////////////////////////////////////////////////////////////////
  114. template <typename Subject, typename Modifiers>
  115. struct make_directive<tag::skip, Subject, Modifiers>
  116. {
  117. typedef reskip_parser<Subject> result_type;
  118. result_type operator()(unused_type, Subject const& subject, unused_type) const
  119. {
  120. return result_type(subject);
  121. }
  122. };
  123. template <typename Skipper, typename Subject, typename Modifiers>
  124. struct make_directive<
  125. terminal_ex<tag::skip, fusion::vector1<Skipper> >, Subject, Modifiers>
  126. {
  127. typedef typename
  128. result_of::compile<qi::domain, Skipper, Modifiers>::type
  129. skipper_type;
  130. typedef skip_parser<Subject, skipper_type> result_type;
  131. template <typename Terminal>
  132. result_type operator()(Terminal const& term, Subject const& subject
  133. , Modifiers const& modifiers) const
  134. {
  135. return result_type(subject
  136. , compile<qi::domain>(fusion::at_c<0>(term.args), modifiers));
  137. }
  138. };
  139. }}}
  140. namespace boost { namespace spirit { namespace traits
  141. {
  142. ///////////////////////////////////////////////////////////////////////////
  143. template <typename Subject>
  144. struct has_semantic_action<qi::reskip_parser<Subject> >
  145. : unary_has_semantic_action<Subject> {};
  146. template <typename Subject, typename Skipper>
  147. struct has_semantic_action<qi::skip_parser<Subject, Skipper> >
  148. : unary_has_semantic_action<Subject> {};
  149. ///////////////////////////////////////////////////////////////////////////
  150. template <typename Subject, typename Attribute, typename Context
  151. , typename Iterator>
  152. struct handles_container<qi::reskip_parser<Subject>, Attribute
  153. , Context, Iterator>
  154. : unary_handles_container<Subject, Attribute, Context, Iterator> {};
  155. template <typename Subject, typename Skipper, typename Attribute
  156. , typename Context, typename Iterator>
  157. struct handles_container<qi::skip_parser<Subject, Skipper>, Attribute
  158. , Context, Iterator>
  159. : unary_handles_container<Subject, Attribute, Context, Iterator> {};
  160. }}}
  161. #endif