From 6409fd79aeb570709ea8b6a2f11182a1aea4240c Mon Sep 17 00:00:00 2001 From: Vadim Kibana <82822460+vadimkibana@users.noreply.github.com> Date: Tue, 1 Jul 2025 15:50:01 +0200 Subject: [PATCH] [ES|QL] Stop ANTLR token monkey-patching in parser (#225629) ## Summary A small cleanup in parser code in the ES|QL AST package. We want to show users readable syntax error token names like `[` and `(` (instead of `LP`), however, we should not monkey-patch ANTLR generated files to achieve this. Instead, this PR moves the message improvement code to the validation & autocomplete package, where the messages are actually used. ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios (cherry picked from commit 750cc7c4c1b91f2713b97a27c89e07dadffbb816) --- .../shared/kbn-esql-ast/src/parser/parser.ts | 28 +------------------ .../src/validation/validation.ts | 9 +++++- 2 files changed, 9 insertions(+), 28 deletions(-) diff --git a/src/platform/packages/shared/kbn-esql-ast/src/parser/parser.ts b/src/platform/packages/shared/kbn-esql-ast/src/parser/parser.ts index c6eeb8b7214c7..334c2fdbf6b36 100644 --- a/src/platform/packages/shared/kbn-esql-ast/src/parser/parser.ts +++ b/src/platform/packages/shared/kbn-esql-ast/src/parser/parser.ts @@ -13,33 +13,10 @@ import { ESQLErrorListener } from './esql_error_listener'; import { ESQLAstBuilderListener } from './esql_ast_builder_listener'; import { GRAMMAR_ROOT_RULE } from './constants'; import { attachDecorations, collectDecorations } from './formatting'; -import type { ESQLAst, ESQLAstQueryExpression, EditorError } from '../types'; import { Builder } from '../builder'; import { default as ESQLLexer } from '../antlr/esql_lexer'; import { default as ESQLParser } from '../antlr/esql_parser'; - -/** - * Some changes to the grammar deleted the literal names for some tokens. - * This is a workaround to restore the literals that were lost. - * - * See https://github.com/elastic/elasticsearch/pull/124177 for context. - */ -const replaceSymbolsWithLiterals = ( - symbolicNames: Array, - literalNames: Array -) => { - const symbolReplacements: Map = new Map([ - ['LP', '('], - ['OPENING_BRACKET', '['], - ]); - - for (let i = 0; i < symbolicNames.length; i++) { - const name = symbolicNames[i]; - if (name && symbolReplacements.has(name)) { - literalNames[i] = `'${symbolReplacements.get(name)!}'`; - } - } -}; +import type { ESQLAst, ESQLAstQueryExpression, EditorError } from '../types'; export interface ParseOptions { /** @@ -100,9 +77,6 @@ export class Parser { const tokens = (this.tokens = new CommonTokenStream(lexer)); const parser = (this.parser = new ESQLParser(tokens)); - replaceSymbolsWithLiterals(lexer.symbolicNames, lexer.literalNames); - replaceSymbolsWithLiterals(parser.symbolicNames, parser.literalNames); - lexer.removeErrorListeners(); lexer.addErrorListener(this.errors); diff --git a/src/platform/packages/shared/kbn-esql-validation-autocomplete/src/validation/validation.ts b/src/platform/packages/shared/kbn-esql-validation-autocomplete/src/validation/validation.ts index 2280ca3d531c6..1095430cb5576 100644 --- a/src/platform/packages/shared/kbn-esql-validation-autocomplete/src/validation/validation.ts +++ b/src/platform/packages/shared/kbn-esql-validation-autocomplete/src/validation/validation.ts @@ -194,8 +194,15 @@ async function validateAst( messages.push(...commandMessages); } + const parserErrors = parsingResult.errors; + + for (const error of parserErrors) { + error.message = error.message.replace(/\bLP\b/, "'('"); + error.message = error.message.replace(/\bOPENING_BRACKET\b/, "'['"); + } + return { - errors: [...parsingResult.errors, ...messages.filter(({ type }) => type === 'error')], + errors: [...parserErrors, ...messages.filter(({ type }) => type === 'error')], warnings: messages.filter(({ type }) => type === 'warning'), }; }