2020-05-09 09:55:04 +00:00
|
|
|
use std::borrow::{Borrow, BorrowMut};
|
|
|
|
use std::cmp::Ordering;
|
|
|
|
use std::fmt;
|
|
|
|
use std::hash::{Hash, Hasher};
|
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
|
|
|
|
/// Original position of element in source code
|
|
|
|
#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Copy, Default, Hash)]
|
|
|
|
pub struct Pos {
|
|
|
|
/// One-based line number
|
|
|
|
pub line: usize,
|
|
|
|
|
|
|
|
/// One-based column number
|
|
|
|
pub column: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for Pos {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "Pos({}:{})", self.line, self.column)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Pos {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}:{}", self.line, self.column)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 02:59:51 +00:00
|
|
|
/// Represents the position of a AST node
|
2020-05-09 09:55:04 +00:00
|
|
|
#[derive(Clone, Debug, Copy, Default)]
|
|
|
|
#[allow(missing_docs)]
|
2020-05-10 02:59:51 +00:00
|
|
|
pub struct Positioned<T: ?Sized> {
|
|
|
|
pub pos: Pos,
|
2020-05-09 09:55:04 +00:00
|
|
|
pub node: T,
|
|
|
|
}
|
|
|
|
|
2020-05-10 02:59:51 +00:00
|
|
|
impl<T: fmt::Display> fmt::Display for Positioned<T> {
|
2020-05-09 09:55:04 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
self.node.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 02:59:51 +00:00
|
|
|
impl<T: Clone> Positioned<T> {
|
2020-05-09 09:55:04 +00:00
|
|
|
#[inline]
|
|
|
|
#[allow(missing_docs)]
|
|
|
|
pub fn clone_inner(&self) -> T {
|
|
|
|
self.node.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 02:59:51 +00:00
|
|
|
impl<T: PartialEq> PartialEq for Positioned<T> {
|
2020-05-09 09:55:04 +00:00
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.node.eq(&other.node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 02:59:51 +00:00
|
|
|
impl<T: PartialOrd> PartialOrd for Positioned<T> {
|
2020-05-09 09:55:04 +00:00
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
|
|
self.node.partial_cmp(&other.node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 02:59:51 +00:00
|
|
|
impl<T: Ord> Ord for Positioned<T> {
|
2020-05-09 09:55:04 +00:00
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
|
|
self.node.cmp(&other.node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 02:59:51 +00:00
|
|
|
impl<T: Ord> Eq for Positioned<T> {}
|
2020-05-09 09:55:04 +00:00
|
|
|
|
2020-05-10 02:59:51 +00:00
|
|
|
impl<T: ?Sized> Deref for Positioned<T> {
|
2020-05-09 09:55:04 +00:00
|
|
|
type Target = T;
|
|
|
|
|
|
|
|
fn deref(&self) -> &T {
|
|
|
|
&self.node
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 02:59:51 +00:00
|
|
|
impl<T: ?Sized> DerefMut for Positioned<T> {
|
2020-05-09 09:55:04 +00:00
|
|
|
fn deref_mut(&mut self) -> &mut T {
|
|
|
|
&mut self.node
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 02:59:51 +00:00
|
|
|
impl<T: Hash> Hash for Positioned<T> {
|
2020-05-09 09:55:04 +00:00
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
self.node.hash(state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 02:59:51 +00:00
|
|
|
impl Borrow<str> for Positioned<String> {
|
2020-05-09 09:55:04 +00:00
|
|
|
fn borrow(&self) -> &str {
|
|
|
|
self.node.as_str()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 02:59:51 +00:00
|
|
|
impl BorrowMut<str> for Positioned<String> {
|
2020-05-09 09:55:04 +00:00
|
|
|
fn borrow_mut(&mut self) -> &mut str {
|
|
|
|
self.node.as_mut_str()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 02:59:51 +00:00
|
|
|
impl<T> Positioned<T> {
|
|
|
|
pub(crate) fn new(node: T, pos: Pos) -> Positioned<T> {
|
|
|
|
Positioned { node, pos }
|
2020-05-09 09:55:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-05-09 14:02:55 +00:00
|
|
|
pub fn into_inner(self) -> T {
|
2020-05-09 09:55:04 +00:00
|
|
|
self.node
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get start position
|
|
|
|
#[inline]
|
|
|
|
pub fn position(&self) -> Pos {
|
2020-05-10 02:59:51 +00:00
|
|
|
self.pos
|
2020-05-09 09:55:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-05-10 02:59:51 +00:00
|
|
|
pub(crate) fn pack<F: FnOnce(Self) -> R, R>(self, f: F) -> Positioned<R> {
|
|
|
|
Positioned {
|
|
|
|
pos: self.pos,
|
2020-05-09 09:55:04 +00:00
|
|
|
node: f(self),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|