Auto generate ast expression nodes (#16285)

## Summary

Part of https://github.com/astral-sh/ruff/issues/15655

- Auto generate AST nodes using definitions in `ast.toml`. I added
attributes similar to
[`Field`](https://github.com/python/cpython/blob/main/Parser/asdl.py#L67)
in ASDL to hold field information

## Test Plan

Nothing outside the `ruff_python_ast` package should change.

---------

Co-authored-by: Douglas Creager <dcreager@dcreager.net>
This commit is contained in:
Shaygan Hooshyari 2025-03-05 14:25:55 +01:00 committed by GitHub
parent cc324abcc2
commit 23fd4927ae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 719 additions and 360 deletions

View file

@ -1,6 +1,8 @@
// This is a generated file. Don't modify it by hand!
// Run `crates/ruff_python_ast/generate.py` to re-generate the file.
use crate::name::Name;
/// See also [mod](https://docs.python.org/3/library/ast.html#ast.mod)
#[derive(Clone, Debug, PartialEq)]
pub enum Mod {
@ -6445,3 +6447,269 @@ impl AnyNodeRef<'_> {
}
}
}
/// See also [BoolOp](https://docs.python.org/3/library/ast.html#ast.BoolOp)
#[derive(Clone, Debug, PartialEq)]
pub struct ExprBoolOp {
pub range: ruff_text_size::TextRange,
pub op: crate::BoolOp,
pub values: Vec<Expr>,
}
/// See also [NamedExpr](https://docs.python.org/3/library/ast.html#ast.NamedExpr)
#[derive(Clone, Debug, PartialEq)]
pub struct ExprNamed {
pub range: ruff_text_size::TextRange,
pub target: Box<Expr>,
pub value: Box<Expr>,
}
/// See also [BinOp](https://docs.python.org/3/library/ast.html#ast.BinOp)
#[derive(Clone, Debug, PartialEq)]
pub struct ExprBinOp {
pub range: ruff_text_size::TextRange,
pub left: Box<Expr>,
pub op: crate::Operator,
pub right: Box<Expr>,
}
/// See also [UnaryOp](https://docs.python.org/3/library/ast.html#ast.UnaryOp)
#[derive(Clone, Debug, PartialEq)]
pub struct ExprUnaryOp {
pub range: ruff_text_size::TextRange,
pub op: crate::UnaryOp,
pub operand: Box<Expr>,
}
/// See also [Lambda](https://docs.python.org/3/library/ast.html#ast.Lambda)
#[derive(Clone, Debug, PartialEq)]
pub struct ExprLambda {
pub range: ruff_text_size::TextRange,
pub parameters: Option<Box<crate::Parameters>>,
pub body: Box<Expr>,
}
/// See also [IfExp](https://docs.python.org/3/library/ast.html#ast.IfExp)
#[derive(Clone, Debug, PartialEq)]
pub struct ExprIf {
pub range: ruff_text_size::TextRange,
pub test: Box<Expr>,
pub body: Box<Expr>,
pub orelse: Box<Expr>,
}
/// See also [Dict](https://docs.python.org/3/library/ast.html#ast.Dict)
#[derive(Clone, Debug, PartialEq)]
pub struct ExprDict {
pub range: ruff_text_size::TextRange,
pub items: Vec<crate::DictItem>,
}
/// See also [Set](https://docs.python.org/3/library/ast.html#ast.Set)
#[derive(Clone, Debug, PartialEq)]
pub struct ExprSet {
pub range: ruff_text_size::TextRange,
pub elts: Vec<Expr>,
}
/// See also [ListComp](https://docs.python.org/3/library/ast.html#ast.ListComp)
#[derive(Clone, Debug, PartialEq)]
pub struct ExprListComp {
pub range: ruff_text_size::TextRange,
pub elt: Box<Expr>,
pub generators: Vec<crate::Comprehension>,
}
/// See also [SetComp](https://docs.python.org/3/library/ast.html#ast.SetComp)
#[derive(Clone, Debug, PartialEq)]
pub struct ExprSetComp {
pub range: ruff_text_size::TextRange,
pub elt: Box<Expr>,
pub generators: Vec<crate::Comprehension>,
}
/// See also [DictComp](https://docs.python.org/3/library/ast.html#ast.DictComp)
#[derive(Clone, Debug, PartialEq)]
pub struct ExprDictComp {
pub range: ruff_text_size::TextRange,
pub key: Box<Expr>,
pub value: Box<Expr>,
pub generators: Vec<crate::Comprehension>,
}
/// See also [GeneratorExp](https://docs.python.org/3/library/ast.html#ast.GeneratorExp)
#[derive(Clone, Debug, PartialEq)]
pub struct ExprGenerator {
pub range: ruff_text_size::TextRange,
pub elt: Box<Expr>,
pub generators: Vec<crate::Comprehension>,
pub parenthesized: bool,
}
/// See also [Await](https://docs.python.org/3/library/ast.html#ast.Await)
#[derive(Clone, Debug, PartialEq)]
pub struct ExprAwait {
pub range: ruff_text_size::TextRange,
pub value: Box<Expr>,
}
/// See also [Yield](https://docs.python.org/3/library/ast.html#ast.Yield)
#[derive(Clone, Debug, PartialEq)]
pub struct ExprYield {
pub range: ruff_text_size::TextRange,
pub value: Option<Box<Expr>>,
}
/// See also [YieldFrom](https://docs.python.org/3/library/ast.html#ast.YieldFrom)
#[derive(Clone, Debug, PartialEq)]
pub struct ExprYieldFrom {
pub range: ruff_text_size::TextRange,
pub value: Box<Expr>,
}
/// See also [Compare](https://docs.python.org/3/library/ast.html#ast.Compare)
#[derive(Clone, Debug, PartialEq)]
pub struct ExprCompare {
pub range: ruff_text_size::TextRange,
pub left: Box<Expr>,
pub ops: Box<[crate::CmpOp]>,
pub comparators: Box<[Expr]>,
}
/// See also [Call](https://docs.python.org/3/library/ast.html#ast.Call)
#[derive(Clone, Debug, PartialEq)]
pub struct ExprCall {
pub range: ruff_text_size::TextRange,
pub func: Box<Expr>,
pub arguments: crate::Arguments,
}
/// An AST node that represents either a single-part f-string literal
/// or an implicitly concatenated f-string literal.
///
/// This type differs from the original Python AST `JoinedStr` in that it
/// doesn't join the implicitly concatenated parts into a single string. Instead,
/// it keeps them separate and provide various methods to access the parts.
///
/// See also [JoinedStr](https://docs.python.org/3/library/ast.html#ast.JoinedStr)
#[derive(Clone, Debug, PartialEq)]
pub struct ExprFString {
pub range: ruff_text_size::TextRange,
pub value: crate::FStringValue,
}
/// An AST node that represents either a single-part string literal
/// or an implicitly concatenated string literal.
#[derive(Clone, Debug, PartialEq)]
pub struct ExprStringLiteral {
pub range: ruff_text_size::TextRange,
pub value: crate::StringLiteralValue,
}
/// An AST node that represents either a single-part bytestring literal
/// or an implicitly concatenated bytestring literal.
#[derive(Clone, Debug, PartialEq)]
pub struct ExprBytesLiteral {
pub range: ruff_text_size::TextRange,
pub value: crate::BytesLiteralValue,
}
#[derive(Clone, Debug, PartialEq)]
pub struct ExprNumberLiteral {
pub range: ruff_text_size::TextRange,
pub value: crate::Number,
}
#[derive(Clone, Debug, PartialEq, Default)]
pub struct ExprBooleanLiteral {
pub range: ruff_text_size::TextRange,
pub value: bool,
}
#[derive(Clone, Debug, PartialEq, Default)]
pub struct ExprNoneLiteral {
pub range: ruff_text_size::TextRange,
}
#[derive(Clone, Debug, PartialEq, Default)]
pub struct ExprEllipsisLiteral {
pub range: ruff_text_size::TextRange,
}
/// See also [Attribute](https://docs.python.org/3/library/ast.html#ast.Attribute)
#[derive(Clone, Debug, PartialEq)]
pub struct ExprAttribute {
pub range: ruff_text_size::TextRange,
pub value: Box<Expr>,
pub attr: crate::Identifier,
pub ctx: crate::ExprContext,
}
/// See also [Subscript](https://docs.python.org/3/library/ast.html#ast.Subscript)
#[derive(Clone, Debug, PartialEq)]
pub struct ExprSubscript {
pub range: ruff_text_size::TextRange,
pub value: Box<Expr>,
pub slice: Box<Expr>,
pub ctx: crate::ExprContext,
}
/// See also [Starred](https://docs.python.org/3/library/ast.html#ast.Starred)
#[derive(Clone, Debug, PartialEq)]
pub struct ExprStarred {
pub range: ruff_text_size::TextRange,
pub value: Box<Expr>,
pub ctx: crate::ExprContext,
}
/// See also [Name](https://docs.python.org/3/library/ast.html#ast.Name)
#[derive(Clone, Debug, PartialEq)]
pub struct ExprName {
pub range: ruff_text_size::TextRange,
pub id: Name,
pub ctx: crate::ExprContext,
}
/// See also [List](https://docs.python.org/3/library/ast.html#ast.List)
#[derive(Clone, Debug, PartialEq)]
pub struct ExprList {
pub range: ruff_text_size::TextRange,
pub elts: Vec<Expr>,
pub ctx: crate::ExprContext,
}
/// See also [Tuple](https://docs.python.org/3/library/ast.html#ast.Tuple)
#[derive(Clone, Debug, PartialEq)]
pub struct ExprTuple {
pub range: ruff_text_size::TextRange,
pub elts: Vec<Expr>,
pub ctx: crate::ExprContext,
pub parenthesized: bool,
}
/// See also [Slice](https://docs.python.org/3/library/ast.html#ast.Slice)
#[derive(Clone, Debug, PartialEq)]
pub struct ExprSlice {
pub range: ruff_text_size::TextRange,
pub lower: Option<Box<Expr>>,
pub upper: Option<Box<Expr>>,
pub step: Option<Box<Expr>>,
}
/// An AST node used to represent a IPython escape command at the expression level.
///
/// For example,
/// ```python
/// dir = !pwd
/// ```
///
/// Here, the escape kind can only be `!` or `%` otherwise it is a syntax error.
///
/// For more information related to terminology and syntax of escape commands,
/// see [`crate::StmtIpyEscapeCommand`].
#[derive(Clone, Debug, PartialEq)]
pub struct ExprIpyEscapeCommand {
pub range: ruff_text_size::TextRange,
pub kind: crate::IpyEscapeKind,
pub value: Box<str>,
}