From 60ead65155faca32cc06c7862a92258ad565365d Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Wed, 6 Sep 2023 23:14:28 +0530 Subject: [PATCH] Code review changes --- crates/ruff_python_parser/src/lexer.rs | 7 ++--- .../ruff_python_parser/src/lexer/fstring.rs | 28 ++++++++++--------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/crates/ruff_python_parser/src/lexer.rs b/crates/ruff_python_parser/src/lexer.rs index d0152819967a54..e92a240f8e059e 100644 --- a/crates/ruff_python_parser/src/lexer.rs +++ b/crates/ruff_python_parser/src/lexer.rs @@ -535,7 +535,7 @@ impl<'source> Lexer<'source> { } if self.cursor.eat_char2(quote, quote) { flags |= FStringContextFlags::TRIPLE; - }; + } self.fstrings.push(FStringContext::new(flags, self.nesting)); Tok::FStringStart @@ -780,16 +780,15 @@ impl<'source> Lexer<'source> { if !fstring.is_in_expression(self.nesting) { self.cursor.start_token(); if let Some(tok) = self.lex_fstring_middle_or_end()? { - if matches!(tok, Tok::FStringEnd) { + if tok == Tok::FStringEnd { self.fstrings.pop(); } return Ok((tok, self.token_range())); } } } - // Return dedent tokens until the current indentation level matches the indentation of the next token. - if let Some(indentation) = self.pending_indentation.take() { + else if let Some(indentation) = self.pending_indentation.take() { if let Ok(Ordering::Greater) = self.indentations.current().try_compare(indentation) { self.pending_indentation = Some(indentation); self.indentations.pop(); diff --git a/crates/ruff_python_parser/src/lexer/fstring.rs b/crates/ruff_python_parser/src/lexer/fstring.rs index c7d964e3297e69..bb4bd6567e34cb 100644 --- a/crates/ruff_python_parser/src/lexer/fstring.rs +++ b/crates/ruff_python_parser/src/lexer/fstring.rs @@ -4,7 +4,7 @@ use ruff_text_size::TextSize; bitflags! { #[derive(Debug)] - pub(crate) struct FStringContextFlags: u32 { + pub(crate) struct FStringContextFlags: u8 { /// The current f-string is a triple-quoted f-string i.e., the number of /// opening quotes is 3. If this flag is not set, the number of opening /// quotes is 1. @@ -35,7 +35,7 @@ pub(crate) struct FStringContext { } impl FStringContext { - pub(crate) fn new(flags: FStringContextFlags, nesting: u32) -> Self { + pub(crate) const fn new(flags: FStringContextFlags, nesting: u32) -> Self { Self { flags, format_spec_depth: 0, @@ -44,7 +44,7 @@ impl FStringContext { } /// Returns the quote character for the current f-string. - pub(crate) fn quote_char(&self) -> char { + pub(crate) const fn quote_char(&self) -> char { if self.flags.contains(FStringContextFlags::DOUBLE) { '"' } else { @@ -53,17 +53,17 @@ impl FStringContext { } /// Returns the number of quotes for the current f-string. - pub(crate) fn quote_size(&self) -> TextSize { + pub(crate) const fn quote_size(&self) -> TextSize { if self.is_triple_quoted() { - TextSize::from(3) + TextSize::new(3) } else { - TextSize::from(1) + TextSize::new(1) } } /// Returns the triple quotes for the current f-string if it is a triple-quoted /// f-string, `None` otherwise. - pub(crate) fn triple_quotes(&self) -> Option<&'static str> { + pub(crate) const fn triple_quotes(&self) -> Option<&'static str> { if self.is_triple_quoted() { if self.flags.contains(FStringContextFlags::DOUBLE) { Some(r#"""""#) @@ -76,32 +76,34 @@ impl FStringContext { } /// Returns `true` if the current f-string is a raw f-string. - pub(crate) fn is_raw_string(&self) -> bool { + pub(crate) const fn is_raw_string(&self) -> bool { self.flags.contains(FStringContextFlags::RAW) } /// Returns `true` if the current f-string is a triple-quoted f-string. - pub(crate) fn is_triple_quoted(&self) -> bool { + pub(crate) const fn is_triple_quoted(&self) -> bool { self.flags.contains(FStringContextFlags::TRIPLE) } - fn open_parentheses_count(&self, current_nesting: u32) -> u32 { + /// Calculates the number of open parentheses for the current f-string + /// based on the current level of nesting for the lexer. + const fn open_parentheses_count(&self, current_nesting: u32) -> u32 { current_nesting.saturating_sub(self.nesting) } /// Returns `true` if the current f-string has open parentheses. - pub(crate) fn has_open_parentheses(&self, current_nesting: u32) -> bool { + pub(crate) const fn has_open_parentheses(&self, current_nesting: u32) -> bool { self.open_parentheses_count(current_nesting) > 0 } /// Returns `true` if the lexer is in a f-string expression i.e., between /// two curly braces. - pub(crate) fn is_in_expression(&self, current_nesting: u32) -> bool { + pub(crate) const fn is_in_expression(&self, current_nesting: u32) -> bool { self.open_parentheses_count(current_nesting) > self.format_spec_depth } /// Returns `true` if the lexer is in a f-string format spec i.e., after a colon. - pub(crate) fn is_in_format_spec(&self, current_nesting: u32) -> bool { + pub(crate) const fn is_in_format_spec(&self, current_nesting: u32) -> bool { self.format_spec_depth > 0 && !self.is_in_expression(current_nesting) }