mod error;
mod stream;
#[doc(inline)]
pub use error::*;
#[doc(inline)]
pub use stream::*;
#[macro_export]
macro_rules! parse {
($src:tt as $ty:ty) => {{
use ::std::str::FromStr;
$crate::TokenStream::from_str(&$src.to_string())
.map_err($crate::parser::ParseError::from)
.and_then(|ts| ts.parse().parse::<$ty>())
}};
($src:tt) => {{
use ::std::str::FromStr;
$crate::TokenStream::from_str(&$src.to_string())
.map_err($crate::parser::ParseError::from)
.and_then(|ts| ts.parse().parse())
}};
}
pub trait Parse: Sized {
fn parse(stream: &mut ParseStream) -> Result<Self, ParseError>;
}
pub trait Peek: Sized {
fn peek(stream: &mut ParseStream) -> bool;
}
impl<T: Parse> Peek for T {
fn peek(stream: &mut ParseStream) -> bool {
Self::parse(stream).is_ok()
}
}
impl<T: Parse> Parse for Option<T> {
#[inline]
fn parse(stream: &mut ParseStream) -> Result<Self, ParseError> {
if stream.peek::<T>() {
stream.parse().map(Some)
} else {
Ok(None)
}
}
}
impl<T: Parse> Parse for Vec<T> {
fn parse(stream: &mut ParseStream) -> Result<Self, ParseError> {
Ok(stream.parse_while::<T>())
}
}
impl<T: Parse> Parse for Box<T> {
fn parse(stream: &mut ParseStream) -> Result<Self, ParseError> {
Ok(Self::new(stream.parse()?))
}
}