Added watchlist support
[com/gs-lite.git] / src / ftacmp / fta.l
1 /* ------------------------------------------------
2 Copyright 2020 AT&T Intellectual Property
3    Licensed under the Apache License, Version 2.0 (the "License");
4    you may not use this file except in compliance with the License.
5    You may obtain a copy of the License at
6
7      http://www.apache.org/licenses/LICENSE-2.0
8
9    Unless required by applicable law or agreed to in writing, software
10    distributed under the License is distributed on an "AS IS" BASIS,
11    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12    See the License for the specific language governing permissions and
13    limitations under the License.
14  ------------------------------------------- */
15
16
17 /*
18         MUST COMPILE WITH
19                 flex -PFtaParser -oftalexer.cc fta.l
20         (or equivalent).
21 */      
22
23 %{
24 /*
25  * AT&T lex can't handle this lexer due to lex bugs.  It works with flex
26  * 2.3.7, pclex 2.0.5, and MKS lex 3.1a.
27  */
28
29  #include "parse_fta.h"
30  #include "parse_schema.h"
31  #include <string.h>
32
33
34
35 #include "fta.tab.cc.h"
36
37 /*
38         Some includes that flex doesn't include as standard,
39         but which are needed.
40 */
41
42 #include <stdlib.h>
43 #include <string.h>
44
45
46 //              Prevent flex from defining yywrap as extern "C" 
47
48 #define YY_SKIP_YYWRAP
49
50 /*              No lex lib, supply the yywrap fcn. that normally resides there
51 */
52
53 //int FtaParserwrap(){return(1);}
54
55 extern int FtaParserdebug;
56
57
58 /*
59                 These variables are used for error reporting:
60                 flex_fta_lineno : the line currently being parsed when the error occurs.
61                 flex_fta_ch : the character on the line where the error occurs
62                 flex_fta_linebuf : store the line for reporting.
63
64                 NOTE : 1) the fixed size flex_fta_linebuf buffer is dangerous.
65                            2) You might get pointed to a place shortly after
66                                   where the syntax error occurs.  It is close enough
67                                   for now.
68 */
69
70 int flex_fta_lineno = 1;
71 int flex_fta_ch = 0;
72 char flex_fta_linebuf[200000];
73
74 char *flex_fta_stringinput = NULL;
75 int flex_fta_stringinput_ptr = 0;
76 FILE *flex_fta_fileinput = NULL;
77 int my_FtaParser_yyinput(char *buf, int max_size);
78
79
80
81 void FtaParsererror(char *s){
82         int i;
83         fprintf(stderr,"On line %d, char %d: %s (token %s):\n%s\n",
84                                 flex_fta_lineno, flex_fta_ch, s, FtaParsertext, flex_fta_linebuf );
85     for(i=0;i<flex_fta_ch;i++){
86                 if(flex_fta_linebuf[i] == '\t'){
87                         fprintf(stderr,"\t");
88                 }else{
89                         fprintf(stderr," ");
90                 }
91         }
92         fprintf(stderr,"^\n");
93         //      fprintf(stderr,"%*s\n",1+flex_fta_ch,"^");
94 }
95
96 #undef YY_INPUT
97 #define YY_INPUT(b, r, ms) (r = my_FtaParser_yyinput(b,ms))
98
99 %}
100         /* MKS needs the next line to increase the NFA table */
101 %e 1200
102 %option noyywrap
103
104 %%
105
106         /* literal keyword tokens */
107
108  /*
109                         The actions associated with each text token are to
110                         keep track of the current location (for syntax error reporting)
111                         and to report any necessary info to the emf.y parse tree builder
112
113                         Its likely that there are a number of omissions, inconsistencies
114                         (some keywords do not need to be in caps), and relics
115                         (keywords such as BETWEEN, INDICATOR, etc., are not used
116                          in emf.y)
117                         This parser is somewhat of a work in progress.
118  */
119
120  /*             Query keywords          */
121
122 AND|And|and             { flex_fta_ch+=FtaParserleng; return AND; }
123 AND_AGGR|And_Aggr|and_aggr              { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup("AND_AGGR"); return AGGR; }
124 OR_AGGR|Or_Aggr|or_aggr         { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup("OR_AGGR"); return AGGR; }
125 XOR_AGGR|Xor_Aggr|xor_aggr              { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup("XOR_AGGR"); return AGGR; }
126 AVG|Avg|avg             { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup("AVG"); return AGGR; }
127 MIN|Min|min             { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup("MIN"); return AGGR; }
128 MAX|Max|max             { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup("MAX"); return AGGR; }
129 SUM|Sum|sum             { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup("SUM"); return AGGR; }
130 COUNT|Count|count       { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup("COUNT"); return AGGR; }
131 BY|By|by                { flex_fta_ch+=FtaParserleng; return BY; }
132 FROM|From|from          { flex_fta_ch+=FtaParserleng; return FROM; }
133 INNER_JOIN|Inner_Join|inner_join        { flex_fta_ch+=FtaParserleng; return INNER_JOIN; }
134 OUTER_JOIN|Outer_Join|outer_join        { flex_fta_ch+=FtaParserleng; return OUTER_JOIN; }
135 RIGHT_OUTER_JOIN|Right_Outer_Join|right_outer_join      { flex_fta_ch+=FtaParserleng; return RIGHT_OUTER_JOIN; }
136 LEFT_OUTER_JOIN|Left_Outer_Join|left_outer_join { flex_fta_ch+=FtaParserleng; return LEFT_OUTER_JOIN; }
137 WATCHLIST_JOIN|Watchlist_Join|watchlist_join    { flex_fta_ch+=FtaParserleng; return WATCHLIST_JOIN; }
138 FILTER_JOIN|Filter_Join|filter_join     { flex_fta_ch+=FtaParserleng; return FILTER_JOIN; }
139 GROUP|Group|group       { flex_fta_ch+=FtaParserleng; return GROUP; }
140 ROLLUP|Rollup|rollup    { flex_fta_ch+=FtaParserleng; return ROLLUP; }
141 CUBE|Cube|cube  { flex_fta_ch+=FtaParserleng; return CUBE; }
142 GROUPING_SETS|Grouping_Sets|grouping_sets       { flex_fta_ch+=FtaParserleng; return GROUPING_SETS; }
143 SUPERGROUP|Supergroup|supergroup        { flex_fta_ch+=FtaParserleng; return SUPERGROUP; }
144 CLEANING_WHEN|Cleaning_When|cleaning_when       { flex_fta_ch+=FtaParserleng; return CLEANING_WHEN; }
145 CLEANING_BY|Cleaning_By|cleaning_by     { flex_fta_ch+=FtaParserleng; return CLEANING_BY; }
146 CLOSING_WHEN|Closing_When|closing_when  { flex_fta_ch+=FtaParserleng; return CLOSING_WHEN; }
147 HAVING|Having|having    { flex_fta_ch+=FtaParserleng; return HAVING; }
148 AS|As|as                { flex_fta_ch+=FtaParserleng; return AS; }
149 IN|In|in                { flex_fta_ch+=FtaParserleng; return IN; }
150 NOT|Not|not             { flex_fta_ch+=FtaParserleng; return NOT; }
151 OR|Or|or        { flex_fta_ch+=FtaParserleng; return OR; }
152
153 SELECT|Select|select            { flex_fta_ch+=FtaParserleng; return SELECT; }
154 WHERE|Where|where               { flex_fta_ch+=FtaParserleng; return WHERE; }
155 SUCH|Such|such  { flex_fta_ch+=FtaParserleng; return SUCH;}
156 THAT|That|that  { flex_fta_ch+=FtaParserleng; return THAT;}
157 MERGE|Merge|merge        {flex_fta_ch+=FtaParserleng; return MERGE;}
158 SLACK    {flex_fta_ch+=FtaParserleng; return SLACK;}
159 WATCHLIST|Watchlist|watchlist    {flex_fta_ch+=FtaParserleng; return WATCHLIST;}
160
161 TRUE    { flex_fta_ch+=FtaParserleng; return TRUE_V;}
162 FALSE   { flex_fta_ch+=FtaParserleng; return FALSE_V;}
163 TIMEVAL { flex_fta_ch+=FtaParserleng; return TIMEVAL_L;}
164 HEX     { flex_fta_ch+=FtaParserleng; return HEX_L;}
165 LHEX    { flex_fta_ch+=FtaParserleng; return LHEX_L;}
166 IP_VAL  { flex_fta_ch+=FtaParserleng; return IP_L;}
167 IPV6_VAL|IPv6_VAL       { flex_fta_ch+=FtaParserleng; return IPV6_L;}
168
169 DEFINE  { flex_fta_ch+=FtaParserleng; return DEFINE_SEC;}
170 PARAM   { flex_fta_ch+=FtaParserleng; return PARAM_SEC;}
171
172 [\{]    {flex_fta_ch+=FtaParserleng; return LEFTBRACE;}
173 [\}]    {flex_fta_ch+=FtaParserleng; return RIGHTBRACE;}
174
175  /*
176                 Table definition keywords
177  */             
178 TABLE           { flex_fta_ch+=FtaParserleng; return TABLE; }
179 PROTOCOL                { flex_fta_ch+=FtaParserleng; return PROTOCOL; }
180 STREAM          { flex_fta_ch+=FtaParserleng; return STREAM; }
181 FTA                     { flex_fta_ch+=FtaParserleng; return FTA; }
182 UNPACK_FCNS                     { flex_fta_ch+=FtaParserleng; return UNPACK_FCNS; }
183
184 OPERATOR                        { flex_fta_ch+=FtaParserleng; return OPERATOR; }
185 OPERATOR_VIEW                   { flex_fta_ch+=FtaParserleng; return OPERATOR_VIEW; }
186 FIELDS                  { flex_fta_ch+=FtaParserleng; return FIELDS; }
187 SUBQUERIES                      { flex_fta_ch+=FtaParserleng; return SUBQUERIES; }
188 SELECTION_PUSHDOWN                      { flex_fta_ch+=FtaParserleng; return SELECTION_PUSHDOWN; }
189 ;       {flex_fta_ch+=FtaParserleng; return SEMICOLON;}
190
191
192         /* punctuation */
193 "<<"    |
194 ">>"                    { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup(yytext); return SHIFT_OP; }
195
196 "="     |
197 "<>"    |
198 "<"     |
199 ">"     |
200 "<="    |
201 ">="            { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup(yytext); return COMPARISON; }
202
203 [%&|!+*/:(),.\[\]$@#]   { flex_fta_ch+=FtaParserleng; return yytext[0]; }
204 "-"     { flex_fta_ch+=FtaParserleng; return yytext[0]; }
205 "~"     { flex_fta_ch+=FtaParserleng; return yytext[0]; }
206
207         /* names */
208
209 [A-Za-z_][A-Za-z0-9_]*  { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup(yytext); return NAME; }
210
211         /* numbers */
212
213 [0-9]+  |
214 [0-9]+UL        { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup(yytext);  return INTNUM; }
215
216 [0-9]+ULL       { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup(yytext);  return LONGINTNUM; }
217
218 [0-9]+"."[0-9]* |
219 "."[0-9]*       |
220 [0-9]+[eE][+-]?[0-9]+   |
221 [0-9]+"."[0-9]*[eE][+-]?[0-9]+ |
222 "."[0-9]*[eE][+-]?[0-9]+        { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup(yytext); return APPROXNUM; }
223
224         /* strings */
225
226 '[^'\n]*'       {
227                 
228                 int c;
229                 
230                 FtaParserlval.strval = strdup(FtaParsertext+1); 
231
232                 c = yyinput();
233
234                 unput(c);       /* just peeking */
235                 if(c != '\'') {
236                         flex_fta_ch+=FtaParserleng; 
237                         FtaParserlval.strval[FtaParserleng-2] = '\0';
238                         return STRING_TOKEN;
239                 } else
240                         yymore();
241         }
242                 
243 '[^'\n]*$       { flex_fta_ch+=FtaParserleng; FtaParsererror("Unterminated string"); }
244
245  /*                                                                                                                     */
246  /*             Newline : advance the error reporting line number       */
247  /*             and grab the next line into flex_fta_linebuf                    */
248  /*                                                                                                                     */
249
250 \n.*            {flex_fta_ch=0; flex_fta_lineno++;
251                            strcpy(flex_fta_linebuf,FtaParsertext+1);
252                            yyless(1);
253                            }
254
255 [ \t\r]+        {flex_fta_ch+=FtaParserleng; }  /* white space */
256
257 "--".*$         {flex_fta_ch+=FtaParserleng; }; /* comment */
258 "//".*$         {flex_fta_ch+=FtaParserleng; }; /* comment */
259
260 .|\n            {flex_fta_ch+=FtaParserleng; fprintf(stderr,"Warning: unknown token (ignored)\n");  FtaParsererror(yytext);}
261
262 %%
263
264 int my_FtaParser_yyinput(char *buf, int max_size){
265         int c = 0;
266         int inchar = 0;
267         
268         if(flex_fta_stringinput != NULL){
269                 while(c<max_size){
270                         if(flex_fta_stringinput[flex_fta_stringinput_ptr] != '\0'){
271                                 buf[c++] = flex_fta_stringinput[flex_fta_stringinput_ptr++];
272                         }else{
273                                 break;
274                         }
275                 }
276                 return(c);
277         }
278         
279         if(flex_fta_fileinput != NULL){
280                 while(c < max_size){
281                         inchar = getc(flex_fta_fileinput);
282                         if(inchar != EOF){
283                                 buf[c++] = inchar;
284                         }else{
285                                 break;
286                         }
287                 }
288                 return(c);
289         }
290         
291         return(0);
292 }
293
294 void FtaParser_setfileinput(FILE *f){
295         FtaParserrestart(NULL);
296
297         flex_fta_fileinput = f;
298         flex_fta_stringinput = NULL;
299         flex_fta_lineno = 1;
300         flex_fta_ch = 0;
301 }
302
303 void FtaParser_setstringinput(char *s){
304         FtaParserrestart(NULL);
305
306         flex_fta_fileinput = NULL;
307         flex_fta_stringinput = s;
308         flex_fta_stringinput_ptr = 0;
309         flex_fta_lineno = 1;
310         flex_fta_ch = 0;
311 }
312         
313                 
314
315