Understand any codebase instantly. System intelligence for codebases, built for humans and AI.
TypeScript aiai-memoryclaude-codeclicode-analysiscode-mappingcode-memorycodebase-understandingcodexdeveloper-toolsllmopenclawpersistent-memoryprogram-analysissoftware-architecturesystem-intelligencesystem-mapping
feat(parser): add Lua parser support (.lua) (#136) (#248)
Joseph Mikhail8d1bf033a0d7
diff --git a/core-ingestion/package-lock.json b/core-ingestion/package-lock.jsonindex 0ffeba04..7514f0ef 100644--- a/core-ingestion/package-lock.json+++ b/core-ingestion/package-lock.json@@ -32,6 +32,7 @@},"optionalDependencies": {"@davisvaughan/tree-sitter-r": "^1.2.0",+ "@tree-sitter-grammars/tree-sitter-lua": "^0.2.0","tree-sitter-elixir": "^0.3.5","tree-sitter-kotlin": "^0.3.8","tree-sitter-make": "^1.1.1",@@ -395,6 +396,26 @@"dev": true,"license": "MIT"},+ "node_modules/@tree-sitter-grammars/tree-sitter-lua": {+ "version": "0.2.0",+ "resolved": "https://registry.npmjs.org/@tree-sitter-grammars/tree-sitter-lua/-/tree-sitter-lua-0.2.0.tgz",+ "integrity": "sha512-VPk971J3hBUNr7CpZJvgTU5UmEkW8iMQ0Sk6br4O3xL00XFEO4PCjSSGeYYakRgPAJiIR8J594UcBK+7ilBvqw==",+ "hasInstallScript": true,+ "license": "MIT",+ "optional": true,+ "dependencies": {+ "node-addon-api": "^8.0.0",+ "node-gyp-build": "^4.8.1"+ },+ "peerDependencies": {+ "tree-sitter": "^0.21.1"+ },+ "peerDependenciesMeta": {+ "tree_sitter": {+ "optional": true+ }+ }+ },"node_modules/@tybys/wasm-util": {"version": "0.10.1","resolved": "https://registry.npmjs.org/@tybys/wasm-util/-/wasm-util-0.10.1.tgz",diff --git a/core-ingestion/package.json b/core-ingestion/package.jsonindex c78df8d0..45fad4a1 100644--- a/core-ingestion/package.json+++ b/core-ingestion/package.json@@ -32,12 +32,13 @@"tree-sitter-typescript": "^0.21.0"},"optionalDependencies": {- "tree-sitter-kotlin": "^0.3.8",- "tree-sitter-sas": "^0.4.2",- "tree-sitter-swift": "^0.6.0",+ "@davisvaughan/tree-sitter-r": "^1.2.0",+ "@tree-sitter-grammars/tree-sitter-lua": "^0.2.0","tree-sitter-elixir": "^0.3.5",+ "tree-sitter-kotlin": "^0.3.8","tree-sitter-make": "^1.1.1",- "@davisvaughan/tree-sitter-r": "^1.2.0"+ "tree-sitter-sas": "^0.4.2",+ "tree-sitter-swift": "^0.6.0"},"devDependencies": {"@emnapi/core": "1.9.2",diff --git a/core-ingestion/src/__tests__/queries.lua.test.ts b/core-ingestion/src/__tests__/queries.lua.test.tsnew file mode 100644index 00000000..9995e94c--- /dev/null+++ b/core-ingestion/src/__tests__/queries.lua.test.ts@@ -0,0 +1,65 @@+import { describe, it, expect } from 'vitest';+import { parseFile } from '../index.js';+import { SupportedLanguages } from '../languages.js';++describe('Lua queries', () => {+ it('detects language as Lua', () => {+ const result = parseFile('/repo/mod.lua', `local x = 1\n`);+ expect(result).not.toBeNull();+ expect(result!.language).toBe(SupportedLanguages.Lua);+ });++ it('captures plain, local, dotted and method function declarations', () => {+ const result = parseFile('/repo/mod.lua', `+function top() return 1 end+local function helper(x) return x + 1 end+function M.process(data) return helper(data) end+function obj:method() return self.x end+`);+ expect(result).not.toBeNull();+ const names = result!.entities.map(e => e.name);+ expect(names).toEqual(expect.arrayContaining(['top', 'helper', 'process']));+ expect(result!.entities).toContainEqual(+ expect.objectContaining({ name: 'method', kind: 'method' }),+ );+ });++ it('captures function expressions assigned to a name or table field', () => {+ const result = parseFile('/repo/mod.lua', `+local M = {}+M.handler = function(req) return req end+local cb = function() return 0 end+local T = { run = function() return 1 end }+`);+ expect(result).not.toBeNull();+ expect(result!.entities.map(e => e.name)).toEqual(+ expect.arrayContaining(['handler', 'cb', 'run']),+ );+ });++ it('emits CALLS for bare, dotted and method calls', () => {+ const result = parseFile('/repo/mod.lua', `+function run(data)+ local v = helper(data)+ local j = json.encode(v)+ return obj:fetch(j)+end+`);+ expect(result).not.toBeNull();+ const calls = result!.relationships+ .filter(r => r.predicate === 'CALLS')+ .map(r => r.dstName);+ expect(calls).toEqual(expect.arrayContaining(['helper', 'encode', 'fetch']));+ });++ it('emits IMPORTS for require with and without parentheses', () => {+ const result = parseFile('/repo/mod.lua', `+local json = require("cjson")+local http = require "socket.http"+`);+ expect(result).not.toBeNull();+ const imports = result!.relationships.filter(r => r.predicate === 'IMPORTS');+ const raws = imports.map(r => r.importRaw);+ expect(raws).toEqual(expect.arrayContaining(['cjson', 'socket.http']));+ });+});diff --git a/core-ingestion/src/index.ts b/core-ingestion/src/index.tsindex 7cab2ca6..1b31b5bd 100644--- a/core-ingestion/src/index.ts+++ b/core-ingestion/src/index.ts@@ -47,6 +47,15 @@ catch (e: any) {process.stderr.write('[tree-sitter-sas load failed] ' + e + '\n');}}+// tree-sitter-lua ships ESM bindings with top-level await (like SAS) — load via import().+let Lua: any = null;+// @ts-ignore+try { Lua = (await import('@tree-sitter-grammars/tree-sitter-lua')).default; }+catch (e: any) {+ if (e?.code !== 'ERR_MODULE_NOT_FOUND' && e?.code !== 'MODULE_NOT_FOUND') {+ process.stderr.write('[tree-sitter-lua load failed] ' + e + '\n');+ }+}import { SupportedLanguages, languageFromPath } from './languages.js';import { LANGUAGE_QUERIES } from './queries.js';@@ -167,6 +176,7 @@ const GRAMMAR_MAP: Partial<Record<SupportedLanguages, any>> = {...(SAS ? { [SupportedLanguages.SAS]: SAS } : {}),...(Elixir ? { [SupportedLanguages.Elixir]: Elixir } : {}),...(Make ? { [SupportedLanguages.Makefile]: Make } : {}),+ ...(Lua ? { [SupportedLanguages.Lua]: Lua } : {}),};// Capture key prefix → NodeKind stringdiff --git a/core-ingestion/src/languages.ts b/core-ingestion/src/languages.tsindex c7819547..e43f7f89 100644--- a/core-ingestion/src/languages.ts+++ b/core-ingestion/src/languages.ts@@ -23,6 +23,7 @@ export enum SupportedLanguages {SAS = 'sas',Elixir = 'elixir',Makefile = 'makefile',+ Lua = 'lua',}const EXT_MAP: Record<string, SupportedLanguages> = {@@ -62,6 +63,7 @@ const EXT_MAP: Record<string, SupportedLanguages> = {'.sas': SupportedLanguages.SAS,'.ex': SupportedLanguages.Elixir,'.exs': SupportedLanguages.Elixir,+ '.lua': SupportedLanguages.Lua,'.mk': SupportedLanguages.Makefile,'.makefile': SupportedLanguages.Makefile,};diff --git a/core-ingestion/src/queries.ts b/core-ingestion/src/queries.tsindex f292433c..7a7e5f79 100644--- a/core-ingestion/src/queries.ts+++ b/core-ingestion/src/queries.ts@@ -1605,6 +1605,43 @@ export const R_QUERIES = `(#eq? @_src "source")) @import`;+// Lua — mirrors the grammar's bundled tags.scm, mapped to Ix capture names.+// Covers: function declarations (plain / local / table-dot / method-colon),+// function expressions assigned to a name or table field, calls (bare / dotted /+// method), and require("mod") / require "mod" imports.+export const LUA_QUERIES = `+(function_declaration+ name: [+ (identifier) @name+ (dot_index_expression field: (identifier) @name)+ ]) @definition.function++(function_declaration+ name: (method_index_expression method: (identifier) @name)) @definition.method++(assignment_statement+ (variable_list . name: [+ (identifier) @name+ (dot_index_expression field: (identifier) @name)+ ])+ (expression_list . value: (function_definition))) @definition.function++(table_constructor+ (field name: (identifier) @name value: (function_definition))) @definition.function++(function_call+ name: [+ (identifier) @call.name+ (dot_index_expression field: (identifier) @call.name)+ (method_index_expression method: (identifier) @call.name)+ ]) @call++(function_call+ name: (identifier) @_req+ arguments: (arguments (string) @import.source)+ (#eq? @_req "require")) @import+`;+export const LANGUAGE_QUERIES: Record<SupportedLanguages, string> = {[SupportedLanguages.TypeScript]: TYPESCRIPT_QUERIES,[SupportedLanguages.JavaScript]: JAVASCRIPT_QUERIES,@@ -1630,5 +1667,6 @@ export const LANGUAGE_QUERIES: Record<SupportedLanguages, string> = {[SupportedLanguages.SAS]: SAS_QUERIES,[SupportedLanguages.Elixir]: ELIXIR_QUERIES,[SupportedLanguages.Makefile]: MAKEFILE_QUERIES,+ [SupportedLanguages.Lua]: LUA_QUERIES,};