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