1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
/*=============================================================================
Boost.Wave: A Standard compliant C++ preprocessor library
A generic C++ lexer token definition
http://www.boost.org/
Copyright (c) 2001-2011 Hartmut Kaiser. Distributed under the Boost
Software License, Version 1.0. (See accompanying file
LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#if !defined(CPP_TOKEN_HPP_53A13BD2_FBAA_444B_9B8B_FCB225C2BBA8_INCLUDED)
#define CPP_TOKEN_HPP_53A13BD2_FBAA_444B_9B8B_FCB225C2BBA8_INCLUDED
#include <boost/wave/wave_config.hpp>
#if BOOST_WAVE_SERIALIZATION != 0
#include <boost/serialization/serialization.hpp>
#endif
#include <boost/wave/util/file_position.hpp>
#include <boost/wave/token_ids.hpp>
#include <boost/wave/language_support.hpp>
#include <boost/throw_exception.hpp>
#include <boost/pool/singleton_pool.hpp>
#include <boost/detail/atomic_count.hpp>
// this must occur after all of the includes and before any code appears
#ifdef BOOST_HAS_ABI_HEADERS
#include BOOST_ABI_PREFIX
#endif
///////////////////////////////////////////////////////////////////////////////
namespace boost {
namespace wave {
namespace cpplexer {
namespace impl {
template <typename StringTypeT, typename PositionT>
class token_data
{
public:
typedef StringTypeT string_type;
typedef PositionT position_type;
// default constructed tokens correspond to EOI tokens
token_data()
: id(T_EOI), refcnt(1)
{}
// construct an invalid token
explicit token_data(int)
: id(T_UNKNOWN), refcnt(1)
{}
token_data(token_id id_, string_type const &value_, position_type const &pos_)
: id(id_), value(value_), pos(pos_), refcnt(1)
{}
token_data(token_data const& rhs)
: id(rhs.id), value(rhs.value), pos(rhs.pos), refcnt(1)
{}
~token_data()
{}
std::size_t addref() { return ++refcnt; }
std::size_t release() { return --refcnt; }
std::size_t get_refcnt() const { return refcnt; }
// accessors
operator token_id() const { return id; }
string_type const &get_value() const { return value; }
position_type const &get_position() const { return pos; }
void set_token_id (token_id id_) { id = id_; }
void set_value (string_type const &value_) { value = value_; }
void set_position (position_type const &pos_) { pos = pos_; }
friend bool operator== (token_data const& lhs, token_data const& rhs)
{
// two tokens are considered equal even if they refer to different
// positions
return (lhs.id == rhs.id && lhs.value == rhs.value) ? true : false;
}
void init(token_id id_, string_type const &value_, position_type const &pos_)
{
BOOST_ASSERT(refcnt == 1);
id = id_;
value = value_;
pos = pos_;
}
void init(token_data const& rhs)
{
BOOST_ASSERT(refcnt == 1);
id = rhs.id;
value = rhs.value;
pos = rhs.pos;
}
static void *operator new(std::size_t size);
static void operator delete(void *p, std::size_t size);
#if defined(BOOST_SPIRIT_DEBUG)
// debug support
void print (std::ostream &stream) const
{
stream << get_token_name(id) << "(";
for (std::size_t i = 0; i < value.size(); ++i) {
switch (value[i]) {
case '\r': stream << "\\r"; break;
case '\n': stream << "\\n"; break;
default:
stream << value[i];
break;
}
}
stream << ")";
}
#endif // defined(BOOST_SPIRIT_DEBUG)
#if BOOST_WAVE_SERIALIZATION != 0
friend class boost::serialization::access;
template<typename Archive>
void serialize(Archive &ar, const unsigned int version)
{
using namespace boost::serialization;
ar & make_nvp("id", id);
ar & make_nvp("value", value);
ar & make_nvp("position", pos);
}
#endif
private:
token_id id; // the token id
string_type value; // the text, which was parsed into this token
position_type pos; // the original file position
boost::detail::atomic_count refcnt;
};
///////////////////////////////////////////////////////////////////////////////
struct token_data_tag {};
template <typename StringTypeT, typename PositionT>
inline void *
token_data<StringTypeT, PositionT>::operator new(std::size_t size)
{
BOOST_ASSERT(sizeof(token_data<StringTypeT, PositionT>) == size);
typedef boost::singleton_pool<
token_data_tag, sizeof(token_data<StringTypeT, PositionT>)
> pool_type;
void *ret = pool_type::malloc();
if (0 == ret)
boost::throw_exception(std::bad_alloc());
return ret;
}
template <typename StringTypeT, typename PositionT>
inline void
token_data<StringTypeT, PositionT>::operator delete(void *p, std::size_t size)
{
BOOST_ASSERT(sizeof(token_data<StringTypeT, PositionT>) == size);
typedef boost::singleton_pool<
token_data_tag, sizeof(token_data<StringTypeT, PositionT>)
> pool_type;
if (0 != p)
pool_type::free(p);
}
} // namespace impl
///////////////////////////////////////////////////////////////////////////////
// forward declaration of the token type
template <typename PositionT = boost::wave::util::file_position_type>
class lex_token;
///////////////////////////////////////////////////////////////////////////////
//
// lex_token
//
///////////////////////////////////////////////////////////////////////////////
template <typename PositionT>
class lex_token
{
public:
typedef BOOST_WAVE_STRINGTYPE string_type;
typedef PositionT position_type;
private:
typedef impl::token_data<string_type, position_type> data_type;
public:
// default constructed tokens correspond to EOI tokens
lex_token()
: data(0)
{}
// construct an invalid token
explicit lex_token(int)
: data(new data_type(0))
{}
lex_token(lex_token const& rhs)
: data(rhs.data)
{
if (0 != data)
data->addref();
}
lex_token(token_id id_, string_type const &value_, PositionT const &pos_)
: data(new data_type(id_, value_, pos_))
{}
~lex_token()
{
if (0 != data && 0 == data->release())
delete data;
data = 0;
}
lex_token& operator=(lex_token const& rhs)
{
if (&rhs != this) {
if (0 != data && 0 == data->release())
delete data;
data = rhs.data;
if (0 != data)
data->addref();
}
return *this;
}
// accessors
operator token_id() const { return 0 != data ? token_id(*data) : T_EOI; }
string_type const &get_value() const { return data->get_value(); }
position_type const &get_position() const { return data->get_position(); }
bool is_eoi() const { return 0 == data || token_id(*data) == T_EOI; }
bool is_valid() const { return 0 != data && token_id(*data) != T_UNKNOWN; }
void set_token_id (token_id id_) { make_unique(); data->set_token_id(id_); }
void set_value (string_type const &value_) { make_unique(); data->set_value(value_); }
void set_position (position_type const &pos_) { make_unique(); data->set_position(pos_); }
friend bool operator== (lex_token const& lhs, lex_token const& rhs)
{
if (0 == rhs.data)
return 0 == lhs.data;
if (0 == lhs.data)
return false;
return *(lhs.data) == *(rhs.data);
}
// debug support
#if BOOST_WAVE_DUMP_PARSE_TREE != 0
// access functions for the tree_to_xml functionality
static int get_token_id(lex_token const &t)
{ return token_id(t); }
static string_type get_token_value(lex_token const &t)
{ return t.get_value(); }
#endif
#if defined(BOOST_SPIRIT_DEBUG)
// debug support
void print (std::ostream &stream) const
{
data->print(stream);
}
#endif // defined(BOOST_SPIRIT_DEBUG)
private:
#if BOOST_WAVE_SERIALIZATION != 0
friend class boost::serialization::access;
template<typename Archive>
void serialize(Archive &ar, const unsigned int version)
{
data->serialize(ar, version);
}
#endif
// make a unique copy of the current object
void make_unique()
{
if (1 == data->get_refcnt())
return;
data_type* newdata = new data_type(*data) ;
data->release(); // release this reference, can't get zero
data = newdata;
}
data_type* data;
};
///////////////////////////////////////////////////////////////////////////////
// This overload is needed by the multi_pass/functor_input_policy to
// validate a token instance. It has to be defined in the same namespace
// as the token class itself to allow ADL to find it.
///////////////////////////////////////////////////////////////////////////////
template <typename Position>
inline bool
token_is_valid(lex_token<Position> const& t)
{
return t.is_valid();
}
///////////////////////////////////////////////////////////////////////////////
#if defined(BOOST_SPIRIT_DEBUG)
template <typename PositionT>
inline std::ostream &
operator<< (std::ostream &stream, lex_token<PositionT> const &object)
{
object.print(stream);
return stream;
}
#endif // defined(BOOST_SPIRIT_DEBUG)
///////////////////////////////////////////////////////////////////////////////
} // namespace cpplexer
} // namespace wave
} // namespace boost
// the suffix header occurs after all of the code
#ifdef BOOST_HAS_ABI_HEADERS
#include BOOST_ABI_SUFFIX
#endif
#endif // !defined(CPP_TOKEN_HPP_53A13BD2_FBAA_444B_9B8B_FCB225C2BBA8_INCLUDED)
|