ast: Add Ord implementation to Location

This commit is contained in:
David Mak 2023-11-14 18:43:08 +08:00
parent 3780a4ad2f
commit e2d6dda26d
1 changed files with 25 additions and 2 deletions

View File

@ -1,8 +1,9 @@
//! Datatypes to support source location information. //! Datatypes to support source location information.
use std::cmp::Ordering;
use crate::ast_gen::StrRef; use crate::ast_gen::StrRef;
use std::fmt; use std::fmt;
#[derive(Clone, Copy, Debug, PartialEq)] #[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct FileName(pub StrRef); pub struct FileName(pub StrRef);
impl Default for FileName { impl Default for FileName {
fn default() -> Self { fn default() -> Self {
@ -17,7 +18,7 @@ impl From<String> for FileName {
} }
/// A location somewhere in the sourcecode. /// A location somewhere in the sourcecode.
#[derive(Clone, Copy, Debug, Default, PartialEq)] #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct Location { pub struct Location {
pub row: usize, pub row: usize,
pub column: usize, pub column: usize,
@ -30,6 +31,28 @@ impl fmt::Display for Location {
} }
} }
impl Ord for Location {
fn cmp(&self, other: &Self) -> Ordering {
let file_cmp = self.file.0.to_string().cmp(&other.file.0.to_string());
if file_cmp != Ordering::Equal {
return file_cmp
}
let row_cmp = self.row.cmp(&other.row);
if row_cmp != Ordering::Equal {
return row_cmp
}
self.column.cmp(&other.column)
}
}
impl PartialOrd for Location {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Location { impl Location {
pub fn visualize<'a>( pub fn visualize<'a>(
&self, &self,