062fd6d6c1c5d1fb289b99325e1305beca840b93
[com/gs-lite.git] / src / ftacmp / fta.l
1 /* ------------------------------------------------
2 Copyright 2014 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 FILTER_JOIN|Filter_Join|filter_join     { flex_fta_ch+=FtaParserleng; return FILTER_JOIN; }
138 GROUP|Group|group       { flex_fta_ch+=FtaParserleng; return GROUP; }
139 ROLLUP|Rollup|rollup    { flex_fta_ch+=FtaParserleng; return ROLLUP; }
140 CUBE|Cube|cube  { flex_fta_ch+=FtaParserleng; return CUBE; }
141 GROUPING_SETS|Grouping_Sets|grouping_sets       { flex_fta_ch+=FtaParserleng; return GROUPING_SETS; }
142 SUPERGROUP|Supergroup|supergroup        { flex_fta_ch+=FtaParserleng; return SUPERGROUP; }
143 CLEANING_WHEN|Cleaning_When|cleaning_when       { flex_fta_ch+=FtaParserleng; return CLEANING_WHEN; }
144 CLEANING_BY|Cleaning_By|cleaning_by     { flex_fta_ch+=FtaParserleng; return CLEANING_BY; }
145 CLOSING_WHEN|Closing_When|closing_when  { flex_fta_ch+=FtaParserleng; return CLOSING_WHEN; }
146 HAVING|Having|having    { flex_fta_ch+=FtaParserleng; return HAVING; }
147 AS|As|as                { flex_fta_ch+=FtaParserleng; return AS; }
148 IN|In|in                { flex_fta_ch+=FtaParserleng; return IN; }
149 NOT|Not|not             { flex_fta_ch+=FtaParserleng; return NOT; }
150 OR|Or|or        { flex_fta_ch+=FtaParserleng; return OR; }
151
152 SELECT|Select|select            { flex_fta_ch+=FtaParserleng; return SELECT; }
153 WHERE|Where|where               { flex_fta_ch+=FtaParserleng; return WHERE; }
154 SUCH|Such|such  { flex_fta_ch+=FtaParserleng; return SUCH;}
155 THAT|That|that  { flex_fta_ch+=FtaParserleng; return THAT;}
156 MERGE|Merge|merge        {flex_fta_ch+=FtaParserleng; return MERGE;}
157 SLACK    {flex_fta_ch+=FtaParserleng; return SLACK;}
158
159 TRUE    { flex_fta_ch+=FtaParserleng; return TRUE_V;}
160 FALSE   { flex_fta_ch+=FtaParserleng; return FALSE_V;}
161 TIMEVAL { flex_fta_ch+=FtaParserleng; return TIMEVAL_L;}
162 HEX     { flex_fta_ch+=FtaParserleng; return HEX_L;}
163 LHEX    { flex_fta_ch+=FtaParserleng; return LHEX_L;}
164 IP_VAL  { flex_fta_ch+=FtaParserleng; return IP_L;}
165 IPV6_VAL|IPv6_VAL       { flex_fta_ch+=FtaParserleng; return IPV6_L;}
166
167 DEFINE  { flex_fta_ch+=FtaParserleng; return DEFINE_SEC;}
168 PARAM   { flex_fta_ch+=FtaParserleng; return PARAM_SEC;}
169
170 [\{]    {flex_fta_ch+=FtaParserleng; return LEFTBRACE;}
171 [\}]    {flex_fta_ch+=FtaParserleng; return RIGHTBRACE;}
172
173  /*
174                 Table definition keywords
175  */             
176 TABLE           { flex_fta_ch+=FtaParserleng; return TABLE; }
177 PROTOCOL                { flex_fta_ch+=FtaParserleng; return PROTOCOL; }
178 STREAM          { flex_fta_ch+=FtaParserleng; return STREAM; }
179 FTA                     { flex_fta_ch+=FtaParserleng; return FTA; }
180 UNPACK_FCNS                     { flex_fta_ch+=FtaParserleng; return UNPACK_FCNS; }
181
182 OPERATOR                        { flex_fta_ch+=FtaParserleng; return OPERATOR; }
183 OPERATOR_VIEW                   { flex_fta_ch+=FtaParserleng; return OPERATOR_VIEW; }
184 FIELDS                  { flex_fta_ch+=FtaParserleng; return FIELDS; }
185 SUBQUERIES                      { flex_fta_ch+=FtaParserleng; return SUBQUERIES; }
186 SELECTION_PUSHDOWN                      { flex_fta_ch+=FtaParserleng; return SELECTION_PUSHDOWN; }
187 ;       {flex_fta_ch+=FtaParserleng; return SEMICOLON;}
188
189
190         /* punctuation */
191 "<<"    |
192 ">>"                    { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup(yytext); return SHIFT_OP; }
193
194 "="     |
195 "<>"    |
196 "<"     |
197 ">"     |
198 "<="    |
199 ">="            { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup(yytext); return COMPARISON; }
200
201 [%&|!+*/:(),.\[\]$@#]   { flex_fta_ch+=FtaParserleng; return yytext[0]; }
202 "-"     { flex_fta_ch+=FtaParserleng; return yytext[0]; }
203 "~"     { flex_fta_ch+=FtaParserleng; return yytext[0]; }
204
205         /* names */
206
207 [A-Za-z_][A-Za-z0-9_]*  { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup(yytext); return NAME; }
208
209         /* numbers */
210
211 [0-9]+  |
212 [0-9]+UL        { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup(yytext);  return INTNUM; }
213
214 [0-9]+ULL       { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup(yytext);  return LONGINTNUM; }
215
216 [0-9]+"."[0-9]* |
217 "."[0-9]*       |
218 [0-9]+[eE][+-]?[0-9]+   |
219 [0-9]+"."[0-9]*[eE][+-]?[0-9]+ |
220 "."[0-9]*[eE][+-]?[0-9]+        { flex_fta_ch+=FtaParserleng; FtaParserlval.strval = strdup(yytext); return APPROXNUM; }
221
222         /* strings */
223
224 '[^'\n]*'       {
225                 
226                 int c;
227                 
228                 FtaParserlval.strval = strdup(FtaParsertext+1); 
229
230                 c = yyinput();
231
232                 unput(c);       /* just peeking */
233                 if(c != '\'') {
234                         flex_fta_ch+=FtaParserleng; 
235                         FtaParserlval.strval[FtaParserleng-2] = '\0';
236                         return STRING_TOKEN;
237                 } else
238                         yymore();
239         }
240                 
241 '[^'\n]*$       { flex_fta_ch+=FtaParserleng; FtaParsererror("Unterminated string"); }
242
243  /*                                                                                                                     */
244  /*             Newline : advance the error reporting line number       */
245  /*             and grab the next line into flex_fta_linebuf                    */
246  /*                                                                                                                     */
247
248 \n.*            {flex_fta_ch=0; flex_fta_lineno++;
249                            strcpy(flex_fta_linebuf,FtaParsertext+1);
250                            yyless(1);
251                            }
252
253 [ \t\r]+        {flex_fta_ch+=FtaParserleng; }  /* white space */
254
255 "--".*$         {flex_fta_ch+=FtaParserleng; }; /* comment */
256 "//".*$         {flex_fta_ch+=FtaParserleng; }; /* comment */
257
258 .|\n            {flex_fta_ch+=FtaParserleng; fprintf(stderr,"Warning: unknown token (ignored)\n");  FtaParsererror(yytext);}
259
260 %%
261
262 int my_FtaParser_yyinput(char *buf, int max_size){
263         int c = 0;
264         int inchar = 0;
265         
266         if(flex_fta_stringinput != NULL){
267                 while(c<max_size){
268                         if(flex_fta_stringinput[flex_fta_stringinput_ptr] != '\0'){
269                                 buf[c++] = flex_fta_stringinput[flex_fta_stringinput_ptr++];
270                         }else{
271                                 break;
272                         }
273                 }
274                 return(c);
275         }
276         
277         if(flex_fta_fileinput != NULL){
278                 while(c < max_size){
279                         inchar = getc(flex_fta_fileinput);
280                         if(inchar != EOF){
281                                 buf[c++] = inchar;
282                         }else{
283                                 break;
284                         }
285                 }
286                 return(c);
287         }
288         
289         return(0);
290 }
291
292 void FtaParser_setfileinput(FILE *f){
293         FtaParserrestart(NULL);
294
295         flex_fta_fileinput = f;
296         flex_fta_stringinput = NULL;
297         flex_fta_lineno = 1;
298         flex_fta_ch = 0;
299 }
300
301 void FtaParser_setstringinput(char *s){
302         FtaParserrestart(NULL);
303
304         flex_fta_fileinput = NULL;
305         flex_fta_stringinput = s;
306         flex_fta_stringinput_ptr = 0;
307         flex_fta_lineno = 1;
308         flex_fta_ch = 0;
309 }
310         
311                 
312
313