use proc_macro2::TokenStream;
use quote::quote;
use serde::{Deserialize, Serialize};
use crate::python_ast::{
Attribute, Await, BinOp, BoolOp, Call, CodeGen, CodeGenContext, Compare, Constant, Dict, DictComp, ExprTypeNotYetImplemented, FormattedValue,
GeneratorExp, IfExp, JoinedStr, Lambda, ListComp, Name, NamedExpr, Node, PythonOptions, Set, SetComp, Starred, Subscript, SymbolTableScopes,
Tuple, UnaryOp, Yield, YieldFrom, dump, err_from,
};
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[repr(transparent)]
pub struct Container<T>(pub T);
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
pub enum ExprType {
BoolOp(BoolOp),
NamedExpr(NamedExpr),
BinOp(BinOp),
UnaryOp(UnaryOp),
Lambda(Lambda),
IfExp(IfExp),
Dict(Dict),
Set(Set),
ListComp(ListComp),
DictComp(DictComp),
SetComp(SetComp),
GeneratorExp(GeneratorExp),
Await(Await),
Yield(Yield),
YieldFrom(YieldFrom),
Compare(Compare),
Call(Call),
FormattedValue(FormattedValue),
JoinedStr(JoinedStr),
Constant(Constant),
Attribute(Attribute),
Subscript(Subscript),
Starred(Starred),
Name(Name),
List(Vec<ExprType>),
Tuple(Tuple),
NoneType(Constant),
Unimplemented(String),
#[default]
Unknown,
}
impl<'a> CodeGen for ExprType {
type Context = CodeGenContext;
type Options = PythonOptions;
type SymbolTable = SymbolTableScopes;
fn to_rust(
self,
ctx: Self::Context,
options: Self::Options,
symbols: Self::SymbolTable,
) -> std::result::Result<TokenStream, Box<dyn std::error::Error>> {
match self {
ExprType::Attribute(attribute) => attribute.to_rust(ctx, options, symbols),
ExprType::Await(func) => func.to_rust(ctx, options, symbols),
ExprType::BinOp(binop) => binop.to_rust(ctx, options, symbols),
ExprType::BoolOp(boolop) => boolop.to_rust(ctx, options, symbols),
ExprType::Call(call) => call.to_rust(ctx, options, symbols),
ExprType::Compare(c) => c.to_rust(ctx, options, symbols),
ExprType::Constant(c) => c.to_rust(ctx, options, symbols),
ExprType::Lambda(l) => l.to_rust(ctx, options, symbols),
ExprType::IfExp(i) => i.to_rust(ctx, options, symbols),
ExprType::Dict(d) => d.to_rust(ctx, options, symbols),
ExprType::Set(s) => s.to_rust(ctx, options, symbols),
ExprType::ListComp(lc) => lc.to_rust(ctx, options, symbols),
ExprType::DictComp(dc) => dc.to_rust(ctx, options, symbols),
ExprType::SetComp(sc) => sc.to_rust(ctx, options, symbols),
ExprType::GeneratorExp(ge) => ge.to_rust(ctx, options, symbols),
ExprType::Tuple(t) => t.to_rust(ctx, options, symbols),
ExprType::Subscript(s) => s.to_rust(ctx, options, symbols),
ExprType::Starred(s) => s.to_rust(ctx, options, symbols),
ExprType::Yield(y) => y.to_rust(ctx, options, symbols),
ExprType::YieldFrom(yf) => yf.to_rust(ctx, options, symbols),
ExprType::JoinedStr(js) => js.to_rust(ctx, options, symbols),
ExprType::FormattedValue(fv) => fv.to_rust(ctx, options, symbols),
ExprType::List(l) => {
let mut elements = Vec::new();
let mut has_starred = false;
for li in l {
let code = li.clone().to_rust(ctx.clone(), options.clone(), symbols.clone())?;
if matches!(li, ExprType::Starred(_)) {
has_starred = true;
let code_str = code.to_string();
if code_str.contains("sys :: argv") {
elements.push(quote! { __STARRED_ARGV_MARKER__ });
} else {
elements.push(code);
}
} else {
elements.push(code);
}
}
if has_starred {
let mut final_elements = Vec::new();
let mut has_argv_starred = false;
for element in elements {
let elem_str = element.to_string();
if elem_str.contains("__STARRED_ARGV_MARKER__") {
has_argv_starred = true;
continue; } else {
final_elements.push(element);
}
}
if has_argv_starred {
if final_elements.is_empty() {
Ok(quote! {
(*sys::argv).clone()
})
} else {
Ok(quote! {
{
let mut vec = Vec::new();
#(vec.push((#final_elements).clone().to_string());)*
vec.extend((*sys::argv).iter().cloned());
vec
}
})
}
} else {
Ok(quote! {
vec![#(#final_elements),*]
})
}
} else {
Ok(quote! {
vec![#(#elements),*]
})
}
}
ExprType::Name(name) => name.to_rust(ctx, options, symbols),
ExprType::NoneType(_) => Ok(quote!(None)),
ExprType::UnaryOp(operand) => operand.to_rust(ctx, options, symbols),
_ => {
let error = err_from(ExprTypeNotYetImplemented(self));
Err(error.into())
}
}
}
}
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
pub struct Expr {
pub value: ExprType,
pub ctx: Option<String>,
pub lineno: Option<usize>,
pub col_offset: Option<usize>,
pub end_lineno: Option<usize>,
pub end_col_offset: Option<usize>,
}
impl CodeGen for Expr {
type Context = CodeGenContext;
type Options = PythonOptions;
type SymbolTable = SymbolTableScopes;
fn to_rust(
self,
ctx: Self::Context,
options: Self::Options,
symbols: Self::SymbolTable,
) -> std::result::Result<TokenStream, Box<dyn std::error::Error>> {
if matches!(self.value, ExprType::NoneType(_)) {
return Ok(quote!());
}
self.value.to_rust(ctx, options, symbols)
}
}
impl Node for Expr {
fn lineno(&self) -> Option<usize> {
self.lineno
}
fn col_offset(&self) -> Option<usize> {
self.col_offset
}
fn end_lineno(&self) -> Option<usize> {
self.end_lineno
}
fn end_col_offset(&self) -> Option<usize> {
self.end_col_offset
}
}
#[cfg(python_ast_upstream_tests)]
mod tests {
use super::*;
#[test]
fn check_call_expression() {
let expression = crate::python_ast::parse("test()", "test.py").unwrap();
let mut options = PythonOptions::default();
options.with_std_python = false;
let symbols = SymbolTableScopes::new();
let tokens = expression.clone().to_rust(CodeGenContext::Module("test".to_string()), options, symbols).unwrap();
assert_eq!(
tokens.to_string(),
"fn __module_init__ () -> Result < () , PyException > { test () ; Ok (()) } \
fn main () { if let Err (e) = __module_init__ () { eprintln ! (\"{}\" , e) ; \
std :: process :: exit (1) ; } }"
);
}
}
pub fn condition_to_rust(
expr: &ExprType,
ctx: CodeGenContext,
options: PythonOptions,
symbols: SymbolTableScopes,
) -> Result<TokenStream, Box<dyn std::error::Error>> {
match expr {
ExprType::BoolOp(op) if matches!(op.op, crate::python_ast::BoolOps::And | crate::python_ast::BoolOps::Or) => {
let mut parts = Vec::new();
for value in &op.values {
parts.push(condition_to_rust(value, ctx.clone(), options.clone(), symbols.clone())?);
}
Ok(match op.op {
crate::python_ast::BoolOps::And => quote!(#((#parts))&&*),
_ => quote!(#((#parts))||*),
})
}
ExprType::UnaryOp(u) if matches!(u.op, crate::python_ast::Ops::Not) => {
let inner = condition_to_rust(&u.operand, ctx, options, symbols)?;
Ok(quote!(!(#inner)))
}
ExprType::Compare(_) => expr.clone().to_rust(ctx, options, symbols),
ExprType::Constant(c) if matches!(&c.0, Some(litrs::Literal::Bool(_))) => expr.clone().to_rust(ctx, options, symbols),
other => {
let tokens = other.clone().to_rust(ctx, options, symbols)?;
Ok(quote!((#tokens).is_truthy()))
}
}
}