1 /*=============================================================================
2 Copyright (c) 2014 Joel de Guzman
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 ==============================================================================*/
7 #if !defined(BOOST_SPIRIT_X3_POSITION_TAGGED_MAY_01_2014_0321PM)
8 #define BOOST_SPIRIT_X3_POSITION_TAGGED_MAY_01_2014_0321PM
10 #include <boost/range.hpp>
11 #include <boost/type_traits/is_base_of.hpp>
13 namespace boost { namespace spirit { namespace x3
15 struct position_tagged
17 // Use this to annotate an AST with the iterator position.
18 // These ids are used as a key to the position_cache (below)
19 // and marks the start and end of an AST node.
24 template <typename Container>
29 typedef typename Container::value_type iterator_type;
34 : first_(first), last_(last) {}
36 // This will catch all nodes inheriting from position_tagged
37 boost::iterator_range<iterator_type>
38 position_of(position_tagged const& ast) const
41 boost::iterator_range<iterator_type>(
42 positions.at(ast.id_first) // throws if out of range
43 , positions.at(ast.id_last) // throws if out of range
47 // This will catch all nodes except those inheriting from position_tagged
48 template <typename AST>
49 boost::iterator_range<iterator_type>
50 position_of(AST const& ast) const
52 // returns an empty position
53 return boost::iterator_range<iterator_type>();
56 // This will catch all nodes except those inheriting from position_tagged
57 template <typename AST>
58 void annotate(AST& ast, iterator_type first, iterator_type last, mpl::false_)
60 // (no-op) no need for tags
63 // This will catch all nodes inheriting from position_tagged
64 void annotate(position_tagged& ast, iterator_type first, iterator_type last, mpl::true_)
66 ast.id_first = int(positions.size());
67 positions.push_back(first);
68 ast.id_last = int(positions.size());
69 positions.push_back(last);
72 template <typename AST>
73 void annotate(AST& ast, iterator_type first, iterator_type last)
75 annotate(ast, first, last, is_base_of<position_tagged, AST>());
84 iterator_type first() const { return first_; }
85 iterator_type last() const { return last_; }