#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinOp {
Add,
Sub,
Mul,
Div, FloorDiv, Mod, Pow, MatMul, BitAnd,
BitOr,
BitXor,
Shl,
Shr,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BoolOp {
And,
Or,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CmpOp {
Eq,
Ne,
Lt,
Le,
Gt,
Ge,
Is,
IsNot,
In,
NotIn,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnOp {
Neg, Pos, Not, Invert, }
#[derive(Debug, Clone, PartialEq)]
pub enum FStrPart {
Lit(String),
Expr {
expr: Box<Expr>,
src: String,
conv: Option<char>,
spec: Vec<FStrPart>,
span: Span,
},
}
#[derive(Debug, Clone, PartialEq)]
pub struct Comprehension {
pub target: Box<Expr>,
pub iter: Box<Expr>,
pub ifs: Vec<Expr>,
pub is_async: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Keyword {
pub name: Option<String>,
pub value: Expr,
pub span: Option<(u32, u32, u32, u32)>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
pub struct Span {
pub line: u32,
pub end_line: u32,
pub start: u32,
pub end: u32,
pub anchor_start: u32,
pub anchor_end: u32,
pub suppress: bool,
}
impl Span {
pub const NONE: Span = Span {
line: 0,
end_line: 0,
start: 0,
end: 0,
anchor_start: 0,
anchor_end: 0,
suppress: false,
};
pub fn is_some(&self) -> bool {
self.line != 0
}
pub fn is_one_line(&self) -> bool {
self.end_line == self.line
}
pub fn has_anchor(&self) -> bool {
self.anchor_end > self.anchor_start
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Expr {
None,
True,
False,
Ellipsis,
Int(i64),
BigInt(String),
Float(f64),
Complex(f64),
Str(String),
Bytes(Vec<u8>),
FString(Vec<FStrPart>),
TString(Vec<FStrPart>),
Name(String),
List(Vec<Expr>),
Tuple(Vec<Expr>),
Set(Vec<Expr>),
Dict(Vec<(Option<Expr>, Expr)>),
Starred(Box<Expr>),
BoolOp(BoolOp, Vec<Expr>),
UnaryOp(UnOp, Box<Expr>),
BinOp(BinOp, Box<Expr>, Box<Expr>),
Compare(Box<Expr>, Vec<(CmpOp, Expr)>),
IfExp {
test: Box<Expr>,
body: Box<Expr>,
orelse: Box<Expr>,
},
Call {
func: Box<Expr>,
args: Vec<Expr>,
keywords: Vec<Keyword>,
},
Attribute(Box<Expr>, String),
Subscript(Box<Expr>, Box<Expr>),
Slice {
lo: Option<Box<Expr>>,
hi: Option<Box<Expr>>,
step: Option<Box<Expr>>,
},
Lambda {
params: Box<Params>,
body: Box<Expr>,
},
ListComp(Box<Expr>, Vec<Comprehension>),
SetComp(Box<Expr>, Vec<Comprehension>),
DictComp(Box<Expr>, Box<Expr>, Vec<Comprehension>),
GenExp(Box<Expr>, Vec<Comprehension>),
Yield(Option<Box<Expr>>),
YieldFrom(Box<Expr>),
Await(Box<Expr>),
NamedExpr(Box<Expr>, Box<Expr>),
Spanned(Box<Expr>, Span),
}
impl Expr {
pub fn unspanned(&self) -> &Expr {
let mut e = self;
while let Expr::Spanned(inner, _) = e {
e = inner;
}
e
}
pub fn span(&self) -> Span {
match self {
Expr::Spanned(_, s) => *s,
_ => Span::NONE,
}
}
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Params {
pub names: Vec<String>,
pub defaults: Vec<Expr>,
pub posonly: usize,
pub star: Option<String>,
pub kwonly: Vec<String>,
pub kwonly_defaults: Vec<Option<Expr>>,
pub kwargs: Option<String>,
pub annotations: Vec<(String, Expr)>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ExceptHandler {
pub typ: Option<Expr>,
pub name: Option<String>,
pub body: Vec<Stmt>,
pub star: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub struct WithItem {
pub context: Expr,
pub vars: Option<Expr>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum StmtKind {
Expr(Expr),
Assign {
targets: Vec<Expr>,
value: Expr,
},
AugAssign {
target: Expr,
op: BinOp,
value: Expr,
},
AnnAssign {
target: Expr,
annotation: Expr,
value: Option<Expr>,
},
If {
test: Expr,
body: Vec<Stmt>,
orelse: Vec<Stmt>,
},
While {
test: Expr,
body: Vec<Stmt>,
orelse: Vec<Stmt>,
},
For {
target: Expr,
iter: Expr,
body: Vec<Stmt>,
orelse: Vec<Stmt>,
is_async: bool,
},
With {
items: Vec<WithItem>,
body: Vec<Stmt>,
is_async: bool,
},
FuncDef {
name: String,
params: Params,
body: Vec<Stmt>,
decorators: Vec<Expr>,
is_async: bool,
},
ClassDef {
name: String,
bases: Vec<Expr>,
keywords: Vec<Keyword>,
body: Vec<Stmt>,
decorators: Vec<Expr>,
},
Return(Option<Expr>),
Delete(Vec<Expr>),
Pass,
Break,
Continue,
Import(Vec<Alias>),
ImportFrom {
module: Option<String>,
names: Vec<Alias>,
level: usize,
},
Global(Vec<String>),
Nonlocal(Vec<String>),
Raise {
exc: Option<Expr>,
cause: Option<Expr>,
},
Try {
body: Vec<Stmt>,
handlers: Vec<ExceptHandler>,
orelse: Vec<Stmt>,
finalbody: Vec<Stmt>,
},
Assert {
test: Expr,
msg: Option<Expr>,
},
Match {
subject: Expr,
cases: Vec<MatchCase>,
},
TypeAlias {
name: String,
params: Vec<TypeParam>,
value: Expr,
},
}
#[derive(Debug, Clone, PartialEq)]
pub struct TypeParam {
pub name: String,
pub kind: TypeParamKind,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TypeParamKind {
TypeVar,
TypeVarTuple,
ParamSpec,
}
#[derive(Debug, Clone, PartialEq)]
pub struct MatchCase {
pub pattern: Pattern,
pub guard: Option<Expr>,
pub body: Vec<Stmt>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Pattern {
pub kind: PatternKind,
pub loc: Loc,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Loc {
pub lineno: u32,
pub col_offset: u32,
pub end_lineno: u32,
pub end_col_offset: u32,
}
#[derive(Debug, Clone, PartialEq)]
pub enum PatternKind {
Wildcard,
Capture(String),
Value(Expr),
Or(Vec<Pattern>),
As(Box<Pattern>, String),
Sequence {
elems: Vec<Pattern>,
star: Option<usize>,
},
Star(Option<String>),
Mapping {
keys: Vec<(Expr, Pattern)>,
rest: Option<String>,
},
Class {
cls: Expr,
pos: Vec<Pattern>,
kw: Vec<(String, Pattern)>,
},
}
#[derive(Debug, Clone, PartialEq)]
pub struct Alias {
pub name: String,
pub asname: Option<String>,
pub span: Option<(u32, u32, u32, u32)>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Stmt {
pub kind: StmtKind,
pub line: u32,
pub span: Option<(u32, u32, u32, u32)>,
}
impl Stmt {
pub fn new(kind: StmtKind, line: u32) -> Stmt {
Stmt {
kind,
line,
span: None,
}
}
}
impl From<StmtKind> for Stmt {
fn from(kind: StmtKind) -> Stmt {
Stmt {
kind,
line: 0,
span: None,
}
}
}
impl Drop for Expr {
fn drop(&mut self) {
let mut pending = Vec::new();
self.take_boxed_children(&mut pending);
while let Some(mut child) = pending.pop() {
child.take_boxed_children(&mut pending);
}
}
}
impl Expr {
fn take_boxed_children(&mut self, out: &mut Vec<Expr>) {
let mut take = |b: &mut Box<Expr>| {
if !matches!(**b, Expr::None) {
out.push(std::mem::replace(&mut **b, Expr::None));
}
};
match self {
Expr::Starred(x)
| Expr::UnaryOp(_, x)
| Expr::Attribute(x, _)
| Expr::YieldFrom(x)
| Expr::Await(x)
| Expr::Spanned(x, _)
| Expr::Compare(x, _)
| Expr::Call { func: x, .. }
| Expr::Lambda { body: x, .. }
| Expr::ListComp(x, _)
| Expr::SetComp(x, _)
| Expr::GenExp(x, _)
| Expr::Yield(Some(x)) => take(x),
Expr::BinOp(_, a, b)
| Expr::Subscript(a, b)
| Expr::NamedExpr(a, b)
| Expr::DictComp(a, b, _) => {
take(a);
take(b);
}
Expr::IfExp { test, body, orelse } => {
take(test);
take(body);
take(orelse);
}
Expr::Slice { lo, hi, step } => {
for b in [lo, hi, step].into_iter().flatten() {
take(b);
}
}
_ => {}
}
}
}