comparison src/ast_convert.rs @ 82:2d906d1cb940

Add ast.Subscript.
author Bastien Orivel <eijebong@bananium.fr>
date Mon, 13 Jun 2016 21:32:43 +0200
parents dc82a0d8f144
children e59cd5754268
comparison
equal deleted inserted replaced
81:dc82a0d8f144 82:2d906d1cb940
1 use python_ast::{Module, stmt, expr, expr_context, cmpop, boolop, operator, unaryop, arguments, arg, alias, comprehension, keyword, withitem, excepthandler}; 1 use python_ast::{Module, stmt, expr, expr_context, cmpop, boolop, operator, unaryop, arguments, arg, alias, comprehension, keyword, withitem, excepthandler, slice};
2 2
3 use cpython::{Python, PyObject}; 3 use cpython::{Python, PyObject};
4 use cpython::ObjectProtocol; //for call method 4 use cpython::ObjectProtocol; //for call method
5 5
6 fn get_str(py: Python, object: PyObject) -> String { 6 fn get_str(py: Python, object: PyObject) -> String {
265 let body = parse_list(py, body, parse_statement); 265 let body = parse_list(py, body, parse_statement);
266 266
267 excepthandler{type_: type_, name: name, body: body} 267 excepthandler{type_: type_, name: name, body: body}
268 } 268 }
269 269
270 fn parse_slice(py: Python, ast: PyObject) -> slice {
271 let builtins_module = py.import("builtins").unwrap();
272 let isinstance = builtins_module.get(py, "isinstance").unwrap();
273
274 let is_instance = |object: &PyObject, type_: &PyObject| {
275 return isinstance.call(py, (object, type_), None).unwrap().is_true(py).unwrap();
276 };
277
278 let ast_module = py.import("ast").unwrap();
279 let ast_type = ast_module.get(py, "AST").unwrap();
280 let index_type = ast_module.get(py, "Index").unwrap();
281 let slice_type = ast_module.get(py, "Slice").unwrap();
282 let ext_slice_type = ast_module.get(py, "ExtSlice").unwrap();
283
284 assert!(is_instance(&ast, &ast_type));
285
286 if is_instance(&ast, &index_type) {
287 let value = ast.getattr(py, "value").unwrap();
288 let value = parse_expr(py, value);
289
290 slice::Index(value)
291 } else if is_instance(&ast, &slice_type) {
292 let lower = ast.getattr(py, "lower").unwrap();
293 let upper = ast.getattr(py, "upper").unwrap();
294 let step = ast.getattr(py, "step").unwrap();
295
296 let lower = parse_optional_expr(py, lower);
297 let upper = parse_optional_expr(py, upper);
298 let step = parse_optional_expr(py, step);
299 slice::Slice(lower, upper, step)
300 } else if is_instance(&ast, &ext_slice_type) {
301 let dims = ast.getattr(py, "dims").unwrap();
302 let dims = parse_list(py, dims, parse_slice);
303
304 slice::ExtSlice(dims)
305 } else {
306 unreachable!()
307 }
308 }
309
270 fn parse_optional_expr(py: Python, ast: PyObject) -> Option<expr> { 310 fn parse_optional_expr(py: Python, ast: PyObject) -> Option<expr> {
271 if ast == py.None() { 311 if ast == py.None() {
272 None 312 None
273 } else { 313 } else {
274 let ast = parse_expr(py, ast); 314 let ast = parse_expr(py, ast);
308 let setcomp_type = ast_module.get(py, "SetComp").unwrap(); 348 let setcomp_type = ast_module.get(py, "SetComp").unwrap();
309 let generatorexp_type = ast_module.get(py, "GeneratorExp").unwrap(); 349 let generatorexp_type = ast_module.get(py, "GeneratorExp").unwrap();
310 let lambda_type = ast_module.get(py, "Lambda").unwrap(); 350 let lambda_type = ast_module.get(py, "Lambda").unwrap();
311 let ifexp_type = ast_module.get(py, "IfExp").unwrap(); 351 let ifexp_type = ast_module.get(py, "IfExp").unwrap();
312 let dict_type = ast_module.get(py, "Dict").unwrap(); 352 let dict_type = ast_module.get(py, "Dict").unwrap();
353 let subscript_type = ast_module.get(py, "Subscript").unwrap();
313 354
314 assert!(is_instance(&ast, &ast_type)); 355 assert!(is_instance(&ast, &ast_type));
315 356
316 if is_instance(&ast, &attribute_type) { 357 if is_instance(&ast, &attribute_type) {
317 let value = ast.getattr(py, "value").unwrap(); 358 let value = ast.getattr(py, "value").unwrap();
471 512
472 let keys = parse_list(py, keys, parse_optional_expr); 513 let keys = parse_list(py, keys, parse_optional_expr);
473 let values = parse_list(py, values, parse_expr); 514 let values = parse_list(py, values, parse_expr);
474 515
475 expr::Dict(keys, values) 516 expr::Dict(keys, values)
517 } else if is_instance(&ast, &subscript_type) {
518 let value = ast.getattr(py, "value").unwrap();
519 let slice = ast.getattr(py, "slice").unwrap();
520 let ctx = get_ctx(py, ast);
521
522 let value = parse_expr(py, value);
523 let slice = parse_slice(py, slice);
524
525 expr::Subscript(Box::new(value), Box::new(slice), ctx)
476 } else { 526 } else {
477 println!("expr {}", ast); 527 println!("expr {}", ast);
478 unreachable!() 528 unreachable!()
479 } 529 }
480 } 530 }