Skip to content

Commit

Permalink
[ES|QL] Catch internal parsing errors (#196249)
Browse files Browse the repository at this point in the history
## Summary

Addresses this comment
#191683 (comment)

Catches parser internal errors, and reports them the same as the regular
parsing errors.

(cherry picked from commit 1f56f21)
  • Loading branch information
vadimkibana committed Oct 16, 2024
1 parent e061949 commit b0ec86a
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 37 deletions.
2 changes: 1 addition & 1 deletion packages/kbn-esql-ast/src/parser/__tests__/columns.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { getAstAndSyntaxErrors as parse } from '..';
import { parse } from '..';

describe('Column Identifier Expressions', () => {
it('can parse un-quoted identifiers', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { getAstAndSyntaxErrors as parse } from '..';
import { parse } from '..';

describe('commands', () => {
describe('correctly formatted, basic usage', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-esql-ast/src/parser/__tests__/from.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { getAstAndSyntaxErrors as parse } from '..';
import { parse } from '..';

describe('FROM', () => {
describe('correctly formatted', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { getAstAndSyntaxErrors as parse } from '..';
import { parse } from '..';
import { Walker } from '../../walker';

describe('function AST nodes', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { getAstAndSyntaxErrors as parse } from '..';
import { parse } from '..';
import { ESQLFunction, ESQLInlineCast, ESQLSingleAstItem } from '../../types';

describe('Inline cast (::)', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-esql-ast/src/parser/__tests__/literal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { getAstAndSyntaxErrors as parse } from '..';
import { parse } from '..';
import { ESQLLiteral } from '../../types';

describe('literal expression', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-esql-ast/src/parser/__tests__/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { getAstAndSyntaxErrors as parse } from '..';
import { parse } from '..';

describe('METRICS', () => {
describe('correctly formatted', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-esql-ast/src/parser/__tests__/params.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { getAstAndSyntaxErrors as parse } from '..';
import { parse } from '..';
import { Walker } from '../../walker';

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-esql-ast/src/parser/__tests__/rename.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { getAstAndSyntaxErrors as parse } from '..';
import { parse } from '..';

describe('RENAME', () => {
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-esql-ast/src/parser/__tests__/sort.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { getAstAndSyntaxErrors as parse } from '..';
import { parse } from '..';

describe('SORT', () => {
describe('correctly formatted', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-esql-ast/src/parser/__tests__/where.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { getAstAndSyntaxErrors as parse } from '..';
import { parse } from '..';

describe('WHERE', () => {
describe('correctly formatted', () => {
Expand Down
85 changes: 59 additions & 26 deletions packages/kbn-esql-ast/src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,33 +100,66 @@ export interface ParseResult {
}

export const parse = (text: string | undefined, options: ParseOptions = {}): ParseResult => {
if (text == null) {
const commands: ESQLAstQueryExpression['commands'] = [];
return { ast: commands, root: Builder.expression.query(commands), errors: [], tokens: [] };
try {
if (text == null) {
const commands: ESQLAstQueryExpression['commands'] = [];
return { ast: commands, root: Builder.expression.query(commands), errors: [], tokens: [] };
}
const errorListener = new ESQLErrorListener();
const parseListener = new ESQLAstBuilderListener();
const { tokens, parser } = getParser(
CharStreams.fromString(text),
errorListener,
parseListener
);

parser[GRAMMAR_ROOT_RULE]();

const errors = errorListener.getErrors().filter((error) => {
return !SYNTAX_ERRORS_TO_IGNORE.includes(error.message);
});
const { ast: commands } = parseListener.getAst();
const root = Builder.expression.query(commands, {
location: {
min: 0,
max: text.length - 1,
},
});

if (options.withFormatting) {
const decorations = collectDecorations(tokens);
attachDecorations(root, tokens.tokens, decorations.lines);
}

return { root, ast: commands, errors, tokens: tokens.tokens };
} catch (error) {
/**
* Parsing should never fail, meaning this branch should never execute. But
* if it does fail, we want to log the error message for easier debugging.
*/
// eslint-disable-next-line no-console
console.error(error);

const root = Builder.expression.query();

return {
root,
ast: root.commands,
errors: [
{
startLineNumber: 0,
endLineNumber: 0,
startColumn: 0,
endColumn: 0,
message:
'Parsing internal error: ' +
(!!error && typeof error === 'object' ? String(error.message) : String(error)),
severity: 'error',
},
],
tokens: [],
};
}
const errorListener = new ESQLErrorListener();
const parseListener = new ESQLAstBuilderListener();
const { tokens, parser } = getParser(CharStreams.fromString(text), errorListener, parseListener);

parser[GRAMMAR_ROOT_RULE]();

const errors = errorListener.getErrors().filter((error) => {
return !SYNTAX_ERRORS_TO_IGNORE.includes(error.message);
});
const { ast: commands } = parseListener.getAst();
const root = Builder.expression.query(commands, {
location: {
min: 0,
max: text.length - 1,
},
});

if (options.withFormatting) {
const decorations = collectDecorations(tokens);
attachDecorations(root, tokens.tokens, decorations.lines);
}

return { root, ast: commands, errors, tokens: tokens.tokens };
};

export const parseErrors = (text: string) => {
Expand Down

0 comments on commit b0ec86a

Please sign in to comment.