changeset 74:97537c90d92d

Add ast.Yield.
author Bastien Orivel <eijebong@bananium.fr>
date Mon, 13 Jun 2016 18:25:02 +0200
parents 2a6629ea82b5
children 1abc8ca9f30b
files src/ast_convert.rs src/ast_dump.rs src/python_ast.rs tests/test_parse_files/test_yield.py
diffstat 4 files changed, 16 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/ast_convert.rs
+++ b/src/ast_convert.rs
@@ -302,6 +302,7 @@ fn parse_expr(py: Python, ast: PyObject)
     let tuple_type = ast_module.get(py, "Tuple").unwrap();
     let ellipsis_type = ast_module.get(py, "Ellipsis").unwrap();
     let await_type = ast_module.get(py, "Await").unwrap();
+    let yield_type = ast_module.get(py, "Yield").unwrap();
 
     assert!(is_instance(&ast, &ast_type));
 
@@ -408,6 +409,11 @@ fn parse_expr(py: Python, ast: PyObject)
         let value = parse_expr(py, value);
 
         expr::Await(Box::new(value))
+    } else if is_instance(&ast, &yield_type) {
+        let value = ast.getattr(py, "value").unwrap();
+        let value = parse_optional_expr(py, value);
+
+        expr::Yield(Box::new(value))
     } else {
         println!("expr {}", ast);
         unreachable!()
--- a/src/ast_dump.rs
+++ b/src/ast_dump.rs
@@ -187,6 +187,12 @@ impl to_string_able for expr {
             expr::Tuple(elts, _) => format!("({})", vec_to_strings_vec(elts).join(", ")),
             expr::Ellipsis => format!("..."),
             expr::Await(value) => format!("await {}", value.to_string()),
+            expr::Yield(value) => {
+                match *value {
+                    None => String::from("yield"),
+                    Some(value) => format!("yield {}", value.to_string())
+                }
+            },
         }
     }
 }
--- a/src/python_ast.rs
+++ b/src/python_ast.rs
@@ -92,7 +92,7 @@ pub enum expr {
 
     // the grammar constrains where yield expressions can occur
     Await(Box<expr>),
-    //Yield(expr? value)
+    Yield(Box<Option<expr>>),
     //YieldFrom(expr value)
 
     // need sequences for compare to distinguish between
new file mode 100644
--- /dev/null
+++ b/tests/test_parse_files/test_yield.py
@@ -0,0 +1,3 @@
+def a():
+    yield
+    yield b()