Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/extraction/grammars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const WASM_GRAMMAR_FILES: Record<GrammarLanguage, string> = {
scala: 'tree-sitter-scala.wasm',
lua: 'tree-sitter-lua.wasm',
luau: 'tree-sitter-luau.wasm',
solidity: 'tree-sitter-solidity.wasm',
};

/**
Expand Down Expand Up @@ -92,6 +93,7 @@ export const EXTENSION_MAP: Record<string, Language> = {
'.sc': 'scala',
'.lua': 'lua',
'.luau': 'luau',
'.sol': 'solidity',
};

/**
Expand Down Expand Up @@ -155,7 +157,7 @@ export async function loadGrammarsForLanguages(languages: Language[]): Promise<v
// ABI-13 build that corrupts the shared WASM heap under web-tree-sitter
// 0.25 (drops nested calls/imports on every file after the first); we
// vendor the upstream ABI-15 wasm instead.
const wasmPath = (lang === 'pascal' || lang === 'scala' || lang === 'lua' || lang === 'luau')
const wasmPath = (lang === 'pascal' || lang === 'scala' || lang === 'lua' || lang === 'luau' || lang === 'solidity')
? path.join(__dirname, 'wasm', wasmFile)
: require.resolve(`tree-sitter-wasms/out/${wasmFile}`);
const language = await WasmLanguage.load(wasmPath);
Expand Down Expand Up @@ -325,6 +327,7 @@ export function getLanguageDisplayName(language: Language): string {
scala: 'Scala',
lua: 'Lua',
luau: 'Luau',
solidity: 'Solidity',
yaml: 'YAML',
twig: 'Twig',
unknown: 'Unknown',
Expand Down
2 changes: 2 additions & 0 deletions src/extraction/languages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { pascalExtractor } from './pascal';
import { scalaExtractor } from './scala';
import { luaExtractor } from './lua';
import { luauExtractor } from './luau';
import { solidityExtractor } from './solidity';

export const EXTRACTORS: Partial<Record<Language, LanguageExtractor>> = {
typescript: typescriptExtractor,
Expand All @@ -47,4 +48,5 @@ export const EXTRACTORS: Partial<Record<Language, LanguageExtractor>> = {
scala: scalaExtractor,
lua: luaExtractor,
luau: luauExtractor,
solidity: solidityExtractor,
};
136 changes: 136 additions & 0 deletions src/extraction/languages/solidity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import type { Node as SyntaxNode } from 'web-tree-sitter';
import { getNodeText, getChildByField } from '../tree-sitter-helpers';
import type { LanguageExtractor, VariableInfo } from '../tree-sitter-types';

/** Collect direct `parameter` children as a formatted string. */
function paramList(node: SyntaxNode, source: string): string {
const parts: string[] = [];
for (let i = 0; i < node.childCount; i++) {
const child = node.child(i);
if (child?.type === 'parameter') parts.push(getNodeText(child, source));
}
return '(' + parts.join(', ') + ')';
}

/** Find the text of a direct `visibility` child node, if present. */
function visibilityText(node: SyntaxNode): string | undefined {
for (let i = 0; i < node.childCount; i++) {
const child = node.child(i);
if (child?.type === 'visibility') return child.text;
}
return undefined;
}

export const solidityExtractor: LanguageExtractor = {
// contract_declaration / library_declaration are "classes" in the graph
functionTypes: ['function_definition', 'event_definition'],
classTypes: ['contract_declaration', 'library_declaration'],
methodTypes: [
'function_definition',
'modifier_definition',
'event_definition',
// constructor and fallback/receive are handled via visitNode (no name field)
],
interfaceTypes: ['interface_declaration'],
structTypes: ['struct_declaration'],
enumTypes: ['enum_declaration'],
enumMemberTypes: ['enum_value'],
typeAliasTypes: ['type_alias', 'user_defined_type_definition'],
importTypes: ['import_directive'],
callTypes: ['call_expression'],
variableTypes: ['state_variable_declaration', 'constant_variable_declaration'],

nameField: 'name',
bodyField: 'body',
paramsField: 'parameters', // not a real field in Solidity; overridden by getSignature
returnField: 'return_type',

getSignature: (node, source) => {
if (node.type === 'function_definition' || node.type === 'modifier_definition') {
let sig = paramList(node, source);
const vis = visibilityText(node);
if (vis) sig += ' ' + vis;
const returnType = getChildByField(node, 'return_type');
if (returnType) sig += ' returns ' + getNodeText(returnType, source);
return sig;
}
if (node.type === 'event_definition') {
return paramList(node, source);
}
return undefined;
},

getVisibility: (node) => {
const vis = visibilityText(node);
if (!vis) return undefined;
if (vis === 'public') return 'public';
if (vis === 'private') return 'private';
if (vis === 'internal') return 'internal';
if (vis === 'external') return 'public'; // external is callable from outside
return undefined;
},

extractImport: (node, source) => {
const sourceNode = getChildByField(node, 'source');
if (!sourceNode) return null;
const raw = getNodeText(sourceNode, source);
const moduleName = raw.replace(/^['"]|['"]$/g, '');
return {
moduleName,
signature: getNodeText(node, source),
};
},

extractVariables: (node, source) => {
const nameNode = getChildByField(node, 'name');
if (!nameNode) return [];
const name = getNodeText(nameNode, source);
const isConstant = node.type === 'constant_variable_declaration';
let signature: string | undefined;
const typeNode = getChildByField(node, 'type');
if (typeNode) signature = getNodeText(typeNode, source) + ' ' + name;
return [{ name, kind: isConstant ? 'constant' : 'variable', signature } satisfies VariableInfo];
},

isConst: (node) => node.type === 'constant_variable_declaration',

// Handle constructor and fallback/receive — neither has a `name` field in the AST.
visitNode: (node, ctx) => {
if (node.type === 'constructor_definition') {
const sig = paramList(node, ctx.source);
const created = ctx.createNode('function', 'constructor', node, { signature: sig });
if (created) {
const body = getChildByField(node, 'body');
if (body) {
ctx.pushScope(created.id);
ctx.visitFunctionBody(body, created.id);
ctx.popScope();
}
}
return true;
}

if (node.type === 'fallback_receive_definition') {
// Determine if it's a fallback or receive from the first keyword child
let fnName = 'fallback';
for (let i = 0; i < node.childCount; i++) {
const child = node.child(i);
if (child?.text === 'receive') { fnName = 'receive'; break; }
if (child?.text === 'fallback') { fnName = 'fallback'; break; }
}
const sig = paramList(node, ctx.source);
const created = ctx.createNode('function', fnName, node, { signature: sig });
if (created) {
const body = getChildByField(node, 'body');
if (body) {
ctx.pushScope(created.id);
ctx.visitFunctionBody(body, created.id);
ctx.popScope();
}
}
return true;
}

return false; // let default dispatch handle everything else
},
};
Binary file added src/extraction/wasm/tree-sitter-solidity.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export const LANGUAGES = [
'scala',
'lua',
'luau',
'solidity',
'yaml',
'twig',
'unknown',
Expand Down