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
// string.
Tok::String { kind, .. } if kind.is_triple_quoted() => {
Tok::String { flags, .. } if flags.is_triple_quoted() => {
if locator.contains_line_break(*range) {
string_mappings.push(TextRange::new(
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;
/// 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.
pub(super) fn raw_contents(contents: &str, kind: AnyStringKind) -> &str {
&contents[kind.opener_len().to_usize()..(contents.text_len() - kind.closer_len()).to_usize()]
pub(super) fn raw_contents(contents: &str, flags: AnyStringFlags) -> &str {
&contents[flags.opener_len().to_usize()..(contents.text_len() - flags.closer_len()).to_usize()]
}
/// Return `true` if the haystack contains an escaped quote.

View file

@ -1,7 +1,7 @@
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
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_text_size::{Ranged, TextRange, TextSize};
@ -123,7 +123,7 @@ impl Visitor<'_> for AvoidableEscapedQuoteChecker<'_> {
self.locator,
self.quotes_settings,
string_literal.range(),
AnyStringKind::from(string_literal.flags),
AnyStringFlags::from(string_literal.flags),
) {
self.diagnostics.push(diagnostic);
}
@ -134,7 +134,7 @@ impl Visitor<'_> for AvoidableEscapedQuoteChecker<'_> {
self.locator,
self.quotes_settings,
bytes_literal.range(),
AnyStringKind::from(bytes_literal.flags),
AnyStringFlags::from(bytes_literal.flags),
) {
self.diagnostics.push(diagnostic);
}
@ -226,20 +226,20 @@ fn check_string_or_bytes(
locator: &Locator,
quotes_settings: &flake8_quotes::settings::Settings,
range: TextRange,
kind: AnyStringKind,
flags: AnyStringFlags,
) -> 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;
}
// 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;
}
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())
|| 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 fixed_contents = format!(
"{prefix}{quote}{value}{quote}",
prefix = kind.prefix(),
prefix = flags.prefix(),
quote = quotes_settings.inline_quotes.opposite().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_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_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(
locator,
string.range(),
AnyStringKind::from(string.flags),
AnyStringFlags::from(string.flags),
) {
checker.diagnostics.push(diagnostic);
}
@ -68,7 +68,7 @@ pub(crate) fn unnecessary_escaped_quote(checker: &mut Checker, string_like: Stri
StringLike::Bytes(expr) => {
for bytes in &expr.value {
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);
}
@ -80,7 +80,7 @@ pub(crate) fn unnecessary_escaped_quote(checker: &mut Checker, string_like: Stri
ast::FStringPart::Literal(string) => check_string_or_bytes(
locator,
string.range(),
AnyStringKind::from(string.flags),
AnyStringFlags::from(string.flags),
),
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(
locator: &Locator,
range: TextRange,
kind: AnyStringKind,
flags: AnyStringFlags,
) -> 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;
}
let contents = raw_contents(locator.slice(range), kind);
let quote = kind.quote_style();
let contents = raw_contents(locator.slice(range), flags);
let quote = flags.quote_style();
let opposite_quote_char = quote.opposite().as_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);
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,
)));
Some(diagnostic)

View file

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

View file

@ -2,7 +2,7 @@ use std::str::FromStr;
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::{AnyStringKind, Expr};
use ruff_python_ast::{AnyStringFlags, Expr};
use ruff_python_literal::{
cformat::{CFormatErrorType, CFormatString},
format::FormatPart,
@ -92,12 +92,12 @@ pub(crate) fn call(checker: &mut Checker, string: &str, range: TextRange) {
/// Ex) `"%z" % "1"`
pub(crate) fn percent(checker: &mut Checker, expr: &Expr) {
// 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
lexer::lex_starts_at(checker.locator().slice(expr), Mode::Module, expr.start()).flatten()
{
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.
Tok::Percent => break,
_ => {}
@ -109,10 +109,10 @@ pub(crate) fn percent(checker: &mut Checker, expr: &Expr) {
return;
}
for (range, kind) in &strings {
for (range, flags) in &strings {
let string = checker.locator().slice(*range);
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`.
if let Err(format_error) = CFormatString::from_str(string) {

View file

@ -1,6 +1,6 @@
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_parser::{lexer, AsMode, Tok};
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) {
// Grab each string segment (in case there's an implicit concatenation).
let content = checker.locator().slice(expr);
let mut strings: Vec<(TextRange, AnyStringKind)> = vec![];
let mut strings: Vec<(TextRange, AnyStringFlags)> = vec![];
for (tok, range) in
lexer::lex_starts_at(content, checker.source_type.as_mode(), expr.start()).flatten()
{
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.
Tok::Percent => break,
_ => {}

View file

@ -4,7 +4,7 @@ use std::str::FromStr;
use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation};
use ruff_macros::{derive_message_formats, violation};
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_literal::cformat::{
CConversionFlags, CFormatPart, CFormatPrecision, CFormatQuantity, CFormatString,
@ -347,7 +347,7 @@ fn convertible(format_string: &CFormatString, params: &Expr) -> bool {
/// UP031
pub(crate) fn printf_string_formatting(checker: &mut Checker, expr: &Expr, right: &Expr) {
// 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;
for (tok, range) in lexer::lex_starts_at(
checker.locator().slice(expr),
@ -357,7 +357,7 @@ pub(crate) fn printf_string_formatting(checker: &mut Checker, expr: &Expr, right
.flatten()
{
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.
Tok::Rpar => extension = Some(range),
// 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_keyword_arguments = 0;
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 = &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`.
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.
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.

View file

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

View file

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

View file

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

View file

@ -56,7 +56,7 @@ impl<'a> FormatFStringLiteralElement<'a> {
impl Format<PyFormatContext<'_>> for FormatFStringLiteralElement<'_> {
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
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 {
Cow::Borrowed(_) => source_text_slice(self.element.range()).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.
//
// 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 ruff_python_ast::{
self as ast, AnyNodeRef, AnyStringKind, Expr, ExprBytesLiteral, ExprFString, ExprStringLiteral,
ExpressionRef, StringLiteral,
self as ast, AnyNodeRef, AnyStringFlags, Expr, ExprBytesLiteral, ExprFString,
ExprStringLiteral, ExpressionRef, StringLiteral,
};
use ruff_source_file::Locator;
use ruff_text_size::{Ranged, TextRange};
@ -72,7 +72,7 @@ impl<'a> AnyString<'a> {
AnyString::String(_) | AnyString::Bytes(_) => {
self.parts(Quoting::default())
.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()
}
AnyString::FString(fstring) => {
@ -176,7 +176,7 @@ pub(super) enum AnyStringPart<'a> {
}
impl AnyStringPart<'_> {
fn kind(&self) -> AnyStringKind {
fn flags(&self) -> AnyStringFlags {
match self {
Self::String { part, .. } => part.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();
// Start the string
let kind = normalized.kind();
let kind = normalized.flags();
let quotes = StringQuotes::from(kind);
write!(f, [kind.prefix(), quotes])?;
// 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,
/// so `content\\ """` doesn't need a space while `content\\\ """` does.
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
}

View file

@ -2,7 +2,7 @@ pub(crate) use any::AnyString;
pub(crate) use normalize::{normalize_string, NormalizedString, StringNormalizer};
use ruff_formatter::format_args;
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 crate::comments::{leading_comments, trailing_comments};
@ -87,8 +87,8 @@ impl Format<PyFormatContext<'_>> for StringQuotes {
}
}
impl From<AnyStringKind> for StringQuotes {
fn from(value: AnyStringKind) -> Self {
impl From<AnyStringFlags> for StringQuotes {
fn from(value: AnyStringFlags) -> Self {
Self {
triple: value.is_triple_quoted(),
quote_char: value.quote_style(),
@ -119,7 +119,7 @@ impl From<Quote> for QuoteStyle {
#[derive(Debug, Clone, Copy)]
pub(crate) struct StringPart {
kind: AnyStringKind,
flags: AnyStringFlags,
range: TextRange,
}
@ -131,13 +131,13 @@ impl Ranged for StringPart {
impl StringPart {
/// Use the `kind()` method to retrieve information about the
fn kind(self) -> AnyStringKind {
self.kind
fn flags(self) -> AnyStringFlags {
self.flags
}
/// Returns the range of the string's content in the source (minus prefix and quotes).
fn content_range(self) -> TextRange {
let kind = self.kind();
let kind = self.flags();
TextRange::new(
self.start() + kind.opener_len(),
self.end() - kind.closer_len(),
@ -149,7 +149,7 @@ impl From<&ast::StringLiteral> for StringPart {
fn from(value: &ast::StringLiteral) -> Self {
Self {
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 {
Self {
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 {
Self {
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 ruff_formatter::FormatContext;
use ruff_python_ast::{str::Quote, AnyStringKind};
use ruff_python_ast::{str::Quote, AnyStringFlags};
use ruff_source_file::Locator;
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 original f-string is valid in terms of quoting, and we don't
// 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.quoting
@ -78,14 +78,14 @@ impl StringNormalizer {
let first_quote_or_normalized_char_offset = raw_content
.bytes()
.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) {
Quoting::Preserve => string_kind,
Quoting::Preserve => string_flags,
Quoting::CanChange => {
// Per PEP 8, always prefer double quotes for triple-quoted strings.
// 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,
// then we specifically want to invert our quote style to avoid
// writing out invalid Python.
@ -146,30 +146,30 @@ impl StringNormalizer {
if let Some(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(
&raw_content[first_quote_or_normalized_char_offset..],
string_kind,
string_flags,
preferred_quote,
)
} else {
choose_quotes_impl(
&raw_content[first_quote_or_normalized_char_offset..],
string_kind,
string_flags,
preferred_quote,
)
}
} else {
string_kind.with_quote_style(preferred_quote)
string_flags.with_quote_style(preferred_quote)
}
} else {
string_kind
string_flags
}
}
};
QuoteSelection {
kind: new_kind,
flags: new_kind,
first_quote_or_normalized_char_offset,
}
}
@ -189,7 +189,7 @@ impl StringNormalizer {
normalize_string(
raw_content,
first_quote_or_escape_offset,
quote_selection.kind,
quote_selection.flags,
// TODO: Remove the `b'{'` in `choose_quotes` when promoting the
// `format_fstring` preview style
self.format_fstring,
@ -199,7 +199,7 @@ impl StringNormalizer {
};
NormalizedString {
kind: quote_selection.kind,
flags: quote_selection.flags,
content_range: string.content_range(),
text: normalized,
}
@ -208,22 +208,22 @@ impl StringNormalizer {
#[derive(Debug)]
pub(crate) struct QuoteSelection {
kind: AnyStringKind,
flags: AnyStringFlags,
/// Offset to the first quote character or character that needs special handling in [`normalize_string`].
first_quote_or_normalized_char_offset: Option<usize>,
}
impl QuoteSelection {
pub(crate) fn kind(&self) -> AnyStringKind {
self.kind
pub(crate) fn flags(&self) -> AnyStringFlags {
self.flags
}
}
#[derive(Debug)]
pub(crate) struct NormalizedString<'a> {
/// 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).
content_range: TextRange,
@ -237,8 +237,8 @@ impl<'a> NormalizedString<'a> {
&self.text
}
pub(crate) fn kind(&self) -> AnyStringKind {
self.kind
pub(crate) fn flags(&self) -> AnyStringFlags {
self.flags
}
}
@ -250,8 +250,8 @@ impl Ranged for NormalizedString<'_> {
impl Format<PyFormatContext<'_>> for NormalizedString<'_> {
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
let quotes = StringQuotes::from(self.kind);
ruff_formatter::write!(f, [self.kind.prefix(), quotes])?;
let quotes = StringQuotes::from(self.flags);
ruff_formatter::write!(f, [self.flags.prefix(), quotes])?;
match &self.text {
Cow::Borrowed(_) => {
source_text_slice(self.range()).fmt(f)?;
@ -271,9 +271,9 @@ impl Format<PyFormatContext<'_>> for NormalizedString<'_> {
/// style is double quotes.
fn choose_quotes_for_raw_string(
input: &str,
kind: AnyStringKind,
flags: AnyStringFlags,
preferred_quote: Quote,
) -> AnyStringKind {
) -> AnyStringFlags {
let preferred_quote_char = preferred_quote.as_char();
let mut chars = input.chars().peekable();
let contains_unescaped_configured_quotes = loop {
@ -284,7 +284,7 @@ fn choose_quotes_for_raw_string(
}
// `"` or `'`
Some(c) if c == preferred_quote_char => {
if !kind.is_triple_quoted() {
if !flags.is_triple_quoted() {
break true;
}
@ -310,9 +310,9 @@ fn choose_quotes_for_raw_string(
}
};
if contains_unescaped_configured_quotes {
kind
flags
} 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
/// a triplet of the quote character (e.g., if double quotes are preferred, double quotes will be
/// used unless the string contains `"""`).
fn choose_quotes_impl(input: &str, kind: AnyStringKind, preferred_quote: Quote) -> AnyStringKind {
let quote = if kind.is_triple_quoted() {
fn choose_quotes_impl(
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.
let mut uses_triple_quotes = false;
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 {
// String contains a triple quote sequence of the configured quote style.
// Keep the existing quote style.
kind.quote_style()
flags.quote_style()
} else {
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`
@ -429,7 +433,7 @@ fn choose_quotes_impl(input: &str, kind: AnyStringKind, preferred_quote: Quote)
pub(crate) fn normalize_string(
input: &str,
start_offset: usize,
kind: AnyStringKind,
flags: AnyStringFlags,
format_fstring: bool,
) -> Cow<str> {
// 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.
let mut last_index = 0;
let quote = kind.quote_style();
let quote = flags.quote_style();
let preferred_quote = quote.as_char();
let opposite_quote = quote.opposite().as_char();
let mut chars = CharIndicesWithOffset::new(input, start_offset).peekable();
let is_raw = kind.is_raw_string();
let is_fstring = !format_fstring && kind.is_f_string();
let is_raw = flags.is_raw_string();
let is_fstring = !format_fstring && flags.is_f_string();
let mut formatted_value_nesting = 0u32;
while let Some((index, c)) = chars.next() {
@ -484,7 +488,7 @@ pub(crate) fn normalize_string(
} else {
// Length of the `\` plus the length of the escape sequence character (`u` | `U` | `x`)
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..]))
{
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)]
if next == opposite_quote && formatted_value_nesting == 0 {
// 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
&& formatted_value_nesting == 0
{
@ -689,7 +693,7 @@ impl UnicodeEscape {
mod tests {
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};
@ -710,7 +714,7 @@ mod tests {
let normalized = normalize_string(
input,
0,
AnyStringKind::new(
AnyStringFlags::new(
AnyStringPrefix::Bytes(ByteStringPrefix::Regular),
Quote::Double,
false,

View file

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

View file

@ -35,7 +35,7 @@ use unicode_ident::{is_xid_continue, is_xid_start};
use unicode_normalization::UnicodeNormalization;
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};
@ -561,7 +561,7 @@ impl<'source> Lexer<'source> {
#[cfg(debug_assertions)]
debug_assert_eq!(self.cursor.previous(), quote);
let mut kind = AnyStringKind::default()
let mut flags = AnyStringFlags::default()
.with_prefix(AnyStringPrefix::Format(prefix))
.with_quote_style(if quote == '"' {
Quote::Double
@ -570,11 +570,11 @@ impl<'source> Lexer<'source> {
});
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));
Tok::FStringStart(kind)
self.fstrings.push(FStringContext::new(flags, self.nesting));
Tok::FStringStart(flags)
}
/// Lex a f-string middle or end token.
@ -707,7 +707,7 @@ impl<'source> Lexer<'source> {
};
Ok(Some(Tok::FStringMiddle {
value: value.into_boxed_str(),
kind: fstring.kind(),
flags: fstring.flags(),
}))
}
@ -716,7 +716,7 @@ impl<'source> Lexer<'source> {
#[cfg(debug_assertions)]
debug_assert_eq!(self.cursor.previous(), quote);
let mut kind = AnyStringKind::default()
let mut flags = AnyStringFlags::default()
.with_prefix(prefix)
.with_quote_style(if quote == '"' {
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
// string; consume those two characters and ensure that we require a triple-quote to close
if self.cursor.eat_char2(quote, quote) {
kind = kind.with_triple_quotes();
flags = flags.with_triple_quotes();
}
let value_start = self.offset();
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
// quotes) or the end of the file.
loop {
@ -821,7 +821,7 @@ impl<'source> Lexer<'source> {
value: self.source[TextRange::new(value_start, value_end)]
.to_string()
.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.
#[derive(Debug)]
pub(crate) struct FStringContext {
kind: AnyStringKind,
flags: AnyStringFlags,
/// 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,
@ -17,18 +17,18 @@ pub(crate) struct FStringContext {
}
impl FStringContext {
pub(crate) const fn new(kind: AnyStringKind, nesting: u32) -> Self {
debug_assert!(kind.is_f_string());
pub(crate) const fn new(flags: AnyStringFlags, nesting: u32) -> Self {
debug_assert!(flags.is_f_string());
Self {
kind,
flags,
nesting,
format_spec_depth: 0,
}
}
pub(crate) const fn kind(&self) -> AnyStringKind {
debug_assert!(self.kind.is_f_string());
self.kind
pub(crate) const fn flags(&self) -> AnyStringFlags {
debug_assert!(self.flags.is_f_string());
self.flags
}
pub(crate) const fn nesting(&self) -> u32 {
@ -37,14 +37,14 @@ impl FStringContext {
/// Returns the quote character for the current f-string.
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
/// f-string, `None` otherwise.
pub(crate) const fn triple_quotes(&self) -> Option<&'static str> {
if self.is_triple_quoted() {
Some(self.kind.quote_str())
Some(self.flags.quote_str())
} else {
None
}
@ -52,12 +52,12 @@ impl FStringContext {
/// Returns `true` if the current f-string is a raw f-string.
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.
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

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>
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!()
};
match parse_string_literal(value, kind, range) {
match parse_string_literal(value, flags, range) {
Ok(string) => string,
Err(error) => {
let location = 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
// b'123a𝐁c'
// rb"a𝐁c123"
@ -1249,7 +1249,7 @@ impl<'src> Parser<'src> {
StringType::Bytes(ast::BytesLiteral {
value: Box::new([]),
range,
flags: ast::BytesLiteralFlags::from(kind).with_invalid(),
flags: ast::BytesLiteralFlags::from(flags).with_invalid(),
})
} else {
// test_err invalid_string_literal
@ -1258,7 +1258,7 @@ impl<'src> Parser<'src> {
StringType::Str(ast::StringLiteral {
value: "".into(),
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())
}
TokenKind::FStringMiddle => {
let (Tok::FStringMiddle { value, kind, .. }, range) = parser.next_token()
let (Tok::FStringMiddle { value, flags, .. }, range) = parser.next_token()
else {
unreachable!()
};
FStringElement::Literal(
parse_fstring_literal_element(value, kind, range).unwrap_or_else(
parse_fstring_literal_element(value, flags, range).unwrap_or_else(
|lex_error| {
// test_err invalid_fstring_literal_element
// f'hello \N{INVALID} world'

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -2,7 +2,7 @@
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 crate::lexer::{LexicalError, LexicalErrorType};
@ -44,8 +44,8 @@ struct StringParser {
source: Box<str>,
/// Current position of the parser in the source.
cursor: usize,
/// The kind of string.
kind: AnyStringKind,
/// Flags that can be used to query information about the string.
flags: AnyStringFlags,
/// The location of the first character in the source from the start of the file.
offset: TextSize,
/// The range of the string literal.
@ -53,11 +53,11 @@ struct 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 {
source,
cursor: 0,
kind,
flags,
offset,
range,
}
@ -214,9 +214,9 @@ impl StringParser {
'v' => '\x0b',
o @ '0'..='7' => self.parse_octet(o as u8),
'x' => self.parse_unicode_literal(2)?,
'u' if !self.kind.is_byte_string() => self.parse_unicode_literal(4)?,
'U' if !self.kind.is_byte_string() => self.parse_unicode_literal(8)?,
'N' if !self.kind.is_byte_string() => self.parse_unicode_name()?,
'u' if !self.flags.is_byte_string() => self.parse_unicode_literal(4)?,
'U' if !self.flags.is_byte_string() => self.parse_unicode_literal(8)?,
'N' if !self.flags.is_byte_string() => self.parse_unicode_name()?,
// Special cases where the escape sequence is not a single character
'\n' => return Ok(None),
'\r' => {
@ -282,7 +282,7 @@ impl StringParser {
// 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
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()? {
None => {}
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.
return Ok(StringType::Bytes(ast::BytesLiteral {
value: self.source.into_boxed_bytes(),
range: self.range,
flags: self.kind.into(),
flags: self.flags.into(),
}));
}
@ -344,7 +344,7 @@ impl StringParser {
return Ok(StringType::Bytes(ast::BytesLiteral {
value: self.source.into_boxed_bytes(),
range: self.range,
flags: self.kind.into(),
flags: self.flags.into(),
}));
};
@ -381,17 +381,17 @@ impl StringParser {
Ok(StringType::Bytes(ast::BytesLiteral {
value: value.into_boxed_slice(),
range: self.range,
flags: self.kind.into(),
flags: self.flags.into(),
}))
}
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.
return Ok(StringType::Str(ast::StringLiteral {
value: self.source,
range: self.range,
flags: self.kind.into(),
flags: self.flags.into(),
}));
}
@ -400,7 +400,7 @@ impl StringParser {
return Ok(StringType::Str(ast::StringLiteral {
value: self.source,
range: self.range,
flags: self.kind.into(),
flags: self.flags.into(),
}));
};
@ -437,12 +437,12 @@ impl StringParser {
Ok(StringType::Str(ast::StringLiteral {
value: value.into_boxed_str(),
range: self.range,
flags: self.kind.into(),
flags: self.flags.into(),
}))
}
fn parse(self) -> Result<StringType, LexicalError> {
if self.kind.is_byte_string() {
if self.flags.is_byte_string() {
self.parse_bytes()
} else {
self.parse_string()
@ -452,19 +452,19 @@ impl StringParser {
pub(crate) fn parse_string_literal(
source: Box<str>,
kind: AnyStringKind,
flags: AnyStringFlags,
range: TextRange,
) -> 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
pub(crate) fn parse_fstring_literal_element(
source: Box<str>,
kind: AnyStringKind,
flags: AnyStringFlags,
range: TextRange,
) -> 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)]

View file

@ -5,7 +5,7 @@
//!
//! [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 crate::Mode;
@ -44,11 +44,11 @@ pub enum Tok {
value: Box<str>,
/// Flags that can be queried to determine the quote style
/// 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
/// and the opening quote(s).
FStringStart(AnyStringKind),
FStringStart(AnyStringFlags),
/// 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.
FStringMiddle {
@ -56,7 +56,7 @@ pub enum Tok {
value: Box<str>,
/// Flags that can be queried to determine the quote style
/// and prefixes of the string
kind: AnyStringKind,
flags: AnyStringFlags,
},
/// Token value for the end of an f-string. This includes the closing quote.
FStringEnd,
@ -245,8 +245,8 @@ impl fmt::Display for Tok {
Int { value } => write!(f, "{value}"),
Float { value } => write!(f, "{value}"),
Complex { real, imag } => write!(f, "{real}j{imag}"),
String { value, kind } => {
write!(f, "{}", kind.format_string_contents(value))
String { value, flags } => {
write!(f, "{}", flags.format_string_contents(value))
}
FStringStart(_) => f.write_str("FStringStart"),
FStringMiddle { value, .. } => f.write_str(value),