| 820 | ) |
| 821 | |
| 822 | def match(self): |
| 823 | if isinstance(self.s, bytes): |
| 824 | self.s = self.s.decode("utf-8") |
| 825 | logger.info(f"matching string {self.s}") |
| 826 | |
| 827 | # limit recursive parsing to a depth of 1 |
| 828 | self.ast = bashlex.parser.parsesingle( |
| 829 | self.s, expansionlimit=1, strictmode=False |
| 830 | ) |
| 831 | if isinstance(self.ast, bashlex.ast.node): |
| 832 | self.visit(self.ast) |
| 833 | assert len(self.group_stack) == 1, ( |
| 834 | "groupstack should contain only shell group after matching" |
| 835 | ) |
| 836 | |
| 837 | # if we only have one command in there and no shell results/expansions, |
| 838 | # reraise the original exception |
| 839 | if ( |
| 840 | len(self.groups) == 2 |
| 841 | and not self.groups[0].results |
| 842 | and self.groups[1].manpage is None |
| 843 | and not self.expansions |
| 844 | ): |
| 845 | raise self.groups[1].error |
| 846 | elif self.ast: |
| 847 | logger.warning( |
| 848 | "ignoring unexpected AST root %r (%s)", |
| 849 | self.ast, |
| 850 | type(self.ast).__name__, |
| 851 | ) |
| 852 | self.ast = None |
| 853 | else: |
| 854 | logger.warning("no AST generated for %r", self.s) |
| 855 | |
| 856 | def debug_match(): |
| 857 | lines = [] |
| 858 | for i, m in enumerate(self.all_matches): |
| 859 | portion = self.s[m.start : m.end] |
| 860 | text = m.text or "" |
| 861 | if len(text) > 40: |
| 862 | text = text[:40] + "..." |
| 863 | lines.append(f"{i}) {portion} = {text}") |
| 864 | return "\n".join(lines) |
| 865 | |
| 866 | self._mark_unparsed_unknown() |
| 867 | |
| 868 | # fix each MatchGroup separately |
| 869 | for group in self.groups: |
| 870 | if group.results: |
| 871 | if getattr(group, "manpage", None): |
| 872 | # ensure that the program part isn't unknown (i.e. it has |
| 873 | # something as its synopsis) |
| 874 | assert not group.results[0].unknown |
| 875 | |
| 876 | group.results = self._merge_adjacent(group.results) |
| 877 | |
| 878 | # add MatchResult.match to existing matches |
| 879 | for i, m in enumerate(group.results): |