|
8 | 8 | from ...types import Fragment, FragmentId |
9 | 9 | from ..base import EdgeBuilder, EdgeDict |
10 | 10 |
|
11 | | -_RUST_USE_STMT_RE = re.compile(r"^\s*use\s+(.+?)\s*;", re.MULTILINE) |
| 11 | +_RUST_USE_STMT_RE = re.compile(r"^\s*use\s+([^;\n]+?)\s*;", re.MULTILINE) |
12 | 12 | _RUST_MOD_RE = re.compile(r"^\s*(?:pub(?:\([^)]*\))?\s+)?mod\s+([a-z_][a-z0-9_]*)\s*[;{]", re.MULTILINE) |
13 | 13 |
|
14 | 14 | _RUST_FN_RE = re.compile(r"^\s*(?:pub(?:\([^)]*\))?\s+)?(?:async\s+)?fn\s+([a-z_][a-z0-9_]*)", re.MULTILINE) |
@@ -126,45 +126,54 @@ def _is_rust_file(path: Path) -> bool: |
126 | 126 | _MAX_USE_TREE_DEPTH = 10 |
127 | 127 |
|
128 | 128 |
|
129 | | -def _parse_use_tree(text: str, _depth: int = 0) -> list[str]: |
130 | | - if _depth > _MAX_USE_TREE_DEPTH: |
131 | | - return [] |
132 | | - text = re.sub(r"^(?:crate|self|super)::", "", text.strip()) |
133 | | - if "{" not in text: |
134 | | - return [text] if text else [] |
135 | | - brace_pos = text.index("{") |
136 | | - prefix = text[:brace_pos].rstrip(":") |
137 | | - inner = text[brace_pos + 1 :] |
| 129 | +def _find_matching_brace(inner: str) -> int: |
138 | 130 | depth = 1 |
139 | | - end = 0 |
140 | 131 | for i, ch in enumerate(inner): |
141 | 132 | if ch == "{": |
142 | 133 | depth += 1 |
143 | 134 | elif ch == "}": |
144 | 135 | depth -= 1 |
145 | 136 | if depth == 0: |
146 | | - end = i |
147 | | - break |
148 | | - items_str = inner[:end] |
149 | | - results: list[str] = [] |
| 137 | + return i |
| 138 | + return 0 |
| 139 | + |
| 140 | + |
| 141 | +def _split_brace_items(items_str: str) -> list[str]: |
| 142 | + items: list[str] = [] |
150 | 143 | current: list[str] = [] |
151 | | - d = 0 |
| 144 | + depth = 0 |
152 | 145 | for ch in items_str: |
153 | 146 | if ch == "{": |
154 | | - d += 1 |
| 147 | + depth += 1 |
155 | 148 | current.append(ch) |
156 | 149 | elif ch == "}": |
157 | | - d -= 1 |
| 150 | + depth -= 1 |
158 | 151 | current.append(ch) |
159 | | - elif ch == "," and d == 0: |
| 152 | + elif ch == "," and depth == 0: |
160 | 153 | item = "".join(current).strip() |
161 | 154 | if item and item != "self": |
162 | | - results.extend(_parse_use_tree(f"{prefix}::{item}" if prefix else item, _depth + 1)) |
| 155 | + items.append(item) |
163 | 156 | current = [] |
164 | 157 | else: |
165 | 158 | current.append(ch) |
166 | 159 | item = "".join(current).strip() |
167 | 160 | if item and item != "self": |
| 161 | + items.append(item) |
| 162 | + return items |
| 163 | + |
| 164 | + |
| 165 | +def _parse_use_tree(text: str, _depth: int = 0) -> list[str]: |
| 166 | + if _depth > _MAX_USE_TREE_DEPTH: |
| 167 | + return [] |
| 168 | + text = re.sub(r"^(?:crate|self|super)::", "", text.strip()) |
| 169 | + if "{" not in text: |
| 170 | + return [text] if text else [] |
| 171 | + brace_pos = text.index("{") |
| 172 | + prefix = text[:brace_pos].rstrip(":") |
| 173 | + inner = text[brace_pos + 1 :] |
| 174 | + end = _find_matching_brace(inner) |
| 175 | + results: list[str] = [] |
| 176 | + for item in _split_brace_items(inner[:end]): |
168 | 177 | results.extend(_parse_use_tree(f"{prefix}::{item}" if prefix else item, _depth + 1)) |
169 | 178 | return results |
170 | 179 |
|
|
0 commit comments