|
28 | 28 |
|
29 | 29 | logger = logging.getLogger(__name__) |
30 | 30 |
|
| 31 | +# `_resolve_lean_name` result: no declaration has this name (try a shorter one). |
| 32 | +_LEAN_NO_MATCH = object() |
| 33 | + |
31 | 34 |
|
32 | 35 | class TimeoutError(BaseException): |
33 | 36 | """Raised when file parsing exceeds timeout. |
@@ -76,6 +79,7 @@ def __init__(self): |
76 | 79 | self._dart_unscoped = False |
77 | 80 | self._go_visible: dict[str, set[str]] = {} |
78 | 81 | self._go_index = None |
| 82 | + self._reset_lean_state() |
79 | 83 | self.call_relationships: list[CallRelationship] = [] |
80 | 84 | self._python_project_modules: set = set() |
81 | 85 | self._python_external_import_roots: set = set() |
@@ -108,6 +112,7 @@ def analyze_code_files(self, code_files: list[dict], base_dir: str) -> dict: |
108 | 112 | self._dart_unscoped = False |
109 | 113 | self._go_visible = {} |
110 | 114 | self._go_index = None |
| 115 | + self._reset_lean_state() |
111 | 116 |
|
112 | 117 | files_analyzed = 0 |
113 | 118 | files_failed = 0 |
@@ -293,6 +298,8 @@ def _analyze_code_file(self, repo_dir: str, file_info: dict): |
293 | 298 | self._analyze_go_file(file_path, content, repo_dir) |
294 | 299 | elif language == "zig": |
295 | 300 | self._analyze_zig_file(file_path, content, repo_dir) |
| 301 | + elif language == "lean": |
| 302 | + self._analyze_lean_file(file_path, content, repo_dir) |
296 | 303 | elif language == "vue": |
297 | 304 | self._analyze_vue_file(file_path, content, repo_dir) |
298 | 305 | # else: |
@@ -668,6 +675,145 @@ def _analyze_vue_file(self, file_path: str, content: str, repo_dir: str): |
668 | 675 | except Exception: |
669 | 676 | logger.exception(f"Failed to analyze Vue file {file_path}") |
670 | 677 |
|
| 678 | + def _reset_lean_state(self) -> None: |
| 679 | + # Relative path -> imported module names, per Lean file. |
| 680 | + self._lean_imports: dict[str, list[str]] = {} |
| 681 | + # Node id -> (namespace prefixes innermost first, open namespaces). |
| 682 | + self._lean_scopes: dict[str, tuple[list[str], list[str]]] = {} |
| 683 | + # Constructor/field FQN -> ids of the structures or inductives it names. |
| 684 | + self._lean_aliases: dict[str, list[str]] = defaultdict(list) |
| 685 | + self._lean_private: set[str] = set() |
| 686 | + self._lean_index: dict[str, list[str]] | None = None |
| 687 | + self._lean_modules: dict[str, set[str]] = {} |
| 688 | + self._lean_visible: dict[str, frozenset[str]] = {} |
| 689 | + |
| 690 | + def _analyze_lean_file(self, file_path: str, content: str, repo_dir: str): |
| 691 | + """ |
| 692 | + Analyze a Lean 4 file. |
| 693 | +
|
| 694 | + Besides components and references, records the file's imports, each |
| 695 | + declaration's namespaces and open namespaces, and the constructor and |
| 696 | + field names of its structures, for Lean resolution by exact name. |
| 697 | +
|
| 698 | + Args: |
| 699 | + file_path: Path to the Lean file |
| 700 | + content: File content string |
| 701 | + repo_dir: Repository base directory |
| 702 | + """ |
| 703 | + from codewiki.src.be.dependency_analyzer.analyzers.lean import TreeSitterLeanAnalyzer |
| 704 | + |
| 705 | + try: |
| 706 | + analyzer = TreeSitterLeanAnalyzer(str(file_path), content, repo_path=repo_dir) |
| 707 | + |
| 708 | + for func in analyzer.nodes: |
| 709 | + self.functions[func.id] = func |
| 710 | + |
| 711 | + self.call_relationships.extend(analyzer.call_relationships) |
| 712 | + self._lean_imports[analyzer.relative_path] = analyzer.imports |
| 713 | + self._lean_scopes.update(analyzer.scopes) |
| 714 | + for alias, node_id in analyzer.aliases.items(): |
| 715 | + self._lean_aliases[alias].append(node_id) |
| 716 | + self._lean_private.update(analyzer.private_ids) |
| 717 | + except Exception: |
| 718 | + logger.exception(f"Failed to analyze Lean file {file_path}") |
| 719 | + |
| 720 | + def _build_lean_index(self) -> dict[str, list[str]]: |
| 721 | + """FQN -> Lean node ids, and module name -> files. A module name maps |
| 722 | + to every file whose path ends with it (`Geo.Point` -> `src/Geo/Point.lean`), |
| 723 | + because the Lake source folder is not known.""" |
| 724 | + if self._lean_index is None: |
| 725 | + index: dict[str, list[str]] = defaultdict(list) |
| 726 | + for func_id, func in self.functions.items(): |
| 727 | + if func.language == "lean" and func.qualified_name: |
| 728 | + index[func.qualified_name].append(func_id) |
| 729 | + for alias, node_ids in self._lean_aliases.items(): |
| 730 | + for node_id in node_ids: |
| 731 | + if node_id not in index[alias]: |
| 732 | + index[alias].append(node_id) |
| 733 | + modules: dict[str, set[str]] = defaultdict(set) |
| 734 | + for rel in self._lean_imports: |
| 735 | + parts = rel[: -len(".lean")].split("/") |
| 736 | + for k in range(len(parts)): |
| 737 | + modules[".".join(parts[k:])].add(rel) |
| 738 | + self._lean_index = index |
| 739 | + self._lean_modules = modules |
| 740 | + return self._lean_index |
| 741 | + |
| 742 | + def _lean_visible_files(self, rel: str) -> frozenset[str]: |
| 743 | + """The file itself and every repository file it imports, transitively.""" |
| 744 | + if rel not in self._lean_visible: |
| 745 | + self._build_lean_index() |
| 746 | + seen = {rel} |
| 747 | + queue = [rel] |
| 748 | + while queue: |
| 749 | + current = queue.pop() |
| 750 | + for module in self._lean_imports.get(current, []): |
| 751 | + for target in self._lean_modules.get(module, ()): |
| 752 | + if target not in seen: |
| 753 | + seen.add(target) |
| 754 | + queue.append(target) |
| 755 | + self._lean_visible[rel] = frozenset(seen) |
| 756 | + return self._lean_visible[rel] |
| 757 | + |
| 758 | + def _resolve_lean_callee(self, callee: str, caller: Node) -> str | None: |
| 759 | + """Resolve a Lean reference to one declaration by exact FQN, in Lean's |
| 760 | + order: the caller's namespaces (innermost first), the root, then the |
| 761 | + open namespaces. No match, or more than one, drops the reference. |
| 762 | +
|
| 763 | + When the whole dotted name matches nothing, its longest prefix that |
| 764 | + matches is the constant and the rest are projections, as in Lean |
| 765 | + (`origin.x` is `origin` then the field `x`).""" |
| 766 | + parts = callee.split(".") |
| 767 | + for end in range(len(parts), 0, -1): |
| 768 | + found = self._resolve_lean_name(".".join(parts[:end]), caller) |
| 769 | + if found is not _LEAN_NO_MATCH: |
| 770 | + return found |
| 771 | + return None |
| 772 | + |
| 773 | + def _resolve_lean_name(self, callee: str, caller: Node): |
| 774 | + """One exact name: a node id, None (ambiguous or recursion: stop), or |
| 775 | + _LEAN_NO_MATCH (try a shorter prefix).""" |
| 776 | + index = self._build_lean_index() |
| 777 | + visible = self._lean_visible_files(caller.relative_path) |
| 778 | + namespaces, opens = self._lean_scopes.get(caller.id, ([], [])) |
| 779 | + |
| 780 | + def qualify(prefix: str, name: str) -> str: |
| 781 | + return f"{prefix}.{name}" if prefix else name |
| 782 | + |
| 783 | + def matches(fqn: str) -> list[str]: |
| 784 | + return [ |
| 785 | + node_id |
| 786 | + for node_id in index.get(fqn, []) |
| 787 | + if self.functions[node_id].relative_path in visible |
| 788 | + and ( |
| 789 | + node_id not in self._lean_private |
| 790 | + or self.functions[node_id].relative_path == caller.relative_path |
| 791 | + ) |
| 792 | + ] |
| 793 | + |
| 794 | + def unique(found) -> str | None: |
| 795 | + # A reference to the caller itself is recursion, not an edge. |
| 796 | + found = list(found) |
| 797 | + return found[0] if len(found) == 1 and found[0] != caller.id else None |
| 798 | + |
| 799 | + if callee.startswith("_root_."): |
| 800 | + found = matches(callee[len("_root_.") :]) |
| 801 | + return unique(found) if found else _LEAN_NO_MATCH |
| 802 | + |
| 803 | + prefixes = [*namespaces, ""] |
| 804 | + for prefix in prefixes: |
| 805 | + found = matches(qualify(prefix, callee)) |
| 806 | + if found: |
| 807 | + return unique(found) # the innermost match shadows the outer ones |
| 808 | + |
| 809 | + opened = { |
| 810 | + node_id |
| 811 | + for name in opens |
| 812 | + for prefix in prefixes |
| 813 | + for node_id in matches(qualify(qualify(prefix, name), callee)) |
| 814 | + } |
| 815 | + return unique(opened) if opened else _LEAN_NO_MATCH |
| 816 | + |
671 | 817 | def _analyze_go_file(self, file_path: str, content: str, repo_dir: str): |
672 | 818 | """ |
673 | 819 | Analyze Go file using tree-sitter based analyzer. |
@@ -860,11 +1006,11 @@ def _resolve_call_relationships(self): |
860 | 1006 | self.call_relationships.extend(link_go_interfaces(self.functions)) |
861 | 1007 |
|
862 | 1008 | def _has_authoritative_resolution(self, relationship: CallRelationship) -> bool: |
863 | | - """A Dart or Go caller's callee that the language's scoped resolution |
| 1009 | + """A Dart, Go or Lean caller's callee that the language's scoped resolution |
864 | 1010 | rejected is external or invisible; drop it so ast_parser's name-only |
865 | 1011 | fallback cannot bind it to an unrelated component.""" |
866 | 1012 | caller = self.functions.get(relationship.caller) |
867 | | - return caller is not None and caller.language in ("dart", "go") |
| 1013 | + return caller is not None and caller.language in ("dart", "go", "lean") |
868 | 1014 |
|
869 | 1015 | def _dotted_project_packages(self) -> dict[str, set]: |
870 | 1016 | """Project packages/namespaces, partitioned by language. Java and C# |
@@ -1007,6 +1153,9 @@ def _resolve_callee( |
1007 | 1153 | if caller_language == "go" and caller is not None: |
1008 | 1154 | # So is Go's: a name resolves only in the package it names. |
1009 | 1155 | return self._resolve_go_callee(relationship.callee, caller) |
| 1156 | + if caller_language == "lean" and caller is not None: |
| 1157 | + # And Lean's: only an exact FQN in a visible file. |
| 1158 | + return self._resolve_lean_callee(relationship.callee, caller) |
1010 | 1159 | if lang_indexes: |
1011 | 1160 | match = self._resolve_callee_in( |
1012 | 1161 | relationship, lang_indexes["exact"], lang_indexes["simple"] |
@@ -1152,6 +1301,8 @@ def _generate_visualization_data(self) -> dict: |
1152 | 1301 | node_classes.append("lang-go") |
1153 | 1302 | elif file_ext == ".zig": |
1154 | 1303 | node_classes.append("lang-zig") |
| 1304 | + elif file_ext == ".lean": |
| 1305 | + node_classes.append("lang-lean") |
1155 | 1306 | elif file_ext == ".vue": |
1156 | 1307 | node_classes.append("lang-vue") |
1157 | 1308 |
|
|
0 commit comments