comparison 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
comparison
equal deleted inserted replaced
67:8ce78e2ba48c 68:c59ad5ccd8a6
1 use python_ast::{Module, stmt, expr, boolop, operator, unaryop, cmpop, arguments, arg, keyword, comprehension, withitem}; 1 use python_ast::{Module, stmt, expr, boolop, operator, unaryop, cmpop, arguments, arg, keyword, comprehension, withitem, excepthandler};
2 2
3 use std::iter; 3 use std::iter;
4 4
5 trait to_string_able { 5 trait to_string_able {
6 fn to_string(&self) -> String; 6 fn to_string(&self) -> String;
112 result.push(format!("for {} in {}", self.target.to_string(), self.iter.to_string())); 112 result.push(format!("for {} in {}", self.target.to_string(), self.iter.to_string()));
113 for if_ in self.ifs.clone() { 113 for if_ in self.ifs.clone() {
114 result.push(format!("if {}", if_.to_string())) 114 result.push(format!("if {}", if_.to_string()))
115 } 115 }
116 result.join(" ") 116 result.join(" ")
117 }
118 }
119
120 impl excepthandler {
121 fn to_string(self, indent: usize) -> String {
122 let current_indent = make_indent(indent);
123 format!("{}except {}{}:\n{}",
124 current_indent,
125 match self.type_ {
126 None => String::new(),
127 Some(ref type_) => type_.to_string()
128 },
129 match self.name {
130 None => String::new(),
131 Some(ref name) => format!(" as {}", name)
132 },
133 statements_to_string(indent, self.body)
134 )
117 } 135 }
118 } 136 }
119 137
120 impl to_string_able for expr { 138 impl to_string_able for expr {
121 fn to_string(&self) -> String { 139 fn to_string(&self) -> String {
344 match cause { 362 match cause {
345 None => String::new(), 363 None => String::new(),
346 Some(ref cause) => format!(" from {}", cause.to_string()) 364 Some(ref cause) => format!(" from {}", cause.to_string())
347 } 365 }
348 ) 366 )
367 },
368 stmt::Try(body, excepthandlers, orelse, finalbody) => {
369 format!("{}try:\n{}\n{}{}\n{}",
370 current_indent,
371 statements_to_string(indent, body),
372 {
373 let mut excepthandlers_ = vec!();
374 for excepthandler in excepthandlers {
375 let excepthandler = excepthandler.to_string(indent);
376 excepthandlers_.push(excepthandler);
377 }
378 excepthandlers_.join("\n")
379 },
380 if !orelse.is_empty() {
381 format!("{}else:\n{}", current_indent, statements_to_string(indent, orelse))
382 } else {
383 String::new()
384 },
385 if !finalbody.is_empty() {
386 format!("{}finally:\n{}", current_indent, statements_to_string(indent, finalbody))
387 } else {
388 String::new()
389 }
390 )
349 } 391 }
350 } 392 }
351 } 393 }
352 } 394 }
353 395