Mercurial > python-compiler.rs
changeset 47:38f59b0efc2c
Handle Import and add a test for it.
author | Bastien Orivel <eijebong@bananium.fr> |
---|---|
date | Tue, 07 Jun 2016 12:51:36 +0200 |
parents | b3f47c8150c0 |
children | 039f85b187f2 |
files | src/ast_convert.rs src/ast_dump.rs src/python_ast.rs tests/test_parse_files/test_import.py |
diffstat | 4 files changed, 18 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/src/ast_convert.rs +++ b/src/ast_convert.rs @@ -434,6 +434,7 @@ fn parse_statement(py: Python, ast: PyOb let aug_assign_type = ast_module.get(py, "AugAssign").unwrap(); let return_type = ast_module.get(py, "Return").unwrap(); let import_from_type = ast_module.get(py, "ImportFrom").unwrap(); + let import_type = ast_module.get(py, "Import").unwrap(); let if_type = ast_module.get(py, "If").unwrap(); let while_type = ast_module.get(py, "While").unwrap(); let for_type = ast_module.get(py, "For").unwrap(); @@ -545,6 +546,11 @@ fn parse_statement(py: Python, ast: PyOb let names = parse_list(py, names, parse_alias); stmt::ImportFrom(module, names, None) + } else if is_instance(&ast, &import_type) { + let names = ast.getattr(py, "names").unwrap(); + let names = parse_list(py, names, parse_alias); + + stmt::Import(names) } else if is_instance(&ast, &return_type) { let value = ast.getattr(py, "value").unwrap(); if value == py.None() {
--- a/src/ast_dump.rs +++ b/src/ast_dump.rs @@ -200,6 +200,16 @@ impl stmt { } names_.join(", ") }), + stmt::Import(names) => format!("{}import {}", current_indent, { + let mut names_ = vec!(); + for name in names { + match name.asname { + None => names_.push(name.name), + Some(asname) => names_.push(format!("{} as {}", name.name, asname)) + } + } + names_.join(", ") + }), stmt::Expr(expr) => format!("{}{}", current_indent, expr.to_string()), stmt::Break => format!("{}break", current_indent), stmt::Delete(targets) => format!("{}del {}", current_indent, args_to_string(targets))
--- a/src/python_ast.rs +++ b/src/python_ast.rs @@ -55,7 +55,7 @@ pub enum stmt { //Assert(expr, Option<expr>) // Import(alias* names) - //Import(Vec<alias>) + Import(Vec<alias>), // ImportFrom(identifier? module, alias* names, int? level) ImportFrom(String, Vec<alias>, Option<i32>), // TODO: check the size of level.