use std::time::{Instant, SystemTime, UNIX_EPOCH};
use tree_sitter::{Node as TsNode, Parser, Tree};
use crate::extraction::complexity::{count_complexity, GDSCRIPT_COMPLEXITY};
use crate::types::{
generate_node_id, Edge, EdgeKind, ExtractionResult, Node, NodeKind, UnresolvedRef, Visibility,
};
pub struct GdScriptExtractor;
#[derive(Clone, Copy, PartialEq, Eq)]
enum ScopeKind {
File,
Script,
Class,
}
struct Scope {
kind: ScopeKind,
qual: String,
id: String,
}
struct ExtractionState {
nodes: Vec<Node>,
edges: Vec<Edge>,
unresolved_refs: Vec<UnresolvedRef>,
errors: Vec<String>,
scope_stack: Vec<Scope>,
file_path: String,
source: Vec<u8>,
timestamp: u64,
}
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(),
scope_stack: Vec::new(),
file_path: file_path.to_string(),
source: source.as_bytes().to_vec(),
timestamp,
}
}
fn node_text(&self, node: TsNode<'_>) -> String {
node.utf8_text(&self.source)
.unwrap_or("<invalid utf8>")
.to_string()
}
fn current_scope(&self) -> &Scope {
match self.scope_stack.last() {
Some(scope) => scope,
None => unreachable!("scope stack underflow"),
}
}
fn parent_node_id(&self) -> Option<&str> {
self.scope_stack.last().map(|s| s.id.as_str())
}
fn member_qualified_name(&self, name: &str) -> String {
let s = self.current_scope();
match s.kind {
ScopeKind::File => format!("{}::{}", s.qual, name),
ScopeKind::Script | ScopeKind::Class => format!("{}.{}", s.qual, name),
}
}
fn push_contains_edge(&mut self, child_id: &str, line: u32) {
if let Some(parent_id) = self.parent_node_id() {
self.edges.push(Edge {
source: parent_id.to_string(),
target: child_id.to_string(),
kind: EdgeKind::Contains,
line: Some(line),
});
}
}
#[allow(clippy::too_many_arguments)]
fn add_node(
&mut self,
kind: NodeKind,
name: &str,
qualified_name: String,
node: TsNode<'_>,
attrs_start_line: u32,
signature: Option<String>,
docstring: Option<String>,
visibility: Visibility,
metrics: crate::extraction::complexity::ComplexityMetrics,
) -> Option<String> {
if name.is_empty() {
return None;
}
let start_line = node.start_position().row as u32;
let id = generate_node_id(&self.file_path, &kind, name, start_line);
let graph_node = Node {
id: id.clone(),
kind,
name: name.to_string(),
qualified_name,
file_path: self.file_path.clone(),
start_line,
attrs_start_line,
end_line: node.end_position().row as u32,
start_column: node.start_position().column as u32,
end_column: node.end_position().column as u32,
signature,
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,
cognitive_complexity: metrics.cognitive_complexity,
distinct_operators: metrics.distinct_operators,
distinct_operands: metrics.distinct_operands,
total_operators: metrics.total_operators,
total_operands: metrics.total_operands,
updated_at: self.timestamp,
parent_id: None,
};
self.nodes.push(graph_node);
self.push_contains_edge(&id, start_line);
Some(id)
}
}
impl GdScriptExtractor {
pub fn extract_gdscript(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 root = tree.root_node();
if root.has_error() {
state
.errors
.push(format!("parse errors in {file_path} (partial extraction)"));
}
let file_id = generate_node_id(file_path, &NodeKind::File, file_path, 0);
state.nodes.push(Node {
id: file_id.clone(),
kind: NodeKind::File,
name: file_path.to_string(),
qualified_name: file_path.to_string(),
file_path: file_path.to_string(),
start_line: 0,
attrs_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,
cognitive_complexity: 0,
distinct_operators: 0,
distinct_operands: 0,
total_operators: 0,
total_operands: 0,
updated_at: state.timestamp,
parent_id: None,
});
state.scope_stack.push(Scope {
kind: ScopeKind::File,
qual: file_path.to_string(),
id: file_id,
});
let class_name_node = Self::find_child_by_kind(root, "class_name_statement");
let script_name = match class_name_node {
Some(cn) => cn
.child_by_field_name("name")
.map_or_else(String::new, |n| state.node_text(n)),
None => Self::file_stem(file_path),
};
let script_kind = if class_name_node.is_some() {
NodeKind::Class
} else {
NodeKind::Module
};
let attrs_start = class_name_node.map_or(0, |n| Self::attrs_start_line(n));
let docstring = class_name_node.and_then(|n| Self::extract_docstring(&state, n));
let signature = class_name_node.map(|n| Self::first_line(&state, n));
let qn = state.member_qualified_name(&script_name);
let script_id = state.add_node(
script_kind,
&script_name,
qn.clone(),
root,
attrs_start,
signature,
docstring,
Visibility::Pub,
crate::extraction::complexity::ComplexityMetrics::default(),
);
let Some(script_id) = script_id else {
state.scope_stack.pop();
return Self::build_result(state, start);
};
let extends_stmt = class_name_node
.and_then(|cn| cn.child_by_field_name("extends"))
.or_else(|| Self::find_child_by_kind(root, "extends_statement"));
if let Some(es) = extends_stmt {
if let Some(target) = Self::extends_text(&state, es) {
Self::push_ref(&mut state, &script_id, &target, EdgeKind::Extends, es);
}
}
state.scope_stack.push(Scope {
kind: ScopeKind::Script,
qual: qn,
id: script_id,
});
Self::visit_members(&mut state, root);
state.scope_stack.pop(); state.scope_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("gdscript");
parser
.set_language(&language)
.map_err(|e| format!("failed to load GDScript grammar: {e}"))?;
parser
.parse(source, None)
.ok_or_else(|| "tree-sitter parse returned None".to_string())
}
fn visit_members(state: &mut ExtractionState, container: TsNode<'_>) {
let mut cursor = container.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if child.is_named() {
Self::visit_node(state, child);
}
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn visit_node(state: &mut ExtractionState, node: TsNode<'_>) {
match node.kind() {
"function_definition" => Self::visit_function(state, node),
"constructor_definition" => Self::visit_constructor(state, node),
"signal_statement" => Self::visit_signal(state, node),
"variable_statement" | "export_variable_statement" | "onready_variable_statement" => {
Self::visit_field(state, node);
}
"const_statement" => Self::visit_const(state, node),
"enum_definition" => Self::visit_enum(state, node),
"class_definition" => Self::visit_inner_class(state, node),
_ => {}
}
}
fn visit_function(state: &mut ExtractionState, node: TsNode<'_>) {
let Some(name_node) = node.child_by_field_name("name") else {
return;
};
let name = state.node_text(name_node);
if name.is_empty() {
return;
}
let kind = match state.current_scope().kind {
ScopeKind::Class => NodeKind::Method,
ScopeKind::Script | ScopeKind::File => NodeKind::Function,
};
let body = node.child_by_field_name("body");
let metrics = body
.map(|b| count_complexity(b, &GDSCRIPT_COMPLEXITY, &state.source))
.unwrap_or_default();
let attrs_start = Self::attrs_start_line(node);
let qn = state.member_qualified_name(&name);
let signature = Some(Self::signature_text(state, node, body));
let id = state.add_node(
kind,
&name,
qn,
node,
attrs_start,
signature,
Self::extract_docstring(state, node),
Visibility::Pub,
metrics,
);
let Some(id) = id else { return };
if let Some(rt) = node.child_by_field_name("return_type") {
let ty = state.node_text(rt);
if !ty.is_empty() {
Self::push_ref(state, &id, &ty, EdgeKind::TypeOf, rt);
}
}
if let Some(body) = body {
Self::extract_call_sites(state, body, &id);
}
}
fn visit_constructor(state: &mut ExtractionState, node: TsNode<'_>) {
let name = "_init";
let body = node.child_by_field_name("body");
let metrics = body
.map(|b| count_complexity(b, &GDSCRIPT_COMPLEXITY, &state.source))
.unwrap_or_default();
let attrs_start = Self::attrs_start_line(node);
let qn = state.member_qualified_name(name);
let signature = Some(Self::signature_text(state, node, body));
let id = state.add_node(
NodeKind::Constructor,
name,
qn,
node,
attrs_start,
signature,
Self::extract_docstring(state, node),
Visibility::Pub,
metrics,
);
let Some(id) = id else { return };
if let Some(rt) = node.child_by_field_name("return_type") {
let ty = state.node_text(rt);
if !ty.is_empty() {
Self::push_ref(state, &id, &ty, EdgeKind::TypeOf, rt);
}
}
if let Some(body) = body {
Self::extract_call_sites(state, body, &id);
}
}
fn visit_signal(state: &mut ExtractionState, node: TsNode<'_>) {
let Some(name_node) = node.child_by_field_name("name") else {
return;
};
let name = state.node_text(name_node);
if name.is_empty() {
return;
}
let attrs_start = Self::attrs_start_line(node);
let qn = state.member_qualified_name(&name);
let signature = Some(Self::first_line(state, node));
state.add_node(
NodeKind::Signal,
&name,
qn,
node,
attrs_start,
signature,
Self::extract_docstring(state, node),
Visibility::Pub,
crate::extraction::complexity::ComplexityMetrics::default(),
);
}
fn visit_field(state: &mut ExtractionState, node: TsNode<'_>) {
let Some(name_node) = node.child_by_field_name("name") else {
return;
};
let name = state.node_text(name_node);
if name.is_empty() {
return;
}
let attrs_start = Self::attrs_start_line(node);
let qn = state.member_qualified_name(&name);
let signature = Some(Self::first_line(state, node));
let id = state.add_node(
NodeKind::Field,
&name,
qn,
node,
attrs_start,
signature,
Self::extract_docstring(state, node),
Visibility::Pub,
crate::extraction::complexity::ComplexityMetrics::default(),
);
let Some(id) = id else { return };
Self::push_type_ref(state, &id, node);
}
fn visit_const(state: &mut ExtractionState, node: TsNode<'_>) {
let Some(name_node) = node.child_by_field_name("name") else {
return;
};
let name = state.node_text(name_node);
if name.is_empty() {
return;
}
let attrs_start = Self::attrs_start_line(node);
let qn = state.member_qualified_name(&name);
let signature = Some(Self::first_line(state, node));
let id = state.add_node(
NodeKind::Const,
&name,
qn,
node,
attrs_start,
signature,
Self::extract_docstring(state, node),
Visibility::Pub,
crate::extraction::complexity::ComplexityMetrics::default(),
);
let Some(id) = id else { return };
Self::push_type_ref(state, &id, node);
}
fn push_type_ref(state: &mut ExtractionState, id: &str, node: TsNode<'_>) {
if let Some(ty_node) = node.child_by_field_name("type") {
if ty_node.kind() == "type" {
let ty = state.node_text(ty_node);
if !ty.is_empty() {
Self::push_ref(state, id, &ty, EdgeKind::TypeOf, ty_node);
}
}
}
}
fn visit_enum(state: &mut ExtractionState, node: TsNode<'_>) {
let name = node
.child_by_field_name("name")
.map_or_else(|| "<anonymous>".to_string(), |n| state.node_text(n));
let attrs_start = Self::attrs_start_line(node);
let qn = state.member_qualified_name(&name);
let signature = Some(Self::first_line(state, node));
let id = state.add_node(
NodeKind::Enum,
&name,
qn.clone(),
node,
attrs_start,
signature,
Self::extract_docstring(state, node),
Visibility::Pub,
crate::extraction::complexity::ComplexityMetrics::default(),
);
let Some(id) = id else { return };
state.scope_stack.push(Scope {
kind: ScopeKind::Class,
qual: qn,
id,
});
if let Some(body) = node.child_by_field_name("body") {
Self::visit_enum_variants(state, body);
}
state.scope_stack.pop();
}
fn visit_enum_variants(state: &mut ExtractionState, list: TsNode<'_>) {
let mut cursor = list.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if child.kind() == "enumerator" {
Self::visit_enumerator(state, child);
}
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn visit_enumerator(state: &mut ExtractionState, node: TsNode<'_>) {
let Some(left) = node.child_by_field_name("left") else {
return;
};
let name = state.node_text(left);
if name.is_empty() {
return;
}
let attrs_start = node.start_position().row as u32;
let qn = state.member_qualified_name(&name);
let signature = Some(Self::first_line(state, node));
state.add_node(
NodeKind::EnumVariant,
&name,
qn,
node,
attrs_start,
signature,
None,
Visibility::Pub,
crate::extraction::complexity::ComplexityMetrics::default(),
);
}
fn visit_inner_class(state: &mut ExtractionState, node: TsNode<'_>) {
let Some(name_node) = node.child_by_field_name("name") else {
return;
};
let name = state.node_text(name_node);
if name.is_empty() {
return;
}
let attrs_start = Self::attrs_start_line(node);
let qn = state.member_qualified_name(&name);
let signature = Some(Self::first_line(state, node));
let id = state.add_node(
NodeKind::InnerClass,
&name,
qn.clone(),
node,
attrs_start,
signature,
Self::extract_docstring(state, node),
Visibility::Pub,
crate::extraction::complexity::ComplexityMetrics::default(),
);
let Some(id) = id else { return };
let extends_stmt = node.child_by_field_name("extends").or_else(|| {
node.child_by_field_name("body")
.and_then(|b| Self::find_child_by_kind(b, "extends_statement"))
});
if let Some(es) = extends_stmt {
if let Some(target) = Self::extends_text(state, es) {
Self::push_ref(state, &id, &target, EdgeKind::Extends, es);
}
}
state.scope_stack.push(Scope {
kind: ScopeKind::Class,
qual: qn,
id,
});
if let Some(body) = node.child_by_field_name("body") {
Self::visit_members(state, body);
}
state.scope_stack.pop();
}
fn push_ref(
state: &mut ExtractionState,
from_id: &str,
name: &str,
kind: EdgeKind,
at: TsNode<'_>,
) {
if name.is_empty() {
return;
}
state.unresolved_refs.push(UnresolvedRef {
from_node_id: from_id.to_string(),
reference_name: name.to_string(),
reference_kind: kind,
line: at.start_position().row as u32,
column: at.start_position().column as u32,
file_path: state.file_path.clone(),
});
}
fn extends_text(state: &ExtractionState, extends_stmt: TsNode<'_>) -> Option<String> {
let mut cursor = extends_stmt.walk();
if cursor.goto_first_child() {
loop {
let c = cursor.node();
match c.kind() {
"type" => return Some(state.node_text(c)),
"string" => {
let raw = state.node_text(c);
return Some(raw.trim_matches(|ch| ch == '"' || ch == '\'').to_string());
}
_ => {}
}
if !cursor.goto_next_sibling() {
break;
}
}
}
None
}
fn file_stem(file_path: &str) -> String {
let base = file_path.rsplit('/').next().unwrap_or(file_path);
base.strip_suffix(".gd").unwrap_or(base).to_string()
}
fn first_line(state: &ExtractionState, node: TsNode<'_>) -> String {
state
.node_text(node)
.lines()
.next()
.unwrap_or_default()
.trim()
.to_string()
}
fn signature_text(
state: &ExtractionState,
node: TsNode<'_>,
body: Option<TsNode<'_>>,
) -> String {
if let Some(body) = body {
let start = node.start_byte();
let end = body.start_byte().min(state.source.len());
if end > start {
if let Ok(s) = std::str::from_utf8(&state.source[start..end]) {
return s.trim().trim_end_matches(':').trim().to_string();
}
}
}
Self::first_line(state, node)
}
fn attrs_start_line(node: TsNode<'_>) -> u32 {
let mut start = node.start_position().row as u32;
let mut prev = node.prev_named_sibling();
while let Some(p) = prev {
if p.kind() == "comment" {
start = p.start_position().row as u32;
prev = p.prev_named_sibling();
} else {
break;
}
}
start
}
fn extract_docstring(state: &ExtractionState, node: TsNode<'_>) -> Option<String> {
let mut comments: Vec<String> = Vec::new();
let mut prev = node.prev_named_sibling();
while let Some(p) = prev {
if p.kind() == "comment" {
let t = state.node_text(p);
comments.push(t.trim_start_matches('#').trim().to_string());
prev = p.prev_named_sibling();
} else {
break;
}
}
if comments.is_empty() {
return None;
}
comments.reverse();
Some(comments.join("\n"))
}
fn extract_call_sites(state: &mut ExtractionState, node: TsNode<'_>, fn_id: &str) {
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
if matches!(child.kind(), "call" | "attribute_call") {
if let Some(callee) = Self::callee_name(state, child) {
state.unresolved_refs.push(UnresolvedRef {
from_node_id: fn_id.to_string(),
reference_name: callee,
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_id);
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn callee_name(state: &ExtractionState, node: TsNode<'_>) -> Option<String> {
match node.kind() {
"attribute_call" => {
Self::find_child_by_kind(node, "identifier").map(|n| state.node_text(n))
}
"call" => {
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let c = cursor.node();
if c.is_named() && c.kind() != "arguments" {
let text = state.node_text(c);
let trimmed = text.trim();
return if trimmed.is_empty() {
None
} else {
Some(trimmed.rsplit('.').next().unwrap_or(trimmed).to_string())
};
}
if !cursor.goto_next_sibling() {
break;
}
}
}
None
}
_ => None,
}
}
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 c = cursor.node();
if c.kind() == kind {
return Some(c);
}
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 GdScriptExtractor {
fn extensions(&self) -> &[&str] {
&["gd"]
}
fn language_name(&self) -> &'static str {
"GDScript"
}
fn extract(&self, file_path: &str, source: &str) -> ExtractionResult {
Self::extract_gdscript(file_path, source)
}
}