# HG changeset patch # User Bastien Orivel # Date 1465843519 -7200 # Node ID dc82a0d8f144172d4878d95526a883afe0e5a1dd # Parent c6d3f0dabbba57f47d23c1bea5f7aa6f7dba132a Add ast.Dict. diff --git a/src/ast_convert.rs b/src/ast_convert.rs --- a/src/ast_convert.rs +++ b/src/ast_convert.rs @@ -309,6 +309,7 @@ fn parse_expr(py: Python, ast: PyObject) let generatorexp_type = ast_module.get(py, "GeneratorExp").unwrap(); let lambda_type = ast_module.get(py, "Lambda").unwrap(); let ifexp_type = ast_module.get(py, "IfExp").unwrap(); + let dict_type = ast_module.get(py, "Dict").unwrap(); assert!(is_instance(&ast, &ast_type)); @@ -464,6 +465,14 @@ fn parse_expr(py: Python, ast: PyObject) let orelse = parse_expr(py, orelse); expr::IfExp(Box::new(test), Box::new(body), Box::new(orelse)) + } else if is_instance(&ast, &dict_type) { + let keys = ast.getattr(py, "keys").unwrap(); + let values = ast.getattr(py, "values").unwrap(); + + let keys = parse_list(py, keys, parse_optional_expr); + let values = parse_list(py, values, parse_expr); + + expr::Dict(keys, values) } else { println!("expr {}", ast); unreachable!() diff --git a/src/ast_dump.rs b/src/ast_dump.rs --- a/src/ast_dump.rs +++ b/src/ast_dump.rs @@ -199,6 +199,21 @@ impl to_string_able for expr { expr::GeneratorExp(elt, generators) => format!("({} {})", elt.to_string(), vec_to_strings_vec(generators).join(" ")), expr::Lambda(args, body) => format!("lambda {}: {}", args.to_string(), body.to_string()), expr::IfExp(test, body, orelse) => format!("{} if {} else {}", body.to_string(), test.to_string(), orelse.to_string()), + expr::Dict(keys, values) => { + format!("{{{}}}", + { + let mut items = vec!(); + for (key, value) in keys.iter().zip(values.iter()) { + let item = match *key { + None => format!("**{}", value.to_string()), + Some(ref key) => format!("{}: {}", key.to_string(), value.to_string()), + }; + items.push(item); + } + items.join(", ") + } + ) + }, } } } diff --git a/src/python_ast.rs b/src/python_ast.rs --- a/src/python_ast.rs +++ b/src/python_ast.rs @@ -83,7 +83,7 @@ pub enum expr { UnaryOp(unaryop, Box), Lambda(Box, Box), IfExp(Box, Box, Box), - //Dict(Vec, Vec) + Dict(Vec>, Vec), Set(Vec), ListComp(Box, Vec), SetComp(Box, Vec), diff --git a/tests/test_parse_files/test_dict.py b/tests/test_parse_files/test_dict.py new file mode 100644 --- /dev/null +++ b/tests/test_parse_files/test_dict.py @@ -0,0 +1,2 @@ +a = {"key": "value", "key2": 4} +a = {"key": None, **d}