Implement position structs
This commit is contained in:
parent
6bc2ffde90
commit
0517db026c
|
@ -0,0 +1,3 @@
|
|||
mod pos;
|
||||
|
||||
pub use pos::*;
|
|
@ -0,0 +1,52 @@
|
|||
#[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(index: usize, lineno: usize, col: usize) -> Self {
|
||||
assert!(col >= 1);
|
||||
Self {
|
||||
line: LinePos::new(index, lineno),
|
||||
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
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue