-
Notifications
You must be signed in to change notification settings - Fork 11
/
TSL.g4
158 lines (134 loc) · 3.39 KB
/
TSL.g4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// TSL.g4
grammar TSL;
// Rules
start : expr EOF;
expr
: mathExp literalOp literalOrMath # LiteralOps
| mathExp stringOp literalOrMath # StringOps
| mathExp keyNot? likeOp literalValue # Like
| mathExp K_IS keyNot? K_NULL # IsNull
| mathExp K_IS keyNot? literalValue # IsLiteral
| mathExp keyNot? K_BETWEEN literalValue K_AND literalValue # Between
| mathExp keyNot? K_IN ( '(' ( literalValue ( ',' literalValue )* )? ')' ) # In
| K_NOT expr # Not
| expr K_AND expr # And
| expr K_OR expr # Or
| '(' expr ')' # Par
;
literalOp
: ( '<' | '<=' | '>' | '>=' )
| ( '=' | '!=' | '<>' )
;
stringOp
: ( '~=' | '~!' )
;
likeOp
: ( K_LIKE | K_ILIKE )
;
databaseName
: IDENTIFIER
;
tableName
: IDENTIFIER
;
columnName
: IDENTIFIER
;
literalValue
: signedNumber # NumberLiteral
| stringValue # StringLiteral
| ( K_TRUE | K_FALSE ) # BooleanLiteral
| RFC3339 # DateLiteral
| DATE # ShortDateLiteral
;
literalOrMath
: ( literalValue | mathExp )
;
mathExp
: columnName # ColumnIdentifier
| mathExp '*' literalOrMath # MulOps
| mathExp '/' literalOrMath # DivOps
| mathExp '%' literalOrMath # ModOps
| mathExp '+' literalOrMath # AddOps
| mathExp '-' literalOrMath # SubOps
| '(' mathExp ')' # MathPar
;
signedNumber
: ( '+' | '-' )? NUMERIC_LITERAL
;
stringValue
: STRING_LITERAL
;
keyNot
: K_NOT
;
// Words
K_LIKE : L I K E;
K_ILIKE : I L I K E;
K_AND : A N D;
K_OR : O R;
K_BETWEEN : B E T W E E N;
K_IN : I N;
K_IS : I S;
K_NULL : N U L L;
K_NOT : N O T;
K_TRUE : T R U E;
K_FALSE : F A L S E;
IDENTIFIER
: '"' (~'"' | '""')* '"'
| '`' (~'`' | '``')* '`'
| '[' ~']'* ']'
| [a-zA-Z_] [a-zA-Z_./0-9]* [a-zA-Z_0-9]* // TODO check: needs more chars in set
;
NUMERIC_LITERAL
: DIGIT+ ( '.' DIGIT* )? ( E [-+]? DIGIT+ )?
| DIGIT+ ('Ki' | 'Mi' | 'Gi' | 'Ti' | 'Pi')
| DIGIT+ ('K' | 'M' | 'G' | 'T' | 'P')
| '.' DIGIT+ ( E [-+]? DIGIT+ )?
;
STRING_LITERAL
: '\'' ( ~'\'' | '\'\'' )* '\''
;
SPACES
: [ \u000B\t\r\n] -> channel(HIDDEN)
;
// RFC3339
YEAR : DIGIT DIGIT DIGIT DIGIT;
MONTH : DIGIT DIGIT;
DAY : DIGIT DIGIT;
HOUR : DIGIT DIGIT;
MINUTE : DIGIT DIGIT;
SEC: DIGIT DIGIT;
SEC_FRAC : '.' DIGIT+;
DATE : YEAR '-' MONTH '-' DAY;
TIME: HOUR ':' MINUTE ':' SEC SEC_FRAC?;
TIME_OFFSET : ('+' | '-') HOUR ':' MINUTE;
RFC3339: DATE 'T' TIME ('Z' | TIME_OFFSET);
// Fragments
fragment DIGIT : [0-9];
fragment A : [aA];
fragment B : [bB];
fragment C : [cC];
fragment D : [dD];
fragment E : [eE];
fragment F : [fF];
fragment G : [gG];
fragment H : [hH];
fragment I : [iI];
fragment J : [jJ];
fragment K : [kK];
fragment L : [lL];
fragment M : [mM];
fragment N : [nN];
fragment O : [oO];
fragment P : [pP];
fragment Q : [qQ];
fragment R : [rR];
fragment S : [sS];
fragment T : [tT];
fragment U : [uU];
fragment V : [vV];
fragment W : [wW];
fragment X : [xX];
fragment Y : [yY];
fragment Z : [zZ];