Check-in [ca08e3a704]

Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
SHA1 Hash:ca08e3a704640f5ad6dac399b36c97df70952dc7
Date: 2008-06-21 10:57:43
User: stephan
Comment:added missing include
Tags And Properties
Changes
hide diffs unified diffs patch

Changes to parsepp.hpp

Old (4a4cc623aaa9b4b9) New (fde3b53ed62d8cea)
1 #ifndef s11n_net_PARSEPP_HPP_INCLUDED 1 #ifndef s11n_net_PARSEPP_HPP_INCLUDED
2 #define s11n_net_PARSEPP_HPP_INCLUDED 2 #define s11n_net_PARSEPP_HPP_INCLUDED
3 3
4 #include <map> 4 #include <map>
5 #include <string> 5 #include <string>
6 #include <cassert> 6 #include <cassert>
7 #include <iostream> 7 #include <iostream>
8 #include <sstream> 8 #include <sstream>
> 9 #include <stdexcept>
9 #include <vector> 10 #include <vector>
10 #include <list> 11 #include <list>
11 #include <set> 12 #include <set>
12 13
13 #include "parsepp_typelist.hpp" 14 #include "parsepp_typelist.hpp"
175 hidden lines
189 - parse_iterators are read-only iterators. They cannot be used 190 - parse_iterators are read-only iterators. They cannot be used
190 to change their input. 191 to change their input.
191 */ 192 */
192 class parse_iterator : public std::iterator<std::input_iterator_tag,const std::string::value_type> 193 class parse_iterator : public std::iterator<std::input_iterator_tag,const std::string::value_type>
193 { 194 {
> 195 private:
> 196 static const int start_column = 1;
194 public: 197 public:
195 typedef std::string::const_iterator iterator; 198 typedef std::string::const_iterator iterator;
196 /** 199 /**
197 Constructs an empty iterator, useful only as the target 200 Constructs an empty iterator, useful only as the target
198 of an assignment. 201 of an assignment.
199 */ 202 */
200 parse_iterator() : m_beg(), m_pos(), m_end(m_pos) | 203 parse_iterator() : m_beg(), m_pos(), m_end(m_pos), m_line(1), m_col(start_column)
201 {} 204 {}
202 /** 205 /**
203 Constructs an iterator for the range [it,end). The string 206 Constructs an iterator for the range [it,end). The string
204 pointed-to by (it,end) must not change (or otherwise 207 pointed-to by (it,end) must not change (or otherwise
205 invalid the iterators) for the life of this object, or 208 invalid the iterators) for the life of this object, or
206 undefined behaviour results. 209 undefined behaviour results.
207 */ 210 */
208 parse_iterator( iterator it, iterator end ) 211 parse_iterator( iterator it, iterator end )
209 : m_beg(it), m_pos(it), m_end(end) | 212 : m_beg(it), m_pos(it), m_end(end), m_line(1), m_col(start_column)
210 {} 213 {}
211 /** 214 /**
212 Equivalent to the parse_iterator(in.begin(),in.end()). 215 Equivalent to the parse_iterator(in.begin(),in.end()).
213 216
214 Note that the input string must outlive this 217 Note that the input string must outlive this
215 iterator. Also, the string MUST NOT change for the life of 218 iterator. Also, the string MUST NOT change for the life of
216 this iterator (and any copies of this iterator). Changing 219 this iterator (and any copies of this iterator). Changing
217 the string invalidates all iterators. 220 the string invalidates all iterators.
218 */ 221 */
219 explicit parse_iterator( std::string const & in ) 222 explicit parse_iterator( std::string const & in )
220 : m_beg(in.begin()),m_pos(m_beg), m_end(in.end()) | 223 : m_beg(in.begin()),m_pos(m_beg), m_end(in.end()),
| 224 m_line(1), m_col(start_column)
221 {} 225 {}
222 226
223 /** 227 /**
224 Returns true if this object has been advanced 228 Returns true if this object has been advanced
225 to its end iterator or if the current 229 to its end iterator or if the current
47 hidden lines
273 input (i.e., ++iterator becomes a no-op once iterator.eof() 277 input (i.e., ++iterator becomes a no-op once iterator.eof()
274 is true). 278 is true).
275 */ 279 */
276 parse_iterator & operator++() 280 parse_iterator & operator++()
277 { 281 {
278 if( this->m_pos != this->m_end ) | 282 if( m_pos != m_end )
279 { 283 {
280 ++this->m_pos; | 284 if( ++m_pos != m_end )
| 285 {
| 286 if('\n' == *m_pos)
| 287 {
| 288 m_line += 1;
| 289 m_col = 0;
| 290 }
| 291 else
| 292 {
| 293 ++m_col;
| 294 }
| 295 }
| 296 else
| 297 {
| 298 ++m_col; // for symmetry with op--
| 299 }
281 } 300 }
282 return *this; 301 return *this;
283 } 302 }
284 303
285 /** 304 /**
3 hidden lines
289 See the Prefix form for notes about advancing past the end 308 See the Prefix form for notes about advancing past the end
290 of the input. 309 of the input.
291 */ 310 */
292 parse_iterator operator++(int) 311 parse_iterator operator++(int)
293 { 312 {
294 return ( this->m_pos != this->m_end ) | 313 return (this->m_pos != this->m_end )
295 ? parse_iterator(this->m_pos++, this->m_end) | 314 ? (++(parse_iterator(*this)))
296 : *this; 315 : *this;
297 } 316 }
298 317
299 /** 318 /**
300 Prefix decrement. Decrements the iternal iterator. It will 319 Prefix decrement. Decrements the iternal iterator. It will
2 hidden lines
303 */ 322 */
304 parse_iterator & operator--() 323 parse_iterator & operator--()
305 { 324 {
306 if( this->m_pos != this->m_beg ) 325 if( this->m_pos != this->m_beg )
307 { 326 {
308 --this->m_pos; | 327 if( m_beg != --m_pos )
| 328 {
| 329 if('\n' == *m_pos)
| 330 {
| 331 m_line -= 1;
| 332 m_col = 0;
| 333 iterator it = m_pos;
| 334 --it;
| 335 for( ; it != m_beg; --it )
| 336 {
| 337 ++m_col;
| 338 if( '\n' == *it ) break;
| 339 }
| 340 }
| 341 else
| 342 {
| 343 --m_col;
| 344 }
| 345 }
| 346 else
| 347 {
| 348 m_col = 0;
| 349 }
309 } 350 }
310 return *this; 351 return *this;
311 } 352 }
312 353
313 /** Postfix decrement. It will not decrement to a point 354 /** Postfix decrement. It will not decrement to a point
314 before begin(). 355 before begin().
315 */ 356 */
316 parse_iterator operator--(int) 357 parse_iterator operator--(int)
317 { 358 {
318 return ( this->m_pos != this->m_beg ) 359 return ( this->m_pos != this->m_beg )
319 ? parse_iterator(this->m_pos--, this->m_end) | 360 ? (--(parse_iterator(*this)))
320 : *this; 361 : *this;
321 } 362 }
322 363
323 /** 364 /**
324 Returns a copy of the pointed-to char, or 0 if 365 Returns a copy of the pointed-to char, or 0 if
30 hidden lines
355 bool operator<( parse_iterator const & rhs ) const 396 bool operator<( parse_iterator const & rhs ) const
356 { 397 {
357 return this->m_pos < rhs.m_pos; 398 return this->m_pos < rhs.m_pos;
358 } 399 }
359 400
> 401 /**
> 402 Returns the 1-based line number of this iterator. Counting starts
> 403 only at the begin iterator this object was initialized with, and is
> 404 updated via the ++/-- operators. If an itertor is used on a partial
> 405 range of input, the line/col numbers won't reflect those of the
> 406 whole input.
> 407 */
> 408 int line() const { return m_line; }
> 409 /**
> 410 Returns the 1-base column number of the input. When (*this == '\n')
> 411 then col() actually returns 0, as doing so simplifies the implementation
> 412 a good deal and seems to coincide with text editors which use row 1/col 1
> 413 as a starting point (as opposed to emacs, which uses row 1/col 0).
> 414 See line() for important caveats.
> 415 */
> 416 int col() const { return m_col; }
> 417
360 private: 418 private:
361 iterator m_beg; 419 iterator m_beg;
362 iterator m_pos; 420 iterator m_pos;
363 iterator m_end; 421 iterator m_end;
> 422 int m_line;
> 423 int m_col;
364 }; 424 };
365 425
366 /*** 426 /***
367 parser_state stores the "significant" information about a 427 parser_state stores the "significant" information about a
368 parsing run, where "significant" means significant to the 428 parsing run, where "significant" means significant to the
228 hidden lines
597 class parse_error : public std::exception 657 class parse_error : public std::exception
598 { 658 {
599 private: 659 private:
600 void calcWhere() 660 void calcWhere()
601 { 661 {
> 662 // these two options actually provide different column numbers:
> 663 #if 0
602 int line = 1, col = 0; 664 int line = 1, col = 0;
603 calc_line_col( m_pos, line, col ); 665 calc_line_col( m_pos, line, col );
> 666 #else
> 667 int line = m_pos.line();
> 668 int col = m_pos.col();
> 669 #endif
604 std::ostringstream os; 670 std::ostringstream os;
605 os << "[line "<<line<<" col "<<col<<']'; 671 os << "[line "<<line<<" col "<<col<<']';
606 m_where = (os.str()); 672 m_where = (os.str());
607 } 673 }
608 674
1083 hidden lines
1692 col = st.col; 1758 col = st.col;
1693 } 1759 }
1694 } // namespace 1760 } // namespace
1695 1761
1696 #endif // s11n_net_PARSEPP_HPP_INCLUDED 1762 #endif // s11n_net_PARSEPP_HPP_INCLUDED