From def21e9a21a10eec3003c15374a81d65f70429e7 Mon Sep 17 00:00:00 2001 From: Lukas Olson Date: Wed, 17 Nov 2021 16:44:39 -0700 Subject: [PATCH 1/3] [kql] Remove named args and unused geo functions --- packages/kbn-es-query/grammar/grammar.peggy | 4 +- .../kuery/functions/geo_bounding_box.test.ts | 137 --- .../src/kuery/functions/geo_bounding_box.ts | 61 -- .../src/kuery/functions/geo_polygon.test.ts | 134 --- .../src/kuery/functions/geo_polygon.ts | 57 -- .../kbn-es-query/src/kuery/functions/index.ts | 4 - .../src/kuery/functions/range.test.ts | 65 +- .../kbn-es-query/src/kuery/functions/range.ts | 56 +- .../src/kuery/grammar/__mocks__/grammar.js | 867 +++++++++++------- .../src/kuery/node_types/index.ts | 2 - .../src/kuery/node_types/named_arg.test.ts | 46 - .../src/kuery/node_types/named_arg.ts | 27 - .../src/kuery/node_types/types.ts | 23 +- 13 files changed, 559 insertions(+), 924 deletions(-) delete mode 100644 packages/kbn-es-query/src/kuery/functions/geo_bounding_box.test.ts delete mode 100644 packages/kbn-es-query/src/kuery/functions/geo_bounding_box.ts delete mode 100644 packages/kbn-es-query/src/kuery/functions/geo_polygon.test.ts delete mode 100644 packages/kbn-es-query/src/kuery/functions/geo_polygon.ts delete mode 100644 packages/kbn-es-query/src/kuery/node_types/named_arg.test.ts delete mode 100644 packages/kbn-es-query/src/kuery/node_types/named_arg.ts diff --git a/packages/kbn-es-query/grammar/grammar.peggy b/packages/kbn-es-query/grammar/grammar.peggy index 8f5ef17340aa9..86074764852d6 100644 --- a/packages/kbn-es-query/grammar/grammar.peggy +++ b/packages/kbn-es-query/grammar/grammar.peggy @@ -12,7 +12,6 @@ const buildFunctionNode = nodeTypes.function.buildNodeWithArgumentNodes; const buildLiteralNode = nodeTypes.literal.buildNode; const buildWildcardNode = nodeTypes.wildcard.buildNode; - const buildNamedArgNode = nodeTypes.namedArg.buildNode; const { wildcardSymbol } = nodeTypes.wildcard; } @@ -100,8 +99,7 @@ FieldRangeExpression suggestionTypes: ['conjunction'] }; } - const range = buildNamedArgNode(operator, value); - return buildFunctionNode('range', [field, range]); + return buildFunctionNode('range', [field, operator, value]); } FieldValueExpression diff --git a/packages/kbn-es-query/src/kuery/functions/geo_bounding_box.test.ts b/packages/kbn-es-query/src/kuery/functions/geo_bounding_box.test.ts deleted file mode 100644 index 7580765d59282..0000000000000 --- a/packages/kbn-es-query/src/kuery/functions/geo_bounding_box.test.ts +++ /dev/null @@ -1,137 +0,0 @@ -/* - * 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. - */ - -import { get } from 'lodash'; -import { nodeTypes } from '../node_types'; -import { fields } from '../../filters/stubs'; -import { DataViewBase } from '../..'; - -import * as geoBoundingBox from './geo_bounding_box'; -import { JsonObject } from '@kbn/utility-types'; - -jest.mock('../grammar'); - -const params = { - bottomRight: { - lat: 50.73, - lon: -135.35, - }, - topLeft: { - lat: 73.12, - lon: -174.37, - }, -}; - -describe('kuery functions', () => { - describe('geoBoundingBox', () => { - let indexPattern: DataViewBase; - - beforeEach(() => { - indexPattern = { - fields, - title: 'dataView', - }; - }); - - describe('buildNodeParams', () => { - test('should return an "arguments" param', () => { - const result = geoBoundingBox.buildNodeParams('geo', params); - - expect(result).toHaveProperty('arguments'); - expect(Object.keys(result).length).toBe(1); - }); - - test('arguments should contain the provided fieldName as a literal', () => { - const result = geoBoundingBox.buildNodeParams('geo', params); - const { - arguments: [fieldName], - } = result; - - expect(fieldName).toHaveProperty('type', 'literal'); - expect(fieldName).toHaveProperty('value', 'geo'); - }); - - test('arguments should contain the provided params as named arguments with "lat, lon" string values', () => { - const result = geoBoundingBox.buildNodeParams('geo', params); - const { - arguments: [, ...args], - } = result; - - args.map((param: any) => { - expect(param).toHaveProperty('type', 'namedArg'); - expect(['bottomRight', 'topLeft'].includes(param.name)).toBe(true); - expect(param.value.type).toBe('literal'); - - const { lat, lon } = get(params, param.name); - - expect(param.value.value).toBe(`${lat}, ${lon}`); - }); - }); - }); - - describe('toElasticsearchQuery', () => { - test('should return an ES geo_bounding_box query representing the given node', () => { - const node = nodeTypes.function.buildNode('geoBoundingBox', 'geo', params); - const result = geoBoundingBox.toElasticsearchQuery(node, indexPattern); - - expect(result).toHaveProperty('geo_bounding_box'); - expect((result.geo_bounding_box as JsonObject).geo).toHaveProperty( - 'top_left', - '73.12, -174.37' - ); - expect((result.geo_bounding_box as JsonObject).geo).toHaveProperty( - 'bottom_right', - '50.73, -135.35' - ); - }); - - test('should return an ES geo_bounding_box query without an index pattern', () => { - const node = nodeTypes.function.buildNode('geoBoundingBox', 'geo', params); - const result = geoBoundingBox.toElasticsearchQuery(node); - - expect(result).toHaveProperty('geo_bounding_box'); - expect((result.geo_bounding_box as JsonObject).geo).toHaveProperty( - 'top_left', - '73.12, -174.37' - ); - expect((result.geo_bounding_box as JsonObject).geo).toHaveProperty( - 'bottom_right', - '50.73, -135.35' - ); - }); - - test('should use the ignore_unmapped parameter', () => { - const node = nodeTypes.function.buildNode('geoBoundingBox', 'geo', params); - const result = geoBoundingBox.toElasticsearchQuery(node, indexPattern); - - expect(result.geo_bounding_box!.ignore_unmapped).toBe(true); - }); - - test('should throw an error for scripted fields', () => { - const node = nodeTypes.function.buildNode('geoBoundingBox', 'script number', params); - - expect(() => geoBoundingBox.toElasticsearchQuery(node, indexPattern)).toThrowError( - /Geo bounding box query does not support scripted fields/ - ); - }); - - test('should use a provided nested context to create a full field name', () => { - const node = nodeTypes.function.buildNode('geoBoundingBox', 'geo', params); - const result = geoBoundingBox.toElasticsearchQuery( - node, - indexPattern, - {}, - { nested: { path: 'nestedField' } } - ); - - expect(result).toHaveProperty('geo_bounding_box'); - expect((result.geo_bounding_box as JsonObject)['nestedField.geo']).toBeDefined(); - }); - }); - }); -}); diff --git a/packages/kbn-es-query/src/kuery/functions/geo_bounding_box.ts b/packages/kbn-es-query/src/kuery/functions/geo_bounding_box.ts deleted file mode 100644 index 1808b7a2c0106..0000000000000 --- a/packages/kbn-es-query/src/kuery/functions/geo_bounding_box.ts +++ /dev/null @@ -1,61 +0,0 @@ -/* - * 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. - */ - -import _ from 'lodash'; -import * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; -import { nodeTypes } from '../node_types'; -import * as ast from '../ast'; -import { IndexPatternBase, KueryNode, KueryQueryOptions, LatLon } from '../..'; - -export function buildNodeParams(fieldName: string, params: any) { - params = _.pick(params, 'topLeft', 'bottomRight'); - const fieldNameArg = nodeTypes.literal.buildNode(fieldName); - const args = _.map(params, (value: LatLon, key: string) => { - const latLon = `${value.lat}, ${value.lon}`; - return nodeTypes.namedArg.buildNode(key, latLon); - }); - - return { - arguments: [fieldNameArg, ...args], - }; -} - -export function toElasticsearchQuery( - node: KueryNode, - indexPattern?: IndexPatternBase, - config: KueryQueryOptions = {}, - context: Record = {} -): estypes.QueryDslQueryContainer { - const [fieldNameArg, ...args] = node.arguments; - const fullFieldNameArg = { - ...fieldNameArg, - value: context?.nested ? `${context.nested.path}.${fieldNameArg.value}` : fieldNameArg.value, - }; - const fieldName = nodeTypes.literal.toElasticsearchQuery(fullFieldNameArg) as string; - const fieldList = indexPattern?.fields ?? []; - const field = fieldList.find((fld) => fld.name === fieldName); - - const queryParams = args.reduce((acc: any, arg: any) => { - const snakeArgName = _.snakeCase(arg.name); - return { - ...acc, - [snakeArgName]: ast.toElasticsearchQuery(arg), - }; - }, {}); - - if (field?.scripted) { - throw new Error(`Geo bounding box query does not support scripted fields`); - } - - return { - geo_bounding_box: { - [fieldName]: queryParams, - ignore_unmapped: true, - }, - }; -} diff --git a/packages/kbn-es-query/src/kuery/functions/geo_polygon.test.ts b/packages/kbn-es-query/src/kuery/functions/geo_polygon.test.ts deleted file mode 100644 index 5a96f6f3cb03d..0000000000000 --- a/packages/kbn-es-query/src/kuery/functions/geo_polygon.test.ts +++ /dev/null @@ -1,134 +0,0 @@ -/* - * 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. - */ - -import { nodeTypes } from '../node_types'; -import { fields } from '../../filters/stubs'; -import { DataViewBase } from '../..'; - -import * as geoPolygon from './geo_polygon'; - -jest.mock('../grammar'); - -const points = [ - { - lat: 69.77, - lon: -171.56, - }, - { - lat: 50.06, - lon: -169.1, - }, - { - lat: 69.16, - lon: -125.85, - }, -]; - -describe('kuery functions', () => { - describe('geoPolygon', () => { - let indexPattern: DataViewBase; - - beforeEach(() => { - indexPattern = { - fields, - title: 'dataView', - }; - }); - - describe('buildNodeParams', () => { - test('should return an "arguments" param', () => { - const result = geoPolygon.buildNodeParams('geo', points); - - expect(result).toHaveProperty('arguments'); - expect(Object.keys(result).length).toBe(1); - }); - - test('arguments should contain the provided fieldName as a literal', () => { - const result = geoPolygon.buildNodeParams('geo', points); - const { - arguments: [fieldName], - } = result; - - expect(fieldName).toHaveProperty('type', 'literal'); - expect(fieldName).toHaveProperty('value', 'geo'); - }); - - test('arguments should contain the provided points literal "lat, lon" string values', () => { - const result = geoPolygon.buildNodeParams('geo', points); - const { - arguments: [, ...args], - } = result; - - args.forEach((param: any, index: number) => { - const expectedPoint = points[index]; - const expectedLatLon = `${expectedPoint.lat}, ${expectedPoint.lon}`; - - expect(param).toHaveProperty('type', 'literal'); - expect(param.value).toBe(expectedLatLon); - }); - }); - }); - - describe('toElasticsearchQuery', () => { - test('should return an ES geo_polygon query representing the given node', () => { - const node = nodeTypes.function.buildNode('geoPolygon', 'geo', points); - const result = geoPolygon.toElasticsearchQuery(node, indexPattern); - - expect(result).toHaveProperty('geo_polygon'); - expect((result.geo_polygon as any).geo).toHaveProperty('points'); - - (result.geo_polygon as any).geo.points.forEach((point: any, index: number) => { - const expectedLatLon = `${points[index].lat}, ${points[index].lon}`; - - expect(point).toBe(expectedLatLon); - }); - }); - - test('should return an ES geo_polygon query without an index pattern', () => { - const node = nodeTypes.function.buildNode('geoPolygon', 'geo', points); - const result = geoPolygon.toElasticsearchQuery(node); - - expect(result).toHaveProperty('geo_polygon'); - expect((result.geo_polygon as any).geo).toHaveProperty('points'); - - (result.geo_polygon as any).geo.points.forEach((point: any, index: number) => { - const expectedLatLon = `${points[index].lat}, ${points[index].lon}`; - - expect(point).toBe(expectedLatLon); - }); - }); - - test('should use the ignore_unmapped parameter', () => { - const node = nodeTypes.function.buildNode('geoPolygon', 'geo', points); - const result = geoPolygon.toElasticsearchQuery(node, indexPattern); - - expect((result.geo_polygon as any).ignore_unmapped).toBe(true); - }); - - test('should throw an error for scripted fields', () => { - const node = nodeTypes.function.buildNode('geoPolygon', 'script number', points); - expect(() => geoPolygon.toElasticsearchQuery(node, indexPattern)).toThrowError( - /Geo polygon query does not support scripted fields/ - ); - }); - - test('should use a provided nested context to create a full field name', () => { - const node = nodeTypes.function.buildNode('geoPolygon', 'geo', points); - const result = geoPolygon.toElasticsearchQuery( - node, - indexPattern, - {}, - { nested: { path: 'nestedField' } } - ); - - expect(result).toHaveProperty('geo_polygon'); - expect((result.geo_polygon as any)['nestedField.geo']).toBeDefined(); - }); - }); - }); -}); diff --git a/packages/kbn-es-query/src/kuery/functions/geo_polygon.ts b/packages/kbn-es-query/src/kuery/functions/geo_polygon.ts deleted file mode 100644 index 0cc95f8012a42..0000000000000 --- a/packages/kbn-es-query/src/kuery/functions/geo_polygon.ts +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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. - */ - -import * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; -import { nodeTypes } from '../node_types'; -import * as ast from '../ast'; -import { IndexPatternBase, KueryNode, KueryQueryOptions, LatLon } from '../..'; -import { LiteralTypeBuildNode } from '../node_types/types'; - -export function buildNodeParams(fieldName: string, points: LatLon[]) { - const fieldNameArg = nodeTypes.literal.buildNode(fieldName); - const args = points.map((point) => { - const latLon = `${point.lat}, ${point.lon}`; - return nodeTypes.literal.buildNode(latLon); - }); - - return { - arguments: [fieldNameArg, ...args], - }; -} - -export function toElasticsearchQuery( - node: KueryNode, - indexPattern?: IndexPatternBase, - config: KueryQueryOptions = {}, - context: Record = {} -): estypes.QueryDslQueryContainer { - const [fieldNameArg, ...points] = node.arguments; - const fullFieldNameArg = { - ...fieldNameArg, - value: context?.nested ? `${context.nested.path}.${fieldNameArg.value}` : fieldNameArg.value, - }; - const fieldName = nodeTypes.literal.toElasticsearchQuery(fullFieldNameArg) as string; - const fieldList = indexPattern?.fields ?? []; - const field = fieldList.find((fld) => fld.name === fieldName); - const queryParams = { - points: points.map((point: LiteralTypeBuildNode) => { - return ast.toElasticsearchQuery(point, indexPattern, config, context); - }), - }; - - if (field?.scripted) { - throw new Error(`Geo polygon query does not support scripted fields`); - } - - return { - geo_polygon: { - [fieldName]: queryParams, - ignore_unmapped: true, - }, - }; -} diff --git a/packages/kbn-es-query/src/kuery/functions/index.ts b/packages/kbn-es-query/src/kuery/functions/index.ts index e48382991b5d7..8e1b44517d3a1 100644 --- a/packages/kbn-es-query/src/kuery/functions/index.ts +++ b/packages/kbn-es-query/src/kuery/functions/index.ts @@ -12,8 +12,6 @@ import * as or from './or'; import * as not from './not'; import * as range from './range'; import * as exists from './exists'; -import * as geoBoundingBox from './geo_bounding_box'; -import * as geoPolygon from './geo_polygon'; import * as nested from './nested'; export const functions = { @@ -23,7 +21,5 @@ export const functions = { not, range, exists, - geoBoundingBox, - geoPolygon, nested, }; diff --git a/packages/kbn-es-query/src/kuery/functions/range.test.ts b/packages/kbn-es-query/src/kuery/functions/range.test.ts index 2a97a74ac385d..575246724d6b2 100644 --- a/packages/kbn-es-query/src/kuery/functions/range.test.ts +++ b/packages/kbn-es-query/src/kuery/functions/range.test.ts @@ -6,14 +6,13 @@ * Side Public License, v 1. */ -import { get } from 'lodash'; import { nodeTypes } from '../node_types'; import { fields } from '../../filters/stubs'; import { DataViewBase } from '../..'; -import { RangeFilterParams } from '../../filters'; import * as range from './range'; import * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; + jest.mock('../grammar'); describe('kuery functions', () => { @@ -29,7 +28,7 @@ describe('kuery functions', () => { describe('buildNodeParams', () => { test('arguments should contain the provided fieldName as a literal', () => { - const result = range.buildNodeParams('bytes', { gt: 1000, lt: 8000 }); + const result = range.buildNodeParams('bytes', 'gt', 1000); const { arguments: [fieldName], } = result; @@ -38,22 +37,14 @@ describe('kuery functions', () => { expect(fieldName).toHaveProperty('value', 'bytes'); }); - test('arguments should contain the provided params as named arguments', () => { - const givenParams: RangeFilterParams = { gt: 1000, lt: 8000, format: 'epoch_millis' }; - const result = range.buildNodeParams('bytes', givenParams); + test('arguments should contain the provided value as a literal', () => { + const result = range.buildNodeParams('bytes', 'gt', 1000); const { - arguments: [, ...params], + arguments: [, , valueArg], } = result; - expect(Array.isArray(params)).toBeTruthy(); - expect(params.length).toBeGreaterThan(1); - - params.map((param: any) => { - expect(param).toHaveProperty('type', 'namedArg'); - expect(['gt', 'lt', 'format'].includes(param.name)).toBe(true); - expect(param.value.type).toBe('literal'); - expect(param.value.value).toBe(get(givenParams, param.name)); - }); + expect(valueArg).toHaveProperty('type', 'literal'); + expect(valueArg).toHaveProperty('value', 1000); }); }); @@ -66,7 +57,6 @@ describe('kuery functions', () => { range: { bytes: { gt: 1000, - lt: 8000, }, }, }, @@ -74,7 +64,7 @@ describe('kuery functions', () => { minimum_should_match: 1, }, }; - const node = nodeTypes.function.buildNode('range', 'bytes', { gt: 1000, lt: 8000 }); + const node = nodeTypes.function.buildNode('range', 'bytes', 'gt', 1000); const result = range.toElasticsearchQuery(node, indexPattern); expect(result).toEqual(expected); @@ -88,7 +78,6 @@ describe('kuery functions', () => { range: { bytes: { gt: 1000, - lt: 8000, }, }, }, @@ -97,7 +86,7 @@ describe('kuery functions', () => { }, }; - const node = nodeTypes.function.buildNode('range', 'bytes', { gt: 1000, lt: 8000 }); + const node = nodeTypes.function.buildNode('range', 'bytes', 'gt', 1000); const result = range.toElasticsearchQuery(node); expect(result).toEqual(expected); @@ -111,7 +100,6 @@ describe('kuery functions', () => { range: { bytes: { gt: 1000, - lt: 8000, }, }, }, @@ -120,14 +108,14 @@ describe('kuery functions', () => { }, }; - const node = nodeTypes.function.buildNode('range', 'byt*', { gt: 1000, lt: 8000 }); + const node = nodeTypes.function.buildNode('range', 'byt*', 'gt', 1000); const result = range.toElasticsearchQuery(node, indexPattern); expect(result).toEqual(expected); }); test('should support scripted fields', () => { - const node = nodeTypes.function.buildNode('range', 'script number', { gt: 1000, lt: 8000 }); + const node = nodeTypes.function.buildNode('range', 'script number', 'gt', 1000); const result = range.toElasticsearchQuery(node, indexPattern); expect((result.bool!.should as estypes.QueryDslQueryContainer[])[0]).toHaveProperty( @@ -143,7 +131,6 @@ describe('kuery functions', () => { range: { '@timestamp': { gt: '2018-01-03T19:04:17', - lt: '2018-04-03T19:04:17', }, }, }, @@ -151,10 +138,12 @@ describe('kuery functions', () => { minimum_should_match: 1, }, }; - const node = nodeTypes.function.buildNode('range', '@timestamp', { - gt: '2018-01-03T19:04:17', - lt: '2018-04-03T19:04:17', - }); + const node = nodeTypes.function.buildNode( + 'range', + '@timestamp', + 'gt', + '2018-01-03T19:04:17' + ); const result = range.toElasticsearchQuery(node, indexPattern); expect(result).toEqual(expected); @@ -169,7 +158,6 @@ describe('kuery functions', () => { range: { '@timestamp': { gt: '2018-01-03T19:04:17', - lt: '2018-04-03T19:04:17', time_zone: 'America/Phoenix', }, }, @@ -178,10 +166,12 @@ describe('kuery functions', () => { minimum_should_match: 1, }, }; - const node = nodeTypes.function.buildNode('range', '@timestamp', { - gt: '2018-01-03T19:04:17', - lt: '2018-04-03T19:04:17', - }); + const node = nodeTypes.function.buildNode( + 'range', + '@timestamp', + 'gt', + '2018-01-03T19:04:17' + ); const result = range.toElasticsearchQuery(node, indexPattern, config); expect(result).toEqual(expected); @@ -195,7 +185,6 @@ describe('kuery functions', () => { range: { 'nestedField.bytes': { gt: 1000, - lt: 8000, }, }, }, @@ -203,7 +192,7 @@ describe('kuery functions', () => { minimum_should_match: 1, }, }; - const node = nodeTypes.function.buildNode('range', 'bytes', { gt: 1000, lt: 8000 }); + const node = nodeTypes.function.buildNode('range', 'bytes', 'gt', 1000); const result = range.toElasticsearchQuery( node, indexPattern, @@ -224,7 +213,6 @@ describe('kuery functions', () => { query: { range: { 'nestedField.nestedChild.doublyNestedChild': { - gt: 1000, lt: 8000, }, }, @@ -236,10 +224,7 @@ describe('kuery functions', () => { minimum_should_match: 1, }, }; - const node = nodeTypes.function.buildNode('range', '*doublyNested*', { - gt: 1000, - lt: 8000, - }); + const node = nodeTypes.function.buildNode('range', '*doublyNested*', 'lt', 8000); const result = range.toElasticsearchQuery(node, indexPattern); expect(result).toEqual(expected); diff --git a/packages/kbn-es-query/src/kuery/functions/range.ts b/packages/kbn-es-query/src/kuery/functions/range.ts index e016feb908bc7..6ebda78aec8a7 100644 --- a/packages/kbn-es-query/src/kuery/functions/range.ts +++ b/packages/kbn-es-query/src/kuery/functions/range.ts @@ -6,30 +6,24 @@ * Side Public License, v 1. */ -import { pick, map, mapValues } from 'lodash'; import * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; import { nodeTypes } from '../node_types'; import * as ast from '../ast'; import { getRangeScript, RangeFilterParams } from '../../filters'; import { getFields } from './utils/get_fields'; -import { getTimeZoneFromSettings, getDataViewFieldSubtypeNested } from '../../utils'; +import { getDataViewFieldSubtypeNested, getTimeZoneFromSettings } from '../../utils'; import { getFullFieldNameNode } from './utils/get_full_field_name_node'; -import { IndexPatternBase, KueryNode, KueryQueryOptions } from '../..'; +import type { IndexPatternBase, KueryNode, KueryQueryOptions } from '../..'; -export function buildNodeParams(fieldName: string, params: RangeFilterParams) { - const paramsToMap = pick(params, 'gt', 'lt', 'gte', 'lte', 'format'); - const fieldNameArg = - typeof fieldName === 'string' - ? ast.fromLiteralExpression(fieldName) - : nodeTypes.literal.buildNode(fieldName); - - const args = map(paramsToMap, (value: number | string, key: string) => { - return nodeTypes.namedArg.buildNode(key, value); - }); - - return { - arguments: [fieldNameArg, ...args], - }; +export function buildNodeParams( + fieldName: string, + operator: keyof Pick, + value: number | string +) { + // Run through the parser instead treating it as a literal because it may contain wildcards + const fieldNameArg = ast.fromLiteralExpression(fieldName); + const valueArg = nodeTypes.literal.buildNode(value); + return { arguments: [fieldNameArg, operator, valueArg] }; } export function toElasticsearchQuery( @@ -38,17 +32,13 @@ export function toElasticsearchQuery( config: KueryQueryOptions = {}, context: Record = {} ): estypes.QueryDslQueryContainer { - const [fieldNameArg, ...args] = node.arguments; + const [fieldNameArg, operatorArg, valueArg] = node.arguments; const fullFieldNameArg = getFullFieldNameNode( fieldNameArg, indexPattern, context?.nested ? context.nested.path : undefined ); const fields = indexPattern ? getFields(fullFieldNameArg, indexPattern) : []; - const namedArgs = extractArguments(args); - const queryParams = mapValues(namedArgs, (arg: KueryNode) => { - return ast.toElasticsearchQuery(arg); - }); // If no fields are found in the index pattern we send through the given field name as-is. We do this to preserve // the behaviour of lucene on dashboards where there are panels based on different index patterns that have different @@ -81,6 +71,10 @@ export function toElasticsearchQuery( } }; + const queryParams = { + [operatorArg]: ast.toElasticsearchQuery(valueArg), + }; + if (field.scripted) { return { script: getRangeScript(field, queryParams), @@ -112,21 +106,3 @@ export function toElasticsearchQuery( }, }; } - -function extractArguments(args: any) { - if ((args.gt && args.gte) || (args.lt && args.lte)) { - throw new Error('range ends cannot be both inclusive and exclusive'); - } - - const unnamedArgOrder = ['gte', 'lte', 'format']; - - return args.reduce((acc: any, arg: any, index: number) => { - if (arg.type === 'namedArg') { - acc[arg.name] = arg.value; - } else { - acc[unnamedArgOrder[index]] = arg; - } - - return acc; - }, {}); -} diff --git a/packages/kbn-es-query/src/kuery/grammar/__mocks__/grammar.js b/packages/kbn-es-query/src/kuery/grammar/__mocks__/grammar.js index 89c13bb2b05c2..4e5958459a6d2 100644 --- a/packages/kbn-es-query/src/kuery/grammar/__mocks__/grammar.js +++ b/packages/kbn-es-query/src/kuery/grammar/__mocks__/grammar.js @@ -11,10 +11,12 @@ // https://peggyjs.org/ /* eslint-disable */ -"use strict"; +'use strict'; function peg$subclass(child, parent) { - function C() { this.constructor = child; } + function C() { + this.constructor = child; + } C.prototype = parent.prototype; child.prototype = new C(); } @@ -27,22 +29,24 @@ function peg$SyntaxError(message, expected, found, location) { self.expected = expected; self.found = found; self.location = location; - self.name = "SyntaxError"; + self.name = 'SyntaxError'; return self; } peg$subclass(peg$SyntaxError, Error); function peg$padEnd(str, targetLength, padString) { - padString = padString || " "; - if (str.length > targetLength) { return str; } + padString = padString || ' '; + if (str.length > targetLength) { + return str; + } targetLength -= str.length; padString += padString.repeat(targetLength); return str + padString.slice(0, targetLength); } -peg$SyntaxError.prototype.format = function(sources) { - var str = "Error: " + this.message; +peg$SyntaxError.prototype.format = function (sources) { + var str = 'Error: ' + this.message; if (this.location) { var src = null; var k; @@ -53,51 +57,60 @@ peg$SyntaxError.prototype.format = function(sources) { } } var s = this.location.start; - var loc = this.location.source + ":" + s.line + ":" + s.column; + var loc = this.location.source + ':' + s.line + ':' + s.column; if (src) { var e = this.location.end; - var filler = peg$padEnd("", s.line.toString().length); + var filler = peg$padEnd('', s.line.toString().length); var line = src[s.line - 1]; var last = s.line === e.line ? e.column : line.length + 1; - str += "\n --> " + loc + "\n" - + filler + " |\n" - + s.line + " | " + line + "\n" - + filler + " | " + peg$padEnd("", s.column - 1) - + peg$padEnd("", last - s.column, "^"); + str += + '\n --> ' + + loc + + '\n' + + filler + + ' |\n' + + s.line + + ' | ' + + line + + '\n' + + filler + + ' | ' + + peg$padEnd('', s.column - 1) + + peg$padEnd('', last - s.column, '^'); } else { - str += "\n at " + loc; + str += '\n at ' + loc; } } return str; }; -peg$SyntaxError.buildMessage = function(expected, found) { +peg$SyntaxError.buildMessage = function (expected, found) { var DESCRIBE_EXPECTATION_FNS = { - literal: function(expectation) { - return "\"" + literalEscape(expectation.text) + "\""; + literal: function (expectation) { + return '"' + literalEscape(expectation.text) + '"'; }, - class: function(expectation) { - var escapedParts = expectation.parts.map(function(part) { + class: function (expectation) { + var escapedParts = expectation.parts.map(function (part) { return Array.isArray(part) - ? classEscape(part[0]) + "-" + classEscape(part[1]) + ? classEscape(part[0]) + '-' + classEscape(part[1]) : classEscape(part); }); - return "[" + (expectation.inverted ? "^" : "") + escapedParts + "]"; + return '[' + (expectation.inverted ? '^' : '') + escapedParts + ']'; }, - any: function() { - return "any character"; + any: function () { + return 'any character'; }, - end: function() { - return "end of input"; + end: function () { + return 'end of input'; }, - other: function(expectation) { + other: function (expectation) { return expectation.description; - } + }, }; function hex(ch) { @@ -106,28 +119,36 @@ peg$SyntaxError.buildMessage = function(expected, found) { function literalEscape(s) { return s - .replace(/\\/g, "\\\\") - .replace(/"/g, "\\\"") - .replace(/\0/g, "\\0") - .replace(/\t/g, "\\t") - .replace(/\n/g, "\\n") - .replace(/\r/g, "\\r") - .replace(/[\x00-\x0F]/g, function(ch) { return "\\x0" + hex(ch); }) - .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return "\\x" + hex(ch); }); + .replace(/\\/g, '\\\\') + .replace(/"/g, '\\"') + .replace(/\0/g, '\\0') + .replace(/\t/g, '\\t') + .replace(/\n/g, '\\n') + .replace(/\r/g, '\\r') + .replace(/[\x00-\x0F]/g, function (ch) { + return '\\x0' + hex(ch); + }) + .replace(/[\x10-\x1F\x7F-\x9F]/g, function (ch) { + return '\\x' + hex(ch); + }); } function classEscape(s) { return s - .replace(/\\/g, "\\\\") - .replace(/\]/g, "\\]") - .replace(/\^/g, "\\^") - .replace(/-/g, "\\-") - .replace(/\0/g, "\\0") - .replace(/\t/g, "\\t") - .replace(/\n/g, "\\n") - .replace(/\r/g, "\\r") - .replace(/[\x00-\x0F]/g, function(ch) { return "\\x0" + hex(ch); }) - .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return "\\x" + hex(ch); }); + .replace(/\\/g, '\\\\') + .replace(/\]/g, '\\]') + .replace(/\^/g, '\\^') + .replace(/-/g, '\\-') + .replace(/\0/g, '\\0') + .replace(/\t/g, '\\t') + .replace(/\n/g, '\\n') + .replace(/\r/g, '\\r') + .replace(/[\x00-\x0F]/g, function (ch) { + return '\\x0' + hex(ch); + }) + .replace(/[\x10-\x1F\x7F-\x9F]/g, function (ch) { + return '\\x' + hex(ch); + }); } function describeExpectation(expectation) { @@ -155,20 +176,20 @@ peg$SyntaxError.buildMessage = function(expected, found) { return descriptions[0]; case 2: - return descriptions[0] + " or " + descriptions[1]; + return descriptions[0] + ' or ' + descriptions[1]; default: - return descriptions.slice(0, -1).join(", ") - + ", or " - + descriptions[descriptions.length - 1]; + return ( + descriptions.slice(0, -1).join(', ') + ', or ' + descriptions[descriptions.length - 1] + ); } } function describeFound(found) { - return found ? "\"" + literalEscape(found) + "\"" : "end of input"; + return found ? '"' + literalEscape(found) + '"' : 'end of input'; } - return "Expected " + describeExpected(expected) + " but " + describeFound(found) + " found."; + return 'Expected ' + describeExpected(expected) + ' but ' + describeFound(found) + ' found.'; }; function peg$parse(input, options) { @@ -180,26 +201,26 @@ function peg$parse(input, options) { var peg$startRuleFunctions = { start: peg$parsestart, Literal: peg$parseLiteral }; var peg$startRuleFunction = peg$parsestart; - var peg$c0 = "("; - var peg$c1 = ")"; - var peg$c2 = ":"; - var peg$c3 = "{"; - var peg$c4 = "}"; - var peg$c5 = "or"; - var peg$c6 = "and"; - var peg$c7 = "not"; - var peg$c8 = "\""; - var peg$c9 = "\\"; - var peg$c10 = "*"; - var peg$c11 = "\\t"; - var peg$c12 = "\\r"; - var peg$c13 = "\\n"; - var peg$c14 = "u"; - var peg$c15 = "<="; - var peg$c16 = ">="; - var peg$c17 = "<"; - var peg$c18 = ">"; - var peg$c19 = "@kuery-cursor@"; + var peg$c0 = '('; + var peg$c1 = ')'; + var peg$c2 = ':'; + var peg$c3 = '{'; + var peg$c4 = '}'; + var peg$c5 = 'or'; + var peg$c6 = 'and'; + var peg$c7 = 'not'; + var peg$c8 = '"'; + var peg$c9 = '\\'; + var peg$c10 = '*'; + var peg$c11 = '\\t'; + var peg$c12 = '\\r'; + var peg$c13 = '\\n'; + var peg$c14 = 'u'; + var peg$c15 = '<='; + var peg$c16 = '>='; + var peg$c17 = '<'; + var peg$c18 = '>'; + var peg$c19 = '@kuery-cursor@'; var peg$r0 = /^[\\"]/; var peg$r1 = /^[^"]/; @@ -207,218 +228,272 @@ function peg$parse(input, options) { var peg$r3 = /^[0-9a-f]/i; var peg$r4 = /^[ \t\r\n\xA0]/; - var peg$e0 = peg$literalExpectation("(", false); - var peg$e1 = peg$literalExpectation(")", false); - var peg$e2 = peg$literalExpectation(":", false); - var peg$e3 = peg$literalExpectation("{", false); - var peg$e4 = peg$literalExpectation("}", false); - var peg$e5 = peg$otherExpectation("fieldName"); - var peg$e6 = peg$otherExpectation("value"); - var peg$e7 = peg$otherExpectation("OR"); - var peg$e8 = peg$literalExpectation("or", true); - var peg$e9 = peg$otherExpectation("AND"); - var peg$e10 = peg$literalExpectation("and", true); - var peg$e11 = peg$otherExpectation("NOT"); - var peg$e12 = peg$literalExpectation("not", true); - var peg$e13 = peg$otherExpectation("literal"); - var peg$e14 = peg$literalExpectation("\"", false); - var peg$e15 = peg$literalExpectation("\\", false); - var peg$e16 = peg$classExpectation(["\\", "\""], false, false); - var peg$e17 = peg$classExpectation(["\""], true, false); + var peg$e0 = peg$literalExpectation('(', false); + var peg$e1 = peg$literalExpectation(')', false); + var peg$e2 = peg$literalExpectation(':', false); + var peg$e3 = peg$literalExpectation('{', false); + var peg$e4 = peg$literalExpectation('}', false); + var peg$e5 = peg$otherExpectation('fieldName'); + var peg$e6 = peg$otherExpectation('value'); + var peg$e7 = peg$otherExpectation('OR'); + var peg$e8 = peg$literalExpectation('or', true); + var peg$e9 = peg$otherExpectation('AND'); + var peg$e10 = peg$literalExpectation('and', true); + var peg$e11 = peg$otherExpectation('NOT'); + var peg$e12 = peg$literalExpectation('not', true); + var peg$e13 = peg$otherExpectation('literal'); + var peg$e14 = peg$literalExpectation('"', false); + var peg$e15 = peg$literalExpectation('\\', false); + var peg$e16 = peg$classExpectation(['\\', '"'], false, false); + var peg$e17 = peg$classExpectation(['"'], true, false); var peg$e18 = peg$anyExpectation(); - var peg$e19 = peg$literalExpectation("*", false); - var peg$e20 = peg$literalExpectation("\\t", false); - var peg$e21 = peg$literalExpectation("\\r", false); - var peg$e22 = peg$literalExpectation("\\n", false); - var peg$e23 = peg$classExpectation(["\\", "(", ")", ":", "<", ">", "\"", "*", "{", "}"], false, false); - var peg$e24 = peg$literalExpectation("u", false); - var peg$e25 = peg$classExpectation([["0", "9"], ["a", "f"]], false, true); - var peg$e26 = peg$literalExpectation("<=", false); - var peg$e27 = peg$literalExpectation(">=", false); - var peg$e28 = peg$literalExpectation("<", false); - var peg$e29 = peg$literalExpectation(">", false); - var peg$e30 = peg$otherExpectation("whitespace"); - var peg$e31 = peg$classExpectation([" ", "\t", "\r", "\n", "\xA0"], false, false); - var peg$e32 = peg$literalExpectation("@kuery-cursor@", false); - - var peg$f0 = function(query, trailing) { - if (trailing.type === 'cursor') { - return { - ...trailing, - suggestionTypes: ['conjunction'] - }; - } - if (query !== null) return query; - return nodeTypes.function.buildNode('is', '*', '*'); - }; - var peg$f1 = function(head, query) { return query; }; - var peg$f2 = function(head, tail) { - const nodes = [head, ...tail]; - const cursor = parseCursor && nodes.find(node => node.type === 'cursor'); - if (cursor) return cursor; - return buildFunctionNode('or', nodes); - }; - var peg$f3 = function(head, tail) { - const nodes = [head, ...tail]; - const cursor = parseCursor && nodes.find(node => node.type === 'cursor'); - if (cursor) return cursor; - return buildFunctionNode('and', nodes); - }; - var peg$f4 = function(query) { - if (query.type === 'cursor') return query; - return buildFunctionNode('not', [query]); - }; - var peg$f5 = function(query, trailing) { - if (trailing.type === 'cursor') { - return { - ...trailing, - suggestionTypes: ['conjunction'] - }; - } - return query; - }; - var peg$f6 = function(field, query, trailing) { - if (query.type === 'cursor') { - return { - ...query, - nestedPath: query.nestedPath ? `${field.value}.${query.nestedPath}` : field.value, - } - }; - - if (trailing.type === 'cursor') { - return { - ...trailing, - suggestionTypes: ['conjunction'] - }; - } - return buildFunctionNode('nested', [field, query]); + var peg$e19 = peg$literalExpectation('*', false); + var peg$e20 = peg$literalExpectation('\\t', false); + var peg$e21 = peg$literalExpectation('\\r', false); + var peg$e22 = peg$literalExpectation('\\n', false); + var peg$e23 = peg$classExpectation( + ['\\', '(', ')', ':', '<', '>', '"', '*', '{', '}'], + false, + false + ); + var peg$e24 = peg$literalExpectation('u', false); + var peg$e25 = peg$classExpectation( + [ + ['0', '9'], + ['a', 'f'], + ], + false, + true + ); + var peg$e26 = peg$literalExpectation('<=', false); + var peg$e27 = peg$literalExpectation('>=', false); + var peg$e28 = peg$literalExpectation('<', false); + var peg$e29 = peg$literalExpectation('>', false); + var peg$e30 = peg$otherExpectation('whitespace'); + var peg$e31 = peg$classExpectation([' ', '\t', '\r', '\n', '\xA0'], false, false); + var peg$e32 = peg$literalExpectation('@kuery-cursor@', false); + + var peg$f0 = function (query, trailing) { + if (trailing.type === 'cursor') { + return { + ...trailing, + suggestionTypes: ['conjunction'], }; - var peg$f7 = function(field, operator, value) { - if (value.type === 'cursor') { - return { - ...value, - suggestionTypes: ['conjunction'] - }; - } - const range = buildNamedArgNode(operator, value); - return buildFunctionNode('range', [field, range]); - }; - var peg$f8 = function(field, partial) { - if (partial.type === 'cursor') { - return { - ...partial, - fieldName: field.value, - suggestionTypes: ['value', 'conjunction'] - }; - } - return partial(field); - }; - var peg$f9 = function(partial) { - if (partial.type === 'cursor') { - const fieldName = `${partial.prefix}${partial.suffix}`.trim(); - return { - ...partial, - fieldName, - suggestionTypes: ['field', 'operator', 'conjunction'] - }; - } - const field = buildLiteralNode(null); - return partial(field); - }; - var peg$f10 = function(partial, trailing) { - if (trailing.type === 'cursor') { - return { - ...trailing, - suggestionTypes: ['conjunction'] - }; - } - return partial; - }; - var peg$f11 = function(head, partial) { return partial; }; - var peg$f12 = function(head, tail) { - const nodes = [head, ...tail]; - const cursor = parseCursor && nodes.find(node => node.type === 'cursor'); - if (cursor) { - return { - ...cursor, - suggestionTypes: ['value'] - }; - } - return (field) => buildFunctionNode('or', nodes.map(partial => partial(field))); - }; - var peg$f13 = function(head, tail) { - const nodes = [head, ...tail]; - const cursor = parseCursor && nodes.find(node => node.type === 'cursor'); - if (cursor) { - return { - ...cursor, - suggestionTypes: ['value'] - }; - } - return (field) => buildFunctionNode('and', nodes.map(partial => partial(field))); - }; - var peg$f14 = function(partial) { - if (partial.type === 'cursor') { - return { - ...list, - suggestionTypes: ['value'] - }; - } - return (field) => buildFunctionNode('not', [partial(field)]); - }; - var peg$f15 = function(value) { - if (value.type === 'cursor') return value; - const isPhrase = buildLiteralNode(true); - return (field) => buildFunctionNode('is', [field, value, isPhrase]); - }; - var peg$f16 = function(value) { - if (value.type === 'cursor') return value; - - if (!allowLeadingWildcards && value.type === 'wildcard' && nodeTypes.wildcard.hasLeadingWildcard(value)) { - error('Leading wildcards are disabled. See query:allowLeadingWildcards in Advanced Settings.'); - } + } + if (query !== null) return query; + return nodeTypes.function.buildNode('is', '*', '*'); + }; + var peg$f1 = function (head, query) { + return query; + }; + var peg$f2 = function (head, tail) { + const nodes = [head, ...tail]; + const cursor = parseCursor && nodes.find((node) => node.type === 'cursor'); + if (cursor) return cursor; + return buildFunctionNode('or', nodes); + }; + var peg$f3 = function (head, tail) { + const nodes = [head, ...tail]; + const cursor = parseCursor && nodes.find((node) => node.type === 'cursor'); + if (cursor) return cursor; + return buildFunctionNode('and', nodes); + }; + var peg$f4 = function (query) { + if (query.type === 'cursor') return query; + return buildFunctionNode('not', [query]); + }; + var peg$f5 = function (query, trailing) { + if (trailing.type === 'cursor') { + return { + ...trailing, + suggestionTypes: ['conjunction'], + }; + } + return query; + }; + var peg$f6 = function (field, query, trailing) { + if (query.type === 'cursor') { + return { + ...query, + nestedPath: query.nestedPath ? `${field.value}.${query.nestedPath}` : field.value, + }; + } - const isPhrase = buildLiteralNode(false); - return (field) => buildFunctionNode('is', [field, value, isPhrase]); - }; - var peg$f17 = function() { return parseCursor; }; - var peg$f18 = function(prefix, cursor, suffix) { - const { start, end } = location(); + if (trailing.type === 'cursor') { return { - type: 'cursor', - start: start.offset, - end: end.offset - cursor.length, - prefix: prefix.join(''), - suffix: suffix.join(''), - text: text().replace(cursor, '') + ...trailing, + suggestionTypes: ['conjunction'], }; - }; - var peg$f19 = function(chars) { - return buildLiteralNode(chars.join('')); - }; - var peg$f20 = function(char) { return char; }; - var peg$f21 = function(chars) { - const sequence = chars.join('').trim(); - if (sequence === 'null') return buildLiteralNode(null); - if (sequence === 'true') return buildLiteralNode(true); - if (sequence === 'false') return buildLiteralNode(false); - if (chars.includes(wildcardSymbol)) return buildWildcardNode(sequence); - return buildLiteralNode(sequence); - }; - var peg$f22 = function() { return wildcardSymbol; }; - var peg$f23 = function() { return '\t'; }; - var peg$f24 = function() { return '\r'; }; - var peg$f25 = function() { return '\n'; }; - var peg$f26 = function(keyword) { return keyword; }; - var peg$f27 = function(sequence) { return sequence; }; - var peg$f28 = function(digits) { - return String.fromCharCode(parseInt(digits, 16)); + } + return buildFunctionNode('nested', [field, query]); + }; + var peg$f7 = function (field, operator, value) { + if (value.type === 'cursor') { + return { + ...value, + suggestionTypes: ['conjunction'], + }; + } + return buildFunctionNode('range', [field, operator, value]); + }; + var peg$f8 = function (field, partial) { + if (partial.type === 'cursor') { + return { + ...partial, + fieldName: field.value, + suggestionTypes: ['value', 'conjunction'], + }; + } + return partial(field); + }; + var peg$f9 = function (partial) { + if (partial.type === 'cursor') { + const fieldName = `${partial.prefix}${partial.suffix}`.trim(); + return { + ...partial, + fieldName, + suggestionTypes: ['field', 'operator', 'conjunction'], + }; + } + const field = buildLiteralNode(null); + return partial(field); + }; + var peg$f10 = function (partial, trailing) { + if (trailing.type === 'cursor') { + return { + ...trailing, + suggestionTypes: ['conjunction'], + }; + } + return partial; + }; + var peg$f11 = function (head, partial) { + return partial; + }; + var peg$f12 = function (head, tail) { + const nodes = [head, ...tail]; + const cursor = parseCursor && nodes.find((node) => node.type === 'cursor'); + if (cursor) { + return { + ...cursor, + suggestionTypes: ['value'], + }; + } + return (field) => + buildFunctionNode( + 'or', + nodes.map((partial) => partial(field)) + ); + }; + var peg$f13 = function (head, tail) { + const nodes = [head, ...tail]; + const cursor = parseCursor && nodes.find((node) => node.type === 'cursor'); + if (cursor) { + return { + ...cursor, + suggestionTypes: ['value'], }; - var peg$f29 = function() { return 'lte'; }; - var peg$f30 = function() { return 'gte'; }; - var peg$f31 = function() { return 'lt'; }; - var peg$f32 = function() { return 'gt'; }; - var peg$f33 = function() { return cursorSymbol; }; + } + return (field) => + buildFunctionNode( + 'and', + nodes.map((partial) => partial(field)) + ); + }; + var peg$f14 = function (partial) { + if (partial.type === 'cursor') { + return { + ...list, + suggestionTypes: ['value'], + }; + } + return (field) => buildFunctionNode('not', [partial(field)]); + }; + var peg$f15 = function (value) { + if (value.type === 'cursor') return value; + const isPhrase = buildLiteralNode(true); + return (field) => buildFunctionNode('is', [field, value, isPhrase]); + }; + var peg$f16 = function (value) { + if (value.type === 'cursor') return value; + + if ( + !allowLeadingWildcards && + value.type === 'wildcard' && + nodeTypes.wildcard.hasLeadingWildcard(value) + ) { + error( + 'Leading wildcards are disabled. See query:allowLeadingWildcards in Advanced Settings.' + ); + } + + const isPhrase = buildLiteralNode(false); + return (field) => buildFunctionNode('is', [field, value, isPhrase]); + }; + var peg$f17 = function () { + return parseCursor; + }; + var peg$f18 = function (prefix, cursor, suffix) { + const { start, end } = location(); + return { + type: 'cursor', + start: start.offset, + end: end.offset - cursor.length, + prefix: prefix.join(''), + suffix: suffix.join(''), + text: text().replace(cursor, ''), + }; + }; + var peg$f19 = function (chars) { + return buildLiteralNode(chars.join('')); + }; + var peg$f20 = function (char) { + return char; + }; + var peg$f21 = function (chars) { + const sequence = chars.join('').trim(); + if (sequence === 'null') return buildLiteralNode(null); + if (sequence === 'true') return buildLiteralNode(true); + if (sequence === 'false') return buildLiteralNode(false); + if (chars.includes(wildcardSymbol)) return buildWildcardNode(sequence); + return buildLiteralNode(sequence); + }; + var peg$f22 = function () { + return wildcardSymbol; + }; + var peg$f23 = function () { + return '\t'; + }; + var peg$f24 = function () { + return '\r'; + }; + var peg$f25 = function () { + return '\n'; + }; + var peg$f26 = function (keyword) { + return keyword; + }; + var peg$f27 = function (sequence) { + return sequence; + }; + var peg$f28 = function (digits) { + return String.fromCharCode(parseInt(digits, 16)); + }; + var peg$f29 = function () { + return 'lte'; + }; + var peg$f30 = function () { + return 'gte'; + }; + var peg$f31 = function () { + return 'lt'; + }; + var peg$f32 = function () { + return 'gt'; + }; + var peg$f33 = function () { + return cursorSymbol; + }; var peg$currPos = 0; var peg$savedPos = 0; @@ -429,9 +504,9 @@ function peg$parse(input, options) { var peg$result; - if ("startRule" in options) { + if ('startRule' in options) { if (!(options.startRule in peg$startRuleFunctions)) { - throw new Error("Can't start parsing from rule \"" + options.startRule + "\"."); + throw new Error('Can\'t start parsing from rule "' + options.startRule + '".'); } peg$startRuleFunction = peg$startRuleFunctions[options.startRule]; @@ -449,7 +524,7 @@ function peg$parse(input, options) { return { source: peg$source, start: peg$savedPos, - end: peg$currPos + end: peg$currPos, }; } @@ -458,9 +533,7 @@ function peg$parse(input, options) { } function expected(description, location) { - location = location !== undefined - ? location - : peg$computeLocation(peg$savedPos, peg$currPos); + location = location !== undefined ? location : peg$computeLocation(peg$savedPos, peg$currPos); throw peg$buildStructuredError( [peg$otherExpectation(description)], @@ -470,31 +543,29 @@ function peg$parse(input, options) { } function error(message, location) { - location = location !== undefined - ? location - : peg$computeLocation(peg$savedPos, peg$currPos); + location = location !== undefined ? location : peg$computeLocation(peg$savedPos, peg$currPos); throw peg$buildSimpleError(message, location); } function peg$literalExpectation(text, ignoreCase) { - return { type: "literal", text: text, ignoreCase: ignoreCase }; + return { type: 'literal', text: text, ignoreCase: ignoreCase }; } function peg$classExpectation(parts, inverted, ignoreCase) { - return { type: "class", parts: parts, inverted: inverted, ignoreCase: ignoreCase }; + return { type: 'class', parts: parts, inverted: inverted, ignoreCase: ignoreCase }; } function peg$anyExpectation() { - return { type: "any" }; + return { type: 'any' }; } function peg$endExpectation() { - return { type: "end" }; + return { type: 'end' }; } function peg$otherExpectation(description) { - return { type: "other", description: description }; + return { type: 'other', description: description }; } function peg$computePosDetails(pos) { @@ -512,7 +583,7 @@ function peg$parse(input, options) { details = peg$posDetailsCache[p]; details = { line: details.line, - column: details.column + column: details.column, }; while (p < pos) { @@ -541,18 +612,20 @@ function peg$parse(input, options) { start: { offset: startPos, line: startPosDetails.line, - column: startPosDetails.column + column: startPosDetails.column, }, end: { offset: endPos, line: endPosDetails.line, - column: endPosDetails.column - } + column: endPosDetails.column, + }, }; } function peg$fail(expected) { - if (peg$currPos < peg$maxFailPos) { return; } + if (peg$currPos < peg$maxFailPos) { + return; + } if (peg$currPos > peg$maxFailPos) { peg$maxFailPos = peg$currPos; @@ -759,7 +832,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e0); } + if (peg$silentFails === 0) { + peg$fail(peg$e0); + } } if (s1 !== peg$FAILED) { s2 = []; @@ -777,7 +852,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s5 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e1); } + if (peg$silentFails === 0) { + peg$fail(peg$e1); + } } if (s5 !== peg$FAILED) { peg$savedPos = s0; @@ -822,7 +899,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s3 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e2); } + if (peg$silentFails === 0) { + peg$fail(peg$e2); + } } if (s3 !== peg$FAILED) { s4 = []; @@ -836,7 +915,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s5 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e3); } + if (peg$silentFails === 0) { + peg$fail(peg$e3); + } } if (s5 !== peg$FAILED) { s6 = []; @@ -854,7 +935,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s9 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e4); } + if (peg$silentFails === 0) { + peg$fail(peg$e4); + } } if (s9 !== peg$FAILED) { peg$savedPos = s0; @@ -912,7 +995,9 @@ function peg$parse(input, options) { peg$silentFails--; if (s0 === peg$FAILED) { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e5); } + if (peg$silentFails === 0) { + peg$fail(peg$e5); + } } return s0; @@ -975,7 +1060,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s3 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e2); } + if (peg$silentFails === 0) { + peg$fail(peg$e2); + } } if (s3 !== peg$FAILED) { s4 = []; @@ -1027,7 +1114,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e0); } + if (peg$silentFails === 0) { + peg$fail(peg$e0); + } } if (s1 !== peg$FAILED) { s2 = []; @@ -1045,7 +1134,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s5 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e1); } + if (peg$silentFails === 0) { + peg$fail(peg$e1); + } } if (s5 !== peg$FAILED) { peg$savedPos = s0; @@ -1245,7 +1336,9 @@ function peg$parse(input, options) { peg$silentFails--; if (s0 === peg$FAILED) { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e6); } + if (peg$silentFails === 0) { + peg$fail(peg$e6); + } } return s0; @@ -1272,7 +1365,9 @@ function peg$parse(input, options) { peg$currPos += 2; } else { s2 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e8); } + if (peg$silentFails === 0) { + peg$fail(peg$e8); + } } if (s2 !== peg$FAILED) { s3 = []; @@ -1303,7 +1398,9 @@ function peg$parse(input, options) { peg$silentFails--; if (s0 === peg$FAILED) { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e7); } + if (peg$silentFails === 0) { + peg$fail(peg$e7); + } } return s0; @@ -1330,7 +1427,9 @@ function peg$parse(input, options) { peg$currPos += 3; } else { s2 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e10); } + if (peg$silentFails === 0) { + peg$fail(peg$e10); + } } if (s2 !== peg$FAILED) { s3 = []; @@ -1361,7 +1460,9 @@ function peg$parse(input, options) { peg$silentFails--; if (s0 === peg$FAILED) { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e9); } + if (peg$silentFails === 0) { + peg$fail(peg$e9); + } } return s0; @@ -1377,7 +1478,9 @@ function peg$parse(input, options) { peg$currPos += 3; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e12); } + if (peg$silentFails === 0) { + peg$fail(peg$e12); + } } if (s1 !== peg$FAILED) { s2 = []; @@ -1404,7 +1507,9 @@ function peg$parse(input, options) { peg$silentFails--; if (s0 === peg$FAILED) { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e11); } + if (peg$silentFails === 0) { + peg$fail(peg$e11); + } } return s0; @@ -1421,7 +1526,9 @@ function peg$parse(input, options) { peg$silentFails--; if (s0 === peg$FAILED) { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e13); } + if (peg$silentFails === 0) { + peg$fail(peg$e13); + } } return s0; @@ -1444,7 +1551,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s2 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e14); } + if (peg$silentFails === 0) { + peg$fail(peg$e14); + } } if (s2 !== peg$FAILED) { s3 = []; @@ -1466,7 +1575,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s6 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e14); } + if (peg$silentFails === 0) { + peg$fail(peg$e14); + } } if (s6 !== peg$FAILED) { peg$savedPos = s0; @@ -1494,7 +1605,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e14); } + if (peg$silentFails === 0) { + peg$fail(peg$e14); + } } if (s1 !== peg$FAILED) { s2 = []; @@ -1508,7 +1621,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s3 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e14); } + if (peg$silentFails === 0) { + peg$fail(peg$e14); + } } if (s3 !== peg$FAILED) { peg$savedPos = s0; @@ -1539,7 +1654,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e15); } + if (peg$silentFails === 0) { + peg$fail(peg$e15); + } } if (s1 !== peg$FAILED) { if (peg$r0.test(input.charAt(peg$currPos))) { @@ -1547,7 +1664,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s2 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e16); } + if (peg$silentFails === 0) { + peg$fail(peg$e16); + } } if (s2 !== peg$FAILED) { peg$savedPos = s0; @@ -1578,7 +1697,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s2 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e17); } + if (peg$silentFails === 0) { + peg$fail(peg$e17); + } } if (s2 !== peg$FAILED) { peg$savedPos = s0; @@ -1708,7 +1829,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s4 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e18); } + if (peg$silentFails === 0) { + peg$fail(peg$e18); + } } if (s4 !== peg$FAILED) { peg$savedPos = s0; @@ -1747,7 +1870,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e19); } + if (peg$silentFails === 0) { + peg$fail(peg$e19); + } } if (s1 !== peg$FAILED) { peg$savedPos = s0; @@ -1815,7 +1940,9 @@ function peg$parse(input, options) { peg$currPos += 2; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e20); } + if (peg$silentFails === 0) { + peg$fail(peg$e20); + } } if (s1 !== peg$FAILED) { peg$savedPos = s0; @@ -1829,7 +1956,9 @@ function peg$parse(input, options) { peg$currPos += 2; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e21); } + if (peg$silentFails === 0) { + peg$fail(peg$e21); + } } if (s1 !== peg$FAILED) { peg$savedPos = s0; @@ -1843,7 +1972,9 @@ function peg$parse(input, options) { peg$currPos += 2; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e22); } + if (peg$silentFails === 0) { + peg$fail(peg$e22); + } } if (s1 !== peg$FAILED) { peg$savedPos = s0; @@ -1865,7 +1996,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e15); } + if (peg$silentFails === 0) { + peg$fail(peg$e15); + } } if (s1 !== peg$FAILED) { s2 = peg$parseSpecialCharacter(); @@ -1893,7 +2026,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e15); } + if (peg$silentFails === 0) { + peg$fail(peg$e15); + } } if (s1 !== peg$FAILED) { if (input.substr(peg$currPos, 2).toLowerCase() === peg$c5) { @@ -1901,7 +2036,9 @@ function peg$parse(input, options) { peg$currPos += 2; } else { s2 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e8); } + if (peg$silentFails === 0) { + peg$fail(peg$e8); + } } if (s2 === peg$FAILED) { if (input.substr(peg$currPos, 3).toLowerCase() === peg$c6) { @@ -1909,7 +2046,9 @@ function peg$parse(input, options) { peg$currPos += 3; } else { s2 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e10); } + if (peg$silentFails === 0) { + peg$fail(peg$e10); + } } if (s2 === peg$FAILED) { if (input.substr(peg$currPos, 3).toLowerCase() === peg$c7) { @@ -1917,7 +2056,9 @@ function peg$parse(input, options) { peg$currPos += 3; } else { s2 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e12); } + if (peg$silentFails === 0) { + peg$fail(peg$e12); + } } } } @@ -1958,7 +2099,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s0 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e23); } + if (peg$silentFails === 0) { + peg$fail(peg$e23); + } } return s0; @@ -1973,7 +2116,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e15); } + if (peg$silentFails === 0) { + peg$fail(peg$e15); + } } if (s1 !== peg$FAILED) { s2 = peg$parseUnicodeSequence(); @@ -2001,7 +2146,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e24); } + if (peg$silentFails === 0) { + peg$fail(peg$e24); + } } if (s1 !== peg$FAILED) { s2 = peg$currPos; @@ -2060,7 +2207,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s0 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e25); } + if (peg$silentFails === 0) { + peg$fail(peg$e25); + } } return s0; @@ -2075,7 +2224,9 @@ function peg$parse(input, options) { peg$currPos += 2; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e26); } + if (peg$silentFails === 0) { + peg$fail(peg$e26); + } } if (s1 !== peg$FAILED) { peg$savedPos = s0; @@ -2089,7 +2240,9 @@ function peg$parse(input, options) { peg$currPos += 2; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e27); } + if (peg$silentFails === 0) { + peg$fail(peg$e27); + } } if (s1 !== peg$FAILED) { peg$savedPos = s0; @@ -2103,7 +2256,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e28); } + if (peg$silentFails === 0) { + peg$fail(peg$e28); + } } if (s1 !== peg$FAILED) { peg$savedPos = s0; @@ -2117,7 +2272,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e29); } + if (peg$silentFails === 0) { + peg$fail(peg$e29); + } } if (s1 !== peg$FAILED) { peg$savedPos = s0; @@ -2140,12 +2297,16 @@ function peg$parse(input, options) { peg$currPos++; } else { s0 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e31); } + if (peg$silentFails === 0) { + peg$fail(peg$e31); + } } peg$silentFails--; if (s0 === peg$FAILED) { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e30); } + if (peg$silentFails === 0) { + peg$fail(peg$e30); + } } return s0; @@ -2168,7 +2329,9 @@ function peg$parse(input, options) { peg$currPos += 14; } else { s2 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e32); } + if (peg$silentFails === 0) { + peg$fail(peg$e32); + } } if (s2 !== peg$FAILED) { peg$savedPos = s0; @@ -2185,14 +2348,16 @@ function peg$parse(input, options) { return s0; } - - const { parseCursor, cursorSymbol, allowLeadingWildcards = true, helpers: { nodeTypes } } = options; - const buildFunctionNode = nodeTypes.function.buildNodeWithArgumentNodes; - const buildLiteralNode = nodeTypes.literal.buildNode; - const buildWildcardNode = nodeTypes.wildcard.buildNode; - const buildNamedArgNode = nodeTypes.namedArg.buildNode; - const { wildcardSymbol } = nodeTypes.wildcard; - + const { + parseCursor, + cursorSymbol, + allowLeadingWildcards = true, + helpers: { nodeTypes }, + } = options; + const buildFunctionNode = nodeTypes.function.buildNodeWithArgumentNodes; + const buildLiteralNode = nodeTypes.literal.buildNode; + const buildWildcardNode = nodeTypes.wildcard.buildNode; + const { wildcardSymbol } = nodeTypes.wildcard; peg$result = peg$startRuleFunction(); @@ -2215,5 +2380,5 @@ function peg$parse(input, options) { module.exports = { SyntaxError: peg$SyntaxError, - parse: peg$parse + parse: peg$parse, }; diff --git a/packages/kbn-es-query/src/kuery/node_types/index.ts b/packages/kbn-es-query/src/kuery/node_types/index.ts index e116b44205853..4f30e635e1661 100644 --- a/packages/kbn-es-query/src/kuery/node_types/index.ts +++ b/packages/kbn-es-query/src/kuery/node_types/index.ts @@ -8,7 +8,6 @@ import * as functionType from './function'; import * as literal from './literal'; -import * as namedArg from './named_arg'; import * as wildcard from './wildcard'; import { NodeTypes } from './types'; @@ -23,6 +22,5 @@ export const nodeTypes: NodeTypes = { // @ts-ignore function: functionType, literal, - namedArg, wildcard, }; diff --git a/packages/kbn-es-query/src/kuery/node_types/named_arg.test.ts b/packages/kbn-es-query/src/kuery/node_types/named_arg.test.ts deleted file mode 100644 index fa944660288d5..0000000000000 --- a/packages/kbn-es-query/src/kuery/node_types/named_arg.test.ts +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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. - */ - -import { nodeTypes } from './index'; -import { buildNode, toElasticsearchQuery } from './named_arg'; - -jest.mock('../grammar'); - -describe('kuery node types', () => { - describe('named arg', () => { - describe('buildNode', () => { - test('should return a node representing a named argument with the given value', () => { - const result = buildNode('fieldName', 'foo'); - expect(result).toHaveProperty('type', 'namedArg'); - expect(result).toHaveProperty('name', 'fieldName'); - expect(result).toHaveProperty('value'); - - const literalValue = result.value; - expect(literalValue).toHaveProperty('type', 'literal'); - expect(literalValue).toHaveProperty('value', 'foo'); - }); - - test('should support literal nodes as values', () => { - const value = nodeTypes.literal.buildNode('foo'); - const result = buildNode('fieldName', value); - - expect(result.value).toBe(value); - expect(result.value).toEqual(value); - }); - }); - - describe('toElasticsearchQuery', () => { - test('should return the argument value represented by the given node', () => { - const node = buildNode('fieldName', 'foo'); - const result = toElasticsearchQuery(node); - - expect(result).toBe('foo'); - }); - }); - }); -}); diff --git a/packages/kbn-es-query/src/kuery/node_types/named_arg.ts b/packages/kbn-es-query/src/kuery/node_types/named_arg.ts deleted file mode 100644 index 1892a4885b705..0000000000000 --- a/packages/kbn-es-query/src/kuery/node_types/named_arg.ts +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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. - */ - -import _ from 'lodash'; -import { JsonObject } from '@kbn/utility-types'; -import * as ast from '../ast'; -import { nodeTypes } from '../node_types'; -import { NamedArgTypeBuildNode } from './types'; - -export function buildNode(name: string, value: any): NamedArgTypeBuildNode { - const argumentNode = - _.get(value, 'type') === 'literal' ? value : nodeTypes.literal.buildNode(value); - return { - type: 'namedArg', - name, - value: argumentNode, - }; -} - -export function toElasticsearchQuery(node: any): JsonObject { - return ast.toElasticsearchQuery(node.value); -} diff --git a/packages/kbn-es-query/src/kuery/node_types/types.ts b/packages/kbn-es-query/src/kuery/node_types/types.ts index 8ee83971c833b..6df292b9929fd 100644 --- a/packages/kbn-es-query/src/kuery/node_types/types.ts +++ b/packages/kbn-es-query/src/kuery/node_types/types.ts @@ -14,16 +14,7 @@ import { JsonValue } from '@kbn/utility-types'; import { KueryNode, KueryQueryOptions } from '..'; import { IndexPatternBase } from '../..'; -export type FunctionName = - | 'is' - | 'and' - | 'or' - | 'not' - | 'range' - | 'exists' - | 'geoBoundingBox' - | 'geoPolygon' - | 'nested'; +export type FunctionName = 'is' | 'and' | 'or' | 'not' | 'range' | 'exists' | 'nested'; interface FunctionType { buildNode: (functionName: FunctionName, ...args: any[]) => FunctionTypeBuildNode; @@ -53,17 +44,6 @@ export interface LiteralTypeBuildNode { value: null | boolean | number | string; } -interface NamedArgType { - buildNode: (name: string, value: any) => NamedArgTypeBuildNode; - toElasticsearchQuery: (node: any) => JsonValue; -} - -export interface NamedArgTypeBuildNode { - type: 'namedArg'; - name: string; - value: any; -} - interface WildcardType { wildcardSymbol: string; buildNode: (value: string) => WildcardTypeBuildNode | KueryNode; @@ -81,6 +61,5 @@ export interface WildcardTypeBuildNode { export interface NodeTypes { function: FunctionType; literal: LiteralType; - namedArg: NamedArgType; wildcard: WildcardType; } From e27cdee5609daa3e6d41797f72de648f87d44bff Mon Sep 17 00:00:00 2001 From: Lukas Olson Date: Thu, 18 Nov 2021 10:38:08 -0700 Subject: [PATCH 2/3] Fix tests --- packages/kbn-es-query/src/kuery/ast/ast.test.ts | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/packages/kbn-es-query/src/kuery/ast/ast.test.ts b/packages/kbn-es-query/src/kuery/ast/ast.test.ts index 0e4366d6e21d1..fe18eb3881ecc 100644 --- a/packages/kbn-es-query/src/kuery/ast/ast.test.ts +++ b/packages/kbn-es-query/src/kuery/ast/ast.test.ts @@ -174,12 +174,8 @@ describe('kuery AST API', () => { test('should support exclusive range operators', () => { const expected = nodeTypes.function.buildNode('and', [ - nodeTypes.function.buildNode('range', 'bytes', { - gt: '1000', - }), - nodeTypes.function.buildNode('range', 'bytes', { - lt: '8000', - }), + nodeTypes.function.buildNode('range', 'bytes', 'gt', '1000'), + nodeTypes.function.buildNode('range', 'bytes', 'lt', '8000'), ]); const actual = fromKueryExpression('bytes > 1000 and bytes < 8000'); expect(actual).toEqual(expected); @@ -187,12 +183,8 @@ describe('kuery AST API', () => { test('should support inclusive range operators', () => { const expected = nodeTypes.function.buildNode('and', [ - nodeTypes.function.buildNode('range', 'bytes', { - gte: '1000', - }), - nodeTypes.function.buildNode('range', 'bytes', { - lte: '8000', - }), + nodeTypes.function.buildNode('range', 'bytes', 'gte', '1000'), + nodeTypes.function.buildNode('range', 'bytes', 'lte', '8000'), ]); const actual = fromKueryExpression('bytes >= 1000 and bytes <= 8000'); expect(actual).toEqual(expected); From 8229c2136710bbf0dfeb92a9bfc9e44dea78336b Mon Sep 17 00:00:00 2001 From: Lukas Olson Date: Tue, 7 Dec 2021 08:59:41 -0700 Subject: [PATCH 3/3] Re-format grammar output --- .../src/kuery/grammar/__mocks__/grammar.js | 865 +++++++----------- 1 file changed, 349 insertions(+), 516 deletions(-) diff --git a/packages/kbn-es-query/src/kuery/grammar/__mocks__/grammar.js b/packages/kbn-es-query/src/kuery/grammar/__mocks__/grammar.js index 4e5958459a6d2..14f08e4a080c2 100644 --- a/packages/kbn-es-query/src/kuery/grammar/__mocks__/grammar.js +++ b/packages/kbn-es-query/src/kuery/grammar/__mocks__/grammar.js @@ -11,12 +11,10 @@ // https://peggyjs.org/ /* eslint-disable */ -'use strict'; +"use strict"; function peg$subclass(child, parent) { - function C() { - this.constructor = child; - } + function C() { this.constructor = child; } C.prototype = parent.prototype; child.prototype = new C(); } @@ -29,24 +27,22 @@ function peg$SyntaxError(message, expected, found, location) { self.expected = expected; self.found = found; self.location = location; - self.name = 'SyntaxError'; + self.name = "SyntaxError"; return self; } peg$subclass(peg$SyntaxError, Error); function peg$padEnd(str, targetLength, padString) { - padString = padString || ' '; - if (str.length > targetLength) { - return str; - } + padString = padString || " "; + if (str.length > targetLength) { return str; } targetLength -= str.length; padString += padString.repeat(targetLength); return str + padString.slice(0, targetLength); } -peg$SyntaxError.prototype.format = function (sources) { - var str = 'Error: ' + this.message; +peg$SyntaxError.prototype.format = function(sources) { + var str = "Error: " + this.message; if (this.location) { var src = null; var k; @@ -57,60 +53,51 @@ peg$SyntaxError.prototype.format = function (sources) { } } var s = this.location.start; - var loc = this.location.source + ':' + s.line + ':' + s.column; + var loc = this.location.source + ":" + s.line + ":" + s.column; if (src) { var e = this.location.end; - var filler = peg$padEnd('', s.line.toString().length); + var filler = peg$padEnd("", s.line.toString().length); var line = src[s.line - 1]; var last = s.line === e.line ? e.column : line.length + 1; - str += - '\n --> ' + - loc + - '\n' + - filler + - ' |\n' + - s.line + - ' | ' + - line + - '\n' + - filler + - ' | ' + - peg$padEnd('', s.column - 1) + - peg$padEnd('', last - s.column, '^'); + str += "\n --> " + loc + "\n" + + filler + " |\n" + + s.line + " | " + line + "\n" + + filler + " | " + peg$padEnd("", s.column - 1) + + peg$padEnd("", last - s.column, "^"); } else { - str += '\n at ' + loc; + str += "\n at " + loc; } } return str; }; -peg$SyntaxError.buildMessage = function (expected, found) { +peg$SyntaxError.buildMessage = function(expected, found) { var DESCRIBE_EXPECTATION_FNS = { - literal: function (expectation) { - return '"' + literalEscape(expectation.text) + '"'; + literal: function(expectation) { + return "\"" + literalEscape(expectation.text) + "\""; }, - class: function (expectation) { - var escapedParts = expectation.parts.map(function (part) { + class: function(expectation) { + var escapedParts = expectation.parts.map(function(part) { return Array.isArray(part) - ? classEscape(part[0]) + '-' + classEscape(part[1]) + ? classEscape(part[0]) + "-" + classEscape(part[1]) : classEscape(part); }); - return '[' + (expectation.inverted ? '^' : '') + escapedParts + ']'; + return "[" + (expectation.inverted ? "^" : "") + escapedParts + "]"; }, - any: function () { - return 'any character'; + any: function() { + return "any character"; }, - end: function () { - return 'end of input'; + end: function() { + return "end of input"; }, - other: function (expectation) { + other: function(expectation) { return expectation.description; - }, + } }; function hex(ch) { @@ -119,36 +106,28 @@ peg$SyntaxError.buildMessage = function (expected, found) { function literalEscape(s) { return s - .replace(/\\/g, '\\\\') - .replace(/"/g, '\\"') - .replace(/\0/g, '\\0') - .replace(/\t/g, '\\t') - .replace(/\n/g, '\\n') - .replace(/\r/g, '\\r') - .replace(/[\x00-\x0F]/g, function (ch) { - return '\\x0' + hex(ch); - }) - .replace(/[\x10-\x1F\x7F-\x9F]/g, function (ch) { - return '\\x' + hex(ch); - }); + .replace(/\\/g, "\\\\") + .replace(/"/g, "\\\"") + .replace(/\0/g, "\\0") + .replace(/\t/g, "\\t") + .replace(/\n/g, "\\n") + .replace(/\r/g, "\\r") + .replace(/[\x00-\x0F]/g, function(ch) { return "\\x0" + hex(ch); }) + .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return "\\x" + hex(ch); }); } function classEscape(s) { return s - .replace(/\\/g, '\\\\') - .replace(/\]/g, '\\]') - .replace(/\^/g, '\\^') - .replace(/-/g, '\\-') - .replace(/\0/g, '\\0') - .replace(/\t/g, '\\t') - .replace(/\n/g, '\\n') - .replace(/\r/g, '\\r') - .replace(/[\x00-\x0F]/g, function (ch) { - return '\\x0' + hex(ch); - }) - .replace(/[\x10-\x1F\x7F-\x9F]/g, function (ch) { - return '\\x' + hex(ch); - }); + .replace(/\\/g, "\\\\") + .replace(/\]/g, "\\]") + .replace(/\^/g, "\\^") + .replace(/-/g, "\\-") + .replace(/\0/g, "\\0") + .replace(/\t/g, "\\t") + .replace(/\n/g, "\\n") + .replace(/\r/g, "\\r") + .replace(/[\x00-\x0F]/g, function(ch) { return "\\x0" + hex(ch); }) + .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return "\\x" + hex(ch); }); } function describeExpectation(expectation) { @@ -176,20 +155,20 @@ peg$SyntaxError.buildMessage = function (expected, found) { return descriptions[0]; case 2: - return descriptions[0] + ' or ' + descriptions[1]; + return descriptions[0] + " or " + descriptions[1]; default: - return ( - descriptions.slice(0, -1).join(', ') + ', or ' + descriptions[descriptions.length - 1] - ); + return descriptions.slice(0, -1).join(", ") + + ", or " + + descriptions[descriptions.length - 1]; } } function describeFound(found) { - return found ? '"' + literalEscape(found) + '"' : 'end of input'; + return found ? "\"" + literalEscape(found) + "\"" : "end of input"; } - return 'Expected ' + describeExpected(expected) + ' but ' + describeFound(found) + ' found.'; + return "Expected " + describeExpected(expected) + " but " + describeFound(found) + " found."; }; function peg$parse(input, options) { @@ -201,26 +180,26 @@ function peg$parse(input, options) { var peg$startRuleFunctions = { start: peg$parsestart, Literal: peg$parseLiteral }; var peg$startRuleFunction = peg$parsestart; - var peg$c0 = '('; - var peg$c1 = ')'; - var peg$c2 = ':'; - var peg$c3 = '{'; - var peg$c4 = '}'; - var peg$c5 = 'or'; - var peg$c6 = 'and'; - var peg$c7 = 'not'; - var peg$c8 = '"'; - var peg$c9 = '\\'; - var peg$c10 = '*'; - var peg$c11 = '\\t'; - var peg$c12 = '\\r'; - var peg$c13 = '\\n'; - var peg$c14 = 'u'; - var peg$c15 = '<='; - var peg$c16 = '>='; - var peg$c17 = '<'; - var peg$c18 = '>'; - var peg$c19 = '@kuery-cursor@'; + var peg$c0 = "("; + var peg$c1 = ")"; + var peg$c2 = ":"; + var peg$c3 = "{"; + var peg$c4 = "}"; + var peg$c5 = "or"; + var peg$c6 = "and"; + var peg$c7 = "not"; + var peg$c8 = "\""; + var peg$c9 = "\\"; + var peg$c10 = "*"; + var peg$c11 = "\\t"; + var peg$c12 = "\\r"; + var peg$c13 = "\\n"; + var peg$c14 = "u"; + var peg$c15 = "<="; + var peg$c16 = ">="; + var peg$c17 = "<"; + var peg$c18 = ">"; + var peg$c19 = "@kuery-cursor@"; var peg$r0 = /^[\\"]/; var peg$r1 = /^[^"]/; @@ -228,272 +207,217 @@ function peg$parse(input, options) { var peg$r3 = /^[0-9a-f]/i; var peg$r4 = /^[ \t\r\n\xA0]/; - var peg$e0 = peg$literalExpectation('(', false); - var peg$e1 = peg$literalExpectation(')', false); - var peg$e2 = peg$literalExpectation(':', false); - var peg$e3 = peg$literalExpectation('{', false); - var peg$e4 = peg$literalExpectation('}', false); - var peg$e5 = peg$otherExpectation('fieldName'); - var peg$e6 = peg$otherExpectation('value'); - var peg$e7 = peg$otherExpectation('OR'); - var peg$e8 = peg$literalExpectation('or', true); - var peg$e9 = peg$otherExpectation('AND'); - var peg$e10 = peg$literalExpectation('and', true); - var peg$e11 = peg$otherExpectation('NOT'); - var peg$e12 = peg$literalExpectation('not', true); - var peg$e13 = peg$otherExpectation('literal'); - var peg$e14 = peg$literalExpectation('"', false); - var peg$e15 = peg$literalExpectation('\\', false); - var peg$e16 = peg$classExpectation(['\\', '"'], false, false); - var peg$e17 = peg$classExpectation(['"'], true, false); + var peg$e0 = peg$literalExpectation("(", false); + var peg$e1 = peg$literalExpectation(")", false); + var peg$e2 = peg$literalExpectation(":", false); + var peg$e3 = peg$literalExpectation("{", false); + var peg$e4 = peg$literalExpectation("}", false); + var peg$e5 = peg$otherExpectation("fieldName"); + var peg$e6 = peg$otherExpectation("value"); + var peg$e7 = peg$otherExpectation("OR"); + var peg$e8 = peg$literalExpectation("or", true); + var peg$e9 = peg$otherExpectation("AND"); + var peg$e10 = peg$literalExpectation("and", true); + var peg$e11 = peg$otherExpectation("NOT"); + var peg$e12 = peg$literalExpectation("not", true); + var peg$e13 = peg$otherExpectation("literal"); + var peg$e14 = peg$literalExpectation("\"", false); + var peg$e15 = peg$literalExpectation("\\", false); + var peg$e16 = peg$classExpectation(["\\", "\""], false, false); + var peg$e17 = peg$classExpectation(["\""], true, false); var peg$e18 = peg$anyExpectation(); - var peg$e19 = peg$literalExpectation('*', false); - var peg$e20 = peg$literalExpectation('\\t', false); - var peg$e21 = peg$literalExpectation('\\r', false); - var peg$e22 = peg$literalExpectation('\\n', false); - var peg$e23 = peg$classExpectation( - ['\\', '(', ')', ':', '<', '>', '"', '*', '{', '}'], - false, - false - ); - var peg$e24 = peg$literalExpectation('u', false); - var peg$e25 = peg$classExpectation( - [ - ['0', '9'], - ['a', 'f'], - ], - false, - true - ); - var peg$e26 = peg$literalExpectation('<=', false); - var peg$e27 = peg$literalExpectation('>=', false); - var peg$e28 = peg$literalExpectation('<', false); - var peg$e29 = peg$literalExpectation('>', false); - var peg$e30 = peg$otherExpectation('whitespace'); - var peg$e31 = peg$classExpectation([' ', '\t', '\r', '\n', '\xA0'], false, false); - var peg$e32 = peg$literalExpectation('@kuery-cursor@', false); - - var peg$f0 = function (query, trailing) { - if (trailing.type === 'cursor') { - return { - ...trailing, - suggestionTypes: ['conjunction'], - }; - } - if (query !== null) return query; - return nodeTypes.function.buildNode('is', '*', '*'); - }; - var peg$f1 = function (head, query) { - return query; - }; - var peg$f2 = function (head, tail) { - const nodes = [head, ...tail]; - const cursor = parseCursor && nodes.find((node) => node.type === 'cursor'); - if (cursor) return cursor; - return buildFunctionNode('or', nodes); - }; - var peg$f3 = function (head, tail) { - const nodes = [head, ...tail]; - const cursor = parseCursor && nodes.find((node) => node.type === 'cursor'); - if (cursor) return cursor; - return buildFunctionNode('and', nodes); - }; - var peg$f4 = function (query) { - if (query.type === 'cursor') return query; - return buildFunctionNode('not', [query]); - }; - var peg$f5 = function (query, trailing) { - if (trailing.type === 'cursor') { - return { - ...trailing, - suggestionTypes: ['conjunction'], - }; - } - return query; - }; - var peg$f6 = function (field, query, trailing) { - if (query.type === 'cursor') { - return { - ...query, - nestedPath: query.nestedPath ? `${field.value}.${query.nestedPath}` : field.value, - }; - } + var peg$e19 = peg$literalExpectation("*", false); + var peg$e20 = peg$literalExpectation("\\t", false); + var peg$e21 = peg$literalExpectation("\\r", false); + var peg$e22 = peg$literalExpectation("\\n", false); + var peg$e23 = peg$classExpectation(["\\", "(", ")", ":", "<", ">", "\"", "*", "{", "}"], false, false); + var peg$e24 = peg$literalExpectation("u", false); + var peg$e25 = peg$classExpectation([["0", "9"], ["a", "f"]], false, true); + var peg$e26 = peg$literalExpectation("<=", false); + var peg$e27 = peg$literalExpectation(">=", false); + var peg$e28 = peg$literalExpectation("<", false); + var peg$e29 = peg$literalExpectation(">", false); + var peg$e30 = peg$otherExpectation("whitespace"); + var peg$e31 = peg$classExpectation([" ", "\t", "\r", "\n", "\xA0"], false, false); + var peg$e32 = peg$literalExpectation("@kuery-cursor@", false); + + var peg$f0 = function(query, trailing) { + if (trailing.type === 'cursor') { + return { + ...trailing, + suggestionTypes: ['conjunction'] + }; + } + if (query !== null) return query; + return nodeTypes.function.buildNode('is', '*', '*'); + }; + var peg$f1 = function(head, query) { return query; }; + var peg$f2 = function(head, tail) { + const nodes = [head, ...tail]; + const cursor = parseCursor && nodes.find(node => node.type === 'cursor'); + if (cursor) return cursor; + return buildFunctionNode('or', nodes); + }; + var peg$f3 = function(head, tail) { + const nodes = [head, ...tail]; + const cursor = parseCursor && nodes.find(node => node.type === 'cursor'); + if (cursor) return cursor; + return buildFunctionNode('and', nodes); + }; + var peg$f4 = function(query) { + if (query.type === 'cursor') return query; + return buildFunctionNode('not', [query]); + }; + var peg$f5 = function(query, trailing) { + if (trailing.type === 'cursor') { + return { + ...trailing, + suggestionTypes: ['conjunction'] + }; + } + return query; + }; + var peg$f6 = function(field, query, trailing) { + if (query.type === 'cursor') { + return { + ...query, + nestedPath: query.nestedPath ? `${field.value}.${query.nestedPath}` : field.value, + } + }; - if (trailing.type === 'cursor') { - return { - ...trailing, - suggestionTypes: ['conjunction'], - }; - } - return buildFunctionNode('nested', [field, query]); - }; - var peg$f7 = function (field, operator, value) { - if (value.type === 'cursor') { - return { - ...value, - suggestionTypes: ['conjunction'], - }; - } - return buildFunctionNode('range', [field, operator, value]); - }; - var peg$f8 = function (field, partial) { - if (partial.type === 'cursor') { - return { - ...partial, - fieldName: field.value, - suggestionTypes: ['value', 'conjunction'], - }; - } - return partial(field); - }; - var peg$f9 = function (partial) { - if (partial.type === 'cursor') { - const fieldName = `${partial.prefix}${partial.suffix}`.trim(); - return { - ...partial, - fieldName, - suggestionTypes: ['field', 'operator', 'conjunction'], - }; - } - const field = buildLiteralNode(null); - return partial(field); - }; - var peg$f10 = function (partial, trailing) { - if (trailing.type === 'cursor') { - return { - ...trailing, - suggestionTypes: ['conjunction'], - }; - } - return partial; - }; - var peg$f11 = function (head, partial) { - return partial; - }; - var peg$f12 = function (head, tail) { - const nodes = [head, ...tail]; - const cursor = parseCursor && nodes.find((node) => node.type === 'cursor'); - if (cursor) { - return { - ...cursor, - suggestionTypes: ['value'], - }; - } - return (field) => - buildFunctionNode( - 'or', - nodes.map((partial) => partial(field)) - ); - }; - var peg$f13 = function (head, tail) { - const nodes = [head, ...tail]; - const cursor = parseCursor && nodes.find((node) => node.type === 'cursor'); - if (cursor) { - return { - ...cursor, - suggestionTypes: ['value'], - }; - } - return (field) => - buildFunctionNode( - 'and', - nodes.map((partial) => partial(field)) - ); - }; - var peg$f14 = function (partial) { - if (partial.type === 'cursor') { - return { - ...list, - suggestionTypes: ['value'], + if (trailing.type === 'cursor') { + return { + ...trailing, + suggestionTypes: ['conjunction'] + }; + } + return buildFunctionNode('nested', [field, query]); }; - } - return (field) => buildFunctionNode('not', [partial(field)]); - }; - var peg$f15 = function (value) { - if (value.type === 'cursor') return value; - const isPhrase = buildLiteralNode(true); - return (field) => buildFunctionNode('is', [field, value, isPhrase]); - }; - var peg$f16 = function (value) { - if (value.type === 'cursor') return value; + var peg$f7 = function(field, operator, value) { + if (value.type === 'cursor') { + return { + ...value, + suggestionTypes: ['conjunction'] + }; + } + return buildFunctionNode('range', [field, operator, value]); + }; + var peg$f8 = function(field, partial) { + if (partial.type === 'cursor') { + return { + ...partial, + fieldName: field.value, + suggestionTypes: ['value', 'conjunction'] + }; + } + return partial(field); + }; + var peg$f9 = function(partial) { + if (partial.type === 'cursor') { + const fieldName = `${partial.prefix}${partial.suffix}`.trim(); + return { + ...partial, + fieldName, + suggestionTypes: ['field', 'operator', 'conjunction'] + }; + } + const field = buildLiteralNode(null); + return partial(field); + }; + var peg$f10 = function(partial, trailing) { + if (trailing.type === 'cursor') { + return { + ...trailing, + suggestionTypes: ['conjunction'] + }; + } + return partial; + }; + var peg$f11 = function(head, partial) { return partial; }; + var peg$f12 = function(head, tail) { + const nodes = [head, ...tail]; + const cursor = parseCursor && nodes.find(node => node.type === 'cursor'); + if (cursor) { + return { + ...cursor, + suggestionTypes: ['value'] + }; + } + return (field) => buildFunctionNode('or', nodes.map(partial => partial(field))); + }; + var peg$f13 = function(head, tail) { + const nodes = [head, ...tail]; + const cursor = parseCursor && nodes.find(node => node.type === 'cursor'); + if (cursor) { + return { + ...cursor, + suggestionTypes: ['value'] + }; + } + return (field) => buildFunctionNode('and', nodes.map(partial => partial(field))); + }; + var peg$f14 = function(partial) { + if (partial.type === 'cursor') { + return { + ...list, + suggestionTypes: ['value'] + }; + } + return (field) => buildFunctionNode('not', [partial(field)]); + }; + var peg$f15 = function(value) { + if (value.type === 'cursor') return value; + const isPhrase = buildLiteralNode(true); + return (field) => buildFunctionNode('is', [field, value, isPhrase]); + }; + var peg$f16 = function(value) { + if (value.type === 'cursor') return value; - if ( - !allowLeadingWildcards && - value.type === 'wildcard' && - nodeTypes.wildcard.hasLeadingWildcard(value) - ) { - error( - 'Leading wildcards are disabled. See query:allowLeadingWildcards in Advanced Settings.' - ); - } + if (!allowLeadingWildcards && value.type === 'wildcard' && nodeTypes.wildcard.hasLeadingWildcard(value)) { + error('Leading wildcards are disabled. See query:allowLeadingWildcards in Advanced Settings.'); + } - const isPhrase = buildLiteralNode(false); - return (field) => buildFunctionNode('is', [field, value, isPhrase]); - }; - var peg$f17 = function () { - return parseCursor; - }; - var peg$f18 = function (prefix, cursor, suffix) { - const { start, end } = location(); - return { - type: 'cursor', - start: start.offset, - end: end.offset - cursor.length, - prefix: prefix.join(''), - suffix: suffix.join(''), - text: text().replace(cursor, ''), + const isPhrase = buildLiteralNode(false); + return (field) => buildFunctionNode('is', [field, value, isPhrase]); }; - }; - var peg$f19 = function (chars) { - return buildLiteralNode(chars.join('')); - }; - var peg$f20 = function (char) { - return char; - }; - var peg$f21 = function (chars) { - const sequence = chars.join('').trim(); - if (sequence === 'null') return buildLiteralNode(null); - if (sequence === 'true') return buildLiteralNode(true); - if (sequence === 'false') return buildLiteralNode(false); - if (chars.includes(wildcardSymbol)) return buildWildcardNode(sequence); - return buildLiteralNode(sequence); - }; - var peg$f22 = function () { - return wildcardSymbol; - }; - var peg$f23 = function () { - return '\t'; - }; - var peg$f24 = function () { - return '\r'; - }; - var peg$f25 = function () { - return '\n'; - }; - var peg$f26 = function (keyword) { - return keyword; - }; - var peg$f27 = function (sequence) { - return sequence; - }; - var peg$f28 = function (digits) { - return String.fromCharCode(parseInt(digits, 16)); - }; - var peg$f29 = function () { - return 'lte'; - }; - var peg$f30 = function () { - return 'gte'; - }; - var peg$f31 = function () { - return 'lt'; - }; - var peg$f32 = function () { - return 'gt'; - }; - var peg$f33 = function () { - return cursorSymbol; - }; + var peg$f17 = function() { return parseCursor; }; + var peg$f18 = function(prefix, cursor, suffix) { + const { start, end } = location(); + return { + type: 'cursor', + start: start.offset, + end: end.offset - cursor.length, + prefix: prefix.join(''), + suffix: suffix.join(''), + text: text().replace(cursor, '') + }; + }; + var peg$f19 = function(chars) { + return buildLiteralNode(chars.join('')); + }; + var peg$f20 = function(char) { return char; }; + var peg$f21 = function(chars) { + const sequence = chars.join('').trim(); + if (sequence === 'null') return buildLiteralNode(null); + if (sequence === 'true') return buildLiteralNode(true); + if (sequence === 'false') return buildLiteralNode(false); + if (chars.includes(wildcardSymbol)) return buildWildcardNode(sequence); + return buildLiteralNode(sequence); + }; + var peg$f22 = function() { return wildcardSymbol; }; + var peg$f23 = function() { return '\t'; }; + var peg$f24 = function() { return '\r'; }; + var peg$f25 = function() { return '\n'; }; + var peg$f26 = function(keyword) { return keyword; }; + var peg$f27 = function(sequence) { return sequence; }; + var peg$f28 = function(digits) { + return String.fromCharCode(parseInt(digits, 16)); + }; + var peg$f29 = function() { return 'lte'; }; + var peg$f30 = function() { return 'gte'; }; + var peg$f31 = function() { return 'lt'; }; + var peg$f32 = function() { return 'gt'; }; + var peg$f33 = function() { return cursorSymbol; }; var peg$currPos = 0; var peg$savedPos = 0; @@ -504,9 +428,9 @@ function peg$parse(input, options) { var peg$result; - if ('startRule' in options) { + if ("startRule" in options) { if (!(options.startRule in peg$startRuleFunctions)) { - throw new Error('Can\'t start parsing from rule "' + options.startRule + '".'); + throw new Error("Can't start parsing from rule \"" + options.startRule + "\"."); } peg$startRuleFunction = peg$startRuleFunctions[options.startRule]; @@ -524,7 +448,7 @@ function peg$parse(input, options) { return { source: peg$source, start: peg$savedPos, - end: peg$currPos, + end: peg$currPos }; } @@ -533,7 +457,9 @@ function peg$parse(input, options) { } function expected(description, location) { - location = location !== undefined ? location : peg$computeLocation(peg$savedPos, peg$currPos); + location = location !== undefined + ? location + : peg$computeLocation(peg$savedPos, peg$currPos); throw peg$buildStructuredError( [peg$otherExpectation(description)], @@ -543,29 +469,31 @@ function peg$parse(input, options) { } function error(message, location) { - location = location !== undefined ? location : peg$computeLocation(peg$savedPos, peg$currPos); + location = location !== undefined + ? location + : peg$computeLocation(peg$savedPos, peg$currPos); throw peg$buildSimpleError(message, location); } function peg$literalExpectation(text, ignoreCase) { - return { type: 'literal', text: text, ignoreCase: ignoreCase }; + return { type: "literal", text: text, ignoreCase: ignoreCase }; } function peg$classExpectation(parts, inverted, ignoreCase) { - return { type: 'class', parts: parts, inverted: inverted, ignoreCase: ignoreCase }; + return { type: "class", parts: parts, inverted: inverted, ignoreCase: ignoreCase }; } function peg$anyExpectation() { - return { type: 'any' }; + return { type: "any" }; } function peg$endExpectation() { - return { type: 'end' }; + return { type: "end" }; } function peg$otherExpectation(description) { - return { type: 'other', description: description }; + return { type: "other", description: description }; } function peg$computePosDetails(pos) { @@ -583,7 +511,7 @@ function peg$parse(input, options) { details = peg$posDetailsCache[p]; details = { line: details.line, - column: details.column, + column: details.column }; while (p < pos) { @@ -612,20 +540,18 @@ function peg$parse(input, options) { start: { offset: startPos, line: startPosDetails.line, - column: startPosDetails.column, + column: startPosDetails.column }, end: { offset: endPos, line: endPosDetails.line, - column: endPosDetails.column, - }, + column: endPosDetails.column + } }; } function peg$fail(expected) { - if (peg$currPos < peg$maxFailPos) { - return; - } + if (peg$currPos < peg$maxFailPos) { return; } if (peg$currPos > peg$maxFailPos) { peg$maxFailPos = peg$currPos; @@ -832,9 +758,7 @@ function peg$parse(input, options) { peg$currPos++; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e0); - } + if (peg$silentFails === 0) { peg$fail(peg$e0); } } if (s1 !== peg$FAILED) { s2 = []; @@ -852,9 +776,7 @@ function peg$parse(input, options) { peg$currPos++; } else { s5 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e1); - } + if (peg$silentFails === 0) { peg$fail(peg$e1); } } if (s5 !== peg$FAILED) { peg$savedPos = s0; @@ -899,9 +821,7 @@ function peg$parse(input, options) { peg$currPos++; } else { s3 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e2); - } + if (peg$silentFails === 0) { peg$fail(peg$e2); } } if (s3 !== peg$FAILED) { s4 = []; @@ -915,9 +835,7 @@ function peg$parse(input, options) { peg$currPos++; } else { s5 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e3); - } + if (peg$silentFails === 0) { peg$fail(peg$e3); } } if (s5 !== peg$FAILED) { s6 = []; @@ -935,9 +853,7 @@ function peg$parse(input, options) { peg$currPos++; } else { s9 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e4); - } + if (peg$silentFails === 0) { peg$fail(peg$e4); } } if (s9 !== peg$FAILED) { peg$savedPos = s0; @@ -995,9 +911,7 @@ function peg$parse(input, options) { peg$silentFails--; if (s0 === peg$FAILED) { s1 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e5); - } + if (peg$silentFails === 0) { peg$fail(peg$e5); } } return s0; @@ -1060,9 +974,7 @@ function peg$parse(input, options) { peg$currPos++; } else { s3 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e2); - } + if (peg$silentFails === 0) { peg$fail(peg$e2); } } if (s3 !== peg$FAILED) { s4 = []; @@ -1114,9 +1026,7 @@ function peg$parse(input, options) { peg$currPos++; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e0); - } + if (peg$silentFails === 0) { peg$fail(peg$e0); } } if (s1 !== peg$FAILED) { s2 = []; @@ -1134,9 +1044,7 @@ function peg$parse(input, options) { peg$currPos++; } else { s5 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e1); - } + if (peg$silentFails === 0) { peg$fail(peg$e1); } } if (s5 !== peg$FAILED) { peg$savedPos = s0; @@ -1336,9 +1244,7 @@ function peg$parse(input, options) { peg$silentFails--; if (s0 === peg$FAILED) { s1 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e6); - } + if (peg$silentFails === 0) { peg$fail(peg$e6); } } return s0; @@ -1365,9 +1271,7 @@ function peg$parse(input, options) { peg$currPos += 2; } else { s2 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e8); - } + if (peg$silentFails === 0) { peg$fail(peg$e8); } } if (s2 !== peg$FAILED) { s3 = []; @@ -1398,9 +1302,7 @@ function peg$parse(input, options) { peg$silentFails--; if (s0 === peg$FAILED) { s1 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e7); - } + if (peg$silentFails === 0) { peg$fail(peg$e7); } } return s0; @@ -1427,9 +1329,7 @@ function peg$parse(input, options) { peg$currPos += 3; } else { s2 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e10); - } + if (peg$silentFails === 0) { peg$fail(peg$e10); } } if (s2 !== peg$FAILED) { s3 = []; @@ -1460,9 +1360,7 @@ function peg$parse(input, options) { peg$silentFails--; if (s0 === peg$FAILED) { s1 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e9); - } + if (peg$silentFails === 0) { peg$fail(peg$e9); } } return s0; @@ -1478,9 +1376,7 @@ function peg$parse(input, options) { peg$currPos += 3; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e12); - } + if (peg$silentFails === 0) { peg$fail(peg$e12); } } if (s1 !== peg$FAILED) { s2 = []; @@ -1507,9 +1403,7 @@ function peg$parse(input, options) { peg$silentFails--; if (s0 === peg$FAILED) { s1 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e11); - } + if (peg$silentFails === 0) { peg$fail(peg$e11); } } return s0; @@ -1526,9 +1420,7 @@ function peg$parse(input, options) { peg$silentFails--; if (s0 === peg$FAILED) { s1 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e13); - } + if (peg$silentFails === 0) { peg$fail(peg$e13); } } return s0; @@ -1551,9 +1443,7 @@ function peg$parse(input, options) { peg$currPos++; } else { s2 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e14); - } + if (peg$silentFails === 0) { peg$fail(peg$e14); } } if (s2 !== peg$FAILED) { s3 = []; @@ -1575,9 +1465,7 @@ function peg$parse(input, options) { peg$currPos++; } else { s6 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e14); - } + if (peg$silentFails === 0) { peg$fail(peg$e14); } } if (s6 !== peg$FAILED) { peg$savedPos = s0; @@ -1605,9 +1493,7 @@ function peg$parse(input, options) { peg$currPos++; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e14); - } + if (peg$silentFails === 0) { peg$fail(peg$e14); } } if (s1 !== peg$FAILED) { s2 = []; @@ -1621,9 +1507,7 @@ function peg$parse(input, options) { peg$currPos++; } else { s3 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e14); - } + if (peg$silentFails === 0) { peg$fail(peg$e14); } } if (s3 !== peg$FAILED) { peg$savedPos = s0; @@ -1654,9 +1538,7 @@ function peg$parse(input, options) { peg$currPos++; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e15); - } + if (peg$silentFails === 0) { peg$fail(peg$e15); } } if (s1 !== peg$FAILED) { if (peg$r0.test(input.charAt(peg$currPos))) { @@ -1664,9 +1546,7 @@ function peg$parse(input, options) { peg$currPos++; } else { s2 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e16); - } + if (peg$silentFails === 0) { peg$fail(peg$e16); } } if (s2 !== peg$FAILED) { peg$savedPos = s0; @@ -1697,9 +1577,7 @@ function peg$parse(input, options) { peg$currPos++; } else { s2 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e17); - } + if (peg$silentFails === 0) { peg$fail(peg$e17); } } if (s2 !== peg$FAILED) { peg$savedPos = s0; @@ -1829,9 +1707,7 @@ function peg$parse(input, options) { peg$currPos++; } else { s4 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e18); - } + if (peg$silentFails === 0) { peg$fail(peg$e18); } } if (s4 !== peg$FAILED) { peg$savedPos = s0; @@ -1870,9 +1746,7 @@ function peg$parse(input, options) { peg$currPos++; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e19); - } + if (peg$silentFails === 0) { peg$fail(peg$e19); } } if (s1 !== peg$FAILED) { peg$savedPos = s0; @@ -1940,9 +1814,7 @@ function peg$parse(input, options) { peg$currPos += 2; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e20); - } + if (peg$silentFails === 0) { peg$fail(peg$e20); } } if (s1 !== peg$FAILED) { peg$savedPos = s0; @@ -1956,9 +1828,7 @@ function peg$parse(input, options) { peg$currPos += 2; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e21); - } + if (peg$silentFails === 0) { peg$fail(peg$e21); } } if (s1 !== peg$FAILED) { peg$savedPos = s0; @@ -1972,9 +1842,7 @@ function peg$parse(input, options) { peg$currPos += 2; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e22); - } + if (peg$silentFails === 0) { peg$fail(peg$e22); } } if (s1 !== peg$FAILED) { peg$savedPos = s0; @@ -1996,9 +1864,7 @@ function peg$parse(input, options) { peg$currPos++; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e15); - } + if (peg$silentFails === 0) { peg$fail(peg$e15); } } if (s1 !== peg$FAILED) { s2 = peg$parseSpecialCharacter(); @@ -2026,9 +1892,7 @@ function peg$parse(input, options) { peg$currPos++; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e15); - } + if (peg$silentFails === 0) { peg$fail(peg$e15); } } if (s1 !== peg$FAILED) { if (input.substr(peg$currPos, 2).toLowerCase() === peg$c5) { @@ -2036,9 +1900,7 @@ function peg$parse(input, options) { peg$currPos += 2; } else { s2 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e8); - } + if (peg$silentFails === 0) { peg$fail(peg$e8); } } if (s2 === peg$FAILED) { if (input.substr(peg$currPos, 3).toLowerCase() === peg$c6) { @@ -2046,9 +1908,7 @@ function peg$parse(input, options) { peg$currPos += 3; } else { s2 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e10); - } + if (peg$silentFails === 0) { peg$fail(peg$e10); } } if (s2 === peg$FAILED) { if (input.substr(peg$currPos, 3).toLowerCase() === peg$c7) { @@ -2056,9 +1916,7 @@ function peg$parse(input, options) { peg$currPos += 3; } else { s2 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e12); - } + if (peg$silentFails === 0) { peg$fail(peg$e12); } } } } @@ -2099,9 +1957,7 @@ function peg$parse(input, options) { peg$currPos++; } else { s0 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e23); - } + if (peg$silentFails === 0) { peg$fail(peg$e23); } } return s0; @@ -2116,9 +1972,7 @@ function peg$parse(input, options) { peg$currPos++; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e15); - } + if (peg$silentFails === 0) { peg$fail(peg$e15); } } if (s1 !== peg$FAILED) { s2 = peg$parseUnicodeSequence(); @@ -2146,9 +2000,7 @@ function peg$parse(input, options) { peg$currPos++; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e24); - } + if (peg$silentFails === 0) { peg$fail(peg$e24); } } if (s1 !== peg$FAILED) { s2 = peg$currPos; @@ -2207,9 +2059,7 @@ function peg$parse(input, options) { peg$currPos++; } else { s0 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e25); - } + if (peg$silentFails === 0) { peg$fail(peg$e25); } } return s0; @@ -2224,9 +2074,7 @@ function peg$parse(input, options) { peg$currPos += 2; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e26); - } + if (peg$silentFails === 0) { peg$fail(peg$e26); } } if (s1 !== peg$FAILED) { peg$savedPos = s0; @@ -2240,9 +2088,7 @@ function peg$parse(input, options) { peg$currPos += 2; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e27); - } + if (peg$silentFails === 0) { peg$fail(peg$e27); } } if (s1 !== peg$FAILED) { peg$savedPos = s0; @@ -2256,9 +2102,7 @@ function peg$parse(input, options) { peg$currPos++; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e28); - } + if (peg$silentFails === 0) { peg$fail(peg$e28); } } if (s1 !== peg$FAILED) { peg$savedPos = s0; @@ -2272,9 +2116,7 @@ function peg$parse(input, options) { peg$currPos++; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e29); - } + if (peg$silentFails === 0) { peg$fail(peg$e29); } } if (s1 !== peg$FAILED) { peg$savedPos = s0; @@ -2297,16 +2139,12 @@ function peg$parse(input, options) { peg$currPos++; } else { s0 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e31); - } + if (peg$silentFails === 0) { peg$fail(peg$e31); } } peg$silentFails--; if (s0 === peg$FAILED) { s1 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e30); - } + if (peg$silentFails === 0) { peg$fail(peg$e30); } } return s0; @@ -2329,9 +2167,7 @@ function peg$parse(input, options) { peg$currPos += 14; } else { s2 = peg$FAILED; - if (peg$silentFails === 0) { - peg$fail(peg$e32); - } + if (peg$silentFails === 0) { peg$fail(peg$e32); } } if (s2 !== peg$FAILED) { peg$savedPos = s0; @@ -2348,16 +2184,13 @@ function peg$parse(input, options) { return s0; } - const { - parseCursor, - cursorSymbol, - allowLeadingWildcards = true, - helpers: { nodeTypes }, - } = options; - const buildFunctionNode = nodeTypes.function.buildNodeWithArgumentNodes; - const buildLiteralNode = nodeTypes.literal.buildNode; - const buildWildcardNode = nodeTypes.wildcard.buildNode; - const { wildcardSymbol } = nodeTypes.wildcard; + + const { parseCursor, cursorSymbol, allowLeadingWildcards = true, helpers: { nodeTypes } } = options; + const buildFunctionNode = nodeTypes.function.buildNodeWithArgumentNodes; + const buildLiteralNode = nodeTypes.literal.buildNode; + const buildWildcardNode = nodeTypes.wildcard.buildNode; + const { wildcardSymbol } = nodeTypes.wildcard; + peg$result = peg$startRuleFunction(); @@ -2380,5 +2213,5 @@ function peg$parse(input, options) { module.exports = { SyntaxError: peg$SyntaxError, - parse: peg$parse, + parse: peg$parse };