Find candidates lexically; recovered trees must later confirm the scope.
(source: bytes, groups: list[dict])
| 3981 | |
| 3982 | |
| 3983 | def _cpp_expression_conditional_groups(source: bytes, groups: list[dict]) -> set[int]: |
| 3984 | """Find candidates lexically; recovered trees must later confirm the scope.""" |
| 3985 | code = bytearray(source) |
| 3986 | for match, end in _cpp_conditional_tokens(source): |
| 3987 | code[match.start():end] = re.sub(rb"[^\r\n]", b" ", source[match.start():end]) |
| 3988 | tokens = iter(re.finditer(rb"[A-Za-z_][A-Za-z_0-9]*|[^\s]", code)) |
| 3989 | token = next(tokens, None) |
| 3990 | stack: list[tuple[bytes, bool]] = [] |
| 3991 | previous = b"" |
| 3992 | statement = 0 |
| 3993 | selected = set() |
| 3994 | for index, group in enumerate(groups): |
| 3995 | while token is not None and token.start() < group["start_byte"]: |
| 3996 | value = token.group() |
| 3997 | if value in (b"(", b"[", b"{"): |
| 3998 | expression = value == b"(" |
| 3999 | if value == b"{": |
| 4000 | prefix = bytes(code[statement:token.start()]) |
| 4001 | expression = (previous in (b"=", b"return", b"co_return") |
| 4002 | or (previous in (b",", b"{") and bool(stack) and stack[-1][1]) |
| 4003 | or ((re.fullmatch(rb"[A-Za-z_][A-Za-z_0-9]*|>|\]", previous) is not None) |
| 4004 | and previous not in (b"else", b"try", b"do") |
| 4005 | and not re.search(rb"\b(?:namespace|class|struct|enum|union|extern)\b", prefix))) |
| 4006 | statement = token.end() |
| 4007 | stack.append((value, expression)) |
| 4008 | elif value in (b")", b"]", b"}"): |
| 4009 | if stack: |
| 4010 | stack.pop() |
| 4011 | if value == b"}": |
| 4012 | statement = token.end() |
| 4013 | elif value == b";": |
| 4014 | statement = token.end() |
| 4015 | previous = value |
| 4016 | token = next(tokens, None) |
| 4017 | if (stack and stack[-1][1]) or group.get("parent") in selected: |
| 4018 | selected.add(index) |
| 4019 | return selected |
| 4020 | |
| 4021 | |
| 4022 | def _recover_cpp_conditional_initializers(path: Path, source: bytes, original: dict) -> dict: |
no test coverage detected