This method tokenizes the text and returns the tokens in a generator. Use this method if you just want to tokenize a template. .. versionchanged:: 3.0 Only ``\\n``, ``\\r\\n`` and ``\\r`` are treated as line breaks.
(
self,
source: str,
name: t.Optional[str],
filename: t.Optional[str] = None,
state: t.Optional[str] = None,
)
| 667 | yield Token(lineno, token, value) |
| 668 | |
| 669 | def tokeniter( |
| 670 | self, |
| 671 | source: str, |
| 672 | name: t.Optional[str], |
| 673 | filename: t.Optional[str] = None, |
| 674 | state: t.Optional[str] = None, |
| 675 | ) -> t.Iterator[t.Tuple[int, str, str]]: |
| 676 | """This method tokenizes the text and returns the tokens in a |
| 677 | generator. Use this method if you just want to tokenize a template. |
| 678 | |
| 679 | .. versionchanged:: 3.0 |
| 680 | Only ``\\n``, ``\\r\\n`` and ``\\r`` are treated as line |
| 681 | breaks. |
| 682 | """ |
| 683 | lines = newline_re.split(source)[::2] |
| 684 | |
| 685 | if not self.keep_trailing_newline and lines[-1] == "": |
| 686 | del lines[-1] |
| 687 | |
| 688 | source = "\n".join(lines) |
| 689 | pos = 0 |
| 690 | lineno = 1 |
| 691 | stack = ["root"] |
| 692 | |
| 693 | if state is not None and state != "root": |
| 694 | assert state in ("variable", "block"), "invalid state" |
| 695 | stack.append(state + "_begin") |
| 696 | |
| 697 | statetokens = self.rules[stack[-1]] |
| 698 | source_length = len(source) |
| 699 | balancing_stack: t.List[str] = [] |
| 700 | newlines_stripped = 0 |
| 701 | line_starting = True |
| 702 | |
| 703 | while True: |
| 704 | # tokenizer loop |
| 705 | for regex, tokens, new_state in statetokens: |
| 706 | m = regex.match(source, pos) |
| 707 | |
| 708 | # if no match we try again with the next rule |
| 709 | if m is None: |
| 710 | continue |
| 711 | |
| 712 | # we only match blocks and variables if braces / parentheses |
| 713 | # are balanced. continue parsing with the lower rule which |
| 714 | # is the operator rule. do this only if the end tags look |
| 715 | # like operators |
| 716 | if balancing_stack and tokens in ( |
| 717 | TOKEN_VARIABLE_END, |
| 718 | TOKEN_BLOCK_END, |
| 719 | TOKEN_LINESTATEMENT_END, |
| 720 | ): |
| 721 | continue |
| 722 | |
| 723 | # tuples support more options |
| 724 | if isinstance(tokens, tuple): |
| 725 | groups: t.Sequence[str] = m.groups() |
| 726 |
no test coverage detected