use super::*; use super::ignore::Ignore; use super::preprocessed::Preprocessed; /// A TokenStream is a specialized [Iterator] which produces [Tokens](Token) pub trait TokenStream<'text>: Iterator> + std::fmt::Debug { /// Gets this stream's [Context] fn context(&self) -> Context; /// Creates an iterator that skips [Type::Space] in the input fn ignore(&'text mut self, variant: Type) -> Ignore<'text, Self> where Self: Sized { Ignore::new(variant, self) } /// Creates a [TokenStream] that performs live substitution of the input fn preprocessed(&'text mut self) -> Preprocessed<'text, Self> where Self: Sized { Preprocessed::new(self) } /// Returns the next [Token] without advancing fn peek(&mut self) -> Self::Item; /// Returns the next [Token] if it is of the expected [Type], without advancing fn peek_expect(&mut self, expected: Type) -> Result; /// Consumes and returns a [Token] if it is the expected [Type] /// /// Otherwise, does not consume a [Token] fn expect(&mut self, expected: Type) -> Result; /// Ignores a [Token] of the expected [Type], propegating errors. fn require(&mut self, expected: Type) -> Result<(), Error> { self.expect(expected).map(|_| ()) } /// Ignores a [Token] of the expected [Type], discarding errors. fn allow(&mut self, expected: Type) { let _ = self.expect(expected); } /// Runs a function on each fn any_of(&mut self, f: fn(&mut Self, Type) -> Result, expected: T) -> Result where T: AsRef<[Type]> { for &expected in expected.as_ref() { match f(self, expected).map_err(|e| e.bare()) { Ok(t) => return Ok(t), Err(Error::UnexpectedToken { .. }) => continue, Err(e) => return Err(e.context(self.context())), } } Err(Error::expected(expected, self.peek()).context(self.context())) } /// Returns the next [Token] if it is of the expected [Types](Type), without advancing fn peek_expect_any_of(&mut self, expected: T) -> Result where T: AsRef<[Type]> { self.any_of(Self::peek_expect, expected) } /// Consumes and returns a [Token] if it matches any of the expected [Types](Type) /// /// Otherwise, does not consume a [Token] fn expect_any_of(&mut self, expected: T) -> Result where T: AsRef<[Type]> { self.any_of(Self::expect, expected) } /// Ignores a [Token] of any expected [Type], discarding errors. fn allow_any_of(&mut self, expected: T) where T: AsRef<[Type]> { let _ = self.expect_any_of(expected); } /// Ignores a [Token] of any expected [Type], propegating errors. fn require_any_of(&mut self, expected: T) -> Result<(), Error> where T: AsRef<[Type]> { self.any_of(Self::require, expected) } }