pub fn expect_enclosure<'t>(
    tokens: &'t [Token],
    opening: Delimiter,
    closing: Delimiter,
) -> ExpectResult<'t, &'t [Token]> {
    let ExpectOk { rest, .. } = expect_token_value(
        tokens,
        &TokenValue::Delimiting(Delimiter::ParenthesisOpening),
    )?;
    let mut current_enclosure_depth: usize = 0;
    let maybe_enclosure_size: Option<usize> =
        rest.iter()
            .position(|current_token| match &current_token.value {
                TokenValue::Delimiting(current_delimiter) => {
                    if current_enclosure_depth == 0 && current_delimiter == &closing {
                        return true;
                    }
                    if current_delimiter == &opening {
                        current_enclosure_depth += 1;
                    } else if current_delimiter == &closing {
                        current_enclosure_depth -= 1;
                    }
                    false
                }
                _ => false,
            });
    match maybe_enclosure_size {
        Some(enclosure_size) => Ok(ExpectOk {
            rest: &tokens[enclosure_size + 2..],
            tokens_consumed_count: enclosure_size + 2, // +2 to account for parentheses
            outcome: &rest[..enclosure_size],
        }),
        None => Err(SyntaxError(format!(
            "Expected to find a matching {} for {}, instead found end of statement.",
            closing, tokens[0]
        ))),
    }
}
// Expert an enclosure, the outcome being enclosure contents excluding opening and closing delimiters.
Expert an enclosure, the outcome being enclosure contents excluding opening and closing delimiters.
Expert an enclosure the outcome being enclosure contents excluding opening and closing delimiters .
pub fn expect_enclosure<'t>(
    tokens: &'t [Token],
    opening: Delimiter,
    closing: Delimiter,
) -> ExpectResult<'t, &'t [Token]> {
    let ExpectOk { rest, .. } = expect_token_value(
        tokens,
        &TokenValue::Delimiting(Delimiter::ParenthesisOpening),
    )?;
    let mut current_enclosure_depth: usize = 0;
    let maybe_enclosure_size: Option<usize> =
        rest.iter()
            .position(|current_token| match &current_token.value {
                TokenValue::Delimiting(current_delimiter) => {
                    if current_enclosure_depth == 0 && current_delimiter == &closing {
                        return true;
                    }
                    if current_delimiter == &opening {
                        current_enclosure_depth += 1;
                    } else if current_delimiter == &closing {
                        current_enclosure_depth -= 1;
                    }
                    false
                }
                _ => false,
            });
    match maybe_enclosure_size {
        Some(enclosure_size) => Ok(ExpectOk {
            rest: &tokens[enclosure_size + 2..],
            tokens_consumed_count: enclosure_size + 2, // +2 to account for parentheses
            outcome: &rest[..enclosure_size],
        }),
        None => Err(SyntaxError(format!(
            "Expected to find a matching {} for {}, instead found end of statement.",
            closing, tokens[0]
        ))),
    }
}
pub fn expect_enclosure < ' t > ( tokens : & ' t [ Token ] , opening : Delimiter , closing : Delimiter , ) -> ExpectResult < ' t , & ' t [ Token ] > { let ExpectOk { rest , .. } = expect_token_value ( tokens , & TokenValue :: Delimiting ( Delimiter :: ParenthesisOpening ) , ) ? ; let mut current_enclosure_depth : usize = 0 ; let maybe_enclosure_size : Option < usize > = rest . iter ( ) . position ( | current_token | match & current_token . value { TokenValue :: Delimiting ( current_delimiter ) => { if current_enclosure_depth == 0 && current_delimiter == & closing { return true ; } if current_delimiter == & opening { current_enclosure_depth += 1 ; } else if current_delimiter == & closing { current_enclosure_depth -= 1 ; } false } _ => false , } ) ; match maybe_enclosure_size { Some ( enclosure_size ) => Ok ( ExpectOk { rest : & tokens [ enclosure_size + 2 .. ] , tokens_consumed_count : enclosure_size + 2 , // +2 to account for parentheses outcome : & rest [ .. enclosure_size ] , } ) , None => Err ( SyntaxError ( format ! ( " " closing tokens [ 0 ] ) ) ) , } }
Expert an enclosure, the outcome being enclosure contents excluding opening and closing delimiters.
Expert an enclosure the outcome being enclosure contents excluding opening and closing delimiters .
tokens
&'t [Token]
opening
Delimiter
closing
Delimiter