Skip to content

Commit

Permalink
Merge pull request #216 from astefan/basic_grammar
Browse files Browse the repository at this point in the history
Basic grammar
  • Loading branch information
astefan committed Sep 1, 2022
2 parents bca0c29 + 9b812d3 commit 93af79a
Show file tree
Hide file tree
Showing 18 changed files with 3,613 additions and 279 deletions.
168 changes: 153 additions & 15 deletions x-pack/plugin/esql/src/main/antlr/EsqlBase.g4
Expand Up @@ -7,19 +7,57 @@

grammar EsqlBase;

statement
singleStatement
: query
;

singleExpression
: booleanExpression EOF
;

query
: sourceCmd
: sourceCommand (PIPE processingCommand)*
;

sourceCommand
: rowCommand
| fromCommand
;

processingCommand
: whereCommand
;

whereCommand
: WHERE booleanExpression
;

booleanExpression
: NOT booleanExpression #logicalNot
| valueExpression #booleanDefault
| left=booleanExpression operator=AND right=booleanExpression #logicalBinary
| left=booleanExpression operator=OR right=booleanExpression #logicalBinary
;

valueExpression
: operatorExpression #valueExpressionDefault
| left=operatorExpression comparisonOperator right=operatorExpression #comparison
;

operatorExpression
: primaryExpression #operatorExpressionDefault
| operator=(MINUS | PLUS) operatorExpression #arithmeticUnary
| left=operatorExpression operator=(ASTERISK | SLASH | PERCENT) right=operatorExpression #arithmeticBinary
| left=operatorExpression operator=(PLUS | MINUS) right=operatorExpression #arithmeticBinary
;

sourceCmd
: rowCmd
primaryExpression
: constant #constantDefault
| qualifiedName #dereference
| LP booleanExpression RP #parenthesizedExpression
;

rowCmd
rowCommand
: ROW fields
;

Expand All @@ -28,28 +66,128 @@ fields
;

field
: expression
| identifier EQUALS expression
: constant
| qualifiedName ASSIGN constant
;

expression : INTEGER_LITERAL;
fromCommand
: FROM identifier (COMMA identifier)*
;

identifier : IDENTIFIER;
qualifiedName
: identifier (DOT identifier)*
;

fragment DIGIT : [0-9];
fragment LETTER : [A-Za-z];
identifier
: UNQUOTED_IDENTIFIER
| QUOTED_IDENTIFIER
;

INTEGER_LITERAL : DIGIT+;
constant
: NULL #nullLiteral
| number #numericLiteral
| booleanValue #booleanLiteral
| string #stringLiteral
;

ROW : 'row';
booleanValue
: TRUE | FALSE
;

number
: DECIMAL_LITERAL #decimalLiteral
| INTEGER_LITERAL #integerLiteral
;

string
: STRING
;

comparisonOperator
: EQ | NEQ | LT | LTE | GT | GTE
;

fragment DIGIT
: [0-9]
;

fragment LETTER
: [A-Za-z]
;

fragment ESCAPE_SEQUENCE
: '\\' [tnr"\\]
;
fragment UNESCAPED_CHARS
: ~[\r\n"\\]
;

fragment EXPONENT
: [Ee] [+-]? DIGIT+
;

STRING
: '"' (ESCAPE_SEQUENCE | UNESCAPED_CHARS)* '"'
| '"""' (~[\r\n])*? '"""' '"'? '"'?
;

INTEGER_LITERAL
: DIGIT+
;

DECIMAL_LITERAL
: DIGIT+ DOT DIGIT*
| DOT DIGIT+
| DIGIT+ (DOT DIGIT*)? EXPONENT
| DOT DIGIT+ EXPONENT
;

AND : 'and';
ASSIGN : '=';
COMMA : ',';
EQUALS : '=';
DOT : '.';
FALSE : 'false';
FROM : 'from';
LP : '(';
NOT : 'not';
NULL : 'null';
OR : 'or';
ROW : 'row';
RP : ')';
PIPE : '|';
TRUE : 'true';
WHERE : 'where';

EQ : '==';
NEQ : '!=';
LT : '<';
LTE : '<=';
GT : '>';
GTE : '>=';

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

IDENTIFIER
UNQUOTED_IDENTIFIER
: (LETTER | '_') (LETTER | DIGIT | '_')*
;

QUOTED_IDENTIFIER
: '`' ( ~'`' | '``' )* '`'
;

LINE_COMMENT
: '//' ~[\r\n]* '\r'? '\n'? -> channel(HIDDEN)
;

MULTILINE_COMMENT
: '/*' (MULTILINE_COMMENT|.)*? '*/' -> channel(HIDDEN)
;

WS
: [ \r\n\t]+ -> channel(HIDDEN)
;
69 changes: 60 additions & 9 deletions x-pack/plugin/esql/src/main/antlr/EsqlBase.tokens
@@ -1,9 +1,60 @@
INTEGER_LITERAL=1
ROW=2
COMMA=3
EQUALS=4
IDENTIFIER=5
WS=6
'row'=2
','=3
'='=4
STRING=1
INTEGER_LITERAL=2
DECIMAL_LITERAL=3
AND=4
ASSIGN=5
COMMA=6
DOT=7
FALSE=8
FROM=9
LP=10
NOT=11
NULL=12
OR=13
ROW=14
RP=15
PIPE=16
TRUE=17
WHERE=18
EQ=19
NEQ=20
LT=21
LTE=22
GT=23
GTE=24
PLUS=25
MINUS=26
ASTERISK=27
SLASH=28
PERCENT=29
UNQUOTED_IDENTIFIER=30
QUOTED_IDENTIFIER=31
LINE_COMMENT=32
MULTILINE_COMMENT=33
WS=34
'and'=4
'='=5
','=6
'.'=7
'false'=8
'from'=9
'('=10
'not'=11
'null'=12
'or'=13
'row'=14
')'=15
'|'=16
'true'=17
'where'=18
'=='=19
'!='=20
'<'=21
'<='=22
'>'=23
'>='=24
'+'=25
'-'=26
'*'=27
'/'=28
'%'=29
69 changes: 60 additions & 9 deletions x-pack/plugin/esql/src/main/antlr/EsqlBaseLexer.tokens
@@ -1,9 +1,60 @@
INTEGER_LITERAL=1
ROW=2
COMMA=3
EQUALS=4
IDENTIFIER=5
WS=6
'row'=2
','=3
'='=4
STRING=1
INTEGER_LITERAL=2
DECIMAL_LITERAL=3
AND=4
ASSIGN=5
COMMA=6
DOT=7
FALSE=8
FROM=9
LP=10
NOT=11
NULL=12
OR=13
ROW=14
RP=15
PIPE=16
TRUE=17
WHERE=18
EQ=19
NEQ=20
LT=21
LTE=22
GT=23
GTE=24
PLUS=25
MINUS=26
ASTERISK=27
SLASH=28
PERCENT=29
UNQUOTED_IDENTIFIER=30
QUOTED_IDENTIFIER=31
LINE_COMMENT=32
MULTILINE_COMMENT=33
WS=34
'and'=4
'='=5
','=6
'.'=7
'false'=8
'from'=9
'('=10
'not'=11
'null'=12
'or'=13
'row'=14
')'=15
'|'=16
'true'=17
'where'=18
'=='=19
'!='=20
'<'=21
'<='=22
'>'=23
'>='=24
'+'=25
'-'=26
'*'=27
'/'=28
'%'=29

0 comments on commit 93af79a

Please sign in to comment.