1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use std::convert::{
	AsRef,
	AsMut
};
use std::ops::{
	Deref,
	DerefMut
};
use std::hash::{
	Hash,
	Hasher
};
use std::cmp::{
	Ord,
	PartialOrd,
	Ordering
};
use std::convert::TryInto;
use std::fmt;
use crate::Span;

/// Located data.
///
/// This is a simple wrapper around data that can be located in a source file.
/// It is useful to wrap abstract syntax tree nodes.
///
/// It derefs into the inner value.
pub struct Loc<T: ?Sized> {
	span: Span,
	value: T
}

impl<T: ?Sized> Loc<T> {
	/// Associate a span location to some data by wrapping it under `Loc`.
	pub fn new(t: T, span: Span) -> Loc<T> where T: Sized {
		Loc {
			span: span,
			value: t
		}
	}

	/// Get the span location of the data.
	pub fn span(&self) -> Span {
		self.span
	}

	/// Convert the inner value.
	pub fn inner_into<U>(self) -> Loc<U> where T: Into<U> {
		Loc {
			span: self.span,
			value: self.value.into()
		}
	}

	/// Try to convert the inner value.
	pub fn inner_try_into<U>(self) -> Result<Loc<U>, <T as TryInto<U>>::Error> where T: TryInto<U> {
		Ok(Loc {
			span: self.span,
			value: self.value.try_into()?
		})
	}

	/// Unwrap the data.
	pub fn into_inner(self) -> T where T: Sized {
		self.value
	}

	/// Break the wrapper into the value and its location.
	pub fn into_raw_parts(self) -> (T, Span) where T: Sized {
		(self.value, self.span)
	}
}

impl<T: Clone> Clone for Loc<T> {
	fn clone(&self) -> Loc<T> {
		Loc {
			span: self.span,
			value: self.value.clone()
		}
	}
}

impl<T> AsRef<T> for Loc<T> {
	fn as_ref(&self) -> &T {
		&self.value
	}
}

impl<T> AsMut<T> for Loc<T> {
	fn as_mut(&mut self) -> &mut T {
		&mut self.value
	}
}

impl<T: fmt::Display> fmt::Display for Loc<T> {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		self.value.fmt(f)
	}
}

impl<T: fmt::Debug> fmt::Debug for Loc<T> {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		write!(f, "{:?}:{}", self.value, self.span)
	}
}

impl<T: Hash> Hash for Loc<T> {
	fn hash<H: Hasher>(&self, state: &mut H) {
		self.span.hash(state);
		self.value.hash(state);
	}
}

impl<T: PartialEq> PartialEq for Loc<T> {
	fn eq(&self, other: &Loc<T>) -> bool {
		self.span == other.span && self.value == other.value
	}
}

impl<T: Eq> Eq for Loc<T> {}

impl<T: PartialOrd> PartialOrd for Loc<T> {
	fn partial_cmp(&self, other: &Loc<T>) -> Option<Ordering> {
		self.value.partial_cmp(&other.value)
	}
}

impl<T: Ord> Ord for Loc<T> {
	fn cmp(&self, other: &Loc<T>) -> Ordering {
		self.value.cmp(&other.value)
	}
}

impl<T> Deref for Loc<T> {
	type Target = T;

	fn deref(&self) -> &T {
		&self.value
	}
}

impl<T> DerefMut for Loc<T> {
	fn deref_mut(&mut self) -> &mut T {
		&mut self.value
	}
}