]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/spirit/home/lex/qi/plain_tokenid_mask.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / spirit / home / lex / qi / plain_tokenid_mask.hpp
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_TOKENID_MASK_JUN_03_2011_0929PM)
7 #define BOOST_SPIRIT_LEX_PLAIN_TOKENID_MASK_JUN_03_2011_0929PM
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/type_traits/is_integral.hpp>
27 #include <boost/type_traits/is_enum.hpp>
28 #include <boost/lexical_cast.hpp>
29 #include <iterator> // for std::iterator_traits
30
31 namespace boost { namespace spirit
32 {
33 ///////////////////////////////////////////////////////////////////////////
34 // Enablers
35 ///////////////////////////////////////////////////////////////////////////
36
37 // enables tokenid_mask(id)
38 template <typename A0>
39 struct use_terminal<qi::domain
40 , terminal_ex<tag::tokenid_mask, fusion::vector1<A0> >
41 > : mpl::or_<is_integral<A0>, is_enum<A0> > {};
42
43 // enables *lazy* tokenid_mask(id)
44 template <>
45 struct use_lazy_terminal<
46 qi::domain, tag::tokenid_mask, 1
47 > : mpl::true_ {};
48 }}
49
50 namespace boost { namespace spirit { namespace qi
51 {
52 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
53 using spirit::tokenid_mask;
54 #endif
55 using spirit::tokenid_mask_type;
56
57 ///////////////////////////////////////////////////////////////////////////
58 // The plain_tokenid represents a simple token defined by the lexer inside
59 // a Qi grammar. The difference to plain_token is that it exposes the
60 // matched token id instead of the iterator_range of the matched input.
61 // Additionally it applies the given mask to the matched token id.
62 template <typename Mask>
63 struct plain_tokenid_mask
64 : primitive_parser<plain_tokenid_mask<Mask> >
65 {
66 template <typename Context, typename Iterator>
67 struct attribute
68 {
69 typedef Mask type;
70 };
71
72 plain_tokenid_mask(Mask const& mask)
73 : mask(mask) {}
74
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
80 {
81 qi::skip_over(first, last, skipper); // always do a pre-skip
82
83 if (first != last) {
84 // simply match the token id with the mask this component has
85 // been initialized with
86
87 typedef typename
88 std::iterator_traits<Iterator>::value_type
89 token_type;
90 typedef typename token_type::id_type id_type;
91
92 token_type const& t = *first;
93 if ((t.id() & mask) == id_type(mask))
94 {
95 spirit::traits::assign_to(t.id(), attr);
96 ++first;
97 return true;
98 }
99 }
100 return false;
101 }
102
103 template <typename Context>
104 info what(Context& /*context*/) const
105 {
106 return info("tokenid_mask",
107 "tokenid_mask(" + boost::lexical_cast<utf8_string>(mask) + ")");
108 }
109
110 Mask mask;
111 };
112
113 ///////////////////////////////////////////////////////////////////////////
114 // Parser generators: make_xxx function (objects)
115 ///////////////////////////////////////////////////////////////////////////
116 template <typename Modifiers, typename Mask>
117 struct make_primitive<terminal_ex<tag::tokenid_mask, fusion::vector1<Mask> >
118 , Modifiers>
119 {
120 typedef plain_tokenid_mask<Mask> result_type;
121
122 template <typename Terminal>
123 result_type operator()(Terminal const& term, unused_type) const
124 {
125 return result_type(fusion::at_c<0>(term.args));
126 }
127 };
128 }}}
129
130 namespace boost { namespace spirit { namespace traits
131 {
132 ///////////////////////////////////////////////////////////////////////////
133 template<typename Mask, typename Attr, typename Context, typename Iterator>
134 struct handles_container<qi::plain_tokenid_mask<Mask>, Attr, Context, Iterator>
135 : mpl::true_
136 {};
137 }}}
138
139 #endif