MCPcopy
hub / github.com/psf/black / generate_matches

Method generate_matches

src/blib2to3/pytree.py:803–846  ·  view source on GitHub ↗

Generator yielding matches for a sequence of nodes. Args: nodes: sequence of nodes Yields: (count, results) tuples where: count: the match comprises nodes[:count]; results: dict containing named submatches.

(self, nodes)

Source from the content-addressed store, hash-verified

801 return False
802
803 def generate_matches(self, nodes) -> Iterator[tuple[int, _Results]]:
804 """
805 Generator yielding matches for a sequence of nodes.
806
807 Args:
808 nodes: sequence of nodes
809
810 Yields:
811 (count, results) tuples where:
812 count: the match comprises nodes[:count];
813 results: dict containing named submatches.
814 """
815 if self.content is None:
816 # Shortcut for special case (see __init__.__doc__)
817 for count in range(self.min, 1 + min(len(nodes), self.max)):
818 r = {}
819 if self.name:
820 r[self.name] = nodes[:count]
821 yield count, r
822 elif self.name == "bare_name":
823 yield self._bare_name_matches(nodes)
824 else:
825 # The reason for this is that hitting the recursion limit usually
826 # results in some ugly messages about how RuntimeErrors are being
827 # ignored. We only have to do this on CPython, though, because other
828 # implementations don't have this nasty bug in the first place.
829 if hasattr(sys, "getrefcount"):
830 save_stderr = sys.stderr
831 sys.stderr = StringIO()
832 try:
833 for count, r in self._recursive_matches(nodes, 0):
834 if self.name:
835 r[self.name] = nodes[:count]
836 yield count, r
837 except RuntimeError:
838 # We fall back to the iterative pattern matching scheme if the recursive
839 # scheme hits the recursion limit.
840 for count, r in self._iterative_matches(nodes):
841 if self.name:
842 r[self.name] = nodes[:count]
843 yield count, r
844 finally:
845 if hasattr(sys, "getrefcount"):
846 sys.stderr = save_stderr
847
848 def _iterative_matches(self, nodes) -> Iterator[tuple[int, _Results]]:
849 """Helper to iteratively yield the matches."""

Callers 1

match_seqMethod · 0.95

Calls 3

_bare_name_matchesMethod · 0.95
_recursive_matchesMethod · 0.95
_iterative_matchesMethod · 0.95

Tested by

no test coverage detected