More Python 3 support.

--HG--
branch : trunk
diff --git a/jinja2/parser.py b/jinja2/parser.py
index f3de6e7..c94afb4 100644
--- a/jinja2/parser.py
+++ b/jinja2/parser.py
@@ -10,6 +10,7 @@
 """
 from jinja2 import nodes
 from jinja2.exceptions import TemplateSyntaxError, TemplateAssertionError
+from jinja2.utils import next
 
 
 _statement_keywords = frozenset(['for', 'if', 'block', 'extends', 'print',
@@ -95,12 +96,12 @@
         result = self.subparse(end_tokens)
 
         if drop_needle:
-            self.stream.next()
+            next(self.stream)
         return result
 
     def parse_set(self):
         """Parse an assign statement."""
-        lineno = self.stream.next().lineno
+        lineno = next(self.stream).lineno
         target = self.parse_assign_target()
         self.stream.expect('assign')
         expr = self.parse_tuple()
@@ -118,7 +119,7 @@
             test = self.parse_expression()
         recursive = self.stream.skip_if('name:recursive')
         body = self.parse_statements(('name:endfor', 'name:else'))
-        if self.stream.next().value == 'endfor':
+        if next(self.stream).value == 'endfor':
             else_ = []
         else:
             else_ = self.parse_statements(('name:endfor',), drop_needle=True)
@@ -132,7 +133,7 @@
             node.test = self.parse_tuple(with_condexpr=False)
             node.body = self.parse_statements(('name:elif', 'name:else',
                                                'name:endif'))
-            token = self.stream.next()
+            token = next(self.stream)
             if token.test('name:elif'):
                 new_node = nodes.If(lineno=self.stream.current.lineno)
                 node.else_ = [new_node]
@@ -147,7 +148,7 @@
         return result
 
     def parse_block(self):
-        node = nodes.Block(lineno=self.stream.next().lineno)
+        node = nodes.Block(lineno=next(self.stream).lineno)
         node.name = self.stream.expect('name').value
         node.scoped = self.stream.skip_if('name:scoped')
         node.body = self.parse_statements(('name:endblock',), drop_needle=True)
@@ -155,21 +156,21 @@
         return node
 
     def parse_extends(self):
-        node = nodes.Extends(lineno=self.stream.next().lineno)
+        node = nodes.Extends(lineno=next(self.stream).lineno)
         node.template = self.parse_expression()
         return node
 
     def parse_import_context(self, node, default):
         if self.stream.current.test_any('name:with', 'name:without') and \
            self.stream.look().test('name:context'):
-            node.with_context = self.stream.next().value == 'with'
+            node.with_context = next(self.stream).value == 'with'
             self.stream.skip()
         else:
             node.with_context = default
         return node
 
     def parse_include(self):
-        node = nodes.Include(lineno=self.stream.next().lineno)
+        node = nodes.Include(lineno=next(self.stream).lineno)
         node.template = self.parse_expression()
         if self.stream.current.test('name:ignore') and \
            self.stream.look().test('name:missing'):
@@ -180,14 +181,14 @@
         return self.parse_import_context(node, True)
 
     def parse_import(self):
-        node = nodes.Import(lineno=self.stream.next().lineno)
+        node = nodes.Import(lineno=next(self.stream).lineno)
         node.template = self.parse_expression()
         self.stream.expect('name:as')
         node.target = self.parse_assign_target(name_only=True).name
         return self.parse_import_context(node, False)
 
     def parse_from(self):
-        node = nodes.FromImport(lineno=self.stream.next().lineno)
+        node = nodes.FromImport(lineno=next(self.stream).lineno)
         node.template = self.parse_expression()
         self.stream.expect('name:import')
         node.names = []
@@ -195,7 +196,7 @@
         def parse_context():
             if self.stream.current.value in ('with', 'without') and \
                self.stream.look().test('name:context'):
-                node.with_context = self.stream.next().value == 'with'
+                node.with_context = next(self.stream).value == 'with'
                 self.stream.skip()
                 return True
             return False
@@ -240,7 +241,7 @@
         self.stream.expect('rparen')
 
     def parse_call_block(self):
-        node = nodes.CallBlock(lineno=self.stream.next().lineno)
+        node = nodes.CallBlock(lineno=next(self.stream).lineno)
         if self.stream.current.type == 'lparen':
             self.parse_signature(node)
         else:
@@ -254,14 +255,14 @@
         return node
 
     def parse_filter_block(self):
-        node = nodes.FilterBlock(lineno=self.stream.next().lineno)
+        node = nodes.FilterBlock(lineno=next(self.stream).lineno)
         node.filter = self.parse_filter(None, start_inline=True)
         node.body = self.parse_statements(('name:endfilter',),
                                           drop_needle=True)
         return node
 
     def parse_macro(self):
-        node = nodes.Macro(lineno=self.stream.next().lineno)
+        node = nodes.Macro(lineno=next(self.stream).lineno)
         node.name = self.parse_assign_target(name_only=True).name
         self.parse_signature(node)
         node.body = self.parse_statements(('name:endmacro',),
@@ -269,7 +270,7 @@
         return node
 
     def parse_print(self):
-        node = nodes.Output(lineno=self.stream.next().lineno)
+        node = nodes.Output(lineno=next(self.stream).lineno)
         node.nodes = []
         while self.stream.current.type != 'block_end':
             if node.nodes:
@@ -343,7 +344,7 @@
 
     def parse_not(self):
         if self.stream.current.test('name:not'):
-            lineno = self.stream.next().lineno
+            lineno = next(self.stream).lineno
             return nodes.Not(self.parse_not(), lineno=lineno)
         return self.parse_compare()
 
@@ -354,7 +355,7 @@
         while 1:
             token_type = self.stream.current.type
             if token_type in _compare_operators:
-                self.stream.next()
+                next(self.stream)
                 ops.append(nodes.Operand(token_type, self.parse_add()))
             elif self.stream.skip_if('name:in'):
                 ops.append(nodes.Operand('in', self.parse_add()))
@@ -373,7 +374,7 @@
         lineno = self.stream.current.lineno
         left = self.parse_sub()
         while self.stream.current.type == 'add':
-            self.stream.next()
+            next(self.stream)
             right = self.parse_sub()
             left = nodes.Add(left, right, lineno=lineno)
             lineno = self.stream.current.lineno
@@ -383,7 +384,7 @@
         lineno = self.stream.current.lineno
         left = self.parse_concat()
         while self.stream.current.type == 'sub':
-            self.stream.next()
+            next(self.stream)
             right = self.parse_concat()
             left = nodes.Sub(left, right, lineno=lineno)
             lineno = self.stream.current.lineno
@@ -393,7 +394,7 @@
         lineno = self.stream.current.lineno
         args = [self.parse_mul()]
         while self.stream.current.type == 'tilde':
-            self.stream.next()
+            next(self.stream)
             args.append(self.parse_mul())
         if len(args) == 1:
             return args[0]
@@ -403,7 +404,7 @@
         lineno = self.stream.current.lineno
         left = self.parse_div()
         while self.stream.current.type == 'mul':
-            self.stream.next()
+            next(self.stream)
             right = self.parse_div()
             left = nodes.Mul(left, right, lineno=lineno)
             lineno = self.stream.current.lineno
@@ -413,7 +414,7 @@
         lineno = self.stream.current.lineno
         left = self.parse_floordiv()
         while self.stream.current.type == 'div':
-            self.stream.next()
+            next(self.stream)
             right = self.parse_floordiv()
             left = nodes.Div(left, right, lineno=lineno)
             lineno = self.stream.current.lineno
@@ -423,7 +424,7 @@
         lineno = self.stream.current.lineno
         left = self.parse_mod()
         while self.stream.current.type == 'floordiv':
-            self.stream.next()
+            next(self.stream)
             right = self.parse_mod()
             left = nodes.FloorDiv(left, right, lineno=lineno)
             lineno = self.stream.current.lineno
@@ -433,7 +434,7 @@
         lineno = self.stream.current.lineno
         left = self.parse_pow()
         while self.stream.current.type == 'mod':
-            self.stream.next()
+            next(self.stream)
             right = self.parse_pow()
             left = nodes.Mod(left, right, lineno=lineno)
             lineno = self.stream.current.lineno
@@ -443,7 +444,7 @@
         lineno = self.stream.current.lineno
         left = self.parse_unary()
         while self.stream.current.type == 'pow':
-            self.stream.next()
+            next(self.stream)
             right = self.parse_unary()
             left = nodes.Pow(left, right, lineno=lineno)
             lineno = self.stream.current.lineno
@@ -453,11 +454,11 @@
         token_type = self.stream.current.type
         lineno = self.stream.current.lineno
         if token_type == 'sub':
-            self.stream.next()
+            next(self.stream)
             node = self.parse_unary()
             return nodes.Neg(node, lineno=lineno)
         if token_type == 'add':
-            self.stream.next()
+            next(self.stream)
             node = self.parse_unary()
             return nodes.Pos(node, lineno=lineno)
         return self.parse_primary()
@@ -472,20 +473,20 @@
                 node = nodes.Const(None, lineno=token.lineno)
             else:
                 node = nodes.Name(token.value, 'load', lineno=token.lineno)
-            self.stream.next()
+            next(self.stream)
         elif token.type == 'string':
-            self.stream.next()
+            next(self.stream)
             buf = [token.value]
             lineno = token.lineno
             while self.stream.current.type == 'string':
                 buf.append(self.stream.current.value)
-                self.stream.next()
+                next(self.stream)
             node = nodes.Const(''.join(buf), lineno=lineno)
         elif token.type in ('integer', 'float'):
-            self.stream.next()
+            next(self.stream)
             node = nodes.Const(token.value, lineno=token.lineno)
         elif token.type == 'lparen':
-            self.stream.next()
+            next(self.stream)
             node = self.parse_tuple()
             self.stream.expect('rparen')
         elif token.type == 'lbracket':
@@ -581,10 +582,10 @@
         return node
 
     def parse_subscript(self, node):
-        token = self.stream.next()
+        token = next(self.stream)
         if token.type == 'dot':
             attr_token = self.stream.current
-            self.stream.next()
+            next(self.stream)
             if attr_token.type == 'name':
                 return nodes.Getattr(node, attr_token.value, 'load',
                                      lineno=token.lineno)
@@ -611,13 +612,13 @@
         lineno = self.stream.current.lineno
 
         if self.stream.current.type == 'colon':
-            self.stream.next()
+            next(self.stream)
             args = [None]
         else:
             node = self.parse_expression()
             if self.stream.current.type != 'colon':
                 return node
-            self.stream.next()
+            next(self.stream)
             args = [node]
 
         if self.stream.current.type == 'colon':
@@ -628,7 +629,7 @@
             args.append(None)
 
         if self.stream.current.type == 'colon':
-            self.stream.next()
+            next(self.stream)
             if self.stream.current.type not in ('rbracket', 'comma'):
                 args.append(self.parse_expression())
             else:
@@ -658,11 +659,11 @@
                     break
             if self.stream.current.type == 'mul':
                 ensure(dyn_args is None and dyn_kwargs is None)
-                self.stream.next()
+                next(self.stream)
                 dyn_args = self.parse_expression()
             elif self.stream.current.type == 'pow':
                 ensure(dyn_kwargs is None)
-                self.stream.next()
+                next(self.stream)
                 dyn_kwargs = self.parse_expression()
             else:
                 ensure(dyn_args is None and dyn_kwargs is None)
@@ -688,11 +689,11 @@
     def parse_filter(self, node, start_inline=False):
         while self.stream.current.type == 'pipe' or start_inline:
             if not start_inline:
-                self.stream.next()
+                next(self.stream)
             token = self.stream.expect('name')
             name = token.value
             while self.stream.current.type == 'dot':
-                self.stream.next()
+                next(self.stream)
                 name += '.' + self.stream.expect('name').value
             if self.stream.current.type == 'lparen':
                 args, kwargs, dyn_args, dyn_kwargs = self.parse_call(None)
@@ -706,15 +707,15 @@
         return node
 
     def parse_test(self, node):
-        token = self.stream.next()
+        token = next(self.stream)
         if self.stream.current.test('name:not'):
-            self.stream.next()
+            next(self.stream)
             negated = True
         else:
             negated = False
         name = self.stream.expect('name').value
         while self.stream.current.type == 'dot':
-            self.stream.next()
+            next(self.stream)
             name += '.' + self.stream.expect('name').value
         dyn_args = dyn_kwargs = None
         kwargs = []
@@ -753,14 +754,14 @@
                 if token.value:
                     add_data(nodes.TemplateData(token.value,
                                                 lineno=token.lineno))
-                self.stream.next()
+                next(self.stream)
             elif token.type == 'variable_begin':
-                self.stream.next()
+                next(self.stream)
                 add_data(self.parse_tuple(with_condexpr=True))
                 self.stream.expect('variable_end')
             elif token.type == 'block_begin':
                 flush_data()
-                self.stream.next()
+                next(self.stream)
                 if end_tokens is not None and \
                    self.stream.current.test_any(*end_tokens):
                     return body