| 1 | /*============================================================================= |
|---|
| 2 | Boost.Wave: A Standard compliant C++ preprocessor library |
|---|
| 3 | |
|---|
| 4 | Definition of the position_iterator and file_position templates |
|---|
| 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(FILE_POSITION_H_52BDEDF7_DAD3_4F24_802F_E66BB8098F68_INCLUDED) |
|---|
| 14 | #define FILE_POSITION_H_52BDEDF7_DAD3_4F24_802F_E66BB8098F68_INCLUDED |
|---|
| 15 | |
|---|
| 16 | #include <string> |
|---|
| 17 | #include <ostream> |
|---|
| 18 | |
|---|
| 19 | #include <boost/spirit/version.hpp> |
|---|
| 20 | #include <boost/spirit/iterator/position_iterator.hpp> |
|---|
| 21 | #include <boost/wave/wave_config.hpp> |
|---|
| 22 | |
|---|
| 23 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 24 | namespace boost { |
|---|
| 25 | namespace wave { |
|---|
| 26 | namespace util { |
|---|
| 27 | |
|---|
| 28 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 29 | // |
|---|
| 30 | // file_position |
|---|
| 31 | // |
|---|
| 32 | // A structure to hold positional information. This includes the filename, |
|---|
| 33 | // line number and column number of a current token position. |
|---|
| 34 | // |
|---|
| 35 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 36 | |
|---|
| 37 | template <typename StringT> |
|---|
| 38 | struct file_position { |
|---|
| 39 | |
|---|
| 40 | public: |
|---|
| 41 | typedef StringT string_type; |
|---|
| 42 | |
|---|
| 43 | file_position() |
|---|
| 44 | : file(), line(1), column(1) |
|---|
| 45 | {} |
|---|
| 46 | explicit file_position(string_type const& file_, int line_ = 1, |
|---|
| 47 | int column_ = 1) |
|---|
| 48 | : file(file_), line(line_), column(column_) |
|---|
| 49 | {} |
|---|
| 50 | |
|---|
| 51 | // accessors |
|---|
| 52 | string_type const &get_file() const { return file; } |
|---|
| 53 | unsigned int get_line() const { return line; } |
|---|
| 54 | unsigned int get_column() const { return column; } |
|---|
| 55 | |
|---|
| 56 | void set_file(string_type const &file_) { file = file_; } |
|---|
| 57 | void set_line(unsigned int line_) { line = line_; } |
|---|
| 58 | void set_column(unsigned int column_) { column = column_; } |
|---|
| 59 | |
|---|
| 60 | private: |
|---|
| 61 | string_type file; |
|---|
| 62 | unsigned int line; |
|---|
| 63 | unsigned int column; |
|---|
| 64 | }; |
|---|
| 65 | |
|---|
| 66 | template <typename StringT> |
|---|
| 67 | bool operator== (file_position<StringT> const &lhs, |
|---|
| 68 | file_position<StringT> const &rhs) |
|---|
| 69 | { |
|---|
| 70 | return lhs.get_column() == rhs.get_column() && |
|---|
| 71 | lhs.get_line() == rhs.get_line() && lhs.get_file() == rhs.get_file(); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | template <typename StringT> |
|---|
| 75 | inline std::ostream & |
|---|
| 76 | operator<< (std::ostream &o, file_position<StringT> const &pos) |
|---|
| 77 | { |
|---|
| 78 | o << pos.get_file() << "(" << pos.get_line() << ")"; |
|---|
| 79 | return o; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | typedef file_position<BOOST_WAVE_STRINGTYPE> file_position_type; |
|---|
| 83 | |
|---|
| 84 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 85 | // |
|---|
| 86 | // position_iterator |
|---|
| 87 | // |
|---|
| 88 | // The position_iterator used by Wave is now based on the corresponding Spirit |
|---|
| 89 | // type. This type is used with our own file_position though. The needed |
|---|
| 90 | // specialization of the boost::spirit::position_policy class is provided |
|---|
| 91 | // below. |
|---|
| 92 | // |
|---|
| 93 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 94 | |
|---|
| 95 | template <typename IteratorT, typename PositionT> |
|---|
| 96 | struct position_iterator |
|---|
| 97 | : boost::spirit::position_iterator<IteratorT, PositionT> |
|---|
| 98 | { |
|---|
| 99 | typedef boost::spirit::position_iterator<IteratorT, PositionT> base_type; |
|---|
| 100 | |
|---|
| 101 | position_iterator() |
|---|
| 102 | { |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | position_iterator(IteratorT const &begin, IteratorT const &end, |
|---|
| 106 | PositionT const &pos) |
|---|
| 107 | : base_type(begin, end, pos) |
|---|
| 108 | { |
|---|
| 109 | } |
|---|
| 110 | }; |
|---|
| 111 | |
|---|
| 112 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 113 | } // namespace util |
|---|
| 114 | } // namespace wave |
|---|
| 115 | |
|---|
| 116 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 117 | |
|---|
| 118 | #if SPIRIT_VERSION >= 0x1700 |
|---|
| 119 | |
|---|
| 120 | namespace spirit { |
|---|
| 121 | |
|---|
| 122 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 123 | // |
|---|
| 124 | // The boost::spirit::position_policy has to be specialized for our |
|---|
| 125 | // file_position class |
|---|
| 126 | // |
|---|
| 127 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 128 | |
|---|
| 129 | template <> |
|---|
| 130 | class position_policy<boost::wave::util::file_position_type> { |
|---|
| 131 | |
|---|
| 132 | public: |
|---|
| 133 | position_policy() |
|---|
| 134 | : m_CharsPerTab(4) |
|---|
| 135 | {} |
|---|
| 136 | |
|---|
| 137 | void next_line(boost::wave::util::file_position_type &pos) |
|---|
| 138 | { |
|---|
| 139 | pos.set_line(pos.get_line() + 1); |
|---|
| 140 | pos.set_column(1); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | void set_tab_chars(unsigned int chars) |
|---|
| 144 | { |
|---|
| 145 | m_CharsPerTab = chars; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | void next_char(boost::wave::util::file_position_type &pos) |
|---|
| 149 | { |
|---|
| 150 | pos.set_column(pos.get_column() + 1); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | void tabulation(boost::wave::util::file_position_type &pos) |
|---|
| 154 | { |
|---|
| 155 | pos.set_column(pos.get_column() + m_CharsPerTab - |
|---|
| 156 | (pos.get_column() - 1) % m_CharsPerTab); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | private: |
|---|
| 160 | unsigned int m_CharsPerTab; |
|---|
| 161 | }; |
|---|
| 162 | |
|---|
| 163 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 164 | } // namespace spirit |
|---|
| 165 | |
|---|
| 166 | #endif // SPIRIT_VERSION >= 0x1700 |
|---|
| 167 | |
|---|
| 168 | } // namespace boost |
|---|
| 169 | |
|---|
| 170 | #endif // !defined(FILE_POSITION_H_52BDEDF7_DAD3_4F24_802F_E66BB8098F68_INCLUDED) |
|---|