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