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-2007 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/assert.hpp> |
---|
20 | #include <boost/spirit/version.hpp> |
---|
21 | #include <boost/spirit/iterator/position_iterator.hpp> |
---|
22 | #include <boost/wave/wave_config.hpp> |
---|
23 | #if BOOST_WAVE_SERIALIZATION != 0 |
---|
24 | #include <boost/serialization/serialization.hpp> |
---|
25 | #endif |
---|
26 | |
---|
27 | // this must occur after all of the includes and before any code appears |
---|
28 | #ifdef BOOST_HAS_ABI_HEADERS |
---|
29 | #include BOOST_ABI_PREFIX |
---|
30 | #endif |
---|
31 | |
---|
32 | /////////////////////////////////////////////////////////////////////////////// |
---|
33 | namespace boost { |
---|
34 | namespace wave { |
---|
35 | namespace util { |
---|
36 | |
---|
37 | /////////////////////////////////////////////////////////////////////////////// |
---|
38 | namespace debug { |
---|
39 | |
---|
40 | // Used only when BOOST_ASSERT expands to something |
---|
41 | // make sure the string literal does not contain any escapes ('\\' just |
---|
42 | // before '\\', '\"' or '?') |
---|
43 | template <typename StringT> |
---|
44 | inline bool |
---|
45 | is_escaped_lit(StringT const &value) |
---|
46 | { |
---|
47 | typename StringT::size_type pos = value.find_first_of ("\\", 0); |
---|
48 | if (StringT::npos != pos) { |
---|
49 | do { |
---|
50 | if ('\\' == value[pos+1] || |
---|
51 | '\"' == value[pos+1] || |
---|
52 | '?' == value[pos+1]) |
---|
53 | { |
---|
54 | return true; |
---|
55 | } |
---|
56 | else { |
---|
57 | pos = value.find_first_of ("\\", pos+1); |
---|
58 | } |
---|
59 | } while (pos != StringT::npos); |
---|
60 | } |
---|
61 | return false; |
---|
62 | } |
---|
63 | |
---|
64 | /////////////////////////////////////////////////////////////////////////////// |
---|
65 | } // namespace debug |
---|
66 | |
---|
67 | /////////////////////////////////////////////////////////////////////////////// |
---|
68 | // |
---|
69 | // file_position |
---|
70 | // |
---|
71 | // A structure to hold positional information. This includes the filename, |
---|
72 | // line number and column number of a current token position. |
---|
73 | // |
---|
74 | /////////////////////////////////////////////////////////////////////////////// |
---|
75 | |
---|
76 | template <typename StringT> |
---|
77 | struct file_position { |
---|
78 | |
---|
79 | public: |
---|
80 | typedef StringT string_type; |
---|
81 | |
---|
82 | file_position() |
---|
83 | : file(), line(1), column(1) |
---|
84 | {} |
---|
85 | explicit file_position(string_type const& file_, int line_ = 1, |
---|
86 | int column_ = 1) |
---|
87 | : file(file_), line(line_), column(column_) |
---|
88 | { |
---|
89 | BOOST_ASSERT(!debug::is_escaped_lit(file)); |
---|
90 | } |
---|
91 | |
---|
92 | // accessors |
---|
93 | string_type const &get_file() const { return file; } |
---|
94 | unsigned int get_line() const { return line; } |
---|
95 | unsigned int get_column() const { return column; } |
---|
96 | |
---|
97 | void set_file(string_type const &file_) |
---|
98 | { |
---|
99 | file = file_; |
---|
100 | BOOST_ASSERT(!debug::is_escaped_lit(file)); |
---|
101 | } |
---|
102 | void set_line(unsigned int line_) { line = line_; } |
---|
103 | void set_column(unsigned int column_) { column = column_; } |
---|
104 | |
---|
105 | private: |
---|
106 | #if BOOST_WAVE_SERIALIZATION != 0 |
---|
107 | friend class boost::serialization::access; |
---|
108 | template<typename Archive> |
---|
109 | void serialize(Archive &ar, const unsigned int version) |
---|
110 | { |
---|
111 | ar & file; |
---|
112 | ar & line; |
---|
113 | ar & column; |
---|
114 | } |
---|
115 | #endif |
---|
116 | |
---|
117 | string_type file; |
---|
118 | unsigned int line; |
---|
119 | unsigned int column; |
---|
120 | }; |
---|
121 | |
---|
122 | template <typename StringT> |
---|
123 | bool operator== (file_position<StringT> const &lhs, |
---|
124 | file_position<StringT> const &rhs) |
---|
125 | { |
---|
126 | return lhs.get_column() == rhs.get_column() && |
---|
127 | lhs.get_line() == rhs.get_line() && lhs.get_file() == rhs.get_file(); |
---|
128 | } |
---|
129 | |
---|
130 | template <typename StringT> |
---|
131 | inline std::ostream & |
---|
132 | operator<< (std::ostream &o, file_position<StringT> const &pos) |
---|
133 | { |
---|
134 | o << pos.get_file() << ":" << pos.get_line() << ":" << pos.get_column(); |
---|
135 | return o; |
---|
136 | } |
---|
137 | |
---|
138 | typedef file_position<BOOST_WAVE_STRINGTYPE> file_position_type; |
---|
139 | |
---|
140 | /////////////////////////////////////////////////////////////////////////////// |
---|
141 | // |
---|
142 | // position_iterator |
---|
143 | // |
---|
144 | // The position_iterator used by Wave is now based on the corresponding Spirit |
---|
145 | // type. This type is used with our own file_position though. The needed |
---|
146 | // specialization of the boost::spirit::position_policy class is provided |
---|
147 | // below. |
---|
148 | // |
---|
149 | /////////////////////////////////////////////////////////////////////////////// |
---|
150 | |
---|
151 | template <typename IteratorT, typename PositionT> |
---|
152 | struct position_iterator |
---|
153 | : boost::spirit::position_iterator<IteratorT, PositionT> |
---|
154 | { |
---|
155 | typedef boost::spirit::position_iterator<IteratorT, PositionT> base_type; |
---|
156 | |
---|
157 | position_iterator() |
---|
158 | { |
---|
159 | } |
---|
160 | |
---|
161 | position_iterator(IteratorT const &begin, IteratorT const &end, |
---|
162 | PositionT const &pos) |
---|
163 | : base_type(begin, end, pos) |
---|
164 | { |
---|
165 | } |
---|
166 | }; |
---|
167 | |
---|
168 | /////////////////////////////////////////////////////////////////////////////// |
---|
169 | } // namespace util |
---|
170 | } // namespace wave |
---|
171 | |
---|
172 | /////////////////////////////////////////////////////////////////////////////// |
---|
173 | |
---|
174 | #if SPIRIT_VERSION >= 0x1700 |
---|
175 | |
---|
176 | namespace spirit { |
---|
177 | |
---|
178 | /////////////////////////////////////////////////////////////////////////////// |
---|
179 | // |
---|
180 | // The boost::spirit::position_policy has to be specialized for our |
---|
181 | // file_position class |
---|
182 | // |
---|
183 | /////////////////////////////////////////////////////////////////////////////// |
---|
184 | |
---|
185 | template <> |
---|
186 | class position_policy<boost::wave::util::file_position_type> { |
---|
187 | |
---|
188 | public: |
---|
189 | position_policy() |
---|
190 | : m_CharsPerTab(4) |
---|
191 | {} |
---|
192 | |
---|
193 | void next_line(boost::wave::util::file_position_type &pos) |
---|
194 | { |
---|
195 | pos.set_line(pos.get_line() + 1); |
---|
196 | pos.set_column(1); |
---|
197 | } |
---|
198 | |
---|
199 | void set_tab_chars(unsigned int chars) |
---|
200 | { |
---|
201 | m_CharsPerTab = chars; |
---|
202 | } |
---|
203 | |
---|
204 | void next_char(boost::wave::util::file_position_type &pos) |
---|
205 | { |
---|
206 | pos.set_column(pos.get_column() + 1); |
---|
207 | } |
---|
208 | |
---|
209 | void tabulation(boost::wave::util::file_position_type &pos) |
---|
210 | { |
---|
211 | pos.set_column(pos.get_column() + m_CharsPerTab - |
---|
212 | (pos.get_column() - 1) % m_CharsPerTab); |
---|
213 | } |
---|
214 | |
---|
215 | private: |
---|
216 | unsigned int m_CharsPerTab; |
---|
217 | }; |
---|
218 | |
---|
219 | /////////////////////////////////////////////////////////////////////////////// |
---|
220 | } // namespace spirit |
---|
221 | |
---|
222 | #endif // SPIRIT_VERSION >= 0x1700 |
---|
223 | |
---|
224 | } // namespace boost |
---|
225 | |
---|
226 | // the suffix header occurs after all of the code |
---|
227 | #ifdef BOOST_HAS_ABI_HEADERS |
---|
228 | #include BOOST_ABI_SUFFIX |
---|
229 | #endif |
---|
230 | |
---|
231 | #endif // !defined(FILE_POSITION_H_52BDEDF7_DAD3_4F24_802F_E66BB8098F68_INCLUDED) |
---|