Fixed newline characters throughout the code
[com/gs-lite.git] / src / ftacmp / nic.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 -PNicParser -oniclexer.cc nic.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 "nic_def.h"
30  #include <string.h>
31
32
33
34 #include "nic.tab.cc.h"
35
36 /*
37         Some includes that flex doesn't include as standard,
38         but which are needed.
39 */
40
41 #include <stdlib.h>
42 #include <string.h>
43
44
45 //              Prevent flex from defining yywrap as extern "C" 
46
47 #define YY_SKIP_YYWRAP
48
49 /*              No lex lib, supply the yywrap fcn. that normally resides there
50 */
51
52 int NicParserwrap(){return(1);}
53
54 extern int NicParserdebug;
55
56
57 /*
58                 These variables are used for error reporting:
59                 flex_nic_lineno : the line currently being parsed when the error occurs.
60                 flex_nic_ch : the character on the line where the error occurs
61                 flex_nic_linebuf : store the line for reporting.
62
63                 NOTE : 1) the fixed size flex_nic_linebuf buffer is dangerous.
64                            2) You might get pointed to a place shortly after
65                                   where the syntax error occurs.  It is close enough
66                                   for now.
67 */
68
69 int flex_nic_lineno = 1;
70 int flex_nic_ch = 0;
71 char flex_nic_linebuf[20000];
72
73 char *flex_nic_stringinput = NULL;
74 int flex_nic_stringinput_ptr = 0;
75 FILE *flex_nic_fileinput = NULL;
76 int my_NicParser_yyinput(char *buf, int max_size);
77
78
79
80 void NicParsererror(char *s){
81         int i;
82         fprintf(stderr,"On line %d, char %d: %s (token %s):\n%s\n",
83                                 flex_nic_lineno, flex_nic_ch, s, NicParsertext, flex_nic_linebuf );
84     for(i=0;i<flex_nic_ch;i++){
85                 if(flex_nic_linebuf[i] == '\t'){
86                         fprintf(stderr,"\t");
87                 }else{
88                         fprintf(stderr," ");
89                 }
90         }
91         fprintf(stderr,"^\n");
92         //      fprintf(stderr,"%*s\n",1+flex_nic_ch,"^");
93 }
94
95 #undef YY_INPUT
96 #define YY_INPUT(b, r, ms) (r = my_NicParser_yyinput(b,ms))
97
98 %}
99         /* MKS needs the next line to increase the NFA table */
100 %e 1200
101 %option noyywrap
102
103 %%
104
105         /* literal keyword tokens */
106
107  /*
108                         The actions associated with each text token are to
109                         keep track of the current location (for syntax error reporting)
110                         and to report any necessary info to the emf.y parse tree builder
111
112                         Its likely that there are a number of omissions, inconsistencies
113                         (some keywords do not need to be in caps), and relics
114                         (keywords such as BETWEEN, INDICATOR, etc., are not used
115                          in emf.y)
116                         This parser is somewhat of a work in progress.
117  */
118
119  /*             Query keywords          */
120
121 OPTIONS:             { flex_nic_ch+=NicParserleng; return OPTIONS; }
122 FUNCTIONS:             { flex_nic_ch+=NicParserleng; return FUNC; }
123 TYPES:             { flex_nic_ch+=NicParserleng; return TYPES; }
124 UNARY_OPS:             { flex_nic_ch+=NicParserleng; return UNARY_OPS; }
125 BINARY_OPS:             { flex_nic_ch+=NicParserleng; return BINARY_OPS; }
126 MISSING_FIELDS:             { flex_nic_ch+=NicParserleng; return FIELDS; }
127
128 [;,=]   { flex_nic_ch+=NicParserleng;  return yytext[0]; }
129
130         /* names */
131
132 [A-Za-z_][A-Za-z0-9_]*  { flex_nic_ch+=NicParserleng; NicParserlval.strval = strdup(yytext); return NAME; }
133
134
135         /* strings */
136
137 '[^'\n]*'       {
138                 
139                 int c;
140                 
141                 NicParserlval.strval = strdup(NicParsertext+1); 
142
143                 c = yyinput();
144
145                 unput(c);       /* just peeking */
146                 if(c != '\'') {
147                         flex_nic_ch+=NicParserleng; 
148                         NicParserlval.strval[NicParserleng-2] = '\0';
149                         return STRING_TOKEN;
150                 } else
151                         yymore();
152         }
153                 
154 '[^'\n]*$       { flex_nic_ch+=NicParserleng; NicParsererror("Unterminated string"); }
155
156  /*                                                                                                                     */
157  /*             Newline : advance the error reporting line number       */
158  /*             and grab the next line into flex_nic_linebuf                    */
159  /*                                                                                                                     */
160
161 \n.*            {flex_nic_ch=0; flex_nic_lineno++;
162                            strcpy(flex_nic_linebuf,NicParsertext+1);
163                            yyless(1);
164                            }
165
166 [ \t\r]+        {flex_nic_ch+=NicParserleng; }  /* white space */
167
168 "--".*$         {flex_nic_ch+=NicParserleng; }; /* comment */
169 "//".*$         {flex_nic_ch+=NicParserleng; }; /* comment */
170
171 .|\n            {flex_nic_ch+=NicParserleng; fprintf(stderr,"Warning: unknown token (ignored)\n");  NicParsererror(yytext);}
172
173 %%
174
175 int my_NicParser_yyinput(char *buf, int max_size){
176         int c = 0;
177         int inchar = 0;
178         
179         if(flex_nic_stringinput != NULL){
180                 while(c<max_size){
181                         if(flex_nic_stringinput[flex_nic_stringinput_ptr] != '\0'){
182                                 buf[c++] = flex_nic_stringinput[flex_nic_stringinput_ptr++];
183                         }else{
184                                 break;
185                         }
186                 }
187                 return(c);
188         }
189         
190         if(flex_nic_fileinput != NULL){
191                 while(c < max_size){
192                         inchar = getc(flex_nic_fileinput);
193                         if(inchar != EOF){
194                                 buf[c++] = inchar;
195                         }else{
196                                 break;
197                         }
198                 }
199                 return(c);
200         }
201         
202         return(0);
203 }
204
205 void NicParser_setfileinput(FILE *f){
206         NicParserrestart(NULL);
207
208         flex_nic_fileinput = f;
209         flex_nic_stringinput = NULL;
210         flex_nic_lineno = 1;
211         flex_nic_ch = 0;
212 }
213
214 void NicParser_setstringinput(char *s){
215         NicParserrestart(NULL);
216
217         flex_nic_fileinput = NULL;
218         flex_nic_stringinput = s;
219         flex_nic_stringinput_ptr = 0;
220         flex_nic_lineno = 1;
221         flex_nic_ch = 0;
222 }
223         
224                 
225
226