Helper class to find comments in a token stream. Can only find comments for gettext calls forwards. Once the comment from line 4 is found, a comment for line 1 will not return a usable value.
| 721 | |
| 722 | |
| 723 | class _CommentFinder: |
| 724 | """Helper class to find comments in a token stream. Can only |
| 725 | find comments for gettext calls forwards. Once the comment |
| 726 | from line 4 is found, a comment for line 1 will not return a |
| 727 | usable value. |
| 728 | """ |
| 729 | |
| 730 | def __init__( |
| 731 | self, tokens: t.Sequence[t.Tuple[int, str, str]], comment_tags: t.Sequence[str] |
| 732 | ) -> None: |
| 733 | self.tokens = tokens |
| 734 | self.comment_tags = comment_tags |
| 735 | self.offset = 0 |
| 736 | self.last_lineno = 0 |
| 737 | |
| 738 | def find_backwards(self, offset: int) -> t.List[str]: |
| 739 | try: |
| 740 | for _, token_type, token_value in reversed( |
| 741 | self.tokens[self.offset : offset] |
| 742 | ): |
| 743 | if token_type in ("comment", "linecomment"): |
| 744 | try: |
| 745 | prefix, comment = token_value.split(None, 1) |
| 746 | except ValueError: |
| 747 | continue |
| 748 | if prefix in self.comment_tags: |
| 749 | return [comment.rstrip()] |
| 750 | return [] |
| 751 | finally: |
| 752 | self.offset = offset |
| 753 | |
| 754 | def find_comments(self, lineno: int) -> t.List[str]: |
| 755 | if not self.comment_tags or self.last_lineno > lineno: |
| 756 | return [] |
| 757 | for idx, (token_lineno, _, _) in enumerate(self.tokens[self.offset :]): |
| 758 | if token_lineno > lineno: |
| 759 | return self.find_backwards(self.offset + idx) |
| 760 | return self.find_backwards(len(self.tokens)) |
| 761 | |
| 762 | |
| 763 | def babel_extract( |