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_RAW_TOKEN_JUN_03_2011_0853PM)
7 #define BOOST_SPIRIT_LEX_PLAIN_RAW_TOKEN_JUN_03_2011_0853PM
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/type_traits/is_integral.hpp>
27 #include <boost/type_traits/is_enum.hpp>
28 #include <boost/lexical_cast.hpp>
30 namespace boost { namespace spirit
32 ///////////////////////////////////////////////////////////////////////////
34 ///////////////////////////////////////////////////////////////////////////
38 struct use_terminal<qi::domain, tag::raw_token>
41 // enables raw_token(id)
42 template <typename A0>
43 struct use_terminal<qi::domain
44 , terminal_ex<tag::raw_token, fusion::vector1<A0> >
45 > : mpl::or_<is_integral<A0>, is_enum<A0> > {};
47 // enables *lazy* raw_token(id)
49 struct use_lazy_terminal<
50 qi::domain, tag::raw_token, 1
54 namespace boost { namespace spirit { namespace qi
56 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
57 using spirit::raw_token;
59 using spirit::raw_token_type;
61 ///////////////////////////////////////////////////////////////////////////
62 template <typename TokenId>
63 struct plain_raw_token
64 : primitive_parser<plain_raw_token<TokenId> >
66 template <typename Context, typename Iterator>
69 typedef unused_type type;
72 plain_raw_token(TokenId const& id)
75 template <typename Iterator, typename Context
76 , typename Skipper, typename Attribute>
77 bool parse(Iterator& first, Iterator const& last
78 , Context& /*context*/, Skipper const& skipper
79 , Attribute& attr) const
81 qi::skip_over(first, last, skipper); // always do a pre-skip
84 // simply match the token id with the id this component has
85 // been initialized with
88 boost::detail::iterator_traits<Iterator>::value_type
90 typedef typename token_type::id_type id_type;
92 token_type const& t = *first;
93 if (id_type(~0) == id_type(id) || id_type(id) == t.id()) {
94 spirit::traits::assign_to(t, attr);
102 template <typename Context>
103 info what(Context& /*context*/) const
105 return info("raw_token",
106 "raw_token(" + boost::lexical_cast<utf8_string>(id) + ")");
112 ///////////////////////////////////////////////////////////////////////////
113 // Parser generators: make_xxx function (objects)
114 ///////////////////////////////////////////////////////////////////////////
115 template <typename Modifiers>
116 struct make_primitive<tag::raw_token, Modifiers>
118 typedef plain_raw_token<std::size_t> result_type;
120 result_type operator()(unused_type, unused_type) const
122 return result_type(std::size_t(~0));
126 template <typename Modifiers, typename TokenId>
127 struct make_primitive<terminal_ex<tag::raw_token, fusion::vector1<TokenId> >
130 typedef plain_raw_token<TokenId> result_type;
132 template <typename Terminal>
133 result_type operator()(Terminal const& term, unused_type) const
135 return result_type(fusion::at_c<0>(term.args));
140 namespace boost { namespace spirit { namespace traits
142 ///////////////////////////////////////////////////////////////////////////
143 template<typename Idtype, typename Attr, typename Context, typename Iterator>
144 struct handles_container<qi::plain_raw_token<Idtype>, Attr, Context, Iterator>