Skip to main content

LanguageParser

Trait LanguageParser 

Source
pub trait LanguageParser: Send + Sync {
    // Required methods
    fn language(&self) -> Language;
    fn extensions(&self) -> &[&str];
    fn extract_nodes(
        &self,
        tree: &Tree,
        source: &str,
        file_path: &str,
    ) -> Vec<CodeNode>;

    // Provided methods
    fn extract_usages(
        &self,
        _tree: &Tree,
        _source: &str,
        _file_path: &str,
    ) -> Option<FileUsages> { ... }
    fn usages_language(&self) -> Option<&'static str> { ... }
    fn may_have_usages(&self, _source: &str) -> bool { ... }
}
Expand description

Trait for language-specific parsing logic.

Each language needs to implement this to handle its unique AST structure and idioms. The trait provides the Tree-sitter language and the extraction logic.

Required Methods§

Source

fn language(&self) -> Language

Returns the Tree-sitter language for this parser.

Source

fn extensions(&self) -> &[&str]

File extensions this parser handles.

Source

fn extract_nodes( &self, tree: &Tree, source: &str, file_path: &str, ) -> Vec<CodeNode>

Extracts CodeNodes from a parsed Tree-sitter tree.

This is where the magic happens. Each language traverses its AST differently to find functions, classes, etc.

Provided Methods§

Source

fn extract_usages( &self, _tree: &Tree, _source: &str, _file_path: &str, ) -> Option<FileUsages>

Extracts tracked SDK usages (see crate::usages) from the same tree.

None means this language has no usage extractor yet, which is different from a file with no usages (an empty FileUsages).

Source

fn usages_language(&self) -> Option<&'static str>

The language name usage records carry, for a language that implements Self::extract_usages.

Source

fn may_have_usages(&self, _source: &str) -> bool

Whether source can contain usages at all. A false answer lets crate::parse_source_usages skip the parse: most files of a large tree never mention an SDK.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§