.. autoclass:: jinja2.parser.Parser
:members: parse_expression, parse_tuple, parse_assign_target,
- parse_statements, free_identifier
+ parse_statements, free_identifier, fail
.. attribute:: filename
raise TemplateAssertionError('It\'s not possible to set and '
'access variables derived from '
'an outer scope! (affects: %s' %
- vars, node.lineno, self.filename)
+ vars, node.lineno, self.name,
+ self.filename)
# remove variables from a closure from the frame's undeclared
# identifiers.
if block.name in self.blocks:
raise TemplateAssertionError('block %r defined twice' %
block.name, block.lineno,
- self.filename)
+ self.name, self.filename)
self.blocks[block.name] = block
# find all imports and import them
if not frame.toplevel:
raise TemplateAssertionError('cannot use extend from a non '
'top-level scope', node.lineno,
- self.filename)
+ self.name, self.filename)
# if the number of extends statements in general is zero so
# far, we don't have to add a check if something extended
func = self.environment.filters.get(node.name)
if func is None:
raise TemplateAssertionError('no filter named %r' % node.name,
- node.lineno, self.filename)
+ node.lineno, self.name,
+ self.filename)
if getattr(func, 'contextfilter', False):
self.write('context, ')
elif getattr(func, 'environmentfilter', False):
self.write(self.tests[node.name] + '(')
if node.name not in self.environment.tests:
raise TemplateAssertionError('no test named %r' % node.name,
- node.lineno, self.filename)
+ node.lineno, self.name,
+ self.filename)
self.visit(node.node, frame)
self.signature(node, frame)
self.write(')')
except (TypeError, LookupError):
return self.undefined(obj=obj, name=argument)
- def parse(self, source, filename=None):
+ def parse(self, source, name=None, filename=None):
"""Parse the sourcecode and return the abstract syntax tree. This
tree of nodes is used by the compiler to convert the template into
executable source- or bytecode. This is useful for debugging or to
this gives you a good overview of the node tree generated.
"""
try:
- return Parser(self, source, filename).parse()
+ return Parser(self, source, name, filename).parse()
except TemplateSyntaxError, e:
from jinja2.debug import translate_syntax_error
exc_type, exc_value, tb = translate_syntax_error(e)
raise exc_type, exc_value, tb
- def lex(self, source, filename=None):
+ def lex(self, source, name=None, filename=None):
"""Lex the given sourcecode and return a generator that yields
tokens as tuples in the form ``(lineno, token_type, value)``.
This can be useful for :ref:`extension development <writing-extensions>`
and debugging templates.
"""
- return self.lexer.tokeniter(source, filename)
+ return self.lexer.tokeniter(source, name, filename)
def compile(self, source, name=None, filename=None, raw=False):
"""Compile a node or template source code. The `name` parameter is
mainly used internally.
"""
if isinstance(source, basestring):
- source = self.parse(source, filename)
+ source = self.parse(source, name, filename)
if self.optimized:
node = optimize(source, self)
source = generate(node, self, name, filename)
class TemplateSyntaxError(TemplateError):
"""Raised to tell the user that there is a problem with the template."""
- def __init__(self, message, lineno, filename):
- TemplateError.__init__(self, '%s (line %s)' % (message, lineno))
+ def __init__(self, message, lineno, name=None, filename=None):
+ if name is not None:
+ extra = '%s, line %d' % (name, lineno)
+ else:
+ extra = 'line %d' % lineno
+ TemplateError.__init__(self, '%s (%s)' % (message, extra))
self.message = message
self.lineno = lineno
+ self.name = name
self.filename = filename
name = parser.stream.expect('name')
if name.value in variables:
- raise TemplateAssertionError('translatable variable %r defined '
- 'twice.' % name.value, name.lineno,
- parser.filename)
+ parser.fail('translatable variable %r defined twice.' %
+ name.value, name.lineno,
+ exc=TemplateAssertionError)
# expressions
if parser.stream.current.type is 'assign':
if not have_plural:
plural_expr = None
elif plural_expr is None:
- raise TemplateAssertionError('pluralize without variables',
- lineno, parser.filename)
+ parser.fail('pluralize without variables', lineno)
if variables:
variables = nodes.Dict([nodes.Pair(nodes.Const(x, lineno=lineno), y)
elif parser.stream.current.test('name:pluralize'):
if allow_pluralize:
break
- raise TemplateSyntaxError('a translatable section can '
- 'have only one pluralize '
- 'section',
- parser.stream.current.lineno,
- parser.filename)
- raise TemplateSyntaxError('control structures in translatable'
- ' sections are not allowed.',
- parser.stream.current.lineno,
- parser.filename)
+ parser.fail('a translatable section can have only one '
+ 'pluralize section')
+ parser.fail('control structures in translatable sections are '
+ 'not allowed')
else:
assert False, 'internal parser error'
operator_re = re.compile('(%s)' % '|'.join(re.escape(x) for x in
sorted(operators, key=lambda x: -len(x))))
-simple_escapes = {
- 'a': '\a',
- 'n': '\n',
- 'r': '\r',
- 'f': '\f',
- 't': '\t',
- 'v': '\v',
- '\\': '\\',
- '"': '"',
- "'": "'",
- '0': '\x00'
-}
-unicode_escapes = {
- 'x': 2,
- 'u': 4,
- 'U': 8
-}
-
-
-def unescape_string(lineno, filename, s):
- r"""Unescape a string. Supported escapes:
- \a, \n, \r\, \f, \v, \\, \", \', \0
-
- \x00, \u0000, \U00000000, \N{...}
- """
- try:
- return s.encode('ascii', 'backslashreplace').decode('unicode-escape')
- except UnicodeError, e:
- msg = str(e).split(':')[-1].strip()
- raise TemplateSyntaxError(msg, lineno, filename)
-
class Failure(object):
"""Class that raises a `TemplateSyntaxError` if called.
one token ahead. The current active token is stored as :attr:`current`.
"""
- def __init__(self, generator, filename):
+ def __init__(self, generator, name, filename):
self._next = generator.next
self._pushed = deque()
self.current = Token(1, 'initial', '')
+ self.name = name
self.filename = filename
self.next()
raise TemplateSyntaxError('unexpected end of template, '
'expected %r.' % expr,
self.current.lineno,
- self.filename)
+ self.name, self.filename)
raise TemplateSyntaxError("expected token %r, got %r" %
(expr, str(self.current)),
self.current.lineno,
- self.filename)
+ self.name, self.filename)
try:
return self.current
finally:
] + tag_rules
}
- def tokenize(self, source, filename=None):
+ def tokenize(self, source, name=None, filename=None):
"""Works like `tokeniter` but returns a tokenstream of tokens and not
a generator or token tuples. Additionally all token values are already
converted into types and postprocessed. For example comments are removed,
"""
source = unicode(source)
def generate():
- for lineno, token, value in self.tokeniter(source, filename):
+ for lineno, token, value in self.tokeniter(source, name, filename):
if token in ('comment_begin', 'comment', 'comment_end'):
continue
elif token == 'linestatement_begin':
elif token == 'name':
value = str(value)
elif token == 'string':
- value = unescape_string(lineno, filename, value[1:-1])
+ # try to unescape string
+ try:
+ value = value[1:-1] \
+ .encode('ascii', 'backslashreplace') \
+ .decode('unicode-escape')
+ except Exception, e:
+ msg = str(e).split(':')[-1].strip()
+ raise TemplateSyntaxError(msg, lineno, name, filename)
+ # if we can express it as bytestring (ascii only)
+ # we do that for support of semi broken APIs
+ # as datetime.datetime.strftime
try:
value = str(value)
except UnicodeError:
elif token == 'operator':
token = operators[value]
yield Token(lineno, token, value)
- return TokenStream(generate(), filename)
+ return TokenStream(generate(), name, filename)
- def tokeniter(self, source, filename=None):
+ def tokeniter(self, source, name, filename=None):
"""This method tokenizes the text and returns the tokens in a
generator. Use this method if you just want to tokenize a template.
The output you get is not compatible with the input the jinja parser
elif data in ('}', ')', ']'):
if not balancing_stack:
raise TemplateSyntaxError('unexpected "%s"' %
- data, lineno,
+ data, lineno, name,
filename)
expected_op = balancing_stack.pop()
if expected_op != data:
raise TemplateSyntaxError('unexpected "%s", '
'expected "%s"' %
(data, expected_op),
- lineno, filename)
+ lineno, name,
+ filename)
# yield items
if tokens is not None:
yield lineno, tokens, data
# something went wrong
raise TemplateSyntaxError('unexpected char %r at %d' %
(source[pos], pos), lineno,
- filename)
+ name, filename)
extensions and can be used to parse expressions or statements.
"""
- def __init__(self, environment, source, filename=None):
+ def __init__(self, environment, source, name=None, filename=None):
self.environment = environment
if isinstance(filename, unicode):
filename = filename.encode('utf-8')
self.source = unicode(source)
+ self.name = name
self.filename = filename
self.closed = False
self.stream = environment.lexer.tokenize(self.source, filename)
self.extensions[tag] = extension.parse
self._last_identifier = 0
+ def fail(self, msg, lineno=None, exc=TemplateSyntaxError):
+ """Convenience method that raises `exc` with the message, passed
+ line number or last line number as well as the current name and
+ filename.
+ """
+ if lineno is None:
+ lineno = self.stream.current.lineno
+ raise TemplateSyntaxError(msg, lineno, self.name, self.filename)
+
def is_tuple_end(self, extra_end_rules=None):
"""Are we at the end of a tuple?"""
if self.stream.current.type in ('variable_end', 'block_end', 'rparen'):
"""Parse a single statement."""
token = self.stream.current
if token.type is not 'name':
- raise TemplateSyntaxError('tag name expected', token.lineno,
- self.filename)
+ self.fail('tag name expected', token.lineno)
if token.value in _statement_keywords:
return getattr(self, 'parse_' + self.stream.current.value)()
if token.value == 'call':
ext = self.extensions.get(token.value)
if ext is not None:
return ext(self)
- raise TemplateSyntaxError('unknown tag %r' % token.value,
- token.lineno, self.filename)
+ self.fail('unknown tag %r' % token.value, token.lineno)
def parse_statements(self, end_tokens, drop_needle=False):
"""Parse multiple statements into a list until one of the end tokens
break
target = self.parse_assign_target(name_only=True)
if target.name.startswith('__'):
- raise TemplateAssertionError('names starting with two '
- 'underscores can not be '
- 'imported', target.lineno,
- self.filename)
+ self.fail('names starting with two underscores can not '
+ 'be imported', target.lineno,
+ exc=TemplateAssertionError)
if self.stream.skip_if('name:as'):
alias = self.parse_assign_target(name_only=True)
node.names.append((target.name, alias.name))
node.call = self.parse_expression()
if not isinstance(node.call, nodes.Call):
- raise TemplateSyntaxError('expected call', node.lineno,
- self.filename)
+ self.fail('expected call', node.lineno)
node.body = self.parse_statements(('name:endcall',), drop_needle=True)
return node
target = self.parse_primary(with_postfix=False)
target.set_ctx('store')
if not target.can_assign():
- raise TemplateSyntaxError('can\'t assign to %r' %
- target.__class__.__name__.lower(),
- target.lineno, self.filename)
+ self.fail('can\'t assign to %r' % target.__class__.
+ __name__.lower(), target.lineno)
return target
def parse_expression(self, with_condexpr=True):
elif token.type is 'lbrace':
node = self.parse_dict()
else:
- raise TemplateSyntaxError("unexpected token '%s'" %
- (token,), token.lineno,
- self.filename)
+ self.fail("unexpected token '%s'" % (token,), token.lineno)
if with_postfix:
node = self.parse_postfix(node)
return node
if token.type is 'dot':
attr_token = self.stream.current
if attr_token.type not in ('name', 'integer'):
- raise TemplateSyntaxError('expected name or number',
- attr_token.lineno, self.filename)
+ self.fail('expected name or number', attr_token.lineno)
arg = nodes.Const(attr_token.value, lineno=attr_token.lineno)
self.stream.next()
elif token.type is 'lbracket':
else:
arg = nodes.Tuple(args, self.lineno, self.filename)
else:
- raise TemplateSyntaxError('expected subscript expression',
- self.lineno, self.filename)
+ self.fail('expected subscript expression', self.lineno)
return nodes.Subscript(node, arg, 'load', lineno=token.lineno)
def parse_subscribed(self):
def ensure(expr):
if not expr:
- raise TemplateSyntaxError('invalid syntax for function '
- 'call expression', token.lineno,
- self.filename)
+ self.fail('invalid syntax for function call expression',
+ token.lineno)
while self.stream.current.type is not 'rparen':
if require_comma: