Skip to content

Commit

Permalink
[Step 2][ESQL] Autocomplete part (#148307)
Browse files Browse the repository at this point in the history
## Summary

Autocomplete support for ESQL lang. Initially target branch for that PR
was
[elastic:feature-esql](https://github.com/elastic/kibana/tree/feature-esql)
but then we decided to merge it separately. This PR is as copy of
#148006

## Notes: 
Important: please do `yarn kbn clean & yarn kbn bootstrap` before
testing.

## How to update syntax. 

`antlr` syntax was copied from `ES` and was slightly modified. In case
if you need to update it please follow next steps:
- modify `esql_parser.g4 `and/or `esql_lexer.g4` files
- go to `kbn-monaco` package and execute `bazel clean & npm run
build:antlr4ts:painless`
- go to /painless_parser.ts file and revert the following change: 

<img width="478" alt="image"
src="https://user-images.githubusercontent.com/20072247/209540586-bb77cad1-a6f0-42fa-9875-025bd4afbace.png">

- do `yarn kbn bootstrap`
  • Loading branch information
alexwizp authored Jan 3, 2023
1 parent 72d2f75 commit 5cf614b
Show file tree
Hide file tree
Showing 31 changed files with 2,422 additions and 1,742 deletions.
3 changes: 2 additions & 1 deletion packages/kbn-monaco/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import './src/register_globals';
export { monaco } from './src/monaco_imports';
export { XJsonLang } from './src/xjson';
export { SQLLang } from './src/sql';
export { ESQL_LANG_ID, ESQL_THEME_ID } from './src/esql';
export { ESQL_LANG_ID, ESQL_THEME_ID, ESQLLang } from './src/esql';
export type { ESQLCustomAutocompleteCallbacks } from './src/esql';

export * from './src/painless';
/* eslint-disable-next-line @kbn/eslint/module_migration */
Expand Down
5 changes: 2 additions & 3 deletions packages/kbn-monaco/src/common/error_listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import { ANTLRErrorListener, RecognitionException, Recognizer } from 'antlr4ts';
import { ANTLRErrorListener, Recognizer } from 'antlr4ts';
import type { EditorError } from '../types';

export class ANTLREErrorListener implements ANTLRErrorListener<any> {
Expand All @@ -17,8 +17,7 @@ export class ANTLREErrorListener implements ANTLRErrorListener<any> {
offendingSymbol: any,
line: number,
column: number,
message: string,
e: RecognitionException | undefined
message: string
): void {
let endColumn = column + 1;

Expand Down
51 changes: 30 additions & 21 deletions packages/kbn-monaco/src/esql/antlr/esql_lexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ WS
: [ \r\n\t]+ -> channel(HIDDEN)
;


mode EXPRESSION;

PIPE : '|' -> popMode;
Expand Down Expand Up @@ -73,43 +72,55 @@ DECIMAL_LITERAL
BY : 'by';

AND : 'and';
ASC : 'asc';
ASSIGN : '=';
COMMA : ',';
DESC : 'desc';
DOT : '.';
FALSE : 'false';
FIRST : 'first';
LAST : 'last';
LP : '(';
OPENING_BRACKET : '[' -> pushMode(DEFAULT_MODE);
CLOSING_BRACKET : ']' -> popMode, popMode; // pop twice, once to clear mode of current cmd and once to exit DEFAULT_MODE
NOT : 'not';
NULL : 'null';
NULLS : 'nulls';
OR : 'or';
RP : ')';
TRUE : 'true';

EQ : '==';
NEQ : '!=';
LT : '<';
LTE : '<=';
GT : '>';
GTE : '>=';
BOOLEAN_VALUE
: 'true'
| 'false'
;

COMPARISON_OPERATOR
: '=='
|'!='
| '<'
| '<='
| '>'
| '>='
;

PLUS : '+';
MINUS : '-';
ASTERISK : '*';
SLASH : '/';
PERCENT : '%';

ROUND_FUNCTION_MATH : 'round';
AVG_FUNCTION_MATH : 'avg';
SUM_FUNCTION_MATH : 'sum';
MIN_FUNCTION_MATH : 'min';
MAX_FUNCTION_MATH : 'max';
ORDERING
: 'asc'
| 'desc'
;

NULLS_ORDERING: 'nulls';
NULLS_ORDERING_DIRECTION
: 'first'
| 'last'
;

UNARY_FUNCTION
: 'round'
| 'avg'
| 'min'
| 'max'
| 'sum'
;

UNQUOTED_IDENTIFIER
: (LETTER | '_') (LETTER | DIGIT | '_')*
Expand Down Expand Up @@ -163,5 +174,3 @@ SRC_MULTILINE_COMMENT
SRC_WS
: WS -> channel(HIDDEN)
;

UNKNOWN_CMD : ~[ \r\n\t[\]/]+ -> pushMode(EXPRESSION);
75 changes: 18 additions & 57 deletions packages/kbn-monaco/src/esql/antlr/esql_lexer.interp

Large diffs are not rendered by default.

120 changes: 45 additions & 75 deletions packages/kbn-monaco/src/esql/antlr/esql_lexer.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -16,50 +16,37 @@ INTEGER_LITERAL=15
DECIMAL_LITERAL=16
BY=17
AND=18
ASC=19
ASSIGN=20
COMMA=21
DESC=22
DOT=23
FALSE=24
FIRST=25
LAST=26
LP=27
OPENING_BRACKET=28
CLOSING_BRACKET=29
NOT=30
NULL=31
NULLS=32
OR=33
RP=34
TRUE=35
EQ=36
NEQ=37
LT=38
LTE=39
GT=40
GTE=41
PLUS=42
MINUS=43
ASTERISK=44
SLASH=45
PERCENT=46
ROUND_FUNCTION_MATH=47
AVG_FUNCTION_MATH=48
SUM_FUNCTION_MATH=49
MIN_FUNCTION_MATH=50
MAX_FUNCTION_MATH=51
UNQUOTED_IDENTIFIER=52
QUOTED_IDENTIFIER=53
EXPR_LINE_COMMENT=54
EXPR_MULTILINE_COMMENT=55
EXPR_WS=56
SRC_UNQUOTED_IDENTIFIER=57
SRC_QUOTED_IDENTIFIER=58
SRC_LINE_COMMENT=59
SRC_MULTILINE_COMMENT=60
SRC_WS=61
UNKNOWN_CMD=62
ASSIGN=19
COMMA=20
DOT=21
LP=22
OPENING_BRACKET=23
CLOSING_BRACKET=24
NOT=25
NULL=26
OR=27
RP=28
BOOLEAN_VALUE=29
COMPARISON_OPERATOR=30
PLUS=31
MINUS=32
ASTERISK=33
SLASH=34
PERCENT=35
ORDERING=36
NULLS_ORDERING=37
NULLS_ORDERING_DIRECTION=38
UNARY_FUNCTION=39
UNQUOTED_IDENTIFIER=40
QUOTED_IDENTIFIER=41
EXPR_LINE_COMMENT=42
EXPR_MULTILINE_COMMENT=43
EXPR_WS=44
SRC_UNQUOTED_IDENTIFIER=45
SRC_QUOTED_IDENTIFIER=46
SRC_LINE_COMMENT=47
SRC_MULTILINE_COMMENT=48
SRC_WS=49
'eval'=1
'explain'=2
'from'=3
Expand All @@ -71,34 +58,17 @@ UNKNOWN_CMD=62
'project'=9
'by'=17
'and'=18
'asc'=19
'desc'=22
'.'=23
'false'=24
'first'=25
'last'=26
'('=27
'['=28
']'=29
'not'=30
'null'=31
'nulls'=32
'or'=33
')'=34
'true'=35
'=='=36
'!='=37
'<'=38
'<='=39
'>'=40
'>='=41
'+'=42
'-'=43
'*'=44
'/'=45
'%'=46
'round'=47
'avg'=48
'sum'=49
'min'=50
'max'=51
'.'=21
'('=22
'['=23
']'=24
'not'=25
'null'=26
'or'=27
')'=28
'+'=31
'-'=32
'*'=33
'/'=34
'%'=35
'nulls'=37
Loading

0 comments on commit 5cf614b

Please sign in to comment.