mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +00:00
New AST nodes for f-string elements (#8835)
Rebase of #6365 authored by @davidszotten. ## Summary This PR updates the AST structure for an f-string elements. The main **motivation** behind this change is to have a dedicated node for the string part of an f-string. Previously, the existing `ExprStringLiteral` node was used for this purpose which isn't exactly correct. The `ExprStringLiteral` node should include the quotes as well in the range but the f-string literal element doesn't include the quote as it's a specific part within an f-string. For example, ```python f"foo {x}" # ^^^^ # This is the literal part of an f-string ``` The introduction of `FStringElement` enum is helpful which represent either the literal part or the expression part of an f-string. ### Rule Updates This means that there'll be two nodes representing a string depending on the context. One for a normal string literal while the other is a string literal within an f-string. The AST checker is updated to accommodate this change. The rules which work on string literal are updated to check on the literal part of f-string as well. #### Notes 1. The `Expr::is_literal_expr` method would check for `ExprStringLiteral` and return true if so. But now that we don't represent the literal part of an f-string using that node, this improves the method's behavior and confines to the actual expression. We do have the `FStringElement::is_literal` method. 2. We avoid checking if we're in a f-string context before adding to `string_type_definitions` because the f-string literal is now a dedicated node and not part of `Expr`. 3. Annotations cannot use f-string so we avoid changing any rules which work on annotation and checks for `ExprStringLiteral`. ## Test Plan - All references of `Expr::StringLiteral` were checked to see if any of the rules require updating to account for the f-string literal element node. - New test cases are added for rules which check against the literal part of an f-string. - Check the ecosystem results and ensure it remains unchanged. ## Performance There's a performance penalty in the parser. The reason for this remains unknown as it seems that the generated assembly code is now different for the `__reduce154` function. The reduce function body is just popping the `ParenthesizedExpr` on top of the stack and pushing it with the new location. - The size of `FStringElement` enum is the same as `Expr` which is what it replaces in `FString::format_spec` - The size of `FStringExpressionElement` is the same as `ExprFormattedValue` which is what it replaces I tried reducing the `Expr` enum from 80 bytes to 72 bytes but it hardly resulted in any performance gain. The difference can be seen here: - Original profile: https://share.firefox.dev/3Taa7ES - Profile after boxing some node fields: https://share.firefox.dev/3GsNXpD ### Backtracking I tried backtracking the changes to see if any of the isolated change produced this regression. The problem here is that the overall change is so small that there's only a single checkpoint where I can backtrack and that checkpoint results in the same regression. This checkpoint is to revert using `Expr` to the `FString::format_spec` field. After this point, the change would revert back to the original implementation. ## Review process The review process is similar to #7927. The first set of commits update the node structure, parser, and related AST files. Then, further commits update the linter and formatter part to account for the AST change. --------- Co-authored-by: David Szotten <davidszotten@gmail.com>
This commit is contained in:
parent
fcc08894cf
commit
cdac90ef68
77 changed files with 1714 additions and 1925 deletions
|
@ -509,6 +509,41 @@ impl<'a> From<&'a ast::ExceptHandler> for ComparableExceptHandler<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub enum ComparableFStringElement<'a> {
|
||||
Literal(&'a str),
|
||||
FStringExpressionElement(FStringExpressionElement<'a>),
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct FStringExpressionElement<'a> {
|
||||
expression: ComparableExpr<'a>,
|
||||
debug_text: Option<&'a ast::DebugText>,
|
||||
conversion: ast::ConversionFlag,
|
||||
format_spec: Option<Vec<ComparableFStringElement<'a>>>,
|
||||
}
|
||||
|
||||
impl<'a> From<&'a ast::FStringElement> for ComparableFStringElement<'a> {
|
||||
fn from(fstring_element: &'a ast::FStringElement) -> Self {
|
||||
match fstring_element {
|
||||
ast::FStringElement::Literal(ast::FStringLiteralElement { value, .. }) => {
|
||||
Self::Literal(value)
|
||||
}
|
||||
ast::FStringElement::Expression(formatted_value) => {
|
||||
Self::FStringExpressionElement(FStringExpressionElement {
|
||||
expression: (&formatted_value.expression).into(),
|
||||
debug_text: formatted_value.debug_text.as_ref(),
|
||||
conversion: formatted_value.conversion,
|
||||
format_spec: formatted_value
|
||||
.format_spec
|
||||
.as_ref()
|
||||
.map(|spec| spec.elements.iter().map(Into::into).collect()),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct ComparableElifElseClause<'a> {
|
||||
test: Option<ComparableExpr<'a>>,
|
||||
|
@ -562,13 +597,13 @@ impl<'a> From<ast::LiteralExpressionRef<'a>> for ComparableLiteral<'a> {
|
|||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct ComparableFString<'a> {
|
||||
values: Vec<ComparableExpr<'a>>,
|
||||
elements: Vec<ComparableFStringElement<'a>>,
|
||||
}
|
||||
|
||||
impl<'a> From<&'a ast::FString> for ComparableFString<'a> {
|
||||
fn from(fstring: &'a ast::FString) -> Self {
|
||||
Self {
|
||||
values: fstring.values.iter().map(Into::into).collect(),
|
||||
elements: fstring.elements.iter().map(Into::into).collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -717,11 +752,11 @@ pub struct ExprCall<'a> {
|
|||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct ExprFormattedValue<'a> {
|
||||
pub struct ExprFStringExpressionElement<'a> {
|
||||
value: Box<ComparableExpr<'a>>,
|
||||
debug_text: Option<&'a ast::DebugText>,
|
||||
conversion: ast::ConversionFlag,
|
||||
format_spec: Option<Box<ComparableExpr<'a>>>,
|
||||
format_spec: Vec<ComparableFStringElement<'a>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
|
@ -813,7 +848,7 @@ pub enum ComparableExpr<'a> {
|
|||
YieldFrom(ExprYieldFrom<'a>),
|
||||
Compare(ExprCompare<'a>),
|
||||
Call(ExprCall<'a>),
|
||||
FormattedValue(ExprFormattedValue<'a>),
|
||||
FStringExpressionElement(ExprFStringExpressionElement<'a>),
|
||||
FString(ExprFString<'a>),
|
||||
StringLiteral(ExprStringLiteral<'a>),
|
||||
BytesLiteral(ExprBytesLiteral<'a>),
|
||||
|
@ -975,18 +1010,6 @@ impl<'a> From<&'a ast::Expr> for ComparableExpr<'a> {
|
|||
func: func.into(),
|
||||
arguments: arguments.into(),
|
||||
}),
|
||||
ast::Expr::FormattedValue(ast::ExprFormattedValue {
|
||||
value,
|
||||
conversion,
|
||||
debug_text,
|
||||
format_spec,
|
||||
range: _,
|
||||
}) => Self::FormattedValue(ExprFormattedValue {
|
||||
value: value.into(),
|
||||
conversion: *conversion,
|
||||
debug_text: debug_text.as_ref(),
|
||||
format_spec: format_spec.as_ref().map(Into::into),
|
||||
}),
|
||||
ast::Expr::FString(ast::ExprFString { value, range: _ }) => {
|
||||
Self::FString(ExprFString {
|
||||
parts: value.parts().map(Into::into).collect(),
|
||||
|
|
|
@ -23,7 +23,6 @@ pub enum ExpressionRef<'a> {
|
|||
YieldFrom(&'a ast::ExprYieldFrom),
|
||||
Compare(&'a ast::ExprCompare),
|
||||
Call(&'a ast::ExprCall),
|
||||
FormattedValue(&'a ast::ExprFormattedValue),
|
||||
FString(&'a ast::ExprFString),
|
||||
StringLiteral(&'a ast::ExprStringLiteral),
|
||||
BytesLiteral(&'a ast::ExprBytesLiteral),
|
||||
|
@ -67,7 +66,6 @@ impl<'a> From<&'a Expr> for ExpressionRef<'a> {
|
|||
Expr::YieldFrom(value) => ExpressionRef::YieldFrom(value),
|
||||
Expr::Compare(value) => ExpressionRef::Compare(value),
|
||||
Expr::Call(value) => ExpressionRef::Call(value),
|
||||
Expr::FormattedValue(value) => ExpressionRef::FormattedValue(value),
|
||||
Expr::FString(value) => ExpressionRef::FString(value),
|
||||
Expr::StringLiteral(value) => ExpressionRef::StringLiteral(value),
|
||||
Expr::BytesLiteral(value) => ExpressionRef::BytesLiteral(value),
|
||||
|
@ -172,11 +170,6 @@ impl<'a> From<&'a ast::ExprCall> for ExpressionRef<'a> {
|
|||
Self::Call(value)
|
||||
}
|
||||
}
|
||||
impl<'a> From<&'a ast::ExprFormattedValue> for ExpressionRef<'a> {
|
||||
fn from(value: &'a ast::ExprFormattedValue) -> Self {
|
||||
Self::FormattedValue(value)
|
||||
}
|
||||
}
|
||||
impl<'a> From<&'a ast::ExprFString> for ExpressionRef<'a> {
|
||||
fn from(value: &'a ast::ExprFString) -> Self {
|
||||
Self::FString(value)
|
||||
|
@ -273,7 +266,6 @@ impl<'a> From<ExpressionRef<'a>> for AnyNodeRef<'a> {
|
|||
ExpressionRef::YieldFrom(expression) => AnyNodeRef::ExprYieldFrom(expression),
|
||||
ExpressionRef::Compare(expression) => AnyNodeRef::ExprCompare(expression),
|
||||
ExpressionRef::Call(expression) => AnyNodeRef::ExprCall(expression),
|
||||
ExpressionRef::FormattedValue(expression) => AnyNodeRef::ExprFormattedValue(expression),
|
||||
ExpressionRef::FString(expression) => AnyNodeRef::ExprFString(expression),
|
||||
ExpressionRef::StringLiteral(expression) => AnyNodeRef::ExprStringLiteral(expression),
|
||||
ExpressionRef::BytesLiteral(expression) => AnyNodeRef::ExprBytesLiteral(expression),
|
||||
|
@ -317,7 +309,6 @@ impl Ranged for ExpressionRef<'_> {
|
|||
ExpressionRef::YieldFrom(expression) => expression.range(),
|
||||
ExpressionRef::Compare(expression) => expression.range(),
|
||||
ExpressionRef::Call(expression) => expression.range(),
|
||||
ExpressionRef::FormattedValue(expression) => expression.range(),
|
||||
ExpressionRef::FString(expression) => expression.range(),
|
||||
ExpressionRef::StringLiteral(expression) => expression.range(),
|
||||
ExpressionRef::BytesLiteral(expression) => expression.range(),
|
||||
|
|
|
@ -12,8 +12,8 @@ use crate::parenthesize::parenthesized_range;
|
|||
use crate::statement_visitor::StatementVisitor;
|
||||
use crate::visitor::Visitor;
|
||||
use crate::{
|
||||
self as ast, Arguments, CmpOp, ExceptHandler, Expr, MatchCase, Operator, Pattern, Stmt,
|
||||
TypeParam,
|
||||
self as ast, Arguments, CmpOp, ExceptHandler, Expr, FStringElement, MatchCase, Operator,
|
||||
Pattern, Stmt, TypeParam,
|
||||
};
|
||||
use crate::{AnyNodeRef, ExprContext};
|
||||
|
||||
|
@ -136,9 +136,9 @@ pub fn any_over_expr(expr: &Expr, func: &dyn Fn(&Expr) -> bool) -> bool {
|
|||
Expr::BoolOp(ast::ExprBoolOp { values, .. }) => {
|
||||
values.iter().any(|expr| any_over_expr(expr, func))
|
||||
}
|
||||
Expr::FString(ast::ExprFString { value, .. }) => {
|
||||
value.elements().any(|expr| any_over_expr(expr, func))
|
||||
}
|
||||
Expr::FString(ast::ExprFString { value, .. }) => value
|
||||
.elements()
|
||||
.any(|expr| any_over_f_string_element(expr, func)),
|
||||
Expr::NamedExpr(ast::ExprNamedExpr {
|
||||
target,
|
||||
value,
|
||||
|
@ -231,14 +231,6 @@ pub fn any_over_expr(expr: &Expr, func: &dyn Fn(&Expr) -> bool) -> bool {
|
|||
.iter()
|
||||
.any(|keyword| any_over_expr(&keyword.value, func))
|
||||
}
|
||||
Expr::FormattedValue(ast::ExprFormattedValue {
|
||||
value, format_spec, ..
|
||||
}) => {
|
||||
any_over_expr(value, func)
|
||||
|| format_spec
|
||||
.as_ref()
|
||||
.is_some_and(|value| any_over_expr(value, func))
|
||||
}
|
||||
Expr::Subscript(ast::ExprSubscript { value, slice, .. }) => {
|
||||
any_over_expr(value, func) || any_over_expr(slice, func)
|
||||
}
|
||||
|
@ -315,6 +307,24 @@ pub fn any_over_pattern(pattern: &Pattern, func: &dyn Fn(&Expr) -> bool) -> bool
|
|||
}
|
||||
}
|
||||
|
||||
pub fn any_over_f_string_element(element: &FStringElement, func: &dyn Fn(&Expr) -> bool) -> bool {
|
||||
match element {
|
||||
FStringElement::Literal(_) => false,
|
||||
FStringElement::Expression(ast::FStringExpressionElement {
|
||||
expression,
|
||||
format_spec,
|
||||
..
|
||||
}) => {
|
||||
any_over_expr(expression, func)
|
||||
|| format_spec.as_ref().is_some_and(|spec| {
|
||||
spec.elements
|
||||
.iter()
|
||||
.any(|spec_element| any_over_f_string_element(spec_element, func))
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn any_over_stmt(stmt: &Stmt, func: &dyn Fn(&Expr) -> bool) -> bool {
|
||||
match stmt {
|
||||
Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||
|
@ -1318,16 +1328,18 @@ impl Truthiness {
|
|||
Expr::FString(ast::ExprFString { value, .. }) => {
|
||||
if value.parts().all(|part| match part {
|
||||
ast::FStringPart::Literal(string_literal) => string_literal.is_empty(),
|
||||
ast::FStringPart::FString(f_string) => f_string.values.is_empty(),
|
||||
ast::FStringPart::FString(f_string) => f_string.elements.is_empty(),
|
||||
}) {
|
||||
Self::Falsey
|
||||
} else if value.elements().any(|expr| {
|
||||
if let Expr::StringLiteral(ast::ExprStringLiteral { value, .. }) = &expr {
|
||||
!value.is_empty()
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}) {
|
||||
} else if value
|
||||
.elements()
|
||||
.any(|f_string_element| match f_string_element {
|
||||
ast::FStringElement::Literal(ast::FStringLiteralElement {
|
||||
value, ..
|
||||
}) => !value.is_empty(),
|
||||
ast::FStringElement::Expression(_) => true,
|
||||
})
|
||||
{
|
||||
Self::Truthy
|
||||
} else {
|
||||
Self::Unknown
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use crate::visitor::preorder::PreorderVisitor;
|
||||
use crate::{
|
||||
self as ast, Alias, ArgOrKeyword, Arguments, Comprehension, Decorator, ExceptHandler, Expr,
|
||||
Keyword, MatchCase, Mod, Parameter, ParameterWithDefault, Parameters, Pattern,
|
||||
FStringElement, Keyword, MatchCase, Mod, Parameter, ParameterWithDefault, Parameters, Pattern,
|
||||
PatternArguments, PatternKeyword, Stmt, TypeParam, TypeParamParamSpec, TypeParamTypeVar,
|
||||
TypeParamTypeVarTuple, TypeParams, WithItem,
|
||||
};
|
||||
|
@ -71,7 +71,6 @@ pub enum AnyNode {
|
|||
ExprYieldFrom(ast::ExprYieldFrom),
|
||||
ExprCompare(ast::ExprCompare),
|
||||
ExprCall(ast::ExprCall),
|
||||
ExprFormattedValue(ast::ExprFormattedValue),
|
||||
ExprFString(ast::ExprFString),
|
||||
ExprStringLiteral(ast::ExprStringLiteral),
|
||||
ExprBytesLiteral(ast::ExprBytesLiteral),
|
||||
|
@ -88,6 +87,8 @@ pub enum AnyNode {
|
|||
ExprSlice(ast::ExprSlice),
|
||||
ExprIpyEscapeCommand(ast::ExprIpyEscapeCommand),
|
||||
ExceptHandlerExceptHandler(ast::ExceptHandlerExceptHandler),
|
||||
FStringExpressionElement(ast::FStringExpressionElement),
|
||||
FStringLiteralElement(ast::FStringLiteralElement),
|
||||
PatternMatchValue(ast::PatternMatchValue),
|
||||
PatternMatchSingleton(ast::PatternMatchSingleton),
|
||||
PatternMatchSequence(ast::PatternMatchSequence),
|
||||
|
@ -166,7 +167,8 @@ impl AnyNode {
|
|||
| AnyNode::ExprYieldFrom(_)
|
||||
| AnyNode::ExprCompare(_)
|
||||
| AnyNode::ExprCall(_)
|
||||
| AnyNode::ExprFormattedValue(_)
|
||||
| AnyNode::FStringExpressionElement(_)
|
||||
| AnyNode::FStringLiteralElement(_)
|
||||
| AnyNode::ExprFString(_)
|
||||
| AnyNode::ExprStringLiteral(_)
|
||||
| AnyNode::ExprBytesLiteral(_)
|
||||
|
@ -233,7 +235,6 @@ impl AnyNode {
|
|||
AnyNode::ExprYieldFrom(node) => Some(Expr::YieldFrom(node)),
|
||||
AnyNode::ExprCompare(node) => Some(Expr::Compare(node)),
|
||||
AnyNode::ExprCall(node) => Some(Expr::Call(node)),
|
||||
AnyNode::ExprFormattedValue(node) => Some(Expr::FormattedValue(node)),
|
||||
AnyNode::ExprFString(node) => Some(Expr::FString(node)),
|
||||
AnyNode::ExprStringLiteral(node) => Some(Expr::StringLiteral(node)),
|
||||
AnyNode::ExprBytesLiteral(node) => Some(Expr::BytesLiteral(node)),
|
||||
|
@ -278,6 +279,8 @@ impl AnyNode {
|
|||
| AnyNode::StmtContinue(_)
|
||||
| AnyNode::StmtIpyEscapeCommand(_)
|
||||
| AnyNode::ExceptHandlerExceptHandler(_)
|
||||
| AnyNode::FStringExpressionElement(_)
|
||||
| AnyNode::FStringLiteralElement(_)
|
||||
| AnyNode::PatternMatchValue(_)
|
||||
| AnyNode::PatternMatchSingleton(_)
|
||||
| AnyNode::PatternMatchSequence(_)
|
||||
|
@ -356,7 +359,8 @@ impl AnyNode {
|
|||
| AnyNode::ExprYieldFrom(_)
|
||||
| AnyNode::ExprCompare(_)
|
||||
| AnyNode::ExprCall(_)
|
||||
| AnyNode::ExprFormattedValue(_)
|
||||
| AnyNode::FStringExpressionElement(_)
|
||||
| AnyNode::FStringLiteralElement(_)
|
||||
| AnyNode::ExprFString(_)
|
||||
| AnyNode::ExprStringLiteral(_)
|
||||
| AnyNode::ExprBytesLiteral(_)
|
||||
|
@ -459,7 +463,8 @@ impl AnyNode {
|
|||
| AnyNode::ExprYieldFrom(_)
|
||||
| AnyNode::ExprCompare(_)
|
||||
| AnyNode::ExprCall(_)
|
||||
| AnyNode::ExprFormattedValue(_)
|
||||
| AnyNode::FStringExpressionElement(_)
|
||||
| AnyNode::FStringLiteralElement(_)
|
||||
| AnyNode::ExprFString(_)
|
||||
| AnyNode::ExprStringLiteral(_)
|
||||
| AnyNode::ExprBytesLiteral(_)
|
||||
|
@ -547,7 +552,8 @@ impl AnyNode {
|
|||
| AnyNode::ExprYieldFrom(_)
|
||||
| AnyNode::ExprCompare(_)
|
||||
| AnyNode::ExprCall(_)
|
||||
| AnyNode::ExprFormattedValue(_)
|
||||
| AnyNode::FStringExpressionElement(_)
|
||||
| AnyNode::FStringLiteralElement(_)
|
||||
| AnyNode::ExprFString(_)
|
||||
| AnyNode::ExprStringLiteral(_)
|
||||
| AnyNode::ExprBytesLiteral(_)
|
||||
|
@ -660,7 +666,8 @@ impl AnyNode {
|
|||
Self::ExprYieldFrom(node) => AnyNodeRef::ExprYieldFrom(node),
|
||||
Self::ExprCompare(node) => AnyNodeRef::ExprCompare(node),
|
||||
Self::ExprCall(node) => AnyNodeRef::ExprCall(node),
|
||||
Self::ExprFormattedValue(node) => AnyNodeRef::ExprFormattedValue(node),
|
||||
Self::FStringExpressionElement(node) => AnyNodeRef::FStringExpressionElement(node),
|
||||
Self::FStringLiteralElement(node) => AnyNodeRef::FStringLiteralElement(node),
|
||||
Self::ExprFString(node) => AnyNodeRef::ExprFString(node),
|
||||
Self::ExprStringLiteral(node) => AnyNodeRef::ExprStringLiteral(node),
|
||||
Self::ExprBytesLiteral(node) => AnyNodeRef::ExprBytesLiteral(node),
|
||||
|
@ -2621,12 +2628,12 @@ impl AstNode for ast::ExprCall {
|
|||
visitor.visit_arguments(arguments);
|
||||
}
|
||||
}
|
||||
impl AstNode for ast::ExprFormattedValue {
|
||||
impl AstNode for ast::FStringExpressionElement {
|
||||
fn cast(kind: AnyNode) -> Option<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
if let AnyNode::ExprFormattedValue(node) = kind {
|
||||
if let AnyNode::FStringExpressionElement(node) = kind {
|
||||
Some(node)
|
||||
} else {
|
||||
None
|
||||
|
@ -2634,7 +2641,7 @@ impl AstNode for ast::ExprFormattedValue {
|
|||
}
|
||||
|
||||
fn cast_ref(kind: AnyNodeRef) -> Option<&Self> {
|
||||
if let AnyNodeRef::ExprFormattedValue(node) = kind {
|
||||
if let AnyNodeRef::FStringExpressionElement(node) = kind {
|
||||
Some(node)
|
||||
} else {
|
||||
None
|
||||
|
@ -2653,16 +2660,54 @@ impl AstNode for ast::ExprFormattedValue {
|
|||
where
|
||||
V: PreorderVisitor<'a> + ?Sized,
|
||||
{
|
||||
let ast::ExprFormattedValue {
|
||||
value, format_spec, ..
|
||||
let ast::FStringExpressionElement {
|
||||
expression,
|
||||
format_spec,
|
||||
..
|
||||
} = self;
|
||||
visitor.visit_expr(value);
|
||||
visitor.visit_expr(expression);
|
||||
|
||||
if let Some(expr) = format_spec {
|
||||
visitor.visit_format_spec(expr);
|
||||
if let Some(format_spec) = format_spec {
|
||||
for spec_part in &format_spec.elements {
|
||||
visitor.visit_f_string_element(spec_part);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
impl AstNode for ast::FStringLiteralElement {
|
||||
fn cast(kind: AnyNode) -> Option<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
if let AnyNode::FStringLiteralElement(node) = kind {
|
||||
Some(node)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn cast_ref(kind: AnyNodeRef) -> Option<&Self> {
|
||||
if let AnyNodeRef::FStringLiteralElement(node) = kind {
|
||||
Some(node)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn as_any_node_ref(&self) -> AnyNodeRef {
|
||||
AnyNodeRef::from(self)
|
||||
}
|
||||
|
||||
fn into_any_node(self) -> AnyNode {
|
||||
AnyNode::from(self)
|
||||
}
|
||||
|
||||
fn visit_preorder<'a, V>(&'a self, _visitor: &mut V)
|
||||
where
|
||||
V: PreorderVisitor<'a> + ?Sized,
|
||||
{
|
||||
}
|
||||
}
|
||||
impl AstNode for ast::ExprFString {
|
||||
fn cast(kind: AnyNode) -> Option<Self>
|
||||
where
|
||||
|
@ -4339,10 +4384,10 @@ impl AstNode for ast::FString {
|
|||
where
|
||||
V: PreorderVisitor<'a> + ?Sized,
|
||||
{
|
||||
let ast::FString { values, range: _ } = self;
|
||||
let ast::FString { elements, range: _ } = self;
|
||||
|
||||
for expr in values {
|
||||
visitor.visit_expr(expr);
|
||||
for fstring_element in elements {
|
||||
visitor.visit_f_string_element(fstring_element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4467,7 +4512,6 @@ impl From<Expr> for AnyNode {
|
|||
Expr::YieldFrom(node) => AnyNode::ExprYieldFrom(node),
|
||||
Expr::Compare(node) => AnyNode::ExprCompare(node),
|
||||
Expr::Call(node) => AnyNode::ExprCall(node),
|
||||
Expr::FormattedValue(node) => AnyNode::ExprFormattedValue(node),
|
||||
Expr::FString(node) => AnyNode::ExprFString(node),
|
||||
Expr::StringLiteral(node) => AnyNode::ExprStringLiteral(node),
|
||||
Expr::BytesLiteral(node) => AnyNode::ExprBytesLiteral(node),
|
||||
|
@ -4496,6 +4540,15 @@ impl From<Mod> for AnyNode {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<FStringElement> for AnyNode {
|
||||
fn from(element: FStringElement) -> Self {
|
||||
match element {
|
||||
FStringElement::Literal(node) => AnyNode::FStringLiteralElement(node),
|
||||
FStringElement::Expression(node) => AnyNode::FStringExpressionElement(node),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Pattern> for AnyNode {
|
||||
fn from(pattern: Pattern) -> Self {
|
||||
match pattern {
|
||||
|
@ -4789,9 +4842,15 @@ impl From<ast::ExprCall> for AnyNode {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<ast::ExprFormattedValue> for AnyNode {
|
||||
fn from(node: ast::ExprFormattedValue) -> Self {
|
||||
AnyNode::ExprFormattedValue(node)
|
||||
impl From<ast::FStringExpressionElement> for AnyNode {
|
||||
fn from(node: ast::FStringExpressionElement) -> Self {
|
||||
AnyNode::FStringExpressionElement(node)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ast::FStringLiteralElement> for AnyNode {
|
||||
fn from(node: ast::FStringLiteralElement) -> Self {
|
||||
AnyNode::FStringLiteralElement(node)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5089,7 +5148,8 @@ impl Ranged for AnyNode {
|
|||
AnyNode::ExprYieldFrom(node) => node.range(),
|
||||
AnyNode::ExprCompare(node) => node.range(),
|
||||
AnyNode::ExprCall(node) => node.range(),
|
||||
AnyNode::ExprFormattedValue(node) => node.range(),
|
||||
AnyNode::FStringExpressionElement(node) => node.range(),
|
||||
AnyNode::FStringLiteralElement(node) => node.range(),
|
||||
AnyNode::ExprFString(node) => node.range(),
|
||||
AnyNode::ExprStringLiteral(node) => node.range(),
|
||||
AnyNode::ExprBytesLiteral(node) => node.range(),
|
||||
|
@ -5184,7 +5244,8 @@ pub enum AnyNodeRef<'a> {
|
|||
ExprYieldFrom(&'a ast::ExprYieldFrom),
|
||||
ExprCompare(&'a ast::ExprCompare),
|
||||
ExprCall(&'a ast::ExprCall),
|
||||
ExprFormattedValue(&'a ast::ExprFormattedValue),
|
||||
FStringExpressionElement(&'a ast::FStringExpressionElement),
|
||||
FStringLiteralElement(&'a ast::FStringLiteralElement),
|
||||
ExprFString(&'a ast::ExprFString),
|
||||
ExprStringLiteral(&'a ast::ExprStringLiteral),
|
||||
ExprBytesLiteral(&'a ast::ExprBytesLiteral),
|
||||
|
@ -5278,7 +5339,8 @@ impl<'a> AnyNodeRef<'a> {
|
|||
AnyNodeRef::ExprYieldFrom(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::ExprCompare(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::ExprCall(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::ExprFormattedValue(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::FStringExpressionElement(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::FStringLiteralElement(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::ExprFString(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::ExprStringLiteral(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::ExprBytesLiteral(node) => NonNull::from(*node).cast(),
|
||||
|
@ -5378,7 +5440,8 @@ impl<'a> AnyNodeRef<'a> {
|
|||
AnyNodeRef::ExprYieldFrom(_) => NodeKind::ExprYieldFrom,
|
||||
AnyNodeRef::ExprCompare(_) => NodeKind::ExprCompare,
|
||||
AnyNodeRef::ExprCall(_) => NodeKind::ExprCall,
|
||||
AnyNodeRef::ExprFormattedValue(_) => NodeKind::ExprFormattedValue,
|
||||
AnyNodeRef::FStringExpressionElement(_) => NodeKind::FStringExpressionElement,
|
||||
AnyNodeRef::FStringLiteralElement(_) => NodeKind::FStringLiteralElement,
|
||||
AnyNodeRef::ExprFString(_) => NodeKind::ExprFString,
|
||||
AnyNodeRef::ExprStringLiteral(_) => NodeKind::ExprStringLiteral,
|
||||
AnyNodeRef::ExprBytesLiteral(_) => NodeKind::ExprBytesLiteral,
|
||||
|
@ -5473,7 +5536,8 @@ impl<'a> AnyNodeRef<'a> {
|
|||
| AnyNodeRef::ExprYieldFrom(_)
|
||||
| AnyNodeRef::ExprCompare(_)
|
||||
| AnyNodeRef::ExprCall(_)
|
||||
| AnyNodeRef::ExprFormattedValue(_)
|
||||
| AnyNodeRef::FStringExpressionElement(_)
|
||||
| AnyNodeRef::FStringLiteralElement(_)
|
||||
| AnyNodeRef::ExprFString(_)
|
||||
| AnyNodeRef::ExprStringLiteral(_)
|
||||
| AnyNodeRef::ExprBytesLiteral(_)
|
||||
|
@ -5540,7 +5604,6 @@ impl<'a> AnyNodeRef<'a> {
|
|||
| AnyNodeRef::ExprYieldFrom(_)
|
||||
| AnyNodeRef::ExprCompare(_)
|
||||
| AnyNodeRef::ExprCall(_)
|
||||
| AnyNodeRef::ExprFormattedValue(_)
|
||||
| AnyNodeRef::ExprFString(_)
|
||||
| AnyNodeRef::ExprStringLiteral(_)
|
||||
| AnyNodeRef::ExprBytesLiteral(_)
|
||||
|
@ -5585,6 +5648,8 @@ impl<'a> AnyNodeRef<'a> {
|
|||
| AnyNodeRef::StmtContinue(_)
|
||||
| AnyNodeRef::StmtIpyEscapeCommand(_)
|
||||
| AnyNodeRef::ExceptHandlerExceptHandler(_)
|
||||
| AnyNodeRef::FStringExpressionElement(_)
|
||||
| AnyNodeRef::FStringLiteralElement(_)
|
||||
| AnyNodeRef::PatternMatchValue(_)
|
||||
| AnyNodeRef::PatternMatchSingleton(_)
|
||||
| AnyNodeRef::PatternMatchSequence(_)
|
||||
|
@ -5662,7 +5727,8 @@ impl<'a> AnyNodeRef<'a> {
|
|||
| AnyNodeRef::ExprYieldFrom(_)
|
||||
| AnyNodeRef::ExprCompare(_)
|
||||
| AnyNodeRef::ExprCall(_)
|
||||
| AnyNodeRef::ExprFormattedValue(_)
|
||||
| AnyNodeRef::FStringExpressionElement(_)
|
||||
| AnyNodeRef::FStringLiteralElement(_)
|
||||
| AnyNodeRef::ExprFString(_)
|
||||
| AnyNodeRef::ExprStringLiteral(_)
|
||||
| AnyNodeRef::ExprBytesLiteral(_)
|
||||
|
@ -5765,7 +5831,8 @@ impl<'a> AnyNodeRef<'a> {
|
|||
| AnyNodeRef::ExprYieldFrom(_)
|
||||
| AnyNodeRef::ExprCompare(_)
|
||||
| AnyNodeRef::ExprCall(_)
|
||||
| AnyNodeRef::ExprFormattedValue(_)
|
||||
| AnyNodeRef::FStringExpressionElement(_)
|
||||
| AnyNodeRef::FStringLiteralElement(_)
|
||||
| AnyNodeRef::ExprFString(_)
|
||||
| AnyNodeRef::ExprStringLiteral(_)
|
||||
| AnyNodeRef::ExprBytesLiteral(_)
|
||||
|
@ -5853,7 +5920,8 @@ impl<'a> AnyNodeRef<'a> {
|
|||
| AnyNodeRef::ExprYieldFrom(_)
|
||||
| AnyNodeRef::ExprCompare(_)
|
||||
| AnyNodeRef::ExprCall(_)
|
||||
| AnyNodeRef::ExprFormattedValue(_)
|
||||
| AnyNodeRef::FStringExpressionElement(_)
|
||||
| AnyNodeRef::FStringLiteralElement(_)
|
||||
| AnyNodeRef::ExprFString(_)
|
||||
| AnyNodeRef::ExprStringLiteral(_)
|
||||
| AnyNodeRef::ExprBytesLiteral(_)
|
||||
|
@ -5975,7 +6043,8 @@ impl<'a> AnyNodeRef<'a> {
|
|||
AnyNodeRef::ExprYieldFrom(node) => node.visit_preorder(visitor),
|
||||
AnyNodeRef::ExprCompare(node) => node.visit_preorder(visitor),
|
||||
AnyNodeRef::ExprCall(node) => node.visit_preorder(visitor),
|
||||
AnyNodeRef::ExprFormattedValue(node) => node.visit_preorder(visitor),
|
||||
AnyNodeRef::FStringExpressionElement(node) => node.visit_preorder(visitor),
|
||||
AnyNodeRef::FStringLiteralElement(node) => node.visit_preorder(visitor),
|
||||
AnyNodeRef::ExprFString(node) => node.visit_preorder(visitor),
|
||||
AnyNodeRef::ExprStringLiteral(node) => node.visit_preorder(visitor),
|
||||
AnyNodeRef::ExprBytesLiteral(node) => node.visit_preorder(visitor),
|
||||
|
@ -6354,9 +6423,15 @@ impl<'a> From<&'a ast::ExprCall> for AnyNodeRef<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a ast::ExprFormattedValue> for AnyNodeRef<'a> {
|
||||
fn from(node: &'a ast::ExprFormattedValue) -> Self {
|
||||
AnyNodeRef::ExprFormattedValue(node)
|
||||
impl<'a> From<&'a ast::FStringExpressionElement> for AnyNodeRef<'a> {
|
||||
fn from(node: &'a ast::FStringExpressionElement) -> Self {
|
||||
AnyNodeRef::FStringExpressionElement(node)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a ast::FStringLiteralElement> for AnyNodeRef<'a> {
|
||||
fn from(node: &'a ast::FStringLiteralElement) -> Self {
|
||||
AnyNodeRef::FStringLiteralElement(node)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6615,7 +6690,6 @@ impl<'a> From<&'a Expr> for AnyNodeRef<'a> {
|
|||
Expr::YieldFrom(node) => AnyNodeRef::ExprYieldFrom(node),
|
||||
Expr::Compare(node) => AnyNodeRef::ExprCompare(node),
|
||||
Expr::Call(node) => AnyNodeRef::ExprCall(node),
|
||||
Expr::FormattedValue(node) => AnyNodeRef::ExprFormattedValue(node),
|
||||
Expr::FString(node) => AnyNodeRef::ExprFString(node),
|
||||
Expr::StringLiteral(node) => AnyNodeRef::ExprStringLiteral(node),
|
||||
Expr::BytesLiteral(node) => AnyNodeRef::ExprBytesLiteral(node),
|
||||
|
@ -6644,6 +6718,15 @@ impl<'a> From<&'a Mod> for AnyNodeRef<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a FStringElement> for AnyNodeRef<'a> {
|
||||
fn from(element: &'a FStringElement) -> Self {
|
||||
match element {
|
||||
FStringElement::Expression(node) => AnyNodeRef::FStringExpressionElement(node),
|
||||
FStringElement::Literal(node) => AnyNodeRef::FStringLiteralElement(node),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a Pattern> for AnyNodeRef<'a> {
|
||||
fn from(pattern: &'a Pattern) -> Self {
|
||||
match pattern {
|
||||
|
@ -6772,7 +6855,8 @@ impl Ranged for AnyNodeRef<'_> {
|
|||
AnyNodeRef::ExprYieldFrom(node) => node.range(),
|
||||
AnyNodeRef::ExprCompare(node) => node.range(),
|
||||
AnyNodeRef::ExprCall(node) => node.range(),
|
||||
AnyNodeRef::ExprFormattedValue(node) => node.range(),
|
||||
AnyNodeRef::FStringExpressionElement(node) => node.range(),
|
||||
AnyNodeRef::FStringLiteralElement(node) => node.range(),
|
||||
AnyNodeRef::ExprFString(node) => node.range(),
|
||||
AnyNodeRef::ExprStringLiteral(node) => node.range(),
|
||||
AnyNodeRef::ExprBytesLiteral(node) => node.range(),
|
||||
|
@ -6869,7 +6953,8 @@ pub enum NodeKind {
|
|||
ExprYieldFrom,
|
||||
ExprCompare,
|
||||
ExprCall,
|
||||
ExprFormattedValue,
|
||||
FStringExpressionElement,
|
||||
FStringLiteralElement,
|
||||
ExprFString,
|
||||
ExprStringLiteral,
|
||||
ExprBytesLiteral,
|
||||
|
|
|
@ -590,8 +590,6 @@ pub enum Expr {
|
|||
Compare(ExprCompare),
|
||||
#[is(name = "call_expr")]
|
||||
Call(ExprCall),
|
||||
#[is(name = "formatted_value_expr")]
|
||||
FormattedValue(ExprFormattedValue),
|
||||
#[is(name = "f_string_expr")]
|
||||
FString(ExprFString),
|
||||
#[is(name = "string_literal_expr")]
|
||||
|
@ -919,19 +917,51 @@ impl From<ExprCall> for Expr {
|
|||
}
|
||||
}
|
||||
|
||||
/// See also [FormattedValue](https://docs.python.org/3/library/ast.html#ast.FormattedValue)
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct ExprFormattedValue {
|
||||
pub struct FStringFormatSpec {
|
||||
pub range: TextRange,
|
||||
pub value: Box<Expr>,
|
||||
pub debug_text: Option<DebugText>,
|
||||
pub conversion: ConversionFlag,
|
||||
pub format_spec: Option<Box<Expr>>,
|
||||
pub elements: Vec<FStringElement>,
|
||||
}
|
||||
|
||||
impl From<ExprFormattedValue> for Expr {
|
||||
fn from(payload: ExprFormattedValue) -> Self {
|
||||
Expr::FormattedValue(payload)
|
||||
impl Ranged for FStringFormatSpec {
|
||||
fn range(&self) -> TextRange {
|
||||
self.range
|
||||
}
|
||||
}
|
||||
|
||||
/// See also [FormattedValue](https://docs.python.org/3/library/ast.html#ast.FormattedValue)
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct FStringExpressionElement {
|
||||
pub range: TextRange,
|
||||
pub expression: Box<Expr>,
|
||||
pub debug_text: Option<DebugText>,
|
||||
pub conversion: ConversionFlag,
|
||||
pub format_spec: Option<Box<FStringFormatSpec>>,
|
||||
}
|
||||
|
||||
impl Ranged for FStringExpressionElement {
|
||||
fn range(&self) -> TextRange {
|
||||
self.range
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct FStringLiteralElement {
|
||||
pub range: TextRange,
|
||||
pub value: String,
|
||||
}
|
||||
|
||||
impl Ranged for FStringLiteralElement {
|
||||
fn range(&self) -> TextRange {
|
||||
self.range
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for FStringLiteralElement {
|
||||
type Target = str;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.value.as_str()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1064,7 +1094,7 @@ impl FStringValue {
|
|||
self.parts().filter_map(|part| part.as_f_string())
|
||||
}
|
||||
|
||||
/// Returns an iterator over all the f-string elements contained in this value.
|
||||
/// Returns an iterator over all the [`FStringElement`] contained in this value.
|
||||
///
|
||||
/// An f-string element is what makes up an [`FString`] i.e., it is either a
|
||||
/// string literal or an expression. In the following example,
|
||||
|
@ -1075,8 +1105,8 @@ impl FStringValue {
|
|||
///
|
||||
/// The f-string elements returned would be string literal (`"bar "`),
|
||||
/// expression (`x`) and string literal (`"qux"`).
|
||||
pub fn elements(&self) -> impl Iterator<Item = &Expr> {
|
||||
self.f_strings().flat_map(|fstring| fstring.values.iter())
|
||||
pub fn elements(&self) -> impl Iterator<Item = &FStringElement> {
|
||||
self.f_strings().flat_map(|fstring| fstring.elements.iter())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1113,7 +1143,7 @@ impl Ranged for FStringPart {
|
|||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct FString {
|
||||
pub range: TextRange,
|
||||
pub values: Vec<Expr>,
|
||||
pub elements: Vec<FStringElement>,
|
||||
}
|
||||
|
||||
impl Ranged for FString {
|
||||
|
@ -1132,6 +1162,21 @@ impl From<FString> for Expr {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
|
||||
pub enum FStringElement {
|
||||
Literal(FStringLiteralElement),
|
||||
Expression(FStringExpressionElement),
|
||||
}
|
||||
|
||||
impl Ranged for FStringElement {
|
||||
fn range(&self) -> TextRange {
|
||||
match self {
|
||||
FStringElement::Literal(node) => node.range(),
|
||||
FStringElement::Expression(node) => node.range(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An AST node that represents either a single string literal or an implicitly
|
||||
/// concatenated string literals.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
|
@ -3483,11 +3528,6 @@ impl Ranged for crate::nodes::ExprCall {
|
|||
self.range
|
||||
}
|
||||
}
|
||||
impl Ranged for crate::nodes::ExprFormattedValue {
|
||||
fn range(&self) -> TextRange {
|
||||
self.range
|
||||
}
|
||||
}
|
||||
impl Ranged for crate::nodes::ExprFString {
|
||||
fn range(&self) -> TextRange {
|
||||
self.range
|
||||
|
@ -3553,7 +3593,6 @@ impl Ranged for crate::Expr {
|
|||
Self::YieldFrom(node) => node.range(),
|
||||
Self::Compare(node) => node.range(),
|
||||
Self::Call(node) => node.range(),
|
||||
Self::FormattedValue(node) => node.range(),
|
||||
Self::FString(node) => node.range(),
|
||||
Self::StringLiteral(node) => node.range(),
|
||||
Self::BytesLiteral(node) => node.range(),
|
||||
|
|
|
@ -68,9 +68,6 @@ impl Transformer for Relocator {
|
|||
Expr::Call(nodes::ExprCall { range, .. }) => {
|
||||
*range = self.range;
|
||||
}
|
||||
Expr::FormattedValue(nodes::ExprFormattedValue { range, .. }) => {
|
||||
*range = self.range;
|
||||
}
|
||||
Expr::FString(nodes::ExprFString { range, .. }) => {
|
||||
*range = self.range;
|
||||
}
|
||||
|
|
|
@ -5,9 +5,9 @@ pub mod transformer;
|
|||
|
||||
use crate::{
|
||||
self as ast, Alias, Arguments, BoolOp, BytesLiteral, CmpOp, Comprehension, Decorator,
|
||||
ElifElseClause, ExceptHandler, Expr, ExprContext, FString, FStringPart, Keyword, MatchCase,
|
||||
Operator, Parameter, Parameters, Pattern, PatternArguments, PatternKeyword, Stmt,
|
||||
StringLiteral, TypeParam, TypeParamTypeVar, TypeParams, UnaryOp, WithItem,
|
||||
ElifElseClause, ExceptHandler, Expr, ExprContext, FString, FStringElement, FStringPart,
|
||||
Keyword, MatchCase, Operator, Parameter, Parameters, Pattern, PatternArguments, PatternKeyword,
|
||||
Stmt, StringLiteral, TypeParam, TypeParamTypeVar, TypeParams, UnaryOp, WithItem,
|
||||
};
|
||||
|
||||
/// A trait for AST visitors. Visits all nodes in the AST recursively in evaluation-order.
|
||||
|
@ -53,9 +53,6 @@ pub trait Visitor<'a> {
|
|||
fn visit_except_handler(&mut self, except_handler: &'a ExceptHandler) {
|
||||
walk_except_handler(self, except_handler);
|
||||
}
|
||||
fn visit_format_spec(&mut self, format_spec: &'a Expr) {
|
||||
walk_format_spec(self, format_spec);
|
||||
}
|
||||
fn visit_arguments(&mut self, arguments: &'a Arguments) {
|
||||
walk_arguments(self, arguments);
|
||||
}
|
||||
|
@ -101,6 +98,9 @@ pub trait Visitor<'a> {
|
|||
fn visit_f_string(&mut self, f_string: &'a FString) {
|
||||
walk_f_string(self, f_string);
|
||||
}
|
||||
fn visit_f_string_element(&mut self, f_string_element: &'a FStringElement) {
|
||||
walk_f_string_element(self, f_string_element);
|
||||
}
|
||||
fn visit_string_literal(&mut self, string_literal: &'a StringLiteral) {
|
||||
walk_string_literal(self, string_literal);
|
||||
}
|
||||
|
@ -476,14 +476,6 @@ pub fn walk_expr<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, expr: &'a Expr) {
|
|||
visitor.visit_expr(func);
|
||||
visitor.visit_arguments(arguments);
|
||||
}
|
||||
Expr::FormattedValue(ast::ExprFormattedValue {
|
||||
value, format_spec, ..
|
||||
}) => {
|
||||
visitor.visit_expr(value);
|
||||
if let Some(expr) = format_spec {
|
||||
visitor.visit_format_spec(expr);
|
||||
}
|
||||
}
|
||||
Expr::FString(ast::ExprFString { value, .. }) => {
|
||||
for part in value.parts() {
|
||||
match part {
|
||||
|
@ -598,16 +590,6 @@ pub fn walk_except_handler<'a, V: Visitor<'a> + ?Sized>(
|
|||
}
|
||||
}
|
||||
|
||||
pub fn walk_f_string<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, f_string: &'a FString) {
|
||||
for expr in &f_string.values {
|
||||
visitor.visit_expr(expr);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn walk_format_spec<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, format_spec: &'a Expr) {
|
||||
visitor.visit_expr(format_spec);
|
||||
}
|
||||
|
||||
pub fn walk_arguments<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, arguments: &'a Arguments) {
|
||||
// Note that the there might be keywords before the last arg, e.g. in
|
||||
// f(*args, a=2, *args2, **kwargs)`, but we follow Python in evaluating first `args` and then
|
||||
|
@ -757,6 +739,31 @@ pub fn walk_pattern_keyword<'a, V: Visitor<'a> + ?Sized>(
|
|||
visitor.visit_pattern(&pattern_keyword.pattern);
|
||||
}
|
||||
|
||||
pub fn walk_f_string<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, f_string: &'a FString) {
|
||||
for f_string_element in &f_string.elements {
|
||||
visitor.visit_f_string_element(f_string_element);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn walk_f_string_element<'a, V: Visitor<'a> + ?Sized>(
|
||||
visitor: &mut V,
|
||||
f_string_element: &'a FStringElement,
|
||||
) {
|
||||
if let ast::FStringElement::Expression(ast::FStringExpressionElement {
|
||||
expression,
|
||||
format_spec,
|
||||
..
|
||||
}) = f_string_element
|
||||
{
|
||||
visitor.visit_expr(expression);
|
||||
if let Some(format_spec) = format_spec {
|
||||
for spec_element in &format_spec.elements {
|
||||
visitor.visit_f_string_element(spec_element);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn walk_expr_context<'a, V: Visitor<'a> + ?Sized>(
|
||||
_visitor: &V,
|
||||
_expr_context: &'a ExprContext,
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::{
|
||||
Alias, Arguments, BoolOp, BytesLiteral, CmpOp, Comprehension, Decorator, ElifElseClause,
|
||||
ExceptHandler, Expr, FString, Keyword, MatchCase, Mod, Operator, Parameter,
|
||||
ExceptHandler, Expr, FString, FStringElement, Keyword, MatchCase, Mod, Operator, Parameter,
|
||||
ParameterWithDefault, Parameters, Pattern, PatternArguments, PatternKeyword, Singleton, Stmt,
|
||||
StringLiteral, TypeParam, TypeParams, UnaryOp, WithItem,
|
||||
};
|
||||
|
@ -74,11 +74,6 @@ pub trait PreorderVisitor<'a> {
|
|||
walk_except_handler(self, except_handler);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_format_spec(&mut self, format_spec: &'a Expr) {
|
||||
walk_format_spec(self, format_spec);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_arguments(&mut self, arguments: &'a Arguments) {
|
||||
walk_arguments(self, arguments);
|
||||
|
@ -158,6 +153,11 @@ pub trait PreorderVisitor<'a> {
|
|||
walk_f_string(self, f_string);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_f_string_element(&mut self, f_string_element: &'a FStringElement) {
|
||||
walk_f_string_element(self, f_string_element);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_string_literal(&mut self, string_literal: &'a StringLiteral) {
|
||||
walk_string_literal(self, string_literal);
|
||||
|
@ -289,7 +289,6 @@ where
|
|||
Expr::YieldFrom(expr) => expr.visit_preorder(visitor),
|
||||
Expr::Compare(expr) => expr.visit_preorder(visitor),
|
||||
Expr::Call(expr) => expr.visit_preorder(visitor),
|
||||
Expr::FormattedValue(expr) => expr.visit_preorder(visitor),
|
||||
Expr::FString(expr) => expr.visit_preorder(visitor),
|
||||
Expr::StringLiteral(expr) => expr.visit_preorder(visitor),
|
||||
Expr::BytesLiteral(expr) => expr.visit_preorder(visitor),
|
||||
|
@ -518,6 +517,20 @@ where
|
|||
visitor.leave_node(node);
|
||||
}
|
||||
|
||||
pub fn walk_f_string_element<'a, V: PreorderVisitor<'a> + ?Sized>(
|
||||
visitor: &mut V,
|
||||
f_string_element: &'a FStringElement,
|
||||
) {
|
||||
let node = AnyNodeRef::from(f_string_element);
|
||||
if visitor.enter_node(node).is_traverse() {
|
||||
match f_string_element {
|
||||
FStringElement::Expression(element) => element.visit_preorder(visitor),
|
||||
FStringElement::Literal(element) => element.visit_preorder(visitor),
|
||||
}
|
||||
}
|
||||
visitor.leave_node(node);
|
||||
}
|
||||
|
||||
pub fn walk_bool_op<'a, V>(_visitor: &mut V, _bool_op: &'a BoolOp)
|
||||
where
|
||||
V: PreorderVisitor<'a> + ?Sized,
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use crate::{
|
||||
self as ast, Alias, Arguments, BoolOp, BytesLiteral, CmpOp, Comprehension, Decorator,
|
||||
ElifElseClause, ExceptHandler, Expr, ExprContext, FString, Keyword, MatchCase, Operator,
|
||||
Parameter, Parameters, Pattern, PatternArguments, PatternKeyword, Stmt, StringLiteral,
|
||||
TypeParam, TypeParamTypeVar, TypeParams, UnaryOp, WithItem,
|
||||
ElifElseClause, ExceptHandler, Expr, ExprContext, FString, FStringElement, Keyword, MatchCase,
|
||||
Operator, Parameter, Parameters, Pattern, PatternArguments, PatternKeyword, Stmt,
|
||||
StringLiteral, TypeParam, TypeParamTypeVar, TypeParams, UnaryOp, WithItem,
|
||||
};
|
||||
|
||||
/// A trait for transforming ASTs. Visits all nodes in the AST recursively in evaluation-order.
|
||||
|
@ -40,9 +40,6 @@ pub trait Transformer {
|
|||
fn visit_except_handler(&self, except_handler: &mut ExceptHandler) {
|
||||
walk_except_handler(self, except_handler);
|
||||
}
|
||||
fn visit_format_spec(&self, format_spec: &mut Expr) {
|
||||
walk_format_spec(self, format_spec);
|
||||
}
|
||||
fn visit_arguments(&self, arguments: &mut Arguments) {
|
||||
walk_arguments(self, arguments);
|
||||
}
|
||||
|
@ -88,6 +85,9 @@ pub trait Transformer {
|
|||
fn visit_f_string(&self, f_string: &mut FString) {
|
||||
walk_f_string(self, f_string);
|
||||
}
|
||||
fn visit_f_string_element(&self, f_string_element: &mut FStringElement) {
|
||||
walk_f_string_element(self, f_string_element);
|
||||
}
|
||||
fn visit_string_literal(&self, string_literal: &mut StringLiteral) {
|
||||
walk_string_literal(self, string_literal);
|
||||
}
|
||||
|
@ -463,14 +463,6 @@ pub fn walk_expr<V: Transformer + ?Sized>(visitor: &V, expr: &mut Expr) {
|
|||
visitor.visit_expr(func);
|
||||
visitor.visit_arguments(arguments);
|
||||
}
|
||||
Expr::FormattedValue(ast::ExprFormattedValue {
|
||||
value, format_spec, ..
|
||||
}) => {
|
||||
visitor.visit_expr(value);
|
||||
if let Some(expr) = format_spec {
|
||||
visitor.visit_format_spec(expr);
|
||||
}
|
||||
}
|
||||
Expr::FString(ast::ExprFString { value, .. }) => {
|
||||
for f_string_part in value.parts_mut() {
|
||||
match f_string_part {
|
||||
|
@ -584,16 +576,6 @@ pub fn walk_except_handler<V: Transformer + ?Sized>(
|
|||
}
|
||||
}
|
||||
|
||||
pub fn walk_f_string<V: Transformer + ?Sized>(visitor: &V, f_string: &mut FString) {
|
||||
for expr in &mut f_string.values {
|
||||
visitor.visit_expr(expr);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn walk_format_spec<V: Transformer + ?Sized>(visitor: &V, format_spec: &mut Expr) {
|
||||
visitor.visit_expr(format_spec);
|
||||
}
|
||||
|
||||
pub fn walk_arguments<V: Transformer + ?Sized>(visitor: &V, arguments: &mut Arguments) {
|
||||
// Note that the there might be keywords before the last arg, e.g. in
|
||||
// f(*args, a=2, *args2, **kwargs)`, but we follow Python in evaluating first `args` and then
|
||||
|
@ -743,6 +725,31 @@ pub fn walk_pattern_keyword<V: Transformer + ?Sized>(
|
|||
visitor.visit_pattern(&mut pattern_keyword.pattern);
|
||||
}
|
||||
|
||||
pub fn walk_f_string<V: Transformer + ?Sized>(visitor: &V, f_string: &mut FString) {
|
||||
for element in &mut f_string.elements {
|
||||
visitor.visit_f_string_element(element);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn walk_f_string_element<V: Transformer + ?Sized>(
|
||||
visitor: &V,
|
||||
f_string_element: &mut FStringElement,
|
||||
) {
|
||||
if let ast::FStringElement::Expression(ast::FStringExpressionElement {
|
||||
expression,
|
||||
format_spec,
|
||||
..
|
||||
}) = f_string_element
|
||||
{
|
||||
visitor.visit_expr(expression);
|
||||
if let Some(format_spec) = format_spec {
|
||||
for spec_element in &mut format_spec.elements {
|
||||
visitor.visit_f_string_element(spec_element);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn walk_expr_context<V: Transformer + ?Sized>(_visitor: &V, _expr_context: &mut ExprContext) {}
|
||||
|
||||
pub fn walk_bool_op<V: Transformer + ?Sized>(_visitor: &V, _bool_op: &mut BoolOp) {}
|
||||
|
|
|
@ -7,19 +7,12 @@ expression: trace
|
|||
- ExprFString
|
||||
- StringLiteral
|
||||
- FString
|
||||
- ExprStringLiteral
|
||||
- StringLiteral
|
||||
- ExprFormattedValue
|
||||
- FStringLiteralElement
|
||||
- FStringExpressionElement
|
||||
- ExprName
|
||||
- ExprFString
|
||||
- ExprFString
|
||||
- FString
|
||||
- ExprStringLiteral
|
||||
- StringLiteral
|
||||
- ExprFormattedValue
|
||||
- ExprName
|
||||
- ExprStringLiteral
|
||||
- StringLiteral
|
||||
- ExprStringLiteral
|
||||
- StringLiteral
|
||||
- FStringLiteralElement
|
||||
- FStringExpressionElement
|
||||
- ExprName
|
||||
- FStringLiteralElement
|
||||
- FStringLiteralElement
|
||||
|
||||
|
|
|
@ -5,17 +5,13 @@ expression: trace
|
|||
- StmtExpr
|
||||
- ExprFString
|
||||
- StringLiteral
|
||||
- ExprStringLiteral
|
||||
- StringLiteral
|
||||
- ExprFormattedValue
|
||||
- ExprName
|
||||
- ExprFString
|
||||
- ExprStringLiteral
|
||||
- StringLiteral
|
||||
- ExprFormattedValue
|
||||
- FString
|
||||
- FStringLiteralElement
|
||||
- FStringExpressionElement
|
||||
- ExprName
|
||||
- FStringLiteralElement
|
||||
- FStringExpressionElement
|
||||
- ExprName
|
||||
- ExprStringLiteral
|
||||
- StringLiteral
|
||||
- ExprStringLiteral
|
||||
- StringLiteral
|
||||
- FStringLiteralElement
|
||||
- FStringLiteralElement
|
||||
|
||||
|
|
|
@ -7,13 +7,15 @@ use ruff_python_parser::{parse_tokens, Mode};
|
|||
|
||||
use ruff_python_ast::visitor::{
|
||||
walk_alias, walk_bytes_literal, walk_comprehension, walk_except_handler, walk_expr,
|
||||
walk_keyword, walk_match_case, walk_parameter, walk_parameters, walk_pattern, walk_stmt,
|
||||
walk_string_literal, walk_type_param, walk_with_item, Visitor,
|
||||
walk_f_string, walk_f_string_element, walk_keyword, walk_match_case, walk_parameter,
|
||||
walk_parameters, walk_pattern, walk_stmt, walk_string_literal, walk_type_param, walk_with_item,
|
||||
Visitor,
|
||||
};
|
||||
use ruff_python_ast::AnyNodeRef;
|
||||
use ruff_python_ast::{
|
||||
Alias, BoolOp, BytesLiteral, CmpOp, Comprehension, ExceptHandler, Expr, Keyword, MatchCase,
|
||||
Operator, Parameter, Parameters, Pattern, Stmt, StringLiteral, TypeParam, UnaryOp, WithItem,
|
||||
Alias, BoolOp, BytesLiteral, CmpOp, Comprehension, ExceptHandler, Expr, FString,
|
||||
FStringElement, Keyword, MatchCase, Operator, Parameter, Parameters, Pattern, Stmt,
|
||||
StringLiteral, TypeParam, UnaryOp, WithItem,
|
||||
};
|
||||
|
||||
#[test]
|
||||
|
@ -255,12 +257,6 @@ impl Visitor<'_> for RecordVisitor {
|
|||
self.exit_node();
|
||||
}
|
||||
|
||||
fn visit_format_spec(&mut self, format_spec: &Expr) {
|
||||
self.enter_node(format_spec);
|
||||
walk_expr(self, format_spec);
|
||||
self.exit_node();
|
||||
}
|
||||
|
||||
fn visit_parameters(&mut self, parameters: &Parameters) {
|
||||
self.enter_node(parameters);
|
||||
walk_parameters(self, parameters);
|
||||
|
@ -320,4 +316,16 @@ impl Visitor<'_> for RecordVisitor {
|
|||
walk_bytes_literal(self, bytes_literal);
|
||||
self.exit_node();
|
||||
}
|
||||
|
||||
fn visit_f_string(&mut self, f_string: &FString) {
|
||||
self.enter_node(f_string);
|
||||
walk_f_string(self, f_string);
|
||||
self.exit_node();
|
||||
}
|
||||
|
||||
fn visit_f_string_element(&mut self, f_string_element: &FStringElement) {
|
||||
self.enter_node(f_string_element);
|
||||
walk_f_string_element(self, f_string_element);
|
||||
self.exit_node();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue