1 /*=============================================================================
2 Boost.Wave: A Standard compliant C++ preprocessor library
4 A generic C++ lexer token definition
8 Copyright (c) 2001-2012 Hartmut Kaiser. Distributed under the Boost
9 Software License, Version 1.0. (See accompanying file
10 LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 =============================================================================*/
13 #if !defined(CPP_TOKEN_HPP_53A13BD2_FBAA_444B_9B8B_FCB225C2BBA8_INCLUDED)
14 #define CPP_TOKEN_HPP_53A13BD2_FBAA_444B_9B8B_FCB225C2BBA8_INCLUDED
16 #include <boost/wave/wave_config.hpp>
17 #if BOOST_WAVE_SERIALIZATION != 0
18 #include <boost/serialization/serialization.hpp>
20 #include <boost/wave/util/file_position.hpp>
21 #include <boost/wave/token_ids.hpp>
22 #include <boost/wave/language_support.hpp>
24 #include <boost/throw_exception.hpp>
25 #include <boost/pool/singleton_pool.hpp>
26 #include <boost/detail/atomic_count.hpp>
28 // this must occur after all of the includes and before any code appears
29 #ifdef BOOST_HAS_ABI_HEADERS
30 #include BOOST_ABI_PREFIX
33 ///////////////////////////////////////////////////////////////////////////////
40 template <typename StringTypeT, typename PositionT>
44 typedef StringTypeT string_type;
45 typedef PositionT position_type;
47 // default constructed tokens correspond to EOI tokens
49 : id(T_EOI), refcnt(1)
52 // construct an invalid token
53 explicit token_data(int)
54 : id(T_UNKNOWN), refcnt(1)
57 token_data(token_id id_, string_type const &value_, position_type const &pos_)
58 : id(id_), value(value_), pos(pos_), refcnt(1)
61 token_data(token_data const& rhs)
62 : id(rhs.id), value(rhs.value), pos(rhs.pos), refcnt(1)
68 std::size_t addref() { return ++refcnt; }
69 std::size_t release() { return --refcnt; }
70 std::size_t get_refcnt() const { return refcnt; }
73 operator token_id() const { return id; }
74 string_type const &get_value() const { return value; }
75 position_type const &get_position() const { return pos; }
77 void set_token_id (token_id id_) { id = id_; }
78 void set_value (string_type const &value_) { value = value_; }
79 void set_position (position_type const &pos_) { pos = pos_; }
81 friend bool operator== (token_data const& lhs, token_data const& rhs)
83 // two tokens are considered equal even if they refer to different
85 return (lhs.id == rhs.id && lhs.value == rhs.value) ? true : false;
88 void init(token_id id_, string_type const &value_, position_type const &pos_)
90 BOOST_ASSERT(refcnt == 1);
96 void init(token_data const& rhs)
98 BOOST_ASSERT(refcnt == 1);
104 static void *operator new(std::size_t size);
105 static void operator delete(void *p, std::size_t size);
107 #if defined(BOOST_SPIRIT_DEBUG)
109 void print (std::ostream &stream) const
111 stream << get_token_name(id) << "(";
112 for (std::size_t i = 0; i < value.size(); ++i) {
114 case '\r': stream << "\\r"; break;
115 case '\n': stream << "\\n"; break;
123 #endif // defined(BOOST_SPIRIT_DEBUG)
125 #if BOOST_WAVE_SERIALIZATION != 0
126 friend class boost::serialization::access;
127 template<typename Archive>
128 void serialize(Archive &ar, const unsigned int version)
130 using namespace boost::serialization;
131 ar & make_nvp("id", id);
132 ar & make_nvp("value", value);
133 ar & make_nvp("position", pos);
138 token_id id; // the token id
139 string_type value; // the text, which was parsed into this token
140 position_type pos; // the original file position
141 boost::detail::atomic_count refcnt;
144 ///////////////////////////////////////////////////////////////////////////////
145 struct token_data_tag {};
147 template <typename StringTypeT, typename PositionT>
149 token_data<StringTypeT, PositionT>::operator new(std::size_t size)
151 BOOST_ASSERT(sizeof(token_data<StringTypeT, PositionT>) == size);
152 typedef boost::singleton_pool<
153 token_data_tag, sizeof(token_data<StringTypeT, PositionT>)
156 void *ret = pool_type::malloc();
158 boost::throw_exception(std::bad_alloc());
162 template <typename StringTypeT, typename PositionT>
164 token_data<StringTypeT, PositionT>::operator delete(void *p, std::size_t size)
166 BOOST_ASSERT(sizeof(token_data<StringTypeT, PositionT>) == size);
167 typedef boost::singleton_pool<
168 token_data_tag, sizeof(token_data<StringTypeT, PositionT>)
177 ///////////////////////////////////////////////////////////////////////////////
178 // forward declaration of the token type
179 template <typename PositionT = boost::wave::util::file_position_type>
182 ///////////////////////////////////////////////////////////////////////////////
186 ///////////////////////////////////////////////////////////////////////////////
188 template <typename PositionT>
192 typedef BOOST_WAVE_STRINGTYPE string_type;
193 typedef PositionT position_type;
196 typedef impl::token_data<string_type, position_type> data_type;
199 // default constructed tokens correspond to EOI tokens
204 // construct an invalid token
205 explicit lex_token(int)
206 : data(new data_type(0))
209 lex_token(lex_token const& rhs)
216 lex_token(token_id id_, string_type const &value_, PositionT const &pos_)
217 : data(new data_type(id_, value_, pos_))
222 if (0 != data && 0 == data->release())
227 lex_token& operator=(lex_token const& rhs)
230 if (0 != data && 0 == data->release())
241 operator token_id() const { return 0 != data ? token_id(*data) : T_EOI; }
242 string_type const &get_value() const { return data->get_value(); }
243 position_type const &get_position() const { return data->get_position(); }
244 bool is_eoi() const { return 0 == data || token_id(*data) == T_EOI; }
245 bool is_valid() const { return 0 != data && token_id(*data) != T_UNKNOWN; }
247 void set_token_id (token_id id_) { make_unique(); data->set_token_id(id_); }
248 void set_value (string_type const &value_) { make_unique(); data->set_value(value_); }
249 void set_position (position_type const &pos_) { make_unique(); data->set_position(pos_); }
251 friend bool operator== (lex_token const& lhs, lex_token const& rhs)
254 return 0 == lhs.data;
257 return *(lhs.data) == *(rhs.data);
261 #if BOOST_WAVE_DUMP_PARSE_TREE != 0
262 // access functions for the tree_to_xml functionality
263 static int get_token_id(lex_token const &t)
264 { return token_id(t); }
265 static string_type get_token_value(lex_token const &t)
266 { return t.get_value(); }
269 #if defined(BOOST_SPIRIT_DEBUG)
271 void print (std::ostream &stream) const
275 #endif // defined(BOOST_SPIRIT_DEBUG)
278 #if BOOST_WAVE_SERIALIZATION != 0
279 friend class boost::serialization::access;
280 template<typename Archive>
281 void serialize(Archive &ar, const unsigned int version)
283 data->serialize(ar, version);
287 // make a unique copy of the current object
290 if (1 == data->get_refcnt())
293 data_type* newdata = new data_type(*data) ;
295 data->release(); // release this reference, can't get zero
302 ///////////////////////////////////////////////////////////////////////////////
303 // This overload is needed by the multi_pass/functor_input_policy to
304 // validate a token instance. It has to be defined in the same namespace
305 // as the token class itself to allow ADL to find it.
306 ///////////////////////////////////////////////////////////////////////////////
307 template <typename Position>
309 token_is_valid(lex_token<Position> const& t)
314 ///////////////////////////////////////////////////////////////////////////////
315 #if defined(BOOST_SPIRIT_DEBUG)
316 template <typename PositionT>
317 inline std::ostream &
318 operator<< (std::ostream &stream, lex_token<PositionT> const &object)
320 object.print(stream);
323 #endif // defined(BOOST_SPIRIT_DEBUG)
325 ///////////////////////////////////////////////////////////////////////////////
326 } // namespace cpplexer
330 // the suffix header occurs after all of the code
331 #ifdef BOOST_HAS_ABI_HEADERS
332 #include BOOST_ABI_SUFFIX
335 #endif // !defined(CPP_TOKEN_HPP_53A13BD2_FBAA_444B_9B8B_FCB225C2BBA8_INCLUDED)