56 lines
967 B
Rust
56 lines
967 B
Rust
#[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
|
|
}
|
|
}
|