// File automatically generated by ast/asdl_rs.py. pub use crate::constant::*; pub use crate::location::Location; use fxhash::FxBuildHasher; use parking_lot::{Mutex, MutexGuard}; use std::{cell::RefCell, collections::HashMap, fmt, sync::LazyLock}; use string_interner::{symbol::SymbolU32, DefaultBackend, StringInterner}; pub type Interner = StringInterner; static INTERNER: LazyLock> = LazyLock::new(|| Mutex::new(StringInterner::with_hasher(FxBuildHasher::default()))); thread_local! { static LOCAL_INTERNER: RefCell> = RefCell::default(); } #[derive(Eq, PartialEq, Copy, Clone, Hash)] pub struct StrRef(SymbolU32); impl fmt::Debug for StrRef { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let s: String = (*self).into(); write!(f, "{s:?}") } } impl fmt::Display for StrRef { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let s: String = (*self).into(); write!(f, "{s}") } } impl From for StrRef { fn from(s: String) -> Self { get_str_ref(&mut get_str_ref_lock(), &s) } } impl From<&str> for StrRef { fn from(s: &str) -> Self { // thread local cache LOCAL_INTERNER.with(|local| { let mut local = local.borrow_mut(); local.get(s).copied().unwrap_or_else(|| { let r = get_str_ref(&mut get_str_ref_lock(), s); local.insert(s.to_string(), r); r }) }) } } impl From for String { fn from(s: StrRef) -> Self { get_str_from_ref(&get_str_ref_lock(), s).to_string() } } pub fn get_str_ref_lock<'a>() -> MutexGuard<'a, Interner> { INTERNER.lock() } pub fn get_str_ref(lock: &mut MutexGuard, str: &str) -> StrRef { StrRef(lock.get_or_intern(str)) } #[must_use] pub fn get_str_from_ref<'a>(lock: &'a MutexGuard, id: StrRef) -> &'a str { lock.resolve(id.0).unwrap() } pub type Ident = StrRef; #[derive(Clone, Debug, PartialEq)] pub struct Located { pub location: Location, pub custom: U, pub node: T, } impl Located { pub fn new(location: Location, node: T) -> Self { Self { location, custom: (), node } } } #[derive(Clone, Debug, PartialEq)] pub enum Mod { Module { body: Vec>, type_ignores: Vec }, Interactive { body: Vec> }, Expression { body: Box> }, FunctionType { argtypes: Vec>, returns: Box> }, } #[derive(Clone, Debug, PartialEq)] pub enum StmtKind { FunctionDef { name: Ident, args: Box>, body: Vec>, decorator_list: Vec>, returns: Option>>, type_comment: Option, config_comment: Vec, }, AsyncFunctionDef { name: Ident, args: Box>, body: Vec>, decorator_list: Vec>, returns: Option>>, type_comment: Option, config_comment: Vec, }, ClassDef { name: Ident, bases: Vec>, keywords: Vec>, body: Vec>, decorator_list: Vec>, config_comment: Vec, }, Return { value: Option>>, config_comment: Vec, }, Delete { targets: Vec>, config_comment: Vec, }, Assign { targets: Vec>, value: Box>, type_comment: Option, config_comment: Vec, }, AugAssign { target: Box>, op: Operator, value: Box>, config_comment: Vec, }, AnnAssign { target: Box>, annotation: Box>, value: Option>>, simple: bool, config_comment: Vec, }, For { target: Box>, iter: Box>, body: Vec>, orelse: Vec>, type_comment: Option, config_comment: Vec, }, AsyncFor { target: Box>, iter: Box>, body: Vec>, orelse: Vec>, type_comment: Option, config_comment: Vec, }, While { test: Box>, body: Vec>, orelse: Vec>, config_comment: Vec, }, If { test: Box>, body: Vec>, orelse: Vec>, config_comment: Vec, }, With { items: Vec>, body: Vec>, type_comment: Option, config_comment: Vec, }, AsyncWith { items: Vec>, body: Vec>, type_comment: Option, config_comment: Vec, }, Raise { exc: Option>>, cause: Option>>, config_comment: Vec, }, Try { body: Vec>, handlers: Vec>, orelse: Vec>, finalbody: Vec>, config_comment: Vec, }, Assert { test: Box>, msg: Option>>, config_comment: Vec, }, Import { names: Vec, config_comment: Vec, }, ImportFrom { module: Option, names: Vec, level: usize, config_comment: Vec, }, Global { names: Vec, config_comment: Vec, }, Nonlocal { names: Vec, config_comment: Vec, }, Expr { value: Box>, config_comment: Vec, }, Pass { config_comment: Vec, }, Break { config_comment: Vec, }, Continue { config_comment: Vec, }, } pub type Stmt = Located, U>; #[derive(Clone, Debug, PartialEq)] pub enum ExprKind { BoolOp { op: Boolop, values: Vec>, }, NamedExpr { target: Box>, value: Box>, }, BinOp { left: Box>, op: Operator, right: Box>, }, UnaryOp { op: Unaryop, operand: Box>, }, Lambda { args: Box>, body: Box>, }, IfExp { test: Box>, body: Box>, orelse: Box>, }, Dict { keys: Vec>>>, values: Vec>, }, Set { elts: Vec>, }, ListComp { elt: Box>, generators: Vec>, }, SetComp { elt: Box>, generators: Vec>, }, DictComp { key: Box>, value: Box>, generators: Vec>, }, GeneratorExp { elt: Box>, generators: Vec>, }, Await { value: Box>, }, Yield { value: Option>>, }, YieldFrom { value: Box>, }, Compare { left: Box>, ops: Vec, comparators: Vec>, }, Call { func: Box>, args: Vec>, keywords: Vec>, }, FormattedValue { value: Box>, conversion: Option, format_spec: Option>>, }, JoinedStr { values: Vec>, }, Constant { value: Constant, kind: Option, }, Attribute { value: Box>, attr: Ident, ctx: ExprContext, }, Subscript { value: Box>, slice: Box>, ctx: ExprContext, }, Starred { value: Box>, ctx: ExprContext, }, Name { id: Ident, ctx: ExprContext, }, List { elts: Vec>, ctx: ExprContext, }, Tuple { elts: Vec>, ctx: ExprContext, }, Slice { lower: Option>>, upper: Option>>, step: Option>>, }, } pub type Expr = Located, U>; #[derive(Clone, Copy, Debug, PartialEq)] pub enum ExprContext { Load, Store, Del, } #[derive(Clone, Copy, Debug, PartialEq)] pub enum Boolop { And, Or, } #[derive(Clone, Copy, Debug, PartialEq)] pub enum Operator { Add, Sub, Mult, MatMult, Div, Mod, Pow, LShift, RShift, BitOr, BitXor, BitAnd, FloorDiv, } #[derive(Clone, Copy, Debug, PartialEq)] pub enum Unaryop { Invert, Not, UAdd, USub, } #[derive(Clone, Copy, Debug, PartialEq)] pub enum Cmpop { Eq, NotEq, Lt, LtE, Gt, GtE, Is, IsNot, In, NotIn, } #[derive(Clone, Debug, PartialEq)] pub struct Comprehension { pub target: Box>, pub iter: Box>, pub ifs: Vec>, pub is_async: bool, } #[derive(Clone, Debug, PartialEq)] pub enum ExcepthandlerKind { ExceptHandler { type_: Option>>, name: Option, body: Vec> }, } pub type Excepthandler = Located, U>; #[derive(Clone, Debug, PartialEq)] pub struct Arguments { pub posonlyargs: Vec>, pub args: Vec>, pub vararg: Option>>, pub kwonlyargs: Vec>, pub kw_defaults: Vec>>>, pub kwarg: Option>>, pub defaults: Vec>, } #[derive(Clone, Debug, PartialEq)] pub struct ArgData { pub arg: Ident, pub annotation: Option>>, pub type_comment: Option, } pub type Arg = Located, U>; #[derive(Clone, Debug, PartialEq)] pub struct KeywordData { pub arg: Option, pub value: Box>, } pub type Keyword = Located, U>; #[derive(Clone, Copy, Debug, PartialEq)] pub struct Alias { pub name: Ident, pub asname: Option, } #[derive(Clone, Debug, PartialEq)] pub struct Withitem { pub context_expr: Box>, pub optional_vars: Option>>, } #[derive(Clone, Debug, PartialEq)] pub enum TypeIgnore { TypeIgnore { lineno: usize, tag: String }, } #[cfg(feature = "fold")] pub mod fold { use super::*; use crate::fold_helpers::Foldable; pub trait Fold { type TargetU; type Error; fn map_user(&mut self, user: U) -> Result; fn fold_mod(&mut self, node: Mod) -> Result, Self::Error> { fold_mod(self, node) } fn fold_stmt(&mut self, node: Stmt) -> Result, Self::Error> { fold_stmt(self, node) } fn fold_expr(&mut self, node: Expr) -> Result, Self::Error> { fold_expr(self, node) } fn fold_expr_context(&mut self, node: ExprContext) -> Result { fold_expr_context(self, node) } fn fold_boolop(&mut self, node: Boolop) -> Result { fold_boolop(self, node) } fn fold_operator(&mut self, node: Operator) -> Result { fold_operator(self, node) } fn fold_unaryop(&mut self, node: Unaryop) -> Result { fold_unaryop(self, node) } fn fold_cmpop(&mut self, node: Cmpop) -> Result { fold_cmpop(self, node) } fn fold_comprehension( &mut self, node: Comprehension, ) -> Result, Self::Error> { fold_comprehension(self, node) } fn fold_excepthandler( &mut self, node: Excepthandler, ) -> Result, Self::Error> { fold_excepthandler(self, node) } fn fold_arguments( &mut self, node: Arguments, ) -> Result, Self::Error> { fold_arguments(self, node) } fn fold_arg(&mut self, node: Arg) -> Result, Self::Error> { fold_arg(self, node) } fn fold_keyword( &mut self, node: Keyword, ) -> Result, Self::Error> { fold_keyword(self, node) } fn fold_alias(&mut self, node: Alias) -> Result { fold_alias(self, node) } fn fold_withitem( &mut self, node: Withitem, ) -> Result, Self::Error> { fold_withitem(self, node) } fn fold_type_ignore(&mut self, node: TypeIgnore) -> Result { fold_type_ignore(self, node) } } fn fold_located + ?Sized, T, MT>( folder: &mut F, node: Located, f: impl FnOnce(&mut F, T) -> Result, ) -> Result, F::Error> { Ok(Located { custom: folder.map_user(node.custom)?, location: node.location, node: f(folder, node.node)?, }) } impl Foldable for Mod { type Mapped = Mod; fn fold + ?Sized>( self, folder: &mut F, ) -> Result { folder.fold_mod(self) } } pub fn fold_mod + ?Sized>( #[allow(unused)] folder: &mut F, node: Mod, ) -> Result, F::Error> { match node { Mod::Module { body, type_ignores } => Ok(Mod::Module { body: Foldable::fold(body, folder)?, type_ignores: Foldable::fold(type_ignores, folder)?, }), Mod::Interactive { body } => { Ok(Mod::Interactive { body: Foldable::fold(body, folder)? }) } Mod::Expression { body } => Ok(Mod::Expression { body: Foldable::fold(body, folder)? }), Mod::FunctionType { argtypes, returns } => Ok(Mod::FunctionType { argtypes: Foldable::fold(argtypes, folder)?, returns: Foldable::fold(returns, folder)?, }), } } impl Foldable for Stmt { type Mapped = Stmt; fn fold + ?Sized>( self, folder: &mut F, ) -> Result { folder.fold_stmt(self) } } pub fn fold_stmt + ?Sized>( #[allow(unused)] folder: &mut F, node: Stmt, ) -> Result, F::Error> { fold_located(folder, node, |folder, node| match node { StmtKind::FunctionDef { name, args, body, decorator_list, returns, type_comment, config_comment, } => Ok(StmtKind::FunctionDef { name: Foldable::fold(name, folder)?, args: Foldable::fold(args, folder)?, body: Foldable::fold(body, folder)?, decorator_list: Foldable::fold(decorator_list, folder)?, returns: Foldable::fold(returns, folder)?, type_comment: Foldable::fold(type_comment, folder)?, config_comment: Foldable::fold(config_comment, folder)?, }), StmtKind::AsyncFunctionDef { name, args, body, decorator_list, returns, type_comment, config_comment, } => Ok(StmtKind::AsyncFunctionDef { name: Foldable::fold(name, folder)?, args: Foldable::fold(args, folder)?, body: Foldable::fold(body, folder)?, decorator_list: Foldable::fold(decorator_list, folder)?, returns: Foldable::fold(returns, folder)?, type_comment: Foldable::fold(type_comment, folder)?, config_comment: Foldable::fold(config_comment, folder)?, }), StmtKind::ClassDef { name, bases, keywords, body, decorator_list, config_comment } => { Ok(StmtKind::ClassDef { name: Foldable::fold(name, folder)?, bases: Foldable::fold(bases, folder)?, keywords: Foldable::fold(keywords, folder)?, body: Foldable::fold(body, folder)?, decorator_list: Foldable::fold(decorator_list, folder)?, config_comment: Foldable::fold(config_comment, folder)?, }) } StmtKind::Return { value, config_comment } => Ok(StmtKind::Return { value: Foldable::fold(value, folder)?, config_comment: Foldable::fold(config_comment, folder)?, }), StmtKind::Delete { targets, config_comment } => Ok(StmtKind::Delete { targets: Foldable::fold(targets, folder)?, config_comment: Foldable::fold(config_comment, folder)?, }), StmtKind::Assign { targets, value, type_comment, config_comment } => { Ok(StmtKind::Assign { targets: Foldable::fold(targets, folder)?, value: Foldable::fold(value, folder)?, type_comment: Foldable::fold(type_comment, folder)?, config_comment: Foldable::fold(config_comment, folder)?, }) } StmtKind::AugAssign { target, op, value, config_comment } => Ok(StmtKind::AugAssign { target: Foldable::fold(target, folder)?, op: Foldable::fold(op, folder)?, value: Foldable::fold(value, folder)?, config_comment: Foldable::fold(config_comment, folder)?, }), StmtKind::AnnAssign { target, annotation, value, simple, config_comment } => { Ok(StmtKind::AnnAssign { target: Foldable::fold(target, folder)?, annotation: Foldable::fold(annotation, folder)?, value: Foldable::fold(value, folder)?, simple: Foldable::fold(simple, folder)?, config_comment: Foldable::fold(config_comment, folder)?, }) } StmtKind::For { target, iter, body, orelse, type_comment, config_comment } => { Ok(StmtKind::For { target: Foldable::fold(target, folder)?, iter: Foldable::fold(iter, folder)?, body: Foldable::fold(body, folder)?, orelse: Foldable::fold(orelse, folder)?, type_comment: Foldable::fold(type_comment, folder)?, config_comment: Foldable::fold(config_comment, folder)?, }) } StmtKind::AsyncFor { target, iter, body, orelse, type_comment, config_comment } => { Ok(StmtKind::AsyncFor { target: Foldable::fold(target, folder)?, iter: Foldable::fold(iter, folder)?, body: Foldable::fold(body, folder)?, orelse: Foldable::fold(orelse, folder)?, type_comment: Foldable::fold(type_comment, folder)?, config_comment: Foldable::fold(config_comment, folder)?, }) } StmtKind::While { test, body, orelse, config_comment } => Ok(StmtKind::While { test: Foldable::fold(test, folder)?, body: Foldable::fold(body, folder)?, orelse: Foldable::fold(orelse, folder)?, config_comment: Foldable::fold(config_comment, folder)?, }), StmtKind::If { test, body, orelse, config_comment } => Ok(StmtKind::If { test: Foldable::fold(test, folder)?, body: Foldable::fold(body, folder)?, orelse: Foldable::fold(orelse, folder)?, config_comment: Foldable::fold(config_comment, folder)?, }), StmtKind::With { items, body, type_comment, config_comment } => Ok(StmtKind::With { items: Foldable::fold(items, folder)?, body: Foldable::fold(body, folder)?, type_comment: Foldable::fold(type_comment, folder)?, config_comment: Foldable::fold(config_comment, folder)?, }), StmtKind::AsyncWith { items, body, type_comment, config_comment } => { Ok(StmtKind::AsyncWith { items: Foldable::fold(items, folder)?, body: Foldable::fold(body, folder)?, type_comment: Foldable::fold(type_comment, folder)?, config_comment: Foldable::fold(config_comment, folder)?, }) } StmtKind::Raise { exc, cause, config_comment } => Ok(StmtKind::Raise { exc: Foldable::fold(exc, folder)?, cause: Foldable::fold(cause, folder)?, config_comment: Foldable::fold(config_comment, folder)?, }), StmtKind::Try { body, handlers, orelse, finalbody, config_comment } => { Ok(StmtKind::Try { body: Foldable::fold(body, folder)?, handlers: Foldable::fold(handlers, folder)?, orelse: Foldable::fold(orelse, folder)?, finalbody: Foldable::fold(finalbody, folder)?, config_comment: Foldable::fold(config_comment, folder)?, }) } StmtKind::Assert { test, msg, config_comment } => Ok(StmtKind::Assert { test: Foldable::fold(test, folder)?, msg: Foldable::fold(msg, folder)?, config_comment: Foldable::fold(config_comment, folder)?, }), StmtKind::Import { names, config_comment } => Ok(StmtKind::Import { names: Foldable::fold(names, folder)?, config_comment: Foldable::fold(config_comment, folder)?, }), StmtKind::ImportFrom { module, names, level, config_comment } => { Ok(StmtKind::ImportFrom { module: Foldable::fold(module, folder)?, names: Foldable::fold(names, folder)?, level: Foldable::fold(level, folder)?, config_comment: Foldable::fold(config_comment, folder)?, }) } StmtKind::Global { names, config_comment } => Ok(StmtKind::Global { names: Foldable::fold(names, folder)?, config_comment: Foldable::fold(config_comment, folder)?, }), StmtKind::Nonlocal { names, config_comment } => Ok(StmtKind::Nonlocal { names: Foldable::fold(names, folder)?, config_comment: Foldable::fold(config_comment, folder)?, }), StmtKind::Expr { value, config_comment } => Ok(StmtKind::Expr { value: Foldable::fold(value, folder)?, config_comment: Foldable::fold(config_comment, folder)?, }), StmtKind::Pass { config_comment } => { Ok(StmtKind::Pass { config_comment: Foldable::fold(config_comment, folder)? }) } StmtKind::Break { config_comment } => { Ok(StmtKind::Break { config_comment: Foldable::fold(config_comment, folder)? }) } StmtKind::Continue { config_comment } => { Ok(StmtKind::Continue { config_comment: Foldable::fold(config_comment, folder)? }) } }) } impl Foldable for Expr { type Mapped = Expr; fn fold + ?Sized>( self, folder: &mut F, ) -> Result { folder.fold_expr(self) } } pub fn fold_expr + ?Sized>( #[allow(unused)] folder: &mut F, node: Expr, ) -> Result, F::Error> { fold_located(folder, node, |folder, node| match node { ExprKind::BoolOp { op, values } => Ok(ExprKind::BoolOp { op: Foldable::fold(op, folder)?, values: Foldable::fold(values, folder)?, }), ExprKind::NamedExpr { target, value } => Ok(ExprKind::NamedExpr { target: Foldable::fold(target, folder)?, value: Foldable::fold(value, folder)?, }), ExprKind::BinOp { left, op, right } => Ok(ExprKind::BinOp { left: Foldable::fold(left, folder)?, op: Foldable::fold(op, folder)?, right: Foldable::fold(right, folder)?, }), ExprKind::UnaryOp { op, operand } => Ok(ExprKind::UnaryOp { op: Foldable::fold(op, folder)?, operand: Foldable::fold(operand, folder)?, }), ExprKind::Lambda { args, body } => Ok(ExprKind::Lambda { args: Foldable::fold(args, folder)?, body: Foldable::fold(body, folder)?, }), ExprKind::IfExp { test, body, orelse } => Ok(ExprKind::IfExp { test: Foldable::fold(test, folder)?, body: Foldable::fold(body, folder)?, orelse: Foldable::fold(orelse, folder)?, }), ExprKind::Dict { keys, values } => Ok(ExprKind::Dict { keys: Foldable::fold(keys, folder)?, values: Foldable::fold(values, folder)?, }), ExprKind::Set { elts } => Ok(ExprKind::Set { elts: Foldable::fold(elts, folder)? }), ExprKind::ListComp { elt, generators } => Ok(ExprKind::ListComp { elt: Foldable::fold(elt, folder)?, generators: Foldable::fold(generators, folder)?, }), ExprKind::SetComp { elt, generators } => Ok(ExprKind::SetComp { elt: Foldable::fold(elt, folder)?, generators: Foldable::fold(generators, folder)?, }), ExprKind::DictComp { key, value, generators } => Ok(ExprKind::DictComp { key: Foldable::fold(key, folder)?, value: Foldable::fold(value, folder)?, generators: Foldable::fold(generators, folder)?, }), ExprKind::GeneratorExp { elt, generators } => Ok(ExprKind::GeneratorExp { elt: Foldable::fold(elt, folder)?, generators: Foldable::fold(generators, folder)?, }), ExprKind::Await { value } => { Ok(ExprKind::Await { value: Foldable::fold(value, folder)? }) } ExprKind::Yield { value } => { Ok(ExprKind::Yield { value: Foldable::fold(value, folder)? }) } ExprKind::YieldFrom { value } => { Ok(ExprKind::YieldFrom { value: Foldable::fold(value, folder)? }) } ExprKind::Compare { left, ops, comparators } => Ok(ExprKind::Compare { left: Foldable::fold(left, folder)?, ops: Foldable::fold(ops, folder)?, comparators: Foldable::fold(comparators, folder)?, }), ExprKind::Call { func, args, keywords } => Ok(ExprKind::Call { func: Foldable::fold(func, folder)?, args: Foldable::fold(args, folder)?, keywords: Foldable::fold(keywords, folder)?, }), ExprKind::FormattedValue { value, conversion, format_spec } => { Ok(ExprKind::FormattedValue { value: Foldable::fold(value, folder)?, conversion: Foldable::fold(conversion, folder)?, format_spec: Foldable::fold(format_spec, folder)?, }) } ExprKind::JoinedStr { values } => { Ok(ExprKind::JoinedStr { values: Foldable::fold(values, folder)? }) } ExprKind::Constant { value, kind } => Ok(ExprKind::Constant { value: Foldable::fold(value, folder)?, kind: Foldable::fold(kind, folder)?, }), ExprKind::Attribute { value, attr, ctx } => Ok(ExprKind::Attribute { value: Foldable::fold(value, folder)?, attr: Foldable::fold(attr, folder)?, ctx: Foldable::fold(ctx, folder)?, }), ExprKind::Subscript { value, slice, ctx } => Ok(ExprKind::Subscript { value: Foldable::fold(value, folder)?, slice: Foldable::fold(slice, folder)?, ctx: Foldable::fold(ctx, folder)?, }), ExprKind::Starred { value, ctx } => Ok(ExprKind::Starred { value: Foldable::fold(value, folder)?, ctx: Foldable::fold(ctx, folder)?, }), ExprKind::Name { id, ctx } => Ok(ExprKind::Name { id: Foldable::fold(id, folder)?, ctx: Foldable::fold(ctx, folder)?, }), ExprKind::List { elts, ctx } => Ok(ExprKind::List { elts: Foldable::fold(elts, folder)?, ctx: Foldable::fold(ctx, folder)?, }), ExprKind::Tuple { elts, ctx } => Ok(ExprKind::Tuple { elts: Foldable::fold(elts, folder)?, ctx: Foldable::fold(ctx, folder)?, }), ExprKind::Slice { lower, upper, step } => Ok(ExprKind::Slice { lower: Foldable::fold(lower, folder)?, upper: Foldable::fold(upper, folder)?, step: Foldable::fold(step, folder)?, }), }) } impl Foldable for ExprContext { type Mapped = ExprContext; fn fold + ?Sized>( self, folder: &mut F, ) -> Result { folder.fold_expr_context(self) } } pub fn fold_expr_context + ?Sized>( #[allow(unused)] folder: &mut F, node: ExprContext, ) -> Result { match node { ExprContext::Load {} => Ok(ExprContext::Load {}), ExprContext::Store {} => Ok(ExprContext::Store {}), ExprContext::Del {} => Ok(ExprContext::Del {}), } } impl Foldable for Boolop { type Mapped = Boolop; fn fold + ?Sized>( self, folder: &mut F, ) -> Result { folder.fold_boolop(self) } } pub fn fold_boolop + ?Sized>( #[allow(unused)] folder: &mut F, node: Boolop, ) -> Result { match node { Boolop::And {} => Ok(Boolop::And {}), Boolop::Or {} => Ok(Boolop::Or {}), } } impl Foldable for Operator { type Mapped = Operator; fn fold + ?Sized>( self, folder: &mut F, ) -> Result { folder.fold_operator(self) } } pub fn fold_operator + ?Sized>( #[allow(unused)] folder: &mut F, node: Operator, ) -> Result { match node { Operator::Add {} => Ok(Operator::Add {}), Operator::Sub {} => Ok(Operator::Sub {}), Operator::Mult {} => Ok(Operator::Mult {}), Operator::MatMult {} => Ok(Operator::MatMult {}), Operator::Div {} => Ok(Operator::Div {}), Operator::Mod {} => Ok(Operator::Mod {}), Operator::Pow {} => Ok(Operator::Pow {}), Operator::LShift {} => Ok(Operator::LShift {}), Operator::RShift {} => Ok(Operator::RShift {}), Operator::BitOr {} => Ok(Operator::BitOr {}), Operator::BitXor {} => Ok(Operator::BitXor {}), Operator::BitAnd {} => Ok(Operator::BitAnd {}), Operator::FloorDiv {} => Ok(Operator::FloorDiv {}), } } impl Foldable for Unaryop { type Mapped = Unaryop; fn fold + ?Sized>( self, folder: &mut F, ) -> Result { folder.fold_unaryop(self) } } pub fn fold_unaryop + ?Sized>( #[allow(unused)] folder: &mut F, node: Unaryop, ) -> Result { match node { Unaryop::Invert {} => Ok(Unaryop::Invert {}), Unaryop::Not {} => Ok(Unaryop::Not {}), Unaryop::UAdd {} => Ok(Unaryop::UAdd {}), Unaryop::USub {} => Ok(Unaryop::USub {}), } } impl Foldable for Cmpop { type Mapped = Cmpop; fn fold + ?Sized>( self, folder: &mut F, ) -> Result { folder.fold_cmpop(self) } } pub fn fold_cmpop + ?Sized>( #[allow(unused)] folder: &mut F, node: Cmpop, ) -> Result { match node { Cmpop::Eq {} => Ok(Cmpop::Eq {}), Cmpop::NotEq {} => Ok(Cmpop::NotEq {}), Cmpop::Lt {} => Ok(Cmpop::Lt {}), Cmpop::LtE {} => Ok(Cmpop::LtE {}), Cmpop::Gt {} => Ok(Cmpop::Gt {}), Cmpop::GtE {} => Ok(Cmpop::GtE {}), Cmpop::Is {} => Ok(Cmpop::Is {}), Cmpop::IsNot {} => Ok(Cmpop::IsNot {}), Cmpop::In {} => Ok(Cmpop::In {}), Cmpop::NotIn {} => Ok(Cmpop::NotIn {}), } } impl Foldable for Comprehension { type Mapped = Comprehension; fn fold + ?Sized>( self, folder: &mut F, ) -> Result { folder.fold_comprehension(self) } } pub fn fold_comprehension + ?Sized>( #[allow(unused)] folder: &mut F, node: Comprehension, ) -> Result, F::Error> { let Comprehension { target, iter, ifs, is_async } = node; Ok(Comprehension { target: Foldable::fold(target, folder)?, iter: Foldable::fold(iter, folder)?, ifs: Foldable::fold(ifs, folder)?, is_async: Foldable::fold(is_async, folder)?, }) } impl Foldable for Excepthandler { type Mapped = Excepthandler; fn fold + ?Sized>( self, folder: &mut F, ) -> Result { folder.fold_excepthandler(self) } } pub fn fold_excepthandler + ?Sized>( #[allow(unused)] folder: &mut F, node: Excepthandler, ) -> Result, F::Error> { fold_located(folder, node, |folder, node| match node { ExcepthandlerKind::ExceptHandler { type_, name, body } => { Ok(ExcepthandlerKind::ExceptHandler { type_: Foldable::fold(type_, folder)?, name: Foldable::fold(name, folder)?, body: Foldable::fold(body, folder)?, }) } }) } impl Foldable for Arguments { type Mapped = Arguments; fn fold + ?Sized>( self, folder: &mut F, ) -> Result { folder.fold_arguments(self) } } pub fn fold_arguments + ?Sized>( #[allow(unused)] folder: &mut F, node: Arguments, ) -> Result, F::Error> { let Arguments { posonlyargs, args, vararg, kwonlyargs, kw_defaults, kwarg, defaults } = node; Ok(Arguments { posonlyargs: Foldable::fold(posonlyargs, folder)?, args: Foldable::fold(args, folder)?, vararg: Foldable::fold(vararg, folder)?, kwonlyargs: Foldable::fold(kwonlyargs, folder)?, kw_defaults: Foldable::fold(kw_defaults, folder)?, kwarg: Foldable::fold(kwarg, folder)?, defaults: Foldable::fold(defaults, folder)?, }) } impl Foldable for Arg { type Mapped = Arg; fn fold + ?Sized>( self, folder: &mut F, ) -> Result { folder.fold_arg(self) } } pub fn fold_arg + ?Sized>( #[allow(unused)] folder: &mut F, node: Arg, ) -> Result, F::Error> { fold_located(folder, node, |folder, node| { let ArgData { arg, annotation, type_comment } = node; Ok(ArgData { arg: Foldable::fold(arg, folder)?, annotation: Foldable::fold(annotation, folder)?, type_comment: Foldable::fold(type_comment, folder)?, }) }) } impl Foldable for Keyword { type Mapped = Keyword; fn fold + ?Sized>( self, folder: &mut F, ) -> Result { folder.fold_keyword(self) } } pub fn fold_keyword + ?Sized>( #[allow(unused)] folder: &mut F, node: Keyword, ) -> Result, F::Error> { fold_located(folder, node, |folder, node| { let KeywordData { arg, value } = node; Ok(KeywordData { arg: Foldable::fold(arg, folder)?, value: Foldable::fold(value, folder)?, }) }) } impl Foldable for Alias { type Mapped = Alias; fn fold + ?Sized>( self, folder: &mut F, ) -> Result { folder.fold_alias(self) } } pub fn fold_alias + ?Sized>( #[allow(unused)] folder: &mut F, node: Alias, ) -> Result { let Alias { name, asname } = node; Ok(Alias { name: Foldable::fold(name, folder)?, asname: Foldable::fold(asname, folder)? }) } impl Foldable for Withitem { type Mapped = Withitem; fn fold + ?Sized>( self, folder: &mut F, ) -> Result { folder.fold_withitem(self) } } pub fn fold_withitem + ?Sized>( #[allow(unused)] folder: &mut F, node: Withitem, ) -> Result, F::Error> { let Withitem { context_expr, optional_vars } = node; Ok(Withitem { context_expr: Foldable::fold(context_expr, folder)?, optional_vars: Foldable::fold(optional_vars, folder)?, }) } impl Foldable for TypeIgnore { type Mapped = TypeIgnore; fn fold + ?Sized>( self, folder: &mut F, ) -> Result { folder.fold_type_ignore(self) } } pub fn fold_type_ignore + ?Sized>( #[allow(unused)] folder: &mut F, node: TypeIgnore, ) -> Result { match node { TypeIgnore::TypeIgnore { lineno, tag } => Ok(TypeIgnore::TypeIgnore { lineno: Foldable::fold(lineno, folder)?, tag: Foldable::fold(tag, folder)?, }), } } }