use crate::source::Chunk;
use crate::Logos;
pub trait LexerInternal<'source> {
type Token;
fn read<T: Chunk<'source>>(&self) -> Option<T>;
fn read_at<T: Chunk<'source>>(&self, n: usize) -> Option<T>;
fn test<T: Chunk<'source>, F: FnOnce(T) -> bool>(&self, test: F) -> bool;
fn test_at<T:Chunk<'source>, F: FnOnce(T) -> bool>(&self, n: usize, test: F) -> bool;
fn bump_unchecked(&mut self, size: usize);
fn trivia(&mut self);
fn error(&mut self);
fn end(&mut self);
fn set(&mut self, token: Self::Token);
}
pub trait CallbackResult<P> {
fn construct<'s, F, T: Logos<'s>>(self, constructor: F) -> T
where
F: Fn(P) -> T;
}
impl<P> CallbackResult<P> for P {
#[inline]
fn construct<'s, F, T: Logos<'s>>(self, constructor: F) -> T
where
F: Fn(P) -> T,
{
constructor(self)
}
}
impl CallbackResult<()> for bool {
#[inline]
fn construct<'s, F, T: Logos<'s>>(self, constructor: F) -> T
where
F: Fn(()) -> T,
{
match self {
true => constructor(()),
false => T::ERROR,
}
}
}
impl<P> CallbackResult<P> for Option<P> {
#[inline]
fn construct<'s, F, T: Logos<'s>>(self, constructor: F) -> T
where
F: Fn(P) -> T,
{
match self {
Some(product) => constructor(product),
None => T::ERROR,
}
}
}
impl<P, E> CallbackResult<P> for Result<P, E> {
#[inline]
fn construct<'s, F, T: Logos<'s>>(self, constructor: F) -> T
where
F: Fn(P) -> T,
{
match self {
Ok(product) => constructor(product),
Err(_) => T::ERROR,
}
}
}