AGENT SKILL

tree-sitter 源码语法解析

tree-sitter-language-pack-parsing-source

当用户需要为源文件生成 tree-sitter 语法树时使用——以 s 表达式形式转储或以 JSON 树形式输出。涵盖 `ts-pack parse`、语言自动检测与通过 `--language` 指定语言的对比、标准输入(stdin)输入,以及读取 `has_errors`。

使用场景开发参考
License 许可
MIT
最近更新
2026-11-15
Stars
★ 485
Forks
71

01 前言 Front Matter

type
Playbook
title
Tree Sitter Language Pack Parsing Source
x-ai-rulez
{ "id": "tree-sitter-language-pack-parsing-source", "kind": "skill", "metadata": { "name": "tree-sitter-language-pack-parsing-source" } }
description
Use when the user wants a tree-sitter syntax tree for a source file — an s-expression dump or JSON tree. Covers `ts-pack parse`, language auto-detection vs `--language`, stdin input, and reading `has_errors`.

02 资源文件 1

03 SKILL.md 正文

Parsing source into a syntax tree

ts-pack parse <file> parses one source file with the matching tree-sitter grammar and prints the resulting concrete syntax tree. Use it when the user wants the tree itself — to inspect node structure, debug a grammar, or feed an s-expression to another tool.

Quick recipe

# S-expression (default) — auto-detects language from the file extension
ts-pack parse src/main.rs

# JSON form: { "language", "sexp", "has_errors" }
ts-pack parse src/main.rs --format json

Flag surface

Flag Short Default Purpose
--language <name> -l auto Override the language (skip extension detection).
--format <fmt> -f sexp sexp or json.

The parser library for the language downloads on first use and is cached.

Language detection

The language is auto-detected from the file extension. Override it when the extension is missing, misleading, or you are reading from stdin:

# Misleading extension or no extension:
ts-pack parse script --language bash

# stdin (use "-" as the file):
cat snippet.py | ts-pack parse - --language python

stdin cannot be auto-detected by path; always pass --language with -. For content-based detection without a path, use the SDK (detect_language_from_content) — see the tree-sitter-language-pack-detecting-languages skill.

Output shapes

S-expression

A nested (node_type child ...) tree. Compact, good for eyeballing structure or piping to a grammar tool.

JSON

{
  "language": "rust",
  "sexp": "(source_file (function_item ...))",
  "has_errors": false
}

Read has_errors to know whether the parse produced any error nodes — a fast syntax check. For positioned, per-error detail use ts-pack process <file> --diagnostics (see tree-sitter-language-pack-extracting-code-structure).

Examples

# Inspect a TypeScript file's tree
ts-pack parse src/app.ts

# Machine-readable, then check for syntax errors with jq
ts-pack parse src/app.ts --format json | jq '.has_errors'

# Parse a heredoc as Python
printf 'def f():\n    return 1\n' | ts-pack parse - -l python

When to reach for process instead

parse gives you the raw tree. If the user wants structured metadata (functions, imports, exports, symbols, docstrings) rather than the tree, use ts-pack process — see the tree-sitter-language-pack-extracting-code-structure skill.

04 同仓库更多 Skills

xberg-io
xberg-io/tree-sitter-language-pack

tree-sitter 代码智能解析

tree-sitter-language-pack

使用 tree-sitter 语法从 371 种编程语言中解析并提取代码智能信息。适用于编写解析源代码、提取结构/导入/导出/符号/文档字符串/注释、检测语言、运行语法诊断或为大模型(LLM)生成语法感知分块的代码——支持 Rust、Python、Node.js/TypeScript 或 ts-pack CLI。涵盖安装、CLI 接口、SDK 接口以及解析器缓存管理。

软件开发工程师代码解析与结构分析
★ 485
xberg-io
xberg-io/tree-sitter-language-pack

tree-sitter MCP 服务使用

tree-sitter-language-pack-using-the-mcp-server

当需要通过 tree-sitter-language-pack 的 MCP 服务器工具来解析源代码、提取代码结构或检测语言(而非调用 ts-pack CLI)时使用。涵盖工具接口、自动安装启动器,以及 MCP 相较于 CLI 或 SDK 的优势场景。

软件开发工程师代码解析与结构分析
★ 485
xberg-io
xberg-io/tree-sitter-language-pack

源代码语法树解析

tree-sitter-language-pack-parsing-source

当用户需要为源文件生成 tree-sitter 语法树时使用——以 s-表达式转储或 JSON 树的形式呈现。涵盖 `ts-pack parse`、语言自动检测与 `--language` 的对比、标准输入(stdin)输入以及读取 `has_errors`。

软件开发工程师代码解析与结构分析
★ 485
xberg-io
xberg-io/tree-sitter-language-pack

解析器缓存管理

tree-sitter-language-pack-managing-parsers

当用户需要管理 tree-sitter 解析器缓存时使用——为离线或 CI 运行预取解析器、列出已下载内容、检查某种语言、查找缓存目录或清理缓存。涵盖 `ts-pack download`、`list`、`info`、`cache-dir`、`clean` 和 `init`。

软件开发工程师开发环境与工具链管理
★ 485
xberg-io
xberg-io/tree-sitter-language-pack

代码结构提取

tree-sitter-language-pack-extracting-code-structure

当用户需要从源文件获取结构化代码元数据时使用——包括函数、类、导入、导出、符号、文档字符串、注释或语法诊断。涵盖 `ts-pack process` 的功能开关、JSON 结果结构以及默认功能集。

软件开发工程师代码解析与结构分析
★ 485
xberg-io
xberg-io/tree-sitter-language-pack

编程语言检测

tree-sitter-language-pack-detecting-languages

当用户想知道某个文件或代码片段属于哪种编程语言时使用。涵盖 `ts-pack parse`/`process` 中的隐式检测、通过 `ts-pack list`/`info` 确认是否支持某种语言,以及用于路径、扩展名和原始内容的 SDK 检测函数。

软件开发工程师代码解析与结构分析
★ 485