Mercurial > python-compiler.rs
changeset 60:9f0f457a67f6
Add ast.Raise.
author | Bastien Orivel <eijebong@bananium.fr> |
---|---|
date | Mon, 13 Jun 2016 01:24:09 +0200 |
parents | 407b3b217928 |
children | 6b73843c5b4a |
files | src/ast_convert.rs src/ast_dump.rs src/python_ast.rs tests/test_parse_files/test_raise.py |
diffstat | 4 files changed, 33 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/src/ast_convert.rs +++ b/src/ast_convert.rs @@ -528,6 +528,7 @@ fn parse_statement(py: Python, ast: PyOb let continue_type = ast_module.get(py, "Continue").unwrap(); let assert_type = ast_module.get(py, "Assert").unwrap(); let with_type = ast_module.get(py, "With").unwrap(); + let raise_type = ast_module.get(py, "Raise").unwrap(); assert!(is_instance(&ast, &ast_type)); @@ -687,6 +688,21 @@ fn parse_statement(py: Python, ast: PyOb let items = parse_list(py, items, parse_withitem); let body = parse_list(py, body, parse_statement); stmt::With(items, body) + } else if is_instance(&ast, &raise_type) { + let exc = ast.getattr(py, "exc").unwrap(); + let cause = ast.getattr(py, "cause").unwrap(); + let mut exc_ = None; + let mut cause_ = None; + + if exc != py.None() { + exc_ = Some(parse_expr(py, exc)); + } + + if cause != py.None() { + cause_ = Some(parse_expr(py, cause)); + } + + stmt::Raise(exc_, cause_) } else { println!("stmt {}", ast); panic!()
--- a/src/ast_dump.rs +++ b/src/ast_dump.rs @@ -296,6 +296,19 @@ impl stmt { }, statements_to_string(indent, body) ) + }, + stmt::Raise(exc, cause) => { + format!("{}raise{}{}", + current_indent, + match exc { + None => String::new(), + Some(ref exc) => format!(" {}", exc.to_string()) + }, + match cause { + None => String::new(), + Some(ref cause) => format!(" from {}", cause.to_string()) + } + ) } } }
--- a/src/python_ast.rs +++ b/src/python_ast.rs @@ -46,7 +46,7 @@ pub enum stmt { //AsyncWith(Vec<withitem>, Vec<stmt>) // Raise(expr? exc, expr? cause) - //Raise(Option<expr>, Option<expr>) + Raise(Option<expr>, Option<expr>), // Try(stmt* body, excepthandler* handlers, stmt* orelse, stmt* finalbody) //Try(Vec<stmt>, Vec<excepthandler>, Vec<stmt>, Vec<stmt>)