@uipath/common 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.
package/dist/index.js CHANGED
@@ -3810,2006 +3810,1483 @@ var init_js_yaml = __esm(() => {
3810
3810
  } = yaml);
3811
3811
  });
3812
3812
 
3813
- // ../../node_modules/@jmespath-community/jmespath/dist/index.mjs
3814
- var exports_dist = {};
3815
- __export(exports_dist, {
3816
- Scope: () => Scope,
3817
- TYPE_ANY: () => TYPE_ANY,
3818
- TYPE_ARRAY: () => TYPE_ARRAY,
3819
- TYPE_ARRAY_ARRAY: () => TYPE_ARRAY_ARRAY,
3820
- TYPE_ARRAY_NUMBER: () => TYPE_ARRAY_NUMBER,
3821
- TYPE_ARRAY_OBJECT: () => TYPE_ARRAY_OBJECT,
3822
- TYPE_ARRAY_STRING: () => TYPE_ARRAY_STRING,
3823
- TYPE_BOOLEAN: () => TYPE_BOOLEAN,
3824
- TYPE_EXPREF: () => TYPE_EXPREF,
3825
- TYPE_NULL: () => TYPE_NULL,
3826
- TYPE_NUMBER: () => TYPE_NUMBER,
3827
- TYPE_OBJECT: () => TYPE_OBJECT,
3828
- TYPE_STRING: () => TYPE_STRING,
3829
- TreeInterpreter: () => TreeInterpreter2,
3830
- clearCustomFunctions: () => clearCustomFunctions,
3831
- compile: () => compile,
3832
- default: () => jmespath,
3833
- getCustomFunctions: () => getCustomFunctions,
3834
- getRegisteredFunctions: () => getRegisteredFunctions,
3835
- isRegistered: () => isRegistered,
3836
- jmespath: () => jmespath,
3837
- register: () => register,
3838
- registerFunction: () => registerFunction,
3839
- search: () => search,
3840
- tokenize: () => tokenize,
3841
- unregisterFunction: () => unregisterFunction
3842
- });
3843
- function compile(expression, options) {
3844
- const nodeTree = Parser_default.parse(expression, options);
3845
- return nodeTree;
3846
- }
3847
- function tokenize(expression, options) {
3848
- return Lexer_default.tokenize(expression, options);
3849
- }
3850
- function search(data, expression, options) {
3851
- const nodeTree = Parser_default.parse(expression, options);
3852
- return TreeInterpreter_default.search(nodeTree, data);
3853
- }
3854
- function Scope() {
3855
- return new ScopeChain;
3856
- }
3857
- var isObject = (obj) => {
3858
- return obj !== null && Object.prototype.toString.call(obj) === "[object Object]";
3859
- }, strictDeepEqual = (first, second) => {
3860
- if (first === second) {
3861
- return true;
3862
- }
3863
- if (typeof first !== typeof second) {
3864
- return false;
3865
- }
3866
- if (Array.isArray(first) && Array.isArray(second)) {
3867
- if (first.length !== second.length) {
3868
- return false;
3869
- }
3870
- for (let i = 0;i < first.length; i += 1) {
3871
- if (!strictDeepEqual(first[i], second[i])) {
3872
- return false;
3873
- }
3874
- }
3875
- return true;
3876
- }
3877
- if (isObject(first) && isObject(second)) {
3878
- const firstEntries = Object.entries(first);
3879
- const secondKeys = new Set(Object.keys(second));
3880
- if (firstEntries.length !== secondKeys.size) {
3881
- return false;
3882
- }
3883
- for (const [key, value] of firstEntries) {
3884
- if (!strictDeepEqual(value, second[key])) {
3813
+ // ../../node_modules/jmespath/jmespath.js
3814
+ var require_jmespath = __commonJS(function(exports) {
3815
+ (function(exports2) {
3816
+ function isArray(obj) {
3817
+ if (obj !== null) {
3818
+ return Object.prototype.toString.call(obj) === "[object Array]";
3819
+ } else {
3885
3820
  return false;
3886
3821
  }
3887
- secondKeys.delete(key);
3888
3822
  }
3889
- return secondKeys.size === 0;
3890
- }
3891
- return false;
3892
- }, isFalse = (obj) => {
3893
- if (obj === null || obj === undefined || obj === false) {
3894
- return true;
3895
- }
3896
- if (typeof obj === "string") {
3897
- return obj === "";
3898
- }
3899
- if (typeof obj === "object") {
3900
- if (Array.isArray(obj)) {
3901
- return obj.length === 0;
3902
- }
3903
- if (obj === null) {
3904
- return true;
3905
- }
3906
- return Object.keys(obj).length === 0;
3907
- }
3908
- return false;
3909
- }, isAlpha = (ch) => {
3910
- return ch >= "a" && ch <= "z" || ch >= "A" && ch <= "Z" || ch === "_";
3911
- }, isNum = (ch) => {
3912
- return ch >= "0" && ch <= "9" || ch === "-";
3913
- }, isAlphaNum = (ch) => {
3914
- return ch >= "a" && ch <= "z" || ch >= "A" && ch <= "Z" || ch >= "0" && ch <= "9" || ch === "_";
3915
- }, ensureInteger = (value) => {
3916
- if (!(typeof value === "number") || Math.floor(value) !== value) {
3917
- throw new Error("invalid-value: expecting an integer.");
3918
- }
3919
- return value;
3920
- }, ensurePositiveInteger = (value) => {
3921
- if (!(typeof value === "number") || value < 0 || Math.floor(value) !== value) {
3922
- throw new Error("invalid-value: expecting a non-negative integer.");
3923
- }
3924
- return value;
3925
- }, ensureNumbers = (...operands) => {
3926
- for (let i = 0;i < operands.length; i++) {
3927
- if (operands[i] === null || operands[i] === undefined) {
3928
- throw new Error("not-a-number: undefined");
3929
- }
3930
- if (typeof operands[i] !== "number") {
3931
- throw new Error("not-a-number");
3932
- }
3933
- }
3934
- }, notZero = (n) => {
3935
- n = +n;
3936
- if (!n) {
3937
- throw new Error("not-a-number: divide by zero");
3938
- }
3939
- return n;
3940
- }, add = (left, right) => {
3941
- ensureNumbers(left, right);
3942
- const result = left + right;
3943
- return result;
3944
- }, sub = (left, right) => {
3945
- ensureNumbers(left, right);
3946
- const result = left - right;
3947
- return result;
3948
- }, mul = (left, right) => {
3949
- ensureNumbers(left, right);
3950
- const result = left * right;
3951
- return result;
3952
- }, divide = (left, right) => {
3953
- ensureNumbers(left, right);
3954
- const result = left / notZero(right);
3955
- return result;
3956
- }, div = (left, right) => {
3957
- ensureNumbers(left, right);
3958
- const result = Math.floor(left / notZero(right));
3959
- return result;
3960
- }, mod = (left, right) => {
3961
- ensureNumbers(left, right);
3962
- const result = left % right;
3963
- return result;
3964
- }, findFirst = (subject, sub2, start, end) => {
3965
- if (!subject || !sub2) {
3966
- return null;
3967
- }
3968
- start = Math.max(ensureInteger(start = start || 0), 0);
3969
- end = Math.min(ensureInteger(end = end || subject.length), subject.length);
3970
- const offset = subject.slice(start, end).indexOf(sub2);
3971
- return offset === -1 ? null : offset + start;
3972
- }, findLast = (subject, sub2, start, end) => {
3973
- if (!subject || !sub2) {
3974
- return null;
3975
- }
3976
- start = Math.max(ensureInteger(start = start || 0), 0);
3977
- end = Math.min(ensureInteger(end = end || subject.length), subject.length);
3978
- const offset = subject.slice(start, end).lastIndexOf(sub2);
3979
- const result = offset === -1 ? null : offset + start;
3980
- return result;
3981
- }, lower = (subject) => subject.toLowerCase(), ensurePadFuncParams = (name, width, padding) => {
3982
- padding = padding || " ";
3983
- if (padding.length > 1) {
3984
- throw new Error(`invalid value, ${name} expects its 'pad' parameter to be a valid string with a single codepoint`);
3985
- }
3986
- ensurePositiveInteger(width);
3987
- return padding;
3988
- }, padLeft = (subject, width, padding) => {
3989
- padding = ensurePadFuncParams("pad_left", width, padding);
3990
- return subject && subject.padStart(width, padding) || "";
3991
- }, padRight = (subject, width, padding) => {
3992
- padding = ensurePadFuncParams("pad_right", width, padding);
3993
- return subject && subject.padEnd(width, padding) || "";
3994
- }, replace = (subject, string, by, count) => {
3995
- if (count === 0) {
3996
- return subject;
3997
- }
3998
- if (!count) {
3999
- return subject.split(string).join(by);
4000
- }
4001
- ensurePositiveInteger(count);
4002
- [...Array(count).keys()].map(() => subject = subject.replace(string, by));
4003
- return subject;
4004
- }, split = (subject, search2, count) => {
4005
- if (subject.length == 0 && search2.length === 0) {
4006
- return [];
4007
- }
4008
- if (count === null || count === undefined) {
4009
- return subject.split(search2);
4010
- }
4011
- ensurePositiveInteger(count);
4012
- if (count === 0) {
4013
- return [subject];
4014
- }
4015
- const split2 = subject.split(search2);
4016
- return [...split2.slice(0, count), split2.slice(count).join(search2)];
4017
- }, trim = (subject, chars) => {
4018
- return trimLeft(trimRight(subject, chars), chars);
4019
- }, trimLeft = (subject, chars) => {
4020
- return trimImpl(subject, (list) => new RegExp(`^[${list}]*(.*?)`), chars);
4021
- }, trimRight = (subject, chars) => {
4022
- return trimImpl(subject, (list) => new RegExp(`(.*?)[${list}]*$`), chars);
4023
- }, trimImpl = (subject, regExper, chars) => {
4024
- const pattern = chars ? chars.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&") : "\\s…";
4025
- return subject.replace(regExper(pattern), "$1");
4026
- }, upper = (subject) => subject.toUpperCase(), basicTokens, operatorStartToken, skipChars, StreamLexer = class {
4027
- _current = 0;
4028
- _enable_legacy_literals = false;
4029
- tokenize(stream, options) {
4030
- const tokens = [];
4031
- this._current = 0;
4032
- this._enable_legacy_literals = options?.enable_legacy_literals || false;
4033
- let start;
4034
- let identifier;
4035
- let token;
4036
- while (this._current < stream.length) {
4037
- if (isAlpha(stream[this._current])) {
4038
- start = this._current;
4039
- identifier = this.consumeUnquotedIdentifier(stream);
4040
- tokens.push({
4041
- start,
4042
- type: "UnquotedIdentifier",
4043
- value: identifier
4044
- });
4045
- } else if (basicTokens[stream[this._current]] !== undefined) {
4046
- tokens.push({
4047
- start: this._current,
4048
- type: basicTokens[stream[this._current]],
4049
- value: stream[this._current]
4050
- });
4051
- this._current += 1;
4052
- } else if (stream[this._current] === "$") {
4053
- start = this._current;
4054
- if (this._current + 1 < stream.length && isAlpha(stream[this._current + 1])) {
4055
- this._current += 1;
4056
- identifier = this.consumeUnquotedIdentifier(stream);
4057
- tokens.push({
4058
- start,
4059
- type: "Variable",
4060
- value: identifier
4061
- });
4062
- } else {
4063
- tokens.push({
4064
- start,
4065
- type: "Root",
4066
- value: stream[this._current]
4067
- });
4068
- this._current += 1;
4069
- }
4070
- } else if (stream[this._current] === "-") {
4071
- if (this._current + 1 < stream.length && isNum(stream[this._current + 1])) {
4072
- const token2 = this.consumeNumber(stream);
4073
- token2 && tokens.push(token2);
4074
- } else {
4075
- const token2 = {
4076
- start: this._current,
4077
- type: "Minus",
4078
- value: "-"
4079
- };
4080
- tokens.push(token2);
4081
- this._current += 1;
4082
- }
4083
- } else if (isNum(stream[this._current])) {
4084
- token = this.consumeNumber(stream);
4085
- tokens.push(token);
4086
- } else if (stream[this._current] === "[") {
4087
- token = this.consumeLBracket(stream);
4088
- tokens.push(token);
4089
- } else if (stream[this._current] === '"') {
4090
- start = this._current;
4091
- identifier = this.consumeQuotedIdentifier(stream);
4092
- tokens.push({
4093
- start,
4094
- type: "QuotedIdentifier",
4095
- value: identifier
4096
- });
4097
- } else if (stream[this._current] === `'`) {
4098
- start = this._current;
4099
- identifier = this.consumeRawStringLiteral(stream);
4100
- tokens.push({
4101
- start,
4102
- type: "Literal",
4103
- value: identifier
4104
- });
4105
- } else if (stream[this._current] === "`") {
4106
- start = this._current;
4107
- const literal = this.consumeLiteral(stream);
4108
- tokens.push({
4109
- start,
4110
- type: "Literal",
4111
- value: literal
4112
- });
4113
- } else if (operatorStartToken[stream[this._current]] !== undefined) {
4114
- token = this.consumeOperator(stream);
4115
- token && tokens.push(token);
4116
- } else if (skipChars[stream[this._current]] !== undefined) {
4117
- this._current += 1;
3823
+ function isObject(obj) {
3824
+ if (obj !== null) {
3825
+ return Object.prototype.toString.call(obj) === "[object Object]";
4118
3826
  } else {
4119
- const error = new Error(`Syntax error: unknown character: ${stream[this._current]}`);
4120
- error.name = "LexerError";
4121
- throw error;
3827
+ return false;
4122
3828
  }
4123
3829
  }
4124
- return tokens;
4125
- }
4126
- consumeUnquotedIdentifier(stream) {
4127
- const start = this._current;
4128
- this._current += 1;
4129
- while (this._current < stream.length && isAlphaNum(stream[this._current])) {
4130
- this._current += 1;
4131
- }
4132
- return stream.slice(start, this._current);
4133
- }
4134
- consumeQuotedIdentifier(stream) {
4135
- const start = this._current;
4136
- this._current += 1;
4137
- const maxLength = stream.length;
4138
- while (stream[this._current] !== '"' && this._current < maxLength) {
4139
- let current = this._current;
4140
- if (stream[current] === "\\" && (stream[current + 1] === "\\" || stream[current + 1] === '"')) {
4141
- current += 2;
4142
- } else {
4143
- current += 1;
3830
+ function strictDeepEqual(first, second) {
3831
+ if (first === second) {
3832
+ return true;
4144
3833
  }
4145
- this._current = current;
4146
- }
4147
- this._current += 1;
4148
- const [value, ok] = this.parseJSON(stream.slice(start, this._current));
4149
- if (!ok) {
4150
- const error = new Error(`syntax: unexpected end of JSON input`);
4151
- error.name = "LexerError";
4152
- throw error;
4153
- }
4154
- return value;
4155
- }
4156
- consumeRawStringLiteral(stream) {
4157
- const start = this._current;
4158
- this._current += 1;
4159
- const maxLength = stream.length;
4160
- while (stream[this._current] !== `'` && this._current < maxLength) {
4161
- let current = this._current;
4162
- if (stream[current] === "\\" && (stream[current + 1] === "\\" || stream[current + 1] === `'`)) {
4163
- current += 2;
4164
- } else {
4165
- current += 1;
4166
- }
4167
- this._current = current;
4168
- }
4169
- this._current += 1;
4170
- const literal = stream.slice(start + 1, this._current - 1);
4171
- return replace(replace(literal, `\\\\`, `\\`), `\\'`, `'`);
4172
- }
4173
- consumeNumber(stream) {
4174
- const start = this._current;
4175
- this._current += 1;
4176
- const maxLength = stream.length;
4177
- while (isNum(stream[this._current]) && this._current < maxLength) {
4178
- this._current += 1;
4179
- }
4180
- const value = parseInt(stream.slice(start, this._current), 10);
4181
- return { start, value, type: "Number" };
4182
- }
4183
- consumeLBracket(stream) {
4184
- const start = this._current;
4185
- this._current += 1;
4186
- if (stream[this._current] === "?") {
4187
- this._current += 1;
4188
- return { start, type: "Filter", value: "[?" };
4189
- }
4190
- if (stream[this._current] === "]") {
4191
- this._current += 1;
4192
- return { start, type: "Flatten", value: "[]" };
4193
- }
4194
- return { start, type: "Lbracket", value: "[" };
4195
- }
4196
- consumeOrElse(stream, peek, token, orElse) {
4197
- const start = this._current;
4198
- this._current += 1;
4199
- if (this._current < stream.length && stream[this._current] === peek) {
4200
- this._current += 1;
4201
- return {
4202
- start,
4203
- type: orElse,
4204
- value: stream.slice(start, this._current)
4205
- };
4206
- }
4207
- return { start, type: token, value: stream[start] };
4208
- }
4209
- consumeOperator(stream) {
4210
- const start = this._current;
4211
- const startingChar = stream[start];
4212
- switch (startingChar) {
4213
- case "!":
4214
- return this.consumeOrElse(stream, "=", "Not", "NE");
4215
- case "<":
4216
- return this.consumeOrElse(stream, "=", "LT", "LTE");
4217
- case ">":
4218
- return this.consumeOrElse(stream, "=", "GT", "GTE");
4219
- case "=":
4220
- return this.consumeOrElse(stream, "=", "Assign", "EQ");
4221
- case "&":
4222
- return this.consumeOrElse(stream, "&", "Expref", "And");
4223
- case "|":
4224
- return this.consumeOrElse(stream, "|", "Pipe", "Or");
4225
- case "/":
4226
- return this.consumeOrElse(stream, "/", "Divide", "Div");
4227
- }
4228
- }
4229
- consumeLiteral(stream) {
4230
- this._current += 1;
4231
- const start = this._current;
4232
- const maxLength = stream.length;
4233
- while (stream[this._current] !== "`" && this._current < maxLength) {
4234
- let current = this._current;
4235
- if (stream[current] === "\\" && (stream[current + 1] === "\\" || stream[current + 1] === "`")) {
4236
- current += 2;
4237
- } else {
4238
- current += 1;
3834
+ var firstType = Object.prototype.toString.call(first);
3835
+ if (firstType !== Object.prototype.toString.call(second)) {
3836
+ return false;
4239
3837
  }
4240
- this._current = current;
4241
- }
4242
- let literalString = stream.slice(start, this._current).trimStart();
4243
- literalString = literalString.replace("\\`", "`");
4244
- let literal = null;
4245
- let ok = false;
4246
- if (this.looksLikeJSON(literalString)) {
4247
- [literal, ok] = this.parseJSON(literalString);
4248
- }
4249
- if (!ok && this._enable_legacy_literals) {
4250
- [literal, ok] = this.parseJSON(`"${literalString}"`);
4251
- }
4252
- if (!ok) {
4253
- const error = new Error(`Syntax error: unexpected end of JSON input or invalid format for a JSON literal: ${stream[this._current]}`);
4254
- error.name = "LexerError";
4255
- throw error;
4256
- }
4257
- this._current += 1;
4258
- return literal;
4259
- }
4260
- looksLikeJSON(literalString) {
4261
- const startingChars = '[{"';
4262
- const jsonLiterals = ["true", "false", "null"];
4263
- const numberLooking = "-0123456789";
4264
- if (literalString === "") {
4265
- return false;
4266
- }
4267
- if (startingChars.includes(literalString[0])) {
4268
- return true;
4269
- }
4270
- if (jsonLiterals.includes(literalString)) {
4271
- return true;
4272
- }
4273
- if (numberLooking.includes(literalString[0])) {
4274
- const [_, ok] = this.parseJSON(literalString);
4275
- return ok;
4276
- }
4277
- return false;
4278
- }
4279
- parseJSON(text) {
4280
- try {
4281
- const json2 = JSON.parse(text);
4282
- return [json2, true];
4283
- } catch {
4284
- return [null, false];
4285
- }
4286
- }
4287
- }, Lexer, Lexer_default, bindingPower, TokenParser = class _TokenParser {
4288
- index = 0;
4289
- tokens = [];
4290
- parse(expression, options) {
4291
- this.loadTokens(expression, options || { enable_legacy_literals: false });
4292
- this.index = 0;
4293
- const ast = this.expression(0);
4294
- if (this.lookahead(0) !== "EOF") {
4295
- const token = this.lookaheadToken(0);
4296
- this.errorToken(token, `Syntax error: unexpected token type: ${token.type}, value: ${token.value}`);
4297
- }
4298
- return ast;
4299
- }
4300
- loadTokens(expression, options) {
4301
- this.tokens = Lexer_default.tokenize(expression, options);
4302
- this.tokens.push({ type: "EOF", value: "", start: expression.length });
4303
- }
4304
- expression(rbp) {
4305
- const leftToken = this.lookaheadToken(0);
4306
- this.advance();
4307
- let left = this.nud(leftToken);
4308
- let currentTokenType = this.lookahead(0);
4309
- while (rbp < bindingPower[currentTokenType]) {
4310
- this.advance();
4311
- left = this.led(currentTokenType, left);
4312
- currentTokenType = this.lookahead(0);
4313
- }
4314
- return left;
4315
- }
4316
- lookahead(offset) {
4317
- return this.tokens[this.index + offset].type;
4318
- }
4319
- lookaheadToken(offset) {
4320
- return this.tokens[this.index + offset];
4321
- }
4322
- advance() {
4323
- this.index += 1;
4324
- }
4325
- nud(token) {
4326
- switch (token.type) {
4327
- case "Variable":
4328
- return { type: "Variable", name: token.value };
4329
- case "Literal":
4330
- return { type: "Literal", value: token.value };
4331
- case "UnquotedIdentifier": {
4332
- if (_TokenParser.isKeyword(token, "let") && this.lookahead(0) === "Variable") {
4333
- return this.parseLetExpression();
4334
- } else {
4335
- return { type: "Field", name: token.value };
3838
+ if (isArray(first) === true) {
3839
+ if (first.length !== second.length) {
3840
+ return false;
4336
3841
  }
4337
- }
4338
- case "QuotedIdentifier":
4339
- if (this.lookahead(0) === "Lparen") {
4340
- throw new Error("Syntax error: quoted identifier not allowed for function names.");
4341
- } else {
4342
- return { type: "Field", name: token.value };
3842
+ for (var i = 0;i < first.length; i++) {
3843
+ if (strictDeepEqual(first[i], second[i]) === false) {
3844
+ return false;
3845
+ }
4343
3846
  }
4344
- case "Not": {
4345
- const child = this.expression(bindingPower.Not);
4346
- return { type: "NotExpression", child };
4347
- }
4348
- case "Minus": {
4349
- const child = this.expression(bindingPower.Minus);
4350
- return {
4351
- type: "Unary",
4352
- operator: token.type,
4353
- operand: child
4354
- };
4355
- }
4356
- case "Plus": {
4357
- const child = this.expression(bindingPower.Plus);
4358
- return {
4359
- type: "Unary",
4360
- operator: token.type,
4361
- operand: child
4362
- };
3847
+ return true;
4363
3848
  }
4364
- case "Star": {
4365
- const left = { type: "Identity" };
4366
- return { type: "ValueProjection", left, right: this.parseProjectionRHS(bindingPower.Star) };
4367
- }
4368
- case "Filter":
4369
- return this.led(token.type, { type: "Identity" });
4370
- case "Lbrace":
4371
- return this.parseMultiselectHash();
4372
- case "Flatten": {
4373
- const left = {
4374
- type: "Flatten",
4375
- child: { type: "Identity" }
4376
- };
4377
- const right = this.parseProjectionRHS(bindingPower.Flatten);
4378
- return { type: "Projection", left, right };
4379
- }
4380
- case "Lbracket": {
4381
- if (this.lookahead(0) === "Number" || this.lookahead(0) === "Colon") {
4382
- const right = this.parseIndexExpression();
4383
- return this.projectIfSlice({ type: "Identity" }, right);
4384
- }
4385
- if (this.lookahead(0) === "Star" && this.lookahead(1) === "Rbracket") {
4386
- this.advance();
4387
- this.advance();
4388
- const right = this.parseProjectionRHS(bindingPower.Star);
4389
- return {
4390
- left: { type: "Identity" },
4391
- right,
4392
- type: "Projection"
4393
- };
3849
+ if (isObject(first) === true) {
3850
+ var keysSeen = {};
3851
+ for (var key in first) {
3852
+ if (hasOwnProperty.call(first, key)) {
3853
+ if (strictDeepEqual(first[key], second[key]) === false) {
3854
+ return false;
3855
+ }
3856
+ keysSeen[key] = true;
3857
+ }
4394
3858
  }
4395
- return this.parseMultiselectList();
4396
- }
4397
- case "Current":
4398
- return { type: "Current" };
4399
- case "Root":
4400
- return { type: "Root" };
4401
- case "Expref": {
4402
- const child = this.expression(bindingPower.Expref);
4403
- return { type: "ExpressionReference", child };
3859
+ for (var key2 in second) {
3860
+ if (hasOwnProperty.call(second, key2)) {
3861
+ if (keysSeen[key2] !== true) {
3862
+ return false;
3863
+ }
3864
+ }
3865
+ }
3866
+ return true;
4404
3867
  }
4405
- case "Lparen": {
4406
- const expression = this.expression(0);
4407
- this.match("Rparen");
4408
- return expression;
3868
+ return false;
3869
+ }
3870
+ function isFalse(obj) {
3871
+ if (obj === "" || obj === false || obj === null) {
3872
+ return true;
3873
+ } else if (isArray(obj) && obj.length === 0) {
3874
+ return true;
3875
+ } else if (isObject(obj)) {
3876
+ for (var key in obj) {
3877
+ if (obj.hasOwnProperty(key)) {
3878
+ return false;
3879
+ }
3880
+ }
3881
+ return true;
3882
+ } else {
3883
+ return false;
4409
3884
  }
4410
- default:
4411
- this.errorToken(token);
4412
3885
  }
4413
- }
4414
- led(tokenName, left) {
4415
- switch (tokenName) {
4416
- case "Question": {
4417
- const trueExpr = this.expression(0);
4418
- this.match("Colon");
4419
- const falseExpr = this.expression(0);
4420
- return {
4421
- type: "Ternary",
4422
- condition: left,
4423
- trueExpr,
4424
- falseExpr
4425
- };
3886
+ function objValues(obj) {
3887
+ var keys = Object.keys(obj);
3888
+ var values = [];
3889
+ for (var i = 0;i < keys.length; i++) {
3890
+ values.push(obj[keys[i]]);
4426
3891
  }
4427
- case "Dot": {
4428
- const rbp = bindingPower.Dot;
4429
- if (this.lookahead(0) !== "Star") {
4430
- const right2 = this.parseDotRHS(rbp);
4431
- return { type: "Subexpression", left, right: right2 };
4432
- }
4433
- this.advance();
4434
- const right = this.parseProjectionRHS(rbp);
4435
- return { type: "ValueProjection", left, right };
4436
- }
4437
- case "Pipe": {
4438
- const right = this.expression(bindingPower.Pipe);
4439
- return { type: "Pipe", left, right };
4440
- }
4441
- case "Or": {
4442
- const right = this.expression(bindingPower.Or);
4443
- return { type: "OrExpression", left, right };
4444
- }
4445
- case "And": {
4446
- const right = this.expression(bindingPower.And);
4447
- return { type: "AndExpression", left, right };
4448
- }
4449
- case "Lparen": {
4450
- if (left.type !== "Field") {
4451
- throw new Error("Syntax error: expected a Field node");
4452
- }
4453
- const name = left.name;
4454
- const args = this.parseCommaSeparatedExpressionsUntilToken("Rparen");
4455
- const node = { name, type: "Function", children: args };
4456
- return node;
4457
- }
4458
- case "Filter": {
4459
- const condition = this.expression(0);
4460
- this.match("Rbracket");
4461
- const right = this.lookahead(0) === "Flatten" ? { type: "Identity" } : this.parseProjectionRHS(bindingPower.Filter);
4462
- return { type: "FilterProjection", left, right, condition };
4463
- }
4464
- case "Flatten": {
4465
- const leftNode = { type: "Flatten", child: left };
4466
- const right = this.parseProjectionRHS(bindingPower.Flatten);
4467
- return { type: "Projection", left: leftNode, right };
4468
- }
4469
- case "Assign": {
4470
- const leftNode = left;
4471
- const right = this.expression(0);
4472
- return {
4473
- type: "Binding",
4474
- variable: leftNode.name,
4475
- reference: right
4476
- };
3892
+ return values;
3893
+ }
3894
+ function merge2(a, b) {
3895
+ var merged = {};
3896
+ for (var key in a) {
3897
+ merged[key] = a[key];
4477
3898
  }
4478
- case "EQ":
4479
- case "NE":
4480
- case "GT":
4481
- case "GTE":
4482
- case "LT":
4483
- case "LTE":
4484
- return this.parseComparator(left, tokenName);
4485
- case "Plus":
4486
- case "Minus":
4487
- case "Multiply":
4488
- case "Star":
4489
- case "Divide":
4490
- case "Modulo":
4491
- case "Div":
4492
- return this.parseArithmetic(left, tokenName);
4493
- case "Lbracket": {
4494
- const token = this.lookaheadToken(0);
4495
- if (token.type === "Number" || token.type === "Colon") {
4496
- const right2 = this.parseIndexExpression();
4497
- return this.projectIfSlice(left, right2);
4498
- }
4499
- this.match("Star");
4500
- this.match("Rbracket");
4501
- const right = this.parseProjectionRHS(bindingPower.Star);
4502
- return { type: "Projection", left, right };
3899
+ for (var key2 in b) {
3900
+ merged[key2] = b[key2];
4503
3901
  }
4504
- default:
4505
- return this.errorToken(this.lookaheadToken(0));
3902
+ return merged;
4506
3903
  }
4507
- }
4508
- static isKeyword(token, keyword) {
4509
- return token.type === "UnquotedIdentifier" && token.value === keyword;
4510
- }
4511
- match(tokenType) {
4512
- if (this.lookahead(0) === tokenType) {
4513
- this.advance();
4514
- return;
3904
+ var trimLeft;
3905
+ if (typeof String.prototype.trimLeft === "function") {
3906
+ trimLeft = function(str2) {
3907
+ return str2.trimLeft();
3908
+ };
4515
3909
  } else {
4516
- const token = this.lookaheadToken(0);
4517
- this.errorToken(token, `Syntax error: expected ${tokenType}, got: ${token.type}`);
4518
- }
4519
- }
4520
- errorToken(token, message = "") {
4521
- const error = new Error(message || `Syntax error: invalid token (${token.type}): "${token.value}"`);
4522
- error.name = "ParserError";
4523
- throw error;
4524
- }
4525
- parseIndexExpression() {
4526
- if (this.lookahead(0) === "Colon" || this.lookahead(1) === "Colon") {
4527
- return this.parseSliceExpression();
4528
- }
4529
- const value = Number(this.lookaheadToken(0).value);
4530
- this.advance();
4531
- this.match("Rbracket");
4532
- return { type: "Index", value };
4533
- }
4534
- projectIfSlice(left, right) {
4535
- const indexExpr = {
4536
- type: "IndexExpression",
4537
- left,
4538
- right
4539
- };
4540
- if (right.type === "Slice") {
4541
- return {
4542
- left: indexExpr,
4543
- right: this.parseProjectionRHS(bindingPower.Star),
4544
- type: "Projection"
3910
+ trimLeft = function(str2) {
3911
+ return str2.match(/^\s*(.*)/)[1];
4545
3912
  };
4546
3913
  }
4547
- return indexExpr;
4548
- }
4549
- parseSliceExpression() {
4550
- const parts = [null, null, null];
4551
- let index = 0;
4552
- let current = this.lookaheadToken(0);
4553
- while (current.type != "Rbracket" && index < 3) {
4554
- if (current.type === "Colon") {
4555
- index++;
4556
- if (index === 3) {
4557
- this.errorToken(this.lookaheadToken(0), "Syntax error, too many colons in slice expression");
4558
- }
4559
- this.advance();
4560
- } else if (current.type === "Number") {
4561
- const part = this.lookaheadToken(0).value;
4562
- parts[index] = part;
4563
- this.advance();
4564
- } else {
4565
- const next = this.lookaheadToken(0);
4566
- this.errorToken(next, `Syntax error, unexpected token: ${next.value}(${next.type})`);
4567
- }
4568
- current = this.lookaheadToken(0);
4569
- }
4570
- this.match("Rbracket");
4571
- const [start, stop, step] = parts;
4572
- return { type: "Slice", start, stop, step };
4573
- }
4574
- parseLetExpression() {
4575
- const separated = this.parseCommaSeparatedExpressionsUntilKeyword("in");
4576
- const expression = this.expression(0);
4577
- const bindings = separated.map((binding) => binding);
4578
- return {
4579
- type: "LetExpression",
4580
- bindings,
4581
- expression
3914
+ var TYPE_NUMBER = 0;
3915
+ var TYPE_ANY = 1;
3916
+ var TYPE_STRING = 2;
3917
+ var TYPE_ARRAY = 3;
3918
+ var TYPE_OBJECT = 4;
3919
+ var TYPE_BOOLEAN = 5;
3920
+ var TYPE_EXPREF = 6;
3921
+ var TYPE_NULL = 7;
3922
+ var TYPE_ARRAY_NUMBER = 8;
3923
+ var TYPE_ARRAY_STRING = 9;
3924
+ var TYPE_NAME_TABLE = {
3925
+ 0: "number",
3926
+ 1: "any",
3927
+ 2: "string",
3928
+ 3: "array",
3929
+ 4: "object",
3930
+ 5: "boolean",
3931
+ 6: "expression",
3932
+ 7: "null",
3933
+ 8: "Array<number>",
3934
+ 9: "Array<string>"
4582
3935
  };
4583
- }
4584
- parseCommaSeparatedExpressionsUntilKeyword(keyword) {
4585
- return this.parseCommaSeparatedExpressionsUntil(() => {
4586
- return _TokenParser.isKeyword(this.lookaheadToken(0), keyword);
4587
- }, () => {
4588
- this.advance();
4589
- });
4590
- }
4591
- parseCommaSeparatedExpressionsUntilToken(token) {
4592
- return this.parseCommaSeparatedExpressionsUntil(() => {
4593
- return this.lookahead(0) === token;
4594
- }, () => {
4595
- return this.match(token);
4596
- });
4597
- }
4598
- parseCommaSeparatedExpressionsUntil(isEndToken, matchEndToken) {
4599
- const args = [];
4600
- let expression;
4601
- while (!isEndToken()) {
4602
- expression = this.expression(0);
4603
- if (this.lookahead(0) === "Comma") {
4604
- this.match("Comma");
4605
- }
4606
- args.push(expression);
4607
- }
4608
- matchEndToken();
4609
- return args;
4610
- }
4611
- parseComparator(left, comparator) {
4612
- const right = this.expression(bindingPower[comparator]);
4613
- return { type: "Comparator", name: comparator, left, right };
4614
- }
4615
- parseArithmetic(left, operator) {
4616
- const right = this.expression(bindingPower[operator]);
4617
- return { type: "Arithmetic", operator, left, right };
4618
- }
4619
- parseDotRHS(rbp) {
4620
- const lookahead = this.lookahead(0);
4621
- const exprTokens = ["UnquotedIdentifier", "QuotedIdentifier", "Star"];
4622
- if (exprTokens.includes(lookahead)) {
4623
- return this.expression(rbp);
4624
- }
4625
- if (lookahead === "Lbracket") {
4626
- this.match("Lbracket");
4627
- return this.parseMultiselectList();
4628
- }
4629
- if (lookahead === "Lbrace") {
4630
- this.match("Lbrace");
4631
- return this.parseMultiselectHash();
4632
- }
4633
- const token = this.lookaheadToken(0);
4634
- this.errorToken(token, `Syntax error, unexpected token: ${token.value}(${token.type})`);
4635
- }
4636
- parseProjectionRHS(rbp) {
4637
- if (bindingPower[this.lookahead(0)] < 10) {
4638
- return { type: "Identity" };
4639
- }
4640
- if (this.lookahead(0) === "Lbracket") {
4641
- return this.expression(rbp);
4642
- }
4643
- if (this.lookahead(0) === "Filter") {
4644
- return this.expression(rbp);
4645
- }
4646
- if (this.lookahead(0) === "Dot") {
4647
- this.match("Dot");
4648
- return this.parseDotRHS(rbp);
4649
- }
4650
- const token = this.lookaheadToken(0);
4651
- this.errorToken(token, `Syntax error, unexpected token: ${token.value}(${token.type})`);
4652
- }
4653
- parseMultiselectList() {
4654
- const expressions = [];
4655
- while (this.lookahead(0) !== "Rbracket") {
4656
- const expression = this.expression(0);
4657
- expressions.push(expression);
4658
- if (this.lookahead(0) === "Comma") {
4659
- this.match("Comma");
4660
- if (this.lookahead(0) === "Rbracket") {
4661
- throw new Error("Syntax error: unexpected token Rbracket");
4662
- }
4663
- }
4664
- }
4665
- this.match("Rbracket");
4666
- return { type: "MultiSelectList", children: expressions };
4667
- }
4668
- parseMultiselectHash() {
4669
- const pairs2 = [];
4670
- const identifierTypes = ["UnquotedIdentifier", "QuotedIdentifier"];
4671
- let keyToken;
4672
- let keyName;
4673
- let value;
4674
- for (;; ) {
4675
- keyToken = this.lookaheadToken(0);
4676
- if (!identifierTypes.includes(keyToken.type)) {
4677
- throw new Error(`Syntax error: expecting an identifier token, got: ${keyToken.type}`);
4678
- }
4679
- keyName = keyToken.value;
4680
- this.advance();
4681
- this.match("Colon");
4682
- value = this.expression(0);
4683
- pairs2.push({ value, type: "KeyValuePair", name: keyName });
4684
- if (this.lookahead(0) === "Comma") {
4685
- this.match("Comma");
4686
- } else if (this.lookahead(0) === "Rbrace") {
4687
- this.match("Rbrace");
4688
- break;
4689
- }
4690
- }
4691
- return { type: "MultiSelectHash", children: pairs2 };
4692
- }
4693
- }, Parser, Parser_default, Text = class _Text {
4694
- _text;
4695
- constructor(text) {
4696
- this._text = text;
4697
- }
4698
- get string() {
4699
- return this._text;
4700
- }
4701
- get length() {
4702
- return this.codePoints.length;
4703
- }
4704
- compareTo(other) {
4705
- return _Text.compare(this, new _Text(other));
4706
- }
4707
- static get comparer() {
4708
- const stringComparer = (left, right) => {
4709
- return new _Text(left).compareTo(right);
3936
+ var TOK_EOF = "EOF";
3937
+ var TOK_UNQUOTEDIDENTIFIER = "UnquotedIdentifier";
3938
+ var TOK_QUOTEDIDENTIFIER = "QuotedIdentifier";
3939
+ var TOK_RBRACKET = "Rbracket";
3940
+ var TOK_RPAREN = "Rparen";
3941
+ var TOK_COMMA = "Comma";
3942
+ var TOK_COLON = "Colon";
3943
+ var TOK_RBRACE = "Rbrace";
3944
+ var TOK_NUMBER = "Number";
3945
+ var TOK_CURRENT = "Current";
3946
+ var TOK_EXPREF = "Expref";
3947
+ var TOK_PIPE = "Pipe";
3948
+ var TOK_OR = "Or";
3949
+ var TOK_AND = "And";
3950
+ var TOK_EQ = "EQ";
3951
+ var TOK_GT = "GT";
3952
+ var TOK_LT = "LT";
3953
+ var TOK_GTE = "GTE";
3954
+ var TOK_LTE = "LTE";
3955
+ var TOK_NE = "NE";
3956
+ var TOK_FLATTEN = "Flatten";
3957
+ var TOK_STAR = "Star";
3958
+ var TOK_FILTER = "Filter";
3959
+ var TOK_DOT = "Dot";
3960
+ var TOK_NOT = "Not";
3961
+ var TOK_LBRACE = "Lbrace";
3962
+ var TOK_LBRACKET = "Lbracket";
3963
+ var TOK_LPAREN = "Lparen";
3964
+ var TOK_LITERAL = "Literal";
3965
+ var basicTokens = {
3966
+ ".": TOK_DOT,
3967
+ "*": TOK_STAR,
3968
+ ",": TOK_COMMA,
3969
+ ":": TOK_COLON,
3970
+ "{": TOK_LBRACE,
3971
+ "}": TOK_RBRACE,
3972
+ "]": TOK_RBRACKET,
3973
+ "(": TOK_LPAREN,
3974
+ ")": TOK_RPAREN,
3975
+ "@": TOK_CURRENT
4710
3976
  };
4711
- return stringComparer;
4712
- }
4713
- static compare(left, right) {
4714
- const leftCp = left.codePoints;
4715
- const rightCp = right.codePoints;
4716
- for (let index = 0;index < Math.min(leftCp.length, rightCp.length); index++) {
4717
- if (leftCp[index] === rightCp[index]) {
4718
- continue;
4719
- }
4720
- return leftCp[index] - rightCp[index] > 0 ? 1 : -1;
4721
- }
4722
- return leftCp.length - rightCp.length > 0 ? 1 : -1;
4723
- }
4724
- reverse() {
4725
- return String.fromCodePoint(...this.codePoints.reverse());
4726
- }
4727
- get codePoints() {
4728
- const array = [...this._text].map((s) => s.codePointAt(0));
4729
- return array;
4730
- }
4731
- }, createMathFunction = (mathFn) => ([value]) => mathFn(value), createStringFunction = (stringFn) => ([subject]) => stringFn(subject), createObjectFunction = (objFn) => ([obj]) => objFn(obj), Runtime = class {
4732
- _interpreter;
4733
- _functionTable;
4734
- _customFunctions = /* @__PURE__ */ new Set;
4735
- TYPE_NAME_TABLE = Object.freeze({
4736
- [0]: "number",
4737
- [1]: "any",
4738
- [2]: "string",
4739
- [3]: "array",
4740
- [4]: "object",
4741
- [5]: "boolean",
4742
- [6]: "expression",
4743
- [7]: "null",
4744
- [8]: "Array<number>",
4745
- [10]: "Array<object>",
4746
- [9]: "Array<string>",
4747
- [11]: "Array<Array<any>>"
4748
- });
4749
- constructor(interpreter) {
4750
- this._interpreter = interpreter;
4751
- this._functionTable = this.buildFunctionTable();
4752
- }
4753
- buildFunctionTable() {
4754
- return {
4755
- abs: { _func: createMathFunction(Math.abs), _signature: [{ types: [0] }] },
4756
- ceil: { _func: createMathFunction(Math.ceil), _signature: [{ types: [0] }] },
4757
- floor: { _func: createMathFunction(Math.floor), _signature: [{ types: [0] }] },
4758
- lower: { _func: createStringFunction(lower), _signature: [{ types: [2] }] },
4759
- upper: { _func: createStringFunction(upper), _signature: [{ types: [2] }] },
4760
- keys: { _func: createObjectFunction(Object.keys), _signature: [{ types: [4] }] },
4761
- values: { _func: createObjectFunction(Object.values), _signature: [{ types: [4] }] },
4762
- avg: { _func: this.functionAvg, _signature: [{ types: [8] }] },
4763
- contains: {
4764
- _func: this.functionContains,
4765
- _signature: [
4766
- { types: [2, 3] },
4767
- { types: [1] }
4768
- ]
4769
- },
4770
- ends_with: {
4771
- _func: this.functionEndsWith,
4772
- _signature: [{ types: [2] }, { types: [2] }]
4773
- },
4774
- find_first: {
4775
- _func: this.functionFindFirst,
4776
- _signature: [
4777
- { types: [2] },
4778
- { types: [2] },
4779
- { types: [0], optional: true },
4780
- { types: [0], optional: true }
4781
- ]
4782
- },
4783
- find_last: {
4784
- _func: this.functionFindLast,
4785
- _signature: [
4786
- { types: [2] },
4787
- { types: [2] },
4788
- { types: [0], optional: true },
4789
- { types: [0], optional: true }
4790
- ]
4791
- },
4792
- from_items: { _func: this.functionFromItems, _signature: [{ types: [11] }] },
4793
- group_by: {
4794
- _func: this.functionGroupBy,
4795
- _signature: [{ types: [3] }, { types: [6] }]
4796
- },
4797
- items: { _func: this.functionItems, _signature: [{ types: [4] }] },
4798
- join: {
4799
- _func: this.functionJoin,
4800
- _signature: [{ types: [2] }, { types: [9] }]
4801
- },
4802
- length: {
4803
- _func: this.functionLength,
4804
- _signature: [{ types: [2, 3, 4] }]
4805
- },
4806
- map: {
4807
- _func: this.functionMap,
4808
- _signature: [{ types: [6] }, { types: [3] }]
4809
- },
4810
- max: {
4811
- _func: this.functionMax,
4812
- _signature: [{ types: [8, 9] }]
4813
- },
4814
- max_by: {
4815
- _func: this.functionMaxBy,
4816
- _signature: [{ types: [3] }, { types: [6] }]
4817
- },
4818
- merge: { _func: this.functionMerge, _signature: [{ types: [4], variadic: true }] },
4819
- min: {
4820
- _func: this.functionMin,
4821
- _signature: [{ types: [8, 9] }]
4822
- },
4823
- min_by: {
4824
- _func: this.functionMinBy,
4825
- _signature: [{ types: [3] }, { types: [6] }]
4826
- },
4827
- not_null: { _func: this.functionNotNull, _signature: [{ types: [1], variadic: true }] },
4828
- pad_left: {
4829
- _func: this.functionPadLeft,
4830
- _signature: [
4831
- { types: [2] },
4832
- { types: [0] },
4833
- { types: [2], optional: true }
4834
- ]
4835
- },
4836
- pad_right: {
4837
- _func: this.functionPadRight,
4838
- _signature: [
4839
- { types: [2] },
4840
- { types: [0] },
4841
- { types: [2], optional: true }
4842
- ]
4843
- },
4844
- replace: {
4845
- _func: this.functionReplace,
4846
- _signature: [
4847
- { types: [2] },
4848
- { types: [2] },
4849
- { types: [2] },
4850
- { types: [0], optional: true }
4851
- ]
4852
- },
4853
- reverse: {
4854
- _func: this.functionReverse,
4855
- _signature: [{ types: [2, 3] }]
3977
+ var operatorStartToken = {
3978
+ "<": true,
3979
+ ">": true,
3980
+ "=": true,
3981
+ "!": true
3982
+ };
3983
+ var skipChars = {
3984
+ " ": true,
3985
+ "\t": true,
3986
+ "\n": true
3987
+ };
3988
+ function isAlpha(ch) {
3989
+ return ch >= "a" && ch <= "z" || ch >= "A" && ch <= "Z" || ch === "_";
3990
+ }
3991
+ function isNum(ch) {
3992
+ return ch >= "0" && ch <= "9" || ch === "-";
3993
+ }
3994
+ function isAlphaNum(ch) {
3995
+ return ch >= "a" && ch <= "z" || ch >= "A" && ch <= "Z" || ch >= "0" && ch <= "9" || ch === "_";
3996
+ }
3997
+ function Lexer() {}
3998
+ Lexer.prototype = {
3999
+ tokenize: function(stream) {
4000
+ var tokens = [];
4001
+ this._current = 0;
4002
+ var start;
4003
+ var identifier;
4004
+ var token;
4005
+ while (this._current < stream.length) {
4006
+ if (isAlpha(stream[this._current])) {
4007
+ start = this._current;
4008
+ identifier = this._consumeUnquotedIdentifier(stream);
4009
+ tokens.push({
4010
+ type: TOK_UNQUOTEDIDENTIFIER,
4011
+ value: identifier,
4012
+ start
4013
+ });
4014
+ } else if (basicTokens[stream[this._current]] !== undefined) {
4015
+ tokens.push({
4016
+ type: basicTokens[stream[this._current]],
4017
+ value: stream[this._current],
4018
+ start: this._current
4019
+ });
4020
+ this._current++;
4021
+ } else if (isNum(stream[this._current])) {
4022
+ token = this._consumeNumber(stream);
4023
+ tokens.push(token);
4024
+ } else if (stream[this._current] === "[") {
4025
+ token = this._consumeLBracket(stream);
4026
+ tokens.push(token);
4027
+ } else if (stream[this._current] === '"') {
4028
+ start = this._current;
4029
+ identifier = this._consumeQuotedIdentifier(stream);
4030
+ tokens.push({
4031
+ type: TOK_QUOTEDIDENTIFIER,
4032
+ value: identifier,
4033
+ start
4034
+ });
4035
+ } else if (stream[this._current] === "'") {
4036
+ start = this._current;
4037
+ identifier = this._consumeRawStringLiteral(stream);
4038
+ tokens.push({
4039
+ type: TOK_LITERAL,
4040
+ value: identifier,
4041
+ start
4042
+ });
4043
+ } else if (stream[this._current] === "`") {
4044
+ start = this._current;
4045
+ var literal = this._consumeLiteral(stream);
4046
+ tokens.push({
4047
+ type: TOK_LITERAL,
4048
+ value: literal,
4049
+ start
4050
+ });
4051
+ } else if (operatorStartToken[stream[this._current]] !== undefined) {
4052
+ tokens.push(this._consumeOperator(stream));
4053
+ } else if (skipChars[stream[this._current]] !== undefined) {
4054
+ this._current++;
4055
+ } else if (stream[this._current] === "&") {
4056
+ start = this._current;
4057
+ this._current++;
4058
+ if (stream[this._current] === "&") {
4059
+ this._current++;
4060
+ tokens.push({ type: TOK_AND, value: "&&", start });
4061
+ } else {
4062
+ tokens.push({ type: TOK_EXPREF, value: "&", start });
4063
+ }
4064
+ } else if (stream[this._current] === "|") {
4065
+ start = this._current;
4066
+ this._current++;
4067
+ if (stream[this._current] === "|") {
4068
+ this._current++;
4069
+ tokens.push({ type: TOK_OR, value: "||", start });
4070
+ } else {
4071
+ tokens.push({ type: TOK_PIPE, value: "|", start });
4072
+ }
4073
+ } else {
4074
+ var error = new Error("Unknown character:" + stream[this._current]);
4075
+ error.name = "LexerError";
4076
+ throw error;
4077
+ }
4078
+ }
4079
+ return tokens;
4856
4080
  },
4857
- sort: {
4858
- _func: this.functionSort,
4859
- _signature: [{ types: [9, 8] }]
4081
+ _consumeUnquotedIdentifier: function(stream) {
4082
+ var start = this._current;
4083
+ this._current++;
4084
+ while (this._current < stream.length && isAlphaNum(stream[this._current])) {
4085
+ this._current++;
4086
+ }
4087
+ return stream.slice(start, this._current);
4860
4088
  },
4861
- sort_by: {
4862
- _func: this.functionSortBy,
4863
- _signature: [{ types: [3] }, { types: [6] }]
4089
+ _consumeQuotedIdentifier: function(stream) {
4090
+ var start = this._current;
4091
+ this._current++;
4092
+ var maxLength = stream.length;
4093
+ while (stream[this._current] !== '"' && this._current < maxLength) {
4094
+ var current = this._current;
4095
+ if (stream[current] === "\\" && (stream[current + 1] === "\\" || stream[current + 1] === '"')) {
4096
+ current += 2;
4097
+ } else {
4098
+ current++;
4099
+ }
4100
+ this._current = current;
4101
+ }
4102
+ this._current++;
4103
+ return JSON.parse(stream.slice(start, this._current));
4864
4104
  },
4865
- split: {
4866
- _func: this.functionSplit,
4867
- _signature: [
4868
- { types: [2] },
4869
- { types: [2] },
4870
- { types: [0], optional: true }
4871
- ]
4105
+ _consumeRawStringLiteral: function(stream) {
4106
+ var start = this._current;
4107
+ this._current++;
4108
+ var maxLength = stream.length;
4109
+ while (stream[this._current] !== "'" && this._current < maxLength) {
4110
+ var current = this._current;
4111
+ if (stream[current] === "\\" && (stream[current + 1] === "\\" || stream[current + 1] === "'")) {
4112
+ current += 2;
4113
+ } else {
4114
+ current++;
4115
+ }
4116
+ this._current = current;
4117
+ }
4118
+ this._current++;
4119
+ var literal = stream.slice(start + 1, this._current - 1);
4120
+ return literal.replace("\\'", "'");
4872
4121
  },
4873
- starts_with: {
4874
- _func: this.functionStartsWith,
4875
- _signature: [{ types: [2] }, { types: [2] }]
4122
+ _consumeNumber: function(stream) {
4123
+ var start = this._current;
4124
+ this._current++;
4125
+ var maxLength = stream.length;
4126
+ while (isNum(stream[this._current]) && this._current < maxLength) {
4127
+ this._current++;
4128
+ }
4129
+ var value = parseInt(stream.slice(start, this._current));
4130
+ return { type: TOK_NUMBER, value, start };
4876
4131
  },
4877
- sum: { _func: this.functionSum, _signature: [{ types: [8] }] },
4878
- to_array: { _func: this.functionToArray, _signature: [{ types: [1] }] },
4879
- to_number: { _func: this.functionToNumber, _signature: [{ types: [1] }] },
4880
- to_string: { _func: this.functionToString, _signature: [{ types: [1] }] },
4881
- trim: {
4882
- _func: this.functionTrim,
4883
- _signature: [{ types: [2] }, { types: [2], optional: true }]
4132
+ _consumeLBracket: function(stream) {
4133
+ var start = this._current;
4134
+ this._current++;
4135
+ if (stream[this._current] === "?") {
4136
+ this._current++;
4137
+ return { type: TOK_FILTER, value: "[?", start };
4138
+ } else if (stream[this._current] === "]") {
4139
+ this._current++;
4140
+ return { type: TOK_FLATTEN, value: "[]", start };
4141
+ } else {
4142
+ return { type: TOK_LBRACKET, value: "[", start };
4143
+ }
4884
4144
  },
4885
- trim_left: {
4886
- _func: this.functionTrimLeft,
4887
- _signature: [{ types: [2] }, { types: [2], optional: true }]
4145
+ _consumeOperator: function(stream) {
4146
+ var start = this._current;
4147
+ var startingChar = stream[start];
4148
+ this._current++;
4149
+ if (startingChar === "!") {
4150
+ if (stream[this._current] === "=") {
4151
+ this._current++;
4152
+ return { type: TOK_NE, value: "!=", start };
4153
+ } else {
4154
+ return { type: TOK_NOT, value: "!", start };
4155
+ }
4156
+ } else if (startingChar === "<") {
4157
+ if (stream[this._current] === "=") {
4158
+ this._current++;
4159
+ return { type: TOK_LTE, value: "<=", start };
4160
+ } else {
4161
+ return { type: TOK_LT, value: "<", start };
4162
+ }
4163
+ } else if (startingChar === ">") {
4164
+ if (stream[this._current] === "=") {
4165
+ this._current++;
4166
+ return { type: TOK_GTE, value: ">=", start };
4167
+ } else {
4168
+ return { type: TOK_GT, value: ">", start };
4169
+ }
4170
+ } else if (startingChar === "=") {
4171
+ if (stream[this._current] === "=") {
4172
+ this._current++;
4173
+ return { type: TOK_EQ, value: "==", start };
4174
+ }
4175
+ }
4888
4176
  },
4889
- trim_right: {
4890
- _func: this.functionTrimRight,
4891
- _signature: [{ types: [2] }, { types: [2], optional: true }]
4177
+ _consumeLiteral: function(stream) {
4178
+ this._current++;
4179
+ var start = this._current;
4180
+ var maxLength = stream.length;
4181
+ var literal;
4182
+ while (stream[this._current] !== "`" && this._current < maxLength) {
4183
+ var current = this._current;
4184
+ if (stream[current] === "\\" && (stream[current + 1] === "\\" || stream[current + 1] === "`")) {
4185
+ current += 2;
4186
+ } else {
4187
+ current++;
4188
+ }
4189
+ this._current = current;
4190
+ }
4191
+ var literalString = trimLeft(stream.slice(start, this._current));
4192
+ literalString = literalString.replace("\\`", "`");
4193
+ if (this._looksLikeJSON(literalString)) {
4194
+ literal = JSON.parse(literalString);
4195
+ } else {
4196
+ literal = JSON.parse('"' + literalString + '"');
4197
+ }
4198
+ this._current++;
4199
+ return literal;
4892
4200
  },
4893
- type: { _func: this.functionType, _signature: [{ types: [1] }] },
4894
- zip: { _func: this.functionZip, _signature: [{ types: [3], variadic: true }] }
4895
- };
4896
- }
4897
- registerFunction(name, customFunction, signature, options) {
4898
- const result = this._registerInternal(name, customFunction, signature, options);
4899
- if (!result.success) {
4900
- throw new Error(result.message);
4901
- }
4902
- }
4903
- _registerInternal(name, customFunction, signature, options = {}) {
4904
- if (!name || typeof name !== "string" || name.trim() === "") {
4905
- return {
4906
- success: false,
4907
- reason: "invalid-name",
4908
- message: "Function name must be a non-empty string"
4909
- };
4910
- }
4911
- try {
4912
- this.validateInputSignatures(name, signature);
4913
- } catch (error) {
4914
- return {
4915
- success: false,
4916
- reason: "invalid-signature",
4917
- message: error instanceof Error ? error.message : "Invalid function signature"
4918
- };
4919
- }
4920
- const { override = false, warn = false } = options;
4921
- const exists = name in this._functionTable;
4922
- if (exists && !override) {
4923
- return {
4924
- success: false,
4925
- reason: "already-exists",
4926
- message: `Function already defined: ${name}(). Use { override: true } to replace it.`
4927
- };
4928
- }
4929
- if (exists && override && warn) {
4930
- console.warn(`Warning: Overriding existing function: ${name}()`);
4931
- }
4932
- this._functionTable[name] = {
4933
- _func: customFunction.bind(this),
4934
- _signature: signature
4935
- };
4936
- this._customFunctions.add(name);
4937
- const message = exists ? `Function ${name}() overridden successfully` : `Function ${name}() registered successfully`;
4938
- return { success: true, message };
4939
- }
4940
- register(name, customFunction, signature, options = {}) {
4941
- return this._registerInternal(name, customFunction, signature, options);
4942
- }
4943
- unregister(name) {
4944
- if (!this._customFunctions.has(name)) {
4945
- return false;
4946
- }
4947
- delete this._functionTable[name];
4948
- this._customFunctions.delete(name);
4949
- return true;
4950
- }
4951
- isRegistered(name) {
4952
- return name in this._functionTable;
4953
- }
4954
- getRegistered() {
4955
- return Object.keys(this._functionTable);
4956
- }
4957
- getCustomFunctions() {
4958
- return Array.from(this._customFunctions);
4959
- }
4960
- clearCustomFunctions() {
4961
- for (const name of this._customFunctions) {
4962
- delete this._functionTable[name];
4963
- }
4964
- this._customFunctions.clear();
4965
- }
4966
- callFunction(name, resolvedArgs) {
4967
- const functionEntry = this._functionTable[name];
4968
- if (functionEntry === undefined) {
4969
- throw new Error(`Unknown function: ${name}()`);
4970
- }
4971
- this.validateArgs(name, resolvedArgs, functionEntry._signature);
4972
- return functionEntry._func.call(this, resolvedArgs);
4973
- }
4974
- validateInputSignatures(name, signature) {
4975
- for (let i = 0;i < signature.length; i += 1) {
4976
- if ("variadic" in signature[i] && i !== signature.length - 1) {
4977
- throw new Error(`Invalid arity: ${name}() 'variadic' argument ${i + 1} must occur last`);
4978
- }
4979
- }
4980
- }
4981
- validateArgs(name, args, signature) {
4982
- this.validateInputSignatures(name, signature);
4983
- this.validateArity(name, args, signature);
4984
- this.validateTypes(name, args, signature);
4985
- }
4986
- validateArity(name, args, signature) {
4987
- const numberOfRequiredArgs = signature.filter((argSignature) => !(argSignature.optional ?? false)).length;
4988
- const lastArgIsVariadic = signature[signature.length - 1]?.variadic ?? false;
4989
- const tooFewArgs = args.length < numberOfRequiredArgs;
4990
- const tooManyArgs = args.length > signature.length;
4991
- if (lastArgIsVariadic && tooFewArgs || !lastArgIsVariadic && (tooFewArgs || tooManyArgs)) {
4992
- const tooFewModifier = tooFewArgs && (!lastArgIsVariadic && numberOfRequiredArgs > 1 || lastArgIsVariadic) ? "at least " : "";
4993
- const pluralized = signature.length > 1;
4994
- throw new Error(`Invalid arity: ${name}() takes ${tooFewModifier}${numberOfRequiredArgs} argument${pluralized && "s" || ""} but received ${args.length}`);
4995
- }
4996
- }
4997
- validateTypes(name, args, signature) {
4998
- for (let i = 0;i < signature.length; i += 1) {
4999
- const currentSpec = signature[i].types;
5000
- const actualType = this.getTypeName(args[i]);
5001
- if (actualType === undefined) {
5002
- continue;
5003
- }
5004
- const typeMatched = currentSpec.some((expectedType) => this.typeMatches(actualType, expectedType, args[i]));
5005
- if (!typeMatched) {
5006
- const expected = currentSpec.map((typeId) => this.TYPE_NAME_TABLE[typeId]).join(" | ");
5007
- throw new Error(`Invalid type: ${name}() expected argument ${i + 1} to be type (${expected}) but received type ${this.TYPE_NAME_TABLE[actualType]} instead.`);
5008
- }
5009
- }
5010
- }
5011
- typeMatches(actual, expected, argValue) {
5012
- if (expected === 1) {
5013
- return true;
5014
- }
5015
- if (expected === 9 || expected === 8 || expected === 10 || expected === 11 || expected === 3) {
5016
- if (expected === 3) {
5017
- return actual === 3;
5018
- }
5019
- if (actual === 3) {
5020
- let subtype;
5021
- if (expected === 8) {
5022
- subtype = 0;
5023
- } else if (expected === 10) {
5024
- subtype = 4;
5025
- } else if (expected === 9) {
5026
- subtype = 2;
5027
- } else if (expected === 11) {
5028
- subtype = 3;
5029
- }
5030
- const array = argValue;
5031
- for (let i = 0;i < array.length; i += 1) {
5032
- const typeName = this.getTypeName(array[i]);
5033
- if (typeName !== undefined && subtype !== undefined && !this.typeMatches(typeName, subtype, array[i])) {
4201
+ _looksLikeJSON: function(literalString) {
4202
+ var startingChars = '[{"';
4203
+ var jsonLiterals = ["true", "false", "null"];
4204
+ var numberLooking = "-0123456789";
4205
+ if (literalString === "") {
4206
+ return false;
4207
+ } else if (startingChars.indexOf(literalString[0]) >= 0) {
4208
+ return true;
4209
+ } else if (jsonLiterals.indexOf(literalString) >= 0) {
4210
+ return true;
4211
+ } else if (numberLooking.indexOf(literalString[0]) >= 0) {
4212
+ try {
4213
+ JSON.parse(literalString);
4214
+ return true;
4215
+ } catch (ex) {
5034
4216
  return false;
5035
4217
  }
5036
- }
5037
- return true;
5038
- }
5039
- } else {
5040
- return actual === expected;
5041
- }
5042
- return false;
5043
- }
5044
- getTypeName(obj) {
5045
- if (obj === null) {
5046
- return 7;
5047
- }
5048
- if (typeof obj === "string") {
5049
- return 2;
5050
- }
5051
- if (typeof obj === "number") {
5052
- return 0;
5053
- }
5054
- if (typeof obj === "boolean") {
5055
- return 5;
5056
- }
5057
- if (Array.isArray(obj)) {
5058
- return 3;
5059
- }
5060
- if (typeof obj === "object") {
5061
- if (obj.expref) {
5062
- return 6;
5063
- }
5064
- return 4;
5065
- }
5066
- return;
5067
- }
5068
- createKeyFunction(exprefNode, allowedTypes) {
5069
- const interpreter = this._interpreter;
5070
- const keyFunc = (x) => {
5071
- const current = interpreter.visit(exprefNode, x);
5072
- if (!allowedTypes.includes(this.getTypeName(current))) {
5073
- const msg = `Invalid type: expected one of (${allowedTypes.map((t) => this.TYPE_NAME_TABLE[t]).join(" | ")}), received ${this.TYPE_NAME_TABLE[this.getTypeName(current)]}`;
5074
- throw new Error(msg);
5075
- }
5076
- return current;
5077
- };
5078
- return keyFunc;
5079
- }
5080
- functionAvg = ([inputArray]) => {
5081
- if (!inputArray || inputArray.length == 0) {
5082
- return null;
5083
- }
5084
- let sum = 0;
5085
- for (let i = 0;i < inputArray.length; i += 1) {
5086
- sum += inputArray[i];
5087
- }
5088
- return sum / inputArray.length;
5089
- };
5090
- functionContains = ([
5091
- searchable,
5092
- searchValue
5093
- ]) => {
5094
- if (Array.isArray(searchable)) {
5095
- const array = searchable;
5096
- return array.includes(searchValue);
5097
- }
5098
- if (typeof searchable === "string") {
5099
- const text = searchable;
5100
- if (typeof searchValue === "string") {
5101
- return text.includes(searchValue);
5102
- }
5103
- }
5104
- return null;
5105
- };
5106
- functionEndsWith = (resolvedArgs) => {
5107
- const [searchStr, suffix] = resolvedArgs;
5108
- return searchStr.includes(suffix, searchStr.length - suffix.length);
5109
- };
5110
- functionFindFirst = this.createFindFunction(findFirst);
5111
- functionFindLast = this.createFindFunction(findLast);
5112
- createFindFunction(findFn) {
5113
- return (resolvedArgs) => {
5114
- const subject = resolvedArgs[0];
5115
- const search2 = resolvedArgs[1];
5116
- const start = resolvedArgs.length > 2 ? resolvedArgs[2] : undefined;
5117
- const end = resolvedArgs.length > 3 ? resolvedArgs[3] : undefined;
5118
- return findFn(subject, search2, start, end);
5119
- };
5120
- }
5121
- functionFromItems = ([array]) => {
5122
- array.map((pair) => {
5123
- if (pair.length != 2 || typeof pair[0] !== "string") {
5124
- throw new Error("invalid value, each array must contain two elements, a pair of string and value");
5125
- }
5126
- });
5127
- return Object.fromEntries(array);
5128
- };
5129
- functionGroupBy = ([array, exprefNode]) => {
5130
- const keyFunction = this.createKeyFunction(exprefNode, [2]);
5131
- return array.reduce((acc, cur) => {
5132
- const k = keyFunction(cur ?? {});
5133
- const target = acc[k] = acc[k] || [];
5134
- target.push(cur);
5135
- return acc;
5136
- }, {});
5137
- };
5138
- functionItems = ([inputValue]) => {
5139
- return Object.entries(inputValue);
5140
- };
5141
- functionJoin = (resolvedArgs) => {
5142
- const [joinChar, listJoin] = resolvedArgs;
5143
- return listJoin.join(joinChar);
5144
- };
5145
- functionLength = ([inputValue]) => {
5146
- if (typeof inputValue === "string") {
5147
- return new Text(inputValue).length;
5148
- }
5149
- if (Array.isArray(inputValue)) {
5150
- return inputValue.length;
5151
- }
5152
- return Object.keys(inputValue).length;
5153
- };
5154
- functionMap = ([exprefNode, elements]) => {
5155
- if (!this._interpreter) {
5156
- return [];
5157
- }
5158
- const mapped = [];
5159
- const interpreter = this._interpreter;
5160
- for (let i = 0;i < elements.length; i += 1) {
5161
- mapped.push(interpreter.visit(exprefNode, elements[i]));
5162
- }
5163
- return mapped;
5164
- };
5165
- functionMax = ([inputValue]) => {
5166
- if (!inputValue.length) {
5167
- return null;
5168
- }
5169
- const typeName = this.getTypeName(inputValue[0]);
5170
- if (typeName === 0) {
5171
- return Math.max(...inputValue);
5172
- }
5173
- const elements = inputValue;
5174
- let maxElement = elements[0];
5175
- for (let i = 1;i < elements.length; i += 1) {
5176
- if (maxElement.localeCompare(elements[i]) < 0) {
5177
- maxElement = elements[i];
5178
- }
5179
- }
5180
- return maxElement;
5181
- };
5182
- functionMaxBy = (resolvedArgs) => {
5183
- const exprefNode = resolvedArgs[1];
5184
- const resolvedArray = resolvedArgs[0];
5185
- const keyFunction = this.createKeyFunction(exprefNode, [0, 2]);
5186
- let maxNumber = -Infinity;
5187
- let maxRecord;
5188
- let current;
5189
- for (let i = 0;i < resolvedArray.length; i += 1) {
5190
- current = keyFunction && keyFunction(resolvedArray[i]);
5191
- if (current !== undefined && current > maxNumber) {
5192
- maxNumber = current;
5193
- maxRecord = resolvedArray[i];
5194
- }
5195
- }
5196
- return maxRecord || null;
5197
- };
5198
- functionMerge = (resolvedArgs) => {
5199
- let merged = {};
5200
- for (let i = 0;i < resolvedArgs.length; i += 1) {
5201
- const current = resolvedArgs[i];
5202
- merged = Object.assign(merged, current);
5203
- }
5204
- return merged;
5205
- };
5206
- functionMin = ([inputValue]) => {
5207
- if (!inputValue.length) {
5208
- return null;
5209
- }
5210
- const typeName = this.getTypeName(inputValue[0]);
5211
- if (typeName === 0) {
5212
- return Math.min(...inputValue);
5213
- }
5214
- const elements = inputValue;
5215
- let minElement = elements[0];
5216
- for (let i = 1;i < elements.length; i += 1) {
5217
- if (elements[i].localeCompare(minElement) < 0) {
5218
- minElement = elements[i];
5219
- }
5220
- }
5221
- return minElement;
5222
- };
5223
- functionMinBy = (resolvedArgs) => {
5224
- const exprefNode = resolvedArgs[1];
5225
- const resolvedArray = resolvedArgs[0];
5226
- const keyFunction = this.createKeyFunction(exprefNode, [0, 2]);
5227
- let minNumber = Infinity;
5228
- let minRecord;
5229
- let current;
5230
- for (let i = 0;i < resolvedArray.length; i += 1) {
5231
- current = keyFunction && keyFunction(resolvedArray[i]);
5232
- if (current !== undefined && current < minNumber) {
5233
- minNumber = current;
5234
- minRecord = resolvedArray[i];
5235
- }
5236
- }
5237
- return minRecord || null;
5238
- };
5239
- functionNotNull = (resolvedArgs) => {
5240
- for (let i = 0;i < resolvedArgs.length; i += 1) {
5241
- if (this.getTypeName(resolvedArgs[i]) !== 7) {
5242
- return resolvedArgs[i];
5243
- }
5244
- }
5245
- return null;
5246
- };
5247
- functionPadLeft = this.createPadFunction(padLeft);
5248
- functionPadRight = this.createPadFunction(padRight);
5249
- createPadFunction(padFn) {
5250
- return (resolvedArgs) => {
5251
- const subject = resolvedArgs[0];
5252
- const width = resolvedArgs[1];
5253
- const padding = resolvedArgs.length > 2 ? resolvedArgs[2] : undefined;
5254
- return padFn(subject, width, padding);
5255
- };
5256
- }
5257
- functionReplace = (resolvedArgs) => {
5258
- const subject = resolvedArgs[0];
5259
- const string = resolvedArgs[1];
5260
- const by = resolvedArgs[2];
5261
- return replace(subject, string, by, resolvedArgs.length > 3 ? resolvedArgs[3] : undefined);
5262
- };
5263
- functionSplit = (resolvedArgs) => {
5264
- const subject = resolvedArgs[0];
5265
- const search2 = resolvedArgs[1];
5266
- return split(subject, search2, resolvedArgs.length > 2 ? resolvedArgs[2] : undefined);
5267
- };
5268
- functionReverse = ([inputValue]) => {
5269
- const typeName = this.getTypeName(inputValue);
5270
- if (typeName === 2) {
5271
- return new Text(inputValue).reverse();
5272
- }
5273
- const reversedArray = inputValue.slice(0);
5274
- reversedArray.reverse();
5275
- return reversedArray;
5276
- };
5277
- functionSort = ([inputValue]) => {
5278
- if (inputValue.length == 0) {
5279
- return inputValue;
5280
- }
5281
- if (typeof inputValue[0] === "string") {
5282
- return [...inputValue].sort(Text.comparer);
5283
- }
5284
- return [...inputValue].sort();
5285
- };
5286
- functionSortBy = (resolvedArgs) => {
5287
- const sortedArray = resolvedArgs[0].slice(0);
5288
- if (sortedArray.length === 0) {
5289
- return sortedArray;
5290
- }
5291
- const interpreter = this._interpreter;
5292
- const exprefNode = resolvedArgs[1];
5293
- const requiredType = this.getTypeName(interpreter.visit(exprefNode, sortedArray[0]));
5294
- if (requiredType !== undefined && ![0, 2].includes(requiredType)) {
5295
- throw new Error(`Invalid type: unexpected type (${this.TYPE_NAME_TABLE[requiredType]})`);
5296
- }
5297
- function throwInvalidTypeError(rt, item) {
5298
- throw new Error(`Invalid type: expected (${rt.TYPE_NAME_TABLE[requiredType]}), received ${rt.TYPE_NAME_TABLE[rt.getTypeName(item)]}`);
5299
- }
5300
- return sortedArray.sort((a, b) => {
5301
- const exprA = interpreter.visit(exprefNode, a);
5302
- const exprB = interpreter.visit(exprefNode, b);
5303
- if (this.getTypeName(exprA) !== requiredType) {
5304
- throwInvalidTypeError(this, exprA);
5305
- } else if (this.getTypeName(exprB) !== requiredType) {
5306
- throwInvalidTypeError(this, exprB);
5307
- }
5308
- if (requiredType === 2) {
5309
- return Text.comparer(exprA, exprB);
5310
- }
5311
- return exprA - exprB;
5312
- });
5313
- };
5314
- functionStartsWith = ([searchable, searchStr]) => {
5315
- return searchable.startsWith(searchStr);
5316
- };
5317
- functionSum = ([inputValue]) => {
5318
- return inputValue.reduce((x, y) => x + y, 0);
5319
- };
5320
- functionToArray = ([inputValue]) => {
5321
- if (this.getTypeName(inputValue) === 3) {
5322
- return inputValue;
5323
- }
5324
- return [inputValue];
5325
- };
5326
- functionToNumber = ([inputValue]) => {
5327
- const typeName = this.getTypeName(inputValue);
5328
- let convertedValue;
5329
- if (typeName === 0) {
5330
- return inputValue;
5331
- }
5332
- if (typeName === 2) {
5333
- convertedValue = +inputValue;
5334
- if (!isNaN(convertedValue)) {
5335
- return convertedValue;
4218
+ } else {
4219
+ return false;
4220
+ }
5336
4221
  }
5337
- }
5338
- return null;
5339
- };
5340
- functionToString = ([inputValue]) => {
5341
- if (this.getTypeName(inputValue) === 2) {
5342
- return inputValue;
5343
- }
5344
- return JSON.stringify(inputValue);
5345
- };
5346
- functionTrim = this.createTrimFunction(trim);
5347
- functionTrimLeft = this.createTrimFunction(trimLeft);
5348
- functionTrimRight = this.createTrimFunction(trimRight);
5349
- createTrimFunction(trimFn) {
5350
- return (resolvedArgs) => {
5351
- const subject = resolvedArgs[0];
5352
- const chars = resolvedArgs.length > 1 ? resolvedArgs[1] : undefined;
5353
- return trimFn(subject, chars);
5354
4222
  };
5355
- }
5356
- functionType = ([inputValue]) => {
5357
- switch (this.getTypeName(inputValue)) {
5358
- case 0:
5359
- return "number";
5360
- case 2:
5361
- return "string";
5362
- case 3:
5363
- return "array";
5364
- case 4:
5365
- return "object";
5366
- case 5:
5367
- return "boolean";
5368
- case 7:
5369
- return "null";
5370
- default:
5371
- throw new Error("invalid-type");
5372
- }
5373
- };
5374
- functionZip = (array) => {
5375
- const length = Math.min(...array.map((arr) => arr.length));
5376
- const result = Array(length).fill(null).map((_, index) => array.map((arr) => arr[index]));
5377
- return result;
5378
- };
5379
- }, ScopeChain = class _ScopeChain {
5380
- inner = undefined;
5381
- data = {};
5382
- get currentScopeData() {
5383
- return this.data;
5384
- }
5385
- withScope(data) {
5386
- const outer = new _ScopeChain;
5387
- outer.inner = this;
5388
- outer.data = data;
5389
- return outer;
5390
- }
5391
- getValue(identifier) {
5392
- if (Object.prototype.hasOwnProperty.call(this.data, identifier)) {
5393
- return this.data[identifier];
5394
- }
5395
- if (this.inner) {
5396
- return this.inner.getValue(identifier);
5397
- }
5398
- return null;
5399
- }
5400
- }, emptyScopeChain, TreeInterpreter = class _TreeInterpreter {
5401
- runtime;
5402
- _rootValue = null;
5403
- _scope;
5404
- constructor() {
5405
- this.runtime = new Runtime(this);
5406
- this._scope = new ScopeChain;
5407
- }
5408
- withScope(scope) {
5409
- const interpreter = new _TreeInterpreter;
5410
- interpreter.runtime._functionTable = this.runtime._functionTable;
5411
- interpreter._rootValue = this._rootValue;
5412
- interpreter._scope = this._scope.withScope(scope);
5413
- return interpreter;
5414
- }
5415
- search(node, value) {
5416
- this._rootValue = value;
5417
- this._scope = emptyScopeChain;
5418
- return this.visit(node, value);
5419
- }
5420
- visit(node, value) {
5421
- switch (node.type) {
5422
- case "Ternary": {
5423
- const condition = this.visit(node.condition, value);
5424
- if (!isFalse(condition)) {
5425
- return this.visit(node.trueExpr, value);
5426
- }
5427
- return this.visit(node.falseExpr, value);
5428
- }
5429
- case "Field":
5430
- const identifier = node.name;
5431
- if (value === null || typeof value !== "object" || Array.isArray(value)) {
5432
- return null;
4223
+ var bindingPower = {};
4224
+ bindingPower[TOK_EOF] = 0;
4225
+ bindingPower[TOK_UNQUOTEDIDENTIFIER] = 0;
4226
+ bindingPower[TOK_QUOTEDIDENTIFIER] = 0;
4227
+ bindingPower[TOK_RBRACKET] = 0;
4228
+ bindingPower[TOK_RPAREN] = 0;
4229
+ bindingPower[TOK_COMMA] = 0;
4230
+ bindingPower[TOK_RBRACE] = 0;
4231
+ bindingPower[TOK_NUMBER] = 0;
4232
+ bindingPower[TOK_CURRENT] = 0;
4233
+ bindingPower[TOK_EXPREF] = 0;
4234
+ bindingPower[TOK_PIPE] = 1;
4235
+ bindingPower[TOK_OR] = 2;
4236
+ bindingPower[TOK_AND] = 3;
4237
+ bindingPower[TOK_EQ] = 5;
4238
+ bindingPower[TOK_GT] = 5;
4239
+ bindingPower[TOK_LT] = 5;
4240
+ bindingPower[TOK_GTE] = 5;
4241
+ bindingPower[TOK_LTE] = 5;
4242
+ bindingPower[TOK_NE] = 5;
4243
+ bindingPower[TOK_FLATTEN] = 9;
4244
+ bindingPower[TOK_STAR] = 20;
4245
+ bindingPower[TOK_FILTER] = 21;
4246
+ bindingPower[TOK_DOT] = 40;
4247
+ bindingPower[TOK_NOT] = 45;
4248
+ bindingPower[TOK_LBRACE] = 50;
4249
+ bindingPower[TOK_LBRACKET] = 55;
4250
+ bindingPower[TOK_LPAREN] = 60;
4251
+ function Parser() {}
4252
+ Parser.prototype = {
4253
+ parse: function(expression) {
4254
+ this._loadTokens(expression);
4255
+ this.index = 0;
4256
+ var ast = this.expression(0);
4257
+ if (this._lookahead(0) !== TOK_EOF) {
4258
+ var t = this._lookaheadToken(0);
4259
+ var error = new Error("Unexpected token type: " + t.type + ", value: " + t.value);
4260
+ error.name = "ParserError";
4261
+ throw error;
4262
+ }
4263
+ return ast;
4264
+ },
4265
+ _loadTokens: function(expression) {
4266
+ var lexer = new Lexer;
4267
+ var tokens = lexer.tokenize(expression);
4268
+ tokens.push({ type: TOK_EOF, value: "", start: expression.length });
4269
+ this.tokens = tokens;
4270
+ },
4271
+ expression: function(rbp) {
4272
+ var leftToken = this._lookaheadToken(0);
4273
+ this._advance();
4274
+ var left = this.nud(leftToken);
4275
+ var currentToken = this._lookahead(0);
4276
+ while (rbp < bindingPower[currentToken]) {
4277
+ this._advance();
4278
+ left = this.led(currentToken, left);
4279
+ currentToken = this._lookahead(0);
4280
+ }
4281
+ return left;
4282
+ },
4283
+ _lookahead: function(number) {
4284
+ return this.tokens[this.index + number].type;
4285
+ },
4286
+ _lookaheadToken: function(number) {
4287
+ return this.tokens[this.index + number];
4288
+ },
4289
+ _advance: function() {
4290
+ this.index++;
4291
+ },
4292
+ nud: function(token) {
4293
+ var left;
4294
+ var right;
4295
+ var expression;
4296
+ switch (token.type) {
4297
+ case TOK_LITERAL:
4298
+ return { type: "Literal", value: token.value };
4299
+ case TOK_UNQUOTEDIDENTIFIER:
4300
+ return { type: "Field", name: token.value };
4301
+ case TOK_QUOTEDIDENTIFIER:
4302
+ var node = { type: "Field", name: token.value };
4303
+ if (this._lookahead(0) === TOK_LPAREN) {
4304
+ throw new Error("Quoted identifier not allowed for function names.");
4305
+ }
4306
+ return node;
4307
+ case TOK_NOT:
4308
+ right = this.expression(bindingPower.Not);
4309
+ return { type: "NotExpression", children: [right] };
4310
+ case TOK_STAR:
4311
+ left = { type: "Identity" };
4312
+ right = null;
4313
+ if (this._lookahead(0) === TOK_RBRACKET) {
4314
+ right = { type: "Identity" };
4315
+ } else {
4316
+ right = this._parseProjectionRHS(bindingPower.Star);
4317
+ }
4318
+ return { type: "ValueProjection", children: [left, right] };
4319
+ case TOK_FILTER:
4320
+ return this.led(token.type, { type: "Identity" });
4321
+ case TOK_LBRACE:
4322
+ return this._parseMultiselectHash();
4323
+ case TOK_FLATTEN:
4324
+ left = { type: TOK_FLATTEN, children: [{ type: "Identity" }] };
4325
+ right = this._parseProjectionRHS(bindingPower.Flatten);
4326
+ return { type: "Projection", children: [left, right] };
4327
+ case TOK_LBRACKET:
4328
+ if (this._lookahead(0) === TOK_NUMBER || this._lookahead(0) === TOK_COLON) {
4329
+ right = this._parseIndexExpression();
4330
+ return this._projectIfSlice({ type: "Identity" }, right);
4331
+ } else if (this._lookahead(0) === TOK_STAR && this._lookahead(1) === TOK_RBRACKET) {
4332
+ this._advance();
4333
+ this._advance();
4334
+ right = this._parseProjectionRHS(bindingPower.Star);
4335
+ return {
4336
+ type: "Projection",
4337
+ children: [{ type: "Identity" }, right]
4338
+ };
4339
+ }
4340
+ return this._parseMultiselectList();
4341
+ case TOK_CURRENT:
4342
+ return { type: TOK_CURRENT };
4343
+ case TOK_EXPREF:
4344
+ expression = this.expression(bindingPower.Expref);
4345
+ return { type: "ExpressionReference", children: [expression] };
4346
+ case TOK_LPAREN:
4347
+ var args = [];
4348
+ while (this._lookahead(0) !== TOK_RPAREN) {
4349
+ if (this._lookahead(0) === TOK_CURRENT) {
4350
+ expression = { type: TOK_CURRENT };
4351
+ this._advance();
4352
+ } else {
4353
+ expression = this.expression(0);
4354
+ }
4355
+ args.push(expression);
4356
+ }
4357
+ this._match(TOK_RPAREN);
4358
+ return args[0];
4359
+ default:
4360
+ this._errorToken(token);
4361
+ }
4362
+ },
4363
+ led: function(tokenName, left) {
4364
+ var right;
4365
+ switch (tokenName) {
4366
+ case TOK_DOT:
4367
+ var rbp = bindingPower.Dot;
4368
+ if (this._lookahead(0) !== TOK_STAR) {
4369
+ right = this._parseDotRHS(rbp);
4370
+ return { type: "Subexpression", children: [left, right] };
4371
+ }
4372
+ this._advance();
4373
+ right = this._parseProjectionRHS(rbp);
4374
+ return { type: "ValueProjection", children: [left, right] };
4375
+ case TOK_PIPE:
4376
+ right = this.expression(bindingPower.Pipe);
4377
+ return { type: TOK_PIPE, children: [left, right] };
4378
+ case TOK_OR:
4379
+ right = this.expression(bindingPower.Or);
4380
+ return { type: "OrExpression", children: [left, right] };
4381
+ case TOK_AND:
4382
+ right = this.expression(bindingPower.And);
4383
+ return { type: "AndExpression", children: [left, right] };
4384
+ case TOK_LPAREN:
4385
+ var name = left.name;
4386
+ var args = [];
4387
+ var expression, node;
4388
+ while (this._lookahead(0) !== TOK_RPAREN) {
4389
+ if (this._lookahead(0) === TOK_CURRENT) {
4390
+ expression = { type: TOK_CURRENT };
4391
+ this._advance();
4392
+ } else {
4393
+ expression = this.expression(0);
4394
+ }
4395
+ if (this._lookahead(0) === TOK_COMMA) {
4396
+ this._match(TOK_COMMA);
4397
+ }
4398
+ args.push(expression);
4399
+ }
4400
+ this._match(TOK_RPAREN);
4401
+ node = { type: "Function", name, children: args };
4402
+ return node;
4403
+ case TOK_FILTER:
4404
+ var condition = this.expression(0);
4405
+ this._match(TOK_RBRACKET);
4406
+ if (this._lookahead(0) === TOK_FLATTEN) {
4407
+ right = { type: "Identity" };
4408
+ } else {
4409
+ right = this._parseProjectionRHS(bindingPower.Filter);
4410
+ }
4411
+ return { type: "FilterProjection", children: [left, right, condition] };
4412
+ case TOK_FLATTEN:
4413
+ var leftNode = { type: TOK_FLATTEN, children: [left] };
4414
+ var rightNode = this._parseProjectionRHS(bindingPower.Flatten);
4415
+ return { type: "Projection", children: [leftNode, rightNode] };
4416
+ case TOK_EQ:
4417
+ case TOK_NE:
4418
+ case TOK_GT:
4419
+ case TOK_GTE:
4420
+ case TOK_LT:
4421
+ case TOK_LTE:
4422
+ return this._parseComparator(left, tokenName);
4423
+ case TOK_LBRACKET:
4424
+ var token = this._lookaheadToken(0);
4425
+ if (token.type === TOK_NUMBER || token.type === TOK_COLON) {
4426
+ right = this._parseIndexExpression();
4427
+ return this._projectIfSlice(left, right);
4428
+ }
4429
+ this._match(TOK_STAR);
4430
+ this._match(TOK_RBRACKET);
4431
+ right = this._parseProjectionRHS(bindingPower.Star);
4432
+ return { type: "Projection", children: [left, right] };
4433
+ default:
4434
+ this._errorToken(this._lookaheadToken(0));
4435
+ }
4436
+ },
4437
+ _match: function(tokenType) {
4438
+ if (this._lookahead(0) === tokenType) {
4439
+ this._advance();
4440
+ } else {
4441
+ var t = this._lookaheadToken(0);
4442
+ var error = new Error("Expected " + tokenType + ", got: " + t.type);
4443
+ error.name = "ParserError";
4444
+ throw error;
5433
4445
  }
5434
- return value[identifier] ?? null;
5435
- case "LetExpression": {
5436
- const { bindings, expression } = node;
5437
- let scope = {};
5438
- bindings.forEach((binding) => {
5439
- const reference = this.visit(binding, value);
5440
- scope = {
5441
- ...scope,
5442
- ...reference
4446
+ },
4447
+ _errorToken: function(token) {
4448
+ var error = new Error("Invalid token (" + token.type + '): "' + token.value + '"');
4449
+ error.name = "ParserError";
4450
+ throw error;
4451
+ },
4452
+ _parseIndexExpression: function() {
4453
+ if (this._lookahead(0) === TOK_COLON || this._lookahead(1) === TOK_COLON) {
4454
+ return this._parseSliceExpression();
4455
+ } else {
4456
+ var node = {
4457
+ type: "Index",
4458
+ value: this._lookaheadToken(0).value
5443
4459
  };
5444
- });
5445
- return this.withScope(scope).visit(expression, value);
5446
- }
5447
- case "Binding": {
5448
- const { variable, reference } = node;
5449
- const result = this.visit(reference, value);
5450
- return { [variable]: result };
5451
- }
5452
- case "Variable": {
5453
- const variable = node.name;
5454
- if (!this._scope.getValue(variable) && !Object.prototype.hasOwnProperty.call(this._scope.currentScopeData, variable)) {
5455
- throw new Error(`Error referencing undefined variable ${variable}`);
4460
+ this._advance();
4461
+ this._match(TOK_RBRACKET);
4462
+ return node;
5456
4463
  }
5457
- return this._scope.getValue(variable);
5458
- }
5459
- case "IndexExpression":
5460
- return this.visit(node.right, this.visit(node.left, value));
5461
- case "Subexpression": {
5462
- const result = this.visit(node.left, value);
5463
- return result != null ? this.visit(node.right, result) ?? null : null;
5464
- }
5465
- case "Index": {
5466
- if (!Array.isArray(value)) {
5467
- return null;
4464
+ },
4465
+ _projectIfSlice: function(left, right) {
4466
+ var indexExpr = { type: "IndexExpression", children: [left, right] };
4467
+ if (right.type === "Slice") {
4468
+ return {
4469
+ type: "Projection",
4470
+ children: [indexExpr, this._parseProjectionRHS(bindingPower.Star)]
4471
+ };
4472
+ } else {
4473
+ return indexExpr;
5468
4474
  }
5469
- const index = node.value < 0 ? value.length + node.value : node.value;
5470
- return value[index] ?? null;
5471
- }
5472
- case "Slice": {
5473
- if (!Array.isArray(value) && typeof value !== "string") {
5474
- return null;
4475
+ },
4476
+ _parseSliceExpression: function() {
4477
+ var parts = [null, null, null];
4478
+ var index = 0;
4479
+ var currentToken = this._lookahead(0);
4480
+ while (currentToken !== TOK_RBRACKET && index < 3) {
4481
+ if (currentToken === TOK_COLON) {
4482
+ index++;
4483
+ this._advance();
4484
+ } else if (currentToken === TOK_NUMBER) {
4485
+ parts[index] = this._lookaheadToken(0).value;
4486
+ this._advance();
4487
+ } else {
4488
+ var t = this._lookahead(0);
4489
+ var error = new Error("Syntax error, unexpected token: " + t.value + "(" + t.type + ")");
4490
+ error.name = "Parsererror";
4491
+ throw error;
4492
+ }
4493
+ currentToken = this._lookahead(0);
4494
+ }
4495
+ this._match(TOK_RBRACKET);
4496
+ return {
4497
+ type: "Slice",
4498
+ children: parts
4499
+ };
4500
+ },
4501
+ _parseComparator: function(left, comparator) {
4502
+ var right = this.expression(bindingPower[comparator]);
4503
+ return { type: "Comparator", name: comparator, children: [left, right] };
4504
+ },
4505
+ _parseDotRHS: function(rbp) {
4506
+ var lookahead = this._lookahead(0);
4507
+ var exprTokens = [TOK_UNQUOTEDIDENTIFIER, TOK_QUOTEDIDENTIFIER, TOK_STAR];
4508
+ if (exprTokens.indexOf(lookahead) >= 0) {
4509
+ return this.expression(rbp);
4510
+ } else if (lookahead === TOK_LBRACKET) {
4511
+ this._match(TOK_LBRACKET);
4512
+ return this._parseMultiselectList();
4513
+ } else if (lookahead === TOK_LBRACE) {
4514
+ this._match(TOK_LBRACE);
4515
+ return this._parseMultiselectHash();
5475
4516
  }
5476
- const { start, stop, step } = this.computeSliceParams(value.length, node);
5477
- if (typeof value === "string") {
5478
- const chars = [...value];
5479
- const sliced = this.slice(chars, start, stop, step);
5480
- return sliced.join("");
4517
+ },
4518
+ _parseProjectionRHS: function(rbp) {
4519
+ var right;
4520
+ if (bindingPower[this._lookahead(0)] < 10) {
4521
+ right = { type: "Identity" };
4522
+ } else if (this._lookahead(0) === TOK_LBRACKET) {
4523
+ right = this.expression(rbp);
4524
+ } else if (this._lookahead(0) === TOK_FILTER) {
4525
+ right = this.expression(rbp);
4526
+ } else if (this._lookahead(0) === TOK_DOT) {
4527
+ this._match(TOK_DOT);
4528
+ right = this._parseDotRHS(rbp);
5481
4529
  } else {
5482
- return this.slice(value, start, stop, step);
4530
+ var t = this._lookaheadToken(0);
4531
+ var error = new Error("Sytanx error, unexpected token: " + t.value + "(" + t.type + ")");
4532
+ error.name = "ParserError";
4533
+ throw error;
4534
+ }
4535
+ return right;
4536
+ },
4537
+ _parseMultiselectList: function() {
4538
+ var expressions = [];
4539
+ while (this._lookahead(0) !== TOK_RBRACKET) {
4540
+ var expression = this.expression(0);
4541
+ expressions.push(expression);
4542
+ if (this._lookahead(0) === TOK_COMMA) {
4543
+ this._match(TOK_COMMA);
4544
+ if (this._lookahead(0) === TOK_RBRACKET) {
4545
+ throw new Error("Unexpected token Rbracket");
4546
+ }
4547
+ }
4548
+ }
4549
+ this._match(TOK_RBRACKET);
4550
+ return { type: "MultiSelectList", children: expressions };
4551
+ },
4552
+ _parseMultiselectHash: function() {
4553
+ var pairs2 = [];
4554
+ var identifierTypes = [TOK_UNQUOTEDIDENTIFIER, TOK_QUOTEDIDENTIFIER];
4555
+ var keyToken, keyName, value, node;
4556
+ for (;; ) {
4557
+ keyToken = this._lookaheadToken(0);
4558
+ if (identifierTypes.indexOf(keyToken.type) < 0) {
4559
+ throw new Error("Expecting an identifier token, got: " + keyToken.type);
4560
+ }
4561
+ keyName = keyToken.value;
4562
+ this._advance();
4563
+ this._match(TOK_COLON);
4564
+ value = this.expression(0);
4565
+ node = { type: "KeyValuePair", name: keyName, value };
4566
+ pairs2.push(node);
4567
+ if (this._lookahead(0) === TOK_COMMA) {
4568
+ this._match(TOK_COMMA);
4569
+ } else if (this._lookahead(0) === TOK_RBRACE) {
4570
+ this._match(TOK_RBRACE);
4571
+ break;
4572
+ }
5483
4573
  }
4574
+ return { type: "MultiSelectHash", children: pairs2 };
5484
4575
  }
5485
- case "Projection": {
5486
- const { left, right } = node;
5487
- let allowString = false;
5488
- if (left.type === "IndexExpression" && left.right.type === "Slice") {
5489
- allowString = true;
4576
+ };
4577
+ function TreeInterpreter(runtime) {
4578
+ this.runtime = runtime;
4579
+ }
4580
+ TreeInterpreter.prototype = {
4581
+ search: function(node, value) {
4582
+ return this.visit(node, value);
4583
+ },
4584
+ visit: function(node, value) {
4585
+ var matched, current, result, first, second, field, left, right, collected, i;
4586
+ switch (node.type) {
4587
+ case "Field":
4588
+ if (value !== null && isObject(value)) {
4589
+ field = value[node.name];
4590
+ if (field === undefined) {
4591
+ return null;
4592
+ } else {
4593
+ return field;
4594
+ }
4595
+ }
4596
+ return null;
4597
+ case "Subexpression":
4598
+ result = this.visit(node.children[0], value);
4599
+ for (i = 1;i < node.children.length; i++) {
4600
+ result = this.visit(node.children[1], result);
4601
+ if (result === null) {
4602
+ return null;
4603
+ }
4604
+ }
4605
+ return result;
4606
+ case "IndexExpression":
4607
+ left = this.visit(node.children[0], value);
4608
+ right = this.visit(node.children[1], left);
4609
+ return right;
4610
+ case "Index":
4611
+ if (!isArray(value)) {
4612
+ return null;
4613
+ }
4614
+ var index = node.value;
4615
+ if (index < 0) {
4616
+ index = value.length + index;
4617
+ }
4618
+ result = value[index];
4619
+ if (result === undefined) {
4620
+ result = null;
4621
+ }
4622
+ return result;
4623
+ case "Slice":
4624
+ if (!isArray(value)) {
4625
+ return null;
4626
+ }
4627
+ var sliceParams = node.children.slice(0);
4628
+ var computed = this.computeSliceParams(value.length, sliceParams);
4629
+ var start = computed[0];
4630
+ var stop = computed[1];
4631
+ var step = computed[2];
4632
+ result = [];
4633
+ if (step > 0) {
4634
+ for (i = start;i < stop; i += step) {
4635
+ result.push(value[i]);
4636
+ }
4637
+ } else {
4638
+ for (i = start;i > stop; i += step) {
4639
+ result.push(value[i]);
4640
+ }
4641
+ }
4642
+ return result;
4643
+ case "Projection":
4644
+ var base = this.visit(node.children[0], value);
4645
+ if (!isArray(base)) {
4646
+ return null;
4647
+ }
4648
+ collected = [];
4649
+ for (i = 0;i < base.length; i++) {
4650
+ current = this.visit(node.children[1], base[i]);
4651
+ if (current !== null) {
4652
+ collected.push(current);
4653
+ }
4654
+ }
4655
+ return collected;
4656
+ case "ValueProjection":
4657
+ base = this.visit(node.children[0], value);
4658
+ if (!isObject(base)) {
4659
+ return null;
4660
+ }
4661
+ collected = [];
4662
+ var values = objValues(base);
4663
+ for (i = 0;i < values.length; i++) {
4664
+ current = this.visit(node.children[1], values[i]);
4665
+ if (current !== null) {
4666
+ collected.push(current);
4667
+ }
4668
+ }
4669
+ return collected;
4670
+ case "FilterProjection":
4671
+ base = this.visit(node.children[0], value);
4672
+ if (!isArray(base)) {
4673
+ return null;
4674
+ }
4675
+ var filtered = [];
4676
+ var finalResults = [];
4677
+ for (i = 0;i < base.length; i++) {
4678
+ matched = this.visit(node.children[2], base[i]);
4679
+ if (!isFalse(matched)) {
4680
+ filtered.push(base[i]);
4681
+ }
4682
+ }
4683
+ for (var j = 0;j < filtered.length; j++) {
4684
+ current = this.visit(node.children[1], filtered[j]);
4685
+ if (current !== null) {
4686
+ finalResults.push(current);
4687
+ }
4688
+ }
4689
+ return finalResults;
4690
+ case "Comparator":
4691
+ first = this.visit(node.children[0], value);
4692
+ second = this.visit(node.children[1], value);
4693
+ switch (node.name) {
4694
+ case TOK_EQ:
4695
+ result = strictDeepEqual(first, second);
4696
+ break;
4697
+ case TOK_NE:
4698
+ result = !strictDeepEqual(first, second);
4699
+ break;
4700
+ case TOK_GT:
4701
+ result = first > second;
4702
+ break;
4703
+ case TOK_GTE:
4704
+ result = first >= second;
4705
+ break;
4706
+ case TOK_LT:
4707
+ result = first < second;
4708
+ break;
4709
+ case TOK_LTE:
4710
+ result = first <= second;
4711
+ break;
4712
+ default:
4713
+ throw new Error("Unknown comparator: " + node.name);
4714
+ }
4715
+ return result;
4716
+ case TOK_FLATTEN:
4717
+ var original = this.visit(node.children[0], value);
4718
+ if (!isArray(original)) {
4719
+ return null;
4720
+ }
4721
+ var merged = [];
4722
+ for (i = 0;i < original.length; i++) {
4723
+ current = original[i];
4724
+ if (isArray(current)) {
4725
+ merged.push.apply(merged, current);
4726
+ } else {
4727
+ merged.push(current);
4728
+ }
4729
+ }
4730
+ return merged;
4731
+ case "Identity":
4732
+ return value;
4733
+ case "MultiSelectList":
4734
+ if (value === null) {
4735
+ return null;
4736
+ }
4737
+ collected = [];
4738
+ for (i = 0;i < node.children.length; i++) {
4739
+ collected.push(this.visit(node.children[i], value));
4740
+ }
4741
+ return collected;
4742
+ case "MultiSelectHash":
4743
+ if (value === null) {
4744
+ return null;
4745
+ }
4746
+ collected = {};
4747
+ var child;
4748
+ for (i = 0;i < node.children.length; i++) {
4749
+ child = node.children[i];
4750
+ collected[child.name] = this.visit(child.value, value);
4751
+ }
4752
+ return collected;
4753
+ case "OrExpression":
4754
+ matched = this.visit(node.children[0], value);
4755
+ if (isFalse(matched)) {
4756
+ matched = this.visit(node.children[1], value);
4757
+ }
4758
+ return matched;
4759
+ case "AndExpression":
4760
+ first = this.visit(node.children[0], value);
4761
+ if (isFalse(first) === true) {
4762
+ return first;
4763
+ }
4764
+ return this.visit(node.children[1], value);
4765
+ case "NotExpression":
4766
+ first = this.visit(node.children[0], value);
4767
+ return isFalse(first);
4768
+ case "Literal":
4769
+ return node.value;
4770
+ case TOK_PIPE:
4771
+ left = this.visit(node.children[0], value);
4772
+ return this.visit(node.children[1], left);
4773
+ case TOK_CURRENT:
4774
+ return value;
4775
+ case "Function":
4776
+ var resolvedArgs = [];
4777
+ for (i = 0;i < node.children.length; i++) {
4778
+ resolvedArgs.push(this.visit(node.children[i], value));
4779
+ }
4780
+ return this.runtime.callFunction(node.name, resolvedArgs);
4781
+ case "ExpressionReference":
4782
+ var refNode = node.children[0];
4783
+ refNode.jmespathType = TOK_EXPREF;
4784
+ return refNode;
4785
+ default:
4786
+ throw new Error("Unknown node type: " + node.type);
4787
+ }
4788
+ },
4789
+ computeSliceParams: function(arrayLength, sliceParams) {
4790
+ var start = sliceParams[0];
4791
+ var stop = sliceParams[1];
4792
+ var step = sliceParams[2];
4793
+ var computed = [null, null, null];
4794
+ if (step === null) {
4795
+ step = 1;
4796
+ } else if (step === 0) {
4797
+ var error = new Error("Invalid slice, step cannot be 0");
4798
+ error.name = "RuntimeError";
4799
+ throw error;
5490
4800
  }
5491
- const base = this.visit(left, value);
5492
- if (allowString && typeof base === "string") {
5493
- return this.visit(right, base);
4801
+ var stepValueNegative = step < 0 ? true : false;
4802
+ if (start === null) {
4803
+ start = stepValueNegative ? arrayLength - 1 : 0;
4804
+ } else {
4805
+ start = this.capSliceRange(arrayLength, start, step);
5494
4806
  }
5495
- if (!Array.isArray(base)) {
5496
- return null;
4807
+ if (stop === null) {
4808
+ stop = stepValueNegative ? -1 : arrayLength;
4809
+ } else {
4810
+ stop = this.capSliceRange(arrayLength, stop, step);
5497
4811
  }
5498
- const collected = [];
5499
- for (const elem of base) {
5500
- const current = this.visit(right, elem);
5501
- if (current !== null) {
5502
- collected.push(current);
4812
+ computed[0] = start;
4813
+ computed[1] = stop;
4814
+ computed[2] = step;
4815
+ return computed;
4816
+ },
4817
+ capSliceRange: function(arrayLength, actualValue, step) {
4818
+ if (actualValue < 0) {
4819
+ actualValue += arrayLength;
4820
+ if (actualValue < 0) {
4821
+ actualValue = step < 0 ? -1 : 0;
5503
4822
  }
4823
+ } else if (actualValue >= arrayLength) {
4824
+ actualValue = step < 0 ? arrayLength - 1 : arrayLength;
5504
4825
  }
5505
- return collected;
4826
+ return actualValue;
5506
4827
  }
5507
- case "ValueProjection": {
5508
- const { left, right } = node;
5509
- const base = this.visit(left, value);
5510
- if (base === null || typeof base !== "object" || Array.isArray(base)) {
5511
- return null;
4828
+ };
4829
+ function Runtime(interpreter) {
4830
+ this._interpreter = interpreter;
4831
+ this.functionTable = {
4832
+ abs: { _func: this._functionAbs, _signature: [{ types: [TYPE_NUMBER] }] },
4833
+ avg: { _func: this._functionAvg, _signature: [{ types: [TYPE_ARRAY_NUMBER] }] },
4834
+ ceil: { _func: this._functionCeil, _signature: [{ types: [TYPE_NUMBER] }] },
4835
+ contains: {
4836
+ _func: this._functionContains,
4837
+ _signature: [
4838
+ { types: [TYPE_STRING, TYPE_ARRAY] },
4839
+ { types: [TYPE_ANY] }
4840
+ ]
4841
+ },
4842
+ ends_with: {
4843
+ _func: this._functionEndsWith,
4844
+ _signature: [{ types: [TYPE_STRING] }, { types: [TYPE_STRING] }]
4845
+ },
4846
+ floor: { _func: this._functionFloor, _signature: [{ types: [TYPE_NUMBER] }] },
4847
+ length: {
4848
+ _func: this._functionLength,
4849
+ _signature: [{ types: [TYPE_STRING, TYPE_ARRAY, TYPE_OBJECT] }]
4850
+ },
4851
+ map: {
4852
+ _func: this._functionMap,
4853
+ _signature: [{ types: [TYPE_EXPREF] }, { types: [TYPE_ARRAY] }]
4854
+ },
4855
+ max: {
4856
+ _func: this._functionMax,
4857
+ _signature: [{ types: [TYPE_ARRAY_NUMBER, TYPE_ARRAY_STRING] }]
4858
+ },
4859
+ merge: {
4860
+ _func: this._functionMerge,
4861
+ _signature: [{ types: [TYPE_OBJECT], variadic: true }]
4862
+ },
4863
+ max_by: {
4864
+ _func: this._functionMaxBy,
4865
+ _signature: [{ types: [TYPE_ARRAY] }, { types: [TYPE_EXPREF] }]
4866
+ },
4867
+ sum: { _func: this._functionSum, _signature: [{ types: [TYPE_ARRAY_NUMBER] }] },
4868
+ starts_with: {
4869
+ _func: this._functionStartsWith,
4870
+ _signature: [{ types: [TYPE_STRING] }, { types: [TYPE_STRING] }]
4871
+ },
4872
+ min: {
4873
+ _func: this._functionMin,
4874
+ _signature: [{ types: [TYPE_ARRAY_NUMBER, TYPE_ARRAY_STRING] }]
4875
+ },
4876
+ min_by: {
4877
+ _func: this._functionMinBy,
4878
+ _signature: [{ types: [TYPE_ARRAY] }, { types: [TYPE_EXPREF] }]
4879
+ },
4880
+ type: { _func: this._functionType, _signature: [{ types: [TYPE_ANY] }] },
4881
+ keys: { _func: this._functionKeys, _signature: [{ types: [TYPE_OBJECT] }] },
4882
+ values: { _func: this._functionValues, _signature: [{ types: [TYPE_OBJECT] }] },
4883
+ sort: { _func: this._functionSort, _signature: [{ types: [TYPE_ARRAY_STRING, TYPE_ARRAY_NUMBER] }] },
4884
+ sort_by: {
4885
+ _func: this._functionSortBy,
4886
+ _signature: [{ types: [TYPE_ARRAY] }, { types: [TYPE_EXPREF] }]
4887
+ },
4888
+ join: {
4889
+ _func: this._functionJoin,
4890
+ _signature: [
4891
+ { types: [TYPE_STRING] },
4892
+ { types: [TYPE_ARRAY_STRING] }
4893
+ ]
4894
+ },
4895
+ reverse: {
4896
+ _func: this._functionReverse,
4897
+ _signature: [{ types: [TYPE_STRING, TYPE_ARRAY] }]
4898
+ },
4899
+ to_array: { _func: this._functionToArray, _signature: [{ types: [TYPE_ANY] }] },
4900
+ to_string: { _func: this._functionToString, _signature: [{ types: [TYPE_ANY] }] },
4901
+ to_number: { _func: this._functionToNumber, _signature: [{ types: [TYPE_ANY] }] },
4902
+ not_null: {
4903
+ _func: this._functionNotNull,
4904
+ _signature: [{ types: [TYPE_ANY], variadic: true }]
4905
+ }
4906
+ };
4907
+ }
4908
+ Runtime.prototype = {
4909
+ callFunction: function(name, resolvedArgs) {
4910
+ var functionEntry = this.functionTable[name];
4911
+ if (functionEntry === undefined) {
4912
+ throw new Error("Unknown function: " + name + "()");
5512
4913
  }
5513
- const collected = [];
5514
- const values = Object.values(base);
5515
- for (const elem of values) {
5516
- const current = this.visit(right, elem);
5517
- if (current !== null) {
5518
- collected.push(current);
4914
+ this._validateArgs(name, resolvedArgs, functionEntry._signature);
4915
+ return functionEntry._func.call(this, resolvedArgs);
4916
+ },
4917
+ _validateArgs: function(name, args, signature) {
4918
+ var pluralized;
4919
+ if (signature[signature.length - 1].variadic) {
4920
+ if (args.length < signature.length) {
4921
+ pluralized = signature.length === 1 ? " argument" : " arguments";
4922
+ throw new Error("ArgumentError: " + name + "() " + "takes at least" + signature.length + pluralized + " but received " + args.length);
4923
+ }
4924
+ } else if (args.length !== signature.length) {
4925
+ pluralized = signature.length === 1 ? " argument" : " arguments";
4926
+ throw new Error("ArgumentError: " + name + "() " + "takes " + signature.length + pluralized + " but received " + args.length);
4927
+ }
4928
+ var currentSpec;
4929
+ var actualType;
4930
+ var typeMatched;
4931
+ for (var i = 0;i < signature.length; i++) {
4932
+ typeMatched = false;
4933
+ currentSpec = signature[i].types;
4934
+ actualType = this._getTypeName(args[i]);
4935
+ for (var j = 0;j < currentSpec.length; j++) {
4936
+ if (this._typeMatches(actualType, currentSpec[j], args[i])) {
4937
+ typeMatched = true;
4938
+ break;
4939
+ }
4940
+ }
4941
+ if (!typeMatched) {
4942
+ var expected = currentSpec.map(function(typeIdentifier) {
4943
+ return TYPE_NAME_TABLE[typeIdentifier];
4944
+ }).join(",");
4945
+ throw new Error("TypeError: " + name + "() " + "expected argument " + (i + 1) + " to be type " + expected + " but received type " + TYPE_NAME_TABLE[actualType] + " instead.");
5519
4946
  }
5520
4947
  }
5521
- return collected;
5522
- }
5523
- case "FilterProjection": {
5524
- const { left, right, condition } = node;
5525
- const base = this.visit(left, value);
5526
- if (!Array.isArray(base)) {
5527
- return null;
4948
+ },
4949
+ _typeMatches: function(actual, expected, argValue) {
4950
+ if (expected === TYPE_ANY) {
4951
+ return true;
5528
4952
  }
5529
- const results = [];
5530
- for (const elem of base) {
5531
- const matched = this.visit(condition, elem);
5532
- if (isFalse(matched)) {
5533
- continue;
4953
+ if (expected === TYPE_ARRAY_STRING || expected === TYPE_ARRAY_NUMBER || expected === TYPE_ARRAY) {
4954
+ if (expected === TYPE_ARRAY) {
4955
+ return actual === TYPE_ARRAY;
4956
+ } else if (actual === TYPE_ARRAY) {
4957
+ var subtype;
4958
+ if (expected === TYPE_ARRAY_NUMBER) {
4959
+ subtype = TYPE_NUMBER;
4960
+ } else if (expected === TYPE_ARRAY_STRING) {
4961
+ subtype = TYPE_STRING;
4962
+ }
4963
+ for (var i = 0;i < argValue.length; i++) {
4964
+ if (!this._typeMatches(this._getTypeName(argValue[i]), subtype, argValue[i])) {
4965
+ return false;
4966
+ }
4967
+ }
4968
+ return true;
5534
4969
  }
5535
- const result = this.visit(right, elem);
5536
- if (result !== null) {
5537
- results.push(result);
4970
+ } else {
4971
+ return actual === expected;
4972
+ }
4973
+ },
4974
+ _getTypeName: function(obj) {
4975
+ switch (Object.prototype.toString.call(obj)) {
4976
+ case "[object String]":
4977
+ return TYPE_STRING;
4978
+ case "[object Number]":
4979
+ return TYPE_NUMBER;
4980
+ case "[object Array]":
4981
+ return TYPE_ARRAY;
4982
+ case "[object Boolean]":
4983
+ return TYPE_BOOLEAN;
4984
+ case "[object Null]":
4985
+ return TYPE_NULL;
4986
+ case "[object Object]":
4987
+ if (obj.jmespathType === TOK_EXPREF) {
4988
+ return TYPE_EXPREF;
4989
+ } else {
4990
+ return TYPE_OBJECT;
4991
+ }
4992
+ }
4993
+ },
4994
+ _functionStartsWith: function(resolvedArgs) {
4995
+ return resolvedArgs[0].lastIndexOf(resolvedArgs[1]) === 0;
4996
+ },
4997
+ _functionEndsWith: function(resolvedArgs) {
4998
+ var searchStr = resolvedArgs[0];
4999
+ var suffix = resolvedArgs[1];
5000
+ return searchStr.indexOf(suffix, searchStr.length - suffix.length) !== -1;
5001
+ },
5002
+ _functionReverse: function(resolvedArgs) {
5003
+ var typeName = this._getTypeName(resolvedArgs[0]);
5004
+ if (typeName === TYPE_STRING) {
5005
+ var originalStr = resolvedArgs[0];
5006
+ var reversedStr = "";
5007
+ for (var i = originalStr.length - 1;i >= 0; i--) {
5008
+ reversedStr += originalStr[i];
5538
5009
  }
5010
+ return reversedStr;
5011
+ } else {
5012
+ var reversedArray = resolvedArgs[0].slice(0);
5013
+ reversedArray.reverse();
5014
+ return reversedArray;
5539
5015
  }
5540
- return results;
5541
- }
5542
- case "Arithmetic": {
5543
- const first = this.visit(node.left, value);
5544
- const second = this.visit(node.right, value);
5545
- switch (node.operator) {
5546
- case "Plus":
5547
- return add(first, second);
5548
- case "Minus":
5549
- return sub(first, second);
5550
- case "Multiply":
5551
- case "Star":
5552
- return mul(first, second);
5553
- case "Divide":
5554
- return divide(first, second);
5555
- case "Modulo":
5556
- return mod(first, second);
5557
- case "Div":
5558
- return div(first, second);
5559
- default:
5560
- throw new Error(`Syntax error: unknown arithmetic operator: ${node.operator}`);
5561
- }
5562
- }
5563
- case "Unary": {
5564
- const operand = this.visit(node.operand, value);
5565
- switch (node.operator) {
5566
- case "Plus":
5567
- ensureNumbers(operand);
5568
- return operand;
5569
- case "Minus":
5570
- ensureNumbers(operand);
5571
- return -operand;
5572
- default:
5573
- throw new Error(`Syntax error: unknown arithmetic operator: ${node.operator}`);
5016
+ },
5017
+ _functionAbs: function(resolvedArgs) {
5018
+ return Math.abs(resolvedArgs[0]);
5019
+ },
5020
+ _functionCeil: function(resolvedArgs) {
5021
+ return Math.ceil(resolvedArgs[0]);
5022
+ },
5023
+ _functionAvg: function(resolvedArgs) {
5024
+ var sum = 0;
5025
+ var inputArray = resolvedArgs[0];
5026
+ for (var i = 0;i < inputArray.length; i++) {
5027
+ sum += inputArray[i];
5574
5028
  }
5575
- }
5576
- case "Comparator": {
5577
- const first = this.visit(node.left, value);
5578
- const second = this.visit(node.right, value);
5579
- switch (node.name) {
5580
- case "EQ":
5581
- return strictDeepEqual(first, second);
5582
- case "NE":
5583
- return !strictDeepEqual(first, second);
5029
+ return sum / inputArray.length;
5030
+ },
5031
+ _functionContains: function(resolvedArgs) {
5032
+ return resolvedArgs[0].indexOf(resolvedArgs[1]) >= 0;
5033
+ },
5034
+ _functionFloor: function(resolvedArgs) {
5035
+ return Math.floor(resolvedArgs[0]);
5036
+ },
5037
+ _functionLength: function(resolvedArgs) {
5038
+ if (!isObject(resolvedArgs[0])) {
5039
+ return resolvedArgs[0].length;
5040
+ } else {
5041
+ return Object.keys(resolvedArgs[0]).length;
5042
+ }
5043
+ },
5044
+ _functionMap: function(resolvedArgs) {
5045
+ var mapped = [];
5046
+ var interpreter = this._interpreter;
5047
+ var exprefNode = resolvedArgs[0];
5048
+ var elements = resolvedArgs[1];
5049
+ for (var i = 0;i < elements.length; i++) {
5050
+ mapped.push(interpreter.visit(exprefNode, elements[i]));
5051
+ }
5052
+ return mapped;
5053
+ },
5054
+ _functionMerge: function(resolvedArgs) {
5055
+ var merged = {};
5056
+ for (var i = 0;i < resolvedArgs.length; i++) {
5057
+ var current = resolvedArgs[i];
5058
+ for (var key in current) {
5059
+ merged[key] = current[key];
5060
+ }
5061
+ }
5062
+ return merged;
5063
+ },
5064
+ _functionMax: function(resolvedArgs) {
5065
+ if (resolvedArgs[0].length > 0) {
5066
+ var typeName = this._getTypeName(resolvedArgs[0][0]);
5067
+ if (typeName === TYPE_NUMBER) {
5068
+ return Math.max.apply(Math, resolvedArgs[0]);
5069
+ } else {
5070
+ var elements = resolvedArgs[0];
5071
+ var maxElement = elements[0];
5072
+ for (var i = 1;i < elements.length; i++) {
5073
+ if (maxElement.localeCompare(elements[i]) < 0) {
5074
+ maxElement = elements[i];
5075
+ }
5076
+ }
5077
+ return maxElement;
5078
+ }
5079
+ } else {
5080
+ return null;
5584
5081
  }
5585
- if (typeof first !== "number" || typeof second !== "number") {
5082
+ },
5083
+ _functionMin: function(resolvedArgs) {
5084
+ if (resolvedArgs[0].length > 0) {
5085
+ var typeName = this._getTypeName(resolvedArgs[0][0]);
5086
+ if (typeName === TYPE_NUMBER) {
5087
+ return Math.min.apply(Math, resolvedArgs[0]);
5088
+ } else {
5089
+ var elements = resolvedArgs[0];
5090
+ var minElement = elements[0];
5091
+ for (var i = 1;i < elements.length; i++) {
5092
+ if (elements[i].localeCompare(minElement) < 0) {
5093
+ minElement = elements[i];
5094
+ }
5095
+ }
5096
+ return minElement;
5097
+ }
5098
+ } else {
5586
5099
  return null;
5587
5100
  }
5588
- switch (node.name) {
5589
- case "GT":
5590
- return first > second;
5591
- case "GTE":
5592
- return first >= second;
5593
- case "LT":
5594
- return first < second;
5595
- case "LTE":
5596
- return first <= second;
5101
+ },
5102
+ _functionSum: function(resolvedArgs) {
5103
+ var sum = 0;
5104
+ var listToSum = resolvedArgs[0];
5105
+ for (var i = 0;i < listToSum.length; i++) {
5106
+ sum += listToSum[i];
5597
5107
  }
5598
- }
5599
- case "Flatten": {
5600
- const original = this.visit(node.child, value);
5601
- return Array.isArray(original) ? original.flat() : null;
5602
- }
5603
- case "Root":
5604
- return this._rootValue;
5605
- case "MultiSelectList": {
5606
- const collected = [];
5607
- for (const child of node.children) {
5608
- collected.push(this.visit(child, value));
5108
+ return sum;
5109
+ },
5110
+ _functionType: function(resolvedArgs) {
5111
+ switch (this._getTypeName(resolvedArgs[0])) {
5112
+ case TYPE_NUMBER:
5113
+ return "number";
5114
+ case TYPE_STRING:
5115
+ return "string";
5116
+ case TYPE_ARRAY:
5117
+ return "array";
5118
+ case TYPE_OBJECT:
5119
+ return "object";
5120
+ case TYPE_BOOLEAN:
5121
+ return "boolean";
5122
+ case TYPE_EXPREF:
5123
+ return "expref";
5124
+ case TYPE_NULL:
5125
+ return "null";
5609
5126
  }
5610
- return collected;
5611
- }
5612
- case "MultiSelectHash": {
5613
- const collected = {};
5614
- for (const child of node.children) {
5615
- collected[child.name] = this.visit(child.value, value);
5127
+ },
5128
+ _functionKeys: function(resolvedArgs) {
5129
+ return Object.keys(resolvedArgs[0]);
5130
+ },
5131
+ _functionValues: function(resolvedArgs) {
5132
+ var obj = resolvedArgs[0];
5133
+ var keys = Object.keys(obj);
5134
+ var values = [];
5135
+ for (var i = 0;i < keys.length; i++) {
5136
+ values.push(obj[keys[i]]);
5137
+ }
5138
+ return values;
5139
+ },
5140
+ _functionJoin: function(resolvedArgs) {
5141
+ var joinChar = resolvedArgs[0];
5142
+ var listJoin = resolvedArgs[1];
5143
+ return listJoin.join(joinChar);
5144
+ },
5145
+ _functionToArray: function(resolvedArgs) {
5146
+ if (this._getTypeName(resolvedArgs[0]) === TYPE_ARRAY) {
5147
+ return resolvedArgs[0];
5148
+ } else {
5149
+ return [resolvedArgs[0]];
5616
5150
  }
5617
- return collected;
5618
- }
5619
- case "OrExpression": {
5620
- const result = this.visit(node.left, value);
5621
- if (isFalse(result)) {
5622
- return this.visit(node.right, value);
5151
+ },
5152
+ _functionToString: function(resolvedArgs) {
5153
+ if (this._getTypeName(resolvedArgs[0]) === TYPE_STRING) {
5154
+ return resolvedArgs[0];
5155
+ } else {
5156
+ return JSON.stringify(resolvedArgs[0]);
5623
5157
  }
5624
- return result;
5625
- }
5626
- case "AndExpression": {
5627
- const result = this.visit(node.left, value);
5628
- if (isFalse(result)) {
5629
- return result;
5158
+ },
5159
+ _functionToNumber: function(resolvedArgs) {
5160
+ var typeName = this._getTypeName(resolvedArgs[0]);
5161
+ var convertedValue;
5162
+ if (typeName === TYPE_NUMBER) {
5163
+ return resolvedArgs[0];
5164
+ } else if (typeName === TYPE_STRING) {
5165
+ convertedValue = +resolvedArgs[0];
5166
+ if (!isNaN(convertedValue)) {
5167
+ return convertedValue;
5168
+ }
5630
5169
  }
5631
- return this.visit(node.right, value);
5632
- }
5633
- case "NotExpression":
5634
- return isFalse(this.visit(node.child, value));
5635
- case "Literal":
5636
- return node.value;
5637
- case "Pipe":
5638
- return this.visit(node.right, this.visit(node.left, value));
5639
- case "Function": {
5640
- const args = [];
5641
- for (const child of node.children) {
5642
- args.push(this.visit(child, value));
5170
+ return null;
5171
+ },
5172
+ _functionNotNull: function(resolvedArgs) {
5173
+ for (var i = 0;i < resolvedArgs.length; i++) {
5174
+ if (this._getTypeName(resolvedArgs[i]) !== TYPE_NULL) {
5175
+ return resolvedArgs[i];
5176
+ }
5643
5177
  }
5644
- return this.runtime.callFunction(node.name, args);
5645
- }
5646
- case "ExpressionReference":
5647
- return {
5648
- expref: true,
5649
- ...node.child
5178
+ return null;
5179
+ },
5180
+ _functionSort: function(resolvedArgs) {
5181
+ var sortedArray = resolvedArgs[0].slice(0);
5182
+ sortedArray.sort();
5183
+ return sortedArray;
5184
+ },
5185
+ _functionSortBy: function(resolvedArgs) {
5186
+ var sortedArray = resolvedArgs[0].slice(0);
5187
+ if (sortedArray.length === 0) {
5188
+ return sortedArray;
5189
+ }
5190
+ var interpreter = this._interpreter;
5191
+ var exprefNode = resolvedArgs[1];
5192
+ var requiredType = this._getTypeName(interpreter.visit(exprefNode, sortedArray[0]));
5193
+ if ([TYPE_NUMBER, TYPE_STRING].indexOf(requiredType) < 0) {
5194
+ throw new Error("TypeError");
5195
+ }
5196
+ var that = this;
5197
+ var decorated = [];
5198
+ for (var i = 0;i < sortedArray.length; i++) {
5199
+ decorated.push([i, sortedArray[i]]);
5200
+ }
5201
+ decorated.sort(function(a, b) {
5202
+ var exprA = interpreter.visit(exprefNode, a[1]);
5203
+ var exprB = interpreter.visit(exprefNode, b[1]);
5204
+ if (that._getTypeName(exprA) !== requiredType) {
5205
+ throw new Error("TypeError: expected " + requiredType + ", received " + that._getTypeName(exprA));
5206
+ } else if (that._getTypeName(exprB) !== requiredType) {
5207
+ throw new Error("TypeError: expected " + requiredType + ", received " + that._getTypeName(exprB));
5208
+ }
5209
+ if (exprA > exprB) {
5210
+ return 1;
5211
+ } else if (exprA < exprB) {
5212
+ return -1;
5213
+ } else {
5214
+ return a[0] - b[0];
5215
+ }
5216
+ });
5217
+ for (var j = 0;j < decorated.length; j++) {
5218
+ sortedArray[j] = decorated[j][1];
5219
+ }
5220
+ return sortedArray;
5221
+ },
5222
+ _functionMaxBy: function(resolvedArgs) {
5223
+ var exprefNode = resolvedArgs[1];
5224
+ var resolvedArray = resolvedArgs[0];
5225
+ var keyFunction = this.createKeyFunction(exprefNode, [TYPE_NUMBER, TYPE_STRING]);
5226
+ var maxNumber = -Infinity;
5227
+ var maxRecord;
5228
+ var current;
5229
+ for (var i = 0;i < resolvedArray.length; i++) {
5230
+ current = keyFunction(resolvedArray[i]);
5231
+ if (current > maxNumber) {
5232
+ maxNumber = current;
5233
+ maxRecord = resolvedArray[i];
5234
+ }
5235
+ }
5236
+ return maxRecord;
5237
+ },
5238
+ _functionMinBy: function(resolvedArgs) {
5239
+ var exprefNode = resolvedArgs[1];
5240
+ var resolvedArray = resolvedArgs[0];
5241
+ var keyFunction = this.createKeyFunction(exprefNode, [TYPE_NUMBER, TYPE_STRING]);
5242
+ var minNumber = Infinity;
5243
+ var minRecord;
5244
+ var current;
5245
+ for (var i = 0;i < resolvedArray.length; i++) {
5246
+ current = keyFunction(resolvedArray[i]);
5247
+ if (current < minNumber) {
5248
+ minNumber = current;
5249
+ minRecord = resolvedArray[i];
5250
+ }
5251
+ }
5252
+ return minRecord;
5253
+ },
5254
+ createKeyFunction: function(exprefNode, allowedTypes) {
5255
+ var that = this;
5256
+ var interpreter = this._interpreter;
5257
+ var keyFunc = function(x) {
5258
+ var current = interpreter.visit(exprefNode, x);
5259
+ if (allowedTypes.indexOf(that._getTypeName(current)) < 0) {
5260
+ var msg = "TypeError: expected one of " + allowedTypes + ", received " + that._getTypeName(current);
5261
+ throw new Error(msg);
5262
+ }
5263
+ return current;
5650
5264
  };
5651
- case "Current":
5652
- case "Identity":
5653
- return value;
5654
- }
5655
- }
5656
- computeSliceParams(arrayLength, sliceNode) {
5657
- let { start, stop, step } = sliceNode;
5658
- if (step === null) {
5659
- step = 1;
5660
- } else if (step === 0) {
5661
- const error = new Error("Invalid value: slice step cannot be 0");
5662
- error.name = "RuntimeError";
5663
- throw error;
5664
- }
5665
- start = start === null ? step < 0 ? arrayLength - 1 : 0 : this.capSliceRange(arrayLength, start, step);
5666
- stop = stop === null ? step < 0 ? -1 : arrayLength : this.capSliceRange(arrayLength, stop, step);
5667
- return { start, stop, step };
5668
- }
5669
- capSliceRange(arrayLength, actualValue, step) {
5670
- let nextActualValue = actualValue;
5671
- if (nextActualValue < 0) {
5672
- nextActualValue += arrayLength;
5673
- if (nextActualValue < 0) {
5674
- nextActualValue = step < 0 ? -1 : 0;
5675
- }
5676
- } else if (nextActualValue >= arrayLength) {
5677
- nextActualValue = step < 0 ? arrayLength - 1 : arrayLength;
5678
- }
5679
- return nextActualValue;
5680
- }
5681
- slice(collection, start, end, step) {
5682
- const result = [];
5683
- if (step > 0) {
5684
- for (let i = start;i < end; i += step) {
5685
- result.push(collection[i]);
5686
- }
5687
- } else {
5688
- for (let i = start;i > end; i += step) {
5689
- result.push(collection[i]);
5265
+ return keyFunc;
5690
5266
  }
5691
- }
5692
- return result;
5693
- }
5694
- }, 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) => {
5695
- TreeInterpreter_default.runtime.registerFunction(functionName, customFunction, signature, options);
5696
- }, register = (name, customFunction, signature, options) => {
5697
- return TreeInterpreter_default.runtime.register(name, customFunction, signature, options);
5698
- }, unregisterFunction = (name) => {
5699
- return TreeInterpreter_default.runtime.unregister(name);
5700
- }, isRegistered = (name) => {
5701
- return TreeInterpreter_default.runtime.isRegistered(name);
5702
- }, getRegisteredFunctions = () => {
5703
- return TreeInterpreter_default.runtime.getRegistered();
5704
- }, getCustomFunctions = () => {
5705
- return TreeInterpreter_default.runtime.getCustomFunctions();
5706
- }, clearCustomFunctions = () => {
5707
- TreeInterpreter_default.runtime.clearCustomFunctions();
5708
- }, TreeInterpreter2, jmespath;
5709
- var init_dist = __esm(() => {
5710
- basicTokens = {
5711
- "(": "Lparen",
5712
- ")": "Rparen",
5713
- "*": "Star",
5714
- ",": "Comma",
5715
- ".": "Dot",
5716
- ":": "Colon",
5717
- "@": "Current",
5718
- "]": "Rbracket",
5719
- "{": "Lbrace",
5720
- "}": "Rbrace",
5721
- "+": "Plus",
5722
- "%": "Modulo",
5723
- "?": "Question",
5724
- "−": "Minus",
5725
- "×": "Multiply",
5726
- "÷": "Divide"
5727
- };
5728
- operatorStartToken = {
5729
- "!": true,
5730
- "<": true,
5731
- "=": true,
5732
- ">": true,
5733
- "&": true,
5734
- "|": true,
5735
- "/": true
5736
- };
5737
- skipChars = {
5738
- "\t": true,
5739
- "\n": true,
5740
- "\r": true,
5741
- " ": true
5742
- };
5743
- Lexer = new StreamLexer;
5744
- Lexer_default = Lexer;
5745
- bindingPower = {
5746
- ["EOF"]: 0,
5747
- ["Variable"]: 0,
5748
- ["UnquotedIdentifier"]: 0,
5749
- ["QuotedIdentifier"]: 0,
5750
- ["Rbracket"]: 0,
5751
- ["Rparen"]: 0,
5752
- ["Comma"]: 0,
5753
- ["Rbrace"]: 0,
5754
- ["Number"]: 0,
5755
- ["Current"]: 0,
5756
- ["Expref"]: 0,
5757
- ["Root"]: 0,
5758
- ["Assign"]: 1,
5759
- ["Pipe"]: 1,
5760
- ["Question"]: 2,
5761
- ["Or"]: 3,
5762
- ["And"]: 4,
5763
- ["EQ"]: 5,
5764
- ["GT"]: 5,
5765
- ["LT"]: 5,
5766
- ["GTE"]: 5,
5767
- ["LTE"]: 5,
5768
- ["NE"]: 5,
5769
- ["Minus"]: 6,
5770
- ["Plus"]: 6,
5771
- ["Div"]: 7,
5772
- ["Divide"]: 7,
5773
- ["Modulo"]: 7,
5774
- ["Multiply"]: 7,
5775
- ["Flatten"]: 9,
5776
- ["Star"]: 20,
5777
- ["Filter"]: 21,
5778
- ["Dot"]: 40,
5779
- ["Not"]: 45,
5780
- ["Lbrace"]: 50,
5781
- ["Lbracket"]: 55,
5782
- ["Lparen"]: 60
5783
- };
5784
- Parser = new TokenParser;
5785
- Parser_default = Parser;
5786
- emptyScopeChain = new ScopeChain;
5787
- TreeInterpreterInstance = new TreeInterpreter;
5788
- TreeInterpreter_default = TreeInterpreterInstance;
5789
- TreeInterpreter2 = TreeInterpreter_default;
5790
- jmespath = {
5791
- compile,
5792
- registerFunction,
5793
- register,
5794
- unregisterFunction,
5795
- isRegistered,
5796
- getRegisteredFunctions,
5797
- getCustomFunctions,
5798
- clearCustomFunctions,
5799
- search,
5800
- tokenize,
5801
- TreeInterpreter: TreeInterpreter2,
5802
- TYPE_ANY,
5803
- TYPE_ARRAY_NUMBER,
5804
- TYPE_ARRAY_STRING,
5805
- TYPE_ARRAY,
5806
- TYPE_BOOLEAN,
5807
- TYPE_EXPREF,
5808
- TYPE_NULL,
5809
- TYPE_NUMBER,
5810
- TYPE_OBJECT,
5811
- TYPE_STRING
5812
- };
5267
+ };
5268
+ function compile(stream) {
5269
+ var parser = new Parser;
5270
+ var ast = parser.parse(stream);
5271
+ return ast;
5272
+ }
5273
+ function tokenize(stream) {
5274
+ var lexer = new Lexer;
5275
+ return lexer.tokenize(stream);
5276
+ }
5277
+ function search(data, expression) {
5278
+ var parser = new Parser;
5279
+ var runtime = new Runtime;
5280
+ var interpreter = new TreeInterpreter(runtime);
5281
+ runtime._interpreter = interpreter;
5282
+ var node = parser.parse(expression);
5283
+ return interpreter.search(node, data);
5284
+ }
5285
+ exports2.tokenize = tokenize;
5286
+ exports2.compile = compile;
5287
+ exports2.search = search;
5288
+ exports2.strictDeepEqual = strictDeepEqual;
5289
+ })(typeof exports === "undefined" ? exports.jmespath = {} : exports);
5813
5290
  });
5814
5291
 
5815
5292
  // ../../node_modules/commander/lib/error.js
@@ -6138,10 +5615,10 @@ var require_help = __commonJS(function(exports) {
6138
5615
  });
6139
5616
  output = output.concat(this.formatItemList("Global Options:", globalOptionList, helper));
6140
5617
  }
6141
- const commandGroups = this.groupItems(cmd.commands, helper.visibleCommands(cmd), (sub2) => sub2.helpGroup() || "Commands:");
5618
+ const commandGroups = this.groupItems(cmd.commands, helper.visibleCommands(cmd), (sub) => sub.helpGroup() || "Commands:");
6142
5619
  commandGroups.forEach((commands, group) => {
6143
- const commandList = commands.map((sub2) => {
6144
- return callFormatItem(helper.styleSubcommandTerm(helper.subcommandTerm(sub2)), helper.styleSubcommandDescription(helper.subcommandDescription(sub2)));
5620
+ const commandList = commands.map((sub) => {
5621
+ return callFormatItem(helper.styleSubcommandTerm(helper.subcommandTerm(sub)), helper.styleSubcommandDescription(helper.subcommandDescription(sub)));
6145
5622
  });
6146
5623
  output = output.concat(this.formatItemList(group, commandList, helper));
6147
5624
  });
@@ -9424,13 +8901,13 @@ var jmespathSlot = singleton("JmespathCodec");
9424
8901
  async function loadOutputCodecsAsync(needed) {
9425
8902
  const loads = [];
9426
8903
  if (needed.yaml && yamlSlot.get() === undefined) {
9427
- loads.push(Promise.resolve().then(() => (init_js_yaml(), exports_js_yaml)).then((mod2) => {
9428
- yamlSlot.set(mod2);
8904
+ loads.push(Promise.resolve().then(() => (init_js_yaml(), exports_js_yaml)).then((mod) => {
8905
+ yamlSlot.set(mod);
9429
8906
  }));
9430
8907
  }
9431
8908
  if (needed.filter && jmespathSlot.get() === undefined) {
9432
- loads.push(Promise.resolve().then(() => (init_dist(), exports_dist)).then((mod2) => {
9433
- jmespathSlot.set(mod2);
8909
+ loads.push(Promise.resolve().then(() => __toESM(require_jmespath(), 1)).then((mod) => {
8910
+ jmespathSlot.set(mod);
9434
8911
  }));
9435
8912
  }
9436
8913
  await Promise.all(loads);
@@ -10325,8 +9802,8 @@ class TelemetryService {
10325
9802
  var TIMINGS_ENV_VAR = "UIP_TIMINGS";
10326
9803
  var TIMINGS_LINE_PREFIX = "[timing]";
10327
9804
  function createStorage2() {
10328
- const [error, mod2] = catchError(() => __require("node:async_hooks"));
10329
- if (error || typeof mod2?.AsyncLocalStorage !== "function") {
9805
+ const [error, mod] = catchError(() => __require("node:async_hooks"));
9806
+ if (error || typeof mod?.AsyncLocalStorage !== "function") {
10330
9807
  return {
10331
9808
  getStore: () => {
10332
9809
  return;
@@ -10334,7 +9811,7 @@ function createStorage2() {
10334
9811
  run: (_store, fn) => fn()
10335
9812
  };
10336
9813
  }
10337
- return new mod2.AsyncLocalStorage;
9814
+ return new mod.AsyncLocalStorage;
10338
9815
  }
10339
9816
  var storageSlot = singleton("TimingStorage");
10340
9817
  var stateSlot = singleton("Timings");
@@ -10678,7 +10155,7 @@ async function loadApplicationInsights() {
10678
10155
  const savedDebug = process.env.DEBUG;
10679
10156
  delete process.env.DEBUG;
10680
10157
  configureAppInsightsEnvironment();
10681
- const [error, mod2] = await catchError(import("applicationinsights"));
10158
+ const [error, mod] = await catchError(import("applicationinsights"));
10682
10159
  if (savedDebug !== undefined) {
10683
10160
  process.env.DEBUG = savedDebug;
10684
10161
  }
@@ -10686,7 +10163,7 @@ async function loadApplicationInsights() {
10686
10163
  logger.warn(`[Telemetry] applicationinsights package not available: ${error.message}. Telemetry will use logger fallback.`);
10687
10164
  return;
10688
10165
  }
10689
- return mod2;
10166
+ return mod;
10690
10167
  }
10691
10168
  function configureAppInsightsClient(client) {
10692
10169
  if (client.config) {
@@ -10720,12 +10197,12 @@ class NodeAppInsightsTelemetryProvider {
10720
10197
  async initialize() {
10721
10198
  if (this.initialized)
10722
10199
  return true;
10723
- const mod2 = await loadApplicationInsights();
10724
- if (!mod2)
10200
+ const mod = await loadApplicationInsights();
10201
+ if (!mod)
10725
10202
  return false;
10726
- this.appInsightsModule = mod2;
10203
+ this.appInsightsModule = mod;
10727
10204
  logger.debug("[AppInsights] NodeAppInsightsTelemetryProvider initialized");
10728
- const client = new mod2.TelemetryClient(this.connectionString);
10205
+ const client = new mod.TelemetryClient(this.connectionString);
10729
10206
  client.context.tags[client.context.keys.sessionId] = this._sessionId;
10730
10207
  this.client = client;
10731
10208
  configureAppInsightsClient(this.client);
@@ -11995,9 +11472,9 @@ function setPreviewBuild(isPreview) {
11995
11472
  function isPreviewBuild() {
11996
11473
  return previewSlot.get(false) ?? false;
11997
11474
  }
11998
- function previewOnly(register2) {
11475
+ function previewOnly(register) {
11999
11476
  if (isPreviewBuild()) {
12000
- register2();
11477
+ register();
12001
11478
  }
12002
11479
  }
12003
11480
  Command.prototype.previewCommand = function(nameAndArgs, opts) {
@@ -12500,9 +11977,9 @@ function collectCommands(command, parentName, result) {
12500
11977
  if (subcommands.length === 0 && !hasAction && parentName) {
12501
11978
  result.push({ command, fullName: parentName });
12502
11979
  }
12503
- for (const sub2 of subcommands) {
12504
- const name = parentName ? `${parentName} ${sub2.name()}` : sub2.name();
12505
- collectCommands(sub2, name, result);
11980
+ for (const sub of subcommands) {
11981
+ const name = parentName ? `${parentName} ${sub.name()}` : sub.name();
11982
+ collectCommands(sub, name, result);
12506
11983
  }
12507
11984
  }
12508
11985
  // src/completer.ts
@@ -13178,16 +12655,16 @@ function mapPackageMetadataOptions(opts) {
13178
12655
  // src/packager-tool-import.ts
13179
12656
  var REGISTER_EXPORT = "registerPackagerFactories";
13180
12657
  async function importPackagerTool(specifier) {
13181
- const [importError, mod2] = await catchError(import(specifier));
12658
+ const [importError, mod] = await catchError(import(specifier));
13182
12659
  if (importError)
13183
12660
  return importError;
13184
- const register2 = mod2?.[REGISTER_EXPORT];
13185
- if (typeof register2 !== "function") {
12661
+ const register = mod?.[REGISTER_EXPORT];
12662
+ if (typeof register !== "function") {
13186
12663
  logger.debug(`'${specifier}' has no '${REGISTER_EXPORT}' export — treating it as a ` + "legacy entry point that registered its factories at import.");
13187
12664
  return;
13188
12665
  }
13189
12666
  const [registerError] = catchError(() => {
13190
- register2();
12667
+ register();
13191
12668
  });
13192
12669
  return registerError;
13193
12670
  }
@@ -14207,4 +13684,4 @@ export {
14207
13684
  writeTelemetrySpoolFile
14208
13685
  };
14209
13686
 
14210
- //# debugId=39D73B2CDA6F6AF764756E2164756E21
13687
+ //# debugId=E388413E446272F364756E2164756E21