-
Notifications
You must be signed in to change notification settings - Fork 19
/
GQL.g4
174 lines (143 loc) · 2.71 KB
/
GQL.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Define a grammar called GQL
// (field1 = "AAA11232" and fieldB = B_C) and fieldC is MISSING and fieldD is not MIsSING or (fieldD in [D-D, "DD sdf", D2D3D] or fieldE != E) and (field1 > 1111-11-11 and field1 <= 20) order by fieldA asc, fieldB desc
grammar GQL;
statement
: expressions order_expression?
| EOF
;
expressions
: expression (condition_operator expressions)?
;
par_expression
: NOT? LPAR expressions RPAR
;
condition_operator
: OR
| AND
;
expression
: equality_expression
| compare_expression
| missing_expression
| list_expression
| par_expression
| functions
;
equality_expression
: field equality_operator term
;
compare_expression
: field compare_operator comparable
;
comparable
: INTEGER
| DATE
;
missing_expression
: field is_operator MISSING
;
is_operator
: IS NOT?
;
equality_operator
: EQUAL
| NEQ
;
compare_operator
: GT
| GTE
| LT
| LTE
;
list_expression
: field list_operator LBRACK terms RBRACK
;
list_operator
: NOT? IN
;
order_expression
: ORDER order_column (COMMA order_column)*
;
order_column
: field sort?
;
sort
: ASC
| DESC
;
field
: UNQUOTED_STRING
;
terms
: term (COMMA term)*
;
term
: UNQUOTED_STRING
| QUOTED_STRING
| INTEGER
| DATE
;
functions
: projects_expression
| date_expression
;
projects_expression
: PROJECTS_FIELD list_operator projects_function
;
projects_function
: MY_PROJECTS_FUNC
;
date_expression
: DATE_FIELD is_operator date_function
;
date_function
: NOW_FUNC
;
// Fragments
fragment DIGIT : '0'..'9' ;
fragment ALPHA : 'a'..'z' | 'A'..'Z' ;
fragment ALPHA_NUM : ALPHA | DIGIT ;
fragment SEP : '_' | '-' | '.' ;
// Symbols
INTEGER : ('0' | '1'..'9' '0'..'9'*) ;
REAL : DIGIT+ '.' DIGIT* ;
COMMA : ',' ;
COLON : ':' ;
SEMI : ';' ;
EQUAL : '=' ;
NEQ : '!=';
GT : '>' ;
GTE : '>=' ;
LT : '<' ;
LTE : '<=';
LPAR : '(' ;
RPAR : ')' ;
LBRACE : '{';
RBRACE : '}';
LBRACK : '[';
RBRACK : ']';
DBLQ : '"' ;
// GQL Keywords
AND : [Aa][Nn][Dd] ;
ASC : [Aa][Ss][Cc] ;
DESC : [Dd][Ee][Ss][Cc] ;
FROM : [Ff][Rr][Oo][Mm] ;
IN : [Ii][Nn] ;
IS : [Ii][Ss] ;
NOT : [Nn][Oo][Tt] ;
OR : [Oo][Rr] ;
ORDER : [Oo][Rr][Dd][Ee][Rr][ ][Bb][Yy] ;
MISSING : [Mm][Ii][Ss][Ss][Ii][Nn][Gg] ;
// GQL Functions - proof of concept
PROJECTS_FIELD : 'participants.project_code' ;
MY_PROJECTS_FUNC : 'myProjects()' ;
DATE_FIELD : 'files.' ('createdAt' | 'lastModified' ) ;
NOW_FUNC : 'now()' ;
// Values
UNQUOTED_STRING : ALPHA (ALPHA_NUM | SEP)*;
QUOTED_STRING : DBLQ (ALPHA_NUM | SEP | WS)*? DBLQ ;
DATE : DIGIT DIGIT DIGIT DIGIT ('-'|'/') DIGIT DIGIT ('-'|'/') DIGIT DIGIT;
// Extra
WS : [ \t\r\n]+ -> skip ;
COMMENT : '/*' .*? '*/' -> skip;
LINE_COMMENT : '--' ~('\n'|'\r')* -> skip;