class documentation

class Parser:

View In Hierarchy

This is the central parsing class Jinja uses. It's passed to extensions and can be used to parse expressions or statements.
Method fail Convenience method that raises exc with the message, passed line number or last line number as well as the current name and filename.
Method free​_identifier Return a new free identifier as ~jinja2.nodes.InternalName.
Method parse​_assign​_target No summary
Method parse​_expression Parse an expression. Per default all expressions are parsed, if the optional with_condexpr parameter is set to False conditional expressions are not parsed.
Method parse​_statements No summary
Method parse​_tuple No summary
Instance Variable filename Undocumented
Instance Variable name Undocumented
Instance Variable stream Undocumented
Method __init__ Undocumented
Method ​_fail​_ut​_eof Undocumented
Method fail​_eof Like fail_unknown_tag but for end of template situations.
Method fail​_unknown​_tag Called if the parser encounters an unknown tag. Tries to fail with a human readable error message that could help to identify the problem.
Method is​_tuple​_end Are we at the end of a tuple?
Method parse Parse the whole template into a Template node.
Method parse​_and Undocumented
Method parse​_autoescape Undocumented
Method parse​_block Undocumented
Method parse​_call Undocumented
Method parse​_call​_args Undocumented
Method parse​_call​_block Undocumented
Method parse​_compare Undocumented
Method parse​_concat Undocumented
Method parse​_condexpr Undocumented
Method parse​_dict Undocumented
Method parse​_extends Undocumented
Method parse​_filter Undocumented
Method parse​_filter​_block Undocumented
Method parse​_filter​_expr Undocumented
Method parse​_for Parse a for loop.
Method parse​_from Undocumented
Method parse​_if Parse an if construct.
Method parse​_import Undocumented
Method parse​_import​_context Undocumented
Method parse​_include Undocumented
Method parse​_list Undocumented
Method parse​_macro Undocumented
Method parse​_math1 Undocumented
Method parse​_math2 Undocumented
Method parse​_not Undocumented
Method parse​_or Undocumented
Method parse​_postfix Undocumented
Method parse​_pow Undocumented
Method parse​_primary Undocumented
Method parse​_print Undocumented
Method parse​_set Parse an assign statement.
Method parse​_signature Undocumented
Method parse​_statement Parse a single statement.
Method parse​_subscribed Undocumented
Method parse​_subscript Undocumented
Method parse​_test Undocumented
Method parse​_unary Undocumented
Method parse​_with Undocumented
Method subparse Undocumented
Instance Variable ​_end​_token​_stack Undocumented
Instance Variable ​_last​_identifier Undocumented
Instance Variable ​_tag​_stack Undocumented
Instance Variable closed Undocumented
Instance Variable environment Undocumented
Instance Variable extensions Undocumented
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.
Parameters
msg:strUndocumented
lineno:t.Optional[int]Undocumented
exc:t.Type[TemplateSyntaxError]Undocumented
Returns
te.NoReturnUndocumented
def free_identifier(self, lineno=None):
Return a new free identifier as ~jinja2.nodes.InternalName.
Parameters
lineno:t.Optional[int]Undocumented
Returns
nodes.InternalNameUndocumented
def parse_assign_target(self, with_tuple=True, name_only=False, extra_end_rules=None, with_namespace=False):
Parse an assignment target. As Jinja 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. If with_namespace is enabled, a namespace assignment may be parsed.
Parameters
with​_tuple:boolUndocumented
name​_only:boolUndocumented
extra​_end​_rules:t.Optional[t.Tuple[str, ...]]Undocumented
with​_namespace:boolUndocumented
Returns
t.Union[nodes.NSRef, nodes.Name, nodes.Tuple]Undocumented
def parse_expression(self, 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.
Parameters
with​_condexpr:boolUndocumented
Returns
nodes.ExprUndocumented
def parse_statements(self, 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.
Parameters
end​_tokens:t.Tuple[str, ...]Undocumented
drop​_needle:boolUndocumented
Returns
t.List[nodes.Node]Undocumented
def parse_tuple(self, 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 ~jinja2.nodes.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.

Parameters
simplified:boolUndocumented
with​_condexpr:boolUndocumented
extra​_end​_rules:t.Optional[t.Tuple[str, ...]]Undocumented
explicit​_parentheses:boolUndocumented
Returns
t.Union[nodes.Tuple, nodes.Expr]Undocumented
filename =

Undocumented

name =

Undocumented

stream =

Undocumented

def __init__(self, environment, source, name=None, filename=None, state=None):

Undocumented

Parameters
environment:EnvironmentUndocumented
source:strUndocumented
name:t.Optional[str]Undocumented
filename:t.Optional[str]Undocumented
state:t.Optional[str]Undocumented
def _fail_ut_eof(self, name, end_token_stack, lineno):

Undocumented

Parameters
name:t.Optional[str]Undocumented
end​_token​_stack:t.List[t.Tuple[str, ...]]Undocumented
lineno:t.Optional[int]Undocumented
Returns
te.NoReturnUndocumented
def fail_eof(self, end_tokens=None, lineno=None):
Like fail_unknown_tag but for end of template situations.
Parameters
end​_tokens:t.Optional[t.Tuple[str, ...]]Undocumented
lineno:t.Optional[int]Undocumented
Returns
te.NoReturnUndocumented
def fail_unknown_tag(self, 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.
Parameters
name:strUndocumented
lineno:t.Optional[int]Undocumented
Returns
te.NoReturnUndocumented
def is_tuple_end(self, extra_end_rules=None):
Are we at the end of a tuple?
Parameters
extra​_end​_rules:t.Optional[t.Tuple[str, ...]]Undocumented
Returns
boolUndocumented
def parse(self):
Parse the whole template into a Template node.
Returns
nodes.TemplateUndocumented
def parse_and(self):

Undocumented

Returns
nodes.ExprUndocumented
def parse_autoescape(self):

Undocumented

Returns
nodes.ScopeUndocumented
def parse_block(self):

Undocumented

Returns
nodes.BlockUndocumented
def parse_call(self, node):

Undocumented

Parameters
node:nodes.ExprUndocumented
Returns
nodes.CallUndocumented
def parse_call_args(self):

Undocumented

Returns
t.TupleUndocumented
def parse_call_block(self):

Undocumented

Returns
nodes.CallBlockUndocumented
def parse_compare(self):

Undocumented

Returns
nodes.ExprUndocumented
def parse_concat(self):

Undocumented

Returns
nodes.ExprUndocumented
def parse_condexpr(self):

Undocumented

Returns
nodes.ExprUndocumented
def parse_dict(self):

Undocumented

Returns
nodes.DictUndocumented
def parse_extends(self):

Undocumented

Returns
nodes.ExtendsUndocumented
def parse_filter(self, node, start_inline=False):

Undocumented

Parameters
node:t.Optional[nodes.Expr]Undocumented
start​_inline:boolUndocumented
Returns
t.Optional[nodes.Expr]Undocumented
def parse_filter_block(self):

Undocumented

Returns
nodes.FilterBlockUndocumented
def parse_filter_expr(self, node):

Undocumented

Parameters
node:nodes.ExprUndocumented
Returns
nodes.ExprUndocumented
def parse_for(self):
Parse a for loop.
Returns
nodes.ForUndocumented
def parse_from(self):

Undocumented

Returns
nodes.FromImportUndocumented
def parse_if(self):
Parse an if construct.
Returns
nodes.IfUndocumented
def parse_import(self):

Undocumented

Returns
nodes.ImportUndocumented
def parse_import_context(self, node, default):

Undocumented

Parameters
node:_ImportIncludeUndocumented
default:boolUndocumented
Returns
_ImportIncludeUndocumented
def parse_include(self):

Undocumented

Returns
nodes.IncludeUndocumented
def parse_list(self):

Undocumented

Returns
nodes.ListUndocumented
def parse_macro(self):

Undocumented

Returns
nodes.MacroUndocumented
def parse_math1(self):

Undocumented

Returns
nodes.ExprUndocumented
def parse_math2(self):

Undocumented

Returns
nodes.ExprUndocumented
def parse_not(self):

Undocumented

Returns
nodes.ExprUndocumented
def parse_or(self):

Undocumented

Returns
nodes.ExprUndocumented
def parse_postfix(self, node):

Undocumented

Parameters
node:nodes.ExprUndocumented
Returns
nodes.ExprUndocumented
def parse_pow(self):

Undocumented

Returns
nodes.ExprUndocumented
def parse_primary(self):

Undocumented

Returns
nodes.ExprUndocumented
def parse_print(self):

Undocumented

Returns
nodes.OutputUndocumented
def parse_set(self):
Parse an assign statement.
Returns
t.Union[nodes.Assign, nodes.AssignBlock]Undocumented
def parse_signature(self, node):

Undocumented

Parameters
node:_MacroCallUndocumented
def parse_statement(self):
Parse a single statement.
Returns
t.Union[nodes.Node, t.List[nodes.Node]]Undocumented
def parse_subscribed(self):

Undocumented

Returns
nodes.ExprUndocumented
def parse_subscript(self, node):

Undocumented

Parameters
node:nodes.ExprUndocumented
Returns
t.Union[nodes.Getattr, nodes.Getitem]Undocumented
def parse_test(self, node):

Undocumented

Parameters
node:nodes.ExprUndocumented
Returns
nodes.ExprUndocumented
def parse_unary(self, with_filter=True):

Undocumented

Parameters
with​_filter:boolUndocumented
Returns
nodes.ExprUndocumented
def parse_with(self):

Undocumented

Returns
nodes.WithUndocumented
def subparse(self, end_tokens=None):

Undocumented

Parameters
end​_tokens:t.Optional[t.Tuple[str, ...]]Undocumented
Returns
t.List[nodes.Node]Undocumented
_end_token_stack: t.List[t.Tuple[str, ...]] =

Undocumented

_last_identifier: int =

Undocumented

_tag_stack: t.List[str] =

Undocumented

closed: bool =

Undocumented

environment =

Undocumented

extensions: t.Dict[str, t.Callable[[Parser], t.Union[nodes.Node, t.List[nodes.Node]]]] =

Undocumented