mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:38:25 +00:00
Re-integrate RustPython parser repository (#4359)
Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
parent
865205d992
commit
be6e00ef6e
270 changed files with 3061 additions and 3361 deletions
|
@ -8,7 +8,6 @@ rust-version = { workspace = true }
|
|||
[lib]
|
||||
|
||||
[dependencies]
|
||||
ruff_rustpython = { path = "../ruff_rustpython" }
|
||||
ruff_text_size = { workspace = true }
|
||||
|
||||
anyhow = { workspace = true }
|
||||
|
@ -22,7 +21,7 @@ num-traits = { version = "0.2.15" }
|
|||
once_cell = { workspace = true }
|
||||
regex = { workspace = true }
|
||||
rustc-hash = { workspace = true }
|
||||
rustpython-common = { workspace = true }
|
||||
rustpython-literal = { workspace = true }
|
||||
rustpython-parser = { workspace = true }
|
||||
serde = { workspace = true, optional = true }
|
||||
smallvec = { workspace = true }
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use bitflags::bitflags;
|
||||
use rustpython_parser::ast::{Constant, Expr, ExprKind, Stmt, StmtKind};
|
||||
use rustpython_parser::ast::{self, Constant, Expr, ExprKind, Stmt, StmtKind};
|
||||
|
||||
bitflags! {
|
||||
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
|
||||
|
@ -18,10 +18,10 @@ where
|
|||
{
|
||||
fn add_to_names<'a>(elts: &'a [Expr], names: &mut Vec<&'a str>, flags: &mut AllNamesFlags) {
|
||||
for elt in elts {
|
||||
if let ExprKind::Constant {
|
||||
if let ExprKind::Constant(ast::ExprConstant {
|
||||
value: Constant::Str(value),
|
||||
..
|
||||
} = &elt.node
|
||||
}) = &elt.node
|
||||
{
|
||||
names.push(value);
|
||||
} else {
|
||||
|
@ -35,39 +35,39 @@ where
|
|||
F: Fn(&str) -> bool,
|
||||
{
|
||||
match &expr.node {
|
||||
ExprKind::List { elts, .. } => {
|
||||
ExprKind::List(ast::ExprList { elts, .. }) => {
|
||||
return (Some(elts), AllNamesFlags::empty());
|
||||
}
|
||||
ExprKind::Tuple { elts, .. } => {
|
||||
ExprKind::Tuple(ast::ExprTuple { elts, .. }) => {
|
||||
return (Some(elts), AllNamesFlags::empty());
|
||||
}
|
||||
ExprKind::ListComp { .. } => {
|
||||
ExprKind::ListComp(_) => {
|
||||
// Allow comprehensions, even though we can't statically analyze them.
|
||||
return (None, AllNamesFlags::empty());
|
||||
}
|
||||
ExprKind::Call {
|
||||
ExprKind::Call(ast::ExprCall {
|
||||
func,
|
||||
args,
|
||||
keywords,
|
||||
..
|
||||
} => {
|
||||
}) => {
|
||||
// Allow `tuple()` and `list()` calls.
|
||||
if keywords.is_empty() && args.len() <= 1 {
|
||||
if let ExprKind::Name { id, .. } = &func.node {
|
||||
if let ExprKind::Name(ast::ExprName { id, .. }) = &func.node {
|
||||
let id = id.as_str();
|
||||
if id == "tuple" || id == "list" {
|
||||
if is_builtin(id) {
|
||||
if args.is_empty() {
|
||||
return (None, AllNamesFlags::empty());
|
||||
}
|
||||
match &args[0].node {
|
||||
ExprKind::List { elts, .. }
|
||||
| ExprKind::Set { elts, .. }
|
||||
| ExprKind::Tuple { elts, .. } => {
|
||||
ExprKind::List(ast::ExprList { elts, .. })
|
||||
| ExprKind::Set(ast::ExprSet { elts })
|
||||
| ExprKind::Tuple(ast::ExprTuple { elts, .. }) => {
|
||||
return (Some(elts), AllNamesFlags::empty());
|
||||
}
|
||||
ExprKind::ListComp { .. }
|
||||
| ExprKind::SetComp { .. }
|
||||
| ExprKind::GeneratorExp { .. } => {
|
||||
ExprKind::ListComp(_)
|
||||
| ExprKind::SetComp(_)
|
||||
| ExprKind::GeneratorExp(_) => {
|
||||
// Allow comprehensions, even though we can't statically analyze
|
||||
// them.
|
||||
return (None, AllNamesFlags::empty());
|
||||
|
@ -88,12 +88,12 @@ where
|
|||
let mut flags = AllNamesFlags::empty();
|
||||
|
||||
if let Some(value) = match &stmt.node {
|
||||
StmtKind::Assign { value, .. } => Some(value),
|
||||
StmtKind::AnnAssign { value, .. } => value.as_ref(),
|
||||
StmtKind::AugAssign { value, .. } => Some(value),
|
||||
StmtKind::Assign(ast::StmtAssign { value, .. }) => Some(value),
|
||||
StmtKind::AnnAssign(ast::StmtAnnAssign { value, .. }) => value.as_ref(),
|
||||
StmtKind::AugAssign(ast::StmtAugAssign { value, .. }) => Some(value),
|
||||
_ => None,
|
||||
} {
|
||||
if let ExprKind::BinOp { left, right, .. } = &value.node {
|
||||
if let ExprKind::BinOp(ast::ExprBinOp { left, right, .. }) = &value.node {
|
||||
let mut current_left = left;
|
||||
let mut current_right = right;
|
||||
loop {
|
||||
|
@ -106,7 +106,7 @@ where
|
|||
|
||||
// Process the left side, which can be a "real" value or the "rest" of the
|
||||
// binary operation.
|
||||
if let ExprKind::BinOp { left, right, .. } = ¤t_left.node {
|
||||
if let ExprKind::BinOp(ast::ExprBinOp { left, right, .. }) = ¤t_left.node {
|
||||
current_left = left;
|
||||
current_right = right;
|
||||
} else {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use rustpython_parser::ast::{Expr, ExprKind};
|
||||
use rustpython_parser::ast::{self, Expr, ExprKind};
|
||||
use smallvec::smallvec;
|
||||
|
||||
/// A representation of a qualified name, like `typing.List`.
|
||||
|
@ -6,16 +6,16 @@ pub type CallPath<'a> = smallvec::SmallVec<[&'a str; 8]>;
|
|||
|
||||
fn collect_call_path_inner<'a>(expr: &'a Expr, parts: &mut CallPath<'a>) -> bool {
|
||||
match &expr.node {
|
||||
ExprKind::Attribute { value, attr, .. } => {
|
||||
ExprKind::Attribute(ast::ExprAttribute { value, attr, .. }) => {
|
||||
if collect_call_path_inner(value, parts) {
|
||||
parts.push(attr);
|
||||
parts.push(attr.as_str());
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
ExprKind::Name { id, .. } => {
|
||||
parts.push(id);
|
||||
ExprKind::Name(ast::ExprName { id, .. }) => {
|
||||
parts.push(id.as_str());
|
||||
true
|
||||
}
|
||||
_ => false,
|
||||
|
|
|
@ -1,16 +1,19 @@
|
|||
use rustpython_parser::ast::{Expr, Stmt, StmtKind};
|
||||
use rustpython_parser::ast::{self, Expr, Stmt, StmtKind};
|
||||
|
||||
pub fn name(stmt: &Stmt) -> &str {
|
||||
match &stmt.node {
|
||||
StmtKind::FunctionDef { name, .. } | StmtKind::AsyncFunctionDef { name, .. } => name,
|
||||
StmtKind::FunctionDef(ast::StmtFunctionDef { name, .. })
|
||||
| StmtKind::AsyncFunctionDef(ast::StmtAsyncFunctionDef { name, .. }) => name.as_str(),
|
||||
_ => panic!("Expected StmtKind::FunctionDef | StmtKind::AsyncFunctionDef"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decorator_list(stmt: &Stmt) -> &Vec<Expr> {
|
||||
match &stmt.node {
|
||||
StmtKind::FunctionDef { decorator_list, .. }
|
||||
| StmtKind::AsyncFunctionDef { decorator_list, .. } => decorator_list,
|
||||
StmtKind::FunctionDef(ast::StmtFunctionDef { decorator_list, .. })
|
||||
| StmtKind::AsyncFunctionDef(ast::StmtAsyncFunctionDef { decorator_list, .. }) => {
|
||||
decorator_list
|
||||
}
|
||||
_ => panic!("Expected StmtKind::FunctionDef | StmtKind::AsyncFunctionDef"),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,9 +3,9 @@
|
|||
|
||||
use num_bigint::BigInt;
|
||||
use rustpython_parser::ast::{
|
||||
Alias, Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, Excepthandler,
|
||||
ExcepthandlerKind, Expr, ExprContext, ExprKind, Keyword, MatchCase, Operator, Pattern,
|
||||
PatternKind, Stmt, StmtKind, Unaryop, Withitem,
|
||||
self, Alias, Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, Excepthandler,
|
||||
ExcepthandlerKind, Expr, ExprContext, ExprKind, Identifier, Int, Keyword, MatchCase, Operator,
|
||||
Pattern, PatternKind, Stmt, StmtKind, Unaryop, Withitem,
|
||||
};
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
|
||||
|
@ -136,7 +136,7 @@ pub struct ComparableAlias<'a> {
|
|||
impl<'a> From<&'a Alias> for ComparableAlias<'a> {
|
||||
fn from(alias: &'a Alias) -> Self {
|
||||
Self {
|
||||
name: &alias.node.name,
|
||||
name: alias.node.name.as_str(),
|
||||
asname: alias.node.asname.as_deref(),
|
||||
}
|
||||
}
|
||||
|
@ -195,43 +195,47 @@ pub enum ComparablePattern<'a> {
|
|||
impl<'a> From<&'a Pattern> for ComparablePattern<'a> {
|
||||
fn from(pattern: &'a Pattern) -> Self {
|
||||
match &pattern.node {
|
||||
PatternKind::MatchValue { value } => Self::MatchValue {
|
||||
PatternKind::MatchValue(ast::PatternMatchValue { value }) => Self::MatchValue {
|
||||
value: value.into(),
|
||||
},
|
||||
PatternKind::MatchSingleton { value } => Self::MatchSingleton {
|
||||
value: value.into(),
|
||||
},
|
||||
PatternKind::MatchSequence { patterns } => Self::MatchSequence {
|
||||
patterns: patterns.iter().map(Into::into).collect(),
|
||||
},
|
||||
PatternKind::MatchMapping {
|
||||
PatternKind::MatchSingleton(ast::PatternMatchSingleton { value }) => {
|
||||
Self::MatchSingleton {
|
||||
value: value.into(),
|
||||
}
|
||||
}
|
||||
PatternKind::MatchSequence(ast::PatternMatchSequence { patterns }) => {
|
||||
Self::MatchSequence {
|
||||
patterns: patterns.iter().map(Into::into).collect(),
|
||||
}
|
||||
}
|
||||
PatternKind::MatchMapping(ast::PatternMatchMapping {
|
||||
keys,
|
||||
patterns,
|
||||
rest,
|
||||
} => Self::MatchMapping {
|
||||
}) => Self::MatchMapping {
|
||||
keys: keys.iter().map(Into::into).collect(),
|
||||
patterns: patterns.iter().map(Into::into).collect(),
|
||||
rest: rest.as_deref(),
|
||||
},
|
||||
PatternKind::MatchClass {
|
||||
PatternKind::MatchClass(ast::PatternMatchClass {
|
||||
cls,
|
||||
patterns,
|
||||
kwd_attrs,
|
||||
kwd_patterns,
|
||||
} => Self::MatchClass {
|
||||
}) => Self::MatchClass {
|
||||
cls: cls.into(),
|
||||
patterns: patterns.iter().map(Into::into).collect(),
|
||||
kwd_attrs: kwd_attrs.iter().map(String::as_str).collect(),
|
||||
kwd_attrs: kwd_attrs.iter().map(Identifier::as_str).collect(),
|
||||
kwd_patterns: kwd_patterns.iter().map(Into::into).collect(),
|
||||
},
|
||||
PatternKind::MatchStar { name } => Self::MatchStar {
|
||||
PatternKind::MatchStar(ast::PatternMatchStar { name }) => Self::MatchStar {
|
||||
name: name.as_deref(),
|
||||
},
|
||||
PatternKind::MatchAs { pattern, name } => Self::MatchAs {
|
||||
PatternKind::MatchAs(ast::PatternMatchAs { pattern, name }) => Self::MatchAs {
|
||||
pattern: pattern.as_ref().map(Into::into),
|
||||
name: name.as_deref(),
|
||||
},
|
||||
PatternKind::MatchOr { patterns } => Self::MatchOr {
|
||||
PatternKind::MatchOr(ast::PatternMatchOr { patterns }) => Self::MatchOr {
|
||||
patterns: patterns.iter().map(Into::into).collect(),
|
||||
},
|
||||
}
|
||||
|
@ -340,7 +344,7 @@ pub struct ComparableArg<'a> {
|
|||
impl<'a> From<&'a Arg> for ComparableArg<'a> {
|
||||
fn from(arg: &'a Arg) -> Self {
|
||||
Self {
|
||||
arg: &arg.node.arg,
|
||||
arg: arg.node.arg.as_str(),
|
||||
annotation: arg.node.annotation.as_ref().map(Into::into),
|
||||
type_comment: arg.node.type_comment.as_deref(),
|
||||
}
|
||||
|
@ -356,7 +360,7 @@ pub struct ComparableKeyword<'a> {
|
|||
impl<'a> From<&'a Keyword> for ComparableKeyword<'a> {
|
||||
fn from(keyword: &'a Keyword) -> Self {
|
||||
Self {
|
||||
arg: keyword.node.arg.as_deref(),
|
||||
arg: keyword.node.arg.as_ref().map(Identifier::as_str),
|
||||
value: (&keyword.node.value).into(),
|
||||
}
|
||||
}
|
||||
|
@ -367,7 +371,7 @@ pub struct ComparableComprehension<'a> {
|
|||
pub target: ComparableExpr<'a>,
|
||||
pub iter: ComparableExpr<'a>,
|
||||
pub ifs: Vec<ComparableExpr<'a>>,
|
||||
pub is_async: usize,
|
||||
pub is_async: bool,
|
||||
}
|
||||
|
||||
impl<'a> From<&'a Comprehension> for ComparableComprehension<'a> {
|
||||
|
@ -392,7 +396,8 @@ pub enum ComparableExcepthandler<'a> {
|
|||
|
||||
impl<'a> From<&'a Excepthandler> for ComparableExcepthandler<'a> {
|
||||
fn from(excepthandler: &'a Excepthandler) -> Self {
|
||||
let ExcepthandlerKind::ExceptHandler { type_, name, body } = &excepthandler.node;
|
||||
let ExcepthandlerKind::ExceptHandler(ast::ExcepthandlerExceptHandler { type_, name, body }) =
|
||||
&excepthandler.node;
|
||||
Self::ExceptHandler {
|
||||
type_: type_.as_ref().map(Into::into),
|
||||
name: name.as_deref(),
|
||||
|
@ -474,7 +479,7 @@ pub enum ComparableExpr<'a> {
|
|||
},
|
||||
FormattedValue {
|
||||
value: Box<ComparableExpr<'a>>,
|
||||
conversion: usize,
|
||||
conversion: Int,
|
||||
format_spec: Option<Box<ComparableExpr<'a>>>,
|
||||
},
|
||||
JoinedStr {
|
||||
|
@ -532,133 +537,135 @@ impl<'a> From<&'a Box<Expr>> for ComparableExpr<'a> {
|
|||
impl<'a> From<&'a Expr> for ComparableExpr<'a> {
|
||||
fn from(expr: &'a Expr) -> Self {
|
||||
match &expr.node {
|
||||
ExprKind::BoolOp { op, values } => Self::BoolOp {
|
||||
ExprKind::BoolOp(ast::ExprBoolOp { op, values }) => Self::BoolOp {
|
||||
op: op.into(),
|
||||
values: values.iter().map(Into::into).collect(),
|
||||
},
|
||||
ExprKind::NamedExpr { target, value } => Self::NamedExpr {
|
||||
ExprKind::NamedExpr(ast::ExprNamedExpr { target, value }) => Self::NamedExpr {
|
||||
target: target.into(),
|
||||
value: value.into(),
|
||||
},
|
||||
ExprKind::BinOp { left, op, right } => Self::BinOp {
|
||||
ExprKind::BinOp(ast::ExprBinOp { left, op, right }) => Self::BinOp {
|
||||
left: left.into(),
|
||||
op: op.into(),
|
||||
right: right.into(),
|
||||
},
|
||||
ExprKind::UnaryOp { op, operand } => Self::UnaryOp {
|
||||
ExprKind::UnaryOp(ast::ExprUnaryOp { op, operand }) => Self::UnaryOp {
|
||||
op: op.into(),
|
||||
operand: operand.into(),
|
||||
},
|
||||
ExprKind::Lambda { args, body } => Self::Lambda {
|
||||
ExprKind::Lambda(ast::ExprLambda { args, body }) => Self::Lambda {
|
||||
args: (&**args).into(),
|
||||
body: body.into(),
|
||||
},
|
||||
ExprKind::IfExp { test, body, orelse } => Self::IfExp {
|
||||
ExprKind::IfExp(ast::ExprIfExp { test, body, orelse }) => Self::IfExp {
|
||||
test: test.into(),
|
||||
body: body.into(),
|
||||
orelse: orelse.into(),
|
||||
},
|
||||
ExprKind::Dict { keys, values } => Self::Dict {
|
||||
ExprKind::Dict(ast::ExprDict { keys, values }) => Self::Dict {
|
||||
keys: keys
|
||||
.iter()
|
||||
.map(|expr| expr.as_ref().map(Into::into))
|
||||
.collect(),
|
||||
values: values.iter().map(Into::into).collect(),
|
||||
},
|
||||
ExprKind::Set { elts } => Self::Set {
|
||||
ExprKind::Set(ast::ExprSet { elts }) => Self::Set {
|
||||
elts: elts.iter().map(Into::into).collect(),
|
||||
},
|
||||
ExprKind::ListComp { elt, generators } => Self::ListComp {
|
||||
ExprKind::ListComp(ast::ExprListComp { elt, generators }) => Self::ListComp {
|
||||
elt: elt.into(),
|
||||
generators: generators.iter().map(Into::into).collect(),
|
||||
},
|
||||
ExprKind::SetComp { elt, generators } => Self::SetComp {
|
||||
ExprKind::SetComp(ast::ExprSetComp { elt, generators }) => Self::SetComp {
|
||||
elt: elt.into(),
|
||||
generators: generators.iter().map(Into::into).collect(),
|
||||
},
|
||||
ExprKind::DictComp {
|
||||
ExprKind::DictComp(ast::ExprDictComp {
|
||||
key,
|
||||
value,
|
||||
generators,
|
||||
} => Self::DictComp {
|
||||
}) => Self::DictComp {
|
||||
key: key.into(),
|
||||
value: value.into(),
|
||||
generators: generators.iter().map(Into::into).collect(),
|
||||
},
|
||||
ExprKind::GeneratorExp { elt, generators } => Self::GeneratorExp {
|
||||
elt: elt.into(),
|
||||
generators: generators.iter().map(Into::into).collect(),
|
||||
},
|
||||
ExprKind::Await { value } => Self::Await {
|
||||
ExprKind::GeneratorExp(ast::ExprGeneratorExp { elt, generators }) => {
|
||||
Self::GeneratorExp {
|
||||
elt: elt.into(),
|
||||
generators: generators.iter().map(Into::into).collect(),
|
||||
}
|
||||
}
|
||||
ExprKind::Await(ast::ExprAwait { value }) => Self::Await {
|
||||
value: value.into(),
|
||||
},
|
||||
ExprKind::Yield { value } => Self::Yield {
|
||||
ExprKind::Yield(ast::ExprYield { value }) => Self::Yield {
|
||||
value: value.as_ref().map(Into::into),
|
||||
},
|
||||
ExprKind::YieldFrom { value } => Self::YieldFrom {
|
||||
ExprKind::YieldFrom(ast::ExprYieldFrom { value }) => Self::YieldFrom {
|
||||
value: value.into(),
|
||||
},
|
||||
ExprKind::Compare {
|
||||
ExprKind::Compare(ast::ExprCompare {
|
||||
left,
|
||||
ops,
|
||||
comparators,
|
||||
} => Self::Compare {
|
||||
}) => Self::Compare {
|
||||
left: left.into(),
|
||||
ops: ops.iter().map(Into::into).collect(),
|
||||
comparators: comparators.iter().map(Into::into).collect(),
|
||||
},
|
||||
ExprKind::Call {
|
||||
ExprKind::Call(ast::ExprCall {
|
||||
func,
|
||||
args,
|
||||
keywords,
|
||||
} => Self::Call {
|
||||
}) => Self::Call {
|
||||
func: func.into(),
|
||||
args: args.iter().map(Into::into).collect(),
|
||||
keywords: keywords.iter().map(Into::into).collect(),
|
||||
},
|
||||
ExprKind::FormattedValue {
|
||||
ExprKind::FormattedValue(ast::ExprFormattedValue {
|
||||
value,
|
||||
conversion,
|
||||
format_spec,
|
||||
} => Self::FormattedValue {
|
||||
}) => Self::FormattedValue {
|
||||
value: value.into(),
|
||||
conversion: *conversion,
|
||||
format_spec: format_spec.as_ref().map(Into::into),
|
||||
},
|
||||
ExprKind::JoinedStr { values } => Self::JoinedStr {
|
||||
ExprKind::JoinedStr(ast::ExprJoinedStr { values }) => Self::JoinedStr {
|
||||
values: values.iter().map(Into::into).collect(),
|
||||
},
|
||||
ExprKind::Constant { value, kind } => Self::Constant {
|
||||
ExprKind::Constant(ast::ExprConstant { value, kind }) => Self::Constant {
|
||||
value: value.into(),
|
||||
kind: kind.as_ref().map(String::as_str),
|
||||
},
|
||||
ExprKind::Attribute { value, attr, ctx } => Self::Attribute {
|
||||
ExprKind::Attribute(ast::ExprAttribute { value, attr, ctx }) => Self::Attribute {
|
||||
value: value.into(),
|
||||
attr,
|
||||
attr: attr.as_str(),
|
||||
ctx: ctx.into(),
|
||||
},
|
||||
ExprKind::Subscript { value, slice, ctx } => Self::Subscript {
|
||||
ExprKind::Subscript(ast::ExprSubscript { value, slice, ctx }) => Self::Subscript {
|
||||
value: value.into(),
|
||||
slice: slice.into(),
|
||||
ctx: ctx.into(),
|
||||
},
|
||||
ExprKind::Starred { value, ctx } => Self::Starred {
|
||||
ExprKind::Starred(ast::ExprStarred { value, ctx }) => Self::Starred {
|
||||
value: value.into(),
|
||||
ctx: ctx.into(),
|
||||
},
|
||||
ExprKind::Name { id, ctx } => Self::Name {
|
||||
id,
|
||||
ExprKind::Name(ast::ExprName { id, ctx }) => Self::Name {
|
||||
id: id.as_str(),
|
||||
ctx: ctx.into(),
|
||||
},
|
||||
ExprKind::List { elts, ctx } => Self::List {
|
||||
ExprKind::List(ast::ExprList { elts, ctx }) => Self::List {
|
||||
elts: elts.iter().map(Into::into).collect(),
|
||||
ctx: ctx.into(),
|
||||
},
|
||||
ExprKind::Tuple { elts, ctx } => Self::Tuple {
|
||||
ExprKind::Tuple(ast::ExprTuple { elts, ctx }) => Self::Tuple {
|
||||
elts: elts.iter().map(Into::into).collect(),
|
||||
ctx: ctx.into(),
|
||||
},
|
||||
ExprKind::Slice { lower, upper, step } => Self::Slice {
|
||||
ExprKind::Slice(ast::ExprSlice { lower, upper, step }) => Self::Slice {
|
||||
lower: lower.as_ref().map(Into::into),
|
||||
upper: upper.as_ref().map(Into::into),
|
||||
step: step.as_ref().map(Into::into),
|
||||
|
@ -712,7 +719,7 @@ pub enum ComparableStmt<'a> {
|
|||
target: ComparableExpr<'a>,
|
||||
annotation: ComparableExpr<'a>,
|
||||
value: Option<ComparableExpr<'a>>,
|
||||
simple: usize,
|
||||
simple: bool,
|
||||
},
|
||||
For {
|
||||
target: ComparableExpr<'a>,
|
||||
|
@ -778,7 +785,7 @@ pub enum ComparableStmt<'a> {
|
|||
ImportFrom {
|
||||
module: Option<&'a str>,
|
||||
names: Vec<ComparableAlias<'a>>,
|
||||
level: Option<usize>,
|
||||
level: Option<Int>,
|
||||
},
|
||||
Global {
|
||||
names: Vec<&'a str>,
|
||||
|
@ -797,187 +804,187 @@ pub enum ComparableStmt<'a> {
|
|||
impl<'a> From<&'a Stmt> for ComparableStmt<'a> {
|
||||
fn from(stmt: &'a Stmt) -> Self {
|
||||
match &stmt.node {
|
||||
StmtKind::FunctionDef {
|
||||
StmtKind::FunctionDef(ast::StmtFunctionDef {
|
||||
name,
|
||||
args,
|
||||
body,
|
||||
decorator_list,
|
||||
returns,
|
||||
type_comment,
|
||||
} => Self::FunctionDef {
|
||||
name,
|
||||
}) => Self::FunctionDef {
|
||||
name: name.as_str(),
|
||||
args: args.into(),
|
||||
body: body.iter().map(Into::into).collect(),
|
||||
decorator_list: decorator_list.iter().map(Into::into).collect(),
|
||||
returns: returns.as_ref().map(Into::into),
|
||||
type_comment: type_comment.as_ref().map(std::string::String::as_str),
|
||||
},
|
||||
StmtKind::AsyncFunctionDef {
|
||||
StmtKind::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
||||
name,
|
||||
args,
|
||||
body,
|
||||
decorator_list,
|
||||
returns,
|
||||
type_comment,
|
||||
} => Self::AsyncFunctionDef {
|
||||
name,
|
||||
}) => Self::AsyncFunctionDef {
|
||||
name: name.as_str(),
|
||||
args: args.into(),
|
||||
body: body.iter().map(Into::into).collect(),
|
||||
decorator_list: decorator_list.iter().map(Into::into).collect(),
|
||||
returns: returns.as_ref().map(Into::into),
|
||||
type_comment: type_comment.as_ref().map(std::string::String::as_str),
|
||||
},
|
||||
StmtKind::ClassDef {
|
||||
StmtKind::ClassDef(ast::StmtClassDef {
|
||||
name,
|
||||
bases,
|
||||
keywords,
|
||||
body,
|
||||
decorator_list,
|
||||
} => Self::ClassDef {
|
||||
name,
|
||||
}) => Self::ClassDef {
|
||||
name: name.as_str(),
|
||||
bases: bases.iter().map(Into::into).collect(),
|
||||
keywords: keywords.iter().map(Into::into).collect(),
|
||||
body: body.iter().map(Into::into).collect(),
|
||||
decorator_list: decorator_list.iter().map(Into::into).collect(),
|
||||
},
|
||||
StmtKind::Return { value } => Self::Return {
|
||||
StmtKind::Return(ast::StmtReturn { value }) => Self::Return {
|
||||
value: value.as_ref().map(Into::into),
|
||||
},
|
||||
StmtKind::Delete { targets } => Self::Delete {
|
||||
StmtKind::Delete(ast::StmtDelete { targets }) => Self::Delete {
|
||||
targets: targets.iter().map(Into::into).collect(),
|
||||
},
|
||||
StmtKind::Assign {
|
||||
StmtKind::Assign(ast::StmtAssign {
|
||||
targets,
|
||||
value,
|
||||
type_comment,
|
||||
} => Self::Assign {
|
||||
}) => Self::Assign {
|
||||
targets: targets.iter().map(Into::into).collect(),
|
||||
value: value.into(),
|
||||
type_comment: type_comment.as_ref().map(std::string::String::as_str),
|
||||
},
|
||||
StmtKind::AugAssign { target, op, value } => Self::AugAssign {
|
||||
StmtKind::AugAssign(ast::StmtAugAssign { target, op, value }) => Self::AugAssign {
|
||||
target: target.into(),
|
||||
op: op.into(),
|
||||
value: value.into(),
|
||||
},
|
||||
StmtKind::AnnAssign {
|
||||
StmtKind::AnnAssign(ast::StmtAnnAssign {
|
||||
target,
|
||||
annotation,
|
||||
value,
|
||||
simple,
|
||||
} => Self::AnnAssign {
|
||||
}) => Self::AnnAssign {
|
||||
target: target.into(),
|
||||
annotation: annotation.into(),
|
||||
value: value.as_ref().map(Into::into),
|
||||
simple: *simple,
|
||||
},
|
||||
StmtKind::For {
|
||||
StmtKind::For(ast::StmtFor {
|
||||
target,
|
||||
iter,
|
||||
body,
|
||||
orelse,
|
||||
type_comment,
|
||||
} => Self::For {
|
||||
}) => Self::For {
|
||||
target: target.into(),
|
||||
iter: iter.into(),
|
||||
body: body.iter().map(Into::into).collect(),
|
||||
orelse: orelse.iter().map(Into::into).collect(),
|
||||
type_comment: type_comment.as_ref().map(String::as_str),
|
||||
},
|
||||
StmtKind::AsyncFor {
|
||||
StmtKind::AsyncFor(ast::StmtAsyncFor {
|
||||
target,
|
||||
iter,
|
||||
body,
|
||||
orelse,
|
||||
type_comment,
|
||||
} => Self::AsyncFor {
|
||||
}) => Self::AsyncFor {
|
||||
target: target.into(),
|
||||
iter: iter.into(),
|
||||
body: body.iter().map(Into::into).collect(),
|
||||
orelse: orelse.iter().map(Into::into).collect(),
|
||||
type_comment: type_comment.as_ref().map(String::as_str),
|
||||
},
|
||||
StmtKind::While { test, body, orelse } => Self::While {
|
||||
StmtKind::While(ast::StmtWhile { test, body, orelse }) => Self::While {
|
||||
test: test.into(),
|
||||
body: body.iter().map(Into::into).collect(),
|
||||
orelse: orelse.iter().map(Into::into).collect(),
|
||||
},
|
||||
StmtKind::If { test, body, orelse } => Self::If {
|
||||
StmtKind::If(ast::StmtIf { test, body, orelse }) => Self::If {
|
||||
test: test.into(),
|
||||
body: body.iter().map(Into::into).collect(),
|
||||
orelse: orelse.iter().map(Into::into).collect(),
|
||||
},
|
||||
StmtKind::With {
|
||||
StmtKind::With(ast::StmtWith {
|
||||
items,
|
||||
body,
|
||||
type_comment,
|
||||
} => Self::With {
|
||||
}) => Self::With {
|
||||
items: items.iter().map(Into::into).collect(),
|
||||
body: body.iter().map(Into::into).collect(),
|
||||
type_comment: type_comment.as_ref().map(String::as_str),
|
||||
},
|
||||
StmtKind::AsyncWith {
|
||||
StmtKind::AsyncWith(ast::StmtAsyncWith {
|
||||
items,
|
||||
body,
|
||||
type_comment,
|
||||
} => Self::AsyncWith {
|
||||
}) => Self::AsyncWith {
|
||||
items: items.iter().map(Into::into).collect(),
|
||||
body: body.iter().map(Into::into).collect(),
|
||||
type_comment: type_comment.as_ref().map(String::as_str),
|
||||
},
|
||||
StmtKind::Match { subject, cases } => Self::Match {
|
||||
StmtKind::Match(ast::StmtMatch { subject, cases }) => Self::Match {
|
||||
subject: subject.into(),
|
||||
cases: cases.iter().map(Into::into).collect(),
|
||||
},
|
||||
StmtKind::Raise { exc, cause } => Self::Raise {
|
||||
StmtKind::Raise(ast::StmtRaise { exc, cause }) => Self::Raise {
|
||||
exc: exc.as_ref().map(Into::into),
|
||||
cause: cause.as_ref().map(Into::into),
|
||||
},
|
||||
StmtKind::Try {
|
||||
StmtKind::Try(ast::StmtTry {
|
||||
body,
|
||||
handlers,
|
||||
orelse,
|
||||
finalbody,
|
||||
} => Self::Try {
|
||||
}) => Self::Try {
|
||||
body: body.iter().map(Into::into).collect(),
|
||||
handlers: handlers.iter().map(Into::into).collect(),
|
||||
orelse: orelse.iter().map(Into::into).collect(),
|
||||
finalbody: finalbody.iter().map(Into::into).collect(),
|
||||
},
|
||||
StmtKind::TryStar {
|
||||
StmtKind::TryStar(ast::StmtTryStar {
|
||||
body,
|
||||
handlers,
|
||||
orelse,
|
||||
finalbody,
|
||||
} => Self::TryStar {
|
||||
}) => Self::TryStar {
|
||||
body: body.iter().map(Into::into).collect(),
|
||||
handlers: handlers.iter().map(Into::into).collect(),
|
||||
orelse: orelse.iter().map(Into::into).collect(),
|
||||
finalbody: finalbody.iter().map(Into::into).collect(),
|
||||
},
|
||||
StmtKind::Assert { test, msg } => Self::Assert {
|
||||
StmtKind::Assert(ast::StmtAssert { test, msg }) => Self::Assert {
|
||||
test: test.into(),
|
||||
msg: msg.as_ref().map(Into::into),
|
||||
},
|
||||
StmtKind::Import { names } => Self::Import {
|
||||
StmtKind::Import(ast::StmtImport { names }) => Self::Import {
|
||||
names: names.iter().map(Into::into).collect(),
|
||||
},
|
||||
StmtKind::ImportFrom {
|
||||
StmtKind::ImportFrom(ast::StmtImportFrom {
|
||||
module,
|
||||
names,
|
||||
level,
|
||||
} => Self::ImportFrom {
|
||||
module: module.as_ref().map(String::as_str),
|
||||
}) => Self::ImportFrom {
|
||||
module: module.as_deref(),
|
||||
names: names.iter().map(Into::into).collect(),
|
||||
level: *level,
|
||||
},
|
||||
StmtKind::Global { names } => Self::Global {
|
||||
names: names.iter().map(String::as_str).collect(),
|
||||
StmtKind::Global(ast::StmtGlobal { names }) => Self::Global {
|
||||
names: names.iter().map(Identifier::as_str).collect(),
|
||||
},
|
||||
StmtKind::Nonlocal { names } => Self::Nonlocal {
|
||||
names: names.iter().map(String::as_str).collect(),
|
||||
StmtKind::Nonlocal(ast::StmtNonlocal { names }) => Self::Nonlocal {
|
||||
names: names.iter().map(Identifier::as_str).collect(),
|
||||
},
|
||||
StmtKind::Expr { value } => Self::Expr {
|
||||
StmtKind::Expr(ast::StmtExpr { value }) => Self::Expr {
|
||||
value: value.into(),
|
||||
},
|
||||
StmtKind::Pass => Self::Pass,
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -22,7 +22,7 @@ pub struct Import<'a> {
|
|||
pub struct ImportFrom<'a> {
|
||||
pub module: Option<&'a str>,
|
||||
pub name: Alias<'a>,
|
||||
pub level: Option<usize>,
|
||||
pub level: Option<u32>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
|
@ -65,7 +65,7 @@ impl std::fmt::Display for ImportFrom<'_> {
|
|||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(f, "from ")?;
|
||||
if let Some(level) = self.level {
|
||||
write!(f, "{}", ".".repeat(level))?;
|
||||
write!(f, "{}", ".".repeat(level as usize))?;
|
||||
}
|
||||
if let Some(module) = self.module {
|
||||
write!(f, "{module}")?;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use ruff_text_size::TextRange;
|
||||
use rustpython_parser::ast::{Expr, ExprKind, Keyword};
|
||||
use rustpython_parser::ast::{self, Expr, ExprKind, Keyword};
|
||||
|
||||
fn relocate_keyword(keyword: &mut Keyword, location: TextRange) {
|
||||
keyword.range = location;
|
||||
|
@ -11,31 +11,31 @@ fn relocate_keyword(keyword: &mut Keyword, location: TextRange) {
|
|||
pub fn relocate_expr(expr: &mut Expr, location: TextRange) {
|
||||
expr.range = location;
|
||||
match &mut expr.node {
|
||||
ExprKind::BoolOp { values, .. } => {
|
||||
ExprKind::BoolOp(ast::ExprBoolOp { values, .. }) => {
|
||||
for expr in values {
|
||||
relocate_expr(expr, location);
|
||||
}
|
||||
}
|
||||
ExprKind::NamedExpr { target, value } => {
|
||||
ExprKind::NamedExpr(ast::ExprNamedExpr { target, value }) => {
|
||||
relocate_expr(target, location);
|
||||
relocate_expr(value, location);
|
||||
}
|
||||
ExprKind::BinOp { left, right, .. } => {
|
||||
ExprKind::BinOp(ast::ExprBinOp { left, right, .. }) => {
|
||||
relocate_expr(left, location);
|
||||
relocate_expr(right, location);
|
||||
}
|
||||
ExprKind::UnaryOp { operand, .. } => {
|
||||
ExprKind::UnaryOp(ast::ExprUnaryOp { operand, .. }) => {
|
||||
relocate_expr(operand, location);
|
||||
}
|
||||
ExprKind::Lambda { body, .. } => {
|
||||
ExprKind::Lambda(ast::ExprLambda { body, .. }) => {
|
||||
relocate_expr(body, location);
|
||||
}
|
||||
ExprKind::IfExp { test, body, orelse } => {
|
||||
ExprKind::IfExp(ast::ExprIfExp { test, body, orelse }) => {
|
||||
relocate_expr(test, location);
|
||||
relocate_expr(body, location);
|
||||
relocate_expr(orelse, location);
|
||||
}
|
||||
ExprKind::Dict { keys, values } => {
|
||||
ExprKind::Dict(ast::ExprDict { keys, values }) => {
|
||||
for expr in keys.iter_mut().flatten() {
|
||||
relocate_expr(expr, location);
|
||||
}
|
||||
|
@ -43,44 +43,44 @@ pub fn relocate_expr(expr: &mut Expr, location: TextRange) {
|
|||
relocate_expr(expr, location);
|
||||
}
|
||||
}
|
||||
ExprKind::Set { elts } => {
|
||||
ExprKind::Set(ast::ExprSet { elts }) => {
|
||||
for expr in elts {
|
||||
relocate_expr(expr, location);
|
||||
}
|
||||
}
|
||||
ExprKind::ListComp { elt, .. } => {
|
||||
ExprKind::ListComp(ast::ExprListComp { elt, .. }) => {
|
||||
relocate_expr(elt, location);
|
||||
}
|
||||
ExprKind::SetComp { elt, .. } => {
|
||||
ExprKind::SetComp(ast::ExprSetComp { elt, .. }) => {
|
||||
relocate_expr(elt, location);
|
||||
}
|
||||
ExprKind::DictComp { key, value, .. } => {
|
||||
ExprKind::DictComp(ast::ExprDictComp { key, value, .. }) => {
|
||||
relocate_expr(key, location);
|
||||
relocate_expr(value, location);
|
||||
}
|
||||
ExprKind::GeneratorExp { elt, .. } => {
|
||||
ExprKind::GeneratorExp(ast::ExprGeneratorExp { elt, .. }) => {
|
||||
relocate_expr(elt, location);
|
||||
}
|
||||
ExprKind::Await { value } => relocate_expr(value, location),
|
||||
ExprKind::Yield { value } => {
|
||||
ExprKind::Await(ast::ExprAwait { value }) => relocate_expr(value, location),
|
||||
ExprKind::Yield(ast::ExprYield { value }) => {
|
||||
if let Some(expr) = value {
|
||||
relocate_expr(expr, location);
|
||||
}
|
||||
}
|
||||
ExprKind::YieldFrom { value } => relocate_expr(value, location),
|
||||
ExprKind::Compare {
|
||||
ExprKind::YieldFrom(ast::ExprYieldFrom { value }) => relocate_expr(value, location),
|
||||
ExprKind::Compare(ast::ExprCompare {
|
||||
left, comparators, ..
|
||||
} => {
|
||||
}) => {
|
||||
relocate_expr(left, location);
|
||||
for expr in comparators {
|
||||
relocate_expr(expr, location);
|
||||
}
|
||||
}
|
||||
ExprKind::Call {
|
||||
ExprKind::Call(ast::ExprCall {
|
||||
func,
|
||||
args,
|
||||
keywords,
|
||||
} => {
|
||||
}) => {
|
||||
relocate_expr(func, location);
|
||||
for expr in args {
|
||||
relocate_expr(expr, location);
|
||||
|
@ -89,42 +89,42 @@ pub fn relocate_expr(expr: &mut Expr, location: TextRange) {
|
|||
relocate_keyword(keyword, location);
|
||||
}
|
||||
}
|
||||
ExprKind::FormattedValue {
|
||||
ExprKind::FormattedValue(ast::ExprFormattedValue {
|
||||
value, format_spec, ..
|
||||
} => {
|
||||
}) => {
|
||||
relocate_expr(value, location);
|
||||
if let Some(expr) = format_spec {
|
||||
relocate_expr(expr, location);
|
||||
}
|
||||
}
|
||||
ExprKind::JoinedStr { values } => {
|
||||
ExprKind::JoinedStr(ast::ExprJoinedStr { values }) => {
|
||||
for expr in values {
|
||||
relocate_expr(expr, location);
|
||||
}
|
||||
}
|
||||
ExprKind::Constant { .. } => {}
|
||||
ExprKind::Attribute { value, .. } => {
|
||||
ExprKind::Constant(_) => {}
|
||||
ExprKind::Attribute(ast::ExprAttribute { value, .. }) => {
|
||||
relocate_expr(value, location);
|
||||
}
|
||||
ExprKind::Subscript { value, slice, .. } => {
|
||||
ExprKind::Subscript(ast::ExprSubscript { value, slice, .. }) => {
|
||||
relocate_expr(value, location);
|
||||
relocate_expr(slice, location);
|
||||
}
|
||||
ExprKind::Starred { value, .. } => {
|
||||
ExprKind::Starred(ast::ExprStarred { value, .. }) => {
|
||||
relocate_expr(value, location);
|
||||
}
|
||||
ExprKind::Name { .. } => {}
|
||||
ExprKind::List { elts, .. } => {
|
||||
ExprKind::Name(_) => {}
|
||||
ExprKind::List(ast::ExprList { elts, .. }) => {
|
||||
for expr in elts {
|
||||
relocate_expr(expr, location);
|
||||
}
|
||||
}
|
||||
ExprKind::Tuple { elts, .. } => {
|
||||
ExprKind::Tuple(ast::ExprTuple { elts, .. }) => {
|
||||
for expr in elts {
|
||||
relocate_expr(expr, location);
|
||||
}
|
||||
}
|
||||
ExprKind::Slice { lower, upper, step } => {
|
||||
ExprKind::Slice(ast::ExprSlice { lower, upper, step }) => {
|
||||
if let Some(expr) = lower {
|
||||
relocate_expr(expr, location);
|
||||
}
|
||||
|
|
|
@ -2,15 +2,14 @@
|
|||
|
||||
use std::ops::Deref;
|
||||
|
||||
use rustpython_literal::escape::{AsciiEscape, Escape, UnicodeEscape};
|
||||
use rustpython_parser::ast::{
|
||||
Alias, Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, Excepthandler,
|
||||
ExcepthandlerKind, Expr, ExprKind, MatchCase, Operator, Pattern, PatternKind, Stmt, StmtKind,
|
||||
Suite, Withitem,
|
||||
self, Alias, Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, ConversionFlag,
|
||||
Excepthandler, ExcepthandlerKind, Expr, ExprKind, Identifier, Int, MatchCase, Operator,
|
||||
Pattern, PatternKind, Stmt, StmtKind, Suite, Withitem,
|
||||
};
|
||||
use rustpython_parser::ConversionFlag;
|
||||
|
||||
use crate::newlines::LineEnding;
|
||||
use ruff_rustpython::vendor::{bytes, str};
|
||||
|
||||
use crate::source_code::stylist::{Indentation, Quote, Stylist};
|
||||
|
||||
|
@ -137,6 +136,26 @@ impl<'a> Generator<'a> {
|
|||
self.buffer += s;
|
||||
}
|
||||
|
||||
fn p_id(&mut self, s: &Identifier) {
|
||||
self.p(s.as_str());
|
||||
}
|
||||
|
||||
fn p_bytes_repr(&mut self, s: &[u8]) {
|
||||
let escape = AsciiEscape::with_preferred_quote(s, self.quote.into());
|
||||
if let Some(len) = escape.layout().len {
|
||||
self.buffer.reserve(len);
|
||||
}
|
||||
escape.bytes_repr().write(&mut self.buffer).unwrap(); // write to string doesn't fail
|
||||
}
|
||||
|
||||
fn p_str_repr(&mut self, s: &str) {
|
||||
let escape = UnicodeEscape::with_preferred_quote(s, self.quote.into());
|
||||
if let Some(len) = escape.layout().len {
|
||||
self.buffer.reserve(len);
|
||||
}
|
||||
escape.str_repr().write(&mut self.buffer).unwrap(); // write to string doesn't fail
|
||||
}
|
||||
|
||||
fn p_if(&mut self, cond: bool, s: &str) {
|
||||
if cond {
|
||||
self.p(s);
|
||||
|
@ -164,14 +183,14 @@ impl<'a> Generator<'a> {
|
|||
}
|
||||
|
||||
match &ast.node {
|
||||
StmtKind::FunctionDef {
|
||||
StmtKind::FunctionDef(ast::StmtFunctionDef {
|
||||
name,
|
||||
args,
|
||||
body,
|
||||
returns,
|
||||
decorator_list,
|
||||
..
|
||||
} => {
|
||||
}) => {
|
||||
self.newlines(if self.indent_depth == 0 { 2 } else { 1 });
|
||||
statement!({
|
||||
for decorator in decorator_list {
|
||||
|
@ -182,7 +201,7 @@ impl<'a> Generator<'a> {
|
|||
}
|
||||
self.newline();
|
||||
self.p("def ");
|
||||
self.p(name);
|
||||
self.p_id(name);
|
||||
self.p("(");
|
||||
self.unparse_args(args);
|
||||
self.p(")");
|
||||
|
@ -197,14 +216,14 @@ impl<'a> Generator<'a> {
|
|||
self.newlines(2);
|
||||
}
|
||||
}
|
||||
StmtKind::AsyncFunctionDef {
|
||||
StmtKind::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
||||
name,
|
||||
args,
|
||||
body,
|
||||
returns,
|
||||
decorator_list,
|
||||
..
|
||||
} => {
|
||||
}) => {
|
||||
self.newlines(if self.indent_depth == 0 { 2 } else { 1 });
|
||||
statement!({
|
||||
for decorator in decorator_list {
|
||||
|
@ -214,7 +233,7 @@ impl<'a> Generator<'a> {
|
|||
}
|
||||
self.newline();
|
||||
self.p("async def ");
|
||||
self.p(name);
|
||||
self.p_id(name);
|
||||
self.p("(");
|
||||
self.unparse_args(args);
|
||||
self.p(")");
|
||||
|
@ -229,14 +248,13 @@ impl<'a> Generator<'a> {
|
|||
self.newlines(2);
|
||||
}
|
||||
}
|
||||
StmtKind::ClassDef {
|
||||
StmtKind::ClassDef(ast::StmtClassDef {
|
||||
name,
|
||||
bases,
|
||||
keywords,
|
||||
body,
|
||||
decorator_list,
|
||||
..
|
||||
} => {
|
||||
}) => {
|
||||
self.newlines(if self.indent_depth == 0 { 2 } else { 1 });
|
||||
statement!({
|
||||
for decorator in decorator_list {
|
||||
|
@ -246,7 +264,7 @@ impl<'a> Generator<'a> {
|
|||
}
|
||||
self.newline();
|
||||
self.p("class ");
|
||||
self.p(name);
|
||||
self.p_id(name);
|
||||
let mut first = true;
|
||||
for base in bases {
|
||||
self.p_if(first, "(");
|
||||
|
@ -257,7 +275,7 @@ impl<'a> Generator<'a> {
|
|||
self.p_if(first, "(");
|
||||
self.p_delim(&mut first, ", ");
|
||||
if let Some(arg) = &keyword.node.arg {
|
||||
self.p(arg);
|
||||
self.p_id(arg);
|
||||
self.p("=");
|
||||
} else {
|
||||
self.p("**");
|
||||
|
@ -272,7 +290,7 @@ impl<'a> Generator<'a> {
|
|||
self.newlines(2);
|
||||
}
|
||||
}
|
||||
StmtKind::Return { value } => {
|
||||
StmtKind::Return(ast::StmtReturn { value }) => {
|
||||
statement!({
|
||||
if let Some(expr) = value {
|
||||
self.p("return ");
|
||||
|
@ -282,7 +300,7 @@ impl<'a> Generator<'a> {
|
|||
}
|
||||
});
|
||||
}
|
||||
StmtKind::Delete { targets } => {
|
||||
StmtKind::Delete(ast::StmtDelete { targets }) => {
|
||||
statement!({
|
||||
self.p("del ");
|
||||
let mut first = true;
|
||||
|
@ -292,7 +310,7 @@ impl<'a> Generator<'a> {
|
|||
}
|
||||
});
|
||||
}
|
||||
StmtKind::Assign { targets, value, .. } => {
|
||||
StmtKind::Assign(ast::StmtAssign { targets, value, .. }) => {
|
||||
statement!({
|
||||
for target in targets {
|
||||
self.unparse_expr(target, precedence::ASSIGN);
|
||||
|
@ -301,7 +319,7 @@ impl<'a> Generator<'a> {
|
|||
self.unparse_expr(value, precedence::ASSIGN);
|
||||
});
|
||||
}
|
||||
StmtKind::AugAssign { target, op, value } => {
|
||||
StmtKind::AugAssign(ast::StmtAugAssign { target, op, value }) => {
|
||||
statement!({
|
||||
self.unparse_expr(target, precedence::AUG_ASSIGN);
|
||||
self.p(" ");
|
||||
|
@ -324,14 +342,14 @@ impl<'a> Generator<'a> {
|
|||
self.unparse_expr(value, precedence::AUG_ASSIGN);
|
||||
});
|
||||
}
|
||||
StmtKind::AnnAssign {
|
||||
StmtKind::AnnAssign(ast::StmtAnnAssign {
|
||||
target,
|
||||
annotation,
|
||||
value,
|
||||
simple,
|
||||
} => {
|
||||
}) => {
|
||||
statement!({
|
||||
let need_parens = matches!(target.node, ExprKind::Name { .. }) && simple == &0;
|
||||
let need_parens = matches!(target.node, ExprKind::Name(_)) && !simple;
|
||||
self.p_if(need_parens, "(");
|
||||
self.unparse_expr(target, precedence::ANN_ASSIGN);
|
||||
self.p_if(need_parens, ")");
|
||||
|
@ -343,13 +361,13 @@ impl<'a> Generator<'a> {
|
|||
}
|
||||
});
|
||||
}
|
||||
StmtKind::For {
|
||||
StmtKind::For(ast::StmtFor {
|
||||
target,
|
||||
iter,
|
||||
body,
|
||||
orelse,
|
||||
..
|
||||
} => {
|
||||
}) => {
|
||||
statement!({
|
||||
self.p("for ");
|
||||
self.unparse_expr(target, precedence::FOR);
|
||||
|
@ -365,13 +383,13 @@ impl<'a> Generator<'a> {
|
|||
self.body(orelse);
|
||||
}
|
||||
}
|
||||
StmtKind::AsyncFor {
|
||||
StmtKind::AsyncFor(ast::StmtAsyncFor {
|
||||
target,
|
||||
iter,
|
||||
body,
|
||||
orelse,
|
||||
..
|
||||
} => {
|
||||
}) => {
|
||||
statement!({
|
||||
self.p("async for ");
|
||||
self.unparse_expr(target, precedence::ASYNC_FOR);
|
||||
|
@ -387,7 +405,7 @@ impl<'a> Generator<'a> {
|
|||
self.body(orelse);
|
||||
}
|
||||
}
|
||||
StmtKind::While { test, body, orelse } => {
|
||||
StmtKind::While(ast::StmtWhile { test, body, orelse }) => {
|
||||
statement!({
|
||||
self.p("while ");
|
||||
self.unparse_expr(test, precedence::WHILE);
|
||||
|
@ -401,7 +419,7 @@ impl<'a> Generator<'a> {
|
|||
self.body(orelse);
|
||||
}
|
||||
}
|
||||
StmtKind::If { test, body, orelse } => {
|
||||
StmtKind::If(ast::StmtIf { test, body, orelse }) => {
|
||||
statement!({
|
||||
self.p("if ");
|
||||
self.unparse_expr(test, precedence::IF);
|
||||
|
@ -411,8 +429,8 @@ impl<'a> Generator<'a> {
|
|||
|
||||
let mut orelse_: &Vec<Stmt<U>> = orelse;
|
||||
loop {
|
||||
if orelse_.len() == 1 && matches!(orelse_[0].node, StmtKind::If { .. }) {
|
||||
if let StmtKind::If { body, test, orelse } = &orelse_[0].node {
|
||||
if orelse_.len() == 1 && matches!(orelse_[0].node, StmtKind::If(_)) {
|
||||
if let StmtKind::If(ast::StmtIf { body, test, orelse }) = &orelse_[0].node {
|
||||
statement!({
|
||||
self.p("elif ");
|
||||
self.unparse_expr(test, precedence::IF);
|
||||
|
@ -432,7 +450,7 @@ impl<'a> Generator<'a> {
|
|||
}
|
||||
}
|
||||
}
|
||||
StmtKind::With { items, body, .. } => {
|
||||
StmtKind::With(ast::StmtWith { items, body, .. }) => {
|
||||
statement!({
|
||||
self.p("with ");
|
||||
let mut first = true;
|
||||
|
@ -444,7 +462,7 @@ impl<'a> Generator<'a> {
|
|||
});
|
||||
self.body(body);
|
||||
}
|
||||
StmtKind::AsyncWith { items, body, .. } => {
|
||||
StmtKind::AsyncWith(ast::StmtAsyncWith { items, body, .. }) => {
|
||||
statement!({
|
||||
self.p("async with ");
|
||||
let mut first = true;
|
||||
|
@ -456,7 +474,7 @@ impl<'a> Generator<'a> {
|
|||
});
|
||||
self.body(body);
|
||||
}
|
||||
StmtKind::Match { subject, cases } => {
|
||||
StmtKind::Match(ast::StmtMatch { subject, cases }) => {
|
||||
statement!({
|
||||
self.p("match ");
|
||||
self.unparse_expr(subject, precedence::MAX);
|
||||
|
@ -470,7 +488,7 @@ impl<'a> Generator<'a> {
|
|||
self.indent_depth -= 1;
|
||||
}
|
||||
}
|
||||
StmtKind::Raise { exc, cause } => {
|
||||
StmtKind::Raise(ast::StmtRaise { exc, cause }) => {
|
||||
statement!({
|
||||
self.p("raise");
|
||||
if let Some(exc) = exc {
|
||||
|
@ -483,12 +501,12 @@ impl<'a> Generator<'a> {
|
|||
}
|
||||
});
|
||||
}
|
||||
StmtKind::Try {
|
||||
StmtKind::Try(ast::StmtTry {
|
||||
body,
|
||||
handlers,
|
||||
orelse,
|
||||
finalbody,
|
||||
} => {
|
||||
}) => {
|
||||
statement!({
|
||||
self.p("try:");
|
||||
});
|
||||
|
@ -513,12 +531,12 @@ impl<'a> Generator<'a> {
|
|||
self.body(finalbody);
|
||||
}
|
||||
}
|
||||
StmtKind::TryStar {
|
||||
StmtKind::TryStar(ast::StmtTryStar {
|
||||
body,
|
||||
handlers,
|
||||
orelse,
|
||||
finalbody,
|
||||
} => {
|
||||
}) => {
|
||||
statement!({
|
||||
self.p("try:");
|
||||
});
|
||||
|
@ -543,7 +561,7 @@ impl<'a> Generator<'a> {
|
|||
self.body(finalbody);
|
||||
}
|
||||
}
|
||||
StmtKind::Assert { test, msg } => {
|
||||
StmtKind::Assert(ast::StmtAssert { test, msg }) => {
|
||||
statement!({
|
||||
self.p("assert ");
|
||||
self.unparse_expr(test, precedence::ASSERT);
|
||||
|
@ -553,7 +571,7 @@ impl<'a> Generator<'a> {
|
|||
}
|
||||
});
|
||||
}
|
||||
StmtKind::Import { names } => {
|
||||
StmtKind::Import(ast::StmtImport { names }) => {
|
||||
statement!({
|
||||
self.p("import ");
|
||||
let mut first = true;
|
||||
|
@ -563,18 +581,18 @@ impl<'a> Generator<'a> {
|
|||
}
|
||||
});
|
||||
}
|
||||
StmtKind::ImportFrom {
|
||||
StmtKind::ImportFrom(ast::StmtImportFrom {
|
||||
module,
|
||||
names,
|
||||
level,
|
||||
} => {
|
||||
}) => {
|
||||
statement!({
|
||||
self.p("from ");
|
||||
if let Some(level) = level {
|
||||
self.p(&".".repeat(*level));
|
||||
self.p(&".".repeat(level.to_usize()));
|
||||
}
|
||||
if let Some(module) = module {
|
||||
self.p(module);
|
||||
self.p_id(module);
|
||||
}
|
||||
self.p(" import ");
|
||||
let mut first = true;
|
||||
|
@ -584,27 +602,27 @@ impl<'a> Generator<'a> {
|
|||
}
|
||||
});
|
||||
}
|
||||
StmtKind::Global { names } => {
|
||||
StmtKind::Global(ast::StmtGlobal { names }) => {
|
||||
statement!({
|
||||
self.p("global ");
|
||||
let mut first = true;
|
||||
for name in names {
|
||||
self.p_delim(&mut first, ", ");
|
||||
self.p(name);
|
||||
self.p_id(name);
|
||||
}
|
||||
});
|
||||
}
|
||||
StmtKind::Nonlocal { names } => {
|
||||
StmtKind::Nonlocal(ast::StmtNonlocal { names }) => {
|
||||
statement!({
|
||||
self.p("nonlocal ");
|
||||
let mut first = true;
|
||||
for name in names {
|
||||
self.p_delim(&mut first, ", ");
|
||||
self.p(name);
|
||||
self.p_id(name);
|
||||
}
|
||||
});
|
||||
}
|
||||
StmtKind::Expr { value } => {
|
||||
StmtKind::Expr(ast::StmtExpr { value }) => {
|
||||
statement!({
|
||||
self.unparse_expr(value, precedence::EXPR);
|
||||
});
|
||||
|
@ -629,7 +647,11 @@ impl<'a> Generator<'a> {
|
|||
|
||||
fn unparse_excepthandler<U>(&mut self, ast: &Excepthandler<U>, star: bool) {
|
||||
match &ast.node {
|
||||
ExcepthandlerKind::ExceptHandler { type_, name, body } => {
|
||||
ExcepthandlerKind::ExceptHandler(ast::ExcepthandlerExceptHandler {
|
||||
type_,
|
||||
name,
|
||||
body,
|
||||
}) => {
|
||||
self.p("except");
|
||||
if star {
|
||||
self.p("*");
|
||||
|
@ -640,7 +662,7 @@ impl<'a> Generator<'a> {
|
|||
}
|
||||
if let Some(name) = name {
|
||||
self.p(" as ");
|
||||
self.p(name);
|
||||
self.p_id(name);
|
||||
}
|
||||
self.p(":");
|
||||
self.body(body);
|
||||
|
@ -650,13 +672,13 @@ impl<'a> Generator<'a> {
|
|||
|
||||
fn unparse_pattern<U>(&mut self, ast: &Pattern<U>) {
|
||||
match &ast.node {
|
||||
PatternKind::MatchValue { value } => {
|
||||
PatternKind::MatchValue(ast::PatternMatchValue { value }) => {
|
||||
self.unparse_expr(value, precedence::MAX);
|
||||
}
|
||||
PatternKind::MatchSingleton { value } => {
|
||||
PatternKind::MatchSingleton(ast::PatternMatchSingleton { value }) => {
|
||||
self.unparse_constant(value);
|
||||
}
|
||||
PatternKind::MatchSequence { patterns } => {
|
||||
PatternKind::MatchSequence(ast::PatternMatchSequence { patterns }) => {
|
||||
self.p("[");
|
||||
let mut first = true;
|
||||
for pattern in patterns {
|
||||
|
@ -665,11 +687,11 @@ impl<'a> Generator<'a> {
|
|||
}
|
||||
self.p("]");
|
||||
}
|
||||
PatternKind::MatchMapping {
|
||||
PatternKind::MatchMapping(ast::PatternMatchMapping {
|
||||
keys,
|
||||
patterns,
|
||||
rest,
|
||||
} => {
|
||||
}) => {
|
||||
self.p("{");
|
||||
let mut first = true;
|
||||
for (key, pattern) in keys.iter().zip(patterns) {
|
||||
|
@ -681,31 +703,31 @@ impl<'a> Generator<'a> {
|
|||
if let Some(rest) = rest {
|
||||
self.p_delim(&mut first, ", ");
|
||||
self.p("**");
|
||||
self.p(rest);
|
||||
self.p_id(rest);
|
||||
}
|
||||
self.p("}");
|
||||
}
|
||||
PatternKind::MatchClass { .. } => {}
|
||||
PatternKind::MatchStar { name } => {
|
||||
PatternKind::MatchClass(_) => {}
|
||||
PatternKind::MatchStar(ast::PatternMatchStar { name }) => {
|
||||
self.p("*");
|
||||
if let Some(name) = name {
|
||||
self.p(name);
|
||||
self.p_id(name);
|
||||
} else {
|
||||
self.p("_");
|
||||
}
|
||||
}
|
||||
PatternKind::MatchAs { pattern, name } => {
|
||||
PatternKind::MatchAs(ast::PatternMatchAs { pattern, name }) => {
|
||||
if let Some(pattern) = pattern {
|
||||
self.unparse_pattern(pattern);
|
||||
self.p(" as ");
|
||||
}
|
||||
if let Some(name) = name {
|
||||
self.p(name);
|
||||
self.p_id(name);
|
||||
} else {
|
||||
self.p("_");
|
||||
}
|
||||
}
|
||||
PatternKind::MatchOr { patterns } => {
|
||||
PatternKind::MatchOr(ast::PatternMatchOr { patterns }) => {
|
||||
let mut first = true;
|
||||
for pattern in patterns {
|
||||
self.p_delim(&mut first, " | ");
|
||||
|
@ -750,7 +772,7 @@ impl<'a> Generator<'a> {
|
|||
}};
|
||||
}
|
||||
match &ast.node {
|
||||
ExprKind::BoolOp { op, values } => {
|
||||
ExprKind::BoolOp(ast::ExprBoolOp { op, values }) => {
|
||||
let (op, prec) = opprec!(bin, op, Boolop, And("and", AND), Or("or", OR));
|
||||
group_if!(prec, {
|
||||
let mut first = true;
|
||||
|
@ -760,14 +782,14 @@ impl<'a> Generator<'a> {
|
|||
}
|
||||
});
|
||||
}
|
||||
ExprKind::NamedExpr { target, value } => {
|
||||
ExprKind::NamedExpr(ast::ExprNamedExpr { target, value }) => {
|
||||
group_if!(precedence::NAMED_EXPR, {
|
||||
self.unparse_expr(target, precedence::NAMED_EXPR);
|
||||
self.p(" := ");
|
||||
self.unparse_expr(value, precedence::NAMED_EXPR + 1);
|
||||
});
|
||||
}
|
||||
ExprKind::BinOp { left, op, right } => {
|
||||
ExprKind::BinOp(ast::ExprBinOp { left, op, right }) => {
|
||||
let rassoc = matches!(op, Operator::Pow);
|
||||
let (op, prec) = opprec!(
|
||||
bin,
|
||||
|
@ -793,7 +815,7 @@ impl<'a> Generator<'a> {
|
|||
self.unparse_expr(right, prec + u8::from(!rassoc));
|
||||
});
|
||||
}
|
||||
ExprKind::UnaryOp { op, operand } => {
|
||||
ExprKind::UnaryOp(ast::ExprUnaryOp { op, operand }) => {
|
||||
let (op, prec) = opprec!(
|
||||
un,
|
||||
op,
|
||||
|
@ -808,7 +830,7 @@ impl<'a> Generator<'a> {
|
|||
self.unparse_expr(operand, prec);
|
||||
});
|
||||
}
|
||||
ExprKind::Lambda { args, body } => {
|
||||
ExprKind::Lambda(ast::ExprLambda { args, body }) => {
|
||||
group_if!(precedence::LAMBDA, {
|
||||
let npos = args.args.len() + args.posonlyargs.len();
|
||||
self.p(if npos > 0 { "lambda " } else { "lambda" });
|
||||
|
@ -817,7 +839,7 @@ impl<'a> Generator<'a> {
|
|||
self.unparse_expr(body, precedence::LAMBDA);
|
||||
});
|
||||
}
|
||||
ExprKind::IfExp { test, body, orelse } => {
|
||||
ExprKind::IfExp(ast::ExprIfExp { test, body, orelse }) => {
|
||||
group_if!(precedence::IF_EXP, {
|
||||
self.unparse_expr(body, precedence::IF_EXP + 1);
|
||||
self.p(" if ");
|
||||
|
@ -826,7 +848,7 @@ impl<'a> Generator<'a> {
|
|||
self.unparse_expr(orelse, precedence::IF_EXP);
|
||||
});
|
||||
}
|
||||
ExprKind::Dict { keys, values } => {
|
||||
ExprKind::Dict(ast::ExprDict { keys, values }) => {
|
||||
self.p("{");
|
||||
let mut first = true;
|
||||
for (k, v) in keys.iter().zip(values) {
|
||||
|
@ -842,7 +864,7 @@ impl<'a> Generator<'a> {
|
|||
}
|
||||
self.p("}");
|
||||
}
|
||||
ExprKind::Set { elts } => {
|
||||
ExprKind::Set(ast::ExprSet { elts }) => {
|
||||
if elts.is_empty() {
|
||||
self.p("set()");
|
||||
} else {
|
||||
|
@ -855,23 +877,23 @@ impl<'a> Generator<'a> {
|
|||
self.p("}");
|
||||
}
|
||||
}
|
||||
ExprKind::ListComp { elt, generators } => {
|
||||
ExprKind::ListComp(ast::ExprListComp { elt, generators }) => {
|
||||
self.p("[");
|
||||
self.unparse_expr(elt, precedence::MAX);
|
||||
self.unparse_comp(generators);
|
||||
self.p("]");
|
||||
}
|
||||
ExprKind::SetComp { elt, generators } => {
|
||||
ExprKind::SetComp(ast::ExprSetComp { elt, generators }) => {
|
||||
self.p("{");
|
||||
self.unparse_expr(elt, precedence::MAX);
|
||||
self.unparse_comp(generators);
|
||||
self.p("}");
|
||||
}
|
||||
ExprKind::DictComp {
|
||||
ExprKind::DictComp(ast::ExprDictComp {
|
||||
key,
|
||||
value,
|
||||
generators,
|
||||
} => {
|
||||
}) => {
|
||||
self.p("{");
|
||||
self.unparse_expr(key, precedence::MAX);
|
||||
self.p(": ");
|
||||
|
@ -879,19 +901,19 @@ impl<'a> Generator<'a> {
|
|||
self.unparse_comp(generators);
|
||||
self.p("}");
|
||||
}
|
||||
ExprKind::GeneratorExp { elt, generators } => {
|
||||
ExprKind::GeneratorExp(ast::ExprGeneratorExp { elt, generators }) => {
|
||||
self.p("(");
|
||||
self.unparse_expr(elt, precedence::COMMA);
|
||||
self.unparse_comp(generators);
|
||||
self.p(")");
|
||||
}
|
||||
ExprKind::Await { value } => {
|
||||
ExprKind::Await(ast::ExprAwait { value }) => {
|
||||
group_if!(precedence::AWAIT, {
|
||||
self.p("await ");
|
||||
self.unparse_expr(value, precedence::MAX);
|
||||
});
|
||||
}
|
||||
ExprKind::Yield { value } => {
|
||||
ExprKind::Yield(ast::ExprYield { value }) => {
|
||||
group_if!(precedence::YIELD, {
|
||||
self.p("yield");
|
||||
if let Some(value) = value {
|
||||
|
@ -900,17 +922,17 @@ impl<'a> Generator<'a> {
|
|||
}
|
||||
});
|
||||
}
|
||||
ExprKind::YieldFrom { value } => {
|
||||
ExprKind::YieldFrom(ast::ExprYieldFrom { value }) => {
|
||||
group_if!(precedence::YIELD_FROM, {
|
||||
self.p("yield from ");
|
||||
self.unparse_expr(value, precedence::MAX);
|
||||
});
|
||||
}
|
||||
ExprKind::Compare {
|
||||
ExprKind::Compare(ast::ExprCompare {
|
||||
left,
|
||||
ops,
|
||||
comparators,
|
||||
} => {
|
||||
}) => {
|
||||
group_if!(precedence::CMP, {
|
||||
let new_lvl = precedence::CMP + 1;
|
||||
self.unparse_expr(left, new_lvl);
|
||||
|
@ -932,16 +954,16 @@ impl<'a> Generator<'a> {
|
|||
}
|
||||
});
|
||||
}
|
||||
ExprKind::Call {
|
||||
ExprKind::Call(ast::ExprCall {
|
||||
func,
|
||||
args,
|
||||
keywords,
|
||||
} => {
|
||||
}) => {
|
||||
self.unparse_expr(func, precedence::MAX);
|
||||
self.p("(");
|
||||
if let (
|
||||
[Expr {
|
||||
node: ExprKind::GeneratorExp { elt, generators },
|
||||
node: ExprKind::GeneratorExp(ast::ExprGeneratorExp { elt, generators }),
|
||||
..
|
||||
}],
|
||||
[],
|
||||
|
@ -959,7 +981,7 @@ impl<'a> Generator<'a> {
|
|||
for kw in keywords {
|
||||
self.p_delim(&mut first, ", ");
|
||||
if let Some(arg) = &kw.node.arg {
|
||||
self.p(arg);
|
||||
self.p_id(arg);
|
||||
self.p("=");
|
||||
self.unparse_expr(&kw.node.value, precedence::COMMA);
|
||||
} else {
|
||||
|
@ -970,23 +992,25 @@ impl<'a> Generator<'a> {
|
|||
}
|
||||
self.p(")");
|
||||
}
|
||||
ExprKind::FormattedValue {
|
||||
ExprKind::FormattedValue(ast::ExprFormattedValue {
|
||||
value,
|
||||
conversion,
|
||||
format_spec,
|
||||
} => self.unparse_formatted(value, *conversion, format_spec.as_deref()),
|
||||
ExprKind::JoinedStr { values } => self.unparse_joinedstr(values, false),
|
||||
ExprKind::Constant { value, kind } => {
|
||||
}) => self.unparse_formatted(value, *conversion, format_spec.as_deref()),
|
||||
ExprKind::JoinedStr(ast::ExprJoinedStr { values }) => {
|
||||
self.unparse_joinedstr(values, false);
|
||||
}
|
||||
ExprKind::Constant(ast::ExprConstant { value, kind }) => {
|
||||
if let Some(kind) = kind {
|
||||
self.p(kind);
|
||||
}
|
||||
self.unparse_constant(value);
|
||||
}
|
||||
ExprKind::Attribute { value, attr, .. } => {
|
||||
if let ExprKind::Constant {
|
||||
ExprKind::Attribute(ast::ExprAttribute { value, attr, .. }) => {
|
||||
if let ExprKind::Constant(ast::ExprConstant {
|
||||
value: Constant::Int(_),
|
||||
..
|
||||
} = &value.node
|
||||
}) = &value.node
|
||||
{
|
||||
self.p("(");
|
||||
self.unparse_expr(value, precedence::MAX);
|
||||
|
@ -995,20 +1019,20 @@ impl<'a> Generator<'a> {
|
|||
self.unparse_expr(value, precedence::MAX);
|
||||
self.p(".");
|
||||
};
|
||||
self.p(attr);
|
||||
self.p_id(attr);
|
||||
}
|
||||
ExprKind::Subscript { value, slice, .. } => {
|
||||
ExprKind::Subscript(ast::ExprSubscript { value, slice, .. }) => {
|
||||
self.unparse_expr(value, precedence::MAX);
|
||||
self.p("[");
|
||||
self.unparse_expr(slice, precedence::SUBSCRIPT);
|
||||
self.p("]");
|
||||
}
|
||||
ExprKind::Starred { value, .. } => {
|
||||
ExprKind::Starred(ast::ExprStarred { value, .. }) => {
|
||||
self.p("*");
|
||||
self.unparse_expr(value, precedence::MAX);
|
||||
}
|
||||
ExprKind::Name { id, .. } => self.p(id),
|
||||
ExprKind::List { elts, .. } => {
|
||||
ExprKind::Name(ast::ExprName { id, .. }) => self.p_id(id),
|
||||
ExprKind::List(ast::ExprList { elts, .. }) => {
|
||||
self.p("[");
|
||||
let mut first = true;
|
||||
for elt in elts {
|
||||
|
@ -1017,7 +1041,7 @@ impl<'a> Generator<'a> {
|
|||
}
|
||||
self.p("]");
|
||||
}
|
||||
ExprKind::Tuple { elts, .. } => {
|
||||
ExprKind::Tuple(ast::ExprTuple { elts, .. }) => {
|
||||
if elts.is_empty() {
|
||||
self.p("()");
|
||||
} else {
|
||||
|
@ -1031,7 +1055,7 @@ impl<'a> Generator<'a> {
|
|||
});
|
||||
}
|
||||
}
|
||||
ExprKind::Slice { lower, upper, step } => {
|
||||
ExprKind::Slice(ast::ExprSlice { lower, upper, step }) => {
|
||||
if let Some(lower) = lower {
|
||||
self.unparse_expr(lower, precedence::SLICE);
|
||||
}
|
||||
|
@ -1052,10 +1076,10 @@ impl<'a> Generator<'a> {
|
|||
let inf_str = "1e309";
|
||||
match constant {
|
||||
Constant::Bytes(b) => {
|
||||
self.p(&bytes::repr(b, self.quote.into()));
|
||||
self.p_bytes_repr(b);
|
||||
}
|
||||
Constant::Str(s) => {
|
||||
self.p(&format!("{}", str::repr(s, self.quote.into())));
|
||||
self.p_str_repr(s);
|
||||
}
|
||||
Constant::None => self.p("None"),
|
||||
Constant::Bool(b) => self.p(if *b { "True" } else { "False" }),
|
||||
|
@ -1081,7 +1105,7 @@ impl<'a> Generator<'a> {
|
|||
if fp.is_infinite() {
|
||||
self.p(inf_str);
|
||||
} else {
|
||||
self.p(&rustpython_common::float_ops::to_string(*fp));
|
||||
self.p(&rustpython_literal::float::to_string(*fp));
|
||||
}
|
||||
}
|
||||
Constant::Complex { real, imag } => {
|
||||
|
@ -1139,7 +1163,7 @@ impl<'a> Generator<'a> {
|
|||
}
|
||||
|
||||
fn unparse_arg<U>(&mut self, arg: &Arg<U>) {
|
||||
self.p(&arg.node.arg);
|
||||
self.p_id(&arg.node.arg);
|
||||
if let Some(ann) = &arg.node.annotation {
|
||||
self.p(": ");
|
||||
self.unparse_expr(ann, precedence::COMMA);
|
||||
|
@ -1148,7 +1172,7 @@ impl<'a> Generator<'a> {
|
|||
|
||||
fn unparse_comp<U>(&mut self, generators: &[Comprehension<U>]) {
|
||||
for comp in generators {
|
||||
self.p(if comp.is_async > 0 {
|
||||
self.p(if comp.is_async {
|
||||
" async for "
|
||||
} else {
|
||||
" for "
|
||||
|
@ -1169,7 +1193,7 @@ impl<'a> Generator<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
fn unparse_formatted<U>(&mut self, val: &Expr<U>, conversion: usize, spec: Option<&Expr<U>>) {
|
||||
fn unparse_formatted<U>(&mut self, val: &Expr<U>, conversion: Int, spec: Option<&Expr<U>>) {
|
||||
let mut generator = Generator::new(self.indent, self.quote, self.line_ending);
|
||||
generator.unparse_expr(val, precedence::FORMATTED_VALUE);
|
||||
let brace = if generator.buffer.starts_with('{') {
|
||||
|
@ -1181,10 +1205,10 @@ impl<'a> Generator<'a> {
|
|||
self.p(brace);
|
||||
self.buffer += &generator.buffer;
|
||||
|
||||
if conversion != ConversionFlag::None as usize {
|
||||
if conversion.to_u32() != ConversionFlag::None as u32 {
|
||||
self.p("!");
|
||||
#[allow(clippy::cast_possible_truncation)]
|
||||
self.p(&format!("{}", conversion as u8 as char));
|
||||
self.p(&format!("{}", conversion.to_u32() as u8 as char));
|
||||
}
|
||||
|
||||
if let Some(spec) = spec {
|
||||
|
@ -1197,19 +1221,21 @@ impl<'a> Generator<'a> {
|
|||
|
||||
fn unparse_fstring_elem<U>(&mut self, expr: &Expr<U>, is_spec: bool) {
|
||||
match &expr.node {
|
||||
ExprKind::Constant { value, .. } => {
|
||||
ExprKind::Constant(ast::ExprConstant { value, .. }) => {
|
||||
if let Constant::Str(s) = value {
|
||||
self.unparse_fstring_str(s);
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
ExprKind::JoinedStr { values } => self.unparse_joinedstr(values, is_spec),
|
||||
ExprKind::FormattedValue {
|
||||
ExprKind::JoinedStr(ast::ExprJoinedStr { values }) => {
|
||||
self.unparse_joinedstr(values, is_spec);
|
||||
}
|
||||
ExprKind::FormattedValue(ast::ExprFormattedValue {
|
||||
value,
|
||||
conversion,
|
||||
format_spec,
|
||||
} => self.unparse_formatted(value, *conversion, format_spec.as_deref()),
|
||||
}) => self.unparse_formatted(value, *conversion, format_spec.as_deref()),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
@ -1234,15 +1260,15 @@ impl<'a> Generator<'a> {
|
|||
);
|
||||
generator.unparse_fstring_body(values, is_spec);
|
||||
let body = &generator.buffer;
|
||||
self.p(&format!("{}", str::repr(body, self.quote.into())));
|
||||
self.p_str_repr(body);
|
||||
}
|
||||
}
|
||||
|
||||
fn unparse_alias<U>(&mut self, alias: &Alias<U>) {
|
||||
self.p(&alias.node.name);
|
||||
self.p_id(&alias.node.name);
|
||||
if let Some(asname) = &alias.node.asname {
|
||||
self.p(" as ");
|
||||
self.p(asname);
|
||||
self.p_id(asname);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4,11 +4,11 @@ use std::fmt;
|
|||
use std::ops::Deref;
|
||||
|
||||
use once_cell::unsync::OnceCell;
|
||||
use rustpython_literal::escape::Quote as StrQuote;
|
||||
use rustpython_parser::lexer::LexResult;
|
||||
use rustpython_parser::Tok;
|
||||
|
||||
use crate::newlines::{find_newline, LineEnding};
|
||||
use ruff_rustpython::vendor;
|
||||
|
||||
use crate::source_code::Locator;
|
||||
use crate::str::leading_quote;
|
||||
|
@ -110,11 +110,11 @@ impl From<Quote> for char {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<Quote> for vendor::str::Quote {
|
||||
impl From<Quote> for StrQuote {
|
||||
fn from(val: Quote) -> Self {
|
||||
match val {
|
||||
Quote::Single => vendor::str::Quote::Single,
|
||||
Quote::Double => vendor::str::Quote::Double,
|
||||
Quote::Single => StrQuote::Single,
|
||||
Quote::Double => StrQuote::Double,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//! Specialized AST visitor trait and walk functions that only visit statements.
|
||||
|
||||
use rustpython_parser::ast::{Excepthandler, ExcepthandlerKind, MatchCase, Stmt, StmtKind};
|
||||
use rustpython_parser::ast::{self, Excepthandler, ExcepthandlerKind, MatchCase, Stmt, StmtKind};
|
||||
|
||||
/// A trait for AST visitors that only need to visit statements.
|
||||
pub trait StatementVisitor<'a> {
|
||||
|
@ -26,48 +26,48 @@ pub fn walk_body<'a, V: StatementVisitor<'a> + ?Sized>(visitor: &mut V, body: &'
|
|||
|
||||
pub fn walk_stmt<'a, V: StatementVisitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
|
||||
match &stmt.node {
|
||||
StmtKind::FunctionDef { body, .. } => {
|
||||
StmtKind::FunctionDef(ast::StmtFunctionDef { body, .. }) => {
|
||||
visitor.visit_body(body);
|
||||
}
|
||||
StmtKind::AsyncFunctionDef { body, .. } => {
|
||||
StmtKind::AsyncFunctionDef(ast::StmtAsyncFunctionDef { body, .. }) => {
|
||||
visitor.visit_body(body);
|
||||
}
|
||||
StmtKind::For { body, orelse, .. } => {
|
||||
StmtKind::For(ast::StmtFor { body, orelse, .. }) => {
|
||||
visitor.visit_body(body);
|
||||
visitor.visit_body(orelse);
|
||||
}
|
||||
StmtKind::ClassDef { body, .. } => {
|
||||
StmtKind::ClassDef(ast::StmtClassDef { body, .. }) => {
|
||||
visitor.visit_body(body);
|
||||
}
|
||||
StmtKind::AsyncFor { body, orelse, .. } => {
|
||||
StmtKind::AsyncFor(ast::StmtAsyncFor { body, orelse, .. }) => {
|
||||
visitor.visit_body(body);
|
||||
visitor.visit_body(orelse);
|
||||
}
|
||||
StmtKind::While { body, orelse, .. } => {
|
||||
StmtKind::While(ast::StmtWhile { body, orelse, .. }) => {
|
||||
visitor.visit_body(body);
|
||||
visitor.visit_body(orelse);
|
||||
}
|
||||
StmtKind::If { body, orelse, .. } => {
|
||||
StmtKind::If(ast::StmtIf { body, orelse, .. }) => {
|
||||
visitor.visit_body(body);
|
||||
visitor.visit_body(orelse);
|
||||
}
|
||||
StmtKind::With { body, .. } => {
|
||||
StmtKind::With(ast::StmtWith { body, .. }) => {
|
||||
visitor.visit_body(body);
|
||||
}
|
||||
StmtKind::AsyncWith { body, .. } => {
|
||||
StmtKind::AsyncWith(ast::StmtAsyncWith { body, .. }) => {
|
||||
visitor.visit_body(body);
|
||||
}
|
||||
StmtKind::Match { cases, .. } => {
|
||||
StmtKind::Match(ast::StmtMatch { cases, .. }) => {
|
||||
for match_case in cases {
|
||||
visitor.visit_match_case(match_case);
|
||||
}
|
||||
}
|
||||
StmtKind::Try {
|
||||
StmtKind::Try(ast::StmtTry {
|
||||
body,
|
||||
handlers,
|
||||
orelse,
|
||||
finalbody,
|
||||
} => {
|
||||
}) => {
|
||||
visitor.visit_body(body);
|
||||
for excepthandler in handlers {
|
||||
visitor.visit_excepthandler(excepthandler);
|
||||
|
@ -75,12 +75,12 @@ pub fn walk_stmt<'a, V: StatementVisitor<'a> + ?Sized>(visitor: &mut V, stmt: &'
|
|||
visitor.visit_body(orelse);
|
||||
visitor.visit_body(finalbody);
|
||||
}
|
||||
StmtKind::TryStar {
|
||||
StmtKind::TryStar(ast::StmtTryStar {
|
||||
body,
|
||||
handlers,
|
||||
orelse,
|
||||
finalbody,
|
||||
} => {
|
||||
}) => {
|
||||
visitor.visit_body(body);
|
||||
for excepthandler in handlers {
|
||||
visitor.visit_excepthandler(excepthandler);
|
||||
|
@ -97,7 +97,7 @@ pub fn walk_excepthandler<'a, V: StatementVisitor<'a> + ?Sized>(
|
|||
excepthandler: &'a Excepthandler,
|
||||
) {
|
||||
match &excepthandler.node {
|
||||
ExcepthandlerKind::ExceptHandler { body, .. } => {
|
||||
ExcepthandlerKind::ExceptHandler(ast::ExcepthandlerExceptHandler { body, .. }) => {
|
||||
visitor.visit_body(body);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@ pub fn parse_type_annotation(
|
|||
// isn't the case, e.g., for implicit concatenations, or for annotations that contain
|
||||
// escaped quotes.
|
||||
let leading_quote = str::leading_quote(expression).unwrap();
|
||||
let expr = parser::parse_expression_located(
|
||||
let expr = parser::parse_expression_starts_at(
|
||||
value,
|
||||
"<filename>",
|
||||
range.start() + leading_quote.text_len(),
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
//! AST visitor trait and walk functions.
|
||||
|
||||
use rustpython_parser::ast::{
|
||||
Alias, Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, Excepthandler,
|
||||
self, Alias, Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, Excepthandler,
|
||||
ExcepthandlerKind, Expr, ExprContext, ExprKind, Keyword, MatchCase, Operator, Pattern,
|
||||
PatternKind, Stmt, StmtKind, Unaryop, Withitem,
|
||||
};
|
||||
|
@ -81,13 +81,13 @@ pub fn walk_body<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, body: &'a [Stmt])
|
|||
|
||||
pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
|
||||
match &stmt.node {
|
||||
StmtKind::FunctionDef {
|
||||
StmtKind::FunctionDef(ast::StmtFunctionDef {
|
||||
args,
|
||||
body,
|
||||
decorator_list,
|
||||
returns,
|
||||
..
|
||||
} => {
|
||||
}) => {
|
||||
visitor.visit_arguments(args);
|
||||
for expr in decorator_list {
|
||||
visitor.visit_expr(expr);
|
||||
|
@ -97,13 +97,13 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
|
|||
}
|
||||
visitor.visit_body(body);
|
||||
}
|
||||
StmtKind::AsyncFunctionDef {
|
||||
StmtKind::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
||||
args,
|
||||
body,
|
||||
decorator_list,
|
||||
returns,
|
||||
..
|
||||
} => {
|
||||
}) => {
|
||||
visitor.visit_arguments(args);
|
||||
for expr in decorator_list {
|
||||
visitor.visit_expr(expr);
|
||||
|
@ -113,13 +113,13 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
|
|||
}
|
||||
visitor.visit_body(body);
|
||||
}
|
||||
StmtKind::ClassDef {
|
||||
StmtKind::ClassDef(ast::StmtClassDef {
|
||||
bases,
|
||||
keywords,
|
||||
body,
|
||||
decorator_list,
|
||||
..
|
||||
} => {
|
||||
}) => {
|
||||
for expr in bases {
|
||||
visitor.visit_expr(expr);
|
||||
}
|
||||
|
@ -131,92 +131,92 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
|
|||
}
|
||||
visitor.visit_body(body);
|
||||
}
|
||||
StmtKind::Return { value } => {
|
||||
StmtKind::Return(ast::StmtReturn { value }) => {
|
||||
if let Some(expr) = value {
|
||||
visitor.visit_expr(expr);
|
||||
}
|
||||
}
|
||||
StmtKind::Delete { targets } => {
|
||||
StmtKind::Delete(ast::StmtDelete { targets }) => {
|
||||
for expr in targets {
|
||||
visitor.visit_expr(expr);
|
||||
}
|
||||
}
|
||||
StmtKind::Assign { targets, value, .. } => {
|
||||
StmtKind::Assign(ast::StmtAssign { targets, value, .. }) => {
|
||||
visitor.visit_expr(value);
|
||||
for expr in targets {
|
||||
visitor.visit_expr(expr);
|
||||
}
|
||||
}
|
||||
StmtKind::AugAssign { target, op, value } => {
|
||||
StmtKind::AugAssign(ast::StmtAugAssign { target, op, value }) => {
|
||||
visitor.visit_expr(target);
|
||||
visitor.visit_operator(op);
|
||||
visitor.visit_expr(value);
|
||||
}
|
||||
StmtKind::AnnAssign {
|
||||
StmtKind::AnnAssign(ast::StmtAnnAssign {
|
||||
target,
|
||||
annotation,
|
||||
value,
|
||||
..
|
||||
} => {
|
||||
}) => {
|
||||
visitor.visit_annotation(annotation);
|
||||
if let Some(expr) = value {
|
||||
visitor.visit_expr(expr);
|
||||
}
|
||||
visitor.visit_expr(target);
|
||||
}
|
||||
StmtKind::For {
|
||||
StmtKind::For(ast::StmtFor {
|
||||
target,
|
||||
iter,
|
||||
body,
|
||||
orelse,
|
||||
..
|
||||
} => {
|
||||
}) => {
|
||||
visitor.visit_expr(iter);
|
||||
visitor.visit_expr(target);
|
||||
visitor.visit_body(body);
|
||||
visitor.visit_body(orelse);
|
||||
}
|
||||
StmtKind::AsyncFor {
|
||||
StmtKind::AsyncFor(ast::StmtAsyncFor {
|
||||
target,
|
||||
iter,
|
||||
body,
|
||||
orelse,
|
||||
..
|
||||
} => {
|
||||
}) => {
|
||||
visitor.visit_expr(iter);
|
||||
visitor.visit_expr(target);
|
||||
visitor.visit_body(body);
|
||||
visitor.visit_body(orelse);
|
||||
}
|
||||
StmtKind::While { test, body, orelse } => {
|
||||
StmtKind::While(ast::StmtWhile { test, body, orelse }) => {
|
||||
visitor.visit_expr(test);
|
||||
visitor.visit_body(body);
|
||||
visitor.visit_body(orelse);
|
||||
}
|
||||
StmtKind::If { test, body, orelse } => {
|
||||
StmtKind::If(ast::StmtIf { test, body, orelse }) => {
|
||||
visitor.visit_expr(test);
|
||||
visitor.visit_body(body);
|
||||
visitor.visit_body(orelse);
|
||||
}
|
||||
StmtKind::With { items, body, .. } => {
|
||||
StmtKind::With(ast::StmtWith { items, body, .. }) => {
|
||||
for withitem in items {
|
||||
visitor.visit_withitem(withitem);
|
||||
}
|
||||
visitor.visit_body(body);
|
||||
}
|
||||
StmtKind::AsyncWith { items, body, .. } => {
|
||||
StmtKind::AsyncWith(ast::StmtAsyncWith { items, body, .. }) => {
|
||||
for withitem in items {
|
||||
visitor.visit_withitem(withitem);
|
||||
}
|
||||
visitor.visit_body(body);
|
||||
}
|
||||
StmtKind::Match { subject, cases } => {
|
||||
StmtKind::Match(ast::StmtMatch { subject, cases }) => {
|
||||
visitor.visit_expr(subject);
|
||||
for match_case in cases {
|
||||
visitor.visit_match_case(match_case);
|
||||
}
|
||||
}
|
||||
StmtKind::Raise { exc, cause } => {
|
||||
StmtKind::Raise(ast::StmtRaise { exc, cause }) => {
|
||||
if let Some(expr) = exc {
|
||||
visitor.visit_expr(expr);
|
||||
};
|
||||
|
@ -224,12 +224,12 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
|
|||
visitor.visit_expr(expr);
|
||||
};
|
||||
}
|
||||
StmtKind::Try {
|
||||
StmtKind::Try(ast::StmtTry {
|
||||
body,
|
||||
handlers,
|
||||
orelse,
|
||||
finalbody,
|
||||
} => {
|
||||
}) => {
|
||||
visitor.visit_body(body);
|
||||
for excepthandler in handlers {
|
||||
visitor.visit_excepthandler(excepthandler);
|
||||
|
@ -237,12 +237,12 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
|
|||
visitor.visit_body(orelse);
|
||||
visitor.visit_body(finalbody);
|
||||
}
|
||||
StmtKind::TryStar {
|
||||
StmtKind::TryStar(ast::StmtTryStar {
|
||||
body,
|
||||
handlers,
|
||||
orelse,
|
||||
finalbody,
|
||||
} => {
|
||||
}) => {
|
||||
visitor.visit_body(body);
|
||||
for excepthandler in handlers {
|
||||
visitor.visit_excepthandler(excepthandler);
|
||||
|
@ -250,25 +250,25 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
|
|||
visitor.visit_body(orelse);
|
||||
visitor.visit_body(finalbody);
|
||||
}
|
||||
StmtKind::Assert { test, msg } => {
|
||||
StmtKind::Assert(ast::StmtAssert { test, msg }) => {
|
||||
visitor.visit_expr(test);
|
||||
if let Some(expr) = msg {
|
||||
visitor.visit_expr(expr);
|
||||
}
|
||||
}
|
||||
StmtKind::Import { names } => {
|
||||
StmtKind::Import(ast::StmtImport { names }) => {
|
||||
for alias in names {
|
||||
visitor.visit_alias(alias);
|
||||
}
|
||||
}
|
||||
StmtKind::ImportFrom { names, .. } => {
|
||||
StmtKind::ImportFrom(ast::StmtImportFrom { names, .. }) => {
|
||||
for alias in names {
|
||||
visitor.visit_alias(alias);
|
||||
}
|
||||
}
|
||||
StmtKind::Global { .. } => {}
|
||||
StmtKind::Nonlocal { .. } => {}
|
||||
StmtKind::Expr { value } => visitor.visit_expr(value),
|
||||
StmtKind::Global(_) => {}
|
||||
StmtKind::Nonlocal(_) => {}
|
||||
StmtKind::Expr(ast::StmtExpr { value }) => visitor.visit_expr(value),
|
||||
StmtKind::Pass => {}
|
||||
StmtKind::Break => {}
|
||||
StmtKind::Continue => {}
|
||||
|
@ -277,35 +277,35 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
|
|||
|
||||
pub fn walk_expr<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, expr: &'a Expr) {
|
||||
match &expr.node {
|
||||
ExprKind::BoolOp { op, values } => {
|
||||
ExprKind::BoolOp(ast::ExprBoolOp { op, values }) => {
|
||||
visitor.visit_boolop(op);
|
||||
for expr in values {
|
||||
visitor.visit_expr(expr);
|
||||
}
|
||||
}
|
||||
ExprKind::NamedExpr { target, value } => {
|
||||
ExprKind::NamedExpr(ast::ExprNamedExpr { target, value }) => {
|
||||
visitor.visit_expr(value);
|
||||
visitor.visit_expr(target);
|
||||
}
|
||||
ExprKind::BinOp { left, op, right } => {
|
||||
ExprKind::BinOp(ast::ExprBinOp { left, op, right }) => {
|
||||
visitor.visit_expr(left);
|
||||
visitor.visit_operator(op);
|
||||
visitor.visit_expr(right);
|
||||
}
|
||||
ExprKind::UnaryOp { op, operand } => {
|
||||
ExprKind::UnaryOp(ast::ExprUnaryOp { op, operand }) => {
|
||||
visitor.visit_unaryop(op);
|
||||
visitor.visit_expr(operand);
|
||||
}
|
||||
ExprKind::Lambda { args, body } => {
|
||||
ExprKind::Lambda(ast::ExprLambda { args, body }) => {
|
||||
visitor.visit_arguments(args);
|
||||
visitor.visit_expr(body);
|
||||
}
|
||||
ExprKind::IfExp { test, body, orelse } => {
|
||||
ExprKind::IfExp(ast::ExprIfExp { test, body, orelse }) => {
|
||||
visitor.visit_expr(test);
|
||||
visitor.visit_expr(body);
|
||||
visitor.visit_expr(orelse);
|
||||
}
|
||||
ExprKind::Dict { keys, values } => {
|
||||
ExprKind::Dict(ast::ExprDict { keys, values }) => {
|
||||
for expr in keys.iter().flatten() {
|
||||
visitor.visit_expr(expr);
|
||||
}
|
||||
|
@ -313,52 +313,52 @@ pub fn walk_expr<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, expr: &'a Expr) {
|
|||
visitor.visit_expr(expr);
|
||||
}
|
||||
}
|
||||
ExprKind::Set { elts } => {
|
||||
ExprKind::Set(ast::ExprSet { elts }) => {
|
||||
for expr in elts {
|
||||
visitor.visit_expr(expr);
|
||||
}
|
||||
}
|
||||
ExprKind::ListComp { elt, generators } => {
|
||||
ExprKind::ListComp(ast::ExprListComp { elt, generators }) => {
|
||||
for comprehension in generators {
|
||||
visitor.visit_comprehension(comprehension);
|
||||
}
|
||||
visitor.visit_expr(elt);
|
||||
}
|
||||
ExprKind::SetComp { elt, generators } => {
|
||||
ExprKind::SetComp(ast::ExprSetComp { elt, generators }) => {
|
||||
for comprehension in generators {
|
||||
visitor.visit_comprehension(comprehension);
|
||||
}
|
||||
visitor.visit_expr(elt);
|
||||
}
|
||||
ExprKind::DictComp {
|
||||
ExprKind::DictComp(ast::ExprDictComp {
|
||||
key,
|
||||
value,
|
||||
generators,
|
||||
} => {
|
||||
}) => {
|
||||
for comprehension in generators {
|
||||
visitor.visit_comprehension(comprehension);
|
||||
}
|
||||
visitor.visit_expr(key);
|
||||
visitor.visit_expr(value);
|
||||
}
|
||||
ExprKind::GeneratorExp { elt, generators } => {
|
||||
ExprKind::GeneratorExp(ast::ExprGeneratorExp { elt, generators }) => {
|
||||
for comprehension in generators {
|
||||
visitor.visit_comprehension(comprehension);
|
||||
}
|
||||
visitor.visit_expr(elt);
|
||||
}
|
||||
ExprKind::Await { value } => visitor.visit_expr(value),
|
||||
ExprKind::Yield { value } => {
|
||||
ExprKind::Await(ast::ExprAwait { value }) => visitor.visit_expr(value),
|
||||
ExprKind::Yield(ast::ExprYield { value }) => {
|
||||
if let Some(expr) = value {
|
||||
visitor.visit_expr(expr);
|
||||
}
|
||||
}
|
||||
ExprKind::YieldFrom { value } => visitor.visit_expr(value),
|
||||
ExprKind::Compare {
|
||||
ExprKind::YieldFrom(ast::ExprYieldFrom { value }) => visitor.visit_expr(value),
|
||||
ExprKind::Compare(ast::ExprCompare {
|
||||
left,
|
||||
ops,
|
||||
comparators,
|
||||
} => {
|
||||
}) => {
|
||||
visitor.visit_expr(left);
|
||||
for cmpop in ops {
|
||||
visitor.visit_cmpop(cmpop);
|
||||
|
@ -367,11 +367,11 @@ pub fn walk_expr<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, expr: &'a Expr) {
|
|||
visitor.visit_expr(expr);
|
||||
}
|
||||
}
|
||||
ExprKind::Call {
|
||||
ExprKind::Call(ast::ExprCall {
|
||||
func,
|
||||
args,
|
||||
keywords,
|
||||
} => {
|
||||
}) => {
|
||||
visitor.visit_expr(func);
|
||||
for expr in args {
|
||||
visitor.visit_expr(expr);
|
||||
|
@ -380,49 +380,49 @@ pub fn walk_expr<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, expr: &'a Expr) {
|
|||
visitor.visit_keyword(keyword);
|
||||
}
|
||||
}
|
||||
ExprKind::FormattedValue {
|
||||
ExprKind::FormattedValue(ast::ExprFormattedValue {
|
||||
value, format_spec, ..
|
||||
} => {
|
||||
}) => {
|
||||
visitor.visit_expr(value);
|
||||
if let Some(expr) = format_spec {
|
||||
visitor.visit_format_spec(expr);
|
||||
}
|
||||
}
|
||||
ExprKind::JoinedStr { values } => {
|
||||
ExprKind::JoinedStr(ast::ExprJoinedStr { values }) => {
|
||||
for expr in values {
|
||||
visitor.visit_expr(expr);
|
||||
}
|
||||
}
|
||||
ExprKind::Constant { value, .. } => visitor.visit_constant(value),
|
||||
ExprKind::Attribute { value, ctx, .. } => {
|
||||
ExprKind::Constant(ast::ExprConstant { value, .. }) => visitor.visit_constant(value),
|
||||
ExprKind::Attribute(ast::ExprAttribute { value, ctx, .. }) => {
|
||||
visitor.visit_expr(value);
|
||||
visitor.visit_expr_context(ctx);
|
||||
}
|
||||
ExprKind::Subscript { value, slice, ctx } => {
|
||||
ExprKind::Subscript(ast::ExprSubscript { value, slice, ctx }) => {
|
||||
visitor.visit_expr(value);
|
||||
visitor.visit_expr(slice);
|
||||
visitor.visit_expr_context(ctx);
|
||||
}
|
||||
ExprKind::Starred { value, ctx } => {
|
||||
ExprKind::Starred(ast::ExprStarred { value, ctx }) => {
|
||||
visitor.visit_expr(value);
|
||||
visitor.visit_expr_context(ctx);
|
||||
}
|
||||
ExprKind::Name { ctx, .. } => {
|
||||
ExprKind::Name(ast::ExprName { ctx, .. }) => {
|
||||
visitor.visit_expr_context(ctx);
|
||||
}
|
||||
ExprKind::List { elts, ctx } => {
|
||||
ExprKind::List(ast::ExprList { elts, ctx }) => {
|
||||
for expr in elts {
|
||||
visitor.visit_expr(expr);
|
||||
}
|
||||
visitor.visit_expr_context(ctx);
|
||||
}
|
||||
ExprKind::Tuple { elts, ctx } => {
|
||||
ExprKind::Tuple(ast::ExprTuple { elts, ctx }) => {
|
||||
for expr in elts {
|
||||
visitor.visit_expr(expr);
|
||||
}
|
||||
visitor.visit_expr_context(ctx);
|
||||
}
|
||||
ExprKind::Slice { lower, upper, step } => {
|
||||
ExprKind::Slice(ast::ExprSlice { lower, upper, step }) => {
|
||||
if let Some(expr) = lower {
|
||||
visitor.visit_expr(expr);
|
||||
}
|
||||
|
@ -460,7 +460,9 @@ pub fn walk_excepthandler<'a, V: Visitor<'a> + ?Sized>(
|
|||
excepthandler: &'a Excepthandler,
|
||||
) {
|
||||
match &excepthandler.node {
|
||||
ExcepthandlerKind::ExceptHandler { type_, body, .. } => {
|
||||
ExcepthandlerKind::ExceptHandler(ast::ExcepthandlerExceptHandler {
|
||||
type_, body, ..
|
||||
}) => {
|
||||
if let Some(expr) = type_ {
|
||||
visitor.visit_expr(expr);
|
||||
}
|
||||
|
@ -520,14 +522,16 @@ pub fn walk_match_case<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, match_case:
|
|||
|
||||
pub fn walk_pattern<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, pattern: &'a Pattern) {
|
||||
match &pattern.node {
|
||||
PatternKind::MatchValue { value } => visitor.visit_expr(value),
|
||||
PatternKind::MatchSingleton { value } => visitor.visit_constant(value),
|
||||
PatternKind::MatchSequence { patterns } => {
|
||||
PatternKind::MatchValue(ast::PatternMatchValue { value }) => visitor.visit_expr(value),
|
||||
PatternKind::MatchSingleton(ast::PatternMatchSingleton { value }) => {
|
||||
visitor.visit_constant(value);
|
||||
}
|
||||
PatternKind::MatchSequence(ast::PatternMatchSequence { patterns }) => {
|
||||
for pattern in patterns {
|
||||
visitor.visit_pattern(pattern);
|
||||
}
|
||||
}
|
||||
PatternKind::MatchMapping { keys, patterns, .. } => {
|
||||
PatternKind::MatchMapping(ast::PatternMatchMapping { keys, patterns, .. }) => {
|
||||
for expr in keys {
|
||||
visitor.visit_expr(expr);
|
||||
}
|
||||
|
@ -535,12 +539,12 @@ pub fn walk_pattern<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, pattern: &'a P
|
|||
visitor.visit_pattern(pattern);
|
||||
}
|
||||
}
|
||||
PatternKind::MatchClass {
|
||||
PatternKind::MatchClass(ast::PatternMatchClass {
|
||||
cls,
|
||||
patterns,
|
||||
kwd_patterns,
|
||||
..
|
||||
} => {
|
||||
}) => {
|
||||
visitor.visit_expr(cls);
|
||||
for pattern in patterns {
|
||||
visitor.visit_pattern(pattern);
|
||||
|
@ -550,13 +554,13 @@ pub fn walk_pattern<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, pattern: &'a P
|
|||
visitor.visit_pattern(pattern);
|
||||
}
|
||||
}
|
||||
PatternKind::MatchStar { .. } => {}
|
||||
PatternKind::MatchAs { pattern, .. } => {
|
||||
PatternKind::MatchStar(_) => {}
|
||||
PatternKind::MatchAs(ast::PatternMatchAs { pattern, .. }) => {
|
||||
if let Some(pattern) = pattern {
|
||||
visitor.visit_pattern(pattern);
|
||||
}
|
||||
}
|
||||
PatternKind::MatchOr { patterns } => {
|
||||
PatternKind::MatchOr(ast::PatternMatchOr { patterns }) => {
|
||||
for pattern in patterns {
|
||||
visitor.visit_pattern(pattern);
|
||||
}
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
use ruff_text_size::TextRange;
|
||||
use rustpython_parser::ast::Located;
|
||||
use rustpython_parser::ast::Attributed;
|
||||
|
||||
use crate::source_code::Locator;
|
||||
|
||||
/// Extract the leading indentation from a line.
|
||||
pub fn indentation<'a, T>(locator: &'a Locator, located: &Located<T>) -> Option<&'a str> {
|
||||
pub fn indentation<'a, T>(locator: &'a Locator, located: &Attributed<T>) -> Option<&'a str> {
|
||||
let line_start = locator.line_start(located.start());
|
||||
let indentation = &locator.contents()[TextRange::new(line_start, located.start())];
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue