Lines Matching refs:self
52 self,
59 self.environment = environment
60 self.stream = environment._tokenize(source, name, filename, state)
61 self.name = name
62 self.filename = filename
63 self.closed = False
64 self.extensions: t.Dict[
69 self.extensions[tag] = extension.parse
70 self._last_identifier = 0
71 self._tag_stack: t.List[str] = []
72 self._end_token_stack: t.List[t.Tuple[str, ...]] = []
75 self,
85 lineno = self.stream.current.lineno
86 raise exc(msg, lineno, self.name, self.filename)
89 self,
120 if self._tag_stack:
123 f" {self._tag_stack[-1]!r}."
126 self.fail(" ".join(message), lineno)
129 self, name: str, lineno: t.Optional[int] = None
135 self._fail_ut_eof(name, self._end_token_stack, lineno)
138 self,
143 stack = list(self._end_token_stack)
146 self._fail_ut_eof(None, stack, lineno)
149 self, extra_end_rules: t.Optional[t.Tuple[str, ...]] = None
152 if self.stream.current.type in ("variable_end", "block_end", "rparen"):
155 return self.stream.current.test_any(extra_end_rules) # type: ignore
158 def free_identifier(self, lineno: t.Optional[int] = None) -> nodes.InternalName:
160 self._last_identifier += 1
162 nodes.Node.__init__(rv, f"fi{self._last_identifier}", lineno=lineno)
165 def parse_statement(self) -> t.Union[nodes.Node, t.List[nodes.Node]]:
167 token = self.stream.current
169 self.fail("tag name expected", token.lineno)
170 self._tag_stack.append(token.value)
174 f = getattr(self, f"parse_{self.stream.current.value}")
177 return self.parse_call_block()
179 return self.parse_filter_block()
180 ext = self.extensions.get(token.value)
182 return ext(self)
187 self._tag_stack.pop()
189 self.fail_unknown_tag(token.value, token.lineno)
192 self._tag_stack.pop()
195 self, end_tokens: t.Tuple[str, ...], drop_needle: bool = False
207 self.stream.skip_if("colon")
211 self.stream.expect("block_end")
212 result = self.subparse(end_tokens)
216 if self.stream.current.type == "eof":
217 self.fail_eof(end_tokens)
220 next(self.stream)
223 def parse_set(self) -> t.Union[nodes.Assign, nodes.AssignBlock]:
225 lineno = next(self.stream).lineno
226 target = self.parse_assign_target(with_namespace=True)
227 if self.stream.skip_if("assign"):
228 expr = self.parse_tuple()
230 filter_node = self.parse_filter(None)
231 body = self.parse_statements(("name:endset",), drop_needle=True)
234 def parse_for(self) -> nodes.For:
236 lineno = self.stream.expect("name:for").lineno
237 target = self.parse_assign_target(extra_end_rules=("name:in",))
238 self.stream.expect("name:in")
239 iter = self.parse_tuple(
243 if self.stream.skip_if("name:if"):
244 test = self.parse_expression()
245 recursive = self.stream.skip_if("name:recursive")
246 body = self.parse_statements(("name:endfor", "name:else"))
247 if next(self.stream).value == "endfor":
250 else_ = self.parse_statements(("name:endfor",), drop_needle=True)
253 def parse_if(self) -> nodes.If:
255 node = result = nodes.If(lineno=self.stream.expect("name:if").lineno)
257 node.test = self.parse_tuple(with_condexpr=False)
258 node.body = self.parse_statements(("name:elif", "name:else", "name:endif"))
261 token = next(self.stream)
263 node = nodes.If(lineno=self.stream.current.lineno)
267 result.else_ = self.parse_statements(("name:endif",), drop_needle=True)
271 def parse_with(self) -> nodes.With:
272 node = nodes.With(lineno=next(self.stream).lineno)
275 while self.stream.current.type != "block_end":
277 self.stream.expect("comma")
278 target = self.parse_assign_target()
281 self.stream.expect("assign")
282 values.append(self.parse_expression())
285 node.body = self.parse_statements(("name:endwith",), drop_needle=True)
288 def parse_autoescape(self) -> nodes.Scope:
289 node = nodes.ScopedEvalContextModifier(lineno=next(self.stream).lineno)
290 node.options = [nodes.Keyword("autoescape", self.parse_expression())]
291 node.body = self.parse_statements(("name:endautoescape",), drop_needle=True)
294 def parse_block(self) -> nodes.Block:
295 node = nodes.Block(lineno=next(self.stream).lineno)
296 node.name = self.stream.expect("name").value
297 node.scoped = self.stream.skip_if("name:scoped")
298 node.required = self.stream.skip_if("name:required")
303 if self.stream.current.type == "sub":
304 self.fail(
309 node.body = self.parse_statements(("name:endblock",), drop_needle=True)
321 self.fail("Required blocks can only contain comments or whitespace")
323 self.stream.skip_if("name:" + node.name)
326 def parse_extends(self) -> nodes.Extends:
327 node = nodes.Extends(lineno=next(self.stream).lineno)
328 node.template = self.parse_expression()
332 self, node: _ImportInclude, default: bool
334 if self.stream.current.test_any(
336 ) and self.stream.look().test("name:context"):
337 node.with_context = next(self.stream).value == "with"
338 self.stream.skip()
343 def parse_include(self) -> nodes.Include:
344 node = nodes.Include(lineno=next(self.stream).lineno)
345 node.template = self.parse_expression()
346 if self.stream.current.test("name:ignore") and self.stream.look().test(
350 self.stream.skip(2)
353 return self.parse_import_context(node, True)
355 def parse_import(self) -> nodes.Import:
356 node = nodes.Import(lineno=next(self.stream).lineno)
357 node.template = self.parse_expression()
358 self.stream.expect("name:as")
359 node.target = self.parse_assign_target(name_only=True).name
360 return self.parse_import_context(node, False)
362 def parse_from(self) -> nodes.FromImport:
363 node = nodes.FromImport(lineno=next(self.stream).lineno)
364 node.template = self.parse_expression()
365 self.stream.expect("name:import")
369 if self.stream.current.value in {
372 } and self.stream.look().test("name:context"):
373 node.with_context = next(self.stream).value == "with"
374 self.stream.skip()
380 self.stream.expect("comma")
381 if self.stream.current.type == "name":
384 target = self.parse_assign_target(name_only=True)
386 self.fail(
391 if self.stream.skip_if("name:as"):
392 alias = self.parse_assign_target(name_only=True)
396 if parse_context() or self.stream.current.type != "comma":
399 self.stream.expect("name")
404 def parse_signature(self, node: _MacroCall) -> None:
407 self.stream.expect("lparen")
408 while self.stream.current.type != "rparen":
410 self.stream.expect("comma")
411 arg = self.parse_assign_target(name_only=True)
413 if self.stream.skip_if("assign"):
414 defaults.append(self.parse_expression())
416 self.fail("non-default argument follows default argument")
418 self.stream.expect("rparen")
420 def parse_call_block(self) -> nodes.CallBlock:
421 node = nodes.CallBlock(lineno=next(self.stream).lineno)
422 if self.stream.current.type == "lparen":
423 self.parse_signature(node)
428 call_node = self.parse_expression()
430 self.fail("expected call", node.lineno)
432 node.body = self.parse_statements(("name:endcall",), drop_needle=True)
435 def parse_filter_block(self) -> nodes.FilterBlock:
436 node = nodes.FilterBlock(lineno=next(self.stream).lineno)
437 node.filter = self.parse_filter(None, start_inline=True) # type: ignore
438 node.body = self.parse_statements(("name:endfilter",), drop_needle=True)
441 def parse_macro(self) -> nodes.Macro:
442 node = nodes.Macro(lineno=next(self.stream).lineno)
443 node.name = self.parse_assign_target(name_only=True).name
444 self.parse_signature(node)
445 node.body = self.parse_statements(("name:endmacro",), drop_needle=True)
448 def parse_print(self) -> nodes.Output:
449 node = nodes.Output(lineno=next(self.stream).lineno)
451 while self.stream.current.type != "block_end":
453 self.stream.expect("comma")
454 node.nodes.append(self.parse_expression())
459 self, with_tuple: bool = ..., name_only: "te.Literal[True]" = ...
465 self,
474 self,
490 if with_namespace and self.stream.look().type == "dot":
491 token = self.stream.expect("name")
492 next(self.stream) # dot
493 attr = self.stream.expect("name")
496 token = self.stream.expect("name")
500 target = self.parse_tuple(
504 target = self.parse_primary()
509 self.fail(
515 def parse_expression(self, with_condexpr: bool = True) -> nodes.Expr:
521 return self.parse_condexpr()
522 return self.parse_or()
524 def parse_condexpr(self) -> nodes.Expr:
525 lineno = self.stream.current.lineno
526 expr1 = self.parse_or()
529 while self.stream.skip_if("name:if"):
530 expr2 = self.parse_or()
531 if self.stream.skip_if("name:else"):
532 expr3 = self.parse_condexpr()
536 lineno = self.stream.current.lineno
539 def parse_or(self) -> nodes.Expr:
540 lineno = self.stream.current.lineno
541 left = self.parse_and()
542 while self.stream.skip_if("name:or"):
543 right = self.parse_and()
545 lineno = self.stream.current.lineno
548 def parse_and(self) -> nodes.Expr:
549 lineno = self.stream.current.lineno
550 left = self.parse_not()
551 while self.stream.skip_if("name:and"):
552 right = self.parse_not()
554 lineno = self.stream.current.lineno
557 def parse_not(self) -> nodes.Expr:
558 if self.stream.current.test("name:not"):
559 lineno = next(self.stream).lineno
560 return nodes.Not(self.parse_not(), lineno=lineno)
561 return self.parse_compare()
563 def parse_compare(self) -> nodes.Expr:
564 lineno = self.stream.current.lineno
565 expr = self.parse_math1()
568 token_type = self.stream.current.type
570 next(self.stream)
571 ops.append(nodes.Operand(token_type, self.parse_math1()))
572 elif self.stream.skip_if("name:in"):
573 ops.append(nodes.Operand("in", self.parse_math1()))
574 elif self.stream.current.test("name:not") and self.stream.look().test(
577 self.stream.skip(2)
578 ops.append(nodes.Operand("notin", self.parse_math1()))
581 lineno = self.stream.current.lineno
586 def parse_math1(self) -> nodes.Expr:
587 lineno = self.stream.current.lineno
588 left = self.parse_concat()
589 while self.stream.current.type in ("add", "sub"):
590 cls = _math_nodes[self.stream.current.type]
591 next(self.stream)
592 right = self.parse_concat()
594 lineno = self.stream.current.lineno
597 def parse_concat(self) -> nodes.Expr:
598 lineno = self.stream.current.lineno
599 args = [self.parse_math2()]
600 while self.stream.current.type == "tilde":
601 next(self.stream)
602 args.append(self.parse_math2())
607 def parse_math2(self) -> nodes.Expr:
608 lineno = self.stream.current.lineno
609 left = self.parse_pow()
610 while self.stream.current.type in ("mul", "div", "floordiv", "mod"):
611 cls = _math_nodes[self.stream.current.type]
612 next(self.stream)
613 right = self.parse_pow()
615 lineno = self.stream.current.lineno
618 def parse_pow(self) -> nodes.Expr:
619 lineno = self.stream.current.lineno
620 left = self.parse_unary()
621 while self.stream.current.type == "pow":
622 next(self.stream)
623 right = self.parse_unary()
625 lineno = self.stream.current.lineno
628 def parse_unary(self, with_filter: bool = True) -> nodes.Expr:
629 token_type = self.stream.current.type
630 lineno = self.stream.current.lineno
634 next(self.stream)
635 node = nodes.Neg(self.parse_unary(False), lineno=lineno)
637 next(self.stream)
638 node = nodes.Pos(self.parse_unary(False), lineno=lineno)
640 node = self.parse_primary()
641 node = self.parse_postfix(node)
643 node = self.parse_filter_expr(node)
646 def parse_primary(self) -> nodes.Expr:
647 token = self.stream.current
656 next(self.stream)
658 next(self.stream)
661 while self.stream.current.type == "string":
662 buf.append(self.stream.current.value)
663 next(self.stream)
666 next(self.stream)
669 next(self.stream)
670 node = self.parse_tuple(explicit_parentheses=True)
671 self.stream.expect("rparen")
673 node = self.parse_list()
675 node = self.parse_dict()
677 self.fail(f"unexpected {describe_token(token)!r}", token.lineno)
681 self,
705 lineno = self.stream.current.lineno
707 parse = self.parse_primary
709 parse = self.parse_expression
713 return self.parse_expression(with_condexpr=False)
720 self.stream.expect("comma")
721 if self.is_tuple_end(extra_end_rules):
724 if self.stream.current.type == "comma":
728 lineno = self.stream.current.lineno
739 self.fail(
741 f" got {describe_token(self.stream.current)!r}"
746 def parse_list(self) -> nodes.List:
747 token = self.stream.expect("lbracket")
749 while self.stream.current.type != "rbracket":
751 self.stream.expect("comma")
752 if self.stream.current.type == "rbracket":
754 items.append(self.parse_expression())
755 self.stream.expect("rbracket")
758 def parse_dict(self) -> nodes.Dict:
759 token = self.stream.expect("lbrace")
761 while self.stream.current.type != "rbrace":
763 self.stream.expect("comma")
764 if self.stream.current.type == "rbrace":
766 key = self.parse_expression()
767 self.stream.expect("colon")
768 value = self.parse_expression()
770 self.stream.expect("rbrace")
773 def parse_postfix(self, node: nodes.Expr) -> nodes.Expr:
775 token_type = self.stream.current.type
777 node = self.parse_subscript(node)
781 node = self.parse_call(node)
786 def parse_filter_expr(self, node: nodes.Expr) -> nodes.Expr:
788 token_type = self.stream.current.type
790 node = self.parse_filter(node) # type: ignore
791 elif token_type == "name" and self.stream.current.value == "is":
792 node = self.parse_test(node)
796 node = self.parse_call(node)
802 self, node: nodes.Expr
804 token = next(self.stream)
808 attr_token = self.stream.current
809 next(self.stream)
815 self.fail("expected name or number", attr_token.lineno)
820 while self.stream.current.type != "rbracket":
822 self.stream.expect("comma")
823 args.append(self.parse_subscribed())
824 self.stream.expect("rbracket")
830 self.fail("expected subscript expression", token.lineno)
832 def parse_subscribed(self) -> nodes.Expr:
833 lineno = self.stream.current.lineno
836 if self.stream.current.type == "colon":
837 next(self.stream)
840 node = self.parse_expression()
841 if self.stream.current.type != "colon":
843 next(self.stream)
846 if self.stream.current.type == "colon":
848 elif self.stream.current.type not in ("rbracket", "comma"):
849 args.append(self.parse_expression())
853 if self.stream.current.type == "colon":
854 next(self.stream)
855 if self.stream.current.type not in ("rbracket", "comma"):
856 args.append(self.parse_expression())
864 def parse_call_args(self) -> t.Tuple:
865 token = self.stream.expect("lparen")
874 self.fail("invalid syntax for function call expression", token.lineno)
876 while self.stream.current.type != "rparen":
878 self.stream.expect("comma")
881 if self.stream.current.type == "rparen":
884 if self.stream.current.type == "mul":
886 next(self.stream)
887 dyn_args = self.parse_expression()
888 elif self.stream.current.type == "pow":
890 next(self.stream)
891 dyn_kwargs = self.parse_expression()
894 self.stream.current.type == "name"
895 and self.stream.look().type == "assign"
899 key = self.stream.current.value
900 self.stream.skip(2)
901 value = self.parse_expression()
906 args.append(self.parse_expression())
910 self.stream.expect("rparen")
913 def parse_call(self, node: nodes.Expr) -> nodes.Call:
916 token = self.stream.current
917 args, kwargs, dyn_args, dyn_kwargs = self.parse_call_args()
921 self, node: t.Optional[nodes.Expr], start_inline: bool = False
923 while self.stream.current.type == "pipe" or start_inline:
925 next(self.stream)
926 token = self.stream.expect("name")
928 while self.stream.current.type == "dot":
929 next(self.stream)
930 name += "." + self.stream.expect("name").value
931 if self.stream.current.type == "lparen":
932 args, kwargs, dyn_args, dyn_kwargs = self.parse_call_args()
943 def parse_test(self, node: nodes.Expr) -> nodes.Expr:
944 token = next(self.stream)
945 if self.stream.current.test("name:not"):
946 next(self.stream)
950 name = self.stream.expect("name").value
951 while self.stream.current.type == "dot":
952 next(self.stream)
953 name += "." + self.stream.expect("name").value
956 if self.stream.current.type == "lparen":
957 args, kwargs, dyn_args, dyn_kwargs = self.parse_call_args()
958 elif self.stream.current.type in {
966 } and not self.stream.current.test_any("name:else", "name:or", "name:and"):
967 if self.stream.current.test("name:is"):
968 self.fail("You cannot chain multiple tests with is")
969 arg_node = self.parse_primary()
970 arg_node = self.parse_postfix(arg_node)
982 self, end_tokens: t.Optional[t.Tuple[str, ...]] = None
989 self._end_token_stack.append(end_tokens)
998 while self.stream:
999 token = self.stream.current
1003 next(self.stream)
1005 next(self.stream)
1006 add_data(self.parse_tuple(with_condexpr=True))
1007 self.stream.expect("variable_end")
1010 next(self.stream)
1011 if end_tokens is not None and self.stream.current.test_any(
1015 rv = self.parse_statement()
1020 self.stream.expect("block_end")
1027 self._end_token_stack.pop()
1030 def parse(self) -> nodes.Template:
1032 result = nodes.Template(self.subparse(), lineno=1)
1033 result.set_environment(self.environment)