From 1d6291b9ba1d8a183cf816a80f17f14a0b4ee7ff Mon Sep 17 00:00:00 2001 From: David Mak Date: Tue, 14 Nov 2023 18:43:08 +0800 Subject: [PATCH] ast: Add Ord implementation to Location --- nac3ast/src/location.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/nac3ast/src/location.rs b/nac3ast/src/location.rs index 2d658d5b7..976dbf055 100644 --- a/nac3ast/src/location.rs +++ b/nac3ast/src/location.rs @@ -1,8 +1,9 @@ //! Datatypes to support source location information. +use std::cmp::Ordering; use crate::ast_gen::StrRef; use std::fmt; -#[derive(Clone, Copy, Debug, PartialEq)] +#[derive(Clone, Copy, Debug, Eq, PartialEq)] pub struct FileName(pub StrRef); impl Default for FileName { fn default() -> Self { @@ -17,7 +18,7 @@ impl From for FileName { } /// A location somewhere in the sourcecode. -#[derive(Clone, Copy, Debug, Default, PartialEq)] +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] pub struct Location { pub row: 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 { + Some(self.cmp(other)) + } +} + impl Location { pub fn visualize<'a>( &self,