1 /*=============================================================================
2     Copyright (c) 2001-2011 Joel de Guzman
3 
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_DIRECTIVE_SKIP_HPP
8 #define BOOST_SPIRIT_QI_DIRECTIVE_SKIP_HPP
9 
10 #if defined(_MSC_VER)
11 #pragma once
12 #endif
13 
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>
30 
31 namespace boost { namespace spirit
32 {
33     ///////////////////////////////////////////////////////////////////////////
34     // Enablers
35     ///////////////////////////////////////////////////////////////////////////
36     template <>
37     struct use_directive<qi::domain, tag::skip>     // enables skip[p]
38       : mpl::true_ {};
39 
40     template <typename T>
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> {};
45 
46     template <>                                     // enables *lazy* skip(s)[p]
47     struct use_lazy_directive<
48         qi::domain
49       , tag::skip
50       , 1 // arity
51     > : mpl::true_ {};
52 }}
53 
54 namespace boost { namespace spirit { namespace qi
55 {
56 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
57     using spirit::skip;
58 #endif
59     using spirit::skip_type;
60 
61     template <typename Subject>
62     struct reskip_parser : unary_parser<reskip_parser<Subject> >
63     {
64         typedef Subject subject_type;
65 
66         template <typename Context, typename Iterator>
67         struct attribute
68         {
69             typedef typename
70                 traits::attribute_of<Subject, Context, Iterator>::type
71             type;
72         };
73 
reskip_parserboost::spirit::qi::reskip_parser74         reskip_parser(Subject const& subject_)
75           : subject(subject_) {}
76 
77         template <typename Iterator, typename Context
78           , typename Skipper, typename Attribute>
79         typename enable_if<detail::is_unused_skipper<Skipper>, bool>::type
parseboost::spirit::qi::reskip_parser80         parse(Iterator& first, Iterator const& last
81           , Context& context, Skipper const& u // --> The skipper is reintroduced
82           , Attribute& attr_) const
83         {
84             return subject.parse(first, last, context
85               , detail::get_skipper(u), attr_);
86         }
87         template <typename Iterator, typename Context
88           , typename Skipper, typename Attribute>
89         typename disable_if<detail::is_unused_skipper<Skipper>, bool>::type
parseboost::spirit::qi::reskip_parser90         parse(Iterator& first, Iterator const& last
91           , Context& context, Skipper const& skipper
92           , Attribute& attr_) const
93         {
94             return subject.parse(first, last, context
95               , skipper, attr_);
96         }
97 
98         template <typename Context>
whatboost::spirit::qi::reskip_parser99         info what(Context& context) const
100         {
101             return info("skip", subject.what(context));
102         }
103 
104         Subject subject;
105     };
106 
107     template <typename Subject, typename Skipper>
108     struct skip_parser : unary_parser<skip_parser<Subject, Skipper> >
109     {
110         typedef Subject subject_type;
111         typedef Skipper skipper_type;
112 
113         template <typename Context, typename Iterator>
114         struct attribute
115         {
116             typedef typename
117                 traits::attribute_of<Subject, Context, Iterator>::type
118             type;
119         };
120 
skip_parserboost::spirit::qi::skip_parser121         skip_parser(Subject const& subject_, Skipper const& skipper_)
122           : subject(subject_), skipper(skipper_) {}
123 
124         template <typename Iterator, typename Context
125           , typename Skipper_, typename Attribute>
parseboost::spirit::qi::skip_parser126         bool parse(Iterator& first, Iterator const& last
127           , Context& context, Skipper_ const& //skipper --> bypass the supplied skipper
128           , Attribute& attr_) const
129         {
130             return subject.parse(first, last, context, skipper, attr_);
131         }
132 
133         template <typename Context>
whatboost::spirit::qi::skip_parser134         info what(Context& context) const
135         {
136             return info("skip", subject.what(context));
137         }
138 
139         Subject subject;
140         Skipper skipper;
141     };
142 
143     ///////////////////////////////////////////////////////////////////////////
144     // Parser generators: make_xxx function (objects)
145     ///////////////////////////////////////////////////////////////////////////
146     template <typename Subject, typename Modifiers>
147     struct make_directive<tag::skip, Subject, Modifiers>
148     {
149         typedef reskip_parser<Subject> result_type;
operator ()boost::spirit::qi::make_directive150         result_type operator()(unused_type, Subject const& subject, unused_type) const
151         {
152             return result_type(subject);
153         }
154     };
155 
156     template <typename Skipper, typename Subject, typename Modifiers>
157     struct make_directive<
158         terminal_ex<tag::skip, fusion::vector1<Skipper> >, Subject, Modifiers>
159     {
160         typedef typename
161             result_of::compile<qi::domain, Skipper, Modifiers>::type
162         skipper_type;
163 
164         typedef skip_parser<Subject, skipper_type> result_type;
165 
166         template <typename Terminal>
operator ()boost::spirit::qi::make_directive167         result_type operator()(Terminal const& term, Subject const& subject
168           , Modifiers const& modifiers) const
169         {
170             return result_type(subject
171               , compile<qi::domain>(fusion::at_c<0>(term.args), modifiers));
172         }
173     };
174 
175 }}}
176 
177 namespace boost { namespace spirit { namespace traits
178 {
179     ///////////////////////////////////////////////////////////////////////////
180     template <typename Subject>
181     struct has_semantic_action<qi::reskip_parser<Subject> >
182       : unary_has_semantic_action<Subject> {};
183 
184     template <typename Subject, typename Skipper>
185     struct has_semantic_action<qi::skip_parser<Subject, Skipper> >
186       : unary_has_semantic_action<Subject> {};
187 
188     ///////////////////////////////////////////////////////////////////////////
189     template <typename Subject, typename Attribute, typename Context
190         , typename Iterator>
191     struct handles_container<qi::reskip_parser<Subject>, Attribute
192         , Context, Iterator>
193       : unary_handles_container<Subject, Attribute, Context, Iterator> {};
194 
195     template <typename Subject, typename Skipper, typename Attribute
196         , typename Context, typename Iterator>
197     struct handles_container<qi::skip_parser<Subject, Skipper>, Attribute
198         , Context, Iterator>
199       : unary_handles_container<Subject, Attribute, Context, Iterator> {};
200 }}}
201 
202 #endif
203