Parse delimiter pattern.
(
self,
m: re.Match[str],
data: str,
remainder: tuple[str, str] | None
)
| 919 | return el, start, end |
| 920 | |
| 921 | def _handle_match( |
| 922 | self, |
| 923 | m: re.Match[str], |
| 924 | data: str, |
| 925 | remainder: tuple[str, str] | None |
| 926 | ) -> tuple[etree.Element | None, int | None, int | None]: |
| 927 | """Parse delimiter pattern.""" |
| 928 | |
| 929 | # If the cache is not empty, it is possible to receive text that we already evaluated |
| 930 | # between found regions, ignore such text. |
| 931 | if self.cache and isinstance(data, EvaluatedText): |
| 932 | skip = self.delimiters[m.group(0)].bad.match(data, m.start(0)) |
| 933 | return None, m.start(0), skip.end(0) if skip else m.end(0) |
| 934 | |
| 935 | # Do we have entries we haven't returned yet? |
| 936 | if self.cache: |
| 937 | return self.get_cached_result(m.start(0)) |
| 938 | |
| 939 | # If token is not an opening, quit |
| 940 | # If this is the rest of a closing run that was partially consumed, the placeholder directly before it can |
| 941 | # change how the run is classified, but the run should be classified by what actually preceded it. |
| 942 | text = data |
| 943 | pos = m.start(0) |
| 944 | if remainder is not None and pos and data[pos - 1] == '\x03' and data[pos] == remainder[0]: |
| 945 | text = f'{data[:pos - 1]}{remainder[1]}{data[pos:]}' |
| 946 | |
| 947 | m2 = self.get_match(text, pos) |
| 948 | if m2 is None or m2.lastgroup[0] == 'e': # type: ignore[index] |
| 949 | if m2 is None: |
| 950 | delim = self.delimiters[m.group(0)] |
| 951 | m2 = delim.bad.match(data, m.start(0)) |
| 952 | # Advance past the full length of the delimiter found |
| 953 | return None, m2.start(0), m2.end(0) |
| 954 | |
| 955 | # Get the stack and regions |
| 956 | token = data[m.start(0)] |
| 957 | delim = self.delimiters[token] |
| 958 | stack = delim.stack |
| 959 | |
| 960 | start = m2.start(0) |
| 961 | end = m2.end(0) |
| 962 | length = end - start |
| 963 | |
| 964 | # Double needs at least a size of 2 |
| 965 | if delim.double and length < 2: |
| 966 | return None, m.start(0), m.end(0) |
| 967 | |
| 968 | is_ambiguous = m2.lastgroup[0] != 's' # type: ignore[index] |
| 969 | stack.append((start, start + length, is_ambiguous, length, length)) |
| 970 | |
| 971 | # Pair tokens until the stack is empty or we can no longer find tokens. |
| 972 | while any(d.stack for d in self.delimiters.values()): |
| 973 | m2 = self.search(data, end) |
| 974 | if m2 is None: |
| 975 | break |
| 976 | |
| 977 | token = data[m2.start(0)] |
| 978 | delim = self.delimiters[token] |
no test coverage detected