1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//! See Grammar.txt for the python reference grammar.
//!
use ::token::OwnedTk;


#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
pub enum Ast {
    Module(Module),
    Statement(Stmt),
    Expression(Expr),
}


impl Default for Ast {
    fn default() -> Self {
        Ast::Module(Module::Body(Vec::new()))
    }
}


#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
pub enum Module {
    Body(Vec<Stmt>)
}


/// Type alias for a boxed expression. The Expr enum needs this heap indirection to break
/// a recursive type definition that would otherwise result in a struct of infinite size.
///
pub type BoxedExpr = Box<Expr>;


#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
pub enum FnType {
    Async,
    Sync,
}


#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
pub enum Stmt {
    FunctionDef { fntype: FnType, name: OwnedTk, arguments: Vec<Expr>, body: Box<Stmt> },
    Block(Vec<Stmt>),
    ClassDef {name: OwnedTk, bases: Vec<Expr>, body: Box<Stmt> },
    Return(Option<Expr>),
    Delete(Vec<Expr>),
    Assign { target: Expr, value: Expr},
    AugAssign { target: Expr, op: Op, value: Expr},

    Import, //(alias* names)
    ImportFrom, // (identifier? module, alias* names, int? level))
    Global(Vec<OwnedTk>),
    Nonlocal(Vec<OwnedTk>),
    Assert { test: Expr, message: Option<Expr> },
    Expr(Expr),
    Pass,
    Break,
    Continue,
    Newline(usize),
}


#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
pub enum Expr {
    Lambda {arguments: Vec<Expr>, body: Box<Expr>},
    Conditional {condition: Box<Expr>, consequent: Box<Expr>, alternative: Box<Expr>},
    BinOp { op: Op, left: BoxedExpr, right: BoxedExpr },
    UnaryOp { op: Op, operand: BoxedExpr },
    Call { func: OwnedTk, args: Vec<Expr>,  keywords: ()},
    Attribute { value: Box<Expr>, attr: OwnedTk },
    Dict { items: Vec<(Expr, Expr)> },
    List { elems: Vec<Expr> },
    NameConstant(OwnedTk),
    Constant(OwnedTk),
    None
}

impl Default for Expr {
    fn default() -> Self {
        Expr::None
    }
}


#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
pub struct Op(pub OwnedTk);