1 /* ------------------------------------------------
\r
2 Copyright 2014 AT&T Intellectual Property
\r
3 Licensed under the Apache License, Version 2.0 (the "License");
\r
4 you may not use this file except in compliance with the License.
\r
5 You may obtain a copy of the License at
\r
7 http://www.apache.org/licenses/LICENSE-2.0
\r
9 Unless required by applicable law or agreed to in writing, software
\r
10 distributed under the License is distributed on an "AS IS" BASIS,
\r
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
12 See the License for the specific language governing permissions and
\r
13 limitations under the License.
\r
14 ------------------------------------------- */
\r
19 flex -PxmlParser -oxmllexer.cc xml.l
\r
25 * AT&T lex can't handle this lexer due to lex bugs. It works with flex
\r
26 * 2.3.7, pclex 2.0.5, and MKS lex 3.1a.
\r
29 // #include "parse_fta.h"
\r
34 #include "xml.tab.hh"
\r
37 Some includes that flex doesn't include as standard,
\r
38 but which are needed.
\r
45 // Prevent flex from defining yywrap as extern "C"
\r
47 #define YY_SKIP_YYWRAP
\r
49 /* No lex lib, supply the yywrap fcn. that normally resides there
\r
52 int xmlParserwrap(){return(1);}
\r
54 extern int xmlParserdebug;
\r
58 These variables are used for error reporting:
\r
59 flex_xml_lineno : the line currently being parsed when the error occurs.
\r
60 flex_xml_ch : the character on the line where the error occurs
\r
61 flex_xml_linebuf : store the line for reporting.
\r
63 NOTE : 1) the fixed size flex_xml_linebuf buffer is dangerous.
\r
64 2) You might get pointed to a place shortly after
\r
65 where the syntax error occurs. It is close enough
\r
69 int flex_xml_lineno = 1;
\r
70 int flex_xml_ch = 0;
\r
71 char flex_xml_linebuf[20000];
\r
73 char *flex_xml_stringinput = NULL;
\r
74 int flex_xml_stringinput_ptr = 0;
\r
75 FILE *flex_xml_fileinput = NULL;
\r
76 int my_xmlParser_yyinput(char *buf, int max_size);
\r
80 void xmlParsererror(char *s){
\r
82 fprintf(stderr,"On line %d, char %d: %s (token %s):\n%s\n",
\r
83 flex_xml_lineno, flex_xml_ch, s, xmlParsertext, flex_xml_linebuf );
\r
84 for(i=0;i<flex_xml_ch;i++){
\r
85 if(flex_xml_linebuf[i] == '\t'){
\r
86 fprintf(stderr,"\t");
\r
88 fprintf(stderr," ");
\r
91 fprintf(stderr,"^\n");
\r
92 // fprintf(stderr,"%*s\n",1+flex_xml_ch,"^");
\r
96 #define YY_INPUT(b, r, ms) (r = my_xmlParser_yyinput(b,ms))
\r
99 /* MKS needs the next line to increase the NFA table */
\r
105 /* literal keyword tokens */
\r
108 The actions associated with each text token are to
\r
109 keep track of the current location (for syntax error reporting)
\r
110 and to report any necessary info to the emf.y parse tree builder
\r
112 Its likely that there are a number of omissions, inconsistencies
\r
113 (some keywords do not need to be in caps), and relics
\r
114 (keywords such as BETWEEN, INDICATOR, etc., are not used
\r
116 This parser is somewhat of a work in progress.
\r
119 /* Query keywords */
\r
124 [<>=/] { flex_xml_ch+=xmlParserleng; return yytext[0]; }
\r
128 [A-Za-z_][A-Za-z0-9_:]* { flex_xml_ch+=xmlParserleng; xmlParserlval.strval = strdup(yytext); return NAME; }
\r
137 xmlParserlval.strval = strdup(xmlParsertext+1);
\r
141 unput(c); /* just peeking */
\r
143 flex_xml_ch+=xmlParserleng;
\r
144 xmlParserlval.strval[xmlParserleng-2] = '\0';
\r
145 return STRING_TOKEN;
\r
150 \"[^\"\n]*$ { flex_xml_ch+=xmlParserleng; xmlParsererror("Unterminated string"); }
\r
153 /* Newline : advance the error reporting line number */
\r
154 /* and grab the next line into flex_xml_linebuf */
\r
157 \n.* {flex_xml_ch=0; flex_xml_lineno++;
\r
158 strcpy(flex_xml_linebuf,xmlParsertext+1);
\r
162 [ \t\r]+ {flex_xml_ch+=xmlParserleng; } /* white space */
\r
164 "--".*$ {flex_xml_ch+=xmlParserleng; }; /* comment */
\r
165 "//".*$ {flex_xml_ch+=xmlParserleng; }; /* comment */
\r
166 "<?xml".*$ {flex_xml_ch+=xmlParserleng; }; /* comment */
\r
168 .|\n {flex_xml_ch+=xmlParserleng; fprintf(stderr,"Warning: unknown token (ignored)\n"); xmlParsererror(yytext);}
\r
172 int my_xmlParser_yyinput(char *buf, int max_size){
\r
176 if(flex_xml_stringinput != NULL){
\r
178 if(flex_xml_stringinput[flex_xml_stringinput_ptr] != '\0'){
\r
179 buf[c++] = flex_xml_stringinput[flex_xml_stringinput_ptr++];
\r
187 if(flex_xml_fileinput != NULL){
\r
188 while(c < max_size){
\r
189 inchar = getc(flex_xml_fileinput);
\r
202 void xmlParser_setfileinput(FILE *f){
\r
203 xmlParserrestart(NULL);
\r
205 flex_xml_fileinput = f;
\r
206 flex_xml_stringinput = NULL;
\r
207 flex_xml_lineno = 1;
\r
211 void xmlParser_setstringinput(char *s){
\r
212 xmlParserrestart(NULL);
\r
214 flex_xml_fileinput = NULL;
\r
215 flex_xml_stringinput = s;
\r
216 flex_xml_stringinput_ptr = 0;
\r
217 flex_xml_lineno = 1;
\r