view src/python_ast.rs @ 71:2c1e2ce41263

Add ast.AsyncFor.
author Bastien Orivel <eijebong@bananium.fr>
date Mon, 13 Jun 2016 18:02:34 +0200
parents a73eaf42bea1
children 7ac23f4336b1
line wrap: on
line source

#![allow(non_camel_case_types)]

#[derive(Clone, Debug)]
pub struct Module {
    pub name: String,
    pub statements: Vec<stmt>,
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum stmt {
    // FunctionDef(identifier name, arguments args, stmt* body, expr* decorator_list, expr? returns)
    // AsyncFunctionDef(identifier name, arguments args, stmt* body, expr* decorator_list, expr? returns)
    // If the last field is true, then it's an AsyncFunctionDef.
    FunctionDef(String, arguments, Vec<stmt>, Vec<expr>, Option<expr>, bool),

    // ClassDef(identifier name, expr* bases, keyword* keywords, stmt* body, expr* decorator_list)
    ClassDef(String, Vec<expr>, Vec<keyword>, Vec<stmt>, Vec<expr>),

    // Return(expr? value)
    Return(Option<expr>),

    // Delete(expr* targets)
    Delete(Vec<expr>),

    // Assign(expr* targets, expr value)
    Assign(Vec<expr>, expr),

    //AugAssign(expr target, operator op, expr value)
    AugAssign(expr, operator, expr),

    // For(expr target, expr iter, stmt* body, stmt* orelse)
    // AsyncFor(expr target, expr iter, stmt* body, stmt* orelse)
    // If the last field is true, then it's an AsyncFor.
    For(expr, expr, Vec<stmt>, Vec<stmt>, bool),

    // While(expr test, stmt* body, stmt* orelse)
    While(expr, Vec<stmt>, Vec<stmt>),

    // If(expr test, stmt* body, stmt* orelse)
    If(expr, Vec<stmt>, Vec<stmt>),

    // With(withitem* items, stmt* body)
    With(Vec<withitem>, Vec<stmt>),

    // AsyncWith(withitem* items, stmt* body)
    //AsyncWith(Vec<withitem>, Vec<stmt>)

    // Raise(expr? exc, expr? cause)
    Raise(Option<expr>, Option<expr>),

    // Try(stmt* body, excepthandler* handlers, stmt* orelse, stmt* finalbody)
    Try(Vec<stmt>, Vec<excepthandler>, Vec<stmt>, Vec<stmt>),

    // Assert(expr test, expr? msg)
    Assert(expr, Option<expr>),

    // Import(alias* names)
    Import(Vec<alias>),

    // ImportFrom(identifier? module, alias* names, int? level)
    ImportFrom(String, Vec<alias>, Option<usize>),

    // Global(identifier* names)
    Global(Vec<String>),

    // Nonlocal(identifier* names)
    Nonlocal(Vec<String>),

    // expr(expr value)
    Expr(expr),

    // Pass | Break | Continue
    Pass,
    Break,
    Continue
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum expr {
    BoolOp(boolop, Vec<expr>),
    BinOp(Box<expr>, operator, Box<expr>),
    UnaryOp(unaryop, Box<expr>),
    //Lambda(arguments, Box<expr>)
    //IfExp(Box<expr>, Box<expr>, Box<expr>)
    //Dict(Vec<expr>, Vec<expr>)
    //Set(Vec<expr>)
    ListComp(Box<expr>, Vec<comprehension>),
    //SetComp(Box<expr>, Vec<comprehension>)
    DictComp(Box<expr>, Box<expr>, Vec<comprehension>),
    //GeneratorExp(Box<expr>, Vec<comprehension>)

    // the grammar constrains where yield expressions can occur
    //Await(expr value)
    //Yield(expr? value)
    //YieldFrom(expr value)

    // need sequences for compare to distinguish between
    // x < 4 < 3 and (x < 4) < 3
    Compare(Box<expr>, Vec<cmpop>, Vec<expr>),

    Call(Box<expr>, Vec<expr>, Vec<keyword>),
    Num(String),
    Str(String),
    //Bytes(String),
    NameConstant(String),
    Ellipsis,

    // the following expression can appear in assignment context
    Attribute(Box<expr>, String, expr_context),
    //Subscript(Box<expr>, slice, expr_context),
    //Starred(Box<expr>, expr_context),
    Name(String, expr_context),
    List(Vec<expr>, expr_context),
    Tuple(Vec<expr>, expr_context),
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum expr_context {
    Load,
    Store,
    Del,
    AugLoad,
    AugStore,
    Param
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum slice {
    Slice(Option<expr>, Option<expr>, Option<expr>),
    ExtSlice(Vec<slice>),
    Index(expr)
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum boolop {
    And,
    Or
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum operator {
    Add,
    Sub,
    Mult,
    MatMult,
    Div,
    Mod,
    Pow,
    LShift,
    RShift,
    BitOr,
    BitXor,
    BitAnd,
    FloorDiv
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum unaryop {
    Invert,
    Not,
    UAdd,
    USub
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum cmpop {
    Eq,
    NotEq,
    Lt,
    LtE,
    Gt,
    GtE,
    Is,
    IsNot,
    In,
    NotIn
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct comprehension {
    pub target: expr,
    pub iter: expr,
    pub ifs: Vec<expr>
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct excepthandler {
    pub type_: Option<expr>,
    pub name: Option<String>,
    pub body: Vec<stmt>,
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct arguments {
    pub args: Vec<arg>,
    pub vararg: Option<arg>,
    pub kwonlyargs: Vec<arg>,
    pub kw_defaults: Vec<Option<expr>>,
    pub kwarg: Option<arg>,
    pub defaults: Vec<expr>
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct arg {
    pub arg: String,
    pub annotation: Option<expr>
}

// keyword arguments supplied to call (NULL identifier for **kwargs)
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct keyword {
    pub arg: Option<String>,
    pub value: expr
}

// import name with optional 'as' alias.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct alias {
    pub name: String,
    pub asname: Option<String>
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct withitem {
    pub context_expr: expr,
    pub optional_vars: Option<expr>
}