pgc.l 16.2 KB
Newer Older
M
Marc G. Fournier 已提交
1
/* This is a modified version of src/backend/parser/scan.l */
2
%{
3
#include "config.h"
M
Marc G. Fournier 已提交
4 5

#include <ctype.h>
M
Marc G. Fournier 已提交
6
#include <sys/types.h>
7
#include <limits.h>
8 9 10 11 12 13

#ifndef PATH_MAX
#include <sys/param.h>
#define PATH_MAX MAXPATHLEN
#endif

14 15 16 17 18
#if defined(HAVE_STRING_H)
#include <string.h>
#else
#include <strings.h>
#endif
M
Marc G. Fournier 已提交
19
#include <errno.h>
20

M
Marc G. Fournier 已提交
21 22 23 24 25 26
#include "postgres.h"
#include "miscadmin.h"
#include "nodes/pg_list.h"
#include "nodes/parsenodes.h"
#include "parser/gramparse.h"
#include "parser/scansup.h"
M
 
Marc G. Fournier 已提交
27
#include "extern.h"
M
 
Marc G. Fournier 已提交
28
#include "preproc.h"
M
Marc G. Fournier 已提交
29
#include "utils/builtins.h"
30

M
Marc G. Fournier 已提交
31 32 33 34 35 36 37 38
/* some versions of lex define this as a macro */
#if defined(yywrap)
#undef yywrap
#endif /* yywrap */

extern YYSTYPE yylval;
int llen;
char literal[MAX_PARSE_BUFFER];
39
int before_comment;
M
Marc G. Fournier 已提交
40

41 42 43 44 45 46
struct _yy_buffer { YY_BUFFER_STATE 	buffer;
		    long		lineno;
		    char	      * filename;
		    struct _yy_buffer * next;
		  } *yy_buffer = NULL;

M
 
Marc G. Fournier 已提交
47 48 49
struct _defines *defines = NULL;
static char *old;

50
%}
51
%option yylineno
M
 
Marc G. Fournier 已提交
52
%s C SQL incl def def_ident
M
Marc G. Fournier 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
/* OK, here is a short description of lex/flex rules behavior.
 * The longest pattern which matches an input string is always chosen.
 * For equal-length patterns, the first occurring in the rules list is chosen.
 * INITIAL is the starting condition, to which all non-conditional rules apply.
 * When in an exclusive condition, only those rules defined for that condition apply.
 *
 * Exclusive states change parsing rules while the state is active.
 * There are exclusive states for quoted strings, extended comments,
 *  and to eliminate parsing troubles for numeric strings.
 * Exclusive states:
 *  <xb> binary numeric string - thomas 1997-11-16
 *  <xc> extended C-style comments - tgl 1997-07-12
 *  <xd> delimited identifiers (double-quoted identifiers) - tgl 1997-10-27
 *  <xh> hexadecimal numeric string - thomas 1997-11-16
 *  <xm> numeric strings with embedded minus sign - tgl 1997-09-05
 *  <xq> quoted strings - tgl 1997-07-30
 *
 * The "extended comment" syntax closely resembles allowable operator syntax.
 * So, when in condition <xc>, only strings which would terminate the
 *  "extended comment" trigger any action other than "ignore".
 * Be sure to match _any_ candidate comment, including those with appended
 *	operator-like symbols. - thomas 1997-07-14
 */

%x xb
%x xc
%x xd
80
%x xdc
M
Marc G. Fournier 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
%x xh
%x xm
%x xq

/* Binary number
 */
