@jetstreamapp/soql-parser-js 6.0.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.
Files changed (50) hide show
  1. package/AUTHORS.md +1 -0
  2. package/CHANGELOG.md +959 -0
  3. package/LICENSE.txt +12 -0
  4. package/README.md +949 -0
  5. package/bin/soql-parser-js +3 -0
  6. package/dist/cjs/index.js +2 -0
  7. package/dist/cjs/index.js.LICENSE.txt +1 -0
  8. package/dist/cjs/main.licenses.txt +898 -0
  9. package/dist/cjs/src/api/api-models.d.ts +175 -0
  10. package/dist/cjs/src/api/api-models.js +2 -0
  11. package/dist/cjs/src/api/public-utils.d.ts +29 -0
  12. package/dist/cjs/src/api/public-utils.js +175 -0
  13. package/dist/cjs/src/composer/composer.d.ts +32 -0
  14. package/dist/cjs/src/composer/composer.js +295 -0
  15. package/dist/cjs/src/formatter/formatter.d.ts +42 -0
  16. package/dist/cjs/src/formatter/formatter.js +236 -0
  17. package/dist/cjs/src/index.d.ts +5 -0
  18. package/dist/cjs/src/index.js +23 -0
  19. package/dist/cjs/src/models.d.ts +266 -0
  20. package/dist/cjs/src/models.js +2 -0
  21. package/dist/cjs/src/parser/lexer.d.ts +186 -0
  22. package/dist/cjs/src/parser/lexer.js +954 -0
  23. package/dist/cjs/src/parser/parser.d.ts +89 -0
  24. package/dist/cjs/src/parser/parser.js +697 -0
  25. package/dist/cjs/src/parser/visitor.d.ts +4 -0
  26. package/dist/cjs/src/parser/visitor.js +856 -0
  27. package/dist/cjs/src/utils.d.ts +42 -0
  28. package/dist/cjs/src/utils.js +245 -0
  29. package/dist/cli/index.js +113 -0
  30. package/dist/esm/api/api-models.js +1 -0
  31. package/dist/esm/api/public-utils.js +153 -0
  32. package/dist/esm/composer/composer.js +273 -0
  33. package/dist/esm/formatter/formatter.js +223 -0
  34. package/dist/esm/index.js +4 -0
  35. package/dist/esm/models.js +1 -0
  36. package/dist/esm/parser/lexer.js +947 -0
  37. package/dist/esm/parser/parser.js +667 -0
  38. package/dist/esm/parser/visitor.js +823 -0
  39. package/dist/esm/utils.js +200 -0
  40. package/dist/src/api/api-models.js +2 -0
  41. package/dist/src/api/public-utils.js +203 -0
  42. package/dist/src/composer/composer.js +358 -0
  43. package/dist/src/formatter/formatter.js +263 -0
  44. package/dist/src/index.js +28 -0
  45. package/dist/src/models.js +2 -0
  46. package/dist/src/parser/lexer.js +997 -0
  47. package/dist/src/parser/parser.js +711 -0
  48. package/dist/src/parser/visitor.js +862 -0
  49. package/dist/src/utils.js +251 -0
  50. package/package.json +78 -0
