Works like `parse_expression` but if multiple expressions are delimited by a comma a :class:`~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
(
self,
simplified: bool = False,
with_condexpr: bool = True,
extra_end_rules: t.Optional[t.Tuple[str, ...]] = None,
explicit_parentheses: bool = False,
with_namespace: bool = False,
)
| 683 | return node |
| 684 | |
| 685 | def parse_tuple( |
| 686 | self, |
| 687 | simplified: bool = False, |
| 688 | with_condexpr: bool = True, |
| 689 | extra_end_rules: t.Optional[t.Tuple[str, ...]] = None, |
| 690 | explicit_parentheses: bool = False, |
| 691 | with_namespace: bool = False, |
| 692 | ) -> t.Union[nodes.Tuple, nodes.Expr]: |
| 693 | """Works like `parse_expression` but if multiple expressions are |
| 694 | delimited by a comma a :class:`~jinja2.nodes.Tuple` node is created. |
| 695 | This method could also return a regular expression instead of a tuple |
| 696 | if no commas where found. |
| 697 | |
| 698 | The default parsing mode is a full tuple. If `simplified` is `True` |
| 699 | only names and literals are parsed; ``with_namespace`` allows namespace |
| 700 | attr refs as well. The `no_condexpr` parameter is forwarded to |
| 701 | :meth:`parse_expression`. |
| 702 | |
| 703 | Because tuples do not require delimiters and may end in a bogus comma |
| 704 | an extra hint is needed that marks the end of a tuple. For example |
| 705 | for loops support tuples between `for` and `in`. In that case the |
| 706 | `extra_end_rules` is set to ``['name:in']``. |
| 707 | |
| 708 | `explicit_parentheses` is true if the parsing was triggered by an |
| 709 | expression in parentheses. This is used to figure out if an empty |
| 710 | tuple is a valid expression or not. |
| 711 | """ |
| 712 | lineno = self.stream.current.lineno |
| 713 | if simplified: |
| 714 | |
| 715 | def parse() -> nodes.Expr: |
| 716 | return self.parse_primary(with_namespace=with_namespace) |
| 717 | |
| 718 | else: |
| 719 | |
| 720 | def parse() -> nodes.Expr: |
| 721 | return self.parse_expression(with_condexpr=with_condexpr) |
| 722 | |
| 723 | args: t.List[nodes.Expr] = [] |
| 724 | is_tuple = False |
| 725 | |
| 726 | while True: |
| 727 | if args: |
| 728 | self.stream.expect("comma") |
| 729 | if self.is_tuple_end(extra_end_rules): |
| 730 | break |
| 731 | args.append(parse()) |
| 732 | if self.stream.current.type == "comma": |
| 733 | is_tuple = True |
| 734 | else: |
| 735 | break |
| 736 | lineno = self.stream.current.lineno |
| 737 | |
| 738 | if not is_tuple: |
| 739 | if args: |
| 740 | return args[0] |
| 741 | |
| 742 | # if we don't have explicit parentheses, an empty tuple is |
no test coverage detected