xbstart			[bB]{quote}
xbstop			{quote}
xbinside		[^']*
xbcat			{quote}{space}*\n{space}*{quote}

/* Hexadecimal number
 */
xhstart			[xX]{quote}
xhstop			{quote}
xhinside		[^']*
xhcat			{quote}{space}*\n{space}*{quote}

/* Extended quote
 * xqdouble implements SQL92 embedded quote
 * xqcat allows strings to cross input lines
 */
quote			'
xqstart			{quote}
xqstop			{quote}
xqdouble		{quote}{quote}
xqinside		[^\\']*
xqliteral		[\\](.|\n)
xqcat			{quote}{space}*\n{space}*{quote}

/* Delimited quote
 * Allows embedded spaces and other special characters into identifiers.
 */
dquote			\"
xdstart			{dquote}
xdstop			{dquote}
xdinside		[^"]*

/* Comments
 * Ignored by the scanner and parser.
 */
xcline			[\/][\*].*[\*][\/]{space}*\n*
xcstart			[\/][\*]{op_and_self}*
xcstop			{op_and_self}*[\*][\/]({space}*|\n)
xcinside		[^*]*
xcstar			[^/]

digit			[0-9]
number			[-+.0-9Ee]
letter			[\200-\377_A-Za-z]
letter_or_digit	[\200-\377_A-Za-z0-9]

identifier		{letter}{letter_or_digit}*

typecast		"::"

M
Michael Meskes 已提交
137 138
self			[,()\[\].;$\:\+\-\*\/\%\<\>\=\|]
op_and_self		[\~\!\@\#\^\&\|\?\$\:\+\-\*\/\%\<\>\=]
M
Marc G. Fournier 已提交
139 140 141 142
operator		{op_and_self}+

xmstop			-

M
 
Marc G. Fournier 已提交
143 144 145 146 147
integer			[\-]?{digit}+
/*
real			[\-]?{digit}+\.{digit}+([Ee][-+]?{digit}+)?
*/
real			[\-]?(((({digit}*\.{digit}+)|({digit}+\.{digit}*))([Ee][-+]?{digit}+)?)|({digit}+[Ee][-+]?{digit}+))
M
Marc G. Fournier 已提交
148 149 150 151

param			\${integer}

comment			("--"|"//").*\n
M
Michael Meskes 已提交
152
ccomment		"//".*\n
M
Marc G. Fournier 已提交
153 154 155 156 157 158

space			[ \t\n\f]
other			.

/* some stuff needed for ecpg */
exec    [eE][xX][eE][cC]
M
 
Marc G. Fournier 已提交
159
define	[dD][eE][fF][iI][nN][eE]
160
include [iI][nN][cC][lL][uU][dD][eE]
M
Marc G. Fournier 已提交
161 162
sql     [sS][qQ][lL]

M
 
Marc G. Fournier 已提交
163 164
cppline		{space}*#.*(\\{space}*\n)*\n*

M
Marc G. Fournier 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177
/* DO NOT PUT ANY COMMENTS IN THE FOLLOWING SECTION.
 * AT&T lex does not properly handle C-style comments in this second lex block.
 * So, put comments here. tgl - 1997-09-08
 *
 * Quoted strings must allow some special characters such as single-quote
 *  and newline.
 * Embedded single-quotes are implemented both in the SQL/92-standard
 *  style of two adjacent single quotes "''" and in the Postgres/Java style
 *  of escaped-quote "\'".
 * Other embedded escaped characters are matched explicitly and the leading
 *  backslash is dropped from the string. - thomas 1997-09-24
 */

178
%%
M
Marc G. Fournier 已提交
179 180
<SQL>{comment}		{ /* ignore */ }

M
Michael Meskes 已提交
181
{xcline}		{ ECHO; }
M
Marc G. Fournier 已提交
182

M
Michael Meskes 已提交
183
<xc>{xcstar}		{ ECHO; }
184 185
{xcstart}		{
				before_comment = YYSTATE;
M
Michael Meskes 已提交
186
				ECHO;
187 188
	 			BEGIN(xc);
			}
M
Marc G. Fournier 已提交
189

M
Michael Meskes 已提交
190
<xc>{xcstop}	{ ECHO; BEGIN(before_comment); }
M
Marc G. Fournier 已提交
191

M
Michael Meskes 已提交
192
<xc>{xcinside}	{ ECHO; }
M
Marc G. Fournier 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206

<SQL>{xbstart}		{
					BEGIN(xb);
					llen = 0;
					*literal = '\0';
				}
<xb>{xbstop}	{
					char* endptr;

					BEGIN(SQL);
					errno = 0;
					yylval.ival = strtol((char *)literal,&endptr,2);
					if (*endptr != '\0' || errno == ERANGE)
						yyerror("ERROR: Bad binary integer input!");
207
					return ICONST;
M
Marc G. Fournier 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
				}
<xh>{xhinside}	|
<xb>{xbinside}	{
					if ((llen+yyleng) > (MAX_PARSE_BUFFER - 1))
						yyerror("ERROR: quoted string parse buffer exceeded");
					memcpy(literal+llen, yytext, yyleng+1);
					llen += yyleng;
				}
<xh>{xhcat}		|
<xb>{xbcat}		{
				}

<SQL>{xhstart}		{
					BEGIN(xh);
					llen = 0;
					*literal = '\0';
				}
<xh>{xhstop}	{
					char* endptr;

					BEGIN(SQL);
					errno = 0;
					yylval.ival = strtol((char *)literal,&endptr,16);
					if (*endptr != '\0' || errno == ERANGE)
						yyerror("ERROR: Bad hexadecimal integer input");
233
					return ICONST;
M
Marc G. Fournier 已提交
234 235 236 237 238 239 240 241 242
				}

<SQL>{xqstart}		{
					BEGIN(xq);
					llen = 0;
					*literal = '\0';
				}
<xq>{xqstop}	{
					BEGIN(SQL);
M
 
Marc G. Fournier 已提交
243
					yylval.str = mm_strdup(scanstr(literal));
244
					return SCONST;
M
Marc G. Fournier 已提交
245 246
				}
<xq>{xqdouble}	|
M
 
Marc G. Fournier 已提交
247
<xq>{xqinside}  |
M
Marc G. Fournier 已提交
248
<xq>{xqliteral} {
M
 
Marc G. Fournier 已提交
249
					if ((llen+yyleng) > (MAX_PARSE_BUFFER - 1))
M
Marc G. Fournier 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
						yyerror("ERROR: quoted string parse buffer exceeded");
					memcpy(literal+llen, yytext, yyleng+1);
					llen += yyleng;
				}
<xq>{xqcat}		{
				}


<SQL>{xdstart}		{
					BEGIN(xd);
					llen = 0;
					*literal = '\0';
				}
<xd>{xdstop}	{
					BEGIN(SQL);
M
 
Marc G. Fournier 已提交
265
					yylval.str = mm_strdup(literal);
266
					return CSTRING;
M
Marc G. Fournier 已提交
267 268 269 270 271 272 273
				}
<xd>{xdinside}	{
					if ((llen+yyleng) > (MAX_PARSE_BUFFER - 1))
						yyerror("ERROR: quoted string parse buffer exceeded");
					memcpy(literal+llen, yytext, yyleng+1);
					llen += yyleng;
				}
M
 
Marc G. Fournier 已提交
274
{xdstart}		{
275 276 277 278 279 280
					BEGIN(xdc);
					llen = 0;
					*literal = '\0';
				}
<xdc>{xdstop}	{
					BEGIN(C);
M
 
Marc G. Fournier 已提交
281
					yylval.str = mm_strdup(literal);
282
					return CSTRING;
283 284 285 286 287 288 289
				}
<xdc>{xdinside}	{
					if ((llen+yyleng) > (MAX_PARSE_BUFFER - 1))
						yyerror("ERROR: quoted string parse buffer exceeded");
					memcpy(literal+llen, yytext, yyleng+1);
					llen += yyleng;
				}
M
Marc G. Fournier 已提交
290 291 292 293

<xm>{space}*	{ /* ignore */ }
<xm>{xmstop}	{
					BEGIN(SQL);
294
					return yytext[0];
M
Marc G. Fournier 已提交
295 296 297 298
				}


<SQL>{typecast}			{ 	return TYPECAST; }
M
 
Marc G. Fournier 已提交
299 300
<SQL>{self}/{space}*-[\.0-9]	{
					BEGIN(xm);
301
					return yytext[0];
M
Marc G. Fournier 已提交
302
				}
M
Michael Meskes 已提交
303 304 305 306 307 308 309 310 311
<SQL>{self}			{ /* 
				   * We may find a ';' inside a structure
				   * definition in a TYPE or VAR statement.
				   * This is not an EOL marker.
				   */
				  if (yytext[0] == ';' && struct_level == 0)
					 BEGIN C;
				  return yytext[0];
				}
M
Marc G. Fournier 已提交
312
<SQL>{operator}/-[\.0-9]	{
M
 
Marc G. Fournier 已提交
313
					yylval.str = mm_strdup((char*)yytext);
314
					return Op;
M
Marc G. Fournier 已提交
315 316 317
				}
<SQL>{operator}		{
					if (strcmp((char*)yytext,"!=") == 0)
M
 
Marc G. Fournier 已提交
318
						yylval.str = mm_strdup("<>"); /* compatability */
M
Marc G. Fournier 已提交
319
					else
M
 
Marc G. Fournier 已提交
320
						yylval.str = mm_strdup((char*)yytext);
321
					return Op;
M
Marc G. Fournier 已提交
322 323 324
				}
<SQL>{param}			{
					yylval.ival = atoi((char*)&yytext[1]);
325
					return PARAM;
M
Marc G. Fournier 已提交
326 327 328 329
				}
<SQL>{identifier}/{space}*-{number}	{
					int i;
					ScanKeyword		*keyword;
M
 
Marc G. Fournier 已提交
330
					char lower_text[NAMEDATALEN];
M
Marc G. Fournier 已提交
331 332

					BEGIN(xm);
M
 
Marc G. Fournier 已提交
333 334 335 336 337 338 339
					/* this should leave the last byte set to '\0' */
					strncpy(lower_text, yytext, NAMEDATALEN-1);
					for(i = 0; lower_text[i]; i++)
						if (isascii((unsigned char)lower_text[i]) && isupper(lower_text[i]))
							lower_text[i] = tolower(lower_text[i]);

					keyword = ScanKeywordLookup((char*)lower_text);
M
Marc G. Fournier 已提交
340
					if (keyword != NULL) {
341
						return keyword->value;
M
Marc G. Fournier 已提交
342 343 344
					}
					else
					{
M
 
Marc G. Fournier 已提交
345
						keyword = ScanECPGKeywordLookup((char*)lower_text);
M
Marc G. Fournier 已提交
346
						if (keyword != NULL) {
347
							return keyword->value;
M
Marc G. Fournier 已提交
348 349 350
						}
						else
						{
M
 
Marc G. Fournier 已提交
351 352 353 354 355 356 357 358 359 360 361 362
							struct _defines *ptr;

							for (ptr = defines; ptr; ptr = ptr->next)
							{
								if (strcmp(yytext, ptr->old) == 0)
								{
									struct _yy_buffer *yb;

									yb = mm_alloc(sizeof(struct _yy_buffer));

						                        yb->buffer =  YY_CURRENT_BUFFER;
						                        yb->lineno = yylineno;
M
 
Marc G. Fournier 已提交
363
						                        yb->filename = mm_strdup(input_filename);
M
 
Marc G. Fournier 已提交
364 365 366 367 368 369 370 371 372 373
						                        yb->next = yy_buffer;

						                        yy_buffer = yb;

 									yy_scan_string(ptr->new);
									break;
								}
							}
							if (ptr == NULL) 
							{
M
 
Marc G. Fournier 已提交
374
								yylval.str = mm_strdup((char*)yytext);
375
								return IDENT;
M
 
Marc G. Fournier 已提交
376
							}
M
Marc G. Fournier 已提交
377 378 379
						}
					}
				}
M
Michael Meskes 已提交
380
<C,SQL>{integer}/{space}*-{number}	{
M
Marc G. Fournier 已提交
381 382 383 384 385 386 387 388 389 390 391 392
					char* endptr;

					BEGIN(xm);
					errno = 0;
					yylval.ival = strtol((char *)yytext,&endptr,10);
					if (*endptr != '\0' || errno == ERANGE)
					{
						errno = 0;
						yylval.dval = strtod(((char *)yytext),&endptr);
						if (*endptr != '\0' || errno == ERANGE)
							yyerror("ERROR: Bad integer input");
						yyerror("WARNING: Integer input is out of range; promoted to float");
393
						return FCONST;
M
Marc G. Fournier 已提交
394
					}
395
					return ICONST;
M
Marc G. Fournier 已提交
396
				}
M
Michael Meskes 已提交
397
<C,SQL>{real}/{space}*-{number} {
M
Marc G. Fournier 已提交
398 399 400 401 402 403 404
					char* endptr;

					BEGIN(xm);
					errno = 0;
					yylval.dval = strtod(((char *)yytext),&endptr);
					if (*endptr != '\0' || errno == ERANGE)
						yyerror("ERROR: Bad float8 input");
405
					return FCONST;
M
Marc G. Fournier 已提交
406
				}
M
Michael Meskes 已提交
407
<C,SQL>{integer}		{
M
Marc G. Fournier 已提交
408 409 410 411 412 413 414 415 416 417 418
					char* endptr;

					errno = 0;
					yylval.ival = strtol((char *)yytext,&endptr,10);
					if (*endptr != '\0' || errno == ERANGE)
					{
						errno = 0;
						yylval.dval = strtod(((char *)yytext),&endptr);
						if (*endptr != '\0' || errno == ERANGE)
							yyerror("ERROR: Bad integer input");
						yyerror("WARNING: Integer input is out of range; promoted to float");
419
						return FCONST;
M
Marc G. Fournier 已提交
420
					}
421
					return ICONST;
M
Marc G. Fournier 已提交
422
				}
M
Michael Meskes 已提交
423
<C,SQL>{real}			{
M
Marc G. Fournier 已提交
424 425 426 427 428 429
					char* endptr;

					errno = 0;
					yylval.dval = strtod((char *)yytext,&endptr);
					if (*endptr != '\0' || errno == ERANGE)
						yyerror("ERROR: Bad float input");
430
					return FCONST;
M
Marc G. Fournier 已提交
431
				}
432
<SQL>:{identifier}(("->"|\.){identifier})*	{
M
 
Marc G. Fournier 已提交
433
					yylval.str = mm_strdup((char*)yytext+1);
434 435
					return(CVARIABLE);
			}
M
Marc G. Fournier 已提交
436 437 438
<SQL>{identifier}	{
					int i;
					ScanKeyword		*keyword;
M
 
Marc G. Fournier 已提交
439
					char lower_text[NAMEDATALEN];
M
Marc G. Fournier 已提交
440

M
 
Marc G. Fournier 已提交
441 442 443 444 445
					/* this should leave the last byte set to '\0' */
					strncpy(lower_text, yytext, NAMEDATALEN-1);
					for(i = 0; lower_text[i]; i++)
						if (isascii((unsigned char)lower_text[i]) && isupper(lower_text[i]))
							lower_text[i] = tolower(lower_text[i]);
M
 
Marc G. Fournier 已提交
446

M
 
Marc G. Fournier 已提交
447
					keyword = ScanKeywordLookup((char*)lower_text);
M
Marc G. Fournier 已提交
448
					if (keyword != NULL) {
449
						return keyword->value;
M
Marc G. Fournier 已提交
450 451 452
					}
					else
					{
M
 
Marc G. Fournier 已提交
453
						keyword = ScanECPGKeywordLookup((char*)lower_text);
M
Marc G. Fournier 已提交
454
						if (keyword != NULL) {
455
							return keyword->value;
M
Marc G. Fournier 已提交
456 457 458
						}
						else
						{
M
 
Marc G. Fournier 已提交
459 460 461 462 463 464 465 466 467 468 469 470
							struct _defines *ptr;

							for (ptr = defines; ptr; ptr = ptr->next)
							{
								if (strcmp(yytext, ptr->old) == 0)
								{
									struct _yy_buffer *yb;

									yb = mm_alloc(sizeof(struct _yy_buffer));

						                        yb->buffer =  YY_CURRENT_BUFFER;
						                        yb->lineno = yylineno;
M
 
Marc G. Fournier 已提交
471
						                        yb->filename = mm_strdup(input_filename);
M
 
Marc G. Fournier 已提交
472 473 474 475 476 477 478 479 480 481
						                        yb->next = yy_buffer;

						                        yy_buffer = yb;

 									yy_scan_string(ptr->new);
									break;
								}
							}
							if (ptr == NULL) 
							{
M
 
Marc G. Fournier 已提交
482
								yylval.str = mm_strdup((char*)yytext);
483
								return IDENT;
M
 
Marc G. Fournier 已提交
484
							}
M
Marc G. Fournier 已提交
485 486 487 488
						}
					}
				}
<SQL>{space}			{ /* ignore */ }
489
<SQL>{other}			{ return yytext[0]; }
M
Marc G. Fournier 已提交
490
<C>{exec}{space}{sql}		{ BEGIN SQL; return SQL_START; }
491
<C>{ccomment}			{ /* ignore */ } 
M
 
Marc G. Fournier 已提交
492
<C>{cppline}			{
M
 
Marc G. Fournier 已提交
493
					yylval.str = mm_strdup((char*)yytext);
M
 
Marc G. Fournier 已提交
494 495
					return(CPP_LINE);
				}
M
Marc G. Fournier 已提交
496 497 498 499 500
<C>{identifier}	{
					ScanKeyword		*keyword;

					keyword = ScanCKeywordLookup((char*)yytext);
					if (keyword != NULL) {
501
						return keyword->value;
M
Marc G. Fournier 已提交
502 503 504
					}
					else
					{
M
 
Marc G. Fournier 已提交
505 506 507 508 509 510 511 512 513 514 515 516
						struct _defines *ptr;

						for (ptr = defines; ptr; ptr = ptr->next)
						{
							if (strcmp(yytext, ptr->old) == 0)
							{
								struct _yy_buffer *yb;

								yb = mm_alloc(sizeof(struct _yy_buffer));

					                        yb->buffer =  YY_CURRENT_BUFFER;
					                        yb->lineno = yylineno;
M
 
Marc G. Fournier 已提交
517
					                        yb->filename = mm_strdup(input_filename);
M
 
Marc G. Fournier 已提交
518 519 520 521 522 523 524 525 526 527
					                        yb->next = yy_buffer;

					                        yy_buffer = yb;

								yy_scan_string(ptr->new);
								break;
							}
						}
						if (ptr == NULL) 
						{
M
 
Marc G. Fournier 已提交
528
							yylval.str = mm_strdup((char*)yytext);
529
							return IDENT;
M
 
Marc G. Fournier 已提交
530
						}
M
Marc G. Fournier 已提交
531 532 533
					}
				}
<C>";"	      	        { return(';'); }
534 535
<C>","	      	        { return(','); }
<C>"*"	      	        { return('*'); }
M
Marc G. Fournier 已提交
536
<C>{space}		{ ECHO; }
537 538 539 540 541
<C>\{			{ return('{'); }
<C>\}			{ return('}'); }
<C>\[			{ return('['); }
<C>\]			{ return(']'); }
<C>\=			{ return('='); }
542
<C>{other}			{ return S_ANYTHING; }
M
 
Marc G. Fournier 已提交
543 544 545
<C>{exec}{space}{sql}{space}{define}	{BEGIN(def_ident);}
<def_ident>{space}	{}
<def_ident>{identifier}	{
M
 
Marc G. Fournier 已提交
546
				old = mm_strdup(yytext);
M
 
Marc G. Fournier 已提交
547 548 549 550 551 552 553 554 555 556 557 558 559
				BEGIN(def);
				llen = 0;
				*literal = '\0';
			}
<def>{space}		/* eat the whitespace */
<def>";"		{
				struct _defines *ptr, *this;
        
                                for (ptr = defines; ptr != NULL; ptr = ptr->next)
                                {
                                     if (strcmp(old, ptr->old) == 0)
                                     {
					free(ptr->new);
M
 
Marc G. Fournier 已提交
560
					ptr->new = mm_strdup(scanstr(literal));
M
 
Marc G. Fournier 已提交
561 562 563 564 565 566 567 568
                                     }
                                }
				if (ptr == NULL)
				{                        
                                        this = (struct _defines *) mm_alloc(sizeof(struct _defines));

                                        /* initial definition */
                                        this->old = old;
M
 
Marc G. Fournier 已提交
569
                                        this->new = mm_strdup(scanstr(literal));
M
 
Marc G. Fournier 已提交
570 571 572 573 574 575 576 577 578 579 580 581
					this->next = defines;
					defines = this;
				}

				BEGIN(C);
			}
<def>[^";"]		{
				if ((llen+yyleng) > (MAX_PARSE_BUFFER - 1))
					yyerror("ERROR: define statement parse buffer exceeded");
				memcpy(literal+llen, yytext, yyleng+1);
				llen += yyleng;
			}
M
Marc G. Fournier 已提交
582 583
<C>{exec}{space}{sql}{space}{include}	{ BEGIN(incl); }
<incl>{space}		/* eat the whitespace */
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
<incl>[^ \t\n]+ 	{ /* got the include file name */
			  struct _yy_buffer *yb;
			  struct _include_path *ip;
			  char inc_file[PATH_MAX];

			  yb = mm_alloc(sizeof(struct _yy_buffer));

			  yb->buffer =  YY_CURRENT_BUFFER;
			  yb->lineno = yylineno;
			  yb->filename = input_filename;
			  yb->next = yy_buffer;

			  yy_buffer = yb;

			  if (yytext[strlen(yytext) - 1] == ';')
				yytext[strlen(yytext) - 1] = '\0';

			  yyin = NULL;
			  for (ip = include_paths; yyin == NULL && ip != NULL; ip = ip->next)
			  {
				if (strlen(ip->path) + strlen(yytext) + 3 > PATH_MAX)
				{
M
Marc G. Fournier 已提交
606
					fprintf(stderr, "Error: Path %s/%s is too long in line %d, skipping.\n", ip->path, yytext, yylineno);
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
					continue;
				}
			  	sprintf (inc_file, "%s/%s", ip->path, yytext);
		          	yyin = fopen( inc_file, "r" );
			  	if (!yyin)
				{
					if (strcmp(inc_file + strlen(inc_file) - 2, ".h"))
					{
						strcat(inc_file, ".h");
						yyin = fopen( inc_file, "r" );
					}

				}
			  }
			  if (!yyin)
			  {
M
Marc G. Fournier 已提交
623
				fprintf(stderr, "Error: Cannot open include file %s in line %d\n", yytext, yylineno);
624
				exit(NO_INCLUDE_FILE); 
625 626
			  }

M
 
Marc G. Fournier 已提交
627
			  input_filename = mm_strdup(inc_file);
628 629
			  yy_switch_to_buffer(yy_create_buffer(yyin,YY_BUF_SIZE ));
			  yylineno = 0;
M
Michael Meskes 已提交
630
			  output_line_number();
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653

			  BEGIN C;
			}
<incl>";"		{ BEGIN C; }
<<EOF>>			{ if (yy_buffer == NULL)
				yyterminate();
			  else
			  {
				struct _yy_buffer *yb = yy_buffer;

				if (yyin != NULL)
					fclose(yyin);

				yy_delete_buffer( YY_CURRENT_BUFFER );
				yy_switch_to_buffer(yy_buffer->buffer);

				yylineno = yy_buffer->lineno;

				free(input_filename);
				input_filename = yy_buffer->filename;

				yy_buffer = yy_buffer->next;
				free(yb);
M
Michael Meskes 已提交
654
				output_line_number();
655 656
			  }
			}
657 658
%%
void
659
lex_init(void)
660
{
661
    braces_open = 0;
662 663 664
    BEGIN C;
}

665
int yywrap(void) 
666 667 668
{ 
    return 1;
}