Accept new for-from syntax.
authorRobert Bradshaw <robertwb@math.washington.edu>
Wed, 28 May 2008 08:58:32 +0000 (01:58 -0700)
committerRobert Bradshaw <robertwb@math.washington.edu>
Wed, 28 May 2008 08:58:32 +0000 (01:58 -0700)
Cython/Compiler/Parsing.py
tests/compile/forfromelse.pyx

index 4b4c9107970c0141ad0d69503a0e2dc293ff9646..b7b2a88cd7bcdb34ccce2c35f3b46c67b823d686 100644 (file)
@@ -1080,9 +1080,13 @@ def p_for_bounds(s):
         s.next()
         iterator = p_for_iterator(s)
         return { 'target': target, 'iterator': iterator }
-    elif s.sy == 'from':
-        s.next()
-        bound1 = p_bit_expr(s)
+    else:
+        if s.sy == 'from':
+            s.next()
+            bound1 = p_bit_expr(s)
+        else:
+            # Support shorter "for a <= x < b" syntax
+            bound1, target = target, None
         rel1 = p_for_from_relation(s)
         name2_pos = s.position()
         name2 = p_ident(s)
@@ -1090,12 +1094,15 @@ def p_for_bounds(s):
         rel2 = p_for_from_relation(s)
         bound2 = p_bit_expr(s)
         step = p_for_from_step(s)
-        if not target.is_name:
-            error(target.pos, 
-                "Target of for-from statement must be a variable name")
-        elif name2 != target.name:
-            error(name2_pos,
-                "Variable name in for-from range does not match target")
+        if target is None:
+            target = ExprNodes.NameNode(name2_pos, name = name2)
+        else:
+            if not target.is_name:
+                error(target.pos, 
+                    "Target of for-from statement must be a variable name")
+            elif name2 != target.name:
+                error(name2_pos,
+                    "Variable name in for-from range does not match target")
         if rel1[0] != rel2[0]:
             error(rel2_pos,
                 "Relation directions in for-from do not match")
@@ -1105,8 +1112,6 @@ def p_for_bounds(s):
                 'relation2': rel2,
                 'bound2': bound2,
                 'step': step }
-    else:
-        s.error("Expected 'in' or 'from'")
 
 def p_for_from_relation(s):
     if s.sy in inequality_relations:
index 014f42b54c83a12f022a1af8515f815a7f6aa8cf..11cad88b3aa46c8adfc8135f400252b46d6970a6 100644 (file)
@@ -5,3 +5,8 @@ cdef void spam():
     else:
         k = j
 
+    # new syntax
+    for 0 <= i < 10:
+        j = i
+    else:
+        j = k