// Recursive helper for parsing parenthesized expressions. Each call
// handles one layer of parentheses. Before every call, the parser must
// be positioned after an opening parenthesis; upon non-error return,
// the parser will be positioned before the corresponding close
// parenthesis. Somewhat weirdly, the returned expression semantically
// includes the opening/closing parentheses, even though this function
// is not responsible for parsing them.
Recursive helper for parsing parenthesized expressions. Each call
handles one layer of parentheses. Before every call, the parser must
be positioned after an opening parenthesis; upon non-error return,
the parser will be positioned before the corresponding close
parenthesis. Somewhat weirdly, the returned expression semantically
includes the opening/closing parentheses, even though this function
is not responsible for parsing them.
Recursive helper for parsing parenthesized expressions . Each call handles one layer of parentheses . Before every call the parser must be positioned after an opening parenthesis ; upon non - error return the parser will be positioned before the corresponding close parenthesis . Somewhat weirdly the returned expression semantically includes the opening / closing parentheses even though this function is not responsible for parsing them .
fn parse(parser: &mut Parser) -> Result<Either, ParserError> {
            if parser.peek_keyword(SELECT) || parser.peek_keyword(WITH) {
                // Easy case one: unambiguously a subquery.
                Ok(Either::Query(parser.parse_query()?))
            } else if !parser.consume_token(&Token::LParen) {
                // Easy case two: unambiguously an expression.
                let exprs = parser.parse_comma_separated(Parser::parse_expr)?;
                if exprs.len() == 1 {
                    Ok(Either::Expr(Expr::Nested(Box::new(exprs.into_element()))))
                } else {
                    Ok(Either::Expr(Expr::Row { exprs }))
                }
            } else {
                // Hard case: we have an open parenthesis, and we need to decide
                // whether it belongs to the query or the expression.
                // Parse to the closing parenthesis.
                let either = parser.checked_recur_mut(parse)?;
                parser.expect_token(&Token::RParen)?;
                // Decide if we need to associate any tokens after the closing
                // parenthesis with what we've parsed so far.
                match (either, parser.peek_token()) {
                    // The next token is another closing parenthesis. Can't
                    // resolve the ambiguity yet. Return to let our caller
                    // handle it.
                    (either, Some(Token::RParen)) => Ok(either.nest()),
                    // The next token is a comma, which means `either` was the
                    // first expression in an implicit row constructor.
                    (either, Some(Token::Comma)) => {
                        let mut exprs = vec![either.into_expr()];
                        while parser.consume_token(&Token::Comma) {
                            exprs.push(parser.parse_expr()?);
                        }
                        Ok(Either::Expr(Expr::Row { exprs }))
                    }
                    // We have a subquery and the next token is a set operator.
                    // That implies we have a partially-parsed subquery (or a
                    // syntax error). Hop into parsing a set expression where
                    // our subquery is the LHS of the set operator.
                    (Either::Query(query), Some(Token::Keyword(kw)))
                        if matches!(kw, UNION | INTERSECT | EXCEPT) =>
                    {
                        let query = SetExpr::Query(Box::new(query));
                        let ctes = vec![];
                        let body = parser.parse_query_body_seeded(SetPrecedence::Zero, query)?;
                        Ok(Either::Query(parser.parse_query_tail(ctes, body)?))
                    }
                    // The next token is something else. That implies we have a
                    // partially-parsed expression (or a syntax error). Hop into
                    // parsing an expression where `either` is the expression
                    // prefix.
                    (either, _) => {
                        let prefix = either.into_expr();
                        let expr = parser.parse_subexpr_seeded(Precedence::Zero, prefix)?;
                        Ok(Either::Expr(expr).nest())
                    }
                }
            }
        }
fn parse ( parser : & mut Parser ) -> Result < Either , ParserError > { if parser . peek_keyword ( SELECT ) || parser . peek_keyword ( WITH ) { // Easy case one: unambiguously a subquery. Ok ( Either :: Query ( parser . parse_query ( ) ? ) ) } else if ! parser . consume_token ( & Token :: LParen ) { // Easy case two: unambiguously an expression. let exprs = parser . parse_comma_separated ( Parser :: parse_expr ) ? ; if exprs . len ( ) == 1 { Ok ( Either :: Expr ( Expr :: Nested ( Box :: new ( exprs . into_element ( ) ) ) ) ) } else { Ok ( Either :: Expr ( Expr :: Row { exprs } ) ) } } else { // Hard case: we have an open parenthesis, and we need to decide // whether it belongs to the query or the expression. // Parse to the closing parenthesis. let either = parser . checked_recur_mut ( parse ) ? ; parser . expect_token ( & Token :: RParen ) ? ; // Decide if we need to associate any tokens after the closing // parenthesis with what we've parsed so far. match ( either , parser . peek_token ( ) ) { // The next token is another closing parenthesis. Can't // resolve the ambiguity yet. Return to let our caller // handle it. ( either , Some ( Token :: RParen ) ) => Ok ( either . nest ( ) ) , // The next token is a comma, which means `either` was the // first expression in an implicit row constructor. ( either , Some ( Token :: Comma ) ) => { let mut exprs = vec ! [ either into_expr ( ) ] ; while parser . consume_token ( & Token :: Comma ) { exprs . push ( parser . parse_expr ( ) ? ) ; } Ok ( Either :: Expr ( Expr :: Row { exprs } ) ) } // We have a subquery and the next token is a set operator. // That implies we have a partially-parsed subquery (or a // syntax error). Hop into parsing a set expression where // our subquery is the LHS of the set operator. ( Either :: Query ( query ) , Some ( Token :: Keyword ( kw ) ) ) if matches ! ( kw UNION INTERSECT EXCEPT ) => { let query = SetExpr :: Query ( Box :: new ( query ) ) ; let ctes = vec ! [ ] ; let body = parser . parse_query_body_seeded ( SetPrecedence :: Zero , query ) ? ; Ok ( Either :: Query ( parser . parse_query_tail ( ctes , body ) ? ) ) } // The next token is something else. That implies we have a // partially-parsed expression (or a syntax error). Hop into // parsing an expression where `either` is the expression // prefix. ( either , _ ) => { let prefix = either . into_expr ( ) ; let expr = parser . parse_subexpr_seeded ( Precedence :: Zero , prefix ) ? ; Ok ( Either :: Expr ( expr ) . nest ( ) ) } } } }
Recursive helper for parsing parenthesized expressions.
Recursive helper for parsing parenthesized expressions .
parser
&mut Parser