Crate tree_parser

Source
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§

CodeConstruct
Represents a parsed code construct (function, class, struct, etc.)
ConstructMetadata
Metadata associated with a code construct
FileError
Represents an error that occurred while processing a specific file
FileFilter
Filter criteria for selecting which files to parse
Parameter
Represents a function or method parameter
ParseOptions
Configuration options for parsing operations
ParsedFile
Represents a successfully parsed source code file
ParsedProject
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
ErrorType
Categorizes different types of errors for easier handling
Language
Supported programming languages
LanguageDetection
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