1 //  Copyright (c) 2001-2011 Hartmut Kaiser
2 //
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)
5 
6 #if !defined(BOOST_SPIRIT_LEX_PLAIN_TOKEN_NOV_11_2007_0451PM)
7 #define BOOST_SPIRIT_LEX_PLAIN_TOKEN_NOV_11_2007_0451PM
8 
9 #if defined(_MSC_VER)
10 #pragma once
11 #endif
12 
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 
31 namespace boost { namespace spirit
32 {
33     ///////////////////////////////////////////////////////////////////////////
34     // Enablers
35     ///////////////////////////////////////////////////////////////////////////
36 
37     // enables token
38     template <>
39     struct use_terminal<qi::domain, tag::token>
40       : mpl::true_ {};
41 
42     // enables token(id)
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> > {};
47 
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> >
52     > : mpl::and_<
53             mpl::or_<is_integral<A0>, is_enum<A0> >
54           , mpl::or_<is_integral<A1>, is_enum<A1> >
55         > {};
56 
57     // enables *lazy* token(id)
58     template <>
59     struct use_lazy_terminal<
60         qi::domain, tag::token, 1
61     > : mpl::true_ {};
62 
63     // enables *lazy* token(idmin, idmax)
64     template <>
65     struct use_lazy_terminal<
66         qi::domain, tag::token, 2
67     > : mpl::true_ {};
68 }}
69 
70 namespace boost { namespace spirit { namespace qi
71 {
72 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
73     using spirit::token;
74 #endif
75     using spirit::token_type;
76 
77     ///////////////////////////////////////////////////////////////////////////
78     template <typename TokenId>
79     struct plain_token
80       : primitive_parser<plain_token<TokenId> >
81     {
82         template <typename Context, typename Iterator>
83         struct attribute
84         {
85             typedef typename Iterator::base_iterator_type iterator_type;
86             typedef iterator_range<iterator_type> type;
87         };
88 
plain_tokenboost::spirit::qi::plain_token89         plain_token(TokenId const& id)
90           : id(id) {}
91 
92         template <typename Iterator, typename Context
93           , typename Skipper, typename Attribute>
parseboost::spirit::qi::plain_token94         bool parse(Iterator& first, Iterator const& last
95           , Context& /*context*/, Skipper const& skipper
96           , Attribute& attr) const
97         {
98             qi::skip_over(first, last, skipper);   // always do a pre-skip
99 
100             if (first != last) {
101                 // simply match the token id with the id this component has
102                 // been initialized with
103 
104                 typedef typename
105                     boost::detail::iterator_traits<Iterator>::value_type
106                 token_type;
107                 typedef typename token_type::id_type id_type;
108 
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);
112                     ++first;
113                     return true;
114                 }
115             }
116             return false;
117         }
118 
119         template <typename Context>
whatboost::spirit::qi::plain_token120         info what(Context& /*context*/) const
121         {
122             return info("token",
123                 "token(" + boost::lexical_cast<utf8_string>(id) + ")");
124         }
125 
126         TokenId id;
127     };
128 
129     ///////////////////////////////////////////////////////////////////////////
130     template <typename TokenId>
131     struct plain_token_range
132       : primitive_parser<plain_token_range<TokenId> >
133     {
134         template <typename Context, typename Iterator>
135         struct attribute
136         {
137             typedef typename Iterator::base_iterator_type iterator_type;
138             typedef iterator_range<iterator_type> type;
139         };
140 
plain_token_rangeboost::spirit::qi::plain_token_range141         plain_token_range(TokenId const& idmin, TokenId const& idmax)
142           : idmin(idmin), idmax(idmax) {}
143 
144         template <typename Iterator, typename Context
145           , typename Skipper, typename Attribute>
parseboost::spirit::qi::plain_token_range146         bool parse(Iterator& first, Iterator const& last
147           , Context& /*context*/, Skipper const& skipper
148           , Attribute& attr) const
149         {
150             qi::skip_over(first, last, skipper);   // always do a pre-skip
151 
152             if (first != last) {
153                 // simply match the token id with the id this component has
154                 // been initialized with
155 
156                 typedef typename
157                     boost::detail::iterator_traits<Iterator>::value_type
158                 token_type;
159                 typedef typename token_type::id_type id_type;
160 
161                 token_type const& t = *first;
162                 if (id_type(idmin) >= t.id() && id_type(idmin) <= t.id())
163                 {
164                     spirit::traits::assign_to(t, attr);
165                     ++first;
166                     return true;
167                 }
168             }
169             return false;
170         }
171 
172         template <typename Context>
whatboost::spirit::qi::plain_token_range173         info what(Context& /*context*/) const
174         {
175             return info("token_range"
176               , "token(" +
177                     boost::lexical_cast<utf8_string>(idmin) + ", " +
178                     boost::lexical_cast<utf8_string>(idmax) + ")"
179             );
180             return info("token_range");
181         }
182 
183         TokenId idmin, idmax;
184     };
185 
186     ///////////////////////////////////////////////////////////////////////////
187     // Parser generators: make_xxx function (objects)
188     ///////////////////////////////////////////////////////////////////////////
189     template <typename Modifiers>
190     struct make_primitive<tag::token, Modifiers>
191     {
192         typedef plain_token<std::size_t> result_type;
193 
operator ()boost::spirit::qi::make_primitive194         result_type operator()(unused_type, unused_type) const
195         {
196             return result_type(std::size_t(~0));
197         }
198     };
199 
200     template <typename Modifiers, typename TokenId>
201     struct make_primitive<terminal_ex<tag::token, fusion::vector1<TokenId> >
202       , Modifiers>
203     {
204         typedef plain_token<TokenId> result_type;
205 
206         template <typename Terminal>
operator ()boost::spirit::qi::make_primitive207         result_type operator()(Terminal const& term, unused_type) const
208         {
209             return result_type(fusion::at_c<0>(term.args));
210         }
211     };
212 
213     template <typename Modifiers, typename TokenId>
214     struct make_primitive<terminal_ex<tag::token, fusion::vector2<TokenId, TokenId> >
215       , Modifiers>
216     {
217         typedef plain_token_range<TokenId> result_type;
218 
219         template <typename Terminal>
operator ()boost::spirit::qi::make_primitive220         result_type operator()(Terminal const& term, unused_type) const
221         {
222             return result_type(fusion::at_c<0>(term.args)
223               , fusion::at_c<1>(term.args));
224         }
225     };
226 }}}
227 
228 namespace boost { namespace spirit { namespace traits
229 {
230     ///////////////////////////////////////////////////////////////////////////
231     template<typename Idtype, typename Attr, typename Context, typename Iterator>
232     struct handles_container<qi::plain_token<Idtype>, Attr, Context, Iterator>
233       : mpl::true_
234     {};
235 
236     template<typename Idtype, typename Attr, typename Context, typename Iterator>
237     struct handles_container<qi::plain_token_range<Idtype>, Attr, Context, Iterator>
238       : mpl::true_
239     {};
240 }}}
241 
242 #endif
243