@eliseev_s/tolk-tlb-transpiler
Version:
Transpile Tolk structs to TLB definitions and generate TypeScript wrappers for TON blockchain smart contracts
115 lines • 4.27 kB
JavaScript
;
// ============================================================================
// Get Methods Parser
// ============================================================================
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetMethodsParser = void 0;
class GetMethodsParser {
/**
* Parse get methods from Tolk AST
*/
static parseGetMethods(rootNode, typeResolver) {
const getMethods = [];
// Find all get method declarations
const getMethodNodes = this.findGetMethodNodes(rootNode);
for (const methodNode of getMethodNodes) {
const method = this.parseGetMethod(methodNode, typeResolver);
if (method) {
getMethods.push(method);
}
}
return getMethods;
}
/**
* Find all get method declaration nodes recursively
*/
static findGetMethodNodes(node) {
const getMethodNodes = [];
// Check if current node is a get method declaration
if (node.type === 'get_method_declaration') {
getMethodNodes.push(node);
}
// Recursively search in children
for (const child of node.namedChildren) {
if (child) {
const childGetMethods = this.findGetMethodNodes(child);
getMethodNodes.push(...childGetMethods);
}
}
return getMethodNodes;
}
/**
* Parse a single get method node
*/
static parseGetMethod(methodNode, typeResolver) {
const nameNode = methodNode.childForFieldName('name');
if (!nameNode) {
return null;
}
const name = nameNode.text;
const parameters = this.parseParameters(methodNode.childForFieldName('parameters'), typeResolver);
const returnType = this.parseReturnType(methodNode.childForFieldName('return_type'), typeResolver);
const annotations = this.parseAnnotations(methodNode.childForFieldName('annotations'));
return {
name,
parameters,
returnType,
annotations,
};
}
/**
* Parse method parameters
*/
static parseParameters(parametersNode, typeResolver) {
if (!parametersNode)
return [];
const parameters = [];
for (const child of parametersNode.namedChildren) {
if (child?.type === 'parameter_declaration') {
const nameNode = child.childForFieldName('name');
const typeNode = child.childForFieldName('type');
const defaultNode = child.childForFieldName('default_value');
if (nameNode && typeNode) {
// Check for mutate keyword
const isMutable = child.namedChildren.some((child) => child?.text === 'mutate');
parameters.push({
name: nameNode.text,
type: typeResolver(typeNode),
isMutable,
defaultValue: defaultNode?.text,
});
}
}
}
return parameters;
}
/**
* Parse return type
*/
static parseReturnType(returnTypeNode, typeResolver) {
if (!returnTypeNode) {
return { kind: 'primitive', name: 'void' };
}
return typeResolver(returnTypeNode);
}
/**
* Parse method annotations
*/
static parseAnnotations(annotationsNode) {
if (!annotationsNode)
return [];
return annotationsNode.namedChildren
.filter((child) => Boolean(child) && child.type === 'annotation')
.map((annotation) => {
const nameNode = annotation.childForFieldName('name');
const name = nameNode ? nameNode.text : '';
// Handle annotation arguments (e.g., @method_id(123))
const args = annotation.namedChildren
.filter((child) => child?.type === 'number_literal' || child?.type === 'string_literal')
.map((arg) => arg.text);
return { name, parameters: args };
});
}
}
exports.GetMethodsParser = GetMethodsParser;
//# sourceMappingURL=get-methods-parser.js.map