@@ -0,0 +1,711 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parse = exports.SoqlParser = void 0;
4
+ const chevrotain_1 = require("chevrotain");
5
+ const lexer = require("./lexer");
6
+ class LexingError extends Error {
7
+ constructor(lexingError) {
8
+ super(`${lexingError.message} (${lexingError.line}:${lexingError.column})`);
9
+ this.name = 'LexingError';
10
+ }
11
+ }
12
+ class ParsingError extends Error {
13
+ constructor(parsingError) {
14
+ super(parsingError.message);
15
+ this.name = parsingError.name;
16
+ }
17
+ }
18
+ class SoqlParser extends chevrotain_1.CstParser {
19
+ // Cache larger OR expressions
20
+ // https://sap.github.io/chevrotain/docs/guide/performance.html#caching-arrays-of-alternatives
21
+ $_dateFunctionOr = undefined;
22
+ $_aggregateFunction = undefined;
23
+ $_otherFunction = undefined;
24
+ $_atomicExpression = undefined;
25
+ $_apexBindVariableExpression = undefined;
26
+ $_arrayExpression = undefined;
27
+ $_relationalOperator = undefined;
28
+ $_selectClause = undefined;
29
+ $_selectClauseFunctionIdentifier = undefined;
30
+ $_withDataCategoryArr = undefined;
31
+ // Set to true to allow apex bind variables, such as "WHERE Id IN :accountIds"
32
+ allowApexBindVariables = false;
33
+ ignoreParseErrors = false;
34
+ constructor({ ignoreParseErrors } = { ignoreParseErrors: false }) {
35
+ super(lexer.allTokens, {
36
+ // true in production (webpack replaces this string)
37
+ skipValidations: false,
38
+ recoveryEnabled: !!ignoreParseErrors,
39
+ // nodeLocationTracking: 'full', // not sure if needed, could look at
40
+ });
41
+ this.ignoreParseErrors = !!ignoreParseErrors;
42
+ this.performSelfAnalysis();
43
+ }
44
+ selectStatement = this.RULE('selectStatement', () => {
45
+ this.SUBRULE(this.selectClause);
46
+ this.SUBRULE(this.fromClause);
47
+ this.SUBRULE(this.clauseStatements);
48
+ });
49
+ selectStatementPartial = this.RULE('selectStatementPartial', () => {
50
+ this.OPTION(() => {
51
+ this.SUBRULE(this.selectClause);
52
+ });
53
+ this.OPTION1(() => {
54
+ this.SUBRULE(this.fromClause);
55
+ });
56
+ this.SUBRULE(this.clauseStatements);
57
+ });
58
+ clauseStatements = this.RULE('clauseStatements', () => {
59
+ this.OPTION(() => {
60
+ this.SUBRULE(this.usingScopeClause);
61
+ });
62
+ this.OPTION1(() => {
63
+ this.SUBRULE(this.whereClause);
64
+ });
65
+ this.OPTION2(() => {
66
+ this.MANY({
67
+ DEF: () => {
68
+ this.SUBRULE(this.withClause);
69
+ },
70
+ });
71
+ });
72
+ this.OPTION3(() => {
73
+ this.SUBRULE(this.groupByClause);
74
+ this.OPTION4(() => {
75
+ this.SUBRULE(this.havingClause);
76
+ });
77
+ });
78
+ this.OPTION5(() => {
79
+ this.SUBRULE(this.orderByClause);
80
+ });
81
+ this.OPTION6(() => {
82
+ this.SUBRULE(this.limitClause);
83
+ });
84
+ this.OPTION7(() => {
85
+ this.SUBRULE(this.offsetClause);
86
+ });
87
+ this.OPTION8(() => {
88
+ this.SUBRULE(this.forViewOrReference);
89
+ });
90
+ this.OPTION9(() => {
91
+ this.SUBRULE(this.updateTrackingViewstat);
92
+ });
93
+ });
94
+ // TODO: this is a hack and the paren expressions should be re-worked, but it is a significant amount of work and difficult to handle in the visitor
95
+ // If There are more open parens than closing parens, force parsing error - RParenMismatch is not a valid token and does not have a pattern
96
+ // Because we are not actually doing any calculations with results, our strategy for calculating parens within an expression is semi-ok
97
+ // but should be considered for a refactor at some point
98
+ /**
99
+ * Will throw a parsing error if there is a paren mismatch
100
+ * @param parenCount
101
+ */
102
+ $_checkBalancedParens(parenCount) {
103
+ if (!this.RECORDING_PHASE && parenCount) {
104
+ const parenMatch = parenCount.left - parenCount.right;
105
+ if (parenMatch !== 0) {
106
+ this.CONSUME(lexer.RParenMismatch, { ERR_MSG: `Expecting a token type of --> RParen <-- but found --> '' <--` });
107
+ }
108
+ }
109
+ }
110
+ selectClause = this.RULE('selectClause', () => {
111
+ this.CONSUME(lexer.Select);
112
+ this.AT_LEAST_ONE_SEP({
113
+ SEP: lexer.Comma,
114
+ DEF: () => {
115
+ this.OR(this.$_selectClause ||
116
+ (this.$_selectClause = [
117
+ // selectClauseFunctionIdentifier must be first because the alias could also be an identifier
118
+ { ALT: () => this.SUBRULE(this.selectClauseFunctionIdentifier, { LABEL: 'field' }) },
119
+ { ALT: () => this.SUBRULE(this.selectClauseSubqueryIdentifier, { LABEL: 'field' }) },
120
+ { ALT: () => this.SUBRULE(this.selectClauseTypeOf, { LABEL: 'field' }) },
121
+ { ALT: () => this.SUBRULE(this.selectClauseIdentifier, { LABEL: 'field' }) },
122
+ ]));
123
+ },
124
+ });
125
+ }, { resyncEnabled: false });
126
+ selectClauseFunctionIdentifier = this.RULE('selectClauseFunctionIdentifier', () => {
127
+ this.OR(this.$_selectClauseFunctionIdentifier ||
128
+ (this.$_selectClauseFunctionIdentifier = [
129
+ { ALT: () => this.SUBRULE(this.dateFunction, { LABEL: 'fn' }) },
130
+ { ALT: () => this.SUBRULE(this.aggregateFunction, { LABEL: 'fn' }) },
131
+ { ALT: () => this.SUBRULE(this.locationFunction, { LABEL: 'fn' }) },
132
+ { ALT: () => this.SUBRULE(this.fieldsFunction, { LABEL: 'fn' }) },
133
+ { ALT: () => this.SUBRULE(this.otherFunction, { LABEL: 'fn' }) },
134
+ ]));
135
+ this.OPTION(() => this.CONSUME(lexer.Identifier, { LABEL: 'alias' }));
136
+ }, { resyncEnabled: false });
137
+ selectClauseSubqueryIdentifier = this.RULE('selectClauseSubqueryIdentifier', () => {
138
+ this.CONSUME(lexer.LParen);
139
+ this.SUBRULE(this.selectStatement);
140
+ this.CONSUME(lexer.RParen);
141
+ }, { resyncEnabled: false });
142
+ selectClauseTypeOf = this.RULE('selectClauseTypeOf', () => {
143
+ this.CONSUME(lexer.Typeof);
144
+ this.CONSUME(lexer.Identifier, { LABEL: 'typeOfField' });
145
+ this.AT_LEAST_ONE({
146
+ DEF: () => {
147
+ this.SUBRULE(this.selectClauseTypeOfThen);
148
+ },
149
+ });
150
+ this.OPTION(() => {
151
+ this.SUBRULE(this.selectClauseTypeOfElse);
152
+ });
153
+ this.CONSUME(lexer.End);
154
+ }, { resyncEnabled: false });
155
+ selectClauseIdentifier = this.RULE('selectClauseIdentifier', () => {
156
+ this.CONSUME(lexer.Identifier, { LABEL: 'field' });
157
+ this.OPTION(() => this.CONSUME1(lexer.Identifier, { LABEL: 'alias' }));
158
+ }, { resyncEnabled: false });
159
+ selectClauseTypeOfThen = this.RULE('selectClauseTypeOfThen', () => {
160
+ this.CONSUME(lexer.When);
161
+ this.CONSUME(lexer.Identifier, { LABEL: 'typeOfField' });
162
+ this.CONSUME(lexer.Then);
163
+ this.AT_LEAST_ONE_SEP({
164
+ SEP: lexer.Comma,
165
+ DEF: () => {
166
+ this.CONSUME1(lexer.Identifier, { LABEL: 'field' });
167
+ },
168
+ });
169
+ }, { resyncEnabled: false });
170
+ selectClauseTypeOfElse = this.RULE('selectClauseTypeOfElse', () => {
171
+ this.CONSUME(lexer.Else);
172
+ this.AT_LEAST_ONE_SEP({
173
+ SEP: lexer.Comma,
174
+ DEF: () => {
175
+ this.CONSUME(lexer.Identifier, { LABEL: 'field' });
176
+ },
177
+ });
178
+ }, { resyncEnabled: false });
179
+ fromClause = this.RULE('fromClause', () => {
180
+ this.CONSUME(lexer.From);
181
+ this.CONSUME(lexer.Identifier);
182
+ this.OPTION({
183
+ GATE: () => !(this.LA(1).tokenType === lexer.Offset && this.LA(2).tokenType === lexer.UnsignedInteger),
184
+ DEF: () => this.CONSUME1(lexer.Identifier, { LABEL: 'alias' }),
185
+ });
186
+ }, { resyncEnabled: false });
187
+ usingScopeClause = this.RULE('usingScopeClause', () => {
188
+ this.CONSUME(lexer.Using);
189
+ this.CONSUME(lexer.Scope);
190
+ this.CONSUME(lexer.UsingScopeEnumeration);
191
+ });
192
+ whereClause = this.RULE('whereClause', () => {
193
+ this.CONSUME(lexer.Where);
194
+ // Get paren count to ensure that we only parse balanced parens for this where clause
195
+ // and do not parse any extra parens that might belong to subquery or might be invalid
196
+ const parenCount = this.getParenCount();
197
+ this.AT_LEAST_ONE({
198
+ DEF: () => {
199
+ this.SUBRULE(this.conditionExpression, { ARGS: [parenCount, true, true] });
200
+ },
201
+ });
202
+ // TODO: this is a hack and the paren expressions should be re-worked, but it is a significant amount of work and difficult to handle in the visitor
203
+ // If There are more open parens than closing parens, force parsing error - RParenMismatch is not a valid token and does not have a pattern
204
+ // Because we are not actually doing any calculations with results, our strategy for calculating parens within an expression is semi-ok
205
+ // but should be considered for a refactor at some point
206
+ this.$_checkBalancedParens(parenCount);
207
+ });
208
+ whereClauseSubqueryIdentifier = this.RULE('whereClauseSubqueryIdentifier', () => {
209
+ this.CONSUME(lexer.LParen);
210
+ this.SUBRULE(this.selectStatement);
211
+ this.CONSUME(lexer.RParen);
212
+ });
213
+ conditionExpression = this.RULE('conditionExpression', (parenCount, allowAggregateFn, allowLocationFn) => {
214
+ // argument is undefined during self-analysis, need to initialize to avoid exception
215
+ parenCount = this.getParenCount(parenCount);
216
+ this.OPTION(() => {
217
+ this.OR([
218
+ { ALT: () => this.CONSUME(lexer.And, { LABEL: 'logicalOperator' }) },
219
+ { ALT: () => this.CONSUME(lexer.Or, { LABEL: 'logicalOperator' }) },
220
+ ]);
221
+ });
222
+ // MAX_LOOKAHEAD -> this is increased because an arbitrary number of parens could be used causing a parsing error
223
+ // this does not allow infinite parentheses, but is more than enough for any real use-cases
224
+ // Under no circumstances would large numbers of nested expressions not be expressible with fewer conditions
225
+ this.MANY({
226
+ MAX_LOOKAHEAD: 10,
227
+ DEF: () => this.SUBRULE(this.expressionPartWithNegation, { ARGS: [parenCount], LABEL: 'expressionNegation' }),
228
+ });
229
+ this.OR1({
230
+ MAX_LOOKAHEAD: 10,
231
+ DEF: [{ ALT: () => this.SUBRULE(this.expression, { ARGS: [parenCount, false, allowAggregateFn, allowLocationFn] }) }],
232
+ });
233
+ });
234
+ withClause = this.RULE('withClause', () => {
235
+ this.CONSUME(lexer.With);
236
+ this.OR([
237
+ { ALT: () => this.CONSUME(lexer.SecurityEnforced, { LABEL: 'withSecurityEnforced' }) },
238
+ { ALT: () => this.CONSUME(lexer.UserMode, { LABEL: 'withAccessLevel' }) },
239
+ { ALT: () => this.CONSUME(lexer.SystemMode, { LABEL: 'withAccessLevel' }) },
240
+ { ALT: () => this.SUBRULE(this.withDataCategory) },
241
+ ]);
242
+ });
243
+ withDataCategory = this.RULE('withDataCategory', () => {
244
+ this.CONSUME(lexer.DataCategory);
245
+ this.AT_LEAST_ONE_SEP({
246
+ SEP: lexer.And,
247
+ DEF: () => {
248
+ this.SUBRULE(this.withDataCategoryArr);
249
+ },
250
+ });
251
+ });
252
+ withDataCategoryArr = this.RULE('withDataCategoryArr', () => {
253
+ this.CONSUME(lexer.Identifier, { LABEL: 'dataCategoryGroupName' });
254
+ this.OR(this.$_withDataCategoryArr ||
255
+ (this.$_withDataCategoryArr = [
256
+ { ALT: () => this.CONSUME(lexer.At, { LABEL: 'filteringSelector' }) },
257
+ { ALT: () => this.CONSUME(lexer.Above, { LABEL: 'filteringSelector' }) },
258
+ { ALT: () => this.CONSUME(lexer.Below, { LABEL: 'filteringSelector' }) },
259
+ { ALT: () => this.CONSUME(lexer.AboveOrBelow, { LABEL: 'filteringSelector' }) },
260
+ ]));
261
+ this.OPTION(() => {
262
+ this.CONSUME1(lexer.LParen);
263
+ });
264
+ this.AT_LEAST_ONE_SEP1({
265
+ SEP: lexer.Comma,
266
+ DEF: () => {
267
+ this.CONSUME1(lexer.Identifier, { LABEL: 'dataCategoryName' });
268
+ },
269
+ });
270
+ this.OPTION1(() => {
271
+ this.CONSUME2(lexer.RParen);
272
+ });
273
+ });
274
+ groupByClause = this.RULE('groupByClause', () => {
275
+ this.CONSUME(lexer.GroupBy);
276
+ this.AT_LEAST_ONE_SEP({
277
+ SEP: lexer.Comma,
278
+ DEF: () => {
279
+ this.OR([
280
+ { ALT: () => this.SUBRULE(this.cubeFunction, { LABEL: 'groupBy' }) },
281
+ { ALT: () => this.SUBRULE(this.rollupFunction, { LABEL: 'groupBy' }) },
282
+ { ALT: () => this.SUBRULE(this.dateFunction, { LABEL: 'groupBy' }) },
283
+ { ALT: () => this.CONSUME(lexer.Identifier, { LABEL: 'groupBy' }) },
284
+ ]);
285
+ },
286
+ });
287
+ });
288
+ havingClause = this.RULE('havingClause', () => {
289
+ this.CONSUME(lexer.Having);
290
+ const parenCount = this.getParenCount();
291
+ this.AT_LEAST_ONE({
292
+ DEF: () => {
293
+ this.SUBRULE(this.conditionExpression, { ARGS: [parenCount, true, false] });
294
+ },
295
+ });
296
+ // TODO: this is a hack and the paren expressions should be re-worked, but it is a significant amount of work and difficult to handle in the visitor
297
+ // If There are more open parens than closing parens, force parsing error - RParenMismatch is not a valid token and does not have a pattern
298
+ // Because we are not actually doing any calculations with results, our strategy for calculating parens within an expression is semi-ok
299
+ // but should be considered for a refactor at some point
300
+ this.$_checkBalancedParens(parenCount);
301
+ });
302
+ orderByClause = this.RULE('orderByClause', () => {
303
+ this.CONSUME(lexer.OrderBy);
304
+ this.AT_LEAST_ONE_SEP({
305
+ SEP: lexer.Comma,
306
+ DEF: () => {
307
+ this.OR([
308
+ { ALT: () => this.SUBRULE(this.orderByGroupingFunctionExpression, { LABEL: 'orderByExpressionOrFn' }) },
309
+ { ALT: () => this.SUBRULE(this.orderBySpecialFunctionExpression, { LABEL: 'orderByExpressionOrFn' }) },
310
+ { ALT: () => this.SUBRULE(this.orderByExpression, { LABEL: 'orderByExpressionOrFn' }) },
311
+ ]);
312
+ },
313
+ });
314
+ });
315
+ orderByExpression = this.RULE('orderByExpression', () => {
316
+ this.CONSUME(lexer.Identifier);
317
+ this.OPTION(() => {
318
+ this.OR([{ ALT: () => this.CONSUME(lexer.Asc, { LABEL: 'order' }) }, { ALT: () => this.CONSUME(lexer.Desc, { LABEL: 'order' }) }]);
319
+ });
320
+ this.OPTION1(() => {
321
+ this.CONSUME(lexer.Nulls);
322
+ this.OR1([{ ALT: () => this.CONSUME(lexer.First, { LABEL: 'nulls' }) }, { ALT: () => this.CONSUME(lexer.Last, { LABEL: 'nulls' }) }]);
323
+ });
324
+ });
325
+ orderByGroupingFunctionExpression = this.RULE('orderByGroupingFunctionExpression', () => {
326
+ this.CONSUME(lexer.Grouping, { LABEL: 'fn' });
327
+ this.SUBRULE(this.functionExpression);
328
+ });
329
+ orderBySpecialFunctionExpression = this.RULE('orderBySpecialFunctionExpression', () => {
330
+ this.OR([
331
+ { ALT: () => this.SUBRULE(this.aggregateFunction) },
332
+ { ALT: () => this.SUBRULE(this.dateFunction) },
333
+ { ALT: () => this.SUBRULE(this.locationFunction) },
334
+ ]);
335
+ this.OPTION(() => {
336
+ this.OR1([{ ALT: () => this.CONSUME(lexer.Asc, { LABEL: 'order' }) }, { ALT: () => this.CONSUME(lexer.Desc, { LABEL: 'order' }) }]);
337
+ });
338
+ this.OPTION1(() => {
339
+ this.CONSUME(lexer.Nulls);
340
+ this.OR2([{ ALT: () => this.CONSUME(lexer.First, { LABEL: 'nulls' }) }, { ALT: () => this.CONSUME(lexer.Last, { LABEL: 'nulls' }) }]);
341
+ });
342
+ });
343
+ limitClause = this.RULE('limitClause', () => {
344
+ this.CONSUME(lexer.Limit);
345
+ this.CONSUME(lexer.UnsignedInteger, { LABEL: 'value' });
346
+ });
347
+ offsetClause = this.RULE('offsetClause', () => {
348
+ this.CONSUME(lexer.Offset);
349
+ this.CONSUME(lexer.UnsignedInteger, { LABEL: 'value' });
350
+ });
351
+ // these are made undefined to ensure that V8 knows about these
352
+ dateFunction = this.RULE('dateFunction', () => {
353
+ this.OR(this.$_dateFunctionOr || // https://sap.github.io/chevrotain/docs/guide/performance.html#caching-arrays-of-alternatives
354
+ (this.$_dateFunctionOr = [
355
+ { ALT: () => this.CONSUME(lexer.CalendarMonth, { LABEL: 'fn' }) },
356
+ { ALT: () => this.CONSUME(lexer.CalendarQuarter, { LABEL: 'fn' }) },
357
+ { ALT: () => this.CONSUME(lexer.CalendarYear, { LABEL: 'fn' }) },
358
+ { ALT: () => this.CONSUME(lexer.DayInMonth, { LABEL: 'fn' }) },
359
+ { ALT: () => this.CONSUME(lexer.DayInWeek, { LABEL: 'fn' }) },
360
+ { ALT: () => this.CONSUME(lexer.DayInYear, { LABEL: 'fn' }) },
361
+ { ALT: () => this.CONSUME(lexer.DayOnly, { LABEL: 'fn' }) },
362
+ { ALT: () => this.CONSUME(lexer.FiscalMonth, { LABEL: 'fn' }) },
363
+ { ALT: () => this.CONSUME(lexer.FiscalQuarter, { LABEL: 'fn' }) },
364
+ { ALT: () => this.CONSUME(lexer.FiscalYear, { LABEL: 'fn' }) },
365
+ { ALT: () => this.CONSUME(lexer.HourInDay, { LABEL: 'fn' }) },
366
+ { ALT: () => this.CONSUME(lexer.WeekInMonth, { LABEL: 'fn' }) },
367
+ { ALT: () => this.CONSUME(lexer.WeekInYear, { LABEL: 'fn' }) },
368
+ ]));
369
+ this.SUBRULE(this.functionExpression);
370
+ });
371
+ aggregateFunction = this.RULE('aggregateFunction', () => {
372
+ this.OR(this.$_aggregateFunction ||
373
+ (this.$_aggregateFunction = [
374
+ { ALT: () => this.CONSUME(lexer.Avg, { LABEL: 'fn' }) },
375
+ { ALT: () => this.CONSUME(lexer.Count, { LABEL: 'fn' }) },
376
+ { ALT: () => this.CONSUME(lexer.CountDistinct, { LABEL: 'fn' }) },
377
+ { ALT: () => this.CONSUME(lexer.Min, { LABEL: 'fn' }) },
378
+ { ALT: () => this.CONSUME(lexer.Max, { LABEL: 'fn' }) },
379
+ { ALT: () => this.CONSUME(lexer.Sum, { LABEL: 'fn' }) },
380
+ ]));
381
+ this.SUBRULE(this.functionExpression, { ARGS: [true] });
382
+ });
383
+ fieldsFunction = this.RULE('fieldsFunction', () => {
384
+ this.CONSUME(lexer.Fields, { LABEL: 'fn' });
385
+ this.CONSUME(lexer.LParen);
386
+ this.CONSUME(lexer.FieldsFunctionParamIdentifier, { LABEL: 'params' });
387
+ this.CONSUME(lexer.RParen);
388
+ });
389
+ otherFunction = this.RULE('otherFunction', () => {
390
+ this.OR(this.$_otherFunction ||
391
+ (this.$_otherFunction = [
392
+ { ALT: () => this.CONSUME(lexer.Format, { LABEL: 'fn' }) },
393
+ { ALT: () => this.CONSUME(lexer.Tolabel, { LABEL: 'fn' }) },
394
+ { ALT: () => this.CONSUME(lexer.ConvertTimeZone, { LABEL: 'fn' }) },
395
+ { ALT: () => this.CONSUME(lexer.ConvertCurrency, { LABEL: 'fn' }) },
396
+ { ALT: () => this.CONSUME(lexer.Grouping, { LABEL: 'fn' }) },
397
+ ]));
398
+ this.SUBRULE(this.functionExpression);
399
+ });
400
+ cubeFunction = this.RULE('cubeFunction', () => {
401
+ this.CONSUME(lexer.Cube, { LABEL: 'fn' });
402
+ this.SUBRULE(this.functionExpression);
403
+ });
404
+ rollupFunction = this.RULE('rollupFunction', () => {
405
+ this.CONSUME(lexer.Rollup, { LABEL: 'fn' });
406
+ this.SUBRULE(this.functionExpression);
407
+ });
408
+ functionExpression = this.RULE('functionExpression', (skipAggregate) => {
409
+ this.CONSUME(lexer.LParen);
410
+ this.MANY_SEP({
411
+ SEP: lexer.Comma,
412
+ DEF: () => {
413
+ this.OR([
414
+ { GATE: () => !skipAggregate, ALT: () => this.SUBRULE(this.aggregateFunction, { LABEL: 'params' }) },
415
+ { ALT: () => this.SUBRULE(this.otherFunction, { LABEL: 'params' }) },
416
+ { ALT: () => this.CONSUME(lexer.StringIdentifier, { LABEL: 'params' }) },
417
+ { ALT: () => this.CONSUME(lexer.NumberIdentifier, { LABEL: 'params' }) },
418
+ { ALT: () => this.CONSUME(lexer.Identifier, { LABEL: 'params' }) },
419
+ ]);
420
+ },
421
+ });
422
+ this.CONSUME(lexer.RParen);
423
+ });
424
+ locationFunction = this.RULE('locationFunction', () => {
425
+ this.CONSUME(lexer.Distance);
426
+ this.CONSUME(lexer.LParen);
427
+ this.CONSUME(lexer.Identifier, { LABEL: 'location1' });
428
+ this.CONSUME(lexer.Comma);
429
+ this.OR([
430
+ { ALT: () => this.SUBRULE(this.geolocationFunction, { LABEL: 'location2' }) },
431
+ { ALT: () => this.CONSUME1(lexer.Identifier, { LABEL: 'location2' }) },
432
+ ]);
433
+ this.CONSUME1(lexer.Comma);
434
+ this.CONSUME(lexer.GeolocationUnit, { LABEL: 'unit' });
435
+ this.CONSUME(lexer.RParen);
436
+ });
437
+ geolocationFunction = this.RULE('geolocationFunction', () => {
438
+ this.CONSUME(lexer.Geolocation);
439
+ this.CONSUME(lexer.LParen);
440
+ this.CONSUME(lexer.NumberIdentifier, { LABEL: 'latitude' });
441
+ this.CONSUME(lexer.Comma);
442
+ this.CONSUME1(lexer.NumberIdentifier, { LABEL: 'longitude' });
443
+ this.CONSUME(lexer.RParen);
444
+ });
445
+ expressionPartWithNegation = this.RULE('expressionPartWithNegation', (parenCount) => {
446
+ let leftParenCount = 0; // ensure parenCount is not mutated unless rule is executed
447
+ this.MANY(() => {
448
+ this.CONSUME(lexer.LParen);
449
+ leftParenCount++;
450
+ });
451
+ this.CONSUME(lexer.Not, { LABEL: 'expressionNegation' });
452
+ if (parenCount && leftParenCount) {
453
+ parenCount.left += leftParenCount;
454
+ }
455
+ });
456
+ expression = this.RULE('expression', (parenCount, allowSubquery, allowAggregateFn, allowLocationFn) => {
457
+ this.OPTION1(() => {
458
+ this.MANY1(() => {
459
+ this.CONSUME(lexer.LParen);
460
+ if (parenCount) {
461
+ parenCount.left++;
462
+ }
463
+ });
464
+ });
465
+ this.OR1([
466
+ { GATE: () => !!allowAggregateFn, ALT: () => this.SUBRULE(this.aggregateFunction, { LABEL: 'lhs' }) },
467
+ { GATE: () => !!allowLocationFn, ALT: () => this.SUBRULE(this.locationFunction, { LABEL: 'lhs' }) },
468
+ { ALT: () => this.SUBRULE(this.dateFunction, { LABEL: 'lhs' }) },
469
+ { ALT: () => this.SUBRULE(this.otherFunction, { LABEL: 'lhs' }) },
470
+ { ALT: () => this.CONSUME(lexer.Identifier, { LABEL: 'lhs' }) },
471
+ ]);
472
+ this.OR2([
473
+ { ALT: () => this.SUBRULE(this.expressionWithRelationalOperator, { LABEL: 'operator' }) },
474
+ { ALT: () => this.SUBRULE(this.expressionWithSetOperator, { LABEL: 'operator', ARGS: [allowSubquery] }) },
475
+ ]);
476
+ this.OPTION3(() => {
477
+ this.MANY2({
478
+ GATE: () => (parenCount ? parenCount.left > parenCount.right : true),
479
+ DEF: () => {
480
+ this.CONSUME(lexer.RParen);
481
+ if (parenCount) {
482
+ parenCount.right++;
483
+ }
484
+ },
485
+ });
486
+ });
487
+ });
488
+ expressionWithRelationalOperator = this.RULE('expressionWithRelationalOperator', () => {
489
+ this.SUBRULE(this.relationalOperator);
490
+ this.SUBRULE(this.atomicExpression, { LABEL: 'rhs' });
491
+ });
492
+ expressionWithSetOperator = this.RULE('expressionWithSetOperator', (allowSubquery) => {
493
+ this.SUBRULE(this.setOperator);
494
+ this.SUBRULE2(this.atomicExpression, { LABEL: 'rhs', ARGS: [true, allowSubquery] });
495
+ });
496
+ atomicExpression = this.RULE('atomicExpression', (isArray, allowSubquery) => {
497
+ this.OR(this.$_atomicExpression ||
498
+ (this.$_atomicExpression = [
499
+ { GATE: () => this.allowApexBindVariables, ALT: () => this.SUBRULE(this.apexBindVariableExpression) },
500
+ // SET / SUBQUERY
501
+ { GATE: () => isArray, ALT: () => this.SUBRULE(this.arrayExpression) },
502
+ { GATE: () => isArray && allowSubquery, ALT: () => this.SUBRULE(this.whereClauseSubqueryIdentifier) },
503
+ // NON-SET
504
+ { GATE: () => !isArray, ALT: () => this.CONSUME(lexer.DateIdentifier) },
505
+ { GATE: () => !isArray, ALT: () => this.CONSUME(lexer.CurrencyPrefixedDecimal, { LABEL: 'CurrencyPrefixedDecimal' }) },
506
+ { GATE: () => !isArray, ALT: () => this.CONSUME(lexer.CurrencyPrefixedInteger, { LABEL: 'CurrencyPrefixedInteger' }) },
507
+ { GATE: () => !isArray, ALT: () => this.CONSUME(lexer.NumberIdentifier) },
508
+ { GATE: () => !isArray, ALT: () => this.CONSUME(lexer.Null) },
509
+ { GATE: () => !isArray, ALT: () => this.SUBRULE(this.booleanValue) },
510
+ { GATE: () => !isArray, ALT: () => this.CONSUME(lexer.DateLiteral) },
511
+ { GATE: () => !isArray, ALT: () => this.SUBRULE(this.dateNLiteral) },
512
+ // GeolocationUnit is matched even when it is an actual string in a WHERE clause - treat as StringIdentifier
513
+ // https://github.com/jetstreamapp/soql-parser-js/issues/188
514
+ { GATE: () => !isArray, ALT: () => this.CONSUME(lexer.GeolocationUnit, { LABEL: 'StringIdentifier' }) },
515
+ { GATE: () => !isArray, ALT: () => this.CONSUME(lexer.StringIdentifier) },
516
+ ]));
517
+ });
518
+ apexBindVariableExpression = this.RULE('apexBindVariableExpression', () => {
519
+ this.CONSUME(lexer.Colon);
520
+ // First item in list could optionally be a new instantiation
521
+ this.OPTION(() => {
522
+ this.SUBRULE(this.apexBindVariableNewInstantiation, { LABEL: 'apex' });
523
+ this.OPTION1(() => {
524
+ this.CONSUME(lexer.Decimal);
525
+ });
526
+ });
527
+ // Chained function calls or nested arguments with function calls at the end
528
+ this.MANY_SEP({
529
+ SEP: lexer.Decimal,
530
+ DEF: () => {
531
+ this.OR(this.$_apexBindVariableExpression ||
532
+ (this.$_apexBindVariableExpression = [
533
+ { ALT: () => this.SUBRULE(this.apexBindVariableFunctionCall, { LABEL: 'apex' }) },
534
+ { ALT: () => this.SUBRULE(this.apexBindVariableIdentifier, { LABEL: 'apex' }) },
535
+ ]));
536
+ },
537
+ });
538
+ });
539
+ apexBindVariableIdentifier = this.RULE('apexBindVariableIdentifier', () => {
540
+ this.CONSUME(lexer.Identifier);
541
+ this.OPTION(() => this.SUBRULE(this.apexBindVariableFunctionArrayAccessor));
542
+ });
543
+ apexBindVariableNewInstantiation = this.RULE('apexBindVariableNewInstantiation', () => {
544
+ this.CONSUME(lexer.ApexNew, { LABEL: 'new' });
545
+ this.CONSUME(lexer.Identifier, { LABEL: 'function' });
546
+ this.OPTION(() => {
547
+ this.SUBRULE(this.apexBindVariableGeneric);
548
+ });
549
+ this.SUBRULE(this.apexBindVariableFunctionParams);
550
+ this.OPTION1(() => this.SUBRULE(this.apexBindVariableFunctionArrayAccessor));
551
+ });
552
+ apexBindVariableFunctionCall = this.RULE('apexBindVariableFunctionCall', () => {
553
+ this.CONSUME(lexer.Identifier, { LABEL: 'function' });
554
+ this.SUBRULE(this.apexBindVariableFunctionParams);
555
+ this.OPTION(() => this.SUBRULE(this.apexBindVariableFunctionArrayAccessor));
556
+ });
557
+ apexBindVariableGeneric = this.RULE('apexBindVariableGeneric', () => {
558
+ this.CONSUME(lexer.LessThan);
559
+ this.AT_LEAST_ONE_SEP({
560
+ SEP: lexer.Comma,
561
+ DEF: () => {
562
+ this.CONSUME(lexer.Identifier, { LABEL: 'parameter' });
563
+ },
564
+ });
565
+ this.CONSUME(lexer.GreaterThan);
566
+ });
567
+ apexBindVariableFunctionParams = this.RULE('apexBindVariableFunctionParams', () => {
568
+ this.CONSUME(lexer.LParen);
569
+ this.MANY_SEP({
570
+ SEP: lexer.Comma,
571
+ DEF: () => {
572
+ this.CONSUME(lexer.Identifier, { LABEL: 'parameter' });
573
+ },
574
+ });
575
+ this.CONSUME(lexer.RParen);
576
+ });
577
+ // foo[3] or foo[somIntVariable]
578
+ apexBindVariableFunctionArrayAccessor = this.RULE('apexBindVariableFunctionArrayAccessor', () => {
579
+ this.CONSUME(lexer.LSquareBracket);
580
+ this.OR([
581
+ { ALT: () => this.CONSUME(lexer.UnsignedInteger, { LABEL: 'value' }) },
582
+ { ALT: () => this.CONSUME(lexer.Identifier, { LABEL: 'value' }) },
583
+ ]);
584
+ this.CONSUME(lexer.RSquareBracket);
585
+ });
586
+ arrayExpression = this.RULE('arrayExpression', () => {
587
+ this.CONSUME(lexer.LParen);
588
+ this.AT_LEAST_ONE_SEP({
589
+ SEP: lexer.Comma,
590
+ DEF: () => {
591
+ this.OR(this.$_arrayExpression ||
592
+ (this.$_arrayExpression = [
593
+ { ALT: () => this.CONSUME(lexer.CurrencyPrefixedDecimal, { LABEL: 'value' }) },
594
+ { ALT: () => this.CONSUME(lexer.CurrencyPrefixedInteger, { LABEL: 'value' }) },
595
+ { ALT: () => this.CONSUME(lexer.NumberIdentifier, { LABEL: 'value' }) },
596
+ { ALT: () => this.CONSUME(lexer.DateIdentifier, { LABEL: 'value' }) },
597
+ { ALT: () => this.CONSUME(lexer.Null, { LABEL: 'value' }) },
598
+ { ALT: () => this.CONSUME(lexer.True, { LABEL: 'value' }) },
599
+ { ALT: () => this.CONSUME(lexer.False, { LABEL: 'value' }) },
600
+ { ALT: () => this.CONSUME(lexer.DateLiteral, { LABEL: 'value' }) },
601
+ { ALT: () => this.SUBRULE(this.dateNLiteral, { LABEL: 'value' }) },
602
+ { ALT: () => this.CONSUME(lexer.GeolocationUnit, { LABEL: 'value' }) },
603
+ { ALT: () => this.CONSUME(lexer.StringIdentifier, { LABEL: 'value' }) },
604
+ ]));
605
+ },
606
+ });
607
+ this.CONSUME(lexer.RParen);
608
+ });
609
+ relationalOperator = this.RULE('relationalOperator', () => {
610
+ this.OR(this.$_relationalOperator ||
611
+ (this.$_relationalOperator = [
612
+ { ALT: () => this.CONSUME(lexer.Equal, { LABEL: 'operator' }) },
613
+ { ALT: () => this.CONSUME(lexer.NotEqual, { LABEL: 'operator' }) },
614
+ { ALT: () => this.CONSUME(lexer.GreaterThan, { LABEL: 'operator' }) },
615
+ { ALT: () => this.CONSUME(lexer.GreaterThanOrEqual, { LABEL: 'operator' }) },
616
+ { ALT: () => this.CONSUME(lexer.LessThan, { LABEL: 'operator' }) },
617
+ { ALT: () => this.CONSUME(lexer.LessThanOrEqual, { LABEL: 'operator' }) },
618
+ { ALT: () => this.CONSUME(lexer.Like, { LABEL: 'operator' }) },
619
+ ]));
620
+ });
621
+ setOperator = this.RULE('setOperator', () => {
622
+ this.OR([
623
+ { ALT: () => this.CONSUME(lexer.In, { LABEL: 'operator' }) },
624
+ { ALT: () => this.SUBRULE(this.notInOperator, { LABEL: 'notIn' }) },
625
+ { ALT: () => this.CONSUME(lexer.Includes, { LABEL: 'operator' }) },
626
+ { ALT: () => this.CONSUME(lexer.Excludes, { LABEL: 'operator' }) },
627
+ ]);
628
+ });
629
+ notInOperator = this.RULE('notInOperator', () => {
630
+ this.CONSUME(lexer.Not, { LABEL: 'operator' });
631
+ this.CONSUME(lexer.In, { LABEL: 'operator' });
632
+ });
633
+ booleanValue = this.RULE('booleanValue', () => {
634
+ this.OR([
635
+ { ALT: () => this.CONSUME(lexer.True, { LABEL: 'boolean' }) },
636
+ { ALT: () => this.CONSUME(lexer.False, { LABEL: 'boolean' }) },
637
+ ]);
638
+ });
639
+ dateNLiteral = this.RULE('dateNLiteral', () => {
640
+ this.CONSUME(lexer.DateNLiteral, { LABEL: 'dateNLiteral' });
641
+ this.CONSUME(lexer.Colon);
642
+ this.OR1([
643
+ { ALT: () => this.CONSUME(lexer.UnsignedInteger, { LABEL: 'variable' }) },
644
+ { ALT: () => this.CONSUME(lexer.SignedInteger, { LABEL: 'variable' }) },
645
+ ]);
646
+ });
647
+ forViewOrReference = this.RULE('forViewOrReference', () => {
648
+ this.CONSUME(lexer.For);
649
+ this.OR([
650
+ { ALT: () => this.CONSUME(lexer.View, { LABEL: 'value' }) },
651
+ { ALT: () => this.CONSUME(lexer.Reference, { LABEL: 'value' }) },
652
+ { ALT: () => this.CONSUME(lexer.Update, { LABEL: 'value' }) },
653
+ ]);
654
+ });
655
+ updateTrackingViewstat = this.RULE('updateTrackingViewstat', () => {
656
+ this.CONSUME(lexer.Update);
657
+ this.OR([
658
+ { ALT: () => this.CONSUME(lexer.Tracking, { LABEL: 'value' }) },
659
+ { ALT: () => this.CONSUME(lexer.Viewstat, { LABEL: 'value' }) },
660
+ ]);
661
+ });
662
+ getParenCount(parenCount) {
663
+ return parenCount || { right: 0, left: 0 };
664
+ }
665
+ }
666
+ exports.SoqlParser = SoqlParser;
667
+ let parser = new SoqlParser();
668
+ function parse(soql, options) {
669
+ const { allowApexBindVariables, logErrors, ignoreParseErrors, allowPartialQuery } = options || {
670
+ allowApexBindVariables: false,
671
+ logErrors: false,
672
+ ignoreParseErrors: false,
673
+ };
674
+ const lexResult = lexer.lex(soql);
675
+ if (lexResult.errors.length > 0) {
676
+ if (logErrors) {
677
+ console.log('Lexing Errors:');
678
+ console.log(lexResult.errors);
679
+ }
680
+ throw new LexingError(lexResult.errors[0]);
681
+ }
682
+ // get new instance if ignoreParseErrors changes
683
+ if (parser.ignoreParseErrors !== ignoreParseErrors) {
684
+ parser = new SoqlParser({ ignoreParseErrors });
685
+ }
686
+ // setting a new input will RESET the parser instance's state.
687
+ parser.input = lexResult.tokens;
688
+ // If true, allows WHERE foo = :bar
689
+ parser.allowApexBindVariables = allowApexBindVariables || false;
690
+ let cst;
691
+ if (allowPartialQuery) {
692
+ cst = parser.selectStatementPartial();
693
+ }
694
+ else {
695
+ cst = parser.selectStatement();
696
+ }
697
+ if (parser.errors.length > 0) {
698
+ if (logErrors) {
699
+ console.log('Parsing Errors:');
700
+ console.log(parser.errors);
701
+ }
702
+ if (!ignoreParseErrors) {
703
+ throw new ParsingError(parser.errors[0]);
704
+ }
705
+ }
706
+ return {
707
+ cst,
708
+ parseErrors: parser.errors.map(err => new ParsingError(err)),
709
+ };
710
+ }
711
+ exports.parse = parse;