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
18 flex -PxmlParser -oxmllexer.cc xml.l
\r
24 * AT&T lex can't handle this lexer due to lex bugs. It works with flex
\r
25 * 2.3.7, pclex 2.0.5, and MKS lex 3.1a.
\r
28 // #include "parse_fta.h"
\r
33 #include "xml.tab.hh"
\r
36 Some includes that flex doesn't include as standard,
\r
37 but which are needed.
\r
44 // Prevent flex from defining yywrap as extern "C"
\r
46 #define YY_SKIP_YYWRAP
\r
48 /* No lex lib, supply the yywrap fcn. that normally resides there
\r
51 int xmlParserwrap(){return(1);}
\r
53 extern int xmlParserdebug;
\r
57 These variables are used for error reporting:
\r
58 flex_xml_lineno : the line currently being parsed when the error occurs.
\r
59 flex_xml_ch : the character on the line where the error occurs
\r
60 flex_xml_linebuf : store the line for reporting.
\r
62 NOTE : 1) the fixed size flex_xml_linebuf buffer is dangerous.
\r
63 2) You might get pointed to a place shortly after
\r
64 where the syntax error occurs. It is close enough
\r
68 int flex_xml_lineno = 1;
\r
69 int flex_xml_ch = 0;
\r
70 char flex_xml_linebuf[20000];
\r
72 char *flex_xml_stringinput = NULL;
\r
73 int flex_xml_stringinput_ptr = 0;
\r
74 FILE *flex_xml_fileinput = NULL;
\r
75 int my_xmlParser_yyinput(char *buf, int max_size);
\r
79 void xmlParsererror(char *s){
\r
81 fprintf(stderr,"On line %d, char %d: %s (token %s):\n%s\n",
\r
82 flex_xml_lineno, flex_xml_ch, s, xmlParsertext, flex_xml_linebuf );
\r
83 for(i=0;i<flex_xml_ch;i++){
\r
84 if(flex_xml_linebuf[i] == '\t'){
\r
85 fprintf(stderr,"\t");
\r
87 fprintf(stderr," ");
\r
90 fprintf(stderr,"^\n");
\r
91 // fprintf(stderr,"%*s\n",1+flex_xml_ch,"^");
\r
95 #define YY_INPUT(b, r, ms) (r = my_xmlParser_yyinput(b,ms))
\r
98 /* MKS needs the next line to increase the NFA table */
\r
104 /* literal keyword tokens */
\r
107 The actions associated with each text token are to
\r
108 keep track of the current location (for syntax error reporting)
\r
109 and to report any necessary info to the emf.y parse tree builder
\r
111 Its likely that there are a number of omissions, inconsistencies
\r
112 (some keywords do not need to be in caps), and relics
\r
113 (keywords such as BETWEEN, INDICATOR, etc., are not used
\r
115 This parser is somewhat of a work in progress.
\r
118 /* Query keywords */
\r
123 [<>=/] { flex_xml_ch+=xmlParserleng; return yytext[0]; }
\r
127 [A-Za-z_][A-Za-z0-9_:\-]* { flex_xml_ch+=xmlParserleng; xmlParserlval.strval = strdup(yytext); return NAME; }
\r
136 xmlParserlval.strval = strdup(xmlParsertext+1);
\r
140 unput(c); /* just peeking */
\r
142 flex_xml_ch+=xmlParserleng;
\r
143 xmlParserlval.strval[xmlParserleng-2] = '\0';
\r
144 return STRING_TOKEN;
\r
149 \'[^\'\n]*$ { flex_xml_ch+=xmlParserleng; xmlParsererror("Unterminated string"); }
\r
152 /* Newline : advance the error reporting line number */
\r
153 /* and grab the next line into flex_xml_linebuf */
\r
156 \n.* {flex_xml_ch=0; flex_xml_lineno++;
\r
157 strcpy(flex_xml_linebuf,xmlParsertext+1);
\r
161 [ \t\r]+ {flex_xml_ch+=xmlParserleng; } /* white space */
\r
163 "--".*$ {flex_xml_ch+=xmlParserleng; }; /* comment */
\r
164 "//".*$ {flex_xml_ch+=xmlParserleng; }; /* comment */
\r
165 "<?xml".*$ {flex_xml_ch+=xmlParserleng; }; /* comment */
\r
167 .|\n {flex_xml_ch+=xmlParserleng; fprintf(stderr,"Warning: unknown token (ignored)\n"); xmlParsererror(yytext);}
\r
171 int my_xmlParser_yyinput(char *buf, int max_size){
\r
175 if(flex_xml_stringinput != NULL){
\r
177 if(flex_xml_stringinput[flex_xml_stringinput_ptr] != '\0'){
\r
178 buf[c++] = flex_xml_stringinput[flex_xml_stringinput_ptr++];
\r
186 if(flex_xml_fileinput != NULL){
\r
187 while(c < max_size){
\r
188 inchar = getc(flex_xml_fileinput);
\r
201 void xmlParser_setfileinput(FILE *f){
\r
202 xmlParserrestart(NULL);
\r
204 flex_xml_fileinput = f;
\r
205 flex_xml_stringinput = NULL;
\r
206 flex_xml_lineno = 1;
\r
210 void xmlParser_setstringinput(char *s){
\r
211 xmlParserrestart(NULL);
\r
213 flex_xml_fileinput = NULL;
\r
214 flex_xml_stringinput = s;
\r
215 flex_xml_stringinput_ptr = 0;
\r
216 flex_xml_lineno = 1;
\r