use cas_error::Error;
use crate::parser::{
token::pair::Pair,
Parse,
Parser,
};
use std::marker::PhantomData;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Surrounded<'source, P, T>
where
P: Pair,
{
pub open: P::Open<'source>,
pub value: T,
pub close: P::Close<'source>,
pair: PhantomData<P>,
}
impl<'source, P, T> Parse<'source> for Surrounded<'source, P, T>
where
P: Parse<'source> + Pair,
T: Parse<'source>,
{
fn std_parse(
input: &mut Parser<'source>,
recoverable_errors: &mut Vec<Error>
) -> Result<Self, Vec<Error>> {
let open = input.try_parse().forward_errors(recoverable_errors)?;
let mut input_ahead = input.clone();
let mut depth = 1;
while depth > 0 {
let token = input_ahead.next_token()
.map_err(|eof| vec![eof])?;
if token.kind == P::OPEN {
depth += 1;
} else if token.kind == P::CLOSE {
depth -= 1;
}
if depth == 0 {
break;
}
}
let value = input.try_parse().forward_errors(recoverable_errors)?;
let close = input.try_parse().forward_errors(recoverable_errors)?;
Ok(Self { open, value, close, pair: PhantomData })
}
}