use crate::alloc::{Box, Vec};
use crate::compile;
use crate::parse::{Parser, Peek};
pub use rune_macros::Parse;
pub trait Parse
where
Self: Sized,
{
fn parse(p: &mut Parser<'_>) -> compile::Result<Self>;
}
impl<A, B> Parse for (A, B)
where
A: Parse + Peek,
B: Parse,
{
#[inline]
fn parse(parser: &mut Parser<'_>) -> compile::Result<Self> {
Ok((parser.parse()?, parser.parse()?))
}
}
impl<T> Parse for Option<T>
where
T: Parse + Peek,
{
#[inline]
fn parse(parser: &mut Parser<'_>) -> compile::Result<Self> {
Ok(if parser.peek::<T>()? {
Some(parser.parse()?)
} else {
None
})
}
}
impl<T> Parse for Box<T>
where
T: Parse,
{
#[inline]
fn parse(parser: &mut Parser<'_>) -> compile::Result<Self> {
Ok(Box::try_new(parser.parse()?)?)
}
}
impl<T> Parse for Vec<T>
where
T: Parse + Peek,
{
#[inline]
fn parse(parser: &mut Parser<'_>) -> compile::Result<Self> {
let mut output = Vec::new();
while parser.peek::<T>()? {
output.try_push(parser.parse()?)?;
}
Ok(output)
}
}