use std::str::Chars;
use crate::Error;
pub(crate) struct Cursor<'a> {
chars: Chars<'a>,
pub(crate) err: Option<Error>,
}
impl<'a> Cursor<'a> {
pub(crate) fn new(input: &'a str) -> Cursor<'a> {
Cursor {
chars: input.chars(),
err: None,
}
}
}
pub(crate) const EOF_CHAR: char = '\0';
impl<'a> Cursor<'a> {
fn nth_char(&self, n: usize) -> char {
self.chars().nth(n).unwrap_or(EOF_CHAR)
}
pub(crate) fn first(&self) -> char {
self.nth_char(0)
}
pub(crate) fn second(&self) -> char {
self.nth_char(1)
}
pub(crate) fn is_eof(&self) -> bool {
self.chars.as_str().is_empty()
}
pub(crate) fn bump(&mut self) -> Option<char> {
let c = self.chars.next()?;
Some(c)
}
pub(crate) fn err(&mut self) -> Option<Error> {
self.err.clone()
}
pub(crate) fn add_err(&mut self, err: Error) {
self.err = Some(err)
}
fn chars(&self) -> Chars<'_> {
self.chars.clone()
}
}