fixed an issue with unary operators having the wrong precendence. 2.5.1
authorArmin Ronacher <armin.ronacher@active-4.com>
Tue, 17 Aug 2010 10:36:38 +0000 (12:36 +0200)
committerArmin Ronacher <armin.ronacher@active-4.com>
Tue, 17 Aug 2010 10:36:38 +0000 (12:36 +0200)
--HG--
branch : trunk

CHANGES
jinja2/parser.py
jinja2/testsuite/lexnparse.py

diff --git a/CHANGES b/CHANGES
index 5590be2053a7d78e536c210f6e87b6ba34680ad2..099c7c35e36b60ecd1b9ead5a20515b87ccf9829 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -18,6 +18,7 @@ Version 2.5.1
   pulled from markupsafe by the Jinja2 developers.  The debug support
   went into a separate feature called "debugsupport" and is disabled
   by default because it is only relevant for Python 2.4
+- fixed an issue with unary operators having the wrong precendence.
 
 Version 2.5
 -----------
index 9814c939c7252e2ca5a20a28e2f1f90486f6b757..39713a1fe991af3e260841ba928476fef9432853 100644 (file)
@@ -370,7 +370,7 @@ class Parser(object):
                 target = self.parse_tuple(simplified=True,
                                           extra_end_rules=extra_end_rules)
             else:
-                target = self.parse_primary(with_postfix=False)
+                target = self.parse_primary()
             target.set_ctx('store')
         if not target.can_assign():
             self.fail('can\'t assign to %r' % target.__class__.
@@ -525,20 +525,22 @@ class Parser(object):
             lineno = self.stream.current.lineno
         return left
 
-    def parse_unary(self):
+    def parse_unary(self, with_postfix=True):
         token_type = self.stream.current.type
         lineno = self.stream.current.lineno
         if token_type == 'sub':
             next(self.stream)
-            node = self.parse_unary()
-            return nodes.Neg(node, lineno=lineno)
-        if token_type == 'add':
+            node = nodes.Neg(self.parse_unary(False), lineno=lineno)
+        elif token_type == 'add':
             next(self.stream)
-            node = self.parse_unary()
-            return nodes.Pos(node, lineno=lineno)
-        return self.parse_primary()
+            node = nodes.Pos(self.parse_unary(False), lineno=lineno)
+        else:
+            node = self.parse_primary()
+        if with_postfix:
+            node = self.parse_postfix(node)
+        return node
 
-    def parse_primary(self, with_postfix=True):
+    def parse_primary(self):
         token = self.stream.current
         if token.type == 'name':
             if token.value in ('true', 'false', 'True', 'False'):
@@ -570,8 +572,6 @@ class Parser(object):
             node = self.parse_dict()
         else:
             self.fail("unexpected '%s'" % describe_token(token), token.lineno)
-        if with_postfix:
-            node = self.parse_postfix(node)
         return node
 
     def parse_tuple(self, simplified=False, with_condexpr=True,
@@ -596,7 +596,7 @@ class Parser(object):
         """
         lineno = self.stream.current.lineno
         if simplified:
-            parse = lambda: self.parse_primary(with_postfix=False)
+            parse = self.parse_primary
         elif with_condexpr:
             parse = self.parse_expression
         else:
index 99802a3353312ea199d57fbaacbb51987160c38c..9afbca47d132dd47778a2d3c3efbd8845bc52520 100644 (file)
@@ -16,7 +16,8 @@ import unittest
 
 from jinja2.testsuite import JinjaTestCase
 
-from jinja2 import Environment, Template, TemplateSyntaxError, UndefinedError
+from jinja2 import Environment, Template, TemplateSyntaxError, \
+     UndefinedError, nodes
 
 env = Environment()
 
@@ -357,6 +358,11 @@ class SyntaxTestCase(JinjaTestCase):
                                '{{ none is defined }}|{{ missing is defined }}')
         assert tmpl.render() == 'True|False|None|True|False'
 
+    def test_neg_filter_priority(self):
+        node = env.parse('{{ -1|foo }}')
+        assert isinstance(node.body[0].nodes[0], nodes.Filter)
+        assert isinstance(node.body[0].nodes[0].node, nodes.Neg)
+
     def test_const_assign(self):
         constass1 = '''{% set true = 42 %}'''
         constass2 = '''{% for none in seq %}{% endfor %}'''