mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 10:48:32 +00:00
Upgrade RustPython (#5192)
## Summary This PR upgrade RustPython to pull in the changes to `Arguments` (zip defaults with their identifiers) and all the renames to `CmpOp` and friends.
This commit is contained in:
parent
ddfdc3bb01
commit
36e01ad6eb
103 changed files with 1291 additions and 1165 deletions
|
@ -22,16 +22,16 @@ impl From<&ast::ExprContext> for ComparableExprContext {
|
|||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
|
||||
pub enum ComparableBoolop {
|
||||
pub enum ComparableBoolOp {
|
||||
And,
|
||||
Or,
|
||||
}
|
||||
|
||||
impl From<ast::Boolop> for ComparableBoolop {
|
||||
fn from(op: ast::Boolop) -> Self {
|
||||
impl From<ast::BoolOp> for ComparableBoolOp {
|
||||
fn from(op: ast::BoolOp) -> Self {
|
||||
match op {
|
||||
ast::Boolop::And => Self::And,
|
||||
ast::Boolop::Or => Self::Or,
|
||||
ast::BoolOp::And => Self::And,
|
||||
ast::BoolOp::Or => Self::Or,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -74,26 +74,26 @@ impl From<ast::Operator> for ComparableOperator {
|
|||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
|
||||
pub enum ComparableUnaryop {
|
||||
pub enum ComparableUnaryOp {
|
||||
Invert,
|
||||
Not,
|
||||
UAdd,
|
||||
USub,
|
||||
}
|
||||
|
||||
impl From<ast::Unaryop> for ComparableUnaryop {
|
||||
fn from(op: ast::Unaryop) -> Self {
|
||||
impl From<ast::UnaryOp> for ComparableUnaryOp {
|
||||
fn from(op: ast::UnaryOp) -> Self {
|
||||
match op {
|
||||
ast::Unaryop::Invert => Self::Invert,
|
||||
ast::Unaryop::Not => Self::Not,
|
||||
ast::Unaryop::UAdd => Self::UAdd,
|
||||
ast::Unaryop::USub => Self::USub,
|
||||
ast::UnaryOp::Invert => Self::Invert,
|
||||
ast::UnaryOp::Not => Self::Not,
|
||||
ast::UnaryOp::UAdd => Self::UAdd,
|
||||
ast::UnaryOp::USub => Self::USub,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
|
||||
pub enum ComparableCmpop {
|
||||
pub enum ComparableCmpOp {
|
||||
Eq,
|
||||
NotEq,
|
||||
Lt,
|
||||
|
@ -106,19 +106,19 @@ pub enum ComparableCmpop {
|
|||
NotIn,
|
||||
}
|
||||
|
||||
impl From<ast::Cmpop> for ComparableCmpop {
|
||||
fn from(op: ast::Cmpop) -> Self {
|
||||
impl From<ast::CmpOp> for ComparableCmpOp {
|
||||
fn from(op: ast::CmpOp) -> Self {
|
||||
match op {
|
||||
ast::Cmpop::Eq => Self::Eq,
|
||||
ast::Cmpop::NotEq => Self::NotEq,
|
||||
ast::Cmpop::Lt => Self::Lt,
|
||||
ast::Cmpop::LtE => Self::LtE,
|
||||
ast::Cmpop::Gt => Self::Gt,
|
||||
ast::Cmpop::GtE => Self::GtE,
|
||||
ast::Cmpop::Is => Self::Is,
|
||||
ast::Cmpop::IsNot => Self::IsNot,
|
||||
ast::Cmpop::In => Self::In,
|
||||
ast::Cmpop::NotIn => Self::NotIn,
|
||||
ast::CmpOp::Eq => Self::Eq,
|
||||
ast::CmpOp::NotEq => Self::NotEq,
|
||||
ast::CmpOp::Lt => Self::Lt,
|
||||
ast::CmpOp::LtE => Self::LtE,
|
||||
ast::CmpOp::Gt => Self::Gt,
|
||||
ast::CmpOp::GtE => Self::GtE,
|
||||
ast::CmpOp::Is => Self::Is,
|
||||
ast::CmpOp::IsNot => Self::IsNot,
|
||||
ast::CmpOp::In => Self::In,
|
||||
ast::CmpOp::NotIn => Self::NotIn,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -139,16 +139,16 @@ impl<'a> From<&'a ast::Alias> for ComparableAlias<'a> {
|
|||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct ComparableWithitem<'a> {
|
||||
pub struct ComparableWithItem<'a> {
|
||||
context_expr: ComparableExpr<'a>,
|
||||
optional_vars: Option<ComparableExpr<'a>>,
|
||||
}
|
||||
|
||||
impl<'a> From<&'a ast::Withitem> for ComparableWithitem<'a> {
|
||||
fn from(withitem: &'a ast::Withitem) -> Self {
|
||||
impl<'a> From<&'a ast::WithItem> for ComparableWithItem<'a> {
|
||||
fn from(with_item: &'a ast::WithItem) -> Self {
|
||||
Self {
|
||||
context_expr: (&withitem.context_expr).into(),
|
||||
optional_vars: withitem.optional_vars.as_ref().map(Into::into),
|
||||
context_expr: (&with_item.context_expr).into(),
|
||||
optional_vars: with_item.optional_vars.as_ref().map(Into::into),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -342,13 +342,11 @@ impl<'a> From<&'a ast::Constant> for ComparableConstant<'a> {
|
|||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct ComparableArguments<'a> {
|
||||
posonlyargs: Vec<ComparableArg<'a>>,
|
||||
args: Vec<ComparableArg<'a>>,
|
||||
posonlyargs: Vec<ComparableArgWithDefault<'a>>,
|
||||
args: Vec<ComparableArgWithDefault<'a>>,
|
||||
vararg: Option<ComparableArg<'a>>,
|
||||
kwonlyargs: Vec<ComparableArg<'a>>,
|
||||
kw_defaults: Vec<ComparableExpr<'a>>,
|
||||
kwonlyargs: Vec<ComparableArgWithDefault<'a>>,
|
||||
kwarg: Option<ComparableArg<'a>>,
|
||||
defaults: Vec<ComparableExpr<'a>>,
|
||||
}
|
||||
|
||||
impl<'a> From<&'a ast::Arguments> for ComparableArguments<'a> {
|
||||
|
@ -358,9 +356,7 @@ impl<'a> From<&'a ast::Arguments> for ComparableArguments<'a> {
|
|||
args: arguments.args.iter().map(Into::into).collect(),
|
||||
vararg: arguments.vararg.as_ref().map(Into::into),
|
||||
kwonlyargs: arguments.kwonlyargs.iter().map(Into::into).collect(),
|
||||
kw_defaults: arguments.kw_defaults.iter().map(Into::into).collect(),
|
||||
kwarg: arguments.kwarg.as_ref().map(Into::into),
|
||||
defaults: arguments.defaults.iter().map(Into::into).collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -394,6 +390,21 @@ impl<'a> From<&'a ast::Arg> for ComparableArg<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct ComparableArgWithDefault<'a> {
|
||||
def: ComparableArg<'a>,
|
||||
default: Option<ComparableExpr<'a>>,
|
||||
}
|
||||
|
||||
impl<'a> From<&'a ast::ArgWithDefault> for ComparableArgWithDefault<'a> {
|
||||
fn from(arg: &'a ast::ArgWithDefault) -> Self {
|
||||
Self {
|
||||
def: (&arg.def).into(),
|
||||
default: arg.default.as_ref().map(Into::into),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct ComparableKeyword<'a> {
|
||||
arg: Option<&'a str>,
|
||||
|
@ -429,26 +440,26 @@ impl<'a> From<&'a ast::Comprehension> for ComparableComprehension<'a> {
|
|||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct ExcepthandlerExceptHandler<'a> {
|
||||
pub struct ExceptHandlerExceptHandler<'a> {
|
||||
type_: Option<Box<ComparableExpr<'a>>>,
|
||||
name: Option<&'a str>,
|
||||
body: Vec<ComparableStmt<'a>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub enum ComparableExcepthandler<'a> {
|
||||
ExceptHandler(ExcepthandlerExceptHandler<'a>),
|
||||
pub enum ComparableExceptHandler<'a> {
|
||||
ExceptHandler(ExceptHandlerExceptHandler<'a>),
|
||||
}
|
||||
|
||||
impl<'a> From<&'a ast::Excepthandler> for ComparableExcepthandler<'a> {
|
||||
fn from(excepthandler: &'a ast::Excepthandler) -> Self {
|
||||
let ast::Excepthandler::ExceptHandler(ast::ExcepthandlerExceptHandler {
|
||||
impl<'a> From<&'a ast::ExceptHandler> for ComparableExceptHandler<'a> {
|
||||
fn from(except_handler: &'a ast::ExceptHandler) -> Self {
|
||||
let ast::ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler {
|
||||
type_,
|
||||
name,
|
||||
body,
|
||||
..
|
||||
}) = excepthandler;
|
||||
Self::ExceptHandler(ExcepthandlerExceptHandler {
|
||||
}) = except_handler;
|
||||
Self::ExceptHandler(ExceptHandlerExceptHandler {
|
||||
type_: type_.as_ref().map(Into::into),
|
||||
name: name.as_deref(),
|
||||
body: body.iter().map(Into::into).collect(),
|
||||
|
@ -458,7 +469,7 @@ impl<'a> From<&'a ast::Excepthandler> for ComparableExcepthandler<'a> {
|
|||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct ExprBoolOp<'a> {
|
||||
op: ComparableBoolop,
|
||||
op: ComparableBoolOp,
|
||||
values: Vec<ComparableExpr<'a>>,
|
||||
}
|
||||
|
||||
|
@ -477,7 +488,7 @@ pub struct ExprBinOp<'a> {
|
|||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct ExprUnaryOp<'a> {
|
||||
op: ComparableUnaryop,
|
||||
op: ComparableUnaryOp,
|
||||
operand: Box<ComparableExpr<'a>>,
|
||||
}
|
||||
|
||||
|
@ -548,7 +559,7 @@ pub struct ExprYieldFrom<'a> {
|
|||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct ExprCompare<'a> {
|
||||
left: Box<ComparableExpr<'a>>,
|
||||
ops: Vec<ComparableCmpop>,
|
||||
ops: Vec<ComparableCmpOp>,
|
||||
comparators: Vec<ComparableExpr<'a>>,
|
||||
}
|
||||
|
||||
|
@ -994,14 +1005,14 @@ pub struct StmtIf<'a> {
|
|||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct StmtWith<'a> {
|
||||
items: Vec<ComparableWithitem<'a>>,
|
||||
items: Vec<ComparableWithItem<'a>>,
|
||||
body: Vec<ComparableStmt<'a>>,
|
||||
type_comment: Option<&'a str>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct StmtAsyncWith<'a> {
|
||||
items: Vec<ComparableWithitem<'a>>,
|
||||
items: Vec<ComparableWithItem<'a>>,
|
||||
body: Vec<ComparableStmt<'a>>,
|
||||
type_comment: Option<&'a str>,
|
||||
}
|
||||
|
@ -1021,7 +1032,7 @@ pub struct StmtRaise<'a> {
|
|||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct StmtTry<'a> {
|
||||
body: Vec<ComparableStmt<'a>>,
|
||||
handlers: Vec<ComparableExcepthandler<'a>>,
|
||||
handlers: Vec<ComparableExceptHandler<'a>>,
|
||||
orelse: Vec<ComparableStmt<'a>>,
|
||||
finalbody: Vec<ComparableStmt<'a>>,
|
||||
}
|
||||
|
@ -1029,7 +1040,7 @@ pub struct StmtTry<'a> {
|
|||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct StmtTryStar<'a> {
|
||||
body: Vec<ComparableStmt<'a>>,
|
||||
handlers: Vec<ComparableExcepthandler<'a>>,
|
||||
handlers: Vec<ComparableExceptHandler<'a>>,
|
||||
orelse: Vec<ComparableStmt<'a>>,
|
||||
finalbody: Vec<ComparableStmt<'a>>,
|
||||
}
|
||||
|
|
|
@ -5,9 +5,9 @@ use std::path::Path;
|
|||
use num_traits::Zero;
|
||||
use ruff_text_size::{TextRange, TextSize};
|
||||
use rustc_hash::{FxHashMap, FxHashSet};
|
||||
use rustpython_ast::Cmpop;
|
||||
use rustpython_ast::CmpOp;
|
||||
use rustpython_parser::ast::{
|
||||
self, Arguments, Constant, Excepthandler, Expr, Keyword, MatchCase, Pattern, Ranged, Stmt,
|
||||
self, Arguments, Constant, ExceptHandler, Expr, Keyword, MatchCase, Pattern, Ranged, Stmt,
|
||||
};
|
||||
use rustpython_parser::{lexer, Mode, Tok};
|
||||
use smallvec::SmallVec;
|
||||
|
@ -333,25 +333,19 @@ where
|
|||
returns,
|
||||
..
|
||||
}) => {
|
||||
args.defaults.iter().any(|expr| any_over_expr(expr, func))
|
||||
|| args
|
||||
.kw_defaults
|
||||
.iter()
|
||||
.any(|expr| any_over_expr(expr, func))
|
||||
|| args.args.iter().any(|arg| {
|
||||
arg.annotation
|
||||
.as_ref()
|
||||
.map_or(false, |expr| any_over_expr(expr, func))
|
||||
})
|
||||
|| args.kwonlyargs.iter().any(|arg| {
|
||||
arg.annotation
|
||||
.as_ref()
|
||||
.map_or(false, |expr| any_over_expr(expr, func))
|
||||
})
|
||||
|| args.posonlyargs.iter().any(|arg| {
|
||||
arg.annotation
|
||||
args.posonlyargs
|
||||
.iter()
|
||||
.chain(args.args.iter().chain(args.kwonlyargs.iter()))
|
||||
.any(|arg_with_default| {
|
||||
arg_with_default
|
||||
.default
|
||||
.as_ref()
|
||||
.map_or(false, |expr| any_over_expr(expr, func))
|
||||
|| arg_with_default
|
||||
.def
|
||||
.annotation
|
||||
.as_ref()
|
||||
.map_or(false, |expr| any_over_expr(expr, func))
|
||||
})
|
||||
|| args.vararg.as_ref().map_or(false, |arg| {
|
||||
arg.annotation
|
||||
|
@ -448,9 +442,9 @@ where
|
|||
}) => any_over_expr(test, func) || any_over_body(body, func) || any_over_body(orelse, func),
|
||||
Stmt::With(ast::StmtWith { items, body, .. })
|
||||
| Stmt::AsyncWith(ast::StmtAsyncWith { items, body, .. }) => {
|
||||
items.iter().any(|withitem| {
|
||||
any_over_expr(&withitem.context_expr, func)
|
||||
|| withitem
|
||||
items.iter().any(|with_item| {
|
||||
any_over_expr(&with_item.context_expr, func)
|
||||
|| with_item
|
||||
.optional_vars
|
||||
.as_ref()
|
||||
.map_or(false, |expr| any_over_expr(expr, func))
|
||||
|
@ -483,7 +477,7 @@ where
|
|||
}) => {
|
||||
any_over_body(body, func)
|
||||
|| handlers.iter().any(|handler| {
|
||||
let Excepthandler::ExceptHandler(ast::ExcepthandlerExceptHandler {
|
||||
let ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler {
|
||||
type_,
|
||||
body,
|
||||
..
|
||||
|
@ -655,11 +649,11 @@ pub fn has_non_none_keyword(keywords: &[Keyword], keyword: &str) -> bool {
|
|||
}
|
||||
|
||||
/// Extract the names of all handled exceptions.
|
||||
pub fn extract_handled_exceptions(handlers: &[Excepthandler]) -> Vec<&Expr> {
|
||||
pub fn extract_handled_exceptions(handlers: &[ExceptHandler]) -> Vec<&Expr> {
|
||||
let mut handled_exceptions = Vec::new();
|
||||
for handler in handlers {
|
||||
match handler {
|
||||
Excepthandler::ExceptHandler(ast::ExcepthandlerExceptHandler { type_, .. }) => {
|
||||
ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler { type_, .. }) => {
|
||||
if let Some(type_) = type_ {
|
||||
if let Expr::Tuple(ast::ExprTuple { elts, .. }) = &type_.as_ref() {
|
||||
for type_ in elts {
|
||||
|
@ -678,17 +672,17 @@ pub fn extract_handled_exceptions(handlers: &[Excepthandler]) -> Vec<&Expr> {
|
|||
/// Return the set of all bound argument names.
|
||||
pub fn collect_arg_names<'a>(arguments: &'a Arguments) -> FxHashSet<&'a str> {
|
||||
let mut arg_names: FxHashSet<&'a str> = FxHashSet::default();
|
||||
for arg in &arguments.posonlyargs {
|
||||
arg_names.insert(arg.arg.as_str());
|
||||
for arg_with_default in &arguments.posonlyargs {
|
||||
arg_names.insert(arg_with_default.def.arg.as_str());
|
||||
}
|
||||
for arg in &arguments.args {
|
||||
arg_names.insert(arg.arg.as_str());
|
||||
for arg_with_default in &arguments.args {
|
||||
arg_names.insert(arg_with_default.def.arg.as_str());
|
||||
}
|
||||
if let Some(arg) = &arguments.vararg {
|
||||
arg_names.insert(arg.arg.as_str());
|
||||
}
|
||||
for arg in &arguments.kwonlyargs {
|
||||
arg_names.insert(arg.arg.as_str());
|
||||
for arg_with_default in &arguments.kwonlyargs {
|
||||
arg_names.insert(arg_with_default.def.arg.as_str());
|
||||
}
|
||||
if let Some(arg) = &arguments.kwarg {
|
||||
arg_names.insert(arg.arg.as_str());
|
||||
|
@ -1409,13 +1403,13 @@ impl Truthiness {
|
|||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct LocatedCmpop {
|
||||
pub struct LocatedCmpOp {
|
||||
pub range: TextRange,
|
||||
pub op: Cmpop,
|
||||
pub op: CmpOp,
|
||||
}
|
||||
|
||||
impl LocatedCmpop {
|
||||
fn new<T: Into<TextRange>>(range: T, op: Cmpop) -> Self {
|
||||
impl LocatedCmpOp {
|
||||
fn new<T: Into<TextRange>>(range: T, op: CmpOp) -> Self {
|
||||
Self {
|
||||
range: range.into(),
|
||||
op,
|
||||
|
@ -1423,13 +1417,13 @@ impl LocatedCmpop {
|
|||
}
|
||||
}
|
||||
|
||||
/// Extract all [`Cmpop`] operators from an expression snippet, with appropriate
|
||||
/// Extract all [`CmpOp`] operators from an expression snippet, with appropriate
|
||||
/// ranges.
|
||||
///
|
||||
/// `RustPython` doesn't include line and column information on [`Cmpop`] nodes.
|
||||
/// `RustPython` doesn't include line and column information on [`CmpOp`] nodes.
|
||||
/// `CPython` doesn't either. This method iterates over the token stream and
|
||||
/// re-identifies [`Cmpop`] nodes, annotating them with valid ranges.
|
||||
pub fn locate_cmpops(expr: &Expr, locator: &Locator) -> Vec<LocatedCmpop> {
|
||||
/// re-identifies [`CmpOp`] nodes, annotating them with valid ranges.
|
||||
pub fn locate_cmp_ops(expr: &Expr, locator: &Locator) -> Vec<LocatedCmpOp> {
|
||||
// If `Expr` is a multi-line expression, we need to parenthesize it to
|
||||
// ensure that it's lexed correctly.
|
||||
let contents = locator.slice(expr.range());
|
||||
|
@ -1441,7 +1435,7 @@ pub fn locate_cmpops(expr: &Expr, locator: &Locator) -> Vec<LocatedCmpop> {
|
|||
.filter(|(tok, _)| !matches!(tok, Tok::NonLogicalNewline | Tok::Comment(_)))
|
||||
.peekable();
|
||||
|
||||
let mut ops: Vec<LocatedCmpop> = vec![];
|
||||
let mut ops: Vec<LocatedCmpOp> = vec![];
|
||||
let mut count = 0u32;
|
||||
loop {
|
||||
let Some((tok, range)) = tok_iter.next() else {
|
||||
|
@ -1460,45 +1454,45 @@ pub fn locate_cmpops(expr: &Expr, locator: &Locator) -> Vec<LocatedCmpop> {
|
|||
if let Some((_, next_range)) =
|
||||
tok_iter.next_if(|(tok, _)| matches!(tok, Tok::In))
|
||||
{
|
||||
ops.push(LocatedCmpop::new(
|
||||
ops.push(LocatedCmpOp::new(
|
||||
TextRange::new(range.start(), next_range.end()),
|
||||
Cmpop::NotIn,
|
||||
CmpOp::NotIn,
|
||||
));
|
||||
}
|
||||
}
|
||||
Tok::In => {
|
||||
ops.push(LocatedCmpop::new(range, Cmpop::In));
|
||||
ops.push(LocatedCmpOp::new(range, CmpOp::In));
|
||||
}
|
||||
Tok::Is => {
|
||||
let op = if let Some((_, next_range)) =
|
||||
tok_iter.next_if(|(tok, _)| matches!(tok, Tok::Not))
|
||||
{
|
||||
LocatedCmpop::new(
|
||||
LocatedCmpOp::new(
|
||||
TextRange::new(range.start(), next_range.end()),
|
||||
Cmpop::IsNot,
|
||||
CmpOp::IsNot,
|
||||
)
|
||||
} else {
|
||||
LocatedCmpop::new(range, Cmpop::Is)
|
||||
LocatedCmpOp::new(range, CmpOp::Is)
|
||||
};
|
||||
ops.push(op);
|
||||
}
|
||||
Tok::NotEqual => {
|
||||
ops.push(LocatedCmpop::new(range, Cmpop::NotEq));
|
||||
ops.push(LocatedCmpOp::new(range, CmpOp::NotEq));
|
||||
}
|
||||
Tok::EqEqual => {
|
||||
ops.push(LocatedCmpop::new(range, Cmpop::Eq));
|
||||
ops.push(LocatedCmpOp::new(range, CmpOp::Eq));
|
||||
}
|
||||
Tok::GreaterEqual => {
|
||||
ops.push(LocatedCmpop::new(range, Cmpop::GtE));
|
||||
ops.push(LocatedCmpOp::new(range, CmpOp::GtE));
|
||||
}
|
||||
Tok::Greater => {
|
||||
ops.push(LocatedCmpop::new(range, Cmpop::Gt));
|
||||
ops.push(LocatedCmpOp::new(range, CmpOp::Gt));
|
||||
}
|
||||
Tok::LessEqual => {
|
||||
ops.push(LocatedCmpop::new(range, Cmpop::LtE));
|
||||
ops.push(LocatedCmpOp::new(range, CmpOp::LtE));
|
||||
}
|
||||
Tok::Less => {
|
||||
ops.push(LocatedCmpop::new(range, Cmpop::Lt));
|
||||
ops.push(LocatedCmpOp::new(range, CmpOp::Lt));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
@ -1513,13 +1507,13 @@ mod tests {
|
|||
|
||||
use anyhow::Result;
|
||||
use ruff_text_size::{TextLen, TextRange, TextSize};
|
||||
use rustpython_ast::{Cmpop, Expr, Stmt};
|
||||
use rustpython_ast::{CmpOp, Expr, Stmt};
|
||||
use rustpython_parser::ast::Suite;
|
||||
use rustpython_parser::Parse;
|
||||
|
||||
use crate::helpers::{
|
||||
elif_else_range, first_colon_range, has_trailing_content, locate_cmpops,
|
||||
resolve_imported_module_path, LocatedCmpop,
|
||||
elif_else_range, first_colon_range, has_trailing_content, locate_cmp_ops,
|
||||
resolve_imported_module_path, LocatedCmpOp,
|
||||
};
|
||||
use crate::source_code::Locator;
|
||||
|
||||
|
@ -1642,15 +1636,15 @@ else:
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn extract_cmpop_location() -> Result<()> {
|
||||
fn extract_cmp_op_location() -> Result<()> {
|
||||
let contents = "x == 1";
|
||||
let expr = Expr::parse(contents, "<filename>")?;
|
||||
let locator = Locator::new(contents);
|
||||
assert_eq!(
|
||||
locate_cmpops(&expr, &locator),
|
||||
vec![LocatedCmpop::new(
|
||||
locate_cmp_ops(&expr, &locator),
|
||||
vec![LocatedCmpOp::new(
|
||||
TextSize::from(2)..TextSize::from(4),
|
||||
Cmpop::Eq
|
||||
CmpOp::Eq
|
||||
)]
|
||||
);
|
||||
|
||||
|
@ -1658,10 +1652,10 @@ else:
|
|||
let expr = Expr::parse(contents, "<filename>")?;
|
||||
let locator = Locator::new(contents);
|
||||
assert_eq!(
|
||||
locate_cmpops(&expr, &locator),
|
||||
vec![LocatedCmpop::new(
|
||||
locate_cmp_ops(&expr, &locator),
|
||||
vec![LocatedCmpOp::new(
|
||||
TextSize::from(2)..TextSize::from(4),
|
||||
Cmpop::NotEq
|
||||
CmpOp::NotEq
|
||||
)]
|
||||
);
|
||||
|
||||
|
@ -1669,10 +1663,10 @@ else:
|
|||
let expr = Expr::parse(contents, "<filename>")?;
|
||||
let locator = Locator::new(contents);
|
||||
assert_eq!(
|
||||
locate_cmpops(&expr, &locator),
|
||||
vec![LocatedCmpop::new(
|
||||
locate_cmp_ops(&expr, &locator),
|
||||
vec![LocatedCmpOp::new(
|
||||
TextSize::from(2)..TextSize::from(4),
|
||||
Cmpop::Is
|
||||
CmpOp::Is
|
||||
)]
|
||||
);
|
||||
|
||||
|
@ -1680,10 +1674,10 @@ else:
|
|||
let expr = Expr::parse(contents, "<filename>")?;
|
||||
let locator = Locator::new(contents);
|
||||
assert_eq!(
|
||||
locate_cmpops(&expr, &locator),
|
||||
vec![LocatedCmpop::new(
|
||||
locate_cmp_ops(&expr, &locator),
|
||||
vec![LocatedCmpOp::new(
|
||||
TextSize::from(2)..TextSize::from(8),
|
||||
Cmpop::IsNot
|
||||
CmpOp::IsNot
|
||||
)]
|
||||
);
|
||||
|
||||
|
@ -1691,10 +1685,10 @@ else:
|
|||
let expr = Expr::parse(contents, "<filename>")?;
|
||||
let locator = Locator::new(contents);
|
||||
assert_eq!(
|
||||
locate_cmpops(&expr, &locator),
|
||||
vec![LocatedCmpop::new(
|
||||
locate_cmp_ops(&expr, &locator),
|
||||
vec![LocatedCmpOp::new(
|
||||
TextSize::from(2)..TextSize::from(4),
|
||||
Cmpop::In
|
||||
CmpOp::In
|
||||
)]
|
||||
);
|
||||
|
||||
|
@ -1702,10 +1696,10 @@ else:
|
|||
let expr = Expr::parse(contents, "<filename>")?;
|
||||
let locator = Locator::new(contents);
|
||||
assert_eq!(
|
||||
locate_cmpops(&expr, &locator),
|
||||
vec![LocatedCmpop::new(
|
||||
locate_cmp_ops(&expr, &locator),
|
||||
vec![LocatedCmpOp::new(
|
||||
TextSize::from(2)..TextSize::from(8),
|
||||
Cmpop::NotIn
|
||||
CmpOp::NotIn
|
||||
)]
|
||||
);
|
||||
|
||||
|
@ -1713,10 +1707,10 @@ else:
|
|||
let expr = Expr::parse(contents, "<filename>")?;
|
||||
let locator = Locator::new(contents);
|
||||
assert_eq!(
|
||||
locate_cmpops(&expr, &locator),
|
||||
vec![LocatedCmpop::new(
|
||||
locate_cmp_ops(&expr, &locator),
|
||||
vec![LocatedCmpOp::new(
|
||||
TextSize::from(2)..TextSize::from(4),
|
||||
Cmpop::NotEq
|
||||
CmpOp::NotEq
|
||||
)]
|
||||
);
|
||||
|
||||
|
|
|
@ -20,8 +20,8 @@ use std::ops::{Add, Sub};
|
|||
use std::str::Chars;
|
||||
|
||||
use ruff_text_size::{TextLen, TextRange, TextSize};
|
||||
use rustpython_ast::{Alias, Arg, Pattern};
|
||||
use rustpython_parser::ast::{self, Excepthandler, Ranged, Stmt};
|
||||
use rustpython_ast::{Alias, Arg, ArgWithDefault, Pattern};
|
||||
use rustpython_parser::ast::{self, ExceptHandler, Ranged, Stmt};
|
||||
|
||||
use ruff_python_whitespace::is_python_whitespace;
|
||||
|
||||
|
@ -94,7 +94,7 @@ impl Identifier for Arg {
|
|||
///
|
||||
/// For example, return the range of `x` in:
|
||||
/// ```python
|
||||
/// def f(x: int = 0):
|
||||
/// def f(x: int):
|
||||
/// ...
|
||||
/// ```
|
||||
fn identifier(&self, locator: &Locator) -> TextRange {
|
||||
|
@ -104,6 +104,19 @@ impl Identifier for Arg {
|
|||
}
|
||||
}
|
||||
|
||||
impl Identifier for ArgWithDefault {
|
||||
/// Return the [`TextRange`] for the identifier defining an [`ArgWithDefault`].
|
||||
///
|
||||
/// For example, return the range of `x` in:
|
||||
/// ```python
|
||||
/// def f(x: int = 0):
|
||||
/// ...
|
||||
/// ```
|
||||
fn identifier(&self, locator: &Locator) -> TextRange {
|
||||
self.def.identifier(locator)
|
||||
}
|
||||
}
|
||||
|
||||
impl Identifier for Alias {
|
||||
/// Return the [`TextRange`] for the identifier defining an [`Alias`].
|
||||
///
|
||||
|
@ -239,8 +252,8 @@ impl TryIdentifier for Pattern {
|
|||
}
|
||||
}
|
||||
|
||||
impl TryIdentifier for Excepthandler {
|
||||
/// Return the [`TextRange`] of a named exception in an [`Excepthandler`].
|
||||
impl TryIdentifier for ExceptHandler {
|
||||
/// Return the [`TextRange`] of a named exception in an [`ExceptHandler`].
|
||||
///
|
||||
/// For example, return the range of `e` in:
|
||||
/// ```python
|
||||
|
@ -250,7 +263,7 @@ impl TryIdentifier for Excepthandler {
|
|||
/// ...
|
||||
/// ```
|
||||
fn try_identifier(&self, locator: &Locator) -> Option<TextRange> {
|
||||
let Excepthandler::ExceptHandler(ast::ExcepthandlerExceptHandler { type_, name, .. }) =
|
||||
let ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler { type_, name, .. }) =
|
||||
self;
|
||||
|
||||
if name.is_none() {
|
||||
|
@ -284,11 +297,11 @@ pub fn names<'a>(stmt: &Stmt, locator: &'a Locator<'a>) -> impl Iterator<Item =
|
|||
IdentifierTokenizer::new(locator.contents(), stmt.range()).skip(1)
|
||||
}
|
||||
|
||||
/// Return the [`TextRange`] of the `except` token in an [`Excepthandler`].
|
||||
pub fn except(handler: &Excepthandler, locator: &Locator) -> TextRange {
|
||||
/// Return the [`TextRange`] of the `except` token in an [`ExceptHandler`].
|
||||
pub fn except(handler: &ExceptHandler, locator: &Locator) -> TextRange {
|
||||
IdentifierTokenizer::new(locator.contents(), handler.range())
|
||||
.next()
|
||||
.expect("Failed to find `except` token in `Excepthandler`")
|
||||
.expect("Failed to find `except` token in `ExceptHandler`")
|
||||
}
|
||||
|
||||
/// Return the [`TextRange`] of the `else` token in a `For`, `AsyncFor`, or `While` statement.
|
||||
|
|
|
@ -75,7 +75,7 @@ pub enum AnyNode {
|
|||
ExprList(ExprList<TextRange>),
|
||||
ExprTuple(ExprTuple<TextRange>),
|
||||
ExprSlice(ExprSlice<TextRange>),
|
||||
ExcepthandlerExceptHandler(ExcepthandlerExceptHandler<TextRange>),
|
||||
ExceptHandlerExceptHandler(ExceptHandlerExceptHandler<TextRange>),
|
||||
PatternMatchValue(PatternMatchValue<TextRange>),
|
||||
PatternMatchSingleton(PatternMatchSingleton<TextRange>),
|
||||
PatternMatchSequence(PatternMatchSequence<TextRange>),
|
||||
|
@ -88,9 +88,10 @@ pub enum AnyNode {
|
|||
Comprehension(Comprehension<TextRange>),
|
||||
Arguments(Arguments<TextRange>),
|
||||
Arg(Arg<TextRange>),
|
||||
ArgWithDefault(ArgWithDefault<TextRange>),
|
||||
Keyword(Keyword<TextRange>),
|
||||
Alias(Alias<TextRange>),
|
||||
Withitem(Withitem<TextRange>),
|
||||
WithItem(WithItem<TextRange>),
|
||||
MatchCase(MatchCase<TextRange>),
|
||||
Decorator(Decorator<TextRange>),
|
||||
}
|
||||
|
@ -157,7 +158,7 @@ impl AnyNode {
|
|||
| AnyNode::ExprList(_)
|
||||
| AnyNode::ExprTuple(_)
|
||||
| AnyNode::ExprSlice(_)
|
||||
| AnyNode::ExcepthandlerExceptHandler(_)
|
||||
| AnyNode::ExceptHandlerExceptHandler(_)
|
||||
| AnyNode::PatternMatchValue(_)
|
||||
| AnyNode::PatternMatchSingleton(_)
|
||||
| AnyNode::PatternMatchSequence(_)
|
||||
|
@ -170,9 +171,10 @@ impl AnyNode {
|
|||
| AnyNode::Comprehension(_)
|
||||
| AnyNode::Arguments(_)
|
||||
| AnyNode::Arg(_)
|
||||
| AnyNode::ArgWithDefault(_)
|
||||
| AnyNode::Keyword(_)
|
||||
| AnyNode::Alias(_)
|
||||
| AnyNode::Withitem(_)
|
||||
| AnyNode::WithItem(_)
|
||||
| AnyNode::MatchCase(_)
|
||||
| AnyNode::Decorator(_) => None,
|
||||
}
|
||||
|
@ -239,7 +241,7 @@ impl AnyNode {
|
|||
| AnyNode::StmtPass(_)
|
||||
| AnyNode::StmtBreak(_)
|
||||
| AnyNode::StmtContinue(_)
|
||||
| AnyNode::ExcepthandlerExceptHandler(_)
|
||||
| AnyNode::ExceptHandlerExceptHandler(_)
|
||||
| AnyNode::PatternMatchValue(_)
|
||||
| AnyNode::PatternMatchSingleton(_)
|
||||
| AnyNode::PatternMatchSequence(_)
|
||||
|
@ -252,9 +254,10 @@ impl AnyNode {
|
|||
| AnyNode::Comprehension(_)
|
||||
| AnyNode::Arguments(_)
|
||||
| AnyNode::Arg(_)
|
||||
| AnyNode::ArgWithDefault(_)
|
||||
| AnyNode::Keyword(_)
|
||||
| AnyNode::Alias(_)
|
||||
| AnyNode::Withitem(_)
|
||||
| AnyNode::WithItem(_)
|
||||
| AnyNode::MatchCase(_)
|
||||
| AnyNode::Decorator(_) => None,
|
||||
}
|
||||
|
@ -321,7 +324,7 @@ impl AnyNode {
|
|||
| AnyNode::ExprList(_)
|
||||
| AnyNode::ExprTuple(_)
|
||||
| AnyNode::ExprSlice(_)
|
||||
| AnyNode::ExcepthandlerExceptHandler(_)
|
||||
| AnyNode::ExceptHandlerExceptHandler(_)
|
||||
| AnyNode::PatternMatchValue(_)
|
||||
| AnyNode::PatternMatchSingleton(_)
|
||||
| AnyNode::PatternMatchSequence(_)
|
||||
|
@ -334,9 +337,10 @@ impl AnyNode {
|
|||
| AnyNode::Comprehension(_)
|
||||
| AnyNode::Arguments(_)
|
||||
| AnyNode::Arg(_)
|
||||
| AnyNode::ArgWithDefault(_)
|
||||
| AnyNode::Keyword(_)
|
||||
| AnyNode::Alias(_)
|
||||
| AnyNode::Withitem(_)
|
||||
| AnyNode::WithItem(_)
|
||||
| AnyNode::MatchCase(_)
|
||||
| AnyNode::Decorator(_) => None,
|
||||
}
|
||||
|
@ -411,22 +415,23 @@ impl AnyNode {
|
|||
| AnyNode::ExprList(_)
|
||||
| AnyNode::ExprTuple(_)
|
||||
| AnyNode::ExprSlice(_)
|
||||
| AnyNode::ExcepthandlerExceptHandler(_)
|
||||
| AnyNode::ExceptHandlerExceptHandler(_)
|
||||
| AnyNode::TypeIgnoreTypeIgnore(_)
|
||||
| AnyNode::Comprehension(_)
|
||||
| AnyNode::Arguments(_)
|
||||
| AnyNode::Arg(_)
|
||||
| AnyNode::ArgWithDefault(_)
|
||||
| AnyNode::Keyword(_)
|
||||
| AnyNode::Alias(_)
|
||||
| AnyNode::Withitem(_)
|
||||
| AnyNode::WithItem(_)
|
||||
| AnyNode::MatchCase(_)
|
||||
| AnyNode::Decorator(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn except_handler(self) -> Option<Excepthandler> {
|
||||
pub fn except_handler(self) -> Option<ExceptHandler> {
|
||||
match self {
|
||||
AnyNode::ExcepthandlerExceptHandler(node) => Some(Excepthandler::ExceptHandler(node)),
|
||||
AnyNode::ExceptHandlerExceptHandler(node) => Some(ExceptHandler::ExceptHandler(node)),
|
||||
|
||||
AnyNode::ModModule(_)
|
||||
| AnyNode::ModInteractive(_)
|
||||
|
@ -498,9 +503,10 @@ impl AnyNode {
|
|||
| AnyNode::Comprehension(_)
|
||||
| AnyNode::Arguments(_)
|
||||
| AnyNode::Arg(_)
|
||||
| AnyNode::ArgWithDefault(_)
|
||||
| AnyNode::Keyword(_)
|
||||
| AnyNode::Alias(_)
|
||||
| AnyNode::Withitem(_)
|
||||
| AnyNode::WithItem(_)
|
||||
| AnyNode::MatchCase(_)
|
||||
| AnyNode::Decorator(_) => None,
|
||||
}
|
||||
|
@ -576,13 +582,14 @@ impl AnyNode {
|
|||
| AnyNode::PatternMatchStar(_)
|
||||
| AnyNode::PatternMatchAs(_)
|
||||
| AnyNode::PatternMatchOr(_)
|
||||
| AnyNode::ExcepthandlerExceptHandler(_)
|
||||
| AnyNode::ExceptHandlerExceptHandler(_)
|
||||
| AnyNode::Comprehension(_)
|
||||
| AnyNode::Arguments(_)
|
||||
| AnyNode::Arg(_)
|
||||
| AnyNode::ArgWithDefault(_)
|
||||
| AnyNode::Keyword(_)
|
||||
| AnyNode::Alias(_)
|
||||
| AnyNode::Withitem(_)
|
||||
| AnyNode::WithItem(_)
|
||||
| AnyNode::MatchCase(_)
|
||||
| AnyNode::Decorator(_) => None,
|
||||
}
|
||||
|
@ -672,7 +679,7 @@ impl AnyNode {
|
|||
Self::ExprList(node) => AnyNodeRef::ExprList(node),
|
||||
Self::ExprTuple(node) => AnyNodeRef::ExprTuple(node),
|
||||
Self::ExprSlice(node) => AnyNodeRef::ExprSlice(node),
|
||||
Self::ExcepthandlerExceptHandler(node) => AnyNodeRef::ExcepthandlerExceptHandler(node),
|
||||
Self::ExceptHandlerExceptHandler(node) => AnyNodeRef::ExceptHandlerExceptHandler(node),
|
||||
Self::PatternMatchValue(node) => AnyNodeRef::PatternMatchValue(node),
|
||||
Self::PatternMatchSingleton(node) => AnyNodeRef::PatternMatchSingleton(node),
|
||||
Self::PatternMatchSequence(node) => AnyNodeRef::PatternMatchSequence(node),
|
||||
|
@ -685,9 +692,10 @@ impl AnyNode {
|
|||
Self::Comprehension(node) => AnyNodeRef::Comprehension(node),
|
||||
Self::Arguments(node) => AnyNodeRef::Arguments(node),
|
||||
Self::Arg(node) => AnyNodeRef::Arg(node),
|
||||
Self::ArgWithDefault(node) => AnyNodeRef::ArgWithDefault(node),
|
||||
Self::Keyword(node) => AnyNodeRef::Keyword(node),
|
||||
Self::Alias(node) => AnyNodeRef::Alias(node),
|
||||
Self::Withitem(node) => AnyNodeRef::Withitem(node),
|
||||
Self::WithItem(node) => AnyNodeRef::WithItem(node),
|
||||
Self::MatchCase(node) => AnyNodeRef::MatchCase(node),
|
||||
Self::Decorator(node) => AnyNodeRef::Decorator(node),
|
||||
}
|
||||
|
@ -2323,12 +2331,12 @@ impl AstNode for ExprSlice<TextRange> {
|
|||
AnyNode::from(self)
|
||||
}
|
||||
}
|
||||
impl AstNode for ExcepthandlerExceptHandler<TextRange> {
|
||||
impl AstNode for ExceptHandlerExceptHandler<TextRange> {
|
||||
fn cast(kind: AnyNode) -> Option<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
if let AnyNode::ExcepthandlerExceptHandler(node) = kind {
|
||||
if let AnyNode::ExceptHandlerExceptHandler(node) = kind {
|
||||
Some(node)
|
||||
} else {
|
||||
None
|
||||
|
@ -2336,7 +2344,7 @@ impl AstNode for ExcepthandlerExceptHandler<TextRange> {
|
|||
}
|
||||
|
||||
fn cast_ref(kind: AnyNodeRef) -> Option<&Self> {
|
||||
if let AnyNodeRef::ExcepthandlerExceptHandler(node) = kind {
|
||||
if let AnyNodeRef::ExceptHandlerExceptHandler(node) = kind {
|
||||
Some(node)
|
||||
} else {
|
||||
None
|
||||
|
@ -2688,6 +2696,34 @@ impl AstNode for Arg<TextRange> {
|
|||
AnyNode::from(self)
|
||||
}
|
||||
}
|
||||
impl AstNode for ArgWithDefault<TextRange> {
|
||||
fn cast(kind: AnyNode) -> Option<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
if let AnyNode::ArgWithDefault(node) = kind {
|
||||
Some(node)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn cast_ref(kind: AnyNodeRef) -> Option<&Self> {
|
||||
if let AnyNodeRef::ArgWithDefault(node) = kind {
|
||||
Some(node)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn as_any_node_ref(&self) -> AnyNodeRef {
|
||||
AnyNodeRef::from(self)
|
||||
}
|
||||
|
||||
fn into_any_node(self) -> AnyNode {
|
||||
AnyNode::from(self)
|
||||
}
|
||||
}
|
||||
impl AstNode for Keyword<TextRange> {
|
||||
fn cast(kind: AnyNode) -> Option<Self>
|
||||
where
|
||||
|
@ -2744,12 +2780,12 @@ impl AstNode for Alias<TextRange> {
|
|||
AnyNode::from(self)
|
||||
}
|
||||
}
|
||||
impl AstNode for Withitem<TextRange> {
|
||||
impl AstNode for WithItem<TextRange> {
|
||||
fn cast(kind: AnyNode) -> Option<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
if let AnyNode::Withitem(node) = kind {
|
||||
if let AnyNode::WithItem(node) = kind {
|
||||
Some(node)
|
||||
} else {
|
||||
None
|
||||
|
@ -2757,7 +2793,7 @@ impl AstNode for Withitem<TextRange> {
|
|||
}
|
||||
|
||||
fn cast_ref(kind: AnyNodeRef) -> Option<&Self> {
|
||||
if let AnyNodeRef::Withitem(node) = kind {
|
||||
if let AnyNodeRef::WithItem(node) = kind {
|
||||
Some(node)
|
||||
} else {
|
||||
None
|
||||
|
@ -2924,10 +2960,10 @@ impl From<Pattern> for AnyNode {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<Excepthandler> for AnyNode {
|
||||
fn from(handler: Excepthandler) -> Self {
|
||||
impl From<ExceptHandler> for AnyNode {
|
||||
fn from(handler: ExceptHandler) -> Self {
|
||||
match handler {
|
||||
Excepthandler::ExceptHandler(handler) => AnyNode::ExcepthandlerExceptHandler(handler),
|
||||
ExceptHandler::ExceptHandler(handler) => AnyNode::ExceptHandlerExceptHandler(handler),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3288,9 +3324,9 @@ impl From<ExprSlice> for AnyNode {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<ExcepthandlerExceptHandler> for AnyNode {
|
||||
fn from(node: ExcepthandlerExceptHandler) -> Self {
|
||||
AnyNode::ExcepthandlerExceptHandler(node)
|
||||
impl From<ExceptHandlerExceptHandler> for AnyNode {
|
||||
fn from(node: ExceptHandlerExceptHandler) -> Self {
|
||||
AnyNode::ExceptHandlerExceptHandler(node)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3363,6 +3399,11 @@ impl From<Arg> for AnyNode {
|
|||
AnyNode::Arg(node)
|
||||
}
|
||||
}
|
||||
impl From<ArgWithDefault> for AnyNode {
|
||||
fn from(node: ArgWithDefault) -> Self {
|
||||
AnyNode::ArgWithDefault(node)
|
||||
}
|
||||
}
|
||||
impl From<Keyword> for AnyNode {
|
||||
fn from(node: Keyword) -> Self {
|
||||
AnyNode::Keyword(node)
|
||||
|
@ -3373,9 +3414,9 @@ impl From<Alias> for AnyNode {
|
|||
AnyNode::Alias(node)
|
||||
}
|
||||
}
|
||||
impl From<Withitem> for AnyNode {
|
||||
fn from(node: Withitem) -> Self {
|
||||
AnyNode::Withitem(node)
|
||||
impl From<WithItem> for AnyNode {
|
||||
fn from(node: WithItem) -> Self {
|
||||
AnyNode::WithItem(node)
|
||||
}
|
||||
}
|
||||
impl From<MatchCase> for AnyNode {
|
||||
|
@ -3450,7 +3491,7 @@ impl Ranged for AnyNode {
|
|||
AnyNode::ExprList(node) => node.range(),
|
||||
AnyNode::ExprTuple(node) => node.range(),
|
||||
AnyNode::ExprSlice(node) => node.range(),
|
||||
AnyNode::ExcepthandlerExceptHandler(node) => node.range(),
|
||||
AnyNode::ExceptHandlerExceptHandler(node) => node.range(),
|
||||
AnyNode::PatternMatchValue(node) => node.range(),
|
||||
AnyNode::PatternMatchSingleton(node) => node.range(),
|
||||
AnyNode::PatternMatchSequence(node) => node.range(),
|
||||
|
@ -3463,9 +3504,10 @@ impl Ranged for AnyNode {
|
|||
AnyNode::Comprehension(node) => node.range(),
|
||||
AnyNode::Arguments(node) => node.range(),
|
||||
AnyNode::Arg(node) => node.range(),
|
||||
AnyNode::ArgWithDefault(node) => node.range(),
|
||||
AnyNode::Keyword(node) => node.range(),
|
||||
AnyNode::Alias(node) => node.range(),
|
||||
AnyNode::Withitem(node) => node.range(),
|
||||
AnyNode::WithItem(node) => node.range(),
|
||||
AnyNode::MatchCase(node) => node.range(),
|
||||
AnyNode::Decorator(node) => node.range(),
|
||||
}
|
||||
|
@ -3532,7 +3574,7 @@ pub enum AnyNodeRef<'a> {
|
|||
ExprList(&'a ExprList<TextRange>),
|
||||
ExprTuple(&'a ExprTuple<TextRange>),
|
||||
ExprSlice(&'a ExprSlice<TextRange>),
|
||||
ExcepthandlerExceptHandler(&'a ExcepthandlerExceptHandler<TextRange>),
|
||||
ExceptHandlerExceptHandler(&'a ExceptHandlerExceptHandler<TextRange>),
|
||||
PatternMatchValue(&'a PatternMatchValue<TextRange>),
|
||||
PatternMatchSingleton(&'a PatternMatchSingleton<TextRange>),
|
||||
PatternMatchSequence(&'a PatternMatchSequence<TextRange>),
|
||||
|
@ -3545,9 +3587,10 @@ pub enum AnyNodeRef<'a> {
|
|||
Comprehension(&'a Comprehension<TextRange>),
|
||||
Arguments(&'a Arguments<TextRange>),
|
||||
Arg(&'a Arg<TextRange>),
|
||||
ArgWithDefault(&'a ArgWithDefault<TextRange>),
|
||||
Keyword(&'a Keyword<TextRange>),
|
||||
Alias(&'a Alias<TextRange>),
|
||||
Withitem(&'a Withitem<TextRange>),
|
||||
WithItem(&'a WithItem<TextRange>),
|
||||
MatchCase(&'a MatchCase<TextRange>),
|
||||
Decorator(&'a Decorator<TextRange>),
|
||||
}
|
||||
|
@ -3613,7 +3656,7 @@ impl AnyNodeRef<'_> {
|
|||
AnyNodeRef::ExprList(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::ExprTuple(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::ExprSlice(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::ExcepthandlerExceptHandler(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::ExceptHandlerExceptHandler(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::PatternMatchValue(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::PatternMatchSingleton(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::PatternMatchSequence(node) => NonNull::from(*node).cast(),
|
||||
|
@ -3626,9 +3669,10 @@ impl AnyNodeRef<'_> {
|
|||
AnyNodeRef::Comprehension(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::Arguments(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::Arg(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::ArgWithDefault(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::Keyword(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::Alias(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::Withitem(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::WithItem(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::MatchCase(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::Decorator(node) => NonNull::from(*node).cast(),
|
||||
}
|
||||
|
@ -3700,7 +3744,7 @@ impl AnyNodeRef<'_> {
|
|||
AnyNodeRef::ExprList(_) => NodeKind::ExprList,
|
||||
AnyNodeRef::ExprTuple(_) => NodeKind::ExprTuple,
|
||||
AnyNodeRef::ExprSlice(_) => NodeKind::ExprSlice,
|
||||
AnyNodeRef::ExcepthandlerExceptHandler(_) => NodeKind::ExcepthandlerExceptHandler,
|
||||
AnyNodeRef::ExceptHandlerExceptHandler(_) => NodeKind::ExceptHandlerExceptHandler,
|
||||
AnyNodeRef::PatternMatchValue(_) => NodeKind::PatternMatchValue,
|
||||
AnyNodeRef::PatternMatchSingleton(_) => NodeKind::PatternMatchSingleton,
|
||||
AnyNodeRef::PatternMatchSequence(_) => NodeKind::PatternMatchSequence,
|
||||
|
@ -3713,9 +3757,10 @@ impl AnyNodeRef<'_> {
|
|||
AnyNodeRef::Comprehension(_) => NodeKind::Comprehension,
|
||||
AnyNodeRef::Arguments(_) => NodeKind::Arguments,
|
||||
AnyNodeRef::Arg(_) => NodeKind::Arg,
|
||||
AnyNodeRef::ArgWithDefault(_) => NodeKind::ArgWithDefault,
|
||||
AnyNodeRef::Keyword(_) => NodeKind::Keyword,
|
||||
AnyNodeRef::Alias(_) => NodeKind::Alias,
|
||||
AnyNodeRef::Withitem(_) => NodeKind::Withitem,
|
||||
AnyNodeRef::WithItem(_) => NodeKind::WithItem,
|
||||
AnyNodeRef::MatchCase(_) => NodeKind::MatchCase,
|
||||
AnyNodeRef::Decorator(_) => NodeKind::Decorator,
|
||||
}
|
||||
|
@ -3782,7 +3827,7 @@ impl AnyNodeRef<'_> {
|
|||
| AnyNodeRef::ExprList(_)
|
||||
| AnyNodeRef::ExprTuple(_)
|
||||
| AnyNodeRef::ExprSlice(_)
|
||||
| AnyNodeRef::ExcepthandlerExceptHandler(_)
|
||||
| AnyNodeRef::ExceptHandlerExceptHandler(_)
|
||||
| AnyNodeRef::PatternMatchValue(_)
|
||||
| AnyNodeRef::PatternMatchSingleton(_)
|
||||
| AnyNodeRef::PatternMatchSequence(_)
|
||||
|
@ -3795,9 +3840,10 @@ impl AnyNodeRef<'_> {
|
|||
| AnyNodeRef::Comprehension(_)
|
||||
| AnyNodeRef::Arguments(_)
|
||||
| AnyNodeRef::Arg(_)
|
||||
| AnyNodeRef::ArgWithDefault(_)
|
||||
| AnyNodeRef::Keyword(_)
|
||||
| AnyNodeRef::Alias(_)
|
||||
| AnyNodeRef::Withitem(_)
|
||||
| AnyNodeRef::WithItem(_)
|
||||
| AnyNodeRef::MatchCase(_)
|
||||
| AnyNodeRef::Decorator(_) => false,
|
||||
}
|
||||
|
@ -3864,7 +3910,7 @@ impl AnyNodeRef<'_> {
|
|||
| AnyNodeRef::StmtPass(_)
|
||||
| AnyNodeRef::StmtBreak(_)
|
||||
| AnyNodeRef::StmtContinue(_)
|
||||
| AnyNodeRef::ExcepthandlerExceptHandler(_)
|
||||
| AnyNodeRef::ExceptHandlerExceptHandler(_)
|
||||
| AnyNodeRef::PatternMatchValue(_)
|
||||
| AnyNodeRef::PatternMatchSingleton(_)
|
||||
| AnyNodeRef::PatternMatchSequence(_)
|
||||
|
@ -3877,9 +3923,10 @@ impl AnyNodeRef<'_> {
|
|||
| AnyNodeRef::Comprehension(_)
|
||||
| AnyNodeRef::Arguments(_)
|
||||
| AnyNodeRef::Arg(_)
|
||||
| AnyNodeRef::ArgWithDefault(_)
|
||||
| AnyNodeRef::Keyword(_)
|
||||
| AnyNodeRef::Alias(_)
|
||||
| AnyNodeRef::Withitem(_)
|
||||
| AnyNodeRef::WithItem(_)
|
||||
| AnyNodeRef::MatchCase(_)
|
||||
| AnyNodeRef::Decorator(_) => false,
|
||||
}
|
||||
|
@ -3946,7 +3993,7 @@ impl AnyNodeRef<'_> {
|
|||
| AnyNodeRef::ExprList(_)
|
||||
| AnyNodeRef::ExprTuple(_)
|
||||
| AnyNodeRef::ExprSlice(_)
|
||||
| AnyNodeRef::ExcepthandlerExceptHandler(_)
|
||||
| AnyNodeRef::ExceptHandlerExceptHandler(_)
|
||||
| AnyNodeRef::PatternMatchValue(_)
|
||||
| AnyNodeRef::PatternMatchSingleton(_)
|
||||
| AnyNodeRef::PatternMatchSequence(_)
|
||||
|
@ -3959,9 +4006,10 @@ impl AnyNodeRef<'_> {
|
|||
| AnyNodeRef::Comprehension(_)
|
||||
| AnyNodeRef::Arguments(_)
|
||||
| AnyNodeRef::Arg(_)
|
||||
| AnyNodeRef::ArgWithDefault(_)
|
||||
| AnyNodeRef::Keyword(_)
|
||||
| AnyNodeRef::Alias(_)
|
||||
| AnyNodeRef::Withitem(_)
|
||||
| AnyNodeRef::WithItem(_)
|
||||
| AnyNodeRef::MatchCase(_)
|
||||
| AnyNodeRef::Decorator(_) => false,
|
||||
}
|
||||
|
@ -4036,14 +4084,15 @@ impl AnyNodeRef<'_> {
|
|||
| AnyNodeRef::ExprList(_)
|
||||
| AnyNodeRef::ExprTuple(_)
|
||||
| AnyNodeRef::ExprSlice(_)
|
||||
| AnyNodeRef::ExcepthandlerExceptHandler(_)
|
||||
| AnyNodeRef::ExceptHandlerExceptHandler(_)
|
||||
| AnyNodeRef::TypeIgnoreTypeIgnore(_)
|
||||
| AnyNodeRef::Comprehension(_)
|
||||
| AnyNodeRef::Arguments(_)
|
||||
| AnyNodeRef::Arg(_)
|
||||
| AnyNodeRef::ArgWithDefault(_)
|
||||
| AnyNodeRef::Keyword(_)
|
||||
| AnyNodeRef::Alias(_)
|
||||
| AnyNodeRef::Withitem(_)
|
||||
| AnyNodeRef::WithItem(_)
|
||||
| AnyNodeRef::MatchCase(_)
|
||||
| AnyNodeRef::Decorator(_) => false,
|
||||
}
|
||||
|
@ -4051,7 +4100,7 @@ impl AnyNodeRef<'_> {
|
|||
|
||||
pub const fn is_except_handler(self) -> bool {
|
||||
match self {
|
||||
AnyNodeRef::ExcepthandlerExceptHandler(_) => true,
|
||||
AnyNodeRef::ExceptHandlerExceptHandler(_) => true,
|
||||
|
||||
AnyNodeRef::ModModule(_)
|
||||
| AnyNodeRef::ModInteractive(_)
|
||||
|
@ -4123,9 +4172,10 @@ impl AnyNodeRef<'_> {
|
|||
| AnyNodeRef::Comprehension(_)
|
||||
| AnyNodeRef::Arguments(_)
|
||||
| AnyNodeRef::Arg(_)
|
||||
| AnyNodeRef::ArgWithDefault(_)
|
||||
| AnyNodeRef::Keyword(_)
|
||||
| AnyNodeRef::Alias(_)
|
||||
| AnyNodeRef::Withitem(_)
|
||||
| AnyNodeRef::WithItem(_)
|
||||
| AnyNodeRef::MatchCase(_)
|
||||
| AnyNodeRef::Decorator(_) => false,
|
||||
}
|
||||
|
@ -4201,13 +4251,14 @@ impl AnyNodeRef<'_> {
|
|||
| AnyNodeRef::PatternMatchStar(_)
|
||||
| AnyNodeRef::PatternMatchAs(_)
|
||||
| AnyNodeRef::PatternMatchOr(_)
|
||||
| AnyNodeRef::ExcepthandlerExceptHandler(_)
|
||||
| AnyNodeRef::ExceptHandlerExceptHandler(_)
|
||||
| AnyNodeRef::Comprehension(_)
|
||||
| AnyNodeRef::Arguments(_)
|
||||
| AnyNodeRef::Arg(_)
|
||||
| AnyNodeRef::ArgWithDefault(_)
|
||||
| AnyNodeRef::Keyword(_)
|
||||
| AnyNodeRef::Alias(_)
|
||||
| AnyNodeRef::Withitem(_)
|
||||
| AnyNodeRef::WithItem(_)
|
||||
| AnyNodeRef::MatchCase(_)
|
||||
| AnyNodeRef::Decorator(_) => false,
|
||||
}
|
||||
|
@ -4578,9 +4629,9 @@ impl<'a> From<&'a ExprSlice> for AnyNodeRef<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a ExcepthandlerExceptHandler> for AnyNodeRef<'a> {
|
||||
fn from(node: &'a ExcepthandlerExceptHandler) -> Self {
|
||||
AnyNodeRef::ExcepthandlerExceptHandler(node)
|
||||
impl<'a> From<&'a ExceptHandlerExceptHandler> for AnyNodeRef<'a> {
|
||||
fn from(node: &'a ExceptHandlerExceptHandler) -> Self {
|
||||
AnyNodeRef::ExceptHandlerExceptHandler(node)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4738,11 +4789,11 @@ impl<'a> From<&'a Pattern> for AnyNodeRef<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a Excepthandler> for AnyNodeRef<'a> {
|
||||
fn from(handler: &'a Excepthandler) -> Self {
|
||||
impl<'a> From<&'a ExceptHandler> for AnyNodeRef<'a> {
|
||||
fn from(handler: &'a ExceptHandler) -> Self {
|
||||
match handler {
|
||||
Excepthandler::ExceptHandler(handler) => {
|
||||
AnyNodeRef::ExcepthandlerExceptHandler(handler)
|
||||
ExceptHandler::ExceptHandler(handler) => {
|
||||
AnyNodeRef::ExceptHandlerExceptHandler(handler)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4771,6 +4822,11 @@ impl<'a> From<&'a Arg> for AnyNodeRef<'a> {
|
|||
AnyNodeRef::Arg(node)
|
||||
}
|
||||
}
|
||||
impl<'a> From<&'a ArgWithDefault> for AnyNodeRef<'a> {
|
||||
fn from(node: &'a ArgWithDefault) -> Self {
|
||||
AnyNodeRef::ArgWithDefault(node)
|
||||
}
|
||||
}
|
||||
impl<'a> From<&'a Keyword> for AnyNodeRef<'a> {
|
||||
fn from(node: &'a Keyword) -> Self {
|
||||
AnyNodeRef::Keyword(node)
|
||||
|
@ -4781,9 +4837,9 @@ impl<'a> From<&'a Alias> for AnyNodeRef<'a> {
|
|||
AnyNodeRef::Alias(node)
|
||||
}
|
||||
}
|
||||
impl<'a> From<&'a Withitem> for AnyNodeRef<'a> {
|
||||
fn from(node: &'a Withitem) -> Self {
|
||||
AnyNodeRef::Withitem(node)
|
||||
impl<'a> From<&'a WithItem> for AnyNodeRef<'a> {
|
||||
fn from(node: &'a WithItem) -> Self {
|
||||
AnyNodeRef::WithItem(node)
|
||||
}
|
||||
}
|
||||
impl<'a> From<&'a MatchCase> for AnyNodeRef<'a> {
|
||||
|
@ -4853,7 +4909,7 @@ impl Ranged for AnyNodeRef<'_> {
|
|||
AnyNodeRef::ExprList(node) => node.range(),
|
||||
AnyNodeRef::ExprTuple(node) => node.range(),
|
||||
AnyNodeRef::ExprSlice(node) => node.range(),
|
||||
AnyNodeRef::ExcepthandlerExceptHandler(node) => node.range(),
|
||||
AnyNodeRef::ExceptHandlerExceptHandler(node) => node.range(),
|
||||
AnyNodeRef::PatternMatchValue(node) => node.range(),
|
||||
AnyNodeRef::PatternMatchSingleton(node) => node.range(),
|
||||
AnyNodeRef::PatternMatchSequence(node) => node.range(),
|
||||
|
@ -4866,9 +4922,10 @@ impl Ranged for AnyNodeRef<'_> {
|
|||
AnyNodeRef::Comprehension(node) => node.range(),
|
||||
AnyNodeRef::Arguments(node) => node.range(),
|
||||
AnyNodeRef::Arg(node) => node.range(),
|
||||
AnyNodeRef::ArgWithDefault(node) => node.range(),
|
||||
AnyNodeRef::Keyword(node) => node.range(),
|
||||
AnyNodeRef::Alias(node) => node.range(),
|
||||
AnyNodeRef::Withitem(node) => node.range(),
|
||||
AnyNodeRef::WithItem(node) => node.range(),
|
||||
AnyNodeRef::MatchCase(node) => node.range(),
|
||||
AnyNodeRef::Decorator(node) => node.range(),
|
||||
}
|
||||
|
@ -4935,7 +4992,7 @@ pub enum NodeKind {
|
|||
ExprList,
|
||||
ExprTuple,
|
||||
ExprSlice,
|
||||
ExcepthandlerExceptHandler,
|
||||
ExceptHandlerExceptHandler,
|
||||
PatternMatchValue,
|
||||
PatternMatchSingleton,
|
||||
PatternMatchSequence,
|
||||
|
@ -4948,9 +5005,10 @@ pub enum NodeKind {
|
|||
Comprehension,
|
||||
Arguments,
|
||||
Arg,
|
||||
ArgWithDefault,
|
||||
Keyword,
|
||||
Alias,
|
||||
Withitem,
|
||||
WithItem,
|
||||
MatchCase,
|
||||
Decorator,
|
||||
}
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
//! Generate Python source code from an abstract syntax tree (AST).
|
||||
|
||||
use rustpython_ast::ArgWithDefault;
|
||||
use std::ops::Deref;
|
||||
|
||||
use rustpython_literal::escape::{AsciiEscape, Escape, UnicodeEscape};
|
||||
use rustpython_parser::ast::{
|
||||
self, Alias, Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, ConversionFlag,
|
||||
Excepthandler, Expr, Identifier, MatchCase, Operator, Pattern, Stmt, Suite, Withitem,
|
||||
self, Alias, Arg, Arguments, BoolOp, CmpOp, Comprehension, Constant, ConversionFlag,
|
||||
ExceptHandler, Expr, Identifier, MatchCase, Operator, Pattern, Stmt, Suite, WithItem,
|
||||
};
|
||||
|
||||
use ruff_python_whitespace::LineEnding;
|
||||
|
@ -501,7 +502,7 @@ impl<'a> Generator<'a> {
|
|||
let mut first = true;
|
||||
for item in items {
|
||||
self.p_delim(&mut first, ", ");
|
||||
self.unparse_withitem(item);
|
||||
self.unparse_with_item(item);
|
||||
}
|
||||
self.p(":");
|
||||
});
|
||||
|
@ -513,7 +514,7 @@ impl<'a> Generator<'a> {
|
|||
let mut first = true;
|
||||
for item in items {
|
||||
self.p_delim(&mut first, ", ");
|
||||
self.unparse_withitem(item);
|
||||
self.unparse_with_item(item);
|
||||
}
|
||||
self.p(":");
|
||||
});
|
||||
|
@ -568,7 +569,7 @@ impl<'a> Generator<'a> {
|
|||
|
||||
for handler in handlers {
|
||||
statement!({
|
||||
self.unparse_excepthandler(handler, false);
|
||||
self.unparse_except_handler(handler, false);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -599,7 +600,7 @@ impl<'a> Generator<'a> {
|
|||
|
||||
for handler in handlers {
|
||||
statement!({
|
||||
self.unparse_excepthandler(handler, true);
|
||||
self.unparse_except_handler(handler, true);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -717,9 +718,9 @@ impl<'a> Generator<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
fn unparse_excepthandler<U>(&mut self, ast: &Excepthandler<U>, star: bool) {
|
||||
fn unparse_except_handler<U>(&mut self, ast: &ExceptHandler<U>, star: bool) {
|
||||
match ast {
|
||||
Excepthandler::ExceptHandler(ast::ExcepthandlerExceptHandler {
|
||||
ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler {
|
||||
type_,
|
||||
name,
|
||||
body,
|
||||
|
@ -870,7 +871,7 @@ impl<'a> Generator<'a> {
|
|||
values,
|
||||
range: _range,
|
||||
}) => {
|
||||
let (op, prec) = opprec!(bin, op, Boolop, And("and", AND), Or("or", OR));
|
||||
let (op, prec) = opprec!(bin, op, BoolOp, And("and", AND), Or("or", OR));
|
||||
group_if!(prec, {
|
||||
let mut first = true;
|
||||
for val in values {
|
||||
|
@ -929,7 +930,7 @@ impl<'a> Generator<'a> {
|
|||
let (op, prec) = opprec!(
|
||||
un,
|
||||
op,
|
||||
rustpython_parser::ast::Unaryop,
|
||||
rustpython_parser::ast::UnaryOp,
|
||||
Invert("~", INVERT),
|
||||
Not("not ", NOT),
|
||||
UAdd("+", UADD),
|
||||
|
@ -1087,16 +1088,16 @@ impl<'a> Generator<'a> {
|
|||
self.unparse_expr(left, new_lvl);
|
||||
for (op, cmp) in ops.iter().zip(comparators) {
|
||||
let op = match op {
|
||||
Cmpop::Eq => " == ",
|
||||
Cmpop::NotEq => " != ",
|
||||
Cmpop::Lt => " < ",
|
||||
Cmpop::LtE => " <= ",
|
||||
Cmpop::Gt => " > ",
|
||||
Cmpop::GtE => " >= ",
|
||||
Cmpop::Is => " is ",
|
||||
Cmpop::IsNot => " is not ",
|
||||
Cmpop::In => " in ",
|
||||
Cmpop::NotIn => " not in ",
|
||||
CmpOp::Eq => " == ",
|
||||
CmpOp::NotEq => " != ",
|
||||
CmpOp::Lt => " < ",
|
||||
CmpOp::LtE => " <= ",
|
||||
CmpOp::Gt => " > ",
|
||||
CmpOp::GtE => " >= ",
|
||||
CmpOp::Is => " is ",
|
||||
CmpOp::IsNot => " is not ",
|
||||
CmpOp::In => " in ",
|
||||
CmpOp::NotIn => " not in ",
|
||||
};
|
||||
self.p(op);
|
||||
self.unparse_expr(cmp, new_lvl);
|
||||
|
@ -1290,14 +1291,9 @@ impl<'a> Generator<'a> {
|
|||
|
||||
fn unparse_args<U>(&mut self, args: &Arguments<U>) {
|
||||
let mut first = true;
|
||||
let defaults_start = args.posonlyargs.len() + args.args.len() - args.defaults.len();
|
||||
for (i, arg) in args.posonlyargs.iter().chain(&args.args).enumerate() {
|
||||
for (i, arg_with_default) in args.posonlyargs.iter().chain(&args.args).enumerate() {
|
||||
self.p_delim(&mut first, ", ");
|
||||
self.unparse_arg(arg);
|
||||
if let Some(i) = i.checked_sub(defaults_start) {
|
||||
self.p("=");
|
||||
self.unparse_expr(&args.defaults[i], precedence::COMMA);
|
||||
}
|
||||
self.unparse_arg_with_default(arg_with_default);
|
||||
self.p_if(i + 1 == args.posonlyargs.len(), ", /");
|
||||
}
|
||||
if args.vararg.is_some() || !args.kwonlyargs.is_empty() {
|
||||
|
@ -1307,17 +1303,9 @@ impl<'a> Generator<'a> {
|
|||
if let Some(vararg) = &args.vararg {
|
||||
self.unparse_arg(vararg);
|
||||
}
|
||||
let defaults_start = args.kwonlyargs.len() - args.kw_defaults.len();
|
||||
for (i, kwarg) in args.kwonlyargs.iter().enumerate() {
|
||||
for kwarg in &args.kwonlyargs {
|
||||
self.p_delim(&mut first, ", ");
|
||||
self.unparse_arg(kwarg);
|
||||
if let Some(default) = i
|
||||
.checked_sub(defaults_start)
|
||||
.and_then(|i| args.kw_defaults.get(i))
|
||||
{
|
||||
self.p("=");
|
||||
self.unparse_expr(default, precedence::COMMA);
|
||||
}
|
||||
self.unparse_arg_with_default(kwarg);
|
||||
}
|
||||
if let Some(kwarg) = &args.kwarg {
|
||||
self.p_delim(&mut first, ", ");
|
||||
|
@ -1334,6 +1322,14 @@ impl<'a> Generator<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
fn unparse_arg_with_default<U>(&mut self, arg_with_default: &ArgWithDefault<U>) {
|
||||
self.unparse_arg(&arg_with_default.def);
|
||||
if let Some(default) = &arg_with_default.default {
|
||||
self.p("=");
|
||||
self.unparse_expr(default, precedence::COMMA);
|
||||
}
|
||||
}
|
||||
|
||||
fn unparse_comp<U>(&mut self, generators: &[Comprehension<U>]) {
|
||||
for comp in generators {
|
||||
self.p(if comp.is_async {
|
||||
|
@ -1445,9 +1441,9 @@ impl<'a> Generator<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
fn unparse_withitem<U>(&mut self, withitem: &Withitem<U>) {
|
||||
self.unparse_expr(&withitem.context_expr, precedence::MAX);
|
||||
if let Some(optional_vars) = &withitem.optional_vars {
|
||||
fn unparse_with_item<U>(&mut self, with_item: &WithItem<U>) {
|
||||
self.unparse_expr(&with_item.context_expr, precedence::MAX);
|
||||
if let Some(optional_vars) = &with_item.optional_vars {
|
||||
self.p(" as ");
|
||||
self.unparse_expr(optional_vars, precedence::MAX);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//! Specialized AST visitor trait and walk functions that only visit statements.
|
||||
|
||||
use rustpython_parser::ast::{self, Excepthandler, MatchCase, Stmt};
|
||||
use rustpython_parser::ast::{self, ExceptHandler, MatchCase, Stmt};
|
||||
|
||||
/// A trait for AST visitors that only need to visit statements.
|
||||
pub trait StatementVisitor<'a> {
|
||||
|
@ -10,8 +10,8 @@ pub trait StatementVisitor<'a> {
|
|||
fn visit_stmt(&mut self, stmt: &'a Stmt) {
|
||||
walk_stmt(self, stmt);
|
||||
}
|
||||
fn visit_excepthandler(&mut self, excepthandler: &'a Excepthandler) {
|
||||
walk_excepthandler(self, excepthandler);
|
||||
fn visit_except_handler(&mut self, except_handler: &'a ExceptHandler) {
|
||||
walk_except_handler(self, except_handler);
|
||||
}
|
||||
fn visit_match_case(&mut self, match_case: &'a MatchCase) {
|
||||
walk_match_case(self, match_case);
|
||||
|
@ -70,8 +70,8 @@ pub fn walk_stmt<'a, V: StatementVisitor<'a> + ?Sized>(visitor: &mut V, stmt: &'
|
|||
range: _range,
|
||||
}) => {
|
||||
visitor.visit_body(body);
|
||||
for excepthandler in handlers {
|
||||
visitor.visit_excepthandler(excepthandler);
|
||||
for except_handler in handlers {
|
||||
visitor.visit_except_handler(except_handler);
|
||||
}
|
||||
visitor.visit_body(orelse);
|
||||
visitor.visit_body(finalbody);
|
||||
|
@ -84,8 +84,8 @@ pub fn walk_stmt<'a, V: StatementVisitor<'a> + ?Sized>(visitor: &mut V, stmt: &'
|
|||
range: _range,
|
||||
}) => {
|
||||
visitor.visit_body(body);
|
||||
for excepthandler in handlers {
|
||||
visitor.visit_excepthandler(excepthandler);
|
||||
for except_handler in handlers {
|
||||
visitor.visit_except_handler(except_handler);
|
||||
}
|
||||
visitor.visit_body(orelse);
|
||||
visitor.visit_body(finalbody);
|
||||
|
@ -94,12 +94,12 @@ pub fn walk_stmt<'a, V: StatementVisitor<'a> + ?Sized>(visitor: &mut V, stmt: &'
|
|||
}
|
||||
}
|
||||
|
||||
pub fn walk_excepthandler<'a, V: StatementVisitor<'a> + ?Sized>(
|
||||
pub fn walk_except_handler<'a, V: StatementVisitor<'a> + ?Sized>(
|
||||
visitor: &mut V,
|
||||
excepthandler: &'a Excepthandler,
|
||||
except_handler: &'a ExceptHandler,
|
||||
) {
|
||||
match excepthandler {
|
||||
Excepthandler::ExceptHandler(ast::ExcepthandlerExceptHandler { body, .. }) => {
|
||||
match except_handler {
|
||||
ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler { body, .. }) => {
|
||||
visitor.visit_body(body);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,10 +2,10 @@
|
|||
|
||||
pub mod preorder;
|
||||
|
||||
use rustpython_ast::Decorator;
|
||||
use rustpython_ast::{ArgWithDefault, Decorator};
|
||||
use rustpython_parser::ast::{
|
||||
self, Alias, Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, Excepthandler, Expr,
|
||||
ExprContext, Keyword, MatchCase, Operator, Pattern, Stmt, Unaryop, Withitem,
|
||||
self, Alias, Arg, Arguments, BoolOp, CmpOp, Comprehension, Constant, ExceptHandler, Expr,
|
||||
ExprContext, Keyword, MatchCase, Operator, Pattern, Stmt, UnaryOp, WithItem,
|
||||
};
|
||||
|
||||
/// A trait for AST visitors. Visits all nodes in the AST recursively in evaluation-order.
|
||||
|
@ -34,23 +34,23 @@ pub trait Visitor<'a> {
|
|||
fn visit_expr_context(&mut self, expr_context: &'a ExprContext) {
|
||||
walk_expr_context(self, expr_context);
|
||||
}
|
||||
fn visit_boolop(&mut self, boolop: &'a Boolop) {
|
||||
walk_boolop(self, boolop);
|
||||
fn visit_bool_op(&mut self, bool_op: &'a BoolOp) {
|
||||
walk_bool_op(self, bool_op);
|
||||
}
|
||||
fn visit_operator(&mut self, operator: &'a Operator) {
|
||||
walk_operator(self, operator);
|
||||
}
|
||||
fn visit_unaryop(&mut self, unaryop: &'a Unaryop) {
|
||||
walk_unaryop(self, unaryop);
|
||||
fn visit_unary_op(&mut self, unary_op: &'a UnaryOp) {
|
||||
walk_unary_op(self, unary_op);
|
||||
}
|
||||
fn visit_cmpop(&mut self, cmpop: &'a Cmpop) {
|
||||
walk_cmpop(self, cmpop);
|
||||
fn visit_cmp_op(&mut self, cmp_op: &'a CmpOp) {
|
||||
walk_cmp_op(self, cmp_op);
|
||||
}
|
||||
fn visit_comprehension(&mut self, comprehension: &'a Comprehension) {
|
||||
walk_comprehension(self, comprehension);
|
||||
}
|
||||
fn visit_excepthandler(&mut self, excepthandler: &'a Excepthandler) {
|
||||
walk_excepthandler(self, excepthandler);
|
||||
fn visit_except_handler(&mut self, except_handler: &'a ExceptHandler) {
|
||||
walk_except_handler(self, except_handler);
|
||||
}
|
||||
fn visit_format_spec(&mut self, format_spec: &'a Expr) {
|
||||
walk_expr(self, format_spec);
|
||||
|
@ -61,14 +61,17 @@ pub trait Visitor<'a> {
|
|||
fn visit_arg(&mut self, arg: &'a Arg) {
|
||||
walk_arg(self, arg);
|
||||
}
|
||||
fn visit_arg_with_default(&mut self, arg_with_default: &'a ArgWithDefault) {
|
||||
walk_arg_with_default(self, arg_with_default);
|
||||
}
|
||||
fn visit_keyword(&mut self, keyword: &'a Keyword) {
|
||||
walk_keyword(self, keyword);
|
||||
}
|
||||
fn visit_alias(&mut self, alias: &'a Alias) {
|
||||
walk_alias(self, alias);
|
||||
}
|
||||
fn visit_withitem(&mut self, withitem: &'a Withitem) {
|
||||
walk_withitem(self, withitem);
|
||||
fn visit_with_item(&mut self, with_item: &'a WithItem) {
|
||||
walk_with_item(self, with_item);
|
||||
}
|
||||
fn visit_match_case(&mut self, match_case: &'a MatchCase) {
|
||||
walk_match_case(self, match_case);
|
||||
|
@ -228,14 +231,14 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
|
|||
visitor.visit_body(orelse);
|
||||
}
|
||||
Stmt::With(ast::StmtWith { items, body, .. }) => {
|
||||
for withitem in items {
|
||||
visitor.visit_withitem(withitem);
|
||||
for with_item in items {
|
||||
visitor.visit_with_item(with_item);
|
||||
}
|
||||
visitor.visit_body(body);
|
||||
}
|
||||
Stmt::AsyncWith(ast::StmtAsyncWith { items, body, .. }) => {
|
||||
for withitem in items {
|
||||
visitor.visit_withitem(withitem);
|
||||
for with_item in items {
|
||||
visitor.visit_with_item(with_item);
|
||||
}
|
||||
visitor.visit_body(body);
|
||||
}
|
||||
|
@ -269,8 +272,8 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
|
|||
range: _range,
|
||||
}) => {
|
||||
visitor.visit_body(body);
|
||||
for excepthandler in handlers {
|
||||
visitor.visit_excepthandler(excepthandler);
|
||||
for except_handler in handlers {
|
||||
visitor.visit_except_handler(except_handler);
|
||||
}
|
||||
visitor.visit_body(orelse);
|
||||
visitor.visit_body(finalbody);
|
||||
|
@ -283,8 +286,8 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
|
|||
range: _range,
|
||||
}) => {
|
||||
visitor.visit_body(body);
|
||||
for excepthandler in handlers {
|
||||
visitor.visit_excepthandler(excepthandler);
|
||||
for except_handler in handlers {
|
||||
visitor.visit_except_handler(except_handler);
|
||||
}
|
||||
visitor.visit_body(orelse);
|
||||
visitor.visit_body(finalbody);
|
||||
|
@ -333,7 +336,7 @@ pub fn walk_expr<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, expr: &'a Expr) {
|
|||
values,
|
||||
range: _range,
|
||||
}) => {
|
||||
visitor.visit_boolop(op);
|
||||
visitor.visit_bool_op(op);
|
||||
for expr in values {
|
||||
visitor.visit_expr(expr);
|
||||
}
|
||||
|
@ -361,7 +364,7 @@ pub fn walk_expr<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, expr: &'a Expr) {
|
|||
operand,
|
||||
range: _range,
|
||||
}) => {
|
||||
visitor.visit_unaryop(op);
|
||||
visitor.visit_unary_op(op);
|
||||
visitor.visit_expr(operand);
|
||||
}
|
||||
Expr::Lambda(ast::ExprLambda {
|
||||
|
@ -467,8 +470,8 @@ pub fn walk_expr<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, expr: &'a Expr) {
|
|||
range: _range,
|
||||
}) => {
|
||||
visitor.visit_expr(left);
|
||||
for cmpop in ops {
|
||||
visitor.visit_cmpop(cmpop);
|
||||
for cmp_op in ops {
|
||||
visitor.visit_cmp_op(cmp_op);
|
||||
}
|
||||
for expr in comparators {
|
||||
visitor.visit_expr(expr);
|
||||
|
@ -588,12 +591,12 @@ pub fn walk_comprehension<'a, V: Visitor<'a> + ?Sized>(
|
|||
}
|
||||
}
|
||||
|
||||
pub fn walk_excepthandler<'a, V: Visitor<'a> + ?Sized>(
|
||||
pub fn walk_except_handler<'a, V: Visitor<'a> + ?Sized>(
|
||||
visitor: &mut V,
|
||||
excepthandler: &'a Excepthandler,
|
||||
except_handler: &'a ExceptHandler,
|
||||
) {
|
||||
match excepthandler {
|
||||
Excepthandler::ExceptHandler(ast::ExcepthandlerExceptHandler { type_, body, .. }) => {
|
||||
match except_handler {
|
||||
ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler { type_, body, .. }) => {
|
||||
if let Some(expr) = type_ {
|
||||
visitor.visit_expr(expr);
|
||||
}
|
||||
|
@ -604,26 +607,20 @@ pub fn walk_excepthandler<'a, V: Visitor<'a> + ?Sized>(
|
|||
|
||||
pub fn walk_arguments<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, arguments: &'a Arguments) {
|
||||
for arg in &arguments.posonlyargs {
|
||||
visitor.visit_arg(arg);
|
||||
visitor.visit_arg_with_default(arg);
|
||||
}
|
||||
for arg in &arguments.args {
|
||||
visitor.visit_arg(arg);
|
||||
visitor.visit_arg_with_default(arg);
|
||||
}
|
||||
if let Some(arg) = &arguments.vararg {
|
||||
visitor.visit_arg(arg);
|
||||
}
|
||||
for arg in &arguments.kwonlyargs {
|
||||
visitor.visit_arg(arg);
|
||||
}
|
||||
for expr in &arguments.kw_defaults {
|
||||
visitor.visit_expr(expr);
|
||||
visitor.visit_arg_with_default(arg);
|
||||
}
|
||||
if let Some(arg) = &arguments.kwarg {
|
||||
visitor.visit_arg(arg);
|
||||
}
|
||||
for expr in &arguments.defaults {
|
||||
visitor.visit_expr(expr);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn walk_arg<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, arg: &'a Arg) {
|
||||
|
@ -632,13 +629,23 @@ pub fn walk_arg<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, arg: &'a Arg) {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn walk_arg_with_default<'a, V: Visitor<'a> + ?Sized>(
|
||||
visitor: &mut V,
|
||||
arg_with_default: &'a ArgWithDefault,
|
||||
) {
|
||||
visitor.visit_arg(&arg_with_default.def);
|
||||
if let Some(expr) = &arg_with_default.default {
|
||||
visitor.visit_expr(expr);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn walk_keyword<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, keyword: &'a Keyword) {
|
||||
visitor.visit_expr(&keyword.value);
|
||||
}
|
||||
|
||||
pub fn walk_withitem<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, withitem: &'a Withitem) {
|
||||
visitor.visit_expr(&withitem.context_expr);
|
||||
if let Some(expr) = &withitem.optional_vars {
|
||||
pub fn walk_with_item<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, with_item: &'a WithItem) {
|
||||
visitor.visit_expr(&with_item.context_expr);
|
||||
if let Some(expr) = &with_item.optional_vars {
|
||||
visitor.visit_expr(expr);
|
||||
}
|
||||
}
|
||||
|
@ -719,16 +726,16 @@ pub fn walk_expr_context<'a, V: Visitor<'a> + ?Sized>(
|
|||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
pub fn walk_boolop<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, boolop: &'a Boolop) {}
|
||||
pub fn walk_bool_op<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, bool_op: &'a BoolOp) {}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
pub fn walk_operator<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, operator: &'a Operator) {}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
pub fn walk_unaryop<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, unaryop: &'a Unaryop) {}
|
||||
pub fn walk_unary_op<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, unary_op: &'a UnaryOp) {}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
pub fn walk_cmpop<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, cmpop: &'a Cmpop) {}
|
||||
pub fn walk_cmp_op<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, cmp_op: &'a CmpOp) {}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
pub fn walk_alias<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, alias: &'a Alias) {}
|
||||
|
|
|
@ -26,28 +26,28 @@ pub trait PreorderVisitor<'a> {
|
|||
walk_constant(self, constant);
|
||||
}
|
||||
|
||||
fn visit_boolop(&mut self, boolop: &'a Boolop) {
|
||||
walk_boolop(self, boolop);
|
||||
fn visit_bool_op(&mut self, bool_op: &'a BoolOp) {
|
||||
walk_bool_op(self, bool_op);
|
||||
}
|
||||
|
||||
fn visit_operator(&mut self, operator: &'a Operator) {
|
||||
walk_operator(self, operator);
|
||||
}
|
||||
|
||||
fn visit_unaryop(&mut self, unaryop: &'a Unaryop) {
|
||||
walk_unaryop(self, unaryop);
|
||||
fn visit_unary_op(&mut self, unary_op: &'a UnaryOp) {
|
||||
walk_unary_op(self, unary_op);
|
||||
}
|
||||
|
||||
fn visit_cmpop(&mut self, cmpop: &'a Cmpop) {
|
||||
walk_cmpop(self, cmpop);
|
||||
fn visit_cmp_op(&mut self, cmp_op: &'a CmpOp) {
|
||||
walk_cmp_op(self, cmp_op);
|
||||
}
|
||||
|
||||
fn visit_comprehension(&mut self, comprehension: &'a Comprehension) {
|
||||
walk_comprehension(self, comprehension);
|
||||
}
|
||||
|
||||
fn visit_excepthandler(&mut self, excepthandler: &'a Excepthandler) {
|
||||
walk_excepthandler(self, excepthandler);
|
||||
fn visit_except_handler(&mut self, except_handler: &'a ExceptHandler) {
|
||||
walk_except_handler(self, except_handler);
|
||||
}
|
||||
|
||||
fn visit_format_spec(&mut self, format_spec: &'a Expr) {
|
||||
|
@ -62,6 +62,10 @@ pub trait PreorderVisitor<'a> {
|
|||
walk_arg(self, arg);
|
||||
}
|
||||
|
||||
fn visit_arg_with_default(&mut self, arg_with_default: &'a ArgWithDefault) {
|
||||
walk_arg_with_default(self, arg_with_default);
|
||||
}
|
||||
|
||||
fn visit_keyword(&mut self, keyword: &'a Keyword) {
|
||||
walk_keyword(self, keyword);
|
||||
}
|
||||
|
@ -70,8 +74,8 @@ pub trait PreorderVisitor<'a> {
|
|||
walk_alias(self, alias);
|
||||
}
|
||||
|
||||
fn visit_withitem(&mut self, withitem: &'a Withitem) {
|
||||
walk_withitem(self, withitem);
|
||||
fn visit_with_item(&mut self, with_item: &'a WithItem) {
|
||||
walk_with_item(self, with_item);
|
||||
}
|
||||
|
||||
fn visit_match_case(&mut self, match_case: &'a MatchCase) {
|
||||
|
@ -300,8 +304,8 @@ where
|
|||
type_comment: _,
|
||||
range: _,
|
||||
}) => {
|
||||
for withitem in items {
|
||||
visitor.visit_withitem(withitem);
|
||||
for with_item in items {
|
||||
visitor.visit_with_item(with_item);
|
||||
}
|
||||
visitor.visit_body(body);
|
||||
}
|
||||
|
@ -345,8 +349,8 @@ where
|
|||
range: _range,
|
||||
}) => {
|
||||
visitor.visit_body(body);
|
||||
for excepthandler in handlers {
|
||||
visitor.visit_excepthandler(excepthandler);
|
||||
for except_handler in handlers {
|
||||
visitor.visit_except_handler(except_handler);
|
||||
}
|
||||
visitor.visit_body(orelse);
|
||||
visitor.visit_body(finalbody);
|
||||
|
@ -410,13 +414,13 @@ where
|
|||
}) => match values.as_slice() {
|
||||
[left, rest @ ..] => {
|
||||
visitor.visit_expr(left);
|
||||
visitor.visit_boolop(op);
|
||||
visitor.visit_bool_op(op);
|
||||
for expr in rest {
|
||||
visitor.visit_expr(expr);
|
||||
}
|
||||
}
|
||||
[] => {
|
||||
visitor.visit_boolop(op);
|
||||
visitor.visit_bool_op(op);
|
||||
}
|
||||
},
|
||||
|
||||
|
@ -445,7 +449,7 @@ where
|
|||
operand,
|
||||
range: _range,
|
||||
}) => {
|
||||
visitor.visit_unaryop(op);
|
||||
visitor.visit_unary_op(op);
|
||||
visitor.visit_expr(operand);
|
||||
}
|
||||
|
||||
|
@ -565,7 +569,7 @@ where
|
|||
visitor.visit_expr(left);
|
||||
|
||||
for (op, comparator) in ops.iter().zip(comparators) {
|
||||
visitor.visit_cmpop(op);
|
||||
visitor.visit_cmp_op(op);
|
||||
visitor.visit_expr(comparator);
|
||||
}
|
||||
}
|
||||
|
@ -703,12 +707,12 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
pub fn walk_excepthandler<'a, V>(visitor: &mut V, excepthandler: &'a Excepthandler)
|
||||
pub fn walk_except_handler<'a, V>(visitor: &mut V, except_handler: &'a ExceptHandler)
|
||||
where
|
||||
V: PreorderVisitor<'a> + ?Sized,
|
||||
{
|
||||
match excepthandler {
|
||||
Excepthandler::ExceptHandler(ExcepthandlerExceptHandler {
|
||||
match except_handler {
|
||||
ExceptHandler::ExceptHandler(ExceptHandlerExceptHandler {
|
||||
range: _,
|
||||
type_,
|
||||
name: _,
|
||||
|
@ -726,34 +730,16 @@ pub fn walk_arguments<'a, V>(visitor: &mut V, arguments: &'a Arguments)
|
|||
where
|
||||
V: PreorderVisitor<'a> + ?Sized,
|
||||
{
|
||||
let non_default_args_len =
|
||||
arguments.posonlyargs.len() + arguments.args.len() - arguments.defaults.len();
|
||||
|
||||
let mut args_iter = arguments.posonlyargs.iter().chain(&arguments.args);
|
||||
|
||||
for _ in 0..non_default_args_len {
|
||||
visitor.visit_arg(args_iter.next().unwrap());
|
||||
}
|
||||
|
||||
for (arg, default) in args_iter.zip(&arguments.defaults) {
|
||||
visitor.visit_arg(arg);
|
||||
visitor.visit_expr(default);
|
||||
for arg in arguments.posonlyargs.iter().chain(&arguments.args) {
|
||||
visitor.visit_arg_with_default(arg);
|
||||
}
|
||||
|
||||
if let Some(arg) = &arguments.vararg {
|
||||
visitor.visit_arg(arg);
|
||||
}
|
||||
|
||||
let non_default_kwargs_len = arguments.kwonlyargs.len() - arguments.kw_defaults.len();
|
||||
let mut kwargsonly_iter = arguments.kwonlyargs.iter();
|
||||
|
||||
for _ in 0..non_default_kwargs_len {
|
||||
visitor.visit_arg(kwargsonly_iter.next().unwrap());
|
||||
}
|
||||
|
||||
for (arg, default) in kwargsonly_iter.zip(&arguments.kw_defaults) {
|
||||
visitor.visit_arg(arg);
|
||||
visitor.visit_expr(default);
|
||||
for arg in &arguments.kwonlyargs {
|
||||
visitor.visit_arg_with_default(arg);
|
||||
}
|
||||
|
||||
if let Some(arg) = &arguments.kwarg {
|
||||
|
@ -770,6 +756,16 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
pub fn walk_arg_with_default<'a, V>(visitor: &mut V, arg_with_default: &'a ArgWithDefault)
|
||||
where
|
||||
V: PreorderVisitor<'a> + ?Sized,
|
||||
{
|
||||
visitor.visit_arg(&arg_with_default.def);
|
||||
if let Some(expr) = &arg_with_default.default {
|
||||
visitor.visit_expr(expr);
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn walk_keyword<'a, V>(visitor: &mut V, keyword: &'a Keyword)
|
||||
where
|
||||
|
@ -778,13 +774,13 @@ where
|
|||
visitor.visit_expr(&keyword.value);
|
||||
}
|
||||
|
||||
pub fn walk_withitem<'a, V>(visitor: &mut V, withitem: &'a Withitem)
|
||||
pub fn walk_with_item<'a, V>(visitor: &mut V, with_item: &'a WithItem)
|
||||
where
|
||||
V: PreorderVisitor<'a> + ?Sized,
|
||||
{
|
||||
visitor.visit_expr(&withitem.context_expr);
|
||||
visitor.visit_expr(&with_item.context_expr);
|
||||
|
||||
if let Some(expr) = &withitem.optional_vars {
|
||||
if let Some(expr) = &with_item.optional_vars {
|
||||
visitor.visit_expr(expr);
|
||||
}
|
||||
}
|
||||
|
@ -885,7 +881,7 @@ where
|
|||
{
|
||||
}
|
||||
|
||||
pub fn walk_boolop<'a, V>(_visitor: &mut V, _boolop: &'a Boolop)
|
||||
pub fn walk_bool_op<'a, V>(_visitor: &mut V, _bool_op: &'a BoolOp)
|
||||
where
|
||||
V: PreorderVisitor<'a> + ?Sized,
|
||||
{
|
||||
|
@ -899,14 +895,14 @@ where
|
|||
}
|
||||
|
||||
#[inline]
|
||||
pub fn walk_unaryop<'a, V>(_visitor: &mut V, _unaryop: &'a Unaryop)
|
||||
pub fn walk_unary_op<'a, V>(_visitor: &mut V, _unary_op: &'a UnaryOp)
|
||||
where
|
||||
V: PreorderVisitor<'a> + ?Sized,
|
||||
{
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn walk_cmpop<'a, V>(_visitor: &mut V, _cmpop: &'a Cmpop)
|
||||
pub fn walk_cmp_op<'a, V>(_visitor: &mut V, _cmp_op: &'a CmpOp)
|
||||
where
|
||||
V: PreorderVisitor<'a> + ?Sized,
|
||||
{
|
||||
|
@ -923,11 +919,11 @@ where
|
|||
mod tests {
|
||||
use crate::node::AnyNodeRef;
|
||||
use crate::visitor::preorder::{
|
||||
walk_alias, walk_arg, walk_arguments, walk_comprehension, walk_excepthandler, walk_expr,
|
||||
walk_alias, walk_arg, walk_arguments, walk_comprehension, walk_except_handler, walk_expr,
|
||||
walk_keyword, walk_match_case, walk_module, walk_pattern, walk_stmt, walk_type_ignore,
|
||||
walk_withitem, Alias, Arg, Arguments, Boolop, Cmpop, Comprehension, Constant,
|
||||
Excepthandler, Expr, Keyword, MatchCase, Mod, Operator, Pattern, PreorderVisitor, Stmt,
|
||||
String, TypeIgnore, Unaryop, Withitem,
|
||||
walk_with_item, Alias, Arg, Arguments, BoolOp, CmpOp, Comprehension, Constant,
|
||||
ExceptHandler, Expr, Keyword, MatchCase, Mod, Operator, Pattern, PreorderVisitor, Stmt,
|
||||
String, TypeIgnore, UnaryOp, WithItem,
|
||||
};
|
||||
use insta::assert_snapshot;
|
||||
use rustpython_parser::lexer::lex;
|
||||
|
@ -1089,20 +1085,20 @@ class A:
|
|||
self.emit(&constant);
|
||||
}
|
||||
|
||||
fn visit_boolop(&mut self, boolop: &Boolop) {
|
||||
self.emit(&boolop);
|
||||
fn visit_bool_op(&mut self, bool_op: &BoolOp) {
|
||||
self.emit(&bool_op);
|
||||
}
|
||||
|
||||
fn visit_operator(&mut self, operator: &Operator) {
|
||||
self.emit(&operator);
|
||||
}
|
||||
|
||||
fn visit_unaryop(&mut self, unaryop: &Unaryop) {
|
||||
self.emit(&unaryop);
|
||||
fn visit_unary_op(&mut self, unary_op: &UnaryOp) {
|
||||
self.emit(&unary_op);
|
||||
}
|
||||
|
||||
fn visit_cmpop(&mut self, cmpop: &Cmpop) {
|
||||
self.emit(&cmpop);
|
||||
fn visit_cmp_op(&mut self, cmp_op: &CmpOp) {
|
||||
self.emit(&cmp_op);
|
||||
}
|
||||
|
||||
fn visit_comprehension(&mut self, comprehension: &Comprehension) {
|
||||
|
@ -1111,9 +1107,9 @@ class A:
|
|||
self.exit_node();
|
||||
}
|
||||
|
||||
fn visit_excepthandler(&mut self, excepthandler: &Excepthandler) {
|
||||
self.enter_node(excepthandler);
|
||||
walk_excepthandler(self, excepthandler);
|
||||
fn visit_except_handler(&mut self, except_handler: &ExceptHandler) {
|
||||
self.enter_node(except_handler);
|
||||
walk_except_handler(self, except_handler);
|
||||
self.exit_node();
|
||||
}
|
||||
|
||||
|
@ -1147,9 +1143,9 @@ class A:
|
|||
self.exit_node();
|
||||
}
|
||||
|
||||
fn visit_withitem(&mut self, withitem: &Withitem) {
|
||||
self.enter_node(withitem);
|
||||
walk_withitem(self, withitem);
|
||||
fn visit_with_item(&mut self, with_item: &WithItem) {
|
||||
self.enter_node(with_item);
|
||||
walk_with_item(self, with_item);
|
||||
self.exit_node();
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue