Lean 4 bindings for tree-sitter — typed grammar schemas, declaration extraction, source maps, and verified properties.
- FFI Bindings: Safe Lean 4 wrappers around tree-sitter C API (parser, tree, node, query)
- Grammar Schemas: Typed inductive types for Java, Python, and Kotlin nodes with a
GrammarSpectypeclass and proof obligations - Extraction Engine: Generic declaration extraction from source code producing typed
Declarationarrays with source locations and nested children - Source Maps: Bidirectional mapping between host-language source locations and generated Lean 4 code, with lookup, error tracing, and transitive composition
- Verified Properties: Kernel-checked proofs of mapping totality, cross-language consistency, and source map correctness
- Lean 4 (v4.32.0 or compatible)
- elan - Lean version manager (recommended)
- C compiler (gcc or clang) for tree-sitter FFI compilation
Add to your lakefile.lean:
require «tree-sitter» from git
"https://github.com/predictable-machines/lean4-tree-sitter" @ "v0.1.0"Then import in your Lean code:
import TreeSitter
open TreeSitter
open TreeSitter.FFI
open TreeSitter.Extract
-- Parse Java source code
def example : IO Unit := do
let parser ← TSParser.new
let lang ← treeSitterJava
let _ ← parser.setLanguage lang
let tree ← parser.parseString "public class Hello { }"
let root ← tree.rootNode
let rootType ← root.type
IO.println s!"Root node type: {rootType}"
-- Extract declarations from source code
def extractExample : IO Unit := do
let decls ← extractJava "public class UserService { public void run() {} }"
for d in decls do
IO.println s!"{d.name} ({repr d.declType}) at line {d.source.startLine}"make build # Build the library (auto-vendors grammars if needed)
make test # Build and run tests
make vendor-grammars # Re-vendor tree-sitter grammar sources
make clean # Clean build artifacts and vendored grammars| Module | Purpose |
|---|---|
TreeSitter.FFI |
Safe Lean 4 wrappers around tree-sitter C API |
TreeSitter.Types |
Declaration, SourceRange, GrammarSpec typeclass |
TreeSitter.Grammars |
Java, Python, Kotlin grammar specifications |
TreeSitter.Extract |
Generic declaration extraction engine |
TreeSitter.SourceMap |
Bidirectional source location mapping |
TreeSitter.Proofs |
Totality, consistency, and source map proofs |
| Language | Parser | Scanner | Declarations |
|---|---|---|---|
| Java | tree-sitter-java | No | class, interface, enum, method, constructor, field, annotation |
| Python | tree-sitter-python | Yes | class, function |
| Kotlin | tree-sitter-kotlin (fwcd) | Yes | class, interface, function, property, type alias, annotation, object |
MIT