1 /*=============================================================================
2 Copyright (c) 2002 Juan Carlos Arevalo-Baeza
3 Copyright (c) 2002-2006 Hartmut Kaiser
4 Copyright (c) 2003 Giovanni Bajo
5 http://spirit.sourceforge.net/
7 Distributed under the Boost Software License, Version 1.0. (See accompanying
8 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 =============================================================================*/
10 #ifndef BOOST_SPIRIT_POSITION_ITERATOR_HPP
11 #define BOOST_SPIRIT_POSITION_ITERATOR_HPP
14 #include <boost/config.hpp>
15 #include <boost/concept_check.hpp>
17 #include <boost/spirit/home/classic/namespace.hpp>
18 #include <boost/spirit/home/classic/iterator/position_iterator_fwd.hpp>
20 namespace boost { namespace spirit {
22 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
24 ///////////////////////////////////////////////////////////////////////////////
26 // file_position_without_column
28 // A structure to hold positional information. This includes the file,
29 // and the line number
31 ///////////////////////////////////////////////////////////////////////////////
32 template <typename String>
33 struct file_position_without_column_base {
37 file_position_without_column_base(String const& file_ = String(),
43 bool operator==(const file_position_without_column_base& fp) const
44 { return line == fp.line && file == fp.file; }
47 ///////////////////////////////////////////////////////////////////////////////
51 // This structure holds complete file position, including file name,
52 // line and column number
54 ///////////////////////////////////////////////////////////////////////////////
55 template <typename String>
56 struct file_position_base : public file_position_without_column_base<String> {
59 file_position_base(String const& file_ = String(),
60 int line_ = 1, int column_ = 1):
61 file_position_without_column_base<String> (file_, line_),
65 bool operator==(const file_position_base& fp) const
66 { return column == fp.column && this->line == fp.line && this->file == fp.file; }
69 ///////////////////////////////////////////////////////////////////////////////
73 // This template is the policy to handle the file position. It is specialized
74 // on the position type. Providing a custom file_position also requires
75 // providing a specialization of this class.
79 // Default constructor of the custom position class must be accessible.
80 // set_tab_chars(unsigned int chars) - Set the tabstop width
81 // next_char(PositionT& pos) - Notify that a new character has been
83 // tabulation(PositionT& pos) - Notify that a tab character has been
85 // next_line(PositionT& pos) - Notify that a new line delimiter has
88 ///////////////////////////////////////////////////////////////////////////////
89 template <typename PositionT> class position_policy;
91 ///////////////////////////////////////////////////////////////////////////////
92 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
94 }} /* namespace BOOST_SPIRIT_CLASSIC_NS */
97 // This must be included here for full compatibility with old MSVC
98 #include <boost/spirit/home/classic/iterator/impl/position_iterator.ipp>
100 ///////////////////////////////////////////////////////////////////////////////
101 namespace boost { namespace spirit {
103 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
105 ///////////////////////////////////////////////////////////////////////////////
109 // It wraps an iterator, and keeps track of the current position in the input,
110 // as it gets incremented.
112 // The wrapped iterator must be at least a Forward iterator. The position
113 // iterator itself will always be a non-mutable Forward iterator.
115 // In order to have begin/end iterators constructed, the end iterator must be
116 // empty constructed. Similar to what happens with stream iterators. The begin
117 // iterator must be constructed from both, the begin and end iterators of the
118 // wrapped iterator type. This is necessary to implement the lookahead of
119 // characters necessary to parse CRLF sequences.
121 // In order to extract the current positional data from the iterator, you may
122 // use the get_position member function.
124 // You can also use the set_position member function to reset the current
125 // position to something new.
127 // The structure that holds the current position can be customized through a
128 // template parameter, and the class position_policy must be specialized
129 // on the new type to define how to handle it. Currently, it's possible
130 // to choose between the file_position and file_position_without_column
131 // (which saves some overhead if managing current column is not required).
133 ///////////////////////////////////////////////////////////////////////////////
135 #if !defined(BOOST_ITERATOR_ADAPTORS_VERSION) || \
136 BOOST_ITERATOR_ADAPTORS_VERSION < 0x0200
137 #error "Please use at least Boost V1.31.0 while compiling the position_iterator class!"
138 #else // BOOST_ITERATOR_ADAPTORS_VERSION < 0x0200
140 ///////////////////////////////////////////////////////////////////////////////
142 // Uses the newer iterator_adaptor version (should be released with
145 ///////////////////////////////////////////////////////////////////////////////
147 typename ForwardIteratorT,
151 class position_iterator
152 : public iterator_::impl::position_iterator_base_generator<
157 public position_policy<PositionT>
161 typedef position_policy<PositionT> position_policy_t;
162 typedef typename iterator_::impl::position_iterator_base_generator<
167 typedef typename iterator_::impl::position_iterator_base_generator<
171 >::main_iter_t main_iter_t;
175 typedef PositionT position_t;
182 const ForwardIteratorT& begin,
183 const ForwardIteratorT& end)
184 : base_t(begin), _end(end), _pos(PositionT()), _isend(begin == end)
187 template <typename FileNameT>
189 const ForwardIteratorT& begin,
190 const ForwardIteratorT& end,
192 : base_t(begin), _end(end), _pos(PositionT(fileName)),
196 template <typename FileNameT, typename LineT>
198 const ForwardIteratorT& begin,
199 const ForwardIteratorT& end,
200 FileNameT fileName, LineT line)
201 : base_t(begin), _end(end), _pos(PositionT(fileName, line)),
205 template <typename FileNameT, typename LineT, typename ColumnT>
207 const ForwardIteratorT& begin,
208 const ForwardIteratorT& end,
209 FileNameT fileName, LineT line, ColumnT column)
210 : base_t(begin), _end(end), _pos(PositionT(fileName, line, column)),
215 const ForwardIteratorT& begin,
216 const ForwardIteratorT& end,
217 const PositionT& pos)
218 : base_t(begin), _end(end), _pos(pos), _isend(begin == end)
221 position_iterator(const position_iterator& iter)
222 : base_t(iter.base()), position_policy_t(iter),
223 _end(iter._end), _pos(iter._pos), _isend(iter._isend)
226 position_iterator& operator=(const position_iterator& iter)
228 base_t::operator=(iter);
229 position_policy_t::operator=(iter);
232 _isend = iter._isend;
236 void set_position(PositionT const& newpos) { _pos = newpos; }
237 PositionT& get_position() { return _pos; }
238 PositionT const& get_position() const { return _pos; }
240 void set_tabchars(unsigned int chars)
242 // This function (which comes from the position_policy) has a
243 // different name on purpose, to avoid messing with using
244 // declarations or qualified calls to access the base template
245 // function, which might break some compilers.
246 this->position_policy_t::set_tab_chars(chars);
250 friend class boost::iterator_core_access;
254 typename base_t::reference val = *(this->base());
256 ++this->base_reference();
257 this->next_line(_pos);
258 static_cast<main_iter_t &>(*this).newline();
260 else if ( val == '\r') {
261 ++this->base_reference();
262 if (this->base_reference() == _end || *(this->base()) != '\n')
264 this->next_line(_pos);
265 static_cast<main_iter_t &>(*this).newline();
268 else if (val == '\t') {
269 this->tabulation(_pos);
270 ++this->base_reference();
273 this->next_char(_pos);
274 ++this->base_reference();
277 // The iterator is at the end only if it's the same
279 _isend = (this->base_reference() == _end);
283 typename OtherDerivedT, typename OtherIteratorT,
284 typename V, typename C, typename R, typename D
286 bool equal(iterator_adaptor<OtherDerivedT, OtherIteratorT, V, C, R, D>
289 OtherDerivedT const &rhs = static_cast<OtherDerivedT const &>(x);
290 bool x_is_end = rhs._isend;
292 return (_isend == x_is_end) && (_isend || this->base() == rhs.base());
300 ForwardIteratorT _end;
305 #endif // BOOST_ITERATOR_ADAPTORS_VERSION < 0x0200
307 ///////////////////////////////////////////////////////////////////////////////
309 // position_iterator2
311 // Equivalent to position_iterator, but it is able to extract the current
312 // line into a string. This is very handy for error reports.
314 // Notice that the footprint of this class is higher than position_iterator,
315 // (how much depends on how bulky the underlying iterator is), so it should
316 // be used only if necessary.
318 ///////////////////////////////////////////////////////////////////////////////
322 typename ForwardIteratorT,
325 class position_iterator2
326 : public position_iterator
330 position_iterator2<ForwardIteratorT, PositionT>
333 typedef position_iterator
337 position_iterator2<ForwardIteratorT, PositionT> // JDG 4-15-03
341 typedef typename base_t::value_type value_type;
342 typedef PositionT position_t;
348 const ForwardIteratorT& begin,
349 const ForwardIteratorT& end):
354 template <typename FileNameT>
356 const ForwardIteratorT& begin,
357 const ForwardIteratorT& end,
359 base_t(begin, end, file),
363 template <typename FileNameT, typename LineT>
365 const ForwardIteratorT& begin,
366 const ForwardIteratorT& end,
367 FileNameT file, LineT line):
368 base_t(begin, end, file, line),
372 template <typename FileNameT, typename LineT, typename ColumnT>
374 const ForwardIteratorT& begin,
375 const ForwardIteratorT& end,
376 FileNameT file, LineT line, ColumnT column):
377 base_t(begin, end, file, line, column),
382 const ForwardIteratorT& begin,
383 const ForwardIteratorT& end,
384 const PositionT& pos):
385 base_t(begin, end, pos),
389 position_iterator2(const position_iterator2& iter)
390 : base_t(iter), _startline(iter._startline)
393 position_iterator2& operator=(const position_iterator2& iter)
395 base_t::operator=(iter);
396 _startline = iter._startline;
400 ForwardIteratorT get_currentline_begin(void) const
401 { return _startline; }
403 ForwardIteratorT get_currentline_end(void) const
404 { return get_endline(); }
406 std::basic_string<value_type> get_currentline(void) const
408 return std::basic_string<value_type>
409 (get_currentline_begin(), get_currentline_end());
413 ForwardIteratorT _startline;
415 friend class position_iterator<ForwardIteratorT, PositionT,
416 position_iterator2<ForwardIteratorT, PositionT> >;
418 ForwardIteratorT get_endline() const
420 ForwardIteratorT endline = _startline;
421 while (endline != this->_end && *endline != '\r' && *endline != '\n')
429 { _startline = this->base(); }
432 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
434 }} // namespace BOOST_SPIRIT_CLASSIC_NS