| 1 |
/* Flex(1) XML processor skeleton scanner (in -*-C-*-). |
| 2 |
* Copyright (C) 1999 Kristoffer Rose. All rights reserved. |
| 3 |
* |
| 4 |
* This file is part of the FleXML XML processor generator system. |
| 5 |
* Copyright (C) 1999 Kristoffer Rose. All rights reserved. |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify it |
| 8 |
* under the terms of the GNU General Public License as published by the Free |
| 9 |
* Software Foundation; either version 2 of the License, or (at your option) |
| 10 |
* any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, but WITHOUT |
| 13 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 15 |
* more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License along |
| 18 |
* with this program; if not, write to the Free Software Foundation, Inc., 59 |
| 19 |
* Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 20 |
* |
| 21 |
* Note: Files generated by the FleXML system have fewer restrictions on them |
| 22 |
* as explained in the header of each generated file. |
| 23 |
*/ |
| 24 |
%{ |
| 25 |
|
| 26 |
/* Version strings. */ |
| 27 |
FLEXML_VERSION |
| 28 |
|
| 29 |
/* ANSI headers. */ |
| 30 |
#include <stdlib.h> /* for realloc() -- needed here when using flex 2.5.4 */ |
| 31 |
#include <stdio.h> |
| 32 |
#include <string.h> |
| 33 |
#include <assert.h> |
| 34 |
#include <stdarg.h> |
| 35 |
#include <ctype.h> |
| 36 |
|
| 37 |
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(__TOS_WIN__) |
| 38 |
# ifndef __STRICT_ANSI__ |
| 39 |
# include <io.h> |
| 40 |
# include <process.h> |
| 41 |
# endif |
| 42 |
#else |
| 43 |
# include <unistd.h> |
| 44 |
#endif |
| 45 |
|
| 46 |
#ifndef FLEXML_INDEXSTACKSIZE |
| 47 |
#define FLEXML_INDEXSTACKSIZE 1000 |
| 48 |
#endif |
| 49 |
|
| 50 |
/* Generated definitions. */ |
| 51 |
FLEXML_DEFINITIONS |
| 52 |
|
| 53 |
/* XML state. */ |
| 54 |
#ifdef FLEX_DEBUG |
| 55 |
# define ENTER(state) debug_enter(state,#state) |
| 56 |
# define LEAVE debug_leave() |
| 57 |
# define SET(state) debug_set(state,#state) |
| 58 |
static void debug_enter(int, const char*); |
| 59 |
static void debug_leave(void); |
| 60 |
static void debug_set(int, const char*); |
| 61 |
#else |
| 62 |
# define ENTER(state) (yy_push_state(state)) |
| 63 |
# define LEAVE (yy_pop_state()) |
| 64 |
# define SET(state) BEGIN(state) |
| 65 |
#endif |
| 66 |
|
| 67 |
/* Generic actions. */ |
| 68 |
#define SKIP /*skip*/ |
| 69 |
#define SUCCEED CLEANUP; return 0 |
| 70 |
|
| 71 |
#define FAIL return fail |
| 72 |
static int fail(const char*, ...); |
| 73 |
|
| 74 |
enum {flexml_max_err_msg_size = 512}; |
| 75 |
static char flexml_err_msg[flexml_max_err_msg_size]; |
| 76 |
const char * parse_err_msg() |
| 77 |
{ |
| 78 |
return flexml_err_msg; |
| 79 |
} |
| 80 |
static void reset_parse_err_msg() |
| 81 |
{ |
| 82 |
flexml_err_msg[0] = '\0'; |
| 83 |
} |
| 84 |
|
| 85 |
/* Cleanup */ |
| 86 |
static void cleanup(void); |
| 87 |
#define CLEANUP cleanup() |
| 88 |
|
| 89 |
/* Text buffer stack handling. */ |
| 90 |
char *bufferstack = NULL; |
| 91 |
static int blimit = FLEXML_BUFFERSTACKSIZE; |
| 92 |
static int bnext = 1; |
| 93 |
|
| 94 |
static int *indexstack = NULL; |
| 95 |
static int ilimit = FLEXML_INDEXSTACKSIZE; |
| 96 |
static int inext = 1; |
| 97 |
|
| 98 |
#define BUFFERSET(P) (P = bnext) |
| 99 |
#define BUFFERPUTC(C) (ck_blimit(), bufferstack[bnext++] = (C)) |
| 100 |
#define BUFFERDONE (BUFFERPUTC('\0')) |
| 101 |
|
| 102 |
#define BUFFERLITERAL(C, P) bufferliteral(C, &(P), yytext) |
| 103 |
|
| 104 |
/* after this is called, there are at least 2 slots left in the stack */ |
| 105 |
static int ck_blimit() |
| 106 |
{ |
| 107 |
if (bnext >= blimit) { |
| 108 |
blimit += FLEXML_BUFFERSTACKSIZE + 2; |
| 109 |
{ |
| 110 |
char *temp = (char *) realloc(bufferstack, blimit); |
| 111 |
assert(temp); |
| 112 |
bufferstack = temp; |
| 113 |
} |
| 114 |
} |
| 115 |
return 0; |
| 116 |
} |
| 117 |
|
| 118 |
/* after this is called, there are at least 2 slots left in the stack */ |
| 119 |
static int ck_ilimit() |
| 120 |
{ |
| 121 |
if (inext >= ilimit) { |
| 122 |
ilimit += FLEXML_INDEXSTACKSIZE + 2; |
| 123 |
{ |
| 124 |
int *temp = (int *) realloc(indexstack, ilimit); |
| 125 |
assert(temp); |
| 126 |
indexstack = temp; |
| 127 |
} |
| 128 |
} |
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
#ifdef FLEXML_NEED_BUFFERLIT |
| 133 |
static void bufferliteral(char c, int* pp, const char* text) |
| 134 |
{ |
| 135 |
BUFFERSET(*pp); |
| 136 |
if (c) { |
| 137 |
const char *s = strchr(text, c), *e = strrchr(text, c); |
| 138 |
assert(s && e && s <= e); |
| 139 |
++s; |
| 140 |
while (s < e) { |
| 141 |
if (isspace(*s)) { |
| 142 |
BUFFERPUTC(' '); |
| 143 |
do ++s; while (s < e && isspace(*s)); |
| 144 |
} else |
| 145 |
BUFFERPUTC(*s++); |
| 146 |
} |
| 147 |
} else { |
| 148 |
const char *s = text; |
| 149 |
while (*s) |
| 150 |
BUFFERPUTC(*s++); |
| 151 |
} |
| 152 |
BUFFERDONE; |
| 153 |
} |
| 154 |
#endif |
| 155 |
|
| 156 |
static void pushbuffer(int p) |
| 157 |
{ |
| 158 |
ck_ilimit(); |
| 159 |
indexstack[inext++] = p; |
| 160 |
indexstack[inext++] = bnext; |
| 161 |
} |
| 162 |
|
| 163 |
static int popbuffer(void) |
| 164 |
{ |
| 165 |
assert(inext >= 2); |
| 166 |
bnext = indexstack[--inext]; |
| 167 |
return indexstack[--inext]; |
| 168 |
} |
| 169 |
|
| 170 |
/* General internal entities are `unput' back onto the input stream... */ |
| 171 |
#define ENTITYTEXT(T) \ |
| 172 |
{ char *s = (T), *e = s+strlen(s);\ |
| 173 |
while (--e >= s) { unput(*e); }} |
| 174 |
|
| 175 |
FLEXML_INCLUDE_INIT_HEADER |
| 176 |
|
| 177 |
%} |
| 178 |
|
| 179 |
/* Flex standard options. */ |
| 180 |
%option stack |
| 181 |
%option noyy_top_state |
| 182 |
%option noinput |
| 183 |
%option noreject |
| 184 |
%option noyymore |
| 185 |
%option noyywrap |
| 186 |
|
| 187 |
/* Flex user-requested options. */ |
| 188 |
FLEXML_FLEX_OPTIONS |
| 189 |
|
| 190 |
/* XML character classes (currently restricted to ASCII). */ |
| 191 |
|
| 192 |
/* "Common syntactic structures." */ |
| 193 |
S [ \t\n\r\f]+ |
| 194 |
s [ \t\n\r\f]* |
| 195 |
|
| 196 |
/* "Names and Tokens." */ |
| 197 |
NameChar [A-Za-z0-9.:_-] |
| 198 |
Name [A-Za-z_:]{NameChar}* |
| 199 |
Names {Name}({S}{Name})* |
| 200 |
Nmtoken ({NameChar})+ |
| 201 |
Nmtokens {Nmtoken}({S}{Nmtoken})* |
| 202 |
|
| 203 |
/* Miscellaneous. */ |
| 204 |
VersionNum [a-zA-Z0-9_.:-]+ |
| 205 |
Eq {s}"="{s} |
| 206 |
Literal \'[^'']*\'|\"[^""]*\" |
| 207 |
|
| 208 |
/* Parser states (flex `exclusive start conditions'): |
| 209 |
* |
| 210 |
* PROLOG the XML prolog of the document before <?xml...> |
| 211 |
* DOCTYPE the XML prolog of the document after <?xml...> |
| 212 |
* EPILOG after the root element |
| 213 |
* INCOMMENT inside an XML comment <!--....--> |
| 214 |
* INPI inside an XML PI <?...?> |
| 215 |
* VALUE1 inside a '...'-delimited literal |
| 216 |
* VALUE2 inside a "..."-delimited literal |
| 217 |
* CDATA inside a <![CDATA[...] ]> section. |
| 218 |
* ROOT_<tag> expect root element <tag> |
| 219 |
* AL_<tag> inside the attribute list for <tag> |
| 220 |
* IN_<tag> inside a <tag> with element contents (ready for end tag) |
| 221 |
* IMPOSSIBLE dummy to permit disabling rules; must be last |
| 222 |
*/ |
| 223 |
%x PROLOG DOCTYPE EPILOG INCOMMENT INPI VALUE1 VALUE2 CDATA |
| 224 |
FLEXML_START_CONDITIONS |
| 225 |
%x IMPOSSIBLE |
| 226 |
|
| 227 |
FLEXML_EXTRA_DEFINITIONS |
| 228 |
|
| 229 |
%% |
| 230 |
|
| 231 |
/* Bypass Flex's default INITIAL state and begin by parsing the XML prolog. */ |
| 232 |
SET(PROLOG); |
| 233 |
reset_parse_err_msg(); |
| 234 |
bufferstack = (char *) malloc(FLEXML_BUFFERSTACKSIZE); |
| 235 |
assert(bufferstack); |
| 236 |
#ifdef FLEX_DEBUG |
| 237 |
{ |
| 238 |
int i; |
| 239 |
for (i = 0; i < blimit; i++) { |
| 240 |
bufferstack[i] = '\377'; |
| 241 |
} |
| 242 |
} |
| 243 |
#endif |
| 244 |
bufferstack[0] = '\0'; |
| 245 |
indexstack = (int *) malloc(FLEXML_INDEXSTACKSIZE * sizeof(int)); |
| 246 |
assert(indexstack); |
| 247 |
indexstack[0] = 0; |
| 248 |
|
| 249 |
FLEXML_EXTRA_DEFINITIONS_INIT |
| 250 |
|
| 251 |
/* COMMENTS and PIs: handled uniformly for efficiency. */ |
| 252 |
|
| 253 |
<FLEXML_COMMENTS,PROLOG,DOCTYPE,EPILOG>{ |
| 254 |
"<!--" ENTER(INCOMMENT); |
| 255 |
"<?" ENTER(INPI); |
| 256 |
} |
| 257 |
<INCOMMENT>{ |
| 258 |
"-->" LEAVE; |
| 259 |
"--" | |
| 260 |
. | |
| 261 |
\n SKIP; |
| 262 |
<<EOF>> FAIL("EOF in comment."); |
| 263 |
} |
| 264 |
<INPI>{ |
| 265 |
"?>" LEAVE; |
| 266 |
. | |
| 267 |
\n SKIP; |
| 268 |
<<EOF>> FAIL("EOF in PI (processing instruction)."); |
| 269 |
} |
| 270 |
|
| 271 |
/* SPACES: skipped uniformly */ |
| 272 |
|
| 273 |
<FLEXML_NON_MIXED,PROLOG,DOCTYPE,EPILOG>{S} SKIP; |
| 274 |
|
| 275 |
/* PROLOG: determine root element and process it. */ |
| 276 |
|
| 277 |
<PROLOG>{ |
| 278 |
"<?xml"({S}version{Eq}(\'{VersionNum}\'|\"{VersionNum}\"))?({S}encoding{Eq}(\'[^']*\'|\"[^"]*\"))?"?>" SET(DOCTYPE); |
| 279 |
"<?xml"[^>]*">" FAIL("Bad declaration %s.",yytext); |
| 280 |
} |
| 281 |
|
| 282 |
<PROLOG,DOCTYPE>{ |
| 283 |
FLEXML_DOCTYPES |
| 284 |
"<!"[^>-][^>]*">" FAIL("Bad declaration %s.",yytext); |
| 285 |
. FAIL("Unexpected character `%c' in prolog.", yytext[0]); |
| 286 |
<<EOF>> FAIL("EOF in prolog."); |
| 287 |
} |
| 288 |
|
| 289 |
/* RULES DERIVED FROM DTD. */ |
| 290 |
FLEXML_RULES |
| 291 |
|
| 292 |
/* EPILOG: after the root element. */ |
| 293 |
|
| 294 |
<EPILOG>{ |
| 295 |
. {SET(PROLOG); yyless(0); CLEANUP; return -1;} |
| 296 |
<<EOF>> SUCCEED; |
| 297 |
} |
| 298 |
|
| 299 |
/* CHARACTER DATA. */ |
| 300 |
|
| 301 |
<FLEXML_MIXED,VALUE1,VALUE2>{ |
| 302 |
FLEXML_ENTITIES |
| 303 |
|
| 304 |
/* Character entities. */ |
| 305 |
"&#"[[:digit:]]+";" BUFFERPUTC((unsigned char)atoi(yytext+2)); |
| 306 |
"&#x"[[:xdigit:]]+";" BUFFERPUTC((unsigned char)strtol(yytext+3,NULL,16)); |
| 307 |
} |
| 308 |
|
| 309 |
<FLEXML_MIXED,VALUE1,VALUE2,CDATA>{ |
| 310 |
"\n" | |
| 311 |
"\r" | |
| 312 |
"\r\n" | |
| 313 |
"\n\r" BUFFERPUTC('\n'); |
| 314 |
} |
| 315 |
|
| 316 |
<FLEXML_MIXED>{ |
| 317 |
"<![CDATA[" ENTER(CDATA); |
| 318 |
"]""]>" FAIL("Unexpected `]""]>' in character data."); |
| 319 |
} |
| 320 |
|
| 321 |
<VALUE1>{ |
| 322 |
\' BUFFERDONE; LEAVE; |
| 323 |
<<EOF>> FAIL("EOF in literal (\"'\" expected)."); |
| 324 |
} |
| 325 |
|
| 326 |
<VALUE2>{ |
| 327 |
\" BUFFERDONE; LEAVE; |
| 328 |
<<EOF>> FAIL("EOF in literal (`\"' expected)."); |
| 329 |
} |
| 330 |
|
| 331 |
<FLEXML_MIXED,VALUE1,VALUE2>{ |
| 332 |
[^<&] BUFFERPUTC(yytext[0]); |
| 333 |
[<&] FAIL("Spurious `%c' in character data.",yytext[0]); |
| 334 |
} |
| 335 |
|
| 336 |
<CDATA>{ |
| 337 |
"]""]>" LEAVE; |
| 338 |
/* "]""]" BUFFERPUTC(yytext[0]); BUFFERPUTC(yytext[1]); */ |
| 339 |
. BUFFERPUTC(yytext[0]); |
| 340 |
<<EOF>> FAIL("EOF in CDATA section."); |
| 341 |
} |
| 342 |
|
| 343 |
/* Impossible rules to avoid warnings from flex(1). */ |
| 344 |
/* Ideally, this should be replaced by code in flexml.pl that |
| 345 |
generates just the states not covered by other rules. */ |
| 346 |
<*>{ |
| 347 |
.|[\n] FAIL("Syntax error on character `%c'.", yytext[0]); |
| 348 |
} |
| 349 |
|
| 350 |
%% |
| 351 |
|
| 352 |
/* Element context stack lookup. */ |
| 353 |
int element_context(int i) |
| 354 |
{ |
| 355 |
return (0<i && i<yy_start_stack_depth |
| 356 |
? yy_start_stack[yy_start_stack_ptr - i] |
| 357 |
: 0); |
| 358 |
} |
| 359 |
|
| 360 |
#ifdef FLEX_DEBUG |
| 361 |
void print_yy_stack(char* fmt, ...) |
| 362 |
{ |
| 363 |
int i = 0; va_list ap; va_start(ap, fmt); |
| 364 |
vfprintf(stderr, fmt, ap); |
| 365 |
if (statenames) { |
| 366 |
for (i=1; i<yy_start_stack_ptr; i++) { |
| 367 |
fprintf(stderr, "%s/", statenames[yy_start_stack[i] ]); |
| 368 |
} |
| 369 |
fprintf(stderr,"%s\n", statenames[YY_START]); |
| 370 |
} |
| 371 |
va_end(ap); |
| 372 |
} |
| 373 |
|
| 374 |
void print_bufferstack() |
| 375 |
{ |
| 376 |
int i; |
| 377 |
fputs("Buffer: ", stderr); |
| 378 |
for (i = 0; i < blimit; i++) { |
| 379 |
if ( bufferstack[i] == '\377' ) break; |
| 380 |
putc(bufferstack[i], stderr); |
| 381 |
} |
| 382 |
putc('\n', stderr); |
| 383 |
} |
| 384 |
|
| 385 |
static void debug_enter(int state, const char* statename) { |
| 386 |
yy_push_state(state); |
| 387 |
if (yy_flex_debug) { |
| 388 |
print_yy_stack("--ENTER(%s) : ",statename); |
| 389 |
print_bufferstack(); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
static void debug_leave(void) { |
| 394 |
if (yy_flex_debug) { |
| 395 |
print_yy_stack("--LEAVE : "); |
| 396 |
print_bufferstack(); |
| 397 |
} |
| 398 |
yy_pop_state(); |
| 399 |
} |
| 400 |
|
| 401 |
static void debug_set(int state, const char* statename) { |
| 402 |
BEGIN(state); |
| 403 |
if (yy_flex_debug) print_yy_stack("--SET(%s) : ",statename); |
| 404 |
} |
| 405 |
#endif |
| 406 |
|
| 407 |
|
| 408 |
static void cleanup(void) |
| 409 |
{ |
| 410 |
if (statenames) { |
| 411 |
free(statenames); |
| 412 |
statenames = NULL; |
| 413 |
} |
| 414 |
free(bufferstack); |
| 415 |
bufferstack = NULL; |
| 416 |
|
| 417 |
free(indexstack); |
| 418 |
indexstack = NULL; |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
static int fail(const char* fmt, ...) |
| 423 |
{ |
| 424 |
int chars_left, used; |
| 425 |
va_list ap; va_start(ap, fmt); |
| 426 |
#ifdef FLEXML_yylineno |
| 427 |
used = sprintf(flexml_err_msg, |
| 428 |
"Invalid XML (XML input line %d, state %d): ", |
| 429 |
yylineno, YY_START); |
| 430 |
#else |
| 431 |
used = sprintf(flexml_err_msg, |
| 432 |
"Invalid XML (state %d): ", |
| 433 |
YY_START); |
| 434 |
#endif |
| 435 |
chars_left = flexml_max_err_msg_size - used - 1; |
| 436 |
vsnprintf(flexml_err_msg + used, chars_left, fmt, ap); |
| 437 |
va_end(ap); |
| 438 |
|
| 439 |
#ifndef FLEXML_quiet_parser |
| 440 |
/* print directly to sdterr */ |
| 441 |
fprintf(stderr, "%s\n", flexml_err_msg); |
| 442 |
flexml_err_msg[0] = '\0'; |
| 443 |
#endif |
| 444 |
|
| 445 |
cleanup(); |
| 446 |
|
| 447 |
return 1; |
| 448 |
} |