Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

feat(parser): add Zig parser support (.zig) (closes #140) (#251)

Joseph Mikhaila9e387ca7882
diff --git a/core-ingestion/package.json b/core-ingestion/package.json
index 02c728ef..852d70e9 100644
--- a/core-ingestion/package.json
+++ b/core-ingestion/package.json
@@ -34,6 +34,7 @@
"optionalDependencies": {
"@davisvaughan/tree-sitter-r": "^1.2.0",
"@tree-sitter-grammars/tree-sitter-lua": "^0.2.0",
+ "@tree-sitter-grammars/tree-sitter-zig": "^1.1.2",
"tree-sitter-bash": "^0.21.0",
"tree-sitter-elixir": "^0.3.5",
"tree-sitter-haskell": "^0.23.1",
diff --git a/core-ingestion/src/__tests__/queries.zig.test.ts b/core-ingestion/src/__tests__/queries.zig.test.ts
new file mode 100644
index 00000000..31d1710c
--- /dev/null
+++ b/core-ingestion/src/__tests__/queries.zig.test.ts
@@ -0,0 +1,58 @@
+import { describe, it, expect } from 'vitest';
+import { parseFile } from '../index.js';
+import { SupportedLanguages } from '../languages.js';
+
+// @tree-sitter-grammars/tree-sitter-zig has no win32/node26 prebuild; runs green
+// in CI (Linux/node24) and is skipped locally when the grammar is absent.
+const z = parseFile('/repo/m.zig', 'pub fn main() void {}\n');
+const describeFn = z && z.language === SupportedLanguages.Zig ? describe : describe.skip;
+
+describeFn('Zig queries', () => {
+ it('detects language as Zig', () => {
+ expect(parseFile('/repo/m.zig', 'const x = 1;\n')!.language).toBe(SupportedLanguages.Zig);
+ });
+
+ it('captures functions (incl. methods), structs and enums', () => {
+ const result = parseFile('/repo/p.zig', `
+pub fn add(a: i32, b: i32) i32 { return a + b; }
+fn helper(x: i32) i32 { return x; }
+const Point = struct {
+ x: i32,
+ pub fn dist(self: Point) i32 { return self.x; }
+};
+const Color = enum { red, green };
+`);
+ expect(result).not.toBeNull();
+ const names = result!.entities.map(e => e.name);
+ expect(names).toEqual(expect.arrayContaining(['add', 'helper', 'dist', 'Point', 'Color']));
+ expect(result!.entities).toContainEqual(
+ expect.objectContaining({ name: 'Point', kind: 'class' }),
+ );
+ });
+
+ it('emits CALLS for bare and field-access calls', () => {
+ const result = parseFile('/repo/r.zig', `
+pub fn main() void {
+ _ = add(1, 2);
+ std.debug.print("x", .{});
+}
+`);
+ expect(result).not.toBeNull();
+ const calls = result!.relationships
+ .filter(r => r.predicate === 'CALLS')
+ .map(r => r.dstName);
+ expect(calls).toEqual(expect.arrayContaining(['add', 'print']));
+ });
+
+ it('emits IMPORTS for @import', () => {
+ const result = parseFile('/repo/i.zig', `
+const std = @import("std");
+const mod = @import("./mod.zig");
+`);
+ expect(result).not.toBeNull();
+ const raws = result!.relationships
+ .filter(r => r.predicate === 'IMPORTS')
+ .map(r => r.importRaw);
+ expect(raws).toEqual(expect.arrayContaining(['std', './mod.zig']));
+ });
+});
diff --git a/core-ingestion/src/index.ts b/core-ingestion/src/index.ts
index 52dd3b41..d08e4215 100644
--- a/core-ingestion/src/index.ts
+++ b/core-ingestion/src/index.ts
@@ -38,6 +38,7 @@ const Elixir = tryLoadGrammar('tree-sitter-elixir');
const Make = tryLoadGrammar('tree-sitter-make');
const Bash = tryLoadGrammar('tree-sitter-bash');
const Haskell = tryLoadGrammar('tree-sitter-haskell');
+const Zig = tryLoadGrammar('@tree-sitter-grammars/tree-sitter-zig');
// tree-sitter-sas uses ESM bindings with top-level await — incompatible with tryLoadGrammar (CJS require)
let SAS: any = null;
// @ts-ignore
@@ -181,6 +182,7 @@ const GRAMMAR_MAP: Partial<Record<SupportedLanguages, any>> = {
...(Lua ? { [SupportedLanguages.Lua]: Lua } : {}),
...(Bash ? { [SupportedLanguages.Bash]: Bash } : {}),
...(Haskell ? { [SupportedLanguages.Haskell]: Haskell } : {}),
+ ...(Zig ? { [SupportedLanguages.Zig]: Zig } : {}),
};
// Capture key prefix → NodeKind string
diff --git a/core-ingestion/src/languages.ts b/core-ingestion/src/languages.ts
index 62beeabe..51587066 100644
--- a/core-ingestion/src/languages.ts
+++ b/core-ingestion/src/languages.ts
@@ -26,6 +26,7 @@ export enum SupportedLanguages {
Lua = 'lua',
Bash = 'bash',
Haskell = 'haskell',
+ Zig = 'zig',
}
const EXT_MAP: Record<string, SupportedLanguages> = {
@@ -74,6 +75,7 @@ const EXT_MAP: Record<string, SupportedLanguages> = {
'.ksh': SupportedLanguages.Bash,
'.hs': SupportedLanguages.Haskell,
'.lhs': SupportedLanguages.Haskell,
+ '.zig': SupportedLanguages.Zig,
};
export function languageFromPath(filePath: string): SupportedLanguages | null {
diff --git a/core-ingestion/src/queries.ts b/core-ingestion/src/queries.ts
index 5eda9918..7e126a36 100644
--- a/core-ingestion/src/queries.ts
+++ b/core-ingestion/src/queries.ts
@@ -1605,6 +1605,25 @@ export const R_QUERIES = `
(#eq? @_src "source")) @import
`;
+// Zig. Functions (top-level + struct/union methods are all function_declaration),
+// const-bound struct/enum/union types, calls (bare + field-access), and the
+// @import("path") builtin as an import.
+export const ZIG_QUERIES = `
+(function_declaration name: (identifier) @name) @definition.function
+
+(variable_declaration (identifier) @name (struct_declaration)) @definition.struct
+(variable_declaration (identifier) @name (enum_declaration)) @definition.enum
+(variable_declaration (identifier) @name (union_declaration)) @definition.struct
+
+(call_expression function: (identifier) @call.name) @call
+(call_expression function: (field_expression member: (identifier) @call.name)) @call
+
+(builtin_function
+ (builtin_identifier) @_imp
+ (arguments (string) @import.source)
+ (#eq? @_imp "@import")) @import
+`;
+
// Haskell. Function definitions (with args) and top-level binds (no args), data
// types / newtypes / type synonyms, type classes, the module header, function
// application as calls (bare and module-qualified), and module imports.
@@ -1701,5 +1720,6 @@ export const LANGUAGE_QUERIES: Record<SupportedLanguages, string> = {
[SupportedLanguages.Lua]: LUA_QUERIES,
[SupportedLanguages.Bash]: BASH_QUERIES,
[SupportedLanguages.Haskell]: HASKELL_QUERIES,
+ [SupportedLanguages.Zig]: ZIG_QUERIES,
};