use std::time::{Instant, SystemTime, UNIX_EPOCH};
use tree_sitter::{Node as TsNode, Parser, Tree};
use crate::extraction::complexity::{count_complexity, PASCAL_COMPLEXITY};
use crate::types::{
generate_node_id, Edge, EdgeKind, ExtractionResult, Node, NodeKind, UnresolvedRef, Visibility,
};
pub struct PascalExtractor;
struct ExtractionState {
nodes: Vec<Node>,
edges: Vec<Edge>,
unresolved_refs: Vec<UnresolvedRef>,
errors: Vec<String>,
node_stack: Vec<(String, String)>,
file_path: String,
source: Vec<u8>,
timestamp: u64,
class_depth: usize,
current_visibility: Visibility,
in_implementation: bool,
}
impl ExtractionState {
fn new(file_path: &str, source: &str) -> Self {
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
Self {
nodes: Vec::new(),
edges: Vec::new(),
unresolved_refs: Vec::new(),
errors: Vec::new(),
node_stack: Vec::new(),
file_path: file_path.to_string(),
source: source.as_bytes().to_vec(),
timestamp,
class_depth: 0,
current_visibility: Visibility::Pub,
in_implementation: false,
}
}
fn qualified_prefix(&self) -> String {
let mut parts = vec![self.file_path.clone()];
for (name, _) in &self.node_stack {
parts.push(name.clone());
}
parts.join("::")
}
fn parent_node_id(&self) -> Option<&str> {
self.node_stack.last().map(|(_, id)| id.as_str())
}
fn node_text(&self, node: TsNode<'_>) -> String {
node.utf8_text(&self.source)
.unwrap_or("<invalid utf8>")
.to_string()
}
}
impl PascalExtractor {
pub fn extract_pascal(file_path: &str, source: &str) -> ExtractionResult {
let start = Instant::now();
let mut state = ExtractionState::new(file_path, source);
let tree = match Self::parse_source(source) {
Ok(tree) => tree,
Err(msg) => {
state.errors.push(msg);
return Self::build_result(state, start);
}
};
let file_node = Node {
id: generate_node_id(file_path, &NodeKind::File, file_path, 0),
kind: NodeKind::File,
name: file_path.to_string(),
qualified_name: file_path.to_string(),
file_path: file_path.to_string(),
start_line: 0,
end_line: source.lines().count().saturating_sub(1) as u32,
start_column: 0,
end_column: 0,
signature: None,
docstring: None,
visibility: Visibility::Pub,
is_async: false,
branches: 0,
loops: 0,
returns: 0,
max_nesting: 0,
unsafe_blocks: 0,
unchecked_calls: 0,
assertions: 0,
updated_at: state.timestamp,
};
let file_node_id = file_node.id.clone();
state.nodes.push(file_node);
state.node_stack.push((file_path.to_string(), file_node_id));
let root = tree.root_node();
Self::visit_children(&mut state, root);
state.node_stack.pop();
Self::build_result(state, start)
}
fn parse_source(source: &str) -> Result<Tree, String> {
let mut parser = Parser::new();
let language = crate::extraction::ts_provider::language("pascal");
parser
.set_language(&language)
.map_err(|e| format!("failed to load Pascal grammar: {e}"))?;
parser
.parse(source, None)
.ok_or_else(|| "tree-sitter parse returned None".to_string())
}
fn visit_children(state: &mut ExtractionState, node: TsNode<'_>) {
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
Self::visit_node(state, child);
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn visit_node(state: &mut ExtractionState, node: TsNode<'_>) {
match node.kind() {
"program" => Self::visit_program(state, node),
"unit" => Self::visit_unit(state, node),
"interface" => Self::visit_interface_section(state, node),
"implementation" => Self::visit_implementation_section(state, node),
"declUses" => Self::visit_uses_clause(state, node),
"declTypes" => Self::visit_type_section(state, node),
"declConsts" => Self::visit_const_section(state, node),
"declVars" => Self::visit_var_section(state, node),
"defProc" => Self::visit_def_proc(state, node),
_ => {
Self::visit_children(state, node);
}
}
}
fn visit_program(state: &mut ExtractionState, node: TsNode<'_>) {
let name = Self::find_module_name(state, node);
let start_line = node.start_position().row as u32;
let end_line = node.end_position().row as u32;
let start_column = node.start_position().column as u32;
let end_column = node.end_position().column as u32;
let qualified_name = format!("{}::{}", state.qualified_prefix(), name);
let id = generate_node_id(&state.file_path, &NodeKind::PascalProgram, &name, start_line);
let graph_node = Node {
id: id.clone(),
kind: NodeKind::PascalProgram,
name: name.clone(),
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature: Some(format!("program {name}")),
docstring: Self::extract_docstring(state, node),
visibility: Visibility::Pub,
is_async: false,
branches: 0,
loops: 0,
returns: 0,
max_nesting: 0,
unsafe_blocks: 0,
unchecked_calls: 0,
assertions: 0,
updated_at: state.timestamp,
};
state.nodes.push(graph_node);
if let Some(parent_id) = state.parent_node_id() {
state.edges.push(Edge {
source: parent_id.to_string(),
target: id.clone(),
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
state.node_stack.push((name, id));
Self::visit_children(state, node);
state.node_stack.pop();
}
fn visit_unit(state: &mut ExtractionState, node: TsNode<'_>) {
let name = Self::find_module_name(state, node);
let start_line = node.start_position().row as u32;
let end_line = node.end_position().row as u32;
let start_column = node.start_position().column as u32;
let end_column = node.end_position().column as u32;
let qualified_name = format!("{}::{}", state.qualified_prefix(), name);
let id = generate_node_id(&state.file_path, &NodeKind::PascalUnit, &name, start_line);
let graph_node = Node {
id: id.clone(),
kind: NodeKind::PascalUnit,
name: name.clone(),
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature: Some(format!("unit {name}")),
docstring: Self::extract_docstring(state, node),
visibility: Visibility::Pub,
is_async: false,
branches: 0,
loops: 0,
returns: 0,
max_nesting: 0,
unsafe_blocks: 0,
unchecked_calls: 0,
assertions: 0,
updated_at: state.timestamp,
};
state.nodes.push(graph_node);
if let Some(parent_id) = state.parent_node_id() {
state.edges.push(Edge {
source: parent_id.to_string(),
target: id.clone(),
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
state.node_stack.push((name, id));
Self::visit_children(state, node);
state.node_stack.pop();
}
fn visit_interface_section(state: &mut ExtractionState, node: TsNode<'_>) {
state.in_implementation = false;
state.current_visibility = Visibility::Pub;
Self::visit_children(state, node);
}
fn visit_implementation_section(state: &mut ExtractionState, node: TsNode<'_>) {
state.in_implementation = true;
state.current_visibility = Visibility::Private;
Self::visit_children(state, node);
}
fn visit_uses_clause(state: &mut ExtractionState, node: TsNode<'_>) {
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if child.kind() == "moduleName" {
Self::visit_single_use(state, child);
}
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn visit_single_use(state: &mut ExtractionState, node: TsNode<'_>) {
let name = Self::find_child_by_kind(node, "identifier")
.map(|n| state.node_text(n))
.unwrap_or_else(|| state.node_text(node));
let start_line = node.start_position().row as u32;
let end_line = node.end_position().row as u32;
let start_column = node.start_position().column as u32;
let end_column = node.end_position().column as u32;
let qualified_name = format!("{}::{}", state.qualified_prefix(), name);
let id = generate_node_id(&state.file_path, &NodeKind::Use, &name, start_line);
let graph_node = Node {
id: id.clone(),
kind: NodeKind::Use,
name: name.clone(),
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature: Some(format!("uses {name}")),
docstring: None,
visibility: Visibility::Private,
is_async: false,
branches: 0,
loops: 0,
returns: 0,
max_nesting: 0,
unsafe_blocks: 0,
unchecked_calls: 0,
assertions: 0,
updated_at: state.timestamp,
};
state.nodes.push(graph_node);
if let Some(parent_id) = state.parent_node_id() {
state.edges.push(Edge {
source: parent_id.to_string(),
target: id.clone(),
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
state.unresolved_refs.push(UnresolvedRef {
from_node_id: id,
reference_name: name,
reference_kind: EdgeKind::Uses,
line: start_line,
column: start_column,
file_path: state.file_path.clone(),
});
}
fn visit_type_section(state: &mut ExtractionState, node: TsNode<'_>) {
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if child.kind() == "declType" {
Self::visit_type_decl(state, child);
}
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn visit_type_decl(state: &mut ExtractionState, node: TsNode<'_>) {
let name = Self::find_child_by_kind(node, "identifier")
.map(|n| state.node_text(n))
.unwrap_or_else(|| "<anonymous>".to_string());
if let Some(class_node) = Self::find_child_by_kind(node, "declClass") {
if Self::find_child_by_kind(class_node, "kRecord").is_some() {
Self::visit_record_type(state, &name, class_node, node);
} else {
Self::visit_class_type(state, &name, class_node, node);
}
} else if let Some(intf_node) = Self::find_child_by_kind(node, "declIntf") {
Self::visit_interface_type(state, &name, intf_node, node);
} else {
Self::visit_type_alias(state, &name, node);
}
}
fn visit_class_type(
state: &mut ExtractionState,
name: &str,
class_node: TsNode<'_>,
decl_node: TsNode<'_>,
) {
let visibility = if state.in_implementation {
Visibility::Private
} else {
Visibility::Pub
};
let docstring = Self::extract_docstring(state, decl_node);
let start_line = decl_node.start_position().row as u32;
let end_line = decl_node.end_position().row as u32;
let start_column = decl_node.start_position().column as u32;
let end_column = decl_node.end_position().column as u32;
let qualified_name = format!("{}::{}", state.qualified_prefix(), name);
let id = generate_node_id(&state.file_path, &NodeKind::Class, name, start_line);
let mut sig = format!("{name} = class");
if let Some(parent_ref) = Self::find_child_by_kind(class_node, "typeref") {
let parent_name = state.node_text(parent_ref);
sig = format!("{name} = class({parent_name})");
}
let graph_node = Node {
id: id.clone(),
kind: NodeKind::Class,
name: name.to_string(),
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature: Some(sig),
docstring,
visibility,
is_async: false,
branches: 0,
loops: 0,
returns: 0,
max_nesting: 0,
unsafe_blocks: 0,
unchecked_calls: 0,
assertions: 0,
updated_at: state.timestamp,
};
state.nodes.push(graph_node);
if let Some(parent_id) = state.parent_node_id() {
state.edges.push(Edge {
source: parent_id.to_string(),
target: id.clone(),
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
if let Some(parent_ref) = Self::find_child_by_kind(class_node, "typeref") {
let parent_name = state.node_text(parent_ref);
state.unresolved_refs.push(UnresolvedRef {
from_node_id: id.clone(),
reference_name: parent_name,
reference_kind: EdgeKind::Extends,
line: parent_ref.start_position().row as u32,
column: parent_ref.start_position().column as u32,
file_path: state.file_path.clone(),
});
}
state.class_depth += 1;
let saved_visibility = state.current_visibility.clone();
state.current_visibility = Visibility::Pub;
state.node_stack.push((name.to_string(), id));
Self::visit_class_body(state, class_node);
state.node_stack.pop();
state.current_visibility = saved_visibility;
state.class_depth -= 1;
}
fn visit_record_type(
state: &mut ExtractionState,
name: &str,
class_node: TsNode<'_>,
decl_node: TsNode<'_>,
) {
let visibility = if state.in_implementation {
Visibility::Private
} else {
Visibility::Pub
};
let docstring = Self::extract_docstring(state, decl_node);
let start_line = decl_node.start_position().row as u32;
let end_line = decl_node.end_position().row as u32;
let start_column = decl_node.start_position().column as u32;
let end_column = decl_node.end_position().column as u32;
let qualified_name = format!("{}::{}", state.qualified_prefix(), name);
let id = generate_node_id(&state.file_path, &NodeKind::PascalRecord, name, start_line);
let graph_node = Node {
id: id.clone(),
kind: NodeKind::PascalRecord,
name: name.to_string(),
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature: Some(format!("{name} = record")),
docstring,
visibility,
is_async: false,
branches: 0,
loops: 0,
returns: 0,
max_nesting: 0,
unsafe_blocks: 0,
unchecked_calls: 0,
assertions: 0,
updated_at: state.timestamp,
};
state.nodes.push(graph_node);
if let Some(parent_id) = state.parent_node_id() {
state.edges.push(Edge {
source: parent_id.to_string(),
target: id.clone(),
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
state.node_stack.push((name.to_string(), id));
Self::visit_record_body(state, class_node);
state.node_stack.pop();
}
fn visit_interface_type(
state: &mut ExtractionState,
name: &str,
intf_node: TsNode<'_>,
decl_node: TsNode<'_>,
) {
let visibility = if state.in_implementation {
Visibility::Private
} else {
Visibility::Pub
};
let docstring = Self::extract_docstring(state, decl_node);
let start_line = decl_node.start_position().row as u32;
let end_line = decl_node.end_position().row as u32;
let start_column = decl_node.start_position().column as u32;
let end_column = decl_node.end_position().column as u32;
let qualified_name = format!("{}::{}", state.qualified_prefix(), name);
let id = generate_node_id(&state.file_path, &NodeKind::Interface, name, start_line);
let graph_node = Node {
id: id.clone(),
kind: NodeKind::Interface,
name: name.to_string(),
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature: Some(format!("{name} = interface")),
docstring,
visibility,
is_async: false,
branches: 0,
loops: 0,
returns: 0,
max_nesting: 0,
unsafe_blocks: 0,
unchecked_calls: 0,
assertions: 0,
updated_at: state.timestamp,
};
state.nodes.push(graph_node);
if let Some(parent_id) = state.parent_node_id() {
state.edges.push(Edge {
source: parent_id.to_string(),
target: id.clone(),
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
state.node_stack.push((name.to_string(), id));
Self::visit_interface_body(state, intf_node);
state.node_stack.pop();
}
fn visit_type_alias(state: &mut ExtractionState, name: &str, decl_node: TsNode<'_>) {
let visibility = if state.in_implementation {
Visibility::Private
} else {
Visibility::Pub
};
let docstring = Self::extract_docstring(state, decl_node);
let text = state.node_text(decl_node);
let start_line = decl_node.start_position().row as u32;
let end_line = decl_node.end_position().row as u32;
let start_column = decl_node.start_position().column as u32;
let end_column = decl_node.end_position().column as u32;
let qualified_name = format!("{}::{}", state.qualified_prefix(), name);
let id = generate_node_id(&state.file_path, &NodeKind::TypeAlias, name, start_line);
let graph_node = Node {
id: id.clone(),
kind: NodeKind::TypeAlias,
name: name.to_string(),
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature: Some(text.trim().trim_end_matches(';').trim().to_string()),
docstring,
visibility,
is_async: false,
branches: 0,
loops: 0,
returns: 0,
max_nesting: 0,
unsafe_blocks: 0,
unchecked_calls: 0,
assertions: 0,
updated_at: state.timestamp,
};
state.nodes.push(graph_node);
if let Some(parent_id) = state.parent_node_id() {
state.edges.push(Edge {
source: parent_id.to_string(),
target: id,
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
}
fn visit_class_body(state: &mut ExtractionState, class_node: TsNode<'_>) {
let mut cursor = class_node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
match child.kind() {
"declSection" => Self::visit_visibility_section(state, child),
"declField" => Self::visit_field(state, child),
"declProc" => Self::visit_class_method_decl(state, child),
"declProp" => Self::visit_property(state, child),
_ => {}
}
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn visit_record_body(state: &mut ExtractionState, class_node: TsNode<'_>) {
let mut cursor = class_node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if child.kind() == "declField" {
Self::visit_field(state, child);
}
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn visit_interface_body(state: &mut ExtractionState, intf_node: TsNode<'_>) {
let mut cursor = intf_node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if child.kind() == "declProc" {
Self::visit_interface_method_decl(state, child);
}
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn visit_visibility_section(state: &mut ExtractionState, node: TsNode<'_>) {
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
match child.kind() {
"kPublic" => state.current_visibility = Visibility::Pub,
"kPrivate" => state.current_visibility = Visibility::Private,
"kProtected" => state.current_visibility = Visibility::PubSuper,
"declField" => Self::visit_field(state, child),
"declProc" => Self::visit_class_method_decl(state, child),
"declProp" => Self::visit_property(state, child),
_ => {}
}
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn visit_field(state: &mut ExtractionState, node: TsNode<'_>) {
let name = Self::find_child_by_kind(node, "identifier")
.map(|n| state.node_text(n))
.unwrap_or_else(|| "<anonymous>".to_string());
let text = state.node_text(node);
let start_line = node.start_position().row as u32;
let end_line = node.end_position().row as u32;
let start_column = node.start_position().column as u32;
let end_column = node.end_position().column as u32;
let qualified_name = format!("{}::{}", state.qualified_prefix(), name);
let id = generate_node_id(&state.file_path, &NodeKind::Field, &name, start_line);
let graph_node = Node {
id: id.clone(),
kind: NodeKind::Field,
name: name.clone(),
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature: Some(text.trim().trim_end_matches(';').trim().to_string()),
docstring: None,
visibility: state.current_visibility.clone(),
is_async: false,
branches: 0,
loops: 0,
returns: 0,
max_nesting: 0,
unsafe_blocks: 0,
unchecked_calls: 0,
assertions: 0,
updated_at: state.timestamp,
};
state.nodes.push(graph_node);
if let Some(parent_id) = state.parent_node_id() {
state.edges.push(Edge {
source: parent_id.to_string(),
target: id,
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
}
fn visit_class_method_decl(state: &mut ExtractionState, node: TsNode<'_>) {
let (_kind_str, node_kind) = Self::determine_proc_kind(node);
let name = Self::find_proc_name(state, node);
let text = state.node_text(node);
let start_line = node.start_position().row as u32;
let end_line = node.end_position().row as u32;
let start_column = node.start_position().column as u32;
let end_column = node.end_position().column as u32;
let qualified_name = format!("{}::{}", state.qualified_prefix(), name);
let id = generate_node_id(&state.file_path, &node_kind, &name, start_line);
let metrics = count_complexity(node, &PASCAL_COMPLEXITY, &state.source);
let graph_node = Node {
id: id.clone(),
kind: node_kind,
name: name.clone(),
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature: Some(text.trim().trim_end_matches(';').trim().to_string()),
docstring: None,
visibility: state.current_visibility.clone(),
is_async: false,
branches: metrics.branches,
loops: metrics.loops,
returns: metrics.returns,
max_nesting: metrics.max_nesting,
unsafe_blocks: metrics.unsafe_blocks,
unchecked_calls: metrics.unchecked_calls,
assertions: metrics.assertions,
updated_at: state.timestamp,
};
state.nodes.push(graph_node);
if let Some(parent_id) = state.parent_node_id() {
state.edges.push(Edge {
source: parent_id.to_string(),
target: id,
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
}
fn visit_interface_method_decl(state: &mut ExtractionState, node: TsNode<'_>) {
let (_, node_kind) = Self::determine_proc_kind(node);
let name = Self::find_proc_name(state, node);
let text = state.node_text(node);
let start_line = node.start_position().row as u32;
let end_line = node.end_position().row as u32;
let start_column = node.start_position().column as u32;
let end_column = node.end_position().column as u32;
let qualified_name = format!("{}::{}", state.qualified_prefix(), name);
let id = generate_node_id(&state.file_path, &node_kind, &name, start_line);
let metrics = count_complexity(node, &PASCAL_COMPLEXITY, &state.source);
let graph_node = Node {
id: id.clone(),
kind: node_kind,
name: name.clone(),
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature: Some(text.trim().trim_end_matches(';').trim().to_string()),
docstring: None,
visibility: Visibility::Pub,
is_async: false,
branches: metrics.branches,
loops: metrics.loops,
returns: metrics.returns,
max_nesting: metrics.max_nesting,
unsafe_blocks: metrics.unsafe_blocks,
unchecked_calls: metrics.unchecked_calls,
assertions: metrics.assertions,
updated_at: state.timestamp,
};
state.nodes.push(graph_node);
if let Some(parent_id) = state.parent_node_id() {
state.edges.push(Edge {
source: parent_id.to_string(),
target: id,
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
}
fn visit_property(state: &mut ExtractionState, node: TsNode<'_>) {
let name = Self::find_child_by_kind(node, "identifier")
.map(|n| state.node_text(n))
.unwrap_or_else(|| "<anonymous>".to_string());
let text = state.node_text(node);
let start_line = node.start_position().row as u32;
let end_line = node.end_position().row as u32;
let start_column = node.start_position().column as u32;
let end_column = node.end_position().column as u32;
let qualified_name = format!("{}::{}", state.qualified_prefix(), name);
let id = generate_node_id(&state.file_path, &NodeKind::Property, &name, start_line);
let graph_node = Node {
id: id.clone(),
kind: NodeKind::Property,
name: name.clone(),
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature: Some(text.trim().trim_end_matches(';').trim().to_string()),
docstring: None,
visibility: state.current_visibility.clone(),
is_async: false,
branches: 0,
loops: 0,
returns: 0,
max_nesting: 0,
unsafe_blocks: 0,
unchecked_calls: 0,
assertions: 0,
updated_at: state.timestamp,
};
state.nodes.push(graph_node);
if let Some(parent_id) = state.parent_node_id() {
state.edges.push(Edge {
source: parent_id.to_string(),
target: id,
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
}
fn visit_const_section(state: &mut ExtractionState, node: TsNode<'_>) {
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if child.kind() == "declConst" {
Self::visit_const(state, child);
}
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn visit_const(state: &mut ExtractionState, node: TsNode<'_>) {
let name = Self::find_child_by_kind(node, "identifier")
.map(|n| state.node_text(n))
.unwrap_or_else(|| "<anonymous>".to_string());
let text = state.node_text(node);
let start_line = node.start_position().row as u32;
let end_line = node.end_position().row as u32;
let start_column = node.start_position().column as u32;
let end_column = node.end_position().column as u32;
let qualified_name = format!("{}::{}", state.qualified_prefix(), name);
let id = generate_node_id(&state.file_path, &NodeKind::Const, &name, start_line);
let visibility = if state.in_implementation {
Visibility::Private
} else {
Visibility::Pub
};
let graph_node = Node {
id: id.clone(),
kind: NodeKind::Const,
name: name.clone(),
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature: Some(text.trim().trim_end_matches(';').trim().to_string()),
docstring: None,
visibility,
is_async: false,
branches: 0,
loops: 0,
returns: 0,
max_nesting: 0,
unsafe_blocks: 0,
unchecked_calls: 0,
assertions: 0,
updated_at: state.timestamp,
};
state.nodes.push(graph_node);
if let Some(parent_id) = state.parent_node_id() {
state.edges.push(Edge {
source: parent_id.to_string(),
target: id,
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
}
fn visit_var_section(state: &mut ExtractionState, node: TsNode<'_>) {
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if child.kind() == "declVar" {
Self::visit_var(state, child);
}
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn visit_var(state: &mut ExtractionState, node: TsNode<'_>) {
let name = Self::find_child_by_kind(node, "identifier")
.map(|n| state.node_text(n))
.unwrap_or_else(|| "<anonymous>".to_string());
let text = state.node_text(node);
let start_line = node.start_position().row as u32;
let end_line = node.end_position().row as u32;
let start_column = node.start_position().column as u32;
let end_column = node.end_position().column as u32;
let qualified_name = format!("{}::{}", state.qualified_prefix(), name);
let id = generate_node_id(&state.file_path, &NodeKind::Static, &name, start_line);
let visibility = if state.in_implementation {
Visibility::Private
} else {
Visibility::Pub
};
let graph_node = Node {
id: id.clone(),
kind: NodeKind::Static,
name: name.clone(),
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature: Some(text.trim().trim_end_matches(';').trim().to_string()),
docstring: None,
visibility,
is_async: false,
branches: 0,
loops: 0,
returns: 0,
max_nesting: 0,
unsafe_blocks: 0,
unchecked_calls: 0,
assertions: 0,
updated_at: state.timestamp,
};
state.nodes.push(graph_node);
if let Some(parent_id) = state.parent_node_id() {
state.edges.push(Edge {
source: parent_id.to_string(),
target: id,
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
}
fn visit_def_proc(state: &mut ExtractionState, node: TsNode<'_>) {
let decl = Self::find_child_by_kind(node, "declProc");
let block = Self::find_child_by_kind(node, "block");
if let Some(decl_node) = decl {
let (kind_str, node_kind) = Self::determine_proc_kind(decl_node);
let name = Self::find_proc_name(state, decl_node);
let decl_text = state.node_text(decl_node);
let docstring = Self::extract_docstring(state, node);
let start_line = node.start_position().row as u32;
let end_line = node.end_position().row as u32;
let start_column = node.start_position().column as u32;
let end_column = node.end_position().column as u32;
let (is_method, class_name, method_name) = Self::parse_dotted_name(state, decl_node);
let display_name = if is_method {
method_name.clone()
} else {
name.clone()
};
let actual_kind = if is_method {
match kind_str {
"constructor" => NodeKind::Constructor,
"destructor" => NodeKind::Method,
_ => NodeKind::Method,
}
} else {
node_kind.clone()
};
let qualified_name = format!("{}::{}", state.qualified_prefix(), display_name);
let id = generate_node_id(
&state.file_path,
&actual_kind,
&display_name,
start_line,
);
let visibility = if state.in_implementation {
Visibility::Private
} else {
Visibility::Pub
};
let metrics = count_complexity(node, &PASCAL_COMPLEXITY, &state.source);
let graph_node = Node {
id: id.clone(),
kind: actual_kind,
name: display_name,
qualified_name,
file_path: state.file_path.clone(),
start_line,
end_line,
start_column,
end_column,
signature: Some(decl_text.trim().trim_end_matches(';').trim().to_string()),
docstring,
visibility,
is_async: false,
branches: metrics.branches,
loops: metrics.loops,
returns: metrics.returns,
max_nesting: metrics.max_nesting,
unsafe_blocks: metrics.unsafe_blocks,
unchecked_calls: metrics.unchecked_calls,
assertions: metrics.assertions,
updated_at: state.timestamp,
};
state.nodes.push(graph_node);
if let Some(parent_id) = state.parent_node_id() {
state.edges.push(Edge {
source: parent_id.to_string(),
target: id.clone(),
kind: EdgeKind::Contains,
line: Some(start_line),
});
}
if is_method && !class_name.is_empty() {
state.unresolved_refs.push(UnresolvedRef {
from_node_id: id.clone(),
reference_name: class_name,
reference_kind: EdgeKind::Receives,
line: start_line,
column: start_column,
file_path: state.file_path.clone(),
});
}
if let Some(block_node) = block {
Self::extract_call_sites(state, block_node, &id);
}
}
}
fn find_module_name(state: &ExtractionState, node: TsNode<'_>) -> String {
if let Some(mod_name) = Self::find_child_by_kind(node, "moduleName") {
if let Some(ident) = Self::find_child_by_kind(mod_name, "identifier") {
return state.node_text(ident);
}
return state.node_text(mod_name);
}
"<unknown>".to_string()
}
fn determine_proc_kind(node: TsNode<'_>) -> (&'static str, NodeKind) {
if Self::find_child_by_kind(node, "kConstructor").is_some() {
("constructor", NodeKind::Constructor)
} else if Self::find_child_by_kind(node, "kDestructor").is_some() {
("destructor", NodeKind::Method)
} else if Self::find_child_by_kind(node, "kProcedure").is_some() {
("procedure", NodeKind::Procedure)
} else if Self::find_child_by_kind(node, "kFunction").is_some() {
("function", NodeKind::Function)
} else {
("unknown", NodeKind::Function)
}
}
fn find_proc_name(state: &ExtractionState, node: TsNode<'_>) -> String {
if let Some(dot_node) = Self::find_child_by_kind(node, "genericDot") {
return state.node_text(dot_node);
}
Self::find_child_by_kind(node, "identifier")
.map(|n| state.node_text(n))
.unwrap_or_else(|| "<anonymous>".to_string())
}
fn parse_dotted_name(
state: &ExtractionState,
decl_node: TsNode<'_>,
) -> (bool, String, String) {
if let Some(dot_node) = Self::find_child_by_kind(decl_node, "genericDot") {
let mut identifiers = Vec::new();
let mut cursor = dot_node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if child.kind() == "identifier" {
identifiers.push(state.node_text(child));
}
if !cursor.goto_next_sibling() {
break;
}
}
}
if identifiers.len() >= 2 {
let class_name = identifiers[0].clone();
let method_name = identifiers[identifiers.len() - 1].clone();
return (true, class_name, method_name);
}
}
(false, String::new(), String::new())
}
fn extract_call_sites(state: &mut ExtractionState, node: TsNode<'_>, fn_node_id: &str) {
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
match child.kind() {
"exprCall" => {
if let Some(callee) = child.named_child(0) {
let callee_name = state.node_text(callee);
state.unresolved_refs.push(UnresolvedRef {
from_node_id: fn_node_id.to_string(),
reference_name: callee_name,
reference_kind: EdgeKind::Calls,
line: child.start_position().row as u32,
column: child.start_position().column as u32,
file_path: state.file_path.clone(),
});
}
Self::extract_call_sites(state, child, fn_node_id);
}
"statement" => {
let first_child = child.named_child(0);
if let Some(fc) = first_child {
if fc.kind() == "identifier" {
let has_call =
Self::find_child_by_kind(child, "exprCall").is_some();
if !has_call {
let callee_name = state.node_text(fc);
if !matches!(
callee_name.as_str(),
"inherited" | "break" | "continue" | "exit"
) {
state.unresolved_refs.push(UnresolvedRef {
from_node_id: fn_node_id.to_string(),
reference_name: callee_name,
reference_kind: EdgeKind::Calls,
line: fc.start_position().row as u32,
column: fc.start_position().column as u32,
file_path: state.file_path.clone(),
});
}
}
}
}
Self::extract_call_sites(state, child, fn_node_id);
}
_ => {
Self::extract_call_sites(state, child, fn_node_id);
}
}
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn extract_docstring(state: &ExtractionState, node: TsNode<'_>) -> Option<String> {
let mut comments = Vec::new();
let mut current = node.prev_named_sibling();
while let Some(sibling) = current {
if sibling.kind() == "comment" {
let text = state.node_text(sibling);
comments.push(text);
current = sibling.prev_named_sibling();
} else {
break;
}
}
if comments.is_empty() {
return None;
}
comments.reverse();
let cleaned: Vec<String> = comments.iter().map(|c| Self::clean_comment(c)).collect();
let result = cleaned.join("\n").trim().to_string();
if result.is_empty() {
None
} else {
Some(result)
}
}
fn clean_comment(comment: &str) -> String {
let trimmed = comment.trim();
if let Some(stripped) = trimmed.strip_prefix("//") {
stripped.strip_prefix(' ').unwrap_or(stripped).to_string()
} else if trimmed.starts_with('{') && trimmed.ends_with('}') {
let inner = &trimmed[1..trimmed.len() - 1];
inner.trim().to_string()
} else if trimmed.starts_with("(*") && trimmed.ends_with("*)") {
let inner = &trimmed[2..trimmed.len() - 2];
inner
.lines()
.map(|line| line.trim())
.collect::<Vec<_>>()
.join("\n")
.trim()
.to_string()
} else {
trimmed.to_string()
}
}
fn find_child_by_kind<'a>(node: TsNode<'a>, kind: &str) -> Option<TsNode<'a>> {
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if child.kind() == kind {
return Some(child);
}
if !cursor.goto_next_sibling() {
break;
}
}
}
None
}
fn build_result(state: ExtractionState, start: Instant) -> ExtractionResult {
ExtractionResult {
nodes: state.nodes,
edges: state.edges,
unresolved_refs: state.unresolved_refs,
errors: state.errors,
duration_ms: start.elapsed().as_millis() as u64,
}
}
}
impl crate::extraction::LanguageExtractor for PascalExtractor {
fn extensions(&self) -> &[&str] {
&["pas", "pp", "dpr", "lpr"]
}
fn language_name(&self) -> &str {
"Pascal"
}
fn extract(&self, file_path: &str, source: &str) -> ExtractionResult {
PascalExtractor::extract_pascal(file_path, source)
}
}