Split Constant to individual literal nodes (#8064)

## Summary

This PR splits the `Constant` enum as individual literal nodes. It
introduces the following new nodes for each variant:
* `ExprStringLiteral`
* `ExprBytesLiteral`
* `ExprNumberLiteral`
* `ExprBooleanLiteral`
* `ExprNoneLiteral`
* `ExprEllipsisLiteral`

The main motivation behind this refactor is to introduce the new AST
node for implicit string concatenation in the coming PR. The elements of
that node will be either a string literal, bytes literal or a f-string
which can be implemented using an enum. This means that a string or
bytes literal cannot be represented by `Constant::Str` /
`Constant::Bytes` which creates an inconsistency.

This PR avoids that inconsistency by splitting the constant nodes into
it's own literal nodes, literal being the more appropriate naming
convention from a static analysis tool perspective.

This also makes working with literals in the linter and formatter much
more ergonomic like, for example, if one would want to check if this is
a string literal, it can be done easily using
`Expr::is_string_literal_expr` or matching against `Expr::StringLiteral`
as oppose to matching against the `ExprConstant` and enum `Constant`. A
few AST helper methods can be simplified as well which will be done in a
follow-up PR.

This introduces a new `Expr::is_literal_expr` method which is the same
as `Expr::is_constant_expr`. There are also intermediary changes related
to implicit string concatenation which are quiet less. This is done so
as to avoid having a huge PR which this already is.

## Test Plan

1. Verify and update all of the existing snapshots (parser, visitor)
2. Verify that the ecosystem check output remains **unchanged** for both
the linter and formatter

### Formatter ecosystem check

#### `main`

| project | similarity index | total files | changed files |

|----------------|------------------:|------------------:|------------------:|
| cpython | 0.75803 | 1799 | 1647 |
| django | 0.99983 | 2772 | 34 |
| home-assistant | 0.99953 | 10596 | 186 |
| poetry | 0.99891 | 317 | 17 |
| transformers | 0.99966 | 2657 | 330 |
| twine | 1.00000 | 33 | 0 |
| typeshed | 0.99978 | 3669 | 20 |
| warehouse | 0.99977 | 654 | 13 |
| zulip | 0.99970 | 1459 | 22 |

#### `dhruv/constant-to-literal`

| project | similarity index | total files | changed files |

|----------------|------------------:|------------------:|------------------:|
| cpython | 0.75803 | 1799 | 1647 |
| django | 0.99983 | 2772 | 34 |
| home-assistant | 0.99953 | 10596 | 186 |
| poetry | 0.99891 | 317 | 17 |
| transformers | 0.99966 | 2657 | 330 |
| twine | 1.00000 | 33 | 0 |
| typeshed | 0.99978 | 3669 | 20 |
| warehouse | 0.99977 | 654 | 13 |
| zulip | 0.99970 | 1459 | 22 |
This commit is contained in:
Dhruv Manilawala 2023-10-30 12:13:23 +05:30 committed by GitHub
parent 78bbf6d403
commit 230c9ce236
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
268 changed files with 6663 additions and 6741 deletions

View file

@ -1,5 +1,5 @@
use crate::helpers::map_subscript;
use crate::{self as ast, Constant, Expr, Stmt};
use crate::{self as ast, Expr, Stmt};
use bitflags::bitflags;
bitflags! {
@ -23,11 +23,7 @@ where
{
fn add_to_names<'a>(elts: &'a [Expr], names: &mut Vec<&'a str>, flags: &mut DunderAllFlags) {
for elt in elts {
if let Expr::Constant(ast::ExprConstant {
value: Constant::Str(ast::StringConstant { value, .. }),
..
}) = elt
{
if let Expr::StringLiteral(ast::ExprStringLiteral { value, .. }) = elt {
names.push(value);
} else {
*flags |= DunderAllFlags::INVALID_OBJECT;

View file

@ -344,45 +344,21 @@ impl From<&ast::Singleton> for ComparableSingleton {
}
#[derive(Debug, PartialEq, Eq, Hash)]
pub enum ComparableConstant<'a> {
None,
Bool(&'a bool),
Str { value: &'a str, unicode: bool },
Bytes(&'a [u8]),
pub enum ComparableNumber<'a> {
Int(&'a ast::Int),
Float(u64),
Complex { real: u64, imag: u64 },
Ellipsis,
}
impl<'a> From<&'a ast::Constant> for ComparableConstant<'a> {
fn from(constant: &'a ast::Constant) -> Self {
match constant {
ast::Constant::None => Self::None,
ast::Constant::Bool(value) => Self::Bool(value),
ast::Constant::Str(ast::StringConstant {
value,
// Compare strings based on resolved value, not representation (i.e., ignore whether
// the string was implicitly concatenated).
implicit_concatenated: _,
unicode,
}) => Self::Str {
value,
unicode: *unicode,
},
ast::Constant::Bytes(ast::BytesConstant {
value,
// Compare bytes based on resolved value, not representation (i.e., ignore whether
// the bytes were implicitly concatenated).
implicit_concatenated: _,
}) => Self::Bytes(value),
ast::Constant::Int(value) => Self::Int(value),
ast::Constant::Float(value) => Self::Float(value.to_bits()),
ast::Constant::Complex { real, imag } => Self::Complex {
impl<'a> From<&'a ast::Number> for ComparableNumber<'a> {
fn from(number: &'a ast::Number) -> Self {
match number {
ast::Number::Int(value) => Self::Int(value),
ast::Number::Float(value) => Self::Float(value.to_bits()),
ast::Number::Complex { real, imag } => Self::Complex {
real: real.to_bits(),
imag: imag.to_bits(),
},
ast::Constant::Ellipsis => Self::Ellipsis,
}
}
}
@ -669,8 +645,24 @@ pub struct ExprFString<'a> {
}
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct ExprConstant<'a> {
value: ComparableConstant<'a>,
pub struct ExprStringLiteral<'a> {
value: &'a str,
unicode: &'a bool,
}
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct ExprBytesLiteral<'a> {
value: &'a [u8],
}
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct ExprNumberLiteral<'a> {
value: ComparableNumber<'a>,
}
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct ExprBoolLiteral<'a> {
value: &'a bool,
}
#[derive(Debug, PartialEq, Eq, Hash)]
@ -739,7 +731,12 @@ pub enum ComparableExpr<'a> {
Call(ExprCall<'a>),
FormattedValue(ExprFormattedValue<'a>),
FString(ExprFString<'a>),
Constant(ExprConstant<'a>),
StringLiteral(ExprStringLiteral<'a>),
BytesLiteral(ExprBytesLiteral<'a>),
NumberLiteral(ExprNumberLiteral<'a>),
BoolLiteral(ExprBoolLiteral<'a>),
NoneLiteral,
EllispsisLiteral,
Attribute(ExprAttribute<'a>),
Subscript(ExprSubscript<'a>),
Starred(ExprStarred<'a>),
@ -913,11 +910,31 @@ impl<'a> From<&'a ast::Expr> for ComparableExpr<'a> {
}) => Self::FString(ExprFString {
values: values.iter().map(Into::into).collect(),
}),
ast::Expr::Constant(ast::ExprConstant { value, range: _ }) => {
Self::Constant(ExprConstant {
ast::Expr::StringLiteral(ast::ExprStringLiteral {
value,
// Compare strings based on resolved value, not representation (i.e., ignore whether
// the string was implicitly concatenated).
implicit_concatenated: _,
unicode,
range: _,
}) => Self::StringLiteral(ExprStringLiteral { value, unicode }),
ast::Expr::BytesLiteral(ast::ExprBytesLiteral {
value,
// Compare bytes based on resolved value, not representation (i.e., ignore whether
// the bytes was implicitly concatenated).
implicit_concatenated: _,
range: _,
}) => Self::BytesLiteral(ExprBytesLiteral { value }),
ast::Expr::NumberLiteral(ast::ExprNumberLiteral { value, range: _ }) => {
Self::NumberLiteral(ExprNumberLiteral {
value: value.into(),
})
}
ast::Expr::BooleanLiteral(ast::ExprBooleanLiteral { value, range: _ }) => {
Self::BoolLiteral(ExprBoolLiteral { value })
}
ast::Expr::NoneLiteral(_) => Self::NoneLiteral,
ast::Expr::EllipsisLiteral(_) => Self::EllispsisLiteral,
ast::Expr::Attribute(ast::ExprAttribute {
value,
attr,

View file

@ -25,7 +25,12 @@ pub enum ExpressionRef<'a> {
Call(&'a ast::ExprCall),
FormattedValue(&'a ast::ExprFormattedValue),
FString(&'a ast::ExprFString),
Constant(&'a ast::ExprConstant),
StringLiteral(&'a ast::ExprStringLiteral),
BytesLiteral(&'a ast::ExprBytesLiteral),
NumberLiteral(&'a ast::ExprNumberLiteral),
BooleanLiteral(&'a ast::ExprBooleanLiteral),
NoneLiteral(&'a ast::ExprNoneLiteral),
EllipsisLiteral(&'a ast::ExprEllipsisLiteral),
Attribute(&'a ast::ExprAttribute),
Subscript(&'a ast::ExprSubscript),
Starred(&'a ast::ExprStarred),
@ -64,7 +69,12 @@ impl<'a> From<&'a Expr> for ExpressionRef<'a> {
Expr::Call(value) => ExpressionRef::Call(value),
Expr::FormattedValue(value) => ExpressionRef::FormattedValue(value),
Expr::FString(value) => ExpressionRef::FString(value),
Expr::Constant(value) => ExpressionRef::Constant(value),
Expr::StringLiteral(value) => ExpressionRef::StringLiteral(value),
Expr::BytesLiteral(value) => ExpressionRef::BytesLiteral(value),
Expr::NumberLiteral(value) => ExpressionRef::NumberLiteral(value),
Expr::BooleanLiteral(value) => ExpressionRef::BooleanLiteral(value),
Expr::NoneLiteral(value) => ExpressionRef::NoneLiteral(value),
Expr::EllipsisLiteral(value) => ExpressionRef::EllipsisLiteral(value),
Expr::Attribute(value) => ExpressionRef::Attribute(value),
Expr::Subscript(value) => ExpressionRef::Subscript(value),
Expr::Starred(value) => ExpressionRef::Starred(value),
@ -172,9 +182,34 @@ impl<'a> From<&'a ast::ExprFString> for ExpressionRef<'a> {
Self::FString(value)
}
}
impl<'a> From<&'a ast::ExprConstant> for ExpressionRef<'a> {
fn from(value: &'a ast::ExprConstant) -> Self {
Self::Constant(value)
impl<'a> From<&'a ast::ExprStringLiteral> for ExpressionRef<'a> {
fn from(value: &'a ast::ExprStringLiteral) -> Self {
Self::StringLiteral(value)
}
}
impl<'a> From<&'a ast::ExprBytesLiteral> for ExpressionRef<'a> {
fn from(value: &'a ast::ExprBytesLiteral) -> Self {
Self::BytesLiteral(value)
}
}
impl<'a> From<&'a ast::ExprNumberLiteral> for ExpressionRef<'a> {
fn from(value: &'a ast::ExprNumberLiteral) -> Self {
Self::NumberLiteral(value)
}
}
impl<'a> From<&'a ast::ExprBooleanLiteral> for ExpressionRef<'a> {
fn from(value: &'a ast::ExprBooleanLiteral) -> Self {
Self::BooleanLiteral(value)
}
}
impl<'a> From<&'a ast::ExprNoneLiteral> for ExpressionRef<'a> {
fn from(value: &'a ast::ExprNoneLiteral) -> Self {
Self::NoneLiteral(value)
}
}
impl<'a> From<&'a ast::ExprEllipsisLiteral> for ExpressionRef<'a> {
fn from(value: &'a ast::ExprEllipsisLiteral) -> Self {
Self::EllipsisLiteral(value)
}
}
impl<'a> From<&'a ast::ExprAttribute> for ExpressionRef<'a> {
@ -240,7 +275,14 @@ impl<'a> From<ExpressionRef<'a>> for AnyNodeRef<'a> {
ExpressionRef::Call(expression) => AnyNodeRef::ExprCall(expression),
ExpressionRef::FormattedValue(expression) => AnyNodeRef::ExprFormattedValue(expression),
ExpressionRef::FString(expression) => AnyNodeRef::ExprFString(expression),
ExpressionRef::Constant(expression) => AnyNodeRef::ExprConstant(expression),
ExpressionRef::StringLiteral(expression) => AnyNodeRef::ExprStringLiteral(expression),
ExpressionRef::BytesLiteral(expression) => AnyNodeRef::ExprBytesLiteral(expression),
ExpressionRef::NumberLiteral(expression) => AnyNodeRef::ExprNumberLiteral(expression),
ExpressionRef::BooleanLiteral(expression) => AnyNodeRef::ExprBooleanLiteral(expression),
ExpressionRef::NoneLiteral(expression) => AnyNodeRef::ExprNoneLiteral(expression),
ExpressionRef::EllipsisLiteral(expression) => {
AnyNodeRef::ExprEllipsisLiteral(expression)
}
ExpressionRef::Attribute(expression) => AnyNodeRef::ExprAttribute(expression),
ExpressionRef::Subscript(expression) => AnyNodeRef::ExprSubscript(expression),
ExpressionRef::Starred(expression) => AnyNodeRef::ExprStarred(expression),
@ -277,7 +319,12 @@ impl Ranged for ExpressionRef<'_> {
ExpressionRef::Call(expression) => expression.range(),
ExpressionRef::FormattedValue(expression) => expression.range(),
ExpressionRef::FString(expression) => expression.range(),
ExpressionRef::Constant(expression) => expression.range(),
ExpressionRef::StringLiteral(expression) => expression.range(),
ExpressionRef::BytesLiteral(expression) => expression.range(),
ExpressionRef::NumberLiteral(expression) => expression.range(),
ExpressionRef::BooleanLiteral(expression) => expression.range(),
ExpressionRef::NoneLiteral(expression) => expression.range(),
ExpressionRef::EllipsisLiteral(expression) => expression.range(),
ExpressionRef::Attribute(expression) => expression.range(),
ExpressionRef::Subscript(expression) => expression.range(),
ExpressionRef::Starred(expression) => expression.range(),

View file

@ -12,8 +12,7 @@ use crate::parenthesize::parenthesized_range;
use crate::statement_visitor::{walk_body, walk_stmt, StatementVisitor};
use crate::AnyNodeRef;
use crate::{
self as ast, Arguments, CmpOp, Constant, ExceptHandler, Expr, MatchCase, Pattern, Stmt,
TypeParam,
self as ast, Arguments, CmpOp, ExceptHandler, Expr, MatchCase, Pattern, Stmt, TypeParam,
};
/// Return `true` if the `Stmt` is a compound statement (as opposed to a simple statement).
@ -69,7 +68,12 @@ where
if let Expr::BinOp(ast::ExprBinOp { left, right, .. }) = expr {
if !matches!(
left.as_ref(),
Expr::Constant(_)
Expr::StringLiteral(_)
| Expr::BytesLiteral(_)
| Expr::NumberLiteral(_)
| Expr::BooleanLiteral(_)
| Expr::NoneLiteral(_)
| Expr::EllipsisLiteral(_)
| Expr::FString(_)
| Expr::List(_)
| Expr::Tuple(_)
@ -83,7 +87,12 @@ where
}
if !matches!(
right.as_ref(),
Expr::Constant(_)
Expr::StringLiteral(_)
| Expr::BytesLiteral(_)
| Expr::NumberLiteral(_)
| Expr::BooleanLiteral(_)
| Expr::NoneLiteral(_)
| Expr::EllipsisLiteral(_)
| Expr::FString(_)
| Expr::List(_)
| Expr::Tuple(_)
@ -245,8 +254,14 @@ pub fn any_over_expr(expr: &Expr, func: &dyn Fn(&Expr) -> bool) -> bool {
.as_ref()
.is_some_and(|value| any_over_expr(value, func))
}
Expr::Name(_) | Expr::Constant(_) => false,
Expr::IpyEscapeCommand(_) => false,
Expr::Name(_)
| Expr::StringLiteral(_)
| Expr::BytesLiteral(_)
| Expr::NumberLiteral(_)
| Expr::BooleanLiteral(_)
| Expr::NoneLiteral(_)
| Expr::EllipsisLiteral(_)
| Expr::IpyEscapeCommand(_) => false,
}
}
@ -557,17 +572,19 @@ pub fn is_assignment_to_a_dunder(stmt: &Stmt) -> bool {
pub const fn is_singleton(expr: &Expr) -> bool {
matches!(
expr,
Expr::Constant(ast::ExprConstant {
value: Constant::None | Constant::Bool(_) | Constant::Ellipsis,
..
})
Expr::NoneLiteral(_) | Expr::BooleanLiteral(_) | Expr::EllipsisLiteral(_)
)
}
/// Return `true` if the [`Expr`] is a constant or tuple of constants.
pub fn is_constant(expr: &Expr) -> bool {
match expr {
Expr::Constant(_) => true,
Expr::StringLiteral(_)
| Expr::BytesLiteral(_)
| Expr::NumberLiteral(_)
| Expr::BooleanLiteral(_)
| Expr::NoneLiteral(_)
| Expr::EllipsisLiteral(_) => true,
Expr::Tuple(ast::ExprTuple { elts, .. }) => elts.iter().all(is_constant),
_ => false,
}
@ -580,23 +597,14 @@ pub fn is_constant_non_singleton(expr: &Expr) -> bool {
/// Return `true` if an [`Expr`] is `None`.
pub const fn is_const_none(expr: &Expr) -> bool {
matches!(
expr,
Expr::Constant(ast::ExprConstant {
value: Constant::None,
..
}),
)
expr.is_none_literal_expr()
}
/// Return `true` if an [`Expr`] is `True`.
pub const fn is_const_true(expr: &Expr) -> bool {
matches!(
expr,
Expr::Constant(ast::ExprConstant {
value: Constant::Bool(true),
..
}),
Expr::BooleanLiteral(ast::ExprBooleanLiteral { value: true, .. }),
)
}
@ -604,10 +612,7 @@ pub const fn is_const_true(expr: &Expr) -> bool {
pub const fn is_const_false(expr: &Expr) -> bool {
matches!(
expr,
Expr::Constant(ast::ExprConstant {
value: Constant::Bool(false),
..
}),
Expr::BooleanLiteral(ast::ExprBooleanLiteral { value: false, .. }),
)
}
@ -939,13 +944,7 @@ where
/// Return `true` if a `Stmt` is a docstring.
pub fn is_docstring_stmt(stmt: &Stmt) -> bool {
if let Stmt::Expr(ast::StmtExpr { value, range: _ }) = stmt {
matches!(
value.as_ref(),
Expr::Constant(ast::ExprConstant {
value: Constant::Str { .. },
..
})
)
value.is_string_literal_expr()
} else {
false
}
@ -1072,25 +1071,21 @@ impl Truthiness {
F: Fn(&str) -> bool,
{
match expr {
Expr::Constant(ast::ExprConstant { value, .. }) => match value {
Constant::Bool(value) => Some(*value),
Constant::None => Some(false),
Constant::Str(ast::StringConstant { value, .. }) => Some(!value.is_empty()),
Constant::Bytes(bytes) => Some(!bytes.is_empty()),
Constant::Int(int) => Some(*int != 0),
Constant::Float(float) => Some(*float != 0.0),
Constant::Complex { real, imag } => Some(*real != 0.0 || *imag != 0.0),
Constant::Ellipsis => Some(true),
Expr::StringLiteral(ast::ExprStringLiteral { value, .. }) => Some(!value.is_empty()),
Expr::BytesLiteral(ast::ExprBytesLiteral { value, .. }) => Some(!value.is_empty()),
Expr::NumberLiteral(ast::ExprNumberLiteral { value, .. }) => match value {
ast::Number::Int(int) => Some(*int != 0),
ast::Number::Float(float) => Some(*float != 0.0),
ast::Number::Complex { real, imag, .. } => Some(*real != 0.0 || *imag != 0.0),
},
Expr::BooleanLiteral(ast::ExprBooleanLiteral { value, .. }) => Some(*value),
Expr::NoneLiteral(_) => Some(false),
Expr::EllipsisLiteral(_) => Some(true),
Expr::FString(ast::ExprFString { values, .. }) => {
if values.is_empty() {
Some(false)
} else if values.iter().any(|value| {
if let Expr::Constant(ast::ExprConstant {
value: Constant::Str(ast::StringConstant { value, .. }),
..
}) = &value
{
if let Expr::StringLiteral(ast::ExprStringLiteral { value, .. }) = &value {
!value.is_empty()
} else {
false
@ -1196,8 +1191,9 @@ mod tests {
use crate::helpers::{any_over_stmt, any_over_type_param, resolve_imported_module_path};
use crate::{
Constant, Expr, ExprConstant, ExprContext, ExprName, Identifier, Int, Stmt, StmtTypeAlias,
TypeParam, TypeParamParamSpec, TypeParamTypeVar, TypeParamTypeVarTuple, TypeParams,
Expr, ExprContext, ExprName, ExprNumberLiteral, Identifier, Int, Number, Stmt,
StmtTypeAlias, TypeParam, TypeParamParamSpec, TypeParamTypeVar, TypeParamTypeVarTuple,
TypeParams,
};
#[test]
@ -1245,16 +1241,16 @@ mod tests {
range: TextRange::default(),
ctx: ExprContext::Load,
});
let constant_one = Expr::Constant(ExprConstant {
value: Constant::Int(1.into()),
let constant_one = Expr::NumberLiteral(ExprNumberLiteral {
value: Number::Int(1.into()),
range: TextRange::default(),
});
let constant_two = Expr::Constant(ExprConstant {
value: Constant::Int(2.into()),
let constant_two = Expr::NumberLiteral(ExprNumberLiteral {
value: Number::Int(2.into()),
range: TextRange::default(),
});
let constant_three = Expr::Constant(ExprConstant {
value: Constant::Int(3.into()),
let constant_three = Expr::NumberLiteral(ExprNumberLiteral {
value: Number::Int(3.into()),
range: TextRange::default(),
});
let type_var_one = TypeParam::TypeVar(TypeParamTypeVar {
@ -1295,8 +1291,8 @@ mod tests {
});
assert!(!any_over_type_param(&type_var_no_bound, &|_expr| true));
let bound = Expr::Constant(ExprConstant {
value: Constant::Int(Int::ONE),
let bound = Expr::NumberLiteral(ExprNumberLiteral {
value: Number::Int(Int::ONE),
range: TextRange::default(),
});

View file

@ -73,7 +73,12 @@ pub enum AnyNode {
ExprCall(ast::ExprCall),
ExprFormattedValue(ast::ExprFormattedValue),
ExprFString(ast::ExprFString),
ExprConstant(ast::ExprConstant),
ExprStringLiteral(ast::ExprStringLiteral),
ExprBytesLiteral(ast::ExprBytesLiteral),
ExprNumberLiteral(ast::ExprNumberLiteral),
ExprBooleanLiteral(ast::ExprBooleanLiteral),
ExprNoneLiteral(ast::ExprNoneLiteral),
ExprEllipsisLiteral(ast::ExprEllipsisLiteral),
ExprAttribute(ast::ExprAttribute),
ExprSubscript(ast::ExprSubscript),
ExprStarred(ast::ExprStarred),
@ -160,7 +165,12 @@ impl AnyNode {
| AnyNode::ExprCall(_)
| AnyNode::ExprFormattedValue(_)
| AnyNode::ExprFString(_)
| AnyNode::ExprConstant(_)
| AnyNode::ExprStringLiteral(_)
| AnyNode::ExprBytesLiteral(_)
| AnyNode::ExprNumberLiteral(_)
| AnyNode::ExprBooleanLiteral(_)
| AnyNode::ExprNoneLiteral(_)
| AnyNode::ExprEllipsisLiteral(_)
| AnyNode::ExprAttribute(_)
| AnyNode::ExprSubscript(_)
| AnyNode::ExprStarred(_)
@ -219,7 +229,12 @@ impl AnyNode {
AnyNode::ExprCall(node) => Some(Expr::Call(node)),
AnyNode::ExprFormattedValue(node) => Some(Expr::FormattedValue(node)),
AnyNode::ExprFString(node) => Some(Expr::FString(node)),
AnyNode::ExprConstant(node) => Some(Expr::Constant(node)),
AnyNode::ExprStringLiteral(node) => Some(Expr::StringLiteral(node)),
AnyNode::ExprBytesLiteral(node) => Some(Expr::BytesLiteral(node)),
AnyNode::ExprNumberLiteral(node) => Some(Expr::NumberLiteral(node)),
AnyNode::ExprBooleanLiteral(node) => Some(Expr::BooleanLiteral(node)),
AnyNode::ExprNoneLiteral(node) => Some(Expr::NoneLiteral(node)),
AnyNode::ExprEllipsisLiteral(node) => Some(Expr::EllipsisLiteral(node)),
AnyNode::ExprAttribute(node) => Some(Expr::Attribute(node)),
AnyNode::ExprSubscript(node) => Some(Expr::Subscript(node)),
AnyNode::ExprStarred(node) => Some(Expr::Starred(node)),
@ -334,7 +349,12 @@ impl AnyNode {
| AnyNode::ExprCall(_)
| AnyNode::ExprFormattedValue(_)
| AnyNode::ExprFString(_)
| AnyNode::ExprConstant(_)
| AnyNode::ExprStringLiteral(_)
| AnyNode::ExprBytesLiteral(_)
| AnyNode::ExprNumberLiteral(_)
| AnyNode::ExprBooleanLiteral(_)
| AnyNode::ExprNoneLiteral(_)
| AnyNode::ExprEllipsisLiteral(_)
| AnyNode::ExprAttribute(_)
| AnyNode::ExprSubscript(_)
| AnyNode::ExprStarred(_)
@ -429,7 +449,12 @@ impl AnyNode {
| AnyNode::ExprCall(_)
| AnyNode::ExprFormattedValue(_)
| AnyNode::ExprFString(_)
| AnyNode::ExprConstant(_)
| AnyNode::ExprStringLiteral(_)
| AnyNode::ExprBytesLiteral(_)
| AnyNode::ExprNumberLiteral(_)
| AnyNode::ExprBooleanLiteral(_)
| AnyNode::ExprNoneLiteral(_)
| AnyNode::ExprEllipsisLiteral(_)
| AnyNode::ExprAttribute(_)
| AnyNode::ExprSubscript(_)
| AnyNode::ExprStarred(_)
@ -509,7 +534,12 @@ impl AnyNode {
| AnyNode::ExprCall(_)
| AnyNode::ExprFormattedValue(_)
| AnyNode::ExprFString(_)
| AnyNode::ExprConstant(_)
| AnyNode::ExprStringLiteral(_)
| AnyNode::ExprBytesLiteral(_)
| AnyNode::ExprNumberLiteral(_)
| AnyNode::ExprBooleanLiteral(_)
| AnyNode::ExprNoneLiteral(_)
| AnyNode::ExprEllipsisLiteral(_)
| AnyNode::ExprAttribute(_)
| AnyNode::ExprSubscript(_)
| AnyNode::ExprStarred(_)
@ -614,7 +644,12 @@ impl AnyNode {
Self::ExprCall(node) => AnyNodeRef::ExprCall(node),
Self::ExprFormattedValue(node) => AnyNodeRef::ExprFormattedValue(node),
Self::ExprFString(node) => AnyNodeRef::ExprFString(node),
Self::ExprConstant(node) => AnyNodeRef::ExprConstant(node),
Self::ExprStringLiteral(node) => AnyNodeRef::ExprStringLiteral(node),
Self::ExprBytesLiteral(node) => AnyNodeRef::ExprBytesLiteral(node),
Self::ExprNumberLiteral(node) => AnyNodeRef::ExprNumberLiteral(node),
Self::ExprBooleanLiteral(node) => AnyNodeRef::ExprBooleanLiteral(node),
Self::ExprNoneLiteral(node) => AnyNodeRef::ExprNoneLiteral(node),
Self::ExprEllipsisLiteral(node) => AnyNodeRef::ExprEllipsisLiteral(node),
Self::ExprAttribute(node) => AnyNodeRef::ExprAttribute(node),
Self::ExprSubscript(node) => AnyNodeRef::ExprSubscript(node),
Self::ExprStarred(node) => AnyNodeRef::ExprStarred(node),
@ -2650,12 +2685,12 @@ impl AstNode for ast::ExprFString {
}
}
}
impl AstNode for ast::ExprConstant {
impl AstNode for ast::ExprStringLiteral {
fn cast(kind: AnyNode) -> Option<Self>
where
Self: Sized,
{
if let AnyNode::ExprConstant(node) = kind {
if let AnyNode::ExprStringLiteral(node) = kind {
Some(node)
} else {
None
@ -2663,7 +2698,7 @@ impl AstNode for ast::ExprConstant {
}
fn cast_ref(kind: AnyNodeRef) -> Option<&Self> {
if let AnyNodeRef::ExprConstant(node) = kind {
if let AnyNodeRef::ExprStringLiteral(node) = kind {
Some(node)
} else {
None
@ -2678,12 +2713,180 @@ impl AstNode for ast::ExprConstant {
AnyNode::from(self)
}
fn visit_preorder<'a, V>(&'a self, visitor: &mut V)
fn visit_preorder<'a, V>(&'a self, _visitor: &mut V)
where
V: PreorderVisitor<'a> + ?Sized,
{
}
}
impl AstNode for ast::ExprBytesLiteral {
fn cast(kind: AnyNode) -> Option<Self>
where
Self: Sized,
{
if let AnyNode::ExprBytesLiteral(node) = kind {
Some(node)
} else {
None
}
}
fn cast_ref(kind: AnyNodeRef) -> Option<&Self> {
if let AnyNodeRef::ExprBytesLiteral(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::ExprNumberLiteral {
fn cast(kind: AnyNode) -> Option<Self>
where
Self: Sized,
{
if let AnyNode::ExprNumberLiteral(node) = kind {
Some(node)
} else {
None
}
}
fn cast_ref(kind: AnyNodeRef) -> Option<&Self> {
if let AnyNodeRef::ExprNumberLiteral(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::ExprBooleanLiteral {
fn cast(kind: AnyNode) -> Option<Self>
where
Self: Sized,
{
if let AnyNode::ExprBooleanLiteral(node) = kind {
Some(node)
} else {
None
}
}
fn cast_ref(kind: AnyNodeRef) -> Option<&Self> {
if let AnyNodeRef::ExprBooleanLiteral(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::ExprNoneLiteral {
fn cast(kind: AnyNode) -> Option<Self>
where
Self: Sized,
{
if let AnyNode::ExprNoneLiteral(node) = kind {
Some(node)
} else {
None
}
}
fn cast_ref(kind: AnyNodeRef) -> Option<&Self> {
if let AnyNodeRef::ExprNoneLiteral(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::ExprEllipsisLiteral {
fn cast(kind: AnyNode) -> Option<Self>
where
Self: Sized,
{
if let AnyNode::ExprEllipsisLiteral(node) = kind {
Some(node)
} else {
None
}
}
fn cast_ref(kind: AnyNodeRef) -> Option<&Self> {
if let AnyNodeRef::ExprEllipsisLiteral(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,
{
let ast::ExprConstant { value, range: _ } = self;
visitor.visit_constant(value);
}
}
impl AstNode for ast::ExprAttribute {
@ -4124,7 +4327,12 @@ impl From<Expr> for AnyNode {
Expr::Call(node) => AnyNode::ExprCall(node),
Expr::FormattedValue(node) => AnyNode::ExprFormattedValue(node),
Expr::FString(node) => AnyNode::ExprFString(node),
Expr::Constant(node) => AnyNode::ExprConstant(node),
Expr::StringLiteral(node) => AnyNode::ExprStringLiteral(node),
Expr::BytesLiteral(node) => AnyNode::ExprBytesLiteral(node),
Expr::NumberLiteral(node) => AnyNode::ExprNumberLiteral(node),
Expr::BooleanLiteral(node) => AnyNode::ExprBooleanLiteral(node),
Expr::NoneLiteral(node) => AnyNode::ExprNoneLiteral(node),
Expr::EllipsisLiteral(node) => AnyNode::ExprEllipsisLiteral(node),
Expr::Attribute(node) => AnyNode::ExprAttribute(node),
Expr::Subscript(node) => AnyNode::ExprSubscript(node),
Expr::Starred(node) => AnyNode::ExprStarred(node),
@ -4451,9 +4659,39 @@ impl From<ast::ExprFString> for AnyNode {
}
}
impl From<ast::ExprConstant> for AnyNode {
fn from(node: ast::ExprConstant) -> Self {
AnyNode::ExprConstant(node)
impl From<ast::ExprStringLiteral> for AnyNode {
fn from(node: ast::ExprStringLiteral) -> Self {
AnyNode::ExprStringLiteral(node)
}
}
impl From<ast::ExprBytesLiteral> for AnyNode {
fn from(node: ast::ExprBytesLiteral) -> Self {
AnyNode::ExprBytesLiteral(node)
}
}
impl From<ast::ExprNumberLiteral> for AnyNode {
fn from(node: ast::ExprNumberLiteral) -> Self {
AnyNode::ExprNumberLiteral(node)
}
}
impl From<ast::ExprBooleanLiteral> for AnyNode {
fn from(node: ast::ExprBooleanLiteral) -> Self {
AnyNode::ExprBooleanLiteral(node)
}
}
impl From<ast::ExprNoneLiteral> for AnyNode {
fn from(node: ast::ExprNoneLiteral) -> Self {
AnyNode::ExprNoneLiteral(node)
}
}
impl From<ast::ExprEllipsisLiteral> for AnyNode {
fn from(node: ast::ExprEllipsisLiteral) -> Self {
AnyNode::ExprEllipsisLiteral(node)
}
}
@ -4693,7 +4931,12 @@ impl Ranged for AnyNode {
AnyNode::ExprCall(node) => node.range(),
AnyNode::ExprFormattedValue(node) => node.range(),
AnyNode::ExprFString(node) => node.range(),
AnyNode::ExprConstant(node) => node.range(),
AnyNode::ExprStringLiteral(node) => node.range(),
AnyNode::ExprBytesLiteral(node) => node.range(),
AnyNode::ExprNumberLiteral(node) => node.range(),
AnyNode::ExprBooleanLiteral(node) => node.range(),
AnyNode::ExprNoneLiteral(node) => node.range(),
AnyNode::ExprEllipsisLiteral(node) => node.range(),
AnyNode::ExprAttribute(node) => node.range(),
AnyNode::ExprSubscript(node) => node.range(),
AnyNode::ExprStarred(node) => node.range(),
@ -4780,7 +5023,12 @@ pub enum AnyNodeRef<'a> {
ExprCall(&'a ast::ExprCall),
ExprFormattedValue(&'a ast::ExprFormattedValue),
ExprFString(&'a ast::ExprFString),
ExprConstant(&'a ast::ExprConstant),
ExprStringLiteral(&'a ast::ExprStringLiteral),
ExprBytesLiteral(&'a ast::ExprBytesLiteral),
ExprNumberLiteral(&'a ast::ExprNumberLiteral),
ExprBooleanLiteral(&'a ast::ExprBooleanLiteral),
ExprNoneLiteral(&'a ast::ExprNoneLiteral),
ExprEllipsisLiteral(&'a ast::ExprEllipsisLiteral),
ExprAttribute(&'a ast::ExprAttribute),
ExprSubscript(&'a ast::ExprSubscript),
ExprStarred(&'a ast::ExprStarred),
@ -4866,7 +5114,12 @@ impl<'a> AnyNodeRef<'a> {
AnyNodeRef::ExprCall(node) => NonNull::from(*node).cast(),
AnyNodeRef::ExprFormattedValue(node) => NonNull::from(*node).cast(),
AnyNodeRef::ExprFString(node) => NonNull::from(*node).cast(),
AnyNodeRef::ExprConstant(node) => NonNull::from(*node).cast(),
AnyNodeRef::ExprStringLiteral(node) => NonNull::from(*node).cast(),
AnyNodeRef::ExprBytesLiteral(node) => NonNull::from(*node).cast(),
AnyNodeRef::ExprNumberLiteral(node) => NonNull::from(*node).cast(),
AnyNodeRef::ExprBooleanLiteral(node) => NonNull::from(*node).cast(),
AnyNodeRef::ExprNoneLiteral(node) => NonNull::from(*node).cast(),
AnyNodeRef::ExprEllipsisLiteral(node) => NonNull::from(*node).cast(),
AnyNodeRef::ExprAttribute(node) => NonNull::from(*node).cast(),
AnyNodeRef::ExprSubscript(node) => NonNull::from(*node).cast(),
AnyNodeRef::ExprStarred(node) => NonNull::from(*node).cast(),
@ -4958,7 +5211,12 @@ impl<'a> AnyNodeRef<'a> {
AnyNodeRef::ExprCall(_) => NodeKind::ExprCall,
AnyNodeRef::ExprFormattedValue(_) => NodeKind::ExprFormattedValue,
AnyNodeRef::ExprFString(_) => NodeKind::ExprFString,
AnyNodeRef::ExprConstant(_) => NodeKind::ExprConstant,
AnyNodeRef::ExprStringLiteral(_) => NodeKind::ExprStringLiteral,
AnyNodeRef::ExprBytesLiteral(_) => NodeKind::ExprBytesLiteral,
AnyNodeRef::ExprNumberLiteral(_) => NodeKind::ExprNumberLiteral,
AnyNodeRef::ExprBooleanLiteral(_) => NodeKind::ExprBooleanLiteral,
AnyNodeRef::ExprNoneLiteral(_) => NodeKind::ExprNoneLiteral,
AnyNodeRef::ExprEllipsisLiteral(_) => NodeKind::ExprEllipsisLiteral,
AnyNodeRef::ExprAttribute(_) => NodeKind::ExprAttribute,
AnyNodeRef::ExprSubscript(_) => NodeKind::ExprSubscript,
AnyNodeRef::ExprStarred(_) => NodeKind::ExprStarred,
@ -5045,7 +5303,12 @@ impl<'a> AnyNodeRef<'a> {
| AnyNodeRef::ExprCall(_)
| AnyNodeRef::ExprFormattedValue(_)
| AnyNodeRef::ExprFString(_)
| AnyNodeRef::ExprConstant(_)
| AnyNodeRef::ExprStringLiteral(_)
| AnyNodeRef::ExprBytesLiteral(_)
| AnyNodeRef::ExprNumberLiteral(_)
| AnyNodeRef::ExprBooleanLiteral(_)
| AnyNodeRef::ExprNoneLiteral(_)
| AnyNodeRef::ExprEllipsisLiteral(_)
| AnyNodeRef::ExprAttribute(_)
| AnyNodeRef::ExprSubscript(_)
| AnyNodeRef::ExprStarred(_)
@ -5104,7 +5367,12 @@ impl<'a> AnyNodeRef<'a> {
| AnyNodeRef::ExprCall(_)
| AnyNodeRef::ExprFormattedValue(_)
| AnyNodeRef::ExprFString(_)
| AnyNodeRef::ExprConstant(_)
| AnyNodeRef::ExprStringLiteral(_)
| AnyNodeRef::ExprBytesLiteral(_)
| AnyNodeRef::ExprNumberLiteral(_)
| AnyNodeRef::ExprBooleanLiteral(_)
| AnyNodeRef::ExprNoneLiteral(_)
| AnyNodeRef::ExprEllipsisLiteral(_)
| AnyNodeRef::ExprAttribute(_)
| AnyNodeRef::ExprSubscript(_)
| AnyNodeRef::ExprStarred(_)
@ -5218,7 +5486,12 @@ impl<'a> AnyNodeRef<'a> {
| AnyNodeRef::ExprCall(_)
| AnyNodeRef::ExprFormattedValue(_)
| AnyNodeRef::ExprFString(_)
| AnyNodeRef::ExprConstant(_)
| AnyNodeRef::ExprStringLiteral(_)
| AnyNodeRef::ExprBytesLiteral(_)
| AnyNodeRef::ExprNumberLiteral(_)
| AnyNodeRef::ExprBooleanLiteral(_)
| AnyNodeRef::ExprNoneLiteral(_)
| AnyNodeRef::ExprEllipsisLiteral(_)
| AnyNodeRef::ExprAttribute(_)
| AnyNodeRef::ExprSubscript(_)
| AnyNodeRef::ExprStarred(_)
@ -5313,7 +5586,12 @@ impl<'a> AnyNodeRef<'a> {
| AnyNodeRef::ExprCall(_)
| AnyNodeRef::ExprFormattedValue(_)
| AnyNodeRef::ExprFString(_)
| AnyNodeRef::ExprConstant(_)
| AnyNodeRef::ExprStringLiteral(_)
| AnyNodeRef::ExprBytesLiteral(_)
| AnyNodeRef::ExprNumberLiteral(_)
| AnyNodeRef::ExprBooleanLiteral(_)
| AnyNodeRef::ExprNoneLiteral(_)
| AnyNodeRef::ExprEllipsisLiteral(_)
| AnyNodeRef::ExprAttribute(_)
| AnyNodeRef::ExprSubscript(_)
| AnyNodeRef::ExprStarred(_)
@ -5393,7 +5671,12 @@ impl<'a> AnyNodeRef<'a> {
| AnyNodeRef::ExprCall(_)
| AnyNodeRef::ExprFormattedValue(_)
| AnyNodeRef::ExprFString(_)
| AnyNodeRef::ExprConstant(_)
| AnyNodeRef::ExprStringLiteral(_)
| AnyNodeRef::ExprBytesLiteral(_)
| AnyNodeRef::ExprNumberLiteral(_)
| AnyNodeRef::ExprBooleanLiteral(_)
| AnyNodeRef::ExprNoneLiteral(_)
| AnyNodeRef::ExprEllipsisLiteral(_)
| AnyNodeRef::ExprAttribute(_)
| AnyNodeRef::ExprSubscript(_)
| AnyNodeRef::ExprStarred(_)
@ -5507,7 +5790,12 @@ impl<'a> AnyNodeRef<'a> {
AnyNodeRef::ExprCall(node) => node.visit_preorder(visitor),
AnyNodeRef::ExprFormattedValue(node) => node.visit_preorder(visitor),
AnyNodeRef::ExprFString(node) => node.visit_preorder(visitor),
AnyNodeRef::ExprConstant(node) => node.visit_preorder(visitor),
AnyNodeRef::ExprStringLiteral(node) => node.visit_preorder(visitor),
AnyNodeRef::ExprBytesLiteral(node) => node.visit_preorder(visitor),
AnyNodeRef::ExprNumberLiteral(node) => node.visit_preorder(visitor),
AnyNodeRef::ExprBooleanLiteral(node) => node.visit_preorder(visitor),
AnyNodeRef::ExprNoneLiteral(node) => node.visit_preorder(visitor),
AnyNodeRef::ExprEllipsisLiteral(node) => node.visit_preorder(visitor),
AnyNodeRef::ExprAttribute(node) => node.visit_preorder(visitor),
AnyNodeRef::ExprSubscript(node) => node.visit_preorder(visitor),
AnyNodeRef::ExprStarred(node) => node.visit_preorder(visitor),
@ -5888,9 +6176,39 @@ impl<'a> From<&'a ast::ExprFString> for AnyNodeRef<'a> {
}
}
impl<'a> From<&'a ast::ExprConstant> for AnyNodeRef<'a> {
fn from(node: &'a ast::ExprConstant) -> Self {
AnyNodeRef::ExprConstant(node)
impl<'a> From<&'a ast::ExprStringLiteral> for AnyNodeRef<'a> {
fn from(node: &'a ast::ExprStringLiteral) -> Self {
AnyNodeRef::ExprStringLiteral(node)
}
}
impl<'a> From<&'a ast::ExprBytesLiteral> for AnyNodeRef<'a> {
fn from(node: &'a ast::ExprBytesLiteral) -> Self {
AnyNodeRef::ExprBytesLiteral(node)
}
}
impl<'a> From<&'a ast::ExprNumberLiteral> for AnyNodeRef<'a> {
fn from(node: &'a ast::ExprNumberLiteral) -> Self {
AnyNodeRef::ExprNumberLiteral(node)
}
}
impl<'a> From<&'a ast::ExprBooleanLiteral> for AnyNodeRef<'a> {
fn from(node: &'a ast::ExprBooleanLiteral) -> Self {
AnyNodeRef::ExprBooleanLiteral(node)
}
}
impl<'a> From<&'a ast::ExprNoneLiteral> for AnyNodeRef<'a> {
fn from(node: &'a ast::ExprNoneLiteral) -> Self {
AnyNodeRef::ExprNoneLiteral(node)
}
}
impl<'a> From<&'a ast::ExprEllipsisLiteral> for AnyNodeRef<'a> {
fn from(node: &'a ast::ExprEllipsisLiteral) -> Self {
AnyNodeRef::ExprEllipsisLiteral(node)
}
}
@ -6091,7 +6409,12 @@ impl<'a> From<&'a Expr> for AnyNodeRef<'a> {
Expr::Call(node) => AnyNodeRef::ExprCall(node),
Expr::FormattedValue(node) => AnyNodeRef::ExprFormattedValue(node),
Expr::FString(node) => AnyNodeRef::ExprFString(node),
Expr::Constant(node) => AnyNodeRef::ExprConstant(node),
Expr::StringLiteral(node) => AnyNodeRef::ExprStringLiteral(node),
Expr::BytesLiteral(node) => AnyNodeRef::ExprBytesLiteral(node),
Expr::NumberLiteral(node) => AnyNodeRef::ExprNumberLiteral(node),
Expr::BooleanLiteral(node) => AnyNodeRef::ExprBooleanLiteral(node),
Expr::NoneLiteral(node) => AnyNodeRef::ExprNoneLiteral(node),
Expr::EllipsisLiteral(node) => AnyNodeRef::ExprEllipsisLiteral(node),
Expr::Attribute(node) => AnyNodeRef::ExprAttribute(node),
Expr::Subscript(node) => AnyNodeRef::ExprSubscript(node),
Expr::Starred(node) => AnyNodeRef::ExprStarred(node),
@ -6243,7 +6566,12 @@ impl Ranged for AnyNodeRef<'_> {
AnyNodeRef::ExprCall(node) => node.range(),
AnyNodeRef::ExprFormattedValue(node) => node.range(),
AnyNodeRef::ExprFString(node) => node.range(),
AnyNodeRef::ExprConstant(node) => node.range(),
AnyNodeRef::ExprStringLiteral(node) => node.range(),
AnyNodeRef::ExprBytesLiteral(node) => node.range(),
AnyNodeRef::ExprNumberLiteral(node) => node.range(),
AnyNodeRef::ExprBooleanLiteral(node) => node.range(),
AnyNodeRef::ExprNoneLiteral(node) => node.range(),
AnyNodeRef::ExprEllipsisLiteral(node) => node.range(),
AnyNodeRef::ExprAttribute(node) => node.range(),
AnyNodeRef::ExprSubscript(node) => node.range(),
AnyNodeRef::ExprStarred(node) => node.range(),
@ -6332,7 +6660,12 @@ pub enum NodeKind {
ExprCall,
ExprFormattedValue,
ExprFString,
ExprConstant,
ExprStringLiteral,
ExprBytesLiteral,
ExprNumberLiteral,
ExprBooleanLiteral,
ExprNoneLiteral,
ExprEllipsisLiteral,
ExprAttribute,
ExprSubscript,
ExprStarred,

View file

@ -591,8 +591,18 @@ pub enum Expr {
FormattedValue(ExprFormattedValue),
#[is(name = "f_string_expr")]
FString(ExprFString),
#[is(name = "constant_expr")]
Constant(ExprConstant),
#[is(name = "string_literal_expr")]
StringLiteral(ExprStringLiteral),
#[is(name = "bytes_literal_expr")]
BytesLiteral(ExprBytesLiteral),
#[is(name = "number_literal_expr")]
NumberLiteral(ExprNumberLiteral),
#[is(name = "boolean_literal_expr")]
BooleanLiteral(ExprBooleanLiteral),
#[is(name = "none_literal_expr")]
NoneLiteral(ExprNoneLiteral),
#[is(name = "ellipsis_literal_expr")]
EllipsisLiteral(ExprEllipsisLiteral),
#[is(name = "attribute_expr")]
Attribute(ExprAttribute),
#[is(name = "subscript_expr")]
@ -613,6 +623,42 @@ pub enum Expr {
IpyEscapeCommand(ExprIpyEscapeCommand),
}
impl Expr {
/// Returns `true` if the expression is a literal expression.
///
/// A literal expression is either a string literal, bytes literal,
/// integer, float, complex number, boolean, `None`, or ellipsis (`...`).
pub fn is_literal_expr(&self) -> bool {
matches!(
self,
Expr::StringLiteral(_)
| Expr::BytesLiteral(_)
| Expr::NumberLiteral(_)
| Expr::BooleanLiteral(_)
| Expr::NoneLiteral(_)
| Expr::EllipsisLiteral(_)
)
}
pub fn is_implicit_concatenated_string(&self) -> bool {
match self {
Expr::StringLiteral(ExprStringLiteral {
implicit_concatenated,
..
})
| Expr::BytesLiteral(ExprBytesLiteral {
implicit_concatenated,
..
})
| Expr::FString(ExprFString {
implicit_concatenated,
..
}) => *implicit_concatenated,
_ => false,
}
}
}
/// An AST node used to represent a IPython escape command at the expression level.
///
/// For example,
@ -941,16 +987,127 @@ impl From<ExprFString> for Expr {
}
}
/// See also [Constant](https://docs.python.org/3/library/ast.html#ast.Constant)
#[derive(Clone, Debug, PartialEq)]
pub struct ExprConstant {
pub struct ExprStringLiteral {
pub range: TextRange,
pub value: Constant,
pub value: String,
pub unicode: bool,
pub implicit_concatenated: bool,
}
impl From<ExprConstant> for Expr {
fn from(payload: ExprConstant) -> Self {
Expr::Constant(payload)
impl From<ExprStringLiteral> for Expr {
fn from(payload: ExprStringLiteral) -> Self {
Expr::StringLiteral(payload)
}
}
impl Ranged for ExprStringLiteral {
fn range(&self) -> TextRange {
self.range
}
}
impl Deref for ExprStringLiteral {
type Target = str;
fn deref(&self) -> &Self::Target {
self.value.as_str()
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct ExprBytesLiteral {
pub range: TextRange,
pub value: Vec<u8>,
pub implicit_concatenated: bool,
}
impl From<ExprBytesLiteral> for Expr {
fn from(payload: ExprBytesLiteral) -> Self {
Expr::BytesLiteral(payload)
}
}
impl Ranged for ExprBytesLiteral {
fn range(&self) -> TextRange {
self.range
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct ExprNumberLiteral {
pub range: TextRange,
pub value: Number,
}
impl From<ExprNumberLiteral> for Expr {
fn from(payload: ExprNumberLiteral) -> Self {
Expr::NumberLiteral(payload)
}
}
impl Ranged for ExprNumberLiteral {
fn range(&self) -> TextRange {
self.range
}
}
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
pub enum Number {
Int(int::Int),
Float(f64),
Complex { real: f64, imag: f64 },
}
#[derive(Clone, Debug, PartialEq)]
pub struct ExprBooleanLiteral {
pub range: TextRange,
pub value: bool,
}
impl From<ExprBooleanLiteral> for Expr {
fn from(payload: ExprBooleanLiteral) -> Self {
Expr::BooleanLiteral(payload)
}
}
impl Ranged for ExprBooleanLiteral {
fn range(&self) -> TextRange {
self.range
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct ExprNoneLiteral {
pub range: TextRange,
}
impl From<ExprNoneLiteral> for Expr {
fn from(payload: ExprNoneLiteral) -> Self {
Expr::NoneLiteral(payload)
}
}
impl Ranged for ExprNoneLiteral {
fn range(&self) -> TextRange {
self.range
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct ExprEllipsisLiteral {
pub range: TextRange,
}
impl From<ExprEllipsisLiteral> for Expr {
fn from(payload: ExprEllipsisLiteral) -> Self {
Expr::EllipsisLiteral(payload)
}
}
impl Ranged for ExprEllipsisLiteral {
fn range(&self) -> TextRange {
self.range
}
}
@ -2595,142 +2752,6 @@ impl From<bool> for Singleton {
}
}
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
pub enum Constant {
None,
Bool(bool),
Str(StringConstant),
Bytes(BytesConstant),
Int(int::Int),
Float(f64),
Complex { real: f64, imag: f64 },
Ellipsis,
}
impl Constant {
/// Returns `true` if the constant is a string or bytes constant that contains multiple,
/// implicitly concatenated string tokens.
pub fn is_implicit_concatenated(&self) -> bool {
match self {
Constant::Str(value) => value.implicit_concatenated,
Constant::Bytes(value) => value.implicit_concatenated,
_ => false,
}
}
/// Returns `true` if the constant is a string constant that is a unicode string (i.e., `u"..."`).
pub fn is_unicode_string(&self) -> bool {
match self {
Constant::Str(value) => value.unicode,
_ => false,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StringConstant {
/// The string value as resolved by the parser (i.e., without quotes, or escape sequences, or
/// implicit concatenations).
pub value: String,
/// Whether the string is a Unicode string (i.e., `u"..."`).
pub unicode: bool,
/// Whether the string contains multiple string tokens that were implicitly concatenated.
pub implicit_concatenated: bool,
}
impl Deref for StringConstant {
type Target = str;
fn deref(&self) -> &Self::Target {
self.value.as_str()
}
}
impl From<String> for StringConstant {
fn from(value: String) -> StringConstant {
Self {
value,
unicode: false,
implicit_concatenated: false,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BytesConstant {
/// The bytes value as resolved by the parser (i.e., without quotes, or escape sequences, or
/// implicit concatenations).
pub value: Vec<u8>,
/// Whether the string contains multiple string tokens that were implicitly concatenated.
pub implicit_concatenated: bool,
}
impl Deref for BytesConstant {
type Target = [u8];
fn deref(&self) -> &Self::Target {
self.value.as_slice()
}
}
impl From<Vec<u8>> for BytesConstant {
fn from(value: Vec<u8>) -> BytesConstant {
Self {
value,
implicit_concatenated: false,
}
}
}
impl From<Vec<u8>> for Constant {
fn from(value: Vec<u8>) -> Constant {
Self::Bytes(BytesConstant {
value,
implicit_concatenated: false,
})
}
}
impl From<String> for Constant {
fn from(value: String) -> Constant {
Self::Str(StringConstant {
value,
unicode: false,
implicit_concatenated: false,
})
}
}
impl From<bool> for Constant {
fn from(value: bool) -> Constant {
Self::Bool(value)
}
}
#[cfg(feature = "rustpython-literal")]
impl std::fmt::Display for Constant {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Constant::None => f.pad("None"),
Constant::Bool(b) => f.pad(if *b { "True" } else { "False" }),
Constant::Str(s) => rustpython_literal::escape::UnicodeEscape::new_repr(s.as_str())
.str_repr()
.write(f),
Constant::Bytes(b) => {
let escape = rustpython_literal::escape::AsciiEscape::new_repr(b);
let repr = escape.bytes_repr().to_string().unwrap();
f.pad(&repr)
}
Constant::Int(i) => std::fmt::Display::fmt(&i, f),
Constant::Float(fp) => f.pad(&rustpython_literal::float::to_string(*fp)),
Constant::Complex { real, imag } => {
if *real == 0.0 {
write!(f, "{imag}j")
} else {
write!(f, "({real}{imag:+}j)")
}
}
Constant::Ellipsis => f.pad("..."),
}
}
}
impl Ranged for crate::nodes::ModModule {
fn range(&self) -> TextRange {
self.range
@ -3007,11 +3028,6 @@ impl Ranged for crate::nodes::ExprFString {
self.range
}
}
impl Ranged for crate::nodes::ExprConstant {
fn range(&self) -> TextRange {
self.range
}
}
impl Ranged for crate::nodes::ExprAttribute {
fn range(&self) -> TextRange {
self.range
@ -3074,7 +3090,12 @@ impl Ranged for crate::Expr {
Self::Call(node) => node.range(),
Self::FormattedValue(node) => node.range(),
Self::FString(node) => node.range(),
Self::Constant(node) => node.range(),
Expr::StringLiteral(node) => node.range(),
Expr::BytesLiteral(node) => node.range(),
Expr::NumberLiteral(node) => node.range(),
Expr::BooleanLiteral(node) => node.range(),
Expr::NoneLiteral(node) => node.range(),
Expr::EllipsisLiteral(node) => node.range(),
Self::Attribute(node) => node.range(),
Self::Subscript(node) => node.range(),
Self::Starred(node) => node.range(),
@ -3375,9 +3396,34 @@ impl From<ExprFString> for ParenthesizedExpr {
Expr::FString(payload).into()
}
}
impl From<ExprConstant> for ParenthesizedExpr {
fn from(payload: ExprConstant) -> Self {
Expr::Constant(payload).into()
impl From<ExprStringLiteral> for ParenthesizedExpr {
fn from(payload: ExprStringLiteral) -> Self {
Expr::StringLiteral(payload).into()
}
}
impl From<ExprBytesLiteral> for ParenthesizedExpr {
fn from(payload: ExprBytesLiteral) -> Self {
Expr::BytesLiteral(payload).into()
}
}
impl From<ExprNumberLiteral> for ParenthesizedExpr {
fn from(payload: ExprNumberLiteral) -> Self {
Expr::NumberLiteral(payload).into()
}
}
impl From<ExprBooleanLiteral> for ParenthesizedExpr {
fn from(payload: ExprBooleanLiteral) -> Self {
Expr::BooleanLiteral(payload).into()
}
}
impl From<ExprNoneLiteral> for ParenthesizedExpr {
fn from(payload: ExprNoneLiteral) -> Self {
Expr::NoneLiteral(payload).into()
}
}
impl From<ExprEllipsisLiteral> for ParenthesizedExpr {
fn from(payload: ExprEllipsisLiteral) -> Self {
Expr::EllipsisLiteral(payload).into()
}
}
impl From<ExprAttribute> for ParenthesizedExpr {
@ -3428,7 +3474,6 @@ mod size_assertions {
assert_eq_size!(StmtClassDef, [u8; 104]);
assert_eq_size!(StmtTry, [u8; 112]);
assert_eq_size!(Expr, [u8; 80]);
assert_eq_size!(Constant, [u8; 40]);
assert_eq_size!(Pattern, [u8; 96]);
assert_eq_size!(Mod, [u8; 32]);
}

View file

@ -146,7 +146,22 @@ pub fn relocate_expr(expr: &mut Expr, location: TextRange) {
relocate_expr(expr, location);
}
}
Expr::Constant(nodes::ExprConstant { range, .. }) => {
Expr::StringLiteral(nodes::ExprStringLiteral { range, .. }) => {
*range = location;
}
Expr::BytesLiteral(nodes::ExprBytesLiteral { range, .. }) => {
*range = location;
}
Expr::NumberLiteral(nodes::ExprNumberLiteral { range, .. }) => {
*range = location;
}
Expr::BooleanLiteral(nodes::ExprBooleanLiteral { range, .. }) => {
*range = location;
}
Expr::NoneLiteral(nodes::ExprNoneLiteral { range }) => {
*range = location;
}
Expr::EllipsisLiteral(nodes::ExprEllipsisLiteral { range }) => {
*range = location;
}
Expr::Attribute(nodes::ExprAttribute { value, range, .. }) => {

View file

@ -477,7 +477,12 @@ pub fn walk_expr<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, expr: &'a Expr) {
visitor.visit_expr(expr);
}
}
Expr::Constant(_) => {}
Expr::StringLiteral(_)
| Expr::BytesLiteral(_)
| Expr::NumberLiteral(_)
| Expr::BooleanLiteral(_)
| Expr::NoneLiteral(_)
| Expr::EllipsisLiteral(_) => {}
Expr::Attribute(ast::ExprAttribute { value, ctx, .. }) => {
visitor.visit_expr(value);
visitor.visit_expr_context(ctx);

View file

@ -1,8 +1,7 @@
use crate::{
Alias, Arguments, BoolOp, CmpOp, Comprehension, Constant, Decorator, ElifElseClause,
ExceptHandler, Expr, Keyword, MatchCase, Mod, Operator, Parameter, ParameterWithDefault,
Parameters, Pattern, PatternArguments, PatternKeyword, Singleton, Stmt, TypeParam, TypeParams,
UnaryOp, WithItem,
Alias, Arguments, BoolOp, CmpOp, Comprehension, Decorator, ElifElseClause, ExceptHandler, Expr,
Keyword, MatchCase, Mod, Operator, Parameter, ParameterWithDefault, Parameters, Pattern,
PatternArguments, PatternKeyword, Singleton, Stmt, TypeParam, TypeParams, UnaryOp, WithItem,
};
use crate::{AnyNodeRef, AstNode};
@ -41,9 +40,6 @@ pub trait PreorderVisitor<'a> {
walk_decorator(self, decorator);
}
#[inline]
fn visit_constant(&mut self, _constant: &'a Constant) {}
#[inline]
fn visit_singleton(&mut self, _singleton: &'a Singleton) {}
@ -280,7 +276,12 @@ where
Expr::Call(expr) => expr.visit_preorder(visitor),
Expr::FormattedValue(expr) => expr.visit_preorder(visitor),
Expr::FString(expr) => expr.visit_preorder(visitor),
Expr::Constant(expr) => expr.visit_preorder(visitor),
Expr::StringLiteral(expr) => expr.visit_preorder(visitor),
Expr::BytesLiteral(expr) => expr.visit_preorder(visitor),
Expr::NumberLiteral(expr) => expr.visit_preorder(visitor),
Expr::BooleanLiteral(expr) => expr.visit_preorder(visitor),
Expr::NoneLiteral(expr) => expr.visit_preorder(visitor),
Expr::EllipsisLiteral(expr) => expr.visit_preorder(visitor),
Expr::Attribute(expr) => expr.visit_preorder(visitor),
Expr::Subscript(expr) => expr.visit_preorder(visitor),
Expr::Starred(expr) => expr.visit_preorder(visitor),