tree-sitter 多语言代码解析
tree-sitter-language-pack
使用 tree-sitter 语法解析并提取 371 种编程语言的代码智能。当编写解析源代码、提取结构/导入/导出/符号/docstring/注释、检测语言、运行语法诊断或为 LLM 生成语法感知分块的代码时使用——支持 Rust、Python、Node.js/TypeScript 或 ts-pack CLI。涵盖安装、CLI 接口、SDK 接口和解析器缓存管理。
- License 许可
- MIT
- 最近更新
- 2026-11-15
- Stars
- ★ 485
- Forks
- 71
01 前言 Front Matter
- name
- tree-sitter-language-pack
- license
- MIT
- metadata
- { "author": "xberg-io", "version": "0.1.0", "repository": "https://github.com/xberg-io/tree-sitter-language-pack" }
- description
- Parse and extract code intelligence from 371 programming languages using tree-sitter grammars. Use when writing code that parses source, extracts structure/imports/exports/symbols/docstrings/comments, detects a language, runs syntax diagnostics, or produces syntax-aware chunks for LLMs — in Rust, Python, Node.js/TypeScript, or the ts-pack CLI. Covers installation, the CLI surface, the SDK surface, and parser-cache management.
02 资源文件 1
- 参考文档index.md729 B
03 SKILL.md 正文
<!-- AI-RULEZ :: GENERATED FILE — DO NOT EDIT Content-Hash: blake3:7860f629c0c291cb2e4f9cb034035fadbc4de3b90bace596a2d2348c12b5ec00 Source-Hash: blake3:fbed630db85ed02cff8e3bc8c836d453724480351ecb0896122f193a21670041 Schema-Version: v1 -->
Tree-Sitter Language Pack
tree-sitter-language-pack is a polyglot code parsing and analysis library with a high-performance Rust core and polyglot bindings for 15 languages — Python, Node.js/TypeScript, Ruby, Go, Java, C#, PHP, Elixir, Dart, Kotlin (Android), Swift, Zig, and WebAssembly, plus a C ABI and JNI. It compiles 371 tree-sitter grammars into efficient parsers and exposes code intelligence on top of them.
Capabilities
- Parse 371 languages into concrete syntax trees (s-expression or JSON).
- Extract structure — functions, classes, methods, modules with line and byte spans, child nesting, and visibility.
- Extract imports and exports — statements, sources, exported kinds.
- Extract symbols — all identifiers, for search and indexing.
- Extract docstrings and comments — attached to definitions or standalone.
- Language detection — from a file path, an extension, or content.
- Syntax diagnostics — error nodes and syntax errors with positions.
- Syntax-aware chunking — split source on syntactic boundaries for LLM context windows, not on arbitrary byte offsets.
- Custom tree-sitter queries — run your own query patterns over a tree (SDK only).
- Parser cache management — download, list, inspect, and clean the on-demand parser cache for offline/CI use.
- Distribution — 15 language bindings (including WebAssembly, a C ABI,
and JNI) plus the
ts-packCLI.
Use this skill when writing code that parses source in any supported language, extracts code metadata, chunks code for an LLM, detects a language, or validates syntax.
When to use
Reach for tree-sitter-language-pack when you need a real syntax tree or structured code metadata — building a code index, feeding code to an LLM in semantically coherent chunks, linting for syntax errors across many languages, or detecting a file's language. For plain text extraction from documents (PDF, Office, HTML), use Xberg instead.
CLI vs SDK
- CLI (
ts-pack) — quick one-shot parsing and extraction over files, parser-cache management, and shell/CI pipelines.parseandprocessprint to stdout (processalways emits JSON). Auto-detects the language from the file extension. - SDK — embed parsing in an application, run custom tree-sitter queries, detect a language from raw content (not just a path), or hold parsers across many calls. Use the Rust core or the Python/Node bindings.
- MCP server (
ts-pack mcp) — exposesparse/process/detect_language(plus language and cache tools) as agent tools, so an MCP client can parse and analyze code directly with no shell-out. This plugin auto-registers it; see the tree-sitter-language-pack-using-the-mcp-server skill.
Prefer the CLI for ad-hoc work in an agent session; prefer the SDK when the result feeds back into a larger program; prefer the MCP server when the agent should call parsing directly as a tool.
Installation
CLI
brew install xberg-io/tap/ts-pack
# or run without a persistent install (the CLI proxy package self-installs the binary):
npx @xberg-io/ts-pack-cli --help
# or download a prebuilt binary from the latest GitHub release:
# https://github.com/xberg-io/tree-sitter-language-pack/releases/latest
# or install from crates.io:
cargo install ts-pack-cli
# binary is installed as `ts-pack`
Python
pip install tree-sitter-language-pack
# or: uv add tree-sitter-language-pack
Node.js / TypeScript
npm install @xberg-io/tree-sitter-language-pack
# or: pnpm add @xberg-io/tree-sitter-language-pack
Rust
# Cargo.toml
[dependencies]
tree-sitter-language-pack = { version = "1", features = ["download"] }
Other bindings: Ruby (gem install tree_sitter_language_pack), Go, Java,
C#, PHP, Elixir, Dart, Kotlin (Android), Swift, Zig, and WebAssembly
(npm install @xberg-io/tree-sitter-language-pack-wasm). See
https://github.com/xberg-io/tree-sitter-language-pack.
CLI surface
ts-pack has these subcommands:
| Command | Purpose |
|---|---|
parse <file> |
Parse a file into a syntax tree (`--format sexp |
process <file> |
Run the code-intelligence pipeline, emit JSON. |
detect* |
No standalone command — parse/process auto-detect from the extension. Use the SDK for content detection. |
list |
List available languages (--downloaded, --manifest, --filter). |
info <language> |
Show whether a language is known and cached. |
download [langs...] |
Download parser libraries (--all, --groups, --fresh). |
clean |
Remove all cached parser libraries (--force). |
cache-dir |
Print the effective cache directory. |
init |
Write a language-pack.toml config (--languages, --cache-dir). |
completions <shell> |
Generate shell completions. |
mcp |
Start the MCP server (--transport stdio). |
* Language detection is implicit in parse/process. There is no
detect or validate subcommand; diagnostics come from
process --diagnostics.
Parse
ts-pack parse src/main.rs # s-expression tree (auto-detect)
ts-pack parse src/main.rs --format json # { language, sexp, has_errors }
ts-pack parse - --language python # read from stdin, explicit language
Process (code intelligence)
# Defaults to structure + imports + exports when no feature flags are given:
ts-pack process src/app.ts
# Pick features explicitly:
ts-pack process src/app.ts --structure --imports --symbols --docstrings
# Everything:
ts-pack process src/app.ts --all
# Syntax-aware chunks (bytes) for an LLM:
ts-pack process src/app.ts --chunk-size 2000
# Syntax diagnostics:
ts-pack process src/app.ts --diagnostics
process always prints JSON. Top-level keys: language, metrics,
structure, imports, exports, comments, docstrings, symbols,
diagnostics, and chunks (when --chunk-size is set).
SDK quick start
from tree_sitter_language_pack import process, ProcessConfig, detect_language_from_path
config = ProcessConfig("python", symbols=True, docstrings=True) # structure/imports/exports default to True
result = process(source_code, config)
for item in result.structure: # ProcessResult is an object, not a dict
print(item.kind, item.name, item.span.start_line)
# Path-based detection (use detect_language_from_content for raw text;
# see the detecting-languages skill):
lang = detect_language_from_path("src/app.py")
import { process, detectLanguageFromPath } from "@xberg-io/tree-sitter-language-pack";
const lang = detectLanguageFromPath("src/app.ts");
const result = process(source, { language: lang ?? "typescript", structure: true, imports: true });
Parser cache
Parsers download on demand the first time a language is used and are cached locally. For offline or CI runs, prefetch:
ts-pack download python rust typescript # specific languages
ts-pack download --all # everything
ts-pack download --groups all # by group (see manifest_groups())
ts-pack list --downloaded # what is cached
ts-pack cache-dir # where the cache lives
ts-pack clean --force # wipe the cache
See the tree-sitter-language-pack-managing-parsers skill for the full cache workflow.
Related skills
tree-sitter-language-pack-parsing-source— parse a file into a syntax tree / s-expression.tree-sitter-language-pack-extracting-code-structure—processwith structure/imports/exports/ symbols/docstrings.tree-sitter-language-pack-chunking-for-llms—--chunk-sizesyntax-aware splitting.tree-sitter-language-pack-detecting-languages— detect by path, extension, or content.tree-sitter-language-pack-managing-parsers— download/clean/list/info parser-cache management.tree-sitter-language-pack-using-the-mcp-server— callparse/process/detect_languageover MCP instead of the CLI.
Full documentation: https://github.com/xberg-io/tree-sitter-language-pack
04 同仓库更多 Skills
tree-sitter 代码智能解析
tree-sitter-language-pack
使用 tree-sitter 语法从 371 种编程语言中解析并提取代码智能信息。适用于编写解析源代码、提取结构/导入/导出/符号/文档字符串/注释、检测语言、运行语法诊断或为大模型(LLM)生成语法感知分块的代码——支持 Rust、Python、Node.js/TypeScript 或 ts-pack CLI。涵盖安装、CLI 接口、SDK 接口以及解析器缓存管理。
tree-sitter MCP 服务使用
tree-sitter-language-pack-using-the-mcp-server
当需要通过 tree-sitter-language-pack 的 MCP 服务器工具来解析源代码、提取代码结构或检测语言(而非调用 ts-pack CLI)时使用。涵盖工具接口、自动安装启动器,以及 MCP 相较于 CLI 或 SDK 的优势场景。
源代码语法树解析
tree-sitter-language-pack-parsing-source
当用户需要为源文件生成 tree-sitter 语法树时使用——以 s-表达式转储或 JSON 树的形式呈现。涵盖 `ts-pack parse`、语言自动检测与 `--language` 的对比、标准输入(stdin)输入以及读取 `has_errors`。
解析器缓存管理
tree-sitter-language-pack-managing-parsers
当用户需要管理 tree-sitter 解析器缓存时使用——为离线或 CI 运行预取解析器、列出已下载内容、检查某种语言、查找缓存目录或清理缓存。涵盖 `ts-pack download`、`list`、`info`、`cache-dir`、`clean` 和 `init`。
代码结构提取
tree-sitter-language-pack-extracting-code-structure
当用户需要从源文件获取结构化代码元数据时使用——包括函数、类、导入、导出、符号、文档字符串、注释或语法诊断。涵盖 `ts-pack process` 的功能开关、JSON 结果结构以及默认功能集。
编程语言检测
tree-sitter-language-pack-detecting-languages
当用户想知道某个文件或代码片段属于哪种编程语言时使用。涵盖 `ts-pack parse`/`process` 中的隐式检测、通过 `ts-pack list`/`info` 确认是否支持某种语言,以及用于路径、扩展名和原始内容的 SDK 检测函数。