-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
ast_helpers.ts
349 lines (318 loc) · 9.65 KB
/
ast_helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
/**
* In case of changes in the grammar, this script should be updated: esql_update_ast_script.js
*/
import { type Token, type ParserRuleContext, type TerminalNode } from 'antlr4';
import type {
ArithmeticUnaryContext,
DecimalValueContext,
IntegerValueContext,
QualifiedIntegerLiteralContext,
} from './antlr/esql_parser';
import { getPosition } from './ast_position_utils';
import { DOUBLE_TICKS_REGEX, SINGLE_BACKTICK, TICKS_REGEX } from './constants';
import type {
ESQLAstBaseItem,
ESQLCommand,
ESQLLiteral,
ESQLList,
ESQLTimeInterval,
ESQLLocation,
ESQLFunction,
ESQLSource,
ESQLColumn,
ESQLCommandOption,
ESQLAstItem,
ESQLCommandMode,
} from './types';
export function nonNullable<T>(v: T): v is NonNullable<T> {
return v != null;
}
export function createAstBaseItem<Name = string>(
name: Name,
ctx: ParserRuleContext
): ESQLAstBaseItem<Name> {
return {
name,
text: ctx.getText(),
location: getPosition(ctx.start, ctx.stop),
incomplete: Boolean(ctx.exception),
};
}
export function createCommand(name: string, ctx: ParserRuleContext): ESQLCommand {
return {
type: 'command',
name,
text: ctx.getText(),
args: [],
location: getPosition(ctx.start, ctx.stop),
incomplete: Boolean(ctx.exception),
};
}
export function createList(ctx: ParserRuleContext, values: ESQLLiteral[]): ESQLList {
return {
type: 'list',
name: ctx.getText(),
values,
text: ctx.getText(),
location: getPosition(ctx.start, ctx.stop),
incomplete: Boolean(ctx.exception),
};
}
export function createNumericLiteral(ctx: DecimalValueContext | IntegerValueContext): ESQLLiteral {
const text = ctx.getText();
return {
type: 'literal',
literalType: 'number',
text,
name: text,
value: Number(text),
location: getPosition(ctx.start, ctx.stop),
incomplete: Boolean(ctx.exception),
};
}
export function createFakeMultiplyLiteral(ctx: ArithmeticUnaryContext): ESQLLiteral {
return {
type: 'literal',
literalType: 'number',
text: ctx.getText(),
name: ctx.getText(),
value: ctx.PLUS() ? 1 : -1,
location: getPosition(ctx.start, ctx.stop),
incomplete: Boolean(ctx.exception),
};
}
export function createLiteralString(token: Token): ESQLLiteral {
const text = token.text!;
return {
type: 'literal',
literalType: 'string',
text,
name: text,
value: text,
location: getPosition(token),
incomplete: Boolean(token.text === ''),
};
}
function isMissingText(text: string) {
return /<missing /.test(text);
}
export function textExistsAndIsValid(text: string | undefined): text is string {
return !!(text && !isMissingText(text));
}
export function createLiteral(
type: ESQLLiteral['literalType'],
node: TerminalNode | undefined
): ESQLLiteral | undefined {
if (!node) {
return;
}
const text = node.getText();
const partialLiteral: Omit<ESQLLiteral, 'literalType' | 'value'> = {
type: 'literal',
text,
name: text,
location: getPosition(node.symbol),
incomplete: isMissingText(text),
};
if (type === 'number') {
return {
...partialLiteral,
literalType: type,
value: Number(text),
};
}
return {
...partialLiteral,
literalType: type,
value: text,
};
}
export function createTimeUnit(ctx: QualifiedIntegerLiteralContext): ESQLTimeInterval {
return {
type: 'timeInterval',
quantity: Number(ctx.integerValue().INTEGER_LITERAL().getText()),
unit: ctx.UNQUOTED_IDENTIFIER().symbol.text,
text: ctx.getText(),
location: getPosition(ctx.start, ctx.stop),
name: `${ctx.integerValue().INTEGER_LITERAL().getText()} ${
ctx.UNQUOTED_IDENTIFIER().symbol.text
}`,
incomplete: Boolean(ctx.exception),
};
}
export function createFunction(
name: string,
ctx: ParserRuleContext,
customPosition?: ESQLLocation
): ESQLFunction {
return {
type: 'function',
name,
text: ctx.getText(),
location: customPosition ?? getPosition(ctx.start, ctx.stop),
args: [],
incomplete: Boolean(ctx.exception),
};
}
function walkFunctionStructure(
args: ESQLAstItem[],
initialLocation: ESQLLocation,
prop: 'min' | 'max',
getNextItemIndex: (arg: ESQLAstItem[]) => number
) {
let nextArg: ESQLAstItem | undefined = args[getNextItemIndex(args)];
const location = { ...initialLocation };
while (Array.isArray(nextArg) || nextArg) {
if (Array.isArray(nextArg)) {
nextArg = nextArg[getNextItemIndex(nextArg)];
} else {
location[prop] = Math[prop](location[prop], nextArg.location[prop]);
if (nextArg.type === 'function') {
nextArg = nextArg.args[getNextItemIndex(nextArg.args)];
} else {
nextArg = undefined;
}
}
}
return location[prop];
}
export function computeLocationExtends(fn: ESQLFunction) {
const location = fn.location;
if (fn.args) {
// get min location navigating in depth keeping the left/first arg
location.min = walkFunctionStructure(fn.args, location, 'min', () => 0);
// get max location navigating in depth keeping the right/last arg
location.max = walkFunctionStructure(fn.args, location, 'max', (args) => args.length - 1);
// in case of empty array as last arg, bump the max location by 3 chars (empty brackets)
if (
Array.isArray(fn.args[fn.args.length - 1]) &&
!(fn.args[fn.args.length - 1] as ESQLAstItem[]).length
) {
location.max += 3;
}
}
return location;
}
// Note: do not import esql_parser or bundle size will grow up by ~500 kb
/**
* Do not touch this piece of code as it is auto-generated by a script
*/
/* SCRIPT_MARKER_START */
function getQuotedText(ctx: ParserRuleContext) {
return [27 /* esql_parser.QUOTED_STRING */, 68 /* esql_parser.QUOTED_IDENTIFIER */]
.map((keyCode) => ctx.getToken(keyCode, 0))
.filter(nonNullable)[0];
}
function getUnquotedText(ctx: ParserRuleContext) {
return [67 /* esql_parser.UNQUOTED_IDENTIFIER */, 73 /* esql_parser.FROM_UNQUOTED_IDENTIFIER */]
.map((keyCode) => ctx.getToken(keyCode, 0))
.filter(nonNullable)[0];
}
/* SCRIPT_MARKER_END */
function isQuoted(text: string | undefined) {
return text && /^(`)/.test(text);
}
/**
* Follow a similar logic to the ES one:
* * remove backticks at the beginning and at the end
* * remove double backticks
*/
function safeBackticksRemoval(text: string | undefined) {
return text?.replace(TICKS_REGEX, '').replace(DOUBLE_TICKS_REGEX, SINGLE_BACKTICK) || '';
}
export function sanitizeIdentifierString(ctx: ParserRuleContext) {
const result =
getUnquotedText(ctx)?.getText() ||
safeBackticksRemoval(getQuotedText(ctx)?.getText()) ||
safeBackticksRemoval(ctx.getText()); // for some reason some quoted text is not detected correctly by the parser
// TODO - understand why <missing null> is now returned as the match text for the FROM command
return result === '<missing null>' ? '' : result;
}
export function wrapIdentifierAsArray<T extends ParserRuleContext>(identifierCtx: T | T[]): T[] {
return Array.isArray(identifierCtx) ? identifierCtx : [identifierCtx];
}
export function createSetting(policyName: Token, mode: string): ESQLCommandMode {
return {
type: 'mode',
name: mode.replace('_', '').toLowerCase(),
text: mode,
location: getPosition(policyName, { stop: policyName.start + mode.length - 1 }), // unfortunately this is the only location we have
incomplete: false,
};
}
/**
* In https://github.com/elastic/elasticsearch/pull/103949 the ENRICH policy name
* changed from rule to token type so we need to handle this specifically
*/
export function createPolicy(token: Token, policy: string): ESQLSource {
return {
type: 'source',
name: policy,
text: policy,
sourceType: 'policy',
location: getPosition({
start: token.stop - policy.length + 1,
stop: token.stop,
}), // take into account ccq modes
incomplete: false,
};
}
export function createSource(
ctx: ParserRuleContext,
type: 'index' | 'policy' = 'index'
): ESQLSource {
const text = sanitizeIdentifierString(ctx);
return {
type: 'source',
name: text,
sourceType: type,
text,
location: getPosition(ctx.start, ctx.stop),
incomplete: Boolean(ctx.exception || text === ''),
};
}
export function createColumnStar(ctx: TerminalNode): ESQLColumn {
return {
type: 'column',
name: ctx.getText(),
text: ctx.getText(),
location: getPosition(ctx.symbol),
incomplete: ctx.getText() === '',
quoted: false,
};
}
export function createColumn(ctx: ParserRuleContext): ESQLColumn {
const text = sanitizeIdentifierString(ctx);
const hasQuotes = Boolean(getQuotedText(ctx) || isQuoted(ctx.getText()));
return {
type: 'column' as const,
name: text,
text: ctx.getText(),
location: getPosition(ctx.start, ctx.stop),
incomplete: Boolean(ctx.exception || text === ''),
quoted: hasQuotes,
};
}
export function createOption(name: string, ctx: ParserRuleContext): ESQLCommandOption {
return {
type: 'option',
name,
text: ctx.getText(),
location: getPosition(ctx.start, ctx.stop),
args: [],
incomplete: Boolean(
ctx.exception ||
ctx.children?.some((c) => {
// @ts-expect-error not exposed in type but exists see https://github.com/antlr/antlr4/blob/v4.11.1/runtime/JavaScript/src/antlr4/tree/ErrorNodeImpl.js#L19
return Boolean(c.isErrorNode);
})
),
};
}