use std::fmt;
#[cfg(feature = "bigint")]
use num_bigint::BigUint;
#[cfg(feature = "wtf8")]
use wtf8;
#[cfg(feature = "bigint")]
pub type IntegerType = BigUint;
#[cfg(not(feature = "bigint"))]
pub type IntegerType = u64;
#[cfg(feature = "wtf8")]
pub type PyStringContent = wtf8::Wtf8Buf;
#[cfg(feature = "wtf8")]
pub type PyStringCodePoint = wtf8::CodePoint;
#[cfg(not(feature = "wtf8"))]
pub type PyStringContent = String;
#[cfg(not(feature = "wtf8"))]
pub type PyStringCodePoint = char;
pub type Name = String;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum StarParams<T> {
No,
Anonymous,
Named(T),
}
impl<T> Default for StarParams<T> {
fn default() -> StarParams<T> {
StarParams::No
}
}
#[derive(Clone, Debug, PartialEq, Default)]
pub struct TypedArgsList {
pub posonly_args: Vec<(Name, Option<Expression>, Option<Expression>)>,
pub args: Vec<(Name, Option<Expression>, Option<Expression>)>,
pub star_args: StarParams<(Name, Option<Expression>)>,
pub keyword_args: Vec<(Name, Option<Expression>, Option<Expression>)>,
pub star_kwargs: Option<(Name, Option<Expression>)>,
}
#[derive(Clone, Debug, PartialEq, Default)]
pub struct UntypedArgsList {
pub posonly_args: Vec<(Name, Option<Expression>)>,
pub args: Vec<(Name, Option<Expression>)>,
pub star_args: StarParams<Name>,
pub keyword_args: Vec<(Name, Option<Expression>)>,
pub star_kwargs: Option<Name>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Decorator {
pub name: Vec<Name>,
pub args: Option<Vec<Argument>>,
}
#[derive(Clone, Debug, PartialEq)]
pub enum Argument {
Positional(Expression),
Starargs(Expression),
Keyword(Name, Expression),
Kwargs(Expression),
}
#[derive(Clone, Debug, PartialEq)]
pub enum Subscript {
Simple(Expression),
Double(Option<Expression>, Option<Expression>),
Triple(Option<Expression>, Option<Expression>, Option<Expression>),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Uop {
Plus,
Minus,
Invert,
Not,
}
impl fmt::Display for Uop {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(
f,
"{}",
match *self {
Uop::Plus => "+",
Uop::Minus => "-",
Uop::Invert => "~",
Uop::Not => "not ",
}
)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Bop {
Add,
Sub,
Mult,
Matmult,
Mod,
Floordiv,
Div,
Power,
Lshift,
Rshift,
BitAnd,
BitXor,
BitOr,
Lt,
Gt,
Eq,
Leq,
Geq,
Neq,
In,
NotIn,
Is,
IsNot,
And,
Or,
}
impl fmt::Display for Bop {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(
f,
"{}",
match *self {
Bop::Add => "+",
Bop::Sub => "-",
Bop::Mult => "*",
Bop::Matmult => "@",
Bop::Mod => "%",
Bop::Floordiv => "//",
Bop::Div => "/",
Bop::Power => "**",
Bop::Lshift => "<<",
Bop::Rshift => ">>",
Bop::BitAnd => "&",
Bop::BitXor => "^",
Bop::BitOr => "|",
Bop::Lt => "<",
Bop::Gt => ">",
Bop::Eq => "==",
Bop::Leq => "<=",
Bop::Geq => ">=",
Bop::Neq => "!=",
Bop::In => " in ",
Bop::NotIn => " not in ",
Bop::Is => " is ",
Bop::IsNot => " is not ",
Bop::And => " and ",
Bop::Or => " or ",
}
)
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum ComprehensionChunk {
If {
cond: Expression,
},
For {
async: bool,
item: Vec<Expression>,
iterator: Expression,
},
}
#[derive(Clone, Debug, PartialEq)]
pub enum DictItem {
Star(Expression),
Unique(Expression, Expression),
}
#[derive(Clone, Debug, PartialEq)]
pub enum SetItem {
Star(Expression),
Unique(Expression),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PyString {
pub prefix: String,
pub content: PyStringContent,
}
#[derive(Clone, Debug, PartialEq)]
pub enum Expression {
Ellipsis,
None,
True,
False,
Name(Name),
Int(IntegerType),
ImaginaryInt(IntegerType),
Float(f64),
ImaginaryFloat(f64),
String(Vec<PyString>),
Bytes(Vec<u8>),
DictLiteral(Vec<DictItem>),
SetLiteral(Vec<SetItem>),
ListLiteral(Vec<SetItem>),
TupleLiteral(Vec<SetItem>),
DictComp(Box<DictItem>, Vec<ComprehensionChunk>),
SetComp(Box<SetItem>, Vec<ComprehensionChunk>),
ListComp(Box<SetItem>, Vec<ComprehensionChunk>),
Generator(Box<SetItem>, Vec<ComprehensionChunk>),
Await(Box<Expression>),
Call(Box<Expression>, Vec<Argument>),
Subscript(Box<Expression>, Vec<Subscript>),
Attribute(Box<Expression>, Name),
Uop(Uop, Box<Expression>),
Bop(Bop, Box<Expression>, Box<Expression>),
MultiBop(Box<Expression>, Vec<(Bop, Expression)>),
Ternary(Box<Expression>, Box<Expression>, Box<Expression>),
Yield(Vec<Expression>),
YieldFrom(Box<Expression>),
Star(Box<Expression>),
Lambdef(UntypedArgsList, Box<Expression>),
Named(Box<Expression>, Box<Expression>),
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Import {
ImportFrom {
leading_dots: usize,
path: Vec<Name>,
names: Vec<(Name, Option<Name>)>,
},
ImportStarFrom {
leading_dots: usize,
path: Vec<Name>,
},
Import {
names: Vec<(Vec<Name>, Option<Name>)>,
},
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum AugAssignOp {
Add,
Sub,
Mult,
MatMult,
Div,
Mod,
BitAnd,
BitOr,
BitXor,
Lshift,
Rshift,
Power,
Floordiv,
}
impl fmt::Display for AugAssignOp {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(
f,
"{}",
match *self {
AugAssignOp::Add => "+=",
AugAssignOp::Sub => "-=",
AugAssignOp::Mult => "*=",
AugAssignOp::MatMult => "@=",
AugAssignOp::Div => "/=",
AugAssignOp::Mod => "%=",
AugAssignOp::BitAnd => "&=",
AugAssignOp::BitOr => "|=",
AugAssignOp::BitXor => "^=",
AugAssignOp::Lshift => "<<=",
AugAssignOp::Rshift => ">>=",
AugAssignOp::Power => "**=",
AugAssignOp::Floordiv => "//=",
}
)
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum Statement {
Pass,
Del(Vec<Expression>),
Break,
Continue,
Return(Vec<Expression>),
RaiseExcFrom(Expression, Expression),
RaiseExc(Expression),
Raise,
Global(Vec<Name>),
Nonlocal(Vec<Name>),
Assert(Expression, Option<Expression>),
Import(Import),
Expressions(Vec<Expression>),
Assignment(Vec<Expression>, Vec<Vec<Expression>>),
TypeAnnotation(Vec<Expression>, Expression),
TypedAssignment(Vec<Expression>, Expression, Vec<Expression>),
AugmentedAssignment(Vec<Expression>, AugAssignOp, Vec<Expression>),
Compound(Box<CompoundStatement>),
}
#[derive(Clone, Debug, PartialEq)]
pub struct Funcdef {
pub async: bool,
pub decorators: Vec<Decorator>,
pub name: String,
pub parameters: TypedArgsList,
pub return_type: Option<Expression>,
pub code: Vec<Statement>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Classdef {
pub decorators: Vec<Decorator>,
pub name: String,
pub arguments: Vec<Argument>,
pub code: Vec<Statement>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Try {
pub try_block: Vec<Statement>,
pub except_clauses: Vec<(Expression, Option<Name>, Vec<Statement>)>,
pub last_except: Vec<Statement>,
pub else_block: Vec<Statement>,
pub finally_block: Vec<Statement>,
}
#[derive(Clone, Debug, PartialEq)]
pub enum CompoundStatement {
If(Vec<(Expression, Vec<Statement>)>, Option<Vec<Statement>>),
For {
async: bool,
item: Vec<Expression>,
iterator: Vec<Expression>,
for_block: Vec<Statement>,
else_block: Option<Vec<Statement>>,
},
While(Expression, Vec<Statement>, Option<Vec<Statement>>),
With(Vec<(Expression, Option<Expression>)>, Vec<Statement>),
Funcdef(Funcdef),
Classdef(Classdef),
Try(Try),
}