]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/include/boost/spirit/home/lex/lexer/token_def.hpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / spirit / include / boost / spirit / home / lex / lexer / token_def.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_TOKEN_DEF_MAR_13_2007_0145PM)
7 #define BOOST_SPIRIT_LEX_TOKEN_DEF_MAR_13_2007_0145PM
8
9 #if defined(_MSC_VER)
10 #pragma once
11 #endif
12
13 #include <boost/spirit/home/support/unused.hpp>
14 #include <boost/spirit/home/support/argument.hpp>
15 #include <boost/spirit/home/support/info.hpp>
16 #include <boost/spirit/home/support/handles_container.hpp>
17 #include <boost/spirit/home/qi/parser.hpp>
18 #include <boost/spirit/home/qi/skip_over.hpp>
19 #include <boost/spirit/home/qi/detail/construct.hpp>
20 #include <boost/spirit/home/qi/detail/assign_to.hpp>
21 #include <boost/spirit/home/lex/reference.hpp>
22 #include <boost/spirit/home/lex/lexer_type.hpp>
23 #include <boost/spirit/home/lex/lexer/terminals.hpp>
24
25 #include <boost/fusion/include/vector.hpp>
26 #include <boost/mpl/if.hpp>
27 #include <boost/detail/iterator.hpp>
28 #include <boost/type_traits/is_same.hpp>
29 #include <boost/variant.hpp>
30
31 #include <string>
32 #include <cstdlib>
33
34 #if defined(BOOST_MSVC)
35 # pragma warning(push)
36 # pragma warning(disable: 4355) // 'this' : used in base member initializer list warning
37 #endif
38
39 namespace boost { namespace spirit { namespace lex
40 {
41 ///////////////////////////////////////////////////////////////////////////
42 // This component represents a token definition
43 ///////////////////////////////////////////////////////////////////////////
44 template<typename Attribute = unused_type
45 , typename Char = char
46 , typename Idtype = std::size_t>
47 struct token_def
48 : proto::extends<
49 typename proto::terminal<
50 lex::reference<token_def<Attribute, Char, Idtype> const, Idtype>
51 >::type
52 , token_def<Attribute, Char, Idtype> >
53 , qi::parser<token_def<Attribute, Char, Idtype> >
54 , lex::lexer_type<token_def<Attribute, Char, Idtype> >
55 {
56 private:
57 // initialize proto base class
58 typedef lex::reference<token_def const, Idtype> reference_;
59 typedef typename proto::terminal<reference_>::type terminal_type;
60 typedef proto::extends<terminal_type, token_def> proto_base_type;
61
62 static std::size_t const all_states_id = static_cast<std::size_t>(-2);
63
64 public:
65 // Qi interface: meta-function calculating parser return type
66 template <typename Context, typename Iterator>
67 struct attribute
68 {
69 // The return value of the token_def is either the specified
70 // attribute type, or the pair of iterators from the match of the
71 // corresponding token (if no attribute type has been specified),
72 // or unused_type (if omit has been specified).
73 typedef typename Iterator::base_iterator_type iterator_type;
74 typedef typename mpl::if_<
75 traits::not_is_unused<Attribute>
76 , typename mpl::if_<
77 is_same<Attribute, lex::omit>, unused_type, Attribute
78 >::type
79 , iterator_range<iterator_type>
80 >::type type;
81 };
82
83 public:
84 // Qi interface: parse functionality
85 template <typename Iterator, typename Context
86 , typename Skipper, typename Attribute_>
87 bool parse(Iterator& first, Iterator const& last
88 , Context& /*context*/, Skipper const& skipper
89 , Attribute_& attr) const
90 {
91 qi::skip_over(first, last, skipper); // always do a pre-skip
92
93 if (first != last) {
94 typedef typename
95 boost::detail::iterator_traits<Iterator>::value_type
96 token_type;
97
98 // If the following assertion fires you probably forgot to
99 // associate this token definition with a lexer instance.
100 BOOST_ASSERT(std::size_t(~0) != token_state_);
101
102 token_type const& t = *first;
103 if (token_id_ == t.id() &&
104 (all_states_id == token_state_ || token_state_ == t.state()))
105 {
106 spirit::traits::assign_to(t, attr);
107 ++first;
108 return true;
109 }
110 }
111 return false;
112 }
113
114 template <typename Context>
115 info what(Context& /*context*/) const
116 {
117 if (0 == def_.which())
118 return info("token_def", boost::get<string_type>(def_));
119
120 return info("token_def", boost::get<char_type>(def_));
121 }
122
123 ///////////////////////////////////////////////////////////////////////
124 // Lex interface: collect token definitions and put it into the
125 // provided lexer def
126 template <typename LexerDef, typename String>
127 void collect(LexerDef& lexdef, String const& state
128 , String const& targetstate) const
129 {
130 std::size_t state_id = lexdef.add_state(state.c_str());
131
132 // If the following assertion fires you are probably trying to use
133 // a single token_def instance in more than one lexer state. This
134 // is not possible. Please create a separate token_def instance
135 // from the same regular expression for each lexer state it needs
136 // to be associated with.
137 BOOST_ASSERT(
138 (std::size_t(~0) == token_state_ || state_id == token_state_) &&
139 "Can't use single token_def with more than one lexer state");
140
141 char_type const* target = targetstate.empty() ? 0 : targetstate.c_str();
142 if (target)
143 lexdef.add_state(target);
144
145 token_state_ = state_id;
146 if (0 == token_id_)
147 token_id_ = lexdef.get_next_id();
148
149 if (0 == def_.which()) {
150 unique_id_ = lexdef.add_token(state.c_str()
151 , boost::get<string_type>(def_), token_id_, target);
152 }
153 else {
154 unique_id_ = lexdef.add_token(state.c_str()
155 , boost::get<char_type>(def_), token_id_, target);
156 }
157 }
158
159 template <typename LexerDef>
160 void add_actions(LexerDef&) const {}
161
162 public:
163 typedef Char char_type;
164 typedef Idtype id_type;
165 typedef std::basic_string<char_type> string_type;
166
167 // Lex interface: constructing token definitions
168 token_def()
169 : proto_base_type(terminal_type::make(reference_(*this)))
170 , def_('\0'), token_id_()
171 , unique_id_(std::size_t(~0)), token_state_(std::size_t(~0)) {}
172
173 token_def(token_def const& rhs)
174 : proto_base_type(terminal_type::make(reference_(*this)))
175 , def_(rhs.def_), token_id_(rhs.token_id_)
176 , unique_id_(rhs.unique_id_), token_state_(rhs.token_state_) {}
177
178 explicit token_def(char_type def_, Idtype id_ = Idtype())
179 : proto_base_type(terminal_type::make(reference_(*this)))
180 , def_(def_)
181 , token_id_(Idtype() == id_ ? Idtype(def_) : id_)
182 , unique_id_(std::size_t(~0)), token_state_(std::size_t(~0)) {}
183
184 explicit token_def(string_type const& def_, Idtype id_ = Idtype())
185 : proto_base_type(terminal_type::make(reference_(*this)))
186 , def_(def_), token_id_(id_)
187 , unique_id_(std::size_t(~0)), token_state_(std::size_t(~0)) {}
188
189 template <typename String>
190 token_def& operator= (String const& definition)
191 {
192 def_ = definition;
193 token_id_ = Idtype();
194 unique_id_ = std::size_t(~0);
195 token_state_ = std::size_t(~0);
196 return *this;
197 }
198 token_def& operator= (token_def const& rhs)
199 {
200 def_ = rhs.def_;
201 token_id_ = rhs.token_id_;
202 unique_id_ = rhs.unique_id_;
203 token_state_ = rhs.token_state_;
204 return *this;
205 }
206
207 // general accessors
208 Idtype const& id() const { return token_id_; }
209 void id(Idtype const& id) { token_id_ = id; }
210 std::size_t unique_id() const { return unique_id_; }
211
212 string_type definition() const
213 {
214 return (0 == def_.which()) ?
215 boost::get<string_type>(def_) :
216 string_type(1, boost::get<char_type>(def_));
217 }
218 std::size_t state() const { return token_state_; }
219
220 private:
221 variant<string_type, char_type> def_;
222 mutable Idtype token_id_;
223 mutable std::size_t unique_id_;
224 mutable std::size_t token_state_;
225 };
226 }}}
227
228 namespace boost { namespace spirit { namespace traits
229 {
230 ///////////////////////////////////////////////////////////////////////////
231 template<typename Attribute, typename Char, typename Idtype
232 , typename Attr, typename Context, typename Iterator>
233 struct handles_container<
234 lex::token_def<Attribute, Char, Idtype>, Attr, Context, Iterator>
235 : traits::is_container<
236 typename attribute_of<
237 lex::token_def<Attribute, Char, Idtype>, Context, Iterator
238 >::type>
239 {};
240 }}}
241
242 #if defined(BOOST_MSVC)
243 # pragma warning(pop)
244 #endif
245
246 #endif