Implement position structs
This commit is contained in:
parent
6bc2ffde90
commit
4b07517d9f
|
@ -0,0 +1,3 @@
|
||||||
|
mod pos;
|
||||||
|
|
||||||
|
pub use pos::*;
|
|
@ -0,0 +1,55 @@
|
||||||
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||||
|
pub struct LinePos {
|
||||||
|
index: usize,
|
||||||
|
lineno: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||||
|
pub struct CharPos {
|
||||||
|
line: LinePos,
|
||||||
|
col: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LinePos {
|
||||||
|
pub fn new(index: usize, lineno: usize) -> Self {
|
||||||
|
assert!(lineno >= 1);
|
||||||
|
Self { index, lineno }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn index(&self) -> usize {
|
||||||
|
self.index
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn lineno(&self) -> usize {
|
||||||
|
self.lineno
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CharPos {
|
||||||
|
pub fn new(line: &LinePos, col: usize) -> Self {
|
||||||
|
assert!(col >= 1);
|
||||||
|
Self { line: *line, col }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn line(&self) -> LinePos {
|
||||||
|
self.line
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn index(&self) -> usize {
|
||||||
|
self.line.index
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn lineno(&self) -> usize {
|
||||||
|
self.line.lineno
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn col(&self) -> usize {
|
||||||
|
self.col
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<CharPos> for LinePos {
|
||||||
|
fn from(char_pos: CharPos) -> Self {
|
||||||
|
char_pos.line
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue