Mercurial > python-compiler.rs
diff src/ast_dump.rs @ 68:c59ad5ccd8a6
Add ast.Try
author | Bastien Orivel <eijebong@bananium.fr> |
---|---|
date | Mon, 13 Jun 2016 03:03:13 +0200 |
parents | 8ce78e2ba48c |
children | a73eaf42bea1 |
line wrap: on
line diff
--- a/src/ast_dump.rs +++ b/src/ast_dump.rs @@ -1,4 +1,4 @@ -use python_ast::{Module, stmt, expr, boolop, operator, unaryop, cmpop, arguments, arg, keyword, comprehension, withitem}; +use python_ast::{Module, stmt, expr, boolop, operator, unaryop, cmpop, arguments, arg, keyword, comprehension, withitem, excepthandler}; use std::iter; @@ -117,6 +117,24 @@ impl to_string_able for comprehension { } } +impl excepthandler { + fn to_string(self, indent: usize) -> String { + let current_indent = make_indent(indent); + format!("{}except {}{}:\n{}", + current_indent, + match self.type_ { + None => String::new(), + Some(ref type_) => type_.to_string() + }, + match self.name { + None => String::new(), + Some(ref name) => format!(" as {}", name) + }, + statements_to_string(indent, self.body) + ) + } +} + impl to_string_able for expr { fn to_string(&self) -> String { match self.clone() { @@ -346,6 +364,30 @@ impl stmt { Some(ref cause) => format!(" from {}", cause.to_string()) } ) + }, + stmt::Try(body, excepthandlers, orelse, finalbody) => { + format!("{}try:\n{}\n{}{}\n{}", + current_indent, + statements_to_string(indent, body), + { + let mut excepthandlers_ = vec!(); + for excepthandler in excepthandlers { + let excepthandler = excepthandler.to_string(indent); + excepthandlers_.push(excepthandler); + } + excepthandlers_.join("\n") + }, + if !orelse.is_empty() { + format!("{}else:\n{}", current_indent, statements_to_string(indent, orelse)) + } else { + String::new() + }, + if !finalbody.is_empty() { + format!("{}finally:\n{}", current_indent, statements_to_string(indent, finalbody)) + } else { + String::new() + } + ) } } }