fn parse_with_end(
    end: TokenKind,
    token_stream: &mut TokenStream<Lexer>,
    diagnostics: &mut Diagnostics,
) -> Result<Expr, ParseError> {
    // Working expression. Initially absent. If still absent by the end of the
    // function, an error is raised.
    let mut curr_expr = None;
    // Whether this expression had a non-eof token.
    let mut had_child_token = false;

    loop {
        match token_stream.current() {
            Ok(token) => match token.kind {
                // Equals to the end? Done.
                kind if end == kind => break,

                // Lambda? Delegate to parse_lambda and stack the expressions.
                Lambda => {
                    if let Ok(lambda) =
                        parse_lambda(end, token_stream, diagnostics)
                    {
                        curr_expr = Some(stack_exprs(curr_expr, lambda));
                    }
                },

                // Identifier? Simply create a variable.
                Ident => {
                    let next_expr = Expr {
                        span: token.span.clone(),
                        kind: ExprKind::Variable(token.span.clone()),
                    };
                    curr_expr = Some(stack_exprs(curr_expr, next_expr));
                    token_stream.next(diagnostics);
                },

                // Opening parenthesis? Simply delegate to parse_parenthesized
                // and stack the expressions.
                OpenParen => {
                    if let Ok(next_expr) = parse_parenthesized(
                        token.clone(),
                        token_stream,
                        diagnostics,
                    ) {
                        curr_expr = Some(stack_exprs(curr_expr, next_expr));
                    }
                },

                // End-Of-Input?? Stop without errors, they will be produced
                // somewhereelse (given that EOF was not the expected finishing
                // token).
                Eof => break,

                // Closing parenthesis (and not expected to finish the
                // expression)? Issue an unmatched closing
                // parenthesis. If it is not the final token of this expression,
                // there is no valid opening parenthesis to match it.
                CloseParen => {
                    let error =
                        UnmatchedCloseParen { span: token.span.clone() };
                    diagnostics.raise(error);
                    token_stream.next(diagnostics);
                },

                // Otherwise, e.g. dot, the token was not expected.
                _ => {
                    let error = MismatchedToken {
                        expected: vec![Lambda, Ident, OpenParen, end],
                        found: token.clone(),
                    };
                    diagnostics.raise(error);
                    token_stream.next(diagnostics);
                },
            },

            // The lexer already issued an error here.
            Err(_) => {
                token_stream.next(diagnostics);
            },
        }
        had_child_token = true;
    }

    match curr_expr {
        // We have an expression? Good, return it even if there are errors.
        Some(expr) => Ok(expr),
        // No expression? Probably an error.
        None => {
            // Only issue the error if EOF was directly found, without any other
            // token kind in the way.
            if !had_child_token {
                let error = MismatchedToken {
                    expected: vec![Lambda, Ident, OpenParen],
                    found: token_stream.current().unwrap().clone(),
                };
                diagnostics.raise(error);
            }
            Err(ParseError)
        },
    }
}
/// Parses an expression given the token kind that finishes it.
Parses an expression given the token kind that finishes it.
Parses an expression given the token kind that finishes it .
fn parse_with_end(
    end: TokenKind,
    token_stream: &mut TokenStream<Lexer>,
    diagnostics: &mut Diagnostics,
) -> Result<Expr, ParseError> {
    // Working expression. Initially absent. If still absent by the end of the
    // function, an error is raised.
    let mut curr_expr = None;
    // Whether this expression had a non-eof token.
    let mut had_child_token = false;
    loop {
        match token_stream.current() {
            Ok(token) => match token.kind {
                // Equals to the end? Done.
                kind if end == kind => break,
                // Lambda? Delegate to parse_lambda and stack the expressions.
                Lambda => {
                    if let Ok(lambda) =
                        parse_lambda(end, token_stream, diagnostics)
                    {
                        curr_expr = Some(stack_exprs(curr_expr, lambda));
                    }
                },
                // Identifier? Simply create a variable.
                Ident => {
                    let next_expr = Expr {
                        span: token.span.clone(),
                        kind: ExprKind::Variable(token.span.clone()),
                    };
                    curr_expr = Some(stack_exprs(curr_expr, next_expr));
                    token_stream.next(diagnostics);
                },
                // Opening parenthesis? Simply delegate to parse_parenthesized
                // and stack the expressions.
                OpenParen => {
                    if let Ok(next_expr) = parse_parenthesized(
                        token.clone(),
                        token_stream,
                        diagnostics,
                    ) {
                        curr_expr = Some(stack_exprs(curr_expr, next_expr));
                    }
                },
                // End-Of-Input?? Stop without errors, they will be produced
                // somewhereelse (given that EOF was not the expected finishing
                // token).
                Eof => break,
                // Closing parenthesis (and not expected to finish the
                // expression)? Issue an unmatched closing
                // parenthesis. If it is not the final token of this expression,
                // there is no valid opening parenthesis to match it.
                CloseParen => {
                    let error =
                        UnmatchedCloseParen { span: token.span.clone() };
                    diagnostics.raise(error);
                    token_stream.next(diagnostics);
                },
                // Otherwise, e.g. dot, the token was not expected.
                _ => {
                    let error = MismatchedToken {
                        expected: vec![Lambda, Ident, OpenParen, end],
                        found: token.clone(),
                    };
                    diagnostics.raise(error);
                    token_stream.next(diagnostics);
                },
            },
            // The lexer already issued an error here.
            Err(_) => {
                token_stream.next(diagnostics);
            },
        }
        had_child_token = true;
    }
    match curr_expr {
        // We have an expression? Good, return it even if there are errors.
        Some(expr) => Ok(expr),
        // No expression? Probably an error.
        None => {
            // Only issue the error if EOF was directly found, without any other
            // token kind in the way.
            if !had_child_token {
                let error = MismatchedToken {
                    expected: vec![Lambda, Ident, OpenParen],
                    found: token_stream.current().unwrap().clone(),
                };
                diagnostics.raise(error);
            }
            Err(ParseError)
        },
    }
}
fn parse_with_end ( end : TokenKind , token_stream : & mut TokenStream < Lexer > , diagnostics : & mut Diagnostics , ) -> Result < Expr , ParseError > { // Working expression. Initially absent. If still absent by the end of the // function, an error is raised. let mut curr_expr = None ; // Whether this expression had a non-eof token. let mut had_child_token = false ; loop { match token_stream . current ( ) { Ok ( token ) => match token . kind { // Equals to the end? Done. kind if end == kind => break , // Lambda? Delegate to parse_lambda and stack the expressions. Lambda => { if let Ok ( lambda ) = parse_lambda ( end , token_stream , diagnostics ) { curr_expr = Some ( stack_exprs ( curr_expr , lambda ) ) ; } } , // Identifier? Simply create a variable. Ident => { let next_expr = Expr { span : token . span . clone ( ) , kind : ExprKind :: Variable ( token . span . clone ( ) ) , } ; curr_expr = Some ( stack_exprs ( curr_expr , next_expr ) ) ; token_stream . next ( diagnostics ) ; } , // Opening parenthesis? Simply delegate to parse_parenthesized // and stack the expressions. OpenParen => { if let Ok ( next_expr ) = parse_parenthesized ( token . clone ( ) , token_stream , diagnostics , ) { curr_expr = Some ( stack_exprs ( curr_expr , next_expr ) ) ; } } , // End-Of-Input?? Stop without errors, they will be produced // somewhereelse (given that EOF was not the expected finishing // token). Eof => break , // Closing parenthesis (and not expected to finish the // expression)? Issue an unmatched closing // parenthesis. If it is not the final token of this expression, // there is no valid opening parenthesis to match it. CloseParen => { let error = UnmatchedCloseParen { span : token . span . clone ( ) } ; diagnostics . raise ( error ) ; token_stream . next ( diagnostics ) ; } , // Otherwise, e.g. dot, the token was not expected. _ => { let error = MismatchedToken { expected : vec ! [ Lambda Ident OpenParen end ] , found : token . clone ( ) , } ; diagnostics . raise ( error ) ; token_stream . next ( diagnostics ) ; } , } , // The lexer already issued an error here. Err ( _ ) => { token_stream . next ( diagnostics ) ; } , } had_child_token = true ; } match curr_expr { // We have an expression? Good, return it even if there are errors. Some ( expr ) => Ok ( expr ) , // No expression? Probably an error. None => { // Only issue the error if EOF was directly found, without any other // token kind in the way. if ! had_child_token { let error = MismatchedToken { expected : vec ! [ Lambda Ident OpenParen ] , found : token_stream . current ( ) . unwrap ( ) . clone ( ) , } ; diagnostics . raise ( error ) ; } Err ( ParseError ) } , } }
Parses an expression given the token kind that finishes it.
Parses an expression given the token kind that finishes it .
end
TokenKind
token_stream
&mut TokenStream<Lexer>
diagnostics
&mut Diagnostics
end
TokenKind
token_stream
&mut TokenStream<Lexer>
diagnostics
&mut Diagnostics