Regex fallback for Pascal/Delphi extraction when tree-sitter-pascal is unavailable. Produces the same node/edge schema as the tree-sitter pass.
(path: Path)
| 13195 | |
| 13196 | |
| 13197 | def _extract_pascal_regex(path: Path) -> dict: |
| 13198 | """Regex fallback for Pascal/Delphi extraction when tree-sitter-pascal |
| 13199 | is unavailable. Produces the same node/edge schema as the tree-sitter pass. |
| 13200 | """ |
| 13201 | try: |
| 13202 | raw = path.read_text(encoding="utf-8", errors="replace") |
| 13203 | except Exception as exc: |
| 13204 | return {"nodes": [], "edges": [], "error": str(exc)} |
| 13205 | |
| 13206 | str_path = str(path) |
| 13207 | stem = _file_stem(path) |
| 13208 | nodes: list[dict] = [] |
| 13209 | edges: list[dict] = [] |
| 13210 | seen_ids: set[str] = set() |
| 13211 | seen_call_pairs: set[tuple[str, str]] = set() |
| 13212 | |
| 13213 | def _add_node(nid: str, label: str, line: int) -> None: |
| 13214 | if nid not in seen_ids: |
| 13215 | seen_ids.add(nid) |
| 13216 | nodes.append({ |
| 13217 | "id": nid, |
| 13218 | "label": label, |
| 13219 | "file_type": "code", |
| 13220 | "source_file": str_path, |
| 13221 | "source_location": f"L{line}", |
| 13222 | }) |
| 13223 | |
| 13224 | def _add_edge(src: str, tgt: str, relation: str, line: int, context: str | None = None) -> None: |
| 13225 | edge: dict = { |
| 13226 | "source": src, |
| 13227 | "target": tgt, |
| 13228 | "relation": relation, |
| 13229 | "confidence": "EXTRACTED", |
| 13230 | "source_file": str_path, |
| 13231 | "source_location": f"L{line}", |
| 13232 | "weight": 1.0, |
| 13233 | } |
| 13234 | if context: |
| 13235 | edge["context"] = context |
| 13236 | edges.append(edge) |
| 13237 | |
| 13238 | def _lineno(text: str, offset: int) -> int: |
| 13239 | return text.count("\n", 0, offset) + 1 |
| 13240 | |
| 13241 | file_nid = _make_id(str_path) |
| 13242 | _add_node(file_nid, path.name, 1) |
| 13243 | |
| 13244 | stripped = _pascal_strip_comments(raw) |
| 13245 | |
| 13246 | # Module header |
| 13247 | module_nid = file_nid |
| 13248 | mod_m = _PAS_MODULE_RE.search(stripped) |
| 13249 | if mod_m: |
| 13250 | mod_name = mod_m.group(2) |
| 13251 | module_nid = _make_id(stem, mod_name) |
| 13252 | _add_node(module_nid, mod_name, _lineno(stripped, mod_m.start())) |
| 13253 | _add_edge(file_nid, module_nid, "contains", _lineno(stripped, mod_m.start())) |
| 13254 |
no test coverage detected