Index: src/Common/ScopedMap.h
===================================================================
--- src/Common/ScopedMap.h	(revision b12c0367752d66bda0955feaa0c3ac4f7fa51973)
+++ src/Common/ScopedMap.h	(revision 242f7053d2148bb454ba918531bf1f5e23a8ffc8)
@@ -22,22 +22,30 @@
 #include <vector>
 
+/// Default (empty) ScopedMap note type
+struct EmptyNote {};
+
 /// A map where the items are placed into nested scopes;
-/// inserted items are placed into the innermost scope, lookup looks from the innermost scope outward
-template<typename Key, typename Value>
+/// inserted items are placed into the innermost scope, lookup looks from the innermost scope outward.
+/// Scopes may be annotated with a value; the annotation defaults to empty
+template<typename Key, typename Value, typename Note = EmptyNote>
 class ScopedMap {
-	typedef std::map< Key, Value > Scope;
+	typedef std::map< Key, Value > MapType;
+	struct Scope {
+		MapType map;
+		Note note;	
+	};
 	typedef std::vector< Scope > ScopeList;
 
 	ScopeList scopes; ///< scoped list of maps
 public:
-	typedef typename Scope::key_type key_type;
-	typedef typename Scope::mapped_type mapped_type;
-	typedef typename Scope::value_type value_type;
+	typedef typename MapType::key_type key_type;
+	typedef typename MapType::mapped_type mapped_type;
+	typedef typename MapType::value_type value_type;
 	typedef typename ScopeList::size_type size_type;
 	typedef typename ScopeList::difference_type difference_type;
-	typedef typename Scope::reference reference;
-	typedef typename Scope::const_reference const_reference;
-	typedef typename Scope::pointer pointer;
-	typedef typename Scope::const_pointer const_pointer;
+	typedef typename MapType::reference reference;
+	typedef typename MapType::const_reference const_reference;
+	typedef typename MapType::pointer pointer;
+	typedef typename MapType::const_pointer const_pointer;
 
 	class iterator : public std::iterator< std::bidirectional_iterator_tag,
@@ -45,11 +53,11 @@
 	friend class ScopedMap;
 	friend class const_iterator;
-		typedef typename std::map< Key, Value >::iterator wrapped_iterator;
-		typedef typename std::vector< std::map< Key, Value > > scope_list;
+		typedef typename ScopedMap::MapType::iterator wrapped_iterator;
+		typedef typename ScopedMap::ScopeList scope_list;
 		typedef typename scope_list::size_type size_type;
 
 		/// Checks if this iterator points to a valid item
 		bool is_valid() const {
-			return it != (*scopes)[level].end();
+			return it != (*scopes)[level].map.end();
 		}
 
@@ -79,8 +87,8 @@
 
 		iterator& operator++ () {
-			if ( it == (*scopes)[level].end() ) {
+			if ( it == (*scopes)[level].map.end() ) {
 				if ( level == 0 ) return *this;
 				--level;
-				it = (*scopes)[level].begin();
+				it = (*scopes)[level].map.begin();
 			} else {
 				++it;
@@ -92,7 +100,7 @@
 		iterator& operator-- () {
 			// may fail if this is the begin iterator; allowed by STL spec
-			if ( it == (*scopes)[level].begin() ) {
+			if ( it == (*scopes)[level].map.begin() ) {
 				++level;
-				it = (*scopes)[level].end();
+				it = (*scopes)[level].map.end();
 			}
 			--it;
@@ -107,4 +115,7 @@
 
 		size_type get_level() const { return level; }
+
+		Note& get_note() { return (*scopes)[level].note; }
+		const Note& get_note() const { return (*scopes)[level].note; }
 
 	private:
@@ -117,12 +128,12 @@
 	                                             value_type > {
 	friend class ScopedMap;
-		typedef typename std::map< Key, Value >::iterator wrapped_iterator;
-		typedef typename std::map< Key, Value >::const_iterator wrapped_const_iterator;
-		typedef typename std::vector< std::map< Key, Value > > scope_list;
+		typedef typename ScopedMap::MapType::iterator wrapped_iterator;
+		typedef typename ScopedMap::MapType::const_iterator wrapped_const_iterator;
+		typedef typename ScopedMap::ScopeList scope_list;
 		typedef typename scope_list::size_type size_type;
 
 		/// Checks if this iterator points to a valid item
 		bool is_valid() const {
-			return it != (*scopes)[level].end();
+			return it != (*scopes)[level].map.end();
 		}
 
@@ -157,8 +168,8 @@
 
 		const_iterator& operator++ () {
-			if ( it == (*scopes)[level].end() ) {
+			if ( it == (*scopes)[level].map.end() ) {
 				if ( level == 0 ) return *this;
 				--level;
-				it = (*scopes)[level].begin();
+				it = (*scopes)[level].map.begin();
 			} else {
 				++it;
@@ -170,7 +181,7 @@
 		const_iterator& operator-- () {
 			// may fail if this is the begin iterator; allowed by STL spec
-			if ( it == (*scopes)[level].begin() ) {
+			if ( it == (*scopes)[level].map.begin() ) {
 				++level;
-				it = (*scopes)[level].end();
+				it = (*scopes)[level].map.end();
 			}
 			--it;
@@ -185,4 +196,6 @@
 
 		size_type get_level() const { return level; }
+
+		const Note& get_note() const { return (*scopes)[level].note; }
 
 	private:
@@ -206,19 +219,23 @@
 	ScopedMap() { beginScope(); }
 
-	iterator begin() { return iterator(scopes, scopes.back().begin(), currentScope()).next_valid(); }
-	const_iterator begin() const { return const_iterator(scopes, scopes.back().begin(), currentScope()).next_valid(); }
-	const_iterator cbegin() const { return const_iterator(scopes, scopes.back().begin(), currentScope()).next_valid(); }
-	iterator end() { return iterator(scopes, scopes[0].end(), 0); }
-	const_iterator end() const { return const_iterator(scopes, scopes[0].end(), 0); }
-	const_iterator cend() const { return const_iterator(scopes, scopes[0].end(), 0); }
+	iterator begin() { return iterator(scopes, scopes.back().map.begin(), currentScope()).next_valid(); }
+	const_iterator begin() const { return const_iterator(scopes, scopes.back().map.begin(), currentScope()).next_valid(); }
+	const_iterator cbegin() const { return const_iterator(scopes, scopes.back().map.begin(), currentScope()).next_valid(); }
+	iterator end() { return iterator(scopes, scopes[0].map.end(), 0); }
+	const_iterator end() const { return const_iterator(scopes, scopes[0].map.end(), 0); }
+	const_iterator cend() const { return const_iterator(scopes, scopes[0].map.end(), 0); }
 
 	/// Gets the index of the current scope (counted from 1)
 	size_type currentScope() const { return scopes.size() - 1; }
+
+	/// Gets the note at the given scope
+	Note& getNote( size_type i ) { return scopes[i].note; }
+	const Note& getNote( size_type i ) const { return scopes[i].note; }
 
 	/// Finds the given key in the outermost scope it occurs; returns end() for none such
 	iterator find( const Key &key ) {
 		for ( size_type i = scopes.size() - 1; ; --i ) {
-			typename Scope::iterator val = scopes[i].find( key );
-			if ( val != scopes[i].end() ) return iterator( scopes, val, i );
+			typename MapType::iterator val = scopes[i].map.find( key );
+			if ( val != scopes[i].map.end() ) return iterator( scopes, val, i );
 			if ( i == 0 ) break;
 		}
@@ -226,15 +243,15 @@
 	}
 	const_iterator find( const Key &key ) const {
-			return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->find( key ) );
+			return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->find( key ) );
 	}
 
 	/// Finds the given key in the provided scope; returns end() for none such
 	iterator findAt( size_type scope, const Key& key ) {
-		typename Scope::iterator val = scopes[scope].find( key );
-		if ( val != scopes[scope].end() ) return iterator( scopes, val, scope );
+		typename MapType::iterator val = scopes[scope].map.find( key );
+		if ( val != scopes[scope].map.end() ) return iterator( scopes, val, scope );
 		return end();
 	}
 	const_iterator findAt( size_type scope, const Key& key ) const {
-		return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->findAt( scope, key ) );
+		return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->findAt( scope, key ) );
 	}
 
@@ -243,6 +260,6 @@
 		if ( it.level == 0 ) return end();
 		for ( size_type i = it.level - 1; ; --i ) {
-			typename Scope::iterator val = scopes[i].find( key );
-			if ( val != scopes[i].end() ) return iterator( scopes, val, i );
+			typename MapType::iterator val = scopes[i].map.find( key );
+			if ( val != scopes[i].map.end() ) return iterator( scopes, val, i );
 			if ( i == 0 ) break;
 		}
@@ -250,5 +267,5 @@
 	}
 	const_iterator findNext( const_iterator &it, const Key &key ) const {
-			return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->findNext( it, key ) );
+			return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->findNext( it, key ) );
 	}
 
@@ -256,5 +273,5 @@
 	template< typename value_type_t >
 	std::pair< iterator, bool > insert( value_type_t&& value ) {
-		std::pair< typename Scope::iterator, bool > res = scopes.back().insert( std::forward<value_type_t>( value ) );
+		std::pair< typename MapType::iterator, bool > res = scopes.back().map.insert( std::forward<value_type_t>( value ) );
 		return std::make_pair( iterator(scopes, std::move( res.first ), scopes.size()-1), std::move( res.second ) );
 	}
@@ -262,6 +279,6 @@
 	template< typename value_type_t >
 	std::pair< iterator, bool > insert( iterator at, value_type_t&& value ) {
-		Scope& scope = (*at.scopes) [ at.level ];
-		std::pair< typename Scope::iterator, bool > res = scope.insert( std::forward<value_type_t>( value ) );
+		MapType& scope = (*at.scopes)[ at.level ].map;
+		std::pair< typename MapType::iterator, bool > res = scope.insert( std::forward<value_type_t>( value ) );
 		return std::make_pair( iterator(scopes, std::move( res.first ), at.level), std::move( res.second ) );
 	}
@@ -272,5 +289,5 @@
 	template< typename value_type_t >
 	std::pair< iterator, bool > insertAt( size_type scope, value_type_t&& value ) {
-		std::pair< typename Scope::iterator, bool > res = scopes.at(scope).insert( std::forward<value_type_t>( value ) );
+		std::pair< typename MapType::iterator, bool > res = scopes.at(scope).map.insert( std::forward<value_type_t>( value ) );
 		return std::make_pair( iterator(scopes, std::move( res.first ), scope), std::move( res.second ) );
 	}
@@ -288,5 +305,5 @@
 
 	iterator erase( iterator pos ) {
-		Scope& scope = (*pos.scopes) [ pos.level ];
+		MapType& scope = (*pos.scopes)[ pos.level ].map;
 		const typename iterator::wrapped_iterator& new_it = scope.erase( pos.it );
 		iterator it( *pos.scopes, new_it, pos.level );
