Impl Ord for Location

This commit is contained in:
Shunsuke Shibayama 2023-02-01 13:36:19 +09:00
parent c5e2b0db1b
commit 4184c5d1f2

View file

@ -1,7 +1,7 @@
//! provides common components for error handling.
//!
//! エラー処理に関する汎用的なコンポーネントを提供する
use std::cmp;
use std::cmp::{self, Ordering};
use std::fmt;
use std::io::{stderr, BufWriter, Write as _};
@ -260,6 +260,38 @@ pub enum Location {
Unknown,
}
impl Ord for Location {
fn cmp(&self, other: &Location) -> Ordering {
if self.ln_end() < other.ln_begin() {
Ordering::Less
} else if other.ln_end() < self.ln_begin() {
Ordering::Greater
} else if self.ln_begin() == self.ln_end() && other.ln_begin() == other.ln_end() {
// assert_eq!(self.line_begin, other.line_begin);
// assert_eq!(self.line_end, other.line_end);
if self.col_end() < other.col_begin() {
Ordering::Less
} else if other.col_end() < self.col_begin() {
Ordering::Greater
} else {
Ordering::Equal
}
} else {
Ordering::Equal
}
}
}
impl PartialOrd for Location {
fn partial_cmp(&self, other: &Location) -> Option<Ordering> {
if self.is_unknown() || other.is_unknown() {
None
} else {
Some(self.cmp(other))
}
}
}
impl Location {
pub fn concat<L: Locational, R: Locational>(l: &L, r: &R) -> Self {
match (l.ln_begin(), l.col_begin(), r.ln_end(), r.col_end()) {