1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
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_SKIP_JANUARY_26_2008_0422PM)
8 #define SPIRIT_SKIP_JANUARY_26_2008_0422PM
14 #include <boost/spirit/home/qi/meta_compiler.hpp>
15 #include <boost/spirit/home/qi/parser.hpp>
16 #include <boost/spirit/home/qi/auxiliary/lazy.hpp>
17 #include <boost/spirit/home/qi/operator/kleene.hpp>
18 #include <boost/spirit/home/qi/directive/lexeme.hpp>
19 #include <boost/spirit/home/qi/skip_over.hpp>
20 #include <boost/spirit/home/qi/detail/unused_skipper.hpp>
21 #include <boost/spirit/home/support/container.hpp>
22 #include <boost/spirit/home/support/common_terminals.hpp>
23 #include <boost/spirit/home/qi/detail/attributes.hpp>
24 #include <boost/spirit/home/support/info.hpp>
25 #include <boost/spirit/home/support/has_semantic_action.hpp>
26 #include <boost/spirit/home/support/handles_container.hpp>
27 #include <boost/fusion/include/at.hpp>
28 #include <boost/fusion/include/vector.hpp>
29 #include <boost/utility/enable_if.hpp>
31 namespace boost { namespace spirit
33 ///////////////////////////////////////////////////////////////////////////
35 ///////////////////////////////////////////////////////////////////////////
37 struct use_directive<qi::domain, tag::skip> // enables skip[p]
41 struct use_directive<qi::domain
42 , terminal_ex<tag::skip // enables skip(s)[p]
43 , fusion::vector1<T> >
44 > : boost::spirit::traits::matches<qi::domain, T> {};
46 template <> // enables *lazy* skip(s)[p]
47 struct use_lazy_directive<
54 namespace boost { namespace spirit { namespace qi
56 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
59 using spirit::skip_type;
61 template <typename Subject>
62 struct reskip_parser : unary_parser<reskip_parser<Subject> >
64 typedef Subject subject_type;
66 template <typename Context, typename Iterator>
70 traits::attribute_of<Subject, Context, Iterator>::type
74 reskip_parser(Subject const& subject_)
75 : subject(subject_) {}
77 template <typename Iterator, typename Context
78 , typename Skipper, typename Attribute>
79 typename enable_if<detail::is_unused_skipper<Skipper>, bool>::type
80 parse(Iterator& first, Iterator const& last
81 , Context& context, Skipper const& u // --> The skipper is reintroduced
82 , Attribute& attr_) const
84 return subject.parse(first, last, context
85 , detail::get_skipper(u), attr_);
87 template <typename Iterator, typename Context
88 , typename Skipper, typename Attribute>
89 typename disable_if<detail::is_unused_skipper<Skipper>, bool>::type
90 parse(Iterator& first, Iterator const& last
91 , Context& context, Skipper const& skipper
92 , Attribute& attr_) const
94 return subject.parse(first, last, context
98 template <typename Context>
99 info what(Context& context) const
101 return info("skip", subject.what(context));
107 template <typename Subject, typename Skipper>
108 struct skip_parser : unary_parser<skip_parser<Subject, Skipper> >
110 typedef Subject subject_type;
111 typedef Skipper skipper_type;
113 template <typename Context, typename Iterator>
117 traits::attribute_of<Subject, Context, Iterator>::type
121 skip_parser(Subject const& subject_, Skipper const& skipper_)
122 : subject(subject_), skipper(skipper_) {}
124 template <typename Iterator, typename Context
125 , typename Skipper_, typename Attribute>
126 bool parse(Iterator& first, Iterator const& last
127 , Context& context, Skipper_ const& //skipper --> bypass the supplied skipper
128 , Attribute& attr_) const
130 return subject.parse(first, last, context, skipper, attr_);
133 template <typename Context>
134 info what(Context& context) const
136 return info("skip", subject.what(context));
143 ///////////////////////////////////////////////////////////////////////////
144 // Parser generators: make_xxx function (objects)
145 ///////////////////////////////////////////////////////////////////////////
146 template <typename Subject, typename Modifiers>
147 struct make_directive<tag::skip, Subject, Modifiers>
149 typedef reskip_parser<Subject> result_type;
150 result_type operator()(unused_type, Subject const& subject, unused_type) const
152 return result_type(subject);
156 template <typename Skipper, typename Subject, typename Modifiers>
157 struct make_directive<
158 terminal_ex<tag::skip, fusion::vector1<Skipper> >, Subject, Modifiers>
161 result_of::compile<qi::domain, Skipper, Modifiers>::type
164 typedef skip_parser<Subject, skipper_type> result_type;
166 template <typename Terminal>
167 result_type operator()(Terminal const& term, Subject const& subject
168 , Modifiers const& modifiers) const
170 return result_type(subject
171 , compile<qi::domain>(fusion::at_c<0>(term.args), modifiers));
177 namespace boost { namespace spirit { namespace traits
179 ///////////////////////////////////////////////////////////////////////////
180 template <typename Subject>
181 struct has_semantic_action<qi::reskip_parser<Subject> >
182 : unary_has_semantic_action<Subject> {};
184 template <typename Subject, typename Skipper>
185 struct has_semantic_action<qi::skip_parser<Subject, Skipper> >
186 : unary_has_semantic_action<Subject> {};
188 ///////////////////////////////////////////////////////////////////////////
189 template <typename Subject, typename Attribute, typename Context
191 struct handles_container<qi::reskip_parser<Subject>, Attribute
193 : unary_handles_container<Subject, Attribute, Context, Iterator> {};
195 template <typename Subject, typename Skipper, typename Attribute
196 , typename Context, typename Iterator>
197 struct handles_container<qi::skip_parser<Subject, Skipper>, Attribute
199 : unary_handles_container<Subject, Attribute, Context, Iterator> {};