diff --git a/crates/ruff_python_parser/src/parser/expression.rs b/crates/ruff_python_parser/src/parser/expression.rs index 7c715a2893796..335d9394574f7 100644 --- a/crates/ruff_python_parser/src/parser/expression.rs +++ b/crates/ruff_python_parser/src/parser/expression.rs @@ -438,7 +438,7 @@ impl<'src> Parser<'src> { if let Expr::Name(name) = &first.expr && !first.is_parenthesized && !self.at(TokenKind::Lpar) - && (self.bracket_level() == 0 + && (self.tokens.bracket_level() == 0 || self.at(TokenKind::String) || matches!(name.id.as_str(), "print" | "exec")) && self.decoder_abort.is_none() @@ -466,7 +466,7 @@ impl<'src> Parser<'src> { let (first_kind, first_range) = self.token_after(first.start()); if (first_kind == TokenKind::Identifier && matches!(self.src_text(first_range), "_" | "case" | "match" | "type")) - || self.bracket_level() == 0 + || self.tokens.bracket_level() == 0 { return; } @@ -2490,7 +2490,7 @@ impl<'src> Parser<'src> { // t"{lambda x: x}" let outer_field = self .replacement_field - .replace((string_kind, self.bracket_level())); + .replace((string_kind, self.tokens.bracket_level())); let value = self.parse_expression_list(ExpressionContext::yield_or_starred_bitwise_or()); self.replacement_field = outer_field; self.check_replacement_field_expression( @@ -4002,7 +4002,7 @@ impl<'src> Parser<'src> { if !is_operand && self.at(TokenKind::Colon) && let Some((kind, level)) = self.replacement_field - && level == self.bracket_level() + && level == self.tokens.bracket_level() { // test_err lambda_in_replacement_field // f"{lambda x:x}" diff --git a/crates/ruff_python_parser/src/parser/mod.rs b/crates/ruff_python_parser/src/parser/mod.rs index e476da76f6fbe..c237b0490ae91 100644 --- a/crates/ruff_python_parser/src/parser/mod.rs +++ b/crates/ruff_python_parser/src/parser/mod.rs @@ -523,21 +523,6 @@ impl<'src> Parser<'src> { .map(|token| (token.kind(), token.range())) } - /// Returns how many brackets enclose the tokens bumped so far. - fn bracket_level(&self) -> usize { - let mut level = 0usize; - for token in self.tokens.bumped() { - match token.kind() { - TokenKind::Lpar | TokenKind::Lsqb | TokenKind::Lbrace => level += 1, - TokenKind::Rpar | TokenKind::Rsqb | TokenKind::Rbrace => { - level = level.saturating_sub(1); - } - _ => {} - } - } - level - } - /// Returns the innermost bracket enclosing the tokens bumped so far, or `None` if there is /// none or it starts an f-string or t-string replacement field. fn innermost_bracket(&self) -> Option { diff --git a/crates/ruff_python_parser/src/token_source.rs b/crates/ruff_python_parser/src/token_source.rs index 4241d07f7b394..9672400393e09 100644 --- a/crates/ruff_python_parser/src/token_source.rs +++ b/crates/ruff_python_parser/src/token_source.rs @@ -16,6 +16,9 @@ pub(crate) struct TokenSource<'src> { /// is finished consuming all the tokens. Note that unlike the emitted tokens, this vector /// holds both the trivia and non-trivia tokens. tokens: Vec, + + /// Unmatched opening brackets in the consumed token stream, not lexer lookahead. + bracket_level: usize, } impl<'src> TokenSource<'src> { @@ -24,6 +27,7 @@ impl<'src> TokenSource<'src> { TokenSource { lexer, tokens: allocate_tokens_vec(&source[start_offset.to_usize()..]), + bracket_level: 0, } } @@ -114,6 +118,7 @@ impl<'src> TokenSource<'src> { .0; // Trim the already bumped logical line token (and comments coming after it) as it might now have become a logical line token + // This suffix is only newline and comment trivia, so bracket_level is unchanged. self.tokens.truncate(non_logical_line_index); #[cfg(debug_assertions)] @@ -176,6 +181,13 @@ impl<'src> TokenSource<'src> { /// /// It pushes the given kind to the token vector with the current token range. pub(crate) fn bump(&mut self, kind: TokenKind) { + match kind { + TokenKind::Lpar | TokenKind::Lsqb | TokenKind::Lbrace => self.bracket_level += 1, + TokenKind::Rpar | TokenKind::Rsqb | TokenKind::Rbrace => { + self.bracket_level = self.bracket_level.saturating_sub(1); + } + _ => {} + } self.tokens .push(Token::new(kind, self.current_range(), self.current_flags())); self.do_bump(); @@ -211,6 +223,7 @@ impl<'src> TokenSource<'src> { TokenSourceCheckpoint { lexer_checkpoint: self.lexer.checkpoint(), tokens_position: self.tokens.len(), + bracket_level: self.bracket_level, } } @@ -219,10 +232,16 @@ impl<'src> TokenSource<'src> { let TokenSourceCheckpoint { lexer_checkpoint, tokens_position, + bracket_level, } = checkpoint; self.lexer.rewind(lexer_checkpoint); self.tokens.truncate(tokens_position); + self.bracket_level = bracket_level; + } + + pub(crate) const fn bracket_level(&self) -> usize { + self.bracket_level } /// Returns the tokens bumped so far, including the trivia tokens. @@ -270,6 +289,7 @@ impl<'src> TokenSource<'src> { pub(crate) struct TokenSourceCheckpoint { lexer_checkpoint: LexerCheckpoint, tokens_position: usize, + bracket_level: usize, } /// Allocates a token buffer with a capacity intended to skip early grows.