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 <iterator> // for std::iterator_traits
32 namespace boost { namespace spirit
34 ///////////////////////////////////////////////////////////////////////////
36 ///////////////////////////////////////////////////////////////////////////
40 struct use_terminal<qi::domain, tag::tokenid>
43 // enables tokenid(id)
44 template <typename A0>
45 struct use_terminal<qi::domain
46 , terminal_ex<tag::tokenid, fusion::vector1<A0> >
47 > : mpl::or_<is_integral<A0>, is_enum<A0> > {};
49 // enables tokenid(idmin, idmax)
50 template <typename A0, typename A1>
51 struct use_terminal<qi::domain
52 , terminal_ex<tag::tokenid, fusion::vector2<A0, A1> >
54 mpl::or_<is_integral<A0>, is_enum<A0> >
55 , mpl::or_<is_integral<A1>, is_enum<A1> >
58 // enables *lazy* tokenid(id)
60 struct use_lazy_terminal<
61 qi::domain, tag::tokenid, 1
64 // enables *lazy* tokenid(idmin, idmax)
66 struct use_lazy_terminal<
67 qi::domain, tag::tokenid, 2
71 namespace boost { namespace spirit { namespace qi
73 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
74 using spirit::tokenid;
76 using spirit::tokenid_type;
78 ///////////////////////////////////////////////////////////////////////////
79 // The plain_tokenid represents a simple token defined by the lexer inside
80 // a Qi grammar. The difference to plain_token is that it exposes the
81 // matched token id instead of the iterator_range of the matched input.
82 template <typename TokenId>
84 : primitive_parser<plain_tokenid<TokenId> >
86 template <typename Context, typename Iterator>
92 plain_tokenid(TokenId const& id)
95 template <typename Iterator, typename Context
96 , typename Skipper, typename Attribute>
97 bool parse(Iterator& first, Iterator const& last
98 , Context& /*context*/, Skipper const& skipper
99 , Attribute& attr) const
101 qi::skip_over(first, last, skipper); // always do a pre-skip
104 // simply match the token id with the id this component has
105 // been initialized with
108 std::iterator_traits<Iterator>::value_type
110 typedef typename token_type::id_type id_type;
112 token_type const& t = *first;
113 if (id_type(~0) == id_type(id) || id_type(id) == t.id()) {
114 spirit::traits::assign_to(id, attr);
122 template <typename Context>
123 info what(Context& /*context*/) const
125 std::stringstream ss;
126 ss << "tokenid(" << id << ")";
127 return info("tokenid", ss.str());
133 ///////////////////////////////////////////////////////////////////////////
134 template <typename TokenId>
135 struct plain_tokenid_range
136 : primitive_parser<plain_tokenid_range<TokenId> >
138 template <typename Context, typename Iterator>
141 typedef TokenId type;
144 plain_tokenid_range(TokenId const& idmin, TokenId const& idmax)
145 : idmin(idmin), idmax(idmax) {}
147 template <typename Iterator, typename Context
148 , typename Skipper, typename Attribute>
149 bool parse(Iterator& first, Iterator const& last
150 , Context& /*context*/, Skipper const& skipper
151 , Attribute& attr) const
153 qi::skip_over(first, last, skipper); // always do a pre-skip
156 // simply match the token id with the id this component has
157 // been initialized with
160 std::iterator_traits<Iterator>::value_type
162 typedef typename token_type::id_type id_type;
164 token_type const& t = *first;
165 if (id_type(idmin) >= t.id() && id_type(idmin) <= t.id())
167 spirit::traits::assign_to(t.id(), attr);
175 template <typename Context>
176 info what(Context& /*context*/) const
178 std::stringstream ss;
179 ss << "token(" << idmin << ", " << idmax << ")";
180 return info("tokenid_range", ss.str());
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>