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