]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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_TOKENID_NOV_26_2010_0944AM) | |
7 | #define BOOST_SPIRIT_LEX_PLAIN_TOKENID_NOV_26_2010_0944AM | |
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 | ||
31 | namespace boost { namespace spirit | |
32 | { | |
33 | /////////////////////////////////////////////////////////////////////////// | |
34 | // Enablers | |
35 | /////////////////////////////////////////////////////////////////////////// | |
36 | ||
37 | // enables tokenid | |
38 | template <> | |
39 | struct use_terminal<qi::domain, tag::tokenid> | |
40 | : mpl::true_ {}; | |
41 | ||
42 | // enables tokenid(id) | |
43 | template <typename A0> | |
44 | struct use_terminal<qi::domain | |
45 | , terminal_ex<tag::tokenid, fusion::vector1<A0> > | |
46 | > : mpl::or_<is_integral<A0>, is_enum<A0> > {}; | |
47 | ||
48 | // enables tokenid(idmin, idmax) | |
49 | template <typename A0, typename A1> | |
50 | struct use_terminal<qi::domain | |
51 | , terminal_ex<tag::tokenid, fusion::vector2<A0, A1> > | |
52 | > : mpl::and_< | |
53 | mpl::or_<is_integral<A0>, is_enum<A0> > | |
54 | , mpl::or_<is_integral<A1>, is_enum<A1> > | |
55 | > {}; | |
56 | ||
57 | // enables *lazy* tokenid(id) | |
58 | template <> | |
59 | struct use_lazy_terminal< | |
60 | qi::domain, tag::tokenid, 1 | |
61 | > : mpl::true_ {}; | |
62 | ||
63 | // enables *lazy* tokenid(idmin, idmax) | |
64 | template <> | |
65 | struct use_lazy_terminal< | |
66 | qi::domain, tag::tokenid, 2 | |
67 | > : mpl::true_ {}; | |
68 | }} | |
69 | ||
70 | namespace boost { namespace spirit { namespace qi | |
71 | { | |
72 | #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS | |
73 | using spirit::tokenid; | |
74 | #endif | |
75 | using spirit::tokenid_type; | |
76 | ||
77 | /////////////////////////////////////////////////////////////////////////// | |
78 | // The plain_tokenid represents a simple token defined by the lexer inside | |
79 | // a Qi grammar. The difference to plain_token is that it exposes the | |
80 | // matched token id instead of the iterator_range of the matched input. | |
81 | template <typename TokenId> | |
82 | struct plain_tokenid | |
83 | : primitive_parser<plain_tokenid<TokenId> > | |
84 | { | |
85 | template <typename Context, typename Iterator> | |
86 | struct attribute | |
87 | { | |
88 | typedef TokenId type; | |
89 | }; | |
90 | ||
91 | plain_tokenid(TokenId const& id) | |
92 | : id(id) {} | |
93 | ||
94 | template <typename Iterator, typename Context | |
95 | , typename Skipper, typename Attribute> | |
96 | bool parse(Iterator& first, Iterator const& last | |
97 | , Context& /*context*/, Skipper const& skipper | |
98 | , Attribute& attr) const | |
99 | { | |
100 | qi::skip_over(first, last, skipper); // always do a pre-skip | |
101 | ||
102 | if (first != last) { | |
103 | // simply match the token id with the id this component has | |
104 | // been initialized with | |
105 | ||
106 | typedef typename | |
107 | boost::detail::iterator_traits<Iterator>::value_type | |
108 | token_type; | |
109 | typedef typename token_type::id_type id_type; | |
110 | ||
111 | token_type const& t = *first; | |
112 | if (id_type(~0) == id_type(id) || id_type(id) == t.id()) { | |
113 | spirit::traits::assign_to(id, attr); | |
114 | ++first; | |
115 | return true; | |
116 | } | |
117 | } | |
118 | return false; | |
119 | } | |
120 | ||
121 | template <typename Context> | |
122 | info what(Context& /*context*/) const | |
123 | { | |
124 | return info("tokenid", | |
125 | "tokenid(" + boost::lexical_cast<utf8_string>(id) + ")"); | |
126 | } | |
127 | ||
128 | TokenId id; | |
129 | }; | |
130 | ||
131 | /////////////////////////////////////////////////////////////////////////// | |
132 | template <typename TokenId> | |
133 | struct plain_tokenid_range | |
134 | : primitive_parser<plain_tokenid_range<TokenId> > | |
135 | { | |
136 | template <typename Context, typename Iterator> | |
137 | struct attribute | |
138 | { | |
139 | typedef TokenId type; | |
140 | }; | |
141 | ||
142 | plain_tokenid_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 | boost::detail::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(idmin) >= t.id() && id_type(idmin) <= t.id()) | |
164 | { | |
165 | spirit::traits::assign_to(t.id(), 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("tokenid_range" | |
177 | , "token(" + | |
178 | boost::lexical_cast<utf8_string>(idmin) + ", " + | |
179 | boost::lexical_cast<utf8_string>(idmax) + ")" | |
180 | ); | |
181 | } | |
182 | ||
183 | TokenId idmin, idmax; | |
184 | }; | |
185 | ||
186 | /////////////////////////////////////////////////////////////////////////// | |
187 | // Parser generators: make_xxx function (objects) | |
188 | /////////////////////////////////////////////////////////////////////////// | |
189 | template <typename Modifiers> | |
190 | struct make_primitive<tag::tokenid, Modifiers> | |
191 | { | |
192 | typedef plain_tokenid<std::size_t> result_type; | |
193 | ||
194 | result_type operator()(unused_type, unused_type) const | |
195 | { | |
196 | return result_type(std::size_t(~0)); | |
197 | } | |
198 | }; | |
199 | ||
200 | template <typename Modifiers, typename TokenId> | |
201 | struct make_primitive<terminal_ex<tag::tokenid, fusion::vector1<TokenId> > | |
202 | , Modifiers> | |
203 | { | |
204 | typedef plain_tokenid<TokenId> result_type; | |
205 | ||
206 | template <typename Terminal> | |
207 | result_type operator()(Terminal const& term, unused_type) const | |
208 | { | |
209 | return result_type(fusion::at_c<0>(term.args)); | |
210 | } | |
211 | }; | |
212 | ||
213 | template <typename Modifiers, typename TokenId> | |
214 | struct make_primitive<terminal_ex<tag::tokenid, fusion::vector2<TokenId, TokenId> > | |
215 | , Modifiers> | |
216 | { | |
217 | typedef plain_tokenid_range<TokenId> result_type; | |
218 | ||
219 | template <typename Terminal> | |
220 | result_type operator()(Terminal const& term, unused_type) const | |
221 | { | |
222 | return result_type(fusion::at_c<0>(term.args) | |
223 | , fusion::at_c<1>(term.args)); | |
224 | } | |
225 | }; | |
226 | }}} | |
227 | ||
228 | namespace boost { namespace spirit { namespace traits | |
229 | { | |
230 | /////////////////////////////////////////////////////////////////////////// | |
231 | template<typename Idtype, typename Attr, typename Context, typename Iterator> | |
232 | struct handles_container<qi::plain_tokenid<Idtype>, Attr, Context, Iterator> | |
233 | : mpl::true_ | |
234 | {}; | |
235 | ||
236 | template<typename Idtype, typename Attr, typename Context, typename Iterator> | |
237 | struct handles_container<qi::plain_tokenid_range<Idtype>, Attr, Context, Iterator> | |
238 | : mpl::true_ | |
239 | {}; | |
240 | }}} | |
241 | ||
242 | #endif |