Skip to content

Commit 9c50ecb

Browse files
authored
Merge pull request #158 from fabceolin/feat/lean-analyzer
feat: Lean 4 analyzer, with header-scanner structure and exact-name resolution
2 parents fdeb1b7 + f4e6ca9 commit 9c50ecb

20 files changed

Lines changed: 2110 additions & 9 deletions

‎CHANGELOG.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,25 @@ uses [Semantic Versioning](https://semver.org/).
6565
`addModule`; `std`, `builtin`, and third-party packages are dropped. `test`
6666
blocks and `zig-cache/`, `.zig-cache/`, `zig-out/` are skipped, `build.zig`
6767
is also a build artifact, and `build.zig.zon` is a manifest.
68+
- **Lean 4 analyzer** ([#154](https://github.com/FSoft-AI4Code/CodeWiki/issues/154)).
69+
`def`, `theorem`/`lemma`, `abbrev`, `instance`, `structure`, `class`,
70+
`inductive`, `axiom` and `opaque` declarations become components, keyed by
71+
their fully qualified name (`Geo.Point.add`). A header scanner reads the
72+
structure from the source with comments and strings masked: declarations,
73+
the `namespace`/`section`/`mutual`/`end` stack, `open` (also `open X in`),
74+
`import`, spans and `/-- -/` docstrings. So a region that the tree-sitter
75+
grammar cannot parse does not hide a declaration. References come from the
76+
tree-sitter identifiers, without binders, `let`/`have`/`fun`/`intro` names
77+
and match patterns. A reference resolves only to an exact name in the
78+
declaration's namespaces, the root or the open namespaces, in a file that
79+
the caller imports (transitively); `private` names stay in their file, and
80+
a reference with no match or more than one is dropped. Constructor and
81+
field names (`Point.mk`, `Point.x`, `Shape.dot`) link to their structure or
82+
inductive, and `origin.x` links to `origin`. `structure` is a `struct`,
83+
`class` an `interface`, `inductive` a `class`, and the rest are functions.
84+
`.lake/` and `lake-packages/` are skipped, `lakefile.lean` and
85+
`lakefile.toml` are build artifacts, and `lake-manifest.json` and
86+
`lean-toolchain` are manifests.
6887
- **Vue single-file components.** Each `.vue` file becomes a component
6988
(`ArticleList.vue::ArticleList`), and its options-API members (`methods`,
7089
`computed`, `watch`, lifecycle hooks such as `mounted` or `setup`) become

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ Full list in the [CHANGELOG](./CHANGELOG.md).
120120
## Features
121121

122122
**Languages.** Python, Java, JavaScript, TypeScript, C, C++, C#, Kotlin,
123-
PHP, Ruby, Scala, Rust, Dart (Flutter-aware), Go, Zig, Vue (single-file
124-
components).
123+
PHP, Ruby, Scala, Rust, Dart (Flutter-aware), Go, Zig, Lean 4, Vue
124+
(single-file components).
125125

126126
**Hierarchical decomposition.** The dependency graph is clustered into a
127127
module tree, recursively, so a 1.4M-line repository gets the same treatment

‎codewiki/cli/utils/repo_validator.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
".dart", # Dart
3838
".go", # Go
3939
".zig", # Zig
40+
".lean", # Lean 4
4041
".vue", # Vue single-file components
4142
}
4243

‎codewiki/cli/utils/validation.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ def detect_supported_languages(directory: Path) -> list[tuple[str, int]]:
171171
"Dart": [".dart"],
172172
"Go": [".go"],
173173
"Zig": [".zig"],
174+
"Lean": [".lean"],
174175
"Vue": [".vue"],
175176
}
176177

‎codewiki/mcp/tools/analysis.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ def _detect_via_mtime(
232232
".dart",
233233
".go",
234234
".zig",
235+
".lean",
235236
".vue",
236237
}
237238

‎codewiki/src/be/dependency_analyzer/analysis/analysis_service.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ def _filter_supported_languages(self, code_files: list[dict]) -> list[dict]:
365365
"scala",
366366
"dart",
367367
"zig",
368+
"lean",
368369
"vue",
369370
}
370371

