use core::{
iter::Peekable,
marker::PhantomData,
ops::{Deref, DerefMut},
};
use crate::parser::Parser;
pub struct TokenStream<P, Token, Error>
where
P: Parser<Token, Error>,
{
pub(crate) inner: Peekable<P>,
pub(crate) token_phantom: PhantomData<Token>,
pub(crate) error_phantom: PhantomData<Error>,
}
impl<P, Token, Error> TokenStream<P, Token, Error>
where
P: Parser<Token, Error>,
{
pub fn expect(&mut self, expected: Token) -> Result<(), Error> {
P::expect(self, expected)
}
}
impl<P, Token, Error> Deref for TokenStream<P, Token, Error>
where
P: Parser<Token, Error>,
{
type Target = Peekable<P>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<P, Token, Error> DerefMut for TokenStream<P, Token, Error>
where
P: Parser<Token, Error>,
{
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
impl<P, Token, Error> Iterator for TokenStream<P, Token, Error>
where
P: Parser<Token, Error>,
{
type Item = Token;
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}
}