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>
30 #include <iterator> // for std::iterator_traits
32 namespace boost { namespace spirit
34 ///////////////////////////////////////////////////////////////////////////
36 ///////////////////////////////////////////////////////////////////////////
40 struct use_terminal<qi::domain, tag::token>
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> > {};
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> >
54 mpl::or_<is_integral<A0>, is_enum<A0> >
55 , mpl::or_<is_integral<A1>, is_enum<A1> >
58 // enables *lazy* token(id)
60 struct use_lazy_terminal<
61 qi::domain, tag::token, 1
64 // enables *lazy* token(idmin, idmax)
66 struct use_lazy_terminal<
67 qi::domain, tag::token, 2
71 namespace boost { namespace spirit { namespace qi
73 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
76 using spirit::token_type;
78 ///////////////////////////////////////////////////////////////////////////
79 template <typename TokenId>
81 : primitive_parser<plain_token<TokenId> >
83 template <typename Context, typename Iterator>
86 typedef typename Iterator::base_iterator_type iterator_type;
87 typedef iterator_range<iterator_type> type;
90 plain_token(TokenId const& id)
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
99 qi::skip_over(first, last, skipper); // always do a pre-skip
102 // simply match the token id with the id this component has
103 // been initialized with
106 std::iterator_traits<Iterator>::value_type
108 typedef typename token_type::id_type id_type;
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);
120 template <typename Context>
121 info what(Context& /*context*/) const
124 "token(" + boost::lexical_cast<utf8_string>(id) + ")");
130 ///////////////////////////////////////////////////////////////////////////
131 template <typename TokenId>
132 struct plain_token_range
133 : primitive_parser<plain_token_range<TokenId> >
135 template <typename Context, typename Iterator>
138 typedef typename Iterator::base_iterator_type iterator_type;
139 typedef iterator_range<iterator_type> type;
142 plain_token_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 std::iterator_traits<Iterator>::value_type
160 typedef typename token_type::id_type id_type;
162 token_type const& t = *first;
163 if (id_type(idmax) >= t.id() && id_type(idmin) <= t.id())
165 spirit::traits::assign_to(t, attr);
173 template <typename Context>
174 info what(Context& /*context*/) const
176 return info("token_range"
178 boost::lexical_cast<utf8_string>(idmin) + ", " +
179 boost::lexical_cast<utf8_string>(idmax) + ")"
181 return info("token_range");
184 TokenId idmin, idmax;
187 ///////////////////////////////////////////////////////////////////////////
188 // Parser generators: make_xxx function (objects)
189 ///////////////////////////////////////////////////////////////////////////
190 template <typename Modifiers>
191 struct make_primitive<tag::token, Modifiers>
193 typedef plain_token<std::size_t> result_type;
195 result_type operator()(unused_type, unused_type) const
197 return result_type(std::size_t(~0));
201 template <typename Modifiers, typename TokenId>
202 struct make_primitive<terminal_ex<tag::token, fusion::vector1<TokenId> >
205 typedef plain_token<TokenId> result_type;
207 template <typename Terminal>
208 result_type operator()(Terminal const& term, unused_type) const
210 return result_type(fusion::at_c<0>(term.args));
214 template <typename Modifiers, typename TokenId>
215 struct make_primitive<terminal_ex<tag::token, fusion::vector2<TokenId, TokenId> >
218 typedef plain_token_range<TokenId> result_type;
220 template <typename Terminal>
221 result_type operator()(Terminal const& term, unused_type) const
223 return result_type(fusion::at_c<0>(term.args)
224 , fusion::at_c<1>(term.args));
229 namespace boost { namespace spirit { namespace traits
231 ///////////////////////////////////////////////////////////////////////////
232 template<typename Idtype, typename Attr, typename Context, typename Iterator>
233 struct handles_container<qi::plain_token<Idtype>, Attr, Context, Iterator>
237 template<typename Idtype, typename Attr, typename Context, typename Iterator>
238 struct handles_container<qi::plain_token_range<Idtype>, Attr, Context, Iterator>