2009-12-10 06:40:36 +08:00
|
|
|
"""
|
|
|
|
Parser and utilities for the smart 'if' tag
|
|
|
|
"""
|
|
|
|
# Using a simple top down parser, as described here:
|
|
|
|
# http://effbot.org/zone/simple-top-down-parsing.htm.
|
|
|
|
# 'led' = left denotation
|
|
|
|
# 'nud' = null denotation
|
|
|
|
# 'bp' = binding power (left = lbp, right = rbp)
|
|
|
|
|
2015-09-06 00:20:05 +08:00
|
|
|
|
2009-12-10 06:40:36 +08:00
|
|
|
class TokenBase(object):
|
|
|
|
"""
|
|
|
|
Base class for operators and literals, mainly for debugging and for throwing
|
|
|
|
syntax errors.
|
|
|
|
"""
|
2013-11-03 03:27:47 +08:00
|
|
|
id = None # node/token type name
|
|
|
|
value = None # used by literals
|
|
|
|
first = second = None # used by tree nodes
|
2009-12-10 06:40:36 +08:00
|
|
|
|
|
|
|
def nud(self, parser):
|
|
|
|
# Null denotation - called in prefix context
|
|
|
|
raise parser.error_class(
|
|
|
|
"Not expecting '%s' in this position in if tag." % self.id
|
|
|
|
)
|
|
|
|
|
|
|
|
def led(self, left, parser):
|
|
|
|
# Left denotation - called in infix context
|
|
|
|
raise parser.error_class(
|
|
|
|
"Not expecting '%s' as infix operator in if tag." % self.id
|
|
|
|
)
|
|
|
|
|
|
|
|
def display(self):
|
|
|
|
"""
|
|
|
|
Returns what to display in error messages for this node
|
|
|
|
"""
|
|
|
|
return self.id
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
out = [str(x) for x in [self.id, self.first, self.second] if x is not None]
|
|
|
|
return "(" + " ".join(out) + ")"
|
|
|
|
|
|
|
|
|
|
|
|
def infix(bp, func):
|
|
|
|
"""
|
|
|
|
Creates an infix operator, given a binding power and a function that
|
|
|
|
evaluates the node
|
|
|
|
"""
|
|
|
|
class Operator(TokenBase):
|
|
|
|
lbp = bp
|
|
|
|
|
|
|
|
def led(self, left, parser):
|
|
|
|
self.first = left
|
|
|
|
self.second = parser.expression(bp)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def eval(self, context):
|
|
|
|
try:
|
2010-04-19 22:18:14 +08:00
|
|
|
return func(context, self.first, self.second)
|
2009-12-10 06:40:36 +08:00
|
|
|
except Exception:
|
|
|
|
# Templates shouldn't throw exceptions when rendering. We are
|
|
|
|
# most likely to get exceptions for things like {% if foo in bar
|
|
|
|
# %} where 'bar' does not support 'in', so default to False
|
|
|
|
return False
|
|
|
|
|
|
|
|
return Operator
|
|
|
|
|
|
|
|
|
|
|
|
def prefix(bp, func):
|
|
|
|
"""
|
|
|
|
Creates a prefix operator, given a binding power and a function that
|
|
|
|
evaluates the node.
|
|
|
|
"""
|
|
|
|
class Operator(TokenBase):
|
|
|
|
lbp = bp
|
|
|
|
|
|
|
|
def nud(self, parser):
|
|
|
|
self.first = parser.expression(bp)
|
|
|
|
self.second = None
|
|
|
|
return self
|
|
|
|
|
|
|
|
def eval(self, context):
|
|
|
|
try:
|
2010-04-19 22:18:14 +08:00
|
|
|
return func(context, self.first)
|
2009-12-10 06:40:36 +08:00
|
|
|
except Exception:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return Operator
|
|
|
|
|
|
|
|
|
|
|
|
# Operator precedence follows Python.
|
|
|
|
# NB - we can get slightly more accurate syntax error messages by not using the
|
|
|
|
# same object for '==' and '='.
|
2010-04-19 22:18:14 +08:00
|
|
|
# We defer variable evaluation to the lambda to ensure that terms are
|
|
|
|
# lazily evaluated using Python's boolean parsing logic.
|
2009-12-10 06:40:36 +08:00
|
|
|
OPERATORS = {
|
2010-04-19 22:18:14 +08:00
|
|
|
'or': infix(6, lambda context, x, y: x.eval(context) or y.eval(context)),
|
|
|
|
'and': infix(7, lambda context, x, y: x.eval(context) and y.eval(context)),
|
|
|
|
'not': prefix(8, lambda context, x: not x.eval(context)),
|
|
|
|
'in': infix(9, lambda context, x, y: x.eval(context) in y.eval(context)),
|
|
|
|
'not in': infix(9, lambda context, x, y: x.eval(context) not in y.eval(context)),
|
2016-01-22 11:50:06 +08:00
|
|
|
'is': infix(10, lambda context, x, y: x.eval(context) is y.eval(context)),
|
2010-04-19 22:18:14 +08:00
|
|
|
'==': infix(10, lambda context, x, y: x.eval(context) == y.eval(context)),
|
|
|
|
'!=': infix(10, lambda context, x, y: x.eval(context) != y.eval(context)),
|
|
|
|
'>': infix(10, lambda context, x, y: x.eval(context) > y.eval(context)),
|
|
|
|
'>=': infix(10, lambda context, x, y: x.eval(context) >= y.eval(context)),
|
|
|
|
'<': infix(10, lambda context, x, y: x.eval(context) < y.eval(context)),
|
|
|
|
'<=': infix(10, lambda context, x, y: x.eval(context) <= y.eval(context)),
|
2009-12-10 06:40:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# Assign 'id' to each:
|
|
|
|
for key, op in OPERATORS.items():
|
|
|
|
op.id = key
|
|
|
|
|
|
|
|
|
|
|
|
class Literal(TokenBase):
|
|
|
|
"""
|
|
|
|
A basic self-resolvable object similar to a Django template variable.
|
|
|
|
"""
|
|
|
|
# IfParser uses Literal in create_var, but TemplateIfParser overrides
|
|
|
|
# create_var so that a proper implementation that actually resolves
|
2016-01-11 00:48:16 +08:00
|
|
|
# variables, filters etc. is used.
|
2009-12-10 06:40:36 +08:00
|
|
|
id = "literal"
|
|
|
|
lbp = 0
|
|
|
|
|
|
|
|
def __init__(self, value):
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
def display(self):
|
|
|
|
return repr(self.value)
|
|
|
|
|
|
|
|
def nud(self, parser):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def eval(self, context):
|
|
|
|
return self.value
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "(%s %r)" % (self.id, self.value)
|
|
|
|
|
|
|
|
|
|
|
|
class EndToken(TokenBase):
|
|
|
|
lbp = 0
|
|
|
|
|
|
|
|
def nud(self, parser):
|
|
|
|
raise parser.error_class("Unexpected end of expression in if tag.")
|
|
|
|
|
|
|
|
EndToken = EndToken()
|
|
|
|
|
|
|
|
|
|
|
|
class IfParser(object):
|
|
|
|
error_class = ValueError
|
|
|
|
|
|
|
|
def __init__(self, tokens):
|
2010-04-19 22:18:14 +08:00
|
|
|
# pre-pass necessary to turn 'not','in' into single token
|
2010-03-09 07:25:37 +08:00
|
|
|
l = len(tokens)
|
|
|
|
mapped_tokens = []
|
|
|
|
i = 0
|
|
|
|
while i < l:
|
|
|
|
token = tokens[i]
|
2013-11-04 02:08:55 +08:00
|
|
|
if token == "not" and i + 1 < l and tokens[i + 1] == "in":
|
2010-03-09 07:25:37 +08:00
|
|
|
token = "not in"
|
2013-11-03 03:27:47 +08:00
|
|
|
i += 1 # skip 'in'
|
2010-03-09 07:25:37 +08:00
|
|
|
mapped_tokens.append(self.translate_token(token))
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
self.tokens = mapped_tokens
|
2009-12-10 06:40:36 +08:00
|
|
|
self.pos = 0
|
2012-06-15 06:10:51 +08:00
|
|
|
self.current_token = self.next_token()
|
2009-12-10 06:40:36 +08:00
|
|
|
|
2010-03-09 07:25:37 +08:00
|
|
|
def translate_token(self, token):
|
2009-12-10 06:40:36 +08:00
|
|
|
try:
|
|
|
|
op = OPERATORS[token]
|
|
|
|
except (KeyError, TypeError):
|
|
|
|
return self.create_var(token)
|
|
|
|
else:
|
|
|
|
return op()
|
|
|
|
|
2012-06-15 06:10:51 +08:00
|
|
|
def next_token(self):
|
2009-12-10 06:40:36 +08:00
|
|
|
if self.pos >= len(self.tokens):
|
|
|
|
return EndToken
|
|
|
|
else:
|
|
|
|
retval = self.tokens[self.pos]
|
|
|
|
self.pos += 1
|
|
|
|
return retval
|
|
|
|
|
|
|
|
def parse(self):
|
|
|
|
retval = self.expression()
|
|
|
|
# Check that we have exhausted all the tokens
|
|
|
|
if self.current_token is not EndToken:
|
|
|
|
raise self.error_class("Unused '%s' at end of if expression." %
|
|
|
|
self.current_token.display())
|
|
|
|
return retval
|
|
|
|
|
|
|
|
def expression(self, rbp=0):
|
|
|
|
t = self.current_token
|
2012-06-15 06:10:51 +08:00
|
|
|
self.current_token = self.next_token()
|
2009-12-10 06:40:36 +08:00
|
|
|
left = t.nud(self)
|
|
|
|
while rbp < self.current_token.lbp:
|
|
|
|
t = self.current_token
|
2012-06-15 06:10:51 +08:00
|
|
|
self.current_token = self.next_token()
|
2009-12-10 06:40:36 +08:00
|
|
|
left = t.led(left, self)
|
|
|
|
return left
|
|
|
|
|
|
|
|
def create_var(self, value):
|
|
|
|
return Literal(value)
|