1 // Copyright (c) 2001-2011 Hartmut Kaiser
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)
6 #if !defined(BOOST_SPIRIT_LEX_PLAIN_TOKENID_NOV_26_2010_0944AM)
7 #define BOOST_SPIRIT_LEX_PLAIN_TOKENID_NOV_26_2010_0944AM
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>
31 namespace boost { namespace spirit
33 ///////////////////////////////////////////////////////////////////////////
35 ///////////////////////////////////////////////////////////////////////////
39 struct use_terminal<qi::domain, tag::tokenid>
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> > {};
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> >
53 mpl::or_<is_integral<A0>, is_enum<A0> >
54 , mpl::or_<is_integral<A1>, is_enum<A1> >
57 // enables *lazy* tokenid(id)
59 struct use_lazy_terminal<
60 qi::domain, tag::tokenid, 1
63 // enables *lazy* tokenid(idmin, idmax)
65 struct use_lazy_terminal<
66 qi::domain, tag::tokenid, 2
70 namespace boost { namespace spirit { namespace qi
72 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
73 using spirit::tokenid;
75 using spirit::tokenid_type;
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>
83 : primitive_parser<plain_tokenid<TokenId> >
85 template <typename Context, typename Iterator>
91 plain_tokenid(TokenId const& id)
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
100 qi::skip_over(first, last, skipper); // always do a pre-skip
103 // simply match the token id with the id this component has
104 // been initialized with
107 boost::detail::iterator_traits<Iterator>::value_type
109 typedef typename token_type::id_type id_type;
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);
121 template <typename Context>
122 info what(Context& /*context*/) const
124 return info("tokenid",
125 "tokenid(" + boost::lexical_cast<utf8_string>(id) + ")");
131 ///////////////////////////////////////////////////////////////////////////
132 template <typename TokenId>
133 struct plain_tokenid_range
134 : primitive_parser<plain_tokenid_range<TokenId> >
136 template <typename Context, typename Iterator>
139 typedef TokenId type;
142 plain_tokenid_range(TokenId const& idmin, TokenId const& idmax)
143 : idmin(idmin), idmax(idmax) {}
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
151 qi::skip_over(first, last, skipper); // always do a pre-skip
154 // simply match the token id with the id this component has
155 // been initialized with
158 boost::detail::iterator_traits<Iterator>::value_type
160 typedef typename token_type::id_type id_type;
162 token_type const& t = *first;
163 if (id_type(idmin) >= t.id() && id_type(idmin) <= t.id())
165 spirit::traits::assign_to(t.id(), attr);
173 template <typename Context>
174 info what(Context& /*context*/) const
176 return info("tokenid_range"
178 boost::lexical_cast<utf8_string>(idmin) + ", " +
179 boost::lexical_cast<utf8_string>(idmax) + ")"
183 TokenId idmin, idmax;
186 ///////////////////////////////////////////////////////////////////////////
187 // Parser generators: make_xxx function (objects)
188 ///////////////////////////////////////////////////////////////////////////
189 template <typename Modifiers>
190 struct make_primitive<tag::tokenid, Modifiers>
192 typedef plain_tokenid<std::size_t> result_type;
194 result_type operator()(unused_type, unused_type) const
196 return result_type(std::size_t(~0));
200 template <typename Modifiers, typename TokenId>
201 struct make_primitive<terminal_ex<tag::tokenid, fusion::vector1<TokenId> >
204 typedef plain_tokenid<TokenId> result_type;
206 template <typename Terminal>
207 result_type operator()(Terminal const& term, unused_type) const
209 return result_type(fusion::at_c<0>(term.args));
213 template <typename Modifiers, typename TokenId>
214 struct make_primitive<terminal_ex<tag::tokenid, fusion::vector2<TokenId, TokenId> >
217 typedef plain_tokenid_range<TokenId> result_type;
219 template <typename Terminal>
220 result_type operator()(Terminal const& term, unused_type) const
222 return result_type(fusion::at_c<0>(term.args)
223 , fusion::at_c<1>(term.args));
228 namespace boost { namespace spirit { namespace traits
230 ///////////////////////////////////////////////////////////////////////////
231 template<typename Idtype, typename Attr, typename Context, typename Iterator>
232 struct handles_container<qi::plain_tokenid<Idtype>, Attr, Context, Iterator>
236 template<typename Idtype, typename Attr, typename Context, typename Iterator>
237 struct handles_container<qi::plain_tokenid_range<Idtype>, Attr, Context, Iterator>