CodeParser

Trait CodeParser 

Source
pub trait CodeParser: Send + Sync {
    // Required methods
    fn language(&self) -> &str;
    fn file_extensions(&self) -> &[&str];
    fn parse_file(
        &self,
        path: &Path,
        graph: &mut CodeGraph,
    ) -> Result<FileInfo, ParserError>;
    fn parse_source(
        &self,
        source: &str,
        file_path: &Path,
        graph: &mut CodeGraph,
    ) -> Result<FileInfo, ParserError>;
    fn config(&self) -> &ParserConfig;
    fn metrics(&self) -> ParserMetrics;
    fn reset_metrics(&mut self);

    // Provided methods
    fn parse_files(
        &self,
        paths: &[PathBuf],
        graph: &mut CodeGraph,
    ) -> Result<ProjectInfo, ParserError> { ... }
    fn parse_directory(
        &self,
        dir: &Path,
        graph: &mut CodeGraph,
    ) -> Result<ProjectInfo, ParserError> { ... }
    fn discover_files(&self, dir: &Path) -> Result<Vec<PathBuf>, ParserError> { ... }
    fn can_parse(&self, path: &Path) -> bool { ... }
}
Expand description

Core trait that all language parsers must implement

This trait defines the contract for extracting code entities and relationships from source code and inserting them into a CodeGraph database.

§Thread Safety

Implementations must be Send + Sync to support parallel parsing.

§Example

use codegraph_parser_api::{CodeParser, ParserConfig};
use codegraph::CodeGraph;

struct MyParser {
    config: ParserConfig,
}

impl CodeParser for MyParser {
    fn language(&self) -> &str {
        "mylang"
    }

    fn file_extensions(&self) -> &[&str] {
        &[".my"]
    }

    // ... implement other required methods
}

Required Methods§

Source

fn language(&self) -> &str

Returns the language identifier (lowercase, e.g., “python”, “rust”)

Source

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

Returns supported file extensions (e.g., [“.py”, “.pyw”])

Source

fn parse_file( &self, path: &Path, graph: &mut CodeGraph, ) -> Result<FileInfo, ParserError>

Parse a single file and insert entities/relationships into the graph

Note on Metrics: This method updates parser metrics (files_attempted, files_succeeded, etc.). Use metrics() to retrieve statistics after parsing operations.

§Arguments
  • path - Path to the source file
  • graph - Mutable reference to the CodeGraph database
§Returns

FileInfo containing metadata about parsed entities

§Errors

Returns ParserError if:

  • File cannot be read
  • Source code has syntax errors
  • Graph insertion fails
Source

fn parse_source( &self, source: &str, file_path: &Path, graph: &mut CodeGraph, ) -> Result<FileInfo, ParserError>

Parse source code string and insert into graph

Useful for parsing code snippets or in-memory source.

Note on Metrics: This method does NOT update parser metrics (files_attempted, files_succeeded, etc.). Only parse_file() updates metrics to avoid double-counting when parse_source() is called internally by parse_file().

§Arguments
  • source - Source code string
  • file_path - Logical path for this source (used for graph nodes)
  • graph - Mutable reference to the CodeGraph database
Source

fn config(&self) -> &ParserConfig

Get parser configuration

Source

fn metrics(&self) -> ParserMetrics

Get accumulated metrics

Returns current parsing metrics (files processed, time taken, etc.)

Source

fn reset_metrics(&mut self)

Reset metrics

Clears accumulated metrics. Useful for benchmarking.

Provided Methods§

Source

fn parse_files( &self, paths: &[PathBuf], graph: &mut CodeGraph, ) -> Result<ProjectInfo, ParserError>

Parse multiple files (can be overridden for parallel parsing)

Default implementation parses files sequentially. Override this for parallel parsing implementation.

§Arguments
  • paths - List of file paths to parse
  • graph - Mutable reference to the CodeGraph database
§Returns

ProjectInfo containing aggregate statistics

Source

fn parse_directory( &self, dir: &Path, graph: &mut CodeGraph, ) -> Result<ProjectInfo, ParserError>

Parse a directory recursively

§Arguments
  • dir - Directory path to parse
  • graph - Mutable reference to the CodeGraph database
Source

fn discover_files(&self, dir: &Path) -> Result<Vec<PathBuf>, ParserError>

Discover parseable files in a directory

Default implementation walks the directory and filters by extension. Can be overridden for custom discovery logic.

Source

fn can_parse(&self, path: &Path) -> bool

Check if this parser can handle the given file

Default implementation checks file extension.

Implementors§