use crate::models::{Class, Function};
use crate::parsers::{ImportInfo, ParseResult};
use anyhow::{Context, Result};
use std::cell::RefCell;
use std::collections::HashMap;
use std::path::Path;
use std::sync::OnceLock;
use tree_sitter::{Node, Parser, Query, QueryCursor, StreamingIterator};
thread_local! {
static CS_PARSER: RefCell<Parser> = RefCell::new({
let mut p = Parser::new();
p.set_language(&tree_sitter_c_sharp::LANGUAGE.into()).expect("C# language");
p
});
}
const CS_IMPORT_QUERY_STR: &str = r#"
(using_directive
(identifier) @import_name
)
(using_directive
(qualified_name) @import_name
)
"#;
static CS_IMPORT_QUERY: OnceLock<Query> = OnceLock::new();
#[allow(dead_code)]
pub fn parse(path: &Path) -> Result<ParseResult> {
let source = std::fs::read_to_string(path)
.with_context(|| format!("Failed to read file: {}", path.display()))?;
parse_source(&source, path)
}
pub fn parse_source(source: &str, path: &Path) -> Result<ParseResult> {
parse_source_with_tree(source, path).map(|(r, _)| r)
}
pub fn parse_source_with_tree(
source: &str,
path: &Path,
) -> Result<(ParseResult, tree_sitter::Tree)> {
let tree = CS_PARSER
.with(|cell| cell.borrow_mut().parse(source, None))
.context("Failed to parse C# source")?;
let root = tree.root_node();
let extractor = CSharpExtractor::new(source.as_bytes(), path);
let result = extractor.run(root)?;
Ok((result, tree))
}
struct CSharpExtractor<'a> {
source: &'a [u8],
path: &'a Path,
result: ParseResult,
}
impl<'a> CSharpExtractor<'a> {
fn new(source: &'a [u8], path: &'a Path) -> Self {
Self {
source,
path,
result: ParseResult::default(),
}
}
fn run(mut self, root: Node<'_>) -> Result<ParseResult> {
self.extract_types(&root)?;
self.extract_imports(&root)?;
self.extract_calls(&root)?;
Ok(self.result)
}
}
impl<'a> CSharpExtractor<'a> {
fn extract_types(&mut self, root: &Node) -> Result<()> {
self.extract_types_recursive(root, None);
Ok(())
}
fn extract_types_recursive(&mut self, node: &Node, parent_type: Option<&str>) {
for child in node.children(&mut node.walk()) {
match child.kind() {
"class_declaration" => {
if let Some(class) = self.parse_class_node(&child, parent_type) {
let class_name = class.name.clone();
self.extract_class_methods(&child, &class_name);
self.result.classes.push(class);
if let Some(body) = child.child_by_field_name("body") {
self.extract_types_recursive(&body, Some(&class_name));
}
}
}
"struct_declaration" => {
if let Some(struct_def) = self.parse_struct_node(&child, parent_type) {
let struct_name = struct_def.name.clone();
self.extract_class_methods(&child, &struct_name);
self.result.classes.push(struct_def);
}
}
"interface_declaration" => {
if let Some(iface) = self.parse_interface_node(&child, parent_type) {
let iface_name = iface.name.clone();
self.extract_interface_methods(&child, &iface_name);
self.result.classes.push(iface);
}
}
"record_declaration" | "record_struct_declaration" => {
if let Some(record) = self.parse_record_node(&child, parent_type) {
let record_name = record.name.clone();
self.extract_class_methods(&child, &record_name);
self.result.classes.push(record);
}
}
"enum_declaration" => {
if let Some(enum_def) = self.parse_enum_node(&child, parent_type) {
self.result.classes.push(enum_def);
}
}
"namespace_declaration" => {
if let Some(body) = child.child_by_field_name("body") {
self.extract_types_recursive(&body, parent_type);
}
}
"file_scoped_namespace_declaration" => {
self.extract_types_recursive(&child, parent_type);
}
_ => {
self.extract_types_recursive(&child, parent_type);
}
}
}
}
}
impl<'a> CSharpExtractor<'a> {
fn parse_class_node(&self, node: &Node, parent: Option<&str>) -> Option<Class> {
let name_node = node.child_by_field_name("name")?;
let name = name_node.utf8_text(self.source).ok()?.to_string();
let full_name = if let Some(parent_name) = parent {
format!("{}.{}", parent_name, name)
} else {
name.clone()
};
let line_start = node.start_position().row as u32 + 1;
let line_end = node.end_position().row as u32 + 1;
let qualified_name = format!("{}::{}:{}", self.path.display(), full_name, line_start);
let bases = extract_base_list(node, self.source);
let methods = extract_method_names(node, self.source);
Some(Class {
name: full_name,
qualified_name,
file_path: self.path.to_path_buf(),
line_start,
line_end,
methods,
field_count: 0,
bases,
doc_comment: None,
annotations: vec![],
})
}
}
impl<'a> CSharpExtractor<'a> {
fn parse_struct_node(&self, node: &Node, parent: Option<&str>) -> Option<Class> {
let name_node = node.child_by_field_name("name")?;
let name = name_node.utf8_text(self.source).ok()?.to_string();
let full_name = if let Some(parent_name) = parent {
format!("{}.{}", parent_name, name)
} else {
name.clone()
};
let line_start = node.start_position().row as u32 + 1;
let line_end = node.end_position().row as u32 + 1;
let qualified_name = format!(
"{}::struct::{}:{}",
self.path.display(),
full_name,
line_start
);
let bases = extract_base_list(node, self.source);
let methods = extract_method_names(node, self.source);
Some(Class {
name: full_name,
qualified_name,
file_path: self.path.to_path_buf(),
line_start,
line_end,
methods,
field_count: 0,
bases,
doc_comment: None,
annotations: vec![],
})
}
}
impl<'a> CSharpExtractor<'a> {
fn parse_interface_node(&self, node: &Node, parent: Option<&str>) -> Option<Class> {
let name_node = node.child_by_field_name("name")?;
let name = name_node.utf8_text(self.source).ok()?.to_string();
let full_name = if let Some(parent_name) = parent {
format!("{}.{}", parent_name, name)
} else {
name.clone()
};
let line_start = node.start_position().row as u32 + 1;
let line_end = node.end_position().row as u32 + 1;
let qualified_name = format!(
"{}::interface::{}:{}",
self.path.display(),
full_name,
line_start
);
let bases = extract_base_list(node, self.source);
let methods = extract_method_names(node, self.source);
Some(Class {
name: full_name,
qualified_name,
file_path: self.path.to_path_buf(),
line_start,
line_end,
methods,
field_count: 0,
bases,
doc_comment: None,
annotations: vec![],
})
}
}
impl<'a> CSharpExtractor<'a> {
fn parse_record_node(&self, node: &Node, parent: Option<&str>) -> Option<Class> {
let name_node = node.child_by_field_name("name")?;
let name = name_node.utf8_text(self.source).ok()?.to_string();
let full_name = if let Some(parent_name) = parent {
format!("{}.{}", parent_name, name)
} else {
name.clone()
};
let line_start = node.start_position().row as u32 + 1;
let line_end = node.end_position().row as u32 + 1;
let qualified_name = format!(
"{}::record::{}:{}",
self.path.display(),
full_name,
line_start
);
let bases = extract_base_list(node, self.source);
let methods = extract_method_names(node, self.source);
Some(Class {
name: full_name,
qualified_name,
file_path: self.path.to_path_buf(),
line_start,
line_end,
methods,
field_count: 0,
bases,
doc_comment: None,
annotations: vec![],
})
}
}
impl<'a> CSharpExtractor<'a> {
fn parse_enum_node(&self, node: &Node, parent: Option<&str>) -> Option<Class> {
let name_node = node.child_by_field_name("name")?;
let name = name_node.utf8_text(self.source).ok()?.to_string();
let full_name = if let Some(parent_name) = parent {
format!("{}.{}", parent_name, name)
} else {
name.clone()
};
let line_start = node.start_position().row as u32 + 1;
let line_end = node.end_position().row as u32 + 1;
let qualified_name = format!(
"{}::enum::{}:{}",
self.path.display(),
full_name,
line_start
);
Some(Class {
name: full_name,
qualified_name,
file_path: self.path.to_path_buf(),
line_start,
line_end,
methods: vec![],
field_count: 0,
bases: vec![],
doc_comment: None,
annotations: vec![],
})
}
}
fn extract_base_list(node: &Node, source: &[u8]) -> Vec<String> {
let mut bases = Vec::new();
if let Some(base_list) = node.child_by_field_name("bases") {
for child in base_list.children(&mut base_list.walk()) {
match child.kind() {
"identifier" | "generic_name" | "qualified_name" => {
if let Ok(text) = child.utf8_text(source) {
bases.push(text.to_string());
}
}
_ => {}
}
}
}
bases
}
fn extract_method_names(type_node: &Node, source: &[u8]) -> Vec<String> {
let mut methods = Vec::new();
let body = type_node.child_by_field_name("body");
let body_node = body.as_ref().unwrap_or(type_node);
for child in body_node.children(&mut body_node.walk()) {
match child.kind() {
"method_declaration" => {
if let Some(name_node) = child.child_by_field_name("name") {
if let Ok(name) = name_node.utf8_text(source) {
methods.push(name.to_string());
}
}
}
"constructor_declaration" => {
if let Some(name_node) = child.child_by_field_name("name") {
if let Ok(name) = name_node.utf8_text(source) {
methods.push(format!(".ctor:{}", name));
}
}
}
"property_declaration" => {
if let Some(name_node) = child.child_by_field_name("name") {
if let Ok(name) = name_node.utf8_text(source) {
methods.push(format!("prop:{}", name));
}
}
}
_ => {}
}
}
methods
}
impl<'a> CSharpExtractor<'a> {
fn extract_class_methods(&mut self, class_node: &Node, class_name: &str) {
let body = class_node.child_by_field_name("body");
let body_node = body.as_ref().unwrap_or(class_node);
for child in body_node.children(&mut body_node.walk()) {
match child.kind() {
"method_declaration" => {
if let Some(func) = self.parse_method_node(&child, class_name) {
self.result.functions.push(func);
}
}
"constructor_declaration" => {
if let Some(func) = self.parse_constructor_node(&child, class_name) {
self.result.functions.push(func);
}
}
"local_function_statement" => {
if let Some(func) = self.parse_local_function(&child, class_name) {
self.result.functions.push(func);
}
}
_ => {}
}
}
}
}
impl<'a> CSharpExtractor<'a> {
fn extract_interface_methods(&mut self, iface_node: &Node, iface_name: &str) {
let body = iface_node.child_by_field_name("body");
let body_node = body.as_ref().unwrap_or(iface_node);
for child in body_node.children(&mut body_node.walk()) {
if child.kind() == "method_declaration" {
if let Some(func) = self.parse_method_node(&child, iface_name) {
self.result.functions.push(func);
}
}
}
}
}
impl<'a> CSharpExtractor<'a> {
fn parse_method_node(&self, node: &Node, class_name: &str) -> Option<Function> {
let name_node = node.child_by_field_name("name")?;
let name = name_node.utf8_text(self.source).ok()?.to_string();
let params_node = node.child_by_field_name("parameters");
let parameters = extract_parameters(params_node, self.source);
let return_type = node
.child_by_field_name("type")
.and_then(|n| n.utf8_text(self.source).ok())
.map(|s| s.to_string());
let is_async = has_async_modifier(node, self.source);
let line_start = node.start_position().row as u32 + 1;
let line_end = node.end_position().row as u32 + 1;
let qualified_name = format!(
"{}::{}.{}:{}",
self.path.display(),
class_name,
name,
line_start
);
let mut annotations = Vec::new();
if has_public_modifier(node, self.source) {
annotations.push("exported".to_string());
}
Some(Function {
name,
qualified_name,
file_path: self.path.to_path_buf(),
line_start,
line_end,
parameters,
return_type,
is_async,
complexity: Some(calculate_complexity(node, self.source)),
max_nesting: None,
doc_comment: None,
annotations,
})
}
}
impl<'a> CSharpExtractor<'a> {
fn parse_constructor_node(&self, node: &Node, class_name: &str) -> Option<Function> {
let name_node = node.child_by_field_name("name")?;
let name = name_node.utf8_text(self.source).ok()?.to_string();
let params_node = node.child_by_field_name("parameters");
let parameters = extract_parameters(params_node, self.source);
let line_start = node.start_position().row as u32 + 1;
let line_end = node.end_position().row as u32 + 1;
let qualified_name = format!(
"{}::{}..ctor:{}",
self.path.display(),
class_name,
line_start
);
let mut annotations = Vec::new();
if has_public_modifier(node, self.source) {
annotations.push("exported".to_string());
}
Some(Function {
name: format!(".ctor:{}", name),
qualified_name,
file_path: self.path.to_path_buf(),
line_start,
line_end,
parameters,
return_type: Some(class_name.to_string()),
is_async: false,
complexity: Some(calculate_complexity(node, self.source)),
max_nesting: None,
doc_comment: None,
annotations,
})
}
}
impl<'a> CSharpExtractor<'a> {
fn parse_local_function(&self, node: &Node, class_name: &str) -> Option<Function> {
let name_node = node.child_by_field_name("name")?;
let name = name_node.utf8_text(self.source).ok()?.to_string();
let params_node = node.child_by_field_name("parameters");
let parameters = extract_parameters(params_node, self.source);
let return_type = node
.child_by_field_name("type")
.and_then(|n| n.utf8_text(self.source).ok())
.map(|s| s.to_string());
let is_async = has_async_modifier(node, self.source);
let line_start = node.start_position().row as u32 + 1;
let line_end = node.end_position().row as u32 + 1;
let qualified_name = format!(
"{}::{}.local::{}:{}",
self.path.display(),
class_name,
name,
line_start
);
Some(Function {
name,
qualified_name,
file_path: self.path.to_path_buf(),
line_start,
line_end,
parameters,
return_type,
is_async,
complexity: Some(calculate_complexity(node, self.source)),
max_nesting: None,
doc_comment: None,
annotations: vec![],
})
}
}
fn has_public_modifier(node: &Node, source: &[u8]) -> bool {
for child in node.children(&mut node.walk()) {
if child.kind() == "modifier" {
if let Ok(text) = child.utf8_text(source) {
if text == "public" {
return true;
}
}
}
}
false
}
fn has_async_modifier(node: &Node, source: &[u8]) -> bool {
for child in node.children(&mut node.walk()) {
if child.kind() == "modifier" {
if let Ok(text) = child.utf8_text(source) {
if text == "async" {
return true;
}
}
}
}
false
}
fn extract_parameters(params_node: Option<Node>, source: &[u8]) -> Vec<String> {
let Some(node) = params_node else {
return vec![];
};
let mut params = Vec::new();
for child in node.children(&mut node.walk()) {
if child.kind() == "parameter" {
if let Some(name_node) = child.child_by_field_name("name") {
if let Ok(text) = name_node.utf8_text(source) {
params.push(text.to_string());
}
}
}
}
params
}
impl<'a> CSharpExtractor<'a> {
fn extract_imports(&mut self, root: &Node) -> Result<()> {
let query = CS_IMPORT_QUERY.get_or_init(|| {
Query::new(&tree_sitter_c_sharp::LANGUAGE.into(), CS_IMPORT_QUERY_STR)
.expect("valid C# import query")
});
let mut cursor = QueryCursor::new();
let mut matches = cursor.matches(query, *root, self.source);
while let Some(m) = matches.next() {
for capture in m.captures.iter() {
if let Ok(text) = capture.node.utf8_text(self.source) {
self.result
.imports
.push(ImportInfo::runtime(text.to_string()));
}
}
}
Ok(())
}
}
impl<'a> CSharpExtractor<'a> {
fn extract_calls(&mut self, root: &Node) -> Result<()> {
let mut scope_map: HashMap<(u32, u32), String> = HashMap::new();
for func in &self.result.functions {
scope_map.insert(
(func.line_start, func.line_end),
func.qualified_name.clone(),
);
}
self.extract_calls_recursive(root, &scope_map);
Ok(())
}
fn extract_calls_recursive(&mut self, node: &Node, scope_map: &HashMap<(u32, u32), String>) {
if node.kind() == "invocation_expression" {
let call_line = node.start_position().row as u32 + 1;
let caller = find_containing_scope(call_line, scope_map)
.unwrap_or_else(|| self.path.display().to_string());
if let Some(func_node) = node.child_by_field_name("function") {
if let Some(callee) = extract_call_target(&func_node, self.source) {
self.result.calls.push((caller, callee));
}
}
}
if node.kind() == "object_creation_expression" {
let call_line = node.start_position().row as u32 + 1;
let caller = find_containing_scope(call_line, scope_map)
.unwrap_or_else(|| self.path.display().to_string());
if let Some(type_node) = node.child_by_field_name("type") {
if let Ok(callee) = type_node.utf8_text(self.source) {
self.result.calls.push((caller, format!("new {}", callee)));
}
}
}
for child in node.children(&mut node.walk()) {
self.extract_calls_recursive(&child, scope_map);
}
}
}
fn find_containing_scope(line: u32, scope_map: &HashMap<(u32, u32), String>) -> Option<String> {
super::find_containing_scope(line, scope_map)
}
fn extract_call_target(node: &Node, source: &[u8]) -> Option<String> {
match node.kind() {
"identifier" => node.utf8_text(source).ok().map(|s| s.to_string()),
"member_access_expression" => node.utf8_text(source).ok().map(|s| s.to_string()),
"generic_name" => node.utf8_text(source).ok().map(|s| s.to_string()),
_ => node.utf8_text(source).ok().map(|s| s.to_string()),
}
}
fn calculate_complexity(node: &Node, _source: &[u8]) -> u32 {
let mut complexity = 1;
fn count_branches(node: &Node, complexity: &mut u32) {
match node.kind() {
"if_statement" | "while_statement" | "for_statement" | "foreach_statement"
| "do_statement" => {
*complexity += 1;
}
"catch_clause" => {
*complexity += 1;
}
"switch_section" => {
*complexity += 1;
}
"conditional_expression" => {
*complexity += 1;
}
"binary_expression" => {
for child in node.children(&mut node.walk()) {
if child.kind() == "&&" || child.kind() == "||" {
*complexity += 1;
}
}
}
"lambda_expression" => {
*complexity += 1;
}
"null_coalescing_expression" => {
*complexity += 1;
}
_ => {}
}
for child in node.children(&mut node.walk()) {
count_branches(&child, complexity);
}
}
count_branches(node, &mut complexity);
complexity
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
#[test]
fn test_parse_simple_class() {
let source = r#"
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
"#;
let path = PathBuf::from("HelloWorld.cs");
let result = parse_source(source, &path).expect("should parse C# source");
assert_eq!(result.classes.len(), 1);
let class = &result.classes[0];
assert_eq!(class.name, "HelloWorld");
}
#[test]
fn test_parse_class_with_inheritance() {
let source = r#"
public class Child : Parent, IDisposable
{
public void Dispose() { }
}
"#;
let path = PathBuf::from("Child.cs");
let result = parse_source(source, &path).expect("should parse C# source");
assert_eq!(result.classes.len(), 1);
let class = &result.classes[0];
assert_eq!(class.name, "Child");
}
#[test]
fn test_parse_interface() {
let source = r#"
public interface IMyInterface
{
void DoSomething();
Task<int> DoAsync();
}
"#;
let path = PathBuf::from("IMyInterface.cs");
let result = parse_source(source, &path).expect("should parse C# source");
assert_eq!(result.classes.len(), 1);
let iface = &result.classes[0];
assert_eq!(iface.name, "IMyInterface");
}
#[test]
fn test_parse_async_method() {
let source = r#"
public class AsyncClass
{
public async Task<string> FetchDataAsync()
{
return await Task.FromResult("data");
}
}
"#;
let path = PathBuf::from("AsyncClass.cs");
let result = parse_source(source, &path).expect("should parse C# source");
assert!(result
.functions
.iter()
.any(|f| f.name == "FetchDataAsync" && f.is_async));
}
#[test]
fn test_parse_imports() {
let source = r#"
using System;
using System.Collections.Generic;
using System.Linq;
public class Test { }
"#;
let path = PathBuf::from("Test.cs");
let result = parse_source(source, &path).expect("should parse C# source");
assert!(result.imports.iter().any(|i| i.path == "System"));
assert!(result
.imports
.iter()
.any(|i| i.path == "System.Collections.Generic"));
}
#[test]
fn test_parse_record() {
let source = r#"
public record Person(string Name, int Age);
"#;
let path = PathBuf::from("Person.cs");
let result = parse_source(source, &path).expect("should parse C# source");
assert_eq!(result.classes.len(), 1);
assert_eq!(result.classes[0].name, "Person");
}
#[test]
fn test_public_methods_exported() {
let source = r#"
public class MyService
{
public void PublicMethod() {}
private void PrivateMethod() {}
protected void ProtectedMethod() {}
internal void InternalMethod() {}
void DefaultMethod() {}
}
"#;
let path = PathBuf::from("MyService.cs");
let result = parse_source(source, &path).expect("should parse C# source");
let public_m = result
.functions
.iter()
.find(|f| f.name == "PublicMethod")
.expect("should find PublicMethod");
assert!(
public_m.annotations.contains(&"exported".to_string()),
"public method should be exported"
);
let private_m = result
.functions
.iter()
.find(|f| f.name == "PrivateMethod")
.expect("should find PrivateMethod");
assert!(
!private_m.annotations.contains(&"exported".to_string()),
"private method should not be exported"
);
let protected_m = result
.functions
.iter()
.find(|f| f.name == "ProtectedMethod")
.expect("should find ProtectedMethod");
assert!(
!protected_m.annotations.contains(&"exported".to_string()),
"protected method should not be exported"
);
let internal_m = result
.functions
.iter()
.find(|f| f.name == "InternalMethod")
.expect("should find InternalMethod");
assert!(
!internal_m.annotations.contains(&"exported".to_string()),
"internal method should not be exported"
);
let default_m = result
.functions
.iter()
.find(|f| f.name == "DefaultMethod")
.expect("should find DefaultMethod");
assert!(
!default_m.annotations.contains(&"exported".to_string()),
"default method should not be exported"
);
}
#[test]
fn test_public_constructor_exported() {
let source = r#"
public class MyClass
{
public MyClass(int x) {}
private MyClass() {}
}
"#;
let path = PathBuf::from("MyClass.cs");
let result = parse_source(source, &path).expect("should parse C# source");
let public_ctor = result
.functions
.iter()
.find(|f| f.name.contains("MyClass") && !f.annotations.is_empty())
.expect("should find public constructor");
assert!(
public_ctor.annotations.contains(&"exported".to_string()),
"public constructor should be exported"
);
let private_ctor = result
.functions
.iter()
.find(|f| f.name.contains("MyClass") && f.annotations.is_empty())
.expect("should find private constructor");
assert!(
!private_ctor.annotations.contains(&"exported".to_string()),
"private constructor should not be exported"
);
}
}