Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions src/platform/packages/shared/kbn-esql-ast/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,7 @@ export type {
ESQLAstChangePointCommand,
} from './src/types';

export {
isColumn,
isDoubleLiteral,
isFunctionExpression,
isBinaryExpression,
isWhereExpression,
isFieldExpression,
isSource,
isIdentifier,
isIntegerLiteral,
isLiteral,
isParamLiteral,
isProperNode,
} from './src/ast/helpers';
export * from './src/ast/is';

export { Builder, type AstNodeParserFields, type AstNodeTemplate } from './src/builder';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

interface JsonWalkerOptions {
visitObject?: (node: Record<string, unknown>) => void;
visitArray?: (node: unknown[]) => void;
visitString?: (node: string) => void;
visitNumber?: (node: number) => void;
visitBigint?: (node: bigint) => void;
visitBoolean?: (node: boolean) => void;
visitNull?: () => void;
visitUndefined?: () => void;
}

const walkJson = (json: unknown, options: JsonWalkerOptions = {}) => {
switch (typeof json) {
case 'string': {
options.visitString?.(json);
break;
}
case 'number': {
options.visitNumber?.(json);
break;
}
case 'bigint': {
options.visitBigint?.(json as bigint);
break;
}
case 'boolean': {
options.visitBoolean?.(json);
break;
}
case 'undefined': {
options.visitUndefined?.();
break;
}
case 'object': {
if (!json) {
options.visitNull?.();
} else if (Array.isArray(json)) {
options.visitArray?.(json);
const length = json.length;

for (let i = 0; i < length; i++) {
walkJson(json[i], options);
}
} else {
options.visitObject?.(json as Record<string, unknown>);
const values = Object.values(json as Record<string, unknown>);
const length = values.length;

for (let i = 0; i < length; i++) {
const value = values[i];
walkJson(value, options);
}
}
}
}
};
20 changes: 0 additions & 20 deletions src/platform/packages/shared/kbn-esql-ast/src/ast/constants.ts

This file was deleted.

99 changes: 99 additions & 0 deletions src/platform/packages/shared/kbn-esql-ast/src/ast/grouping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { isBinaryExpression } from './is';
import type { ESQLAstNode } from '../types';

/**
* The group name of a binary expression. Groups are ordered by precedence.
*/
export enum BinaryExpressionGroup {
/**
* No group, not a binary expression.
*/
none = 0,

/**
* Binary expression, but its group is unknown.
*/
unknown = 1,

/**
* Logical: `and`, `or`
*/
or = 10,
and = 11,

/**
* Regular expression: `like`, `not like`, `rlike`, `not rlike`
*/
regex = 20,

/**
* Assignment: `=`, `:=`
*/
assignment = 30,

/**
* Comparison: `==`, `=~`, `!=`, `<`, `<=`, `>`, `>=`
*/
comparison = 40,

/**
* Additive: `+`, `-`
*/
additive = 50,

/**
* Multiplicative: `*`, `/`, `%`
*/
multiplicative = 60,
}

/**
* Returns the group of a binary expression.
*
* @param node Any ES|QL AST node.
* @returns Binary expression group or undefined if the node is
* not a binary expression.
*/
export const binaryExpressionGroup = (node: ESQLAstNode): BinaryExpressionGroup => {
if (isBinaryExpression(node)) {
switch (node.name) {
case '+':
case '-':
return BinaryExpressionGroup.additive;
case '*':
case '/':
case '%':
return BinaryExpressionGroup.multiplicative;
case '=':
return BinaryExpressionGroup.assignment;
case '==':
case '=~':
case '!=':
case '<':
case '<=':
case '>':
case '>=':
return BinaryExpressionGroup.comparison;
case 'like':
case 'not like':
case 'rlike':
case 'not rlike':
return BinaryExpressionGroup.regex;
case 'or':
return BinaryExpressionGroup.or;
case 'and':
return BinaryExpressionGroup.and;
}
return BinaryExpressionGroup.unknown;
}
return BinaryExpressionGroup.none;
};
137 changes: 0 additions & 137 deletions src/platform/packages/shared/kbn-esql-ast/src/ast/helpers.ts

This file was deleted.

Loading