UNPKG

@rudderstack/json-template-engine

Version:
1,554 lines 93.3 kB
var d = /* @__PURE__ */ ((i) => (i.FUNCTION = "function", i.NEW = "new", i.TYPEOF = "typeof", i.LET = "let", i.CONST = "const", i.LAMBDA = "lambda", i.AWAIT = "await", i.ASYNC = "async", i.IN = "in", i.NOT_IN = "nin", i.NOT = "not", i.CONTAINS = "contains", i.SUBSETOF = "subsetof", i.ANYOF = "anyof", i.NONEOF = "noneof", i.EMPTY = "empty", i.SIZE = "size", i.RETURN = "return", i.THROW = "throw", i.CONTINUE = "continue", i.BREAK = "break", i.FOR = "for", i))(d || {}), p = /* @__PURE__ */ ((i) => (i.UNKNOWN = "unknown", i.ID = "id", i.INT = "int", i.FLOAT = "float", i.TEMPLATE = "template", i.STR = "str", i.BOOL = "bool", i.NULL = "null", i.UNDEFINED = "undefined", i.LAMBDA_ARG = "lambda_arg", i.PUNCT = "punct", i.THROW = "throw", i.KEYWORD = "keyword", i.EOT = "eot", i.REGEXP = "regexp", i))(p || {}), l = /* @__PURE__ */ ((i) => (i.BASE = "base", i.CONDITIONAL = "conditional", i.ASSIGNMENT = "assignment", i.COALESCING = "coalescing", i.OR = "or", i.AND = "and", i.EQUALITY = "equality", i.RELATIONAL = "relational", i.SHIFT = "shift", i.ADDITION = "addition", i.MULTIPLICATION = "multiplication", i.POWER = "power", i.UNARY = "unary", i.PREFIX_INCREMENT = "prefix_increment", i.POSTFIX_INCREMENT = "postfix_increment", i))(l || {}), n = /* @__PURE__ */ ((i) => (i.EMPTY = "empty", i.PATH = "path", i.PATH_OPTIONS = "path_options", i.SELECTOR = "selector", i.LAMBDA_ARG = "lambda_arg", i.INCREMENT = "increment", i.LITERAL = "literal", i.LOGICAL_COALESCE_EXPR = "logical_coalesce_expr", i.LOGICAL_OR_EXPR = "logical_or_expr", i.LOGICAL_AND_EXPR = "logical_and_expr", i.COMPARISON_EXPR = "comparison_expr", i.IN_EXPR = "in_expr", i.MATH_EXPR = "math_expr", i.UNARY_EXPR = "unary_expr", i.SPREAD_EXPR = "spread_expr", i.CONDITIONAL_EXPR = "conditional_expr", i.ARRAY_INDEX_FILTER_EXPR = "array_index_filter_expr", i.ALL_FILTER_EXPR = "all_filter_expr", i.OBJECT_INDEX_FILTER_EXPR = "object_index_filter_expr", i.RANGE_FILTER_EXPR = "range_filter_expr", i.OBJECT_FILTER_EXPR = "object_filter_expr", i.ARRAY_FILTER_EXPR = "array_filter_expr", i.DEFINITION_EXPR = "definition_expr", i.ASSIGNMENT_EXPR = "assignment_expr", i.OBJECT_PROP_EXPR = "object_prop_expr", i.OBJECT_EXPR = "object_expr", i.ARRAY_EXPR = "array_expr", i.BLOCK_EXPR = "block_expr", i.FUNCTION_EXPR = "function_expr", i.FUNCTION_CALL_EXPR = "function_call_expr", i.RETURN_EXPR = "return_expr", i.THROW_EXPR = "throw_expr", i.STATEMENTS_EXPR = "statements_expr", i.LOOP_CONTROL_EXPR = "loop_control_expr", i.LOOP_EXPR = "loop_expr", i.TEMPLATE_EXPR = "TEMPLATE_EXPR", i))(n || {}), f = /* @__PURE__ */ ((i) => (i.SIMPLE = "simple", i.RICH = "rich", i.JSON = "json", i.UNKNOWN = "unknown", i))(f || {}); const A = "___", I = "___d", O = "___b", tt = "___b.context.", et = "___r", rt = "___f", st = 4, k = { type: n.EMPTY }; class C extends Error { constructor(t, e, r) { super(`${t}. Input: ${e}, Output: ${r}`), this.inputMapping = e, this.outputMapping = r; } } class Y extends Error { } const F = { RESERVED_ID: 'Reserved ID pattern "%0"', UNEXP_TOKEN: 'Unexpected token "%0"', UNKNOWN_TOKEN: "Unknown token", UNEXP_EOT: "Unexpected end of template" }; class y { constructor(t) { this.idx = 0, this.buf = [], this.codeChars = t.split(""); } init() { this.idx = 0, this.buf = []; } currentIndex() { return this.idx; } reset(t) { this.idx = t, this.buf = []; } getCode(t, e) { return this.codeChars.slice(t, e).join(""); } match(t, e = 0) { if (!t) return !1; const r = this.lookahead(e); return r.type === p.PUNCT && r.value === t; } matchAssignment() { return this.match("=") || this.match("+=") || this.match("-=") || this.match("*=") || this.match("/="); } matchLiteral() { return y.isLiteralToken(this.lookahead()); } matchTemplate() { return this.matchTokenType(p.TEMPLATE); } matchINT(t = 0) { return this.matchTokenType(p.INT, t); } matchToArray() { return this.match("[") && this.match("]", 1); } matchCompileTimeExpr() { return this.match("{") && this.match("{", 1); } matchMappings() { return this.match("~m"); } matchSimplePath() { return this.match("~s"); } matchRichPath() { return this.match("~r"); } matchJsonPath() { return this.match("~j"); } matchPathType() { return this.matchRichPath() || this.matchJsonPath() || this.matchSimplePath(); } matchPath() { return this.matchPathSelector() || this.matchID(); } matchObjectContextProp() { return this.match("@") && this.matchID(1); } matchSpread() { return this.match("..."); } matchIncrement() { return this.match("++"); } matchDecrement() { return this.match("--"); } matchPathPartSelector() { const t = this.lookahead(); return t.type === p.PUNCT ? t.value === "." || t.value === ".." : !1; } matchPathSelector() { const t = this.lookahead(); if (t.type === p.PUNCT) { const { value: e } = t; return e === "." || e === ".." || e === "^" || e === "@"; } return !1; } matchTokenType(t, e = 0) { return this.lookahead(e).type === t; } matchID(t = 0) { return this.matchTokenType(p.ID, t); } matchEOT() { return this.matchTokenType(p.EOT); } static isOperator(t) { return Object.values(d).some((e) => e.toString() === t); } matchKeyword() { return this.matchTokenType(p.KEYWORD); } matchKeywordValue(t) { const e = this.lookahead(); return e.type === p.KEYWORD && e.value === t; } matchContains() { return this.matchKeywordValue(d.CONTAINS); } matchEmpty() { return this.matchKeywordValue(d.EMPTY); } matchSize() { return this.matchKeywordValue(d.SIZE); } matchSubsetOf() { return this.matchKeywordValue(d.SUBSETOF); } matchAnyOf() { return this.matchKeywordValue(d.ANYOF); } matchNoneOf() { return this.matchKeywordValue(d.NONEOF); } matchIN() { return this.matchKeywordValue(d.IN); } matchNotIN() { return this.matchKeywordValue(d.NOT_IN); } matchFunction() { return this.matchKeywordValue(d.FUNCTION); } matchNew() { return this.matchKeywordValue(d.NEW); } matchTypeOf() { return this.matchKeywordValue(d.TYPEOF); } matchAwait() { return this.matchKeywordValue(d.AWAIT); } matchLambda() { return this.matchKeywordValue(d.LAMBDA); } expect(t) { const e = this.lex(); (e.type !== p.PUNCT || e.value !== t) && this.throwUnexpectedToken(e); } lookahead(t = 0) { if (this.buf[t] !== void 0) return this.buf[t]; const e = this.idx; this.buf.length && (this.idx = this.buf[this.buf.length - 1].range[1]); for (let r = this.buf.length; r <= t; r += 1) this.buf.push(this.advance()); return this.idx = e, this.buf[t]; } isLineCommentStart() { return this.codeChars[this.idx] === "/" && this.codeChars[this.idx + 1] === "/"; } isLineCommentEnd() { return this.idx >= this.codeChars.length || this.codeChars[this.idx] === ` `; } isBlockCommentStart() { return this.codeChars[this.idx] === "/" && this.codeChars[this.idx + 1] === "*"; } isBlockCommentEnd() { return this.codeChars[this.idx] === "*" && this.codeChars[this.idx + 1] === "/"; } skipLineComment() { if (this.isLineCommentStart()) { for (; !this.isLineCommentEnd(); ) ++this.idx; ++this.idx; } } skipBlockComment() { if (this.isBlockCommentStart()) { for (; !this.isBlockCommentEnd(); ) ++this.idx; this.idx += 2; } } isWhiteSpace() { return ` \r `.includes(this.codeChars[this.idx]); } skipWhitespace() { for (; this.isWhiteSpace(); ) ++this.idx; } skipInput() { for (; this.isWhiteSpace() || this.isBlockCommentStart() || this.isLineCommentStart(); ) this.skipWhitespace(), this.skipLineComment(), this.skipBlockComment(); } advance() { if (this.skipInput(), this.idx >= this.codeChars.length) return { type: p.EOT, range: [this.idx, this.idx], value: void 0 }; const t = this.scanRegularExpressions() ?? this.scanPunctuator() ?? this.scanID() ?? this.scanString() ?? this.scanInteger(); if (t) return t; y.throwError(F.UNKNOWN_TOKEN); } value() { return this.lex().value; } ignoreTokens(t) { for (let e = 0; e < t; e++) this.lex(); } lex() { if (this.buf[0]) { this.idx = this.buf[0].range[1]; const t = this.buf[0]; return this.buf = this.buf.slice(1), t; } return this.advance(); } static isLiteralToken(t) { return t.type === p.BOOL || t.type === p.INT || t.type === p.FLOAT || t.type === p.STR || t.type === p.NULL || t.type === p.UNDEFINED || t.type === p.REGEXP; } throwUnexpectedToken(t) { const e = t ?? this.lookahead(); e.type === p.EOT && y.throwError(F.UNEXP_EOT), y.throwError(F.UNEXP_TOKEN, e.value); } static throwError(t, ...e) { const r = t.replace(/%(\d)/g, (s, a) => e[a]); throw new Y(r); } static isDigit(t) { return "0123456789".indexOf(t) >= 0; } static isIdStart(t) { return t === "_" || t === "$" || t >= "a" && t <= "z" || t >= "A" && t <= "Z"; } static isIdPart(t) { return this.isIdStart(t) || t >= "0" && t <= "9"; } static validateID(t) { t.startsWith(A) && y.throwError(F.RESERVED_ID, t); } scanID() { let t = this.codeChars[this.idx]; if (!y.isIdStart(t)) return; const e = this.idx; let r = t; for (; ++this.idx < this.codeChars.length && (t = this.codeChars[this.idx], !!y.isIdPart(t)); ) r += t; if (y.isOperator(r)) return { type: p.KEYWORD, value: r, range: [e, this.idx] }; switch (r) { case "true": case "false": return { type: p.BOOL, value: r === "true", range: [e, this.idx] }; case "null": return { type: p.NULL, value: null, range: [e, this.idx] }; case "undefined": return { type: p.UNDEFINED, value: void 0, range: [e, this.idx] }; default: return y.validateID(r), { type: p.ID, value: r, range: [e, this.idx] }; } } scanString() { if (this.codeChars[this.idx] !== '"' && this.codeChars[this.idx] !== "`" && this.codeChars[this.idx] !== "'") return; const t = this.codeChars[this.idx], e = ++this.idx; let r = "", s = !1, a; for (; this.idx < this.codeChars.length; ) { if (a = this.codeChars[this.idx++], a === "\\") a = this.codeChars[this.idx++]; else if ("'\"`".includes(a) && a === t) { s = !0; break; } r += a; } if (s) return { type: t === "`" ? p.TEMPLATE : p.STR, value: r, range: [e, this.idx] }; this.throwUnexpectedToken(); } scanInteger() { const t = this.idx; let e = this.codeChars[this.idx]; if (!y.isDigit(e)) return; let r = e; for (; ++this.idx < this.codeChars.length && (e = this.codeChars[this.idx], !!y.isDigit(e)); ) r += e; return { type: p.INT, value: r, range: [t, this.idx] }; } scanPunctuatorForDots() { const t = this.idx, e = this.codeChars[this.idx], r = this.codeChars[this.idx + 1], s = this.codeChars[this.idx + 2]; if (e === ".") return r === "(" && s === ")" ? (this.idx += 3, { type: p.PUNCT, value: ".()", range: [t, this.idx] }) : r === "." && s === "." ? (this.idx += 3, { type: p.PUNCT, value: "...", range: [t, this.idx] }) : r === "." ? (this.idx += 2, { type: p.PUNCT, value: "..", range: [t, this.idx] }) : { type: p.PUNCT, value: ".", range: [t, ++this.idx] }; } scanPunctuatorForEquality() { const t = this.idx, e = this.codeChars[this.idx], r = this.codeChars[this.idx + 1], s = this.codeChars[this.idx + 2]; if (r === "=") { if (s === "=") { if ("=!^$*".indexOf(e) >= 0) return this.idx += 3, { type: p.PUNCT, value: e + r + s, range: [t, this.idx] }; } else if ("^$*".indexOf(s) >= 0) { if (e === "=") return this.idx += 3, { type: p.PUNCT, value: e + r + s, range: [t, this.idx] }; } else if ("=!^$><".indexOf(e) >= 0) return this.idx += 2, { type: p.PUNCT, value: e + r, range: [t, this.idx] }; } else if (e === "=") return "^$*~".indexOf(r) >= 0 ? (this.idx += 2, { type: p.PUNCT, value: e + r, range: [t, this.idx] }) : { type: p.PUNCT, value: e, range: [t, ++this.idx] }; } scanPunctuatorForRepeatedTokens(t, e = 2) { const r = this.idx, s = this.codeChars[this.idx]; let a = s; for (let h = 1; h < e; h++) { if (this.codeChars[this.idx + h] !== s) return; a += s; } if (t.includes(s)) return this.idx += e, { type: p.PUNCT, value: a, range: [r, this.idx] }; } scanSingleCharPunctuators() { const t = this.idx, e = this.codeChars[this.idx]; if (`,;:{}()[]^+-*/%!><|=@~#? `.includes(e)) return { type: p.PUNCT, value: e, range: [t, ++this.idx] }; } scanPunctuatorForQuestionMarks() { const t = this.idx, e = this.codeChars[this.idx], r = this.codeChars[this.idx + 1]; if (e === "?" && y.isDigit(r)) return this.idx += 2, { type: p.LAMBDA_ARG, value: Number(r), range: [t, this.idx] }; } scanPunctuatorForPaths() { const t = this.idx, e = this.codeChars[this.idx], r = this.codeChars[this.idx + 1]; if (e === "~" && "rsjm".includes(r)) return this.idx += 2, { type: p.PUNCT, value: e + r, range: [t, this.idx] }; } scanPunctuatorForArithmeticAssignment() { const t = this.idx, e = this.codeChars[this.idx], r = this.codeChars[this.idx + 1]; if ("+-/*".includes(e) && r === "=") return this.idx += 2, { type: p.PUNCT, value: e + r, range: [t, this.idx] }; } static isValidRegExp(t, e) { try { return RegExp(t, e), !0; } catch { return !1; } } getRegExpModifiers() { let t = ""; for (; "gimsuyv".includes(this.codeChars[this.idx]); ) t += this.codeChars[this.idx], this.idx++; return t; } scanRegularExpressions() { const t = this.idx; if (this.codeChars[this.idx] === "/") { let r = this.idx + 1; for (; r < this.codeChars.length; ) { if (this.codeChars[r] === ` `) return; if (this.codeChars[r] === "/") break; r++; } if (r < this.codeChars.length) { this.idx = r + 1; const s = this.getCode(t + 1, r), a = this.getRegExpModifiers(); return y.isValidRegExp(s, a) || y.throwError("invalid regular expression '%0'", s), { type: p.REGEXP, value: this.getCode(t, this.idx), range: [t, this.idx] }; } } } scanPunctuator() { return this.scanPunctuatorForDots() ?? this.scanPunctuatorForQuestionMarks() ?? this.scanPunctuatorForArithmeticAssignment() ?? this.scanPunctuatorForEquality() ?? this.scanPunctuatorForPaths() ?? this.scanPunctuatorForRepeatedTokens("?", 3) ?? this.scanPunctuatorForRepeatedTokens("|&*.=>?<+-", 2) ?? this.scanSingleCharPunctuators(); } } class P extends Error { } function w(i) { if (i != null) return Array.isArray(i) ? i : [i]; } function $(i) { if (i.length) return i[i.length - 1]; } function S(i) { return { type: n.BLOCK_EXPR, statements: [i] }; } function b(...i) { return { type: n.STATEMENTS_EXPR, statements: i }; } function it(...i) { return (async function() { }).constructor(...i); } function nt(i) { return typeof i == "object" && !Array.isArray(i) && Object.values(n).includes(i.type); } function N(i) { return typeof i != "string" ? "" : `"${i.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`; } class m { constructor(t, e) { this.pathTypesStack = [], this.loopCount = 0, this.lexer = t, this.options = e; } parse() { return this.lexer.init(), this.parseStatementsExpr(); } parseEndOfStatement(t) { if (this.lexer.matchEOT() || this.lexer.match(t)) return; if (this.lexer.match(";")) { this.lexer.ignoreTokens(1); return; } const e = this.lexer.currentIndex(), r = this.lexer.lookahead().range[0]; this.lexer.getCode(e, r).includes(` `) || this.lexer.throwUnexpectedToken(); } parseStatements(t) { const e = []; for (; !this.lexer.matchEOT() && !this.lexer.match(t); ) e.push(this.parseStatementExpr()), this.parseEndOfStatement(t); return e; } static validateStatements(t, e) { if (!t.length) { if ((e == null ? void 0 : e.parentType) === n.CONDITIONAL_EXPR || (e == null ? void 0 : e.parentType) === n.LOOP_EXPR) throw new P( "Empty statements are not allowed in loop and condtional expressions" ); return; } for (let r = 0; r < t.length; r += 1) { const s = t[r]; if ((s.type === n.RETURN_EXPR || s.type === n.THROW_EXPR || s.type === n.LOOP_CONTROL_EXPR) && ((e == null ? void 0 : e.parentType) !== n.CONDITIONAL_EXPR || r !== t.length - 1)) throw new P( "return, throw, continue and break statements are only allowed as last statements in conditional expressions" ); } } parseStatementsExpr(t) { const e = this.parseStatements(t == null ? void 0 : t.blockEnd); return m.validateStatements(e, t), { type: n.STATEMENTS_EXPR, statements: e }; } parseStatementExpr() { return this.parseBaseExpr(); } parseAssignmentExpr() { const t = this.parseNextExpr(l.ASSIGNMENT); if (t.type === n.PATH && this.lexer.matchAssignment()) { const e = this.lexer.value(), r = t; if (!r.root || typeof r.root == "object" || r.root === I) throw new P("Invalid assignment path"); if (!m.isSimplePath(t)) throw new P("Invalid assignment path"); return r.inferredPathType = f.SIMPLE, { type: n.ASSIGNMENT_EXPR, value: this.parseBaseExpr(), op: e, path: r }; } return t; } parseBaseExpr() { const t = this.lexer.currentIndex(); try { return this.parseNextExpr(l.BASE); } catch (e) { const r = this.lexer.getCode(t, this.lexer.currentIndex()); throw e.message.includes("at") ? e : new P(`${e.message} at ${r}`); } } parseNextExpr(t) { switch (t) { case l.CONDITIONAL: return this.parseAssignmentExpr(); case l.ASSIGNMENT: return this.parseCoalesceExpr(); case l.COALESCING: return this.parseLogicalORExpr(); case l.OR: return this.parseLogicalANDExpr(); case l.AND: return this.parseEqualityExpr(); case l.EQUALITY: return this.parseRelationalExpr(); case l.RELATIONAL: return this.parseShiftExpr(); case l.SHIFT: return this.parseAdditiveExpr(); case l.ADDITION: return this.parseMultiplicativeExpr(); case l.MULTIPLICATION: return this.parsePowerExpr(); case l.POWER: return this.parseUnaryExpr(); case l.UNARY: return this.parsePrefixIncreamentExpr(); case l.PREFIX_INCREMENT: return this.parsePostfixIncreamentExpr(); case l.POSTFIX_INCREMENT: return this.parsePathAfterExpr(); default: return this.parseConditionalExpr(); } } parsePathPart() { if (this.lexer.match(".()")) this.lexer.ignoreTokens(1); else { if (this.lexer.match(".") && this.lexer.match("(", 1)) return this.lexer.ignoreTokens(1), this.parseBlockExpr(); if (this.lexer.match("(")) return this.parseFunctionCallExpr(); if (this.lexer.matchPathPartSelector()) return this.parseSelector(); if (this.lexer.matchToArray()) return this.parsePathOptions(); if (this.lexer.match("{")) return this.parseObjectFiltersExpr(); if (this.lexer.match("[")) return this.parseArrayFilterExpr(); if (this.lexer.match("@") || this.lexer.match("#")) return this.parsePathOptions(); } } parsePathParts() { let t = [], e = w(this.parsePathPart()); for (; e && (t = t.concat(e), e[0].type !== n.FUNCTION_CALL_EXPR); ) e = w(this.parsePathPart()); return m.ignoreEmptySelectors(t); } parseContextVariable() { return this.lexer.ignoreTokens(1), this.lexer.matchID() || this.lexer.throwUnexpectedToken(), this.lexer.value(); } parsePathOptions() { const t = {}; for (; this.lexer.match("@") || this.lexer.match("#") || this.lexer.matchToArray(); ) { if (this.lexer.match("@")) { t.item = this.parseContextVariable(); continue; } if (this.lexer.match("#")) { t.index = this.parseContextVariable(); continue; } this.lexer.matchToArray() && (this.lexer.ignoreTokens(2), t.toArray = !0); } return { type: n.PATH_OPTIONS, options: t }; } parsePathRoot(t, e) { if (e) return e; const r = this.lexer.lookahead(); if (r.type === p.ID && r.value !== "$") return this.lexer.value(); const s = { "^": I, $: t.inferredPathType === f.JSON ? I : O, "@": void 0 }; if (Object.prototype.hasOwnProperty.call(s, r.value)) return this.lexer.ignoreTokens(1), s[r.value]; } getInferredPathType() { var t; return this.pathTypesStack.length > 0 ? this.pathTypesStack[this.pathTypesStack.length - 1] : { pathType: f.UNKNOWN, inferredPathType: ((t = this.options) == null ? void 0 : t.defaultPathType) ?? f.RICH }; } createPathResult(t) { return { pathType: t, inferredPathType: t }; } parsePathType() { return this.lexer.matchSimplePath() ? (this.lexer.ignoreTokens(1), this.createPathResult(f.SIMPLE)) : this.lexer.matchRichPath() ? (this.lexer.ignoreTokens(1), this.createPathResult(f.RICH)) : this.lexer.matchJsonPath() ? (this.lexer.ignoreTokens(1), this.createPathResult(f.JSON)) : this.getInferredPathType(); } parsePathTypeExpr() { const t = this.parsePathType(); this.pathTypesStack.push(t); const e = this.parseBaseExpr(); return this.pathTypesStack.pop(), e; } parsePath(t) { const e = this.parsePathType(), r = { type: n.PATH, root: this.parsePathRoot(e, t == null ? void 0 : t.root), parts: this.parsePathParts(), ...e }; return r.parts.length ? this.updatePathExpr(r) : m.setPathTypeIfNotJSON(r, f.SIMPLE); } static createArrayIndexFilterExpr(t) { return { type: n.ARRAY_INDEX_FILTER_EXPR, indexes: { type: n.ARRAY_EXPR, elements: [t] } }; } static createArrayFilterExpr(t) { return { type: n.ARRAY_FILTER_EXPR, filter: t }; } parseSelector() { const t = this.lexer.value(); if (this.lexer.matchINT()) return m.createArrayFilterExpr( m.createArrayIndexFilterExpr(this.parseLiteralExpr()) ); let e; return (this.lexer.match("*") || this.lexer.matchID() || this.lexer.matchKeyword() || this.lexer.matchTokenType(p.STR)) && (e = this.lexer.lex(), e.type === p.KEYWORD && (e.type = p.ID)), { type: n.SELECTOR, selector: t, prop: e }; } parseRangeFilterExpr() { if (this.lexer.match(":")) return this.lexer.ignoreTokens(1), { type: n.RANGE_FILTER_EXPR, toIdx: this.parseBaseExpr() }; const t = this.parseBaseExpr(); return this.lexer.match(":") ? (this.lexer.ignoreTokens(1), this.lexer.match("]") ? { type: n.RANGE_FILTER_EXPR, fromIdx: t } : { type: n.RANGE_FILTER_EXPR, fromIdx: t, toIdx: this.parseBaseExpr() }) : t; } parseArrayIndexFilterExpr(t) { const e = []; return t && (e.push(t), this.lexer.match("]") || this.lexer.expect(",")), { type: n.ARRAY_INDEX_FILTER_EXPR, indexes: { type: n.ARRAY_EXPR, elements: [ ...e, ...this.parseCommaSeparatedElements("]", () => this.parseSpreadExpr()) ] } }; } parseArrayFilter() { if (this.lexer.matchSpread()) return this.parseArrayIndexFilterExpr(); const t = this.parseRangeFilterExpr(); return t.type === n.RANGE_FILTER_EXPR ? t : this.parseArrayIndexFilterExpr(t); } parseObjectFilter() { let t = !1; return (this.lexer.match("~") || this.lexer.match("!")) && this.lexer.match("[", 1) && (this.lexer.ignoreTokens(1), t = !0), this.lexer.match("[") ? { type: n.OBJECT_INDEX_FILTER_EXPR, indexes: this.parseArrayExpr(), exclude: t } : { type: n.OBJECT_FILTER_EXPR, filter: this.parseBaseExpr() }; } parseObjectFiltersExpr() { const t = [], e = []; for (; this.lexer.match("{"); ) { this.lexer.expect("{"); const s = this.parseObjectFilter(); s.type === n.OBJECT_INDEX_FILTER_EXPR ? e.push(s) : t.push(s.filter), this.lexer.expect("}"), this.lexer.match(".") && this.lexer.match("{", 1) && this.lexer.ignoreTokens(1); } return t.length ? [{ type: n.OBJECT_FILTER_EXPR, filter: this.combineExpressionsAsBinaryExpr(t, n.LOGICAL_AND_EXPR, "&&") }, ...e] : e; } parseLoopControlExpr() { const t = this.lexer.value(); if (!this.loopCount) throw new P(`encounted loop control outside loop: ${t}`); return { type: n.LOOP_CONTROL_EXPR, control: t }; } parseCurlyBlockExpr(t) { this.lexer.expect("{"); const e = this.parseStatementsExpr(t); return this.lexer.expect("}"), e; } parseConditionalBodyExpr() { const t = this.lexer.currentIndex(); if (this.lexer.match("{")) try { return this.parseObjectExpr(); } catch (e) { return e instanceof Y && this.lexer.reset(t), this.parseCurlyBlockExpr({ blockEnd: "}", parentType: n.CONDITIONAL_EXPR }); } return this.parseBaseExpr(); } parseConditionalExpr() { const t = this.parseNextExpr(l.CONDITIONAL); if (this.lexer.match("?")) { this.lexer.ignoreTokens(1); const e = this.parseConditionalBodyExpr(); let r; return this.lexer.match(":") && (this.lexer.ignoreTokens(1), r = this.parseConditionalBodyExpr()), { type: n.CONDITIONAL_EXPR, if: t, then: e, else: r }; } return t; } parseLoopExpr() { this.loopCount++, this.lexer.ignoreTokens(1); let t, e, r; this.lexer.match("{") || (this.lexer.expect("("), this.lexer.match(";") || (t = this.parseAssignmentExpr()), this.lexer.expect(";"), this.lexer.match(";") || (e = this.parseLogicalORExpr()), this.lexer.expect(";"), this.lexer.match(")") || (r = this.parseAssignmentExpr()), this.lexer.expect(")")); const s = this.parseCurlyBlockExpr({ blockEnd: "}", parentType: n.LOOP_EXPR }); return this.loopCount--, { type: n.LOOP_EXPR, init: t, test: e, update: r, body: s }; } parseJSONObjectFilter() { this.lexer.expect("?"), this.lexer.expect("("); const t = this.parseBaseExpr(); return this.lexer.expect(")"), { type: n.OBJECT_FILTER_EXPR, filter: t }; } parseAllFilter() { return this.lexer.expect("*"), { type: n.OBJECT_FILTER_EXPR, filter: { type: n.ALL_FILTER_EXPR } }; } handleArrayFilterExpr(t) { if (t.type === n.ARRAY_INDEX_FILTER_EXPR && t.indexes.elements.length === 1) { const [r] = t.indexes.elements; if (r.type === n.LITERAL && r.tokenType === p.STR) return { type: n.SELECTOR, selector: ".", prop: { type: p.STR, value: r.value } }; } return { type: n.ARRAY_FILTER_EXPR, filter: t }; } parseArrayFilterExpr() { this.lexer.expect("["); let t; return this.lexer.match("?") ? t = this.parseJSONObjectFilter() : this.lexer.match("*") ? t = this.parseAllFilter() : t = this.handleArrayFilterExpr(this.parseArrayFilter()), this.lexer.expect("]"), t; } combineExpressionsAsBinaryExpr(t, e, r) { if (!(t != null && t.length)) throw new P("expected at least 1 expression"); return t.length === 1 ? t[0] : { type: e, op: r, args: [t.shift(), this.combineExpressionsAsBinaryExpr(t, e, r)] }; } parseArrayCoalesceExpr() { this.lexer.ignoreTokens(1); const t = this.parseArrayExpr(); return this.combineExpressionsAsBinaryExpr( t.elements, n.LOGICAL_COALESCE_EXPR, "??" ); } parseCoalesceExpr() { const t = this.parseNextExpr(l.COALESCING); return this.lexer.match("??") ? { type: n.LOGICAL_COALESCE_EXPR, op: this.lexer.value(), args: [t, this.parseCoalesceExpr()] } : t; } parseLogicalORExpr() { const t = this.parseNextExpr(l.OR); return this.lexer.match("||") ? { type: n.LOGICAL_OR_EXPR, op: this.lexer.value(), args: [t, this.parseLogicalORExpr()] } : t; } parseLogicalANDExpr() { const t = this.parseNextExpr(l.AND); return this.lexer.match("&&") ? { type: n.LOGICAL_AND_EXPR, op: this.lexer.value(), args: [t, this.parseLogicalANDExpr()] } : t; } parseEqualityExpr() { const t = this.parseNextExpr(l.EQUALITY); return this.lexer.match("==") || this.lexer.match("!=") || this.lexer.match("===") || this.lexer.match("!==") || this.lexer.match("^==") || this.lexer.match("==^") || this.lexer.match("^=") || this.lexer.match("=^") || this.lexer.match("$==") || this.lexer.match("==$") || this.lexer.match("$=") || this.lexer.match("=$") || this.lexer.match("==*") || this.lexer.match("=~") || this.lexer.match("=*") ? { type: n.COMPARISON_EXPR, op: this.lexer.value(), args: [t, this.parseEqualityExpr()] } : t; } parseInExpr(t) { return this.lexer.ignoreTokens(1), { type: n.IN_EXPR, op: d.IN, args: [t, this.parseRelationalExpr()] }; } parseRelationalExpr() { const t = this.parseNextExpr(l.RELATIONAL); return this.lexer.match("<") || this.lexer.match(">") || this.lexer.match("<=") || this.lexer.match(">=") || this.lexer.matchContains() || this.lexer.matchSize() || this.lexer.matchEmpty() || this.lexer.matchAnyOf() || this.lexer.matchSubsetOf() ? { type: n.COMPARISON_EXPR, op: this.lexer.value(), args: [t, this.parseRelationalExpr()] } : this.lexer.matchIN() ? this.parseInExpr(t) : this.lexer.matchNotIN() ? { type: n.UNARY_EXPR, op: "!", arg: S(this.parseInExpr(t)) } : this.lexer.matchNoneOf() ? (this.lexer.ignoreTokens(1), { type: n.UNARY_EXPR, op: "!", arg: S({ type: n.COMPARISON_EXPR, op: d.ANYOF, args: [t, this.parseRelationalExpr()] }) }) : t; } parseShiftExpr() { const t = this.parseNextExpr(l.SHIFT); return this.lexer.match(">>") || this.lexer.match("<<") ? { type: n.MATH_EXPR, op: this.lexer.value(), args: [t, this.parseShiftExpr()] } : t; } parseAdditiveExpr() { const t = this.parseNextExpr(l.ADDITION); return this.lexer.match("+") || this.lexer.match("-") ? { type: n.MATH_EXPR, op: this.lexer.value(), args: [t, this.parseAdditiveExpr()] } : t; } parseMultiplicativeExpr() { const t = this.parseNextExpr(l.MULTIPLICATION); return this.lexer.match("*") || this.lexer.match("/") || this.lexer.match("%") ? { type: n.MATH_EXPR, op: this.lexer.value(), args: [t, this.parseMultiplicativeExpr()] } : t; } parsePowerExpr() { const t = this.parseNextExpr(l.POWER); return this.lexer.match("**") ? { type: n.MATH_EXPR, op: this.lexer.value(), args: [t, this.parsePowerExpr()] } : t; } parsePrefixIncreamentExpr() { if (this.lexer.matchIncrement() || this.lexer.matchDecrement()) { const t = this.lexer.value(); if (!this.lexer.matchID()) throw new P("Invalid prefix increment expression"); const e = this.lexer.value(); return { type: n.INCREMENT, op: t, id: e }; } return this.parseNextExpr(l.PREFIX_INCREMENT); } static convertToID(t) { if (t.type === n.PATH) { const e = t; if (!e.root || typeof e.root != "string" || e.parts.length !== 0 || e.root === I || e.root === O) throw new P("Invalid postfix increment expression"); return e.root; } throw new P("Invalid postfix increment expression"); } parsePostfixIncreamentExpr() { const t = this.parseNextExpr(l.POSTFIX_INCREMENT); return this.lexer.matchIncrement() || this.lexer.matchDecrement() ? { type: n.INCREMENT, op: this.lexer.value(), id: m.convertToID(t), postfix: !0 } : t; } parseUnaryExpr() { return this.lexer.match("!") || this.lexer.match("+") || this.lexer.match("-") || this.lexer.matchTypeOf() || this.lexer.matchAwait() ? { type: n.UNARY_EXPR, op: this.lexer.value(), arg: this.parseUnaryExpr() } : this.parseNextExpr(l.UNARY); } shouldSkipPathParsing(t) { switch (t.type) { case n.EMPTY: case n.DEFINITION_EXPR: case n.ASSIGNMENT_EXPR: case n.SPREAD_EXPR: return !0; case n.LITERAL: case n.MATH_EXPR: case n.COMPARISON_EXPR: case n.ARRAY_EXPR: case n.OBJECT_EXPR: if (this.lexer.match("(")) return !0; break; case n.FUNCTION_EXPR: if (!this.lexer.match("(")) return !0; break; } return !1; } parsePathAfterExpr() { let t = this.parsePrimaryExpr(); if (this.shouldSkipPathParsing(t)) return t; for (; this.lexer.matchPathType() || this.lexer.matchPathPartSelector() || this.lexer.match("{") || this.lexer.match("[") || this.lexer.match("("); ) t = this.parsePath({ root: t }); return t; } static createLiteralExpr(t) { return { type: n.LITERAL, value: t.value, tokenType: t.type }; } parseLiteralExpr() { return m.createLiteralExpr(this.lexer.lex()); } parseTemplateExpr() { const t = this.lexer.value(); let e = 0; const r = []; for (; e < t.length; ) { const s = t.indexOf("${", e); if (s === -1) { r.push({ type: n.LITERAL, value: t.slice(e), tokenType: p.STR }); break; } const a = t.indexOf("}", s); if (a === -1) throw new P( `Invalid template expression: unclosed expression at ${t}` ); s > e && r.push({ type: n.LITERAL, value: t.slice(e, s), tokenType: p.STR }); try { const h = E.parse( t.slice(s + 2, a), this.options ); r.push(h.statements[0]); } catch (h) { throw new P( `Invalid template expression: ${h.message} at ${t}` ); } e = a + 1; } return { type: n.TEMPLATE_EXPR, parts: r }; } parseIDPath() { const t = []; for (; this.lexer.matchID(); ) { let e = this.lexer.value(); e === "$" && (e = O), t.push(e), this.lexer.match(".") && this.lexer.matchID(1) && this.lexer.ignoreTokens(1); } return t.length || this.lexer.throwUnexpectedToken(), t.join("."); } parseObjectDefVars() { const t = []; for (this.lexer.expect("{"); !this.lexer.match("}"); ) { if (!this.lexer.matchID()) throw new P("Invalid object vars"); t.push(this.lexer.value()), this.lexer.match("}") || this.lexer.expect(","); } if (this.lexer.expect("}"), t.length === 0) throw new P("Empty object vars"); return t; } parseNormalDefVars() { const t = []; if (!this.lexer.matchID()) throw new P("Invalid normal vars"); return t.push(this.lexer.value()), t; } parseDefinitionExpr() { const t = this.lexer.value(), e = this.lexer.match("{"), r = e ? this.parseObjectDefVars() : this.parseNormalDefVars(); return this.lexer.expect("="), { type: n.DEFINITION_EXPR, value: this.parseBaseExpr(), vars: r, definition: t, fromObject: e }; } parseFunctionCallArgs() { this.lexer.expect("("); const t = this.parseCommaSeparatedElements(")", () => this.parseSpreadExpr()); return this.lexer.expect(")"), t; } parseFunctionCallExpr() { let t; return this.lexer.matchNew() && (this.lexer.ignoreTokens(1), t = `new ${this.parseIDPath()}`), { type: n.FUNCTION_CALL_EXPR, args: this.parseFunctionCallArgs(), id: t }; } parseFunctionDefinitionParam() { let t = ""; return this.lexer.matchSpread() && (this.lexer.ignoreTokens(1), t = "...", this.lexer.match(")", 1) || this.lexer.throwUnexpectedToken()), this.lexer.matchID() || this.lexer.throwUnexpectedToken(), `${t}${this.lexer.value()}`; } parseFunctionDefinitionParams() { this.lexer.expect("("); const t = this.parseCommaSeparatedElements(")", () => this.parseFunctionDefinitionParam()); return this.lexer.expect(")"), t; } parseFunctionExpr(t = !1) { this.lexer.ignoreTokens(1); const e = this.parseFunctionDefinitionParams(); return { type: n.FUNCTION_EXPR, params: e, body: this.parseCurlyBlockExpr({ blockEnd: "}" }), async: t }; } parseObjectKeyExpr() { let t; return this.lexer.match("[") ? (this.lexer.ignoreTokens(1), t = this.parseBaseExpr(), this.lexer.expect("]")) : this.lexer.matchID() || this.lexer.matchKeyword() ? t = this.lexer.value() : this.lexer.matchLiteral() && !this.lexer.matchTokenType(p.REGEXP) ? t = this.lexer.value().toString() : this.lexer.throwUnexpectedToken(), t; } parseShortKeyValueObjectPropExpr() { if ((this.lexer.matchID() || this.lexer.matchKeyword()) && (this.lexer.match(",", 1) || this.lexer.match("}", 1))) { const t = this.lexer.lookahead().value, e = this.parseBaseExpr(); return { type: n.OBJECT_PROP_EXPR, key: t, value: e }; } } parseSpreadObjectPropExpr() { if (this.lexer.matchSpread()) return { type: n.OBJECT_PROP_EXPR, value: this.parseSpreadExpr() }; } getObjectPropContextVar() { if (this.lexer.matchObjectContextProp()) return this.lexer.ignoreTokens(1), this.lexer.value(); } parseNormalObjectPropExpr() { const t = this.getObjectPropContextVar(), e = this.parseObjectKeyExpr(); if (t && typeof e == "string") throw new P("Context prop should be used with a key expression"); this.lexer.expect(":"); const r = this.parseBaseExpr(); return { type: n.OBJECT_PROP_EXPR, key: e, value: r, contextVar: t }; } parseObjectPropExpr() { return this.parseSpreadObjectPropExpr() ?? this.parseShortKeyValueObjectPropExpr() ?? this.parseNormalObjectPropExpr(); } parseObjectExpr() { this.lexer.expect("{"); const t = this.parseCommaSeparatedElements("}", () => this.parseObjectPropExpr()); return this.lexer.expect("}"), { type: n.OBJECT_EXPR, props: t }; } parseCommaSeparatedElements(t, e) { const r = []; for (; !this.lexer.match(t); ) r.push(e()), this.lexer.match(t) || this.lexer.expect(","); return r; } parseSpreadExpr() { return this.lexer.matchSpread() ? (this.lexer.ignoreTokens(1), { type: n.SPREAD_EXPR, value: this.parseBaseExpr() }) : this.parseBaseExpr(); } parseArrayExpr() { this.lexer.expect("["); const t = this.parseCommaSeparatedElements("]", () => this.parseSpreadExpr()); return this.lexer.expect("]"), { type: n.ARRAY_EXPR, elements: t }; } parseBlockExpr() { this.lexer.expect("("); const t = this.parseStatements(")"); if (this.lexer.expect(")"), t.length === 0) throw new P("empty block is not allowed"); return { type: n.BLOCK_EXPR, statements: t }; } parseAsyncFunctionExpr() { if (this.lexer.ignoreTokens(1), this.lexer.matchFunction()) return this.parseFunctionExpr(!0); if (this.lexer.matchLambda()) return this.parseLambdaExpr(!0); this.lexer.throwUnexpectedToken(); } parseLambdaExpr(t = !1) { this.lexer.ignoreTokens(1); const e = this.parseBaseExpr(); return { type: n.FUNCTION_EXPR, body: b(e), params: ["...args"], async: t, lambda: !0 }; } parseCompileTimeBaseExpr() { this.lexer.expect("{"), this.lexer.expect("{"); const t = this.parseBaseExpr(); return this.lexer.expect("}"), this.lexer.expect("}"), t; } parseCompileTimeExpr() { var a; this.lexer.expect("{"), this.lexer.expect("{"); const t = this.lexer.matchCompileTimeExpr(), e = t ? this.parseCompileTimeBaseExpr() : this.parseBaseExpr(); this.lexer.expect("}"), this.lexer.expect("}"); const r = E.createAsSync(e).evaluate( {}, (a = this.options) == null ? void 0 : a.compileTimeBindings ), s = t ? r : JSON.stringify(r); return m.parseBaseExprFromTemplate(s); } parseNumber() { let t = this.lexer.value(); return this.lexer.match(".") ? (t += this.lexer.value(), this.lexer.matchINT() && (t += this.lexer.value()), { type: n.LITERAL, value: parseFloat(t), tokenType: p.FLOAT }) : { type: n.LITERAL, value: parseInt(t, 10), tokenType: p.INT }; } parseFloatingNumber() { const t = this.lexer.value() + this.lexer.value(); return { type: n.LITERAL, value: parseFloat(t), tokenType: p.FLOAT }; } parseReturnExpr() { this.lexer.ignoreTokens(1); let t; return this.lexer.match(";") || (t = this.parseBaseExpr()), { type: n.RETURN_EXPR, value: t }; } parseThrowExpr() { return this.lexer.ignoreTokens(1), { type: n.THROW_EXPR, value: this.parseBaseExpr() }; } parseKeywordBasedExpr() { switch (this.lexer.lookahead().value) { case d.NEW: return this.parseFunctionCallExpr(); case d.LAMBDA: return this.parseLambdaExpr(); case d.ASYNC: return this.parseAsyncFunctionExpr(); case d.RETURN: return this.parseReturnExpr(); case d.THROW: return this.parseThrowExpr(); case d.FUNCTION: return this.parseFunctionExpr(); case d.FOR: return this.parseLoopExpr(); case d.CONTINUE: case d.BREAK: return this.parseLoopControlExpr(); default: return this.parseDefinitionExpr(); } } static isValidMapping(t) { return typeof t.key == "string" && t.value.type === n.LITERAL && t.value.tokenType === p.STR; } static convertMappingsToFlatPaths(t) { const e = {}; for (const r of t.props) { if (!m.isValidMapping(r)) throw new P( `Invalid mapping key=${JSON.stringify(r.key)} or value=${JSON.stringify( r.value )}, expected string key and string value` ); e[r.key] = r.value.value; } if (!e.input || !e.output) throw new P( `Invalid mapping: ${JSON.stringify(e)}, missing input or output` ); return e; } parseMappings() { this.lexer.expect("~m"); const t = this.parseArrayExpr(), e = []; for (const r of t.elements) { if (r.type !== n.OBJECT_EXPR) throw new P( `Invalid mapping=${JSON.stringify(r)}, expected object` ); e.push(m.convertMappingsToFlatPaths(r)); } return E.parseMappingPaths(e); } isFloatingNumber() { return this.lexer.match(".") && this.lexer.matchINT(1) && !this.lexer.match(".", 2); } isLambdaArg() { return this.lexer.matchTokenType(p.LAMBDA_ARG); } parseLambdaArgExpr() { return { type: n.LAMBDA_ARG, index: this.lexer.value() }; } parsePrimaryExpr() { return this.lexer.match(";") ? k : this.isLambdaArg() ? this.parseLambdaArgExpr() : this.lexer.matchKeyword() ? this.parseKeywordBasedExpr() : this.lexer.matchINT() ? this.parseNumber() : this.lexer.match("???") ? this.parseArrayCoalesceExpr() : this.isFloatingNumber() ? this.parseFloatingNumber() : this.lexer.matchLiteral() ? this.parseLiteralExpr() : this.lexer.matchTemplate() ? this.parseTemplateExpr() : this.lexer.matchCompileTimeExpr() ? this.parseCompileTimeExpr() : this.lexer.match("{") ? this.parseObjectExpr() : this.lexer.match("[") ? this.parseArrayExpr() : this.lexer.matchPathType() ? this.parsePathTypeExpr() : this.lexer.matchMappings() ? this.parseMappings() : this.lexer.matchPath() ? this.parsePath() : this.lexer.match("(") ? this.parseBlockExpr() : this.lexer.throwUnexpectedToken(); } shouldPathBeConvertedAsBlock(t) { var e; return !((e = this.options) != null && e.mappings) && t.filter( (r, s) => r.type === n.PATH_OPTIONS && s !== t.length - 1 ).some((r) => { var s, a; return ((s = r.options) == null ? void 0 : s.index) ?? ((a = r.options) == null ? void 0 : a.item); }); } static convertToBlockExpr(t) { return { type: n.FUNCTION_EXPR, block: !0, body: b(t) }; } static ignoreEmptySelectors(t) { return t.filter( (e) => !(e.type === n.SELECTOR && e.selector === "." && !e.prop) ); } static combinePathOptionParts(t) { if (t.length < 2) return t; const e = []; for (let r = 0; r < t.length; r += 1) { const s = t[r]; r < t.length - 1 && t[r + 1].type === n.PATH_OPTIONS && (s.options = t[r + 1].options, r++), e.push(s); } return e; } static convertToFunctionCallExpr(t, e) { var a; const r = $(e.parts), s = t; if ((r == null ? void 0 : r.type) === n.SELECTOR) { const h = r; h.selector === "." && ((a = h.prop) == null ? void 0 : a.type) === p.ID && (e.parts.pop(), s.id = h.prop.value); } return !e.parts.length && e.root && typeof e.root != "object" ? s.parent = e.root : s.object = e, s; } static isArrayFilterExpressionSimple(t) { return t.filter.type !== n.ARRAY_INDEX_FILTER_EXPR ? !1 : t.filter.indexes.elements.length <= 1; } static isSimplePathPart(t) { if (t.type === n.SELECTOR) { const e = t; return e.selector === "." && !!e.prop && e.prop.type !== p.PUNCT; } return t.type === n.ARRAY_FILTER_EXPR ? this.isArrayFilterExpressionSimple(t) : !1; } static isSimplePath(t) { return t.parts.every((e) => this.isSimplePathPart(e)); } static isRichPath(t) { return t.parts.length ? !this.isSimplePath(t) : !1; } static setPathTypeIfNotJSON(t, e) { const r = t; return t.inferredPathType !== f.JSON && (r.inferredPathType = e), r; } updatePathExpr(t) { var o, c; const e = t; e.parts.length > 1 && e.parts[0].type === n.PATH_OPTIONS && (e.options = e.parts[0].options, e.parts.shift()); const r = this.shouldPathBeConvertedAsBlock(e.parts); let s = $(e.parts), a; (s == null ? void 0 : s.type) === n.FUNCTION_CALL_EXPR && (a = e.parts.pop()), s = $(e.parts), (s == null ? void 0 : s.type) === n.PATH_OPTIONS && ((o = s.options) != null && o.toArray) && (e.returnAsArray = (c = s.options) == null ? void 0 : c.toArray, !s.options.item && !s.options.index ? e.parts.pop() : s.options.toArray = !1), e.parts = m.combinePathOptionParts(e.parts); let h = e; return a && (h = m.convertToFunctionCallExpr(a, e)), r ? (h = m.convertToBlockExpr(h), m.setPathTypeIfNotJSON(e, f.RICH)) : m.isRichPath(e) && m.setPathTypeIfNotJSON(e, f.RICH), h; } static parseBaseExprFromTemplate(t) { const e = new y(t); return new m(e).parseBaseExpr(); } } function H(i, t) { return i === p.STR ? N(String(t)) : String(t); } function T(i = []) { return { type: n.OBJECT_EXPR, props: i }; } function q(i) { return { type: n.OBJECT_PROP_EXPR, value: { type: n.SPREAD_EXPR, value: i } }; } function at(i) { return T([q(i)]); } function ht(i, t) { let e = i.find((r) => r.key === t); return e || (e = { type: n.OBJECT_PROP_EXPR, key: t, value: T() }, i.push(e)), e; } function ot(i, t) { for (let e = 0; e < t.length; e++) if (t[e] === void 0) throw new C( `Invalid mapping: missing a mapping at index#${e}; make sure to define all the mappings in order of indexes`, i.input, i.output ); } function pt(i, t, e, r) { const s = e.indexes.elements[0].value; if (t.value.type !== n.ARRAY_EXPR) { const h = []; h[s] = r ? i.inputExpr : t.value, t.value = { type: n.ARRAY_EXPR, elements: h }; } else t.value.elements[s] || (t.value.elements[s] = r ? i.inputExpr : T()); ot(i, t.value.elements); const a = t.value.elements[s]; if (!r && (a == null ? void 0 : a.type) !== n.OBJECT_EXPR) throw new C( "Invalid mapping: invalid array index mapping", i.input, i.output ); return a; } function G(i) { var t; return i.type === n.PATH && i.parts.length === 0 && ((t = i.root) == null ? void 0 : t.type) === n.OBJECT_EXPR; } function K(i, t, e = []) { return { type: n.PATH, root: t, pathType: i.pathType || f.UNKNOWN, inferredPathType: i.inferredPathType || f.UNKNOWN, parts: e, returnAsArray: !0 }; } function ut(i, t) { if (!(i != null && i.props) || i.type !== n.OBJECT_EXPR || !Array.isArray(i.props)) throw new C( "Invalid mapping: invalid array mapping", t.input, t.output );