0% found this document useful (0 votes)
8 views9 pages

Parser

The document outlines a parser class for a templating language, detailing methods for parsing various constructs like statements, loops, conditionals, and imports. It includes error handling for unknown tags and unexpected end of templates. The parser also manages extensions and provides functionality to handle different types of nodes in the templating system.

Uploaded by

koveci9131
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views9 pages

Parser

The document outlines a parser class for a templating language, detailing methods for parsing various constructs like statements, loops, conditionals, and imports. It includes error handling for unknown tags and unexpected end of templates. The parser also manages extensions and provides functionality to handle different types of nodes in the templating system.

Uploaded by

koveci9131
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

"""Parse tokens from the lexer into nodes for the compiler.

"""

import typing
import typing as t

from . import nodes


from .exceptions import TemplateAssertionError
from .exceptions import TemplateSyntaxError
from .lexer import describe_token
from .lexer import describe_token_expr

if t.TYPE_CHECKING:
import typing_extensions as te

from .environment import Environment

_ImportInclude = [Link]("_ImportInclude", [Link], [Link])


_MacroCall = [Link]("_MacroCall", [Link], [Link])

_statement_keywords = frozenset(
[
"for",
"if",
"block",
"extends",
"print",
"macro",
"include",
"from",
"import",
"set",
"with",
"autoescape",
]
)
_compare_operators = frozenset(["eq", "ne", "lt", "lteq", "gt", "gteq"])

_math_nodes: [Link][str, [Link][[Link]]] = {


"add": [Link],
"sub": [Link],
"mul": [Link],
"div": [Link],
"floordiv": [Link],
"mod": [Link],
}

class Parser:
"""This is the central parsing class Jinja uses. It's passed to
extensions and can be used to parse expressions or statements.
"""

def __init__(
self,
environment: "Environment",
source: str,
name: [Link][str] = None,
filename: [Link][str] = None,
state: [Link][str] = None,
) -> None:
[Link] = environment
[Link] = environment._tokenize(source, name, filename, state)
[Link] = name
[Link] = filename
[Link] = False
[Link]: [Link][
str, [Link][[Parser], [Link][[Link], [Link][[Link]]]]
] = {}
for extension in environment.iter_extensions():
for tag in [Link]:
[Link][tag] = [Link]
self._last_identifier = 0
self._tag_stack: [Link][str] = []
self._end_token_stack: [Link][[Link][str, ...]] = []

