treesitter-patterns
Universal patterns for tree-sitter code parsing. Covers AST visitors, query patterns, and language plugin development. Framework-agnostic.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Universal patterns for tree-sitter code parsing. Covers AST visitors, query patterns, and language plugin development. Framework-agnostic.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Generic BAML patterns for type-safe LLM prompting. Covers schema design, DTO generation, client wrappers, and cross-language codegen. Framework-agnostic.
Browser automation for documentation discovery. Use when curl fails on JS-rendered sites, when detecting available browser tools, or when configuring browser-based documentation collection.
C4 architectural modeling for documenting software architecture. Use when creating architecture diagrams, planning new systems, communicating with stakeholders, or conducting architecture reviews.
Debug and analyze web applications using Chrome DevTools MCP. Use for console log inspection, network request monitoring, performance analysis, and debugging authenticated sessions. For basic browser automation (screenshots, form filling), use browser-discovery skill instead.
Patterns and techniques for analyzing brownfield codebases. Use when onboarding to unfamiliar code, preparing for refactoring, conducting architecture reviews, or identifying technical debt.
Detect new imports in modified files and auto-install missing dependencies. Works with npm, uv, pip, cargo, go mod, and other package managers. Triggers after code implementation to keep manifests in sync.
| name | treesitter-patterns |
| description | Universal patterns for tree-sitter code parsing. Covers AST visitors, query patterns, and language plugin development. Framework-agnostic. |
Universal patterns for working with tree-sitter in any project. Covers AST parsing, query patterns, visitors, and language plugin development.
This skill is framework-generic. It provides universal tree-sitter patterns:
| Variable | Default | Description |
|---|---|---|
| TREE_SITTER_DIR | tree_sitter | Directory for language parsers |
| QUERY_DIR | queries | Directory for .scm query files |
| LANGUAGES | auto | Auto-detect or list of languages |
MANDATORY - Follow the Workflow steps below in order.
If you're about to:
STOP -> Add error handling -> Test on edge cases -> Then proceed
./cookbook/language-plugin.md./cookbook/ast-visitor.md./cookbook/query-patterns.mdimport tree_sitter_python as tspython
from tree_sitter import Language, Parser
# Create parser
parser = Parser(Language(tspython.language()))
# Parse code
source = b"def hello(): pass"
tree = parser.parse(source)
# Access root node
root = tree.root_node
print(root.sexp())
# Get children
for child in node.children:
print(child.type, child.text)
# Named children only (skip punctuation)
for child in node.named_children:
print(child.type)
# Find by type
def find_all(node, type_name):
results = []
if node.type == type_name:
results.append(node)
for child in node.children:
results.extend(find_all(child, type_name))
return results
functions = find_all(root, "function_definition")
; Match function definitions
(function_definition
name: (identifier) @function.name
parameters: (parameters) @function.params
body: (block) @function.body)
; Match class definitions
(class_definition
name: (identifier) @class.name
body: (block) @class.body)
; Match imports
(import_statement
(dotted_name) @import.module)
; Match decorated functions
(decorated_definition
(decorator) @decorator
definition: (function_definition
name: (identifier) @function.name))
from tree_sitter import Query
query = Query(Language(tspython.language()), """
(function_definition
name: (identifier) @name
body: (block) @body)
""")
captures = query.captures(root)
for node, name in captures:
print(f"{name}: {node.text.decode()}")
| Language | Functions | Classes | Imports |
|---|---|---|---|
| Python | function_definition | class_definition | import_statement |
| JavaScript | function_declaration | class_declaration | import_statement |
| TypeScript | function_declaration | class_declaration | import_statement |
| Go | function_declaration | type_declaration | import_declaration |
| Rust | function_item | impl_item | use_declaration |
def safe_parse(source: bytes) -> tuple[Tree | None, list[str]]:
"""Parse with error collection."""
tree = parser.parse(source)
errors = []
def collect_errors(node):
if node.type == "ERROR" or node.is_missing:
errors.append(f"Error at {node.start_point}: {node.text[:50]}")
for child in node.children:
collect_errors(child)
collect_errors(tree.root_node)
return tree, errors
tree, errors = safe_parse(source)
if errors:
print(f"Parse errors: {errors}")
from abc import ABC, abstractmethod
class ASTVisitor(ABC):
"""Base visitor for tree-sitter AST."""
def visit(self, node):
method_name = f"visit_{node.type}"
visitor = getattr(self, method_name, self.generic_visit)
return visitor(node)
def generic_visit(self, node):
for child in node.named_children:
self.visit(child)
@abstractmethod
def visit_function_definition(self, node):
pass
class FunctionExtractor(ASTVisitor):
def __init__(self):
self.functions = []
def visit_function_definition(self, node):
name_node = node.child_by_field_name("name")
if name_node:
self.functions.append(name_node.text.decode())
self.generic_visit(node)
extractor = FunctionExtractor()
extractor.visit(tree.root_node)
print(extractor.functions)
parser.parse(new_source, old_tree)def analyze_file(path: Path) -> CodeAnalysis:
source = path.read_bytes()
tree = parser.parse(source)
return CodeAnalysis(
functions=extract_functions(tree),
classes=extract_classes(tree),
imports=extract_imports(tree),
complexity=calculate_complexity(tree)
)
class CodeStructure {
functions FunctionInfo[]
classes ClassInfo[]
imports string[]
}
class FunctionInfo {
name string
parameters string[]
return_type string?
line_start int
line_end int
}