mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-15 16:10:38 +00:00
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:
parent
be0ccabbaa
commit
6ecb4776de
54 changed files with 378 additions and 371 deletions
|
@ -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)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue