UNPKG

@convo-lang/convo-lang

Version:
76 lines 2.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseConvoType = exports.requireParseConvoCached = exports.parseConvoCached = exports.parseConvoCachedKeyed = void 0; const common_1 = require("@iyio/common"); const ConvoExecutionContext_1 = require("./ConvoExecutionContext"); const convo_parser_1 = require("./convo-parser"); const parsingCache = {}; /** * Parses Convo-Lang code with caching, returning both the parsing result and cache key. * Uses a hash-based cache to avoid re-parsing identical code strings. * * @param code - The Convo-Lang source code to parse * @returns Object containing the parsing result and the cache key used */ const parseConvoCachedKeyed = (code) => { const key = (0, common_1.strHashBase64)(code); const cached = parsingCache[key]; if (cached) { return { result: cached, key }; } const r = (0, convo_parser_1.parseConvoCode)(code, { logErrors: true }); parsingCache[key] = r; return { result: r, key }; }; exports.parseConvoCachedKeyed = parseConvoCachedKeyed; /** * Parses Convo-Lang code with caching, returning only the parsing result. * This is a convenience wrapper around parseConvoCachedKeyed for when only the result is needed. * * @param code - The Convo-Lang source code to parse * @returns The parsing result containing parsed ConvoMessage array or errors */ const parseConvoCached = (code) => { return (0, exports.parseConvoCachedKeyed)(code).result; }; exports.parseConvoCached = parseConvoCached; /** * Parses Convo-Lang code with caching and throws an error if the code fails to parse */ const requireParseConvoCached = (code) => { const r = (0, exports.parseConvoCachedKeyed)(code).result; if (!r?.result) { console.error('Failed to parse required cached convo code code:', code); throw new Error(); } return r.result; }; exports.requireParseConvoCached = requireParseConvoCached; const typeCache = {}; /** * Parses and caches a specific type definition from Convo-Lang code. * Executes the parsed code to extract a named type definition and caches the result. * * @param typeName - The name of the type to extract from the parsed code * @param convo - The Convo-Lang source code containing the type definition * @returns The parsed type definition, or undefined if not found */ const parseConvoType = (typeName, convo) => { const r = (0, exports.parseConvoCachedKeyed)(convo); const fn = r.result.result?.[0]?.fn; if (!fn) { return undefined; } const key = r.key + ':' + typeName; const cached = typeCache[key]; if (cached !== undefined) { return cached ?? undefined; } const exe = new ConvoExecutionContext_1.ConvoExecutionContext(); exe.executeFunction(fn); const type = exe.getVar(typeName); typeCache[key] = type ?? null; return type; }; exports.parseConvoType = parseConvoType; //# sourceMappingURL=convo-cached-parsing.js.map