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_TOKEN_NOV_11_2007_0451PM)
7 #define BOOST_SPIRIT_LEX_PLAIN_TOKEN_NOV_11_2007_0451PM
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::token>
43 template <typename A0>
44 struct use_terminal<qi::domain
45 , terminal_ex<tag::token, fusion::vector1<A0> >
46 > : mpl::or_<is_integral<A0>, is_enum<A0> > {};
48 // enables token(idmin, idmax)
49 template <typename A0, typename A1>
50 struct use_terminal<qi::domain
51 , terminal_ex<tag::token, 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* token(id)
59 struct use_lazy_terminal<
60 qi::domain, tag::token, 1
63 // enables *lazy* token(idmin, idmax)
65 struct use_lazy_terminal<
66 qi::domain, tag::token, 2
70 namespace boost { namespace spirit { namespace qi
72 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
75 using spirit::token_type;
77 ///////////////////////////////////////////////////////////////////////////
78 template <typename TokenId>
80 : primitive_parser<plain_token<TokenId> >
82 template <typename Context, typename Iterator>
85 typedef typename Iterator::base_iterator_type iterator_type;
86 typedef iterator_range<iterator_type> type;
89 plain_token(TokenId const& id)
92 template <typename Iterator, typename Context
93 , typename Skipper, typename Attribute>
94 bool parse(Iterator& first, Iterator const& last
95 , Context& /*context*/, Skipper const& skipper
96 , Attribute& attr) const
98 qi::skip_over(first, last, skipper); // always do a pre-skip
101 // simply match the token id with the id this component has
102 // been initialized with
105 boost::detail::iterator_traits<Iterator>::value_type
107 typedef typename token_type::id_type id_type;
109 token_type const& t = *first;
110 if (id_type(~0) == id_type(id) || id_type(id) == t.id()) {
111 spirit::traits::assign_to(t, attr);
119 template <typename Context>
120 info what(Context& /*context*/) const
123 "token(" + boost::lexical_cast<utf8_string>(id) + ")");
129 ///////////////////////////////////////////////////////////////////////////
130 template <typename TokenId>
131 struct plain_token_range
132 : primitive_parser<plain_token_range<TokenId> >
134 template <typename Context, typename Iterator>
137 typedef typename Iterator::base_iterator_type iterator_type;
138 typedef iterator_range<iterator_type> type;
141 plain_token_range(TokenId const& idmin, TokenId const& idmax)
142 : idmin(idmin), idmax(idmax) {}
144 template <typename Iterator, typename Context
145 , typename Skipper, typename Attribute>
146 bool parse(Iterator& first, Iterator const& last
147 , Context& /*context*/, Skipper const& skipper
148 , Attribute& attr) const
150 qi::skip_over(first, last, skipper); // always do a pre-skip
153 // simply match the token id with the id this component has
154 // been initialized with
157 boost::detail::iterator_traits<Iterator>::value_type
159 typedef typename token_type::id_type id_type;
161 token_type const& t = *first;
162 if (id_type(idmax) >= t.id() && id_type(idmin) <= t.id())
164 spirit::traits::assign_to(t, attr);
172 template <typename Context>
173 info what(Context& /*context*/) const
175 return info("token_range"
177 boost::lexical_cast<utf8_string>(idmin) + ", " +
178 boost::lexical_cast<utf8_string>(idmax) + ")"
180 return info("token_range");
183 TokenId idmin, idmax;
186 ///////////////////////////////////////////////////////////////////////////
187 // Parser generators: make_xxx function (objects)
188 ///////////////////////////////////////////////////////////////////////////
189 template <typename Modifiers>
190 struct make_primitive<tag::token, Modifiers>
192 typedef plain_token<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::token, fusion::vector1<TokenId> >
204 typedef plain_token<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::token, fusion::vector2<TokenId, TokenId> >
217 typedef plain_token_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_token<Idtype>, Attr, Context, Iterator>
236 template<typename Idtype, typename Attr, typename Context, typename Iterator>
237 struct handles_container<qi::plain_token_range<Idtype>, Attr, Context, Iterator>