Mercurial > python-compiler.rs
diff src/python_ast.rs @ 13:38b0d63697b1
Import the full AST grammar from CPython 3.5.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Thu, 02 Jun 2016 05:33:23 +0100 |
parents | fa7e285f88e7 |
children | 719a27f1c1c7 |
line wrap: on
line diff
--- a/src/python_ast.rs +++ b/src/python_ast.rs @@ -1,57 +1,225 @@ #[derive(Clone, Debug)] pub struct Module { pub name: String, - pub statements: Vec<Statement>, + pub statements: Vec<stmt>, } -#[derive(Clone, Debug)] -pub enum Statement { - ClassDef(String, Vec<Expr>, Vec<Statement>), - FunctionDef(String, Vec<String>, Vec<Statement>), +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub enum stmt { + // FunctionDef(identifier name, arguments args, stmt* body, expr* decorator_list, expr? returns) + FunctionDef(String, arguments, Vec<stmt>, Vec<expr>, Option<expr>), + + // AsyncFunctionDef(identifier name, arguments args, stmt* body, expr* decorator_list, expr? returns) + //AsyncFunctionDef(String/*, arguments, Vec<expr>), + + // 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) + For(expr, expr, Vec<stmt>, Vec<stmt>), + + // AsyncFor(expr target, expr iter, stmt* body, stmt* orelse) + //AsyncFor(expr, expr, Vec<stmt>, Vec<stmt>), + + // 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<i32>), // TODO: check the size of level. + + // Global(identifier* names) Global(Vec<String>), - If(Expr, Vec<Statement>, Vec<Statement>), - For(Expr, Expr, Vec<Statement>, Vec<Statement>), - While(Expr, Vec<Statement>, Vec<Statement>), - Assign(Vec<Expr>, Expr), - AugAssign(Expr, BinOp, Expr), - Return(Expr), - ImportFrom(String, Vec<Expr>), - Expr(Expr), + + // Nonlocal(identifier* names) + //Nonlocal(Vec<String>), + + // expr(expr value) + Expr(expr), + + // Pass | Break | Continue + //Pass, Break, - Error + //Continue } #[derive(Clone, Debug, PartialEq, Eq, Hash)] -pub enum Expr { - UnaryOp(UnaryOp, Box<Expr>), - BinOp(Box<Expr>, BinOp, Box<Expr>), - Compare(Box<Expr>, Vec<BinOp>, Vec<Expr>), - Call(Box<Expr>, Vec<Expr>), - Alias(String, Option<String>), - Attribute(Box<Expr>, String), - Name(String), +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), - Str(String), - Num(String), - List(Vec<Expr>), - Error + //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 UnaryOp { +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, - Error + 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 enum BinOp { - BinAdd, - BinMult, - BinEq, - BinLt, - BinGt, - Sub, - Div, - Error +pub struct comprehension { + pub target: expr, + pub iter: expr, + pub ifs: Vec<expr> +} + +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub enum excepthandler { + ExceptHandler(Option<expr>, Option<String>, 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<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> +}