/// Parse a list of items separated with a specific token, until a terminator
/// oktne has been reached. The terminator is not consumed.
Parse a list of items separated with a specific token, until a terminator
oktne has been reached. The terminator is not consumed.
Parse a list of items separated with a specific token until a terminator oktne has been reached . The terminator is not consumed .
pub fn separated<P: Parser, M, R, F, T>(
    p: &mut P,
    sep: Token,
    mut term: T,
    msg: &M,
    mut parse: F,
) -> RecoveredResult<Vec<R>>
where
    F: FnMut(&mut P) -> ReportedResult<R>,
    T: Predicate<P>,
    M: Display + ?Sized,
{
    let mut v = Vec::new();
    while !p.is_fatal() && p.peek(0).value != Eof && !term.matches(p) {
        // Parse the item. If the parser fails, recover to the terminator and
        // abort.
        match parse(p) {
            Ok(x) => v.push(x),
            Err(_) => {
                term.recover(p, false);
                return Err(Recovered);
            }
        }
        // Try to match the terminator. If it does not, consume a separator and
        // catch the case where the separator is immediately followed by the
        // terminator (superfluous separator).
        if term.matches(p) {
            break;
        } else if accept(p, sep) {
            if term.matches(p) {
                let q = p.last_span();
                p.emit(DiagBuilder2::warning(format!("Superfluous trailing {}", sep)).span(q));
                break;
            }
        } else {
            let Spanned { value: tkn, span } = p.peek(0);
            p.emit(
                DiagBuilder2::error(format!(
                    "Expected {} or {} after {}, but found {} instead",
                    term, sep, msg, tkn
                ))
                .span(span),
            );
            term.recover(p, false);
            return Err(Recovered);
        }
    }
    Ok(v)
}
pub fn separated < P : Parser , M , R , F , T > ( p : & mut P , sep : Token , mut term : T , msg : & M , mut parse : F , ) -> RecoveredResult < Vec < R > > where F : FnMut ( & mut P ) -> ReportedResult < R > , T : Predicate < P > , M : Display + ? Sized , { let mut v = Vec :: new ( ) ; while ! p . is_fatal ( ) && p . peek ( 0 ) . value != Eof && ! term . matches ( p ) { // Parse the item. If the parser fails, recover to the terminator and // abort. match parse ( p ) { Ok ( x ) => v . push ( x ) , Err ( _ ) => { term . recover ( p , false ) ; return Err ( Recovered ) ; } } // Try to match the terminator. If it does not, consume a separator and // catch the case where the separator is immediately followed by the // terminator (superfluous separator). if term . matches ( p ) { break ; } else if accept ( p , sep ) { if term . matches ( p ) { let q = p . last_span ( ) ; p . emit ( DiagBuilder2 :: warning ( format ! ( " " sep ) ) . span ( q ) ) ; break ; } } else { let Spanned { value : tkn , span } = p . peek ( 0 ) ; p . emit ( DiagBuilder2 :: error ( format ! ( " " term sep msg tkn ) ) . span ( span ) , ) ; term . recover ( p , false ) ; return Err ( Recovered ) ; } } Ok ( v ) }
Parse a list of items separated with a specific token, until a terminator
oktne has been reached.
Parse a list of items separated with a specific token until a terminator oktne has been reached .
p
&mut P
sep
Token
term
T
msg
&M
parse
F