|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r61331 - in trunk/libs/spirit/example/scheme: input support
From: joel_at_[hidden]
Date: 2010-04-17 04:30:22
Author: djowel
Date: 2010-04-17 04:30:19 EDT (Sat, 17 Apr 2010)
New Revision: 61331
URL: http://svn.boost.org/trac/boost/changeset/61331
Log:
line_pos_iterator: a lighweight line position iterator.
Added:
trunk/libs/spirit/example/scheme/support/
trunk/libs/spirit/example/scheme/support/line_pos_iterator.hpp (contents, props changed)
Text files modified:
trunk/libs/spirit/example/scheme/input/parse_sexpr_impl.hpp | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
Modified: trunk/libs/spirit/example/scheme/input/parse_sexpr_impl.hpp
==============================================================================
--- trunk/libs/spirit/example/scheme/input/parse_sexpr_impl.hpp (original)
+++ trunk/libs/spirit/example/scheme/input/parse_sexpr_impl.hpp 2010-04-17 04:30:19 EDT (Sat, 17 Apr 2010)
@@ -14,6 +14,7 @@
#include <input/sexpr.hpp>
#include <input/parse_sexpr.hpp>
+#include <support/line_pos_iterator.hpp>
namespace scheme { namespace input
{
Added: trunk/libs/spirit/example/scheme/support/line_pos_iterator.hpp
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/scheme/support/line_pos_iterator.hpp 2010-04-17 04:30:19 EDT (Sat, 17 Apr 2010)
@@ -0,0 +1,109 @@
+/*=============================================================================
+ Copyright (c) 2001-2010 Joel de Guzman
+
+ Distributed under the Boost Software License, Version 1.0. (See accompanying
+ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+=============================================================================*/
+#if !defined(BOOST_SPIRIT_LINE_POS_ITERATOR)
+#define BOOST_SPIRIT_LINE_POS_ITERATOR
+
+#include <boost/iterator/iterator_adaptor.hpp>
+#include <boost/range/iterator_range.hpp>
+
+namespace scheme
+{
+ ///////////////////////////////////////////////////////////////////////////
+ // line_pos_iterator: a lighweight line position iterator. This iterator
+ // adapter only stores the current line number, nothing else. Unlike
+ // spirit classic's position_iterator, it does not store the column
+ // number and does not need an end iterator.
+ ///////////////////////////////////////////////////////////////////////////
+ template <typename Iterator>
+ class line_pos_iterator
+ : public boost::iterator_adaptor<
+ line_pos_iterator<Iterator> // Derived
+ , Iterator // Base
+ , boost::use_default // Value
+ , boost::forward_traversal_tag // CategoryOrTraversal
+ >
+ {
+ public:
+
+ line_pos_iterator()
+ : line_pos_iterator::iterator_adaptor_()
+ line(0), prev(0) {}
+
+ explicit line_pos_iterator(Iterator base)
+ : node_iter::iterator_adaptor_(base)
+ line(0), prev(0) {}
+
+ std::size_t position() const
+ {
+ return line;
+ }
+
+ private:
+
+ friend class boost::iterator_core_access;
+ void increment()
+ {
+ typename Iterator::reference ref = *(this->base());
+ switch (ref)
+ {
+ case '\r':
+ if (prev != '\n')
+ ++line;
+ break;
+ case '\n':
+ if (prev != '\r')
+ ++line;
+ break;
+ default:
+ break;
+ }
+ prev = ref;
+ }
+
+ std::size_t line;
+ typename Iterator::value_type prev;
+ };
+
+ ///////////////////////////////////////////////////////////////////////////
+ // Utilities
+ ///////////////////////////////////////////////////////////////////////////
+
+ // Get an iterator to the beginning of the line. Applicable to any
+ // iterator.
+ template <typename Iterator>
+ inline Iterator
+ get_line_start(Iterator lower_bound, Iterator current)
+ {
+ Iterator latest = lower_bound;
+ for (Iterator i = lower_bound; i != current; ++i)
+ {
+ switch (*i)
+ {
+ case '\r':
+ case '\n':
+ latest = i;
+ }
+ }
+ return latest;
+ }
+
+ // Get the iterator range containing the current line. Applicable to
+ // any iterator.
+ template <typename Iterator>
+ inline iterator_range<Iterator>
+ get_current_line(
+ Iterator lower_bound, Iterator current, Iterator upper_bound)
+ {
+ Iterator first = get_line_start(lower_bound, current);
+ Iterator last = get_line_start(current, upper_bound);
+ if (last == current)
+ last = upper_bound;
+ return iterator_range<Iterator>(first, last);
+ }
+}
+
+#endif
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk