slex_token.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*=============================================================================
  2. Boost.Wave: A Standard compliant C++ preprocessor library
  3. A generic C++ lexer token definition
  4. http://www.boost.org/
  5. Copyright (c) 2001-2012 Hartmut Kaiser. Distributed under the Boost
  6. Software License, Version 1.0. (See accompanying file
  7. LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. =============================================================================*/
  9. #if !defined(SLEX_TOKEN_HPP_53A13BD2_FBAA_444B_9B8B_FCB225C2BBA8_INCLUDED)
  10. #define SLEX_TOKEN_HPP_53A13BD2_FBAA_444B_9B8B_FCB225C2BBA8_INCLUDED
  11. #include <iomanip>
  12. #include <ios>
  13. #include <boost/wave/wave_config.hpp>
  14. #include <boost/wave/token_ids.hpp>
  15. #include <boost/wave/language_support.hpp>
  16. #include <boost/wave/util/file_position.hpp>
  17. ///////////////////////////////////////////////////////////////////////////////
  18. namespace boost {
  19. namespace wave {
  20. namespace cpplexer {
  21. ///////////////////////////////////////////////////////////////////////////////
  22. // forward declaration of the token type
  23. template <typename PositionT = boost::wave::util::file_position_type>
  24. class slex_token;
  25. ///////////////////////////////////////////////////////////////////////////////
  26. //
  27. // lex_token
  28. //
  29. ///////////////////////////////////////////////////////////////////////////////
  30. template <typename PositionT>
  31. class slex_token
  32. {
  33. public:
  34. typedef BOOST_WAVE_STRINGTYPE string_type;
  35. typedef PositionT position_type;
  36. slex_token()
  37. : id(T_EOI)
  38. {}
  39. // construct an invalid token
  40. explicit slex_token(int)
  41. : id(T_UNKNOWN)
  42. {}
  43. slex_token(token_id id_, string_type const &value_, PositionT const &pos_)
  44. : id(id_), value(value_), pos(pos_)
  45. {}
  46. // accessors
  47. operator token_id() const { return id; }
  48. string_type const &get_value() const { return value; }
  49. position_type const &get_position() const { return pos; }
  50. bool is_eoi() const { return id == T_EOI; }
  51. bool is_valid() const { return id != T_UNKNOWN; }
  52. void set_token_id (token_id id_) { id = id_; }
  53. void set_value (string_type const &newval) { value = newval; }
  54. void set_position (position_type const &pos_) { pos = pos_; }
  55. friend bool operator== (slex_token const& lhs, slex_token const& rhs)
  56. {
  57. // two tokens are considered equal even if they contain different
  58. // positions
  59. return (lhs.id == rhs.id && lhs.value == rhs.value) ? true : false;
  60. }
  61. // debug support
  62. #if BOOST_WAVE_DUMP_PARSE_TREE != 0
  63. // access functions for the tree_to_xml functionality
  64. static int get_token_id(slex_token const &t)
  65. { return ID_FROM_TOKEN(token_id(t)); }
  66. static string_type get_token_value(slex_token const &t)
  67. { return t.get_value(); }
  68. #endif
  69. // print support
  70. void print (std::ostream &stream) const
  71. {
  72. using namespace std;
  73. using namespace boost::wave;
  74. stream << std::setw(16)
  75. << std::left << boost::wave::get_token_name(id) << " ("
  76. << "#" << token_id(BASEID_FROM_TOKEN(*this))
  77. << ") at " << get_position().get_file() << " ("
  78. << std::setw(3) << std::right << get_position().get_line() << "/"
  79. << std::setw(2) << std::right << get_position().get_column()
  80. << "): >";
  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. case '\t': stream << "\\t"; break;
  86. default:
  87. stream << value[i];
  88. break;
  89. }
  90. }
  91. stream << "<";
  92. }
  93. private:
  94. boost::wave::token_id id; // the token id
  95. string_type value; // the text, which was parsed into this token
  96. PositionT pos; // the original file position
  97. };
  98. template <typename PositionT>
  99. inline std::ostream &
  100. operator<< (std::ostream &stream, slex_token<PositionT> const &object)
  101. {
  102. object.print(stream);
  103. return stream;
  104. }
  105. ///////////////////////////////////////////////////////////////////////////////
  106. // This overload is needed by the multi_pass/functor_input_policy to
  107. // validate a token instance. It has to be defined in the same namespace
  108. // as the token class itself to allow ADL to find it.
  109. ///////////////////////////////////////////////////////////////////////////////
  110. template <typename Position>
  111. inline bool
  112. token_is_valid(slex_token<Position> const& t)
  113. {
  114. return t.is_valid();
  115. }
  116. ///////////////////////////////////////////////////////////////////////////////
  117. } // namespace cpplexer
  118. } // namespace wave
  119. } // namespace boost
  120. #endif // !defined(SLEX_TOKEN_HPP_53A13BD2_FBAA_444B_9B8B_FCB225C2BBA8_INCLUDED)