@@ -392,6 +393,7 @@ def _get_supported_languages(self) -> list[str]:
392393
"dart",
393394
"go",
394395
"zig",
396+
"lean",
395397
"vue",
396398
]
397399

‎codewiki/src/be/dependency_analyzer/analysis/call_graph_analyzer.py‎

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from collections import defaultdict
1616
from contextlib import contextmanager
1717
from pathlib import Path
18+
from typing import TYPE_CHECKING
1819

1920
from codewiki.src.be.dependency_analyzer.models.core import CallRelationship, Node
2021
from codewiki.src.be.dependency_analyzer.utils.external_symbols import (
@@ -26,6 +27,10 @@
2627
from codewiki.src.be.dependency_analyzer.utils.patterns import CODE_EXTENSIONS
2728
from codewiki.src.be.dependency_analyzer.utils.security import safe_open_text
2829

30+
if TYPE_CHECKING:
31+
from codewiki.src.be.dependency_analyzer.analyzers.lean import LeanFile, LeanScope
32+
from codewiki.src.be.dependency_analyzer.analyzers.lean_resolution import LeanNameResolver
33+
2934
logger = logging.getLogger(__name__)
3035

3136

@@ -76,6 +81,7 @@ def __init__(self):
7681
self._dart_unscoped = False
7782
self._go_visible: dict[str, set[str]] = {}
7883
self._go_index = None
84+
self._reset_lean_state()
7985
self.call_relationships: list[CallRelationship] = []
8086
self._python_project_modules: set = set()
8187
self._python_external_import_roots: set = set()
@@ -108,6 +114,7 @@ def analyze_code_files(self, code_files: list[dict], base_dir: str) -> dict:
108114
self._dart_unscoped = False
109115
self._go_visible = {}
110116
self._go_index = None
117+
self._reset_lean_state()
111118

112119
files_analyzed = 0
113120
files_failed = 0
@@ -293,6 +300,8 @@ def _analyze_code_file(self, repo_dir: str, file_info: dict):
293300
self._analyze_go_file(file_path, content, repo_dir)
294301
elif language == "zig":
295302
self._analyze_zig_file(file_path, content, repo_dir)
303+
elif language == "lean":
304+
self._analyze_lean_file(file_path, content, repo_dir)
296305
elif language == "vue":
297306
self._analyze_vue_file(file_path, content, repo_dir)
298307
# else:
@@ -668,6 +677,59 @@ def _analyze_vue_file(self, file_path: str, content: str, repo_dir: str):
668677
except Exception:
669678
logger.exception(f"Failed to analyze Vue file {file_path}")
670679

680+
def _reset_lean_state(self) -> None:
681+
self._lean_files: dict[str, LeanFile] = {}
682+
self._lean_scopes: dict[str, LeanScope] = {}
683+
self._lean_aliases: dict[str, list[str]] = defaultdict(list)
684+
self._lean_private: set[str] = set()
685+
self._lean_protected: set[str] = set()
686+
self._lean_resolver: LeanNameResolver | None = None
687+
688+
def _analyze_lean_file(self, file_path: str, content: str, repo_dir: str):
689+
"""
690+
Analyze a Lean 4 file.
691+
692+
Besides components and references, records the file's imports, each
693+
declaration's namespaces and open namespaces, and the constructor and
694+
field names of its structures, for Lean resolution by exact name.
695+
696+
Args:
697+
file_path: Path to the Lean file
698+
content: File content string
699+
repo_dir: Repository base directory
700+
"""
701+
from codewiki.src.be.dependency_analyzer.analyzers.lean import TreeSitterLeanAnalyzer
702+
703+
try:
704+
analyzer = TreeSitterLeanAnalyzer(str(file_path), content, repo_path=repo_dir)
705+
706+
for func in analyzer.nodes:
707+
self.functions[func.id] = func
708+
709+
self.call_relationships.extend(analyzer.call_relationships)
710+
self._lean_files[analyzer.relative_path] = analyzer.scan
711+
self._lean_scopes.update(analyzer.scopes)
712+
for alias, node_id in analyzer.aliases.items():
713+
self._lean_aliases[alias].append(node_id)
714+
self._lean_private.update(analyzer.private_ids)
715+
self._lean_protected.update(analyzer.protected_ids)
716+
except Exception:
717+
logger.exception(f"Failed to analyze Lean file {file_path}")
718+
719+
def _resolve_lean_callee(self, callee: str, caller: Node) -> str | None:
720+
from codewiki.src.be.dependency_analyzer.analyzers.lean_resolution import LeanNameResolver
721+
722+
if self._lean_resolver is None:
723+
self._lean_resolver = LeanNameResolver(
724+
self.functions,
725+
self._lean_files,
726+
self._lean_scopes,
727+
self._lean_aliases,
728+
self._lean_private,
729+
self._lean_protected,
730+
)
731+
return self._lean_resolver.resolve(callee, caller)
732+
671733
def _analyze_go_file(self, file_path: str, content: str, repo_dir: str):
672734
"""
673735
Analyze Go file using tree-sitter based analyzer.
@@ -860,11 +922,11 @@ def _resolve_call_relationships(self):
860922
self.call_relationships.extend(link_go_interfaces(self.functions))
861923

862924
def _has_authoritative_resolution(self, relationship: CallRelationship) -> bool:
863-
"""A Dart or Go caller's callee that the language's scoped resolution
925+
"""A Dart, Go or Lean caller's callee that the language's scoped resolution
864926
rejected is external or invisible; drop it so ast_parser's name-only
865927
fallback cannot bind it to an unrelated component."""
866928
caller = self.functions.get(relationship.caller)
867-
return caller is not None and caller.language in ("dart", "go")
929+
return caller is not None and caller.language in ("dart", "go", "lean")
868930

869931
def _dotted_project_packages(self) -> dict[str, set]:
870932
"""Project packages/namespaces, partitioned by language. Java and C#
@@ -1007,6 +1069,9 @@ def _resolve_callee(
10071069
if caller_language == "go" and caller is not None:
10081070
# So is Go's: a name resolves only in the package it names.
10091071
return self._resolve_go_callee(relationship.callee, caller)
1072+
if caller_language == "lean" and caller is not None:
1073+
# And Lean's: only an exact FQN in a visible file.
1074+
return self._resolve_lean_callee(relationship.callee, caller)
10101075
if lang_indexes:
10111076
match = self._resolve_callee_in(
10121077
relationship, lang_indexes["exact"], lang_indexes["simple"]
@@ -1152,6 +1217,8 @@ def _generate_visualization_data(self) -> dict:
11521217
node_classes.append("lang-go")
11531218
elif file_ext == ".zig":
11541219
node_classes.append("lang-zig")
1220+
elif file_ext == ".lean":
1221+
node_classes.append("lang-lean")
11551222
elif file_ext == ".vue":
11561223
node_classes.append("lang-vue")
11571224

‎codewiki/src/be/dependency_analyzer/analyzers/artifact.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ def is_artifact_file_node(node: Any) -> bool:
244244
"build.sbt",
245245
"Package.swift",
246246
"build.zig.zon",
247+
"lake-manifest.json",
248+
"lean-toolchain",
247249
"pubspec.yaml",
248250
"Pipfile",
249251
"environment.yml",
@@ -296,6 +298,8 @@ def is_artifact_file_node(node: Any) -> bool:
296298
"Gruntfile.js",
297299
"Herebyfile.mjs",
298300
"build.zig",
301+
"lakefile.lean",
302+
"lakefile.toml",
299303
# Autotools inputs; the generated configure, Makefile.in and aclocal.m4
300304
# are not build sources.
301305
"configure.ac",
@@ -451,6 +455,7 @@ def classify_artifact(
451455
".scala",
452456
".sc",
453457
".zig",
458+
".lean",
454459
".vue",
455460
}:
456461
if name in _TEST_INFRA_NAMES:

0 commit comments

Comments
 (0)