28 lines
1.1 KiB
Lean4
28 lines
1.1 KiB
Lean4
import LeanLibclang
|
|
|
|
/-- Print an indented tree of the AST rooted at `cursor`,
|
|
filtering to only nodes from the main file. -/
|
|
partial def printAST (cursor : Clang.Cursor) (indent : Nat := 0) : IO Unit := do
|
|
let kind ← Clang.getCursorKind cursor
|
|
let spelling ← Clang.getCursorSpelling cursor
|
|
let typeSpelling ← Clang.getCursorTypeSpelling cursor
|
|
let loc ← Clang.getCursorLocation cursor
|
|
let pad := "".pushn ' ' (indent * 2)
|
|
IO.println s!"{pad}{kind} \"{spelling}\" type=\"{typeSpelling}\" {loc.file}:{loc.line}:{loc.column}"
|
|
let children ← Clang.getChildren cursor
|
|
for child in children do
|
|
let fromMain ← Clang.isFromMainFile child
|
|
if fromMain then
|
|
printAST child (indent + 1)
|
|
|
|
def main (args : List String) : IO Unit := do
|
|
match args with
|
|
| [] =>
|
|
IO.eprintln "Usage: lean-libclang <file.c> [-- <clang-args>...]"
|
|
| filename :: rest =>
|
|
let clangArgs := rest.toArray
|
|
let idx ← Clang.createIndex false true
|
|
let tu ← Clang.parseTranslationUnit idx filename clangArgs
|
|
let cursor ← Clang.getTranslationUnitCursor tu
|
|
printAST cursor
|