Expand description
§Tree Parser Library
A comprehensive Rust library for parsing and searching code elements across multiple programming languages using tree-sitter. This library provides powerful tools for static code analysis, code search, and AST manipulation.
§Features
- Multi-language Support: Parse Python, Rust, JavaScript, TypeScript, Java, C, C++, Go, and more
- High Performance: Concurrent parsing with async/await for maximum efficiency
- Advanced Search: Find functions, classes, structs, interfaces with regex pattern matching
- Flexible Filtering: Custom file filters and parsing options
- Rich Metadata: Extract detailed information about code constructs
- Type Safety: Full Rust type safety with comprehensive error handling
- Configurable: Extensive configuration options for different use cases
§Quick Start
use tree_parser::{parse_file, ParseOptions, Language};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Parse a single file
let parsed_file = parse_file("src/main.rs", ParseOptions::default()).await?;
println!("Found {} constructs", parsed_file.constructs.len());
for construct in &parsed_file.constructs {
if let Some(name) = &construct.name {
println!("{}: {} (lines {}-{})",
construct.node_type, name,
construct.start_line, construct.end_line);
}
}
Ok(())
}
§Examples
See the examples/
directory for comprehensive usage examples including:
- Basic parsing and directory traversal
- Advanced search with regex patterns
- Custom file filtering
- Performance optimization
- Error handling strategies
Structs§
- Code
Construct - Represents a parsed code construct (function, class, struct, etc.)
- Construct
Metadata - Metadata associated with a code construct
- File
Error - Represents an error that occurred while processing a specific file
- File
Filter - Filter criteria for selecting which files to parse
- Parameter
- Represents a function or method parameter
- Parse
Options - Configuration options for parsing operations
- Parsed
File - Represents a successfully parsed source code file
- Parsed
Project - Represents the results of parsing an entire project or directory
- Point
- A position in a multi-line text document, in terms of rows and columns.
- Range
- A range of positions in a multi-line text document, both in terms of bytes and of rows and columns.
Enums§
- Error
- Main error type for the tree parser library
- Error
Type - Categorizes different types of errors for easier handling
- Language
- Supported programming languages
- Language
Detection - Methods for detecting the programming language of a file
Functions§
- detect_
language - Combined language detection using multiple methods
- detect_
language_ by_ content - Detect language by file content patterns
- detect_
language_ by_ extension - Detect language by file extension
- detect_
language_ by_ shebang - Detect language by shebang line
- format_
duration - Format duration in human-readable format
- format_
file_ size - Format file size in human-readable format
- get_
file_ extension - Extract the file extension from a file path
- get_
file_ name_ without_ extension - Extract the file name without its extension
- get_
supported_ extensions - Get a list of all file extensions supported by the parser
- get_
supported_ node_ types - Get supported node types for a language
- get_
tree_ sitter_ language - Get the tree-sitter language for a given Language enum
- is_
supported_ extension - Check if a file extension is supported by the parser
- is_
valid_ directory_ path - Validate that a directory path exists
- is_
valid_ file_ path - Validate that a file path exists
- language_
from_ string - Convert a string representation to a Language enum
- language_
to_ string - Convert a Language enum to its string representation
- matches_
ignore_ patterns - Check if a path matches any of the specified ignore patterns
- parse_
directory - Parse an entire project directory recursively
- parse_
directory_ with_ filter - Parse a project directory with custom file filtering
- parse_
file - Parse a single source code file and extract code constructs
- sanitize_
path - Sanitize a file path for safe usage
- search_
by_ multiple_ node_ types - Search for code constructs matching any of the specified node types
- search_
by_ node_ type - Search for code constructs by their tree-sitter node type
- search_
by_ query - Execute a custom tree-sitter query for advanced searching
- search_
classes - Search for class and type definitions in a parsed file
- search_
functions - Search for function definitions in a parsed file
- search_
imports - Search for imports in a parsed file
- search_
variables - Search for variables/constants in a parsed file