use std::str::CharIndices;
pub type PosInput<'s> = peekmore::PeekMoreIterator<PosIter<'s>>;
#[derive(Debug, Clone)]
pub struct PosIter<'s> {
iter: std::str::CharIndices<'s>,
lpos: usize,
line: usize,
}
impl<'s> From<CharIndices<'s>> for PosIter<'s> {
fn from(iter: CharIndices<'s>) -> Self {
Self { iter, lpos: 0, line: 1 }
}
}
impl Iterator for PosIter<'_> {
type Item = PosChar;
fn next(&mut self) -> Option<Self::Item> {
let (index, current) = self.iter.next()?;
self.lpos += 1;
if current == '\n' {
self.line += 1;
self.lpos = 0;
}
Some(PosChar {
char: current,
line: self.line,
col: self.lpos,
idx: index
})
}
}
#[derive(Debug, Clone, Copy)]
pub struct PosChar {
pub char: char,
pub line: usize,
pub col: usize,
pub idx: usize,
}
impl std::ops::Deref for PosChar {
type Target = char;
fn deref(&self) -> &Self::Target {
&self.char
}
}