#[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>,
},
}
#[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,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
pub struct Span {
pub 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,
start: 0,
end: 0,
anchor_start: 0,
anchor_end: 0,
suppress: false,
};
pub fn is_some(&self) -> bool {
self.line != 0
}
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: 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>,
},
}
#[derive(Debug, Clone, PartialEq)]
pub struct MatchCase {
pub pattern: Pattern,
pub guard: Option<Expr>,
pub body: Vec<Stmt>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Pattern {
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>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Stmt {
pub kind: StmtKind,
pub line: u32,
}
impl Stmt {
pub fn new(kind: StmtKind, line: u32) -> Stmt {
Stmt { kind, line }
}
}
impl From<StmtKind> for Stmt {
fn from(kind: StmtKind) -> Stmt {
Stmt { kind, line: 0 }
}
}