]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/spirit/home/lex/qi/plain_token.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / spirit / home / lex / qi / plain_token.hpp
1 // Copyright (c) 2001-2011 Hartmut Kaiser
2 //
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(BOOST_SPIRIT_LEX_PLAIN_TOKEN_NOV_11_2007_0451PM)
7 #define BOOST_SPIRIT_LEX_PLAIN_TOKEN_NOV_11_2007_0451PM
8
9 #if defined(_MSC_VER)
10 #pragma once
11 #endif
12
13 #include <boost/spirit/home/support/info.hpp>
14 #include <boost/spirit/home/qi/detail/attributes.hpp>
15 #include <boost/spirit/home/support/common_terminals.hpp>
16 #include <boost/spirit/home/support/handles_container.hpp>
17 #include <boost/spirit/home/qi/skip_over.hpp>
18 #include <boost/spirit/home/qi/domain.hpp>
19 #include <boost/spirit/home/qi/parser.hpp>
20 #include <boost/spirit/home/qi/meta_compiler.hpp>
21 #include <boost/spirit/home/qi/detail/assign_to.hpp>
22 #include <boost/range/iterator_range.hpp>
23 #include <boost/fusion/include/vector.hpp>
24 #include <boost/fusion/include/at.hpp>
25 #include <boost/mpl/or.hpp>
26 #include <boost/mpl/and.hpp>
27 #include <boost/type_traits/is_integral.hpp>
28 #include <boost/type_traits/is_enum.hpp>
29 #include <boost/lexical_cast.hpp>
30 #include <iterator> // for std::iterator_traits
31
32 namespace boost { namespace spirit
33 {
34 ///////////////////////////////////////////////////////////////////////////
35 // Enablers
36 ///////////////////////////////////////////////////////////////////////////
37
38 // enables token
39 template <>
40 struct use_terminal<qi::domain, tag::token>
41 : mpl::true_ {};
42
43 // enables token(id)
44 template <typename A0>
45 struct use_terminal<qi::domain
46 , terminal_ex<tag::token, fusion::vector1<A0> >
47 > : mpl::or_<is_integral<A0>, is_enum<A0> > {};
48
49 // enables token(idmin, idmax)
50 template <typename A0, typename A1>
51 struct use_terminal<qi::domain
52 , terminal_ex<tag::token, fusion::vector2<A0, A1> >
53 > : mpl::and_<
54 mpl::or_<is_integral<A0>, is_enum<A0> >
55 , mpl::or_<is_integral<A1>, is_enum<A1> >
56 > {};
57
58 // enables *lazy* token(id)
59 template <>
60 struct use_lazy_terminal<
61 qi::domain, tag::token, 1
62 > : mpl::true_ {};
63
64 // enables *lazy* token(idmin, idmax)
65 template <>
66 struct use_lazy_terminal<
67 qi::domain, tag::token, 2
68 > : mpl::true_ {};
69 }}
70
71 namespace boost { namespace spirit { namespace qi
72 {
73 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
74 using spirit::token;
75 #endif
76 using spirit::token_type;
77
78 ///////////////////////////////////////////////////////////////////////////
79 template <typename TokenId>
80 struct plain_token
81 : primitive_parser<plain_token<TokenId> >
82 {
83 template <typename Context, typename Iterator>
84 struct attribute
85 {
86 typedef typename Iterator::base_iterator_type iterator_type;
87 typedef iterator_range<iterator_type> type;
88 };
89
90 plain_token(TokenId const& id)
91 : id(id) {}
92
93 template <typename Iterator, typename Context
94 , typename Skipper, typename Attribute>
95 bool parse(Iterator& first, Iterator const& last
96 , Context& /*context*/, Skipper const& skipper
97 , Attribute& attr) const
98 {
99 qi::skip_over(first, last, skipper); // always do a pre-skip
100
101 if (first != last) {
102 // simply match the token id with the id this component has
103 // been initialized with
104
105 typedef typename
106 std::iterator_traits<Iterator>::value_type
107 token_type;
108 typedef typename token_type::id_type id_type;
109
110 token_type const& t = *first;
111 if (id_type(~0) == id_type(id) || id_type(id) == t.id()) {
112 spirit::traits::assign_to(t, attr);
113 ++first;
114 return true;
115 }
116 }
117 return false;
118 }
119
120 template <typename Context>
121 info what(Context& /*context*/) const
122 {
123 return info("token",
124 "token(" + boost::lexical_cast<utf8_string>(id) + ")");
125 }
126
127 TokenId id;
128 };
129
130 ///////////////////////////////////////////////////////////////////////////
131 template <typename TokenId>
132 struct plain_token_range
133 : primitive_parser<plain_token_range<TokenId> >
134 {
135 template <typename Context, typename Iterator>
136 struct attribute
137 {
138 typedef typename Iterator::base_iterator_type iterator_type;
139 typedef iterator_range<iterator_type> type;
140 };
141
142 plain_token_range(TokenId const& idmin, TokenId const& idmax)
143 : idmin(idmin), idmax(idmax) {}
144
145 template <typename Iterator, typename Context
146 , typename Skipper, typename Attribute>
147 bool parse(Iterator& first, Iterator const& last
148 , Context& /*context*/, Skipper const& skipper
149 , Attribute& attr) const
150 {
151 qi::skip_over(first, last, skipper); // always do a pre-skip
152
153 if (first != last) {
154 // simply match the token id with the id this component has
155 // been initialized with
156
157 typedef typename
158 std::iterator_traits<Iterator>::value_type
159 token_type;
160 typedef typename token_type::id_type id_type;
161
162 token_type const& t = *first;
163 if (id_type(idmax) >= t.id() && id_type(idmin) <= t.id())
164 {
165 spirit::traits::assign_to(t, attr);
166 ++first;
167 return true;
168 }
169 }
170 return false;
171 }
172
173 template <typename Context>
174 info what(Context& /*context*/) const
175 {
176 return info("token_range"
177 , "token(" +
178 boost::lexical_cast<utf8_string>(idmin) + ", " +
179 boost::lexical_cast<utf8_string>(idmax) + ")"
180 );
181 return info("token_range");
182 }
183
184 TokenId idmin, idmax;
185 };
186
187 ///////////////////////////////////////////////////////////////////////////
188 // Parser generators: make_xxx function (objects)
189 ///////////////////////////////////////////////////////////////////////////
190 template <typename Modifiers>
191 struct make_primitive<tag::token, Modifiers>
192 {
193 typedef plain_token<std::size_t> result_type;
194
195 result_type operator()(unused_type, unused_type) const
196 {
197 return result_type(std::size_t(~0));
198 }
199 };
200
201 template <typename Modifiers, typename TokenId>
202 struct make_primitive<terminal_ex<tag::token, fusion::vector1<TokenId> >
203 , Modifiers>
204 {
205 typedef plain_token<TokenId> result_type;
206
207 template <typename Terminal>
208 result_type operator()(Terminal const& term, unused_type) const
209 {
210 return result_type(fusion::at_c<0>(term.args));
211 }
212 };
213
214 template <typename Modifiers, typename TokenId>
215 struct make_primitive<terminal_ex<tag::token, fusion::vector2<TokenId, TokenId> >
216 , Modifiers>
217 {
218 typedef plain_token_range<TokenId> result_type;
219
220 template <typename Terminal>
221 result_type operator()(Terminal const& term, unused_type) const
222 {
223 return result_type(fusion::at_c<0>(term.args)
224 , fusion::at_c<1>(term.args));
225 }
226 };
227 }}}
228
229 namespace boost { namespace spirit { namespace traits
230 {
231 ///////////////////////////////////////////////////////////////////////////
232 template<typename Idtype, typename Attr, typename Context, typename Iterator>
233 struct handles_container<qi::plain_token<Idtype>, Attr, Context, Iterator>
234 : mpl::true_
235 {};
236
237 template<typename Idtype, typename Attr, typename Context, typename Iterator>
238 struct handles_container<qi::plain_token_range<Idtype>, Attr, Context, Iterator>
239 : mpl::true_
240 {};
241 }}}
242
243 #endif