Browse files

Priority of `not` raised. It's now possible to write `not foo in bar`

as an alias to `foo not in bar` like in python.  Previously the grammar
required parentheses (`not (foo in bar)`) which was odd.

--HG--
branch : trunk
  • Loading branch information...
1 parent 8de6f18 commit d89f0f3defdebbf01a2f9f4a381b7e99a49ced03 @mitsuhiko mitsuhiko committed Feb 4, 2009
Showing with 21 additions and 8 deletions.
  1. +1 −0 .hgignore
  2. +3 −0 CHANGES
  3. +1 −0 docs/_static/style.css
  4. +2 −2 docs/cache_extension.py
  5. +8 −6 jinja2/parser.py
  6. +6 −0 tests/test_syntax.py
View
1 .hgignore
@@ -5,3 +5,4 @@
^(build|dist|Jinja2\.egg-info)/
\.py[co]$
\.DS_Store$
+^env/
View
3 CHANGES
@@ -7,6 +7,9 @@ Version 2.2
- Include statements can now be marked with ``ignore missing`` to skip
non existing templates.
+- Priority of `not` raised. It's now possible to write `not foo in bar`
+ as an alias to `foo not in bar` like in python. Previously the grammar
+ required parentheses (`not (foo in bar)`) which was odd.
Version 2.1.1
-------------
View
1 docs/_static/style.css
@@ -253,6 +253,7 @@ h3:hover > a.headerlink,
h4:hover > a.headerlink,
h5:hover > a.headerlink,
h6:hover > a.headerlink,
+dt:hover > a.headerlink,
dt:hover > a.headerlink {
visibility: visible;
}
View
4 docs/cache_extension.py
@@ -51,6 +51,6 @@ def _cache_support(self, name, timeout, caller):
rv = self.environment.fragment_cache.get(key)
if rv is None:
return rv
- rv = caller()
- self.environment.fragment_cache.add(key, rv, timeout)
+ rv = caller()
+ self.environment.fragment_cache.add(key, rv, timeout)
return rv
View
14 jinja2/parser.py
@@ -333,13 +333,19 @@ def parse_or(self):
def parse_and(self):
lineno = self.stream.current.lineno
- left = self.parse_compare()
+ left = self.parse_not()
while self.stream.skip_if('name:and'):
- right = self.parse_compare()
+ right = self.parse_not()
left = nodes.And(left, right, lineno=lineno)
lineno = self.stream.current.lineno
return left
+ def parse_not(self):
+ if self.stream.current.test('name:not'):
+ lineno = self.stream.next().lineno
+ return nodes.Not(self.parse_not(), lineno=lineno)
+ return self.parse_compare()
+
def parse_compare(self):
lineno = self.stream.current.lineno
expr = self.parse_add()
@@ -445,10 +451,6 @@ def parse_pow(self):
def parse_unary(self):
token_type = self.stream.current.type
lineno = self.stream.current.lineno
- if token_type == 'name' and self.stream.current.value == 'not':
- self.stream.next()
- node = self.parse_unary()
- return nodes.Not(node, lineno=lineno)
if token_type == 'sub':
self.stream.next()
node = self.parse_unary()
View
6 tests/test_syntax.py
@@ -189,3 +189,9 @@ def test_test_chaining(env):
def test_string_concatenation(env):
tmpl = env.from_string('{{ "foo" "bar" "baz" }}')
assert tmpl.render() == 'foobarbaz'
+
+
+def test_notin(env):
+ bar = xrange(100)
+ tmpl = env.from_string('''{{ not 42 in bar }}''')
+ assert tmpl.render(bar=bar) == unicode(not 42 in bar)

0 comments on commit d89f0f3

Please sign in to comment.