solara-testing-beta
Version:
A modern, customizable, and lightweight Discord framework.
87 lines • 4.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FunctionParser = void 0;
class FunctionParser {
client;
functionRegex = /\$([a-zA-Z0-9_]+)(?:\[((?:[^\[\]]+|\[(?:[^\[\]]+|\[(?:[^\[\]]+)*\])*\])*)\])?/g;
constructor(client) {
this.client = client;
}
async parse(code, ctx) {
let currentCode = code;
let previousCode = '';
let iterations = 0;
const maxIterations = 100;
while (currentCode !== previousCode && iterations < maxIterations) {
previousCode = currentCode;
iterations++;
this.functionRegex.lastIndex = 0;
const matches = Array.from(currentCode.matchAll(this.functionRegex));
if (matches.length === 0)
break;
for (let i = matches.length - 1; i >= 0; i--) {
const match = matches[i];
const fullMatch = match[0];
const functionName = `$${match[1]}`;
const rawArgsContent = match[2];
if (rawArgsContent && /\$[a-zA-Z0-9_]+/.test(rawArgsContent)) { }
const funcData = this.client.functionManager.getFunction(functionName);
if (funcData) {
try {
let args = [];
if (rawArgsContent !== undefined) {
args = await this.parseArgs(rawArgsContent, ctx);
}
const funcCtx = { ...ctx, args: args };
const funcResult = await funcData.execute(funcCtx);
const resultString = String(funcResult ?? '');
const matchIndex = match.index ?? -1;
if (matchIndex !== -1) {
currentCode = currentCode.substring(0, matchIndex) + resultString + currentCode.substring(matchIndex + fullMatch.length);
this.functionRegex.lastIndex = 0;
break;
}
else {
currentCode = currentCode.replace(fullMatch, resultString);
}
}
catch (error) {
console.error(`❌ Error executing function ${functionName}:\n`, error);
const errorMsg = `\`\`\`diff\n- Error in ${functionName}: ${error.message || error}\n\`\`\``;
const matchIndex = match.index ?? -1;
if (matchIndex !== -1) {
currentCode = currentCode.substring(0, matchIndex) + errorMsg + currentCode.substring(matchIndex + fullMatch.length);
}
else {
currentCode = currentCode.replace(fullMatch, errorMsg);
}
this.functionRegex.lastIndex = 0;
break;
}
}
}
}
if (iterations >= maxIterations) {
console.warn(`⚠️ Function parsing reached maximum iterations (${maxIterations}) for code: ${code}. Returning potentially partially parsed result.`);
}
return currentCode.trim();
}
async parseArgs(rawArgsString, ctx) {
if (rawArgsString === null || rawArgsString === undefined)
return [];
const args = [];
const argSegments = rawArgsString.split(';');
for (const segment of argSegments) {
const trimmedSegment = segment.trim();
const parsedSegment = await this.parse(trimmedSegment, ctx);
args.push(parsedSegment);
}
return args;
}
static async formatError(commandName, error) {
const errorMessage = error instanceof Error ? error.message : String(error);
return `❌ **Error executing command \`${commandName}\`**\n\`\`\`xl\n${errorMessage}\n\`\`\``;
}
}
exports.FunctionParser = FunctionParser;
//# sourceMappingURL=FunctionParser.js.map