Rename AnyStringKind -> AnyStringFlags (#11405)

## Summary

This PR renames `AnyStringKind` to `AnyStringFlags` and `AnyStringFlags`
to `AnyStringFlagsInner`.

The main motivation is to have consistent usage of "kind" and "flags".
For each string kind, it's "flags" like `StringLiteralFlags`,
`BytesLiteralFlags`, and `FStringFlags` but it was `AnyStringKind` for
the "any" variant.
This commit is contained in:
Dhruv Manilawala 2024-05-13 18:48:07 +05:30 committed by GitHub
parent be0ccabbaa
commit 6ecb4776de
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
54 changed files with 378 additions and 371 deletions

View file

@ -131,7 +131,7 @@ fn extract_noqa_line_for(lxr: &[LexResult], locator: &Locator, indexer: &Indexer
// For multi-line strings, we expect `noqa` directives on the last line of the // For multi-line strings, we expect `noqa` directives on the last line of the
// string. // string.
Tok::String { kind, .. } if kind.is_triple_quoted() => { Tok::String { flags, .. } if flags.is_triple_quoted() => {
if locator.contains_line_break(*range) { if locator.contains_line_break(*range) {
string_mappings.push(TextRange::new( string_mappings.push(TextRange::new(
locator.line_start(range.start()), locator.line_start(range.start()),

View file

@ -1,10 +1,10 @@
use ruff_python_ast::AnyStringKind; use ruff_python_ast::AnyStringFlags;
use ruff_text_size::TextLen; use ruff_text_size::TextLen;
/// Returns the raw contents of the string given the string's contents and kind. /// Returns the raw contents of the string given the string's contents and flags.
/// This is a string without the prefix and quotes. /// This is a string without the prefix and quotes.
pub(super) fn raw_contents(contents: &str, kind: AnyStringKind) -> &str { pub(super) fn raw_contents(contents: &str, flags: AnyStringFlags) -> &str {
&contents[kind.opener_len().to_usize()..(contents.text_len() - kind.closer_len()).to_usize()] &contents[flags.opener_len().to_usize()..(contents.text_len() - flags.closer_len()).to_usize()]
} }
/// Return `true` if the haystack contains an escaped quote. /// Return `true` if the haystack contains an escaped quote.

View file

@ -1,7 +1,7 @@
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix}; use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation}; use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::visitor::{walk_f_string, Visitor}; use ruff_python_ast::visitor::{walk_f_string, Visitor};
use ruff_python_ast::{self as ast, AnyStringKind, StringLike}; use ruff_python_ast::{self as ast, AnyStringFlags, StringLike};
use ruff_source_file::Locator; use ruff_source_file::Locator;
use ruff_text_size::{Ranged, TextRange, TextSize}; use ruff_text_size::{Ranged, TextRange, TextSize};
@ -123,7 +123,7 @@ impl Visitor<'_> for AvoidableEscapedQuoteChecker<'_> {
self.locator, self.locator,
self.quotes_settings, self.quotes_settings,
string_literal.range(), string_literal.range(),
AnyStringKind::from(string_literal.flags), AnyStringFlags::from(string_literal.flags),
) { ) {
self.diagnostics.push(diagnostic); self.diagnostics.push(diagnostic);
} }
@ -134,7 +134,7 @@ impl Visitor<'_> for AvoidableEscapedQuoteChecker<'_> {
self.locator, self.locator,
self.quotes_settings, self.quotes_settings,
bytes_literal.range(), bytes_literal.range(),
AnyStringKind::from(bytes_literal.flags), AnyStringFlags::from(bytes_literal.flags),
) { ) {
self.diagnostics.push(diagnostic); self.diagnostics.push(diagnostic);
} }
@ -226,20 +226,20 @@ fn check_string_or_bytes(
locator: &Locator, locator: &Locator,
quotes_settings: &flake8_quotes::settings::Settings, quotes_settings: &flake8_quotes::settings::Settings,
range: TextRange, range: TextRange,
kind: AnyStringKind, flags: AnyStringFlags,
) -> Option<Diagnostic> { ) -> Option<Diagnostic> {
assert!(!kind.is_f_string()); assert!(!flags.is_f_string());
if kind.is_triple_quoted() || kind.is_raw_string() { if flags.is_triple_quoted() || flags.is_raw_string() {
return None; return None;
} }
// Check if we're using the preferred quotation style. // Check if we're using the preferred quotation style.
if Quote::from(kind.quote_style()) != quotes_settings.inline_quotes { if Quote::from(flags.quote_style()) != quotes_settings.inline_quotes {
return None; return None;
} }
let contents = raw_contents(locator.slice(range), kind); let contents = raw_contents(locator.slice(range), flags);
if !contains_escaped_quote(contents, quotes_settings.inline_quotes.as_char()) if !contains_escaped_quote(contents, quotes_settings.inline_quotes.as_char())
|| contains_quote(contents, quotes_settings.inline_quotes.opposite().as_char()) || contains_quote(contents, quotes_settings.inline_quotes.opposite().as_char())
@ -250,7 +250,7 @@ fn check_string_or_bytes(
let mut diagnostic = Diagnostic::new(AvoidableEscapedQuote, range); let mut diagnostic = Diagnostic::new(AvoidableEscapedQuote, range);
let fixed_contents = format!( let fixed_contents = format!(
"{prefix}{quote}{value}{quote}", "{prefix}{quote}{value}{quote}",
prefix = kind.prefix(), prefix = flags.prefix(),
quote = quotes_settings.inline_quotes.opposite().as_char(), quote = quotes_settings.inline_quotes.opposite().as_char(),
value = unescape_string(contents, quotes_settings.inline_quotes.as_char()) value = unescape_string(contents, quotes_settings.inline_quotes.as_char())
); );

View file

@ -1,6 +1,6 @@
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix}; use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation}; use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::{self as ast, AnyStringKind, StringLike}; use ruff_python_ast::{self as ast, AnyStringFlags, StringLike};
use ruff_source_file::Locator; use ruff_source_file::Locator;
use ruff_text_size::{Ranged, TextRange}; use ruff_text_size::{Ranged, TextRange};
@ -59,7 +59,7 @@ pub(crate) fn unnecessary_escaped_quote(checker: &mut Checker, string_like: Stri
if let Some(diagnostic) = check_string_or_bytes( if let Some(diagnostic) = check_string_or_bytes(
locator, locator,
string.range(), string.range(),
AnyStringKind::from(string.flags), AnyStringFlags::from(string.flags),
) { ) {
checker.diagnostics.push(diagnostic); checker.diagnostics.push(diagnostic);
} }
@ -68,7 +68,7 @@ pub(crate) fn unnecessary_escaped_quote(checker: &mut Checker, string_like: Stri
StringLike::Bytes(expr) => { StringLike::Bytes(expr) => {
for bytes in &expr.value { for bytes in &expr.value {
if let Some(diagnostic) = if let Some(diagnostic) =
check_string_or_bytes(locator, bytes.range(), AnyStringKind::from(bytes.flags)) check_string_or_bytes(locator, bytes.range(), AnyStringFlags::from(bytes.flags))
{ {
checker.diagnostics.push(diagnostic); checker.diagnostics.push(diagnostic);
} }
@ -80,7 +80,7 @@ pub(crate) fn unnecessary_escaped_quote(checker: &mut Checker, string_like: Stri
ast::FStringPart::Literal(string) => check_string_or_bytes( ast::FStringPart::Literal(string) => check_string_or_bytes(
locator, locator,
string.range(), string.range(),
AnyStringKind::from(string.flags), AnyStringFlags::from(string.flags),
), ),
ast::FStringPart::FString(f_string) => check_f_string(locator, f_string), ast::FStringPart::FString(f_string) => check_f_string(locator, f_string),
} { } {
@ -99,16 +99,16 @@ pub(crate) fn unnecessary_escaped_quote(checker: &mut Checker, string_like: Stri
fn check_string_or_bytes( fn check_string_or_bytes(
locator: &Locator, locator: &Locator,
range: TextRange, range: TextRange,
kind: AnyStringKind, flags: AnyStringFlags,
) -> Option<Diagnostic> { ) -> Option<Diagnostic> {
assert!(!kind.is_f_string()); assert!(!flags.is_f_string());
if kind.is_triple_quoted() || kind.is_raw_string() { if flags.is_triple_quoted() || flags.is_raw_string() {
return None; return None;
} }
let contents = raw_contents(locator.slice(range), kind); let contents = raw_contents(locator.slice(range), flags);
let quote = kind.quote_style(); let quote = flags.quote_style();
let opposite_quote_char = quote.opposite().as_char(); let opposite_quote_char = quote.opposite().as_char();
if !contains_escaped_quote(contents, opposite_quote_char) { if !contains_escaped_quote(contents, opposite_quote_char) {
@ -117,7 +117,7 @@ fn check_string_or_bytes(
let mut diagnostic = Diagnostic::new(UnnecessaryEscapedQuote, range); let mut diagnostic = Diagnostic::new(UnnecessaryEscapedQuote, range);
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement( diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
kind.format_string_contents(&unescape_string(contents, opposite_quote_char)), flags.format_string_contents(&unescape_string(contents, opposite_quote_char)),
range, range,
))); )));
Some(diagnostic) Some(diagnostic)

View file

@ -66,22 +66,22 @@ pub(crate) fn invalid_escape_sequence(
token: &Tok, token: &Tok,
token_range: TextRange, token_range: TextRange,
) { ) {
let (token_source_code, string_start_location, kind) = match token { let (token_source_code, string_start_location, flags) = match token {
Tok::FStringMiddle { kind, .. } => { Tok::FStringMiddle { flags, .. } => {
if kind.is_raw_string() { if flags.is_raw_string() {
return; return;
} }
let Some(f_string_range) = indexer.fstring_ranges().innermost(token_range.start()) let Some(f_string_range) = indexer.fstring_ranges().innermost(token_range.start())
else { else {
return; return;
}; };
(locator.slice(token_range), f_string_range.start(), kind) (locator.slice(token_range), f_string_range.start(), flags)
} }
Tok::String { kind, .. } => { Tok::String { flags, .. } => {
if kind.is_raw_string() { if flags.is_raw_string() {
return; return;
} }
(locator.slice(token_range), token_range.start(), kind) (locator.slice(token_range), token_range.start(), flags)
} }
_ => return, _ => return,
}; };
@ -208,7 +208,7 @@ pub(crate) fn invalid_escape_sequence(
invalid_escape_char.range(), invalid_escape_char.range(),
); );
if kind.is_u_string() { if flags.is_u_string() {
// Replace the Unicode prefix with `r`. // Replace the Unicode prefix with `r`.
diagnostic.set_fix(Fix::safe_edit(Edit::replacement( diagnostic.set_fix(Fix::safe_edit(Edit::replacement(
"r".to_string(), "r".to_string(),

View file

@ -2,7 +2,7 @@ use std::str::FromStr;
use ruff_diagnostics::{Diagnostic, Violation}; use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation}; use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::{AnyStringKind, Expr}; use ruff_python_ast::{AnyStringFlags, Expr};
use ruff_python_literal::{ use ruff_python_literal::{
cformat::{CFormatErrorType, CFormatString}, cformat::{CFormatErrorType, CFormatString},
format::FormatPart, format::FormatPart,
@ -92,12 +92,12 @@ pub(crate) fn call(checker: &mut Checker, string: &str, range: TextRange) {
/// Ex) `"%z" % "1"` /// Ex) `"%z" % "1"`
pub(crate) fn percent(checker: &mut Checker, expr: &Expr) { pub(crate) fn percent(checker: &mut Checker, expr: &Expr) {
// Grab each string segment (in case there's an implicit concatenation). // Grab each string segment (in case there's an implicit concatenation).
let mut strings: Vec<(TextRange, AnyStringKind)> = vec![]; let mut strings: Vec<(TextRange, AnyStringFlags)> = vec![];
for (tok, range) in for (tok, range) in
lexer::lex_starts_at(checker.locator().slice(expr), Mode::Module, expr.start()).flatten() lexer::lex_starts_at(checker.locator().slice(expr), Mode::Module, expr.start()).flatten()
{ {
match tok { match tok {
Tok::String { kind, .. } => strings.push((range, kind)), Tok::String { flags, .. } => strings.push((range, flags)),
// Break as soon as we find the modulo symbol. // Break as soon as we find the modulo symbol.
Tok::Percent => break, Tok::Percent => break,
_ => {} _ => {}
@ -109,10 +109,10 @@ pub(crate) fn percent(checker: &mut Checker, expr: &Expr) {
return; return;
} }
for (range, kind) in &strings { for (range, flags) in &strings {
let string = checker.locator().slice(*range); let string = checker.locator().slice(*range);
let string = &string let string = &string
[usize::from(kind.opener_len())..(string.len() - usize::from(kind.closer_len()))]; [usize::from(flags.opener_len())..(string.len() - usize::from(flags.closer_len()))];
// Parse the format string (e.g. `"%s"`) into a list of `PercentFormat`. // Parse the format string (e.g. `"%s"`) into a list of `PercentFormat`.
if let Err(format_error) = CFormatString::from_str(string) { if let Err(format_error) = CFormatString::from_str(string) {

View file

@ -1,6 +1,6 @@
use std::str::FromStr; use std::str::FromStr;
use ruff_python_ast::{self as ast, AnyStringKind, Expr}; use ruff_python_ast::{self as ast, AnyStringFlags, Expr};
use ruff_python_literal::cformat::{CFormatPart, CFormatSpec, CFormatStrOrBytes, CFormatString}; use ruff_python_literal::cformat::{CFormatPart, CFormatSpec, CFormatStrOrBytes, CFormatString};
use ruff_python_parser::{lexer, AsMode, Tok}; use ruff_python_parser::{lexer, AsMode, Tok};
use ruff_text_size::{Ranged, TextRange}; use ruff_text_size::{Ranged, TextRange};
@ -214,12 +214,12 @@ fn is_valid_dict(formats: &[CFormatStrOrBytes<String>], items: &[ast::DictItem])
pub(crate) fn bad_string_format_type(checker: &mut Checker, expr: &Expr, right: &Expr) { pub(crate) fn bad_string_format_type(checker: &mut Checker, expr: &Expr, right: &Expr) {
// Grab each string segment (in case there's an implicit concatenation). // Grab each string segment (in case there's an implicit concatenation).
let content = checker.locator().slice(expr); let content = checker.locator().slice(expr);
let mut strings: Vec<(TextRange, AnyStringKind)> = vec![]; let mut strings: Vec<(TextRange, AnyStringFlags)> = vec![];
for (tok, range) in for (tok, range) in
lexer::lex_starts_at(content, checker.source_type.as_mode(), expr.start()).flatten() lexer::lex_starts_at(content, checker.source_type.as_mode(), expr.start()).flatten()
{ {
match tok { match tok {
Tok::String { kind, .. } => strings.push((range, kind)), Tok::String { flags, .. } => strings.push((range, flags)),
// Break as soon as we find the modulo symbol. // Break as soon as we find the modulo symbol.
Tok::Percent => break, Tok::Percent => break,
_ => {} _ => {}

View file

@ -4,7 +4,7 @@ use std::str::FromStr;
use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation}; use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation};
use ruff_macros::{derive_message_formats, violation}; use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::whitespace::indentation; use ruff_python_ast::whitespace::indentation;
use ruff_python_ast::{self as ast, AnyStringKind, Expr}; use ruff_python_ast::{self as ast, AnyStringFlags, Expr};
use ruff_python_codegen::Stylist; use ruff_python_codegen::Stylist;
use ruff_python_literal::cformat::{ use ruff_python_literal::cformat::{
CConversionFlags, CFormatPart, CFormatPrecision, CFormatQuantity, CFormatString, CConversionFlags, CFormatPart, CFormatPrecision, CFormatQuantity, CFormatString,
@ -347,7 +347,7 @@ fn convertible(format_string: &CFormatString, params: &Expr) -> bool {
/// UP031 /// UP031
pub(crate) fn printf_string_formatting(checker: &mut Checker, expr: &Expr, right: &Expr) { pub(crate) fn printf_string_formatting(checker: &mut Checker, expr: &Expr, right: &Expr) {
// Grab each string segment (in case there's an implicit concatenation). // Grab each string segment (in case there's an implicit concatenation).
let mut strings: Vec<(TextRange, AnyStringKind)> = vec![]; let mut strings: Vec<(TextRange, AnyStringFlags)> = vec![];
let mut extension = None; let mut extension = None;
for (tok, range) in lexer::lex_starts_at( for (tok, range) in lexer::lex_starts_at(
checker.locator().slice(expr), checker.locator().slice(expr),
@ -357,7 +357,7 @@ pub(crate) fn printf_string_formatting(checker: &mut Checker, expr: &Expr, right
.flatten() .flatten()
{ {
match tok { match tok {
Tok::String { kind, .. } => strings.push((range, kind)), Tok::String { flags, .. } => strings.push((range, flags)),
// If we hit a right paren, we have to preserve it. // If we hit a right paren, we have to preserve it.
Tok::Rpar => extension = Some(range), Tok::Rpar => extension = Some(range),
// Break as soon as we find the modulo symbol. // Break as soon as we find the modulo symbol.
@ -375,10 +375,10 @@ pub(crate) fn printf_string_formatting(checker: &mut Checker, expr: &Expr, right
let mut num_positional_arguments = 0; let mut num_positional_arguments = 0;
let mut num_keyword_arguments = 0; let mut num_keyword_arguments = 0;
let mut format_strings = Vec::with_capacity(strings.len()); let mut format_strings = Vec::with_capacity(strings.len());
for (range, kind) in &strings { for (range, flags) in &strings {
let string = checker.locator().slice(*range); let string = checker.locator().slice(*range);
let string = &string let string = &string
[usize::from(kind.opener_len())..(string.len() - usize::from(kind.closer_len()))]; [usize::from(flags.opener_len())..(string.len() - usize::from(flags.closer_len()))];
// Parse the format string (e.g. `"%s"`) into a list of `PercentFormat`. // Parse the format string (e.g. `"%s"`) into a list of `PercentFormat`.
let Ok(format_string) = CFormatString::from_str(string) else { let Ok(format_string) = CFormatString::from_str(string) else {
@ -401,7 +401,7 @@ pub(crate) fn printf_string_formatting(checker: &mut Checker, expr: &Expr, right
} }
// Convert the `%`-format string to a `.format` string. // Convert the `%`-format string to a `.format` string.
format_strings.push(kind.format_string_contents(&percent_to_format(&format_string))); format_strings.push(flags.format_string_contents(&percent_to_format(&format_string)));
} }
// Parse the parameters. // Parse the parameters.

View file

@ -2321,7 +2321,7 @@ bitflags! {
/// prefix flags is by calling the `as_flags()` method on the /// prefix flags is by calling the `as_flags()` method on the
/// `StringPrefix` enum. /// `StringPrefix` enum.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Hash)] #[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Hash)]
struct AnyStringFlags: u8 { struct AnyStringFlagsInner: u8 {
/// The string uses double quotes (`"`). /// The string uses double quotes (`"`).
/// If this flag is not set, the string uses single quotes (`'`). /// If this flag is not set, the string uses single quotes (`'`).
const DOUBLE = 1 << 0; const DOUBLE = 1 << 0;
@ -2425,71 +2425,71 @@ impl TryFrom<[char; 2]> for AnyStringPrefix {
} }
impl AnyStringPrefix { impl AnyStringPrefix {
const fn as_flags(self) -> AnyStringFlags { const fn as_flags(self) -> AnyStringFlagsInner {
match self { match self {
// regular strings // regular strings
Self::Regular(StringLiteralPrefix::Empty) => AnyStringFlags::empty(), Self::Regular(StringLiteralPrefix::Empty) => AnyStringFlagsInner::empty(),
Self::Regular(StringLiteralPrefix::Unicode) => AnyStringFlags::U_PREFIX, Self::Regular(StringLiteralPrefix::Unicode) => AnyStringFlagsInner::U_PREFIX,
Self::Regular(StringLiteralPrefix::Raw { uppercase: false }) => { Self::Regular(StringLiteralPrefix::Raw { uppercase: false }) => {
AnyStringFlags::R_PREFIX_LOWER AnyStringFlagsInner::R_PREFIX_LOWER
} }
Self::Regular(StringLiteralPrefix::Raw { uppercase: true }) => { Self::Regular(StringLiteralPrefix::Raw { uppercase: true }) => {
AnyStringFlags::R_PREFIX_UPPER AnyStringFlagsInner::R_PREFIX_UPPER
} }
// bytestrings // bytestrings
Self::Bytes(ByteStringPrefix::Regular) => AnyStringFlags::B_PREFIX, Self::Bytes(ByteStringPrefix::Regular) => AnyStringFlagsInner::B_PREFIX,
Self::Bytes(ByteStringPrefix::Raw { uppercase_r: false }) => { Self::Bytes(ByteStringPrefix::Raw { uppercase_r: false }) => {
AnyStringFlags::B_PREFIX.union(AnyStringFlags::R_PREFIX_LOWER) AnyStringFlagsInner::B_PREFIX.union(AnyStringFlagsInner::R_PREFIX_LOWER)
} }
Self::Bytes(ByteStringPrefix::Raw { uppercase_r: true }) => { Self::Bytes(ByteStringPrefix::Raw { uppercase_r: true }) => {
AnyStringFlags::B_PREFIX.union(AnyStringFlags::R_PREFIX_UPPER) AnyStringFlagsInner::B_PREFIX.union(AnyStringFlagsInner::R_PREFIX_UPPER)
} }
// f-strings // f-strings
Self::Format(FStringPrefix::Regular) => AnyStringFlags::F_PREFIX, Self::Format(FStringPrefix::Regular) => AnyStringFlagsInner::F_PREFIX,
Self::Format(FStringPrefix::Raw { uppercase_r: false }) => { Self::Format(FStringPrefix::Raw { uppercase_r: false }) => {
AnyStringFlags::F_PREFIX.union(AnyStringFlags::R_PREFIX_LOWER) AnyStringFlagsInner::F_PREFIX.union(AnyStringFlagsInner::R_PREFIX_LOWER)
} }
Self::Format(FStringPrefix::Raw { uppercase_r: true }) => { Self::Format(FStringPrefix::Raw { uppercase_r: true }) => {
AnyStringFlags::F_PREFIX.union(AnyStringFlags::R_PREFIX_UPPER) AnyStringFlagsInner::F_PREFIX.union(AnyStringFlagsInner::R_PREFIX_UPPER)
} }
} }
} }
const fn from_kind(kind: AnyStringKind) -> Self { const fn from_kind(kind: AnyStringFlags) -> Self {
let AnyStringKind(flags) = kind; let AnyStringFlags(flags) = kind;
// f-strings // f-strings
if flags.contains(AnyStringFlags::F_PREFIX) { if flags.contains(AnyStringFlagsInner::F_PREFIX) {
if flags.contains(AnyStringFlags::R_PREFIX_LOWER) { if flags.contains(AnyStringFlagsInner::R_PREFIX_LOWER) {
return Self::Format(FStringPrefix::Raw { uppercase_r: false }); return Self::Format(FStringPrefix::Raw { uppercase_r: false });
} }
if flags.contains(AnyStringFlags::R_PREFIX_UPPER) { if flags.contains(AnyStringFlagsInner::R_PREFIX_UPPER) {
return Self::Format(FStringPrefix::Raw { uppercase_r: true }); return Self::Format(FStringPrefix::Raw { uppercase_r: true });
} }
return Self::Format(FStringPrefix::Regular); return Self::Format(FStringPrefix::Regular);
} }
// bytestrings // bytestrings
if flags.contains(AnyStringFlags::B_PREFIX) { if flags.contains(AnyStringFlagsInner::B_PREFIX) {
if flags.contains(AnyStringFlags::R_PREFIX_LOWER) { if flags.contains(AnyStringFlagsInner::R_PREFIX_LOWER) {
return Self::Bytes(ByteStringPrefix::Raw { uppercase_r: false }); return Self::Bytes(ByteStringPrefix::Raw { uppercase_r: false });
} }
if flags.contains(AnyStringFlags::R_PREFIX_UPPER) { if flags.contains(AnyStringFlagsInner::R_PREFIX_UPPER) {
return Self::Bytes(ByteStringPrefix::Raw { uppercase_r: true }); return Self::Bytes(ByteStringPrefix::Raw { uppercase_r: true });
} }
return Self::Bytes(ByteStringPrefix::Regular); return Self::Bytes(ByteStringPrefix::Regular);
} }
// all other strings // all other strings
if flags.contains(AnyStringFlags::R_PREFIX_LOWER) { if flags.contains(AnyStringFlagsInner::R_PREFIX_LOWER) {
return Self::Regular(StringLiteralPrefix::Raw { uppercase: false }); return Self::Regular(StringLiteralPrefix::Raw { uppercase: false });
} }
if flags.contains(AnyStringFlags::R_PREFIX_UPPER) { if flags.contains(AnyStringFlagsInner::R_PREFIX_UPPER) {
return Self::Regular(StringLiteralPrefix::Raw { uppercase: true }); return Self::Regular(StringLiteralPrefix::Raw { uppercase: true });
} }
if flags.contains(AnyStringFlags::U_PREFIX) { if flags.contains(AnyStringFlagsInner::U_PREFIX) {
return Self::Regular(StringLiteralPrefix::Unicode); return Self::Regular(StringLiteralPrefix::Unicode);
} }
Self::Regular(StringLiteralPrefix::Empty) Self::Regular(StringLiteralPrefix::Empty)
@ -2517,9 +2517,9 @@ impl Default for AnyStringPrefix {
} }
#[derive(Default, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Default, Clone, Copy, PartialEq, Eq, Hash)]
pub struct AnyStringKind(AnyStringFlags); pub struct AnyStringFlags(AnyStringFlagsInner);
impl AnyStringKind { impl AnyStringFlags {
#[must_use] #[must_use]
pub fn with_prefix(mut self, prefix: AnyStringPrefix) -> Self { pub fn with_prefix(mut self, prefix: AnyStringPrefix) -> Self {
self.0 |= prefix.as_flags(); self.0 |= prefix.as_flags();
@ -2541,28 +2541,29 @@ impl AnyStringKind {
/// Does the string have a `u` or `U` prefix? /// Does the string have a `u` or `U` prefix?
pub const fn is_u_string(self) -> bool { pub const fn is_u_string(self) -> bool {
self.0.contains(AnyStringFlags::U_PREFIX) self.0.contains(AnyStringFlagsInner::U_PREFIX)
} }
/// Does the string have an `r` or `R` prefix? /// Does the string have an `r` or `R` prefix?
pub const fn is_raw_string(self) -> bool { pub const fn is_raw_string(self) -> bool {
self.0 self.0.intersects(
.intersects(AnyStringFlags::R_PREFIX_LOWER.union(AnyStringFlags::R_PREFIX_UPPER)) AnyStringFlagsInner::R_PREFIX_LOWER.union(AnyStringFlagsInner::R_PREFIX_UPPER),
)
} }
/// Does the string have an `f` or `F` prefix? /// Does the string have an `f` or `F` prefix?
pub const fn is_f_string(self) -> bool { pub const fn is_f_string(self) -> bool {
self.0.contains(AnyStringFlags::F_PREFIX) self.0.contains(AnyStringFlagsInner::F_PREFIX)
} }
/// Does the string have a `b` or `B` prefix? /// Does the string have a `b` or `B` prefix?
pub const fn is_byte_string(self) -> bool { pub const fn is_byte_string(self) -> bool {
self.0.contains(AnyStringFlags::B_PREFIX) self.0.contains(AnyStringFlagsInner::B_PREFIX)
} }
/// Does the string use single or double quotes in its opener and closer? /// Does the string use single or double quotes in its opener and closer?
pub const fn quote_style(self) -> Quote { pub const fn quote_style(self) -> Quote {
if self.0.contains(AnyStringFlags::DOUBLE) { if self.0.contains(AnyStringFlagsInner::DOUBLE) {
Quote::Double Quote::Double
} else { } else {
Quote::Single Quote::Single
@ -2572,7 +2573,7 @@ impl AnyStringKind {
/// Is the string triple-quoted, i.e., /// Is the string triple-quoted, i.e.,
/// does it begin and end with three consecutive quote characters? /// does it begin and end with three consecutive quote characters?
pub const fn is_triple_quoted(self) -> bool { pub const fn is_triple_quoted(self) -> bool {
self.0.contains(AnyStringFlags::TRIPLE_QUOTED) self.0.contains(AnyStringFlagsInner::TRIPLE_QUOTED)
} }
/// A `str` representation of the quotes used to start and close. /// A `str` representation of the quotes used to start and close.
@ -2634,22 +2635,22 @@ impl AnyStringKind {
#[must_use] #[must_use]
pub fn with_quote_style(mut self, quotes: Quote) -> Self { pub fn with_quote_style(mut self, quotes: Quote) -> Self {
match quotes { match quotes {
Quote::Double => self.0 |= AnyStringFlags::DOUBLE, Quote::Double => self.0 |= AnyStringFlagsInner::DOUBLE,
Quote::Single => self.0 -= AnyStringFlags::DOUBLE, Quote::Single => self.0 -= AnyStringFlagsInner::DOUBLE,
}; };
self self
} }
#[must_use] #[must_use]
pub fn with_triple_quotes(mut self) -> Self { pub fn with_triple_quotes(mut self) -> Self {
self.0 |= AnyStringFlags::TRIPLE_QUOTED; self.0 |= AnyStringFlagsInner::TRIPLE_QUOTED;
self self
} }
} }
impl fmt::Debug for AnyStringKind { impl fmt::Debug for AnyStringFlags {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("StringKind") f.debug_struct("AnyStringFlags")
.field("prefix", &self.prefix()) .field("prefix", &self.prefix())
.field("triple_quoted", &self.is_triple_quoted()) .field("triple_quoted", &self.is_triple_quoted())
.field("quote_style", &self.quote_style()) .field("quote_style", &self.quote_style())
@ -2657,8 +2658,8 @@ impl fmt::Debug for AnyStringKind {
} }
} }
impl From<AnyStringKind> for StringLiteralFlags { impl From<AnyStringFlags> for StringLiteralFlags {
fn from(value: AnyStringKind) -> StringLiteralFlags { fn from(value: AnyStringFlags) -> StringLiteralFlags {
let AnyStringPrefix::Regular(prefix) = value.prefix() else { let AnyStringPrefix::Regular(prefix) = value.prefix() else {
unreachable!( unreachable!(
"Should never attempt to convert {} into a regular string", "Should never attempt to convert {} into a regular string",
@ -2676,7 +2677,7 @@ impl From<AnyStringKind> for StringLiteralFlags {
} }
} }
impl From<StringLiteralFlags> for AnyStringKind { impl From<StringLiteralFlags> for AnyStringFlags {
fn from(value: StringLiteralFlags) -> Self { fn from(value: StringLiteralFlags) -> Self {
Self::new( Self::new(
AnyStringPrefix::Regular(value.prefix()), AnyStringPrefix::Regular(value.prefix()),
@ -2686,8 +2687,8 @@ impl From<StringLiteralFlags> for AnyStringKind {
} }
} }
impl From<AnyStringKind> for BytesLiteralFlags { impl From<AnyStringFlags> for BytesLiteralFlags {
fn from(value: AnyStringKind) -> BytesLiteralFlags { fn from(value: AnyStringFlags) -> BytesLiteralFlags {
let AnyStringPrefix::Bytes(bytestring_prefix) = value.prefix() else { let AnyStringPrefix::Bytes(bytestring_prefix) = value.prefix() else {
unreachable!( unreachable!(
"Should never attempt to convert {} into a bytestring", "Should never attempt to convert {} into a bytestring",
@ -2705,7 +2706,7 @@ impl From<AnyStringKind> for BytesLiteralFlags {
} }
} }
impl From<BytesLiteralFlags> for AnyStringKind { impl From<BytesLiteralFlags> for AnyStringFlags {
fn from(value: BytesLiteralFlags) -> Self { fn from(value: BytesLiteralFlags) -> Self {
Self::new( Self::new(
AnyStringPrefix::Bytes(value.prefix()), AnyStringPrefix::Bytes(value.prefix()),
@ -2715,8 +2716,8 @@ impl From<BytesLiteralFlags> for AnyStringKind {
} }
} }
impl From<AnyStringKind> for FStringFlags { impl From<AnyStringFlags> for FStringFlags {
fn from(value: AnyStringKind) -> FStringFlags { fn from(value: AnyStringFlags) -> FStringFlags {
let AnyStringPrefix::Format(fstring_prefix) = value.prefix() else { let AnyStringPrefix::Format(fstring_prefix) = value.prefix() else {
unreachable!( unreachable!(
"Should never attempt to convert {} into an f-string", "Should never attempt to convert {} into an f-string",
@ -2734,7 +2735,7 @@ impl From<AnyStringKind> for FStringFlags {
} }
} }
impl From<FStringFlags> for AnyStringKind { impl From<FStringFlags> for AnyStringFlags {
fn from(value: FStringFlags) -> Self { fn from(value: FStringFlags) -> Self {
Self::new( Self::new(
AnyStringPrefix::Format(value.prefix()), AnyStringPrefix::Format(value.prefix()),

View file

@ -50,8 +50,8 @@ impl<'a> Stylist<'a> {
fn detect_quote(tokens: &[LexResult]) -> Quote { fn detect_quote(tokens: &[LexResult]) -> Quote {
for (token, _) in tokens.iter().flatten() { for (token, _) in tokens.iter().flatten() {
match token { match token {
Tok::String { kind, .. } if !kind.is_triple_quoted() => return kind.quote_style(), Tok::String { flags, .. } if !flags.is_triple_quoted() => return flags.quote_style(),
Tok::FStringStart(kind) => return kind.quote_style(), Tok::FStringStart(flags) => return flags.quote_style(),
_ => continue, _ => continue,
} }
} }

View file

@ -1,5 +1,5 @@
use ruff_formatter::write; use ruff_formatter::write;
use ruff_python_ast::{AnyStringKind, FString}; use ruff_python_ast::{AnyStringFlags, FString};
use ruff_source_file::Locator; use ruff_source_file::Locator;
use crate::prelude::*; use crate::prelude::*;
@ -56,7 +56,9 @@ impl Format<PyFormatContext<'_>> for FormatFString<'_> {
return result; return result;
} }
let string_kind = normalizer.choose_quotes(self.value.into(), &locator).kind(); let string_kind = normalizer
.choose_quotes(self.value.into(), &locator)
.flags();
let context = FStringContext::new( let context = FStringContext::new(
string_kind, string_kind,
@ -83,17 +85,17 @@ impl Format<PyFormatContext<'_>> for FormatFString<'_> {
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub(crate) struct FStringContext { pub(crate) struct FStringContext {
kind: AnyStringKind, flags: AnyStringFlags,
layout: FStringLayout, layout: FStringLayout,
} }
impl FStringContext { impl FStringContext {
const fn new(kind: AnyStringKind, layout: FStringLayout) -> Self { const fn new(flags: AnyStringFlags, layout: FStringLayout) -> Self {
Self { kind, layout } Self { flags, layout }
} }
pub(crate) fn kind(self) -> AnyStringKind { pub(crate) fn flags(self) -> AnyStringFlags {
self.kind self.flags
} }
pub(crate) const fn layout(self) -> FStringLayout { pub(crate) const fn layout(self) -> FStringLayout {

View file

@ -56,7 +56,7 @@ impl<'a> FormatFStringLiteralElement<'a> {
impl Format<PyFormatContext<'_>> for FormatFStringLiteralElement<'_> { impl Format<PyFormatContext<'_>> for FormatFStringLiteralElement<'_> {
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> { fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
let literal_content = f.context().locator().slice(self.element.range()); let literal_content = f.context().locator().slice(self.element.range());
let normalized = normalize_string(literal_content, 0, self.context.kind(), true); let normalized = normalize_string(literal_content, 0, self.context.flags(), true);
match &normalized { match &normalized {
Cow::Borrowed(_) => source_text_slice(self.element.range()).fmt(f), Cow::Borrowed(_) => source_text_slice(self.element.range()).fmt(f),
Cow::Owned(normalized) => text(normalized).fmt(f), Cow::Owned(normalized) => text(normalized).fmt(f),
@ -102,7 +102,7 @@ impl FStringExpressionElementContext {
// But, if the original source code already contained a newline, they'll be preserved. // But, if the original source code already contained a newline, they'll be preserved.
// //
// The Python version is irrelevant in this case. // The Python version is irrelevant in this case.
&& !(self.parent_context.kind().is_triple_quoted() && self.has_format_spec) && !(self.parent_context.flags().is_triple_quoted() && self.has_format_spec)
} }
} }

View file

@ -3,8 +3,8 @@ use std::iter::FusedIterator;
use memchr::memchr2; use memchr::memchr2;
use ruff_python_ast::{ use ruff_python_ast::{
self as ast, AnyNodeRef, AnyStringKind, Expr, ExprBytesLiteral, ExprFString, ExprStringLiteral, self as ast, AnyNodeRef, AnyStringFlags, Expr, ExprBytesLiteral, ExprFString,
ExpressionRef, StringLiteral, ExprStringLiteral, ExpressionRef, StringLiteral,
}; };
use ruff_source_file::Locator; use ruff_source_file::Locator;
use ruff_text_size::{Ranged, TextRange}; use ruff_text_size::{Ranged, TextRange};
@ -72,7 +72,7 @@ impl<'a> AnyString<'a> {
AnyString::String(_) | AnyString::Bytes(_) => { AnyString::String(_) | AnyString::Bytes(_) => {
self.parts(Quoting::default()) self.parts(Quoting::default())
.next() .next()
.is_some_and(|part| part.kind().is_triple_quoted()) .is_some_and(|part| part.flags().is_triple_quoted())
&& memchr2(b'\n', b'\r', source[self.range()].as_bytes()).is_some() && memchr2(b'\n', b'\r', source[self.range()].as_bytes()).is_some()
} }
AnyString::FString(fstring) => { AnyString::FString(fstring) => {
@ -176,7 +176,7 @@ pub(super) enum AnyStringPart<'a> {
} }
impl AnyStringPart<'_> { impl AnyStringPart<'_> {
fn kind(&self) -> AnyStringKind { fn flags(&self) -> AnyStringFlags {
match self { match self {
Self::String { part, .. } => part.flags.into(), Self::String { part, .. } => part.flags.into(),
Self::Bytes(bytes_literal) => bytes_literal.flags.into(), Self::Bytes(bytes_literal) => bytes_literal.flags.into(),

View file

@ -127,7 +127,7 @@ pub(crate) fn format(normalized: &NormalizedString, f: &mut PyFormatter) -> Form
let mut lines = docstring.split('\n').peekable(); let mut lines = docstring.split('\n').peekable();
// Start the string // Start the string
let kind = normalized.kind(); let kind = normalized.flags();
let quotes = StringQuotes::from(kind); let quotes = StringQuotes::from(kind);
write!(f, [kind.prefix(), quotes])?; write!(f, [kind.prefix(), quotes])?;
// We track where in the source docstring we are (in source code byte offsets) // We track where in the source docstring we are (in source code byte offsets)
@ -1573,7 +1573,7 @@ fn docstring_format_source(
/// that avoids `content""""` and `content\"""`. This does only applies to un-escaped backslashes, /// that avoids `content""""` and `content\"""`. This does only applies to un-escaped backslashes,
/// so `content\\ """` doesn't need a space while `content\\\ """` does. /// so `content\\ """` doesn't need a space while `content\\\ """` does.
fn needs_chaperone_space(normalized: &NormalizedString, trim_end: &str) -> bool { fn needs_chaperone_space(normalized: &NormalizedString, trim_end: &str) -> bool {
trim_end.ends_with(normalized.kind().quote_style().as_char()) trim_end.ends_with(normalized.flags().quote_style().as_char())
|| trim_end.chars().rev().take_while(|c| *c == '\\').count() % 2 == 1 || trim_end.chars().rev().take_while(|c| *c == '\\').count() % 2 == 1
} }

View file

@ -2,7 +2,7 @@ pub(crate) use any::AnyString;
pub(crate) use normalize::{normalize_string, NormalizedString, StringNormalizer}; pub(crate) use normalize::{normalize_string, NormalizedString, StringNormalizer};
use ruff_formatter::format_args; use ruff_formatter::format_args;
use ruff_python_ast::str::Quote; use ruff_python_ast::str::Quote;
use ruff_python_ast::{self as ast, AnyStringKind, AnyStringPrefix}; use ruff_python_ast::{self as ast, AnyStringFlags, AnyStringPrefix};
use ruff_text_size::{Ranged, TextRange}; use ruff_text_size::{Ranged, TextRange};
use crate::comments::{leading_comments, trailing_comments}; use crate::comments::{leading_comments, trailing_comments};
@ -87,8 +87,8 @@ impl Format<PyFormatContext<'_>> for StringQuotes {
} }
} }
impl From<AnyStringKind> for StringQuotes { impl From<AnyStringFlags> for StringQuotes {
fn from(value: AnyStringKind) -> Self { fn from(value: AnyStringFlags) -> Self {
Self { Self {
triple: value.is_triple_quoted(), triple: value.is_triple_quoted(),
quote_char: value.quote_style(), quote_char: value.quote_style(),
@ -119,7 +119,7 @@ impl From<Quote> for QuoteStyle {
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub(crate) struct StringPart { pub(crate) struct StringPart {
kind: AnyStringKind, flags: AnyStringFlags,
range: TextRange, range: TextRange,
} }
@ -131,13 +131,13 @@ impl Ranged for StringPart {
impl StringPart { impl StringPart {
/// Use the `kind()` method to retrieve information about the /// Use the `kind()` method to retrieve information about the
fn kind(self) -> AnyStringKind { fn flags(self) -> AnyStringFlags {
self.kind self.flags
} }
/// Returns the range of the string's content in the source (minus prefix and quotes). /// Returns the range of the string's content in the source (minus prefix and quotes).
fn content_range(self) -> TextRange { fn content_range(self) -> TextRange {
let kind = self.kind(); let kind = self.flags();
TextRange::new( TextRange::new(
self.start() + kind.opener_len(), self.start() + kind.opener_len(),
self.end() - kind.closer_len(), self.end() - kind.closer_len(),
@ -149,7 +149,7 @@ impl From<&ast::StringLiteral> for StringPart {
fn from(value: &ast::StringLiteral) -> Self { fn from(value: &ast::StringLiteral) -> Self {
Self { Self {
range: value.range, range: value.range,
kind: value.flags.into(), flags: value.flags.into(),
} }
} }
} }
@ -158,7 +158,7 @@ impl From<&ast::BytesLiteral> for StringPart {
fn from(value: &ast::BytesLiteral) -> Self { fn from(value: &ast::BytesLiteral) -> Self {
Self { Self {
range: value.range, range: value.range,
kind: value.flags.into(), flags: value.flags.into(),
} }
} }
} }
@ -167,7 +167,7 @@ impl From<&ast::FString> for StringPart {
fn from(value: &ast::FString) -> Self { fn from(value: &ast::FString) -> Self {
Self { Self {
range: value.range, range: value.range,
kind: value.flags.into(), flags: value.flags.into(),
} }
} }
} }

View file

@ -2,7 +2,7 @@ use std::borrow::Cow;
use std::iter::FusedIterator; use std::iter::FusedIterator;
use ruff_formatter::FormatContext; use ruff_formatter::FormatContext;
use ruff_python_ast::{str::Quote, AnyStringKind}; use ruff_python_ast::{str::Quote, AnyStringFlags};
use ruff_source_file::Locator; use ruff_source_file::Locator;
use ruff_text_size::{Ranged, TextRange}; use ruff_text_size::{Ranged, TextRange};
@ -60,7 +60,7 @@ impl StringNormalizer {
// The reason to preserve the quotes is based on the assumption that // The reason to preserve the quotes is based on the assumption that
// the original f-string is valid in terms of quoting, and we don't // the original f-string is valid in terms of quoting, and we don't
// want to change that to make it invalid. // want to change that to make it invalid.
if (context.f_string().kind().is_triple_quoted() && !string.kind().is_triple_quoted()) if (context.f_string().flags().is_triple_quoted() && !string.flags().is_triple_quoted())
|| self.target_version.supports_pep_701() || self.target_version.supports_pep_701()
{ {
self.quoting self.quoting
@ -78,14 +78,14 @@ impl StringNormalizer {
let first_quote_or_normalized_char_offset = raw_content let first_quote_or_normalized_char_offset = raw_content
.bytes() .bytes()
.position(|b| matches!(b, b'\\' | b'"' | b'\'' | b'\r' | b'{')); .position(|b| matches!(b, b'\\' | b'"' | b'\'' | b'\r' | b'{'));
let string_kind = string.kind(); let string_flags = string.flags();
let new_kind = match self.quoting(string) { let new_kind = match self.quoting(string) {
Quoting::Preserve => string_kind, Quoting::Preserve => string_flags,
Quoting::CanChange => { Quoting::CanChange => {
// Per PEP 8, always prefer double quotes for triple-quoted strings. // Per PEP 8, always prefer double quotes for triple-quoted strings.
// Except when using quote-style-preserve. // Except when using quote-style-preserve.
let preferred_style = if string_kind.is_triple_quoted() { let preferred_style = if string_flags.is_triple_quoted() {
// ... unless we're formatting a code snippet inside a docstring, // ... unless we're formatting a code snippet inside a docstring,
// then we specifically want to invert our quote style to avoid // then we specifically want to invert our quote style to avoid
// writing out invalid Python. // writing out invalid Python.
@ -146,30 +146,30 @@ impl StringNormalizer {
if let Some(first_quote_or_normalized_char_offset) = if let Some(first_quote_or_normalized_char_offset) =
first_quote_or_normalized_char_offset first_quote_or_normalized_char_offset
{ {
if string_kind.is_raw_string() { if string_flags.is_raw_string() {
choose_quotes_for_raw_string( choose_quotes_for_raw_string(
&raw_content[first_quote_or_normalized_char_offset..], &raw_content[first_quote_or_normalized_char_offset..],
string_kind, string_flags,
preferred_quote, preferred_quote,
) )
} else { } else {
choose_quotes_impl( choose_quotes_impl(
&raw_content[first_quote_or_normalized_char_offset..], &raw_content[first_quote_or_normalized_char_offset..],
string_kind, string_flags,
preferred_quote, preferred_quote,
) )
} }
} else { } else {
string_kind.with_quote_style(preferred_quote) string_flags.with_quote_style(preferred_quote)
} }
} else { } else {
string_kind string_flags
} }
} }
}; };
QuoteSelection { QuoteSelection {
kind: new_kind, flags: new_kind,
first_quote_or_normalized_char_offset, first_quote_or_normalized_char_offset,
} }
} }
@ -189,7 +189,7 @@ impl StringNormalizer {
normalize_string( normalize_string(
raw_content, raw_content,
first_quote_or_escape_offset, first_quote_or_escape_offset,
quote_selection.kind, quote_selection.flags,
// TODO: Remove the `b'{'` in `choose_quotes` when promoting the // TODO: Remove the `b'{'` in `choose_quotes` when promoting the
// `format_fstring` preview style // `format_fstring` preview style
self.format_fstring, self.format_fstring,
@ -199,7 +199,7 @@ impl StringNormalizer {
}; };
NormalizedString { NormalizedString {
kind: quote_selection.kind, flags: quote_selection.flags,
content_range: string.content_range(), content_range: string.content_range(),
text: normalized, text: normalized,
} }
@ -208,22 +208,22 @@ impl StringNormalizer {
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct QuoteSelection { pub(crate) struct QuoteSelection {
kind: AnyStringKind, flags: AnyStringFlags,
/// Offset to the first quote character or character that needs special handling in [`normalize_string`]. /// Offset to the first quote character or character that needs special handling in [`normalize_string`].
first_quote_or_normalized_char_offset: Option<usize>, first_quote_or_normalized_char_offset: Option<usize>,
} }
impl QuoteSelection { impl QuoteSelection {
pub(crate) fn kind(&self) -> AnyStringKind { pub(crate) fn flags(&self) -> AnyStringFlags {
self.kind self.flags
} }
} }
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct NormalizedString<'a> { pub(crate) struct NormalizedString<'a> {
/// Holds data about the quotes and prefix of the string /// Holds data about the quotes and prefix of the string
kind: AnyStringKind, flags: AnyStringFlags,
/// The range of the string's content in the source (minus prefix and quotes). /// The range of the string's content in the source (minus prefix and quotes).
content_range: TextRange, content_range: TextRange,
@ -237,8 +237,8 @@ impl<'a> NormalizedString<'a> {
&self.text &self.text
} }
pub(crate) fn kind(&self) -> AnyStringKind { pub(crate) fn flags(&self) -> AnyStringFlags {
self.kind self.flags
} }
} }
@ -250,8 +250,8 @@ impl Ranged for NormalizedString<'_> {
impl Format<PyFormatContext<'_>> for NormalizedString<'_> { impl Format<PyFormatContext<'_>> for NormalizedString<'_> {
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> { fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
let quotes = StringQuotes::from(self.kind); let quotes = StringQuotes::from(self.flags);
ruff_formatter::write!(f, [self.kind.prefix(), quotes])?; ruff_formatter::write!(f, [self.flags.prefix(), quotes])?;
match &self.text { match &self.text {
Cow::Borrowed(_) => { Cow::Borrowed(_) => {
source_text_slice(self.range()).fmt(f)?; source_text_slice(self.range()).fmt(f)?;
@ -271,9 +271,9 @@ impl Format<PyFormatContext<'_>> for NormalizedString<'_> {
/// style is double quotes. /// style is double quotes.
fn choose_quotes_for_raw_string( fn choose_quotes_for_raw_string(
input: &str, input: &str,
kind: AnyStringKind, flags: AnyStringFlags,
preferred_quote: Quote, preferred_quote: Quote,
) -> AnyStringKind { ) -> AnyStringFlags {
let preferred_quote_char = preferred_quote.as_char(); let preferred_quote_char = preferred_quote.as_char();
let mut chars = input.chars().peekable(); let mut chars = input.chars().peekable();
let contains_unescaped_configured_quotes = loop { let contains_unescaped_configured_quotes = loop {
@ -284,7 +284,7 @@ fn choose_quotes_for_raw_string(
} }
// `"` or `'` // `"` or `'`
Some(c) if c == preferred_quote_char => { Some(c) if c == preferred_quote_char => {
if !kind.is_triple_quoted() { if !flags.is_triple_quoted() {
break true; break true;
} }
@ -310,9 +310,9 @@ fn choose_quotes_for_raw_string(
} }
}; };
if contains_unescaped_configured_quotes { if contains_unescaped_configured_quotes {
kind flags
} else { } else {
kind.with_quote_style(preferred_quote) flags.with_quote_style(preferred_quote)
} }
} }
@ -324,8 +324,12 @@ fn choose_quotes_for_raw_string(
/// For triple quoted strings, the preferred quote style is always used, unless the string contains /// For triple quoted strings, the preferred quote style is always used, unless the string contains
/// a triplet of the quote character (e.g., if double quotes are preferred, double quotes will be /// a triplet of the quote character (e.g., if double quotes are preferred, double quotes will be
/// used unless the string contains `"""`). /// used unless the string contains `"""`).
fn choose_quotes_impl(input: &str, kind: AnyStringKind, preferred_quote: Quote) -> AnyStringKind { fn choose_quotes_impl(
let quote = if kind.is_triple_quoted() { input: &str,
flags: AnyStringFlags,
preferred_quote: Quote,
) -> AnyStringFlags {
let quote = if flags.is_triple_quoted() {
// True if the string contains a triple quote sequence of the configured quote style. // True if the string contains a triple quote sequence of the configured quote style.
let mut uses_triple_quotes = false; let mut uses_triple_quotes = false;
let mut chars = input.chars().peekable(); let mut chars = input.chars().peekable();
@ -379,7 +383,7 @@ fn choose_quotes_impl(input: &str, kind: AnyStringKind, preferred_quote: Quote)
if uses_triple_quotes { if uses_triple_quotes {
// String contains a triple quote sequence of the configured quote style. // String contains a triple quote sequence of the configured quote style.
// Keep the existing quote style. // Keep the existing quote style.
kind.quote_style() flags.quote_style()
} else { } else {
preferred_quote preferred_quote
} }
@ -419,7 +423,7 @@ fn choose_quotes_impl(input: &str, kind: AnyStringKind, preferred_quote: Quote)
} }
}; };
kind.with_quote_style(quote) flags.with_quote_style(quote)
} }
/// Adds the necessary quote escapes and removes unnecessary escape sequences when quoting `input` /// Adds the necessary quote escapes and removes unnecessary escape sequences when quoting `input`
@ -429,7 +433,7 @@ fn choose_quotes_impl(input: &str, kind: AnyStringKind, preferred_quote: Quote)
pub(crate) fn normalize_string( pub(crate) fn normalize_string(
input: &str, input: &str,
start_offset: usize, start_offset: usize,
kind: AnyStringKind, flags: AnyStringFlags,
format_fstring: bool, format_fstring: bool,
) -> Cow<str> { ) -> Cow<str> {
// The normalized string if `input` is not yet normalized. // The normalized string if `input` is not yet normalized.
@ -439,14 +443,14 @@ pub(crate) fn normalize_string(
// If `last_index` is `0` at the end, then the input is already normalized and can be returned as is. // If `last_index` is `0` at the end, then the input is already normalized and can be returned as is.
let mut last_index = 0; let mut last_index = 0;
let quote = kind.quote_style(); let quote = flags.quote_style();
let preferred_quote = quote.as_char(); let preferred_quote = quote.as_char();
let opposite_quote = quote.opposite().as_char(); let opposite_quote = quote.opposite().as_char();
let mut chars = CharIndicesWithOffset::new(input, start_offset).peekable(); let mut chars = CharIndicesWithOffset::new(input, start_offset).peekable();
let is_raw = kind.is_raw_string(); let is_raw = flags.is_raw_string();
let is_fstring = !format_fstring && kind.is_f_string(); let is_fstring = !format_fstring && flags.is_f_string();
let mut formatted_value_nesting = 0u32; let mut formatted_value_nesting = 0u32;
while let Some((index, c)) = chars.next() { while let Some((index, c)) = chars.next() {
@ -484,7 +488,7 @@ pub(crate) fn normalize_string(
} else { } else {
// Length of the `\` plus the length of the escape sequence character (`u` | `U` | `x`) // Length of the `\` plus the length of the escape sequence character (`u` | `U` | `x`)
let escape_start_len = '\\'.len_utf8() + next.len_utf8(); let escape_start_len = '\\'.len_utf8() + next.len_utf8();
if let Some(normalised) = UnicodeEscape::new(next, !kind.is_byte_string()) if let Some(normalised) = UnicodeEscape::new(next, !flags.is_byte_string())
.and_then(|escape| escape.normalize(&input[index + escape_start_len..])) .and_then(|escape| escape.normalize(&input[index + escape_start_len..]))
{ {
let escape_start_offset = index + escape_start_len; let escape_start_offset = index + escape_start_len;
@ -503,7 +507,7 @@ pub(crate) fn normalize_string(
} }
} }
if !kind.is_triple_quoted() { if !flags.is_triple_quoted() {
#[allow(clippy::if_same_then_else)] #[allow(clippy::if_same_then_else)]
if next == opposite_quote && formatted_value_nesting == 0 { if next == opposite_quote && formatted_value_nesting == 0 {
// Remove the escape by ending before the backslash and starting again with the quote // Remove the escape by ending before the backslash and starting again with the quote
@ -516,7 +520,7 @@ pub(crate) fn normalize_string(
} }
} }
} }
} else if !kind.is_triple_quoted() } else if !flags.is_triple_quoted()
&& c == preferred_quote && c == preferred_quote
&& formatted_value_nesting == 0 && formatted_value_nesting == 0
{ {
@ -689,7 +693,7 @@ impl UnicodeEscape {
mod tests { mod tests {
use std::borrow::Cow; use std::borrow::Cow;
use ruff_python_ast::{str::Quote, AnyStringKind, AnyStringPrefix, ByteStringPrefix}; use ruff_python_ast::{str::Quote, AnyStringFlags, AnyStringPrefix, ByteStringPrefix};
use super::{normalize_string, UnicodeEscape}; use super::{normalize_string, UnicodeEscape};
@ -710,7 +714,7 @@ mod tests {
let normalized = normalize_string( let normalized = normalize_string(
input, input,
0, 0,
AnyStringKind::new( AnyStringFlags::new(
AnyStringPrefix::Bytes(ByteStringPrefix::Regular), AnyStringPrefix::Bytes(ByteStringPrefix::Regular),
Quote::Double, Quote::Double,
false, false,

View file

@ -46,8 +46,8 @@ pub(crate) struct MultilineRangesBuilder {
impl MultilineRangesBuilder { impl MultilineRangesBuilder {
pub(crate) fn visit_token(&mut self, token: &Tok, range: TextRange) { pub(crate) fn visit_token(&mut self, token: &Tok, range: TextRange) {
if let Tok::String { kind, .. } | Tok::FStringMiddle { kind, .. } = token { if let Tok::String { flags, .. } | Tok::FStringMiddle { flags, .. } = token {
if kind.is_triple_quoted() { if flags.is_triple_quoted() {
self.ranges.push(range); self.ranges.push(range);
} }
} }

View file

@ -35,7 +35,7 @@ use unicode_ident::{is_xid_continue, is_xid_start};
use unicode_normalization::UnicodeNormalization; use unicode_normalization::UnicodeNormalization;
use ruff_python_ast::{ use ruff_python_ast::{
str::Quote, AnyStringKind, AnyStringPrefix, FStringPrefix, Int, IpyEscapeKind, str::Quote, AnyStringFlags, AnyStringPrefix, FStringPrefix, Int, IpyEscapeKind,
}; };
use ruff_text_size::{TextLen, TextRange, TextSize}; use ruff_text_size::{TextLen, TextRange, TextSize};
@ -561,7 +561,7 @@ impl<'source> Lexer<'source> {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
debug_assert_eq!(self.cursor.previous(), quote); debug_assert_eq!(self.cursor.previous(), quote);
let mut kind = AnyStringKind::default() let mut flags = AnyStringFlags::default()
.with_prefix(AnyStringPrefix::Format(prefix)) .with_prefix(AnyStringPrefix::Format(prefix))
.with_quote_style(if quote == '"' { .with_quote_style(if quote == '"' {
Quote::Double Quote::Double
@ -570,11 +570,11 @@ impl<'source> Lexer<'source> {
}); });
if self.cursor.eat_char2(quote, quote) { if self.cursor.eat_char2(quote, quote) {
kind = kind.with_triple_quotes(); flags = flags.with_triple_quotes();
} }
self.fstrings.push(FStringContext::new(kind, self.nesting)); self.fstrings.push(FStringContext::new(flags, self.nesting));
Tok::FStringStart(kind) Tok::FStringStart(flags)
} }
/// Lex a f-string middle or end token. /// Lex a f-string middle or end token.
@ -707,7 +707,7 @@ impl<'source> Lexer<'source> {
}; };
Ok(Some(Tok::FStringMiddle { Ok(Some(Tok::FStringMiddle {
value: value.into_boxed_str(), value: value.into_boxed_str(),
kind: fstring.kind(), flags: fstring.flags(),
})) }))
} }
@ -716,7 +716,7 @@ impl<'source> Lexer<'source> {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
debug_assert_eq!(self.cursor.previous(), quote); debug_assert_eq!(self.cursor.previous(), quote);
let mut kind = AnyStringKind::default() let mut flags = AnyStringFlags::default()
.with_prefix(prefix) .with_prefix(prefix)
.with_quote_style(if quote == '"' { .with_quote_style(if quote == '"' {
Quote::Double Quote::Double
@ -727,13 +727,13 @@ impl<'source> Lexer<'source> {
// If the next two characters are also the quote character, then we have a triple-quoted // If the next two characters are also the quote character, then we have a triple-quoted
// string; consume those two characters and ensure that we require a triple-quote to close // string; consume those two characters and ensure that we require a triple-quote to close
if self.cursor.eat_char2(quote, quote) { if self.cursor.eat_char2(quote, quote) {
kind = kind.with_triple_quotes(); flags = flags.with_triple_quotes();
} }
let value_start = self.offset(); let value_start = self.offset();
let quote_byte = u8::try_from(quote).expect("char that fits in u8"); let quote_byte = u8::try_from(quote).expect("char that fits in u8");
let value_end = if kind.is_triple_quoted() { let value_end = if flags.is_triple_quoted() {
// For triple-quoted strings, scan until we find the closing quote (ignoring escaped // For triple-quoted strings, scan until we find the closing quote (ignoring escaped
// quotes) or the end of the file. // quotes) or the end of the file.
loop { loop {
@ -821,7 +821,7 @@ impl<'source> Lexer<'source> {
value: self.source[TextRange::new(value_start, value_end)] value: self.source[TextRange::new(value_start, value_end)]
.to_string() .to_string()
.into_boxed_str(), .into_boxed_str(),
kind, flags,
}) })
} }

View file

@ -1,9 +1,9 @@
use ruff_python_ast::AnyStringKind; use ruff_python_ast::AnyStringFlags;
/// The context representing the current f-string that the lexer is in. /// The context representing the current f-string that the lexer is in.
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct FStringContext { pub(crate) struct FStringContext {
kind: AnyStringKind, flags: AnyStringFlags,
/// The level of nesting for the lexer when it entered the current f-string. /// The level of nesting for the lexer when it entered the current f-string.
/// The nesting level includes all kinds of parentheses i.e., round, square, /// The nesting level includes all kinds of parentheses i.e., round, square,
@ -17,18 +17,18 @@ pub(crate) struct FStringContext {
} }
impl FStringContext { impl FStringContext {
pub(crate) const fn new(kind: AnyStringKind, nesting: u32) -> Self { pub(crate) const fn new(flags: AnyStringFlags, nesting: u32) -> Self {
debug_assert!(kind.is_f_string()); debug_assert!(flags.is_f_string());
Self { Self {
kind, flags,
nesting, nesting,
format_spec_depth: 0, format_spec_depth: 0,
} }
} }
pub(crate) const fn kind(&self) -> AnyStringKind { pub(crate) const fn flags(&self) -> AnyStringFlags {
debug_assert!(self.kind.is_f_string()); debug_assert!(self.flags.is_f_string());
self.kind self.flags
} }
pub(crate) const fn nesting(&self) -> u32 { pub(crate) const fn nesting(&self) -> u32 {
@ -37,14 +37,14 @@ impl FStringContext {
/// Returns the quote character for the current f-string. /// Returns the quote character for the current f-string.
pub(crate) const fn quote_char(&self) -> char { pub(crate) const fn quote_char(&self) -> char {
self.kind.quote_style().as_char() self.flags.quote_style().as_char()
} }
/// Returns the triple quotes for the current f-string if it is a triple-quoted /// Returns the triple quotes for the current f-string if it is a triple-quoted
/// f-string, `None` otherwise. /// f-string, `None` otherwise.
pub(crate) const fn triple_quotes(&self) -> Option<&'static str> { pub(crate) const fn triple_quotes(&self) -> Option<&'static str> {
if self.is_triple_quoted() { if self.is_triple_quoted() {
Some(self.kind.quote_str()) Some(self.flags.quote_str())
} else { } else {
None None
} }
@ -52,12 +52,12 @@ impl FStringContext {
/// Returns `true` if the current f-string is a raw f-string. /// Returns `true` if the current f-string is a raw f-string.
pub(crate) fn is_raw_string(&self) -> bool { pub(crate) fn is_raw_string(&self) -> bool {
self.kind.is_raw_string() self.flags.is_raw_string()
} }
/// Returns `true` if the current f-string is a triple-quoted f-string. /// Returns `true` if the current f-string is a triple-quoted f-string.
pub(crate) const fn is_triple_quoted(&self) -> bool { pub(crate) const fn is_triple_quoted(&self) -> bool {
self.kind.is_triple_quoted() self.flags.is_triple_quoted()
} }
/// Calculates the number of open parentheses for the current f-string /// Calculates the number of open parentheses for the current f-string

View file

@ -1231,17 +1231,17 @@ impl<'src> Parser<'src> {
/// ///
/// See: <https://docs.python.org/3.13/reference/lexical_analysis.html#string-and-bytes-literals> /// See: <https://docs.python.org/3.13/reference/lexical_analysis.html#string-and-bytes-literals>
fn parse_string_or_byte_literal(&mut self) -> StringType { fn parse_string_or_byte_literal(&mut self) -> StringType {
let (Tok::String { value, kind }, range) = self.bump(TokenKind::String) else { let (Tok::String { value, flags }, range) = self.bump(TokenKind::String) else {
unreachable!() unreachable!()
}; };
match parse_string_literal(value, kind, range) { match parse_string_literal(value, flags, range) {
Ok(string) => string, Ok(string) => string,
Err(error) => { Err(error) => {
let location = error.location(); let location = error.location();
self.add_error(ParseErrorType::Lexical(error.into_error()), location); self.add_error(ParseErrorType::Lexical(error.into_error()), location);
if kind.is_byte_string() { if flags.is_byte_string() {
// test_err invalid_byte_literal // test_err invalid_byte_literal
// b'123a𝐁c' // b'123a𝐁c'
// rb"a𝐁c123" // rb"a𝐁c123"
@ -1249,7 +1249,7 @@ impl<'src> Parser<'src> {
StringType::Bytes(ast::BytesLiteral { StringType::Bytes(ast::BytesLiteral {
value: Box::new([]), value: Box::new([]),
range, range,
flags: ast::BytesLiteralFlags::from(kind).with_invalid(), flags: ast::BytesLiteralFlags::from(flags).with_invalid(),
}) })
} else { } else {
// test_err invalid_string_literal // test_err invalid_string_literal
@ -1258,7 +1258,7 @@ impl<'src> Parser<'src> {
StringType::Str(ast::StringLiteral { StringType::Str(ast::StringLiteral {
value: "".into(), value: "".into(),
range, range,
flags: ast::StringLiteralFlags::from(kind).with_invalid(), flags: ast::StringLiteralFlags::from(flags).with_invalid(),
}) })
} }
} }
@ -1306,12 +1306,12 @@ impl<'src> Parser<'src> {
FStringElement::Expression(parser.parse_fstring_expression_element()) FStringElement::Expression(parser.parse_fstring_expression_element())
} }
TokenKind::FStringMiddle => { TokenKind::FStringMiddle => {
let (Tok::FStringMiddle { value, kind, .. }, range) = parser.next_token() let (Tok::FStringMiddle { value, flags, .. }, range) = parser.next_token()
else { else {
unreachable!() unreachable!()
}; };
FStringElement::Literal( FStringElement::Literal(
parse_fstring_literal_element(value, kind, range).unwrap_or_else( parse_fstring_literal_element(value, flags, range).unwrap_or_else(
|lex_error| { |lex_error| {
// test_err invalid_fstring_literal_element // test_err invalid_fstring_literal_element
// f'hello \N{INVALID} world' // f'hello \N{INVALID} world'

View file

@ -5,7 +5,7 @@ expression: lex_source(source)
[ [
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -22,7 +22,7 @@ expression: lex_source(source)
( (
String { String {
value: "", value: "",
kind: StringKind { flags: AnyStringFlags {
prefix: Regular( prefix: Regular(
Empty, Empty,
), ),
@ -34,7 +34,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -50,7 +50,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -67,7 +67,7 @@ expression: lex_source(source)
( (
String { String {
value: "", value: "",
kind: StringKind { flags: AnyStringFlags {
prefix: Regular( prefix: Regular(
Empty, Empty,
), ),
@ -79,7 +79,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -95,7 +95,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),

View file

@ -6,7 +6,7 @@ expression: lex_source(source)
( (
String { String {
value: "\\N{EN SPACE}", value: "\\N{EN SPACE}",
kind: StringKind { flags: AnyStringFlags {
prefix: Regular( prefix: Regular(
Empty, Empty,
), ),

View file

@ -5,7 +5,7 @@ expression: lex_source(source)
[ [
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -18,7 +18,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "normal ", value: "normal ",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -45,7 +45,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: " {another} ", value: " {another} ",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -72,7 +72,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: " {", value: " {",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -99,7 +99,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "}", value: "}",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),

View file

@ -5,7 +5,7 @@ expression: lex_source(source)
[ [
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -18,7 +18,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "\n# not a comment ", value: "\n# not a comment ",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -59,7 +59,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: " # not a comment\n", value: " # not a comment\n",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),

View file

@ -5,7 +5,7 @@ expression: lex_source(source)
[ [
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -42,7 +42,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: " ", value: " ",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -83,7 +83,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: " ", value: " ",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -110,7 +110,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: ".3f!r", value: ".3f!r",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -127,7 +127,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: " {x!r}", value: " {x!r}",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),

View file

@ -5,7 +5,7 @@ expression: lex_source(source)
[ [
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -18,7 +18,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "\\", value: "\\",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -45,7 +45,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "\\\"\\", value: "\\\"\\",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -76,7 +76,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: " \\\"\\\"\\\n end", value: " \\\"\\\"\\\n end",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),

View file

@ -5,7 +5,7 @@ expression: lex_source(source)
[ [
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -18,7 +18,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "\\", value: "\\",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -48,7 +48,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -61,7 +61,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "\\\\", value: "\\\\",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -91,7 +91,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -104,7 +104,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "\\{foo}", value: "\\{foo}",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -120,7 +120,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -133,7 +133,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "\\\\{foo}", value: "\\\\{foo}",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),

View file

@ -5,7 +5,7 @@ expression: lex_source(source)
[ [
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Raw { Raw {
uppercase_r: false, uppercase_r: false,
@ -20,7 +20,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "\\", value: "\\",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Raw { Raw {
uppercase_r: false, uppercase_r: false,
@ -49,7 +49,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "\\\"\\", value: "\\\"\\",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Raw { Raw {
uppercase_r: false, uppercase_r: false,
@ -82,7 +82,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: " \\\"\\\"\\\n end", value: " \\\"\\\"\\\n end",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Raw { Raw {
uppercase_r: false, uppercase_r: false,

View file

@ -5,7 +5,7 @@ expression: lex_source(source)
[ [
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -18,7 +18,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "first ", value: "first ",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -71,7 +71,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: " second", value: " second",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),

View file

@ -5,7 +5,7 @@ expression: lex_source(source)
[ [
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -18,7 +18,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "\nhello\n world\n", value: "\nhello\n world\n",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -34,7 +34,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -47,7 +47,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "\n world\nhello\n", value: "\n world\nhello\n",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -63,7 +63,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -76,7 +76,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "some ", value: "some ",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -92,7 +92,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -105,7 +105,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "multiline\nallowed ", value: "multiline\nallowed ",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -140,7 +140,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: " string", value: " string",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),

View file

@ -5,7 +5,7 @@ expression: lex_source(source)
[ [
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -18,7 +18,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "\\N{BULLET} normal \\Nope \\N", value: "\\N{BULLET} normal \\Nope \\N",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),

View file

@ -5,7 +5,7 @@ expression: lex_source(source)
[ [
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Raw { Raw {
uppercase_r: false, uppercase_r: false,
@ -20,7 +20,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "\\N", value: "\\N",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Raw { Raw {
uppercase_r: false, uppercase_r: false,
@ -49,7 +49,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: " normal", value: " normal",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Raw { Raw {
uppercase_r: false, uppercase_r: false,

View file

@ -5,7 +5,7 @@ expression: lex_source(source)
[ [
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -18,7 +18,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "foo ", value: "foo ",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -34,7 +34,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -47,7 +47,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "bar ", value: "bar ",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -73,7 +73,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -116,7 +116,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: " baz", value: " baz",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -132,7 +132,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -145,7 +145,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "foo ", value: "foo ",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -161,7 +161,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -174,7 +174,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "bar", value: "bar",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -195,7 +195,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: " some ", value: " some ",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -211,7 +211,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -224,7 +224,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "another", value: "another",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),

View file

@ -5,7 +5,7 @@ expression: lex_source(source)
[ [
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -29,7 +29,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -42,7 +42,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "{}", value: "{}",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -58,7 +58,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -71,7 +71,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: " ", value: " ",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -95,7 +95,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -108,7 +108,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "{", value: "{",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -129,7 +129,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "}", value: "}",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -145,7 +145,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -158,7 +158,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "{{}}", value: "{{}}",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -174,7 +174,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -187,7 +187,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: " ", value: " ",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -208,7 +208,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: " {} {", value: " {} {",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -229,7 +229,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "} {{}} ", value: "} {{}} ",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),

View file

@ -5,7 +5,7 @@ expression: lex_source(source)
[ [
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -21,7 +21,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -37,7 +37,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Raw { Raw {
uppercase_r: false, uppercase_r: false,
@ -55,7 +55,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Raw { Raw {
uppercase_r: false, uppercase_r: false,
@ -73,7 +73,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Raw { Raw {
uppercase_r: true, uppercase_r: true,
@ -91,7 +91,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Raw { Raw {
uppercase_r: true, uppercase_r: true,
@ -109,7 +109,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Raw { Raw {
uppercase_r: false, uppercase_r: false,
@ -127,7 +127,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Raw { Raw {
uppercase_r: false, uppercase_r: false,
@ -145,7 +145,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Raw { Raw {
uppercase_r: true, uppercase_r: true,
@ -163,7 +163,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Raw { Raw {
uppercase_r: true, uppercase_r: true,

View file

@ -5,7 +5,7 @@ expression: fstring_single_quote_escape_eol(MAC_EOL)
[ [
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -18,7 +18,7 @@ expression: fstring_single_quote_escape_eol(MAC_EOL)
( (
FStringMiddle { FStringMiddle {
value: "text \\\r more text", value: "text \\\r more text",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),

View file

@ -5,7 +5,7 @@ expression: fstring_single_quote_escape_eol(UNIX_EOL)
[ [
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -18,7 +18,7 @@ expression: fstring_single_quote_escape_eol(UNIX_EOL)
( (
FStringMiddle { FStringMiddle {
value: "text \\\n more text", value: "text \\\n more text",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),

View file

@ -5,7 +5,7 @@ expression: fstring_single_quote_escape_eol(WINDOWS_EOL)
[ [
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -18,7 +18,7 @@ expression: fstring_single_quote_escape_eol(WINDOWS_EOL)
( (
FStringMiddle { FStringMiddle {
value: "text \\\r\n more text", value: "text \\\r\n more text",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),

View file

@ -5,7 +5,7 @@ expression: lex_source(source)
[ [
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -36,7 +36,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: " ", value: " ",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -77,7 +77,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: ".3f", value: ".3f",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -94,7 +94,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: " ", value: " ",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -121,7 +121,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: ".", value: ".",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -148,7 +148,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "f", value: "f",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -165,7 +165,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: " ", value: " ",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -182,7 +182,7 @@ expression: lex_source(source)
( (
String { String {
value: "", value: "",
kind: StringKind { flags: AnyStringFlags {
prefix: Regular( prefix: Regular(
Empty, Empty,
), ),
@ -199,7 +199,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "*^", value: "*^",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -248,7 +248,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: " ", value: " ",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),

View file

@ -5,7 +5,7 @@ expression: lex_source(source)
[ [
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -18,7 +18,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "foo ", value: "foo ",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -49,7 +49,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: " bar", value: " bar",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),

View file

@ -5,7 +5,7 @@ expression: lex_source(source)
[ [
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -61,7 +61,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),

View file

@ -5,7 +5,7 @@ expression: lex_source(source)
[ [
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -18,7 +18,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "__", value: "__",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -49,7 +49,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "d\n", value: "d\n",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -66,7 +66,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "__", value: "__",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -86,7 +86,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -99,7 +99,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "__", value: "__",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -130,7 +130,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "a\n b\n c\n", value: "a\n b\n c\n",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -147,7 +147,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "__", value: "__",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -167,7 +167,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -180,7 +180,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "__", value: "__",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -211,7 +211,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "d", value: "d",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -232,7 +232,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "__", value: "__",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -252,7 +252,7 @@ expression: lex_source(source)
), ),
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -265,7 +265,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "__", value: "__",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -296,7 +296,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "a", value: "a",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -327,7 +327,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "__", value: "__",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),

View file

@ -5,7 +5,7 @@ expression: lex_source(source)
[ [
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -32,7 +32,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "=10", value: "=10",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -49,7 +49,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: " ", value: " ",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -94,7 +94,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: " ", value: " ",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -149,7 +149,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: " ", value: " ",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),

View file

@ -5,7 +5,7 @@ expression: lex_source(source)
[ [
( (
FStringStart( FStringStart(
StringKind { AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),
@ -18,7 +18,7 @@ expression: lex_source(source)
( (
FStringMiddle { FStringMiddle {
value: "\\0", value: "\\0",
kind: StringKind { flags: AnyStringFlags {
prefix: Format( prefix: Format(
Regular, Regular,
), ),

View file

@ -14,7 +14,7 @@ expression: lex_source(source)
( (
String { String {
value: "a", value: "a",
kind: StringKind { flags: AnyStringFlags {
prefix: Regular( prefix: Regular(
Empty, Empty,
), ),
@ -31,7 +31,7 @@ expression: lex_source(source)
( (
String { String {
value: "b", value: "b",
kind: StringKind { flags: AnyStringFlags {
prefix: Regular( prefix: Regular(
Empty, Empty,
), ),
@ -52,7 +52,7 @@ expression: lex_source(source)
( (
String { String {
value: "c", value: "c",
kind: StringKind { flags: AnyStringFlags {
prefix: Regular( prefix: Regular(
Empty, Empty,
), ),
@ -65,7 +65,7 @@ expression: lex_source(source)
( (
String { String {
value: "d", value: "d",
kind: StringKind { flags: AnyStringFlags {
prefix: Regular( prefix: Regular(
Empty, Empty,
), ),

View file

@ -6,7 +6,7 @@ expression: lex_source(source)
( (
String { String {
value: "double", value: "double",
kind: StringKind { flags: AnyStringFlags {
prefix: Regular( prefix: Regular(
Empty, Empty,
), ),
@ -19,7 +19,7 @@ expression: lex_source(source)
( (
String { String {
value: "single", value: "single",
kind: StringKind { flags: AnyStringFlags {
prefix: Regular( prefix: Regular(
Empty, Empty,
), ),
@ -32,7 +32,7 @@ expression: lex_source(source)
( (
String { String {
value: "can\\'t", value: "can\\'t",
kind: StringKind { flags: AnyStringFlags {
prefix: Regular( prefix: Regular(
Empty, Empty,
), ),
@ -45,7 +45,7 @@ expression: lex_source(source)
( (
String { String {
value: "\\\\\\\"", value: "\\\\\\\"",
kind: StringKind { flags: AnyStringFlags {
prefix: Regular( prefix: Regular(
Empty, Empty,
), ),
@ -58,7 +58,7 @@ expression: lex_source(source)
( (
String { String {
value: "\\t\\r\\n", value: "\\t\\r\\n",
kind: StringKind { flags: AnyStringFlags {
prefix: Regular( prefix: Regular(
Empty, Empty,
), ),
@ -71,7 +71,7 @@ expression: lex_source(source)
( (
String { String {
value: "\\g", value: "\\g",
kind: StringKind { flags: AnyStringFlags {
prefix: Regular( prefix: Regular(
Empty, Empty,
), ),
@ -84,7 +84,7 @@ expression: lex_source(source)
( (
String { String {
value: "raw\\'", value: "raw\\'",
kind: StringKind { flags: AnyStringFlags {
prefix: Regular( prefix: Regular(
Raw { Raw {
uppercase: false, uppercase: false,
@ -99,7 +99,7 @@ expression: lex_source(source)
( (
String { String {
value: "\\420", value: "\\420",
kind: StringKind { flags: AnyStringFlags {
prefix: Regular( prefix: Regular(
Empty, Empty,
), ),
@ -112,7 +112,7 @@ expression: lex_source(source)
( (
String { String {
value: "\\200\\0a", value: "\\200\\0a",
kind: StringKind { flags: AnyStringFlags {
prefix: Regular( prefix: Regular(
Empty, Empty,
), ),

View file

@ -6,7 +6,7 @@ expression: string_continuation_with_eol(MAC_EOL)
( (
String { String {
value: "abc\\\rdef", value: "abc\\\rdef",
kind: StringKind { flags: AnyStringFlags {
prefix: Regular( prefix: Regular(
Empty, Empty,
), ),

View file

@ -6,7 +6,7 @@ expression: string_continuation_with_eol(UNIX_EOL)
( (
String { String {
value: "abc\\\ndef", value: "abc\\\ndef",
kind: StringKind { flags: AnyStringFlags {
prefix: Regular( prefix: Regular(
Empty, Empty,
), ),

View file

@ -6,7 +6,7 @@ expression: string_continuation_with_eol(WINDOWS_EOL)
( (
String { String {
value: "abc\\\r\ndef", value: "abc\\\r\ndef",
kind: StringKind { flags: AnyStringFlags {
prefix: Regular( prefix: Regular(
Empty, Empty,
), ),

View file

@ -6,7 +6,7 @@ expression: triple_quoted_eol(MAC_EOL)
( (
String { String {
value: "\r test string\r ", value: "\r test string\r ",
kind: StringKind { flags: AnyStringFlags {
prefix: Regular( prefix: Regular(
Empty, Empty,
), ),

View file

@ -6,7 +6,7 @@ expression: triple_quoted_eol(UNIX_EOL)
( (
String { String {
value: "\n test string\n ", value: "\n test string\n ",
kind: StringKind { flags: AnyStringFlags {
prefix: Regular( prefix: Regular(
Empty, Empty,
), ),

View file

@ -6,7 +6,7 @@ expression: triple_quoted_eol(WINDOWS_EOL)
( (
String { String {
value: "\r\n test string\r\n ", value: "\r\n test string\r\n ",
kind: StringKind { flags: AnyStringFlags {
prefix: Regular( prefix: Regular(
Empty, Empty,
), ),

View file

@ -2,7 +2,7 @@
use bstr::ByteSlice; use bstr::ByteSlice;
use ruff_python_ast::{self as ast, AnyStringKind, Expr}; use ruff_python_ast::{self as ast, AnyStringFlags, Expr};
use ruff_text_size::{Ranged, TextRange, TextSize}; use ruff_text_size::{Ranged, TextRange, TextSize};
use crate::lexer::{LexicalError, LexicalErrorType}; use crate::lexer::{LexicalError, LexicalErrorType};
@ -44,8 +44,8 @@ struct StringParser {
source: Box<str>, source: Box<str>,
/// Current position of the parser in the source. /// Current position of the parser in the source.
cursor: usize, cursor: usize,
/// The kind of string. /// Flags that can be used to query information about the string.
kind: AnyStringKind, flags: AnyStringFlags,
/// The location of the first character in the source from the start of the file. /// The location of the first character in the source from the start of the file.
offset: TextSize, offset: TextSize,
/// The range of the string literal. /// The range of the string literal.
@ -53,11 +53,11 @@ struct StringParser {
} }
impl StringParser { impl StringParser {
fn new(source: Box<str>, kind: AnyStringKind, offset: TextSize, range: TextRange) -> Self { fn new(source: Box<str>, flags: AnyStringFlags, offset: TextSize, range: TextRange) -> Self {
Self { Self {
source, source,
cursor: 0, cursor: 0,
kind, flags,
offset, offset,
range, range,
} }
@ -214,9 +214,9 @@ impl StringParser {
'v' => '\x0b', 'v' => '\x0b',
o @ '0'..='7' => self.parse_octet(o as u8), o @ '0'..='7' => self.parse_octet(o as u8),
'x' => self.parse_unicode_literal(2)?, 'x' => self.parse_unicode_literal(2)?,
'u' if !self.kind.is_byte_string() => self.parse_unicode_literal(4)?, 'u' if !self.flags.is_byte_string() => self.parse_unicode_literal(4)?,
'U' if !self.kind.is_byte_string() => self.parse_unicode_literal(8)?, 'U' if !self.flags.is_byte_string() => self.parse_unicode_literal(8)?,
'N' if !self.kind.is_byte_string() => self.parse_unicode_name()?, 'N' if !self.flags.is_byte_string() => self.parse_unicode_name()?,
// Special cases where the escape sequence is not a single character // Special cases where the escape sequence is not a single character
'\n' => return Ok(None), '\n' => return Ok(None),
'\r' => { '\r' => {
@ -282,7 +282,7 @@ impl StringParser {
// raise a syntax error as is done by the CPython parser. It might // raise a syntax error as is done by the CPython parser. It might
// be supported in the future, refer to point 3: https://peps.python.org/pep-0701/#rejected-ideas // be supported in the future, refer to point 3: https://peps.python.org/pep-0701/#rejected-ideas
b'\\' => { b'\\' => {
if !self.kind.is_raw_string() && self.peek_byte().is_some() { if !self.flags.is_raw_string() && self.peek_byte().is_some() {
match self.parse_escaped_char()? { match self.parse_escaped_char()? {
None => {} None => {}
Some(EscapedChar::Literal(c)) => value.push(c), Some(EscapedChar::Literal(c)) => value.push(c),
@ -330,12 +330,12 @@ impl StringParser {
)); ));
} }
if self.kind.is_raw_string() { if self.flags.is_raw_string() {
// For raw strings, no escaping is necessary. // For raw strings, no escaping is necessary.
return Ok(StringType::Bytes(ast::BytesLiteral { return Ok(StringType::Bytes(ast::BytesLiteral {
value: self.source.into_boxed_bytes(), value: self.source.into_boxed_bytes(),
range: self.range, range: self.range,
flags: self.kind.into(), flags: self.flags.into(),
})); }));
} }
@ -344,7 +344,7 @@ impl StringParser {
return Ok(StringType::Bytes(ast::BytesLiteral { return Ok(StringType::Bytes(ast::BytesLiteral {
value: self.source.into_boxed_bytes(), value: self.source.into_boxed_bytes(),
range: self.range, range: self.range,
flags: self.kind.into(), flags: self.flags.into(),
})); }));
}; };
@ -381,17 +381,17 @@ impl StringParser {
Ok(StringType::Bytes(ast::BytesLiteral { Ok(StringType::Bytes(ast::BytesLiteral {
value: value.into_boxed_slice(), value: value.into_boxed_slice(),
range: self.range, range: self.range,
flags: self.kind.into(), flags: self.flags.into(),
})) }))
} }
fn parse_string(mut self) -> Result<StringType, LexicalError> { fn parse_string(mut self) -> Result<StringType, LexicalError> {
if self.kind.is_raw_string() { if self.flags.is_raw_string() {
// For raw strings, no escaping is necessary. // For raw strings, no escaping is necessary.
return Ok(StringType::Str(ast::StringLiteral { return Ok(StringType::Str(ast::StringLiteral {
value: self.source, value: self.source,
range: self.range, range: self.range,
flags: self.kind.into(), flags: self.flags.into(),
})); }));
} }
@ -400,7 +400,7 @@ impl StringParser {
return Ok(StringType::Str(ast::StringLiteral { return Ok(StringType::Str(ast::StringLiteral {
value: self.source, value: self.source,
range: self.range, range: self.range,
flags: self.kind.into(), flags: self.flags.into(),
})); }));
}; };
@ -437,12 +437,12 @@ impl StringParser {
Ok(StringType::Str(ast::StringLiteral { Ok(StringType::Str(ast::StringLiteral {
value: value.into_boxed_str(), value: value.into_boxed_str(),
range: self.range, range: self.range,
flags: self.kind.into(), flags: self.flags.into(),
})) }))
} }
fn parse(self) -> Result<StringType, LexicalError> { fn parse(self) -> Result<StringType, LexicalError> {
if self.kind.is_byte_string() { if self.flags.is_byte_string() {
self.parse_bytes() self.parse_bytes()
} else { } else {
self.parse_string() self.parse_string()
@ -452,19 +452,19 @@ impl StringParser {
pub(crate) fn parse_string_literal( pub(crate) fn parse_string_literal(
source: Box<str>, source: Box<str>,
kind: AnyStringKind, flags: AnyStringFlags,
range: TextRange, range: TextRange,
) -> Result<StringType, LexicalError> { ) -> Result<StringType, LexicalError> {
StringParser::new(source, kind, range.start() + kind.opener_len(), range).parse() StringParser::new(source, flags, range.start() + flags.opener_len(), range).parse()
} }
// TODO(dhruvmanila): Move this to the new parser // TODO(dhruvmanila): Move this to the new parser
pub(crate) fn parse_fstring_literal_element( pub(crate) fn parse_fstring_literal_element(
source: Box<str>, source: Box<str>,
kind: AnyStringKind, flags: AnyStringFlags,
range: TextRange, range: TextRange,
) -> Result<ast::FStringLiteralElement, LexicalError> { ) -> Result<ast::FStringLiteralElement, LexicalError> {
StringParser::new(source, kind, range.start(), range).parse_fstring_middle() StringParser::new(source, flags, range.start(), range).parse_fstring_middle()
} }
#[cfg(test)] #[cfg(test)]

View file

@ -5,7 +5,7 @@
//! //!
//! [CPython source]: https://github.com/python/cpython/blob/dfc2e065a2e71011017077e549cd2f9bf4944c54/Grammar/Tokens //! [CPython source]: https://github.com/python/cpython/blob/dfc2e065a2e71011017077e549cd2f9bf4944c54/Grammar/Tokens
use ruff_python_ast::{AnyStringKind, BoolOp, Int, IpyEscapeKind, Operator, UnaryOp}; use ruff_python_ast::{AnyStringFlags, BoolOp, Int, IpyEscapeKind, Operator, UnaryOp};
use std::fmt; use std::fmt;
use crate::Mode; use crate::Mode;
@ -44,11 +44,11 @@ pub enum Tok {
value: Box<str>, value: Box<str>,
/// Flags that can be queried to determine the quote style /// Flags that can be queried to determine the quote style
/// and prefixes of the string /// and prefixes of the string
kind: AnyStringKind, flags: AnyStringFlags,
}, },
/// Token value for the start of an f-string. This includes the `f`/`F`/`fr` prefix /// Token value for the start of an f-string. This includes the `f`/`F`/`fr` prefix
/// and the opening quote(s). /// and the opening quote(s).
FStringStart(AnyStringKind), FStringStart(AnyStringFlags),
/// Token value that includes the portion of text inside the f-string that's not /// Token value that includes the portion of text inside the f-string that's not
/// part of the expression part and isn't an opening or closing brace. /// part of the expression part and isn't an opening or closing brace.
FStringMiddle { FStringMiddle {
@ -56,7 +56,7 @@ pub enum Tok {
value: Box<str>, value: Box<str>,
/// Flags that can be queried to determine the quote style /// Flags that can be queried to determine the quote style
/// and prefixes of the string /// and prefixes of the string
kind: AnyStringKind, flags: AnyStringFlags,
}, },
/// Token value for the end of an f-string. This includes the closing quote. /// Token value for the end of an f-string. This includes the closing quote.
FStringEnd, FStringEnd,
@ -245,8 +245,8 @@ impl fmt::Display for Tok {
Int { value } => write!(f, "{value}"), Int { value } => write!(f, "{value}"),
Float { value } => write!(f, "{value}"), Float { value } => write!(f, "{value}"),
Complex { real, imag } => write!(f, "{real}j{imag}"), Complex { real, imag } => write!(f, "{real}j{imag}"),
String { value, kind } => { String { value, flags } => {
write!(f, "{}", kind.format_string_contents(value)) write!(f, "{}", flags.format_string_contents(value))
} }
FStringStart(_) => f.write_str("FStringStart"), FStringStart(_) => f.write_str("FStringStart"),
FStringMiddle { value, .. } => f.write_str(value), FStringMiddle { value, .. } => f.write_str(value),