Skip to content

Commit cfc2369

Browse files
unhappychoiceclaude
andcommitted
feat: add Zig tree-sitter parser and extractor
- Implement ZigExtractor with query patterns for functions, structs, enums, and unions - Add support for extracting code blocks (loops, conditionals, etc.) - Register Zig parser in ParserRegistry 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 9de8234 commit cfc2369

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

src/domain/services/source_code_parser/parsers/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::domain::models::languages::{
22
CSharp, Cpp, Dart, Go, Haskell, Java, JavaScript, Kotlin, Php, Python, Ruby, Rust, Scala,
3-
Swift, TypeScript, C,
3+
Swift, TypeScript, Zig, C,
44
};
55
use crate::domain::models::ChunkType;
66
use crate::domain::models::Language;
@@ -26,6 +26,7 @@ pub mod rust;
2626
pub mod scala;
2727
pub mod swift;
2828
pub mod typescript;
29+
pub mod zig;
2930

3031
pub trait LanguageExtractor {
3132
fn tree_sitter_language(&self) -> tree_sitter::Language;
@@ -82,6 +83,7 @@ impl ParserRegistry {
8283
register_language!(Rust, rust, RustExtractor);
8384
register_language!(Scala, scala, ScalaExtractor);
8485
register_language!(Swift, swift, SwiftExtractor);
86+
register_language!(Zig, zig, ZigExtractor);
8587

8688
registry
8789
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
use super::LanguageExtractor;
2+
use crate::domain::models::ChunkType;
3+
use crate::{GitTypeError, Result};
4+
use tree_sitter::{Node, Parser};
5+
6+
pub struct ZigExtractor;
7+
8+
impl LanguageExtractor for ZigExtractor {
9+
fn tree_sitter_language(&self) -> tree_sitter::Language {
10+
tree_sitter_zig::LANGUAGE.into()
11+
}
12+
13+
fn query_patterns(&self) -> &str {
14+
"
15+
(function_declaration name: (identifier) @name) @function
16+
(variable_declaration (identifier) @name \"=\" (struct_declaration)) @struct
17+
(variable_declaration (identifier) @name \"=\" (enum_declaration)) @enum
18+
(variable_declaration (identifier) @name \"=\" (union_declaration)) @union
19+
"
20+
}
21+
22+
fn comment_query(&self) -> &str {
23+
"(comment) @comment"
24+
}
25+
26+
fn capture_name_to_chunk_type(&self, capture_name: &str) -> Option<ChunkType> {
27+
match capture_name {
28+
"function" => Some(ChunkType::Function),
29+
"struct" => Some(ChunkType::Struct),
30+
"enum" => Some(ChunkType::Enum),
31+
"union" => Some(ChunkType::Struct),
32+
_ => None,
33+
}
34+
}
35+
36+
fn extract_name(&self, node: Node, source_code: &str, _capture_name: &str) -> Option<String> {
37+
self.extract_name_from_node(node, source_code)
38+
}
39+
40+
fn middle_implementation_query(&self) -> &str {
41+
"
42+
(for_statement) @for_loop
43+
(while_statement) @while_loop
44+
(if_statement) @if_block
45+
(switch_expression) @switch_expr
46+
(block) @code_block
47+
"
48+
}
49+
50+
fn middle_capture_name_to_chunk_type(&self, capture_name: &str) -> Option<ChunkType> {
51+
match capture_name {
52+
"for_loop" | "while_loop" => Some(ChunkType::Loop),
53+
"if_block" => Some(ChunkType::Conditional),
54+
"switch_expr" => Some(ChunkType::Conditional),
55+
"code_block" => Some(ChunkType::CodeBlock),
56+
_ => None,
57+
}
58+
}
59+
}
60+
61+
impl ZigExtractor {
62+
fn extract_name_from_node(&self, node: Node, source_code: &str) -> Option<String> {
63+
let mut cursor = node.walk();
64+
if cursor.goto_first_child() {
65+
loop {
66+
let child = cursor.node();
67+
if child.kind() == "identifier" {
68+
let start = child.start_byte();
69+
let end = child.end_byte();
70+
return Some(source_code[start..end].to_string());
71+
}
72+
if !cursor.goto_next_sibling() {
73+
break;
74+
}
75+
}
76+
}
77+
None
78+
}
79+
80+
pub fn create_parser() -> Result<Parser> {
81+
let mut parser = Parser::new();
82+
parser
83+
.set_language(&tree_sitter_zig::LANGUAGE.into())
84+
.map_err(|e| {
85+
GitTypeError::ExtractionFailed(format!("Failed to set Zig language: {}", e))
86+
})?;
87+
Ok(parser)
88+
}
89+
}

0 commit comments

Comments
 (0)