Consider binary expr for parenthesized with items parsing (#11012)

## Summary

This PR fixes the bug in with items parsing where it would fail to
recognize that the parenthesized expression is part of a large binary
expression.

## Test Plan

Add test cases and verified the snapshots.
This commit is contained in:
Dhruv Manilawala 2024-04-18 21:39:30 +05:30 committed by GitHub
parent 6c4d779140
commit b7066e64e7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 417 additions and 9 deletions

View file

@ -347,8 +347,18 @@ impl<'src> Parser<'src> {
/// [Pratt parsing algorithm]: https://matklad.github.io/2020/04/13/simple-but-powerful-pratt-parsing.html
fn parse_expression_with_precedence(&mut self, previous_precedence: Precedence) -> ParsedExpr {
let start = self.node_start();
let mut lhs = self.parse_lhs_expression(previous_precedence);
let lhs = self.parse_lhs_expression(previous_precedence);
self.parse_expression_with_precedence_recursive(lhs, previous_precedence, start)
}
/// Parses an expression with binding power of at least `previous_precedence` given the
/// left-hand side expression.
pub(super) fn parse_expression_with_precedence_recursive(
&mut self,
mut lhs: ParsedExpr,
previous_precedence: Precedence,
start: TextSize,
) -> ParsedExpr {
let mut progress = ParserProgress::default();
loop {
@ -2498,7 +2508,7 @@ enum Associativity {
///
/// See: <https://docs.python.org/3/reference/expressions.html#operator-precedence>
#[derive(Debug, Ord, Eq, PartialEq, PartialOrd, Copy, Clone)]
enum Precedence {
pub(super) enum Precedence {
/// Precedence for an unknown operator.
Unknown,
/// The initital precedence when parsing an expression.