Parser Class

Inheritance diagram of Parser

class Parser(environment, source, name=None, filename=None, state=None)

This is the central parsing class Jinja2 uses. It’s passed to extensions and can be used to parse expressions or statements.

Methods

__init__(environment, source[, name, ...])
fail(msg[, lineno, exc]) Convenience method that raises exc with the message, passed
fail_eof([end_tokens, lineno]) Like fail_unknown_tag but for end of template situations.
fail_unknown_tag(name[, lineno]) Called if the parser encounters an unknown tag.
free_identifier([lineno]) Return a new free identifier as InternalName.
is_tuple_end([extra_end_rules]) Are we at the end of a tuple?
parse() Parse the whole template into a Template node.
parse_add()
parse_and()
parse_assign_target([with_tuple, name_only, ...]) Parse an assignment target.
parse_block()
parse_call(node)
parse_call_block()
parse_compare()
parse_concat()
parse_condexpr()
parse_dict()
parse_div()
parse_expression([with_condexpr]) Parse an expression.
parse_extends()
parse_filter(node[, start_inline])
parse_filter_block()
parse_filter_expr(node)
parse_floordiv()
parse_for() Parse a for loop.
parse_from()
parse_if() Parse an if construct.
parse_import()
parse_import_context(node, default)
parse_include()
parse_list()
parse_macro()
parse_mod()
parse_mul()
parse_not()
parse_or()
parse_postfix(node)
parse_pow()
parse_primary()
parse_print()
parse_set() Parse an assign statement.
parse_signature(node)
parse_statement() Parse a single statement.
parse_statements(end_tokens[, drop_needle]) Parse multiple statements into a list until one of the end tokens is reached.
parse_sub()
parse_subscribed()
parse_subscript(node)
parse_test(node)
parse_tuple([simplified, with_condexpr, ...]) Works like parse_expression but if multiple expressions are
parse_unary([with_filter])
subparse([end_tokens])

Descriptions

class Parser

Method details

__init__(environment, source, name=None, filename=None, state=None)
fail(msg, lineno=None, exc=<class 'jinja2.exceptions.TemplateSyntaxError'>)

Convenience method that raises exc with the message, passed line number or last line number as well as the current name and filename.

fail_eof(end_tokens=None, lineno=None)

Like fail_unknown_tag but for end of template situations.

fail_unknown_tag(name, lineno=None)

Called if the parser encounters an unknown tag. Tries to fail with a human readable error message that could help to identify the problem.

free_identifier(lineno=None)

Return a new free identifier as InternalName.

is_tuple_end(extra_end_rules=None)

Are we at the end of a tuple?

parse()

Parse the whole template into a Template node.

parse_add()
parse_and()
parse_assign_target(with_tuple=True, name_only=False, extra_end_rules=None)

Parse an assignment target. As Jinja2 allows assignments to tuples, this function can parse all allowed assignment targets. Per default assignments to tuples are parsed, that can be disable however by setting with_tuple to False. If only assignments to names are wanted name_only can be set to True. The extra_end_rules parameter is forwarded to the tuple parsing function.

parse_block()
parse_call(node)
parse_call_block()
parse_compare()
parse_concat()
parse_condexpr()
parse_dict()
parse_div()
parse_expression(with_condexpr=True)

Parse an expression. Per default all expressions are parsed, if the optional with_condexpr parameter is set to False conditional expressions are not parsed.

parse_extends()
parse_filter(node, start_inline=False)
parse_filter_block()
parse_filter_expr(node)
parse_floordiv()
parse_for()

Parse a for loop.

parse_from()
parse_if()

Parse an if construct.

parse_import()
parse_import_context(node, default)
parse_include()
parse_list()
parse_macro()
parse_mod()
parse_mul()
parse_not()
parse_or()
parse_postfix(node)
parse_pow()
parse_primary()
parse_print()
parse_set()

Parse an assign statement.

parse_signature(node)
parse_statement()

Parse a single statement.

parse_statements(end_tokens, drop_needle=False)

Parse multiple statements into a list until one of the end tokens is reached. This is used to parse the body of statements as it also parses template data if appropriate. The parser checks first if the current token is a colon and skips it if there is one. Then it checks for the block end and parses until if one of the end_tokens is reached. Per default the active token in the stream at the end of the call is the matched end token. If this is not wanted drop_needle can be set to True and the end token is removed.

parse_sub()
parse_subscribed()
parse_subscript(node)
parse_test(node)
parse_tuple(simplified=False, with_condexpr=True, extra_end_rules=None, explicit_parentheses=False)

Works like parse_expression but if multiple expressions are delimited by a comma a Tuple node is created. This method could also return a regular expression instead of a tuple if no commas where found.

The default parsing mode is a full tuple. If simplified is True only names and literals are parsed. The no_condexpr parameter is forwarded to parse_expression().

Because tuples do not require delimiters and may end in a bogus comma an extra hint is needed that marks the end of a tuple. For example for loops support tuples between for and in. In that case the extra_end_rules is set to ['name:in'].

explicit_parentheses is true if the parsing was triggered by an expression in parentheses. This is used to figure out if an empty tuple is a valid expression or not.

parse_unary(with_filter=True)
subparse(end_tokens=None)

Table Of Contents

Previous topic

next

Next topic

TemplateAssertionError