@uipath/maestro-sdk 1.202.0-preview.142 → 1.202.0

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.
@@ -5192,2019 +5192,1496 @@ var init_js_yaml = __esm(() => {
5192
5192
  } = yaml);
5193
5193
  });
5194
5194
 
5195
- // ../../node_modules/@jmespath-community/jmespath/dist/index.mjs
5196
- var exports_dist = {};
5197
- __export(exports_dist, {
5198
- Scope: () => Scope,
5199
- TYPE_ANY: () => TYPE_ANY,
5200
- TYPE_ARRAY: () => TYPE_ARRAY,
5201
- TYPE_ARRAY_ARRAY: () => TYPE_ARRAY_ARRAY,
5202
- TYPE_ARRAY_NUMBER: () => TYPE_ARRAY_NUMBER,
5203
- TYPE_ARRAY_OBJECT: () => TYPE_ARRAY_OBJECT,
5204
- TYPE_ARRAY_STRING: () => TYPE_ARRAY_STRING,
5205
- TYPE_BOOLEAN: () => TYPE_BOOLEAN,
5206
- TYPE_EXPREF: () => TYPE_EXPREF,
5207
- TYPE_NULL: () => TYPE_NULL,
5208
- TYPE_NUMBER: () => TYPE_NUMBER,
5209
- TYPE_OBJECT: () => TYPE_OBJECT,
5210
- TYPE_STRING: () => TYPE_STRING,
5211
- TreeInterpreter: () => TreeInterpreter2,
5212
- clearCustomFunctions: () => clearCustomFunctions,
5213
- compile: () => compile,
5214
- default: () => jmespath,
5215
- getCustomFunctions: () => getCustomFunctions,
5216
- getRegisteredFunctions: () => getRegisteredFunctions,
5217
- isRegistered: () => isRegistered,
5218
- jmespath: () => jmespath,
5219
- register: () => register,
5220
- registerFunction: () => registerFunction,
5221
- search: () => search,
5222
- tokenize: () => tokenize,
5223
- unregisterFunction: () => unregisterFunction
5224
- });
5225
- function compile(expression, options) {
5226
- const nodeTree = Parser_default.parse(expression, options);
5227
- return nodeTree;
5228
- }
5229
- function tokenize(expression, options) {
5230
- return Lexer_default.tokenize(expression, options);
5231
- }
5232
- function search(data, expression, options) {
5233
- const nodeTree = Parser_default.parse(expression, options);
5234
- return TreeInterpreter_default.search(nodeTree, data);
5235
- }
5236
- function Scope() {
5237
- return new ScopeChain;
5238
- }
5239
- var isObject = (obj) => {
5240
- return obj !== null && Object.prototype.toString.call(obj) === "[object Object]";
5241
- }, strictDeepEqual = (first, second) => {
5242
- if (first === second) {
5243
- return true;
5244
- }
5245
- if (typeof first !== typeof second) {
5246
- return false;
5247
- }
5248
- if (Array.isArray(first) && Array.isArray(second)) {
5249
- if (first.length !== second.length) {
5250
- return false;
5251
- }
5252
- for (let i = 0;i < first.length; i += 1) {
5253
- if (!strictDeepEqual(first[i], second[i])) {
5254
- return false;
5255
- }
5256
- }
5257
- return true;
5258
- }
5259
- if (isObject(first) && isObject(second)) {
5260
- const firstEntries = Object.entries(first);
5261
- const secondKeys = new Set(Object.keys(second));
5262
- if (firstEntries.length !== secondKeys.size) {
5263
- return false;
5264
- }
5265
- for (const [key, value] of firstEntries) {
5266
- if (!strictDeepEqual(value, second[key])) {
5195
+ // ../../node_modules/jmespath/jmespath.js
5196
+ var require_jmespath = __commonJS(function(exports) {
5197
+ (function(exports2) {
5198
+ function isArray(obj) {
5199
+ if (obj !== null) {
5200
+ return Object.prototype.toString.call(obj) === "[object Array]";
5201
+ } else {
5267
5202
  return false;
5268
5203
  }
5269
- secondKeys.delete(key);
5270
5204
  }
5271
- return secondKeys.size === 0;
5272
- }
5273
- return false;
5274
- }, isFalse = (obj) => {
5275
- if (obj === null || obj === undefined || obj === false) {
5276
- return true;
5277
- }
5278
- if (typeof obj === "string") {
5279
- return obj === "";
5280
- }
5281
- if (typeof obj === "object") {
5282
- if (Array.isArray(obj)) {
5283
- return obj.length === 0;
5284
- }
5285
- if (obj === null) {
5286
- return true;
5287
- }
5288
- return Object.keys(obj).length === 0;
5289
- }
5290
- return false;
5291
- }, isAlpha = (ch) => {
5292
- return ch >= "a" && ch <= "z" || ch >= "A" && ch <= "Z" || ch === "_";
5293
- }, isNum = (ch) => {
5294
- return ch >= "0" && ch <= "9" || ch === "-";
5295
- }, isAlphaNum = (ch) => {
5296
- return ch >= "a" && ch <= "z" || ch >= "A" && ch <= "Z" || ch >= "0" && ch <= "9" || ch === "_";
5297
- }, ensureInteger = (value) => {
5298
- if (!(typeof value === "number") || Math.floor(value) !== value) {
5299
- throw new Error("invalid-value: expecting an integer.");
5300
- }
5301
- return value;
5302
- }, ensurePositiveInteger = (value) => {
5303
- if (!(typeof value === "number") || value < 0 || Math.floor(value) !== value) {
5304
- throw new Error("invalid-value: expecting a non-negative integer.");
5305
- }
5306
- return value;
5307
- }, ensureNumbers = (...operands) => {
5308
- for (let i = 0;i < operands.length; i++) {
5309
- if (operands[i] === null || operands[i] === undefined) {
5310
- throw new Error("not-a-number: undefined");
5311
- }
5312
- if (typeof operands[i] !== "number") {
5313
- throw new Error("not-a-number");
5314
- }
5315
- }
5316
- }, notZero = (n) => {
5317
- n = +n;
5318
- if (!n) {
5319
- throw new Error("not-a-number: divide by zero");
5320
- }
5321
- return n;
5322
- }, add = (left, right) => {
5323
- ensureNumbers(left, right);
5324
- const result = left + right;
5325
- return result;
5326
- }, sub = (left, right) => {
5327
- ensureNumbers(left, right);
5328
- const result = left - right;
5329
- return result;
5330
- }, mul = (left, right) => {
5331
- ensureNumbers(left, right);
5332
- const result = left * right;
5333
- return result;
5334
- }, divide = (left, right) => {
5335
- ensureNumbers(left, right);
5336
- const result = left / notZero(right);
5337
- return result;
5338
- }, div = (left, right) => {
5339
- ensureNumbers(left, right);
5340
- const result = Math.floor(left / notZero(right));
5341
- return result;
5342
- }, mod = (left, right) => {
5343
- ensureNumbers(left, right);
5344
- const result = left % right;
5345
- return result;
5346
- }, findFirst = (subject, sub2, start, end) => {
5347
- if (!subject || !sub2) {
5348
- return null;
5349
- }
5350
- start = Math.max(ensureInteger(start = start || 0), 0);
5351
- end = Math.min(ensureInteger(end = end || subject.length), subject.length);
5352
- const offset = subject.slice(start, end).indexOf(sub2);
5353
- return offset === -1 ? null : offset + start;
5354
- }, findLast = (subject, sub2, start, end) => {
5355
- if (!subject || !sub2) {
5356
- return null;
5357
- }
5358
- start = Math.max(ensureInteger(start = start || 0), 0);
5359
- end = Math.min(ensureInteger(end = end || subject.length), subject.length);
5360
- const offset = subject.slice(start, end).lastIndexOf(sub2);
5361
- const result = offset === -1 ? null : offset + start;
5362
- return result;
5363
- }, lower = (subject) => subject.toLowerCase(), ensurePadFuncParams = (name, width, padding) => {
5364
- padding = padding || " ";
5365
- if (padding.length > 1) {
5366
- throw new Error(`invalid value, ${name} expects its 'pad' parameter to be a valid string with a single codepoint`);
5367
- }
5368
- ensurePositiveInteger(width);
5369
- return padding;
5370
- }, padLeft = (subject, width, padding) => {
5371
- padding = ensurePadFuncParams("pad_left", width, padding);
5372
- return subject && subject.padStart(width, padding) || "";
5373
- }, padRight = (subject, width, padding) => {
5374
- padding = ensurePadFuncParams("pad_right", width, padding);
5375
- return subject && subject.padEnd(width, padding) || "";
5376
- }, replace = (subject, string, by, count) => {
5377
- if (count === 0) {
5378
- return subject;
5379
- }
5380
- if (!count) {
5381
- return subject.split(string).join(by);
5382
- }
5383
- ensurePositiveInteger(count);
5384
- [...Array(count).keys()].map(() => subject = subject.replace(string, by));
5385
- return subject;
5386
- }, split = (subject, search2, count) => {
5387
- if (subject.length == 0 && search2.length === 0) {
5388
- return [];
5389
- }
5390
- if (count === null || count === undefined) {
5391
- return subject.split(search2);
5392
- }
5393
- ensurePositiveInteger(count);
5394
- if (count === 0) {
5395
- return [subject];
5396
- }
5397
- const split2 = subject.split(search2);
5398
- return [...split2.slice(0, count), split2.slice(count).join(search2)];
5399
- }, trim = (subject, chars) => {
5400
- return trimLeft(trimRight(subject, chars), chars);
5401
- }, trimLeft = (subject, chars) => {
5402
- return trimImpl(subject, (list) => new RegExp(`^[${list}]*(.*?)`), chars);
5403
- }, trimRight = (subject, chars) => {
5404
- return trimImpl(subject, (list) => new RegExp(`(.*?)[${list}]*$`), chars);
5405
- }, trimImpl = (subject, regExper, chars) => {
5406
- const pattern = chars ? chars.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&") : "\\s…";
5407
- return subject.replace(regExper(pattern), "$1");
5408
- }, upper = (subject) => subject.toUpperCase(), basicTokens, operatorStartToken, skipChars, StreamLexer = class {
5409
- _current = 0;
5410
- _enable_legacy_literals = false;
5411
- tokenize(stream, options) {
5412
- const tokens = [];
5413
- this._current = 0;
5414
- this._enable_legacy_literals = options?.enable_legacy_literals || false;
5415
- let start;
5416
- let identifier;
5417
- let token;
5418
- while (this._current < stream.length) {
5419
- if (isAlpha(stream[this._current])) {
5420
- start = this._current;
5421
- identifier = this.consumeUnquotedIdentifier(stream);
5422
- tokens.push({
5423
- start,
5424
- type: "UnquotedIdentifier",
5425
- value: identifier
5426
- });
5427
- } else if (basicTokens[stream[this._current]] !== undefined) {
5428
- tokens.push({
5429
- start: this._current,
5430
- type: basicTokens[stream[this._current]],
5431
- value: stream[this._current]
5432
- });
5433
- this._current += 1;
5434
- } else if (stream[this._current] === "$") {
5435
- start = this._current;
5436
- if (this._current + 1 < stream.length && isAlpha(stream[this._current + 1])) {
5437
- this._current += 1;
5438
- identifier = this.consumeUnquotedIdentifier(stream);
5439
- tokens.push({
5440
- start,
5441
- type: "Variable",
5442
- value: identifier
5443
- });
5444
- } else {
5445
- tokens.push({
5446
- start,
5447
- type: "Root",
5448
- value: stream[this._current]
5449
- });
5450
- this._current += 1;
5451
- }
5452
- } else if (stream[this._current] === "-") {
5453
- if (this._current + 1 < stream.length && isNum(stream[this._current + 1])) {
5454
- const token2 = this.consumeNumber(stream);
5455
- token2 && tokens.push(token2);
5456
- } else {
5457
- const token2 = {
5458
- start: this._current,
5459
- type: "Minus",
5460
- value: "-"
5461
- };
5462
- tokens.push(token2);
5463
- this._current += 1;
5464
- }
5465
- } else if (isNum(stream[this._current])) {
5466
- token = this.consumeNumber(stream);
5467
- tokens.push(token);
5468
- } else if (stream[this._current] === "[") {
5469
- token = this.consumeLBracket(stream);
5470
- tokens.push(token);
5471
- } else if (stream[this._current] === '"') {
5472
- start = this._current;
5473
- identifier = this.consumeQuotedIdentifier(stream);
5474
- tokens.push({
5475
- start,
5476
- type: "QuotedIdentifier",
5477
- value: identifier
5478
- });
5479
- } else if (stream[this._current] === `'`) {
5480
- start = this._current;
5481
- identifier = this.consumeRawStringLiteral(stream);
5482
- tokens.push({
5483
- start,
5484
- type: "Literal",
5485
- value: identifier
5486
- });
5487
- } else if (stream[this._current] === "`") {
5488
- start = this._current;
5489
- const literal = this.consumeLiteral(stream);
5490
- tokens.push({
5491
- start,
5492
- type: "Literal",
5493
- value: literal
5494
- });
5495
- } else if (operatorStartToken[stream[this._current]] !== undefined) {
5496
- token = this.consumeOperator(stream);
5497
- token && tokens.push(token);
5498
- } else if (skipChars[stream[this._current]] !== undefined) {
5499
- this._current += 1;
5205
+ function isObject(obj) {
5206
+ if (obj !== null) {
5207
+ return Object.prototype.toString.call(obj) === "[object Object]";
5500
5208
  } else {
5501
- const error = new Error(`Syntax error: unknown character: ${stream[this._current]}`);
5502
- error.name = "LexerError";
5503
- throw error;
5209
+ return false;
5504
5210
  }
5505
5211
  }
5506
- return tokens;
5507
- }
5508
- consumeUnquotedIdentifier(stream) {
5509
- const start = this._current;
5510
- this._current += 1;
5511
- while (this._current < stream.length && isAlphaNum(stream[this._current])) {
5512
- this._current += 1;
5513
- }
5514
- return stream.slice(start, this._current);
5515
- }
5516
- consumeQuotedIdentifier(stream) {
5517
- const start = this._current;
5518
- this._current += 1;
5519
- const maxLength = stream.length;
5520
- while (stream[this._current] !== '"' && this._current < maxLength) {
5521
- let current = this._current;
5522
- if (stream[current] === "\\" && (stream[current + 1] === "\\" || stream[current + 1] === '"')) {
5523
- current += 2;
5524
- } else {
5525
- current += 1;
5212
+ function strictDeepEqual(first, second) {
5213
+ if (first === second) {
5214
+ return true;
5526
5215
  }
5527
- this._current = current;
5528
- }
5529
- this._current += 1;
5530
- const [value, ok] = this.parseJSON(stream.slice(start, this._current));
5531
- if (!ok) {
5532
- const error = new Error(`syntax: unexpected end of JSON input`);
5533
- error.name = "LexerError";
5534
- throw error;
5535
- }
5536
- return value;
5537
- }
5538
- consumeRawStringLiteral(stream) {
5539
- const start = this._current;
5540
- this._current += 1;
5541
- const maxLength = stream.length;
5542
- while (stream[this._current] !== `'` && this._current < maxLength) {
5543
- let current = this._current;
5544
- if (stream[current] === "\\" && (stream[current + 1] === "\\" || stream[current + 1] === `'`)) {
5545
- current += 2;
5546
- } else {
5547
- current += 1;
5548
- }
5549
- this._current = current;
5550
- }
5551
- this._current += 1;
5552
- const literal = stream.slice(start + 1, this._current - 1);
5553
- return replace(replace(literal, `\\\\`, `\\`), `\\'`, `'`);
5554
- }
5555
- consumeNumber(stream) {
5556
- const start = this._current;
5557
- this._current += 1;
5558
- const maxLength = stream.length;
5559
- while (isNum(stream[this._current]) && this._current < maxLength) {
5560
- this._current += 1;
5561
- }
5562
- const value = parseInt(stream.slice(start, this._current), 10);
5563
- return { start, value, type: "Number" };
5564
- }
5565
- consumeLBracket(stream) {
5566
- const start = this._current;
5567
- this._current += 1;
5568
- if (stream[this._current] === "?") {
5569
- this._current += 1;
5570
- return { start, type: "Filter", value: "[?" };
5571
- }
5572
- if (stream[this._current] === "]") {
5573
- this._current += 1;
5574
- return { start, type: "Flatten", value: "[]" };
5575
- }
5576
- return { start, type: "Lbracket", value: "[" };
5577
- }
5578
- consumeOrElse(stream, peek, token, orElse) {
5579
- const start = this._current;
5580
- this._current += 1;
5581
- if (this._current < stream.length && stream[this._current] === peek) {
5582
- this._current += 1;
5583
- return {
5584
- start,
5585
- type: orElse,
5586
- value: stream.slice(start, this._current)
5587
- };
5588
- }
5589
- return { start, type: token, value: stream[start] };
5590
- }
5591
- consumeOperator(stream) {
5592
- const start = this._current;
5593
- const startingChar = stream[start];
5594
- switch (startingChar) {
5595
- case "!":
5596
- return this.consumeOrElse(stream, "=", "Not", "NE");
5597
- case "<":
5598
- return this.consumeOrElse(stream, "=", "LT", "LTE");
5599
- case ">":
5600
- return this.consumeOrElse(stream, "=", "GT", "GTE");
5601
- case "=":
5602
- return this.consumeOrElse(stream, "=", "Assign", "EQ");
5603
- case "&":
5604
- return this.consumeOrElse(stream, "&", "Expref", "And");
5605
- case "|":
5606
- return this.consumeOrElse(stream, "|", "Pipe", "Or");
5607
- case "/":
5608
- return this.consumeOrElse(stream, "/", "Divide", "Div");
5609
- }
5610
- }
5611
- consumeLiteral(stream) {
5612
- this._current += 1;
5613
- const start = this._current;
5614
- const maxLength = stream.length;
5615
- while (stream[this._current] !== "`" && this._current < maxLength) {
5616
- let current = this._current;
5617
- if (stream[current] === "\\" && (stream[current + 1] === "\\" || stream[current + 1] === "`")) {
5618
- current += 2;
5619
- } else {
5620
- current += 1;
5216
+ var firstType = Object.prototype.toString.call(first);
5217
+ if (firstType !== Object.prototype.toString.call(second)) {
5218
+ return false;
5621
5219
  }
5622
- this._current = current;
5623
- }
5624
- let literalString = stream.slice(start, this._current).trimStart();
5625
- literalString = literalString.replace("\\`", "`");
5626
- let literal = null;
5627
- let ok = false;
5628
- if (this.looksLikeJSON(literalString)) {
5629
- [literal, ok] = this.parseJSON(literalString);
5630
- }
5631
- if (!ok && this._enable_legacy_literals) {
5632
- [literal, ok] = this.parseJSON(`"${literalString}"`);
5633
- }
5634
- if (!ok) {
5635
- const error = new Error(`Syntax error: unexpected end of JSON input or invalid format for a JSON literal: ${stream[this._current]}`);
5636
- error.name = "LexerError";
5637
- throw error;
5638
- }
5639
- this._current += 1;
5640
- return literal;
5641
- }
5642
- looksLikeJSON(literalString) {
5643
- const startingChars = '[{"';
5644
- const jsonLiterals = ["true", "false", "null"];
5645
- const numberLooking = "-0123456789";
5646
- if (literalString === "") {
5647
- return false;
5648
- }
5649
- if (startingChars.includes(literalString[0])) {
5650
- return true;
5651
- }
5652
- if (jsonLiterals.includes(literalString)) {
5653
- return true;
5654
- }
5655
- if (numberLooking.includes(literalString[0])) {
5656
- const [_, ok] = this.parseJSON(literalString);
5657
- return ok;
5658
- }
5659
- return false;
5660
- }
5661
- parseJSON(text) {
5662
- try {
5663
- const json2 = JSON.parse(text);
5664
- return [json2, true];
5665
- } catch {
5666
- return [null, false];
5667
- }
5668
- }
5669
- }, Lexer, Lexer_default, bindingPower, TokenParser = class _TokenParser {
5670
- index = 0;
5671
- tokens = [];
5672
- parse(expression, options) {
5673
- this.loadTokens(expression, options || { enable_legacy_literals: false });
5674
- this.index = 0;
5675
- const ast = this.expression(0);
5676
- if (this.lookahead(0) !== "EOF") {
5677
- const token = this.lookaheadToken(0);
5678
- this.errorToken(token, `Syntax error: unexpected token type: ${token.type}, value: ${token.value}`);
5679
- }
5680
- return ast;
5681
- }
5682
- loadTokens(expression, options) {
5683
- this.tokens = Lexer_default.tokenize(expression, options);
5684
- this.tokens.push({ type: "EOF", value: "", start: expression.length });
5685
- }
5686
- expression(rbp) {
5687
- const leftToken = this.lookaheadToken(0);
5688
- this.advance();
5689
- let left = this.nud(leftToken);
5690
- let currentTokenType = this.lookahead(0);
5691
- while (rbp < bindingPower[currentTokenType]) {
5692
- this.advance();
5693
- left = this.led(currentTokenType, left);
5694
- currentTokenType = this.lookahead(0);
5695
- }
5696
- return left;
5697
- }
5698
- lookahead(offset) {
5699
- return this.tokens[this.index + offset].type;
5700
- }
5701
- lookaheadToken(offset) {
5702
- return this.tokens[this.index + offset];
5703
- }
5704
- advance() {
5705
- this.index += 1;
5706
- }
5707
- nud(token) {
5708
- switch (token.type) {
5709
- case "Variable":
5710
- return { type: "Variable", name: token.value };
5711
- case "Literal":
5712
- return { type: "Literal", value: token.value };
5713
- case "UnquotedIdentifier": {
5714
- if (_TokenParser.isKeyword(token, "let") && this.lookahead(0) === "Variable") {
5715
- return this.parseLetExpression();
5716
- } else {
5717
- return { type: "Field", name: token.value };
5220
+ if (isArray(first) === true) {
5221
+ if (first.length !== second.length) {
5222
+ return false;
5718
5223
  }
5719
- }
5720
- case "QuotedIdentifier":
5721
- if (this.lookahead(0) === "Lparen") {
5722
- throw new Error("Syntax error: quoted identifier not allowed for function names.");
5723
- } else {
5724
- return { type: "Field", name: token.value };
5224
+ for (var i = 0;i < first.length; i++) {
5225
+ if (strictDeepEqual(first[i], second[i]) === false) {
5226
+ return false;
5227
+ }
5725
5228
  }
5726
- case "Not": {
5727
- const child = this.expression(bindingPower.Not);
5728
- return { type: "NotExpression", child };
5729
- }
5730
- case "Minus": {
5731
- const child = this.expression(bindingPower.Minus);
5732
- return {
5733
- type: "Unary",
5734
- operator: token.type,
5735
- operand: child
5736
- };
5737
- }
5738
- case "Plus": {
5739
- const child = this.expression(bindingPower.Plus);
5740
- return {
5741
- type: "Unary",
5742
- operator: token.type,
5743
- operand: child
5744
- };
5229
+ return true;
5745
5230
  }
5746
- case "Star": {
5747
- const left = { type: "Identity" };
5748
- return { type: "ValueProjection", left, right: this.parseProjectionRHS(bindingPower.Star) };
5749
- }
5750
- case "Filter":
5751
- return this.led(token.type, { type: "Identity" });
5752
- case "Lbrace":
5753
- return this.parseMultiselectHash();
5754
- case "Flatten": {
5755
- const left = {
5756
- type: "Flatten",
5757
- child: { type: "Identity" }
5758
- };
5759
- const right = this.parseProjectionRHS(bindingPower.Flatten);
5760
- return { type: "Projection", left, right };
5761
- }
5762
- case "Lbracket": {
5763
- if (this.lookahead(0) === "Number" || this.lookahead(0) === "Colon") {
5764
- const right = this.parseIndexExpression();
5765
- return this.projectIfSlice({ type: "Identity" }, right);
5766
- }
5767
- if (this.lookahead(0) === "Star" && this.lookahead(1) === "Rbracket") {
5768
- this.advance();
5769
- this.advance();
5770
- const right = this.parseProjectionRHS(bindingPower.Star);
5771
- return {
5772
- left: { type: "Identity" },
5773
- right,
5774
- type: "Projection"
5775
- };
5231
+ if (isObject(first) === true) {
5232
+ var keysSeen = {};
5233
+ for (var key in first) {
5234
+ if (hasOwnProperty.call(first, key)) {
5235
+ if (strictDeepEqual(first[key], second[key]) === false) {
5236
+ return false;
5237
+ }
5238
+ keysSeen[key] = true;
5239
+ }
5776
5240
  }
5777
- return this.parseMultiselectList();
5778
- }
5779
- case "Current":
5780
- return { type: "Current" };
5781
- case "Root":
5782
- return { type: "Root" };
5783
- case "Expref": {
5784
- const child = this.expression(bindingPower.Expref);
5785
- return { type: "ExpressionReference", child };
5241
+ for (var key2 in second) {
5242
+ if (hasOwnProperty.call(second, key2)) {
5243
+ if (keysSeen[key2] !== true) {
5244
+ return false;
5245
+ }
5246
+ }
5247
+ }
5248
+ return true;
5786
5249
  }
5787
- case "Lparen": {
5788
- const expression = this.expression(0);
5789
- this.match("Rparen");
5790
- return expression;
5250
+ return false;
5251
+ }
5252
+ function isFalse(obj) {
5253
+ if (obj === "" || obj === false || obj === null) {
5254
+ return true;
5255
+ } else if (isArray(obj) && obj.length === 0) {
5256
+ return true;
5257
+ } else if (isObject(obj)) {
5258
+ for (var key in obj) {
5259
+ if (obj.hasOwnProperty(key)) {
5260
+ return false;
5261
+ }
5262
+ }
5263
+ return true;
5264
+ } else {
5265
+ return false;
5791
5266
  }
5792
- default:
5793
- this.errorToken(token);
5794
5267
  }
5795
- }
5796
- led(tokenName, left) {
5797
- switch (tokenName) {
5798
- case "Question": {
5799
- const trueExpr = this.expression(0);
5800
- this.match("Colon");
5801
- const falseExpr = this.expression(0);
5802
- return {
5803
- type: "Ternary",
5804
- condition: left,
5805
- trueExpr,
5806
- falseExpr
5807
- };
5268
+ function objValues(obj) {
5269
+ var keys = Object.keys(obj);
5270
+ var values = [];
5271
+ for (var i = 0;i < keys.length; i++) {
5272
+ values.push(obj[keys[i]]);
5808
5273
  }
5809
- case "Dot": {
5810
- const rbp = bindingPower.Dot;
5811
- if (this.lookahead(0) !== "Star") {
5812
- const right2 = this.parseDotRHS(rbp);
5813
- return { type: "Subexpression", left, right: right2 };
5814
- }
5815
- this.advance();
5816
- const right = this.parseProjectionRHS(rbp);
5817
- return { type: "ValueProjection", left, right };
5818
- }
5819
- case "Pipe": {
5820
- const right = this.expression(bindingPower.Pipe);
5821
- return { type: "Pipe", left, right };
5822
- }
5823
- case "Or": {
5824
- const right = this.expression(bindingPower.Or);
5825
- return { type: "OrExpression", left, right };
5826
- }
5827
- case "And": {
5828
- const right = this.expression(bindingPower.And);
5829
- return { type: "AndExpression", left, right };
5830
- }
5831
- case "Lparen": {
5832
- if (left.type !== "Field") {
5833
- throw new Error("Syntax error: expected a Field node");
5834
- }
5835
- const name = left.name;
5836
- const args = this.parseCommaSeparatedExpressionsUntilToken("Rparen");
5837
- const node = { name, type: "Function", children: args };
5838
- return node;
5839
- }
5840
- case "Filter": {
5841
- const condition = this.expression(0);
5842
- this.match("Rbracket");
5843
- const right = this.lookahead(0) === "Flatten" ? { type: "Identity" } : this.parseProjectionRHS(bindingPower.Filter);
5844
- return { type: "FilterProjection", left, right, condition };
5845
- }
5846
- case "Flatten": {
5847
- const leftNode = { type: "Flatten", child: left };
5848
- const right = this.parseProjectionRHS(bindingPower.Flatten);
5849
- return { type: "Projection", left: leftNode, right };
5850
- }
5851
- case "Assign": {
5852
- const leftNode = left;
5853
- const right = this.expression(0);
5854
- return {
5855
- type: "Binding",
5856
- variable: leftNode.name,
5857
- reference: right
5858
- };
5274
+ return values;
5275
+ }
5276
+ function merge2(a, b) {
5277
+ var merged = {};
5278
+ for (var key in a) {
5279
+ merged[key] = a[key];
5859
5280
  }
5860
- case "EQ":
5861
- case "NE":
5862
- case "GT":
5863
- case "GTE":
5864
- case "LT":
5865
- case "LTE":
5866
- return this.parseComparator(left, tokenName);
5867
- case "Plus":
5868
- case "Minus":
5869
- case "Multiply":
5870
- case "Star":
5871
- case "Divide":
5872
- case "Modulo":
5873
- case "Div":
5874
- return this.parseArithmetic(left, tokenName);
5875
- case "Lbracket": {
5876
- const token = this.lookaheadToken(0);
5877
- if (token.type === "Number" || token.type === "Colon") {
5878
- const right2 = this.parseIndexExpression();
5879
- return this.projectIfSlice(left, right2);
5880
- }
5881
- this.match("Star");
5882
- this.match("Rbracket");
5883
- const right = this.parseProjectionRHS(bindingPower.Star);
5884
- return { type: "Projection", left, right };
5281
+ for (var key2 in b) {
5282
+ merged[key2] = b[key2];
5885
5283
  }
5886
- default:
5887
- return this.errorToken(this.lookaheadToken(0));
5284
+ return merged;
5888
5285
  }
5889
- }
5890
- static isKeyword(token, keyword) {
5891
- return token.type === "UnquotedIdentifier" && token.value === keyword;
5892
- }
5893
- match(tokenType) {
5894
- if (this.lookahead(0) === tokenType) {
5895
- this.advance();
5896
- return;
5286
+ var trimLeft;
5287
+ if (typeof String.prototype.trimLeft === "function") {
5288
+ trimLeft = function(str2) {
5289
+ return str2.trimLeft();
5290
+ };
5897
5291
  } else {
5898
- const token = this.lookaheadToken(0);
5899
- this.errorToken(token, `Syntax error: expected ${tokenType}, got: ${token.type}`);
5900
- }
5901
- }
5902
- errorToken(token, message = "") {
5903
- const error = new Error(message || `Syntax error: invalid token (${token.type}): "${token.value}"`);
5904
- error.name = "ParserError";
5905
- throw error;
5906
- }
5907
- parseIndexExpression() {
5908
- if (this.lookahead(0) === "Colon" || this.lookahead(1) === "Colon") {
5909
- return this.parseSliceExpression();
5910
- }
5911
- const value = Number(this.lookaheadToken(0).value);
5912
- this.advance();
5913
- this.match("Rbracket");
5914
- return { type: "Index", value };
5915
- }
5916
- projectIfSlice(left, right) {
5917
- const indexExpr = {
5918
- type: "IndexExpression",
5919
- left,
5920
- right
5921
- };
5922
- if (right.type === "Slice") {
5923
- return {
5924
- left: indexExpr,
5925
- right: this.parseProjectionRHS(bindingPower.Star),
5926
- type: "Projection"
5292
+ trimLeft = function(str2) {
5293
+ return str2.match(/^\s*(.*)/)[1];
5927
5294
  };
5928
5295
  }
5929
- return indexExpr;
5930
- }
5931
- parseSliceExpression() {
5932
- const parts = [null, null, null];
5933
- let index = 0;
5934
- let current = this.lookaheadToken(0);
5935
- while (current.type != "Rbracket" && index < 3) {
5936
- if (current.type === "Colon") {
5937
- index++;
5938
- if (index === 3) {
5939
- this.errorToken(this.lookaheadToken(0), "Syntax error, too many colons in slice expression");
5940
- }
5941
- this.advance();
5942
- } else if (current.type === "Number") {
5943
- const part = this.lookaheadToken(0).value;
5944
- parts[index] = part;
5945
- this.advance();
5946
- } else {
5947
- const next = this.lookaheadToken(0);
5948
- this.errorToken(next, `Syntax error, unexpected token: ${next.value}(${next.type})`);
5949
- }
5950
- current = this.lookaheadToken(0);
5951
- }
5952
- this.match("Rbracket");
5953
- const [start, stop, step] = parts;
5954
- return { type: "Slice", start, stop, step };
5955
- }
5956
- parseLetExpression() {
5957
- const separated = this.parseCommaSeparatedExpressionsUntilKeyword("in");
5958
- const expression = this.expression(0);
5959
- const bindings = separated.map((binding) => binding);
5960
- return {
5961
- type: "LetExpression",
5962
- bindings,
5963
- expression
5296
+ var TYPE_NUMBER = 0;
5297
+ var TYPE_ANY = 1;
5298
+ var TYPE_STRING = 2;
5299
+ var TYPE_ARRAY = 3;
5300
+ var TYPE_OBJECT = 4;
5301
+ var TYPE_BOOLEAN = 5;
5302
+ var TYPE_EXPREF = 6;
5303
+ var TYPE_NULL = 7;
5304
+ var TYPE_ARRAY_NUMBER = 8;
5305
+ var TYPE_ARRAY_STRING = 9;
5306
+ var TYPE_NAME_TABLE = {
5307
+ 0: "number",
5308
+ 1: "any",
5309
+ 2: "string",
5310
+ 3: "array",
5311
+ 4: "object",
5312
+ 5: "boolean",
5313
+ 6: "expression",
5314
+ 7: "null",
5315
+ 8: "Array<number>",
5316
+ 9: "Array<string>"
5964
5317
  };
5965
- }
5966
- parseCommaSeparatedExpressionsUntilKeyword(keyword) {
5967
- return this.parseCommaSeparatedExpressionsUntil(() => {
5968
- return _TokenParser.isKeyword(this.lookaheadToken(0), keyword);
5969
- }, () => {
5970
- this.advance();
5971
- });
5972
- }
5973
- parseCommaSeparatedExpressionsUntilToken(token) {
5974
- return this.parseCommaSeparatedExpressionsUntil(() => {
5975
- return this.lookahead(0) === token;
5976
- }, () => {
5977
- return this.match(token);
5978
- });
5979
- }
5980
- parseCommaSeparatedExpressionsUntil(isEndToken, matchEndToken) {
5981
- const args = [];
5982
- let expression;
5983
- while (!isEndToken()) {
5984
- expression = this.expression(0);
5985
- if (this.lookahead(0) === "Comma") {
5986
- this.match("Comma");
5987
- }
5988
- args.push(expression);
5989
- }
5990
- matchEndToken();
5991
- return args;
5992
- }
5993
- parseComparator(left, comparator) {
5994
- const right = this.expression(bindingPower[comparator]);
5995
- return { type: "Comparator", name: comparator, left, right };
5996
- }
5997
- parseArithmetic(left, operator) {
5998
- const right = this.expression(bindingPower[operator]);
5999
- return { type: "Arithmetic", operator, left, right };
6000
- }
6001
- parseDotRHS(rbp) {
6002
- const lookahead = this.lookahead(0);
6003
- const exprTokens = ["UnquotedIdentifier", "QuotedIdentifier", "Star"];
6004
- if (exprTokens.includes(lookahead)) {
6005
- return this.expression(rbp);
6006
- }
6007
- if (lookahead === "Lbracket") {
6008
- this.match("Lbracket");
6009
- return this.parseMultiselectList();
6010
- }
6011
- if (lookahead === "Lbrace") {
6012
- this.match("Lbrace");
6013
- return this.parseMultiselectHash();
6014
- }
6015
- const token = this.lookaheadToken(0);
6016
- this.errorToken(token, `Syntax error, unexpected token: ${token.value}(${token.type})`);
6017
- }
6018
- parseProjectionRHS(rbp) {
6019
- if (bindingPower[this.lookahead(0)] < 10) {
6020
- return { type: "Identity" };
6021
- }
6022
- if (this.lookahead(0) === "Lbracket") {
6023
- return this.expression(rbp);
6024
- }
6025
- if (this.lookahead(0) === "Filter") {
6026
- return this.expression(rbp);
6027
- }
6028
- if (this.lookahead(0) === "Dot") {
6029
- this.match("Dot");
6030
- return this.parseDotRHS(rbp);
6031
- }
6032
- const token = this.lookaheadToken(0);
6033
- this.errorToken(token, `Syntax error, unexpected token: ${token.value}(${token.type})`);
6034
- }
6035
- parseMultiselectList() {
6036
- const expressions = [];
6037
- while (this.lookahead(0) !== "Rbracket") {
6038
- const expression = this.expression(0);
6039
- expressions.push(expression);
6040
- if (this.lookahead(0) === "Comma") {
6041
- this.match("Comma");
6042
- if (this.lookahead(0) === "Rbracket") {
6043
- throw new Error("Syntax error: unexpected token Rbracket");
6044
- }
6045
- }
6046
- }
6047
- this.match("Rbracket");
6048
- return { type: "MultiSelectList", children: expressions };
6049
- }
6050
- parseMultiselectHash() {
6051
- const pairs2 = [];
6052
- const identifierTypes = ["UnquotedIdentifier", "QuotedIdentifier"];
6053
- let keyToken;
6054
- let keyName;
6055
- let value;
6056
- for (;; ) {
6057
- keyToken = this.lookaheadToken(0);
6058
- if (!identifierTypes.includes(keyToken.type)) {
6059
- throw new Error(`Syntax error: expecting an identifier token, got: ${keyToken.type}`);
6060
- }
6061
- keyName = keyToken.value;
6062
- this.advance();
6063
- this.match("Colon");
6064
- value = this.expression(0);
6065
- pairs2.push({ value, type: "KeyValuePair", name: keyName });
6066
- if (this.lookahead(0) === "Comma") {
6067
- this.match("Comma");
6068
- } else if (this.lookahead(0) === "Rbrace") {
6069
- this.match("Rbrace");
6070
- break;
6071
- }
6072
- }
6073
- return { type: "MultiSelectHash", children: pairs2 };
6074
- }
6075
- }, Parser, Parser_default, Text = class _Text {
6076
- _text;
6077
- constructor(text) {
6078
- this._text = text;
6079
- }
6080
- get string() {
6081
- return this._text;
6082
- }
6083
- get length() {
6084
- return this.codePoints.length;
6085
- }
6086
- compareTo(other) {
6087
- return _Text.compare(this, new _Text(other));
6088
- }
6089
- static get comparer() {
6090
- const stringComparer = (left, right) => {
6091
- return new _Text(left).compareTo(right);
5318
+ var TOK_EOF = "EOF";
5319
+ var TOK_UNQUOTEDIDENTIFIER = "UnquotedIdentifier";
5320
+ var TOK_QUOTEDIDENTIFIER = "QuotedIdentifier";
5321
+ var TOK_RBRACKET = "Rbracket";
5322
+ var TOK_RPAREN = "Rparen";
5323
+ var TOK_COMMA = "Comma";
5324
+ var TOK_COLON = "Colon";
5325
+ var TOK_RBRACE = "Rbrace";
5326
+ var TOK_NUMBER = "Number";
5327
+ var TOK_CURRENT = "Current";
5328
+ var TOK_EXPREF = "Expref";
5329
+ var TOK_PIPE = "Pipe";
5330
+ var TOK_OR = "Or";
5331
+ var TOK_AND = "And";
5332
+ var TOK_EQ = "EQ";
5333
+ var TOK_GT = "GT";
5334
+ var TOK_LT = "LT";
5335
+ var TOK_GTE = "GTE";
5336
+ var TOK_LTE = "LTE";
5337
+ var TOK_NE = "NE";
5338
+ var TOK_FLATTEN = "Flatten";
5339
+ var TOK_STAR = "Star";
5340
+ var TOK_FILTER = "Filter";
5341
+ var TOK_DOT = "Dot";
5342
+ var TOK_NOT = "Not";
5343
+ var TOK_LBRACE = "Lbrace";
5344
+ var TOK_LBRACKET = "Lbracket";
5345
+ var TOK_LPAREN = "Lparen";
5346
+ var TOK_LITERAL = "Literal";
5347
+ var basicTokens = {
5348
+ ".": TOK_DOT,
5349
+ "*": TOK_STAR,
5350
+ ",": TOK_COMMA,
5351
+ ":": TOK_COLON,
5352
+ "{": TOK_LBRACE,
5353
+ "}": TOK_RBRACE,
5354
+ "]": TOK_RBRACKET,
5355
+ "(": TOK_LPAREN,
5356
+ ")": TOK_RPAREN,
5357
+ "@": TOK_CURRENT
6092
5358
  };
6093
- return stringComparer;
6094
- }
6095
- static compare(left, right) {
6096
- const leftCp = left.codePoints;
6097
- const rightCp = right.codePoints;
6098
- for (let index = 0;index < Math.min(leftCp.length, rightCp.length); index++) {
6099
- if (leftCp[index] === rightCp[index]) {
6100
- continue;
6101
- }
6102
- return leftCp[index] - rightCp[index] > 0 ? 1 : -1;
6103
- }
6104
- return leftCp.length - rightCp.length > 0 ? 1 : -1;
6105
- }
6106
- reverse() {
6107
- return String.fromCodePoint(...this.codePoints.reverse());
6108
- }
6109
- get codePoints() {
6110
- const array = [...this._text].map((s) => s.codePointAt(0));
6111
- return array;
6112
- }
6113
- }, createMathFunction = (mathFn) => ([value]) => mathFn(value), createStringFunction = (stringFn) => ([subject]) => stringFn(subject), createObjectFunction = (objFn) => ([obj]) => objFn(obj), Runtime = class {
6114
- _interpreter;
6115
- _functionTable;
6116
- _customFunctions = /* @__PURE__ */ new Set;
6117
- TYPE_NAME_TABLE = Object.freeze({
6118
- [0]: "number",
6119
- [1]: "any",
6120
- [2]: "string",
6121
- [3]: "array",
6122
- [4]: "object",
6123
- [5]: "boolean",
6124
- [6]: "expression",
6125
- [7]: "null",
6126
- [8]: "Array<number>",
6127
- [10]: "Array<object>",
6128
- [9]: "Array<string>",
6129
- [11]: "Array<Array<any>>"
6130
- });
6131
- constructor(interpreter) {
6132
- this._interpreter = interpreter;
6133
- this._functionTable = this.buildFunctionTable();
6134
- }
6135
- buildFunctionTable() {
6136
- return {
6137
- abs: { _func: createMathFunction(Math.abs), _signature: [{ types: [0] }] },
6138
- ceil: { _func: createMathFunction(Math.ceil), _signature: [{ types: [0] }] },
6139
- floor: { _func: createMathFunction(Math.floor), _signature: [{ types: [0] }] },
6140
- lower: { _func: createStringFunction(lower), _signature: [{ types: [2] }] },
6141
- upper: { _func: createStringFunction(upper), _signature: [{ types: [2] }] },
6142
- keys: { _func: createObjectFunction(Object.keys), _signature: [{ types: [4] }] },
6143
- values: { _func: createObjectFunction(Object.values), _signature: [{ types: [4] }] },
6144
- avg: { _func: this.functionAvg, _signature: [{ types: [8] }] },
6145
- contains: {
6146
- _func: this.functionContains,
6147
- _signature: [
6148
- { types: [2, 3] },
6149
- { types: [1] }
6150
- ]
6151
- },
6152
- ends_with: {
6153
- _func: this.functionEndsWith,
6154
- _signature: [{ types: [2] }, { types: [2] }]
6155
- },
6156
- find_first: {
6157
- _func: this.functionFindFirst,
6158
- _signature: [
6159
- { types: [2] },
6160
- { types: [2] },
6161
- { types: [0], optional: true },
6162
- { types: [0], optional: true }
6163
- ]
6164
- },
6165
- find_last: {
6166
- _func: this.functionFindLast,
6167
- _signature: [
6168
- { types: [2] },
6169
- { types: [2] },
6170
- { types: [0], optional: true },
6171
- { types: [0], optional: true }
6172
- ]
6173
- },
6174
- from_items: { _func: this.functionFromItems, _signature: [{ types: [11] }] },
6175
- group_by: {
6176
- _func: this.functionGroupBy,
6177
- _signature: [{ types: [3] }, { types: [6] }]
6178
- },
6179
- items: { _func: this.functionItems, _signature: [{ types: [4] }] },
6180
- join: {
6181
- _func: this.functionJoin,
6182
- _signature: [{ types: [2] }, { types: [9] }]
6183
- },
6184
- length: {
6185
- _func: this.functionLength,
6186
- _signature: [{ types: [2, 3, 4] }]
6187
- },
6188
- map: {
6189
- _func: this.functionMap,
6190
- _signature: [{ types: [6] }, { types: [3] }]
6191
- },
6192
- max: {
6193
- _func: this.functionMax,
6194
- _signature: [{ types: [8, 9] }]
6195
- },
6196
- max_by: {
6197
- _func: this.functionMaxBy,
6198
- _signature: [{ types: [3] }, { types: [6] }]
6199
- },
6200
- merge: { _func: this.functionMerge, _signature: [{ types: [4], variadic: true }] },
6201
- min: {
6202
- _func: this.functionMin,
6203
- _signature: [{ types: [8, 9] }]
6204
- },
6205
- min_by: {
6206
- _func: this.functionMinBy,
6207
- _signature: [{ types: [3] }, { types: [6] }]
6208
- },
6209
- not_null: { _func: this.functionNotNull, _signature: [{ types: [1], variadic: true }] },
6210
- pad_left: {
6211
- _func: this.functionPadLeft,
6212
- _signature: [
6213
- { types: [2] },
6214
- { types: [0] },
6215
- { types: [2], optional: true }
6216
- ]
6217
- },
6218
- pad_right: {
6219
- _func: this.functionPadRight,
6220
- _signature: [
6221
- { types: [2] },
6222
- { types: [0] },
6223
- { types: [2], optional: true }
6224
- ]
6225
- },
6226
- replace: {
6227
- _func: this.functionReplace,
6228
- _signature: [
6229
- { types: [2] },
6230
- { types: [2] },
6231
- { types: [2] },
6232
- { types: [0], optional: true }
6233
- ]
6234
- },
6235
- reverse: {
6236
- _func: this.functionReverse,
6237
- _signature: [{ types: [2, 3] }]
6238
- },
6239
- sort: {
6240
- _func: this.functionSort,
6241
- _signature: [{ types: [9, 8] }]
5359
+ var operatorStartToken = {
5360
+ "<": true,
5361
+ ">": true,
5362
+ "=": true,
5363
+ "!": true
5364
+ };
5365
+ var skipChars = {
5366
+ " ": true,
5367
+ "\t": true,
5368
+ "\n": true
5369
+ };
5370
+ function isAlpha(ch) {
5371
+ return ch >= "a" && ch <= "z" || ch >= "A" && ch <= "Z" || ch === "_";
5372
+ }
5373
+ function isNum(ch) {
5374
+ return ch >= "0" && ch <= "9" || ch === "-";
5375
+ }
5376
+ function isAlphaNum(ch) {
5377
+ return ch >= "a" && ch <= "z" || ch >= "A" && ch <= "Z" || ch >= "0" && ch <= "9" || ch === "_";
5378
+ }
5379
+ function Lexer() {}
5380
+ Lexer.prototype = {
5381
+ tokenize: function(stream) {
5382
+ var tokens = [];
5383
+ this._current = 0;
5384
+ var start;
5385
+ var identifier;
5386
+ var token;
5387
+ while (this._current < stream.length) {
5388
+ if (isAlpha(stream[this._current])) {
5389
+ start = this._current;
5390
+ identifier = this._consumeUnquotedIdentifier(stream);
5391
+ tokens.push({
5392
+ type: TOK_UNQUOTEDIDENTIFIER,
5393
+ value: identifier,
5394
+ start
5395
+ });
5396
+ } else if (basicTokens[stream[this._current]] !== undefined) {
5397
+ tokens.push({
5398
+ type: basicTokens[stream[this._current]],
5399
+ value: stream[this._current],
5400
+ start: this._current
5401
+ });
5402
+ this._current++;
5403
+ } else if (isNum(stream[this._current])) {
5404
+ token = this._consumeNumber(stream);
5405
+ tokens.push(token);
5406
+ } else if (stream[this._current] === "[") {
5407
+ token = this._consumeLBracket(stream);
5408
+ tokens.push(token);
5409
+ } else if (stream[this._current] === '"') {
5410
+ start = this._current;
5411
+ identifier = this._consumeQuotedIdentifier(stream);
5412
+ tokens.push({
5413
+ type: TOK_QUOTEDIDENTIFIER,
5414
+ value: identifier,
5415
+ start
5416
+ });
5417
+ } else if (stream[this._current] === "'") {
5418
+ start = this._current;
5419
+ identifier = this._consumeRawStringLiteral(stream);
5420
+ tokens.push({
5421
+ type: TOK_LITERAL,
5422
+ value: identifier,
5423
+ start
5424
+ });
5425
+ } else if (stream[this._current] === "`") {
5426
+ start = this._current;
5427
+ var literal = this._consumeLiteral(stream);
5428
+ tokens.push({
5429
+ type: TOK_LITERAL,
5430
+ value: literal,
5431
+ start
5432
+ });
5433
+ } else if (operatorStartToken[stream[this._current]] !== undefined) {
5434
+ tokens.push(this._consumeOperator(stream));
5435
+ } else if (skipChars[stream[this._current]] !== undefined) {
5436
+ this._current++;
5437
+ } else if (stream[this._current] === "&") {
5438
+ start = this._current;
5439
+ this._current++;
5440
+ if (stream[this._current] === "&") {
5441
+ this._current++;
5442
+ tokens.push({ type: TOK_AND, value: "&&", start });
5443
+ } else {
5444
+ tokens.push({ type: TOK_EXPREF, value: "&", start });
5445
+ }
5446
+ } else if (stream[this._current] === "|") {
5447
+ start = this._current;
5448
+ this._current++;
5449
+ if (stream[this._current] === "|") {
5450
+ this._current++;
5451
+ tokens.push({ type: TOK_OR, value: "||", start });
5452
+ } else {
5453
+ tokens.push({ type: TOK_PIPE, value: "|", start });
5454
+ }
5455
+ } else {
5456
+ var error = new Error("Unknown character:" + stream[this._current]);
5457
+ error.name = "LexerError";
5458
+ throw error;
5459
+ }
5460
+ }
5461
+ return tokens;
6242
5462
  },
6243
- sort_by: {
6244
- _func: this.functionSortBy,
6245
- _signature: [{ types: [3] }, { types: [6] }]
5463
+ _consumeUnquotedIdentifier: function(stream) {
5464
+ var start = this._current;
5465
+ this._current++;
5466
+ while (this._current < stream.length && isAlphaNum(stream[this._current])) {
5467
+ this._current++;
5468
+ }
5469
+ return stream.slice(start, this._current);
6246
5470
  },
6247
- split: {
6248
- _func: this.functionSplit,
6249
- _signature: [
6250
- { types: [2] },
6251
- { types: [2] },
6252
- { types: [0], optional: true }
6253
- ]
5471
+ _consumeQuotedIdentifier: function(stream) {
5472
+ var start = this._current;
5473
+ this._current++;
5474
+ var maxLength = stream.length;
5475
+ while (stream[this._current] !== '"' && this._current < maxLength) {
5476
+ var current = this._current;
5477
+ if (stream[current] === "\\" && (stream[current + 1] === "\\" || stream[current + 1] === '"')) {
5478
+ current += 2;
5479
+ } else {
5480
+ current++;
5481
+ }
5482
+ this._current = current;
5483
+ }
5484
+ this._current++;
5485
+ return JSON.parse(stream.slice(start, this._current));
6254
5486
  },
6255
- starts_with: {
6256
- _func: this.functionStartsWith,
6257
- _signature: [{ types: [2] }, { types: [2] }]
5487
+ _consumeRawStringLiteral: function(stream) {
5488
+ var start = this._current;
5489
+ this._current++;
5490
+ var maxLength = stream.length;
5491
+ while (stream[this._current] !== "'" && this._current < maxLength) {
5492
+ var current = this._current;
5493
+ if (stream[current] === "\\" && (stream[current + 1] === "\\" || stream[current + 1] === "'")) {
5494
+ current += 2;
5495
+ } else {
5496
+ current++;
5497
+ }
5498
+ this._current = current;
5499
+ }
5500
+ this._current++;
5501
+ var literal = stream.slice(start + 1, this._current - 1);
5502
+ return literal.replace("\\'", "'");
6258
5503
  },
6259
- sum: { _func: this.functionSum, _signature: [{ types: [8] }] },
6260
- to_array: { _func: this.functionToArray, _signature: [{ types: [1] }] },
6261
- to_number: { _func: this.functionToNumber, _signature: [{ types: [1] }] },
6262
- to_string: { _func: this.functionToString, _signature: [{ types: [1] }] },
6263
- trim: {
6264
- _func: this.functionTrim,
6265
- _signature: [{ types: [2] }, { types: [2], optional: true }]
5504
+ _consumeNumber: function(stream) {
5505
+ var start = this._current;
5506
+ this._current++;
5507
+ var maxLength = stream.length;
5508
+ while (isNum(stream[this._current]) && this._current < maxLength) {
5509
+ this._current++;
5510
+ }
5511
+ var value = parseInt(stream.slice(start, this._current));
5512
+ return { type: TOK_NUMBER, value, start };
6266
5513
  },
6267
- trim_left: {
6268
- _func: this.functionTrimLeft,
6269
- _signature: [{ types: [2] }, { types: [2], optional: true }]
5514
+ _consumeLBracket: function(stream) {
5515
+ var start = this._current;
5516
+ this._current++;
5517
+ if (stream[this._current] === "?") {
5518
+ this._current++;
5519
+ return { type: TOK_FILTER, value: "[?", start };
5520
+ } else if (stream[this._current] === "]") {
5521
+ this._current++;
5522
+ return { type: TOK_FLATTEN, value: "[]", start };
5523
+ } else {
5524
+ return { type: TOK_LBRACKET, value: "[", start };
5525
+ }
6270
5526
  },
6271
- trim_right: {
6272
- _func: this.functionTrimRight,
6273
- _signature: [{ types: [2] }, { types: [2], optional: true }]
5527
+ _consumeOperator: function(stream) {
5528
+ var start = this._current;
5529
+ var startingChar = stream[start];
5530
+ this._current++;
5531
+ if (startingChar === "!") {
5532
+ if (stream[this._current] === "=") {
5533
+ this._current++;
5534
+ return { type: TOK_NE, value: "!=", start };
5535
+ } else {
5536
+ return { type: TOK_NOT, value: "!", start };
5537
+ }
5538
+ } else if (startingChar === "<") {
5539
+ if (stream[this._current] === "=") {
5540
+ this._current++;
5541
+ return { type: TOK_LTE, value: "<=", start };
5542
+ } else {
5543
+ return { type: TOK_LT, value: "<", start };
5544
+ }
5545
+ } else if (startingChar === ">") {
5546
+ if (stream[this._current] === "=") {
5547
+ this._current++;
5548
+ return { type: TOK_GTE, value: ">=", start };
5549
+ } else {
5550
+ return { type: TOK_GT, value: ">", start };
5551
+ }
5552
+ } else if (startingChar === "=") {
5553
+ if (stream[this._current] === "=") {
5554
+ this._current++;
5555
+ return { type: TOK_EQ, value: "==", start };
5556
+ }
5557
+ }
6274
5558
  },
6275
- type: { _func: this.functionType, _signature: [{ types: [1] }] },
6276
- zip: { _func: this.functionZip, _signature: [{ types: [3], variadic: true }] }
6277
- };
6278
- }
6279
- registerFunction(name, customFunction, signature, options) {
6280
- const result = this._registerInternal(name, customFunction, signature, options);
6281
- if (!result.success) {
6282
- throw new Error(result.message);
6283
- }
6284
- }
6285
- _registerInternal(name, customFunction, signature, options = {}) {
6286
- if (!name || typeof name !== "string" || name.trim() === "") {
6287
- return {
6288
- success: false,
6289
- reason: "invalid-name",
6290
- message: "Function name must be a non-empty string"
6291
- };
6292
- }
6293
- try {
6294
- this.validateInputSignatures(name, signature);
6295
- } catch (error) {
6296
- return {
6297
- success: false,
6298
- reason: "invalid-signature",
6299
- message: error instanceof Error ? error.message : "Invalid function signature"
6300
- };
6301
- }
6302
- const { override = false, warn = false } = options;
6303
- const exists = name in this._functionTable;
6304
- if (exists && !override) {
6305
- return {
6306
- success: false,
6307
- reason: "already-exists",
6308
- message: `Function already defined: ${name}(). Use { override: true } to replace it.`
6309
- };
6310
- }
6311
- if (exists && override && warn) {
6312
- console.warn(`Warning: Overriding existing function: ${name}()`);
6313
- }
6314
- this._functionTable[name] = {
6315
- _func: customFunction.bind(this),
6316
- _signature: signature
6317
- };
6318
- this._customFunctions.add(name);
6319
- const message = exists ? `Function ${name}() overridden successfully` : `Function ${name}() registered successfully`;
6320
- return { success: true, message };
6321
- }
6322
- register(name, customFunction, signature, options = {}) {
6323
- return this._registerInternal(name, customFunction, signature, options);
6324
- }
6325
- unregister(name) {
6326
- if (!this._customFunctions.has(name)) {
6327
- return false;
6328
- }
6329
- delete this._functionTable[name];
6330
- this._customFunctions.delete(name);
6331
- return true;
6332
- }
6333
- isRegistered(name) {
6334
- return name in this._functionTable;
6335
- }
6336
- getRegistered() {
6337
- return Object.keys(this._functionTable);
6338
- }
6339
- getCustomFunctions() {
6340
- return Array.from(this._customFunctions);
6341
- }
6342
- clearCustomFunctions() {
6343
- for (const name of this._customFunctions) {
6344
- delete this._functionTable[name];
6345
- }
6346
- this._customFunctions.clear();
6347
- }
6348
- callFunction(name, resolvedArgs) {
6349
- const functionEntry = this._functionTable[name];
6350
- if (functionEntry === undefined) {
6351
- throw new Error(`Unknown function: ${name}()`);
6352
- }
6353
- this.validateArgs(name, resolvedArgs, functionEntry._signature);
6354
- return functionEntry._func.call(this, resolvedArgs);
6355
- }
6356
- validateInputSignatures(name, signature) {
6357
- for (let i = 0;i < signature.length; i += 1) {
6358
- if ("variadic" in signature[i] && i !== signature.length - 1) {
6359
- throw new Error(`Invalid arity: ${name}() 'variadic' argument ${i + 1} must occur last`);
6360
- }
6361
- }
6362
- }
6363
- validateArgs(name, args, signature) {
6364
- this.validateInputSignatures(name, signature);
6365
- this.validateArity(name, args, signature);
6366
- this.validateTypes(name, args, signature);
6367
- }
6368
- validateArity(name, args, signature) {
6369
- const numberOfRequiredArgs = signature.filter((argSignature) => !(argSignature.optional ?? false)).length;
6370
- const lastArgIsVariadic = signature[signature.length - 1]?.variadic ?? false;
6371
- const tooFewArgs = args.length < numberOfRequiredArgs;
6372
- const tooManyArgs = args.length > signature.length;
6373
- if (lastArgIsVariadic && tooFewArgs || !lastArgIsVariadic && (tooFewArgs || tooManyArgs)) {
6374
- const tooFewModifier = tooFewArgs && (!lastArgIsVariadic && numberOfRequiredArgs > 1 || lastArgIsVariadic) ? "at least " : "";
6375
- const pluralized = signature.length > 1;
6376
- throw new Error(`Invalid arity: ${name}() takes ${tooFewModifier}${numberOfRequiredArgs} argument${pluralized && "s" || ""} but received ${args.length}`);
6377
- }
6378
- }
6379
- validateTypes(name, args, signature) {
6380
- for (let i = 0;i < signature.length; i += 1) {
6381
- const currentSpec = signature[i].types;
6382
- const actualType = this.getTypeName(args[i]);
6383
- if (actualType === undefined) {
6384
- continue;
6385
- }
6386
- const typeMatched = currentSpec.some((expectedType) => this.typeMatches(actualType, expectedType, args[i]));
6387
- if (!typeMatched) {
6388
- const expected = currentSpec.map((typeId) => this.TYPE_NAME_TABLE[typeId]).join(" | ");
6389
- throw new Error(`Invalid type: ${name}() expected argument ${i + 1} to be type (${expected}) but received type ${this.TYPE_NAME_TABLE[actualType]} instead.`);
6390
- }
6391
- }
6392
- }
6393
- typeMatches(actual, expected, argValue) {
6394
- if (expected === 1) {
6395
- return true;
6396
- }
6397
- if (expected === 9 || expected === 8 || expected === 10 || expected === 11 || expected === 3) {
6398
- if (expected === 3) {
6399
- return actual === 3;
6400
- }
6401
- if (actual === 3) {
6402
- let subtype;
6403
- if (expected === 8) {
6404
- subtype = 0;
6405
- } else if (expected === 10) {
6406
- subtype = 4;
6407
- } else if (expected === 9) {
6408
- subtype = 2;
6409
- } else if (expected === 11) {
6410
- subtype = 3;
6411
- }
6412
- const array = argValue;
6413
- for (let i = 0;i < array.length; i += 1) {
6414
- const typeName = this.getTypeName(array[i]);
6415
- if (typeName !== undefined && subtype !== undefined && !this.typeMatches(typeName, subtype, array[i])) {
6416
- return false;
5559
+ _consumeLiteral: function(stream) {
5560
+ this._current++;
5561
+ var start = this._current;
5562
+ var maxLength = stream.length;
5563
+ var literal;
5564
+ while (stream[this._current] !== "`" && this._current < maxLength) {
5565
+ var current = this._current;
5566
+ if (stream[current] === "\\" && (stream[current + 1] === "\\" || stream[current + 1] === "`")) {
5567
+ current += 2;
5568
+ } else {
5569
+ current++;
6417
5570
  }
5571
+ this._current = current;
5572
+ }
5573
+ var literalString = trimLeft(stream.slice(start, this._current));
5574
+ literalString = literalString.replace("\\`", "`");
5575
+ if (this._looksLikeJSON(literalString)) {
5576
+ literal = JSON.parse(literalString);
5577
+ } else {
5578
+ literal = JSON.parse('"' + literalString + '"');
5579
+ }
5580
+ this._current++;
5581
+ return literal;
5582
+ },
5583
+ _looksLikeJSON: function(literalString) {
5584
+ var startingChars = '[{"';
5585
+ var jsonLiterals = ["true", "false", "null"];
5586
+ var numberLooking = "-0123456789";
5587
+ if (literalString === "") {
5588
+ return false;
5589
+ } else if (startingChars.indexOf(literalString[0]) >= 0) {
5590
+ return true;
5591
+ } else if (jsonLiterals.indexOf(literalString) >= 0) {
5592
+ return true;
5593
+ } else if (numberLooking.indexOf(literalString[0]) >= 0) {
5594
+ try {
5595
+ JSON.parse(literalString);
5596
+ return true;
5597
+ } catch (ex) {
5598
+ return false;
5599
+ }
5600
+ } else {
5601
+ return false;
6418
5602
  }
6419
- return true;
6420
- }
6421
- } else {
6422
- return actual === expected;
6423
- }
6424
- return false;
6425
- }
6426
- getTypeName(obj) {
6427
- if (obj === null) {
6428
- return 7;
6429
- }
6430
- if (typeof obj === "string") {
6431
- return 2;
6432
- }
6433
- if (typeof obj === "number") {
6434
- return 0;
6435
- }
6436
- if (typeof obj === "boolean") {
6437
- return 5;
6438
- }
6439
- if (Array.isArray(obj)) {
6440
- return 3;
6441
- }
6442
- if (typeof obj === "object") {
6443
- if (obj.expref) {
6444
- return 6;
6445
- }
6446
- return 4;
6447
- }
6448
- return;
6449
- }
6450
- createKeyFunction(exprefNode, allowedTypes) {
6451
- const interpreter = this._interpreter;
6452
- const keyFunc = (x) => {
6453
- const current = interpreter.visit(exprefNode, x);
6454
- if (!allowedTypes.includes(this.getTypeName(current))) {
6455
- const msg = `Invalid type: expected one of (${allowedTypes.map((t) => this.TYPE_NAME_TABLE[t]).join(" | ")}), received ${this.TYPE_NAME_TABLE[this.getTypeName(current)]}`;
6456
- throw new Error(msg);
6457
- }
6458
- return current;
6459
- };
6460
- return keyFunc;
6461
- }
6462
- functionAvg = ([inputArray]) => {
6463
- if (!inputArray || inputArray.length == 0) {
6464
- return null;
6465
- }
6466
- let sum = 0;
6467
- for (let i = 0;i < inputArray.length; i += 1) {
6468
- sum += inputArray[i];
6469
- }
6470
- return sum / inputArray.length;
6471
- };
6472
- functionContains = ([
6473
- searchable,
6474
- searchValue
6475
- ]) => {
6476
- if (Array.isArray(searchable)) {
6477
- const array = searchable;
6478
- return array.includes(searchValue);
6479
- }
6480
- if (typeof searchable === "string") {
6481
- const text = searchable;
6482
- if (typeof searchValue === "string") {
6483
- return text.includes(searchValue);
6484
- }
6485
- }
6486
- return null;
6487
- };
6488
- functionEndsWith = (resolvedArgs) => {
6489
- const [searchStr, suffix] = resolvedArgs;
6490
- return searchStr.includes(suffix, searchStr.length - suffix.length);
6491
- };
6492
- functionFindFirst = this.createFindFunction(findFirst);
6493
- functionFindLast = this.createFindFunction(findLast);
6494
- createFindFunction(findFn) {
6495
- return (resolvedArgs) => {
6496
- const subject = resolvedArgs[0];
6497
- const search2 = resolvedArgs[1];
6498
- const start = resolvedArgs.length > 2 ? resolvedArgs[2] : undefined;
6499
- const end = resolvedArgs.length > 3 ? resolvedArgs[3] : undefined;
6500
- return findFn(subject, search2, start, end);
6501
- };
6502
- }
6503
- functionFromItems = ([array]) => {
6504
- array.map((pair) => {
6505
- if (pair.length != 2 || typeof pair[0] !== "string") {
6506
- throw new Error("invalid value, each array must contain two elements, a pair of string and value");
6507
- }
6508
- });
6509
- return Object.fromEntries(array);
6510
- };
6511
- functionGroupBy = ([array, exprefNode]) => {
6512
- const keyFunction = this.createKeyFunction(exprefNode, [2]);
6513
- return array.reduce((acc, cur) => {
6514
- const k = keyFunction(cur ?? {});
6515
- const target = acc[k] = acc[k] || [];
6516
- target.push(cur);
6517
- return acc;
6518
- }, {});
6519
- };
6520
- functionItems = ([inputValue]) => {
6521
- return Object.entries(inputValue);
6522
- };
6523
- functionJoin = (resolvedArgs) => {
6524
- const [joinChar, listJoin] = resolvedArgs;
6525
- return listJoin.join(joinChar);
6526
- };
6527
- functionLength = ([inputValue]) => {
6528
- if (typeof inputValue === "string") {
6529
- return new Text(inputValue).length;
6530
- }
6531
- if (Array.isArray(inputValue)) {
6532
- return inputValue.length;
6533
- }
6534
- return Object.keys(inputValue).length;
6535
- };
6536
- functionMap = ([exprefNode, elements]) => {
6537
- if (!this._interpreter) {
6538
- return [];
6539
- }
6540
- const mapped = [];
6541
- const interpreter = this._interpreter;
6542
- for (let i = 0;i < elements.length; i += 1) {
6543
- mapped.push(interpreter.visit(exprefNode, elements[i]));
6544
- }
6545
- return mapped;
6546
- };
6547
- functionMax = ([inputValue]) => {
6548
- if (!inputValue.length) {
6549
- return null;
6550
- }
6551
- const typeName = this.getTypeName(inputValue[0]);
6552
- if (typeName === 0) {
6553
- return Math.max(...inputValue);
6554
- }
6555
- const elements = inputValue;
6556
- let maxElement = elements[0];
6557
- for (let i = 1;i < elements.length; i += 1) {
6558
- if (maxElement.localeCompare(elements[i]) < 0) {
6559
- maxElement = elements[i];
6560
- }
6561
- }
6562
- return maxElement;
6563
- };
6564
- functionMaxBy = (resolvedArgs) => {
6565
- const exprefNode = resolvedArgs[1];
6566
- const resolvedArray = resolvedArgs[0];
6567
- const keyFunction = this.createKeyFunction(exprefNode, [0, 2]);
6568
- let maxNumber = -Infinity;
6569
- let maxRecord;
6570
- let current;
6571
- for (let i = 0;i < resolvedArray.length; i += 1) {
6572
- current = keyFunction && keyFunction(resolvedArray[i]);
6573
- if (current !== undefined && current > maxNumber) {
6574
- maxNumber = current;
6575
- maxRecord = resolvedArray[i];
6576
- }
6577
- }
6578
- return maxRecord || null;
6579
- };
6580
- functionMerge = (resolvedArgs) => {
6581
- let merged = {};
6582
- for (let i = 0;i < resolvedArgs.length; i += 1) {
6583
- const current = resolvedArgs[i];
6584
- merged = Object.assign(merged, current);
6585
- }
6586
- return merged;
6587
- };
6588
- functionMin = ([inputValue]) => {
6589
- if (!inputValue.length) {
6590
- return null;
6591
- }
6592
- const typeName = this.getTypeName(inputValue[0]);
6593
- if (typeName === 0) {
6594
- return Math.min(...inputValue);
6595
- }
6596
- const elements = inputValue;
6597
- let minElement = elements[0];
6598
- for (let i = 1;i < elements.length; i += 1) {
6599
- if (elements[i].localeCompare(minElement) < 0) {
6600
- minElement = elements[i];
6601
- }
6602
- }
6603
- return minElement;
6604
- };
6605
- functionMinBy = (resolvedArgs) => {
6606
- const exprefNode = resolvedArgs[1];
6607
- const resolvedArray = resolvedArgs[0];
6608
- const keyFunction = this.createKeyFunction(exprefNode, [0, 2]);
6609
- let minNumber = Infinity;
6610
- let minRecord;
6611
- let current;
6612
- for (let i = 0;i < resolvedArray.length; i += 1) {
6613
- current = keyFunction && keyFunction(resolvedArray[i]);
6614
- if (current !== undefined && current < minNumber) {
6615
- minNumber = current;
6616
- minRecord = resolvedArray[i];
6617
- }
6618
- }
6619
- return minRecord || null;
6620
- };
6621
- functionNotNull = (resolvedArgs) => {
6622
- for (let i = 0;i < resolvedArgs.length; i += 1) {
6623
- if (this.getTypeName(resolvedArgs[i]) !== 7) {
6624
- return resolvedArgs[i];
6625
- }
6626
- }
6627
- return null;
6628
- };
6629
- functionPadLeft = this.createPadFunction(padLeft);
6630
- functionPadRight = this.createPadFunction(padRight);
6631
- createPadFunction(padFn) {
6632
- return (resolvedArgs) => {
6633
- const subject = resolvedArgs[0];
6634
- const width = resolvedArgs[1];
6635
- const padding = resolvedArgs.length > 2 ? resolvedArgs[2] : undefined;
6636
- return padFn(subject, width, padding);
6637
- };
6638
- }
6639
- functionReplace = (resolvedArgs) => {
6640
- const subject = resolvedArgs[0];
6641
- const string = resolvedArgs[1];
6642
- const by = resolvedArgs[2];
6643
- return replace(subject, string, by, resolvedArgs.length > 3 ? resolvedArgs[3] : undefined);
6644
- };
6645
- functionSplit = (resolvedArgs) => {
6646
- const subject = resolvedArgs[0];
6647
- const search2 = resolvedArgs[1];
6648
- return split(subject, search2, resolvedArgs.length > 2 ? resolvedArgs[2] : undefined);
6649
- };
6650
- functionReverse = ([inputValue]) => {
6651
- const typeName = this.getTypeName(inputValue);
6652
- if (typeName === 2) {
6653
- return new Text(inputValue).reverse();
6654
- }
6655
- const reversedArray = inputValue.slice(0);
6656
- reversedArray.reverse();
6657
- return reversedArray;
6658
- };
6659
- functionSort = ([inputValue]) => {
6660
- if (inputValue.length == 0) {
6661
- return inputValue;
6662
- }
6663
- if (typeof inputValue[0] === "string") {
6664
- return [...inputValue].sort(Text.comparer);
6665
- }
6666
- return [...inputValue].sort();
6667
- };
6668
- functionSortBy = (resolvedArgs) => {
6669
- const sortedArray = resolvedArgs[0].slice(0);
6670
- if (sortedArray.length === 0) {
6671
- return sortedArray;
6672
- }
6673
- const interpreter = this._interpreter;
6674
- const exprefNode = resolvedArgs[1];
6675
- const requiredType = this.getTypeName(interpreter.visit(exprefNode, sortedArray[0]));
6676
- if (requiredType !== undefined && ![0, 2].includes(requiredType)) {
6677
- throw new Error(`Invalid type: unexpected type (${this.TYPE_NAME_TABLE[requiredType]})`);
6678
- }
6679
- function throwInvalidTypeError(rt, item) {
6680
- throw new Error(`Invalid type: expected (${rt.TYPE_NAME_TABLE[requiredType]}), received ${rt.TYPE_NAME_TABLE[rt.getTypeName(item)]}`);
6681
- }
6682
- return sortedArray.sort((a, b) => {
6683
- const exprA = interpreter.visit(exprefNode, a);
6684
- const exprB = interpreter.visit(exprefNode, b);
6685
- if (this.getTypeName(exprA) !== requiredType) {
6686
- throwInvalidTypeError(this, exprA);
6687
- } else if (this.getTypeName(exprB) !== requiredType) {
6688
- throwInvalidTypeError(this, exprB);
6689
- }
6690
- if (requiredType === 2) {
6691
- return Text.comparer(exprA, exprB);
6692
- }
6693
- return exprA - exprB;
6694
- });
6695
- };
6696
- functionStartsWith = ([searchable, searchStr]) => {
6697
- return searchable.startsWith(searchStr);
6698
- };
6699
- functionSum = ([inputValue]) => {
6700
- return inputValue.reduce((x, y) => x + y, 0);
6701
- };
6702
- functionToArray = ([inputValue]) => {
6703
- if (this.getTypeName(inputValue) === 3) {
6704
- return inputValue;
6705
- }
6706
- return [inputValue];
6707
- };
6708
- functionToNumber = ([inputValue]) => {
6709
- const typeName = this.getTypeName(inputValue);
6710
- let convertedValue;
6711
- if (typeName === 0) {
6712
- return inputValue;
6713
- }
6714
- if (typeName === 2) {
6715
- convertedValue = +inputValue;
6716
- if (!isNaN(convertedValue)) {
6717
- return convertedValue;
6718
5603
  }
6719
- }
6720
- return null;
6721
- };
6722
- functionToString = ([inputValue]) => {
6723
- if (this.getTypeName(inputValue) === 2) {
6724
- return inputValue;
6725
- }
6726
- return JSON.stringify(inputValue);
6727
- };
6728
- functionTrim = this.createTrimFunction(trim);
6729
- functionTrimLeft = this.createTrimFunction(trimLeft);
6730
- functionTrimRight = this.createTrimFunction(trimRight);
6731
- createTrimFunction(trimFn) {
6732
- return (resolvedArgs) => {
6733
- const subject = resolvedArgs[0];
6734
- const chars = resolvedArgs.length > 1 ? resolvedArgs[1] : undefined;
6735
- return trimFn(subject, chars);
6736
5604
  };
6737
- }
6738
- functionType = ([inputValue]) => {
6739
- switch (this.getTypeName(inputValue)) {
6740
- case 0:
6741
- return "number";
6742
- case 2:
6743
- return "string";
6744
- case 3:
6745
- return "array";
6746
- case 4:
6747
- return "object";
6748
- case 5:
6749
- return "boolean";
6750
- case 7:
6751
- return "null";
6752
- default:
6753
- throw new Error("invalid-type");
6754
- }
6755
- };
6756
- functionZip = (array) => {
6757
- const length = Math.min(...array.map((arr) => arr.length));
6758
- const result = Array(length).fill(null).map((_, index) => array.map((arr) => arr[index]));
6759
- return result;
6760
- };
6761
- }, ScopeChain = class _ScopeChain {
6762
- inner = undefined;
6763
- data = {};
6764
- get currentScopeData() {
6765
- return this.data;
6766
- }
6767
- withScope(data) {
6768
- const outer = new _ScopeChain;
6769
- outer.inner = this;
6770
- outer.data = data;
6771
- return outer;
6772
- }
6773
- getValue(identifier) {
6774
- if (Object.prototype.hasOwnProperty.call(this.data, identifier)) {
6775
- return this.data[identifier];
6776
- }
6777
- if (this.inner) {
6778
- return this.inner.getValue(identifier);
6779
- }
6780
- return null;
6781
- }
6782
- }, emptyScopeChain, TreeInterpreter = class _TreeInterpreter {
6783
- runtime;
6784
- _rootValue = null;
6785
- _scope;
6786
- constructor() {
6787
- this.runtime = new Runtime(this);
6788
- this._scope = new ScopeChain;
6789
- }
6790
- withScope(scope) {
6791
- const interpreter = new _TreeInterpreter;
6792
- interpreter.runtime._functionTable = this.runtime._functionTable;
6793
- interpreter._rootValue = this._rootValue;
6794
- interpreter._scope = this._scope.withScope(scope);
6795
- return interpreter;
6796
- }
6797
- search(node, value) {
6798
- this._rootValue = value;
6799
- this._scope = emptyScopeChain;
6800
- return this.visit(node, value);
6801
- }
6802
- visit(node, value) {
6803
- switch (node.type) {
6804
- case "Ternary": {
6805
- const condition = this.visit(node.condition, value);
6806
- if (!isFalse(condition)) {
6807
- return this.visit(node.trueExpr, value);
6808
- }
6809
- return this.visit(node.falseExpr, value);
6810
- }
6811
- case "Field":
6812
- const identifier = node.name;
6813
- if (value === null || typeof value !== "object" || Array.isArray(value)) {
6814
- return null;
5605
+ var bindingPower = {};
5606
+ bindingPower[TOK_EOF] = 0;
5607
+ bindingPower[TOK_UNQUOTEDIDENTIFIER] = 0;
5608
+ bindingPower[TOK_QUOTEDIDENTIFIER] = 0;
5609
+ bindingPower[TOK_RBRACKET] = 0;
5610
+ bindingPower[TOK_RPAREN] = 0;
5611
+ bindingPower[TOK_COMMA] = 0;
5612
+ bindingPower[TOK_RBRACE] = 0;
5613
+ bindingPower[TOK_NUMBER] = 0;
5614
+ bindingPower[TOK_CURRENT] = 0;
5615
+ bindingPower[TOK_EXPREF] = 0;
5616
+ bindingPower[TOK_PIPE] = 1;
5617
+ bindingPower[TOK_OR] = 2;
5618
+ bindingPower[TOK_AND] = 3;
5619
+ bindingPower[TOK_EQ] = 5;
5620
+ bindingPower[TOK_GT] = 5;
5621
+ bindingPower[TOK_LT] = 5;
5622
+ bindingPower[TOK_GTE] = 5;
5623
+ bindingPower[TOK_LTE] = 5;
5624
+ bindingPower[TOK_NE] = 5;
5625
+ bindingPower[TOK_FLATTEN] = 9;
5626
+ bindingPower[TOK_STAR] = 20;
5627
+ bindingPower[TOK_FILTER] = 21;
5628
+ bindingPower[TOK_DOT] = 40;
5629
+ bindingPower[TOK_NOT] = 45;
5630
+ bindingPower[TOK_LBRACE] = 50;
5631
+ bindingPower[TOK_LBRACKET] = 55;
5632
+ bindingPower[TOK_LPAREN] = 60;
5633
+ function Parser() {}
5634
+ Parser.prototype = {
5635
+ parse: function(expression) {
5636
+ this._loadTokens(expression);
5637
+ this.index = 0;
5638
+ var ast = this.expression(0);
5639
+ if (this._lookahead(0) !== TOK_EOF) {
5640
+ var t = this._lookaheadToken(0);
5641
+ var error = new Error("Unexpected token type: " + t.type + ", value: " + t.value);
5642
+ error.name = "ParserError";
5643
+ throw error;
5644
+ }
5645
+ return ast;
5646
+ },
5647
+ _loadTokens: function(expression) {
5648
+ var lexer = new Lexer;
5649
+ var tokens = lexer.tokenize(expression);
5650
+ tokens.push({ type: TOK_EOF, value: "", start: expression.length });
5651
+ this.tokens = tokens;
5652
+ },
5653
+ expression: function(rbp) {
5654
+ var leftToken = this._lookaheadToken(0);
5655
+ this._advance();
5656
+ var left = this.nud(leftToken);
5657
+ var currentToken = this._lookahead(0);
5658
+ while (rbp < bindingPower[currentToken]) {
5659
+ this._advance();
5660
+ left = this.led(currentToken, left);
5661
+ currentToken = this._lookahead(0);
5662
+ }
5663
+ return left;
5664
+ },
5665
+ _lookahead: function(number) {
5666
+ return this.tokens[this.index + number].type;
5667
+ },
5668
+ _lookaheadToken: function(number) {
5669
+ return this.tokens[this.index + number];
5670
+ },
5671
+ _advance: function() {
5672
+ this.index++;
5673
+ },
5674
+ nud: function(token) {
5675
+ var left;
5676
+ var right;
5677
+ var expression;
5678
+ switch (token.type) {
5679
+ case TOK_LITERAL:
5680
+ return { type: "Literal", value: token.value };
5681
+ case TOK_UNQUOTEDIDENTIFIER:
5682
+ return { type: "Field", name: token.value };
5683
+ case TOK_QUOTEDIDENTIFIER:
5684
+ var node = { type: "Field", name: token.value };
5685
+ if (this._lookahead(0) === TOK_LPAREN) {
5686
+ throw new Error("Quoted identifier not allowed for function names.");
5687
+ }
5688
+ return node;
5689
+ case TOK_NOT:
5690
+ right = this.expression(bindingPower.Not);
5691
+ return { type: "NotExpression", children: [right] };
5692
+ case TOK_STAR:
5693
+ left = { type: "Identity" };
5694
+ right = null;
5695
+ if (this._lookahead(0) === TOK_RBRACKET) {
5696
+ right = { type: "Identity" };
5697
+ } else {
5698
+ right = this._parseProjectionRHS(bindingPower.Star);
5699
+ }
5700
+ return { type: "ValueProjection", children: [left, right] };
5701
+ case TOK_FILTER:
5702
+ return this.led(token.type, { type: "Identity" });
5703
+ case TOK_LBRACE:
5704
+ return this._parseMultiselectHash();
5705
+ case TOK_FLATTEN:
5706
+ left = { type: TOK_FLATTEN, children: [{ type: "Identity" }] };
5707
+ right = this._parseProjectionRHS(bindingPower.Flatten);
5708
+ return { type: "Projection", children: [left, right] };
5709
+ case TOK_LBRACKET:
5710
+ if (this._lookahead(0) === TOK_NUMBER || this._lookahead(0) === TOK_COLON) {
5711
+ right = this._parseIndexExpression();
5712
+ return this._projectIfSlice({ type: "Identity" }, right);
5713
+ } else if (this._lookahead(0) === TOK_STAR && this._lookahead(1) === TOK_RBRACKET) {
5714
+ this._advance();
5715
+ this._advance();
5716
+ right = this._parseProjectionRHS(bindingPower.Star);
5717
+ return {
5718
+ type: "Projection",
5719
+ children: [{ type: "Identity" }, right]
5720
+ };
5721
+ }
5722
+ return this._parseMultiselectList();
5723
+ case TOK_CURRENT:
5724
+ return { type: TOK_CURRENT };
5725
+ case TOK_EXPREF:
5726
+ expression = this.expression(bindingPower.Expref);
5727
+ return { type: "ExpressionReference", children: [expression] };
5728
+ case TOK_LPAREN:
5729
+ var args = [];
5730
+ while (this._lookahead(0) !== TOK_RPAREN) {
5731
+ if (this._lookahead(0) === TOK_CURRENT) {
5732
+ expression = { type: TOK_CURRENT };
5733
+ this._advance();
5734
+ } else {
5735
+ expression = this.expression(0);
5736
+ }
5737
+ args.push(expression);
5738
+ }
5739
+ this._match(TOK_RPAREN);
5740
+ return args[0];
5741
+ default:
5742
+ this._errorToken(token);
5743
+ }
5744
+ },
5745
+ led: function(tokenName, left) {
5746
+ var right;
5747
+ switch (tokenName) {
5748
+ case TOK_DOT:
5749
+ var rbp = bindingPower.Dot;
5750
+ if (this._lookahead(0) !== TOK_STAR) {
5751
+ right = this._parseDotRHS(rbp);
5752
+ return { type: "Subexpression", children: [left, right] };
5753
+ }
5754
+ this._advance();
5755
+ right = this._parseProjectionRHS(rbp);
5756
+ return { type: "ValueProjection", children: [left, right] };
5757
+ case TOK_PIPE:
5758
+ right = this.expression(bindingPower.Pipe);
5759
+ return { type: TOK_PIPE, children: [left, right] };
5760
+ case TOK_OR:
5761
+ right = this.expression(bindingPower.Or);
5762
+ return { type: "OrExpression", children: [left, right] };
5763
+ case TOK_AND:
5764
+ right = this.expression(bindingPower.And);
5765
+ return { type: "AndExpression", children: [left, right] };
5766
+ case TOK_LPAREN:
5767
+ var name = left.name;
5768
+ var args = [];
5769
+ var expression, node;
5770
+ while (this._lookahead(0) !== TOK_RPAREN) {
5771
+ if (this._lookahead(0) === TOK_CURRENT) {
5772
+ expression = { type: TOK_CURRENT };
5773
+ this._advance();
5774
+ } else {
5775
+ expression = this.expression(0);
5776
+ }
5777
+ if (this._lookahead(0) === TOK_COMMA) {
5778
+ this._match(TOK_COMMA);
5779
+ }
5780
+ args.push(expression);
5781
+ }
5782
+ this._match(TOK_RPAREN);
5783
+ node = { type: "Function", name, children: args };
5784
+ return node;
5785
+ case TOK_FILTER:
5786
+ var condition = this.expression(0);
5787
+ this._match(TOK_RBRACKET);
5788
+ if (this._lookahead(0) === TOK_FLATTEN) {
5789
+ right = { type: "Identity" };
5790
+ } else {
5791
+ right = this._parseProjectionRHS(bindingPower.Filter);
5792
+ }
5793
+ return { type: "FilterProjection", children: [left, right, condition] };
5794
+ case TOK_FLATTEN:
5795
+ var leftNode = { type: TOK_FLATTEN, children: [left] };
5796
+ var rightNode = this._parseProjectionRHS(bindingPower.Flatten);
5797
+ return { type: "Projection", children: [leftNode, rightNode] };
5798
+ case TOK_EQ:
5799
+ case TOK_NE:
5800
+ case TOK_GT:
5801
+ case TOK_GTE:
5802
+ case TOK_LT:
5803
+ case TOK_LTE:
5804
+ return this._parseComparator(left, tokenName);
5805
+ case TOK_LBRACKET:
5806
+ var token = this._lookaheadToken(0);
5807
+ if (token.type === TOK_NUMBER || token.type === TOK_COLON) {
5808
+ right = this._parseIndexExpression();
5809
+ return this._projectIfSlice(left, right);
5810
+ }
5811
+ this._match(TOK_STAR);
5812
+ this._match(TOK_RBRACKET);
5813
+ right = this._parseProjectionRHS(bindingPower.Star);
5814
+ return { type: "Projection", children: [left, right] };
5815
+ default:
5816
+ this._errorToken(this._lookaheadToken(0));
5817
+ }
5818
+ },
5819
+ _match: function(tokenType) {
5820
+ if (this._lookahead(0) === tokenType) {
5821
+ this._advance();
5822
+ } else {
5823
+ var t = this._lookaheadToken(0);
5824
+ var error = new Error("Expected " + tokenType + ", got: " + t.type);
5825
+ error.name = "ParserError";
5826
+ throw error;
6815
5827
  }
6816
- return value[identifier] ?? null;
6817
- case "LetExpression": {
6818
- const { bindings, expression } = node;
6819
- let scope = {};
6820
- bindings.forEach((binding) => {
6821
- const reference = this.visit(binding, value);
6822
- scope = {
6823
- ...scope,
6824
- ...reference
5828
+ },
5829
+ _errorToken: function(token) {
5830
+ var error = new Error("Invalid token (" + token.type + '): "' + token.value + '"');
5831
+ error.name = "ParserError";
5832
+ throw error;
5833
+ },
5834
+ _parseIndexExpression: function() {
5835
+ if (this._lookahead(0) === TOK_COLON || this._lookahead(1) === TOK_COLON) {
5836
+ return this._parseSliceExpression();
5837
+ } else {
5838
+ var node = {
5839
+ type: "Index",
5840
+ value: this._lookaheadToken(0).value
6825
5841
  };
6826
- });
6827
- return this.withScope(scope).visit(expression, value);
6828
- }
6829
- case "Binding": {
6830
- const { variable, reference } = node;
6831
- const result = this.visit(reference, value);
6832
- return { [variable]: result };
6833
- }
6834
- case "Variable": {
6835
- const variable = node.name;
6836
- if (!this._scope.getValue(variable) && !Object.prototype.hasOwnProperty.call(this._scope.currentScopeData, variable)) {
6837
- throw new Error(`Error referencing undefined variable ${variable}`);
5842
+ this._advance();
5843
+ this._match(TOK_RBRACKET);
5844
+ return node;
6838
5845
  }
6839
- return this._scope.getValue(variable);
6840
- }
6841
- case "IndexExpression":
6842
- return this.visit(node.right, this.visit(node.left, value));
6843
- case "Subexpression": {
6844
- const result = this.visit(node.left, value);
6845
- return result != null ? this.visit(node.right, result) ?? null : null;
6846
- }
6847
- case "Index": {
6848
- if (!Array.isArray(value)) {
6849
- return null;
5846
+ },
5847
+ _projectIfSlice: function(left, right) {
5848
+ var indexExpr = { type: "IndexExpression", children: [left, right] };
5849
+ if (right.type === "Slice") {
5850
+ return {
5851
+ type: "Projection",
5852
+ children: [indexExpr, this._parseProjectionRHS(bindingPower.Star)]
5853
+ };
5854
+ } else {
5855
+ return indexExpr;
6850
5856
  }
6851
- const index = node.value < 0 ? value.length + node.value : node.value;
6852
- return value[index] ?? null;
6853
- }
6854
- case "Slice": {
6855
- if (!Array.isArray(value) && typeof value !== "string") {
6856
- return null;
5857
+ },
5858
+ _parseSliceExpression: function() {
5859
+ var parts = [null, null, null];
5860
+ var index = 0;
5861
+ var currentToken = this._lookahead(0);
5862
+ while (currentToken !== TOK_RBRACKET && index < 3) {
5863
+ if (currentToken === TOK_COLON) {
5864
+ index++;
5865
+ this._advance();
5866
+ } else if (currentToken === TOK_NUMBER) {
5867
+ parts[index] = this._lookaheadToken(0).value;
5868
+ this._advance();
5869
+ } else {
5870
+ var t = this._lookahead(0);
5871
+ var error = new Error("Syntax error, unexpected token: " + t.value + "(" + t.type + ")");
5872
+ error.name = "Parsererror";
5873
+ throw error;
5874
+ }
5875
+ currentToken = this._lookahead(0);
5876
+ }
5877
+ this._match(TOK_RBRACKET);
5878
+ return {
5879
+ type: "Slice",
5880
+ children: parts
5881
+ };
5882
+ },
5883
+ _parseComparator: function(left, comparator) {
5884
+ var right = this.expression(bindingPower[comparator]);
5885
+ return { type: "Comparator", name: comparator, children: [left, right] };
5886
+ },
5887
+ _parseDotRHS: function(rbp) {
5888
+ var lookahead = this._lookahead(0);
5889
+ var exprTokens = [TOK_UNQUOTEDIDENTIFIER, TOK_QUOTEDIDENTIFIER, TOK_STAR];
5890
+ if (exprTokens.indexOf(lookahead) >= 0) {
5891
+ return this.expression(rbp);
5892
+ } else if (lookahead === TOK_LBRACKET) {
5893
+ this._match(TOK_LBRACKET);
5894
+ return this._parseMultiselectList();
5895
+ } else if (lookahead === TOK_LBRACE) {
5896
+ this._match(TOK_LBRACE);
5897
+ return this._parseMultiselectHash();
6857
5898
  }
6858
- const { start, stop, step } = this.computeSliceParams(value.length, node);
6859
- if (typeof value === "string") {
6860
- const chars = [...value];
6861
- const sliced = this.slice(chars, start, stop, step);
6862
- return sliced.join("");
5899
+ },
5900
+ _parseProjectionRHS: function(rbp) {
5901
+ var right;
5902
+ if (bindingPower[this._lookahead(0)] < 10) {
5903
+ right = { type: "Identity" };
5904
+ } else if (this._lookahead(0) === TOK_LBRACKET) {
5905
+ right = this.expression(rbp);
5906
+ } else if (this._lookahead(0) === TOK_FILTER) {
5907
+ right = this.expression(rbp);
5908
+ } else if (this._lookahead(0) === TOK_DOT) {
5909
+ this._match(TOK_DOT);
5910
+ right = this._parseDotRHS(rbp);
6863
5911
  } else {
6864
- return this.slice(value, start, stop, step);
5912
+ var t = this._lookaheadToken(0);
5913
+ var error = new Error("Sytanx error, unexpected token: " + t.value + "(" + t.type + ")");
5914
+ error.name = "ParserError";
5915
+ throw error;
6865
5916
  }
5917
+ return right;
5918
+ },
5919
+ _parseMultiselectList: function() {
5920
+ var expressions = [];
5921
+ while (this._lookahead(0) !== TOK_RBRACKET) {
5922
+ var expression = this.expression(0);
5923
+ expressions.push(expression);
5924
+ if (this._lookahead(0) === TOK_COMMA) {
5925
+ this._match(TOK_COMMA);
5926
+ if (this._lookahead(0) === TOK_RBRACKET) {
5927
+ throw new Error("Unexpected token Rbracket");
5928
+ }
5929
+ }
5930
+ }
5931
+ this._match(TOK_RBRACKET);
5932
+ return { type: "MultiSelectList", children: expressions };
5933
+ },
5934
+ _parseMultiselectHash: function() {
5935
+ var pairs2 = [];
5936
+ var identifierTypes = [TOK_UNQUOTEDIDENTIFIER, TOK_QUOTEDIDENTIFIER];
5937
+ var keyToken, keyName, value, node;
5938
+ for (;; ) {
5939
+ keyToken = this._lookaheadToken(0);
5940
+ if (identifierTypes.indexOf(keyToken.type) < 0) {
5941
+ throw new Error("Expecting an identifier token, got: " + keyToken.type);
5942
+ }
5943
+ keyName = keyToken.value;
5944
+ this._advance();
5945
+ this._match(TOK_COLON);
5946
+ value = this.expression(0);
5947
+ node = { type: "KeyValuePair", name: keyName, value };
5948
+ pairs2.push(node);
5949
+ if (this._lookahead(0) === TOK_COMMA) {
5950
+ this._match(TOK_COMMA);
5951
+ } else if (this._lookahead(0) === TOK_RBRACE) {
5952
+ this._match(TOK_RBRACE);
5953
+ break;
5954
+ }
5955
+ }
5956
+ return { type: "MultiSelectHash", children: pairs2 };
6866
5957
  }
6867
- case "Projection": {
6868
- const { left, right } = node;
6869
- let allowString = false;
6870
- if (left.type === "IndexExpression" && left.right.type === "Slice") {
6871
- allowString = true;
5958
+ };
5959
+ function TreeInterpreter(runtime) {
5960
+ this.runtime = runtime;
5961
+ }
5962
+ TreeInterpreter.prototype = {
5963
+ search: function(node, value) {
5964
+ return this.visit(node, value);
5965
+ },
5966
+ visit: function(node, value) {
5967
+ var matched, current, result, first, second, field, left, right, collected, i;
5968
+ switch (node.type) {
5969
+ case "Field":
5970
+ if (value !== null && isObject(value)) {
5971
+ field = value[node.name];
5972
+ if (field === undefined) {
5973
+ return null;
5974
+ } else {
5975
+ return field;
5976
+ }
5977
+ }
5978
+ return null;
5979
+ case "Subexpression":
5980
+ result = this.visit(node.children[0], value);
5981
+ for (i = 1;i < node.children.length; i++) {
5982
+ result = this.visit(node.children[1], result);
5983
+ if (result === null) {
5984
+ return null;
5985
+ }
5986
+ }
5987
+ return result;
5988
+ case "IndexExpression":
5989
+ left = this.visit(node.children[0], value);
5990
+ right = this.visit(node.children[1], left);
5991
+ return right;
5992
+ case "Index":
5993
+ if (!isArray(value)) {
5994
+ return null;
5995
+ }
5996
+ var index = node.value;
5997
+ if (index < 0) {
5998
+ index = value.length + index;
5999
+ }
6000
+ result = value[index];
6001
+ if (result === undefined) {
6002
+ result = null;
6003
+ }
6004
+ return result;
6005
+ case "Slice":
6006
+ if (!isArray(value)) {
6007
+ return null;
6008
+ }
6009
+ var sliceParams = node.children.slice(0);
6010
+ var computed = this.computeSliceParams(value.length, sliceParams);
6011
+ var start = computed[0];
6012
+ var stop = computed[1];
6013
+ var step = computed[2];
6014
+ result = [];
6015
+ if (step > 0) {
6016
+ for (i = start;i < stop; i += step) {
6017
+ result.push(value[i]);
6018
+ }
6019
+ } else {
6020
+ for (i = start;i > stop; i += step) {
6021
+ result.push(value[i]);
6022
+ }
6023
+ }
6024
+ return result;
6025
+ case "Projection":
6026
+ var base = this.visit(node.children[0], value);
6027
+ if (!isArray(base)) {
6028
+ return null;
6029
+ }
6030
+ collected = [];
6031
+ for (i = 0;i < base.length; i++) {
6032
+ current = this.visit(node.children[1], base[i]);
6033
+ if (current !== null) {
6034
+ collected.push(current);
6035
+ }
6036
+ }
6037
+ return collected;
6038
+ case "ValueProjection":
6039
+ base = this.visit(node.children[0], value);
6040
+ if (!isObject(base)) {
6041
+ return null;
6042
+ }
6043
+ collected = [];
6044
+ var values = objValues(base);
6045
+ for (i = 0;i < values.length; i++) {
6046
+ current = this.visit(node.children[1], values[i]);
6047
+ if (current !== null) {
6048
+ collected.push(current);
6049
+ }
6050
+ }
6051
+ return collected;
6052
+ case "FilterProjection":
6053
+ base = this.visit(node.children[0], value);
6054
+ if (!isArray(base)) {
6055
+ return null;
6056
+ }
6057
+ var filtered = [];
6058
+ var finalResults = [];
6059
+ for (i = 0;i < base.length; i++) {
6060
+ matched = this.visit(node.children[2], base[i]);
6061
+ if (!isFalse(matched)) {
6062
+ filtered.push(base[i]);
6063
+ }
6064
+ }
6065
+ for (var j = 0;j < filtered.length; j++) {
6066
+ current = this.visit(node.children[1], filtered[j]);
6067
+ if (current !== null) {
6068
+ finalResults.push(current);
6069
+ }
6070
+ }
6071
+ return finalResults;
6072
+ case "Comparator":
6073
+ first = this.visit(node.children[0], value);
6074
+ second = this.visit(node.children[1], value);
6075
+ switch (node.name) {
6076
+ case TOK_EQ:
6077
+ result = strictDeepEqual(first, second);
6078
+ break;
6079
+ case TOK_NE:
6080
+ result = !strictDeepEqual(first, second);
6081
+ break;
6082
+ case TOK_GT:
6083
+ result = first > second;
6084
+ break;
6085
+ case TOK_GTE:
6086
+ result = first >= second;
6087
+ break;
6088
+ case TOK_LT:
6089
+ result = first < second;
6090
+ break;
6091
+ case TOK_LTE:
6092
+ result = first <= second;
6093
+ break;
6094
+ default:
6095
+ throw new Error("Unknown comparator: " + node.name);
6096
+ }
6097
+ return result;
6098
+ case TOK_FLATTEN:
6099
+ var original = this.visit(node.children[0], value);
6100
+ if (!isArray(original)) {
6101
+ return null;
6102
+ }
6103
+ var merged = [];
6104
+ for (i = 0;i < original.length; i++) {
6105
+ current = original[i];
6106
+ if (isArray(current)) {
6107
+ merged.push.apply(merged, current);
6108
+ } else {
6109
+ merged.push(current);
6110
+ }
6111
+ }
6112
+ return merged;
6113
+ case "Identity":
6114
+ return value;
6115
+ case "MultiSelectList":
6116
+ if (value === null) {
6117
+ return null;
6118
+ }
6119
+ collected = [];
6120
+ for (i = 0;i < node.children.length; i++) {
6121
+ collected.push(this.visit(node.children[i], value));
6122
+ }
6123
+ return collected;
6124
+ case "MultiSelectHash":
6125
+ if (value === null) {
6126
+ return null;
6127
+ }
6128
+ collected = {};
6129
+ var child;
6130
+ for (i = 0;i < node.children.length; i++) {
6131
+ child = node.children[i];
6132
+ collected[child.name] = this.visit(child.value, value);
6133
+ }
6134
+ return collected;
6135
+ case "OrExpression":
6136
+ matched = this.visit(node.children[0], value);
6137
+ if (isFalse(matched)) {
6138
+ matched = this.visit(node.children[1], value);
6139
+ }
6140
+ return matched;
6141
+ case "AndExpression":
6142
+ first = this.visit(node.children[0], value);
6143
+ if (isFalse(first) === true) {
6144
+ return first;
6145
+ }
6146
+ return this.visit(node.children[1], value);
6147
+ case "NotExpression":
6148
+ first = this.visit(node.children[0], value);
6149
+ return isFalse(first);
6150
+ case "Literal":
6151
+ return node.value;
6152
+ case TOK_PIPE:
6153
+ left = this.visit(node.children[0], value);
6154
+ return this.visit(node.children[1], left);
6155
+ case TOK_CURRENT:
6156
+ return value;
6157
+ case "Function":
6158
+ var resolvedArgs = [];
6159
+ for (i = 0;i < node.children.length; i++) {
6160
+ resolvedArgs.push(this.visit(node.children[i], value));
6161
+ }
6162
+ return this.runtime.callFunction(node.name, resolvedArgs);
6163
+ case "ExpressionReference":
6164
+ var refNode = node.children[0];
6165
+ refNode.jmespathType = TOK_EXPREF;
6166
+ return refNode;
6167
+ default:
6168
+ throw new Error("Unknown node type: " + node.type);
6872
6169
  }
6873
- const base = this.visit(left, value);
6874
- if (allowString && typeof base === "string") {
6875
- return this.visit(right, base);
6170
+ },
6171
+ computeSliceParams: function(arrayLength, sliceParams) {
6172
+ var start = sliceParams[0];
6173
+ var stop = sliceParams[1];
6174
+ var step = sliceParams[2];
6175
+ var computed = [null, null, null];
6176
+ if (step === null) {
6177
+ step = 1;
6178
+ } else if (step === 0) {
6179
+ var error = new Error("Invalid slice, step cannot be 0");
6180
+ error.name = "RuntimeError";
6181
+ throw error;
6876
6182
  }
6877
- if (!Array.isArray(base)) {
6878
- return null;
6183
+ var stepValueNegative = step < 0 ? true : false;
6184
+ if (start === null) {
6185
+ start = stepValueNegative ? arrayLength - 1 : 0;
6186
+ } else {
6187
+ start = this.capSliceRange(arrayLength, start, step);
6188
+ }
6189
+ if (stop === null) {
6190
+ stop = stepValueNegative ? -1 : arrayLength;
6191
+ } else {
6192
+ stop = this.capSliceRange(arrayLength, stop, step);
6879
6193
  }
6880
- const collected = [];
6881
- for (const elem of base) {
6882
- const current = this.visit(right, elem);
6883
- if (current !== null) {
6884
- collected.push(current);
6194
+ computed[0] = start;
6195
+ computed[1] = stop;
6196
+ computed[2] = step;
6197
+ return computed;
6198
+ },
6199
+ capSliceRange: function(arrayLength, actualValue, step) {
6200
+ if (actualValue < 0) {
6201
+ actualValue += arrayLength;
6202
+ if (actualValue < 0) {
6203
+ actualValue = step < 0 ? -1 : 0;
6885
6204
  }
6205
+ } else if (actualValue >= arrayLength) {
6206
+ actualValue = step < 0 ? arrayLength - 1 : arrayLength;
6886
6207
  }
6887
- return collected;
6208
+ return actualValue;
6888
6209
  }
6889
- case "ValueProjection": {
6890
- const { left, right } = node;
6891
- const base = this.visit(left, value);
6892
- if (base === null || typeof base !== "object" || Array.isArray(base)) {
6893
- return null;
6210
+ };
6211
+ function Runtime(interpreter) {
6212
+ this._interpreter = interpreter;
6213
+ this.functionTable = {
6214
+ abs: { _func: this._functionAbs, _signature: [{ types: [TYPE_NUMBER] }] },
6215
+ avg: { _func: this._functionAvg, _signature: [{ types: [TYPE_ARRAY_NUMBER] }] },
6216
+ ceil: { _func: this._functionCeil, _signature: [{ types: [TYPE_NUMBER] }] },
6217
+ contains: {
6218
+ _func: this._functionContains,
6219
+ _signature: [
6220
+ { types: [TYPE_STRING, TYPE_ARRAY] },
6221
+ { types: [TYPE_ANY] }
6222
+ ]
6223
+ },
6224
+ ends_with: {
6225
+ _func: this._functionEndsWith,
6226
+ _signature: [{ types: [TYPE_STRING] }, { types: [TYPE_STRING] }]
6227
+ },
6228
+ floor: { _func: this._functionFloor, _signature: [{ types: [TYPE_NUMBER] }] },
6229
+ length: {
6230
+ _func: this._functionLength,
6231
+ _signature: [{ types: [TYPE_STRING, TYPE_ARRAY, TYPE_OBJECT] }]
6232
+ },
6233
+ map: {
6234
+ _func: this._functionMap,
6235
+ _signature: [{ types: [TYPE_EXPREF] }, { types: [TYPE_ARRAY] }]
6236
+ },
6237
+ max: {
6238
+ _func: this._functionMax,
6239
+ _signature: [{ types: [TYPE_ARRAY_NUMBER, TYPE_ARRAY_STRING] }]
6240
+ },
6241
+ merge: {
6242
+ _func: this._functionMerge,
6243
+ _signature: [{ types: [TYPE_OBJECT], variadic: true }]
6244
+ },
6245
+ max_by: {
6246
+ _func: this._functionMaxBy,
6247
+ _signature: [{ types: [TYPE_ARRAY] }, { types: [TYPE_EXPREF] }]
6248
+ },
6249
+ sum: { _func: this._functionSum, _signature: [{ types: [TYPE_ARRAY_NUMBER] }] },
6250
+ starts_with: {
6251
+ _func: this._functionStartsWith,
6252
+ _signature: [{ types: [TYPE_STRING] }, { types: [TYPE_STRING] }]
6253
+ },
6254
+ min: {
6255
+ _func: this._functionMin,
6256
+ _signature: [{ types: [TYPE_ARRAY_NUMBER, TYPE_ARRAY_STRING] }]
6257
+ },
6258
+ min_by: {
6259
+ _func: this._functionMinBy,
6260
+ _signature: [{ types: [TYPE_ARRAY] }, { types: [TYPE_EXPREF] }]
6261
+ },
6262
+ type: { _func: this._functionType, _signature: [{ types: [TYPE_ANY] }] },
6263
+ keys: { _func: this._functionKeys, _signature: [{ types: [TYPE_OBJECT] }] },
6264
+ values: { _func: this._functionValues, _signature: [{ types: [TYPE_OBJECT] }] },
6265
+ sort: { _func: this._functionSort, _signature: [{ types: [TYPE_ARRAY_STRING, TYPE_ARRAY_NUMBER] }] },
6266
+ sort_by: {
6267
+ _func: this._functionSortBy,
6268
+ _signature: [{ types: [TYPE_ARRAY] }, { types: [TYPE_EXPREF] }]
6269
+ },
6270
+ join: {
6271
+ _func: this._functionJoin,
6272
+ _signature: [
6273
+ { types: [TYPE_STRING] },
6274
+ { types: [TYPE_ARRAY_STRING] }
6275
+ ]
6276
+ },
6277
+ reverse: {
6278
+ _func: this._functionReverse,
6279
+ _signature: [{ types: [TYPE_STRING, TYPE_ARRAY] }]
6280
+ },
6281
+ to_array: { _func: this._functionToArray, _signature: [{ types: [TYPE_ANY] }] },
6282
+ to_string: { _func: this._functionToString, _signature: [{ types: [TYPE_ANY] }] },
6283
+ to_number: { _func: this._functionToNumber, _signature: [{ types: [TYPE_ANY] }] },
6284
+ not_null: {
6285
+ _func: this._functionNotNull,
6286
+ _signature: [{ types: [TYPE_ANY], variadic: true }]
6287
+ }
6288
+ };
6289
+ }
6290
+ Runtime.prototype = {
6291
+ callFunction: function(name, resolvedArgs) {
6292
+ var functionEntry = this.functionTable[name];
6293
+ if (functionEntry === undefined) {
6294
+ throw new Error("Unknown function: " + name + "()");
6894
6295
  }
6895
- const collected = [];
6896
- const values = Object.values(base);
6897
- for (const elem of values) {
6898
- const current = this.visit(right, elem);
6899
- if (current !== null) {
6900
- collected.push(current);
6296
+ this._validateArgs(name, resolvedArgs, functionEntry._signature);
6297
+ return functionEntry._func.call(this, resolvedArgs);
6298
+ },
6299
+ _validateArgs: function(name, args, signature) {
6300
+ var pluralized;
6301
+ if (signature[signature.length - 1].variadic) {
6302
+ if (args.length < signature.length) {
6303
+ pluralized = signature.length === 1 ? " argument" : " arguments";
6304
+ throw new Error("ArgumentError: " + name + "() " + "takes at least" + signature.length + pluralized + " but received " + args.length);
6305
+ }
6306
+ } else if (args.length !== signature.length) {
6307
+ pluralized = signature.length === 1 ? " argument" : " arguments";
6308
+ throw new Error("ArgumentError: " + name + "() " + "takes " + signature.length + pluralized + " but received " + args.length);
6309
+ }
6310
+ var currentSpec;
6311
+ var actualType;
6312
+ var typeMatched;
6313
+ for (var i = 0;i < signature.length; i++) {
6314
+ typeMatched = false;
6315
+ currentSpec = signature[i].types;
6316
+ actualType = this._getTypeName(args[i]);
6317
+ for (var j = 0;j < currentSpec.length; j++) {
6318
+ if (this._typeMatches(actualType, currentSpec[j], args[i])) {
6319
+ typeMatched = true;
6320
+ break;
6321
+ }
6322
+ }
6323
+ if (!typeMatched) {
6324
+ var expected = currentSpec.map(function(typeIdentifier) {
6325
+ return TYPE_NAME_TABLE[typeIdentifier];
6326
+ }).join(",");
6327
+ throw new Error("TypeError: " + name + "() " + "expected argument " + (i + 1) + " to be type " + expected + " but received type " + TYPE_NAME_TABLE[actualType] + " instead.");
6901
6328
  }
6902
6329
  }
6903
- return collected;
6904
- }
6905
- case "FilterProjection": {
6906
- const { left, right, condition } = node;
6907
- const base = this.visit(left, value);
6908
- if (!Array.isArray(base)) {
6909
- return null;
6330
+ },
6331
+ _typeMatches: function(actual, expected, argValue) {
6332
+ if (expected === TYPE_ANY) {
6333
+ return true;
6910
6334
  }
6911
- const results = [];
6912
- for (const elem of base) {
6913
- const matched = this.visit(condition, elem);
6914
- if (isFalse(matched)) {
6915
- continue;
6335
+ if (expected === TYPE_ARRAY_STRING || expected === TYPE_ARRAY_NUMBER || expected === TYPE_ARRAY) {
6336
+ if (expected === TYPE_ARRAY) {
6337
+ return actual === TYPE_ARRAY;
6338
+ } else if (actual === TYPE_ARRAY) {
6339
+ var subtype;
6340
+ if (expected === TYPE_ARRAY_NUMBER) {
6341
+ subtype = TYPE_NUMBER;
6342
+ } else if (expected === TYPE_ARRAY_STRING) {
6343
+ subtype = TYPE_STRING;
6344
+ }
6345
+ for (var i = 0;i < argValue.length; i++) {
6346
+ if (!this._typeMatches(this._getTypeName(argValue[i]), subtype, argValue[i])) {
6347
+ return false;
6348
+ }
6349
+ }
6350
+ return true;
6916
6351
  }
6917
- const result = this.visit(right, elem);
6918
- if (result !== null) {
6919
- results.push(result);
6352
+ } else {
6353
+ return actual === expected;
6354
+ }
6355
+ },
6356
+ _getTypeName: function(obj) {
6357
+ switch (Object.prototype.toString.call(obj)) {
6358
+ case "[object String]":
6359
+ return TYPE_STRING;
6360
+ case "[object Number]":
6361
+ return TYPE_NUMBER;
6362
+ case "[object Array]":
6363
+ return TYPE_ARRAY;
6364
+ case "[object Boolean]":
6365
+ return TYPE_BOOLEAN;
6366
+ case "[object Null]":
6367
+ return TYPE_NULL;
6368
+ case "[object Object]":
6369
+ if (obj.jmespathType === TOK_EXPREF) {
6370
+ return TYPE_EXPREF;
6371
+ } else {
6372
+ return TYPE_OBJECT;
6373
+ }
6374
+ }
6375
+ },
6376
+ _functionStartsWith: function(resolvedArgs) {
6377
+ return resolvedArgs[0].lastIndexOf(resolvedArgs[1]) === 0;
6378
+ },
6379
+ _functionEndsWith: function(resolvedArgs) {
6380
+ var searchStr = resolvedArgs[0];
6381
+ var suffix = resolvedArgs[1];
6382
+ return searchStr.indexOf(suffix, searchStr.length - suffix.length) !== -1;
6383
+ },
6384
+ _functionReverse: function(resolvedArgs) {
6385
+ var typeName = this._getTypeName(resolvedArgs[0]);
6386
+ if (typeName === TYPE_STRING) {
6387
+ var originalStr = resolvedArgs[0];
6388
+ var reversedStr = "";
6389
+ for (var i = originalStr.length - 1;i >= 0; i--) {
6390
+ reversedStr += originalStr[i];
6920
6391
  }
6392
+ return reversedStr;
6393
+ } else {
6394
+ var reversedArray = resolvedArgs[0].slice(0);
6395
+ reversedArray.reverse();
6396
+ return reversedArray;
6921
6397
  }
6922
- return results;
6923
- }
6924
- case "Arithmetic": {
6925
- const first = this.visit(node.left, value);
6926
- const second = this.visit(node.right, value);
6927
- switch (node.operator) {
6928
- case "Plus":
6929
- return add(first, second);
6930
- case "Minus":
6931
- return sub(first, second);
6932
- case "Multiply":
6933
- case "Star":
6934
- return mul(first, second);
6935
- case "Divide":
6936
- return divide(first, second);
6937
- case "Modulo":
6938
- return mod(first, second);
6939
- case "Div":
6940
- return div(first, second);
6941
- default:
6942
- throw new Error(`Syntax error: unknown arithmetic operator: ${node.operator}`);
6943
- }
6944
- }
6945
- case "Unary": {
6946
- const operand = this.visit(node.operand, value);
6947
- switch (node.operator) {
6948
- case "Plus":
6949
- ensureNumbers(operand);
6950
- return operand;
6951
- case "Minus":
6952
- ensureNumbers(operand);
6953
- return -operand;
6954
- default:
6955
- throw new Error(`Syntax error: unknown arithmetic operator: ${node.operator}`);
6398
+ },
6399
+ _functionAbs: function(resolvedArgs) {
6400
+ return Math.abs(resolvedArgs[0]);
6401
+ },
6402
+ _functionCeil: function(resolvedArgs) {
6403
+ return Math.ceil(resolvedArgs[0]);
6404
+ },
6405
+ _functionAvg: function(resolvedArgs) {
6406
+ var sum = 0;
6407
+ var inputArray = resolvedArgs[0];
6408
+ for (var i = 0;i < inputArray.length; i++) {
6409
+ sum += inputArray[i];
6956
6410
  }
6957
- }
6958
- case "Comparator": {
6959
- const first = this.visit(node.left, value);
6960
- const second = this.visit(node.right, value);
6961
- switch (node.name) {
6962
- case "EQ":
6963
- return strictDeepEqual(first, second);
6964
- case "NE":
6965
- return !strictDeepEqual(first, second);
6411
+ return sum / inputArray.length;
6412
+ },
6413
+ _functionContains: function(resolvedArgs) {
6414
+ return resolvedArgs[0].indexOf(resolvedArgs[1]) >= 0;
6415
+ },
6416
+ _functionFloor: function(resolvedArgs) {
6417
+ return Math.floor(resolvedArgs[0]);
6418
+ },
6419
+ _functionLength: function(resolvedArgs) {
6420
+ if (!isObject(resolvedArgs[0])) {
6421
+ return resolvedArgs[0].length;
6422
+ } else {
6423
+ return Object.keys(resolvedArgs[0]).length;
6424
+ }
6425
+ },
6426
+ _functionMap: function(resolvedArgs) {
6427
+ var mapped = [];
6428
+ var interpreter = this._interpreter;
6429
+ var exprefNode = resolvedArgs[0];
6430
+ var elements = resolvedArgs[1];
6431
+ for (var i = 0;i < elements.length; i++) {
6432
+ mapped.push(interpreter.visit(exprefNode, elements[i]));
6433
+ }
6434
+ return mapped;
6435
+ },
6436
+ _functionMerge: function(resolvedArgs) {
6437
+ var merged = {};
6438
+ for (var i = 0;i < resolvedArgs.length; i++) {
6439
+ var current = resolvedArgs[i];
6440
+ for (var key in current) {
6441
+ merged[key] = current[key];
6442
+ }
6443
+ }
6444
+ return merged;
6445
+ },
6446
+ _functionMax: function(resolvedArgs) {
6447
+ if (resolvedArgs[0].length > 0) {
6448
+ var typeName = this._getTypeName(resolvedArgs[0][0]);
6449
+ if (typeName === TYPE_NUMBER) {
6450
+ return Math.max.apply(Math, resolvedArgs[0]);
6451
+ } else {
6452
+ var elements = resolvedArgs[0];
6453
+ var maxElement = elements[0];
6454
+ for (var i = 1;i < elements.length; i++) {
6455
+ if (maxElement.localeCompare(elements[i]) < 0) {
6456
+ maxElement = elements[i];
6457
+ }
6458
+ }
6459
+ return maxElement;
6460
+ }
6461
+ } else {
6462
+ return null;
6966
6463
  }
6967
- if (typeof first !== "number" || typeof second !== "number") {
6464
+ },
6465
+ _functionMin: function(resolvedArgs) {
6466
+ if (resolvedArgs[0].length > 0) {
6467
+ var typeName = this._getTypeName(resolvedArgs[0][0]);
6468
+ if (typeName === TYPE_NUMBER) {
6469
+ return Math.min.apply(Math, resolvedArgs[0]);
6470
+ } else {
6471
+ var elements = resolvedArgs[0];
6472
+ var minElement = elements[0];
6473
+ for (var i = 1;i < elements.length; i++) {
6474
+ if (elements[i].localeCompare(minElement) < 0) {
6475
+ minElement = elements[i];
6476
+ }
6477
+ }
6478
+ return minElement;
6479
+ }
6480
+ } else {
6968
6481
  return null;
6969
6482
  }
6970
- switch (node.name) {
6971
- case "GT":
6972
- return first > second;
6973
- case "GTE":
6974
- return first >= second;
6975
- case "LT":
6976
- return first < second;
6977
- case "LTE":
6978
- return first <= second;
6483
+ },
6484
+ _functionSum: function(resolvedArgs) {
6485
+ var sum = 0;
6486
+ var listToSum = resolvedArgs[0];
6487
+ for (var i = 0;i < listToSum.length; i++) {
6488
+ sum += listToSum[i];
6979
6489
  }
6980
- }
6981
- case "Flatten": {
6982
- const original = this.visit(node.child, value);
6983
- return Array.isArray(original) ? original.flat() : null;
6984
- }
6985
- case "Root":
6986
- return this._rootValue;
6987
- case "MultiSelectList": {
6988
- const collected = [];
6989
- for (const child of node.children) {
6990
- collected.push(this.visit(child, value));
6490
+ return sum;
6491
+ },
6492
+ _functionType: function(resolvedArgs) {
6493
+ switch (this._getTypeName(resolvedArgs[0])) {
6494
+ case TYPE_NUMBER:
6495
+ return "number";
6496
+ case TYPE_STRING:
6497
+ return "string";
6498
+ case TYPE_ARRAY:
6499
+ return "array";
6500
+ case TYPE_OBJECT:
6501
+ return "object";
6502
+ case TYPE_BOOLEAN:
6503
+ return "boolean";
6504
+ case TYPE_EXPREF:
6505
+ return "expref";
6506
+ case TYPE_NULL:
6507
+ return "null";
6991
6508
  }
6992
- return collected;
6993
- }
6994
- case "MultiSelectHash": {
6995
- const collected = {};
6996
- for (const child of node.children) {
6997
- collected[child.name] = this.visit(child.value, value);
6509
+ },
6510
+ _functionKeys: function(resolvedArgs) {
6511
+ return Object.keys(resolvedArgs[0]);
6512
+ },
6513
+ _functionValues: function(resolvedArgs) {
6514
+ var obj = resolvedArgs[0];
6515
+ var keys = Object.keys(obj);
6516
+ var values = [];
6517
+ for (var i = 0;i < keys.length; i++) {
6518
+ values.push(obj[keys[i]]);
6519
+ }
6520
+ return values;
6521
+ },
6522
+ _functionJoin: function(resolvedArgs) {
6523
+ var joinChar = resolvedArgs[0];
6524
+ var listJoin = resolvedArgs[1];
6525
+ return listJoin.join(joinChar);
6526
+ },
6527
+ _functionToArray: function(resolvedArgs) {
6528
+ if (this._getTypeName(resolvedArgs[0]) === TYPE_ARRAY) {
6529
+ return resolvedArgs[0];
6530
+ } else {
6531
+ return [resolvedArgs[0]];
6998
6532
  }
6999
- return collected;
7000
- }
7001
- case "OrExpression": {
7002
- const result = this.visit(node.left, value);
7003
- if (isFalse(result)) {
7004
- return this.visit(node.right, value);
6533
+ },
6534
+ _functionToString: function(resolvedArgs) {
6535
+ if (this._getTypeName(resolvedArgs[0]) === TYPE_STRING) {
6536
+ return resolvedArgs[0];
6537
+ } else {
6538
+ return JSON.stringify(resolvedArgs[0]);
7005
6539
  }
7006
- return result;
7007
- }
7008
- case "AndExpression": {
7009
- const result = this.visit(node.left, value);
7010
- if (isFalse(result)) {
7011
- return result;
6540
+ },
6541
+ _functionToNumber: function(resolvedArgs) {
6542
+ var typeName = this._getTypeName(resolvedArgs[0]);
6543
+ var convertedValue;
6544
+ if (typeName === TYPE_NUMBER) {
6545
+ return resolvedArgs[0];
6546
+ } else if (typeName === TYPE_STRING) {
6547
+ convertedValue = +resolvedArgs[0];
6548
+ if (!isNaN(convertedValue)) {
6549
+ return convertedValue;
6550
+ }
7012
6551
  }
7013
- return this.visit(node.right, value);
7014
- }
7015
- case "NotExpression":
7016
- return isFalse(this.visit(node.child, value));
7017
- case "Literal":
7018
- return node.value;
7019
- case "Pipe":
7020
- return this.visit(node.right, this.visit(node.left, value));
7021
- case "Function": {
7022
- const args = [];
7023
- for (const child of node.children) {
7024
- args.push(this.visit(child, value));
6552
+ return null;
6553
+ },
6554
+ _functionNotNull: function(resolvedArgs) {
6555
+ for (var i = 0;i < resolvedArgs.length; i++) {
6556
+ if (this._getTypeName(resolvedArgs[i]) !== TYPE_NULL) {
6557
+ return resolvedArgs[i];
6558
+ }
7025
6559
  }
7026
- return this.runtime.callFunction(node.name, args);
7027
- }
7028
- case "ExpressionReference":
7029
- return {
7030
- expref: true,
7031
- ...node.child
6560
+ return null;
6561
+ },
6562
+ _functionSort: function(resolvedArgs) {
6563
+ var sortedArray = resolvedArgs[0].slice(0);
6564
+ sortedArray.sort();
6565
+ return sortedArray;
6566
+ },
6567
+ _functionSortBy: function(resolvedArgs) {
6568
+ var sortedArray = resolvedArgs[0].slice(0);
6569
+ if (sortedArray.length === 0) {
6570
+ return sortedArray;
6571
+ }
6572
+ var interpreter = this._interpreter;
6573
+ var exprefNode = resolvedArgs[1];
6574
+ var requiredType = this._getTypeName(interpreter.visit(exprefNode, sortedArray[0]));
6575
+ if ([TYPE_NUMBER, TYPE_STRING].indexOf(requiredType) < 0) {
6576
+ throw new Error("TypeError");
6577
+ }
6578
+ var that = this;
6579
+ var decorated = [];
6580
+ for (var i = 0;i < sortedArray.length; i++) {
6581
+ decorated.push([i, sortedArray[i]]);
6582
+ }
6583
+ decorated.sort(function(a, b) {
6584
+ var exprA = interpreter.visit(exprefNode, a[1]);
6585
+ var exprB = interpreter.visit(exprefNode, b[1]);
6586
+ if (that._getTypeName(exprA) !== requiredType) {
6587
+ throw new Error("TypeError: expected " + requiredType + ", received " + that._getTypeName(exprA));
6588
+ } else if (that._getTypeName(exprB) !== requiredType) {
6589
+ throw new Error("TypeError: expected " + requiredType + ", received " + that._getTypeName(exprB));
6590
+ }
6591
+ if (exprA > exprB) {
6592
+ return 1;
6593
+ } else if (exprA < exprB) {
6594
+ return -1;
6595
+ } else {
6596
+ return a[0] - b[0];
6597
+ }
6598
+ });
6599
+ for (var j = 0;j < decorated.length; j++) {
6600
+ sortedArray[j] = decorated[j][1];
6601
+ }
6602
+ return sortedArray;
6603
+ },
6604
+ _functionMaxBy: function(resolvedArgs) {
6605
+ var exprefNode = resolvedArgs[1];
6606
+ var resolvedArray = resolvedArgs[0];
6607
+ var keyFunction = this.createKeyFunction(exprefNode, [TYPE_NUMBER, TYPE_STRING]);
6608
+ var maxNumber = -Infinity;
6609
+ var maxRecord;
6610
+ var current;
6611
+ for (var i = 0;i < resolvedArray.length; i++) {
6612
+ current = keyFunction(resolvedArray[i]);
6613
+ if (current > maxNumber) {
6614
+ maxNumber = current;
6615
+ maxRecord = resolvedArray[i];
6616
+ }
6617
+ }
6618
+ return maxRecord;
6619
+ },
6620
+ _functionMinBy: function(resolvedArgs) {
6621
+ var exprefNode = resolvedArgs[1];
6622
+ var resolvedArray = resolvedArgs[0];
6623
+ var keyFunction = this.createKeyFunction(exprefNode, [TYPE_NUMBER, TYPE_STRING]);
6624
+ var minNumber = Infinity;
6625
+ var minRecord;
6626
+ var current;
6627
+ for (var i = 0;i < resolvedArray.length; i++) {
6628
+ current = keyFunction(resolvedArray[i]);
6629
+ if (current < minNumber) {
6630
+ minNumber = current;
6631
+ minRecord = resolvedArray[i];
6632
+ }
6633
+ }
6634
+ return minRecord;
6635
+ },
6636
+ createKeyFunction: function(exprefNode, allowedTypes) {
6637
+ var that = this;
6638
+ var interpreter = this._interpreter;
6639
+ var keyFunc = function(x) {
6640
+ var current = interpreter.visit(exprefNode, x);
6641
+ if (allowedTypes.indexOf(that._getTypeName(current)) < 0) {
6642
+ var msg = "TypeError: expected one of " + allowedTypes + ", received " + that._getTypeName(current);
6643
+ throw new Error(msg);
6644
+ }
6645
+ return current;
7032
6646
  };
7033
- case "Current":
7034
- case "Identity":
7035
- return value;
7036
- }
7037
- }
7038
- computeSliceParams(arrayLength, sliceNode) {
7039
- let { start, stop, step } = sliceNode;
7040
- if (step === null) {
7041
- step = 1;
7042
- } else if (step === 0) {
7043
- const error = new Error("Invalid value: slice step cannot be 0");
7044
- error.name = "RuntimeError";
7045
- throw error;
7046
- }
7047
- start = start === null ? step < 0 ? arrayLength - 1 : 0 : this.capSliceRange(arrayLength, start, step);
7048
- stop = stop === null ? step < 0 ? -1 : arrayLength : this.capSliceRange(arrayLength, stop, step);
7049
- return { start, stop, step };
7050
- }
7051
- capSliceRange(arrayLength, actualValue, step) {
7052
- let nextActualValue = actualValue;
7053
- if (nextActualValue < 0) {
7054
- nextActualValue += arrayLength;
7055
- if (nextActualValue < 0) {
7056
- nextActualValue = step < 0 ? -1 : 0;
7057
- }
7058
- } else if (nextActualValue >= arrayLength) {
7059
- nextActualValue = step < 0 ? arrayLength - 1 : arrayLength;
7060
- }
7061
- return nextActualValue;
7062
- }
7063
- slice(collection, start, end, step) {
7064
- const result = [];
7065
- if (step > 0) {
7066
- for (let i = start;i < end; i += step) {
7067
- result.push(collection[i]);
7068
- }
7069
- } else {
7070
- for (let i = start;i > end; i += step) {
7071
- result.push(collection[i]);
6647
+ return keyFunc;
7072
6648
  }
7073
- }
7074
- return result;
7075
- }
7076
- }, TreeInterpreterInstance, TreeInterpreter_default, TYPE_ANY = 1, TYPE_ARRAY = 3, TYPE_ARRAY_ARRAY = 11, TYPE_ARRAY_NUMBER = 8, TYPE_ARRAY_OBJECT = 10, TYPE_ARRAY_STRING = 9, TYPE_BOOLEAN = 5, TYPE_EXPREF = 6, TYPE_NULL = 7, TYPE_NUMBER = 0, TYPE_OBJECT = 4, TYPE_STRING = 2, registerFunction = (functionName, customFunction, signature, options) => {
7077
- TreeInterpreter_default.runtime.registerFunction(functionName, customFunction, signature, options);
7078
- }, register = (name, customFunction, signature, options) => {
7079
- return TreeInterpreter_default.runtime.register(name, customFunction, signature, options);
7080
- }, unregisterFunction = (name) => {
7081
- return TreeInterpreter_default.runtime.unregister(name);
7082
- }, isRegistered = (name) => {
7083
- return TreeInterpreter_default.runtime.isRegistered(name);
7084
- }, getRegisteredFunctions = () => {
7085
- return TreeInterpreter_default.runtime.getRegistered();
7086
- }, getCustomFunctions = () => {
7087
- return TreeInterpreter_default.runtime.getCustomFunctions();
7088
- }, clearCustomFunctions = () => {
7089
- TreeInterpreter_default.runtime.clearCustomFunctions();
7090
- }, TreeInterpreter2, jmespath;
7091
- var init_dist = __esm(() => {
7092
- basicTokens = {
7093
- "(": "Lparen",
7094
- ")": "Rparen",
7095
- "*": "Star",
7096
- ",": "Comma",
7097
- ".": "Dot",
7098
- ":": "Colon",
7099
- "@": "Current",
7100
- "]": "Rbracket",
7101
- "{": "Lbrace",
7102
- "}": "Rbrace",
7103
- "+": "Plus",
7104
- "%": "Modulo",
7105
- "?": "Question",
7106
- "−": "Minus",
7107
- "×": "Multiply",
7108
- "÷": "Divide"
7109
- };
7110
- operatorStartToken = {
7111
- "!": true,
7112
- "<": true,
7113
- "=": true,
7114
- ">": true,
7115
- "&": true,
7116
- "|": true,
7117
- "/": true
7118
- };
7119
- skipChars = {
7120
- "\t": true,
7121
- "\n": true,
7122
- "\r": true,
7123
- " ": true
7124
- };
7125
- Lexer = new StreamLexer;
7126
- Lexer_default = Lexer;
7127
- bindingPower = {
7128
- ["EOF"]: 0,
7129
- ["Variable"]: 0,
7130
- ["UnquotedIdentifier"]: 0,
7131
- ["QuotedIdentifier"]: 0,
7132
- ["Rbracket"]: 0,
7133
- ["Rparen"]: 0,
7134
- ["Comma"]: 0,
7135
- ["Rbrace"]: 0,
7136
- ["Number"]: 0,
7137
- ["Current"]: 0,
7138
- ["Expref"]: 0,
7139
- ["Root"]: 0,
7140
- ["Assign"]: 1,
7141
- ["Pipe"]: 1,
7142
- ["Question"]: 2,
7143
- ["Or"]: 3,
7144
- ["And"]: 4,
7145
- ["EQ"]: 5,
7146
- ["GT"]: 5,
7147
- ["LT"]: 5,
7148
- ["GTE"]: 5,
7149
- ["LTE"]: 5,
7150
- ["NE"]: 5,
7151
- ["Minus"]: 6,
7152
- ["Plus"]: 6,
7153
- ["Div"]: 7,
7154
- ["Divide"]: 7,
7155
- ["Modulo"]: 7,
7156
- ["Multiply"]: 7,
7157
- ["Flatten"]: 9,
7158
- ["Star"]: 20,
7159
- ["Filter"]: 21,
7160
- ["Dot"]: 40,
7161
- ["Not"]: 45,
7162
- ["Lbrace"]: 50,
7163
- ["Lbracket"]: 55,
7164
- ["Lparen"]: 60
7165
- };
7166
- Parser = new TokenParser;
7167
- Parser_default = Parser;
7168
- emptyScopeChain = new ScopeChain;
7169
- TreeInterpreterInstance = new TreeInterpreter;
7170
- TreeInterpreter_default = TreeInterpreterInstance;
7171
- TreeInterpreter2 = TreeInterpreter_default;
7172
- jmespath = {
7173
- compile,
7174
- registerFunction,
7175
- register,
7176
- unregisterFunction,
7177
- isRegistered,
7178
- getRegisteredFunctions,
7179
- getCustomFunctions,
7180
- clearCustomFunctions,
7181
- search,
7182
- tokenize,
7183
- TreeInterpreter: TreeInterpreter2,
7184
- TYPE_ANY,
7185
- TYPE_ARRAY_NUMBER,
7186
- TYPE_ARRAY_STRING,
7187
- TYPE_ARRAY,
7188
- TYPE_BOOLEAN,
7189
- TYPE_EXPREF,
7190
- TYPE_NULL,
7191
- TYPE_NUMBER,
7192
- TYPE_OBJECT,
7193
- TYPE_STRING
7194
- };
6649
+ };
6650
+ function compile(stream) {
6651
+ var parser = new Parser;
6652
+ var ast = parser.parse(stream);
6653
+ return ast;
6654
+ }
6655
+ function tokenize(stream) {
6656
+ var lexer = new Lexer;
6657
+ return lexer.tokenize(stream);
6658
+ }
6659
+ function search(data, expression) {
6660
+ var parser = new Parser;
6661
+ var runtime = new Runtime;
6662
+ var interpreter = new TreeInterpreter(runtime);
6663
+ runtime._interpreter = interpreter;
6664
+ var node = parser.parse(expression);
6665
+ return interpreter.search(node, data);
6666
+ }
6667
+ exports2.tokenize = tokenize;
6668
+ exports2.compile = compile;
6669
+ exports2.search = search;
6670
+ exports2.strictDeepEqual = strictDeepEqual;
6671
+ })(typeof exports === "undefined" ? exports.jmespath = {} : exports);
7195
6672
  });
7196
6673
 
7197
6674
  // ../common/src/output-codecs.ts
7198
6675
  async function loadOutputCodecsAsync(needed) {
7199
6676
  const loads = [];
7200
6677
  if (needed.yaml && yamlSlot.get() === undefined) {
7201
- loads.push(Promise.resolve().then(() => (init_js_yaml(), exports_js_yaml)).then((mod2) => {
7202
- yamlSlot.set(mod2);
6678
+ loads.push(Promise.resolve().then(() => (init_js_yaml(), exports_js_yaml)).then((mod) => {
6679
+ yamlSlot.set(mod);
7203
6680
  }));
7204
6681
  }
7205
6682
  if (needed.filter && jmespathSlot.get() === undefined) {
7206
- loads.push(Promise.resolve().then(() => (init_dist(), exports_dist)).then((mod2) => {
7207
- jmespathSlot.set(mod2);
6683
+ loads.push(Promise.resolve().then(() => __toESM(require_jmespath(), 1)).then((mod) => {
6684
+ jmespathSlot.set(mod);
7208
6685
  }));
7209
6686
  }
7210
6687
  await Promise.all(loads);
@@ -8017,8 +7494,8 @@ var init_telemetry_service = __esm(() => {
8017
7494
 
8018
7495
  // ../common/src/timings.ts
8019
7496
  function createStorage2() {
8020
- const [error, mod2] = catchError(() => __require("node:async_hooks"));
8021
- if (error || typeof mod2?.AsyncLocalStorage !== "function") {
7497
+ const [error, mod] = catchError(() => __require("node:async_hooks"));
7498
+ if (error || typeof mod?.AsyncLocalStorage !== "function") {
8022
7499
  return {
8023
7500
  getStore: () => {
8024
7501
  return;
@@ -8026,7 +7503,7 @@ function createStorage2() {
8026
7503
  run: (_store, fn) => fn()
8027
7504
  };
8028
7505
  }
8029
- return new mod2.AsyncLocalStorage;
7506
+ return new mod.AsyncLocalStorage;
8030
7507
  }
8031
7508
  function newState(baseline) {
8032
7509
  return { baseline, httpMs: 0, httpCalls: 0 };
@@ -9160,10 +8637,10 @@ var require_help = __commonJS(function(exports) {
9160
8637
  });
9161
8638
  output = output.concat(this.formatItemList("Global Options:", globalOptionList, helper));
9162
8639
  }
9163
- const commandGroups = this.groupItems(cmd.commands, helper.visibleCommands(cmd), (sub2) => sub2.helpGroup() || "Commands:");
8640
+ const commandGroups = this.groupItems(cmd.commands, helper.visibleCommands(cmd), (sub) => sub.helpGroup() || "Commands:");
9164
8641
  commandGroups.forEach((commands, group) => {
9165
- const commandList = commands.map((sub2) => {
9166
- return callFormatItem(helper.styleSubcommandTerm(helper.subcommandTerm(sub2)), helper.styleSubcommandDescription(helper.subcommandDescription(sub2)));
8642
+ const commandList = commands.map((sub) => {
8643
+ return callFormatItem(helper.styleSubcommandTerm(helper.subcommandTerm(sub)), helper.styleSubcommandDescription(helper.subcommandDescription(sub)));
9167
8644
  });
9168
8645
  output = output.concat(this.formatItemList(group, commandList, helper));
9169
8646
  });
@@ -12272,4 +11749,4 @@ export {
12272
11749
  studioWebPost
12273
11750
  };
12274
11751
 
12275
- //# debugId=5B32F6308AC8847564756E2164756E21
11752
+ //# debugId=C0165D474749BAC464756E2164756E21