Skip to content

Commit

Permalink
Remove is_current_token_postfix (#10855)
Browse files Browse the repository at this point in the history
## Summary

This PR removes the `is_current_token_postifx` method and instead
directly calls the `parse_postifx_expression`. The condition was
required because postfix expression parsing would reset the
`is_parenthesized` field on the `ParsedExpr`. Now, we'll make that
explicit.
  • Loading branch information
dhruvmanila committed Apr 16, 2024
1 parent 921f174 commit 75304d2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
16 changes: 11 additions & 5 deletions crates/ruff_python_parser/src/parser/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,8 @@ impl<'src> Parser<'src> {
/// be [`Precedence::Await`].
fn parse_lhs_expression(&mut self, previous_precedence: Precedence) -> ParsedExpr {
let start = self.node_start();
let mut lhs = match self.current_token_kind() {

let lhs = match self.current_token_kind() {
unary_tok @ (TokenKind::Plus | TokenKind::Minus | TokenKind::Tilde) => {
let unary_expr = self.parse_unary_expression();
if previous_precedence > Precedence::PosNegBitNot
Expand Down Expand Up @@ -448,11 +449,10 @@ impl<'src> Parser<'src> {
_ => self.parse_atom(),
};

if self.is_current_token_postfix() {
lhs = self.parse_postfix_expression(lhs.expr, start).into();
ParsedExpr {
expr: self.parse_postfix_expression(lhs.expr, start),
is_parenthesized: lhs.is_parenthesized,
}

lhs
}

/// Parses an expression with a minimum precedence of bitwise `or`.
Expand Down Expand Up @@ -669,6 +669,12 @@ impl<'src> Parser<'src> {
lhs.into()
}

/// Parses a postfix expression in a loop until there are no postfix expressions left to parse.
///
/// For a given left-hand side, a postfix expression can begin with either `(` for a call
/// expression, `[` for a subscript expression, or `.` for an attribute expression.
///
/// This method does nothing if the current token is not a candidate for a postfix expression.
pub(super) fn parse_postfix_expression(&mut self, mut lhs: Expr, start: TextSize) -> Expr {
loop {
lhs = match self.current_token_kind() {
Expand Down
7 changes: 0 additions & 7 deletions crates/ruff_python_parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,13 +634,6 @@ impl<'src> Parser<'src> {

false
}

fn is_current_token_postfix(&self) -> bool {
matches!(
self.current_token_kind(),
TokenKind::Lpar | TokenKind::Lsqb | TokenKind::Dot
)
}
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
Expand Down

0 comments on commit 75304d2

Please sign in to comment.