changeset 71:2c1e2ce41263

Add ast.AsyncFor.
author Bastien Orivel <eijebong@bananium.fr>
date Mon, 13 Jun 2016 18:02:34 +0200
parents c9b9165abb9e
children 7ac23f4336b1
files src/ast_convert.rs src/ast_dump.rs src/python_ast.rs tests/test_parse_files/test_async_for.py
diffstat 4 files changed, 19 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/ast_convert.rs
+++ b/src/ast_convert.rs
@@ -536,6 +536,7 @@ fn parse_statement(py: Python, ast: PyOb
     let if_type = ast_module.get(py, "If").unwrap();
     let while_type = ast_module.get(py, "While").unwrap();
     let for_type = ast_module.get(py, "For").unwrap();
+    let async_for_type = ast_module.get(py, "AsyncFor").unwrap();
     let expr_type = ast_module.get(py, "Expr").unwrap();
     let break_type = ast_module.get(py, "Break").unwrap();
     let delete_type = ast_module.get(py, "Delete").unwrap();
@@ -618,7 +619,7 @@ fn parse_statement(py: Python, ast: PyOb
         let orelse = parse_list(py, orelse, parse_statement);
 
         stmt::While(test, body, orelse)
-    } else if is_instance(&ast, &for_type) {
+    } else if is_instance(&ast, &for_type) || is_instance(&ast, &async_for_type) {
         let target = ast.getattr(py, "target").unwrap();
         let iter = ast.getattr(py, "iter").unwrap();
         let body = ast.getattr(py, "body").unwrap();
@@ -629,7 +630,7 @@ fn parse_statement(py: Python, ast: PyOb
         let body = parse_list(py, body, parse_statement);
         let orelse = parse_list(py, orelse, parse_statement);
 
-        stmt::For(target, iter, body, orelse)
+        stmt::For(target, iter, body, orelse, is_instance(&ast, &async_for_type))
     } else if is_instance(&ast, &assign_type) {
         let targets = ast.getattr(py, "targets").unwrap();
         let value = ast.getattr(py, "value").unwrap();
--- a/src/ast_dump.rs
+++ b/src/ast_dump.rs
@@ -290,7 +290,15 @@ impl stmt {
             stmt::Nonlocal(names) => format!("{}nonlocal {}", current_indent, names.join(", ")),
             stmt::If(test, body, orelse) => format!("{}if {}:\n{}", current_indent, test.to_string(), if_else_statements_to_string(indent, body, orelse)),
             stmt::While(test, body, orelse) => format!("{}while {}:\n{}", current_indent, test.to_string(), if_else_statements_to_string(indent, body, orelse)),
-            stmt::For(target, iter, body, orelse) => format!("{}for {} in {}:\n{}", current_indent, target.to_string(), iter.to_string(), if_else_statements_to_string(indent, body, orelse)),
+            stmt::For(target, iter, body, orelse, async) => {
+                format!("{}{}for {} in {}:\n{}",
+                    current_indent,
+                    if async { "async " } else { "" },
+                    target.to_string(),
+                    iter.to_string(),
+                    if_else_statements_to_string(indent, body, orelse)
+                )
+            },
             stmt::Assign(targets, value) => format!("{}{} = {}", current_indent, vec_to_strings_vec(targets).join(", "), value.to_string()),
             stmt::AugAssign(target, op, value) => format!("{}{} {}= {}", current_indent, target.to_string(), op.to_string(), value.to_string()),
             stmt::Return(expr) => format!("{}return{}", current_indent, match expr {
--- a/src/python_ast.rs
+++ b/src/python_ast.rs
@@ -29,10 +29,9 @@ pub enum stmt {
     AugAssign(expr, operator, expr),
 
     // For(expr target, expr iter, stmt* body, stmt* orelse)
-    For(expr, expr, Vec<stmt>, Vec<stmt>),
-
     // AsyncFor(expr target, expr iter, stmt* body, stmt* orelse)
-    //AsyncFor(expr, expr, Vec<stmt>, Vec<stmt>),
+    // If the last field is true, then it's an AsyncFor.
+    For(expr, expr, Vec<stmt>, Vec<stmt>, bool),
 
     // While(expr test, stmt* body, stmt* orelse)
     While(expr, Vec<stmt>, Vec<stmt>),
new file mode 100644
--- /dev/null
+++ b/tests/test_parse_files/test_async_for.py
@@ -0,0 +1,5 @@
+async def a():
+    async for i in range(5):
+        print(i)
+    else:
+        print("he")