mirror of
https://github.com/rm-dr/daisy
synced 2025-10-11 00:41:54 -07:00
Added linelocation to all errors
This commit is contained in:
59
src/parser/linelocation.rs
Normal file
59
src/parser/linelocation.rs
Normal file
@ -0,0 +1,59 @@
|
||||
use std::cmp;
|
||||
use std::ops::Add;
|
||||
use std::ops::AddAssign;
|
||||
use std::cmp::Ordering;
|
||||
|
||||
/// Specifies the location of a token in an input string.
|
||||
#[derive(Debug)]
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct LineLocation {
|
||||
pub pos: usize,
|
||||
pub len: usize
|
||||
}
|
||||
|
||||
impl LineLocation {
|
||||
pub fn zero(&self) -> bool {
|
||||
return self.pos == 0 && self.len == 0
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for LineLocation {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.pos == other.pos &&
|
||||
self.len ==other.len
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for LineLocation {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
return self.pos.partial_cmp(&other.pos);
|
||||
}
|
||||
}
|
||||
|
||||
impl Add for LineLocation {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, other: Self) -> Self::Output {
|
||||
if self.zero() { return other; }
|
||||
if other.zero() { return self; }
|
||||
|
||||
let start = cmp::min(self.pos, other.pos);
|
||||
let end = cmp::max(self.pos+self.len-1, other.pos+other.len-1);
|
||||
return LineLocation{
|
||||
pos: start,
|
||||
len: end - start+1
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAssign for LineLocation where {
|
||||
fn add_assign(&mut self, other: Self) {
|
||||
if self.zero() {*self = other}
|
||||
if other.zero() { return }
|
||||
|
||||
let start = cmp::min(self.pos, other.pos);
|
||||
let end = cmp::max(self.pos+self.len-1, other.pos+other.len-1);
|
||||
self.pos = start;
|
||||
self.len = end - start + 1;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user