comparison src/ast_convert.rs @ 91:859d44f143b8

Implement Bytes in the AST.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 25 Jun 2016 02:08:50 +0100
parents 5923cd4bfc36
children
comparison
equal deleted inserted replaced
90:4e62a8927dcc 91:859d44f143b8
1 use python_ast::{Module, stmt, expr, expr_context, cmpop, boolop, operator, unaryop, arguments, arg, alias, comprehension, keyword, withitem, excepthandler, slice, name_constant}; 1 use python_ast::{Module, stmt, expr, expr_context, cmpop, boolop, operator, unaryop, arguments, arg, alias, comprehension, keyword, withitem, excepthandler, slice, name_constant};
2 2
3 use cpython::{Python, PyObject, PyBool, PyResult}; 3 use cpython::{Python, PyObject, PyBool, PyResult, PyBytes};
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 {
7 object.extract(py).unwrap() 7 object.extract(py).unwrap()
8 } 8 }
336 let name_constant_type = ast_module.get(py, "NameConstant").unwrap(); 336 let name_constant_type = ast_module.get(py, "NameConstant").unwrap();
337 let attribute_type = ast_module.get(py, "Attribute").unwrap(); 337 let attribute_type = ast_module.get(py, "Attribute").unwrap();
338 let name_type = ast_module.get(py, "Name").unwrap(); 338 let name_type = ast_module.get(py, "Name").unwrap();
339 let num_type = ast_module.get(py, "Num").unwrap(); 339 let num_type = ast_module.get(py, "Num").unwrap();
340 let str_type = ast_module.get(py, "Str").unwrap(); 340 let str_type = ast_module.get(py, "Str").unwrap();
341 let bytes_type = ast_module.get(py, "Bytes").unwrap();
341 let list_type = ast_module.get(py, "List").unwrap(); 342 let list_type = ast_module.get(py, "List").unwrap();
342 let compare_type = ast_module.get(py, "Compare").unwrap(); 343 let compare_type = ast_module.get(py, "Compare").unwrap();
343 let call_type = ast_module.get(py, "Call").unwrap(); 344 let call_type = ast_module.get(py, "Call").unwrap();
344 let listcomp_type = ast_module.get(py, "ListComp").unwrap(); 345 let listcomp_type = ast_module.get(py, "ListComp").unwrap();
345 let dictcomp_type = ast_module.get(py, "DictComp").unwrap(); 346 let dictcomp_type = ast_module.get(py, "DictComp").unwrap();
394 expr::Num(n) 395 expr::Num(n)
395 } else if is_instance(&ast, &str_type) { 396 } else if is_instance(&ast, &str_type) {
396 let s = ast.getattr(py, "s").unwrap(); 397 let s = ast.getattr(py, "s").unwrap();
397 let s = get_str(py, s); 398 let s = get_str(py, s);
398 expr::Str(s) 399 expr::Str(s)
400 } else if is_instance(&ast, &bytes_type) {
401 let s = ast.getattr(py, "s").unwrap();
402 let s: PyBytes = s.extract(py).unwrap();
403 let s = s.as_slice(py).to_vec();
404 expr::Bytes(s)
399 } else if is_instance(&ast, &list_type) { 405 } else if is_instance(&ast, &list_type) {
400 let elts = ast.getattr(py, "elts").unwrap(); 406 let elts = ast.getattr(py, "elts").unwrap();
401 let elements = parse_list(py, elts, parse_expr); 407 let elements = parse_list(py, elts, parse_expr);
402 expr::List(elements, get_ctx(py, ast)) 408 expr::List(elements, get_ctx(py, ast))
403 } else if is_instance(&ast, &unary_op_type) { 409 } else if is_instance(&ast, &unary_op_type) {