From 85c94006af958201c96da9abd0e8ec6323fb0176 Mon Sep 17 00:00:00 2001 From: Glyphack Date: Thu, 20 Feb 2025 20:09:00 +0100 Subject: [PATCH 1/8] Auto generate ast expression nodes --- crates/ruff_python_ast/ast.toml | 36 +- crates/ruff_python_ast/generate.py | 63 ++ crates/ruff_python_ast/src/generated.rs | 1244 ++++++++++++----------- crates/ruff_python_ast/src/nodes.rs | 128 +-- crates/ruff_python_ast/src/relocate.rs | 66 +- 5 files changed, 836 insertions(+), 701 deletions(-) diff --git a/crates/ruff_python_ast/ast.toml b/crates/ruff_python_ast/ast.toml index 10c9cd91321c56..a36c7de9f9a525 100644 --- a/crates/ruff_python_ast/ast.toml +++ b/crates/ruff_python_ast/ast.toml @@ -81,13 +81,6 @@ anynode_is_label = "expression" rustdoc = "/// See also [expr](https://docs.python.org/3/library/ast.html#ast.expr)" [Expr.nodes] -ExprBoolOp = {} -ExprNamed = {} -ExprBinOp = {} -ExprUnaryOp = {} -ExprLambda = {} -ExprIf = {} -ExprDict = {} ExprSet = {} ExprListComp = {} ExprSetComp = {} @@ -113,6 +106,35 @@ ExprList = {} ExprTuple = {} ExprSlice = {} ExprIpyEscapeCommand = {} +ExprBoolOp = { fields = [ + { name = "op", type = "BoolOp" }, + { name = "values", type = "Expr", seq = true } +]} +ExprNamed = { fields = [ + { name = "target", type = "Expr" }, + { name = "value", type = "Expr" } +]} +ExprBinOp = { fields = [ + { name = "left", type = "Expr" }, + { name = "op", type = "Operator" }, + { name = "right", type = "Expr" } +]} +ExprUnaryOp = { fields = [ + { name = "op", type = "UnaryOp" }, + { name = "operand", type = "Expr" } +]} +ExprLambda = { fields = [ + { name = "parameters", type = "Parameters", optional = true }, + { name = "body", type = "Expr" } +]} +ExprIf = { fields = [ + { name = "test", type = "Expr" }, + { name = "body", type = "Expr" }, + { name = "orelse", type = "Expr" } +]} +ExprDict = { fields = [ + { name = "items", type = "DictItem", seq = true }, +]} [ExceptHandler] rustdoc = "/// See also [excepthandler](https://docs.python.org/3/library/ast.html#ast.excepthandler)" diff --git a/crates/ruff_python_ast/generate.py b/crates/ruff_python_ast/generate.py index 35ce26cc5df1f0..6155f0297c4942 100644 --- a/crates/ruff_python_ast/generate.py +++ b/crates/ruff_python_ast/generate.py @@ -90,11 +90,30 @@ class Node: name: str variant: str ty: str + fields: list[Field] | None def __init__(self, group: Group, node_name: str, node: dict[str, Any]) -> None: self.name = node_name self.variant = node.get("variant", node_name.removeprefix(group.name)) self.ty = f"crate::{node_name}" + self.fields = None + fields = node.get("fields") + if fields is not None: + self.fields = [Field(f) for f in fields] + + +@dataclass +class Field: + name: str + ty: str + seq: bool + optional: bool + + def __init__(self, field: dict[str, Any]) -> None: + self.name = field["name"] + self.ty = field["type"] + self.seq = field.get("seq", False) + self.optional = field.get("optional", False) # ------------------------------------------------------------------------------ @@ -547,6 +566,49 @@ def write_nodekind(out: list[str], ast: Ast) -> None: """) +# ------------------------------------------------------------------------------ + + +def write_node(out: list[str], ast: Ast) -> None: + write_node_list = [ + "ExprBoolOp", + "ExprNamed", + "ExprBinOp", + "ExprUnaryOp", + "ExprLambda", + "ExprIf", + "ExprDict", + ] + group_names = [group.name for group in ast.groups] + node_names = [node.name for node in ast.all_nodes] + for group in ast.groups: + for node in group.nodes: + if node.name not in write_node_list: + continue + if node.fields is not None: + out.append("#[derive(Clone, Debug, PartialEq)]") + name = node.name + out.append(f"pub struct {name} {{") + out.append("pub range: ruff_text_size::TextRange,") + for field in node.fields: + field_str = f"pub {field.name}: " + inner = f"crate::{field.ty}" + if field.ty in node_names or ( + field.ty in group_names and (field.seq is False) + ): + inner = f"Box<{inner}>" + + if field.seq: + field_str += f"Vec<{inner}>," + elif field.optional: + field_str += f"Option<{inner}>," + else: + field_str += f"{inner}," + out.append(field_str) + out.append("}") + out.append("") + + # ------------------------------------------------------------------------------ # Format and write output @@ -558,6 +620,7 @@ def generate(ast: Ast) -> list[str]: write_ref_enum(out, ast) write_anynoderef(out, ast) write_nodekind(out, ast) + write_node(out, ast) return out diff --git a/crates/ruff_python_ast/src/generated.rs b/crates/ruff_python_ast/src/generated.rs index c33073da483df2..2ac593f62887f3 100644 --- a/crates/ruff_python_ast/src/generated.rs +++ b/crates/ruff_python_ast/src/generated.rs @@ -1249,13 +1249,6 @@ impl Stmt { /// See also [expr](https://docs.python.org/3/library/ast.html#ast.expr) #[derive(Clone, Debug, PartialEq)] pub enum Expr { - BoolOp(crate::ExprBoolOp), - Named(crate::ExprNamed), - BinOp(crate::ExprBinOp), - UnaryOp(crate::ExprUnaryOp), - Lambda(crate::ExprLambda), - If(crate::ExprIf), - Dict(crate::ExprDict), Set(crate::ExprSet), ListComp(crate::ExprListComp), SetComp(crate::ExprSetComp), @@ -1281,48 +1274,13 @@ pub enum Expr { Tuple(crate::ExprTuple), Slice(crate::ExprSlice), IpyEscapeCommand(crate::ExprIpyEscapeCommand), -} - -impl From for Expr { - fn from(node: crate::ExprBoolOp) -> Self { - Self::BoolOp(node) - } -} - -impl From for Expr { - fn from(node: crate::ExprNamed) -> Self { - Self::Named(node) - } -} - -impl From for Expr { - fn from(node: crate::ExprBinOp) -> Self { - Self::BinOp(node) - } -} - -impl From for Expr { - fn from(node: crate::ExprUnaryOp) -> Self { - Self::UnaryOp(node) - } -} - -impl From for Expr { - fn from(node: crate::ExprLambda) -> Self { - Self::Lambda(node) - } -} - -impl From for Expr { - fn from(node: crate::ExprIf) -> Self { - Self::If(node) - } -} - -impl From for Expr { - fn from(node: crate::ExprDict) -> Self { - Self::Dict(node) - } + BoolOp(crate::ExprBoolOp), + Named(crate::ExprNamed), + BinOp(crate::ExprBinOp), + UnaryOp(crate::ExprUnaryOp), + Lambda(crate::ExprLambda), + If(crate::ExprIf), + Dict(crate::ExprDict), } impl From for Expr { @@ -1475,16 +1433,51 @@ impl From for Expr { } } +impl From for Expr { + fn from(node: crate::ExprBoolOp) -> Self { + Self::BoolOp(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprNamed) -> Self { + Self::Named(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprBinOp) -> Self { + Self::BinOp(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprUnaryOp) -> Self { + Self::UnaryOp(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprLambda) -> Self { + Self::Lambda(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprIf) -> Self { + Self::If(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprDict) -> Self { + Self::Dict(node) + } +} + impl ruff_text_size::Ranged for Expr { fn range(&self) -> ruff_text_size::TextRange { match self { - Self::BoolOp(node) => node.range(), - Self::Named(node) => node.range(), - Self::BinOp(node) => node.range(), - Self::UnaryOp(node) => node.range(), - Self::Lambda(node) => node.range(), - Self::If(node) => node.range(), - Self::Dict(node) => node.range(), Self::Set(node) => node.range(), Self::ListComp(node) => node.range(), Self::SetComp(node) => node.range(), @@ -1510,6 +1503,13 @@ impl ruff_text_size::Ranged for Expr { Self::Tuple(node) => node.range(), Self::Slice(node) => node.range(), Self::IpyEscapeCommand(node) => node.range(), + Self::BoolOp(node) => node.range(), + Self::Named(node) => node.range(), + Self::BinOp(node) => node.range(), + Self::UnaryOp(node) => node.range(), + Self::Lambda(node) => node.range(), + Self::If(node) => node.range(), + Self::Dict(node) => node.range(), } } } @@ -1517,1185 +1517,1185 @@ impl ruff_text_size::Ranged for Expr { #[allow(dead_code, clippy::match_wildcard_for_single_variants)] impl Expr { #[inline] - pub const fn is_bool_op_expr(&self) -> bool { - matches!(self, Self::BoolOp(_)) + pub const fn is_set_expr(&self) -> bool { + matches!(self, Self::Set(_)) } #[inline] - pub fn bool_op_expr(self) -> Option { + pub fn set_expr(self) -> Option { match self { - Self::BoolOp(val) => Some(val), + Self::Set(val) => Some(val), _ => None, } } #[inline] - pub fn expect_bool_op_expr(self) -> crate::ExprBoolOp { + pub fn expect_set_expr(self) -> crate::ExprSet { match self { - Self::BoolOp(val) => val, + Self::Set(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_bool_op_expr_mut(&mut self) -> Option<&mut crate::ExprBoolOp> { + pub fn as_set_expr_mut(&mut self) -> Option<&mut crate::ExprSet> { match self { - Self::BoolOp(val) => Some(val), + Self::Set(val) => Some(val), _ => None, } } #[inline] - pub fn as_bool_op_expr(&self) -> Option<&crate::ExprBoolOp> { + pub fn as_set_expr(&self) -> Option<&crate::ExprSet> { match self { - Self::BoolOp(val) => Some(val), + Self::Set(val) => Some(val), _ => None, } } #[inline] - pub const fn is_named_expr(&self) -> bool { - matches!(self, Self::Named(_)) + pub const fn is_list_comp_expr(&self) -> bool { + matches!(self, Self::ListComp(_)) } #[inline] - pub fn named_expr(self) -> Option { + pub fn list_comp_expr(self) -> Option { match self { - Self::Named(val) => Some(val), + Self::ListComp(val) => Some(val), _ => None, } } #[inline] - pub fn expect_named_expr(self) -> crate::ExprNamed { + pub fn expect_list_comp_expr(self) -> crate::ExprListComp { match self { - Self::Named(val) => val, + Self::ListComp(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_named_expr_mut(&mut self) -> Option<&mut crate::ExprNamed> { + pub fn as_list_comp_expr_mut(&mut self) -> Option<&mut crate::ExprListComp> { match self { - Self::Named(val) => Some(val), + Self::ListComp(val) => Some(val), _ => None, } } #[inline] - pub fn as_named_expr(&self) -> Option<&crate::ExprNamed> { + pub fn as_list_comp_expr(&self) -> Option<&crate::ExprListComp> { match self { - Self::Named(val) => Some(val), + Self::ListComp(val) => Some(val), _ => None, } } #[inline] - pub const fn is_bin_op_expr(&self) -> bool { - matches!(self, Self::BinOp(_)) + pub const fn is_set_comp_expr(&self) -> bool { + matches!(self, Self::SetComp(_)) } #[inline] - pub fn bin_op_expr(self) -> Option { + pub fn set_comp_expr(self) -> Option { match self { - Self::BinOp(val) => Some(val), + Self::SetComp(val) => Some(val), _ => None, } } #[inline] - pub fn expect_bin_op_expr(self) -> crate::ExprBinOp { + pub fn expect_set_comp_expr(self) -> crate::ExprSetComp { match self { - Self::BinOp(val) => val, + Self::SetComp(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_bin_op_expr_mut(&mut self) -> Option<&mut crate::ExprBinOp> { + pub fn as_set_comp_expr_mut(&mut self) -> Option<&mut crate::ExprSetComp> { match self { - Self::BinOp(val) => Some(val), + Self::SetComp(val) => Some(val), _ => None, } } #[inline] - pub fn as_bin_op_expr(&self) -> Option<&crate::ExprBinOp> { + pub fn as_set_comp_expr(&self) -> Option<&crate::ExprSetComp> { match self { - Self::BinOp(val) => Some(val), + Self::SetComp(val) => Some(val), _ => None, } } #[inline] - pub const fn is_unary_op_expr(&self) -> bool { - matches!(self, Self::UnaryOp(_)) + pub const fn is_dict_comp_expr(&self) -> bool { + matches!(self, Self::DictComp(_)) } #[inline] - pub fn unary_op_expr(self) -> Option { + pub fn dict_comp_expr(self) -> Option { match self { - Self::UnaryOp(val) => Some(val), + Self::DictComp(val) => Some(val), _ => None, } } #[inline] - pub fn expect_unary_op_expr(self) -> crate::ExprUnaryOp { + pub fn expect_dict_comp_expr(self) -> crate::ExprDictComp { match self { - Self::UnaryOp(val) => val, + Self::DictComp(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_unary_op_expr_mut(&mut self) -> Option<&mut crate::ExprUnaryOp> { + pub fn as_dict_comp_expr_mut(&mut self) -> Option<&mut crate::ExprDictComp> { match self { - Self::UnaryOp(val) => Some(val), + Self::DictComp(val) => Some(val), _ => None, } } #[inline] - pub fn as_unary_op_expr(&self) -> Option<&crate::ExprUnaryOp> { + pub fn as_dict_comp_expr(&self) -> Option<&crate::ExprDictComp> { match self { - Self::UnaryOp(val) => Some(val), + Self::DictComp(val) => Some(val), _ => None, } } #[inline] - pub const fn is_lambda_expr(&self) -> bool { - matches!(self, Self::Lambda(_)) + pub const fn is_generator_expr(&self) -> bool { + matches!(self, Self::Generator(_)) } #[inline] - pub fn lambda_expr(self) -> Option { + pub fn generator_expr(self) -> Option { match self { - Self::Lambda(val) => Some(val), + Self::Generator(val) => Some(val), _ => None, } } #[inline] - pub fn expect_lambda_expr(self) -> crate::ExprLambda { + pub fn expect_generator_expr(self) -> crate::ExprGenerator { match self { - Self::Lambda(val) => val, + Self::Generator(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_lambda_expr_mut(&mut self) -> Option<&mut crate::ExprLambda> { + pub fn as_generator_expr_mut(&mut self) -> Option<&mut crate::ExprGenerator> { match self { - Self::Lambda(val) => Some(val), + Self::Generator(val) => Some(val), _ => None, } } #[inline] - pub fn as_lambda_expr(&self) -> Option<&crate::ExprLambda> { + pub fn as_generator_expr(&self) -> Option<&crate::ExprGenerator> { match self { - Self::Lambda(val) => Some(val), + Self::Generator(val) => Some(val), _ => None, } } #[inline] - pub const fn is_if_expr(&self) -> bool { - matches!(self, Self::If(_)) + pub const fn is_await_expr(&self) -> bool { + matches!(self, Self::Await(_)) } #[inline] - pub fn if_expr(self) -> Option { + pub fn await_expr(self) -> Option { match self { - Self::If(val) => Some(val), + Self::Await(val) => Some(val), _ => None, } } #[inline] - pub fn expect_if_expr(self) -> crate::ExprIf { + pub fn expect_await_expr(self) -> crate::ExprAwait { match self { - Self::If(val) => val, + Self::Await(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_if_expr_mut(&mut self) -> Option<&mut crate::ExprIf> { + pub fn as_await_expr_mut(&mut self) -> Option<&mut crate::ExprAwait> { match self { - Self::If(val) => Some(val), + Self::Await(val) => Some(val), _ => None, } } #[inline] - pub fn as_if_expr(&self) -> Option<&crate::ExprIf> { + pub fn as_await_expr(&self) -> Option<&crate::ExprAwait> { match self { - Self::If(val) => Some(val), + Self::Await(val) => Some(val), _ => None, } } #[inline] - pub const fn is_dict_expr(&self) -> bool { - matches!(self, Self::Dict(_)) + pub const fn is_yield_expr(&self) -> bool { + matches!(self, Self::Yield(_)) } #[inline] - pub fn dict_expr(self) -> Option { + pub fn yield_expr(self) -> Option { match self { - Self::Dict(val) => Some(val), + Self::Yield(val) => Some(val), _ => None, } } #[inline] - pub fn expect_dict_expr(self) -> crate::ExprDict { + pub fn expect_yield_expr(self) -> crate::ExprYield { match self { - Self::Dict(val) => val, + Self::Yield(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_dict_expr_mut(&mut self) -> Option<&mut crate::ExprDict> { + pub fn as_yield_expr_mut(&mut self) -> Option<&mut crate::ExprYield> { match self { - Self::Dict(val) => Some(val), + Self::Yield(val) => Some(val), _ => None, } } #[inline] - pub fn as_dict_expr(&self) -> Option<&crate::ExprDict> { + pub fn as_yield_expr(&self) -> Option<&crate::ExprYield> { match self { - Self::Dict(val) => Some(val), + Self::Yield(val) => Some(val), _ => None, } } #[inline] - pub const fn is_set_expr(&self) -> bool { - matches!(self, Self::Set(_)) + pub const fn is_yield_from_expr(&self) -> bool { + matches!(self, Self::YieldFrom(_)) } #[inline] - pub fn set_expr(self) -> Option { + pub fn yield_from_expr(self) -> Option { match self { - Self::Set(val) => Some(val), + Self::YieldFrom(val) => Some(val), _ => None, } } #[inline] - pub fn expect_set_expr(self) -> crate::ExprSet { + pub fn expect_yield_from_expr(self) -> crate::ExprYieldFrom { match self { - Self::Set(val) => val, + Self::YieldFrom(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_set_expr_mut(&mut self) -> Option<&mut crate::ExprSet> { + pub fn as_yield_from_expr_mut(&mut self) -> Option<&mut crate::ExprYieldFrom> { match self { - Self::Set(val) => Some(val), + Self::YieldFrom(val) => Some(val), _ => None, } } #[inline] - pub fn as_set_expr(&self) -> Option<&crate::ExprSet> { + pub fn as_yield_from_expr(&self) -> Option<&crate::ExprYieldFrom> { match self { - Self::Set(val) => Some(val), + Self::YieldFrom(val) => Some(val), _ => None, } } #[inline] - pub const fn is_list_comp_expr(&self) -> bool { - matches!(self, Self::ListComp(_)) + pub const fn is_compare_expr(&self) -> bool { + matches!(self, Self::Compare(_)) } #[inline] - pub fn list_comp_expr(self) -> Option { + pub fn compare_expr(self) -> Option { match self { - Self::ListComp(val) => Some(val), + Self::Compare(val) => Some(val), _ => None, } } #[inline] - pub fn expect_list_comp_expr(self) -> crate::ExprListComp { + pub fn expect_compare_expr(self) -> crate::ExprCompare { match self { - Self::ListComp(val) => val, + Self::Compare(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_list_comp_expr_mut(&mut self) -> Option<&mut crate::ExprListComp> { + pub fn as_compare_expr_mut(&mut self) -> Option<&mut crate::ExprCompare> { match self { - Self::ListComp(val) => Some(val), + Self::Compare(val) => Some(val), _ => None, } } #[inline] - pub fn as_list_comp_expr(&self) -> Option<&crate::ExprListComp> { + pub fn as_compare_expr(&self) -> Option<&crate::ExprCompare> { match self { - Self::ListComp(val) => Some(val), + Self::Compare(val) => Some(val), _ => None, } } #[inline] - pub const fn is_set_comp_expr(&self) -> bool { - matches!(self, Self::SetComp(_)) + pub const fn is_call_expr(&self) -> bool { + matches!(self, Self::Call(_)) } #[inline] - pub fn set_comp_expr(self) -> Option { + pub fn call_expr(self) -> Option { match self { - Self::SetComp(val) => Some(val), + Self::Call(val) => Some(val), _ => None, } } #[inline] - pub fn expect_set_comp_expr(self) -> crate::ExprSetComp { + pub fn expect_call_expr(self) -> crate::ExprCall { match self { - Self::SetComp(val) => val, + Self::Call(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_set_comp_expr_mut(&mut self) -> Option<&mut crate::ExprSetComp> { + pub fn as_call_expr_mut(&mut self) -> Option<&mut crate::ExprCall> { match self { - Self::SetComp(val) => Some(val), + Self::Call(val) => Some(val), _ => None, } } #[inline] - pub fn as_set_comp_expr(&self) -> Option<&crate::ExprSetComp> { + pub fn as_call_expr(&self) -> Option<&crate::ExprCall> { match self { - Self::SetComp(val) => Some(val), + Self::Call(val) => Some(val), _ => None, } } #[inline] - pub const fn is_dict_comp_expr(&self) -> bool { - matches!(self, Self::DictComp(_)) + pub const fn is_f_string_expr(&self) -> bool { + matches!(self, Self::FString(_)) } #[inline] - pub fn dict_comp_expr(self) -> Option { + pub fn f_string_expr(self) -> Option { match self { - Self::DictComp(val) => Some(val), + Self::FString(val) => Some(val), _ => None, } } #[inline] - pub fn expect_dict_comp_expr(self) -> crate::ExprDictComp { + pub fn expect_f_string_expr(self) -> crate::ExprFString { match self { - Self::DictComp(val) => val, + Self::FString(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_dict_comp_expr_mut(&mut self) -> Option<&mut crate::ExprDictComp> { + pub fn as_f_string_expr_mut(&mut self) -> Option<&mut crate::ExprFString> { match self { - Self::DictComp(val) => Some(val), + Self::FString(val) => Some(val), _ => None, } } #[inline] - pub fn as_dict_comp_expr(&self) -> Option<&crate::ExprDictComp> { + pub fn as_f_string_expr(&self) -> Option<&crate::ExprFString> { match self { - Self::DictComp(val) => Some(val), + Self::FString(val) => Some(val), _ => None, } } #[inline] - pub const fn is_generator_expr(&self) -> bool { - matches!(self, Self::Generator(_)) + pub const fn is_string_literal_expr(&self) -> bool { + matches!(self, Self::StringLiteral(_)) } #[inline] - pub fn generator_expr(self) -> Option { + pub fn string_literal_expr(self) -> Option { match self { - Self::Generator(val) => Some(val), + Self::StringLiteral(val) => Some(val), _ => None, } } #[inline] - pub fn expect_generator_expr(self) -> crate::ExprGenerator { + pub fn expect_string_literal_expr(self) -> crate::ExprStringLiteral { match self { - Self::Generator(val) => val, + Self::StringLiteral(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_generator_expr_mut(&mut self) -> Option<&mut crate::ExprGenerator> { + pub fn as_string_literal_expr_mut(&mut self) -> Option<&mut crate::ExprStringLiteral> { match self { - Self::Generator(val) => Some(val), + Self::StringLiteral(val) => Some(val), _ => None, } } #[inline] - pub fn as_generator_expr(&self) -> Option<&crate::ExprGenerator> { + pub fn as_string_literal_expr(&self) -> Option<&crate::ExprStringLiteral> { match self { - Self::Generator(val) => Some(val), + Self::StringLiteral(val) => Some(val), _ => None, } } #[inline] - pub const fn is_await_expr(&self) -> bool { - matches!(self, Self::Await(_)) + pub const fn is_bytes_literal_expr(&self) -> bool { + matches!(self, Self::BytesLiteral(_)) } #[inline] - pub fn await_expr(self) -> Option { + pub fn bytes_literal_expr(self) -> Option { match self { - Self::Await(val) => Some(val), + Self::BytesLiteral(val) => Some(val), _ => None, } } #[inline] - pub fn expect_await_expr(self) -> crate::ExprAwait { + pub fn expect_bytes_literal_expr(self) -> crate::ExprBytesLiteral { match self { - Self::Await(val) => val, + Self::BytesLiteral(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_await_expr_mut(&mut self) -> Option<&mut crate::ExprAwait> { + pub fn as_bytes_literal_expr_mut(&mut self) -> Option<&mut crate::ExprBytesLiteral> { match self { - Self::Await(val) => Some(val), + Self::BytesLiteral(val) => Some(val), _ => None, } } #[inline] - pub fn as_await_expr(&self) -> Option<&crate::ExprAwait> { + pub fn as_bytes_literal_expr(&self) -> Option<&crate::ExprBytesLiteral> { match self { - Self::Await(val) => Some(val), + Self::BytesLiteral(val) => Some(val), _ => None, } } #[inline] - pub const fn is_yield_expr(&self) -> bool { - matches!(self, Self::Yield(_)) + pub const fn is_number_literal_expr(&self) -> bool { + matches!(self, Self::NumberLiteral(_)) } #[inline] - pub fn yield_expr(self) -> Option { + pub fn number_literal_expr(self) -> Option { match self { - Self::Yield(val) => Some(val), + Self::NumberLiteral(val) => Some(val), _ => None, } } #[inline] - pub fn expect_yield_expr(self) -> crate::ExprYield { + pub fn expect_number_literal_expr(self) -> crate::ExprNumberLiteral { match self { - Self::Yield(val) => val, + Self::NumberLiteral(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_yield_expr_mut(&mut self) -> Option<&mut crate::ExprYield> { + pub fn as_number_literal_expr_mut(&mut self) -> Option<&mut crate::ExprNumberLiteral> { match self { - Self::Yield(val) => Some(val), + Self::NumberLiteral(val) => Some(val), _ => None, } } #[inline] - pub fn as_yield_expr(&self) -> Option<&crate::ExprYield> { + pub fn as_number_literal_expr(&self) -> Option<&crate::ExprNumberLiteral> { match self { - Self::Yield(val) => Some(val), + Self::NumberLiteral(val) => Some(val), _ => None, } } #[inline] - pub const fn is_yield_from_expr(&self) -> bool { - matches!(self, Self::YieldFrom(_)) + pub const fn is_boolean_literal_expr(&self) -> bool { + matches!(self, Self::BooleanLiteral(_)) } #[inline] - pub fn yield_from_expr(self) -> Option { + pub fn boolean_literal_expr(self) -> Option { match self { - Self::YieldFrom(val) => Some(val), + Self::BooleanLiteral(val) => Some(val), _ => None, } } #[inline] - pub fn expect_yield_from_expr(self) -> crate::ExprYieldFrom { + pub fn expect_boolean_literal_expr(self) -> crate::ExprBooleanLiteral { match self { - Self::YieldFrom(val) => val, + Self::BooleanLiteral(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_yield_from_expr_mut(&mut self) -> Option<&mut crate::ExprYieldFrom> { + pub fn as_boolean_literal_expr_mut(&mut self) -> Option<&mut crate::ExprBooleanLiteral> { match self { - Self::YieldFrom(val) => Some(val), + Self::BooleanLiteral(val) => Some(val), _ => None, } } #[inline] - pub fn as_yield_from_expr(&self) -> Option<&crate::ExprYieldFrom> { + pub fn as_boolean_literal_expr(&self) -> Option<&crate::ExprBooleanLiteral> { match self { - Self::YieldFrom(val) => Some(val), + Self::BooleanLiteral(val) => Some(val), _ => None, } } #[inline] - pub const fn is_compare_expr(&self) -> bool { - matches!(self, Self::Compare(_)) + pub const fn is_none_literal_expr(&self) -> bool { + matches!(self, Self::NoneLiteral(_)) } #[inline] - pub fn compare_expr(self) -> Option { + pub fn none_literal_expr(self) -> Option { match self { - Self::Compare(val) => Some(val), + Self::NoneLiteral(val) => Some(val), _ => None, } } #[inline] - pub fn expect_compare_expr(self) -> crate::ExprCompare { + pub fn expect_none_literal_expr(self) -> crate::ExprNoneLiteral { match self { - Self::Compare(val) => val, + Self::NoneLiteral(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_compare_expr_mut(&mut self) -> Option<&mut crate::ExprCompare> { + pub fn as_none_literal_expr_mut(&mut self) -> Option<&mut crate::ExprNoneLiteral> { match self { - Self::Compare(val) => Some(val), + Self::NoneLiteral(val) => Some(val), _ => None, } } #[inline] - pub fn as_compare_expr(&self) -> Option<&crate::ExprCompare> { + pub fn as_none_literal_expr(&self) -> Option<&crate::ExprNoneLiteral> { match self { - Self::Compare(val) => Some(val), + Self::NoneLiteral(val) => Some(val), _ => None, } } #[inline] - pub const fn is_call_expr(&self) -> bool { - matches!(self, Self::Call(_)) + pub const fn is_ellipsis_literal_expr(&self) -> bool { + matches!(self, Self::EllipsisLiteral(_)) } #[inline] - pub fn call_expr(self) -> Option { + pub fn ellipsis_literal_expr(self) -> Option { match self { - Self::Call(val) => Some(val), + Self::EllipsisLiteral(val) => Some(val), _ => None, } } #[inline] - pub fn expect_call_expr(self) -> crate::ExprCall { + pub fn expect_ellipsis_literal_expr(self) -> crate::ExprEllipsisLiteral { match self { - Self::Call(val) => val, + Self::EllipsisLiteral(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_call_expr_mut(&mut self) -> Option<&mut crate::ExprCall> { + pub fn as_ellipsis_literal_expr_mut(&mut self) -> Option<&mut crate::ExprEllipsisLiteral> { match self { - Self::Call(val) => Some(val), + Self::EllipsisLiteral(val) => Some(val), _ => None, } } #[inline] - pub fn as_call_expr(&self) -> Option<&crate::ExprCall> { + pub fn as_ellipsis_literal_expr(&self) -> Option<&crate::ExprEllipsisLiteral> { match self { - Self::Call(val) => Some(val), + Self::EllipsisLiteral(val) => Some(val), _ => None, } } #[inline] - pub const fn is_f_string_expr(&self) -> bool { - matches!(self, Self::FString(_)) + pub const fn is_attribute_expr(&self) -> bool { + matches!(self, Self::Attribute(_)) } #[inline] - pub fn f_string_expr(self) -> Option { + pub fn attribute_expr(self) -> Option { match self { - Self::FString(val) => Some(val), + Self::Attribute(val) => Some(val), _ => None, } } #[inline] - pub fn expect_f_string_expr(self) -> crate::ExprFString { + pub fn expect_attribute_expr(self) -> crate::ExprAttribute { match self { - Self::FString(val) => val, + Self::Attribute(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_f_string_expr_mut(&mut self) -> Option<&mut crate::ExprFString> { + pub fn as_attribute_expr_mut(&mut self) -> Option<&mut crate::ExprAttribute> { match self { - Self::FString(val) => Some(val), + Self::Attribute(val) => Some(val), _ => None, } } #[inline] - pub fn as_f_string_expr(&self) -> Option<&crate::ExprFString> { + pub fn as_attribute_expr(&self) -> Option<&crate::ExprAttribute> { match self { - Self::FString(val) => Some(val), + Self::Attribute(val) => Some(val), _ => None, } } #[inline] - pub const fn is_string_literal_expr(&self) -> bool { - matches!(self, Self::StringLiteral(_)) + pub const fn is_subscript_expr(&self) -> bool { + matches!(self, Self::Subscript(_)) } #[inline] - pub fn string_literal_expr(self) -> Option { + pub fn subscript_expr(self) -> Option { match self { - Self::StringLiteral(val) => Some(val), + Self::Subscript(val) => Some(val), _ => None, } } #[inline] - pub fn expect_string_literal_expr(self) -> crate::ExprStringLiteral { + pub fn expect_subscript_expr(self) -> crate::ExprSubscript { match self { - Self::StringLiteral(val) => val, + Self::Subscript(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_string_literal_expr_mut(&mut self) -> Option<&mut crate::ExprStringLiteral> { + pub fn as_subscript_expr_mut(&mut self) -> Option<&mut crate::ExprSubscript> { match self { - Self::StringLiteral(val) => Some(val), + Self::Subscript(val) => Some(val), _ => None, } } #[inline] - pub fn as_string_literal_expr(&self) -> Option<&crate::ExprStringLiteral> { + pub fn as_subscript_expr(&self) -> Option<&crate::ExprSubscript> { match self { - Self::StringLiteral(val) => Some(val), + Self::Subscript(val) => Some(val), _ => None, } } #[inline] - pub const fn is_bytes_literal_expr(&self) -> bool { - matches!(self, Self::BytesLiteral(_)) + pub const fn is_starred_expr(&self) -> bool { + matches!(self, Self::Starred(_)) } #[inline] - pub fn bytes_literal_expr(self) -> Option { + pub fn starred_expr(self) -> Option { match self { - Self::BytesLiteral(val) => Some(val), + Self::Starred(val) => Some(val), _ => None, } } #[inline] - pub fn expect_bytes_literal_expr(self) -> crate::ExprBytesLiteral { + pub fn expect_starred_expr(self) -> crate::ExprStarred { match self { - Self::BytesLiteral(val) => val, + Self::Starred(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_bytes_literal_expr_mut(&mut self) -> Option<&mut crate::ExprBytesLiteral> { + pub fn as_starred_expr_mut(&mut self) -> Option<&mut crate::ExprStarred> { match self { - Self::BytesLiteral(val) => Some(val), + Self::Starred(val) => Some(val), _ => None, } } #[inline] - pub fn as_bytes_literal_expr(&self) -> Option<&crate::ExprBytesLiteral> { + pub fn as_starred_expr(&self) -> Option<&crate::ExprStarred> { match self { - Self::BytesLiteral(val) => Some(val), + Self::Starred(val) => Some(val), _ => None, } } #[inline] - pub const fn is_number_literal_expr(&self) -> bool { - matches!(self, Self::NumberLiteral(_)) + pub const fn is_name_expr(&self) -> bool { + matches!(self, Self::Name(_)) } #[inline] - pub fn number_literal_expr(self) -> Option { + pub fn name_expr(self) -> Option { match self { - Self::NumberLiteral(val) => Some(val), + Self::Name(val) => Some(val), _ => None, } } #[inline] - pub fn expect_number_literal_expr(self) -> crate::ExprNumberLiteral { + pub fn expect_name_expr(self) -> crate::ExprName { match self { - Self::NumberLiteral(val) => val, + Self::Name(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_number_literal_expr_mut(&mut self) -> Option<&mut crate::ExprNumberLiteral> { + pub fn as_name_expr_mut(&mut self) -> Option<&mut crate::ExprName> { match self { - Self::NumberLiteral(val) => Some(val), + Self::Name(val) => Some(val), _ => None, } } #[inline] - pub fn as_number_literal_expr(&self) -> Option<&crate::ExprNumberLiteral> { + pub fn as_name_expr(&self) -> Option<&crate::ExprName> { match self { - Self::NumberLiteral(val) => Some(val), + Self::Name(val) => Some(val), _ => None, } } #[inline] - pub const fn is_boolean_literal_expr(&self) -> bool { - matches!(self, Self::BooleanLiteral(_)) + pub const fn is_list_expr(&self) -> bool { + matches!(self, Self::List(_)) } #[inline] - pub fn boolean_literal_expr(self) -> Option { + pub fn list_expr(self) -> Option { match self { - Self::BooleanLiteral(val) => Some(val), + Self::List(val) => Some(val), _ => None, } } #[inline] - pub fn expect_boolean_literal_expr(self) -> crate::ExprBooleanLiteral { + pub fn expect_list_expr(self) -> crate::ExprList { match self { - Self::BooleanLiteral(val) => val, + Self::List(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_boolean_literal_expr_mut(&mut self) -> Option<&mut crate::ExprBooleanLiteral> { + pub fn as_list_expr_mut(&mut self) -> Option<&mut crate::ExprList> { match self { - Self::BooleanLiteral(val) => Some(val), + Self::List(val) => Some(val), _ => None, } } #[inline] - pub fn as_boolean_literal_expr(&self) -> Option<&crate::ExprBooleanLiteral> { + pub fn as_list_expr(&self) -> Option<&crate::ExprList> { match self { - Self::BooleanLiteral(val) => Some(val), + Self::List(val) => Some(val), _ => None, } } #[inline] - pub const fn is_none_literal_expr(&self) -> bool { - matches!(self, Self::NoneLiteral(_)) + pub const fn is_tuple_expr(&self) -> bool { + matches!(self, Self::Tuple(_)) } #[inline] - pub fn none_literal_expr(self) -> Option { + pub fn tuple_expr(self) -> Option { match self { - Self::NoneLiteral(val) => Some(val), + Self::Tuple(val) => Some(val), _ => None, } } #[inline] - pub fn expect_none_literal_expr(self) -> crate::ExprNoneLiteral { + pub fn expect_tuple_expr(self) -> crate::ExprTuple { match self { - Self::NoneLiteral(val) => val, + Self::Tuple(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_none_literal_expr_mut(&mut self) -> Option<&mut crate::ExprNoneLiteral> { + pub fn as_tuple_expr_mut(&mut self) -> Option<&mut crate::ExprTuple> { match self { - Self::NoneLiteral(val) => Some(val), + Self::Tuple(val) => Some(val), _ => None, } } #[inline] - pub fn as_none_literal_expr(&self) -> Option<&crate::ExprNoneLiteral> { + pub fn as_tuple_expr(&self) -> Option<&crate::ExprTuple> { match self { - Self::NoneLiteral(val) => Some(val), + Self::Tuple(val) => Some(val), _ => None, } } #[inline] - pub const fn is_ellipsis_literal_expr(&self) -> bool { - matches!(self, Self::EllipsisLiteral(_)) + pub const fn is_slice_expr(&self) -> bool { + matches!(self, Self::Slice(_)) } #[inline] - pub fn ellipsis_literal_expr(self) -> Option { + pub fn slice_expr(self) -> Option { match self { - Self::EllipsisLiteral(val) => Some(val), + Self::Slice(val) => Some(val), _ => None, } } #[inline] - pub fn expect_ellipsis_literal_expr(self) -> crate::ExprEllipsisLiteral { + pub fn expect_slice_expr(self) -> crate::ExprSlice { match self { - Self::EllipsisLiteral(val) => val, + Self::Slice(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_ellipsis_literal_expr_mut(&mut self) -> Option<&mut crate::ExprEllipsisLiteral> { + pub fn as_slice_expr_mut(&mut self) -> Option<&mut crate::ExprSlice> { match self { - Self::EllipsisLiteral(val) => Some(val), + Self::Slice(val) => Some(val), _ => None, } } #[inline] - pub fn as_ellipsis_literal_expr(&self) -> Option<&crate::ExprEllipsisLiteral> { + pub fn as_slice_expr(&self) -> Option<&crate::ExprSlice> { match self { - Self::EllipsisLiteral(val) => Some(val), + Self::Slice(val) => Some(val), _ => None, } } #[inline] - pub const fn is_attribute_expr(&self) -> bool { - matches!(self, Self::Attribute(_)) + pub const fn is_ipy_escape_command_expr(&self) -> bool { + matches!(self, Self::IpyEscapeCommand(_)) } #[inline] - pub fn attribute_expr(self) -> Option { + pub fn ipy_escape_command_expr(self) -> Option { match self { - Self::Attribute(val) => Some(val), + Self::IpyEscapeCommand(val) => Some(val), _ => None, } } #[inline] - pub fn expect_attribute_expr(self) -> crate::ExprAttribute { + pub fn expect_ipy_escape_command_expr(self) -> crate::ExprIpyEscapeCommand { match self { - Self::Attribute(val) => val, + Self::IpyEscapeCommand(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_attribute_expr_mut(&mut self) -> Option<&mut crate::ExprAttribute> { + pub fn as_ipy_escape_command_expr_mut(&mut self) -> Option<&mut crate::ExprIpyEscapeCommand> { match self { - Self::Attribute(val) => Some(val), + Self::IpyEscapeCommand(val) => Some(val), _ => None, } } #[inline] - pub fn as_attribute_expr(&self) -> Option<&crate::ExprAttribute> { + pub fn as_ipy_escape_command_expr(&self) -> Option<&crate::ExprIpyEscapeCommand> { match self { - Self::Attribute(val) => Some(val), + Self::IpyEscapeCommand(val) => Some(val), _ => None, } } #[inline] - pub const fn is_subscript_expr(&self) -> bool { - matches!(self, Self::Subscript(_)) + pub const fn is_bool_op_expr(&self) -> bool { + matches!(self, Self::BoolOp(_)) } #[inline] - pub fn subscript_expr(self) -> Option { + pub fn bool_op_expr(self) -> Option { match self { - Self::Subscript(val) => Some(val), + Self::BoolOp(val) => Some(val), _ => None, } } #[inline] - pub fn expect_subscript_expr(self) -> crate::ExprSubscript { + pub fn expect_bool_op_expr(self) -> crate::ExprBoolOp { match self { - Self::Subscript(val) => val, + Self::BoolOp(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_subscript_expr_mut(&mut self) -> Option<&mut crate::ExprSubscript> { + pub fn as_bool_op_expr_mut(&mut self) -> Option<&mut crate::ExprBoolOp> { match self { - Self::Subscript(val) => Some(val), + Self::BoolOp(val) => Some(val), _ => None, } } #[inline] - pub fn as_subscript_expr(&self) -> Option<&crate::ExprSubscript> { + pub fn as_bool_op_expr(&self) -> Option<&crate::ExprBoolOp> { match self { - Self::Subscript(val) => Some(val), + Self::BoolOp(val) => Some(val), _ => None, } } #[inline] - pub const fn is_starred_expr(&self) -> bool { - matches!(self, Self::Starred(_)) + pub const fn is_named_expr(&self) -> bool { + matches!(self, Self::Named(_)) } #[inline] - pub fn starred_expr(self) -> Option { + pub fn named_expr(self) -> Option { match self { - Self::Starred(val) => Some(val), + Self::Named(val) => Some(val), _ => None, } } #[inline] - pub fn expect_starred_expr(self) -> crate::ExprStarred { + pub fn expect_named_expr(self) -> crate::ExprNamed { match self { - Self::Starred(val) => val, + Self::Named(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_starred_expr_mut(&mut self) -> Option<&mut crate::ExprStarred> { + pub fn as_named_expr_mut(&mut self) -> Option<&mut crate::ExprNamed> { match self { - Self::Starred(val) => Some(val), + Self::Named(val) => Some(val), _ => None, } } #[inline] - pub fn as_starred_expr(&self) -> Option<&crate::ExprStarred> { + pub fn as_named_expr(&self) -> Option<&crate::ExprNamed> { match self { - Self::Starred(val) => Some(val), + Self::Named(val) => Some(val), _ => None, } } #[inline] - pub const fn is_name_expr(&self) -> bool { - matches!(self, Self::Name(_)) + pub const fn is_bin_op_expr(&self) -> bool { + matches!(self, Self::BinOp(_)) } #[inline] - pub fn name_expr(self) -> Option { + pub fn bin_op_expr(self) -> Option { match self { - Self::Name(val) => Some(val), + Self::BinOp(val) => Some(val), _ => None, } } #[inline] - pub fn expect_name_expr(self) -> crate::ExprName { + pub fn expect_bin_op_expr(self) -> crate::ExprBinOp { match self { - Self::Name(val) => val, + Self::BinOp(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_name_expr_mut(&mut self) -> Option<&mut crate::ExprName> { + pub fn as_bin_op_expr_mut(&mut self) -> Option<&mut crate::ExprBinOp> { match self { - Self::Name(val) => Some(val), + Self::BinOp(val) => Some(val), _ => None, } } #[inline] - pub fn as_name_expr(&self) -> Option<&crate::ExprName> { + pub fn as_bin_op_expr(&self) -> Option<&crate::ExprBinOp> { match self { - Self::Name(val) => Some(val), + Self::BinOp(val) => Some(val), _ => None, } } #[inline] - pub const fn is_list_expr(&self) -> bool { - matches!(self, Self::List(_)) + pub const fn is_unary_op_expr(&self) -> bool { + matches!(self, Self::UnaryOp(_)) } #[inline] - pub fn list_expr(self) -> Option { + pub fn unary_op_expr(self) -> Option { match self { - Self::List(val) => Some(val), + Self::UnaryOp(val) => Some(val), _ => None, } } #[inline] - pub fn expect_list_expr(self) -> crate::ExprList { + pub fn expect_unary_op_expr(self) -> crate::ExprUnaryOp { match self { - Self::List(val) => val, + Self::UnaryOp(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_list_expr_mut(&mut self) -> Option<&mut crate::ExprList> { + pub fn as_unary_op_expr_mut(&mut self) -> Option<&mut crate::ExprUnaryOp> { match self { - Self::List(val) => Some(val), + Self::UnaryOp(val) => Some(val), _ => None, } } #[inline] - pub fn as_list_expr(&self) -> Option<&crate::ExprList> { + pub fn as_unary_op_expr(&self) -> Option<&crate::ExprUnaryOp> { match self { - Self::List(val) => Some(val), + Self::UnaryOp(val) => Some(val), _ => None, } } #[inline] - pub const fn is_tuple_expr(&self) -> bool { - matches!(self, Self::Tuple(_)) + pub const fn is_lambda_expr(&self) -> bool { + matches!(self, Self::Lambda(_)) } #[inline] - pub fn tuple_expr(self) -> Option { + pub fn lambda_expr(self) -> Option { match self { - Self::Tuple(val) => Some(val), + Self::Lambda(val) => Some(val), _ => None, } } #[inline] - pub fn expect_tuple_expr(self) -> crate::ExprTuple { + pub fn expect_lambda_expr(self) -> crate::ExprLambda { match self { - Self::Tuple(val) => val, + Self::Lambda(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_tuple_expr_mut(&mut self) -> Option<&mut crate::ExprTuple> { + pub fn as_lambda_expr_mut(&mut self) -> Option<&mut crate::ExprLambda> { match self { - Self::Tuple(val) => Some(val), + Self::Lambda(val) => Some(val), _ => None, } } #[inline] - pub fn as_tuple_expr(&self) -> Option<&crate::ExprTuple> { + pub fn as_lambda_expr(&self) -> Option<&crate::ExprLambda> { match self { - Self::Tuple(val) => Some(val), + Self::Lambda(val) => Some(val), _ => None, } } #[inline] - pub const fn is_slice_expr(&self) -> bool { - matches!(self, Self::Slice(_)) + pub const fn is_if_expr(&self) -> bool { + matches!(self, Self::If(_)) } #[inline] - pub fn slice_expr(self) -> Option { + pub fn if_expr(self) -> Option { match self { - Self::Slice(val) => Some(val), + Self::If(val) => Some(val), _ => None, } } #[inline] - pub fn expect_slice_expr(self) -> crate::ExprSlice { + pub fn expect_if_expr(self) -> crate::ExprIf { match self { - Self::Slice(val) => val, + Self::If(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_slice_expr_mut(&mut self) -> Option<&mut crate::ExprSlice> { + pub fn as_if_expr_mut(&mut self) -> Option<&mut crate::ExprIf> { match self { - Self::Slice(val) => Some(val), + Self::If(val) => Some(val), _ => None, } } #[inline] - pub fn as_slice_expr(&self) -> Option<&crate::ExprSlice> { + pub fn as_if_expr(&self) -> Option<&crate::ExprIf> { match self { - Self::Slice(val) => Some(val), + Self::If(val) => Some(val), _ => None, } } #[inline] - pub const fn is_ipy_escape_command_expr(&self) -> bool { - matches!(self, Self::IpyEscapeCommand(_)) + pub const fn is_dict_expr(&self) -> bool { + matches!(self, Self::Dict(_)) } #[inline] - pub fn ipy_escape_command_expr(self) -> Option { + pub fn dict_expr(self) -> Option { match self { - Self::IpyEscapeCommand(val) => Some(val), + Self::Dict(val) => Some(val), _ => None, } } #[inline] - pub fn expect_ipy_escape_command_expr(self) -> crate::ExprIpyEscapeCommand { + pub fn expect_dict_expr(self) -> crate::ExprDict { match self { - Self::IpyEscapeCommand(val) => val, + Self::Dict(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_ipy_escape_command_expr_mut(&mut self) -> Option<&mut crate::ExprIpyEscapeCommand> { + pub fn as_dict_expr_mut(&mut self) -> Option<&mut crate::ExprDict> { match self { - Self::IpyEscapeCommand(val) => Some(val), + Self::Dict(val) => Some(val), _ => None, } } #[inline] - pub fn as_ipy_escape_command_expr(&self) -> Option<&crate::ExprIpyEscapeCommand> { + pub fn as_dict_expr(&self) -> Option<&crate::ExprDict> { match self { - Self::IpyEscapeCommand(val) => Some(val), + Self::Dict(val) => Some(val), _ => None, } } @@ -3548,193 +3548,193 @@ impl ruff_text_size::Ranged for crate::StmtIpyEscapeCommand { } } -impl ruff_text_size::Ranged for crate::ExprBoolOp { +impl ruff_text_size::Ranged for crate::ExprSet { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprNamed { +impl ruff_text_size::Ranged for crate::ExprListComp { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprBinOp { +impl ruff_text_size::Ranged for crate::ExprSetComp { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprUnaryOp { +impl ruff_text_size::Ranged for crate::ExprDictComp { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprLambda { +impl ruff_text_size::Ranged for crate::ExprGenerator { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprIf { +impl ruff_text_size::Ranged for crate::ExprAwait { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprDict { +impl ruff_text_size::Ranged for crate::ExprYield { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprSet { +impl ruff_text_size::Ranged for crate::ExprYieldFrom { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprListComp { +impl ruff_text_size::Ranged for crate::ExprCompare { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprSetComp { +impl ruff_text_size::Ranged for crate::ExprCall { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprDictComp { +impl ruff_text_size::Ranged for crate::ExprFString { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprGenerator { +impl ruff_text_size::Ranged for crate::ExprStringLiteral { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprAwait { +impl ruff_text_size::Ranged for crate::ExprBytesLiteral { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprYield { +impl ruff_text_size::Ranged for crate::ExprNumberLiteral { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprYieldFrom { +impl ruff_text_size::Ranged for crate::ExprBooleanLiteral { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprCompare { +impl ruff_text_size::Ranged for crate::ExprNoneLiteral { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprCall { +impl ruff_text_size::Ranged for crate::ExprEllipsisLiteral { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprFString { +impl ruff_text_size::Ranged for crate::ExprAttribute { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprStringLiteral { +impl ruff_text_size::Ranged for crate::ExprSubscript { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprBytesLiteral { +impl ruff_text_size::Ranged for crate::ExprStarred { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprNumberLiteral { +impl ruff_text_size::Ranged for crate::ExprName { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprBooleanLiteral { +impl ruff_text_size::Ranged for crate::ExprList { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprNoneLiteral { +impl ruff_text_size::Ranged for crate::ExprTuple { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprEllipsisLiteral { +impl ruff_text_size::Ranged for crate::ExprSlice { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprAttribute { +impl ruff_text_size::Ranged for crate::ExprIpyEscapeCommand { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprSubscript { +impl ruff_text_size::Ranged for crate::ExprBoolOp { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprStarred { +impl ruff_text_size::Ranged for crate::ExprNamed { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprName { +impl ruff_text_size::Ranged for crate::ExprBinOp { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprList { +impl ruff_text_size::Ranged for crate::ExprUnaryOp { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprTuple { +impl ruff_text_size::Ranged for crate::ExprLambda { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprSlice { +impl ruff_text_size::Ranged for crate::ExprIf { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprIpyEscapeCommand { +impl ruff_text_size::Ranged for crate::ExprDict { fn range(&self) -> ruff_text_size::TextRange { self.range } @@ -3994,13 +3994,6 @@ impl Expr { V: crate::visitor::source_order::SourceOrderVisitor<'a> + ?Sized, { match self { - Expr::BoolOp(node) => node.visit_source_order(visitor), - Expr::Named(node) => node.visit_source_order(visitor), - Expr::BinOp(node) => node.visit_source_order(visitor), - Expr::UnaryOp(node) => node.visit_source_order(visitor), - Expr::Lambda(node) => node.visit_source_order(visitor), - Expr::If(node) => node.visit_source_order(visitor), - Expr::Dict(node) => node.visit_source_order(visitor), Expr::Set(node) => node.visit_source_order(visitor), Expr::ListComp(node) => node.visit_source_order(visitor), Expr::SetComp(node) => node.visit_source_order(visitor), @@ -4026,6 +4019,13 @@ impl Expr { Expr::Tuple(node) => node.visit_source_order(visitor), Expr::Slice(node) => node.visit_source_order(visitor), Expr::IpyEscapeCommand(node) => node.visit_source_order(visitor), + Expr::BoolOp(node) => node.visit_source_order(visitor), + Expr::Named(node) => node.visit_source_order(visitor), + Expr::BinOp(node) => node.visit_source_order(visitor), + Expr::UnaryOp(node) => node.visit_source_order(visitor), + Expr::Lambda(node) => node.visit_source_order(visitor), + Expr::If(node) => node.visit_source_order(visitor), + Expr::Dict(node) => node.visit_source_order(visitor), } } } @@ -4397,20 +4397,6 @@ impl ruff_text_size::Ranged for StmtRef<'_> { /// See also [expr](https://docs.python.org/3/library/ast.html#ast.expr) #[derive(Clone, Copy, Debug, PartialEq, is_macro::Is)] pub enum ExprRef<'a> { - #[is(name = "bool_op_expr")] - BoolOp(&'a crate::ExprBoolOp), - #[is(name = "named_expr")] - Named(&'a crate::ExprNamed), - #[is(name = "bin_op_expr")] - BinOp(&'a crate::ExprBinOp), - #[is(name = "unary_op_expr")] - UnaryOp(&'a crate::ExprUnaryOp), - #[is(name = "lambda_expr")] - Lambda(&'a crate::ExprLambda), - #[is(name = "if_expr")] - If(&'a crate::ExprIf), - #[is(name = "dict_expr")] - Dict(&'a crate::ExprDict), #[is(name = "set_expr")] Set(&'a crate::ExprSet), #[is(name = "list_comp_expr")] @@ -4461,18 +4447,25 @@ pub enum ExprRef<'a> { Slice(&'a crate::ExprSlice), #[is(name = "ipy_escape_command_expr")] IpyEscapeCommand(&'a crate::ExprIpyEscapeCommand), + #[is(name = "bool_op_expr")] + BoolOp(&'a crate::ExprBoolOp), + #[is(name = "named_expr")] + Named(&'a crate::ExprNamed), + #[is(name = "bin_op_expr")] + BinOp(&'a crate::ExprBinOp), + #[is(name = "unary_op_expr")] + UnaryOp(&'a crate::ExprUnaryOp), + #[is(name = "lambda_expr")] + Lambda(&'a crate::ExprLambda), + #[is(name = "if_expr")] + If(&'a crate::ExprIf), + #[is(name = "dict_expr")] + Dict(&'a crate::ExprDict), } impl<'a> From<&'a Expr> for ExprRef<'a> { fn from(node: &'a Expr) -> Self { match node { - Expr::BoolOp(node) => ExprRef::BoolOp(node), - Expr::Named(node) => ExprRef::Named(node), - Expr::BinOp(node) => ExprRef::BinOp(node), - Expr::UnaryOp(node) => ExprRef::UnaryOp(node), - Expr::Lambda(node) => ExprRef::Lambda(node), - Expr::If(node) => ExprRef::If(node), - Expr::Dict(node) => ExprRef::Dict(node), Expr::Set(node) => ExprRef::Set(node), Expr::ListComp(node) => ExprRef::ListComp(node), Expr::SetComp(node) => ExprRef::SetComp(node), @@ -4498,52 +4491,17 @@ impl<'a> From<&'a Expr> for ExprRef<'a> { Expr::Tuple(node) => ExprRef::Tuple(node), Expr::Slice(node) => ExprRef::Slice(node), Expr::IpyEscapeCommand(node) => ExprRef::IpyEscapeCommand(node), + Expr::BoolOp(node) => ExprRef::BoolOp(node), + Expr::Named(node) => ExprRef::Named(node), + Expr::BinOp(node) => ExprRef::BinOp(node), + Expr::UnaryOp(node) => ExprRef::UnaryOp(node), + Expr::Lambda(node) => ExprRef::Lambda(node), + Expr::If(node) => ExprRef::If(node), + Expr::Dict(node) => ExprRef::Dict(node), } } } -impl<'a> From<&'a crate::ExprBoolOp> for ExprRef<'a> { - fn from(node: &'a crate::ExprBoolOp) -> Self { - Self::BoolOp(node) - } -} - -impl<'a> From<&'a crate::ExprNamed> for ExprRef<'a> { - fn from(node: &'a crate::ExprNamed) -> Self { - Self::Named(node) - } -} - -impl<'a> From<&'a crate::ExprBinOp> for ExprRef<'a> { - fn from(node: &'a crate::ExprBinOp) -> Self { - Self::BinOp(node) - } -} - -impl<'a> From<&'a crate::ExprUnaryOp> for ExprRef<'a> { - fn from(node: &'a crate::ExprUnaryOp) -> Self { - Self::UnaryOp(node) - } -} - -impl<'a> From<&'a crate::ExprLambda> for ExprRef<'a> { - fn from(node: &'a crate::ExprLambda) -> Self { - Self::Lambda(node) - } -} - -impl<'a> From<&'a crate::ExprIf> for ExprRef<'a> { - fn from(node: &'a crate::ExprIf) -> Self { - Self::If(node) - } -} - -impl<'a> From<&'a crate::ExprDict> for ExprRef<'a> { - fn from(node: &'a crate::ExprDict) -> Self { - Self::Dict(node) - } -} - impl<'a> From<&'a crate::ExprSet> for ExprRef<'a> { fn from(node: &'a crate::ExprSet) -> Self { Self::Set(node) @@ -4682,28 +4640,63 @@ impl<'a> From<&'a crate::ExprTuple> for ExprRef<'a> { } } -impl<'a> From<&'a crate::ExprSlice> for ExprRef<'a> { - fn from(node: &'a crate::ExprSlice) -> Self { - Self::Slice(node) +impl<'a> From<&'a crate::ExprSlice> for ExprRef<'a> { + fn from(node: &'a crate::ExprSlice) -> Self { + Self::Slice(node) + } +} + +impl<'a> From<&'a crate::ExprIpyEscapeCommand> for ExprRef<'a> { + fn from(node: &'a crate::ExprIpyEscapeCommand) -> Self { + Self::IpyEscapeCommand(node) + } +} + +impl<'a> From<&'a crate::ExprBoolOp> for ExprRef<'a> { + fn from(node: &'a crate::ExprBoolOp) -> Self { + Self::BoolOp(node) + } +} + +impl<'a> From<&'a crate::ExprNamed> for ExprRef<'a> { + fn from(node: &'a crate::ExprNamed) -> Self { + Self::Named(node) + } +} + +impl<'a> From<&'a crate::ExprBinOp> for ExprRef<'a> { + fn from(node: &'a crate::ExprBinOp) -> Self { + Self::BinOp(node) + } +} + +impl<'a> From<&'a crate::ExprUnaryOp> for ExprRef<'a> { + fn from(node: &'a crate::ExprUnaryOp) -> Self { + Self::UnaryOp(node) + } +} + +impl<'a> From<&'a crate::ExprLambda> for ExprRef<'a> { + fn from(node: &'a crate::ExprLambda) -> Self { + Self::Lambda(node) + } +} + +impl<'a> From<&'a crate::ExprIf> for ExprRef<'a> { + fn from(node: &'a crate::ExprIf) -> Self { + Self::If(node) } } -impl<'a> From<&'a crate::ExprIpyEscapeCommand> for ExprRef<'a> { - fn from(node: &'a crate::ExprIpyEscapeCommand) -> Self { - Self::IpyEscapeCommand(node) +impl<'a> From<&'a crate::ExprDict> for ExprRef<'a> { + fn from(node: &'a crate::ExprDict) -> Self { + Self::Dict(node) } } impl ruff_text_size::Ranged for ExprRef<'_> { fn range(&self) -> ruff_text_size::TextRange { match self { - Self::BoolOp(node) => node.range(), - Self::Named(node) => node.range(), - Self::BinOp(node) => node.range(), - Self::UnaryOp(node) => node.range(), - Self::Lambda(node) => node.range(), - Self::If(node) => node.range(), - Self::Dict(node) => node.range(), Self::Set(node) => node.range(), Self::ListComp(node) => node.range(), Self::SetComp(node) => node.range(), @@ -4729,6 +4722,13 @@ impl ruff_text_size::Ranged for ExprRef<'_> { Self::Tuple(node) => node.range(), Self::Slice(node) => node.range(), Self::IpyEscapeCommand(node) => node.range(), + Self::BoolOp(node) => node.range(), + Self::Named(node) => node.range(), + Self::BinOp(node) => node.range(), + Self::UnaryOp(node) => node.range(), + Self::Lambda(node) => node.range(), + Self::If(node) => node.range(), + Self::Dict(node) => node.range(), } } } @@ -4963,13 +4963,6 @@ pub enum AnyNodeRef<'a> { StmtBreak(&'a crate::StmtBreak), StmtContinue(&'a crate::StmtContinue), StmtIpyEscapeCommand(&'a crate::StmtIpyEscapeCommand), - ExprBoolOp(&'a crate::ExprBoolOp), - ExprNamed(&'a crate::ExprNamed), - ExprBinOp(&'a crate::ExprBinOp), - ExprUnaryOp(&'a crate::ExprUnaryOp), - ExprLambda(&'a crate::ExprLambda), - ExprIf(&'a crate::ExprIf), - ExprDict(&'a crate::ExprDict), ExprSet(&'a crate::ExprSet), ExprListComp(&'a crate::ExprListComp), ExprSetComp(&'a crate::ExprSetComp), @@ -4995,6 +4988,13 @@ pub enum AnyNodeRef<'a> { ExprTuple(&'a crate::ExprTuple), ExprSlice(&'a crate::ExprSlice), ExprIpyEscapeCommand(&'a crate::ExprIpyEscapeCommand), + ExprBoolOp(&'a crate::ExprBoolOp), + ExprNamed(&'a crate::ExprNamed), + ExprBinOp(&'a crate::ExprBinOp), + ExprUnaryOp(&'a crate::ExprUnaryOp), + ExprLambda(&'a crate::ExprLambda), + ExprIf(&'a crate::ExprIf), + ExprDict(&'a crate::ExprDict), ExceptHandlerExceptHandler(&'a crate::ExceptHandlerExceptHandler), FStringExpressionElement(&'a crate::FStringExpressionElement), FStringLiteralElement(&'a crate::FStringLiteralElement), @@ -5115,13 +5115,6 @@ impl<'a> From> for AnyNodeRef<'a> { impl<'a> From<&'a Expr> for AnyNodeRef<'a> { fn from(node: &'a Expr) -> AnyNodeRef<'a> { match node { - Expr::BoolOp(node) => AnyNodeRef::ExprBoolOp(node), - Expr::Named(node) => AnyNodeRef::ExprNamed(node), - Expr::BinOp(node) => AnyNodeRef::ExprBinOp(node), - Expr::UnaryOp(node) => AnyNodeRef::ExprUnaryOp(node), - Expr::Lambda(node) => AnyNodeRef::ExprLambda(node), - Expr::If(node) => AnyNodeRef::ExprIf(node), - Expr::Dict(node) => AnyNodeRef::ExprDict(node), Expr::Set(node) => AnyNodeRef::ExprSet(node), Expr::ListComp(node) => AnyNodeRef::ExprListComp(node), Expr::SetComp(node) => AnyNodeRef::ExprSetComp(node), @@ -5147,6 +5140,13 @@ impl<'a> From<&'a Expr> for AnyNodeRef<'a> { Expr::Tuple(node) => AnyNodeRef::ExprTuple(node), Expr::Slice(node) => AnyNodeRef::ExprSlice(node), Expr::IpyEscapeCommand(node) => AnyNodeRef::ExprIpyEscapeCommand(node), + Expr::BoolOp(node) => AnyNodeRef::ExprBoolOp(node), + Expr::Named(node) => AnyNodeRef::ExprNamed(node), + Expr::BinOp(node) => AnyNodeRef::ExprBinOp(node), + Expr::UnaryOp(node) => AnyNodeRef::ExprUnaryOp(node), + Expr::Lambda(node) => AnyNodeRef::ExprLambda(node), + Expr::If(node) => AnyNodeRef::ExprIf(node), + Expr::Dict(node) => AnyNodeRef::ExprDict(node), } } } @@ -5154,13 +5154,6 @@ impl<'a> From<&'a Expr> for AnyNodeRef<'a> { impl<'a> From> for AnyNodeRef<'a> { fn from(node: ExprRef<'a>) -> AnyNodeRef<'a> { match node { - ExprRef::BoolOp(node) => AnyNodeRef::ExprBoolOp(node), - ExprRef::Named(node) => AnyNodeRef::ExprNamed(node), - ExprRef::BinOp(node) => AnyNodeRef::ExprBinOp(node), - ExprRef::UnaryOp(node) => AnyNodeRef::ExprUnaryOp(node), - ExprRef::Lambda(node) => AnyNodeRef::ExprLambda(node), - ExprRef::If(node) => AnyNodeRef::ExprIf(node), - ExprRef::Dict(node) => AnyNodeRef::ExprDict(node), ExprRef::Set(node) => AnyNodeRef::ExprSet(node), ExprRef::ListComp(node) => AnyNodeRef::ExprListComp(node), ExprRef::SetComp(node) => AnyNodeRef::ExprSetComp(node), @@ -5186,6 +5179,13 @@ impl<'a> From> for AnyNodeRef<'a> { ExprRef::Tuple(node) => AnyNodeRef::ExprTuple(node), ExprRef::Slice(node) => AnyNodeRef::ExprSlice(node), ExprRef::IpyEscapeCommand(node) => AnyNodeRef::ExprIpyEscapeCommand(node), + ExprRef::BoolOp(node) => AnyNodeRef::ExprBoolOp(node), + ExprRef::Named(node) => AnyNodeRef::ExprNamed(node), + ExprRef::BinOp(node) => AnyNodeRef::ExprBinOp(node), + ExprRef::UnaryOp(node) => AnyNodeRef::ExprUnaryOp(node), + ExprRef::Lambda(node) => AnyNodeRef::ExprLambda(node), + ExprRef::If(node) => AnyNodeRef::ExprIf(node), + ExprRef::Dict(node) => AnyNodeRef::ExprDict(node), } } } @@ -5436,48 +5436,6 @@ impl<'a> From<&'a crate::StmtIpyEscapeCommand> for AnyNodeRef<'a> { } } -impl<'a> From<&'a crate::ExprBoolOp> for AnyNodeRef<'a> { - fn from(node: &'a crate::ExprBoolOp) -> AnyNodeRef<'a> { - AnyNodeRef::ExprBoolOp(node) - } -} - -impl<'a> From<&'a crate::ExprNamed> for AnyNodeRef<'a> { - fn from(node: &'a crate::ExprNamed) -> AnyNodeRef<'a> { - AnyNodeRef::ExprNamed(node) - } -} - -impl<'a> From<&'a crate::ExprBinOp> for AnyNodeRef<'a> { - fn from(node: &'a crate::ExprBinOp) -> AnyNodeRef<'a> { - AnyNodeRef::ExprBinOp(node) - } -} - -impl<'a> From<&'a crate::ExprUnaryOp> for AnyNodeRef<'a> { - fn from(node: &'a crate::ExprUnaryOp) -> AnyNodeRef<'a> { - AnyNodeRef::ExprUnaryOp(node) - } -} - -impl<'a> From<&'a crate::ExprLambda> for AnyNodeRef<'a> { - fn from(node: &'a crate::ExprLambda) -> AnyNodeRef<'a> { - AnyNodeRef::ExprLambda(node) - } -} - -impl<'a> From<&'a crate::ExprIf> for AnyNodeRef<'a> { - fn from(node: &'a crate::ExprIf) -> AnyNodeRef<'a> { - AnyNodeRef::ExprIf(node) - } -} - -impl<'a> From<&'a crate::ExprDict> for AnyNodeRef<'a> { - fn from(node: &'a crate::ExprDict) -> AnyNodeRef<'a> { - AnyNodeRef::ExprDict(node) - } -} - impl<'a> From<&'a crate::ExprSet> for AnyNodeRef<'a> { fn from(node: &'a crate::ExprSet) -> AnyNodeRef<'a> { AnyNodeRef::ExprSet(node) @@ -5628,6 +5586,48 @@ impl<'a> From<&'a crate::ExprIpyEscapeCommand> for AnyNodeRef<'a> { } } +impl<'a> From<&'a crate::ExprBoolOp> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprBoolOp) -> AnyNodeRef<'a> { + AnyNodeRef::ExprBoolOp(node) + } +} + +impl<'a> From<&'a crate::ExprNamed> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprNamed) -> AnyNodeRef<'a> { + AnyNodeRef::ExprNamed(node) + } +} + +impl<'a> From<&'a crate::ExprBinOp> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprBinOp) -> AnyNodeRef<'a> { + AnyNodeRef::ExprBinOp(node) + } +} + +impl<'a> From<&'a crate::ExprUnaryOp> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprUnaryOp) -> AnyNodeRef<'a> { + AnyNodeRef::ExprUnaryOp(node) + } +} + +impl<'a> From<&'a crate::ExprLambda> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprLambda) -> AnyNodeRef<'a> { + AnyNodeRef::ExprLambda(node) + } +} + +impl<'a> From<&'a crate::ExprIf> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprIf) -> AnyNodeRef<'a> { + AnyNodeRef::ExprIf(node) + } +} + +impl<'a> From<&'a crate::ExprDict> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprDict) -> AnyNodeRef<'a> { + AnyNodeRef::ExprDict(node) + } +} + impl<'a> From<&'a crate::ExceptHandlerExceptHandler> for AnyNodeRef<'a> { fn from(node: &'a crate::ExceptHandlerExceptHandler) -> AnyNodeRef<'a> { AnyNodeRef::ExceptHandlerExceptHandler(node) @@ -5856,13 +5856,6 @@ impl ruff_text_size::Ranged for AnyNodeRef<'_> { AnyNodeRef::StmtBreak(node) => node.range(), AnyNodeRef::StmtContinue(node) => node.range(), AnyNodeRef::StmtIpyEscapeCommand(node) => node.range(), - AnyNodeRef::ExprBoolOp(node) => node.range(), - AnyNodeRef::ExprNamed(node) => node.range(), - AnyNodeRef::ExprBinOp(node) => node.range(), - AnyNodeRef::ExprUnaryOp(node) => node.range(), - AnyNodeRef::ExprLambda(node) => node.range(), - AnyNodeRef::ExprIf(node) => node.range(), - AnyNodeRef::ExprDict(node) => node.range(), AnyNodeRef::ExprSet(node) => node.range(), AnyNodeRef::ExprListComp(node) => node.range(), AnyNodeRef::ExprSetComp(node) => node.range(), @@ -5888,6 +5881,13 @@ impl ruff_text_size::Ranged for AnyNodeRef<'_> { AnyNodeRef::ExprTuple(node) => node.range(), AnyNodeRef::ExprSlice(node) => node.range(), AnyNodeRef::ExprIpyEscapeCommand(node) => node.range(), + AnyNodeRef::ExprBoolOp(node) => node.range(), + AnyNodeRef::ExprNamed(node) => node.range(), + AnyNodeRef::ExprBinOp(node) => node.range(), + AnyNodeRef::ExprUnaryOp(node) => node.range(), + AnyNodeRef::ExprLambda(node) => node.range(), + AnyNodeRef::ExprIf(node) => node.range(), + AnyNodeRef::ExprDict(node) => node.range(), AnyNodeRef::ExceptHandlerExceptHandler(node) => node.range(), AnyNodeRef::FStringExpressionElement(node) => node.range(), AnyNodeRef::FStringLiteralElement(node) => node.range(), @@ -5955,13 +5955,6 @@ impl AnyNodeRef<'_> { AnyNodeRef::StmtBreak(node) => std::ptr::NonNull::from(*node).cast(), AnyNodeRef::StmtContinue(node) => std::ptr::NonNull::from(*node).cast(), AnyNodeRef::StmtIpyEscapeCommand(node) => std::ptr::NonNull::from(*node).cast(), - AnyNodeRef::ExprBoolOp(node) => std::ptr::NonNull::from(*node).cast(), - AnyNodeRef::ExprNamed(node) => std::ptr::NonNull::from(*node).cast(), - AnyNodeRef::ExprBinOp(node) => std::ptr::NonNull::from(*node).cast(), - AnyNodeRef::ExprUnaryOp(node) => std::ptr::NonNull::from(*node).cast(), - AnyNodeRef::ExprLambda(node) => std::ptr::NonNull::from(*node).cast(), - AnyNodeRef::ExprIf(node) => std::ptr::NonNull::from(*node).cast(), - AnyNodeRef::ExprDict(node) => std::ptr::NonNull::from(*node).cast(), AnyNodeRef::ExprSet(node) => std::ptr::NonNull::from(*node).cast(), AnyNodeRef::ExprListComp(node) => std::ptr::NonNull::from(*node).cast(), AnyNodeRef::ExprSetComp(node) => std::ptr::NonNull::from(*node).cast(), @@ -5987,6 +5980,13 @@ impl AnyNodeRef<'_> { AnyNodeRef::ExprTuple(node) => std::ptr::NonNull::from(*node).cast(), AnyNodeRef::ExprSlice(node) => std::ptr::NonNull::from(*node).cast(), AnyNodeRef::ExprIpyEscapeCommand(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprBoolOp(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprNamed(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprBinOp(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprUnaryOp(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprLambda(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprIf(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprDict(node) => std::ptr::NonNull::from(*node).cast(), AnyNodeRef::ExceptHandlerExceptHandler(node) => std::ptr::NonNull::from(*node).cast(), AnyNodeRef::FStringExpressionElement(node) => std::ptr::NonNull::from(*node).cast(), AnyNodeRef::FStringLiteralElement(node) => std::ptr::NonNull::from(*node).cast(), @@ -6058,13 +6058,6 @@ impl<'a> AnyNodeRef<'a> { AnyNodeRef::StmtBreak(node) => node.visit_source_order(visitor), AnyNodeRef::StmtContinue(node) => node.visit_source_order(visitor), AnyNodeRef::StmtIpyEscapeCommand(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprBoolOp(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprNamed(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprBinOp(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprUnaryOp(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprLambda(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprIf(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprDict(node) => node.visit_source_order(visitor), AnyNodeRef::ExprSet(node) => node.visit_source_order(visitor), AnyNodeRef::ExprListComp(node) => node.visit_source_order(visitor), AnyNodeRef::ExprSetComp(node) => node.visit_source_order(visitor), @@ -6090,6 +6083,13 @@ impl<'a> AnyNodeRef<'a> { AnyNodeRef::ExprTuple(node) => node.visit_source_order(visitor), AnyNodeRef::ExprSlice(node) => node.visit_source_order(visitor), AnyNodeRef::ExprIpyEscapeCommand(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprBoolOp(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprNamed(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprBinOp(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprUnaryOp(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprLambda(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprIf(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprDict(node) => node.visit_source_order(visitor), AnyNodeRef::ExceptHandlerExceptHandler(node) => node.visit_source_order(visitor), AnyNodeRef::FStringExpressionElement(node) => node.visit_source_order(visitor), AnyNodeRef::FStringLiteralElement(node) => node.visit_source_order(visitor), @@ -6173,14 +6173,7 @@ impl AnyNodeRef<'_> { pub const fn is_expression(self) -> bool { matches!( self, - AnyNodeRef::ExprBoolOp(_) - | AnyNodeRef::ExprNamed(_) - | AnyNodeRef::ExprBinOp(_) - | AnyNodeRef::ExprUnaryOp(_) - | AnyNodeRef::ExprLambda(_) - | AnyNodeRef::ExprIf(_) - | AnyNodeRef::ExprDict(_) - | AnyNodeRef::ExprSet(_) + AnyNodeRef::ExprSet(_) | AnyNodeRef::ExprListComp(_) | AnyNodeRef::ExprSetComp(_) | AnyNodeRef::ExprDictComp(_) @@ -6205,6 +6198,13 @@ impl AnyNodeRef<'_> { | AnyNodeRef::ExprTuple(_) | AnyNodeRef::ExprSlice(_) | AnyNodeRef::ExprIpyEscapeCommand(_) + | AnyNodeRef::ExprBoolOp(_) + | AnyNodeRef::ExprNamed(_) + | AnyNodeRef::ExprBinOp(_) + | AnyNodeRef::ExprUnaryOp(_) + | AnyNodeRef::ExprLambda(_) + | AnyNodeRef::ExprIf(_) + | AnyNodeRef::ExprDict(_) ) } } @@ -6280,13 +6280,6 @@ pub enum NodeKind { StmtBreak, StmtContinue, StmtIpyEscapeCommand, - ExprBoolOp, - ExprNamed, - ExprBinOp, - ExprUnaryOp, - ExprLambda, - ExprIf, - ExprDict, ExprSet, ExprListComp, ExprSetComp, @@ -6312,6 +6305,13 @@ pub enum NodeKind { ExprTuple, ExprSlice, ExprIpyEscapeCommand, + ExprBoolOp, + ExprNamed, + ExprBinOp, + ExprUnaryOp, + ExprLambda, + ExprIf, + ExprDict, ExceptHandlerExceptHandler, FStringExpressionElement, FStringLiteralElement, @@ -6377,13 +6377,6 @@ impl AnyNodeRef<'_> { AnyNodeRef::StmtBreak(_) => NodeKind::StmtBreak, AnyNodeRef::StmtContinue(_) => NodeKind::StmtContinue, AnyNodeRef::StmtIpyEscapeCommand(_) => NodeKind::StmtIpyEscapeCommand, - AnyNodeRef::ExprBoolOp(_) => NodeKind::ExprBoolOp, - AnyNodeRef::ExprNamed(_) => NodeKind::ExprNamed, - AnyNodeRef::ExprBinOp(_) => NodeKind::ExprBinOp, - AnyNodeRef::ExprUnaryOp(_) => NodeKind::ExprUnaryOp, - AnyNodeRef::ExprLambda(_) => NodeKind::ExprLambda, - AnyNodeRef::ExprIf(_) => NodeKind::ExprIf, - AnyNodeRef::ExprDict(_) => NodeKind::ExprDict, AnyNodeRef::ExprSet(_) => NodeKind::ExprSet, AnyNodeRef::ExprListComp(_) => NodeKind::ExprListComp, AnyNodeRef::ExprSetComp(_) => NodeKind::ExprSetComp, @@ -6409,6 +6402,13 @@ impl AnyNodeRef<'_> { AnyNodeRef::ExprTuple(_) => NodeKind::ExprTuple, AnyNodeRef::ExprSlice(_) => NodeKind::ExprSlice, AnyNodeRef::ExprIpyEscapeCommand(_) => NodeKind::ExprIpyEscapeCommand, + AnyNodeRef::ExprBoolOp(_) => NodeKind::ExprBoolOp, + AnyNodeRef::ExprNamed(_) => NodeKind::ExprNamed, + AnyNodeRef::ExprBinOp(_) => NodeKind::ExprBinOp, + AnyNodeRef::ExprUnaryOp(_) => NodeKind::ExprUnaryOp, + AnyNodeRef::ExprLambda(_) => NodeKind::ExprLambda, + AnyNodeRef::ExprIf(_) => NodeKind::ExprIf, + AnyNodeRef::ExprDict(_) => NodeKind::ExprDict, AnyNodeRef::ExceptHandlerExceptHandler(_) => NodeKind::ExceptHandlerExceptHandler, AnyNodeRef::FStringExpressionElement(_) => NodeKind::FStringExpressionElement, AnyNodeRef::FStringLiteralElement(_) => NodeKind::FStringLiteralElement, @@ -6445,3 +6445,53 @@ impl AnyNodeRef<'_> { } } } + +#[derive(Clone, Debug, PartialEq)] +pub struct ExprBoolOp { + pub range: ruff_text_size::TextRange, + pub op: crate::BoolOp, + pub values: Vec, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct ExprNamed { + pub range: ruff_text_size::TextRange, + pub target: Box, + pub value: Box, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct ExprBinOp { + pub range: ruff_text_size::TextRange, + pub left: Box, + pub op: crate::Operator, + pub right: Box, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct ExprUnaryOp { + pub range: ruff_text_size::TextRange, + pub op: crate::UnaryOp, + pub operand: Box, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct ExprLambda { + pub range: ruff_text_size::TextRange, + pub parameters: Option>, + pub body: Box, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct ExprIf { + pub range: ruff_text_size::TextRange, + pub test: Box, + pub body: Box, + pub orelse: Box, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct ExprDict { + pub range: ruff_text_size::TextRange, + pub items: Vec, +} diff --git a/crates/ruff_python_ast/src/nodes.rs b/crates/ruff_python_ast/src/nodes.rs index 7463fe5068c624..32d6451f36a81d 100644 --- a/crates/ruff_python_ast/src/nodes.rs +++ b/crates/ruff_python_ast/src/nodes.rs @@ -397,55 +397,55 @@ pub struct ExprIpyEscapeCommand { pub value: Box, } -/// See also [BoolOp](https://docs.python.org/3/library/ast.html#ast.BoolOp) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprBoolOp { - pub range: TextRange, - pub op: BoolOp, - pub values: Vec, -} - -/// See also [NamedExpr](https://docs.python.org/3/library/ast.html#ast.NamedExpr) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprNamed { - pub range: TextRange, - pub target: Box, - pub value: Box, -} - -/// See also [BinOp](https://docs.python.org/3/library/ast.html#ast.BinOp) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprBinOp { - pub range: TextRange, - pub left: Box, - pub op: Operator, - pub right: Box, -} - -/// See also [UnaryOp](https://docs.python.org/3/library/ast.html#ast.UnaryOp) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprUnaryOp { - pub range: TextRange, - pub op: UnaryOp, - pub operand: Box, -} - -/// See also [Lambda](https://docs.python.org/3/library/ast.html#ast.Lambda) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprLambda { - pub range: TextRange, - pub parameters: Option>, - pub body: Box, -} - -/// See also [IfExp](https://docs.python.org/3/library/ast.html#ast.IfExp) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprIf { - pub range: TextRange, - pub test: Box, - pub body: Box, - pub orelse: Box, -} +// /// See also [BoolOp](https://docs.python.org/3/library/ast.html#ast.BoolOp) +// #[derive(Clone, Debug, PartialEq)] +// pub struct ExprBoolOp { +// pub range: TextRange, +// pub op: BoolOp, +// pub values: Vec, +// } + +// /// See also [NamedExpr](https://docs.python.org/3/library/ast.html#ast.NamedExpr) +// #[derive(Clone, Debug, PartialEq)] +// pub struct ExprNamed { +// pub range: TextRange, +// pub target: Box, +// pub value: Box, +// } + +// /// See also [BinOp](https://docs.python.org/3/library/ast.html#ast.BinOp) +// #[derive(Clone, Debug, PartialEq)] +// pub struct ExprBinOp { +// pub range: TextRange, +// pub left: Box, +// pub op: Operator, +// pub right: Box, +// } + +// /// See also [UnaryOp](https://docs.python.org/3/library/ast.html#ast.UnaryOp) +// #[derive(Clone, Debug, PartialEq)] +// pub struct ExprUnaryOp { +// pub range: TextRange, +// pub op: UnaryOp, +// pub operand: Box, +// } + +// /// See also [Lambda](https://docs.python.org/3/library/ast.html#ast.Lambda) +// #[derive(Clone, Debug, PartialEq)] +// pub struct ExprLambda { +// pub range: TextRange, +// pub parameters: Option>, +// pub body: Box, +// } + +// /// See also [IfExp](https://docs.python.org/3/library/ast.html#ast.IfExp) +// #[derive(Clone, Debug, PartialEq)] +// pub struct ExprIf { +// pub range: TextRange, +// pub test: Box, +// pub body: Box, +// pub orelse: Box, +// } /// Represents an item in a [dictionary literal display][1]. /// @@ -495,14 +495,14 @@ impl Ranged for DictItem { } } -/// See also [Dict](https://docs.python.org/3/library/ast.html#ast.Dict) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprDict { - pub range: TextRange, - pub items: Vec, -} +// /// See also [Dict](https://docs.python.org/3/library/ast.html#ast.Dict) +// #[derive(Clone, Debug, PartialEq)] +// pub struct ExprDict { +// pub range: TextRange, +// pub items: Vec, +// } -impl ExprDict { +impl crate::ExprDict { /// Returns an `Iterator` over the AST nodes representing the /// dictionary's keys. pub fn iter_keys(&self) -> DictKeyIterator { @@ -544,7 +544,7 @@ impl ExprDict { } } -impl<'a> IntoIterator for &'a ExprDict { +impl<'a> IntoIterator for &'a crate::ExprDict { type IntoIter = std::slice::Iter<'a, DictItem>; type Item = &'a DictItem; @@ -3641,25 +3641,25 @@ mod tests { assert_eq!(std::mem::size_of::(), 64); assert_eq!(std::mem::size_of::(), 56); assert_eq!(std::mem::size_of::(), 16); - assert_eq!(std::mem::size_of::(), 32); - assert_eq!(std::mem::size_of::(), 40); + assert_eq!(std::mem::size_of::(), 32); + assert_eq!(std::mem::size_of::(), 40); assert_eq!(std::mem::size_of::(), 12); assert_eq!(std::mem::size_of::(), 40); assert_eq!(std::mem::size_of::(), 56); assert_eq!(std::mem::size_of::(), 48); - assert_eq!(std::mem::size_of::(), 32); + assert_eq!(std::mem::size_of::(), 32); assert_eq!(std::mem::size_of::(), 48); assert_eq!(std::mem::size_of::(), 8); // 56 for Rustc < 1.76 assert!(matches!(std::mem::size_of::(), 48 | 56)); assert_eq!(std::mem::size_of::(), 48); - assert_eq!(std::mem::size_of::(), 32); + assert_eq!(std::mem::size_of::(), 32); assert_eq!(std::mem::size_of::(), 32); - assert_eq!(std::mem::size_of::(), 24); + assert_eq!(std::mem::size_of::(), 24); assert_eq!(std::mem::size_of::(), 40); assert_eq!(std::mem::size_of::(), 40); assert_eq!(std::mem::size_of::(), 40); - assert_eq!(std::mem::size_of::(), 24); + assert_eq!(std::mem::size_of::(), 24); assert_eq!(std::mem::size_of::(), 8); assert_eq!(std::mem::size_of::(), 32); assert_eq!(std::mem::size_of::(), 32); @@ -3669,7 +3669,7 @@ mod tests { assert_eq!(std::mem::size_of::(), 56); assert_eq!(std::mem::size_of::(), 32); assert_eq!(std::mem::size_of::(), 40); - assert_eq!(std::mem::size_of::(), 24); + assert_eq!(std::mem::size_of::(), 24); assert_eq!(std::mem::size_of::(), 16); assert_eq!(std::mem::size_of::(), 16); } diff --git a/crates/ruff_python_ast/src/relocate.rs b/crates/ruff_python_ast/src/relocate.rs index f4f327d0735a7f..51faf19d178d7b 100644 --- a/crates/ruff_python_ast/src/relocate.rs +++ b/crates/ruff_python_ast/src/relocate.rs @@ -1,7 +1,7 @@ use ruff_text_size::TextRange; use crate::visitor::transformer::{walk_expr, walk_keyword, Transformer}; -use crate::{nodes, Expr, Keyword}; +use crate::{Expr, Keyword}; /// Change an expression's location (recursively) to match a desired, fixed /// range. @@ -17,100 +17,100 @@ struct Relocator { impl Transformer for Relocator { fn visit_expr(&self, expr: &mut Expr) { match expr { - Expr::BoolOp(nodes::ExprBoolOp { range, .. }) => { + Expr::BoolOp(crate::ExprBoolOp { range, .. }) => { *range = self.range; } - Expr::Named(nodes::ExprNamed { range, .. }) => { + Expr::Named(crate::ExprNamed { range, .. }) => { *range = self.range; } - Expr::BinOp(nodes::ExprBinOp { range, .. }) => { + Expr::BinOp(crate::ExprBinOp { range, .. }) => { *range = self.range; } - Expr::UnaryOp(nodes::ExprUnaryOp { range, .. }) => { + Expr::UnaryOp(crate::ExprUnaryOp { range, .. }) => { *range = self.range; } - Expr::Lambda(nodes::ExprLambda { range, .. }) => { + Expr::Lambda(crate::ExprLambda { range, .. }) => { *range = self.range; } - Expr::If(nodes::ExprIf { range, .. }) => { + Expr::If(crate::ExprIf { range, .. }) => { *range = self.range; } - Expr::Dict(nodes::ExprDict { range, .. }) => { + Expr::Dict(crate::ExprDict { range, .. }) => { *range = self.range; } - Expr::Set(nodes::ExprSet { range, .. }) => { + Expr::Set(crate::ExprSet { range, .. }) => { *range = self.range; } - Expr::ListComp(nodes::ExprListComp { range, .. }) => { + Expr::ListComp(crate::ExprListComp { range, .. }) => { *range = self.range; } - Expr::SetComp(nodes::ExprSetComp { range, .. }) => { + Expr::SetComp(crate::ExprSetComp { range, .. }) => { *range = self.range; } - Expr::DictComp(nodes::ExprDictComp { range, .. }) => { + Expr::DictComp(crate::ExprDictComp { range, .. }) => { *range = self.range; } - Expr::Generator(nodes::ExprGenerator { range, .. }) => { + Expr::Generator(crate::ExprGenerator { range, .. }) => { *range = self.range; } - Expr::Await(nodes::ExprAwait { range, .. }) => { + Expr::Await(crate::ExprAwait { range, .. }) => { *range = self.range; } - Expr::Yield(nodes::ExprYield { range, .. }) => { + Expr::Yield(crate::ExprYield { range, .. }) => { *range = self.range; } - Expr::YieldFrom(nodes::ExprYieldFrom { range, .. }) => { + Expr::YieldFrom(crate::ExprYieldFrom { range, .. }) => { *range = self.range; } - Expr::Compare(nodes::ExprCompare { range, .. }) => { + Expr::Compare(crate::ExprCompare { range, .. }) => { *range = self.range; } - Expr::Call(nodes::ExprCall { range, .. }) => { + Expr::Call(crate::ExprCall { range, .. }) => { *range = self.range; } - Expr::FString(nodes::ExprFString { range, .. }) => { + Expr::FString(crate::ExprFString { range, .. }) => { *range = self.range; } - Expr::StringLiteral(nodes::ExprStringLiteral { range, .. }) => { + Expr::StringLiteral(crate::ExprStringLiteral { range, .. }) => { *range = self.range; } - Expr::BytesLiteral(nodes::ExprBytesLiteral { range, .. }) => { + Expr::BytesLiteral(crate::ExprBytesLiteral { range, .. }) => { *range = self.range; } - Expr::NumberLiteral(nodes::ExprNumberLiteral { range, .. }) => { + Expr::NumberLiteral(crate::ExprNumberLiteral { range, .. }) => { *range = self.range; } - Expr::BooleanLiteral(nodes::ExprBooleanLiteral { range, .. }) => { + Expr::BooleanLiteral(crate::ExprBooleanLiteral { range, .. }) => { *range = self.range; } - Expr::NoneLiteral(nodes::ExprNoneLiteral { range }) => { + Expr::NoneLiteral(crate::ExprNoneLiteral { range }) => { *range = self.range; } - Expr::EllipsisLiteral(nodes::ExprEllipsisLiteral { range }) => { + Expr::EllipsisLiteral(crate::ExprEllipsisLiteral { range }) => { *range = self.range; } - Expr::Attribute(nodes::ExprAttribute { range, .. }) => { + Expr::Attribute(crate::ExprAttribute { range, .. }) => { *range = self.range; } - Expr::Subscript(nodes::ExprSubscript { range, .. }) => { + Expr::Subscript(crate::ExprSubscript { range, .. }) => { *range = self.range; } - Expr::Starred(nodes::ExprStarred { range, .. }) => { + Expr::Starred(crate::ExprStarred { range, .. }) => { *range = self.range; } - Expr::Name(nodes::ExprName { range, .. }) => { + Expr::Name(crate::ExprName { range, .. }) => { *range = self.range; } - Expr::List(nodes::ExprList { range, .. }) => { + Expr::List(crate::ExprList { range, .. }) => { *range = self.range; } - Expr::Tuple(nodes::ExprTuple { range, .. }) => { + Expr::Tuple(crate::ExprTuple { range, .. }) => { *range = self.range; } - Expr::Slice(nodes::ExprSlice { range, .. }) => { + Expr::Slice(crate::ExprSlice { range, .. }) => { *range = self.range; } - Expr::IpyEscapeCommand(nodes::ExprIpyEscapeCommand { range, .. }) => { + Expr::IpyEscapeCommand(crate::ExprIpyEscapeCommand { range, .. }) => { *range = self.range; } } From 18f64ea7b09368e36c93c53dac12488a769d3490 Mon Sep 17 00:00:00 2001 From: Glyphack Date: Fri, 21 Feb 2025 23:00:30 +0100 Subject: [PATCH 2/8] Auto generate all expression AST nodes --- crates/ruff_python_ast/ast.toml | 121 +- crates/ruff_python_ast/generate.py | 62 +- crates/ruff_python_ast/src/generated.rs | 1375 +++++++++++++---------- crates/ruff_python_ast/src/name.rs | 23 +- crates/ruff_python_ast/src/nodes.rs | 391 +++---- crates/ruff_python_ast/src/relocate.rs | 65 +- 6 files changed, 1138 insertions(+), 899 deletions(-) diff --git a/crates/ruff_python_ast/ast.toml b/crates/ruff_python_ast/ast.toml index a36c7de9f9a525..b447165681fa2b 100644 --- a/crates/ruff_python_ast/ast.toml +++ b/crates/ruff_python_ast/ast.toml @@ -81,31 +81,6 @@ anynode_is_label = "expression" rustdoc = "/// See also [expr](https://docs.python.org/3/library/ast.html#ast.expr)" [Expr.nodes] -ExprSet = {} -ExprListComp = {} -ExprSetComp = {} -ExprDictComp = {} -ExprGenerator = {} -ExprAwait = {} -ExprYield = {} -ExprYieldFrom = {} -ExprCompare = {} -ExprCall = {} -ExprFString = {} -ExprStringLiteral = {} -ExprBytesLiteral = {} -ExprNumberLiteral = {} -ExprBooleanLiteral = {} -ExprNoneLiteral = {} -ExprEllipsisLiteral = {} -ExprAttribute = {} -ExprSubscript = {} -ExprStarred = {} -ExprName = {} -ExprList = {} -ExprTuple = {} -ExprSlice = {} -ExprIpyEscapeCommand = {} ExprBoolOp = { fields = [ { name = "op", type = "BoolOp" }, { name = "values", type = "Expr", seq = true } @@ -124,7 +99,7 @@ ExprUnaryOp = { fields = [ { name = "operand", type = "Expr" } ]} ExprLambda = { fields = [ - { name = "parameters", type = "Parameters", optional = true }, + { name = "parameters", type = "Box", optional = true }, { name = "body", type = "Expr" } ]} ExprIf = { fields = [ @@ -135,6 +110,100 @@ ExprIf = { fields = [ ExprDict = { fields = [ { name = "items", type = "DictItem", seq = true }, ]} +ExprSet = { fields = [ + { name = "elts", type = "Expr", seq = true } +]} +ExprListComp = { fields = [ + { name = "elt", type = "Expr" }, + { name = "generators", type = "Comprehension", seq = true } +]} +ExprSetComp = { fields = [ + { name = "elt", type = "Expr" }, + { name = "generators", type = "Comprehension", seq = true } +]} +ExprDictComp = { fields = [ + { name = "key", type = "Expr" }, + { name = "value", type = "Expr" }, + { name = "generators", type = "Comprehension", seq = true } +]} +ExprGenerator = { fields = [ + { name = "elt", type = "Expr" }, + { name = "generators", type = "Comprehension", seq = true }, + { name = "parenthesized", type = "bool" } +]} +ExprAwait = { fields = [ + { name = "value", type = "Expr" } +]} +ExprYield = { fields = [ + { name = "value", type = "Expr", optional = true } +]} +ExprYieldFrom = { fields = [ + { name = "value", type = "Expr" } +]} +ExprCompare = { fields = [ + { name = "left", type = "Expr" }, + # We don't want this field to be a vec hence the seq is not enabled. + { name = "ops", type = "Box<[crate::CmpOp]>"}, + { name = "comparators", type = "Box<[Expr]>"} +]} +ExprCall = { fields = [ + { name = "func", type = "Expr" }, + { name = "arguments", type = "Arguments"} +]} +ExprFString = { fields = [ + { name = "value", type = "FStringValue" } +]} +ExprStringLiteral = { fields = [ + { name = "value", type = "StringLiteralValue" } +]} +ExprBytesLiteral = { fields = [ + { name = "value", type = "BytesLiteralValue" } +]} +ExprNumberLiteral = { fields = [ + { name = "value", type = "Number" } +]} +ExprBooleanLiteral = { fields = [ + { name = "value", type = "bool" } +], derives = ["Default"]} +ExprNoneLiteral = { fields = [], derives = ["Default"]} +ExprEllipsisLiteral = { fields = [], derives = ["Default"]} +ExprAttribute = { fields = [ + { name = "value", type = "Expr" }, + { name = "attr", type = "Identifier" }, + { name = "ctx", type = "ExprContext" } +]} +ExprSubscript = { fields = [ + { name = "value", type = "Expr" }, + { name = "slice", type = "Expr" }, + { name = "ctx", type = "ExprContext" } +]} +ExprStarred = { fields = [ + { name = "value", type = "Expr" }, + { name = "ctx", type = "ExprContext" } +]} +ExprName = { fields = [ + { name = "id", type = "name::Name" }, + { name = "ctx", type = "ExprContext" } +]} +ExprList = { fields = [ + { name = "elts", type = "Expr", seq = true }, + { name = "ctx", type = "ExprContext" } +]} +ExprTuple = { fields = [ + { name = "elts", type = "Expr", seq = true }, + { name = "ctx", type = "ExprContext" }, + { name = "parenthesized", type = "bool" } +]} +ExprSlice = { fields = [ + { name = "lower", type = "Expr", optional = true }, + { name = "upper", type = "Expr", optional = true }, + { name = "step", type = "Expr", optional = true } +]} +ExprIpyEscapeCommand = { fields = [ + { name = "kind", type = "IpyEscapeKind" }, + { name = "value", type = "Box" } + +]} [ExceptHandler] rustdoc = "/// See also [excepthandler](https://docs.python.org/3/library/ast.html#ast.excepthandler)" diff --git a/crates/ruff_python_ast/generate.py b/crates/ruff_python_ast/generate.py index 6155f0297c4942..2864e06e553bf6 100644 --- a/crates/ruff_python_ast/generate.py +++ b/crates/ruff_python_ast/generate.py @@ -91,6 +91,7 @@ class Node: variant: str ty: str fields: list[Field] | None + derives: list[str] | None def __init__(self, group: Group, node_name: str, node: dict[str, Any]) -> None: self.name = node_name @@ -100,6 +101,7 @@ def __init__(self, group: Group, node_name: str, node: dict[str, Any]) -> None: fields = node.get("fields") if fields is not None: self.fields = [Field(f) for f in fields] + self.derives = node.get("derives", []) @dataclass @@ -570,43 +572,39 @@ def write_nodekind(out: list[str], ast: Ast) -> None: def write_node(out: list[str], ast: Ast) -> None: - write_node_list = [ - "ExprBoolOp", - "ExprNamed", - "ExprBinOp", - "ExprUnaryOp", - "ExprLambda", - "ExprIf", - "ExprDict", - ] group_names = [group.name for group in ast.groups] node_names = [node.name for node in ast.all_nodes] for group in ast.groups: for node in group.nodes: - if node.name not in write_node_list: + if node.fields is None: continue - if node.fields is not None: - out.append("#[derive(Clone, Debug, PartialEq)]") - name = node.name - out.append(f"pub struct {name} {{") - out.append("pub range: ruff_text_size::TextRange,") - for field in node.fields: - field_str = f"pub {field.name}: " - inner = f"crate::{field.ty}" - if field.ty in node_names or ( - field.ty in group_names and (field.seq is False) - ): - inner = f"Box<{inner}>" - - if field.seq: - field_str += f"Vec<{inner}>," - elif field.optional: - field_str += f"Option<{inner}>," - else: - field_str += f"{inner}," - out.append(field_str) - out.append("}") - out.append("") + out.append( + "#[derive(Clone, Debug, PartialEq, " + + ", ".join(node.derives or []) + + ")]" + ) + name = node.name + out.append(f"pub struct {name} {{") + out.append("pub range: ruff_text_size::TextRange,") + for field in node.fields: + field_str = f"pub {field.name}: " + inner = f"crate::{field.ty}" + if (field.ty == "Expr" or (field.ty in group_names)) and ( + field.seq is False + ): + inner = f"Box<{inner}>" + if field.ty == "bool" or field.ty.startswith("Box"): + inner = field.ty + + if field.seq: + field_str += f"Vec<{inner}>," + elif field.optional: + field_str += f"Option<{inner}>," + else: + field_str += f"{inner}," + out.append(field_str) + out.append("}") + out.append("") # ------------------------------------------------------------------------------ diff --git a/crates/ruff_python_ast/src/generated.rs b/crates/ruff_python_ast/src/generated.rs index 2ac593f62887f3..a4f5ba4ef1c205 100644 --- a/crates/ruff_python_ast/src/generated.rs +++ b/crates/ruff_python_ast/src/generated.rs @@ -1249,6 +1249,13 @@ impl Stmt { /// See also [expr](https://docs.python.org/3/library/ast.html#ast.expr) #[derive(Clone, Debug, PartialEq)] pub enum Expr { + BoolOp(crate::ExprBoolOp), + Named(crate::ExprNamed), + BinOp(crate::ExprBinOp), + UnaryOp(crate::ExprUnaryOp), + Lambda(crate::ExprLambda), + If(crate::ExprIf), + Dict(crate::ExprDict), Set(crate::ExprSet), ListComp(crate::ExprListComp), SetComp(crate::ExprSetComp), @@ -1274,13 +1281,48 @@ pub enum Expr { Tuple(crate::ExprTuple), Slice(crate::ExprSlice), IpyEscapeCommand(crate::ExprIpyEscapeCommand), - BoolOp(crate::ExprBoolOp), - Named(crate::ExprNamed), - BinOp(crate::ExprBinOp), - UnaryOp(crate::ExprUnaryOp), - Lambda(crate::ExprLambda), - If(crate::ExprIf), - Dict(crate::ExprDict), +} + +impl From for Expr { + fn from(node: crate::ExprBoolOp) -> Self { + Self::BoolOp(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprNamed) -> Self { + Self::Named(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprBinOp) -> Self { + Self::BinOp(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprUnaryOp) -> Self { + Self::UnaryOp(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprLambda) -> Self { + Self::Lambda(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprIf) -> Self { + Self::If(node) + } +} + +impl From for Expr { + fn from(node: crate::ExprDict) -> Self { + Self::Dict(node) + } } impl From for Expr { @@ -1433,51 +1475,16 @@ impl From for Expr { } } -impl From for Expr { - fn from(node: crate::ExprBoolOp) -> Self { - Self::BoolOp(node) - } -} - -impl From for Expr { - fn from(node: crate::ExprNamed) -> Self { - Self::Named(node) - } -} - -impl From for Expr { - fn from(node: crate::ExprBinOp) -> Self { - Self::BinOp(node) - } -} - -impl From for Expr { - fn from(node: crate::ExprUnaryOp) -> Self { - Self::UnaryOp(node) - } -} - -impl From for Expr { - fn from(node: crate::ExprLambda) -> Self { - Self::Lambda(node) - } -} - -impl From for Expr { - fn from(node: crate::ExprIf) -> Self { - Self::If(node) - } -} - -impl From for Expr { - fn from(node: crate::ExprDict) -> Self { - Self::Dict(node) - } -} - impl ruff_text_size::Ranged for Expr { fn range(&self) -> ruff_text_size::TextRange { match self { + Self::BoolOp(node) => node.range(), + Self::Named(node) => node.range(), + Self::BinOp(node) => node.range(), + Self::UnaryOp(node) => node.range(), + Self::Lambda(node) => node.range(), + Self::If(node) => node.range(), + Self::Dict(node) => node.range(), Self::Set(node) => node.range(), Self::ListComp(node) => node.range(), Self::SetComp(node) => node.range(), @@ -1503,13 +1510,6 @@ impl ruff_text_size::Ranged for Expr { Self::Tuple(node) => node.range(), Self::Slice(node) => node.range(), Self::IpyEscapeCommand(node) => node.range(), - Self::BoolOp(node) => node.range(), - Self::Named(node) => node.range(), - Self::BinOp(node) => node.range(), - Self::UnaryOp(node) => node.range(), - Self::Lambda(node) => node.range(), - Self::If(node) => node.range(), - Self::Dict(node) => node.range(), } } } @@ -1517,1185 +1517,1185 @@ impl ruff_text_size::Ranged for Expr { #[allow(dead_code, clippy::match_wildcard_for_single_variants)] impl Expr { #[inline] - pub const fn is_set_expr(&self) -> bool { - matches!(self, Self::Set(_)) + pub const fn is_bool_op_expr(&self) -> bool { + matches!(self, Self::BoolOp(_)) } #[inline] - pub fn set_expr(self) -> Option { + pub fn bool_op_expr(self) -> Option { match self { - Self::Set(val) => Some(val), + Self::BoolOp(val) => Some(val), _ => None, } } #[inline] - pub fn expect_set_expr(self) -> crate::ExprSet { + pub fn expect_bool_op_expr(self) -> crate::ExprBoolOp { match self { - Self::Set(val) => val, + Self::BoolOp(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_set_expr_mut(&mut self) -> Option<&mut crate::ExprSet> { + pub fn as_bool_op_expr_mut(&mut self) -> Option<&mut crate::ExprBoolOp> { match self { - Self::Set(val) => Some(val), + Self::BoolOp(val) => Some(val), _ => None, } } #[inline] - pub fn as_set_expr(&self) -> Option<&crate::ExprSet> { + pub fn as_bool_op_expr(&self) -> Option<&crate::ExprBoolOp> { match self { - Self::Set(val) => Some(val), + Self::BoolOp(val) => Some(val), _ => None, } } #[inline] - pub const fn is_list_comp_expr(&self) -> bool { - matches!(self, Self::ListComp(_)) + pub const fn is_named_expr(&self) -> bool { + matches!(self, Self::Named(_)) } #[inline] - pub fn list_comp_expr(self) -> Option { + pub fn named_expr(self) -> Option { match self { - Self::ListComp(val) => Some(val), + Self::Named(val) => Some(val), _ => None, } } #[inline] - pub fn expect_list_comp_expr(self) -> crate::ExprListComp { + pub fn expect_named_expr(self) -> crate::ExprNamed { match self { - Self::ListComp(val) => val, + Self::Named(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_list_comp_expr_mut(&mut self) -> Option<&mut crate::ExprListComp> { + pub fn as_named_expr_mut(&mut self) -> Option<&mut crate::ExprNamed> { match self { - Self::ListComp(val) => Some(val), + Self::Named(val) => Some(val), _ => None, } } #[inline] - pub fn as_list_comp_expr(&self) -> Option<&crate::ExprListComp> { + pub fn as_named_expr(&self) -> Option<&crate::ExprNamed> { match self { - Self::ListComp(val) => Some(val), + Self::Named(val) => Some(val), _ => None, } } #[inline] - pub const fn is_set_comp_expr(&self) -> bool { - matches!(self, Self::SetComp(_)) + pub const fn is_bin_op_expr(&self) -> bool { + matches!(self, Self::BinOp(_)) } #[inline] - pub fn set_comp_expr(self) -> Option { + pub fn bin_op_expr(self) -> Option { match self { - Self::SetComp(val) => Some(val), + Self::BinOp(val) => Some(val), _ => None, } } #[inline] - pub fn expect_set_comp_expr(self) -> crate::ExprSetComp { + pub fn expect_bin_op_expr(self) -> crate::ExprBinOp { match self { - Self::SetComp(val) => val, + Self::BinOp(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_set_comp_expr_mut(&mut self) -> Option<&mut crate::ExprSetComp> { + pub fn as_bin_op_expr_mut(&mut self) -> Option<&mut crate::ExprBinOp> { match self { - Self::SetComp(val) => Some(val), + Self::BinOp(val) => Some(val), _ => None, } } #[inline] - pub fn as_set_comp_expr(&self) -> Option<&crate::ExprSetComp> { + pub fn as_bin_op_expr(&self) -> Option<&crate::ExprBinOp> { match self { - Self::SetComp(val) => Some(val), + Self::BinOp(val) => Some(val), _ => None, } } #[inline] - pub const fn is_dict_comp_expr(&self) -> bool { - matches!(self, Self::DictComp(_)) + pub const fn is_unary_op_expr(&self) -> bool { + matches!(self, Self::UnaryOp(_)) } #[inline] - pub fn dict_comp_expr(self) -> Option { + pub fn unary_op_expr(self) -> Option { match self { - Self::DictComp(val) => Some(val), + Self::UnaryOp(val) => Some(val), _ => None, } } #[inline] - pub fn expect_dict_comp_expr(self) -> crate::ExprDictComp { + pub fn expect_unary_op_expr(self) -> crate::ExprUnaryOp { match self { - Self::DictComp(val) => val, + Self::UnaryOp(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_dict_comp_expr_mut(&mut self) -> Option<&mut crate::ExprDictComp> { + pub fn as_unary_op_expr_mut(&mut self) -> Option<&mut crate::ExprUnaryOp> { match self { - Self::DictComp(val) => Some(val), + Self::UnaryOp(val) => Some(val), _ => None, } } #[inline] - pub fn as_dict_comp_expr(&self) -> Option<&crate::ExprDictComp> { + pub fn as_unary_op_expr(&self) -> Option<&crate::ExprUnaryOp> { match self { - Self::DictComp(val) => Some(val), + Self::UnaryOp(val) => Some(val), _ => None, } } #[inline] - pub const fn is_generator_expr(&self) -> bool { - matches!(self, Self::Generator(_)) + pub const fn is_lambda_expr(&self) -> bool { + matches!(self, Self::Lambda(_)) } #[inline] - pub fn generator_expr(self) -> Option { + pub fn lambda_expr(self) -> Option { match self { - Self::Generator(val) => Some(val), + Self::Lambda(val) => Some(val), _ => None, } } #[inline] - pub fn expect_generator_expr(self) -> crate::ExprGenerator { + pub fn expect_lambda_expr(self) -> crate::ExprLambda { match self { - Self::Generator(val) => val, + Self::Lambda(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_generator_expr_mut(&mut self) -> Option<&mut crate::ExprGenerator> { + pub fn as_lambda_expr_mut(&mut self) -> Option<&mut crate::ExprLambda> { match self { - Self::Generator(val) => Some(val), + Self::Lambda(val) => Some(val), _ => None, } } #[inline] - pub fn as_generator_expr(&self) -> Option<&crate::ExprGenerator> { + pub fn as_lambda_expr(&self) -> Option<&crate::ExprLambda> { match self { - Self::Generator(val) => Some(val), + Self::Lambda(val) => Some(val), _ => None, } } #[inline] - pub const fn is_await_expr(&self) -> bool { - matches!(self, Self::Await(_)) + pub const fn is_if_expr(&self) -> bool { + matches!(self, Self::If(_)) } #[inline] - pub fn await_expr(self) -> Option { + pub fn if_expr(self) -> Option { match self { - Self::Await(val) => Some(val), + Self::If(val) => Some(val), _ => None, } } #[inline] - pub fn expect_await_expr(self) -> crate::ExprAwait { + pub fn expect_if_expr(self) -> crate::ExprIf { match self { - Self::Await(val) => val, + Self::If(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_await_expr_mut(&mut self) -> Option<&mut crate::ExprAwait> { + pub fn as_if_expr_mut(&mut self) -> Option<&mut crate::ExprIf> { match self { - Self::Await(val) => Some(val), + Self::If(val) => Some(val), _ => None, } } #[inline] - pub fn as_await_expr(&self) -> Option<&crate::ExprAwait> { + pub fn as_if_expr(&self) -> Option<&crate::ExprIf> { match self { - Self::Await(val) => Some(val), + Self::If(val) => Some(val), _ => None, } } #[inline] - pub const fn is_yield_expr(&self) -> bool { - matches!(self, Self::Yield(_)) + pub const fn is_dict_expr(&self) -> bool { + matches!(self, Self::Dict(_)) } #[inline] - pub fn yield_expr(self) -> Option { + pub fn dict_expr(self) -> Option { match self { - Self::Yield(val) => Some(val), + Self::Dict(val) => Some(val), _ => None, } } #[inline] - pub fn expect_yield_expr(self) -> crate::ExprYield { + pub fn expect_dict_expr(self) -> crate::ExprDict { match self { - Self::Yield(val) => val, + Self::Dict(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_yield_expr_mut(&mut self) -> Option<&mut crate::ExprYield> { + pub fn as_dict_expr_mut(&mut self) -> Option<&mut crate::ExprDict> { match self { - Self::Yield(val) => Some(val), + Self::Dict(val) => Some(val), _ => None, } } #[inline] - pub fn as_yield_expr(&self) -> Option<&crate::ExprYield> { + pub fn as_dict_expr(&self) -> Option<&crate::ExprDict> { match self { - Self::Yield(val) => Some(val), + Self::Dict(val) => Some(val), _ => None, } } #[inline] - pub const fn is_yield_from_expr(&self) -> bool { - matches!(self, Self::YieldFrom(_)) + pub const fn is_set_expr(&self) -> bool { + matches!(self, Self::Set(_)) } #[inline] - pub fn yield_from_expr(self) -> Option { + pub fn set_expr(self) -> Option { match self { - Self::YieldFrom(val) => Some(val), + Self::Set(val) => Some(val), _ => None, } } #[inline] - pub fn expect_yield_from_expr(self) -> crate::ExprYieldFrom { + pub fn expect_set_expr(self) -> crate::ExprSet { match self { - Self::YieldFrom(val) => val, + Self::Set(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_yield_from_expr_mut(&mut self) -> Option<&mut crate::ExprYieldFrom> { + pub fn as_set_expr_mut(&mut self) -> Option<&mut crate::ExprSet> { match self { - Self::YieldFrom(val) => Some(val), + Self::Set(val) => Some(val), _ => None, } } #[inline] - pub fn as_yield_from_expr(&self) -> Option<&crate::ExprYieldFrom> { + pub fn as_set_expr(&self) -> Option<&crate::ExprSet> { match self { - Self::YieldFrom(val) => Some(val), + Self::Set(val) => Some(val), _ => None, } } #[inline] - pub const fn is_compare_expr(&self) -> bool { - matches!(self, Self::Compare(_)) + pub const fn is_list_comp_expr(&self) -> bool { + matches!(self, Self::ListComp(_)) } #[inline] - pub fn compare_expr(self) -> Option { + pub fn list_comp_expr(self) -> Option { match self { - Self::Compare(val) => Some(val), + Self::ListComp(val) => Some(val), _ => None, } } #[inline] - pub fn expect_compare_expr(self) -> crate::ExprCompare { + pub fn expect_list_comp_expr(self) -> crate::ExprListComp { match self { - Self::Compare(val) => val, + Self::ListComp(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_compare_expr_mut(&mut self) -> Option<&mut crate::ExprCompare> { + pub fn as_list_comp_expr_mut(&mut self) -> Option<&mut crate::ExprListComp> { match self { - Self::Compare(val) => Some(val), + Self::ListComp(val) => Some(val), _ => None, } } #[inline] - pub fn as_compare_expr(&self) -> Option<&crate::ExprCompare> { + pub fn as_list_comp_expr(&self) -> Option<&crate::ExprListComp> { match self { - Self::Compare(val) => Some(val), + Self::ListComp(val) => Some(val), _ => None, } } #[inline] - pub const fn is_call_expr(&self) -> bool { - matches!(self, Self::Call(_)) + pub const fn is_set_comp_expr(&self) -> bool { + matches!(self, Self::SetComp(_)) } #[inline] - pub fn call_expr(self) -> Option { + pub fn set_comp_expr(self) -> Option { match self { - Self::Call(val) => Some(val), + Self::SetComp(val) => Some(val), _ => None, } } #[inline] - pub fn expect_call_expr(self) -> crate::ExprCall { + pub fn expect_set_comp_expr(self) -> crate::ExprSetComp { match self { - Self::Call(val) => val, + Self::SetComp(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_call_expr_mut(&mut self) -> Option<&mut crate::ExprCall> { + pub fn as_set_comp_expr_mut(&mut self) -> Option<&mut crate::ExprSetComp> { match self { - Self::Call(val) => Some(val), + Self::SetComp(val) => Some(val), _ => None, } } #[inline] - pub fn as_call_expr(&self) -> Option<&crate::ExprCall> { + pub fn as_set_comp_expr(&self) -> Option<&crate::ExprSetComp> { match self { - Self::Call(val) => Some(val), + Self::SetComp(val) => Some(val), _ => None, } } #[inline] - pub const fn is_f_string_expr(&self) -> bool { - matches!(self, Self::FString(_)) + pub const fn is_dict_comp_expr(&self) -> bool { + matches!(self, Self::DictComp(_)) } #[inline] - pub fn f_string_expr(self) -> Option { + pub fn dict_comp_expr(self) -> Option { match self { - Self::FString(val) => Some(val), + Self::DictComp(val) => Some(val), _ => None, } } #[inline] - pub fn expect_f_string_expr(self) -> crate::ExprFString { + pub fn expect_dict_comp_expr(self) -> crate::ExprDictComp { match self { - Self::FString(val) => val, + Self::DictComp(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_f_string_expr_mut(&mut self) -> Option<&mut crate::ExprFString> { + pub fn as_dict_comp_expr_mut(&mut self) -> Option<&mut crate::ExprDictComp> { match self { - Self::FString(val) => Some(val), + Self::DictComp(val) => Some(val), _ => None, } } #[inline] - pub fn as_f_string_expr(&self) -> Option<&crate::ExprFString> { + pub fn as_dict_comp_expr(&self) -> Option<&crate::ExprDictComp> { match self { - Self::FString(val) => Some(val), + Self::DictComp(val) => Some(val), _ => None, } } #[inline] - pub const fn is_string_literal_expr(&self) -> bool { - matches!(self, Self::StringLiteral(_)) + pub const fn is_generator_expr(&self) -> bool { + matches!(self, Self::Generator(_)) } #[inline] - pub fn string_literal_expr(self) -> Option { + pub fn generator_expr(self) -> Option { match self { - Self::StringLiteral(val) => Some(val), + Self::Generator(val) => Some(val), _ => None, } } #[inline] - pub fn expect_string_literal_expr(self) -> crate::ExprStringLiteral { + pub fn expect_generator_expr(self) -> crate::ExprGenerator { match self { - Self::StringLiteral(val) => val, + Self::Generator(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_string_literal_expr_mut(&mut self) -> Option<&mut crate::ExprStringLiteral> { + pub fn as_generator_expr_mut(&mut self) -> Option<&mut crate::ExprGenerator> { match self { - Self::StringLiteral(val) => Some(val), + Self::Generator(val) => Some(val), _ => None, } } #[inline] - pub fn as_string_literal_expr(&self) -> Option<&crate::ExprStringLiteral> { + pub fn as_generator_expr(&self) -> Option<&crate::ExprGenerator> { match self { - Self::StringLiteral(val) => Some(val), + Self::Generator(val) => Some(val), _ => None, } } #[inline] - pub const fn is_bytes_literal_expr(&self) -> bool { - matches!(self, Self::BytesLiteral(_)) + pub const fn is_await_expr(&self) -> bool { + matches!(self, Self::Await(_)) } #[inline] - pub fn bytes_literal_expr(self) -> Option { + pub fn await_expr(self) -> Option { match self { - Self::BytesLiteral(val) => Some(val), + Self::Await(val) => Some(val), _ => None, } } #[inline] - pub fn expect_bytes_literal_expr(self) -> crate::ExprBytesLiteral { + pub fn expect_await_expr(self) -> crate::ExprAwait { match self { - Self::BytesLiteral(val) => val, + Self::Await(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_bytes_literal_expr_mut(&mut self) -> Option<&mut crate::ExprBytesLiteral> { + pub fn as_await_expr_mut(&mut self) -> Option<&mut crate::ExprAwait> { match self { - Self::BytesLiteral(val) => Some(val), + Self::Await(val) => Some(val), _ => None, } } #[inline] - pub fn as_bytes_literal_expr(&self) -> Option<&crate::ExprBytesLiteral> { + pub fn as_await_expr(&self) -> Option<&crate::ExprAwait> { match self { - Self::BytesLiteral(val) => Some(val), + Self::Await(val) => Some(val), _ => None, } } #[inline] - pub const fn is_number_literal_expr(&self) -> bool { - matches!(self, Self::NumberLiteral(_)) + pub const fn is_yield_expr(&self) -> bool { + matches!(self, Self::Yield(_)) } #[inline] - pub fn number_literal_expr(self) -> Option { + pub fn yield_expr(self) -> Option { match self { - Self::NumberLiteral(val) => Some(val), + Self::Yield(val) => Some(val), _ => None, } } #[inline] - pub fn expect_number_literal_expr(self) -> crate::ExprNumberLiteral { + pub fn expect_yield_expr(self) -> crate::ExprYield { match self { - Self::NumberLiteral(val) => val, + Self::Yield(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_number_literal_expr_mut(&mut self) -> Option<&mut crate::ExprNumberLiteral> { + pub fn as_yield_expr_mut(&mut self) -> Option<&mut crate::ExprYield> { match self { - Self::NumberLiteral(val) => Some(val), + Self::Yield(val) => Some(val), _ => None, } } #[inline] - pub fn as_number_literal_expr(&self) -> Option<&crate::ExprNumberLiteral> { + pub fn as_yield_expr(&self) -> Option<&crate::ExprYield> { match self { - Self::NumberLiteral(val) => Some(val), + Self::Yield(val) => Some(val), _ => None, } } #[inline] - pub const fn is_boolean_literal_expr(&self) -> bool { - matches!(self, Self::BooleanLiteral(_)) + pub const fn is_yield_from_expr(&self) -> bool { + matches!(self, Self::YieldFrom(_)) } #[inline] - pub fn boolean_literal_expr(self) -> Option { + pub fn yield_from_expr(self) -> Option { match self { - Self::BooleanLiteral(val) => Some(val), + Self::YieldFrom(val) => Some(val), _ => None, } } #[inline] - pub fn expect_boolean_literal_expr(self) -> crate::ExprBooleanLiteral { + pub fn expect_yield_from_expr(self) -> crate::ExprYieldFrom { match self { - Self::BooleanLiteral(val) => val, + Self::YieldFrom(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_boolean_literal_expr_mut(&mut self) -> Option<&mut crate::ExprBooleanLiteral> { + pub fn as_yield_from_expr_mut(&mut self) -> Option<&mut crate::ExprYieldFrom> { match self { - Self::BooleanLiteral(val) => Some(val), + Self::YieldFrom(val) => Some(val), _ => None, } } #[inline] - pub fn as_boolean_literal_expr(&self) -> Option<&crate::ExprBooleanLiteral> { + pub fn as_yield_from_expr(&self) -> Option<&crate::ExprYieldFrom> { match self { - Self::BooleanLiteral(val) => Some(val), + Self::YieldFrom(val) => Some(val), _ => None, } } #[inline] - pub const fn is_none_literal_expr(&self) -> bool { - matches!(self, Self::NoneLiteral(_)) + pub const fn is_compare_expr(&self) -> bool { + matches!(self, Self::Compare(_)) } #[inline] - pub fn none_literal_expr(self) -> Option { + pub fn compare_expr(self) -> Option { match self { - Self::NoneLiteral(val) => Some(val), + Self::Compare(val) => Some(val), _ => None, } } #[inline] - pub fn expect_none_literal_expr(self) -> crate::ExprNoneLiteral { + pub fn expect_compare_expr(self) -> crate::ExprCompare { match self { - Self::NoneLiteral(val) => val, + Self::Compare(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_none_literal_expr_mut(&mut self) -> Option<&mut crate::ExprNoneLiteral> { + pub fn as_compare_expr_mut(&mut self) -> Option<&mut crate::ExprCompare> { match self { - Self::NoneLiteral(val) => Some(val), + Self::Compare(val) => Some(val), _ => None, } } #[inline] - pub fn as_none_literal_expr(&self) -> Option<&crate::ExprNoneLiteral> { + pub fn as_compare_expr(&self) -> Option<&crate::ExprCompare> { match self { - Self::NoneLiteral(val) => Some(val), + Self::Compare(val) => Some(val), _ => None, } } #[inline] - pub const fn is_ellipsis_literal_expr(&self) -> bool { - matches!(self, Self::EllipsisLiteral(_)) + pub const fn is_call_expr(&self) -> bool { + matches!(self, Self::Call(_)) } #[inline] - pub fn ellipsis_literal_expr(self) -> Option { + pub fn call_expr(self) -> Option { match self { - Self::EllipsisLiteral(val) => Some(val), + Self::Call(val) => Some(val), _ => None, } } #[inline] - pub fn expect_ellipsis_literal_expr(self) -> crate::ExprEllipsisLiteral { + pub fn expect_call_expr(self) -> crate::ExprCall { match self { - Self::EllipsisLiteral(val) => val, + Self::Call(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_ellipsis_literal_expr_mut(&mut self) -> Option<&mut crate::ExprEllipsisLiteral> { + pub fn as_call_expr_mut(&mut self) -> Option<&mut crate::ExprCall> { match self { - Self::EllipsisLiteral(val) => Some(val), + Self::Call(val) => Some(val), _ => None, } } #[inline] - pub fn as_ellipsis_literal_expr(&self) -> Option<&crate::ExprEllipsisLiteral> { + pub fn as_call_expr(&self) -> Option<&crate::ExprCall> { match self { - Self::EllipsisLiteral(val) => Some(val), + Self::Call(val) => Some(val), _ => None, } } #[inline] - pub const fn is_attribute_expr(&self) -> bool { - matches!(self, Self::Attribute(_)) + pub const fn is_f_string_expr(&self) -> bool { + matches!(self, Self::FString(_)) } #[inline] - pub fn attribute_expr(self) -> Option { + pub fn f_string_expr(self) -> Option { match self { - Self::Attribute(val) => Some(val), + Self::FString(val) => Some(val), _ => None, } } #[inline] - pub fn expect_attribute_expr(self) -> crate::ExprAttribute { + pub fn expect_f_string_expr(self) -> crate::ExprFString { match self { - Self::Attribute(val) => val, + Self::FString(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_attribute_expr_mut(&mut self) -> Option<&mut crate::ExprAttribute> { + pub fn as_f_string_expr_mut(&mut self) -> Option<&mut crate::ExprFString> { match self { - Self::Attribute(val) => Some(val), + Self::FString(val) => Some(val), _ => None, } } #[inline] - pub fn as_attribute_expr(&self) -> Option<&crate::ExprAttribute> { + pub fn as_f_string_expr(&self) -> Option<&crate::ExprFString> { match self { - Self::Attribute(val) => Some(val), + Self::FString(val) => Some(val), _ => None, } } #[inline] - pub const fn is_subscript_expr(&self) -> bool { - matches!(self, Self::Subscript(_)) + pub const fn is_string_literal_expr(&self) -> bool { + matches!(self, Self::StringLiteral(_)) } #[inline] - pub fn subscript_expr(self) -> Option { + pub fn string_literal_expr(self) -> Option { match self { - Self::Subscript(val) => Some(val), + Self::StringLiteral(val) => Some(val), _ => None, } } #[inline] - pub fn expect_subscript_expr(self) -> crate::ExprSubscript { + pub fn expect_string_literal_expr(self) -> crate::ExprStringLiteral { match self { - Self::Subscript(val) => val, + Self::StringLiteral(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_subscript_expr_mut(&mut self) -> Option<&mut crate::ExprSubscript> { + pub fn as_string_literal_expr_mut(&mut self) -> Option<&mut crate::ExprStringLiteral> { match self { - Self::Subscript(val) => Some(val), + Self::StringLiteral(val) => Some(val), _ => None, } } #[inline] - pub fn as_subscript_expr(&self) -> Option<&crate::ExprSubscript> { + pub fn as_string_literal_expr(&self) -> Option<&crate::ExprStringLiteral> { match self { - Self::Subscript(val) => Some(val), + Self::StringLiteral(val) => Some(val), _ => None, } } #[inline] - pub const fn is_starred_expr(&self) -> bool { - matches!(self, Self::Starred(_)) + pub const fn is_bytes_literal_expr(&self) -> bool { + matches!(self, Self::BytesLiteral(_)) } #[inline] - pub fn starred_expr(self) -> Option { + pub fn bytes_literal_expr(self) -> Option { match self { - Self::Starred(val) => Some(val), + Self::BytesLiteral(val) => Some(val), _ => None, } } #[inline] - pub fn expect_starred_expr(self) -> crate::ExprStarred { + pub fn expect_bytes_literal_expr(self) -> crate::ExprBytesLiteral { match self { - Self::Starred(val) => val, + Self::BytesLiteral(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_starred_expr_mut(&mut self) -> Option<&mut crate::ExprStarred> { + pub fn as_bytes_literal_expr_mut(&mut self) -> Option<&mut crate::ExprBytesLiteral> { match self { - Self::Starred(val) => Some(val), + Self::BytesLiteral(val) => Some(val), _ => None, } } #[inline] - pub fn as_starred_expr(&self) -> Option<&crate::ExprStarred> { + pub fn as_bytes_literal_expr(&self) -> Option<&crate::ExprBytesLiteral> { match self { - Self::Starred(val) => Some(val), + Self::BytesLiteral(val) => Some(val), _ => None, } } #[inline] - pub const fn is_name_expr(&self) -> bool { - matches!(self, Self::Name(_)) + pub const fn is_number_literal_expr(&self) -> bool { + matches!(self, Self::NumberLiteral(_)) } #[inline] - pub fn name_expr(self) -> Option { + pub fn number_literal_expr(self) -> Option { match self { - Self::Name(val) => Some(val), + Self::NumberLiteral(val) => Some(val), _ => None, } } #[inline] - pub fn expect_name_expr(self) -> crate::ExprName { + pub fn expect_number_literal_expr(self) -> crate::ExprNumberLiteral { match self { - Self::Name(val) => val, + Self::NumberLiteral(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_name_expr_mut(&mut self) -> Option<&mut crate::ExprName> { + pub fn as_number_literal_expr_mut(&mut self) -> Option<&mut crate::ExprNumberLiteral> { match self { - Self::Name(val) => Some(val), + Self::NumberLiteral(val) => Some(val), _ => None, } } #[inline] - pub fn as_name_expr(&self) -> Option<&crate::ExprName> { + pub fn as_number_literal_expr(&self) -> Option<&crate::ExprNumberLiteral> { match self { - Self::Name(val) => Some(val), + Self::NumberLiteral(val) => Some(val), _ => None, } } #[inline] - pub const fn is_list_expr(&self) -> bool { - matches!(self, Self::List(_)) + pub const fn is_boolean_literal_expr(&self) -> bool { + matches!(self, Self::BooleanLiteral(_)) } #[inline] - pub fn list_expr(self) -> Option { + pub fn boolean_literal_expr(self) -> Option { match self { - Self::List(val) => Some(val), + Self::BooleanLiteral(val) => Some(val), _ => None, } } #[inline] - pub fn expect_list_expr(self) -> crate::ExprList { + pub fn expect_boolean_literal_expr(self) -> crate::ExprBooleanLiteral { match self { - Self::List(val) => val, + Self::BooleanLiteral(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_list_expr_mut(&mut self) -> Option<&mut crate::ExprList> { + pub fn as_boolean_literal_expr_mut(&mut self) -> Option<&mut crate::ExprBooleanLiteral> { match self { - Self::List(val) => Some(val), + Self::BooleanLiteral(val) => Some(val), _ => None, } } #[inline] - pub fn as_list_expr(&self) -> Option<&crate::ExprList> { + pub fn as_boolean_literal_expr(&self) -> Option<&crate::ExprBooleanLiteral> { match self { - Self::List(val) => Some(val), + Self::BooleanLiteral(val) => Some(val), _ => None, } } #[inline] - pub const fn is_tuple_expr(&self) -> bool { - matches!(self, Self::Tuple(_)) + pub const fn is_none_literal_expr(&self) -> bool { + matches!(self, Self::NoneLiteral(_)) } #[inline] - pub fn tuple_expr(self) -> Option { + pub fn none_literal_expr(self) -> Option { match self { - Self::Tuple(val) => Some(val), + Self::NoneLiteral(val) => Some(val), _ => None, } } #[inline] - pub fn expect_tuple_expr(self) -> crate::ExprTuple { + pub fn expect_none_literal_expr(self) -> crate::ExprNoneLiteral { match self { - Self::Tuple(val) => val, + Self::NoneLiteral(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_tuple_expr_mut(&mut self) -> Option<&mut crate::ExprTuple> { + pub fn as_none_literal_expr_mut(&mut self) -> Option<&mut crate::ExprNoneLiteral> { match self { - Self::Tuple(val) => Some(val), + Self::NoneLiteral(val) => Some(val), _ => None, } } #[inline] - pub fn as_tuple_expr(&self) -> Option<&crate::ExprTuple> { + pub fn as_none_literal_expr(&self) -> Option<&crate::ExprNoneLiteral> { match self { - Self::Tuple(val) => Some(val), + Self::NoneLiteral(val) => Some(val), _ => None, } } #[inline] - pub const fn is_slice_expr(&self) -> bool { - matches!(self, Self::Slice(_)) + pub const fn is_ellipsis_literal_expr(&self) -> bool { + matches!(self, Self::EllipsisLiteral(_)) } #[inline] - pub fn slice_expr(self) -> Option { + pub fn ellipsis_literal_expr(self) -> Option { match self { - Self::Slice(val) => Some(val), + Self::EllipsisLiteral(val) => Some(val), _ => None, } } #[inline] - pub fn expect_slice_expr(self) -> crate::ExprSlice { + pub fn expect_ellipsis_literal_expr(self) -> crate::ExprEllipsisLiteral { match self { - Self::Slice(val) => val, + Self::EllipsisLiteral(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_slice_expr_mut(&mut self) -> Option<&mut crate::ExprSlice> { + pub fn as_ellipsis_literal_expr_mut(&mut self) -> Option<&mut crate::ExprEllipsisLiteral> { match self { - Self::Slice(val) => Some(val), + Self::EllipsisLiteral(val) => Some(val), _ => None, } } #[inline] - pub fn as_slice_expr(&self) -> Option<&crate::ExprSlice> { + pub fn as_ellipsis_literal_expr(&self) -> Option<&crate::ExprEllipsisLiteral> { match self { - Self::Slice(val) => Some(val), + Self::EllipsisLiteral(val) => Some(val), _ => None, } } #[inline] - pub const fn is_ipy_escape_command_expr(&self) -> bool { - matches!(self, Self::IpyEscapeCommand(_)) + pub const fn is_attribute_expr(&self) -> bool { + matches!(self, Self::Attribute(_)) } #[inline] - pub fn ipy_escape_command_expr(self) -> Option { + pub fn attribute_expr(self) -> Option { match self { - Self::IpyEscapeCommand(val) => Some(val), + Self::Attribute(val) => Some(val), _ => None, } } #[inline] - pub fn expect_ipy_escape_command_expr(self) -> crate::ExprIpyEscapeCommand { + pub fn expect_attribute_expr(self) -> crate::ExprAttribute { match self { - Self::IpyEscapeCommand(val) => val, + Self::Attribute(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_ipy_escape_command_expr_mut(&mut self) -> Option<&mut crate::ExprIpyEscapeCommand> { + pub fn as_attribute_expr_mut(&mut self) -> Option<&mut crate::ExprAttribute> { match self { - Self::IpyEscapeCommand(val) => Some(val), + Self::Attribute(val) => Some(val), _ => None, } } #[inline] - pub fn as_ipy_escape_command_expr(&self) -> Option<&crate::ExprIpyEscapeCommand> { + pub fn as_attribute_expr(&self) -> Option<&crate::ExprAttribute> { match self { - Self::IpyEscapeCommand(val) => Some(val), + Self::Attribute(val) => Some(val), _ => None, } } #[inline] - pub const fn is_bool_op_expr(&self) -> bool { - matches!(self, Self::BoolOp(_)) + pub const fn is_subscript_expr(&self) -> bool { + matches!(self, Self::Subscript(_)) } #[inline] - pub fn bool_op_expr(self) -> Option { + pub fn subscript_expr(self) -> Option { match self { - Self::BoolOp(val) => Some(val), + Self::Subscript(val) => Some(val), _ => None, } } #[inline] - pub fn expect_bool_op_expr(self) -> crate::ExprBoolOp { + pub fn expect_subscript_expr(self) -> crate::ExprSubscript { match self { - Self::BoolOp(val) => val, + Self::Subscript(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_bool_op_expr_mut(&mut self) -> Option<&mut crate::ExprBoolOp> { + pub fn as_subscript_expr_mut(&mut self) -> Option<&mut crate::ExprSubscript> { match self { - Self::BoolOp(val) => Some(val), + Self::Subscript(val) => Some(val), _ => None, } } #[inline] - pub fn as_bool_op_expr(&self) -> Option<&crate::ExprBoolOp> { + pub fn as_subscript_expr(&self) -> Option<&crate::ExprSubscript> { match self { - Self::BoolOp(val) => Some(val), + Self::Subscript(val) => Some(val), _ => None, } } #[inline] - pub const fn is_named_expr(&self) -> bool { - matches!(self, Self::Named(_)) + pub const fn is_starred_expr(&self) -> bool { + matches!(self, Self::Starred(_)) } #[inline] - pub fn named_expr(self) -> Option { + pub fn starred_expr(self) -> Option { match self { - Self::Named(val) => Some(val), + Self::Starred(val) => Some(val), _ => None, } } #[inline] - pub fn expect_named_expr(self) -> crate::ExprNamed { + pub fn expect_starred_expr(self) -> crate::ExprStarred { match self { - Self::Named(val) => val, + Self::Starred(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_named_expr_mut(&mut self) -> Option<&mut crate::ExprNamed> { + pub fn as_starred_expr_mut(&mut self) -> Option<&mut crate::ExprStarred> { match self { - Self::Named(val) => Some(val), + Self::Starred(val) => Some(val), _ => None, } } #[inline] - pub fn as_named_expr(&self) -> Option<&crate::ExprNamed> { + pub fn as_starred_expr(&self) -> Option<&crate::ExprStarred> { match self { - Self::Named(val) => Some(val), + Self::Starred(val) => Some(val), _ => None, } } #[inline] - pub const fn is_bin_op_expr(&self) -> bool { - matches!(self, Self::BinOp(_)) + pub const fn is_name_expr(&self) -> bool { + matches!(self, Self::Name(_)) } #[inline] - pub fn bin_op_expr(self) -> Option { + pub fn name_expr(self) -> Option { match self { - Self::BinOp(val) => Some(val), + Self::Name(val) => Some(val), _ => None, } } #[inline] - pub fn expect_bin_op_expr(self) -> crate::ExprBinOp { + pub fn expect_name_expr(self) -> crate::ExprName { match self { - Self::BinOp(val) => val, + Self::Name(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_bin_op_expr_mut(&mut self) -> Option<&mut crate::ExprBinOp> { + pub fn as_name_expr_mut(&mut self) -> Option<&mut crate::ExprName> { match self { - Self::BinOp(val) => Some(val), + Self::Name(val) => Some(val), _ => None, } } #[inline] - pub fn as_bin_op_expr(&self) -> Option<&crate::ExprBinOp> { + pub fn as_name_expr(&self) -> Option<&crate::ExprName> { match self { - Self::BinOp(val) => Some(val), + Self::Name(val) => Some(val), _ => None, } } #[inline] - pub const fn is_unary_op_expr(&self) -> bool { - matches!(self, Self::UnaryOp(_)) + pub const fn is_list_expr(&self) -> bool { + matches!(self, Self::List(_)) } #[inline] - pub fn unary_op_expr(self) -> Option { + pub fn list_expr(self) -> Option { match self { - Self::UnaryOp(val) => Some(val), + Self::List(val) => Some(val), _ => None, } } #[inline] - pub fn expect_unary_op_expr(self) -> crate::ExprUnaryOp { + pub fn expect_list_expr(self) -> crate::ExprList { match self { - Self::UnaryOp(val) => val, + Self::List(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_unary_op_expr_mut(&mut self) -> Option<&mut crate::ExprUnaryOp> { + pub fn as_list_expr_mut(&mut self) -> Option<&mut crate::ExprList> { match self { - Self::UnaryOp(val) => Some(val), + Self::List(val) => Some(val), _ => None, } } #[inline] - pub fn as_unary_op_expr(&self) -> Option<&crate::ExprUnaryOp> { + pub fn as_list_expr(&self) -> Option<&crate::ExprList> { match self { - Self::UnaryOp(val) => Some(val), + Self::List(val) => Some(val), _ => None, } } #[inline] - pub const fn is_lambda_expr(&self) -> bool { - matches!(self, Self::Lambda(_)) + pub const fn is_tuple_expr(&self) -> bool { + matches!(self, Self::Tuple(_)) } #[inline] - pub fn lambda_expr(self) -> Option { + pub fn tuple_expr(self) -> Option { match self { - Self::Lambda(val) => Some(val), + Self::Tuple(val) => Some(val), _ => None, } } #[inline] - pub fn expect_lambda_expr(self) -> crate::ExprLambda { + pub fn expect_tuple_expr(self) -> crate::ExprTuple { match self { - Self::Lambda(val) => val, + Self::Tuple(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_lambda_expr_mut(&mut self) -> Option<&mut crate::ExprLambda> { + pub fn as_tuple_expr_mut(&mut self) -> Option<&mut crate::ExprTuple> { match self { - Self::Lambda(val) => Some(val), + Self::Tuple(val) => Some(val), _ => None, } } #[inline] - pub fn as_lambda_expr(&self) -> Option<&crate::ExprLambda> { + pub fn as_tuple_expr(&self) -> Option<&crate::ExprTuple> { match self { - Self::Lambda(val) => Some(val), + Self::Tuple(val) => Some(val), _ => None, } } #[inline] - pub const fn is_if_expr(&self) -> bool { - matches!(self, Self::If(_)) + pub const fn is_slice_expr(&self) -> bool { + matches!(self, Self::Slice(_)) } #[inline] - pub fn if_expr(self) -> Option { + pub fn slice_expr(self) -> Option { match self { - Self::If(val) => Some(val), + Self::Slice(val) => Some(val), _ => None, } } #[inline] - pub fn expect_if_expr(self) -> crate::ExprIf { + pub fn expect_slice_expr(self) -> crate::ExprSlice { match self { - Self::If(val) => val, + Self::Slice(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_if_expr_mut(&mut self) -> Option<&mut crate::ExprIf> { + pub fn as_slice_expr_mut(&mut self) -> Option<&mut crate::ExprSlice> { match self { - Self::If(val) => Some(val), + Self::Slice(val) => Some(val), _ => None, } } #[inline] - pub fn as_if_expr(&self) -> Option<&crate::ExprIf> { + pub fn as_slice_expr(&self) -> Option<&crate::ExprSlice> { match self { - Self::If(val) => Some(val), + Self::Slice(val) => Some(val), _ => None, } } #[inline] - pub const fn is_dict_expr(&self) -> bool { - matches!(self, Self::Dict(_)) + pub const fn is_ipy_escape_command_expr(&self) -> bool { + matches!(self, Self::IpyEscapeCommand(_)) } #[inline] - pub fn dict_expr(self) -> Option { + pub fn ipy_escape_command_expr(self) -> Option { match self { - Self::Dict(val) => Some(val), + Self::IpyEscapeCommand(val) => Some(val), _ => None, } } #[inline] - pub fn expect_dict_expr(self) -> crate::ExprDict { + pub fn expect_ipy_escape_command_expr(self) -> crate::ExprIpyEscapeCommand { match self { - Self::Dict(val) => val, + Self::IpyEscapeCommand(val) => val, _ => panic!("called expect on {self:?}"), } } #[inline] - pub fn as_dict_expr_mut(&mut self) -> Option<&mut crate::ExprDict> { + pub fn as_ipy_escape_command_expr_mut(&mut self) -> Option<&mut crate::ExprIpyEscapeCommand> { match self { - Self::Dict(val) => Some(val), + Self::IpyEscapeCommand(val) => Some(val), _ => None, } } #[inline] - pub fn as_dict_expr(&self) -> Option<&crate::ExprDict> { + pub fn as_ipy_escape_command_expr(&self) -> Option<&crate::ExprIpyEscapeCommand> { match self { - Self::Dict(val) => Some(val), + Self::IpyEscapeCommand(val) => Some(val), _ => None, } } @@ -3548,193 +3548,193 @@ impl ruff_text_size::Ranged for crate::StmtIpyEscapeCommand { } } -impl ruff_text_size::Ranged for crate::ExprSet { +impl ruff_text_size::Ranged for crate::ExprBoolOp { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprListComp { +impl ruff_text_size::Ranged for crate::ExprNamed { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprSetComp { +impl ruff_text_size::Ranged for crate::ExprBinOp { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprDictComp { +impl ruff_text_size::Ranged for crate::ExprUnaryOp { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprGenerator { +impl ruff_text_size::Ranged for crate::ExprLambda { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprAwait { +impl ruff_text_size::Ranged for crate::ExprIf { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprYield { +impl ruff_text_size::Ranged for crate::ExprDict { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprYieldFrom { +impl ruff_text_size::Ranged for crate::ExprSet { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprCompare { +impl ruff_text_size::Ranged for crate::ExprListComp { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprCall { +impl ruff_text_size::Ranged for crate::ExprSetComp { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprFString { +impl ruff_text_size::Ranged for crate::ExprDictComp { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprStringLiteral { +impl ruff_text_size::Ranged for crate::ExprGenerator { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprBytesLiteral { +impl ruff_text_size::Ranged for crate::ExprAwait { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprNumberLiteral { +impl ruff_text_size::Ranged for crate::ExprYield { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprBooleanLiteral { +impl ruff_text_size::Ranged for crate::ExprYieldFrom { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprNoneLiteral { +impl ruff_text_size::Ranged for crate::ExprCompare { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprEllipsisLiteral { +impl ruff_text_size::Ranged for crate::ExprCall { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprAttribute { +impl ruff_text_size::Ranged for crate::ExprFString { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprSubscript { +impl ruff_text_size::Ranged for crate::ExprStringLiteral { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprStarred { +impl ruff_text_size::Ranged for crate::ExprBytesLiteral { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprName { +impl ruff_text_size::Ranged for crate::ExprNumberLiteral { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprList { +impl ruff_text_size::Ranged for crate::ExprBooleanLiteral { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprTuple { +impl ruff_text_size::Ranged for crate::ExprNoneLiteral { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprSlice { +impl ruff_text_size::Ranged for crate::ExprEllipsisLiteral { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprIpyEscapeCommand { +impl ruff_text_size::Ranged for crate::ExprAttribute { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprBoolOp { +impl ruff_text_size::Ranged for crate::ExprSubscript { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprNamed { +impl ruff_text_size::Ranged for crate::ExprStarred { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprBinOp { +impl ruff_text_size::Ranged for crate::ExprName { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprUnaryOp { +impl ruff_text_size::Ranged for crate::ExprList { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprLambda { +impl ruff_text_size::Ranged for crate::ExprTuple { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprIf { +impl ruff_text_size::Ranged for crate::ExprSlice { fn range(&self) -> ruff_text_size::TextRange { self.range } } -impl ruff_text_size::Ranged for crate::ExprDict { +impl ruff_text_size::Ranged for crate::ExprIpyEscapeCommand { fn range(&self) -> ruff_text_size::TextRange { self.range } @@ -3994,6 +3994,13 @@ impl Expr { V: crate::visitor::source_order::SourceOrderVisitor<'a> + ?Sized, { match self { + Expr::BoolOp(node) => node.visit_source_order(visitor), + Expr::Named(node) => node.visit_source_order(visitor), + Expr::BinOp(node) => node.visit_source_order(visitor), + Expr::UnaryOp(node) => node.visit_source_order(visitor), + Expr::Lambda(node) => node.visit_source_order(visitor), + Expr::If(node) => node.visit_source_order(visitor), + Expr::Dict(node) => node.visit_source_order(visitor), Expr::Set(node) => node.visit_source_order(visitor), Expr::ListComp(node) => node.visit_source_order(visitor), Expr::SetComp(node) => node.visit_source_order(visitor), @@ -4019,13 +4026,6 @@ impl Expr { Expr::Tuple(node) => node.visit_source_order(visitor), Expr::Slice(node) => node.visit_source_order(visitor), Expr::IpyEscapeCommand(node) => node.visit_source_order(visitor), - Expr::BoolOp(node) => node.visit_source_order(visitor), - Expr::Named(node) => node.visit_source_order(visitor), - Expr::BinOp(node) => node.visit_source_order(visitor), - Expr::UnaryOp(node) => node.visit_source_order(visitor), - Expr::Lambda(node) => node.visit_source_order(visitor), - Expr::If(node) => node.visit_source_order(visitor), - Expr::Dict(node) => node.visit_source_order(visitor), } } } @@ -4397,6 +4397,20 @@ impl ruff_text_size::Ranged for StmtRef<'_> { /// See also [expr](https://docs.python.org/3/library/ast.html#ast.expr) #[derive(Clone, Copy, Debug, PartialEq, is_macro::Is)] pub enum ExprRef<'a> { + #[is(name = "bool_op_expr")] + BoolOp(&'a crate::ExprBoolOp), + #[is(name = "named_expr")] + Named(&'a crate::ExprNamed), + #[is(name = "bin_op_expr")] + BinOp(&'a crate::ExprBinOp), + #[is(name = "unary_op_expr")] + UnaryOp(&'a crate::ExprUnaryOp), + #[is(name = "lambda_expr")] + Lambda(&'a crate::ExprLambda), + #[is(name = "if_expr")] + If(&'a crate::ExprIf), + #[is(name = "dict_expr")] + Dict(&'a crate::ExprDict), #[is(name = "set_expr")] Set(&'a crate::ExprSet), #[is(name = "list_comp_expr")] @@ -4447,25 +4461,18 @@ pub enum ExprRef<'a> { Slice(&'a crate::ExprSlice), #[is(name = "ipy_escape_command_expr")] IpyEscapeCommand(&'a crate::ExprIpyEscapeCommand), - #[is(name = "bool_op_expr")] - BoolOp(&'a crate::ExprBoolOp), - #[is(name = "named_expr")] - Named(&'a crate::ExprNamed), - #[is(name = "bin_op_expr")] - BinOp(&'a crate::ExprBinOp), - #[is(name = "unary_op_expr")] - UnaryOp(&'a crate::ExprUnaryOp), - #[is(name = "lambda_expr")] - Lambda(&'a crate::ExprLambda), - #[is(name = "if_expr")] - If(&'a crate::ExprIf), - #[is(name = "dict_expr")] - Dict(&'a crate::ExprDict), } impl<'a> From<&'a Expr> for ExprRef<'a> { fn from(node: &'a Expr) -> Self { match node { + Expr::BoolOp(node) => ExprRef::BoolOp(node), + Expr::Named(node) => ExprRef::Named(node), + Expr::BinOp(node) => ExprRef::BinOp(node), + Expr::UnaryOp(node) => ExprRef::UnaryOp(node), + Expr::Lambda(node) => ExprRef::Lambda(node), + Expr::If(node) => ExprRef::If(node), + Expr::Dict(node) => ExprRef::Dict(node), Expr::Set(node) => ExprRef::Set(node), Expr::ListComp(node) => ExprRef::ListComp(node), Expr::SetComp(node) => ExprRef::SetComp(node), @@ -4491,17 +4498,52 @@ impl<'a> From<&'a Expr> for ExprRef<'a> { Expr::Tuple(node) => ExprRef::Tuple(node), Expr::Slice(node) => ExprRef::Slice(node), Expr::IpyEscapeCommand(node) => ExprRef::IpyEscapeCommand(node), - Expr::BoolOp(node) => ExprRef::BoolOp(node), - Expr::Named(node) => ExprRef::Named(node), - Expr::BinOp(node) => ExprRef::BinOp(node), - Expr::UnaryOp(node) => ExprRef::UnaryOp(node), - Expr::Lambda(node) => ExprRef::Lambda(node), - Expr::If(node) => ExprRef::If(node), - Expr::Dict(node) => ExprRef::Dict(node), } } } +impl<'a> From<&'a crate::ExprBoolOp> for ExprRef<'a> { + fn from(node: &'a crate::ExprBoolOp) -> Self { + Self::BoolOp(node) + } +} + +impl<'a> From<&'a crate::ExprNamed> for ExprRef<'a> { + fn from(node: &'a crate::ExprNamed) -> Self { + Self::Named(node) + } +} + +impl<'a> From<&'a crate::ExprBinOp> for ExprRef<'a> { + fn from(node: &'a crate::ExprBinOp) -> Self { + Self::BinOp(node) + } +} + +impl<'a> From<&'a crate::ExprUnaryOp> for ExprRef<'a> { + fn from(node: &'a crate::ExprUnaryOp) -> Self { + Self::UnaryOp(node) + } +} + +impl<'a> From<&'a crate::ExprLambda> for ExprRef<'a> { + fn from(node: &'a crate::ExprLambda) -> Self { + Self::Lambda(node) + } +} + +impl<'a> From<&'a crate::ExprIf> for ExprRef<'a> { + fn from(node: &'a crate::ExprIf) -> Self { + Self::If(node) + } +} + +impl<'a> From<&'a crate::ExprDict> for ExprRef<'a> { + fn from(node: &'a crate::ExprDict) -> Self { + Self::Dict(node) + } +} + impl<'a> From<&'a crate::ExprSet> for ExprRef<'a> { fn from(node: &'a crate::ExprSet) -> Self { Self::Set(node) @@ -4652,51 +4694,16 @@ impl<'a> From<&'a crate::ExprIpyEscapeCommand> for ExprRef<'a> { } } -impl<'a> From<&'a crate::ExprBoolOp> for ExprRef<'a> { - fn from(node: &'a crate::ExprBoolOp) -> Self { - Self::BoolOp(node) - } -} - -impl<'a> From<&'a crate::ExprNamed> for ExprRef<'a> { - fn from(node: &'a crate::ExprNamed) -> Self { - Self::Named(node) - } -} - -impl<'a> From<&'a crate::ExprBinOp> for ExprRef<'a> { - fn from(node: &'a crate::ExprBinOp) -> Self { - Self::BinOp(node) - } -} - -impl<'a> From<&'a crate::ExprUnaryOp> for ExprRef<'a> { - fn from(node: &'a crate::ExprUnaryOp) -> Self { - Self::UnaryOp(node) - } -} - -impl<'a> From<&'a crate::ExprLambda> for ExprRef<'a> { - fn from(node: &'a crate::ExprLambda) -> Self { - Self::Lambda(node) - } -} - -impl<'a> From<&'a crate::ExprIf> for ExprRef<'a> { - fn from(node: &'a crate::ExprIf) -> Self { - Self::If(node) - } -} - -impl<'a> From<&'a crate::ExprDict> for ExprRef<'a> { - fn from(node: &'a crate::ExprDict) -> Self { - Self::Dict(node) - } -} - impl ruff_text_size::Ranged for ExprRef<'_> { fn range(&self) -> ruff_text_size::TextRange { match self { + Self::BoolOp(node) => node.range(), + Self::Named(node) => node.range(), + Self::BinOp(node) => node.range(), + Self::UnaryOp(node) => node.range(), + Self::Lambda(node) => node.range(), + Self::If(node) => node.range(), + Self::Dict(node) => node.range(), Self::Set(node) => node.range(), Self::ListComp(node) => node.range(), Self::SetComp(node) => node.range(), @@ -4722,13 +4729,6 @@ impl ruff_text_size::Ranged for ExprRef<'_> { Self::Tuple(node) => node.range(), Self::Slice(node) => node.range(), Self::IpyEscapeCommand(node) => node.range(), - Self::BoolOp(node) => node.range(), - Self::Named(node) => node.range(), - Self::BinOp(node) => node.range(), - Self::UnaryOp(node) => node.range(), - Self::Lambda(node) => node.range(), - Self::If(node) => node.range(), - Self::Dict(node) => node.range(), } } } @@ -4963,6 +4963,13 @@ pub enum AnyNodeRef<'a> { StmtBreak(&'a crate::StmtBreak), StmtContinue(&'a crate::StmtContinue), StmtIpyEscapeCommand(&'a crate::StmtIpyEscapeCommand), + ExprBoolOp(&'a crate::ExprBoolOp), + ExprNamed(&'a crate::ExprNamed), + ExprBinOp(&'a crate::ExprBinOp), + ExprUnaryOp(&'a crate::ExprUnaryOp), + ExprLambda(&'a crate::ExprLambda), + ExprIf(&'a crate::ExprIf), + ExprDict(&'a crate::ExprDict), ExprSet(&'a crate::ExprSet), ExprListComp(&'a crate::ExprListComp), ExprSetComp(&'a crate::ExprSetComp), @@ -4988,13 +4995,6 @@ pub enum AnyNodeRef<'a> { ExprTuple(&'a crate::ExprTuple), ExprSlice(&'a crate::ExprSlice), ExprIpyEscapeCommand(&'a crate::ExprIpyEscapeCommand), - ExprBoolOp(&'a crate::ExprBoolOp), - ExprNamed(&'a crate::ExprNamed), - ExprBinOp(&'a crate::ExprBinOp), - ExprUnaryOp(&'a crate::ExprUnaryOp), - ExprLambda(&'a crate::ExprLambda), - ExprIf(&'a crate::ExprIf), - ExprDict(&'a crate::ExprDict), ExceptHandlerExceptHandler(&'a crate::ExceptHandlerExceptHandler), FStringExpressionElement(&'a crate::FStringExpressionElement), FStringLiteralElement(&'a crate::FStringLiteralElement), @@ -5115,6 +5115,13 @@ impl<'a> From> for AnyNodeRef<'a> { impl<'a> From<&'a Expr> for AnyNodeRef<'a> { fn from(node: &'a Expr) -> AnyNodeRef<'a> { match node { + Expr::BoolOp(node) => AnyNodeRef::ExprBoolOp(node), + Expr::Named(node) => AnyNodeRef::ExprNamed(node), + Expr::BinOp(node) => AnyNodeRef::ExprBinOp(node), + Expr::UnaryOp(node) => AnyNodeRef::ExprUnaryOp(node), + Expr::Lambda(node) => AnyNodeRef::ExprLambda(node), + Expr::If(node) => AnyNodeRef::ExprIf(node), + Expr::Dict(node) => AnyNodeRef::ExprDict(node), Expr::Set(node) => AnyNodeRef::ExprSet(node), Expr::ListComp(node) => AnyNodeRef::ExprListComp(node), Expr::SetComp(node) => AnyNodeRef::ExprSetComp(node), @@ -5140,13 +5147,6 @@ impl<'a> From<&'a Expr> for AnyNodeRef<'a> { Expr::Tuple(node) => AnyNodeRef::ExprTuple(node), Expr::Slice(node) => AnyNodeRef::ExprSlice(node), Expr::IpyEscapeCommand(node) => AnyNodeRef::ExprIpyEscapeCommand(node), - Expr::BoolOp(node) => AnyNodeRef::ExprBoolOp(node), - Expr::Named(node) => AnyNodeRef::ExprNamed(node), - Expr::BinOp(node) => AnyNodeRef::ExprBinOp(node), - Expr::UnaryOp(node) => AnyNodeRef::ExprUnaryOp(node), - Expr::Lambda(node) => AnyNodeRef::ExprLambda(node), - Expr::If(node) => AnyNodeRef::ExprIf(node), - Expr::Dict(node) => AnyNodeRef::ExprDict(node), } } } @@ -5154,6 +5154,13 @@ impl<'a> From<&'a Expr> for AnyNodeRef<'a> { impl<'a> From> for AnyNodeRef<'a> { fn from(node: ExprRef<'a>) -> AnyNodeRef<'a> { match node { + ExprRef::BoolOp(node) => AnyNodeRef::ExprBoolOp(node), + ExprRef::Named(node) => AnyNodeRef::ExprNamed(node), + ExprRef::BinOp(node) => AnyNodeRef::ExprBinOp(node), + ExprRef::UnaryOp(node) => AnyNodeRef::ExprUnaryOp(node), + ExprRef::Lambda(node) => AnyNodeRef::ExprLambda(node), + ExprRef::If(node) => AnyNodeRef::ExprIf(node), + ExprRef::Dict(node) => AnyNodeRef::ExprDict(node), ExprRef::Set(node) => AnyNodeRef::ExprSet(node), ExprRef::ListComp(node) => AnyNodeRef::ExprListComp(node), ExprRef::SetComp(node) => AnyNodeRef::ExprSetComp(node), @@ -5179,13 +5186,6 @@ impl<'a> From> for AnyNodeRef<'a> { ExprRef::Tuple(node) => AnyNodeRef::ExprTuple(node), ExprRef::Slice(node) => AnyNodeRef::ExprSlice(node), ExprRef::IpyEscapeCommand(node) => AnyNodeRef::ExprIpyEscapeCommand(node), - ExprRef::BoolOp(node) => AnyNodeRef::ExprBoolOp(node), - ExprRef::Named(node) => AnyNodeRef::ExprNamed(node), - ExprRef::BinOp(node) => AnyNodeRef::ExprBinOp(node), - ExprRef::UnaryOp(node) => AnyNodeRef::ExprUnaryOp(node), - ExprRef::Lambda(node) => AnyNodeRef::ExprLambda(node), - ExprRef::If(node) => AnyNodeRef::ExprIf(node), - ExprRef::Dict(node) => AnyNodeRef::ExprDict(node), } } } @@ -5412,27 +5412,69 @@ impl<'a> From<&'a crate::StmtExpr> for AnyNodeRef<'a> { } } -impl<'a> From<&'a crate::StmtPass> for AnyNodeRef<'a> { - fn from(node: &'a crate::StmtPass) -> AnyNodeRef<'a> { - AnyNodeRef::StmtPass(node) +impl<'a> From<&'a crate::StmtPass> for AnyNodeRef<'a> { + fn from(node: &'a crate::StmtPass) -> AnyNodeRef<'a> { + AnyNodeRef::StmtPass(node) + } +} + +impl<'a> From<&'a crate::StmtBreak> for AnyNodeRef<'a> { + fn from(node: &'a crate::StmtBreak) -> AnyNodeRef<'a> { + AnyNodeRef::StmtBreak(node) + } +} + +impl<'a> From<&'a crate::StmtContinue> for AnyNodeRef<'a> { + fn from(node: &'a crate::StmtContinue) -> AnyNodeRef<'a> { + AnyNodeRef::StmtContinue(node) + } +} + +impl<'a> From<&'a crate::StmtIpyEscapeCommand> for AnyNodeRef<'a> { + fn from(node: &'a crate::StmtIpyEscapeCommand) -> AnyNodeRef<'a> { + AnyNodeRef::StmtIpyEscapeCommand(node) + } +} + +impl<'a> From<&'a crate::ExprBoolOp> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprBoolOp) -> AnyNodeRef<'a> { + AnyNodeRef::ExprBoolOp(node) + } +} + +impl<'a> From<&'a crate::ExprNamed> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprNamed) -> AnyNodeRef<'a> { + AnyNodeRef::ExprNamed(node) + } +} + +impl<'a> From<&'a crate::ExprBinOp> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprBinOp) -> AnyNodeRef<'a> { + AnyNodeRef::ExprBinOp(node) + } +} + +impl<'a> From<&'a crate::ExprUnaryOp> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprUnaryOp) -> AnyNodeRef<'a> { + AnyNodeRef::ExprUnaryOp(node) } } -impl<'a> From<&'a crate::StmtBreak> for AnyNodeRef<'a> { - fn from(node: &'a crate::StmtBreak) -> AnyNodeRef<'a> { - AnyNodeRef::StmtBreak(node) +impl<'a> From<&'a crate::ExprLambda> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprLambda) -> AnyNodeRef<'a> { + AnyNodeRef::ExprLambda(node) } } -impl<'a> From<&'a crate::StmtContinue> for AnyNodeRef<'a> { - fn from(node: &'a crate::StmtContinue) -> AnyNodeRef<'a> { - AnyNodeRef::StmtContinue(node) +impl<'a> From<&'a crate::ExprIf> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprIf) -> AnyNodeRef<'a> { + AnyNodeRef::ExprIf(node) } } -impl<'a> From<&'a crate::StmtIpyEscapeCommand> for AnyNodeRef<'a> { - fn from(node: &'a crate::StmtIpyEscapeCommand) -> AnyNodeRef<'a> { - AnyNodeRef::StmtIpyEscapeCommand(node) +impl<'a> From<&'a crate::ExprDict> for AnyNodeRef<'a> { + fn from(node: &'a crate::ExprDict) -> AnyNodeRef<'a> { + AnyNodeRef::ExprDict(node) } } @@ -5586,48 +5628,6 @@ impl<'a> From<&'a crate::ExprIpyEscapeCommand> for AnyNodeRef<'a> { } } -impl<'a> From<&'a crate::ExprBoolOp> for AnyNodeRef<'a> { - fn from(node: &'a crate::ExprBoolOp) -> AnyNodeRef<'a> { - AnyNodeRef::ExprBoolOp(node) - } -} - -impl<'a> From<&'a crate::ExprNamed> for AnyNodeRef<'a> { - fn from(node: &'a crate::ExprNamed) -> AnyNodeRef<'a> { - AnyNodeRef::ExprNamed(node) - } -} - -impl<'a> From<&'a crate::ExprBinOp> for AnyNodeRef<'a> { - fn from(node: &'a crate::ExprBinOp) -> AnyNodeRef<'a> { - AnyNodeRef::ExprBinOp(node) - } -} - -impl<'a> From<&'a crate::ExprUnaryOp> for AnyNodeRef<'a> { - fn from(node: &'a crate::ExprUnaryOp) -> AnyNodeRef<'a> { - AnyNodeRef::ExprUnaryOp(node) - } -} - -impl<'a> From<&'a crate::ExprLambda> for AnyNodeRef<'a> { - fn from(node: &'a crate::ExprLambda) -> AnyNodeRef<'a> { - AnyNodeRef::ExprLambda(node) - } -} - -impl<'a> From<&'a crate::ExprIf> for AnyNodeRef<'a> { - fn from(node: &'a crate::ExprIf) -> AnyNodeRef<'a> { - AnyNodeRef::ExprIf(node) - } -} - -impl<'a> From<&'a crate::ExprDict> for AnyNodeRef<'a> { - fn from(node: &'a crate::ExprDict) -> AnyNodeRef<'a> { - AnyNodeRef::ExprDict(node) - } -} - impl<'a> From<&'a crate::ExceptHandlerExceptHandler> for AnyNodeRef<'a> { fn from(node: &'a crate::ExceptHandlerExceptHandler) -> AnyNodeRef<'a> { AnyNodeRef::ExceptHandlerExceptHandler(node) @@ -5856,6 +5856,13 @@ impl ruff_text_size::Ranged for AnyNodeRef<'_> { AnyNodeRef::StmtBreak(node) => node.range(), AnyNodeRef::StmtContinue(node) => node.range(), AnyNodeRef::StmtIpyEscapeCommand(node) => node.range(), + AnyNodeRef::ExprBoolOp(node) => node.range(), + AnyNodeRef::ExprNamed(node) => node.range(), + AnyNodeRef::ExprBinOp(node) => node.range(), + AnyNodeRef::ExprUnaryOp(node) => node.range(), + AnyNodeRef::ExprLambda(node) => node.range(), + AnyNodeRef::ExprIf(node) => node.range(), + AnyNodeRef::ExprDict(node) => node.range(), AnyNodeRef::ExprSet(node) => node.range(), AnyNodeRef::ExprListComp(node) => node.range(), AnyNodeRef::ExprSetComp(node) => node.range(), @@ -5881,13 +5888,6 @@ impl ruff_text_size::Ranged for AnyNodeRef<'_> { AnyNodeRef::ExprTuple(node) => node.range(), AnyNodeRef::ExprSlice(node) => node.range(), AnyNodeRef::ExprIpyEscapeCommand(node) => node.range(), - AnyNodeRef::ExprBoolOp(node) => node.range(), - AnyNodeRef::ExprNamed(node) => node.range(), - AnyNodeRef::ExprBinOp(node) => node.range(), - AnyNodeRef::ExprUnaryOp(node) => node.range(), - AnyNodeRef::ExprLambda(node) => node.range(), - AnyNodeRef::ExprIf(node) => node.range(), - AnyNodeRef::ExprDict(node) => node.range(), AnyNodeRef::ExceptHandlerExceptHandler(node) => node.range(), AnyNodeRef::FStringExpressionElement(node) => node.range(), AnyNodeRef::FStringLiteralElement(node) => node.range(), @@ -5955,6 +5955,13 @@ impl AnyNodeRef<'_> { AnyNodeRef::StmtBreak(node) => std::ptr::NonNull::from(*node).cast(), AnyNodeRef::StmtContinue(node) => std::ptr::NonNull::from(*node).cast(), AnyNodeRef::StmtIpyEscapeCommand(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprBoolOp(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprNamed(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprBinOp(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprUnaryOp(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprLambda(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprIf(node) => std::ptr::NonNull::from(*node).cast(), + AnyNodeRef::ExprDict(node) => std::ptr::NonNull::from(*node).cast(), AnyNodeRef::ExprSet(node) => std::ptr::NonNull::from(*node).cast(), AnyNodeRef::ExprListComp(node) => std::ptr::NonNull::from(*node).cast(), AnyNodeRef::ExprSetComp(node) => std::ptr::NonNull::from(*node).cast(), @@ -5980,13 +5987,6 @@ impl AnyNodeRef<'_> { AnyNodeRef::ExprTuple(node) => std::ptr::NonNull::from(*node).cast(), AnyNodeRef::ExprSlice(node) => std::ptr::NonNull::from(*node).cast(), AnyNodeRef::ExprIpyEscapeCommand(node) => std::ptr::NonNull::from(*node).cast(), - AnyNodeRef::ExprBoolOp(node) => std::ptr::NonNull::from(*node).cast(), - AnyNodeRef::ExprNamed(node) => std::ptr::NonNull::from(*node).cast(), - AnyNodeRef::ExprBinOp(node) => std::ptr::NonNull::from(*node).cast(), - AnyNodeRef::ExprUnaryOp(node) => std::ptr::NonNull::from(*node).cast(), - AnyNodeRef::ExprLambda(node) => std::ptr::NonNull::from(*node).cast(), - AnyNodeRef::ExprIf(node) => std::ptr::NonNull::from(*node).cast(), - AnyNodeRef::ExprDict(node) => std::ptr::NonNull::from(*node).cast(), AnyNodeRef::ExceptHandlerExceptHandler(node) => std::ptr::NonNull::from(*node).cast(), AnyNodeRef::FStringExpressionElement(node) => std::ptr::NonNull::from(*node).cast(), AnyNodeRef::FStringLiteralElement(node) => std::ptr::NonNull::from(*node).cast(), @@ -6058,6 +6058,13 @@ impl<'a> AnyNodeRef<'a> { AnyNodeRef::StmtBreak(node) => node.visit_source_order(visitor), AnyNodeRef::StmtContinue(node) => node.visit_source_order(visitor), AnyNodeRef::StmtIpyEscapeCommand(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprBoolOp(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprNamed(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprBinOp(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprUnaryOp(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprLambda(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprIf(node) => node.visit_source_order(visitor), + AnyNodeRef::ExprDict(node) => node.visit_source_order(visitor), AnyNodeRef::ExprSet(node) => node.visit_source_order(visitor), AnyNodeRef::ExprListComp(node) => node.visit_source_order(visitor), AnyNodeRef::ExprSetComp(node) => node.visit_source_order(visitor), @@ -6083,13 +6090,6 @@ impl<'a> AnyNodeRef<'a> { AnyNodeRef::ExprTuple(node) => node.visit_source_order(visitor), AnyNodeRef::ExprSlice(node) => node.visit_source_order(visitor), AnyNodeRef::ExprIpyEscapeCommand(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprBoolOp(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprNamed(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprBinOp(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprUnaryOp(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprLambda(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprIf(node) => node.visit_source_order(visitor), - AnyNodeRef::ExprDict(node) => node.visit_source_order(visitor), AnyNodeRef::ExceptHandlerExceptHandler(node) => node.visit_source_order(visitor), AnyNodeRef::FStringExpressionElement(node) => node.visit_source_order(visitor), AnyNodeRef::FStringLiteralElement(node) => node.visit_source_order(visitor), @@ -6173,7 +6173,14 @@ impl AnyNodeRef<'_> { pub const fn is_expression(self) -> bool { matches!( self, - AnyNodeRef::ExprSet(_) + AnyNodeRef::ExprBoolOp(_) + | AnyNodeRef::ExprNamed(_) + | AnyNodeRef::ExprBinOp(_) + | AnyNodeRef::ExprUnaryOp(_) + | AnyNodeRef::ExprLambda(_) + | AnyNodeRef::ExprIf(_) + | AnyNodeRef::ExprDict(_) + | AnyNodeRef::ExprSet(_) | AnyNodeRef::ExprListComp(_) | AnyNodeRef::ExprSetComp(_) | AnyNodeRef::ExprDictComp(_) @@ -6198,13 +6205,6 @@ impl AnyNodeRef<'_> { | AnyNodeRef::ExprTuple(_) | AnyNodeRef::ExprSlice(_) | AnyNodeRef::ExprIpyEscapeCommand(_) - | AnyNodeRef::ExprBoolOp(_) - | AnyNodeRef::ExprNamed(_) - | AnyNodeRef::ExprBinOp(_) - | AnyNodeRef::ExprUnaryOp(_) - | AnyNodeRef::ExprLambda(_) - | AnyNodeRef::ExprIf(_) - | AnyNodeRef::ExprDict(_) ) } } @@ -6280,6 +6280,13 @@ pub enum NodeKind { StmtBreak, StmtContinue, StmtIpyEscapeCommand, + ExprBoolOp, + ExprNamed, + ExprBinOp, + ExprUnaryOp, + ExprLambda, + ExprIf, + ExprDict, ExprSet, ExprListComp, ExprSetComp, @@ -6305,13 +6312,6 @@ pub enum NodeKind { ExprTuple, ExprSlice, ExprIpyEscapeCommand, - ExprBoolOp, - ExprNamed, - ExprBinOp, - ExprUnaryOp, - ExprLambda, - ExprIf, - ExprDict, ExceptHandlerExceptHandler, FStringExpressionElement, FStringLiteralElement, @@ -6377,6 +6377,13 @@ impl AnyNodeRef<'_> { AnyNodeRef::StmtBreak(_) => NodeKind::StmtBreak, AnyNodeRef::StmtContinue(_) => NodeKind::StmtContinue, AnyNodeRef::StmtIpyEscapeCommand(_) => NodeKind::StmtIpyEscapeCommand, + AnyNodeRef::ExprBoolOp(_) => NodeKind::ExprBoolOp, + AnyNodeRef::ExprNamed(_) => NodeKind::ExprNamed, + AnyNodeRef::ExprBinOp(_) => NodeKind::ExprBinOp, + AnyNodeRef::ExprUnaryOp(_) => NodeKind::ExprUnaryOp, + AnyNodeRef::ExprLambda(_) => NodeKind::ExprLambda, + AnyNodeRef::ExprIf(_) => NodeKind::ExprIf, + AnyNodeRef::ExprDict(_) => NodeKind::ExprDict, AnyNodeRef::ExprSet(_) => NodeKind::ExprSet, AnyNodeRef::ExprListComp(_) => NodeKind::ExprListComp, AnyNodeRef::ExprSetComp(_) => NodeKind::ExprSetComp, @@ -6402,13 +6409,6 @@ impl AnyNodeRef<'_> { AnyNodeRef::ExprTuple(_) => NodeKind::ExprTuple, AnyNodeRef::ExprSlice(_) => NodeKind::ExprSlice, AnyNodeRef::ExprIpyEscapeCommand(_) => NodeKind::ExprIpyEscapeCommand, - AnyNodeRef::ExprBoolOp(_) => NodeKind::ExprBoolOp, - AnyNodeRef::ExprNamed(_) => NodeKind::ExprNamed, - AnyNodeRef::ExprBinOp(_) => NodeKind::ExprBinOp, - AnyNodeRef::ExprUnaryOp(_) => NodeKind::ExprUnaryOp, - AnyNodeRef::ExprLambda(_) => NodeKind::ExprLambda, - AnyNodeRef::ExprIf(_) => NodeKind::ExprIf, - AnyNodeRef::ExprDict(_) => NodeKind::ExprDict, AnyNodeRef::ExceptHandlerExceptHandler(_) => NodeKind::ExceptHandlerExceptHandler, AnyNodeRef::FStringExpressionElement(_) => NodeKind::FStringExpressionElement, AnyNodeRef::FStringLiteralElement(_) => NodeKind::FStringLiteralElement, @@ -6495,3 +6495,172 @@ pub struct ExprDict { pub range: ruff_text_size::TextRange, pub items: Vec, } + +#[derive(Clone, Debug, PartialEq)] +pub struct ExprSet { + pub range: ruff_text_size::TextRange, + pub elts: Vec, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct ExprListComp { + pub range: ruff_text_size::TextRange, + pub elt: Box, + pub generators: Vec, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct ExprSetComp { + pub range: ruff_text_size::TextRange, + pub elt: Box, + pub generators: Vec, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct ExprDictComp { + pub range: ruff_text_size::TextRange, + pub key: Box, + pub value: Box, + pub generators: Vec, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct ExprGenerator { + pub range: ruff_text_size::TextRange, + pub elt: Box, + pub generators: Vec, + pub parenthesized: bool, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct ExprAwait { + pub range: ruff_text_size::TextRange, + pub value: Box, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct ExprYield { + pub range: ruff_text_size::TextRange, + pub value: Option>, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct ExprYieldFrom { + pub range: ruff_text_size::TextRange, + pub value: Box, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct ExprCompare { + pub range: ruff_text_size::TextRange, + pub left: Box, + pub ops: Box<[crate::CmpOp]>, + pub comparators: Box<[Expr]>, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct ExprCall { + pub range: ruff_text_size::TextRange, + pub func: Box, + pub arguments: crate::Arguments, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct ExprFString { + pub range: ruff_text_size::TextRange, + pub value: crate::FStringValue, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct ExprStringLiteral { + pub range: ruff_text_size::TextRange, + pub value: crate::StringLiteralValue, +} + +#[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, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct ExprAttribute { + pub range: ruff_text_size::TextRange, + pub value: Box, + pub attr: crate::Identifier, + pub ctx: crate::ExprContext, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct ExprSubscript { + pub range: ruff_text_size::TextRange, + pub value: Box, + pub slice: Box, + pub ctx: crate::ExprContext, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct ExprStarred { + pub range: ruff_text_size::TextRange, + pub value: Box, + pub ctx: crate::ExprContext, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct ExprName { + pub range: ruff_text_size::TextRange, + pub id: crate::name::Name, + pub ctx: crate::ExprContext, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct ExprList { + pub range: ruff_text_size::TextRange, + pub elts: Vec, + pub ctx: crate::ExprContext, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct ExprTuple { + pub range: ruff_text_size::TextRange, + pub elts: Vec, + pub ctx: crate::ExprContext, + pub parenthesized: bool, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct ExprSlice { + pub range: ruff_text_size::TextRange, + pub lower: Option>, + pub upper: Option>, + pub step: Option>, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct ExprIpyEscapeCommand { + pub range: ruff_text_size::TextRange, + pub kind: crate::IpyEscapeKind, + pub value: Box, +} diff --git a/crates/ruff_python_ast/src/name.rs b/crates/ruff_python_ast/src/name.rs index 2cc8843ab23469..6765f2aacfaf63 100644 --- a/crates/ruff_python_ast/src/name.rs +++ b/crates/ruff_python_ast/src/name.rs @@ -3,7 +3,8 @@ use std::fmt::{Debug, Display, Formatter, Write}; use std::hash::{Hash, Hasher}; use std::ops::Deref; -use crate::{nodes, Expr}; +use crate::generated::ExprName; +use crate::Expr; #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] @@ -387,16 +388,14 @@ impl<'a> UnqualifiedName<'a> { let attr1 = match expr { Expr::Attribute(attr1) => attr1, // Ex) `foo` - Expr::Name(nodes::ExprName { id, .. }) => { - return Some(Self::from_slice(&[id.as_str()])) - } + Expr::Name(ExprName { id, .. }) => return Some(Self::from_slice(&[id.as_str()])), _ => return None, }; let attr2 = match attr1.value.as_ref() { Expr::Attribute(attr2) => attr2, // Ex) `foo.bar` - Expr::Name(nodes::ExprName { id, .. }) => { + Expr::Name(ExprName { id, .. }) => { return Some(Self::from_slice(&[id.as_str(), attr1.attr.as_str()])) } _ => return None, @@ -405,7 +404,7 @@ impl<'a> UnqualifiedName<'a> { let attr3 = match attr2.value.as_ref() { Expr::Attribute(attr3) => attr3, // Ex) `foo.bar.baz` - Expr::Name(nodes::ExprName { id, .. }) => { + Expr::Name(ExprName { id, .. }) => { return Some(Self::from_slice(&[ id.as_str(), attr2.attr.as_str(), @@ -418,7 +417,7 @@ impl<'a> UnqualifiedName<'a> { let attr4 = match attr3.value.as_ref() { Expr::Attribute(attr4) => attr4, // Ex) `foo.bar.baz.bop` - Expr::Name(nodes::ExprName { id, .. }) => { + Expr::Name(ExprName { id, .. }) => { return Some(Self::from_slice(&[ id.as_str(), attr3.attr.as_str(), @@ -432,7 +431,7 @@ impl<'a> UnqualifiedName<'a> { let attr5 = match attr4.value.as_ref() { Expr::Attribute(attr5) => attr5, // Ex) `foo.bar.baz.bop.bap` - Expr::Name(nodes::ExprName { id, .. }) => { + Expr::Name(ExprName { id, .. }) => { return Some(Self::from_slice(&[ id.as_str(), attr4.attr.as_str(), @@ -447,7 +446,7 @@ impl<'a> UnqualifiedName<'a> { let attr6 = match attr5.value.as_ref() { Expr::Attribute(attr6) => attr6, // Ex) `foo.bar.baz.bop.bap.bab` - Expr::Name(nodes::ExprName { id, .. }) => { + Expr::Name(ExprName { id, .. }) => { return Some(Self::from_slice(&[ id.as_str(), attr5.attr.as_str(), @@ -463,7 +462,7 @@ impl<'a> UnqualifiedName<'a> { let attr7 = match attr6.value.as_ref() { Expr::Attribute(attr7) => attr7, // Ex) `foo.bar.baz.bop.bap.bab.bob` - Expr::Name(nodes::ExprName { id, .. }) => { + Expr::Name(ExprName { id, .. }) => { return Some(Self::from_slice(&[ id.as_str(), attr6.attr.as_str(), @@ -480,7 +479,7 @@ impl<'a> UnqualifiedName<'a> { let attr8 = match attr7.value.as_ref() { Expr::Attribute(attr8) => attr8, // Ex) `foo.bar.baz.bop.bap.bab.bob.bib` - Expr::Name(nodes::ExprName { id, .. }) => { + Expr::Name(ExprName { id, .. }) => { return Some(Self(SegmentsVec::from([ id.as_str(), attr7.attr.as_str(), @@ -505,7 +504,7 @@ impl<'a> UnqualifiedName<'a> { segments.push(attr.attr.as_str()); &*attr.value } - Expr::Name(nodes::ExprName { id, .. }) => { + Expr::Name(ExprName { id, .. }) => { segments.push(id.as_str()); break; } diff --git a/crates/ruff_python_ast/src/nodes.rs b/crates/ruff_python_ast/src/nodes.rs index 32d6451f36a81d..a864e9cc09b697 100644 --- a/crates/ruff_python_ast/src/nodes.rs +++ b/crates/ruff_python_ast/src/nodes.rs @@ -1,5 +1,6 @@ #![allow(clippy::derive_partial_eq_without_eq)] +use crate::generated::*; use std::borrow::Cow; use std::fmt; use std::fmt::Debug; @@ -120,6 +121,7 @@ pub struct StmtClassDef { pub decorator_list: Vec, pub name: Identifier, pub type_params: Option>, + // TODO: can remove? pub arguments: Option>, pub body: Vec, } @@ -379,23 +381,23 @@ impl ExprRef<'_> { } } -/// 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 [`StmtIpyEscapeCommand`]. -#[derive(Clone, Debug, PartialEq)] -pub struct ExprIpyEscapeCommand { - pub range: TextRange, - pub kind: IpyEscapeKind, - pub value: Box, -} +// /// 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 [`StmtIpyEscapeCommand`]. +// #[derive(Clone, Debug, PartialEq)] +// pub struct ExprIpyEscapeCommand { +// pub range: TextRange, +// pub kind: IpyEscapeKind, +// pub value: Box, +// } // /// See also [BoolOp](https://docs.python.org/3/library/ast.html#ast.BoolOp) // #[derive(Clone, Debug, PartialEq)] @@ -502,7 +504,7 @@ impl Ranged for DictItem { // pub items: Vec, // } -impl crate::ExprDict { +impl ExprDict { /// Returns an `Iterator` over the AST nodes representing the /// dictionary's keys. pub fn iter_keys(&self) -> DictKeyIterator { @@ -544,7 +546,7 @@ impl crate::ExprDict { } } -impl<'a> IntoIterator for &'a crate::ExprDict { +impl<'a> IntoIterator for &'a ExprDict { type IntoIter = std::slice::Iter<'a, DictItem>; type Item = &'a DictItem; @@ -638,11 +640,11 @@ impl FusedIterator for DictValueIterator<'_> {} impl ExactSizeIterator for DictValueIterator<'_> {} /// See also [Set](https://docs.python.org/3/library/ast.html#ast.Set) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprSet { - pub range: TextRange, - pub elts: Vec, -} +// #[derive(Clone, Debug, PartialEq)] +// pub struct ExprSet { +// pub range: TextRange, +// pub elts: Vec, +// } impl ExprSet { pub fn iter(&self) -> std::slice::Iter<'_, Expr> { @@ -667,77 +669,77 @@ impl<'a> IntoIterator for &'a ExprSet { } } -/// See also [ListComp](https://docs.python.org/3/library/ast.html#ast.ListComp) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprListComp { - pub range: TextRange, - pub elt: Box, - pub generators: Vec, -} +// /// See also [ListComp](https://docs.python.org/3/library/ast.html#ast.ListComp) +// #[derive(Clone, Debug, PartialEq)] +// pub struct ExprListComp { +// pub range: TextRange, +// pub elt: Box, +// pub generators: Vec, +// } -/// See also [SetComp](https://docs.python.org/3/library/ast.html#ast.SetComp) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprSetComp { - pub range: TextRange, - pub elt: Box, - pub generators: Vec, -} +// /// See also [SetComp](https://docs.python.org/3/library/ast.html#ast.SetComp) +// #[derive(Clone, Debug, PartialEq)] +// pub struct ExprSetComp { +// pub range: TextRange, +// pub elt: Box, +// pub generators: Vec, +// } -/// See also [DictComp](https://docs.python.org/3/library/ast.html#ast.DictComp) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprDictComp { - pub range: TextRange, - pub key: Box, - pub value: Box, - pub generators: Vec, -} +// /// See also [DictComp](https://docs.python.org/3/library/ast.html#ast.DictComp) +// #[derive(Clone, Debug, PartialEq)] +// pub struct ExprDictComp { +// pub range: TextRange, +// pub key: Box, +// pub value: Box, +// pub generators: Vec, +// } -/// See also [GeneratorExp](https://docs.python.org/3/library/ast.html#ast.GeneratorExp) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprGenerator { - pub range: TextRange, - pub elt: Box, - pub generators: Vec, - pub parenthesized: bool, -} +// /// See also [GeneratorExp](https://docs.python.org/3/library/ast.html#ast.GeneratorExp) +// #[derive(Clone, Debug, PartialEq)] +// pub struct ExprGenerator { +// pub range: TextRange, +// pub elt: Box, +// pub generators: Vec, +// 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: TextRange, - pub value: Box, -} +// /// See also [Await](https://docs.python.org/3/library/ast.html#ast.Await) +// #[derive(Clone, Debug, PartialEq)] +// pub struct ExprAwait { +// pub range: TextRange, +// pub value: Box, +// } -/// See also [Yield](https://docs.python.org/3/library/ast.html#ast.Yield) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprYield { - pub range: TextRange, - pub value: Option>, -} +// /// See also [Yield](https://docs.python.org/3/library/ast.html#ast.Yield) +// #[derive(Clone, Debug, PartialEq)] +// pub struct ExprYield { +// pub range: TextRange, +// pub value: Option>, +// } -/// See also [YieldFrom](https://docs.python.org/3/library/ast.html#ast.YieldFrom) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprYieldFrom { - pub range: TextRange, - pub value: Box, -} +// /// See also [YieldFrom](https://docs.python.org/3/library/ast.html#ast.YieldFrom) +// #[derive(Clone, Debug, PartialEq)] +// pub struct ExprYieldFrom { +// pub range: TextRange, +// pub value: Box, +// } -/// See also [Compare](https://docs.python.org/3/library/ast.html#ast.Compare) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprCompare { - pub range: TextRange, - pub left: Box, - pub ops: Box<[CmpOp]>, - pub comparators: Box<[Expr]>, -} +// /// See also [Compare](https://docs.python.org/3/library/ast.html#ast.Compare) +// #[derive(Clone, Debug, PartialEq)] +// pub struct ExprCompare { +// pub range: TextRange, +// pub left: Box, +// pub ops: Box<[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: TextRange, - pub func: Box, - pub arguments: Arguments, -} +// /// See also [Call](https://docs.python.org/3/library/ast.html#ast.Call) +// #[derive(Clone, Debug, PartialEq)] +// pub struct ExprCall { +// pub range: TextRange, +// pub func: Box, +// pub arguments: Arguments, +// } #[derive(Clone, Debug, PartialEq)] pub struct FStringFormatSpec { @@ -811,19 +813,19 @@ pub struct DebugText { pub trailing: String, } -/// 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. -/// -/// [JoinedStr]: https://docs.python.org/3/library/ast.html#ast.JoinedStr -#[derive(Clone, Debug, PartialEq)] -pub struct ExprFString { - pub range: TextRange, - pub value: FStringValue, -} +// /// 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. +// /// +// /// [JoinedStr]: https://docs.python.org/3/library/ast.html#ast.JoinedStr +// #[derive(Clone, Debug, PartialEq)] +// pub struct ExprFString { +// pub range: TextRange, +// pub value: FStringValue, +// } impl ExprFString { /// Returns the single [`FString`] if the f-string isn't implicitly concatenated, [`None`] @@ -1286,13 +1288,13 @@ impl fmt::Debug for FStringElements { } } -/// 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: TextRange, - pub value: StringLiteralValue, -} +// /// 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: TextRange, +// pub value: StringLiteralValue, +// } impl ExprStringLiteral { /// Return `Some(literal)` if the string only consists of a single `StringLiteral` part @@ -1738,13 +1740,13 @@ impl Debug for ConcatenatedStringLiteral { } } -/// 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: TextRange, - pub value: BytesLiteralValue, -} +// /// 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: TextRange, +// pub value: BytesLiteralValue, +// } impl ExprBytesLiteral { /// Return `Some(literal)` if the bytestring only consists of a single `BytesLiteral` part @@ -2347,11 +2349,11 @@ impl From for AnyStringFlags { } } -#[derive(Clone, Debug, PartialEq)] -pub struct ExprNumberLiteral { - pub range: TextRange, - pub value: Number, -} +// #[derive(Clone, Debug, PartialEq)] +// pub struct ExprNumberLiteral { +// pub range: TextRange, +// pub value: Number, +// } #[derive(Clone, Debug, PartialEq, is_macro::Is)] pub enum Number { @@ -2360,55 +2362,55 @@ pub enum Number { Complex { real: f64, imag: f64 }, } -#[derive(Clone, Debug, Default, PartialEq)] -pub struct ExprBooleanLiteral { - pub range: TextRange, - pub value: bool, -} +// #[derive(Clone, Debug, Default, PartialEq)] +// pub struct ExprBooleanLiteral { +// pub range: TextRange, +// pub value: bool, +// } -#[derive(Clone, Debug, Default, PartialEq)] -pub struct ExprNoneLiteral { - pub range: TextRange, -} +// #[derive(Clone, Debug, Default, PartialEq)] +// pub struct ExprNoneLiteral { +// pub range: TextRange, +// } -#[derive(Clone, Debug, Default, PartialEq)] -pub struct ExprEllipsisLiteral { - pub range: TextRange, -} +// #[derive(Clone, Debug, Default, PartialEq)] +// pub struct ExprEllipsisLiteral { +// pub range: TextRange, +// } -/// See also [Attribute](https://docs.python.org/3/library/ast.html#ast.Attribute) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprAttribute { - pub range: TextRange, - pub value: Box, - pub attr: Identifier, - pub ctx: ExprContext, -} +// /// See also [Attribute](https://docs.python.org/3/library/ast.html#ast.Attribute) +// #[derive(Clone, Debug, PartialEq)] +// pub struct ExprAttribute { +// pub range: TextRange, +// pub value: Box, +// pub attr: Identifier, +// pub ctx: ExprContext, +// } -/// See also [Subscript](https://docs.python.org/3/library/ast.html#ast.Subscript) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprSubscript { - pub range: TextRange, - pub value: Box, - pub slice: Box, - pub ctx: ExprContext, -} +// /// See also [Subscript](https://docs.python.org/3/library/ast.html#ast.Subscript) +// #[derive(Clone, Debug, PartialEq)] +// pub struct ExprSubscript { +// pub range: TextRange, +// pub value: Box, +// pub slice: Box, +// pub ctx: ExprContext, +// } -/// See also [Starred](https://docs.python.org/3/library/ast.html#ast.Starred) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprStarred { - pub range: TextRange, - pub value: Box, - pub ctx: ExprContext, -} +// /// See also [Starred](https://docs.python.org/3/library/ast.html#ast.Starred) +// #[derive(Clone, Debug, PartialEq)] +// pub struct ExprStarred { +// pub range: TextRange, +// pub value: Box, +// pub ctx: ExprContext, +// } -/// See also [Name](https://docs.python.org/3/library/ast.html#ast.Name) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprName { - pub range: TextRange, - pub id: Name, - pub ctx: ExprContext, -} +// /// See also [Name](https://docs.python.org/3/library/ast.html#ast.Name) +// #[derive(Clone, Debug, PartialEq)] +// pub struct ExprName { +// pub range: TextRange, +// pub id: Name, +// pub ctx: ExprContext, +// } impl ExprName { pub fn id(&self) -> &Name { @@ -2416,13 +2418,13 @@ impl ExprName { } } -/// See also [List](https://docs.python.org/3/library/ast.html#ast.List) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprList { - pub range: TextRange, - pub elts: Vec, - pub ctx: ExprContext, -} +// /// See also [List](https://docs.python.org/3/library/ast.html#ast.List) +// #[derive(Clone, Debug, PartialEq)] +// pub struct ExprList { +// pub range: TextRange, +// pub elts: Vec, +// pub ctx: ExprContext, +// } impl ExprList { pub fn iter(&self) -> std::slice::Iter<'_, Expr> { @@ -2447,16 +2449,16 @@ impl<'a> IntoIterator for &'a ExprList { } } -/// See also [Tuple](https://docs.python.org/3/library/ast.html#ast.Tuple) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprTuple { - pub range: TextRange, - pub elts: Vec, - pub ctx: ExprContext, - - /// Whether the tuple is parenthesized in the source code. - pub parenthesized: bool, -} +// /// See also [Tuple](https://docs.python.org/3/library/ast.html#ast.Tuple) +// #[derive(Clone, Debug, PartialEq)] +// pub struct ExprTuple { +// pub range: TextRange, +// pub elts: Vec, +// pub ctx: ExprContext, +// +// /// Whether the tuple is parenthesized in the source code. +// pub parenthesized: bool, +// } impl ExprTuple { pub fn iter(&self) -> std::slice::Iter<'_, Expr> { @@ -2481,14 +2483,14 @@ impl<'a> IntoIterator for &'a ExprTuple { } } -/// See also [Slice](https://docs.python.org/3/library/ast.html#ast.Slice) -#[derive(Clone, Debug, PartialEq)] -pub struct ExprSlice { - pub range: TextRange, - pub lower: Option>, - pub upper: Option>, - pub step: Option>, -} +// /// See also [Slice](https://docs.python.org/3/library/ast.html#ast.Slice) +// #[derive(Clone, Debug, PartialEq)] +// pub struct ExprSlice { +// pub range: TextRange, +// pub lower: Option>, +// pub upper: Option>, +// pub step: Option>, +// } /// See also [expr_context](https://docs.python.org/3/library/ast.html#ast.expr_context) #[derive(Clone, Debug, PartialEq, is_macro::Is, Copy, Hash, Eq)] @@ -3625,6 +3627,7 @@ mod tests { #[allow(clippy::wildcard_imports)] use super::*; + use crate::generated::*; use crate::Mod; #[test] @@ -3641,25 +3644,25 @@ mod tests { assert_eq!(std::mem::size_of::(), 64); assert_eq!(std::mem::size_of::(), 56); assert_eq!(std::mem::size_of::(), 16); - assert_eq!(std::mem::size_of::(), 32); - assert_eq!(std::mem::size_of::(), 40); + assert_eq!(std::mem::size_of::(), 32); + assert_eq!(std::mem::size_of::(), 40); assert_eq!(std::mem::size_of::(), 12); assert_eq!(std::mem::size_of::(), 40); assert_eq!(std::mem::size_of::(), 56); assert_eq!(std::mem::size_of::(), 48); - assert_eq!(std::mem::size_of::(), 32); + assert_eq!(std::mem::size_of::(), 32); assert_eq!(std::mem::size_of::(), 48); assert_eq!(std::mem::size_of::(), 8); // 56 for Rustc < 1.76 assert!(matches!(std::mem::size_of::(), 48 | 56)); assert_eq!(std::mem::size_of::(), 48); - assert_eq!(std::mem::size_of::(), 32); + assert_eq!(std::mem::size_of::(), 32); assert_eq!(std::mem::size_of::(), 32); - assert_eq!(std::mem::size_of::(), 24); + assert_eq!(std::mem::size_of::(), 24); assert_eq!(std::mem::size_of::(), 40); assert_eq!(std::mem::size_of::(), 40); assert_eq!(std::mem::size_of::(), 40); - assert_eq!(std::mem::size_of::(), 24); + assert_eq!(std::mem::size_of::(), 24); assert_eq!(std::mem::size_of::(), 8); assert_eq!(std::mem::size_of::(), 32); assert_eq!(std::mem::size_of::(), 32); @@ -3669,7 +3672,7 @@ mod tests { assert_eq!(std::mem::size_of::(), 56); assert_eq!(std::mem::size_of::(), 32); assert_eq!(std::mem::size_of::(), 40); - assert_eq!(std::mem::size_of::(), 24); + assert_eq!(std::mem::size_of::(), 24); assert_eq!(std::mem::size_of::(), 16); assert_eq!(std::mem::size_of::(), 16); } diff --git a/crates/ruff_python_ast/src/relocate.rs b/crates/ruff_python_ast/src/relocate.rs index 51faf19d178d7b..803aea06f029b2 100644 --- a/crates/ruff_python_ast/src/relocate.rs +++ b/crates/ruff_python_ast/src/relocate.rs @@ -1,6 +1,7 @@ use ruff_text_size::TextRange; use crate::visitor::transformer::{walk_expr, walk_keyword, Transformer}; +use crate::{self as ast}; use crate::{Expr, Keyword}; /// Change an expression's location (recursively) to match a desired, fixed @@ -17,100 +18,100 @@ struct Relocator { impl Transformer for Relocator { fn visit_expr(&self, expr: &mut Expr) { match expr { - Expr::BoolOp(crate::ExprBoolOp { range, .. }) => { + Expr::BoolOp(ast::ExprBoolOp { range, .. }) => { *range = self.range; } - Expr::Named(crate::ExprNamed { range, .. }) => { + Expr::Named(ast::ExprNamed { range, .. }) => { *range = self.range; } - Expr::BinOp(crate::ExprBinOp { range, .. }) => { + Expr::BinOp(ast::ExprBinOp { range, .. }) => { *range = self.range; } - Expr::UnaryOp(crate::ExprUnaryOp { range, .. }) => { + Expr::UnaryOp(ast::ExprUnaryOp { range, .. }) => { *range = self.range; } - Expr::Lambda(crate::ExprLambda { range, .. }) => { + Expr::Lambda(ast::ExprLambda { range, .. }) => { *range = self.range; } - Expr::If(crate::ExprIf { range, .. }) => { + Expr::If(ast::ExprIf { range, .. }) => { *range = self.range; } - Expr::Dict(crate::ExprDict { range, .. }) => { + Expr::Dict(ast::ExprDict { range, .. }) => { *range = self.range; } - Expr::Set(crate::ExprSet { range, .. }) => { + Expr::Set(ast::ExprSet { range, .. }) => { *range = self.range; } - Expr::ListComp(crate::ExprListComp { range, .. }) => { + Expr::ListComp(ast::ExprListComp { range, .. }) => { *range = self.range; } - Expr::SetComp(crate::ExprSetComp { range, .. }) => { + Expr::SetComp(ast::ExprSetComp { range, .. }) => { *range = self.range; } - Expr::DictComp(crate::ExprDictComp { range, .. }) => { + Expr::DictComp(ast::ExprDictComp { range, .. }) => { *range = self.range; } - Expr::Generator(crate::ExprGenerator { range, .. }) => { + Expr::Generator(ast::ExprGenerator { range, .. }) => { *range = self.range; } - Expr::Await(crate::ExprAwait { range, .. }) => { + Expr::Await(ast::ExprAwait { range, .. }) => { *range = self.range; } - Expr::Yield(crate::ExprYield { range, .. }) => { + Expr::Yield(ast::ExprYield { range, .. }) => { *range = self.range; } - Expr::YieldFrom(crate::ExprYieldFrom { range, .. }) => { + Expr::YieldFrom(ast::ExprYieldFrom { range, .. }) => { *range = self.range; } - Expr::Compare(crate::ExprCompare { range, .. }) => { + Expr::Compare(ast::ExprCompare { range, .. }) => { *range = self.range; } - Expr::Call(crate::ExprCall { range, .. }) => { + Expr::Call(ast::ExprCall { range, .. }) => { *range = self.range; } - Expr::FString(crate::ExprFString { range, .. }) => { + Expr::FString(ast::ExprFString { range, .. }) => { *range = self.range; } - Expr::StringLiteral(crate::ExprStringLiteral { range, .. }) => { + Expr::StringLiteral(ast::ExprStringLiteral { range, .. }) => { *range = self.range; } - Expr::BytesLiteral(crate::ExprBytesLiteral { range, .. }) => { + Expr::BytesLiteral(ast::ExprBytesLiteral { range, .. }) => { *range = self.range; } - Expr::NumberLiteral(crate::ExprNumberLiteral { range, .. }) => { + Expr::NumberLiteral(ast::ExprNumberLiteral { range, .. }) => { *range = self.range; } - Expr::BooleanLiteral(crate::ExprBooleanLiteral { range, .. }) => { + Expr::BooleanLiteral(ast::ExprBooleanLiteral { range, .. }) => { *range = self.range; } - Expr::NoneLiteral(crate::ExprNoneLiteral { range }) => { + Expr::NoneLiteral(ast::ExprNoneLiteral { range }) => { *range = self.range; } - Expr::EllipsisLiteral(crate::ExprEllipsisLiteral { range }) => { + Expr::EllipsisLiteral(ast::ExprEllipsisLiteral { range }) => { *range = self.range; } - Expr::Attribute(crate::ExprAttribute { range, .. }) => { + Expr::Attribute(ast::ExprAttribute { range, .. }) => { *range = self.range; } - Expr::Subscript(crate::ExprSubscript { range, .. }) => { + Expr::Subscript(ast::ExprSubscript { range, .. }) => { *range = self.range; } - Expr::Starred(crate::ExprStarred { range, .. }) => { + Expr::Starred(ast::ExprStarred { range, .. }) => { *range = self.range; } - Expr::Name(crate::ExprName { range, .. }) => { + Expr::Name(ast::ExprName { range, .. }) => { *range = self.range; } - Expr::List(crate::ExprList { range, .. }) => { + Expr::List(ast::ExprList { range, .. }) => { *range = self.range; } - Expr::Tuple(crate::ExprTuple { range, .. }) => { + Expr::Tuple(ast::ExprTuple { range, .. }) => { *range = self.range; } - Expr::Slice(crate::ExprSlice { range, .. }) => { + Expr::Slice(ast::ExprSlice { range, .. }) => { *range = self.range; } - Expr::IpyEscapeCommand(crate::ExprIpyEscapeCommand { range, .. }) => { + Expr::IpyEscapeCommand(ast::ExprIpyEscapeCommand { range, .. }) => { *range = self.range; } } From cfad8f480186aa73137a8a5410e1747232423090 Mon Sep 17 00:00:00 2001 From: Glyphack Date: Fri, 21 Feb 2025 23:31:48 +0100 Subject: [PATCH 3/8] Fix linter issues --- crates/ruff_python_ast/ast.toml | 2 +- crates/ruff_python_ast/generate.py | 8 ++------ crates/ruff_python_ast/src/nodes.rs | 7 +++++-- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/crates/ruff_python_ast/ast.toml b/crates/ruff_python_ast/ast.toml index b447165681fa2b..7fe48f5fae9398 100644 --- a/crates/ruff_python_ast/ast.toml +++ b/crates/ruff_python_ast/ast.toml @@ -142,7 +142,7 @@ ExprYieldFrom = { fields = [ ]} ExprCompare = { fields = [ { name = "left", type = "Expr" }, - # We don't want this field to be a vec hence the seq is not enabled. + # TODO: We don't want this field to be a vec hence the seq is not enabled. { name = "ops", type = "Box<[crate::CmpOp]>"}, { name = "comparators", type = "Box<[Expr]>"} ]} diff --git a/crates/ruff_python_ast/generate.py b/crates/ruff_python_ast/generate.py index 2864e06e553bf6..d71f7503ead453 100644 --- a/crates/ruff_python_ast/generate.py +++ b/crates/ruff_python_ast/generate.py @@ -572,8 +572,6 @@ def write_nodekind(out: list[str], ast: Ast) -> None: def write_node(out: list[str], ast: Ast) -> None: - group_names = [group.name for group in ast.groups] - node_names = [node.name for node in ast.all_nodes] for group in ast.groups: for node in group.nodes: if node.fields is None: @@ -589,12 +587,10 @@ def write_node(out: list[str], ast: Ast) -> None: for field in node.fields: field_str = f"pub {field.name}: " inner = f"crate::{field.ty}" - if (field.ty == "Expr" or (field.ty in group_names)) and ( - field.seq is False - ): - inner = f"Box<{inner}>" if field.ty == "bool" or field.ty.startswith("Box"): inner = field.ty + if field.ty == group.name and field.seq is False: + inner = f"Box<{inner}>" if field.seq: field_str += f"Vec<{inner}>," diff --git a/crates/ruff_python_ast/src/nodes.rs b/crates/ruff_python_ast/src/nodes.rs index a864e9cc09b697..055ffe59835a79 100644 --- a/crates/ruff_python_ast/src/nodes.rs +++ b/crates/ruff_python_ast/src/nodes.rs @@ -1,6 +1,9 @@ #![allow(clippy::derive_partial_eq_without_eq)] -use crate::generated::*; +use crate::generated::{ + ExprBytesLiteral, ExprDict, ExprFString, ExprList, ExprName, ExprSet, ExprStringLiteral, + ExprTuple, +}; use std::borrow::Cow; use std::fmt; use std::fmt::Debug; @@ -639,7 +642,7 @@ impl DoubleEndedIterator for DictValueIterator<'_> { impl FusedIterator for DictValueIterator<'_> {} impl ExactSizeIterator for DictValueIterator<'_> {} -/// See also [Set](https://docs.python.org/3/library/ast.html#ast.Set) +// /// See also [Set](https://docs.python.org/3/library/ast.html#ast.Set) // #[derive(Clone, Debug, PartialEq)] // pub struct ExprSet { // pub range: TextRange, From 8b9bbb6fb9563063c576e1f30535d124ab42354a Mon Sep 17 00:00:00 2001 From: Shaygan Hooshyari Date: Tue, 25 Feb 2025 19:08:45 +0100 Subject: [PATCH 4/8] Update crates/ruff_python_ast/generate.py Co-authored-by: Douglas Creager --- crates/ruff_python_ast/generate.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/ruff_python_ast/generate.py b/crates/ruff_python_ast/generate.py index d71f7503ead453..da86ce2c1567bf 100644 --- a/crates/ruff_python_ast/generate.py +++ b/crates/ruff_python_ast/generate.py @@ -577,8 +577,8 @@ def write_node(out: list[str], ast: Ast) -> None: if node.fields is None: continue out.append( - "#[derive(Clone, Debug, PartialEq, " - + ", ".join(node.derives or []) + "#[derive(Clone, Debug, PartialEq" + + "".join(f", {derive}" for derive in node.derives) + ")]" ) name = node.name From ce4cf6f8a5ead4cc584ef34e056fb0dd1477e925 Mon Sep 17 00:00:00 2001 From: Glyphack Date: Tue, 25 Feb 2025 20:19:56 +0100 Subject: [PATCH 5/8] Apply review suggestions --- crates/ruff_python_ast/ast.toml | 432 +++++++++++++++++------- crates/ruff_python_ast/generate.py | 28 +- crates/ruff_python_ast/src/generated.rs | 4 +- crates/ruff_python_ast/src/nodes.rs | 268 --------------- 4 files changed, 333 insertions(+), 399 deletions(-) diff --git a/crates/ruff_python_ast/ast.toml b/crates/ruff_python_ast/ast.toml index 7fe48f5fae9398..1b449d700ed581 100644 --- a/crates/ruff_python_ast/ast.toml +++ b/crates/ruff_python_ast/ast.toml @@ -80,130 +80,314 @@ add_suffix_to_is_methods = true anynode_is_label = "expression" rustdoc = "/// See also [expr](https://docs.python.org/3/library/ast.html#ast.expr)" -[Expr.nodes] -ExprBoolOp = { fields = [ - { name = "op", type = "BoolOp" }, - { name = "values", type = "Expr", seq = true } -]} -ExprNamed = { fields = [ - { name = "target", type = "Expr" }, - { name = "value", type = "Expr" } -]} -ExprBinOp = { fields = [ - { name = "left", type = "Expr" }, - { name = "op", type = "Operator" }, - { name = "right", type = "Expr" } -]} -ExprUnaryOp = { fields = [ - { name = "op", type = "UnaryOp" }, - { name = "operand", type = "Expr" } -]} -ExprLambda = { fields = [ - { name = "parameters", type = "Box", optional = true }, - { name = "body", type = "Expr" } -]} -ExprIf = { fields = [ - { name = "test", type = "Expr" }, - { name = "body", type = "Expr" }, - { name = "orelse", type = "Expr" } -]} -ExprDict = { fields = [ - { name = "items", type = "DictItem", seq = true }, -]} -ExprSet = { fields = [ - { name = "elts", type = "Expr", seq = true } -]} -ExprListComp = { fields = [ - { name = "elt", type = "Expr" }, - { name = "generators", type = "Comprehension", seq = true } -]} -ExprSetComp = { fields = [ - { name = "elt", type = "Expr" }, - { name = "generators", type = "Comprehension", seq = true } -]} -ExprDictComp = { fields = [ - { name = "key", type = "Expr" }, - { name = "value", type = "Expr" }, - { name = "generators", type = "Comprehension", seq = true } -]} -ExprGenerator = { fields = [ - { name = "elt", type = "Expr" }, - { name = "generators", type = "Comprehension", seq = true }, - { name = "parenthesized", type = "bool" } -]} -ExprAwait = { fields = [ - { name = "value", type = "Expr" } -]} -ExprYield = { fields = [ - { name = "value", type = "Expr", optional = true } -]} -ExprYieldFrom = { fields = [ - { name = "value", type = "Expr" } -]} -ExprCompare = { fields = [ - { name = "left", type = "Expr" }, - # TODO: We don't want this field to be a vec hence the seq is not enabled. - { name = "ops", type = "Box<[crate::CmpOp]>"}, - { name = "comparators", type = "Box<[Expr]>"} -]} -ExprCall = { fields = [ - { name = "func", type = "Expr" }, - { name = "arguments", type = "Arguments"} -]} -ExprFString = { fields = [ - { name = "value", type = "FStringValue" } -]} -ExprStringLiteral = { fields = [ - { name = "value", type = "StringLiteralValue" } -]} -ExprBytesLiteral = { fields = [ - { name = "value", type = "BytesLiteralValue" } -]} -ExprNumberLiteral = { fields = [ - { name = "value", type = "Number" } -]} -ExprBooleanLiteral = { fields = [ - { name = "value", type = "bool" } -], derives = ["Default"]} -ExprNoneLiteral = { fields = [], derives = ["Default"]} -ExprEllipsisLiteral = { fields = [], derives = ["Default"]} -ExprAttribute = { fields = [ - { name = "value", type = "Expr" }, - { name = "attr", type = "Identifier" }, - { name = "ctx", type = "ExprContext" } -]} -ExprSubscript = { fields = [ - { name = "value", type = "Expr" }, - { name = "slice", type = "Expr" }, - { name = "ctx", type = "ExprContext" } -]} -ExprStarred = { fields = [ - { name = "value", type = "Expr" }, - { name = "ctx", type = "ExprContext" } -]} -ExprName = { fields = [ - { name = "id", type = "name::Name" }, - { name = "ctx", type = "ExprContext" } -]} -ExprList = { fields = [ - { name = "elts", type = "Expr", seq = true }, - { name = "ctx", type = "ExprContext" } -]} -ExprTuple = { fields = [ - { name = "elts", type = "Expr", seq = true }, - { name = "ctx", type = "ExprContext" }, - { name = "parenthesized", type = "bool" } -]} -ExprSlice = { fields = [ - { name = "lower", type = "Expr", optional = true }, - { name = "upper", type = "Expr", optional = true }, - { name = "step", type = "Expr", optional = true } -]} -ExprIpyEscapeCommand = { fields = [ - { name = "kind", type = "IpyEscapeKind" }, - { name = "value", type = "Box" } - -]} +# See also [BoolOp](https://docs.python.org/3/library/ast.html#ast.BoolOp) +[[Expr.nodes.ExprBoolOp.fields]] +name = "op" +type = "BoolOp" + +[[Expr.nodes.ExprBoolOp.fields]] +name = "values" +type = "Expr" +seq = true + +# See also [NamedExpr](https://docs.python.org/3/library/ast.html#ast.NamedExpr) +[[Expr.nodes.ExprNamed.fields]] +name = "target" +type = "Expr" + +[[Expr.nodes.ExprNamed.fields]] +name = "value" +type = "Expr" + +# See also [BinOp](https://docs.python.org/3/library/ast.html#ast.BinOp) +[[Expr.nodes.ExprBinOp.fields]] +name = "left" +type = "Expr" + +[[Expr.nodes.ExprBinOp.fields]] +name = "op" +type = "Operator" + +[[Expr.nodes.ExprBinOp.fields]] +name = "right" +type = "Expr" + +# See also [UnaryOp](https://docs.python.org/3/library/ast.html#ast.UnaryOp) +[[Expr.nodes.ExprUnaryOp.fields]] +name = "op" +type = "UnaryOp" + +[[Expr.nodes.ExprUnaryOp.fields]] +name = "operand" +type = "Expr" + +# See also [Lambda](https://docs.python.org/3/library/ast.html#ast.Lambda) +[[Expr.nodes.ExprLambda.fields]] +name = "parameters" +type = "Box" +optional = true + +[[Expr.nodes.ExprLambda.fields]] +name = "body" +type = "Expr" + +# See also [IfExp](https://docs.python.org/3/library/ast.html#ast.IfExp) +[[Expr.nodes.ExprIf.fields]] +name = "test" +type = "Expr" + +[[Expr.nodes.ExprIf.fields]] +name = "body" +type = "Expr" + +[[Expr.nodes.ExprIf.fields]] +name = "orelse" +type = "Expr" + +# See also [Dict](https://docs.python.org/3/library/ast.html#ast.Dict) +[[Expr.nodes.ExprDict.fields]] +name = "items" +type = "DictItem" +seq = true + +# See also [Set](https://docs.python.org/3/library/ast.html#ast.Set) +[[Expr.nodes.ExprSet.fields]] +name = "elts" +type = "Expr" +seq = true + +# See also [ListComp](https://docs.python.org/3/library/ast.html#ast.ListComp) +[[Expr.nodes.ExprListComp.fields]] +name = "elt" +type = "Expr" + +[[Expr.nodes.ExprListComp.fields]] +name = "generators" +type = "Comprehension" +seq = true + +# See also [SetComp](https://docs.python.org/3/library/ast.html#ast.SetComp) +[[Expr.nodes.ExprSetComp.fields]] +name = "elt" +type = "Expr" + +[[Expr.nodes.ExprSetComp.fields]] +name = "generators" +type = "Comprehension" +seq = true + +# See also [DictComp](https://docs.python.org/3/library/ast.html#ast.DictComp) +[[Expr.nodes.ExprDictComp.fields]] +name = "key" +type = "Expr" + +[[Expr.nodes.ExprDictComp.fields]] +name = "value" +type = "Expr" + +[[Expr.nodes.ExprDictComp.fields]] +name = "generators" +type = "Comprehension" +seq = true + +# See also [GeneratorExp](https://docs.python.org/3/library/ast.html#ast.GeneratorExp) +[[Expr.nodes.ExprGenerator.fields]] +name = "elt" +type = "Expr" + +[[Expr.nodes.ExprGenerator.fields]] +name = "generators" +type = "Comprehension" +seq = true + +[[Expr.nodes.ExprGenerator.fields]] +name = "parenthesized" +type = "bool" + +# See also [Await](https://docs.python.org/3/library/ast.html#ast.Await) +[[Expr.nodes.ExprAwait.fields]] +name = "value" +type = "Expr" + +# See also [Yield](https://docs.python.org/3/library/ast.html#ast.Yield) +[[Expr.nodes.ExprYield.fields]] +name = "value" +type = "Expr" +optional = true + +# See also [YieldFrom](https://docs.python.org/3/library/ast.html#ast.YieldFrom) +[[Expr.nodes.ExprYieldFrom.fields]] +name = "value" +type = "Expr" + +# See also [Compare](https://docs.python.org/3/library/ast.html#ast.Compare) +[[Expr.nodes.ExprCompare.fields]] +name = "left" +type = "Expr" + +[[Expr.nodes.ExprCompare.fields]] +name = "ops" +type = "Box<[crate::CmpOp]>" + +[[Expr.nodes.ExprCompare.fields]] +name = "comparators" +type = "Box<[Expr]>" + +# See also [Call](https://docs.python.org/3/library/ast.html#ast.Call) +[[Expr.nodes.ExprCall.fields]] +name = "func" +type = "Expr" + +[[Expr.nodes.ExprCall.fields]] +name = "arguments" +type = "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. +# +# [JoinedStr]: https://docs.python.org/3/library/ast.html#ast.JoinedStr +[[Expr.nodes.ExprFString.fields]] +name = "value" +type = "FStringValue" + +# An AST node that represents either a single-part string literal +# or an implicitly concatenated string literal. +[[Expr.nodes.ExprStringLiteral.fields]] +name = "value" +type = "StringLiteralValue" + +# An AST node that represents either a single-part bytestring literal +# or an implicitly concatenated bytestring literal. +[[Expr.nodes.ExprBytesLiteral.fields]] +name = "value" +type = "BytesLiteralValue" + +[[Expr.nodes.ExprNumberLiteral.fields]] +name = "value" +type = "Number" + +[[Expr.nodes.ExprBooleanLiteral.fields]] +name = "value" +type = "bool" + +[Expr.nodes.ExprBooleanLiteral] +derives = ["Default"] + +[Expr.nodes.ExprNoneLiteral] +fields = [] +derives = ["Default"] + +[Expr.nodes.ExprEllipsisLiteral] +fields = [] +derives = ["Default"] + +# See also [Attribute](https://docs.python.org/3/library/ast.html#ast.Attribute) +[[Expr.nodes.ExprAttribute.fields]] +name = "value" +type = "Expr" + +[[Expr.nodes.ExprAttribute.fields]] +name = "attr" +type = "Identifier" + +[[Expr.nodes.ExprAttribute.fields]] +name = "ctx" +type = "ExprContext" + +# See also [Subscript](https://docs.python.org/3/library/ast.html#ast.Subscript) +[[Expr.nodes.ExprSubscript.fields]] +name = "value" +type = "Expr" + +[[Expr.nodes.ExprSubscript.fields]] +name = "slice" +type = "Expr" + +[[Expr.nodes.ExprSubscript.fields]] +name = "ctx" +type = "ExprContext" + +# See also [Starred](https://docs.python.org/3/library/ast.html#ast.Starred) +[[Expr.nodes.ExprStarred.fields]] +name = "value" +type = "Expr" + +[[Expr.nodes.ExprStarred.fields]] +name = "ctx" +type = "ExprContext" + +# See also [Name](https://docs.python.org/3/library/ast.html#ast.Name) +[[Expr.nodes.ExprName.fields]] +name = "id" +type = "Name" + +[[Expr.nodes.ExprName.fields]] +name = "ctx" +type = "ExprContext" + +# See also [List](https://docs.python.org/3/library/ast.html#ast.List) +[[Expr.nodes.ExprList.fields]] +name = "elts" +type = "Expr" +seq = true + +[[Expr.nodes.ExprList.fields]] +name = "ctx" +type = "ExprContext" + +# See also [Tuple](https://docs.python.org/3/library/ast.html#ast.Tuple) +[[Expr.nodes.ExprTuple.fields]] +name = "elts" +type = "Expr" +seq = true + +[[Expr.nodes.ExprTuple.fields]] +name = "ctx" +type = "ExprContext" + +[[Expr.nodes.ExprTuple.fields]] +name = "parenthesized" +type = "bool" + +# See also [Slice](https://docs.python.org/3/library/ast.html#ast.Slice) +[[Expr.nodes.ExprSlice.fields]] +name = "lower" +type = "Expr" +optional = true + +[[Expr.nodes.ExprSlice.fields]] +name = "upper" +type = "Expr" +optional = true + +[[Expr.nodes.ExprSlice.fields]] +name = "step" +type = "Expr" +optional = true + +# 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 [`StmtIpyEscapeCommand`]. +[[Expr.nodes.ExprIpyEscapeCommand.fields]] +name = "kind" +type = "IpyEscapeKind" + +[[Expr.nodes.ExprIpyEscapeCommand.fields]] +name = "value" +type = "Box" + [ExceptHandler] rustdoc = "/// See also [excepthandler](https://docs.python.org/3/library/ast.html#ast.excepthandler)" diff --git a/crates/ruff_python_ast/generate.py b/crates/ruff_python_ast/generate.py index da86ce2c1567bf..76fa29c8a2fe1a 100644 --- a/crates/ruff_python_ast/generate.py +++ b/crates/ruff_python_ast/generate.py @@ -14,6 +14,16 @@ import tomllib +imports = ["crate::name::Name"] + + +def requires_crate_prefix(ty: str) -> bool: + no_crate_prefix_types = [ + "Name", # Imported at the top + "bool", + ] + return not (ty in no_crate_prefix_types or ty.startswith("Box")) + def rustfmt(code: str) -> str: return check_output(["rustfmt", "--emit=stdout"], input=code, text=True) @@ -91,7 +101,7 @@ class Node: variant: str ty: str fields: list[Field] | None - derives: list[str] | None + derives: list[str] def __init__(self, group: Group, node_name: str, node: dict[str, Any]) -> None: self.name = node_name @@ -123,9 +133,12 @@ def __init__(self, field: dict[str, Any]) -> None: def write_preamble(out: list[str]) -> None: - out.append(""" + import_section = "\n".join(f"use {im};" for im in imports) + out.append(f""" // This is a generated file. Don't modify it by hand! // Run `crates/ruff_python_ast/generate.py` to re-generate the file. + + { import_section } """) @@ -569,9 +582,11 @@ def write_nodekind(out: list[str], ast: Ast) -> None: # ------------------------------------------------------------------------------ +# Node structs def write_node(out: list[str], ast: Ast) -> None: + group_names = [group.name for group in ast.groups] for group in ast.groups: for node in group.nodes: if node.fields is None: @@ -586,10 +601,11 @@ def write_node(out: list[str], ast: Ast) -> None: out.append("pub range: ruff_text_size::TextRange,") for field in node.fields: field_str = f"pub {field.name}: " - inner = f"crate::{field.ty}" - if field.ty == "bool" or field.ty.startswith("Box"): - inner = field.ty - if field.ty == group.name and field.seq is False: + + inner = f"{field.ty}" + if requires_crate_prefix(field.ty): + inner = f"crate::{inner}" + if field.ty in group_names and field.seq is False: inner = f"Box<{inner}>" if field.seq: diff --git a/crates/ruff_python_ast/src/generated.rs b/crates/ruff_python_ast/src/generated.rs index a4f5ba4ef1c205..0017f7d0c2359e 100644 --- a/crates/ruff_python_ast/src/generated.rs +++ b/crates/ruff_python_ast/src/generated.rs @@ -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 { @@ -6631,7 +6633,7 @@ pub struct ExprStarred { #[derive(Clone, Debug, PartialEq)] pub struct ExprName { pub range: ruff_text_size::TextRange, - pub id: crate::name::Name, + pub id: Name, pub ctx: crate::ExprContext, } diff --git a/crates/ruff_python_ast/src/nodes.rs b/crates/ruff_python_ast/src/nodes.rs index 055ffe59835a79..da38b42c5a8670 100644 --- a/crates/ruff_python_ast/src/nodes.rs +++ b/crates/ruff_python_ast/src/nodes.rs @@ -384,74 +384,6 @@ impl ExprRef<'_> { } } -// /// 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 [`StmtIpyEscapeCommand`]. -// #[derive(Clone, Debug, PartialEq)] -// pub struct ExprIpyEscapeCommand { -// pub range: TextRange, -// pub kind: IpyEscapeKind, -// pub value: Box, -// } - -// /// See also [BoolOp](https://docs.python.org/3/library/ast.html#ast.BoolOp) -// #[derive(Clone, Debug, PartialEq)] -// pub struct ExprBoolOp { -// pub range: TextRange, -// pub op: BoolOp, -// pub values: Vec, -// } - -// /// See also [NamedExpr](https://docs.python.org/3/library/ast.html#ast.NamedExpr) -// #[derive(Clone, Debug, PartialEq)] -// pub struct ExprNamed { -// pub range: TextRange, -// pub target: Box, -// pub value: Box, -// } - -// /// See also [BinOp](https://docs.python.org/3/library/ast.html#ast.BinOp) -// #[derive(Clone, Debug, PartialEq)] -// pub struct ExprBinOp { -// pub range: TextRange, -// pub left: Box, -// pub op: Operator, -// pub right: Box, -// } - -// /// See also [UnaryOp](https://docs.python.org/3/library/ast.html#ast.UnaryOp) -// #[derive(Clone, Debug, PartialEq)] -// pub struct ExprUnaryOp { -// pub range: TextRange, -// pub op: UnaryOp, -// pub operand: Box, -// } - -// /// See also [Lambda](https://docs.python.org/3/library/ast.html#ast.Lambda) -// #[derive(Clone, Debug, PartialEq)] -// pub struct ExprLambda { -// pub range: TextRange, -// pub parameters: Option>, -// pub body: Box, -// } - -// /// See also [IfExp](https://docs.python.org/3/library/ast.html#ast.IfExp) -// #[derive(Clone, Debug, PartialEq)] -// pub struct ExprIf { -// pub range: TextRange, -// pub test: Box, -// pub body: Box, -// pub orelse: Box, -// } - /// Represents an item in a [dictionary literal display][1]. /// /// Consider the following Python dictionary literal: @@ -500,13 +432,6 @@ impl Ranged for DictItem { } } -// /// See also [Dict](https://docs.python.org/3/library/ast.html#ast.Dict) -// #[derive(Clone, Debug, PartialEq)] -// pub struct ExprDict { -// pub range: TextRange, -// pub items: Vec, -// } - impl ExprDict { /// Returns an `Iterator` over the AST nodes representing the /// dictionary's keys. @@ -642,13 +567,6 @@ impl DoubleEndedIterator for DictValueIterator<'_> { impl FusedIterator for DictValueIterator<'_> {} impl ExactSizeIterator for DictValueIterator<'_> {} -// /// See also [Set](https://docs.python.org/3/library/ast.html#ast.Set) -// #[derive(Clone, Debug, PartialEq)] -// pub struct ExprSet { -// pub range: TextRange, -// pub elts: Vec, -// } - impl ExprSet { pub fn iter(&self) -> std::slice::Iter<'_, Expr> { self.elts.iter() @@ -672,78 +590,6 @@ impl<'a> IntoIterator for &'a ExprSet { } } -// /// See also [ListComp](https://docs.python.org/3/library/ast.html#ast.ListComp) -// #[derive(Clone, Debug, PartialEq)] -// pub struct ExprListComp { -// pub range: TextRange, -// pub elt: Box, -// pub generators: Vec, -// } - -// /// See also [SetComp](https://docs.python.org/3/library/ast.html#ast.SetComp) -// #[derive(Clone, Debug, PartialEq)] -// pub struct ExprSetComp { -// pub range: TextRange, -// pub elt: Box, -// pub generators: Vec, -// } - -// /// See also [DictComp](https://docs.python.org/3/library/ast.html#ast.DictComp) -// #[derive(Clone, Debug, PartialEq)] -// pub struct ExprDictComp { -// pub range: TextRange, -// pub key: Box, -// pub value: Box, -// pub generators: Vec, -// } - -// /// See also [GeneratorExp](https://docs.python.org/3/library/ast.html#ast.GeneratorExp) -// #[derive(Clone, Debug, PartialEq)] -// pub struct ExprGenerator { -// pub range: TextRange, -// pub elt: Box, -// pub generators: Vec, -// 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: TextRange, -// pub value: Box, -// } - -// /// See also [Yield](https://docs.python.org/3/library/ast.html#ast.Yield) -// #[derive(Clone, Debug, PartialEq)] -// pub struct ExprYield { -// pub range: TextRange, -// pub value: Option>, -// } - -// /// See also [YieldFrom](https://docs.python.org/3/library/ast.html#ast.YieldFrom) -// #[derive(Clone, Debug, PartialEq)] -// pub struct ExprYieldFrom { -// pub range: TextRange, -// pub value: Box, -// } - -// /// See also [Compare](https://docs.python.org/3/library/ast.html#ast.Compare) -// #[derive(Clone, Debug, PartialEq)] -// pub struct ExprCompare { -// pub range: TextRange, -// pub left: Box, -// pub ops: Box<[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: TextRange, -// pub func: Box, -// pub arguments: Arguments, -// } - #[derive(Clone, Debug, PartialEq)] pub struct FStringFormatSpec { pub range: TextRange, @@ -816,20 +662,6 @@ pub struct DebugText { pub trailing: String, } -// /// 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. -// /// -// /// [JoinedStr]: https://docs.python.org/3/library/ast.html#ast.JoinedStr -// #[derive(Clone, Debug, PartialEq)] -// pub struct ExprFString { -// pub range: TextRange, -// pub value: FStringValue, -// } - impl ExprFString { /// Returns the single [`FString`] if the f-string isn't implicitly concatenated, [`None`] /// otherwise. @@ -1291,14 +1123,6 @@ impl fmt::Debug for FStringElements { } } -// /// 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: TextRange, -// pub value: StringLiteralValue, -// } - impl ExprStringLiteral { /// Return `Some(literal)` if the string only consists of a single `StringLiteral` part /// (indicating that it is not implicitly concatenated). Otherwise, return `None`. @@ -1743,14 +1567,6 @@ impl Debug for ConcatenatedStringLiteral { } } -// /// 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: TextRange, -// pub value: BytesLiteralValue, -// } - impl ExprBytesLiteral { /// Return `Some(literal)` if the bytestring only consists of a single `BytesLiteral` part /// (indicating that it is not implicitly concatenated). Otherwise, return `None`. @@ -2352,12 +2168,6 @@ impl From for AnyStringFlags { } } -// #[derive(Clone, Debug, PartialEq)] -// pub struct ExprNumberLiteral { -// pub range: TextRange, -// pub value: Number, -// } - #[derive(Clone, Debug, PartialEq, is_macro::Is)] pub enum Number { Int(int::Int), @@ -2365,70 +2175,12 @@ pub enum Number { Complex { real: f64, imag: f64 }, } -// #[derive(Clone, Debug, Default, PartialEq)] -// pub struct ExprBooleanLiteral { -// pub range: TextRange, -// pub value: bool, -// } - -// #[derive(Clone, Debug, Default, PartialEq)] -// pub struct ExprNoneLiteral { -// pub range: TextRange, -// } - -// #[derive(Clone, Debug, Default, PartialEq)] -// pub struct ExprEllipsisLiteral { -// pub range: TextRange, -// } - -// /// See also [Attribute](https://docs.python.org/3/library/ast.html#ast.Attribute) -// #[derive(Clone, Debug, PartialEq)] -// pub struct ExprAttribute { -// pub range: TextRange, -// pub value: Box, -// pub attr: Identifier, -// pub ctx: ExprContext, -// } - -// /// See also [Subscript](https://docs.python.org/3/library/ast.html#ast.Subscript) -// #[derive(Clone, Debug, PartialEq)] -// pub struct ExprSubscript { -// pub range: TextRange, -// pub value: Box, -// pub slice: Box, -// pub ctx: ExprContext, -// } - -// /// See also [Starred](https://docs.python.org/3/library/ast.html#ast.Starred) -// #[derive(Clone, Debug, PartialEq)] -// pub struct ExprStarred { -// pub range: TextRange, -// pub value: Box, -// pub ctx: ExprContext, -// } - -// /// See also [Name](https://docs.python.org/3/library/ast.html#ast.Name) -// #[derive(Clone, Debug, PartialEq)] -// pub struct ExprName { -// pub range: TextRange, -// pub id: Name, -// pub ctx: ExprContext, -// } - impl ExprName { pub fn id(&self) -> &Name { &self.id } } -// /// See also [List](https://docs.python.org/3/library/ast.html#ast.List) -// #[derive(Clone, Debug, PartialEq)] -// pub struct ExprList { -// pub range: TextRange, -// pub elts: Vec, -// pub ctx: ExprContext, -// } - impl ExprList { pub fn iter(&self) -> std::slice::Iter<'_, Expr> { self.elts.iter() @@ -2452,17 +2204,6 @@ impl<'a> IntoIterator for &'a ExprList { } } -// /// See also [Tuple](https://docs.python.org/3/library/ast.html#ast.Tuple) -// #[derive(Clone, Debug, PartialEq)] -// pub struct ExprTuple { -// pub range: TextRange, -// pub elts: Vec, -// pub ctx: ExprContext, -// -// /// Whether the tuple is parenthesized in the source code. -// pub parenthesized: bool, -// } - impl ExprTuple { pub fn iter(&self) -> std::slice::Iter<'_, Expr> { self.elts.iter() @@ -2486,15 +2227,6 @@ impl<'a> IntoIterator for &'a ExprTuple { } } -// /// See also [Slice](https://docs.python.org/3/library/ast.html#ast.Slice) -// #[derive(Clone, Debug, PartialEq)] -// pub struct ExprSlice { -// pub range: TextRange, -// pub lower: Option>, -// pub upper: Option>, -// pub step: Option>, -// } - /// See also [expr_context](https://docs.python.org/3/library/ast.html#ast.expr_context) #[derive(Clone, Debug, PartialEq, is_macro::Is, Copy, Hash, Eq)] pub enum ExprContext { From afba5fb13f414f6da8d1dfa7461c95f08c9fcf36 Mon Sep 17 00:00:00 2001 From: Glyphack Date: Tue, 25 Feb 2025 20:45:23 +0100 Subject: [PATCH 6/8] Move node comments into rustdoc --- crates/ruff_python_ast/ast.toml | 147 ++++++++++++++++-------- crates/ruff_python_ast/generate.py | 6 +- crates/ruff_python_ast/src/generated.rs | 44 +++++++ 3 files changed, 149 insertions(+), 48 deletions(-) diff --git a/crates/ruff_python_ast/ast.toml b/crates/ruff_python_ast/ast.toml index 1b449d700ed581..6002954c90a82c 100644 --- a/crates/ruff_python_ast/ast.toml +++ b/crates/ruff_python_ast/ast.toml @@ -80,7 +80,9 @@ add_suffix_to_is_methods = true anynode_is_label = "expression" rustdoc = "/// See also [expr](https://docs.python.org/3/library/ast.html#ast.expr)" -# See also [BoolOp](https://docs.python.org/3/library/ast.html#ast.BoolOp) +[Expr.nodes.ExprBoolOp] +rustdoc = "/// See also [BoolOp](https://docs.python.org/3/library/ast.html#ast.BoolOp)" + [[Expr.nodes.ExprBoolOp.fields]] name = "op" type = "BoolOp" @@ -90,7 +92,9 @@ name = "values" type = "Expr" seq = true -# See also [NamedExpr](https://docs.python.org/3/library/ast.html#ast.NamedExpr) +[Expr.nodes.ExprNamed] +rustdoc = "/// See also [NamedExpr](https://docs.python.org/3/library/ast.html#ast.NamedExpr)" + [[Expr.nodes.ExprNamed.fields]] name = "target" type = "Expr" @@ -99,7 +103,9 @@ type = "Expr" name = "value" type = "Expr" -# See also [BinOp](https://docs.python.org/3/library/ast.html#ast.BinOp) +[Expr.nodes.ExprBinOp] +rustdoc = "/// See also [BinOp](https://docs.python.org/3/library/ast.html#ast.BinOp)" + [[Expr.nodes.ExprBinOp.fields]] name = "left" type = "Expr" @@ -112,7 +118,9 @@ type = "Operator" name = "right" type = "Expr" -# See also [UnaryOp](https://docs.python.org/3/library/ast.html#ast.UnaryOp) +[Expr.nodes.ExprUnaryOp] +rustdoc = "/// See also [UnaryOp](https://docs.python.org/3/library/ast.html#ast.UnaryOp)" + [[Expr.nodes.ExprUnaryOp.fields]] name = "op" type = "UnaryOp" @@ -121,7 +129,9 @@ type = "UnaryOp" name = "operand" type = "Expr" -# See also [Lambda](https://docs.python.org/3/library/ast.html#ast.Lambda) +[Expr.nodes.ExprLambda] +rustdoc = "/// See also [Lambda](https://docs.python.org/3/library/ast.html#ast.Lambda)" + [[Expr.nodes.ExprLambda.fields]] name = "parameters" type = "Box" @@ -131,7 +141,9 @@ optional = true name = "body" type = "Expr" -# See also [IfExp](https://docs.python.org/3/library/ast.html#ast.IfExp) +[Expr.nodes.ExprIf] +rustdoc = "/// See also [IfExp](https://docs.python.org/3/library/ast.html#ast.IfExp)" + [[Expr.nodes.ExprIf.fields]] name = "test" type = "Expr" @@ -144,19 +156,25 @@ type = "Expr" name = "orelse" type = "Expr" -# See also [Dict](https://docs.python.org/3/library/ast.html#ast.Dict) +[Expr.nodes.ExprDict] +rustdoc = "/// See also [Dict](https://docs.python.org/3/library/ast.html#ast.Dict)" + [[Expr.nodes.ExprDict.fields]] name = "items" type = "DictItem" seq = true -# See also [Set](https://docs.python.org/3/library/ast.html#ast.Set) +[Expr.nodes.ExprSet] +rustdoc = "/// See also [Set](https://docs.python.org/3/library/ast.html#ast.Set)" + [[Expr.nodes.ExprSet.fields]] name = "elts" type = "Expr" seq = true -# See also [ListComp](https://docs.python.org/3/library/ast.html#ast.ListComp) +[Expr.nodes.ExprListComp] +rustdoc = "/// See also [ListComp](https://docs.python.org/3/library/ast.html#ast.ListComp)" + [[Expr.nodes.ExprListComp.fields]] name = "elt" type = "Expr" @@ -166,7 +184,9 @@ name = "generators" type = "Comprehension" seq = true -# See also [SetComp](https://docs.python.org/3/library/ast.html#ast.SetComp) +[Expr.nodes.ExprSetComp] +rustdoc = "/// See also [SetComp](https://docs.python.org/3/library/ast.html#ast.SetComp)" + [[Expr.nodes.ExprSetComp.fields]] name = "elt" type = "Expr" @@ -176,7 +196,9 @@ name = "generators" type = "Comprehension" seq = true -# See also [DictComp](https://docs.python.org/3/library/ast.html#ast.DictComp) +[Expr.nodes.ExprDictComp] +rustdoc = "/// See also [DictComp](https://docs.python.org/3/library/ast.html#ast.DictComp)" + [[Expr.nodes.ExprDictComp.fields]] name = "key" type = "Expr" @@ -190,7 +212,9 @@ name = "generators" type = "Comprehension" seq = true -# See also [GeneratorExp](https://docs.python.org/3/library/ast.html#ast.GeneratorExp) +[Expr.nodes.ExprGenerator] +rustdoc = "/// See also [GeneratorExp](https://docs.python.org/3/library/ast.html#ast.GeneratorExp)" + [[Expr.nodes.ExprGenerator.fields]] name = "elt" type = "Expr" @@ -204,23 +228,31 @@ seq = true name = "parenthesized" type = "bool" -# See also [Await](https://docs.python.org/3/library/ast.html#ast.Await) +[Expr.nodes.ExprAwait] +rustdoc = "/// See also [Await](https://docs.python.org/3/library/ast.html#ast.Await)" + [[Expr.nodes.ExprAwait.fields]] name = "value" type = "Expr" -# See also [Yield](https://docs.python.org/3/library/ast.html#ast.Yield) +[Expr.nodes.ExprYield] +rustdoc = "/// See also [Yield](https://docs.python.org/3/library/ast.html#ast.Yield)" + [[Expr.nodes.ExprYield.fields]] name = "value" type = "Expr" optional = true -# See also [YieldFrom](https://docs.python.org/3/library/ast.html#ast.YieldFrom) +[Expr.nodes.ExprYieldFrom] +rustdoc = "/// See also [YieldFrom](https://docs.python.org/3/library/ast.html#ast.YieldFrom)" + [[Expr.nodes.ExprYieldFrom.fields]] name = "value" type = "Expr" -# See also [Compare](https://docs.python.org/3/library/ast.html#ast.Compare) +[Expr.nodes.ExprCompare] +rustdoc = "/// See also [Compare](https://docs.python.org/3/library/ast.html#ast.Compare)" + [[Expr.nodes.ExprCompare.fields]] name = "left" type = "Expr" @@ -233,7 +265,9 @@ type = "Box<[crate::CmpOp]>" name = "comparators" type = "Box<[Expr]>" -# See also [Call](https://docs.python.org/3/library/ast.html#ast.Call) +[Expr.nodes.ExprCall] +rustdoc = "/// See also [Call](https://docs.python.org/3/library/ast.html#ast.Call)" + [[Expr.nodes.ExprCall.fields]] name = "func" type = "Expr" @@ -242,26 +276,32 @@ type = "Expr" name = "arguments" type = "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. -# -# [JoinedStr]: https://docs.python.org/3/library/ast.html#ast.JoinedStr +[Expr.nodes.ExprFString] +rustdoc = """/// 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. +/// +/// [JoinedStr]: https://docs.python.org/3/library/ast.html#ast.JoinedStr""" + [[Expr.nodes.ExprFString.fields]] name = "value" type = "FStringValue" -# An AST node that represents either a single-part string literal -# or an implicitly concatenated string literal. +[Expr.nodes.ExprStringLiteral] +rustdoc = """/// An AST node that represents either a single-part string literal +/// or an implicitly concatenated string literal.""" + [[Expr.nodes.ExprStringLiteral.fields]] name = "value" type = "StringLiteralValue" -# An AST node that represents either a single-part bytestring literal -# or an implicitly concatenated bytestring literal. +[Expr.nodes.ExprBytesLiteral] +rustdoc = """/// An AST node that represents either a single-part bytestring literal +/// or an implicitly concatenated bytestring literal.""" + [[Expr.nodes.ExprBytesLiteral.fields]] name = "value" type = "BytesLiteralValue" @@ -285,7 +325,9 @@ derives = ["Default"] fields = [] derives = ["Default"] -# See also [Attribute](https://docs.python.org/3/library/ast.html#ast.Attribute) +[Expr.nodes.ExprAttribute] +rustdoc = "/// See also [Attribute](https://docs.python.org/3/library/ast.html#ast.Attribute)" + [[Expr.nodes.ExprAttribute.fields]] name = "value" type = "Expr" @@ -298,7 +340,9 @@ type = "Identifier" name = "ctx" type = "ExprContext" -# See also [Subscript](https://docs.python.org/3/library/ast.html#ast.Subscript) +[Expr.nodes.ExprSubscript] +rustdoc = "/// See also [Subscript](https://docs.python.org/3/library/ast.html#ast.Subscript)" + [[Expr.nodes.ExprSubscript.fields]] name = "value" type = "Expr" @@ -311,7 +355,9 @@ type = "Expr" name = "ctx" type = "ExprContext" -# See also [Starred](https://docs.python.org/3/library/ast.html#ast.Starred) +[Expr.nodes.ExprStarred] +rustdoc = "/// See also [Starred](https://docs.python.org/3/library/ast.html#ast.Starred)" + [[Expr.nodes.ExprStarred.fields]] name = "value" type = "Expr" @@ -320,7 +366,9 @@ type = "Expr" name = "ctx" type = "ExprContext" -# See also [Name](https://docs.python.org/3/library/ast.html#ast.Name) +[Expr.nodes.ExprName] +rustdoc = "/// See also [Name](https://docs.python.org/3/library/ast.html#ast.Name)" + [[Expr.nodes.ExprName.fields]] name = "id" type = "Name" @@ -329,7 +377,9 @@ type = "Name" name = "ctx" type = "ExprContext" -# See also [List](https://docs.python.org/3/library/ast.html#ast.List) +[Expr.nodes.ExprList] +rustdoc = "/// See also [List](https://docs.python.org/3/library/ast.html#ast.List)" + [[Expr.nodes.ExprList.fields]] name = "elts" type = "Expr" @@ -339,7 +389,9 @@ seq = true name = "ctx" type = "ExprContext" -# See also [Tuple](https://docs.python.org/3/library/ast.html#ast.Tuple) +[Expr.nodes.ExprTuple] +rustdoc = "/// See also [Tuple](https://docs.python.org/3/library/ast.html#ast.Tuple)" + [[Expr.nodes.ExprTuple.fields]] name = "elts" type = "Expr" @@ -353,7 +405,9 @@ type = "ExprContext" name = "parenthesized" type = "bool" -# See also [Slice](https://docs.python.org/3/library/ast.html#ast.Slice) +[Expr.nodes.ExprSlice] +rustdoc = "/// See also [Slice](https://docs.python.org/3/library/ast.html#ast.Slice)" + [[Expr.nodes.ExprSlice.fields]] name = "lower" type = "Expr" @@ -369,17 +423,16 @@ name = "step" type = "Expr" optional = true -# 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 [`StmtIpyEscapeCommand`]. +[Expr.nodes.ExprIpyEscapeCommand] +rustdoc = """/// An AST node used to represent a IPython escape command at the expression level. +/// +/// For example, +/// ```python +/// dir = !pwd +/// +/// For more information related to terminology and syntax of escape commands, +/// see [`StmtIpyEscapeCommand`].""" + [[Expr.nodes.ExprIpyEscapeCommand.fields]] name = "kind" type = "IpyEscapeKind" diff --git a/crates/ruff_python_ast/generate.py b/crates/ruff_python_ast/generate.py index 76fa29c8a2fe1a..5afebca8613067 100644 --- a/crates/ruff_python_ast/generate.py +++ b/crates/ruff_python_ast/generate.py @@ -100,6 +100,7 @@ class Node: name: str variant: str ty: str + rustdoc: str | None fields: list[Field] | None derives: list[str] @@ -112,6 +113,7 @@ def __init__(self, group: Group, node_name: str, node: dict[str, Any]) -> None: if fields is not None: self.fields = [Field(f) for f in fields] self.derives = node.get("derives", []) + self.rustdoc = node.get("rustdoc") @dataclass @@ -138,7 +140,7 @@ def write_preamble(out: list[str]) -> None: // This is a generated file. Don't modify it by hand! // Run `crates/ruff_python_ast/generate.py` to re-generate the file. - { import_section } + {import_section} """) @@ -591,6 +593,8 @@ def write_node(out: list[str], ast: Ast) -> None: for node in group.nodes: if node.fields is None: continue + if node.rustdoc is not None: + out.append(node.rustdoc) out.append( "#[derive(Clone, Debug, PartialEq" + "".join(f", {derive}" for derive in node.derives) diff --git a/crates/ruff_python_ast/src/generated.rs b/crates/ruff_python_ast/src/generated.rs index 0017f7d0c2359e..62190f93367910 100644 --- a/crates/ruff_python_ast/src/generated.rs +++ b/crates/ruff_python_ast/src/generated.rs @@ -6448,6 +6448,7 @@ 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, @@ -6455,6 +6456,7 @@ pub struct ExprBoolOp { pub values: Vec, } +/// 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, @@ -6462,6 +6464,7 @@ pub struct ExprNamed { pub value: Box, } +/// 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, @@ -6470,6 +6473,7 @@ pub struct ExprBinOp { pub right: Box, } +/// 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, @@ -6477,6 +6481,7 @@ pub struct ExprUnaryOp { pub operand: Box, } +/// 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, @@ -6484,6 +6489,7 @@ pub struct ExprLambda { pub body: Box, } +/// 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, @@ -6492,18 +6498,21 @@ pub struct ExprIf { pub orelse: Box, } +/// 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, } +/// 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, } +/// 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, @@ -6511,6 +6520,7 @@ pub struct ExprListComp { pub generators: Vec, } +/// 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, @@ -6518,6 +6528,7 @@ pub struct ExprSetComp { pub generators: Vec, } +/// 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, @@ -6526,6 +6537,7 @@ pub struct ExprDictComp { pub generators: Vec, } +/// 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, @@ -6534,24 +6546,28 @@ pub struct ExprGenerator { 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, } +/// 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>, } +/// 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, } +/// 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, @@ -6560,6 +6576,7 @@ pub struct ExprCompare { 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, @@ -6567,18 +6584,30 @@ pub struct ExprCall { 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. +/// +/// [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, @@ -6607,6 +6636,7 @@ 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, @@ -6615,6 +6645,7 @@ pub struct ExprAttribute { 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, @@ -6623,6 +6654,7 @@ pub struct ExprSubscript { 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, @@ -6630,6 +6662,7 @@ pub struct ExprStarred { 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, @@ -6637,6 +6670,7 @@ pub struct ExprName { 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, @@ -6644,6 +6678,7 @@ pub struct ExprList { 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, @@ -6652,6 +6687,7 @@ pub struct ExprTuple { 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, @@ -6660,6 +6696,14 @@ pub struct ExprSlice { pub step: Option>, } +/// An AST node used to represent a IPython escape command at the expression level. +/// +/// For example, +/// ```python +/// dir = !pwd +/// +/// For more information related to terminology and syntax of escape commands, +/// see [`StmtIpyEscapeCommand`]. #[derive(Clone, Debug, PartialEq)] pub struct ExprIpyEscapeCommand { pub range: ruff_text_size::TextRange, From 4951013539e2512b3e76bbcd6958a5ba85dc7727 Mon Sep 17 00:00:00 2001 From: Shaygan Hooshyari Date: Wed, 26 Feb 2025 12:10:27 -0800 Subject: [PATCH 7/8] Update crates/ruff_python_ast/ast.toml Co-authored-by: Douglas Creager --- crates/ruff_python_ast/ast.toml | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/crates/ruff_python_ast/ast.toml b/crates/ruff_python_ast/ast.toml index 6002954c90a82c..5bad90b358c102 100644 --- a/crates/ruff_python_ast/ast.toml +++ b/crates/ruff_python_ast/ast.toml @@ -277,14 +277,15 @@ name = "arguments" type = "Arguments" [Expr.nodes.ExprFString] -rustdoc = """/// 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. -/// -/// [JoinedStr]: https://docs.python.org/3/library/ast.html#ast.JoinedStr""" +rustdoc = """ +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. + +[JoinedStr]: https://docs.python.org/3/library/ast.html#ast.JoinedStr""" [[Expr.nodes.ExprFString.fields]] name = "value" From 6f28c8147af95d539c893fed5ed25b7b1be0086f Mon Sep 17 00:00:00 2001 From: Glyphack Date: Wed, 26 Feb 2025 22:53:50 +0100 Subject: [PATCH 8/8] Refactor rustdoc --- crates/ruff_python_ast/ast.toml | 92 +++++++++++++------------ crates/ruff_python_ast/generate.py | 25 ++++--- crates/ruff_python_ast/src/generated.rs | 9 ++- 3 files changed, 68 insertions(+), 58 deletions(-) diff --git a/crates/ruff_python_ast/ast.toml b/crates/ruff_python_ast/ast.toml index 5bad90b358c102..201c20ae9c3cd4 100644 --- a/crates/ruff_python_ast/ast.toml +++ b/crates/ruff_python_ast/ast.toml @@ -37,7 +37,7 @@ [Mod] anynode_is_label = "module" -rustdoc = "/// See also [mod](https://docs.python.org/3/library/ast.html#ast.mod)" +doc = "See also [mod](https://docs.python.org/3/library/ast.html#ast.mod)" [Mod.nodes] ModModule = {} @@ -46,7 +46,7 @@ ModExpression = {} [Stmt] add_suffix_to_is_methods = true anynode_is_label = "statement" -rustdoc = "/// See also [stmt](https://docs.python.org/3/library/ast.html#ast.stmt)" +doc = "See also [stmt](https://docs.python.org/3/library/ast.html#ast.stmt)" [Stmt.nodes] StmtFunctionDef = {} @@ -78,10 +78,10 @@ StmtIpyEscapeCommand = {} [Expr] add_suffix_to_is_methods = true anynode_is_label = "expression" -rustdoc = "/// See also [expr](https://docs.python.org/3/library/ast.html#ast.expr)" +doc = "See also [expr](https://docs.python.org/3/library/ast.html#ast.expr)" [Expr.nodes.ExprBoolOp] -rustdoc = "/// See also [BoolOp](https://docs.python.org/3/library/ast.html#ast.BoolOp)" +doc = "See also [BoolOp](https://docs.python.org/3/library/ast.html#ast.BoolOp)" [[Expr.nodes.ExprBoolOp.fields]] name = "op" @@ -93,7 +93,7 @@ type = "Expr" seq = true [Expr.nodes.ExprNamed] -rustdoc = "/// See also [NamedExpr](https://docs.python.org/3/library/ast.html#ast.NamedExpr)" +doc = "See also [NamedExpr](https://docs.python.org/3/library/ast.html#ast.NamedExpr)" [[Expr.nodes.ExprNamed.fields]] name = "target" @@ -104,7 +104,7 @@ name = "value" type = "Expr" [Expr.nodes.ExprBinOp] -rustdoc = "/// See also [BinOp](https://docs.python.org/3/library/ast.html#ast.BinOp)" +doc = "See also [BinOp](https://docs.python.org/3/library/ast.html#ast.BinOp)" [[Expr.nodes.ExprBinOp.fields]] name = "left" @@ -119,7 +119,7 @@ name = "right" type = "Expr" [Expr.nodes.ExprUnaryOp] -rustdoc = "/// See also [UnaryOp](https://docs.python.org/3/library/ast.html#ast.UnaryOp)" +doc = "See also [UnaryOp](https://docs.python.org/3/library/ast.html#ast.UnaryOp)" [[Expr.nodes.ExprUnaryOp.fields]] name = "op" @@ -130,7 +130,7 @@ name = "operand" type = "Expr" [Expr.nodes.ExprLambda] -rustdoc = "/// See also [Lambda](https://docs.python.org/3/library/ast.html#ast.Lambda)" +doc = "See also [Lambda](https://docs.python.org/3/library/ast.html#ast.Lambda)" [[Expr.nodes.ExprLambda.fields]] name = "parameters" @@ -142,7 +142,7 @@ name = "body" type = "Expr" [Expr.nodes.ExprIf] -rustdoc = "/// See also [IfExp](https://docs.python.org/3/library/ast.html#ast.IfExp)" +doc = "See also [IfExp](https://docs.python.org/3/library/ast.html#ast.IfExp)" [[Expr.nodes.ExprIf.fields]] name = "test" @@ -157,7 +157,7 @@ name = "orelse" type = "Expr" [Expr.nodes.ExprDict] -rustdoc = "/// See also [Dict](https://docs.python.org/3/library/ast.html#ast.Dict)" +doc = "See also [Dict](https://docs.python.org/3/library/ast.html#ast.Dict)" [[Expr.nodes.ExprDict.fields]] name = "items" @@ -165,7 +165,7 @@ type = "DictItem" seq = true [Expr.nodes.ExprSet] -rustdoc = "/// See also [Set](https://docs.python.org/3/library/ast.html#ast.Set)" +doc = "See also [Set](https://docs.python.org/3/library/ast.html#ast.Set)" [[Expr.nodes.ExprSet.fields]] name = "elts" @@ -173,7 +173,7 @@ type = "Expr" seq = true [Expr.nodes.ExprListComp] -rustdoc = "/// See also [ListComp](https://docs.python.org/3/library/ast.html#ast.ListComp)" +doc = "See also [ListComp](https://docs.python.org/3/library/ast.html#ast.ListComp)" [[Expr.nodes.ExprListComp.fields]] name = "elt" @@ -185,7 +185,7 @@ type = "Comprehension" seq = true [Expr.nodes.ExprSetComp] -rustdoc = "/// See also [SetComp](https://docs.python.org/3/library/ast.html#ast.SetComp)" +doc = "See also [SetComp](https://docs.python.org/3/library/ast.html#ast.SetComp)" [[Expr.nodes.ExprSetComp.fields]] name = "elt" @@ -197,7 +197,7 @@ type = "Comprehension" seq = true [Expr.nodes.ExprDictComp] -rustdoc = "/// See also [DictComp](https://docs.python.org/3/library/ast.html#ast.DictComp)" +doc = "See also [DictComp](https://docs.python.org/3/library/ast.html#ast.DictComp)" [[Expr.nodes.ExprDictComp.fields]] name = "key" @@ -213,7 +213,7 @@ type = "Comprehension" seq = true [Expr.nodes.ExprGenerator] -rustdoc = "/// See also [GeneratorExp](https://docs.python.org/3/library/ast.html#ast.GeneratorExp)" +doc = "See also [GeneratorExp](https://docs.python.org/3/library/ast.html#ast.GeneratorExp)" [[Expr.nodes.ExprGenerator.fields]] name = "elt" @@ -229,14 +229,14 @@ name = "parenthesized" type = "bool" [Expr.nodes.ExprAwait] -rustdoc = "/// See also [Await](https://docs.python.org/3/library/ast.html#ast.Await)" +doc = "See also [Await](https://docs.python.org/3/library/ast.html#ast.Await)" [[Expr.nodes.ExprAwait.fields]] name = "value" type = "Expr" [Expr.nodes.ExprYield] -rustdoc = "/// See also [Yield](https://docs.python.org/3/library/ast.html#ast.Yield)" +doc = "See also [Yield](https://docs.python.org/3/library/ast.html#ast.Yield)" [[Expr.nodes.ExprYield.fields]] name = "value" @@ -244,14 +244,14 @@ type = "Expr" optional = true [Expr.nodes.ExprYieldFrom] -rustdoc = "/// See also [YieldFrom](https://docs.python.org/3/library/ast.html#ast.YieldFrom)" +doc = "See also [YieldFrom](https://docs.python.org/3/library/ast.html#ast.YieldFrom)" [[Expr.nodes.ExprYieldFrom.fields]] name = "value" type = "Expr" [Expr.nodes.ExprCompare] -rustdoc = "/// See also [Compare](https://docs.python.org/3/library/ast.html#ast.Compare)" +doc = "See also [Compare](https://docs.python.org/3/library/ast.html#ast.Compare)" [[Expr.nodes.ExprCompare.fields]] name = "left" @@ -266,7 +266,7 @@ name = "comparators" type = "Box<[Expr]>" [Expr.nodes.ExprCall] -rustdoc = "/// See also [Call](https://docs.python.org/3/library/ast.html#ast.Call)" +doc = "See also [Call](https://docs.python.org/3/library/ast.html#ast.Call)" [[Expr.nodes.ExprCall.fields]] name = "func" @@ -277,31 +277,30 @@ name = "arguments" type = "Arguments" [Expr.nodes.ExprFString] -rustdoc = """ -An AST node that represents either a single-part f-string literal +doc = """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. -[JoinedStr]: https://docs.python.org/3/library/ast.html#ast.JoinedStr""" +See also [JoinedStr](https://docs.python.org/3/library/ast.html#ast.JoinedStr)""" [[Expr.nodes.ExprFString.fields]] name = "value" type = "FStringValue" [Expr.nodes.ExprStringLiteral] -rustdoc = """/// An AST node that represents either a single-part string literal -/// or an implicitly concatenated string literal.""" +doc = """An AST node that represents either a single-part string literal +or an implicitly concatenated string literal.""" [[Expr.nodes.ExprStringLiteral.fields]] name = "value" type = "StringLiteralValue" [Expr.nodes.ExprBytesLiteral] -rustdoc = """/// An AST node that represents either a single-part bytestring literal -/// or an implicitly concatenated bytestring literal.""" +doc = """An AST node that represents either a single-part bytestring literal +or an implicitly concatenated bytestring literal.""" [[Expr.nodes.ExprBytesLiteral.fields]] name = "value" @@ -327,7 +326,7 @@ fields = [] derives = ["Default"] [Expr.nodes.ExprAttribute] -rustdoc = "/// See also [Attribute](https://docs.python.org/3/library/ast.html#ast.Attribute)" +doc = "See also [Attribute](https://docs.python.org/3/library/ast.html#ast.Attribute)" [[Expr.nodes.ExprAttribute.fields]] name = "value" @@ -342,7 +341,7 @@ name = "ctx" type = "ExprContext" [Expr.nodes.ExprSubscript] -rustdoc = "/// See also [Subscript](https://docs.python.org/3/library/ast.html#ast.Subscript)" +doc = "See also [Subscript](https://docs.python.org/3/library/ast.html#ast.Subscript)" [[Expr.nodes.ExprSubscript.fields]] name = "value" @@ -357,7 +356,7 @@ name = "ctx" type = "ExprContext" [Expr.nodes.ExprStarred] -rustdoc = "/// See also [Starred](https://docs.python.org/3/library/ast.html#ast.Starred)" +doc = "See also [Starred](https://docs.python.org/3/library/ast.html#ast.Starred)" [[Expr.nodes.ExprStarred.fields]] name = "value" @@ -368,7 +367,7 @@ name = "ctx" type = "ExprContext" [Expr.nodes.ExprName] -rustdoc = "/// See also [Name](https://docs.python.org/3/library/ast.html#ast.Name)" +doc = "See also [Name](https://docs.python.org/3/library/ast.html#ast.Name)" [[Expr.nodes.ExprName.fields]] name = "id" @@ -379,7 +378,7 @@ name = "ctx" type = "ExprContext" [Expr.nodes.ExprList] -rustdoc = "/// See also [List](https://docs.python.org/3/library/ast.html#ast.List)" +doc = "See also [List](https://docs.python.org/3/library/ast.html#ast.List)" [[Expr.nodes.ExprList.fields]] name = "elts" @@ -391,7 +390,7 @@ name = "ctx" type = "ExprContext" [Expr.nodes.ExprTuple] -rustdoc = "/// See also [Tuple](https://docs.python.org/3/library/ast.html#ast.Tuple)" +doc = "See also [Tuple](https://docs.python.org/3/library/ast.html#ast.Tuple)" [[Expr.nodes.ExprTuple.fields]] name = "elts" @@ -407,7 +406,7 @@ name = "parenthesized" type = "bool" [Expr.nodes.ExprSlice] -rustdoc = "/// See also [Slice](https://docs.python.org/3/library/ast.html#ast.Slice)" +doc = "See also [Slice](https://docs.python.org/3/library/ast.html#ast.Slice)" [[Expr.nodes.ExprSlice.fields]] name = "lower" @@ -425,14 +424,17 @@ type = "Expr" optional = true [Expr.nodes.ExprIpyEscapeCommand] -rustdoc = """/// An AST node used to represent a IPython escape command at the expression level. -/// -/// For example, -/// ```python -/// dir = !pwd -/// -/// For more information related to terminology and syntax of escape commands, -/// see [`StmtIpyEscapeCommand`].""" +doc = """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 [`StmtIpyEscapeCommand`].""" [[Expr.nodes.ExprIpyEscapeCommand.fields]] name = "kind" @@ -444,7 +446,7 @@ type = "Box" [ExceptHandler] -rustdoc = "/// See also [excepthandler](https://docs.python.org/3/library/ast.html#ast.excepthandler)" +doc = "See also [excepthandler](https://docs.python.org/3/library/ast.html#ast.excepthandler)" [ExceptHandler.nodes] ExceptHandlerExceptHandler = {} @@ -454,7 +456,7 @@ FStringExpressionElement = {variant = "Expression"} FStringLiteralElement = {variant = "Literal"} [Pattern] -rustdoc = "/// See also [pattern](https://docs.python.org/3/library/ast.html#ast.pattern)" +doc = "See also [pattern](https://docs.python.org/3/library/ast.html#ast.pattern)" [Pattern.nodes] PatternMatchValue = {} @@ -467,7 +469,7 @@ PatternMatchAs = {} PatternMatchOr = {} [TypeParam] -rustdoc = "/// See also [type_param](https://docs.python.org/3/library/ast.html#ast.type_param)" +doc = "See also [type_param](https://docs.python.org/3/library/ast.html#ast.type_param)" [TypeParam.nodes] TypeParamTypeVar = {} diff --git a/crates/ruff_python_ast/generate.py b/crates/ruff_python_ast/generate.py index 5afebca8613067..d15a2852ed69a3 100644 --- a/crates/ruff_python_ast/generate.py +++ b/crates/ruff_python_ast/generate.py @@ -34,6 +34,11 @@ def to_snake_case(node: str) -> str: return re.sub("([A-Z])", r"_\1", node).lower().lstrip("_") +def write_rustdoc(out: list[str], doc: str) -> None: + for line in doc.split("\n"): + out.append(f"/// {line}") + + # ------------------------------------------------------------------------------ # Read AST description @@ -81,7 +86,7 @@ class Group: add_suffix_to_is_methods: bool anynode_is_label: str - rustdoc: str | None + doc: str | None def __init__(self, group_name: str, group: dict[str, Any]) -> None: self.name = group_name @@ -89,7 +94,7 @@ def __init__(self, group_name: str, group: dict[str, Any]) -> None: self.ref_enum_ty = group_name + "Ref" self.add_suffix_to_is_methods = group.get("add_suffix_to_is_methods", False) self.anynode_is_label = group.get("anynode_is_label", to_snake_case(group_name)) - self.rustdoc = group.get("rustdoc") + self.doc = group.get("doc") self.nodes = [ Node(self, node_name, node) for node_name, node in group["nodes"].items() ] @@ -100,7 +105,7 @@ class Node: name: str variant: str ty: str - rustdoc: str | None + doc: str | None fields: list[Field] | None derives: list[str] @@ -113,7 +118,7 @@ def __init__(self, group: Group, node_name: str, node: dict[str, Any]) -> None: if fields is not None: self.fields = [Field(f) for f in fields] self.derives = node.get("derives", []) - self.rustdoc = node.get("rustdoc") + self.doc = node.get("doc") @dataclass @@ -173,8 +178,8 @@ def write_owned_enum(out: list[str], ast: Ast) -> None: for group in ast.groups: out.append("") - if group.rustdoc is not None: - out.append(group.rustdoc) + if group.doc is not None: + write_rustdoc(out, group.doc) out.append("#[derive(Clone, Debug, PartialEq)]") out.append(f"pub enum {group.owned_enum_ty} {{") for node in group.nodes: @@ -349,8 +354,8 @@ def write_ref_enum(out: list[str], ast: Ast) -> None: for group in ast.groups: out.append("") - if group.rustdoc is not None: - out.append(group.rustdoc) + if group.doc is not None: + write_rustdoc(out, group.doc) out.append("""#[derive(Clone, Copy, Debug, PartialEq, is_macro::Is)]""") out.append(f"""pub enum {group.ref_enum_ty}<'a> {{""") for node in group.nodes: @@ -593,8 +598,8 @@ def write_node(out: list[str], ast: Ast) -> None: for node in group.nodes: if node.fields is None: continue - if node.rustdoc is not None: - out.append(node.rustdoc) + if node.doc is not None: + write_rustdoc(out, node.doc) out.append( "#[derive(Clone, Debug, PartialEq" + "".join(f", {derive}" for derive in node.derives) diff --git a/crates/ruff_python_ast/src/generated.rs b/crates/ruff_python_ast/src/generated.rs index 62190f93367910..2d8c078cb5876c 100644 --- a/crates/ruff_python_ast/src/generated.rs +++ b/crates/ruff_python_ast/src/generated.rs @@ -6591,7 +6591,7 @@ pub struct ExprCall { /// doesn't join the implicitly concatenated parts into a single string. Instead, /// it keeps them separate and provide various methods to access the parts. /// -/// [JoinedStr]: https://docs.python.org/3/library/ast.html#ast.JoinedStr +/// 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, @@ -6674,7 +6674,7 @@ pub struct ExprName { #[derive(Clone, Debug, PartialEq)] pub struct ExprList { pub range: ruff_text_size::TextRange, - pub elts: Vec, + pub elts: Option>, pub ctx: crate::ExprContext, } @@ -6701,9 +6701,12 @@ pub struct ExprSlice { /// 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 [`StmtIpyEscapeCommand`]. +/// see [`StmtIpyEscapeCommand`]. #[derive(Clone, Debug, PartialEq)] pub struct ExprIpyEscapeCommand { pub range: ruff_text_size::TextRange,