UNPKG

web-ai-toolkit

Version:

AI powered features on the web made easy

1,407 lines (1,406 loc) 1.51 MB
var Um = { /***/ "./node_modules/onnxruntime-web/dist/ort-wasm-simd-threaded.jsep.wasm": ( /*!****************************************************************************!*\ !*** ./node_modules/onnxruntime-web/dist/ort-wasm-simd-threaded.jsep.wasm ***! \****************************************************************************/ /***/ (Le, F, s) => { Le.exports = s.p + "ort-wasm-simd-threaded.jsep.wasm"; } ), /***/ "?2ce3": ( /*!**********************************!*\ !*** onnxruntime-node (ignored) ***! \**********************************/ /***/ () => { } ), /***/ "?7a2c": ( /*!********************!*\ !*** fs (ignored) ***! \********************/ /***/ () => { } ), /***/ "?a42a": ( /*!**********************!*\ !*** path (ignored) ***! \**********************/ /***/ () => { } ), /***/ "?2b25": ( /*!***********************!*\ !*** sharp (ignored) ***! \***********************/ /***/ () => { } ), /***/ "?569f": ( /*!********************!*\ !*** fs (ignored) ***! \********************/ /***/ () => { } ), /***/ "?3f59": ( /*!**********************!*\ !*** path (ignored) ***! \**********************/ /***/ () => { } ), /***/ "?154a": ( /*!*********************!*\ !*** url (ignored) ***! \*********************/ /***/ () => { } ), /***/ "./node_modules/@huggingface/jinja/dist/index.js": ( /*!*******************************************************!*\ !*** ./node_modules/@huggingface/jinja/dist/index.js ***! \*******************************************************/ /***/ (Le, F, s) => { s.r(F), s.d(F, { /* harmony export */ Environment: () => ( /* binding */ Qe ), /* harmony export */ Interpreter: () => ( /* binding */ ot ), /* harmony export */ Template: () => ( /* binding */ dt ), /* harmony export */ parse: () => ( /* binding */ J ), /* harmony export */ tokenize: () => ( /* binding */ T ) /* harmony export */ }); var w = Object.freeze({ Text: "Text", // The text between Jinja statements or expressions NumericLiteral: "NumericLiteral", // e.g., 123 BooleanLiteral: "BooleanLiteral", // true or false NullLiteral: "NullLiteral", // none StringLiteral: "StringLiteral", // 'string' Identifier: "Identifier", // Variables, functions, etc. Equals: "Equals", // = OpenParen: "OpenParen", // ( CloseParen: "CloseParen", // ) OpenStatement: "OpenStatement", // {% CloseStatement: "CloseStatement", // %} OpenExpression: "OpenExpression", // {{ CloseExpression: "CloseExpression", // }} OpenSquareBracket: "OpenSquareBracket", // [ CloseSquareBracket: "CloseSquareBracket", // ] OpenCurlyBracket: "OpenCurlyBracket", // { CloseCurlyBracket: "CloseCurlyBracket", // } Comma: "Comma", // , Dot: "Dot", // . Colon: "Colon", // : Pipe: "Pipe", // | CallOperator: "CallOperator", // () AdditiveBinaryOperator: "AdditiveBinaryOperator", // + - MultiplicativeBinaryOperator: "MultiplicativeBinaryOperator", // * / % ComparisonBinaryOperator: "ComparisonBinaryOperator", // < > <= >= == != UnaryOperator: "UnaryOperator", // ! - + // Keywords Set: "Set", If: "If", For: "For", In: "In", Is: "Is", NotIn: "NotIn", Else: "Else", EndIf: "EndIf", ElseIf: "ElseIf", EndFor: "EndFor", And: "And", Or: "Or", Not: "UnaryOperator", Macro: "Macro", EndMacro: "EndMacro" }), D = Object.freeze({ set: w.Set, for: w.For, in: w.In, is: w.Is, if: w.If, else: w.Else, endif: w.EndIf, elif: w.ElseIf, endfor: w.EndFor, and: w.And, or: w.Or, not: w.Not, "not in": w.NotIn, macro: w.Macro, endmacro: w.EndMacro, // Literals true: w.BooleanLiteral, false: w.BooleanLiteral, none: w.NullLiteral, // NOTE: According to the Jinja docs: The special constants true, false, and none are indeed lowercase. // Because that caused confusion in the past, (True used to expand to an undefined variable that was considered false), // all three can now also be written in title case (True, False, and None). However, for consistency, (all Jinja identifiers are lowercase) // you should use the lowercase versions. True: w.BooleanLiteral, False: w.BooleanLiteral, None: w.NullLiteral }), W = class { /** * Constructs a new Token. * @param {string} value The raw value as seen inside the source code. * @param {TokenType} type The type of token. */ constructor(A, oe) { this.value = A, this.type = oe; } }; function te(A) { return /\w/.test(A); } function j(A) { return /[0-9]/.test(A); } var M = [ // Control sequences ["{%", w.OpenStatement], ["%}", w.CloseStatement], ["{{", w.OpenExpression], ["}}", w.CloseExpression], // Single character tokens ["(", w.OpenParen], [")", w.CloseParen], ["{", w.OpenCurlyBracket], ["}", w.CloseCurlyBracket], ["[", w.OpenSquareBracket], ["]", w.CloseSquareBracket], [",", w.Comma], [".", w.Dot], [":", w.Colon], ["|", w.Pipe], // Comparison operators ["<=", w.ComparisonBinaryOperator], [">=", w.ComparisonBinaryOperator], ["==", w.ComparisonBinaryOperator], ["!=", w.ComparisonBinaryOperator], ["<", w.ComparisonBinaryOperator], [">", w.ComparisonBinaryOperator], // Arithmetic operators ["+", w.AdditiveBinaryOperator], ["-", w.AdditiveBinaryOperator], ["*", w.MultiplicativeBinaryOperator], ["/", w.MultiplicativeBinaryOperator], ["%", w.MultiplicativeBinaryOperator], // Assignment operator ["=", w.Equals] ], P = /* @__PURE__ */ new Map([ ["n", ` `], // New line ["t", " "], // Horizontal tab ["r", "\r"], // Carriage return ["b", "\b"], // Backspace ["f", "\f"], // Form feed ["v", "\v"], // Vertical tab ["'", "'"], // Single quote ['"', '"'], // Double quote ["\\", "\\"] // Backslash ]); function v(A, oe = {}) { return A.endsWith(` `) && (A = A.slice(0, -1)), A = A.replace(/{#.*?#}/gs, "{##}"), oe.lstrip_blocks && (A = A.replace(/^[ \t]*({[#%])/gm, "$1")), oe.trim_blocks && (A = A.replace(/([#%]})\n/g, "$1")), A.replace(/{##}/g, "").replace(/-%}\s*/g, "%}").replace(/\s*{%-/g, "{%").replace(/-}}\s*/g, "}}").replace(/\s*{{-/g, "{{"); } function T(A, oe = {}) { const q = [], _e = v(A, oe); let Se = 0; const He = (et) => { let it = ""; for (; et(_e[Se]); ) { if (_e[Se] === "\\") { if (++Se, Se >= _e.length) throw new SyntaxError("Unexpected end of input"); const ct = _e[Se++], vt = P.get(ct); if (vt === void 0) throw new SyntaxError(`Unexpected escaped character: ${ct}`); it += vt; continue; } if (it += _e[Se++], Se >= _e.length) throw new SyntaxError("Unexpected end of input"); } return it; }; e: for (; Se < _e.length; ) { const et = q.at(-1)?.type; if (et === void 0 || et === w.CloseStatement || et === w.CloseExpression) { let ct = ""; for (; Se < _e.length && // Keep going until we hit the next Jinja statement or expression !(_e[Se] === "{" && (_e[Se + 1] === "%" || _e[Se + 1] === "{")); ) ct += _e[Se++]; if (ct.length > 0) { q.push(new W(ct, w.Text)); continue; } } He((ct) => /\s/.test(ct)); const it = _e[Se]; if (it === "-" || it === "+") { const ct = q.at(-1)?.type; if (ct === w.Text || ct === void 0) throw new SyntaxError(`Unexpected character: ${it}`); switch (ct) { case w.Identifier: case w.NumericLiteral: case w.BooleanLiteral: case w.NullLiteral: case w.StringLiteral: case w.CloseParen: case w.CloseSquareBracket: break; default: { ++Se; const vt = He(j); q.push( new W(`${it}${vt}`, vt.length > 0 ? w.NumericLiteral : w.UnaryOperator) ); continue; } } } for (const [ct, vt] of M) if (_e.slice(Se, Se + ct.length) === ct) { q.push(new W(ct, vt)), Se += ct.length; continue e; } if (it === "'" || it === '"') { ++Se; const ct = He((vt) => vt !== it); q.push(new W(ct, w.StringLiteral)), ++Se; continue; } if (j(it)) { const ct = He(j); q.push(new W(ct, w.NumericLiteral)); continue; } if (te(it)) { const ct = He(te), vt = Object.hasOwn(D, ct) ? D[ct] : w.Identifier; vt === w.In && q.at(-1)?.type === w.Not ? (q.pop(), q.push(new W("not in", w.NotIn))) : q.push(new W(ct, vt)); continue; } throw new SyntaxError(`Unexpected character: ${it}`); } return q; } var b = class { type = "Statement"; }, z = class extends b { constructor(A) { super(), this.body = A; } type = "Program"; }, K = class extends b { constructor(A, oe, q) { super(), this.test = A, this.body = oe, this.alternate = q; } type = "If"; }, se = class extends b { constructor(A, oe, q, _e) { super(), this.loopvar = A, this.iterable = oe, this.body = q, this.defaultBlock = _e; } type = "For"; }, ae = class extends b { constructor(A, oe) { super(), this.assignee = A, this.value = oe; } type = "Set"; }, R = class extends b { constructor(A, oe, q) { super(), this.name = A, this.args = oe, this.body = q; } type = "Macro"; }, G = class extends b { type = "Expression"; }, X = class extends G { constructor(A, oe, q) { super(), this.object = A, this.property = oe, this.computed = q; } type = "MemberExpression"; }, O = class extends G { constructor(A, oe) { super(), this.callee = A, this.args = oe; } type = "CallExpression"; }, $ = class extends G { /** * @param {string} value The name of the identifier */ constructor(A) { super(), this.value = A; } type = "Identifier"; }, g = class extends G { constructor(A) { super(), this.value = A; } type = "Literal"; }, C = class extends g { type = "NumericLiteral"; }, x = class extends g { type = "StringLiteral"; }, Z = class extends g { type = "BooleanLiteral"; }, Q = class extends g { type = "NullLiteral"; }, le = class extends g { type = "ArrayLiteral"; }, ce = class extends g { type = "TupleLiteral"; }, fe = class extends g { type = "ObjectLiteral"; }, Pe = class extends G { constructor(A, oe, q) { super(), this.operator = A, this.left = oe, this.right = q; } type = "BinaryExpression"; }, ve = class extends G { constructor(A, oe) { super(), this.operand = A, this.filter = oe; } type = "FilterExpression"; }, Ie = class extends G { constructor(A, oe) { super(), this.iterable = A, this.test = oe; } type = "SelectExpression"; }, Ge = class extends G { constructor(A, oe, q) { super(), this.operand = A, this.negate = oe, this.test = q; } type = "TestExpression"; }, Re = class extends G { constructor(A, oe) { super(), this.operator = A, this.argument = oe; } type = "UnaryExpression"; }, ut = class extends G { constructor(A = void 0, oe = void 0, q = void 0) { super(), this.start = A, this.stop = oe, this.step = q; } type = "SliceExpression"; }, ue = class extends G { constructor(A, oe) { super(), this.key = A, this.value = oe; } type = "KeywordArgumentExpression"; }; function J(A) { const oe = new z([]); let q = 0; function _e(nt, pt) { const St = A[q++]; if (!St || St.type !== nt) throw new Error(`Parser Error: ${pt}. ${St.type} !== ${nt}.`); return St; } function Se() { switch (A[q].type) { case w.Text: return it(); case w.OpenStatement: return ct(); case w.OpenExpression: return vt(); default: throw new SyntaxError(`Unexpected token type: ${A[q].type}`); } } function He(...nt) { return q + nt.length <= A.length && nt.some((pt, St) => pt !== A[q + St].type); } function et(...nt) { return q + nt.length <= A.length && nt.every((pt, St) => pt === A[q + St].type); } function it() { return new x(_e(w.Text, "Expected text token").value); } function ct() { _e(w.OpenStatement, "Expected opening statement token"); let nt; switch (A[q].type) { case w.Set: ++q, nt = Yt(), _e(w.CloseStatement, "Expected closing statement token"); break; case w.If: ++q, nt = tr(), _e(w.OpenStatement, "Expected {% token"), _e(w.EndIf, "Expected endif token"), _e(w.CloseStatement, "Expected %} token"); break; case w.Macro: ++q, nt = xr(), _e(w.OpenStatement, "Expected {% token"), _e(w.EndMacro, "Expected endmacro token"), _e(w.CloseStatement, "Expected %} token"); break; case w.For: ++q, nt = Pr(), _e(w.OpenStatement, "Expected {% token"), _e(w.EndFor, "Expected endfor token"), _e(w.CloseStatement, "Expected %} token"); break; default: throw new SyntaxError(`Unknown statement type: ${A[q].type}`); } return nt; } function vt() { _e(w.OpenExpression, "Expected opening expression token"); const nt = Sr(); return _e(w.CloseExpression, "Expected closing expression token"), nt; } function Yt() { const nt = Sr(); if (et(w.Equals)) { ++q; const pt = Yt(); return new ae(nt, pt); } return nt; } function tr() { const nt = Sr(); _e(w.CloseStatement, "Expected closing statement token"); const pt = [], St = []; for (; !(A[q]?.type === w.OpenStatement && (A[q + 1]?.type === w.ElseIf || A[q + 1]?.type === w.Else || A[q + 1]?.type === w.EndIf)); ) pt.push(Se()); if (A[q]?.type === w.OpenStatement && A[q + 1]?.type !== w.EndIf) if (++q, et(w.ElseIf)) _e(w.ElseIf, "Expected elseif token"), St.push(tr()); else for (_e(w.Else, "Expected else token"), _e(w.CloseStatement, "Expected closing statement token"); !(A[q]?.type === w.OpenStatement && A[q + 1]?.type === w.EndIf); ) St.push(Se()); return new K(nt, pt, St); } function xr() { const nt = ns(); if (nt.type !== "Identifier") throw new SyntaxError("Expected identifier following macro statement"); const pt = Os(); _e(w.CloseStatement, "Expected closing statement token"); const St = []; for (; He(w.OpenStatement, w.EndMacro); ) St.push(Se()); return new R(nt, pt, St); } function mr(nt = !1) { const pt = nt ? ns : Sr, St = [pt()], dr = et(w.Comma); for (; dr && (++q, St.push(pt()), !!et(w.Comma)); ) ; return dr ? new ce(St) : St[0]; } function Pr() { const nt = mr(!0); if (!(nt instanceof $ || nt instanceof ce)) throw new SyntaxError(`Expected identifier/tuple for the loop variable, got ${nt.type} instead`); _e(w.In, "Expected `in` keyword following loop variable"); const pt = Sr(); _e(w.CloseStatement, "Expected closing statement token"); const St = []; for (; He(w.OpenStatement, w.EndFor) && He(w.OpenStatement, w.Else); ) St.push(Se()); const dr = []; if (et(w.OpenStatement, w.Else)) for (++q, ++q, _e(w.CloseStatement, "Expected closing statement token"); He(w.OpenStatement, w.EndFor); ) dr.push(Se()); return new se(nt, pt, St, dr); } function Sr() { return Hr(); } function Hr() { const nt = rs(); if (et(w.If)) { ++q; const pt = rs(); if (et(w.Else)) { ++q; const St = rs(); return new K(pt, [nt], [St]); } else return new Ie(nt, pt); } return nt; } function rs() { let nt = Us(); for (; et(w.Or); ) { const pt = A[q]; ++q; const St = Us(); nt = new Pe(pt, nt, St); } return nt; } function Us() { let nt = Fs(); for (; et(w.And); ) { const pt = A[q]; ++q; const St = Fs(); nt = new Pe(pt, nt, St); } return nt; } function Fs() { let nt; for (; et(w.Not); ) { const pt = A[q]; ++q; const St = Fs(); nt = new Re(pt, St); } return nt ?? ws(); } function ws() { let nt = Bt(); for (; et(w.ComparisonBinaryOperator) || et(w.In) || et(w.NotIn); ) { const pt = A[q]; ++q; const St = Bt(); nt = new Pe(pt, nt, St); } return nt; } function Bt() { let nt = cs(); for (; et(w.AdditiveBinaryOperator); ) { const pt = A[q]; ++q; const St = cs(); nt = new Pe(pt, nt, St); } return nt; } function Vs() { const nt = Ps(); return et(w.OpenParen) ? Ts(nt) : nt; } function Ts(nt) { let pt = new O(nt, Os()); return et(w.OpenParen) && (pt = Ts(pt)), pt; } function Os() { _e(w.OpenParen, "Expected opening parenthesis for arguments list"); const nt = xs(); return _e(w.CloseParen, "Expected closing parenthesis for arguments list"), nt; } function xs() { const nt = []; for (; !et(w.CloseParen); ) { let pt = Sr(); if (et(w.Equals)) { if (++q, !(pt instanceof $)) throw new SyntaxError("Expected identifier for keyword argument"); const St = Sr(); pt = new ue(pt, St); } nt.push(pt), et(w.Comma) && ++q; } return nt; } function ss() { const nt = []; let pt = !1; for (; !et(w.CloseSquareBracket); ) et(w.Colon) ? (nt.push(void 0), ++q, pt = !0) : (nt.push(Sr()), et(w.Colon) && (++q, pt = !0)); if (nt.length === 0) throw new SyntaxError("Expected at least one argument for member/slice expression"); if (pt) { if (nt.length > 3) throw new SyntaxError("Expected 0-3 arguments for slice expression"); return new ut(...nt); } return nt[0]; } function Ps() { let nt = ns(); for (; et(w.Dot) || et(w.OpenSquareBracket); ) { const pt = A[q]; ++q; let St; const dr = pt.type !== w.Dot; if (dr) St = ss(), _e(w.CloseSquareBracket, "Expected closing square bracket"); else if (St = ns(), St.type !== "Identifier") throw new SyntaxError("Expected identifier following dot operator"); nt = new X(nt, St, dr); } return nt; } function cs() { let nt = Es(); for (; et(w.MultiplicativeBinaryOperator); ) { const pt = A[q]; ++q; const St = Es(); nt = new Pe(pt, nt, St); } return nt; } function Es() { let nt = Gs(); for (; et(w.Is); ) { ++q; const pt = et(w.Not); pt && ++q; let St = ns(); if (St instanceof Z ? St = new $(St.value.toString()) : St instanceof Q && (St = new $("none")), !(St instanceof $)) throw new SyntaxError("Expected identifier for the test"); nt = new Ge(nt, pt, St); } return nt; } function Gs() { let nt = Vs(); for (; et(w.Pipe); ) { ++q; let pt = ns(); if (!(pt instanceof $)) throw new SyntaxError("Expected identifier for the filter"); et(w.OpenParen) && (pt = Ts(pt)), nt = new ve(nt, pt); } return nt; } function ns() { const nt = A[q]; switch (nt.type) { case w.NumericLiteral: return ++q, new C(Number(nt.value)); case w.StringLiteral: return ++q, new x(nt.value); case w.BooleanLiteral: return ++q, new Z(nt.value.toLowerCase() === "true"); case w.NullLiteral: return ++q, new Q(null); case w.Identifier: return ++q, new $(nt.value); case w.OpenParen: { ++q; const pt = mr(); if (A[q].type !== w.CloseParen) throw new SyntaxError(`Expected closing parenthesis, got ${A[q].type} instead`); return ++q, pt; } case w.OpenSquareBracket: { ++q; const pt = []; for (; !et(w.CloseSquareBracket); ) pt.push(Sr()), et(w.Comma) && ++q; return ++q, new le(pt); } case w.OpenCurlyBracket: { ++q; const pt = /* @__PURE__ */ new Map(); for (; !et(w.CloseCurlyBracket); ) { const St = Sr(); _e(w.Colon, "Expected colon between key and value in object literal"); const dr = Sr(); pt.set(St, dr), et(w.Comma) && ++q; } return ++q, new fe(pt); } default: throw new SyntaxError(`Unexpected token: ${nt.type}`); } } for (; q < A.length; ) oe.body.push(Se()); return oe; } function he(A, oe, q = 1) { oe === void 0 && (oe = A, A = 0); const _e = []; for (let Se = A; Se < oe; Se += q) _e.push(Se); return _e; } function be(A, oe, q, _e = 1) { const Se = Math.sign(_e); Se >= 0 ? (oe = (oe ??= 0) < 0 ? Math.max(A.length + oe, 0) : Math.min(oe, A.length), q = (q ??= A.length) < 0 ? Math.max(A.length + q, 0) : Math.min(q, A.length)) : (oe = (oe ??= A.length - 1) < 0 ? Math.max(A.length + oe, -1) : Math.min(oe, A.length - 1), q = (q ??= -1) < -1 ? Math.max(A.length + q, -1) : Math.min(q, A.length - 1)); const He = []; for (let et = oe; Se * et < Se * q; et += _e) He.push(A[et]); return He; } function Oe(A) { return A.replace(/\b\w/g, (oe) => oe.toUpperCase()); } var Ze = class { type = "RuntimeValue"; value; /** * A collection of built-in functions for this type. */ builtins = /* @__PURE__ */ new Map(); /** * Creates a new RuntimeValue. */ constructor(A = void 0) { this.value = A; } /** * Determines truthiness or falsiness of the runtime value. * This function should be overridden by subclasses if it has custom truthiness criteria. * @returns {BooleanValue} BooleanValue(true) if the value is truthy, BooleanValue(false) otherwise. */ __bool__() { return new Xe(!!this.value); } }, Ke = class extends Ze { type = "NumericValue"; }, ne = class extends Ze { type = "StringValue"; builtins = /* @__PURE__ */ new Map([ [ "upper", new Ne(() => new ne(this.value.toUpperCase())) ], [ "lower", new Ne(() => new ne(this.value.toLowerCase())) ], [ "strip", new Ne(() => new ne(this.value.trim())) ], [ "title", new Ne(() => new ne(Oe(this.value))) ], ["length", new Ke(this.value.length)], [ "rstrip", new Ne(() => new ne(this.value.trimEnd())) ], [ "lstrip", new Ne(() => new ne(this.value.trimStart())) ] ]); }, Xe = class extends Ze { type = "BooleanValue"; }, $e = class extends Ze { type = "ObjectValue"; /** * NOTE: necessary to override since all JavaScript arrays are considered truthy, * while only non-empty Python arrays are consider truthy. * * e.g., * - JavaScript: {} && 5 -> 5 * - Python: {} and 5 -> {} */ __bool__() { return new Xe(this.value.size > 0); } builtins = /* @__PURE__ */ new Map([ [ "get", new Ne(([A, oe]) => { if (!(A instanceof ne)) throw new Error(`Object key must be a string: got ${A.type}`); return this.value.get(A.value) ?? oe ?? new Ve(); }) ], [ "items", new Ne(() => new Me( Array.from(this.value.entries()).map(([A, oe]) => new Me([new ne(A), oe])) )) ] ]); }, ie = class extends $e { type = "KeywordArgumentsValue"; }, Me = class extends Ze { type = "ArrayValue"; builtins = /* @__PURE__ */ new Map([["length", new Ke(this.value.length)]]); /** * NOTE: necessary to override since all JavaScript arrays are considered truthy, * while only non-empty Python arrays are consider truthy. * * e.g., * - JavaScript: [] && 5 -> 5 * - Python: [] and 5 -> [] */ __bool__() { return new Xe(this.value.length > 0); } }, je = class extends Me { type = "TupleValue"; }, Ne = class extends Ze { type = "FunctionValue"; }, Ve = class extends Ze { type = "NullValue"; }, Be = class extends Ze { type = "UndefinedValue"; }, Qe = class { constructor(A) { this.parent = A; } /** * The variables declared in this environment. */ variables = /* @__PURE__ */ new Map([ [ "namespace", new Ne((A) => { if (A.length === 0) return new $e(/* @__PURE__ */ new Map()); if (A.length !== 1 || !(A[0] instanceof $e)) throw new Error("`namespace` expects either zero arguments or a single object argument"); return A[0]; }) ] ]); /** * The tests available in this environment. */ tests = /* @__PURE__ */ new Map([ ["boolean", (A) => A.type === "BooleanValue"], ["callable", (A) => A instanceof Ne], [ "odd", (A) => { if (A.type !== "NumericValue") throw new Error(`Cannot apply test "odd" to type: ${A.type}`); return A.value % 2 !== 0; } ], [ "even", (A) => { if (A.type !== "NumericValue") throw new Error(`Cannot apply test "even" to type: ${A.type}`); return A.value % 2 === 0; } ], ["false", (A) => A.type === "BooleanValue" && !A.value], ["true", (A) => A.type === "BooleanValue" && A.value], ["none", (A) => A.type === "NullValue"], ["string", (A) => A.type === "StringValue"], ["number", (A) => A.type === "NumericValue"], ["integer", (A) => A.type === "NumericValue" && Number.isInteger(A.value)], ["iterable", (A) => A.type === "ArrayValue" || A.type === "StringValue"], ["mapping", (A) => A.type === "ObjectValue"], [ "lower", (A) => { const oe = A.value; return A.type === "StringValue" && oe === oe.toLowerCase(); } ], [ "upper", (A) => { const oe = A.value; return A.type === "StringValue" && oe === oe.toUpperCase(); } ], ["none", (A) => A.type === "NullValue"], ["defined", (A) => A.type !== "UndefinedValue"], ["undefined", (A) => A.type === "UndefinedValue"], ["equalto", (A, oe) => A.value === oe.value], ["eq", (A, oe) => A.value === oe.value] ]); /** * Set the value of a variable in the current environment. */ set(A, oe) { return this.declareVariable(A, gt(oe)); } declareVariable(A, oe) { if (this.variables.has(A)) throw new SyntaxError(`Variable already declared: ${A}`); return this.variables.set(A, oe), oe; } // private assignVariable(name: string, value: AnyRuntimeValue): AnyRuntimeValue { // const env = this.resolve(name); // env.variables.set(name, value); // return value; // } /** * Set variable in the current scope. * See https://jinja.palletsprojects.com/en/3.0.x/templates/#assignments for more information. */ setVariable(A, oe) { return this.variables.set(A, oe), oe; } /** * Resolve the environment in which the variable is declared. * @param {string} name The name of the variable. * @returns {Environment} The environment in which the variable is declared. */ resolve(A) { if (this.variables.has(A)) return this; if (this.parent) return this.parent.resolve(A); throw new Error(`Unknown variable: ${A}`); } lookupVariable(A) { try { return this.resolve(A).variables.get(A) ?? new Be(); } catch { return new Be(); } } }, ot = class { global; constructor(A) { this.global = A ?? new Qe(); } /** * Run the program. */ run(A) { return this.evaluate(A, this.global); } /** * Evaluates expressions following the binary operation type. */ evaluateBinaryExpression(A, oe) { const q = this.evaluate(A.left, oe); switch (A.operator.value) { case "and": return q.__bool__().value ? this.evaluate(A.right, oe) : q; case "or": return q.__bool__().value ? q : this.evaluate(A.right, oe); } const _e = this.evaluate(A.right, oe); switch (A.operator.value) { case "==": return new Xe(q.value == _e.value); case "!=": return new Xe(q.value != _e.value); } if (q instanceof Be || _e instanceof Be) throw new Error("Cannot perform operation on undefined values"); if (q instanceof Ve || _e instanceof Ve) throw new Error("Cannot perform operation on null values"); if (q instanceof Ke && _e instanceof Ke) switch (A.operator.value) { case "+": return new Ke(q.value + _e.value); case "-": return new Ke(q.value - _e.value); case "*": return new Ke(q.value * _e.value); case "/": return new Ke(q.value / _e.value); case "%": return new Ke(q.value % _e.value); case "<": return new Xe(q.value < _e.value); case ">": return new Xe(q.value > _e.value); case ">=": return new Xe(q.value >= _e.value); case "<=": return new Xe(q.value <= _e.value); } else if (q instanceof Me && _e instanceof Me) switch (A.operator.value) { case "+": return new Me(q.value.concat(_e.value)); } else if (_e instanceof Me) { const Se = _e.value.find((He) => He.value === q.value) !== void 0; switch (A.operator.value) { case "in": return new Xe(Se); case "not in": return new Xe(!Se); } } if (q instanceof ne || _e instanceof ne) switch (A.operator.value) { case "+": return new ne(q.value.toString() + _e.value.toString()); } if (q instanceof ne && _e instanceof ne) switch (A.operator.value) { case "in": return new Xe(_e.value.includes(q.value)); case "not in": return new Xe(!_e.value.includes(q.value)); } if (q instanceof ne && _e instanceof $e) switch (A.operator.value) { case "in": return new Xe(_e.value.has(q.value)); case "not in": return new Xe(!_e.value.has(q.value)); } throw new SyntaxError(`Unknown operator "${A.operator.value}" between ${q.type} and ${_e.type}`); } evaluateArguments(A, oe) { const q = [], _e = /* @__PURE__ */ new Map(); for (const Se of A) if (Se.type === "KeywordArgumentExpression") { const He = Se; _e.set(He.key.value, this.evaluate(He.value, oe)); } else { if (_e.size > 0) throw new Error("Positional arguments must come before keyword arguments"); q.push(this.evaluate(Se, oe)); } return [q, _e]; } /** * Evaluates expressions following the filter operation type. */ evaluateFilterExpression(A, oe) { const q = this.evaluate(A.operand, oe); if (A.filter.type === "Identifier") { const _e = A.filter; if (_e.value === "tojson") return new ne(lt(q)); if (q instanceof Me) switch (_e.value) { case "list": return q; case "first": return q.value[0]; case "last": return q.value[q.value.length - 1]; case "length": return new Ke(q.value.length); case "reverse": return new Me(q.value.reverse()); case "sort": return new Me( q.value.sort((Se, He) => { if (Se.type !== He.type) throw new Error(`Cannot compare different types: ${Se.type} and ${He.type}`); switch (Se.type) { case "NumericValue": return Se.value - He.value; case "StringValue": return Se.value.localeCompare(He.value); default: throw new Error(`Cannot compare type: ${Se.type}`); } }) ); default: throw new Error(`Unknown ArrayValue filter: ${_e.value}`); } else if (q instanceof ne) switch (_e.value) { case "length": return new Ke(q.value.length); case "upper": return new ne(q.value.toUpperCase()); case "lower": return new ne(q.value.toLowerCase()); case "title": return new ne(Oe(q.value)); case "capitalize": return new ne(q.value.charAt(0).toUpperCase() + q.value.slice(1)); case "trim": return new ne(q.value.trim()); case "indent": return new ne( q.value.split(` `).map( (Se, He) => ( // By default, don't indent the first line or empty lines He === 0 || Se.length === 0 ? Se : " " + Se ) ).join(` `) ); case "string": return q; default: throw new Error(`Unknown StringValue filter: ${_e.value}`); } else if (q instanceof Ke) switch (_e.value) { case "abs": return new Ke(Math.abs(q.value)); default: throw new Error(`Unknown NumericValue filter: ${_e.value}`); } else if (q instanceof $e) switch (_e.value) { case "items": return new Me( Array.from(q.value.entries()).map(([Se, He]) => new Me([new ne(Se), He])) ); case "length": return new Ke(q.value.size); default: throw new Error(`Unknown ObjectValue filter: ${_e.value}`); } throw new Error(`Cannot apply filter "${_e.value}" to type: ${q.type}`); } else if (A.filter.type === "CallExpression") { const _e = A.filter; if (_e.callee.type !== "Identifier") throw new Error(`Unknown filter: ${_e.callee.type}`); const Se = _e.callee.value; if (Se === "tojson") { const [, He] = this.evaluateArguments(_e.args, oe), et = He.get("indent") ?? new Ve(); if (!(et instanceof Ke || et instanceof Ve)) throw new Error("If set, indent must be a number"); return new ne(lt(q, et.value)); } if (q instanceof Me) { switch (Se) { case "selectattr": case "rejectattr": { const He = Se === "selectattr"; if (q.value.some((tr) => !(tr instanceof $e))) throw new Error(`\`${Se}\` can only be applied to array of objects`); if (_e.args.some((tr) => tr.type !== "StringLiteral")) throw new Error(`arguments of \`${Se}\` must be strings`); const [et, it, ct] = _e.args.map((tr) => this.evaluate(tr, oe)); let vt; if (it) { const tr = oe.tests.get(it.value); if (!tr) throw new Error(`Unknown test: ${it.value}`); vt = tr; } else vt = (...tr) => tr[0].__bool__().value; const Yt = q.value.filter((tr) => { const xr = tr.value.get(et.value), mr = xr ? vt(xr, ct) : !1; return He ? mr : !mr; }); return new Me(Yt); } case "map": { const [, He] = this.evaluateArguments(_e.args, oe); if (He.has("attribute")) { const et = He.get("attribute"); if (!(et instanceof ne)) throw new Error("attribute must be a string"); const it = He.get("default"), ct = q.value.map((vt) => { if (!(vt instanceof $e)) throw new Error("items in map must be an object"); return vt.value.get(et.value) ?? it ?? new Be(); }); return new Me(ct); } else throw new Error("`map` expressions without `attribute` set are not currently supported."); } } throw new Error(`Unknown ArrayValue filter: ${Se}`); } else if (q instanceof ne) { switch (Se) { case "indent": { const [He, et] = this.evaluateArguments(_e.args, oe), it = He.at(0) ?? et.get("width") ?? new Ke(4); if (!(it instanceof Ke)) throw new Error("width must be a number"); const ct = He.at(1) ?? et.get("first") ?? new Xe(!1), vt = He.at(2) ?? et.get("blank") ?? new Xe(!1), Yt = q.value.split(` `), tr = " ".repeat(it.value), xr = Yt.map( (mr, Pr) => !ct.value && Pr === 0 || !vt.value && mr.length === 0 ? mr : tr + mr ); return new ne(xr.join(` `)); } } throw new Error(`Unknown StringValue filter: ${Se}`); } else throw new Error(`Cannot apply filter "${Se}" to type: ${q.type}`); } throw new Error(`Unknown filter: ${A.filter.type}`); } /** * Evaluates expressions following the test operation type. */ evaluateTestExpression(A, oe) { const q = this.evaluate(A.operand, oe), _e = oe.tests.get(A.test.value); if (!_e) throw new Error(`Unknown test: ${A.test.value}`); const Se = _e(q); return new Xe(A.negate ? !Se : Se); } /** * Evaluates expressions following the unary operation type. */ evaluateUnaryExpression(A, oe) { const q = this.evaluate(A.argument, oe); switch (A.operator.value) { case "not": return new Xe(!q.value); default: throw new SyntaxError(`Unknown operator: ${A.operator.value}`); } } evalProgram(A, oe) { return this.evaluateBlock(A.body, oe); } evaluateBlock(A, oe) { let q = ""; for (const _e of A) { const Se = this.evaluate(_e, oe); Se.type !== "NullValue" && Se.type !== "UndefinedValue" && (q += Se.value); } return new ne(q); } evaluateIdentifier(A, oe) { return oe.lookupVariable(A.value); } evaluateCallExpression(A, oe) { const [q, _e] = this.evaluateArguments(A.args, oe); _e.size > 0 && q.push(new ie(_e)); const Se = this.evaluate(A.callee, oe); if (Se.type !== "FunctionValue") throw new Error(`Cannot call something that is not a function: got ${Se.type}`); return Se.value(q, oe); } evaluateSliceExpression(A, oe, q) { if (!(A instanceof Me || A instanceof ne)) throw new Error("Slice object must be an array or string"); const _e = this.evaluate(oe.start, q), Se = this.evaluate(oe.stop, q), He = this.evaluate(oe.step, q); if (!(_e instanceof Ke || _e instanceof Be)) throw new Error("Slice start must be numeric or undefined"); if (!(Se instanceof Ke || Se instanceof Be)) throw new Error("Slice stop must be numeric or undefined"); if (!(He instanceof Ke || He instanceof Be)) throw new Error("Slice step must be numeric or undefined"); return A instanceof Me ? new Me(be(A.value, _e.value, Se.value, He.value)) : new ne(be(Array.from(A.value), _e.value, Se.value, He.value).join("")); } evaluateMemberExpression(A, oe) { const q = this.evaluate(A.object, oe); let _e; if (A.computed) { if (A.property.type === "SliceExpression") return this.evaluateSliceExpression(q, A.property, oe); _e = this.evaluate(A.property, oe); } else _e = new ne(A.property.value); let Se; if (q instanceof $e) { if (!(_e instanceof ne)) throw new Error(`Cannot access property with non-string: got ${_e.type}`); Se = q.value.get(_e.value) ?? q.builtins.get(_e.value); } else if (q instanceof Me || q instanceof ne) if (_e instanceof Ke) Se = q.value.at(_e.value), q instanceof ne && (Se = new ne(q.value.at(_e.value))); else if (_e instanceof ne) Se = q.builtins.get(_e.value); else throw new Error(`Cannot access property with non-string/non-number: got ${_e.type}`); else { if (!(_e instanceof ne)) throw new Error(`Cannot access property with non-string: got ${_e.type}`); Se = q.builtins.get(_e.value); } return Se instanceof Ze ? Se : new Be(); } evaluateSet(A, oe) { const q = this.evaluate(A.value, oe); if (A.assignee.type === "Identifier") { const _e = A.assignee.value; oe.setVariable(_e, q); } else if (A.assignee.type === "MemberExpression") { const _e = A.assignee, Se = this.evaluate(_e.object, oe); if (!(Se instanceof $e)) throw new Error("Cannot assign to member of non-object"); if (_e.property.type !== "Identifier") throw new Error("Cannot assign to member with non-identifier property"); Se.value.set(_e.property.value, q); } else throw new Error(`Invalid LHS inside assignment expression: ${JSON.stringify(A.assignee)}`); return new Ve(); } evaluateIf(A, oe) { const q = this.evaluate(A.test, oe); return this.evaluateBlock(q.__bool__().value ? A.body : A.alternate, oe); } evaluateFor(A, oe) { const q = new Qe(oe); let _e, Se; if (A.iterable.type === "SelectExpression") { const vt = A.iterable; Se = this.evaluate(vt.iterable, q), _e = vt.test; } else Se = this.evaluate(A.iterable, q); if (!(Se instanceof Me)) throw new Error(`Expected iterable type in for loop: got ${Se.type}`); const He = [], et = []; for (let vt = 0; vt < Se.value.length; ++vt) { const Yt = new Qe(q), tr = Se.value[vt]; let xr; if (A.loopvar.type === "Identifier") xr = (mr) => mr.setVariable(A.loopvar.value, tr); else if (A.loopvar.type === "TupleLiteral") { const mr = A.loopvar; if (tr.type !== "ArrayValue") throw new Error(`Cannot unpack non-iterable type: ${tr.type}`); const Pr = tr; if (mr.value.length !== Pr.value.length) throw new Error(`Too ${mr.value.length > Pr.value.length ? "few" : "many"} items to unpack`); xr = (Sr) => { for (let Hr = 0; Hr < mr.value.length; ++Hr) { if (mr.value[Hr].type !== "Identifier")