[857] | 1 | /*=============================================================================
|
---|
| 2 | Boost.Wave: A Standard compliant C++ preprocessor library
|
---|
| 3 |
|
---|
| 4 | A generic C++ lexer token definition
|
---|
| 5 |
|
---|
| 6 | http://www.boost.org/
|
---|
| 7 |
|
---|
| 8 | Copyright (c) 2001-2005 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 | =============================================================================*/
|
---|
| 12 |
|
---|
| 13 | #if !defined(CPP_TOKEN_HPP_53A13BD2_FBAA_444B_9B8B_FCB225C2BBA8_INCLUDED)
|
---|
| 14 | #define CPP_TOKEN_HPP_53A13BD2_FBAA_444B_9B8B_FCB225C2BBA8_INCLUDED
|
---|
| 15 |
|
---|
| 16 | #include <boost/wave/wave_config.hpp>
|
---|
| 17 | #include <boost/wave/util/file_position.hpp>
|
---|
| 18 | #include <boost/wave/token_ids.hpp>
|
---|
| 19 | #include <boost/wave/language_support.hpp>
|
---|
| 20 |
|
---|
| 21 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 22 | namespace boost {
|
---|
| 23 | namespace wave {
|
---|
| 24 | namespace cpplexer {
|
---|
| 25 |
|
---|
| 26 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 27 | // forward declaration of the token type
|
---|
| 28 | template <typename PositionT = boost::wave::util::file_position_type>
|
---|
| 29 | class lex_token;
|
---|
| 30 |
|
---|
| 31 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 32 | //
|
---|
| 33 | // lex_token
|
---|
| 34 | //
|
---|
| 35 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 36 |
|
---|
| 37 | template <typename PositionT>
|
---|
| 38 | class lex_token
|
---|
| 39 | {
|
---|
| 40 | public:
|
---|
| 41 | typedef BOOST_WAVE_STRINGTYPE string_type;
|
---|
| 42 | typedef PositionT position_type;
|
---|
| 43 |
|
---|
| 44 | lex_token()
|
---|
| 45 | : id(T_EOI)
|
---|
| 46 | {}
|
---|
| 47 |
|
---|
| 48 | lex_token(token_id id_, string_type const &value_, PositionT const &pos_)
|
---|
| 49 | : id(id_), value(value_), pos(pos_)
|
---|
| 50 | {}
|
---|
| 51 |
|
---|
| 52 | // accessors
|
---|
| 53 | operator token_id() const { return id; }
|
---|
| 54 | string_type const &get_value() const { return value; }
|
---|
| 55 | position_type const &get_position() const { return pos; }
|
---|
| 56 | void set_token_id (token_id id_) { id = id_; }
|
---|
| 57 | void set_value (string_type const &newval) { value = newval; }
|
---|
| 58 | void set_position (position_type const &pos_) { pos = pos_; }
|
---|
| 59 |
|
---|
| 60 | friend bool operator== (lex_token const& lhs, lex_token const& rhs)
|
---|
| 61 | {
|
---|
| 62 | // two tokens are considered equal even if they contain different
|
---|
| 63 | // positions
|
---|
| 64 | return (lhs.id == rhs.id && lhs.value == rhs.value) ? true : false;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | // debug support
|
---|
| 68 | #if BOOST_WAVE_DUMP_PARSE_TREE != 0
|
---|
| 69 | // access functions for the tree_to_xml functionality
|
---|
| 70 | static int get_token_id(lex_token const &t)
|
---|
| 71 | { return ID_FROM_TOKEN(token_id(t)); }
|
---|
| 72 | static string_type get_token_value(lex_token const &t)
|
---|
| 73 | { return t.get_value(); }
|
---|
| 74 | #endif
|
---|
| 75 |
|
---|
| 76 | #if defined(BOOST_SPIRIT_DEBUG)
|
---|
| 77 | // debug support
|
---|
| 78 | void print (std::ostream &stream) const
|
---|
| 79 | {
|
---|
| 80 | stream << get_token_name(id) << "(";
|
---|
| 81 | for (std::size_t i = 0; i < value.size(); ++i) {
|
---|
| 82 | switch (value[i]) {
|
---|
| 83 | case '\r': stream << "\\r"; break;
|
---|
| 84 | case '\n': stream << "\\n"; break;
|
---|
| 85 | default:
|
---|
| 86 | stream << value[i];
|
---|
| 87 | break;
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | stream << ")";
|
---|
| 91 | }
|
---|
| 92 | #endif // defined(BOOST_SPIRIT_DEBUG)
|
---|
| 93 |
|
---|
| 94 | private:
|
---|
| 95 | token_id id; // the token id
|
---|
| 96 | string_type value; // the text, which was parsed into this token
|
---|
| 97 | PositionT pos; // the original file position
|
---|
| 98 | };
|
---|
| 99 |
|
---|
| 100 | #if defined(BOOST_SPIRIT_DEBUG)
|
---|
| 101 | template <typename PositionT>
|
---|
| 102 | inline std::ostream &
|
---|
| 103 | operator<< (std::ostream &stream, lex_token<PositionT> const &object)
|
---|
| 104 | {
|
---|
| 105 | object.print(stream);
|
---|
| 106 | return stream;
|
---|
| 107 | }
|
---|
| 108 | #endif // defined(BOOST_SPIRIT_DEBUG)
|
---|
| 109 |
|
---|
| 110 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 111 | } // namespace cpplexer
|
---|
| 112 | } // namespace wave
|
---|
| 113 | } // namespace boost
|
---|
| 114 |
|
---|
| 115 | #endif // !defined(CPP_TOKEN_HPP_53A13BD2_FBAA_444B_9B8B_FCB225C2BBA8_INCLUDED)
|
---|