@jmespath-community/jmespath 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,8 @@
1
1
  var Token;
2
2
  (function (Token) {
3
3
  Token["TOK_EOF"] = "EOF";
4
+ Token["TOK_VARIABLE"] = "Variable";
5
+ Token["TOK_ASSIGN"] = "Assign";
4
6
  Token["TOK_UNQUOTEDIDENTIFIER"] = "UnquotedIdentifier";
5
7
  Token["TOK_QUOTEDIDENTIFIER"] = "QuotedIdentifier";
6
8
  Token["TOK_RBRACKET"] = "Rbracket";
@@ -172,7 +174,6 @@ const basicTokens = {
172
174
  '.': Token.TOK_DOT,
173
175
  ':': Token.TOK_COLON,
174
176
  '@': Token.TOK_CURRENT,
175
- ['$']: Token.TOK_ROOT,
176
177
  ']': Token.TOK_RBRACKET,
177
178
  '{': Token.TOK_LBRACE,
178
179
  '}': Token.TOK_RBRACE,
@@ -227,6 +228,26 @@ class StreamLexer {
227
228
  });
228
229
  this._current += 1;
229
230
  }
231
+ else if (stream[this._current] === '$') {
232
+ start = this._current;
233
+ if (this._current + 1 < stream.length && isAlpha(stream[this._current + 1])) {
234
+ this._current += 1;
235
+ identifier = this.consumeUnquotedIdentifier(stream);
236
+ tokens.push({
237
+ start,
238
+ type: Token.TOK_VARIABLE,
239
+ value: identifier,
240
+ });
241
+ }
242
+ else {
243
+ tokens.push({
244
+ start: start,
245
+ type: Token.TOK_ROOT,
246
+ value: stream[this._current],
247
+ });
248
+ this._current += 1;
249
+ }
250
+ }
230
251
  else if (stream[this._current] === '-') {
231
252
  if (this._current + 1 < stream.length && isNum(stream[this._current + 1])) {
232
253
  const token = this.consumeNumber(stream);
@@ -371,11 +392,6 @@ class StreamLexer {
371
392
  this._current += 1;
372
393
  return { start: start, type: orElse, value: stream.slice(start, this._current) };
373
394
  }
374
- if (token === Token.TOK_EOF) {
375
- const error = new Error(`syntax: unknown incomplete token: ${stream[start]}`);
376
- error.name = 'LexerError';
377
- throw error;
378
- }
379
395
  return { start: start, type: token, value: stream[start] };
380
396
  }
381
397
  consumeOperator(stream) {
@@ -389,7 +405,7 @@ class StreamLexer {
389
405
  case '>':
390
406
  return this.consumeOrElse(stream, '=', Token.TOK_GT, Token.TOK_GTE);
391
407
  case '=':
392
- return this.consumeOrElse(stream, '=', Token.TOK_EOF, Token.TOK_EQ);
408
+ return this.consumeOrElse(stream, '=', Token.TOK_ASSIGN, Token.TOK_EQ);
393
409
  case '&':
394
410
  return this.consumeOrElse(stream, '&', Token.TOK_EXPREF, Token.TOK_AND);
395
411
  case '|':
@@ -447,6 +463,7 @@ class StreamLexer {
447
463
  return true;
448
464
  }
449
465
  if (numberLooking.includes(literalString[0])) {
466
+ // eslint-disable-next-line @typescript-eslint/naming-convention
450
467
  const [_, ok] = this.parseJSON(literalString);
451
468
  return ok;
452
469
  }
@@ -466,6 +483,7 @@ const Lexer = new StreamLexer();
466
483
 
467
484
  const bindingPower = {
468
485
  [Token.TOK_EOF]: 0,
486
+ [Token.TOK_VARIABLE]: 0,
469
487
  [Token.TOK_UNQUOTEDIDENTIFIER]: 0,
470
488
  [Token.TOK_QUOTEDIDENTIFIER]: 0,
471
489
  [Token.TOK_RBRACKET]: 0,
@@ -476,6 +494,7 @@ const bindingPower = {
476
494
  [Token.TOK_CURRENT]: 0,
477
495
  [Token.TOK_EXPREF]: 0,
478
496
  [Token.TOK_ROOT]: 0,
497
+ [Token.TOK_ASSIGN]: 1,
479
498
  [Token.TOK_PIPE]: 1,
480
499
  [Token.TOK_OR]: 2,
481
500
  [Token.TOK_AND]: 3,
@@ -506,7 +525,7 @@ class TokenParser {
506
525
  this.tokens = [];
507
526
  }
508
527
  parse(expression, options) {
509
- this.loadTokens(expression, options || { enable_legacy_literals: false, });
528
+ this.loadTokens(expression, options || { enable_legacy_literals: false });
510
529
  this.index = 0;
511
530
  const ast = this.expression(0);
512
531
  if (this.lookahead(0) !== Token.TOK_EOF) {
@@ -516,7 +535,10 @@ class TokenParser {
516
535
  return ast;
517
536
  }
518
537
  loadTokens(expression, options) {
519
- this.tokens = [...Lexer.tokenize(expression, options), { type: Token.TOK_EOF, value: '', start: expression.length }];
538
+ this.tokens = [
539
+ ...Lexer.tokenize(expression, options),
540
+ { type: Token.TOK_EOF, value: '', start: expression.length },
541
+ ];
520
542
  }
521
543
  expression(rbp) {
522
544
  const leftToken = this.lookaheadToken(0);
@@ -541,10 +563,18 @@ class TokenParser {
541
563
  }
542
564
  nud(token) {
543
565
  switch (token.type) {
566
+ case Token.TOK_VARIABLE:
567
+ return { type: 'Variable', name: token.value };
544
568
  case Token.TOK_LITERAL:
545
569
  return { type: 'Literal', value: token.value };
546
- case Token.TOK_UNQUOTEDIDENTIFIER:
547
- return { type: 'Field', name: token.value };
570
+ case Token.TOK_UNQUOTEDIDENTIFIER: {
571
+ if (TokenParser.isKeyword(token, 'let') && this.lookahead(0) === Token.TOK_VARIABLE) {
572
+ return this.parseLetExpression();
573
+ }
574
+ else {
575
+ return { type: 'Field', name: token.value };
576
+ }
577
+ }
548
578
  case Token.TOK_QUOTEDIDENTIFIER:
549
579
  if (this.lookahead(0) === Token.TOK_LPAREN) {
550
580
  throw new Error('Syntax error: quoted identifier not allowed for function names.');
@@ -652,22 +682,7 @@ class TokenParser {
652
682
  throw new Error('Syntax error: expected a Field node');
653
683
  }
654
684
  const name = left.name;
655
- const args = [];
656
- let expression;
657
- while (this.lookahead(0) !== Token.TOK_RPAREN) {
658
- if (this.lookahead(0) === Token.TOK_CURRENT) {
659
- expression = { type: Token.TOK_CURRENT };
660
- this.advance();
661
- }
662
- else {
663
- expression = this.expression(0);
664
- }
665
- if (this.lookahead(0) === Token.TOK_COMMA) {
666
- this.match(Token.TOK_COMMA);
667
- }
668
- args.push(expression);
669
- }
670
- this.match(Token.TOK_RPAREN);
685
+ const args = this.parseCommaSeparatedExpressionsUntilToken(Token.TOK_RPAREN);
671
686
  const node = { name, type: 'Function', children: args };
672
687
  return node;
673
688
  }
@@ -682,6 +697,15 @@ class TokenParser {
682
697
  const right = this.parseProjectionRHS(bindingPower.Flatten);
683
698
  return { type: 'Projection', left: leftNode, right };
684
699
  }
700
+ case Token.TOK_ASSIGN: {
701
+ const leftNode = left;
702
+ const right = this.expression(0);
703
+ return {
704
+ type: 'Binding',
705
+ variable: leftNode.name,
706
+ reference: right,
707
+ };
708
+ }
685
709
  case Token.TOK_EQ:
686
710
  case Token.TOK_NE:
687
711
  case Token.TOK_GT:
@@ -712,6 +736,9 @@ class TokenParser {
712
736
  return this.errorToken(this.lookaheadToken(0));
713
737
  }
714
738
  }
739
+ static isKeyword(token, keyword) {
740
+ return token.type === Token.TOK_UNQUOTEDIDENTIFIER && token.value === keyword;
741
+ }
715
742
  match(tokenType) {
716
743
  if (this.lookahead(0) === tokenType) {
717
744
  this.advance();
@@ -771,9 +798,46 @@ class TokenParser {
771
798
  current = this.lookaheadToken(0);
772
799
  }
773
800
  this.match(Token.TOK_RBRACKET);
774
- const [start = null, stop = null, step = null] = parts;
801
+ const [start, stop, step] = parts;
775
802
  return { type: 'Slice', start, stop, step };
776
803
  }
804
+ parseLetExpression() {
805
+ const separated = this.parseCommaSeparatedExpressionsUntilKeyword('in');
806
+ const expression = this.expression(0);
807
+ const bindings = separated.map(binding => binding);
808
+ return {
809
+ type: 'LetExpression',
810
+ bindings: bindings,
811
+ expression: expression,
812
+ };
813
+ }
814
+ parseCommaSeparatedExpressionsUntilKeyword(keyword) {
815
+ return this.parseCommaSeparatedExpressionsUntil(() => {
816
+ return TokenParser.isKeyword(this.lookaheadToken(0), keyword);
817
+ }, () => {
818
+ this.advance();
819
+ });
820
+ }
821
+ parseCommaSeparatedExpressionsUntilToken(token) {
822
+ return this.parseCommaSeparatedExpressionsUntil(() => {
823
+ return this.lookahead(0) === token;
824
+ }, () => {
825
+ return this.match(token);
826
+ });
827
+ }
828
+ parseCommaSeparatedExpressionsUntil(isEndToken, matchEndToken) {
829
+ const args = [];
830
+ let expression;
831
+ while (!isEndToken()) {
832
+ expression = this.expression(0);
833
+ if (this.lookahead(0) === Token.TOK_COMMA) {
834
+ this.match(Token.TOK_COMMA);
835
+ }
836
+ args.push(expression);
837
+ }
838
+ matchEndToken();
839
+ return args;
840
+ }
777
841
  parseComparator(left, comparator) {
778
842
  const right = this.expression(bindingPower[comparator]);
779
843
  return { type: 'Comparator', name: comparator, left, right };
@@ -887,14 +951,15 @@ class Text {
887
951
  if (leftCp[index] === rightCp[index]) {
888
952
  continue;
889
953
  }
890
- return (leftCp[index] - rightCp[index]) > 0 ? 1 : -1;
954
+ return leftCp[index] - rightCp[index] > 0 ? 1 : -1;
891
955
  }
892
- return (leftCp.length - rightCp.length) > 0 ? 1 : -1;
956
+ return leftCp.length - rightCp.length > 0 ? 1 : -1;
893
957
  }
894
958
  reverse() {
895
959
  return String.fromCodePoint(...this.codePoints.reverse());
896
960
  }
897
961
  get codePoints() {
962
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
898
963
  const array = [...this._text].map(s => s.codePointAt(0));
899
964
  return array;
900
965
  }
@@ -960,10 +1025,7 @@ const split = (subject, search, count) => {
960
1025
  return [subject];
961
1026
  }
962
1027
  const split = subject.split(search);
963
- return [
964
- ...split.slice(0, count),
965
- split.slice(count).join(search)
966
- ];
1028
+ return [...split.slice(0, count), split.slice(count).join(search)];
967
1029
  };
968
1030
  const trim = (subject, chars) => {
969
1031
  return trimLeft(trimRight(subject, chars), chars);
@@ -1097,11 +1159,6 @@ class Runtime {
1097
1159
  }
1098
1160
  return Object.keys(inputValue).length;
1099
1161
  };
1100
- this.functionLet = ([inputScope, exprefNode]) => {
1101
- var _a;
1102
- const interpreter = (_a = this._interpreter) === null || _a === void 0 ? void 0 : _a.withScope(inputScope);
1103
- return interpreter.visit(exprefNode, exprefNode.context);
1104
- };
1105
1162
  this.functionLower = ([subject]) => {
1106
1163
  return lower(subject);
1107
1164
  };
@@ -1224,8 +1281,7 @@ class Runtime {
1224
1281
  this.functionReverse = ([inputValue]) => {
1225
1282
  const typeName = this.getTypeName(inputValue);
1226
1283
  if (typeName === InputArgument.TYPE_STRING) {
1227
- return new Text(inputValue)
1228
- .reverse();
1284
+ return new Text(inputValue).reverse();
1229
1285
  }
1230
1286
  const reversedArray = inputValue.slice(0);
1231
1287
  reversedArray.reverse();
@@ -1476,10 +1532,6 @@ class Runtime {
1476
1532
  },
1477
1533
  ],
1478
1534
  },
1479
- let: {
1480
- _func: this.functionLet,
1481
- _signature: [{ types: [InputArgument.TYPE_OBJECT] }, { types: [InputArgument.TYPE_EXPREF] }],
1482
- },
1483
1535
  length: {
1484
1536
  _func: this.functionLength,
1485
1537
  _signature: [
@@ -1921,7 +1973,7 @@ class ScopeChain {
1921
1973
  getValue(identifier) {
1922
1974
  if (identifier in this.data) {
1923
1975
  const result = this.data[identifier];
1924
- if (result) {
1976
+ if (result !== null && result !== undefined) {
1925
1977
  return result;
1926
1978
  }
1927
1979
  }
@@ -1935,14 +1987,13 @@ class ScopeChain {
1935
1987
  class TreeInterpreter$1 {
1936
1988
  constructor() {
1937
1989
  this._rootValue = null;
1938
- this._scope = undefined;
1939
1990
  this.runtime = new Runtime(this);
1991
+ this._scope = new ScopeChain();
1940
1992
  }
1941
1993
  withScope(scope) {
1942
- var _a;
1943
1994
  const interpreter = new TreeInterpreter$1();
1944
1995
  interpreter._rootValue = this._rootValue;
1945
- interpreter._scope = (_a = this._scope) === null || _a === void 0 ? void 0 : _a.withScope(scope);
1996
+ interpreter._scope = this._scope.withScope(scope);
1946
1997
  return interpreter;
1947
1998
  }
1948
1999
  search(node, value) {
@@ -1951,7 +2002,7 @@ class TreeInterpreter$1 {
1951
2002
  return this.visit(node, value);
1952
2003
  }
1953
2004
  visit(node, value) {
1954
- var _a, _b, _c, _d;
2005
+ var _a, _b, _c;
1955
2006
  switch (node.type) {
1956
2007
  case 'Field':
1957
2008
  const identifier = node.name;
@@ -1959,22 +2010,41 @@ class TreeInterpreter$1 {
1959
2010
  if (value !== null && typeof value === 'object' && !Array.isArray(value)) {
1960
2011
  result = (_a = value[identifier]) !== null && _a !== void 0 ? _a : null;
1961
2012
  }
1962
- if (result == null) {
1963
- result = (_c = (_b = this._scope) === null || _b === void 0 ? void 0 : _b.getValue(identifier)) !== null && _c !== void 0 ? _c : null;
2013
+ return result;
2014
+ case 'LetExpression': {
2015
+ const { bindings, expression } = node;
2016
+ let scope = {};
2017
+ bindings.forEach(binding => {
2018
+ const reference = this.visit(binding, value);
2019
+ scope = Object.assign(Object.assign({}, scope), reference);
2020
+ });
2021
+ return this.withScope(scope).visit(expression, value);
2022
+ }
2023
+ case 'Binding': {
2024
+ const { variable, reference } = node;
2025
+ const result = this.visit(reference, value);
2026
+ return { [variable]: result };
2027
+ }
2028
+ case 'Variable': {
2029
+ const variable = node.name;
2030
+ const result = (_b = this._scope.getValue(variable)) !== null && _b !== void 0 ? _b : null;
2031
+ if (result === null || result === undefined) {
2032
+ throw new Error(`Error referencing undefined variable ${variable}`);
1964
2033
  }
1965
2034
  return result;
2035
+ }
1966
2036
  case 'IndexExpression':
1967
2037
  return this.visit(node.right, this.visit(node.left, value));
1968
2038
  case 'Subexpression': {
1969
2039
  const result = this.visit(node.left, value);
1970
- return result != null && this.visit(node.right, result) || null;
2040
+ return (result != null && this.visit(node.right, result)) || null;
1971
2041
  }
1972
2042
  case 'Index': {
1973
2043
  if (!Array.isArray(value)) {
1974
2044
  return null;
1975
2045
  }
1976
2046
  const index = node.value < 0 ? value.length + node.value : node.value;
1977
- return (_d = value[index]) !== null && _d !== void 0 ? _d : null;
2047
+ return (_c = value[index]) !== null && _c !== void 0 ? _c : null;
1978
2048
  }
1979
2049
  case 'Slice': {
1980
2050
  if (!Array.isArray(value) && typeof value !== 'string') {
@@ -2164,7 +2234,7 @@ class TreeInterpreter$1 {
2164
2234
  return this.runtime.callFunction(node.name, args);
2165
2235
  }
2166
2236
  case 'ExpressionReference':
2167
- return Object.assign({ expref: true, context: value }, node.child);
2237
+ return Object.assign({ expref: true }, node.child);
2168
2238
  case 'Current':
2169
2239
  case 'Identity':
2170
2240
  return value;
@@ -1,2 +1,2 @@
1
- var t;!function(t){t.TOK_EOF="EOF",t.TOK_UNQUOTEDIDENTIFIER="UnquotedIdentifier",t.TOK_QUOTEDIDENTIFIER="QuotedIdentifier",t.TOK_RBRACKET="Rbracket",t.TOK_RPAREN="Rparen",t.TOK_COMMA="Comma",t.TOK_COLON="Colon",t.TOK_RBRACE="Rbrace",t.TOK_NUMBER="Number",t.TOK_CURRENT="Current",t.TOK_ROOT="Root",t.TOK_EXPREF="Expref",t.TOK_PIPE="Pipe",t.TOK_OR="Or",t.TOK_AND="And",t.TOK_EQ="EQ",t.TOK_GT="GT",t.TOK_LT="LT",t.TOK_GTE="GTE",t.TOK_LTE="LTE",t.TOK_NE="NE",t.TOK_PLUS="Plus",t.TOK_MINUS="Minus",t.TOK_MULTIPLY="Multiply",t.TOK_DIVIDE="Divide",t.TOK_MODULO="Modulo",t.TOK_DIV="Div",t.TOK_FLATTEN="Flatten",t.TOK_STAR="Star",t.TOK_FILTER="Filter",t.TOK_DOT="Dot",t.TOK_NOT="Not",t.TOK_LBRACE="Lbrace",t.TOK_LBRACKET="Lbracket",t.TOK_LPAREN="Lparen",t.TOK_LITERAL="Literal"}(t||(t={}));const e=t=>null!==t&&"[object Object]"===Object.prototype.toString.call(t),r=(t,n)=>{if(t===n)return!0;if(typeof t!=typeof n)return!1;if(Array.isArray(t)&&Array.isArray(n)){if(t.length!==n.length)return!1;for(let e=0;e<t.length;e+=1)if(!r(t[e],n[e]))return!1;return!0}if(e(t)&&e(n)){const e=Object.entries(t),i=new Set(Object.keys(n));if(e.length!==i.size)return!1;for(const[t,s]of e){if(!r(s,n[t]))return!1;i.delete(t)}return 0===i.size}return!1},n=t=>{if("object"==typeof t){if(null===t)return!0;if(Array.isArray(t))return 0===t.length;for(const e in t)return!1;return!0}return!("number"==typeof t||t)},i=t=>t>="0"&&t<="9"||"-"===t,s=t=>{if("number"!=typeof t||Math.floor(t)!==t)throw new Error("invalid-value: expecting an integer.");return t},o=t=>{if("number"!=typeof t||t<0||Math.floor(t)!==t)throw new Error("invalid-value: expecting a non-negative integer.");return t},a=(...t)=>{for(let e=0;e<t.length;e++){if(null===t[e]||void 0===t[e])throw new Error("not-a-number: undefined");if("number"!=typeof t[e])throw new Error("not-a-number")}},u=t=>{if(!(t=+t))throw new Error("not-a-number: divide by zero");return t},c={"(":t.TOK_LPAREN,")":t.TOK_RPAREN,"*":t.TOK_STAR,",":t.TOK_COMMA,".":t.TOK_DOT,":":t.TOK_COLON,"@":t.TOK_CURRENT,$:t.TOK_ROOT,"]":t.TOK_RBRACKET,"{":t.TOK_LBRACE,"}":t.TOK_RBRACE,"+":t.TOK_PLUS,"%":t.TOK_MODULO,"−":t.TOK_MINUS,"×":t.TOK_MULTIPLY,"÷":t.TOK_DIVIDE},h={"!":!0,"<":!0,"=":!0,">":!0,"&":!0,"|":!0,"/":!0},T={"\t":!0,"\n":!0,"\r":!0," ":!0};const _=new class{constructor(){this._current=0,this._enable_legacy_literals=!1}tokenize(e,r){const n=[];let s,o,a;for(this._current=0,this._enable_legacy_literals=(null==r?void 0:r.enable_legacy_literals)||!1;this._current<e.length;)if((u=e[this._current])>="a"&&u<="z"||u>="A"&&u<="Z"||"_"===u)s=this._current,o=this.consumeUnquotedIdentifier(e),n.push({start:s,type:t.TOK_UNQUOTEDIDENTIFIER,value:o});else if(void 0!==c[e[this._current]])n.push({start:this._current,type:c[e[this._current]],value:e[this._current]}),this._current+=1;else if("-"===e[this._current])if(this._current+1<e.length&&i(e[this._current+1])){const t=this.consumeNumber(e);t&&n.push(t)}else{const e={start:this._current,type:t.TOK_MINUS,value:"-"};n.push(e),this._current+=1}else if(i(e[this._current]))a=this.consumeNumber(e),n.push(a);else if("["===e[this._current])a=this.consumeLBracket(e),n.push(a);else if('"'===e[this._current])s=this._current,o=this.consumeQuotedIdentifier(e),n.push({start:s,type:t.TOK_QUOTEDIDENTIFIER,value:o});else if("'"===e[this._current])s=this._current,o=this.consumeRawStringLiteral(e),n.push({start:s,type:t.TOK_LITERAL,value:o});else if("`"===e[this._current]){s=this._current;const r=this.consumeLiteral(e);n.push({start:s,type:t.TOK_LITERAL,value:r})}else if(void 0!==h[e[this._current]])a=this.consumeOperator(e),a&&n.push(a);else{if(void 0===T[e[this._current]]){const t=new Error(`Syntax error: unknown character: ${e[this._current]}`);throw t.name="LexerError",t}this._current+=1}var u;return n}consumeUnquotedIdentifier(t){const e=this._current;for(this._current+=1;this._current<t.length&&((r=t[this._current])>="a"&&r<="z"||r>="A"&&r<="Z"||r>="0"&&r<="9"||"_"===r);)this._current+=1;var r;return t.slice(e,this._current)}consumeQuotedIdentifier(t){const e=this._current;this._current+=1;const r=t.length;for(;'"'!==t[this._current]&&this._current<r;){let e=this._current;"\\"!==t[e]||"\\"!==t[e+1]&&'"'!==t[e+1]?e+=1:e+=2,this._current=e}this._current+=1;const[n,i]=this.parseJSON(t.slice(e,this._current));if(!i){const t=new Error("syntax: unexpected end of JSON input");throw t.name="LexerError",t}return n}consumeRawStringLiteral(t){const e=this._current;this._current+=1;const r=t.length;for(;"'"!==t[this._current]&&this._current<r;){let e=this._current;"\\"!==t[e]||"\\"!==t[e+1]&&"'"!==t[e+1]?e+=1:e+=2,this._current=e}this._current+=1;return t.slice(e+1,this._current-1).replace("\\'","'").replace("\\\\","\\")}consumeNumber(e){const r=this._current;this._current+=1;const n=e.length;for(;i(e[this._current])&&this._current<n;)this._current+=1;return{start:r,value:parseInt(e.slice(r,this._current),10),type:t.TOK_NUMBER}}consumeLBracket(e){const r=this._current;return this._current+=1,"?"===e[this._current]?(this._current+=1,{start:r,type:t.TOK_FILTER,value:"[?"}):"]"===e[this._current]?(this._current+=1,{start:r,type:t.TOK_FLATTEN,value:"[]"}):{start:r,type:t.TOK_LBRACKET,value:"["}}consumeOrElse(e,r,n,i){const s=this._current;if(this._current+=1,this._current<e.length&&e[this._current]===r)return this._current+=1,{start:s,type:i,value:e.slice(s,this._current)};if(n===t.TOK_EOF){const t=new Error(`syntax: unknown incomplete token: ${e[s]}`);throw t.name="LexerError",t}return{start:s,type:n,value:e[s]}}consumeOperator(e){switch(e[this._current]){case"!":return this.consumeOrElse(e,"=",t.TOK_NOT,t.TOK_NE);case"<":return this.consumeOrElse(e,"=",t.TOK_LT,t.TOK_LTE);case">":return this.consumeOrElse(e,"=",t.TOK_GT,t.TOK_GTE);case"=":return this.consumeOrElse(e,"=",t.TOK_EOF,t.TOK_EQ);case"&":return this.consumeOrElse(e,"&",t.TOK_EXPREF,t.TOK_AND);case"|":return this.consumeOrElse(e,"|",t.TOK_PIPE,t.TOK_OR);case"/":return this.consumeOrElse(e,"/",t.TOK_DIVIDE,t.TOK_DIV)}}consumeLiteral(t){this._current+=1;const e=this._current,r=t.length;for(;"`"!==t[this._current]&&this._current<r;){let e=this._current;"\\"!==t[e]||"\\"!==t[e+1]&&"`"!==t[e+1]?e+=1:e+=2,this._current=e}let n=t.slice(e,this._current).trimStart();n=n.replace("\\`","`");let i=null,s=!1;if(this.looksLikeJSON(n)&&([i,s]=this.parseJSON(n)),!s&&this._enable_legacy_literals&&([i,s]=this.parseJSON(`"${n}"`)),!s){const e=new Error(`Syntax error: unexpected end of JSON input or invalid format for a JSON literal: ${t[this._current]}`);throw e.name="LexerError",e}return this._current+=1,i}looksLikeJSON(t){if(""===t)return!1;if('[{"'.includes(t[0]))return!0;if(["true","false","null"].includes(t))return!0;if("-0123456789".includes(t[0])){const[e,r]=this.parseJSON(t);return r}return!1}parseJSON(t){try{return[JSON.parse(t),!0]}catch(t){return[null,!1]}}},l={[t.TOK_EOF]:0,[t.TOK_UNQUOTEDIDENTIFIER]:0,[t.TOK_QUOTEDIDENTIFIER]:0,[t.TOK_RBRACKET]:0,[t.TOK_RPAREN]:0,[t.TOK_COMMA]:0,[t.TOK_RBRACE]:0,[t.TOK_NUMBER]:0,[t.TOK_CURRENT]:0,[t.TOK_EXPREF]:0,[t.TOK_ROOT]:0,[t.TOK_PIPE]:1,[t.TOK_OR]:2,[t.TOK_AND]:3,[t.TOK_EQ]:5,[t.TOK_GT]:5,[t.TOK_LT]:5,[t.TOK_GTE]:5,[t.TOK_LTE]:5,[t.TOK_NE]:5,[t.TOK_MINUS]:6,[t.TOK_PLUS]:6,[t.TOK_DIV]:7,[t.TOK_DIVIDE]:7,[t.TOK_MODULO]:7,[t.TOK_MULTIPLY]:7,[t.TOK_FLATTEN]:9,[t.TOK_STAR]:20,[t.TOK_FILTER]:21,[t.TOK_DOT]:40,[t.TOK_NOT]:45,[t.TOK_LBRACE]:50,[t.TOK_LBRACKET]:55,[t.TOK_LPAREN]:60};const E=new class{constructor(){this.index=0,this.tokens=[]}parse(e,r){this.loadTokens(e,r||{enable_legacy_literals:!1}),this.index=0;const n=this.expression(0);if(this.lookahead(0)!==t.TOK_EOF){const t=this.lookaheadToken(0);this.errorToken(t,`Syntax error: unexpected token type: ${t.type}, value: ${t.value}`)}return n}loadTokens(e,r){this.tokens=[..._.tokenize(e,r),{type:t.TOK_EOF,value:"",start:e.length}]}expression(t){const e=this.lookaheadToken(0);this.advance();let r=this.nud(e),n=this.lookahead(0);for(;t<l[n];)this.advance(),r=this.led(n,r),n=this.lookahead(0);return r}lookahead(t){return this.tokens[this.index+t].type}lookaheadToken(t){return this.tokens[this.index+t]}advance(){this.index+=1}nud(e){switch(e.type){case t.TOK_LITERAL:return{type:"Literal",value:e.value};case t.TOK_UNQUOTEDIDENTIFIER:return{type:"Field",name:e.value};case t.TOK_QUOTEDIDENTIFIER:if(this.lookahead(0)===t.TOK_LPAREN)throw new Error("Syntax error: quoted identifier not allowed for function names.");return{type:"Field",name:e.value};case t.TOK_NOT:return{type:"NotExpression",child:this.expression(l.Not)};case t.TOK_MINUS:{const t=this.expression(l.Minus);return{type:"Unary",operator:e.type,operand:t}}case t.TOK_PLUS:{const t=this.expression(l.Plus);return{type:"Unary",operator:e.type,operand:t}}case t.TOK_STAR:{const e={type:"Identity"};return{type:"ValueProjection",left:e,right:this.lookahead(0)===t.TOK_RBRACKET?e:this.parseProjectionRHS(l.Star)}}case t.TOK_FILTER:return this.led(e.type,{type:"Identity"});case t.TOK_LBRACE:return this.parseMultiselectHash();case t.TOK_FLATTEN:return{type:"Projection",left:{type:"Flatten",child:{type:"Identity"}},right:this.parseProjectionRHS(l.Flatten)};case t.TOK_LBRACKET:if(this.lookahead(0)===t.TOK_NUMBER||this.lookahead(0)===t.TOK_COLON){const t=this.parseIndexExpression();return this.projectIfSlice({type:"Identity"},t)}if(this.lookahead(0)===t.TOK_STAR&&this.lookahead(1)===t.TOK_RBRACKET){this.advance(),this.advance();return{left:{type:"Identity"},right:this.parseProjectionRHS(l.Star),type:"Projection"}}return this.parseMultiselectList();case t.TOK_CURRENT:return{type:t.TOK_CURRENT};case t.TOK_ROOT:return{type:t.TOK_ROOT};case t.TOK_EXPREF:return{type:"ExpressionReference",child:this.expression(l.Expref)};case t.TOK_LPAREN:{const e=[];for(;this.lookahead(0)!==t.TOK_RPAREN;){let r;this.lookahead(0)===t.TOK_CURRENT?(r={type:t.TOK_CURRENT},this.advance()):r=this.expression(0),e.push(r)}return this.match(t.TOK_RPAREN),e[0]}default:this.errorToken(e)}}led(e,r){switch(e){case t.TOK_DOT:{const e=l.Dot;if(this.lookahead(0)!==t.TOK_STAR){return{type:"Subexpression",left:r,right:this.parseDotRHS(e)}}this.advance();return{type:"ValueProjection",left:r,right:this.parseProjectionRHS(e)}}case t.TOK_PIPE:return{type:"Pipe",left:r,right:this.expression(l.Pipe)};case t.TOK_OR:return{type:"OrExpression",left:r,right:this.expression(l.Or)};case t.TOK_AND:return{type:"AndExpression",left:r,right:this.expression(l.And)};case t.TOK_LPAREN:{if("Field"!==r.type)throw new Error("Syntax error: expected a Field node");const e=r.name,n=[];let i;for(;this.lookahead(0)!==t.TOK_RPAREN;)this.lookahead(0)===t.TOK_CURRENT?(i={type:t.TOK_CURRENT},this.advance()):i=this.expression(0),this.lookahead(0)===t.TOK_COMMA&&this.match(t.TOK_COMMA),n.push(i);this.match(t.TOK_RPAREN);return{name:e,type:"Function",children:n}}case t.TOK_FILTER:{const e=this.expression(0);this.match(t.TOK_RBRACKET);return{type:"FilterProjection",left:r,right:this.lookahead(0)===t.TOK_FLATTEN?{type:"Identity"}:this.parseProjectionRHS(l.Filter),condition:e}}case t.TOK_FLATTEN:return{type:"Projection",left:{type:"Flatten",child:r},right:this.parseProjectionRHS(l.Flatten)};case t.TOK_EQ:case t.TOK_NE:case t.TOK_GT:case t.TOK_GTE:case t.TOK_LT:case t.TOK_LTE:return this.parseComparator(r,e);case t.TOK_PLUS:case t.TOK_MINUS:case t.TOK_MULTIPLY:case t.TOK_STAR:case t.TOK_DIVIDE:case t.TOK_MODULO:case t.TOK_DIV:return this.parseArithmetic(r,e);case t.TOK_LBRACKET:{const e=this.lookaheadToken(0);if(e.type===t.TOK_NUMBER||e.type===t.TOK_COLON){const t=this.parseIndexExpression();return this.projectIfSlice(r,t)}this.match(t.TOK_STAR),this.match(t.TOK_RBRACKET);return{type:"Projection",left:r,right:this.parseProjectionRHS(l.Star)}}default:return this.errorToken(this.lookaheadToken(0))}}match(t){if(this.lookahead(0)!==t){const e=this.lookaheadToken(0);this.errorToken(e,`Syntax error: expected ${t}, got: ${e.type}`)}else this.advance()}errorToken(t,e=""){const r=new Error(e||`Syntax error: invalid token (${t.type}): "${t.value}"`);throw r.name="ParserError",r}parseIndexExpression(){if(this.lookahead(0)===t.TOK_COLON||this.lookahead(1)===t.TOK_COLON)return this.parseSliceExpression();const e=Number(this.lookaheadToken(0).value);return this.advance(),this.match(t.TOK_RBRACKET),{type:"Index",value:e}}projectIfSlice(t,e){const r={type:"IndexExpression",left:t,right:e};return"Slice"===e.type?{left:r,right:this.parseProjectionRHS(l.Star),type:"Projection"}:r}parseSliceExpression(){const e=[null,null,null];let r=0,n=this.lookaheadToken(0);for(;n.type!=t.TOK_RBRACKET&&r<3;){if(n.type===t.TOK_COLON)r++,3===r&&this.errorToken(this.lookaheadToken(0),"Syntax error, too many colons in slice expression"),this.advance();else if(n.type===t.TOK_NUMBER){const t=this.lookaheadToken(0).value;e[r]=t,this.advance()}else{const t=this.lookaheadToken(0);this.errorToken(t,`Syntax error, unexpected token: ${t.value}(${t.type})`)}n=this.lookaheadToken(0)}this.match(t.TOK_RBRACKET);const[i=null,s=null,o=null]=e;return{type:"Slice",start:i,stop:s,step:o}}parseComparator(t,e){return{type:"Comparator",name:e,left:t,right:this.expression(l[e])}}parseArithmetic(t,e){return{type:"Arithmetic",operator:e,left:t,right:this.expression(l[e])}}parseDotRHS(e){const r=this.lookahead(0);if([t.TOK_UNQUOTEDIDENTIFIER,t.TOK_QUOTEDIDENTIFIER,t.TOK_STAR].includes(r))return this.expression(e);if(r===t.TOK_LBRACKET)return this.match(t.TOK_LBRACKET),this.parseMultiselectList();if(r===t.TOK_LBRACE)return this.match(t.TOK_LBRACE),this.parseMultiselectHash();const n=this.lookaheadToken(0);this.errorToken(n,`Syntax error, unexpected token: ${n.value}(${n.type})`)}parseProjectionRHS(e){if(l[this.lookahead(0)]<10)return{type:"Identity"};if(this.lookahead(0)===t.TOK_LBRACKET)return this.expression(e);if(this.lookahead(0)===t.TOK_FILTER)return this.expression(e);if(this.lookahead(0)===t.TOK_DOT)return this.match(t.TOK_DOT),this.parseDotRHS(e);const r=this.lookaheadToken(0);this.errorToken(r,`Syntax error, unexpected token: ${r.value}(${r.type})`)}parseMultiselectList(){const e=[];for(;this.lookahead(0)!==t.TOK_RBRACKET;){const r=this.expression(0);if(e.push(r),this.lookahead(0)===t.TOK_COMMA&&(this.match(t.TOK_COMMA),this.lookahead(0)===t.TOK_RBRACKET))throw new Error("Syntax error: unexpected token Rbracket")}return this.match(t.TOK_RBRACKET),{type:"MultiSelectList",children:e}}parseMultiselectHash(){const e=[],r=[t.TOK_UNQUOTEDIDENTIFIER,t.TOK_QUOTEDIDENTIFIER];let n,i,s;for(;;){if(n=this.lookaheadToken(0),!r.includes(n.type))throw new Error(`Syntax error: expecting an identifier token, got: ${n.type}`);if(i=n.value,this.advance(),this.match(t.TOK_COLON),s=this.expression(0),e.push({value:s,type:"KeyValuePair",name:i}),this.lookahead(0)===t.TOK_COMMA)this.match(t.TOK_COMMA);else if(this.lookahead(0)===t.TOK_RBRACE){this.match(t.TOK_RBRACE);break}}return{type:"MultiSelectHash",children:e}}};class p{constructor(t){this._text=t}get string(){return this._text}get length(){return this.codePoints.length}compareTo(t){return p.compare(this,new p(t))}static get comparer(){return(t,e)=>new p(t).compareTo(e)}static compare(t,e){const r=t.codePoints,n=e.codePoints;for(let t=0;t<Math.min(r.length,n.length);t++)if(r[t]!==n[t])return r[t]-n[t]>0?1:-1;return r.length-n.length>0?1:-1}reverse(){return String.fromCodePoint(...this.codePoints.reverse())}get codePoints(){return[...this._text].map((t=>t.codePointAt(0)))}}const R=(t,e,r)=>{if((r=r||" ").length>1)throw new Error(`invalid value, ${t} expects its 'pad' parameter to be a valid string with a single codepoint`);return o(e),r},f=(t,e)=>y(t,(t=>new RegExp(`^[${t}]*(.*?)`)),e),O=(t,e)=>y(t,(t=>new RegExp(`(.*?)[${t}]*$`)),e),y=(t,e,r)=>{const n=r?r.replace(/[-/\\^$*+?.()|[\]{}]/g,"\\$&"):"\\s…";return t.replace(e(n),"$1")};var A;!function(t){t[t.TYPE_NUMBER=0]="TYPE_NUMBER",t[t.TYPE_ANY=1]="TYPE_ANY",t[t.TYPE_STRING=2]="TYPE_STRING",t[t.TYPE_ARRAY=3]="TYPE_ARRAY",t[t.TYPE_OBJECT=4]="TYPE_OBJECT",t[t.TYPE_BOOLEAN=5]="TYPE_BOOLEAN",t[t.TYPE_EXPREF=6]="TYPE_EXPREF",t[t.TYPE_NULL=7]="TYPE_NULL",t[t.TYPE_ARRAY_NUMBER=8]="TYPE_ARRAY_NUMBER",t[t.TYPE_ARRAY_STRING=9]="TYPE_ARRAY_STRING",t[t.TYPE_ARRAY_OBJECT=10]="TYPE_ARRAY_OBJECT",t[t.TYPE_ARRAY_ARRAY=11]="TYPE_ARRAY_ARRAY"}(A||(A={}));class d{constructor(t){this.TYPE_NAME_TABLE={[A.TYPE_NUMBER]:"number",[A.TYPE_ANY]:"any",[A.TYPE_STRING]:"string",[A.TYPE_ARRAY]:"array",[A.TYPE_OBJECT]:"object",[A.TYPE_BOOLEAN]:"boolean",[A.TYPE_EXPREF]:"expression",[A.TYPE_NULL]:"null",[A.TYPE_ARRAY_NUMBER]:"Array<number>",[A.TYPE_ARRAY_OBJECT]:"Array<object>",[A.TYPE_ARRAY_STRING]:"Array<string>",[A.TYPE_ARRAY_ARRAY]:"Array<Array<any>>"},this.functionAbs=([t])=>Math.abs(t),this.functionAvg=([t])=>{if(!t||0==t.length)return null;let e=0;for(let r=0;r<t.length;r+=1)e+=t[r];return e/t.length},this.functionCeil=([t])=>Math.ceil(t),this.functionContains=([t,e])=>{if(Array.isArray(t)){return t.includes(e)}if("string"==typeof t){if("string"==typeof e)return t.includes(e)}return null},this.functionEndsWith=t=>{const[e,r]=t;return e.includes(r,e.length-r.length)},this.functionFindFirst=t=>((t,e,r,n)=>{if(!t||!e)return null;r=Math.max(s(r=r||0),0),n=Math.min(s(n=n||t.length),t.length);const i=t.slice(r,n).indexOf(e);return-1===i?null:i+r})(t[0],t[1],t.length>2&&t[2]||void 0,t.length>3&&t[3]||void 0),this.functionFindLast=t=>((t,e,r,n)=>{if(!t||!e)return null;r=Math.max(s(r=r||0),0),n=Math.min(s(n=n||t.length),t.length);const i=t.slice(r,n).lastIndexOf(e);return-1===i?null:i+r})(t[0],t[1],t.length>2&&t[2]||void 0,t.length>3&&t[3]||void 0),this.functionFloor=([t])=>Math.floor(t),this.functionFromItems=([t])=>(t.map((t=>{if(2!=t.length||"string"!=typeof t[0])throw new Error("invalid value, each array must contain two elements, a pair of string and value")})),Object.fromEntries(t)),this.functionGroupBy=([t,e])=>{const r=this.createKeyFunction(e,[A.TYPE_STRING]);return t.reduce(((t,e)=>{const n=r(null!=e?e:{});return(t[n]=t[n]||[]).push(e),t}),{})},this.functionItems=([t])=>Object.entries(t),this.functionJoin=t=>{const[e,r]=t;return r.join(e)},this.functionKeys=([t])=>Object.keys(t),this.functionLength=([t])=>"string"==typeof t?new p(t).length:Array.isArray(t)?t.length:Object.keys(t).length,this.functionLet=([t,e])=>{var r;return(null===(r=this._interpreter)||void 0===r?void 0:r.withScope(t)).visit(e,e.context)},this.functionLower=([t])=>(t=>t.toLowerCase())(t),this.functionMap=([t,e])=>{if(!this._interpreter)return[];const r=[],n=this._interpreter;for(let i=0;i<e.length;i+=1)r.push(n.visit(t,e[i]));return r},this.functionMax=([t])=>{if(!t.length)return null;if(this.getTypeName(t[0])===A.TYPE_NUMBER)return Math.max(...t);const e=t;let r=e[0];for(let t=1;t<e.length;t+=1)r.localeCompare(e[t])<0&&(r=e[t]);return r},this.functionMaxBy=t=>{const e=t[1],r=t[0],n=this.createKeyFunction(e,[A.TYPE_NUMBER,A.TYPE_STRING]);let i,s,o=-1/0;for(let t=0;t<r.length;t+=1)s=n&&n(r[t]),void 0!==s&&s>o&&(o=s,i=r[t]);return i||null},this.functionMerge=t=>{let e={};for(let r=0;r<t.length;r+=1){const n=t[r];e=Object.assign(e,n)}return e},this.functionMin=([t])=>{if(!t.length)return null;if(this.getTypeName(t[0])===A.TYPE_NUMBER)return Math.min(...t);const e=t;let r=e[0];for(let t=1;t<e.length;t+=1)e[t].localeCompare(r)<0&&(r=e[t]);return r},this.functionMinBy=t=>{const e=t[1],r=t[0],n=this.createKeyFunction(e,[A.TYPE_NUMBER,A.TYPE_STRING]);let i,s,o=1/0;for(let t=0;t<r.length;t+=1)s=n&&n(r[t]),void 0!==s&&s<o&&(o=s,i=r[t]);return i||null},this.functionNotNull=t=>{for(let e=0;e<t.length;e+=1)if(this.getTypeName(t[e])!==A.TYPE_NULL)return t[e];return null},this.functionPadLeft=t=>((t,e,r)=>(r=R("pad_left",e,r),t&&t.padStart(e,r)||""))(t[0],t[1],t.length>2&&t[2]||void 0),this.functionPadRight=t=>((t,e,r)=>(r=R("pad_right",e,r),t&&t.padEnd(e,r)||""))(t[0],t[1],t.length>2&&t[2]||void 0),this.functionReplace=t=>((t,e,r,n)=>0===n?t:n?(o(n),[...Array(n).keys()].map((()=>t=t.replace(e,r))),t):t.split(e).join(r))(t[0],t[1],t[2],t.length>3?t[3]:void 0),this.functionSplit=t=>((t,e,r)=>{if(0==t.length&&0===e.length)return[];if(null==r)return t.split(e);if(o(r),0===r)return[t];const n=t.split(e);return[...n.slice(0,r),n.slice(r).join(e)]})(t[0],t[1],t.length>2?t[2]:void 0),this.functionReverse=([t])=>{if(this.getTypeName(t)===A.TYPE_STRING)return new p(t).reverse();const e=t.slice(0);return e.reverse(),e},this.functionSort=([t])=>0==t.length?t:"string"==typeof t[0]?[...t].sort(p.comparer):[...t].sort(),this.functionSortBy=t=>{const e=t[0].slice(0);if(0===e.length)return e;const r=this._interpreter,n=t[1],i=this.getTypeName(r.visit(n,e[0]));if(void 0!==i&&![A.TYPE_NUMBER,A.TYPE_STRING].includes(i))throw new Error(`Invalid type: unexpected type (${this.TYPE_NAME_TABLE[i]})`);function s(t,e){throw new Error(`Invalid type: expected (${t.TYPE_NAME_TABLE[i]}), received ${t.TYPE_NAME_TABLE[t.getTypeName(e)]}`)}return e.sort(((t,e)=>{const o=r.visit(n,t),a=r.visit(n,e);return this.getTypeName(o)!==i?s(this,o):this.getTypeName(a)!==i&&s(this,a),i===A.TYPE_STRING?p.comparer(o,a):o-a}))},this.functionStartsWith=([t,e])=>t.startsWith(e),this.functionSum=([t])=>t.reduce(((t,e)=>t+e),0),this.functionToArray=([t])=>this.getTypeName(t)===A.TYPE_ARRAY?t:[t],this.functionToNumber=([t])=>{const e=this.getTypeName(t);let r;return e===A.TYPE_NUMBER?t:e!==A.TYPE_STRING||(r=+t,isNaN(r))?null:r},this.functionToString=([t])=>this.getTypeName(t)===A.TYPE_STRING?t:JSON.stringify(t),this.functionTrim=t=>((t,e)=>f(O(t,e),e))(t[0],t.length>1?t[1]:void 0),this.functionTrimLeft=t=>{const e=t[0];return f(e,t.length>1?t[1]:void 0)},this.functionTrimRight=t=>{const e=t[0];return O(e,t.length>1?t[1]:void 0)},this.functionType=([t])=>{switch(this.getTypeName(t)){case A.TYPE_NUMBER:return"number";case A.TYPE_STRING:return"string";case A.TYPE_ARRAY:return"array";case A.TYPE_OBJECT:return"object";case A.TYPE_BOOLEAN:return"boolean";case A.TYPE_NULL:return"null";default:throw new Error("invalid-type")}},this.functionUpper=([t])=>(t=>t.toUpperCase())(t),this.functionValues=([t])=>Object.values(t),this.functionZip=t=>{const e=Math.min(...t.map((t=>t.length)));return Array(e).fill(null).map(((e,r)=>t.map((t=>t[r]))))},this.functionTable={abs:{_func:this.functionAbs,_signature:[{types:[A.TYPE_NUMBER]}]},avg:{_func:this.functionAvg,_signature:[{types:[A.TYPE_ARRAY_NUMBER]}]},ceil:{_func:this.functionCeil,_signature:[{types:[A.TYPE_NUMBER]}]},contains:{_func:this.functionContains,_signature:[{types:[A.TYPE_STRING,A.TYPE_ARRAY]},{types:[A.TYPE_ANY]}]},ends_with:{_func:this.functionEndsWith,_signature:[{types:[A.TYPE_STRING]},{types:[A.TYPE_STRING]}]},find_first:{_func:this.functionFindFirst,_signature:[{types:[A.TYPE_STRING]},{types:[A.TYPE_STRING]},{types:[A.TYPE_NUMBER],optional:!0},{types:[A.TYPE_NUMBER],optional:!0}]},find_last:{_func:this.functionFindLast,_signature:[{types:[A.TYPE_STRING]},{types:[A.TYPE_STRING]},{types:[A.TYPE_NUMBER],optional:!0},{types:[A.TYPE_NUMBER],optional:!0}]},floor:{_func:this.functionFloor,_signature:[{types:[A.TYPE_NUMBER]}]},from_items:{_func:this.functionFromItems,_signature:[{types:[A.TYPE_ARRAY_ARRAY]}]},group_by:{_func:this.functionGroupBy,_signature:[{types:[A.TYPE_ARRAY]},{types:[A.TYPE_EXPREF]}]},items:{_func:this.functionItems,_signature:[{types:[A.TYPE_OBJECT]}]},join:{_func:this.functionJoin,_signature:[{types:[A.TYPE_STRING]},{types:[A.TYPE_ARRAY_STRING]}]},keys:{_func:this.functionKeys,_signature:[{types:[A.TYPE_OBJECT]}]},let:{_func:this.functionLet,_signature:[{types:[A.TYPE_OBJECT]},{types:[A.TYPE_EXPREF]}]},length:{_func:this.functionLength,_signature:[{types:[A.TYPE_STRING,A.TYPE_ARRAY,A.TYPE_OBJECT]}]},lower:{_func:this.functionLower,_signature:[{types:[A.TYPE_STRING]}]},map:{_func:this.functionMap,_signature:[{types:[A.TYPE_EXPREF]},{types:[A.TYPE_ARRAY]}]},max:{_func:this.functionMax,_signature:[{types:[A.TYPE_ARRAY_NUMBER,A.TYPE_ARRAY_STRING]}]},max_by:{_func:this.functionMaxBy,_signature:[{types:[A.TYPE_ARRAY]},{types:[A.TYPE_EXPREF]}]},merge:{_func:this.functionMerge,_signature:[{types:[A.TYPE_OBJECT],variadic:!0}]},min:{_func:this.functionMin,_signature:[{types:[A.TYPE_ARRAY_NUMBER,A.TYPE_ARRAY_STRING]}]},min_by:{_func:this.functionMinBy,_signature:[{types:[A.TYPE_ARRAY]},{types:[A.TYPE_EXPREF]}]},not_null:{_func:this.functionNotNull,_signature:[{types:[A.TYPE_ANY],variadic:!0}]},pad_left:{_func:this.functionPadLeft,_signature:[{types:[A.TYPE_STRING]},{types:[A.TYPE_NUMBER]},{types:[A.TYPE_STRING],optional:!0}]},pad_right:{_func:this.functionPadRight,_signature:[{types:[A.TYPE_STRING]},{types:[A.TYPE_NUMBER]},{types:[A.TYPE_STRING],optional:!0}]},replace:{_func:this.functionReplace,_signature:[{types:[A.TYPE_STRING]},{types:[A.TYPE_STRING]},{types:[A.TYPE_STRING]},{types:[A.TYPE_NUMBER],optional:!0}]},split:{_func:this.functionSplit,_signature:[{types:[A.TYPE_STRING]},{types:[A.TYPE_STRING]},{types:[A.TYPE_NUMBER],optional:!0}]},reverse:{_func:this.functionReverse,_signature:[{types:[A.TYPE_STRING,A.TYPE_ARRAY]}]},sort:{_func:this.functionSort,_signature:[{types:[A.TYPE_ARRAY_STRING,A.TYPE_ARRAY_NUMBER]}]},sort_by:{_func:this.functionSortBy,_signature:[{types:[A.TYPE_ARRAY]},{types:[A.TYPE_EXPREF]}]},starts_with:{_func:this.functionStartsWith,_signature:[{types:[A.TYPE_STRING]},{types:[A.TYPE_STRING]}]},sum:{_func:this.functionSum,_signature:[{types:[A.TYPE_ARRAY_NUMBER]}]},to_array:{_func:this.functionToArray,_signature:[{types:[A.TYPE_ANY]}]},to_number:{_func:this.functionToNumber,_signature:[{types:[A.TYPE_ANY]}]},to_string:{_func:this.functionToString,_signature:[{types:[A.TYPE_ANY]}]},trim:{_func:this.functionTrim,_signature:[{types:[A.TYPE_STRING]},{types:[A.TYPE_STRING],optional:!0}]},trim_left:{_func:this.functionTrimLeft,_signature:[{types:[A.TYPE_STRING]},{types:[A.TYPE_STRING],optional:!0}]},trim_right:{_func:this.functionTrimRight,_signature:[{types:[A.TYPE_STRING]},{types:[A.TYPE_STRING],optional:!0}]},type:{_func:this.functionType,_signature:[{types:[A.TYPE_ANY]}]},upper:{_func:this.functionUpper,_signature:[{types:[A.TYPE_STRING]}]},values:{_func:this.functionValues,_signature:[{types:[A.TYPE_OBJECT]}]},zip:{_func:this.functionZip,_signature:[{types:[A.TYPE_ARRAY],variadic:!0}]}},this._interpreter=t}registerFunction(t,e,r){if(t in this.functionTable)throw new Error(`Function already defined: ${t}()`);this.functionTable[t]={_func:e.bind(this),_signature:r}}callFunction(t,e){const r=this.functionTable[t];if(void 0===r)throw new Error(`Unknown function: ${t}()`);return this.validateArgs(t,e,r._signature),r._func.call(this,e)}validateInputSignatures(t,e){for(let r=0;r<e.length;r+=1)if("variadic"in e[r]&&r!==e.length-1)throw new Error(`Invalid arity: ${t}() 'variadic' argument ${r+1} must occur last`)}validateArgs(t,e,r){var n,i;let s;this.validateInputSignatures(t,r);const o=r.filter((t=>{var e;return null!==(e=!t.optional)&&void 0!==e&&e})).length,a=null!==(i=null===(n=r[r.length-1])||void 0===n?void 0:n.variadic)&&void 0!==i&&i,u=e.length<o,c=e.length>r.length,h=u&&(!a&&o>1||a)?"at least ":"";if(a&&u||!a&&(u||c))throw s=r.length>1,new Error(`Invalid arity: ${t}() takes ${h}${o} argument${s?"s":""} but received ${e.length}`);let T,_,l;for(let n=0;n<r.length;n+=1){let i;for(l=!1,T=r[n].types,_=this.getTypeName(e[n]),i=0;i<T.length;i+=1)if(void 0!==_&&this.typeMatches(_,T[i],e[n])){l=!0;break}if(!l&&void 0!==_){const e=T.map((t=>this.TYPE_NAME_TABLE[t])).join(" | ");throw new Error(`Invalid type: ${t}() expected argument ${n+1} to be type (${e}) but received type ${this.TYPE_NAME_TABLE[_]} instead.`)}}}typeMatches(t,e,r){if(e===A.TYPE_ANY)return!0;if(e!==A.TYPE_ARRAY_STRING&&e!==A.TYPE_ARRAY_NUMBER&&e!==A.TYPE_ARRAY_OBJECT&&e!==A.TYPE_ARRAY_ARRAY&&e!==A.TYPE_ARRAY)return t===e;if(e===A.TYPE_ARRAY)return t===A.TYPE_ARRAY;if(t===A.TYPE_ARRAY){let t;e===A.TYPE_ARRAY_NUMBER?t=A.TYPE_NUMBER:e===A.TYPE_ARRAY_OBJECT?t=A.TYPE_OBJECT:e===A.TYPE_ARRAY_STRING?t=A.TYPE_STRING:e===A.TYPE_ARRAY_ARRAY&&(t=A.TYPE_ARRAY);const n=r;for(let e=0;e<n.length;e+=1){const r=this.getTypeName(n[e]);if(void 0!==r&&void 0!==t&&!this.typeMatches(r,t,n[e]))return!1}return!0}return!1}getTypeName(t){switch(Object.prototype.toString.call(t)){case"[object String]":return A.TYPE_STRING;case"[object Number]":return A.TYPE_NUMBER;case"[object Array]":return A.TYPE_ARRAY;case"[object Boolean]":return A.TYPE_BOOLEAN;case"[object Null]":return A.TYPE_NULL;case"[object Object]":return t.expref?A.TYPE_EXPREF:A.TYPE_OBJECT;default:return}}createKeyFunction(t,e){const r=this._interpreter;return n=>{const i=r.visit(t,n);if(!e.includes(this.getTypeName(i))){const t=`Invalid type: expected one of (${e.map((t=>this.TYPE_NAME_TABLE[t])).join(" | ")}), received ${this.TYPE_NAME_TABLE[this.getTypeName(i)]}`;throw new Error(t)}return i}}}class P{constructor(){this.inner=void 0,this.data={}}withScope(t){const e=new P;return e.inner=this,e.data=t,e}getValue(t){if(t in this.data){const e=this.data[t];if(e)return e}return this.inner?this.inner.getValue(t):null}}class Y{constructor(){this._rootValue=null,this._scope=void 0,this.runtime=new d(this)}withScope(t){var e;const r=new Y;return r._rootValue=this._rootValue,r._scope=null===(e=this._scope)||void 0===e?void 0:e.withScope(t),r}search(t,e){return this._rootValue=e,this._scope=new P,this.visit(t,e)}visit(e,i){var s,o,c,h,T,_;switch(e.type){case"Field":const l=e.name;let E=null;return null===i||"object"!=typeof i||Array.isArray(i)||(E=null!==(s=i[l])&&void 0!==s?s:null),null==E&&(E=null!==(c=null===(o=this._scope)||void 0===o?void 0:o.getValue(l))&&void 0!==c?c:null),E;case"IndexExpression":return this.visit(e.right,this.visit(e.left,i));case"Subexpression":{const t=this.visit(e.left,i);return null!=t&&this.visit(e.right,t)||null}case"Index":if(!Array.isArray(i))return null;return null!==(h=i[e.value<0?i.length+e.value:e.value])&&void 0!==h?h:null;case"Slice":{if(!Array.isArray(i)&&"string"!=typeof i)return null;const{start:t,stop:r,step:n}=this.computeSliceParams(i.length,e);if("string"==typeof i){const e=[...i];return this.slice(e,t,r,n).join("")}return this.slice(i,t,r,n)}case"Projection":{const{left:t,right:r}=e;let n=!1;"IndexExpression"===t.type&&"Slice"===t.right.type&&(n=!0);const s=this.visit(t,i);if(n&&"string"==typeof s)return s;if(!Array.isArray(s))return null;const o=[];for(const t of s){const e=this.visit(r,t);null!==e&&o.push(e)}return o}case"ValueProjection":{const{left:t,right:r}=e,n=this.visit(t,i);if(null===n||"object"!=typeof n||Array.isArray(n))return null;const s=[],o=Object.values(n);for(const t of o){const e=this.visit(r,t);null!==e&&s.push(e)}return s}case"FilterProjection":{const{left:t,right:r,condition:s}=e,o=this.visit(t,i);if(!Array.isArray(o))return null;const a=[];for(const t of o){const e=this.visit(s,t);if(n(e))continue;const i=this.visit(r,t);null!==i&&a.push(i)}return a}case"Arithmetic":{const r=this.visit(e.left,i),n=this.visit(e.right,i);switch(e.operator){case t.TOK_PLUS:return a(T=r,_=n),T+_;case t.TOK_MINUS:return((t,e)=>(a(t,e),t-e))(r,n);case t.TOK_MULTIPLY:case t.TOK_STAR:return((t,e)=>(a(t,e),t*e))(r,n);case t.TOK_DIVIDE:return((t,e)=>(a(t,e),t/u(e)))(r,n);case t.TOK_MODULO:return((t,e)=>(a(t,e),t%e))(r,n);case t.TOK_DIV:return((t,e)=>(a(t,e),Math.floor(t/u(e))))(r,n);default:throw new Error(`Syntax error: unknown arithmetic operator: ${e.operator}`)}}case"Unary":{const r=this.visit(e.operand,i);switch(e.operator){case t.TOK_PLUS:return a(r),r;case t.TOK_MINUS:return a(r),-r;default:throw new Error(`Syntax error: unknown arithmetic operator: ${e.operator}`)}}case"Comparator":{const t=this.visit(e.left,i),n=this.visit(e.right,i);switch(e.name){case"EQ":return r(t,n);case"NE":return!r(t,n)}if("number"!=typeof t||"number"!=typeof n)return null;switch(e.name){case"GT":return t>n;case"GTE":return t>=n;case"LT":return t<n;case"LTE":return t<=n}}case"Flatten":{const t=this.visit(e.child,i);return Array.isArray(t)?t.flat():null}case"Root":return this._rootValue;case"MultiSelectList":{const t=[];for(const r of e.children)t.push(this.visit(r,i));return t}case"MultiSelectHash":{const t={};for(const r of e.children)t[r.name]=this.visit(r.value,i);return t}case"OrExpression":{const t=this.visit(e.left,i);return n(t)?this.visit(e.right,i):t}case"AndExpression":{const t=this.visit(e.left,i);return n(t)?t:this.visit(e.right,i)}case"NotExpression":return n(this.visit(e.child,i));case"Literal":return e.value;case"Pipe":return this.visit(e.right,this.visit(e.left,i));case"Function":{const t=[];for(const r of e.children)t.push(this.visit(r,i));return this.runtime.callFunction(e.name,t)}case"ExpressionReference":return Object.assign({expref:!0,context:i},e.child);case"Current":case"Identity":return i}}computeSliceParams(t,e){let{start:r,stop:n,step:i}=e;if(null===i)i=1;else if(0===i){const t=new Error("Invalid value: slice step cannot be 0");throw t.name="RuntimeError",t}return r=null===r?i<0?t-1:0:this.capSliceRange(t,r,i),n=null===n?i<0?-1:t:this.capSliceRange(t,n,i),{start:r,stop:n,step:i}}capSliceRange(t,e,r){let n=e;return n<0?(n+=t,n<0&&(n=r<0?-1:0)):n>=t&&(n=r<0?t-1:t),n}slice(t,e,r,n){const i=[];if(n>0)for(let s=e;s<r;s+=n)i.push(t[s]);else for(let s=e;s>r;s+=n)i.push(t[s]);return i}}const N=new Y,g=A.TYPE_ANY,K=A.TYPE_ARRAY,m=A.TYPE_ARRAY_ARRAY,v=A.TYPE_ARRAY_NUMBER,I=A.TYPE_ARRAY_OBJECT,S=A.TYPE_ARRAY_STRING,L=A.TYPE_BOOLEAN,M=A.TYPE_EXPREF,B=A.TYPE_NULL,x=A.TYPE_NUMBER,U=A.TYPE_OBJECT,k=A.TYPE_STRING;function w(t,e){return E.parse(t,e)}function C(t,e){return _.tokenize(t,e)}const F=(t,e,r)=>{N.runtime.registerFunction(t,e,r)};function b(t,e,r){const n=E.parse(e,r);return N.search(n,t)}function G(){return new P}const D=N,j={compile:w,registerFunction:F,search:b,tokenize:C,TreeInterpreter:D,TYPE_ANY:g,TYPE_ARRAY_NUMBER:v,TYPE_ARRAY_STRING:S,TYPE_ARRAY:K,TYPE_BOOLEAN:L,TYPE_EXPREF:M,TYPE_NULL:B,TYPE_NUMBER:x,TYPE_OBJECT:U,TYPE_STRING:k};export{G as Scope,g as TYPE_ANY,K as TYPE_ARRAY,m as TYPE_ARRAY_ARRAY,v as TYPE_ARRAY_NUMBER,I as TYPE_ARRAY_OBJECT,S as TYPE_ARRAY_STRING,L as TYPE_BOOLEAN,M as TYPE_EXPREF,B as TYPE_NULL,x as TYPE_NUMBER,U as TYPE_OBJECT,k as TYPE_STRING,D as TreeInterpreter,w as compile,j as default,j as jmespath,F as registerFunction,b as search,C as tokenize};
1
+ var t;!function(t){t.TOK_EOF="EOF",t.TOK_VARIABLE="Variable",t.TOK_ASSIGN="Assign",t.TOK_UNQUOTEDIDENTIFIER="UnquotedIdentifier",t.TOK_QUOTEDIDENTIFIER="QuotedIdentifier",t.TOK_RBRACKET="Rbracket",t.TOK_RPAREN="Rparen",t.TOK_COMMA="Comma",t.TOK_COLON="Colon",t.TOK_RBRACE="Rbrace",t.TOK_NUMBER="Number",t.TOK_CURRENT="Current",t.TOK_ROOT="Root",t.TOK_EXPREF="Expref",t.TOK_PIPE="Pipe",t.TOK_OR="Or",t.TOK_AND="And",t.TOK_EQ="EQ",t.TOK_GT="GT",t.TOK_LT="LT",t.TOK_GTE="GTE",t.TOK_LTE="LTE",t.TOK_NE="NE",t.TOK_PLUS="Plus",t.TOK_MINUS="Minus",t.TOK_MULTIPLY="Multiply",t.TOK_DIVIDE="Divide",t.TOK_MODULO="Modulo",t.TOK_DIV="Div",t.TOK_FLATTEN="Flatten",t.TOK_STAR="Star",t.TOK_FILTER="Filter",t.TOK_DOT="Dot",t.TOK_NOT="Not",t.TOK_LBRACE="Lbrace",t.TOK_LBRACKET="Lbracket",t.TOK_LPAREN="Lparen",t.TOK_LITERAL="Literal"}(t||(t={}));const e=t=>null!==t&&"[object Object]"===Object.prototype.toString.call(t),r=(t,n)=>{if(t===n)return!0;if(typeof t!=typeof n)return!1;if(Array.isArray(t)&&Array.isArray(n)){if(t.length!==n.length)return!1;for(let e=0;e<t.length;e+=1)if(!r(t[e],n[e]))return!1;return!0}if(e(t)&&e(n)){const e=Object.entries(t),i=new Set(Object.keys(n));if(e.length!==i.size)return!1;for(const[t,s]of e){if(!r(s,n[t]))return!1;i.delete(t)}return 0===i.size}return!1},n=t=>{if("object"==typeof t){if(null===t)return!0;if(Array.isArray(t))return 0===t.length;for(const e in t)return!1;return!0}return!("number"==typeof t||t)},i=t=>t>="a"&&t<="z"||t>="A"&&t<="Z"||"_"===t,s=t=>t>="0"&&t<="9"||"-"===t,o=t=>{if("number"!=typeof t||Math.floor(t)!==t)throw new Error("invalid-value: expecting an integer.");return t},a=t=>{if("number"!=typeof t||t<0||Math.floor(t)!==t)throw new Error("invalid-value: expecting a non-negative integer.");return t},u=(...t)=>{for(let e=0;e<t.length;e++){if(null===t[e]||void 0===t[e])throw new Error("not-a-number: undefined");if("number"!=typeof t[e])throw new Error("not-a-number")}},c=t=>{if(!(t=+t))throw new Error("not-a-number: divide by zero");return t},h={"(":t.TOK_LPAREN,")":t.TOK_RPAREN,"*":t.TOK_STAR,",":t.TOK_COMMA,".":t.TOK_DOT,":":t.TOK_COLON,"@":t.TOK_CURRENT,"]":t.TOK_RBRACKET,"{":t.TOK_LBRACE,"}":t.TOK_RBRACE,"+":t.TOK_PLUS,"%":t.TOK_MODULO,"−":t.TOK_MINUS,"×":t.TOK_MULTIPLY,"÷":t.TOK_DIVIDE},T={"!":!0,"<":!0,"=":!0,">":!0,"&":!0,"|":!0,"/":!0},_={"\t":!0,"\n":!0,"\r":!0," ":!0};const l=new class{constructor(){this._current=0,this._enable_legacy_literals=!1}tokenize(e,r){const n=[];let o,a,u;for(this._current=0,this._enable_legacy_literals=(null==r?void 0:r.enable_legacy_literals)||!1;this._current<e.length;)if(i(e[this._current]))o=this._current,a=this.consumeUnquotedIdentifier(e),n.push({start:o,type:t.TOK_UNQUOTEDIDENTIFIER,value:a});else if(void 0!==h[e[this._current]])n.push({start:this._current,type:h[e[this._current]],value:e[this._current]}),this._current+=1;else if("$"===e[this._current])o=this._current,this._current+1<e.length&&i(e[this._current+1])?(this._current+=1,a=this.consumeUnquotedIdentifier(e),n.push({start:o,type:t.TOK_VARIABLE,value:a})):(n.push({start:o,type:t.TOK_ROOT,value:e[this._current]}),this._current+=1);else if("-"===e[this._current])if(this._current+1<e.length&&s(e[this._current+1])){const t=this.consumeNumber(e);t&&n.push(t)}else{const e={start:this._current,type:t.TOK_MINUS,value:"-"};n.push(e),this._current+=1}else if(s(e[this._current]))u=this.consumeNumber(e),n.push(u);else if("["===e[this._current])u=this.consumeLBracket(e),n.push(u);else if('"'===e[this._current])o=this._current,a=this.consumeQuotedIdentifier(e),n.push({start:o,type:t.TOK_QUOTEDIDENTIFIER,value:a});else if("'"===e[this._current])o=this._current,a=this.consumeRawStringLiteral(e),n.push({start:o,type:t.TOK_LITERAL,value:a});else if("`"===e[this._current]){o=this._current;const r=this.consumeLiteral(e);n.push({start:o,type:t.TOK_LITERAL,value:r})}else if(void 0!==T[e[this._current]])u=this.consumeOperator(e),u&&n.push(u);else{if(void 0===_[e[this._current]]){const t=new Error(`Syntax error: unknown character: ${e[this._current]}`);throw t.name="LexerError",t}this._current+=1}return n}consumeUnquotedIdentifier(t){const e=this._current;for(this._current+=1;this._current<t.length&&((r=t[this._current])>="a"&&r<="z"||r>="A"&&r<="Z"||r>="0"&&r<="9"||"_"===r);)this._current+=1;var r;return t.slice(e,this._current)}consumeQuotedIdentifier(t){const e=this._current;this._current+=1;const r=t.length;for(;'"'!==t[this._current]&&this._current<r;){let e=this._current;"\\"!==t[e]||"\\"!==t[e+1]&&'"'!==t[e+1]?e+=1:e+=2,this._current=e}this._current+=1;const[n,i]=this.parseJSON(t.slice(e,this._current));if(!i){const t=new Error("syntax: unexpected end of JSON input");throw t.name="LexerError",t}return n}consumeRawStringLiteral(t){const e=this._current;this._current+=1;const r=t.length;for(;"'"!==t[this._current]&&this._current<r;){let e=this._current;"\\"!==t[e]||"\\"!==t[e+1]&&"'"!==t[e+1]?e+=1:e+=2,this._current=e}this._current+=1;return t.slice(e+1,this._current-1).replace("\\'","'").replace("\\\\","\\")}consumeNumber(e){const r=this._current;this._current+=1;const n=e.length;for(;s(e[this._current])&&this._current<n;)this._current+=1;return{start:r,value:parseInt(e.slice(r,this._current),10),type:t.TOK_NUMBER}}consumeLBracket(e){const r=this._current;return this._current+=1,"?"===e[this._current]?(this._current+=1,{start:r,type:t.TOK_FILTER,value:"[?"}):"]"===e[this._current]?(this._current+=1,{start:r,type:t.TOK_FLATTEN,value:"[]"}):{start:r,type:t.TOK_LBRACKET,value:"["}}consumeOrElse(t,e,r,n){const i=this._current;return this._current+=1,this._current<t.length&&t[this._current]===e?(this._current+=1,{start:i,type:n,value:t.slice(i,this._current)}):{start:i,type:r,value:t[i]}}consumeOperator(e){switch(e[this._current]){case"!":return this.consumeOrElse(e,"=",t.TOK_NOT,t.TOK_NE);case"<":return this.consumeOrElse(e,"=",t.TOK_LT,t.TOK_LTE);case">":return this.consumeOrElse(e,"=",t.TOK_GT,t.TOK_GTE);case"=":return this.consumeOrElse(e,"=",t.TOK_ASSIGN,t.TOK_EQ);case"&":return this.consumeOrElse(e,"&",t.TOK_EXPREF,t.TOK_AND);case"|":return this.consumeOrElse(e,"|",t.TOK_PIPE,t.TOK_OR);case"/":return this.consumeOrElse(e,"/",t.TOK_DIVIDE,t.TOK_DIV)}}consumeLiteral(t){this._current+=1;const e=this._current,r=t.length;for(;"`"!==t[this._current]&&this._current<r;){let e=this._current;"\\"!==t[e]||"\\"!==t[e+1]&&"`"!==t[e+1]?e+=1:e+=2,this._current=e}let n=t.slice(e,this._current).trimStart();n=n.replace("\\`","`");let i=null,s=!1;if(this.looksLikeJSON(n)&&([i,s]=this.parseJSON(n)),!s&&this._enable_legacy_literals&&([i,s]=this.parseJSON(`"${n}"`)),!s){const e=new Error(`Syntax error: unexpected end of JSON input or invalid format for a JSON literal: ${t[this._current]}`);throw e.name="LexerError",e}return this._current+=1,i}looksLikeJSON(t){if(""===t)return!1;if('[{"'.includes(t[0]))return!0;if(["true","false","null"].includes(t))return!0;if("-0123456789".includes(t[0])){const[e,r]=this.parseJSON(t);return r}return!1}parseJSON(t){try{return[JSON.parse(t),!0]}catch(t){return[null,!1]}}},E={[t.TOK_EOF]:0,[t.TOK_VARIABLE]:0,[t.TOK_UNQUOTEDIDENTIFIER]:0,[t.TOK_QUOTEDIDENTIFIER]:0,[t.TOK_RBRACKET]:0,[t.TOK_RPAREN]:0,[t.TOK_COMMA]:0,[t.TOK_RBRACE]:0,[t.TOK_NUMBER]:0,[t.TOK_CURRENT]:0,[t.TOK_EXPREF]:0,[t.TOK_ROOT]:0,[t.TOK_ASSIGN]:1,[t.TOK_PIPE]:1,[t.TOK_OR]:2,[t.TOK_AND]:3,[t.TOK_EQ]:5,[t.TOK_GT]:5,[t.TOK_LT]:5,[t.TOK_GTE]:5,[t.TOK_LTE]:5,[t.TOK_NE]:5,[t.TOK_MINUS]:6,[t.TOK_PLUS]:6,[t.TOK_DIV]:7,[t.TOK_DIVIDE]:7,[t.TOK_MODULO]:7,[t.TOK_MULTIPLY]:7,[t.TOK_FLATTEN]:9,[t.TOK_STAR]:20,[t.TOK_FILTER]:21,[t.TOK_DOT]:40,[t.TOK_NOT]:45,[t.TOK_LBRACE]:50,[t.TOK_LBRACKET]:55,[t.TOK_LPAREN]:60};class p{constructor(){this.index=0,this.tokens=[]}parse(e,r){this.loadTokens(e,r||{enable_legacy_literals:!1}),this.index=0;const n=this.expression(0);if(this.lookahead(0)!==t.TOK_EOF){const t=this.lookaheadToken(0);this.errorToken(t,`Syntax error: unexpected token type: ${t.type}, value: ${t.value}`)}return n}loadTokens(e,r){this.tokens=[...l.tokenize(e,r),{type:t.TOK_EOF,value:"",start:e.length}]}expression(t){const e=this.lookaheadToken(0);this.advance();let r=this.nud(e),n=this.lookahead(0);for(;t<E[n];)this.advance(),r=this.led(n,r),n=this.lookahead(0);return r}lookahead(t){return this.tokens[this.index+t].type}lookaheadToken(t){return this.tokens[this.index+t]}advance(){this.index+=1}nud(e){switch(e.type){case t.TOK_VARIABLE:return{type:"Variable",name:e.value};case t.TOK_LITERAL:return{type:"Literal",value:e.value};case t.TOK_UNQUOTEDIDENTIFIER:return p.isKeyword(e,"let")&&this.lookahead(0)===t.TOK_VARIABLE?this.parseLetExpression():{type:"Field",name:e.value};case t.TOK_QUOTEDIDENTIFIER:if(this.lookahead(0)===t.TOK_LPAREN)throw new Error("Syntax error: quoted identifier not allowed for function names.");return{type:"Field",name:e.value};case t.TOK_NOT:return{type:"NotExpression",child:this.expression(E.Not)};case t.TOK_MINUS:{const t=this.expression(E.Minus);return{type:"Unary",operator:e.type,operand:t}}case t.TOK_PLUS:{const t=this.expression(E.Plus);return{type:"Unary",operator:e.type,operand:t}}case t.TOK_STAR:{const e={type:"Identity"};return{type:"ValueProjection",left:e,right:this.lookahead(0)===t.TOK_RBRACKET?e:this.parseProjectionRHS(E.Star)}}case t.TOK_FILTER:return this.led(e.type,{type:"Identity"});case t.TOK_LBRACE:return this.parseMultiselectHash();case t.TOK_FLATTEN:return{type:"Projection",left:{type:"Flatten",child:{type:"Identity"}},right:this.parseProjectionRHS(E.Flatten)};case t.TOK_LBRACKET:if(this.lookahead(0)===t.TOK_NUMBER||this.lookahead(0)===t.TOK_COLON){const t=this.parseIndexExpression();return this.projectIfSlice({type:"Identity"},t)}if(this.lookahead(0)===t.TOK_STAR&&this.lookahead(1)===t.TOK_RBRACKET){this.advance(),this.advance();return{left:{type:"Identity"},right:this.parseProjectionRHS(E.Star),type:"Projection"}}return this.parseMultiselectList();case t.TOK_CURRENT:return{type:t.TOK_CURRENT};case t.TOK_ROOT:return{type:t.TOK_ROOT};case t.TOK_EXPREF:return{type:"ExpressionReference",child:this.expression(E.Expref)};case t.TOK_LPAREN:{const e=[];for(;this.lookahead(0)!==t.TOK_RPAREN;){let r;this.lookahead(0)===t.TOK_CURRENT?(r={type:t.TOK_CURRENT},this.advance()):r=this.expression(0),e.push(r)}return this.match(t.TOK_RPAREN),e[0]}default:this.errorToken(e)}}led(e,r){switch(e){case t.TOK_DOT:{const e=E.Dot;if(this.lookahead(0)!==t.TOK_STAR){return{type:"Subexpression",left:r,right:this.parseDotRHS(e)}}this.advance();return{type:"ValueProjection",left:r,right:this.parseProjectionRHS(e)}}case t.TOK_PIPE:return{type:"Pipe",left:r,right:this.expression(E.Pipe)};case t.TOK_OR:return{type:"OrExpression",left:r,right:this.expression(E.Or)};case t.TOK_AND:return{type:"AndExpression",left:r,right:this.expression(E.And)};case t.TOK_LPAREN:if("Field"!==r.type)throw new Error("Syntax error: expected a Field node");return{name:r.name,type:"Function",children:this.parseCommaSeparatedExpressionsUntilToken(t.TOK_RPAREN)};case t.TOK_FILTER:{const e=this.expression(0);this.match(t.TOK_RBRACKET);return{type:"FilterProjection",left:r,right:this.lookahead(0)===t.TOK_FLATTEN?{type:"Identity"}:this.parseProjectionRHS(E.Filter),condition:e}}case t.TOK_FLATTEN:return{type:"Projection",left:{type:"Flatten",child:r},right:this.parseProjectionRHS(E.Flatten)};case t.TOK_ASSIGN:{const t=r,e=this.expression(0);return{type:"Binding",variable:t.name,reference:e}}case t.TOK_EQ:case t.TOK_NE:case t.TOK_GT:case t.TOK_GTE:case t.TOK_LT:case t.TOK_LTE:return this.parseComparator(r,e);case t.TOK_PLUS:case t.TOK_MINUS:case t.TOK_MULTIPLY:case t.TOK_STAR:case t.TOK_DIVIDE:case t.TOK_MODULO:case t.TOK_DIV:return this.parseArithmetic(r,e);case t.TOK_LBRACKET:{const e=this.lookaheadToken(0);if(e.type===t.TOK_NUMBER||e.type===t.TOK_COLON){const t=this.parseIndexExpression();return this.projectIfSlice(r,t)}this.match(t.TOK_STAR),this.match(t.TOK_RBRACKET);return{type:"Projection",left:r,right:this.parseProjectionRHS(E.Star)}}default:return this.errorToken(this.lookaheadToken(0))}}static isKeyword(e,r){return e.type===t.TOK_UNQUOTEDIDENTIFIER&&e.value===r}match(t){if(this.lookahead(0)!==t){const e=this.lookaheadToken(0);this.errorToken(e,`Syntax error: expected ${t}, got: ${e.type}`)}else this.advance()}errorToken(t,e=""){const r=new Error(e||`Syntax error: invalid token (${t.type}): "${t.value}"`);throw r.name="ParserError",r}parseIndexExpression(){if(this.lookahead(0)===t.TOK_COLON||this.lookahead(1)===t.TOK_COLON)return this.parseSliceExpression();const e=Number(this.lookaheadToken(0).value);return this.advance(),this.match(t.TOK_RBRACKET),{type:"Index",value:e}}projectIfSlice(t,e){const r={type:"IndexExpression",left:t,right:e};return"Slice"===e.type?{left:r,right:this.parseProjectionRHS(E.Star),type:"Projection"}:r}parseSliceExpression(){const e=[null,null,null];let r=0,n=this.lookaheadToken(0);for(;n.type!=t.TOK_RBRACKET&&r<3;){if(n.type===t.TOK_COLON)r++,3===r&&this.errorToken(this.lookaheadToken(0),"Syntax error, too many colons in slice expression"),this.advance();else if(n.type===t.TOK_NUMBER){const t=this.lookaheadToken(0).value;e[r]=t,this.advance()}else{const t=this.lookaheadToken(0);this.errorToken(t,`Syntax error, unexpected token: ${t.value}(${t.type})`)}n=this.lookaheadToken(0)}this.match(t.TOK_RBRACKET);const[i,s,o]=e;return{type:"Slice",start:i,stop:s,step:o}}parseLetExpression(){const t=this.parseCommaSeparatedExpressionsUntilKeyword("in"),e=this.expression(0);return{type:"LetExpression",bindings:t.map((t=>t)),expression:e}}parseCommaSeparatedExpressionsUntilKeyword(t){return this.parseCommaSeparatedExpressionsUntil((()=>p.isKeyword(this.lookaheadToken(0),t)),(()=>{this.advance()}))}parseCommaSeparatedExpressionsUntilToken(t){return this.parseCommaSeparatedExpressionsUntil((()=>this.lookahead(0)===t),(()=>this.match(t)))}parseCommaSeparatedExpressionsUntil(e,r){const n=[];let i;for(;!e();)i=this.expression(0),this.lookahead(0)===t.TOK_COMMA&&this.match(t.TOK_COMMA),n.push(i);return r(),n}parseComparator(t,e){return{type:"Comparator",name:e,left:t,right:this.expression(E[e])}}parseArithmetic(t,e){return{type:"Arithmetic",operator:e,left:t,right:this.expression(E[e])}}parseDotRHS(e){const r=this.lookahead(0);if([t.TOK_UNQUOTEDIDENTIFIER,t.TOK_QUOTEDIDENTIFIER,t.TOK_STAR].includes(r))return this.expression(e);if(r===t.TOK_LBRACKET)return this.match(t.TOK_LBRACKET),this.parseMultiselectList();if(r===t.TOK_LBRACE)return this.match(t.TOK_LBRACE),this.parseMultiselectHash();const n=this.lookaheadToken(0);this.errorToken(n,`Syntax error, unexpected token: ${n.value}(${n.type})`)}parseProjectionRHS(e){if(E[this.lookahead(0)]<10)return{type:"Identity"};if(this.lookahead(0)===t.TOK_LBRACKET)return this.expression(e);if(this.lookahead(0)===t.TOK_FILTER)return this.expression(e);if(this.lookahead(0)===t.TOK_DOT)return this.match(t.TOK_DOT),this.parseDotRHS(e);const r=this.lookaheadToken(0);this.errorToken(r,`Syntax error, unexpected token: ${r.value}(${r.type})`)}parseMultiselectList(){const e=[];for(;this.lookahead(0)!==t.TOK_RBRACKET;){const r=this.expression(0);if(e.push(r),this.lookahead(0)===t.TOK_COMMA&&(this.match(t.TOK_COMMA),this.lookahead(0)===t.TOK_RBRACKET))throw new Error("Syntax error: unexpected token Rbracket")}return this.match(t.TOK_RBRACKET),{type:"MultiSelectList",children:e}}parseMultiselectHash(){const e=[],r=[t.TOK_UNQUOTEDIDENTIFIER,t.TOK_QUOTEDIDENTIFIER];let n,i,s;for(;;){if(n=this.lookaheadToken(0),!r.includes(n.type))throw new Error(`Syntax error: expecting an identifier token, got: ${n.type}`);if(i=n.value,this.advance(),this.match(t.TOK_COLON),s=this.expression(0),e.push({value:s,type:"KeyValuePair",name:i}),this.lookahead(0)===t.TOK_COMMA)this.match(t.TOK_COMMA);else if(this.lookahead(0)===t.TOK_RBRACE){this.match(t.TOK_RBRACE);break}}return{type:"MultiSelectHash",children:e}}}const R=new p;class f{constructor(t){this._text=t}get string(){return this._text}get length(){return this.codePoints.length}compareTo(t){return f.compare(this,new f(t))}static get comparer(){return(t,e)=>new f(t).compareTo(e)}static compare(t,e){const r=t.codePoints,n=e.codePoints;for(let t=0;t<Math.min(r.length,n.length);t++)if(r[t]!==n[t])return r[t]-n[t]>0?1:-1;return r.length-n.length>0?1:-1}reverse(){return String.fromCodePoint(...this.codePoints.reverse())}get codePoints(){return[...this._text].map((t=>t.codePointAt(0)))}}const O=(t,e,r)=>{if((r=r||" ").length>1)throw new Error(`invalid value, ${t} expects its 'pad' parameter to be a valid string with a single codepoint`);return a(e),r},y=(t,e)=>d(t,(t=>new RegExp(`^[${t}]*(.*?)`)),e),A=(t,e)=>d(t,(t=>new RegExp(`(.*?)[${t}]*$`)),e),d=(t,e,r)=>{const n=r?r.replace(/[-/\\^$*+?.()|[\]{}]/g,"\\$&"):"\\s…";return t.replace(e(n),"$1")};var P;!function(t){t[t.TYPE_NUMBER=0]="TYPE_NUMBER",t[t.TYPE_ANY=1]="TYPE_ANY",t[t.TYPE_STRING=2]="TYPE_STRING",t[t.TYPE_ARRAY=3]="TYPE_ARRAY",t[t.TYPE_OBJECT=4]="TYPE_OBJECT",t[t.TYPE_BOOLEAN=5]="TYPE_BOOLEAN",t[t.TYPE_EXPREF=6]="TYPE_EXPREF",t[t.TYPE_NULL=7]="TYPE_NULL",t[t.TYPE_ARRAY_NUMBER=8]="TYPE_ARRAY_NUMBER",t[t.TYPE_ARRAY_STRING=9]="TYPE_ARRAY_STRING",t[t.TYPE_ARRAY_OBJECT=10]="TYPE_ARRAY_OBJECT",t[t.TYPE_ARRAY_ARRAY=11]="TYPE_ARRAY_ARRAY"}(P||(P={}));class Y{constructor(t){this.TYPE_NAME_TABLE={[P.TYPE_NUMBER]:"number",[P.TYPE_ANY]:"any",[P.TYPE_STRING]:"string",[P.TYPE_ARRAY]:"array",[P.TYPE_OBJECT]:"object",[P.TYPE_BOOLEAN]:"boolean",[P.TYPE_EXPREF]:"expression",[P.TYPE_NULL]:"null",[P.TYPE_ARRAY_NUMBER]:"Array<number>",[P.TYPE_ARRAY_OBJECT]:"Array<object>",[P.TYPE_ARRAY_STRING]:"Array<string>",[P.TYPE_ARRAY_ARRAY]:"Array<Array<any>>"},this.functionAbs=([t])=>Math.abs(t),this.functionAvg=([t])=>{if(!t||0==t.length)return null;let e=0;for(let r=0;r<t.length;r+=1)e+=t[r];return e/t.length},this.functionCeil=([t])=>Math.ceil(t),this.functionContains=([t,e])=>{if(Array.isArray(t)){return t.includes(e)}if("string"==typeof t){if("string"==typeof e)return t.includes(e)}return null},this.functionEndsWith=t=>{const[e,r]=t;return e.includes(r,e.length-r.length)},this.functionFindFirst=t=>((t,e,r,n)=>{if(!t||!e)return null;r=Math.max(o(r=r||0),0),n=Math.min(o(n=n||t.length),t.length);const i=t.slice(r,n).indexOf(e);return-1===i?null:i+r})(t[0],t[1],t.length>2&&t[2]||void 0,t.length>3&&t[3]||void 0),this.functionFindLast=t=>((t,e,r,n)=>{if(!t||!e)return null;r=Math.max(o(r=r||0),0),n=Math.min(o(n=n||t.length),t.length);const i=t.slice(r,n).lastIndexOf(e);return-1===i?null:i+r})(t[0],t[1],t.length>2&&t[2]||void 0,t.length>3&&t[3]||void 0),this.functionFloor=([t])=>Math.floor(t),this.functionFromItems=([t])=>(t.map((t=>{if(2!=t.length||"string"!=typeof t[0])throw new Error("invalid value, each array must contain two elements, a pair of string and value")})),Object.fromEntries(t)),this.functionGroupBy=([t,e])=>{const r=this.createKeyFunction(e,[P.TYPE_STRING]);return t.reduce(((t,e)=>{const n=r(null!=e?e:{});return(t[n]=t[n]||[]).push(e),t}),{})},this.functionItems=([t])=>Object.entries(t),this.functionJoin=t=>{const[e,r]=t;return r.join(e)},this.functionKeys=([t])=>Object.keys(t),this.functionLength=([t])=>"string"==typeof t?new f(t).length:Array.isArray(t)?t.length:Object.keys(t).length,this.functionLower=([t])=>(t=>t.toLowerCase())(t),this.functionMap=([t,e])=>{if(!this._interpreter)return[];const r=[],n=this._interpreter;for(let i=0;i<e.length;i+=1)r.push(n.visit(t,e[i]));return r},this.functionMax=([t])=>{if(!t.length)return null;if(this.getTypeName(t[0])===P.TYPE_NUMBER)return Math.max(...t);const e=t;let r=e[0];for(let t=1;t<e.length;t+=1)r.localeCompare(e[t])<0&&(r=e[t]);return r},this.functionMaxBy=t=>{const e=t[1],r=t[0],n=this.createKeyFunction(e,[P.TYPE_NUMBER,P.TYPE_STRING]);let i,s,o=-1/0;for(let t=0;t<r.length;t+=1)s=n&&n(r[t]),void 0!==s&&s>o&&(o=s,i=r[t]);return i||null},this.functionMerge=t=>{let e={};for(let r=0;r<t.length;r+=1){const n=t[r];e=Object.assign(e,n)}return e},this.functionMin=([t])=>{if(!t.length)return null;if(this.getTypeName(t[0])===P.TYPE_NUMBER)return Math.min(...t);const e=t;let r=e[0];for(let t=1;t<e.length;t+=1)e[t].localeCompare(r)<0&&(r=e[t]);return r},this.functionMinBy=t=>{const e=t[1],r=t[0],n=this.createKeyFunction(e,[P.TYPE_NUMBER,P.TYPE_STRING]);let i,s,o=1/0;for(let t=0;t<r.length;t+=1)s=n&&n(r[t]),void 0!==s&&s<o&&(o=s,i=r[t]);return i||null},this.functionNotNull=t=>{for(let e=0;e<t.length;e+=1)if(this.getTypeName(t[e])!==P.TYPE_NULL)return t[e];return null},this.functionPadLeft=t=>((t,e,r)=>(r=O("pad_left",e,r),t&&t.padStart(e,r)||""))(t[0],t[1],t.length>2&&t[2]||void 0),this.functionPadRight=t=>((t,e,r)=>(r=O("pad_right",e,r),t&&t.padEnd(e,r)||""))(t[0],t[1],t.length>2&&t[2]||void 0),this.functionReplace=t=>((t,e,r,n)=>0===n?t:n?(a(n),[...Array(n).keys()].map((()=>t=t.replace(e,r))),t):t.split(e).join(r))(t[0],t[1],t[2],t.length>3?t[3]:void 0),this.functionSplit=t=>((t,e,r)=>{if(0==t.length&&0===e.length)return[];if(null==r)return t.split(e);if(a(r),0===r)return[t];const n=t.split(e);return[...n.slice(0,r),n.slice(r).join(e)]})(t[0],t[1],t.length>2?t[2]:void 0),this.functionReverse=([t])=>{if(this.getTypeName(t)===P.TYPE_STRING)return new f(t).reverse();const e=t.slice(0);return e.reverse(),e},this.functionSort=([t])=>0==t.length?t:"string"==typeof t[0]?[...t].sort(f.comparer):[...t].sort(),this.functionSortBy=t=>{const e=t[0].slice(0);if(0===e.length)return e;const r=this._interpreter,n=t[1],i=this.getTypeName(r.visit(n,e[0]));if(void 0!==i&&![P.TYPE_NUMBER,P.TYPE_STRING].includes(i))throw new Error(`Invalid type: unexpected type (${this.TYPE_NAME_TABLE[i]})`);function s(t,e){throw new Error(`Invalid type: expected (${t.TYPE_NAME_TABLE[i]}), received ${t.TYPE_NAME_TABLE[t.getTypeName(e)]}`)}return e.sort(((t,e)=>{const o=r.visit(n,t),a=r.visit(n,e);return this.getTypeName(o)!==i?s(this,o):this.getTypeName(a)!==i&&s(this,a),i===P.TYPE_STRING?f.comparer(o,a):o-a}))},this.functionStartsWith=([t,e])=>t.startsWith(e),this.functionSum=([t])=>t.reduce(((t,e)=>t+e),0),this.functionToArray=([t])=>this.getTypeName(t)===P.TYPE_ARRAY?t:[t],this.functionToNumber=([t])=>{const e=this.getTypeName(t);let r;return e===P.TYPE_NUMBER?t:e!==P.TYPE_STRING||(r=+t,isNaN(r))?null:r},this.functionToString=([t])=>this.getTypeName(t)===P.TYPE_STRING?t:JSON.stringify(t),this.functionTrim=t=>((t,e)=>y(A(t,e),e))(t[0],t.length>1?t[1]:void 0),this.functionTrimLeft=t=>{const e=t[0];return y(e,t.length>1?t[1]:void 0)},this.functionTrimRight=t=>{const e=t[0];return A(e,t.length>1?t[1]:void 0)},this.functionType=([t])=>{switch(this.getTypeName(t)){case P.TYPE_NUMBER:return"number";case P.TYPE_STRING:return"string";case P.TYPE_ARRAY:return"array";case P.TYPE_OBJECT:return"object";case P.TYPE_BOOLEAN:return"boolean";case P.TYPE_NULL:return"null";default:throw new Error("invalid-type")}},this.functionUpper=([t])=>(t=>t.toUpperCase())(t),this.functionValues=([t])=>Object.values(t),this.functionZip=t=>{const e=Math.min(...t.map((t=>t.length)));return Array(e).fill(null).map(((e,r)=>t.map((t=>t[r]))))},this.functionTable={abs:{_func:this.functionAbs,_signature:[{types:[P.TYPE_NUMBER]}]},avg:{_func:this.functionAvg,_signature:[{types:[P.TYPE_ARRAY_NUMBER]}]},ceil:{_func:this.functionCeil,_signature:[{types:[P.TYPE_NUMBER]}]},contains:{_func:this.functionContains,_signature:[{types:[P.TYPE_STRING,P.TYPE_ARRAY]},{types:[P.TYPE_ANY]}]},ends_with:{_func:this.functionEndsWith,_signature:[{types:[P.TYPE_STRING]},{types:[P.TYPE_STRING]}]},find_first:{_func:this.functionFindFirst,_signature:[{types:[P.TYPE_STRING]},{types:[P.TYPE_STRING]},{types:[P.TYPE_NUMBER],optional:!0},{types:[P.TYPE_NUMBER],optional:!0}]},find_last:{_func:this.functionFindLast,_signature:[{types:[P.TYPE_STRING]},{types:[P.TYPE_STRING]},{types:[P.TYPE_NUMBER],optional:!0},{types:[P.TYPE_NUMBER],optional:!0}]},floor:{_func:this.functionFloor,_signature:[{types:[P.TYPE_NUMBER]}]},from_items:{_func:this.functionFromItems,_signature:[{types:[P.TYPE_ARRAY_ARRAY]}]},group_by:{_func:this.functionGroupBy,_signature:[{types:[P.TYPE_ARRAY]},{types:[P.TYPE_EXPREF]}]},items:{_func:this.functionItems,_signature:[{types:[P.TYPE_OBJECT]}]},join:{_func:this.functionJoin,_signature:[{types:[P.TYPE_STRING]},{types:[P.TYPE_ARRAY_STRING]}]},keys:{_func:this.functionKeys,_signature:[{types:[P.TYPE_OBJECT]}]},length:{_func:this.functionLength,_signature:[{types:[P.TYPE_STRING,P.TYPE_ARRAY,P.TYPE_OBJECT]}]},lower:{_func:this.functionLower,_signature:[{types:[P.TYPE_STRING]}]},map:{_func:this.functionMap,_signature:[{types:[P.TYPE_EXPREF]},{types:[P.TYPE_ARRAY]}]},max:{_func:this.functionMax,_signature:[{types:[P.TYPE_ARRAY_NUMBER,P.TYPE_ARRAY_STRING]}]},max_by:{_func:this.functionMaxBy,_signature:[{types:[P.TYPE_ARRAY]},{types:[P.TYPE_EXPREF]}]},merge:{_func:this.functionMerge,_signature:[{types:[P.TYPE_OBJECT],variadic:!0}]},min:{_func:this.functionMin,_signature:[{types:[P.TYPE_ARRAY_NUMBER,P.TYPE_ARRAY_STRING]}]},min_by:{_func:this.functionMinBy,_signature:[{types:[P.TYPE_ARRAY]},{types:[P.TYPE_EXPREF]}]},not_null:{_func:this.functionNotNull,_signature:[{types:[P.TYPE_ANY],variadic:!0}]},pad_left:{_func:this.functionPadLeft,_signature:[{types:[P.TYPE_STRING]},{types:[P.TYPE_NUMBER]},{types:[P.TYPE_STRING],optional:!0}]},pad_right:{_func:this.functionPadRight,_signature:[{types:[P.TYPE_STRING]},{types:[P.TYPE_NUMBER]},{types:[P.TYPE_STRING],optional:!0}]},replace:{_func:this.functionReplace,_signature:[{types:[P.TYPE_STRING]},{types:[P.TYPE_STRING]},{types:[P.TYPE_STRING]},{types:[P.TYPE_NUMBER],optional:!0}]},split:{_func:this.functionSplit,_signature:[{types:[P.TYPE_STRING]},{types:[P.TYPE_STRING]},{types:[P.TYPE_NUMBER],optional:!0}]},reverse:{_func:this.functionReverse,_signature:[{types:[P.TYPE_STRING,P.TYPE_ARRAY]}]},sort:{_func:this.functionSort,_signature:[{types:[P.TYPE_ARRAY_STRING,P.TYPE_ARRAY_NUMBER]}]},sort_by:{_func:this.functionSortBy,_signature:[{types:[P.TYPE_ARRAY]},{types:[P.TYPE_EXPREF]}]},starts_with:{_func:this.functionStartsWith,_signature:[{types:[P.TYPE_STRING]},{types:[P.TYPE_STRING]}]},sum:{_func:this.functionSum,_signature:[{types:[P.TYPE_ARRAY_NUMBER]}]},to_array:{_func:this.functionToArray,_signature:[{types:[P.TYPE_ANY]}]},to_number:{_func:this.functionToNumber,_signature:[{types:[P.TYPE_ANY]}]},to_string:{_func:this.functionToString,_signature:[{types:[P.TYPE_ANY]}]},trim:{_func:this.functionTrim,_signature:[{types:[P.TYPE_STRING]},{types:[P.TYPE_STRING],optional:!0}]},trim_left:{_func:this.functionTrimLeft,_signature:[{types:[P.TYPE_STRING]},{types:[P.TYPE_STRING],optional:!0}]},trim_right:{_func:this.functionTrimRight,_signature:[{types:[P.TYPE_STRING]},{types:[P.TYPE_STRING],optional:!0}]},type:{_func:this.functionType,_signature:[{types:[P.TYPE_ANY]}]},upper:{_func:this.functionUpper,_signature:[{types:[P.TYPE_STRING]}]},values:{_func:this.functionValues,_signature:[{types:[P.TYPE_OBJECT]}]},zip:{_func:this.functionZip,_signature:[{types:[P.TYPE_ARRAY],variadic:!0}]}},this._interpreter=t}registerFunction(t,e,r){if(t in this.functionTable)throw new Error(`Function already defined: ${t}()`);this.functionTable[t]={_func:e.bind(this),_signature:r}}callFunction(t,e){const r=this.functionTable[t];if(void 0===r)throw new Error(`Unknown function: ${t}()`);return this.validateArgs(t,e,r._signature),r._func.call(this,e)}validateInputSignatures(t,e){for(let r=0;r<e.length;r+=1)if("variadic"in e[r]&&r!==e.length-1)throw new Error(`Invalid arity: ${t}() 'variadic' argument ${r+1} must occur last`)}validateArgs(t,e,r){var n,i;let s;this.validateInputSignatures(t,r);const o=r.filter((t=>{var e;return null!==(e=!t.optional)&&void 0!==e&&e})).length,a=null!==(i=null===(n=r[r.length-1])||void 0===n?void 0:n.variadic)&&void 0!==i&&i,u=e.length<o,c=e.length>r.length,h=u&&(!a&&o>1||a)?"at least ":"";if(a&&u||!a&&(u||c))throw s=r.length>1,new Error(`Invalid arity: ${t}() takes ${h}${o} argument${s?"s":""} but received ${e.length}`);let T,_,l;for(let n=0;n<r.length;n+=1){let i;for(l=!1,T=r[n].types,_=this.getTypeName(e[n]),i=0;i<T.length;i+=1)if(void 0!==_&&this.typeMatches(_,T[i],e[n])){l=!0;break}if(!l&&void 0!==_){const e=T.map((t=>this.TYPE_NAME_TABLE[t])).join(" | ");throw new Error(`Invalid type: ${t}() expected argument ${n+1} to be type (${e}) but received type ${this.TYPE_NAME_TABLE[_]} instead.`)}}}typeMatches(t,e,r){if(e===P.TYPE_ANY)return!0;if(e!==P.TYPE_ARRAY_STRING&&e!==P.TYPE_ARRAY_NUMBER&&e!==P.TYPE_ARRAY_OBJECT&&e!==P.TYPE_ARRAY_ARRAY&&e!==P.TYPE_ARRAY)return t===e;if(e===P.TYPE_ARRAY)return t===P.TYPE_ARRAY;if(t===P.TYPE_ARRAY){let t;e===P.TYPE_ARRAY_NUMBER?t=P.TYPE_NUMBER:e===P.TYPE_ARRAY_OBJECT?t=P.TYPE_OBJECT:e===P.TYPE_ARRAY_STRING?t=P.TYPE_STRING:e===P.TYPE_ARRAY_ARRAY&&(t=P.TYPE_ARRAY);const n=r;for(let e=0;e<n.length;e+=1){const r=this.getTypeName(n[e]);if(void 0!==r&&void 0!==t&&!this.typeMatches(r,t,n[e]))return!1}return!0}return!1}getTypeName(t){switch(Object.prototype.toString.call(t)){case"[object String]":return P.TYPE_STRING;case"[object Number]":return P.TYPE_NUMBER;case"[object Array]":return P.TYPE_ARRAY;case"[object Boolean]":return P.TYPE_BOOLEAN;case"[object Null]":return P.TYPE_NULL;case"[object Object]":return t.expref?P.TYPE_EXPREF:P.TYPE_OBJECT;default:return}}createKeyFunction(t,e){const r=this._interpreter;return n=>{const i=r.visit(t,n);if(!e.includes(this.getTypeName(i))){const t=`Invalid type: expected one of (${e.map((t=>this.TYPE_NAME_TABLE[t])).join(" | ")}), received ${this.TYPE_NAME_TABLE[this.getTypeName(i)]}`;throw new Error(t)}return i}}}class N{constructor(){this.inner=void 0,this.data={}}withScope(t){const e=new N;return e.inner=this,e.data=t,e}getValue(t){if(t in this.data){const e=this.data[t];if(null!=e)return e}return this.inner?this.inner.getValue(t):null}}class K{constructor(){this._rootValue=null,this.runtime=new Y(this),this._scope=new N}withScope(t){const e=new K;return e._rootValue=this._rootValue,e._scope=this._scope.withScope(t),e}search(t,e){return this._rootValue=e,this._scope=new N,this.visit(t,e)}visit(e,i){var s,o,a,h,T;switch(e.type){case"Field":const _=e.name;let l=null;return null===i||"object"!=typeof i||Array.isArray(i)||(l=null!==(s=i[_])&&void 0!==s?s:null),l;case"LetExpression":{const{bindings:t,expression:r}=e;let n={};return t.forEach((t=>{const e=this.visit(t,i);n=Object.assign(Object.assign({},n),e)})),this.withScope(n).visit(r,i)}case"Binding":{const{variable:t,reference:r}=e;return{[t]:this.visit(r,i)}}case"Variable":{const t=e.name,r=null!==(o=this._scope.getValue(t))&&void 0!==o?o:null;if(null==r)throw new Error(`Error referencing undefined variable ${t}`);return r}case"IndexExpression":return this.visit(e.right,this.visit(e.left,i));case"Subexpression":{const t=this.visit(e.left,i);return null!=t&&this.visit(e.right,t)||null}case"Index":{if(!Array.isArray(i))return null;const t=e.value<0?i.length+e.value:e.value;return null!==(a=i[t])&&void 0!==a?a:null}case"Slice":{if(!Array.isArray(i)&&"string"!=typeof i)return null;const{start:t,stop:r,step:n}=this.computeSliceParams(i.length,e);if("string"==typeof i){const e=[...i];return this.slice(e,t,r,n).join("")}return this.slice(i,t,r,n)}case"Projection":{const{left:t,right:r}=e;let n=!1;"IndexExpression"===t.type&&"Slice"===t.right.type&&(n=!0);const s=this.visit(t,i);if(n&&"string"==typeof s)return s;if(!Array.isArray(s))return null;const o=[];for(const t of s){const e=this.visit(r,t);null!==e&&o.push(e)}return o}case"ValueProjection":{const{left:t,right:r}=e,n=this.visit(t,i);if(null===n||"object"!=typeof n||Array.isArray(n))return null;const s=[],o=Object.values(n);for(const t of o){const e=this.visit(r,t);null!==e&&s.push(e)}return s}case"FilterProjection":{const{left:t,right:r,condition:s}=e,o=this.visit(t,i);if(!Array.isArray(o))return null;const a=[];for(const t of o){const e=this.visit(s,t);if(n(e))continue;const i=this.visit(r,t);null!==i&&a.push(i)}return a}case"Arithmetic":{const r=this.visit(e.left,i),n=this.visit(e.right,i);switch(e.operator){case t.TOK_PLUS:return u(h=r,T=n),h+T;case t.TOK_MINUS:return((t,e)=>(u(t,e),t-e))(r,n);case t.TOK_MULTIPLY:case t.TOK_STAR:return((t,e)=>(u(t,e),t*e))(r,n);case t.TOK_DIVIDE:return((t,e)=>(u(t,e),t/c(e)))(r,n);case t.TOK_MODULO:return((t,e)=>(u(t,e),t%e))(r,n);case t.TOK_DIV:return((t,e)=>(u(t,e),Math.floor(t/c(e))))(r,n);default:throw new Error(`Syntax error: unknown arithmetic operator: ${e.operator}`)}}case"Unary":{const r=this.visit(e.operand,i);switch(e.operator){case t.TOK_PLUS:return u(r),r;case t.TOK_MINUS:return u(r),-r;default:throw new Error(`Syntax error: unknown arithmetic operator: ${e.operator}`)}}case"Comparator":{const t=this.visit(e.left,i),n=this.visit(e.right,i);switch(e.name){case"EQ":return r(t,n);case"NE":return!r(t,n)}if("number"!=typeof t||"number"!=typeof n)return null;switch(e.name){case"GT":return t>n;case"GTE":return t>=n;case"LT":return t<n;case"LTE":return t<=n}}case"Flatten":{const t=this.visit(e.child,i);return Array.isArray(t)?t.flat():null}case"Root":return this._rootValue;case"MultiSelectList":{const t=[];for(const r of e.children)t.push(this.visit(r,i));return t}case"MultiSelectHash":{const t={};for(const r of e.children)t[r.name]=this.visit(r.value,i);return t}case"OrExpression":{const t=this.visit(e.left,i);return n(t)?this.visit(e.right,i):t}case"AndExpression":{const t=this.visit(e.left,i);return n(t)?t:this.visit(e.right,i)}case"NotExpression":return n(this.visit(e.child,i));case"Literal":return e.value;case"Pipe":return this.visit(e.right,this.visit(e.left,i));case"Function":{const t=[];for(const r of e.children)t.push(this.visit(r,i));return this.runtime.callFunction(e.name,t)}case"ExpressionReference":return Object.assign({expref:!0},e.child);case"Current":case"Identity":return i}}computeSliceParams(t,e){let{start:r,stop:n,step:i}=e;if(null===i)i=1;else if(0===i){const t=new Error("Invalid value: slice step cannot be 0");throw t.name="RuntimeError",t}return r=null===r?i<0?t-1:0:this.capSliceRange(t,r,i),n=null===n?i<0?-1:t:this.capSliceRange(t,n,i),{start:r,stop:n,step:i}}capSliceRange(t,e,r){let n=e;return n<0?(n+=t,n<0&&(n=r<0?-1:0)):n>=t&&(n=r<0?t-1:t),n}slice(t,e,r,n){const i=[];if(n>0)for(let s=e;s<r;s+=n)i.push(t[s]);else for(let s=e;s>r;s+=n)i.push(t[s]);return i}}const g=new K,m=P.TYPE_ANY,I=P.TYPE_ARRAY,S=P.TYPE_ARRAY_ARRAY,v=P.TYPE_ARRAY_NUMBER,L=P.TYPE_ARRAY_OBJECT,M=P.TYPE_ARRAY_STRING,B=P.TYPE_BOOLEAN,x=P.TYPE_EXPREF,U=P.TYPE_NULL,k=P.TYPE_NUMBER,w=P.TYPE_OBJECT,C=P.TYPE_STRING;function b(t,e){return R.parse(t,e)}function F(t,e){return l.tokenize(t,e)}const G=(t,e,r)=>{g.runtime.registerFunction(t,e,r)};function D(t,e,r){const n=R.parse(e,r);return g.search(n,t)}function j(){return new N}const $=g,J={compile:b,registerFunction:G,search:D,tokenize:F,TreeInterpreter:$,TYPE_ANY:m,TYPE_ARRAY_NUMBER:v,TYPE_ARRAY_STRING:M,TYPE_ARRAY:I,TYPE_BOOLEAN:B,TYPE_EXPREF:x,TYPE_NULL:U,TYPE_NUMBER:k,TYPE_OBJECT:w,TYPE_STRING:C};export{j as Scope,m as TYPE_ANY,I as TYPE_ARRAY,S as TYPE_ARRAY_ARRAY,v as TYPE_ARRAY_NUMBER,L as TYPE_ARRAY_OBJECT,M as TYPE_ARRAY_STRING,B as TYPE_BOOLEAN,x as TYPE_EXPREF,U as TYPE_NULL,k as TYPE_NUMBER,w as TYPE_OBJECT,C as TYPE_STRING,$ as TreeInterpreter,b as compile,J as default,J as jmespath,G as registerFunction,D as search,F as tokenize};
2
2
  //# sourceMappingURL=jmespath.esm.min.js.map