use ruff_python_ast::{
self as ruff, Arguments, BoolOp, Comprehension, ConversionFlag, Expr, Identifier, Operator,
Parameter, ParameterWithDefault, Parameters,
};
use ruff_text_size::Ranged;
use rustpython_compiler_core::SourceFile;
use rustpython_literal::escape::{AsciiEscape, UnicodeEscape};
use std::fmt::{self, Display as _};
mod precedence {
macro_rules! precedence {
($($op:ident,)*) => {
precedence!(@0, $($op,)*);
};
(@$i:expr, $op1:ident, $($op:ident,)*) => {
pub const $op1: u8 = $i;
precedence!(@$i + 1, $($op,)*);
};
(@$i:expr,) => {};
}
precedence!(
TUPLE, TEST, OR, AND, NOT, CMP, // "EXPR" =
BOR, BXOR, BAND, SHIFT, ARITH, TERM, FACTOR, POWER, AWAIT, ATOM,
);
pub const EXPR: u8 = BOR;
}
struct Unparser {
const fn new(f: &'b mut fmt::Formatter Self {
Self { f, source }
}
fn p(&mut self, s: &str) -> fmt::Result {
self.f.write_str(s)
}
fn p_id(&mut self, s: &Identifier) -> fmt::Result {
self.f.write_str(s.as_str())
}
fn p_if(&mut self, cond: bool, s: &str) -> fmt::Result {
if cond {
self.f.write_str(s)?;
}
Ok(())
}
fn p_delim(&mut self, first: &mut bool, s: &str) -> fmt::Result {
self.p_if(!std::mem::take(first), s)
}
fn write_fmt(&mut self, f: fmt::Arguments self.unparse_fstring(value)?,
Expr::TString(_) => self.p("t\"\"")?,
Expr::StringLiteral(ruff::ExprStringLiteral { value, .. }) => {
if value.is_unicode() {
self.p("u")?
}
UnicodeEscape::new_repr(value.to_str().as_ref())
.str_repr()
.fmt(self.f)?
}
Expr::BytesLiteral(ruff::ExprBytesLiteral { value, .. }) => {
AsciiEscape::new_repr(&value.bytes().collect::())
.bytes_repr()
.fmt(self.f)?
}
Expr::NumberLiteral(ruff::ExprNumberLiteral { value, .. }) => {
const { assert!(f64::MAX_10_EXP == 308) };
let inf_str = "1e309";
match value {
ruff::Number::Int(int) => int.fmt(self.f)?,
&ruff::Number::Float(fp) => {
if fp.is_infinite() {
self.p(inf_str)?
} else {
self.p(&rustpython_literal::float::to_string(fp))?
}
}
&ruff::Number::Complex { real, imag } => self
.p(&rustpython_literal::complex::to_string(real, imag)
.replace("inf", inf_str))?,
}
}
Expr::BooleanLiteral(ruff::ExprBooleanLiteral { value, .. }) => {
self.p(if *value { "True" } else { "False" })?
}
Expr::NoneLiteral(ruff::ExprNoneLiteral { .. }) => self.p("None")?,
Expr::EllipsisLiteral(ruff::ExprEllipsisLiteral { .. }) => self.p("...")?,
Expr::Attribute(ruff::ExprAttribute { value, attr, .. }) => {
self.unparse_expr(value, precedence::ATOM)?;
let period = if let Expr::NumberLiteral(ruff::ExprNumberLiteral {
value: ruff::Number::Int(_),
..
}) = value.as_ref()
{
" ."
} else {
"."
};
self.p(period)?;
self.p_id(attr)?;
}
Expr::Subscript(ruff::ExprSubscript { value, slice, .. }) => {
self.unparse_expr(value, precedence::ATOM)?;
let lvl = precedence::TUPLE;
self.p("[")?;
self.unparse_expr(slice, lvl)?;
self.p("]")?;
}
Expr::Starred(ruff::ExprStarred { value, .. }) => {
self.p("*")?;
self.unparse_expr(value, precedence::EXPR)?;
}
Expr::Name(ruff::ExprName { id, .. }) => self.p(id.as_str())?,
Expr::List(ruff::ExprList { elts, .. }) => {
self.p("[")?;
let mut first = true;
for elt in elts {
self.p_delim(&mut first, ", ")?;
self.unparse_expr(elt, precedence::TEST)?;
}
self.p("]")?;
}
Expr::Tuple(ruff::ExprTuple { elts, .. }) => {
if elts.is_empty() {
self.p("()")?;
} else {
group_if!(precedence::TUPLE, {
let mut first = true;
for elt in elts {
self.p_delim(&mut first, ", ")?;
self.unparse_expr(elt, precedence::TEST)?;
}
self.p_if(elts.len() == 1, ",")?;
})
}
}
Expr::Slice(ruff::ExprSlice {
lower,
upper,
step,
node_index: _,
range: _range,
}) => {
if let Some(lower) = lower {
self.unparse_expr(lower, precedence::TEST)?;
}
self.p(":")?;
if let Some(upper) = upper {
self.unparse_expr(upper, precedence::TEST)?;
}
if let Some(step) = step {
self.p(":")?;
self.unparse_expr(step, precedence::TEST)?;
}
}
Expr::IpyEscapeCommand(_) => {}
}
Ok(())
}
fn unparse_arguments(&mut self, args: &Parameters) -> fmt::Result {
let mut first = true;
for (i, arg) in args.posonlyargs.iter().chain(&args.args).enumerate() {
self.p_delim(&mut first, ", ")?;
self.unparse_function_arg(arg)?;
self.p_if(i + 1 == args.posonlyargs.len(), ", /")?;
}
if args.vararg.is_some() || !args.kwonlyargs.is_empty() {
self.p_delim(&mut first, ", ")?;
self.p("*")?;
}
if let Some(vararg) = &args.vararg {
self.unparse_arg(vararg)?;
}
for kwarg in &args.kwonlyargs {
self.p_delim(&mut first, ", ")?;
self.unparse_function_arg(kwarg)?;
}
if let Some(kwarg) = &args.kwarg {
self.p_delim(&mut first, ", ")?;
self.p("**")?;
self.unparse_arg(kwarg)?;
}
Ok(())
}
fn unparse_function_arg(&mut self, arg: &ParameterWithDefault) -> fmt::Result {
self.unparse_arg(&arg.parameter)?;
if let Some(default) = &arg.default {
write!(self, "={}", UnparseExpr::new(default, self.source))?;
}
Ok(())
}
fn unparse_arg(&mut self, arg: &Parameter) -> fmt::Result {
self.p_id(&arg.name)?;
if let Some(ann) = &arg.annotation {
write!(self, ": {}", UnparseExpr::new(ann, self.source))?;
}
Ok(())
}
fn unparse_comp(&mut self, generators: &[Comprehension]) -> fmt::Result {
for comp in generators {
self.p(if comp.is_async {
" async for "
} else {
" for "
})?;
self.unparse_expr(&comp.target, precedence::TUPLE)?;
self.p(" in ")?;
self.unparse_expr(&comp.iter, precedence::TEST + 1)?;
for cond in &comp.ifs {
self.p(" if ")?;
self.unparse_expr(cond, precedence::TEST + 1)?;
}
}
Ok(())
}
fn unparse_fstring_body(
&mut self,
elements: &[ruff::InterpolatedStringElement],
) -> fmt::Result {
for elem in elements {
self.unparse_fstring_elem(elem)?;
}
Ok(())
}
fn unparse_formatted(
&mut self,
val: &Expr,
debug_text: Option,
conversion: ConversionFlag,
spec: Option,
) -> fmt::Result {
let buffered = to_string_fmt(|f| {
Unparser::new(f, self.source).unparse_expr(val, precedence::TEST + 1)
});
if let Some(ruff::DebugText { leading, trailing }) = debug_text {
self.p(leading)?;
self.p(self.source.slice(val.range()))?;
self.p(trailing)?;
}
let brace = if buffered.starts_with('{') {
// put a space to avoid escaping the bracket
"{ "
} else {
"{"
};
self.p(brace)?;
self.p(&buffered)?;
drop(buffered);
if conversion != ConversionFlag::None {
self.p("!")?;
let buf = &[conversion as u8];
let c = std::str::from_utf8(buf).unwrap();
self.p(c)?;
}
if let Some(spec) = spec {
self.p(":")?;
self.unparse_fstring_body(&spec.elements)?;
}
self.p("}")?;
Ok(())
}
fn unparse_fstring_elem(&mut self, elem: &ruff::InterpolatedStringElement) -> fmt::Result {
match elem {
ruff::InterpolatedStringElement::Interpolation(ruff::InterpolatedElement {
expression,
debug_text,
conversion,
format_spec,
..
}) => self.unparse_formatted(
expression,
debug_text.as_ref(),
*conversion,
format_spec.as_deref(),
),
ruff::InterpolatedStringElement::Literal(ruff::InterpolatedStringLiteralElement {
value,
..
}) => self.unparse_fstring_str(value),
}
}
fn unparse_fstring_str(&mut self, s: &str) -> fmt::Result {
let s = s.replace('{', "{{").replace('}', "}}");
self.p(&s)
}
fn unparse_fstring(&mut self, value: &ruff::FStringValue) -> fmt::Result {
self.p("f")?;
let body = to_string_fmt(|f| {
value.iter().try_for_each(|part| match part {
ruff::FStringPart::Literal(lit) => f.write_str(lit),
ruff::FStringPart::FString(ruff::FString { elements, .. }) => {
Unparser::new(f, self.source).unparse_fstring_body(elements)
}
})
});
// .unparse_fstring_body(elements));
UnicodeEscape::new_repr(body.as_str().as_ref())
.str_repr()
.write(self.f)
}
}
pub struct UnparseExpr UnparseExpr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result) -> String {
use std::cell::Cell;
struct Fmt(Cell);
impl) -> fmt::Result {
self.0.take().unwrap()(f)
}
}
Fmt(Cell::new(Some(f))).to_string()
}