use crate::ast::{AstAnalysis, ClassInfo, FunctionInfo, ImportInfo, VariableInfo};
use crate::parser::error::ParseError;
use std::path::Path;
pub trait CodeParser: Send + Sync {
fn parse_file(&self, path: &Path) -> Result<AstAnalysis, ParseError> {
let content =
std::fs::read_to_string(path).map_err(|e| ParseError::IoError(e.to_string()))?;
self.parse_content(&content)
}
fn parse_content(&self, content: &str) -> Result<AstAnalysis, ParseError>;
fn extract_functions(&self, content: &str) -> Vec<FunctionInfo>;
fn extract_classes(&self, content: &str) -> Vec<ClassInfo>;
fn extract_imports(&self, content: &str) -> Vec<ImportInfo>;
fn extract_variables(&self, content: &str) -> Vec<VariableInfo>;
fn language_name(&self) -> &'static str;
fn extensions(&self) -> &[&'static str];
}
pub trait ControlFlowParser {
fn extract_basic_blocks(&self, content: &str) -> Result<Vec<BasicBlock>, ParseError>;
fn extract_branches(&self, content: &str) -> Result<Vec<Branch>, ParseError>;
fn extract_loops(&self, content: &str) -> Result<Vec<Loop>, ParseError>;
}
pub trait ScopeParser {
fn extract_scopes(&self, content: &str) -> Result<Vec<Scope>, ParseError>;
fn find_variable_references(
&self,
content: &str,
var_name: &str,
) -> Result<Vec<Reference>, ParseError>;
}
#[derive(Debug, Clone)]
pub struct BasicBlock {
pub id: usize,
pub start_line: usize,
pub end_line: usize,
pub kind: BlockKind,
pub statements: Vec<String>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum BlockKind {
Entry,
Exit,
Normal,
Branch,
Loop,
Return,
}
#[derive(Debug, Clone)]
pub struct Branch {
pub condition: String,
pub line: usize,
pub true_block: Option<usize>,
pub false_block: Option<usize>,
}
#[derive(Debug, Clone)]
pub struct Loop {
pub loop_type: LoopType,
pub line: usize,
pub body_block: Option<usize>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum LoopType {
While,
Loop,
For,
ForIn,
}
#[derive(Debug, Clone)]
pub struct Scope {
pub id: usize,
pub start_line: usize,
pub end_line: usize,
pub parent_id: Option<usize>,
pub variables: Vec<String>,
}
#[derive(Debug, Clone)]
pub struct Reference {
pub name: String,
pub line: usize,
pub column: usize,
pub is_definition: bool,
pub is_usage: bool,
}