def fail(
self,
msg: str,
lineno: [Link][int] = None,
exc: [Link][TemplateSyntaxError] = TemplateSyntaxError,
) -> "[Link]":
"""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 = [Link]
raise exc(msg, lineno, [Link], [Link])

def _fail_ut_eof(
self,
name: [Link][str],
end_token_stack: [Link][[Link][str, ...]],
lineno: [Link][int],
) -> "[Link]":
expected: [Link][str] = set()
for exprs in end_token_stack:
[Link](map(describe_token_expr, exprs))
if end_token_stack:
currently_looking: [Link][str] = " or ".join(
map(repr, map(describe_token_expr, end_token_stack[-1]))
)
else:
currently_looking = None

if name is None:
message = ["Unexpected end of template."]
else:
message = [f"Encountered unknown tag {name!r}."]

if currently_looking:
if name is not None and name in expected:
[Link](
"You probably made a nesting mistake. Jinja is expecting this tag,"
f" but currently looking for {currently_looking}."
)
else:
[Link](
f"Jinja was looking for the following tags: {currently_looking}."
)

if self._tag_stack:
[Link](
"The innermost block that needs to be closed is"
f" {self._tag_stack[-1]!r}."
)

[Link](" ".join(message), lineno)

def fail_unknown_tag(
self, name: str, lineno: [Link][int] = None
) -> "[Link]":
"""Called if the parser encounters an unknown tag. Tries to fail
with a human readable error message that could help to identify
the problem.
"""
self._fail_ut_eof(name, self._end_token_stack, lineno)

def fail_eof(
self,
end_tokens: [Link][[Link][str, ...]] = None,
lineno: [Link][int] = None,
) -> "[Link]":
"""Like fail_unknown_tag but for end of template situations."""
stack = list(self._end_token_stack)
if end_tokens is not None:
[Link](end_tokens)
self._fail_ut_eof(None, stack, lineno)

def is_tuple_end(
self, extra_end_rules: [Link][[Link][str, ...]] = None
) -> bool:
"""Are we at the end of a tuple?"""
if [Link] in ("variable_end", "block_end", "rparen"):
return True
elif extra_end_rules is not None:
return [Link].test_any(extra_end_rules) # type: ignore
return False

def free_identifier(self, lineno: [Link][int] = None) -> [Link]:


"""Return a new free identifier as :class:`~[Link]`."""
self._last_identifier += 1
rv = object.__new__([Link])
[Link].__init__(rv, f"fi{self._last_identifier}", lineno=lineno)
return rv

def parse_statement(self) -> [Link][[Link], [Link][[Link]]]:


"""Parse a single statement."""
token = [Link]
if [Link] != "name":
[Link]("tag name expected", [Link])
self._tag_stack.append([Link])
pop_tag = True
try:
if [Link] in _statement_keywords:
f = getattr(self, f"parse_{[Link]}")
return f() # type: ignore
if [Link] == "call":
return self.parse_call_block()
if [Link] == "filter":
return self.parse_filter_block()
ext = [Link]([Link])
if ext is not None:
return ext(self)

# did not work out, remove the token we pushed by accident


# from the stack so that the unknown tag fail function can
# produce a proper error message.
self._tag_stack.pop()
pop_tag = False
self.fail_unknown_tag([Link], [Link])
finally:
if pop_tag:
self._tag_stack.pop()

def parse_statements(
self, end_tokens: [Link][str, ...], drop_needle: bool = False
) -> [Link][[Link]]:
"""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.
"""
# the first token may be a colon for python compatibility
[Link].skip_if("colon")

# in the future it would be possible to add whole code sections


# by adding some sort of end of statement token and parsing those here.
[Link]("block_end")
result = [Link](end_tokens)

# we reached the end of the template too early, the subparser


# does not check for this, so we do that now
if [Link] == "eof":
self.fail_eof(end_tokens)

if drop_needle:
next([Link])
return result

def parse_set(self) -> [Link][[Link], [Link]]:


"""Parse an assign statement."""
lineno = next([Link]).lineno
target = self.parse_assign_target(with_namespace=True)
if [Link].skip_if("assign"):
expr = self.parse_tuple()
return [Link](target, expr, lineno=lineno)
filter_node = self.parse_filter(None)
body = self.parse_statements(("name:endset",), drop_needle=True)
return [Link](target, filter_node, body, lineno=lineno)

def parse_for(self) -> [Link]:


"""Parse a for loop."""
lineno = [Link]("name:for").lineno
target = self.parse_assign_target(extra_end_rules=("name:in",))
[Link]("name:in")
iter = self.parse_tuple(
with_condexpr=False, extra_end_rules=("name:recursive",)
)
test = None
if [Link].skip_if("name:if"):
test = self.parse_expression()
recursive = [Link].skip_if("name:recursive")
body = self.parse_statements(("name:endfor", "name:else"))
if next([Link]).value == "endfor":
else_ = []
else:
else_ = self.parse_statements(("name:endfor",), drop_needle=True)
return [Link](target, iter, body, else_, test, recursive, lineno=lineno)

def parse_if(self) -> [Link]:


"""Parse an if construct."""
node = result = [Link](lineno=[Link]("name:if").lineno)
while True:
[Link] = self.parse_tuple(with_condexpr=False)
[Link] = self.parse_statements(("name:elif", "name:else", "name:endif"))
node.elif_ = []
node.else_ = []
token = next([Link])
if [Link]("name:elif"):
node = [Link](lineno=[Link])
result.elif_.append(node)
continue
elif [Link]("name:else"):
result.else_ = self.parse_statements(("name:endif",), drop_needle=True)
break
return result

def parse_with(self) -> [Link]:


node = [Link](lineno=next([Link]).lineno)
targets: [Link][[Link]] = []
values: [Link][[Link]] = []
while [Link] != "block_end":
if targets:
[Link]("comma")
target = self.parse_assign_target()
target.set_ctx("param")
[Link](target)
[Link]("assign")
[Link](self.parse_expression())
[Link] = targets
[Link] = values
[Link] = self.parse_statements(("name:endwith",), drop_needle=True)
return node

def parse_autoescape(self) -> [Link]:


node = [Link](lineno=next([Link]).lineno)
[Link] = [[Link]("autoescape", self.parse_expression())]
[Link] = self.parse_statements(("name:endautoescape",), drop_needle=True)
return [Link]([node])

def parse_block(self) -> [Link]:


node = [Link](lineno=next([Link]).lineno)
[Link] = [Link]("name").value
[Link] = [Link].skip_if("name:scoped")
[Link] = [Link].skip_if("name:required")

# common problem people encounter when switching from django


# to jinja. we do not support hyphens in block names, so let's
# raise a nicer error message in that case.
if [Link] == "sub":
[Link](
"Block names in Jinja have to be valid Python identifiers and may not"
" contain hyphens, use an underscore instead."
)

[Link] = self.parse_statements(("name:endblock",), drop_needle=True)

# enforce that required blocks only contain whitespace or comments


# by asserting that the body, if not empty, is just TemplateData nodes
# with whitespace data
if [Link]:
for body_node in [Link]:
if not isinstance(body_node, [Link]) or any(
not isinstance(output_node, [Link])
or not output_node.[Link]()
for output_node in body_node.nodes
):
[Link]("Required blocks can only contain comments or whitespace")

[Link].skip_if("name:" + [Link])
return node

def parse_extends(self) -> [Link]:


node = [Link](lineno=next([Link]).lineno)
[Link] = self.parse_expression()
return node

def parse_import_context(
self, node: _ImportInclude, default: bool
) -> _ImportInclude:
if [Link].test_any(
"name:with", "name:without"
) and [Link]().test("name:context"):
node.with_context = next([Link]).value == "with"
[Link]()
else:
node.with_context = default
return node

def parse_include(self) -> [Link]:


node = [Link](lineno=next([Link]).lineno)
[Link] = self.parse_expression()
if [Link]("name:ignore") and [Link]().test(
"name:missing"
):
node.ignore_missing = True
[Link](2)
else:
node.ignore_missing = False
return self.parse_import_context(node, True)

def parse_import(self) -> [Link]:


node = [Link](lineno=next([Link]).lineno)
[Link] = self.parse_expression()
[Link]("name:as")
[Link] = self.parse_assign_target(name_only=True).name
return self.parse_import_context(node, False)

def parse_from(self) -> [Link]:


node = [Link](lineno=next([Link]).lineno)
[Link] = self.parse_expression()
[Link]("name:import")
[Link] = []
def parse_context() -> bool:
if [Link] in {
"with",
"without",
} and [Link]().test("name:context"):
node.with_context = next([Link]).value == "with"
[Link]()
return True
return False

while True:
if [Link]:
[Link]("comma")
if [Link] == "name":
if parse_context():
break
target = self.parse_assign_target(name_only=True)
if [Link]("_"):
[Link](
"names starting with an underline can not be imported",
[Link],
exc=TemplateAssertionError,
)
if [Link].skip_if("name:as"):
alias = self.parse_assign_target(name_only=True)
[Link](([Link], [Link]))
else:
[Link]([Link])
if parse_context() or [Link] != "comma":
break
else:
[Link]("name")
if not hasattr(node, "with_context"):
node.with_context = False
return node

def parse_signature(self, node: _MacroCall) -> None:


args = [Link] = []
defaults = [Link] = []
[Link]("lparen")
while [Link] != "rparen":
if args:
[Link]("comma")
arg = self.parse_assign_target(name_only=True)
arg.set_ctx("param")
if [Link].skip_if("assign"):
[Link](self.parse_expression())
elif defaults:
[Link]("non-default argument follows default argument")
[Link](arg)
[Link]("rparen")

def parse_call_block(self) -> [Link]:


node = [Link](lineno=next([Link]).lineno)
if [Link] == "lparen":
self.parse_signature(node)
else:
[Link] = []
[Link] = []

call_node = self.parse_expression()
if not isinstance(call_node, [Link]):
[Link]("expected call", [Link])
[Link] = call_node
[Link] = self.parse_statements(("name:endcall",), drop_needle=True)
return node

def parse_filter_block(self) -> [Link]:


node = [Link](lineno=next([Link]).lineno)
[Link] = self.parse_filter(None, start_inline=True) # type: ignore
[Link] = self.parse_statements(("name:endfilter",), drop_needle=True)
return node

def parse_macro(self) -> [Link]:


node = [Link](lineno=next([Link]).lineno)
[Link] = self.parse_assign_target(name_only=True).name
self.parse_signature(node)
[Link] = self.parse_statements(("name:endmacro",), drop_needle=True)
return node

def parse_print(self) -> [Link]:


node = [Link](lineno=next([Link]).lineno)
[Link] = []
while [Link] != "block_end":
if [Link]:
[Link]("comma")
[Link](self.parse_expression())
return node

@[Link]
def parse_assign_target(
self, with_tuple: bool = ..., name_only: "[Link][True]" = ...
) -> [Link]: ...

@[Link]
def parse_assign_target(
self,
with_tuple: bool = True,
name_only: bool = False,
extra_end_rules: [Link][[Link][str, ...]] = None,
with_namespace: bool = False,
) -> [Link][[Link], [Link], [Link]]: ...

def parse_assign_target(
self,
with_tuple: bool = True,
name_only: bool = False,
extra_end_rules: [Link][[Link][str, ...]] = None,
with_namespace: bool = False,
) -> [Link][[Link], [Link], [Link]]:
"""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.
"""
target: [Link]

if name_only:
token = [Link]("name")
target = [Link]([Link], "store", lineno=[Link])
else:
if with_tuple:
target = self.parse_tuple(
simplified=True,
extra_end_rules=extra_end_rules,
with_namespace=with_namespace,
)
else:
target = self.parse_primary(with_namespace=with_namespace)

target.set_ctx("store")

if not target.can_assign():
[Link](
f"can't assign to {type(target).__name__.lower()!r}", [Link]
)

return target # type: ignore

def parse_expression(self, with_condexpr: bool = True) -> [Link]:


"""Parse an expression. Per default all expressions are parsed, if
the optional `with_condexpr` parameter is set to `False` conditional
expressions are not parsed.
"""
if with_condexpr:
return self.parse_condexpr()
return self.parse_or()

def parse_condexpr(self) -> [Link]:


lineno = [Link]
expr1 = self.parse_or()
expr3: [Link][[Link]]

while [Link].skip_if("name:if"):
expr2 = self.parse_or()
if [Link].skip_if("name:else"):
expr3 = self.parse_condexpr()
else:
expr3 = None
expr1 = [Link](expr2, expr1, expr3, lineno=lineno)
lineno = [Link]
return expr1

def parse_or(self) -> [Link]:


lineno = [Link]
left = self.parse_and()
while [Link].skip_if("name:or"):
right = self.parse_and()
left = [Link](left, right, lineno=lineno)
lineno = [Link]
return left

def parse_and(self) -> [Link]:


lineno = [Link]
left = self.parse_not()
while [Link].skip_if("name:and"):
right = self.parse_not()
left = [Link](left, right, lineno=lineno)
lineno = [Link]
return left

def parse_not(self) -> [Link]:


if [Link]("name:not"):
lineno = next([Link]).lineno
return [Link](self.parse_not(), lineno=lineno)
return self.parse_compare()

def parse_compare(self) -> [Link]:


lineno = [Link]
expr = self.parse_math1()
ops = []
while True:
token_type = [Link]
if token_type in _compare_operators:
next([Link])
[Link]([Link](token_type, self.parse_math1()))
elif [Link].skip_if("name:in"):
[Link]([Link]("in", self.parse_math1()))
elif [Link]("name:not") and [Link]().test(
"name:in"
):
[Link](2)
[Link]([Link]("notin", self.parse_math1()))
else:
break
lineno = [Link]
if not ops:
return expr
return [Link](expr, ops, lineno=lineno)

def parse_math1(self) -> [Link]:


lineno = [Link]
left = self.parse_concat()
while [Link] in ("add", "sub"):
cls = _math_nodes[[Link]]
next([Link])
right = self.parse_concat()
left = cls(left, right, lineno=lineno)
lineno = [Link]
return left

def parse_concat(self) -> [Link]:


lineno = [Link]
args = [self.parse_math2()]
while [Link] == "tilde":
next([Link])
[Link](self.parse_math2())
if len(args) == 1:
return args[0]
return [Link](args, lineno=lineno)

def parse_math2(self) -> [Link]:


lineno = [Link]
left = self.parse_pow()
while [Link] in ("mul", "div", "floordiv", "mod"):
cls = _math_nodes[[Link]]
next([Link])
right = self.parse_pow()
left = cls(left, right, lineno=lineno)
lineno = [Link]
return left

def parse_pow(self) -> [Link]:


lineno = [Link]
left = self.parse_unary()
while [Link] == "pow":
next([Link])
right = self.parse_unary()
left = [Link](left, right, lineno=lineno)
lineno = [Link]
return left

def parse_unary(self, with_filter: bool = True) -> [Link]:


token_type = [Link]
lineno = [Link]
node: [Link]

if token_type == "sub":
next([Link])
node = [Link](self.parse_unary(False), lineno=lineno)
elif token_type == "add":
next([Link])
node = [Link](self.parse_unary(False), lineno=lineno)
else:
node = self.parse_primary()
node = self.parse_postfix(node)
if with_filter:
node = self.parse_filter_expr(node)
return node

def parse_primary(self, with_namespace: bool = False) -> [Link]:


"""Parse a name or literal value. If ``with_namespace`` is enabled, also
parse namespace attr refs, for use in assignments."""
token = [Link]
node: [Link]
if [Link] == "name":
next([Link])
if [Link] in ("true", "false", "True", "False"):
node = [Link]([Link] in ("true", "True"), lineno=[Link])
elif [Link] in ("none", "None"):
node = [Link](None, lineno=[Link])
elif with_namespace and [Link] == "dot":
# If namespace attributes are allowed at this point, and the next
# token is a dot, produce a namespace reference.
next([Link])
attr = [Link]("name")
node = [Link]([Link], [Link], lineno=[Link])
else:
node = [Link]([Link], "load", lineno=[Link])
elif [Link] == "string":
next([Link])
buf = [[Link]]
lineno = [Link]
while [Link] == "string":
[Link]([Link])
next([Link])
node = [Link]("".join(buf), lineno=lineno)
elif [Link] in ("integer", "float"):
next([Link])
node = [Link]([Link], lineno=[Link])
elif [Link] == "lparen":
next([Link])
node = self.parse_tuple(explicit_parentheses=True)
[Link]("rparen")
elif [Link] == "lbracket":
node = self.parse_list()
elif [Link] == "lbrace":
node = self.parse_dict()
else:
[Link](f"unexpected {describe_token(token)!r}", [Link])
return node

def parse_tuple(
self,
simplified: bool = False,
with_condexpr: bool = True,
extra_end_rules: [Link][[Link][str, ...]] = None,
explicit_parentheses: bool = False,
with_namespace: bool = False,
) -> [Link][[Link], [Link]]:
"""Works like `parse_expression` but if multiple expressions are
delimited by a comma a :class:`~[Link]` 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; ``with_namespace`` allows namespace
attr refs as well. The `no_condexpr` parameter is forwarded to
:meth:`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.
"""
lineno = [Link]
if simplified:

def parse() -> [Link]:


return self.parse_primary(with_namespace=with_namespace)

else:

def parse() -> [Link]:


return self.parse_expression(with_condexpr=with_condexpr)

args: [Link][[Link]] = []
is_tuple = False

while True:
if args:
[Link]("comma")
if self.is_tuple_end(extra_end_rules):
break
[Link](parse())
if [Link] == "comma":
is_tuple = True
else:
break
lineno = [Link]

if not is_tuple:
if args:
return args[0]

# if we don't have explicit parentheses, an empty tuple is


# not a valid expression. This would mean nothing (literally
# nothing) in the spot of an expression would be an empty
# tuple.
if not explicit_parentheses:
[Link](
"Expected an expression,"
f" got {describe_token([Link])!r}"
)

return [Link](args, "load", lineno=lineno)

def parse_list(self) -> [Link]:


token = [Link]("lbracket")
items: [Link][[Link]] = []
while [Link] != "rbracket":
if items:
[Link]("comma")
if [Link] == "rbracket":
break
[Link](self.parse_expression())
[Link]("rbracket")
return [Link](items, lineno=[Link])

def parse_dict(self) -> [Link]:


token = [Link]("lbrace")
items: [Link][[Link]] = []
while [Link] != "rbrace":
if items:
[Link]("comma")
if [Link] == "rbrace":
break
key = self.parse_expression()
[Link]("colon")
value = self.parse_expression()
[Link]([Link](key, value, lineno=[Link]))
[Link]("rbrace")
return [Link](items, lineno=[Link])

def parse_postfix(self, node: [Link]) -> [Link]:


while True:
token_type = [Link]
if token_type == "dot" or token_type == "lbracket":
node = self.parse_subscript(node)
# calls are valid both after postfix expressions (getattr
# and getitem) as well as filters and tests
elif token_type == "lparen":
node = self.parse_call(node)
else:
break
return node

def parse_filter_expr(self, node: [Link]) -> [Link]:


while True:
token_type = [Link]
if token_type == "pipe":
node = self.parse_filter(node) # type: ignore
elif token_type == "name" and [Link] == "is":
node = self.parse_test(node)
# calls are valid both after postfix expressions (getattr
# and getitem) as well as filters and tests
elif token_type == "lparen":
node = self.parse_call(node)
else:
break
return node

def parse_subscript(
self, node: [Link]
) -> [Link][[Link], [Link]]:
token = next([Link])
arg: [Link]

if [Link] == "dot":
attr_token = [Link]
next([Link])
if attr_token.type == "name":
return [Link](
node, attr_token.value, "load", lineno=[Link]
)
elif attr_token.type != "integer":
[Link]("expected name or number", attr_token.lineno)
arg = [Link](attr_token.value, lineno=attr_token.lineno)
return [Link](node, arg, "load", lineno=[Link])
if [Link] == "lbracket":
args: [Link][[Link]] = []
while [Link] != "rbracket":
if args:
[Link]("comma")
[Link](self.parse_subscribed())
[Link]("rbracket")
if len(args) == 1:
arg = args[0]
else:
arg = [Link](args, "load", lineno=[Link])
return [Link](node, arg, "load", lineno=[Link])
[Link]("expected subscript expression", [Link])

def parse_subscribed(self) -> [Link]:


lineno = [Link]
args: [Link][[Link][[Link]]]

if [Link] == "colon":
next([Link])
args = [None]
else:
node = self.parse_expression()
if [Link] != "colon":
return node
next([Link])
args = [node]

if [Link] == "colon":
[Link](None)
elif [Link] not in ("rbracket", "comma"):
[Link](self.parse_expression())
else:
[Link](None)

if [Link] == "colon":
next([Link])
if [Link] not in ("rbracket", "comma"):
[Link](self.parse_expression())
else:
[Link](None)
else:
[Link](None)

return [Link](lineno=lineno, *args) # noqa: B026

def parse_call_args(
self,
) -> [Link][
[Link][[Link]],
[Link][[Link]],
[Link][[Link]],
[Link][[Link]],
]:
token = [Link]("lparen")
args = []
kwargs = []
dyn_args = None
dyn_kwargs = None
require_comma = False

def ensure(expr: bool) -> None:


if not expr:
[Link]("invalid syntax for function call expression", [Link])

while [Link] != "rparen":


if require_comma:
[Link]("comma")

# support for trailing comma


if [Link] == "rparen":
break

if [Link] == "mul":
ensure(dyn_args is None and dyn_kwargs is None)
next([Link])
dyn_args = self.parse_expression()
elif [Link] == "pow":
ensure(dyn_kwargs is None)
next([Link])
dyn_kwargs = self.parse_expression()
else:
if (
[Link] == "name"
and [Link]().type == "assign"
):
# Parsing a kwarg
ensure(dyn_kwargs is None)
key = [Link]
[Link](2)
value = self.parse_expression()
[Link]([Link](key, value, lineno=[Link]))
else:
# Parsing an arg
ensure(dyn_args is None and dyn_kwargs is None and not kwargs)
[Link](self.parse_expression())

require_comma = True

[Link]("rparen")
return args, kwargs, dyn_args, dyn_kwargs

def parse_call(self, node: [Link]) -> [Link]:


# The lparen will be expected in parse_call_args, but the lineno
# needs to be recorded before the stream is advanced.
token = [Link]
args, kwargs, dyn_args, dyn_kwargs = self.parse_call_args()
return [Link](node, args, kwargs, dyn_args, dyn_kwargs, lineno=[Link])

def parse_filter(
self, node: [Link][[Link]], start_inline: bool = False
) -> [Link][[Link]]:
while [Link] == "pipe" or start_inline:
if not start_inline:
next([Link])
token = [Link]("name")
name = [Link]
while [Link] == "dot":
next([Link])
name += "." + [Link]("name").value
if [Link] == "lparen":
args, kwargs, dyn_args, dyn_kwargs = self.parse_call_args()
else:
args = []
kwargs = []
dyn_args = dyn_kwargs = None
node = [Link](
node, name, args, kwargs, dyn_args, dyn_kwargs, lineno=[Link]
)
start_inline = False
return node

def parse_test(self, node: [Link]) -> [Link]:


token = next([Link])
if [Link]("name:not"):
next([Link])
negated = True
else:
negated = False
name = [Link]("name").value
while [Link] == "dot":
next([Link])
name += "." + [Link]("name").value
dyn_args = dyn_kwargs = None
kwargs: [Link][[Link]] = []
if [Link] == "lparen":
args, kwargs, dyn_args, dyn_kwargs = self.parse_call_args()
elif [Link] in {
"name",
"string",
"integer",
"float",
"lparen",
"lbracket",
"lbrace",
} and not [Link].test_any("name:else", "name:or", "name:and"):
if [Link]("name:is"):
[Link]("You cannot chain multiple tests with is")
arg_node = self.parse_primary()
arg_node = self.parse_postfix(arg_node)
args = [arg_node]
else:
args = []
node = [Link](
node, name, args, kwargs, dyn_args, dyn_kwargs, lineno=[Link]
)
if negated:
node = [Link](node, lineno=[Link])
return node

def subparse(
self, end_tokens: [Link][[Link][str, ...]] = None
) -> [Link][[Link]]:
body: [Link][[Link]] = []
data_buffer: [Link][[Link]] = []
add_data = data_buffer.append

if end_tokens is not None:


self._end_token_stack.append(end_tokens)

def flush_data() -> None:


if data_buffer:
lineno = data_buffer[0].lineno
[Link]([Link](data_buffer[:], lineno=lineno))
del data_buffer[:]

try:
while [Link]:
token = [Link]
if [Link] == "data":
if [Link]:
add_data([Link]([Link], lineno=[Link]))
next([Link])
elif [Link] == "variable_begin":
next([Link])
add_data(self.parse_tuple(with_condexpr=True))
[Link]("variable_end")
elif [Link] == "block_begin":
flush_data()
next([Link])
if end_tokens is not None and [Link].test_any(
*end_tokens
):
return body
rv = self.parse_statement()
if isinstance(rv, list):
[Link](rv)
else:
[Link](rv)
[Link]("block_end")
else:
raise AssertionError("internal parsing error")

flush_data()
finally:
if end_tokens is not None:
self._end_token_stack.pop()
return body

def parse(self) -> [Link]:


"""Parse the whole template into a `Template` node."""
result = [Link]([Link](), lineno=1)
result.set_environment([Link])
return result

You might also like