comparison src/ast_dump.rs @ 14:719a27f1c1c7

Add ast.ListComp
author Bastien Orivel <eijebong@bananium.fr>
date Thu, 02 Jun 2016 20:40:24 +0200
parents 38b0d63697b1
children b21a246349f3
comparison
equal deleted inserted replaced
13:38b0d63697b1 14:719a27f1c1c7
1 use python_ast::{Module, stmt, expr, boolop, operator, unaryop, cmpop, arguments, arg, keyword}; 1 use python_ast::{Module, stmt, expr, boolop, operator, unaryop, cmpop, arguments, arg, keyword, comprehension};
2 2
3 use std::iter; 3 use std::iter;
4 4
5 impl boolop { 5 impl boolop {
6 fn to_string(&self) -> &'static str { 6 fn to_string(&self) -> &'static str {
101 } else { 101 } else {
102 format!("{}\n{}else:\n{}", body, make_indent(indent), statements_to_string(indent, orelse)) 102 format!("{}\n{}else:\n{}", body, make_indent(indent), statements_to_string(indent, orelse))
103 } 103 }
104 } 104 }
105 105
106 fn generators_to_string(generators: Vec<comprehension>) -> String {
107 let mut result = vec!();
108 for generator in generators {
109 result.push(format!("for {} in {}", generator.target.to_string(), generator.iter.to_string()));
110 for if_ in generator.ifs {
111 result.push(format!("if {}", if_.to_string()))
112 }
113 }
114 result.join(" ")
115 }
106 impl expr { 116 impl expr {
107 fn to_string(&self) -> String { 117 fn to_string(&self) -> String {
108 match self.clone() { 118 match self.clone() {
109 expr::BoolOp(op, values) => format!("{}({})", op.to_string(), args_to_string(values)), 119 expr::BoolOp(op, values) => format!("{}({})", op.to_string(), args_to_string(values)),
110 expr::BinOp(a, op, b) => format!("{} {} {}", a.to_string(), op.to_string(), b.to_string()), 120 expr::BinOp(a, op, b) => format!("{} {} {}", a.to_string(), op.to_string(), b.to_string()),
134 expr::Num(n) => format!("{}", n), 144 expr::Num(n) => format!("{}", n),
135 expr::Str(s) => format!("\"{}\"", s), 145 expr::Str(s) => format!("\"{}\"", s),
136 expr::NameConstant(name) => format!("{}", name), 146 expr::NameConstant(name) => format!("{}", name),
137 expr::Attribute(value, attr, ctx) => format!("{}.{}", value.to_string(), attr), 147 expr::Attribute(value, attr, ctx) => format!("{}.{}", value.to_string(), attr),
138 expr::Name(name, ctx) => format!("{}", name), 148 expr::Name(name, ctx) => format!("{}", name),
139 expr::List(elts, ctx) => format!("[{}]", args_to_string(elts)) 149 expr::List(elts, ctx) => format!("[{}]", args_to_string(elts)),
150 expr::ListComp(elt, generators) => format!("[{} {}]", elt.to_string(), generators_to_string(generators))
140 } 151 }
141 } 152 }
142 } 153 }
143 154
144 impl arguments { 155 impl arguments {