pgc.l 34.7 KB
Newer Older
1
%{
2 3 4 5 6 7 8 9
/*-------------------------------------------------------------------------
 *
 * pgc.l
 *	  lexical scanner for ecpg
 *
 * This is a modified version of src/backend/parser/scan.l
 *
 *
10
 * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
11
 * Portions Copyright (c) 1994, Regents of the University of California
12 13 14
 *
 *
 * IDENTIFICATION
15
 *	  $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/pgc.l,v 1.154 2007/08/22 08:20:58 meskes Exp $
16 17 18
 *
 *-------------------------------------------------------------------------
 */
19 20
#include "postgres_fe.h"

M
Marc G. Fournier 已提交
21
#include <ctype.h>
M
Marc G. Fournier 已提交
22
#include <sys/types.h>
23
#include <limits.h>
24

M
 
Marc G. Fournier 已提交
25
#include "extern.h"
26

M
Marc G. Fournier 已提交
27
extern YYSTYPE yylval;
28

29
static int		xcdepth = 0;	/* depth of nesting in slash-star comments */
30
static char	   *dolqstart;      /* current $foo$ quote start string */
31
static bool		escape_string_warning;
32
static bool 	warn_on_first_escape;
33 34
static YY_BUFFER_STATE scanbufhandle;
static char *scanbuf;
35

36 37 38 39 40 41
/*
 * literalbuf is used to accumulate literal values when multiple rules
 * are needed to parse a single literal.  Call startlit to reset buffer
 * to empty, addlit to add text.  Note that the buffer is permanently
 * malloc'd to the largest size needed so far in the current run.
 */
42 43 44
static char    *literalbuf = NULL;		/* expandable buffer */
static int		literallen;				/* actual current length */
static int		literalalloc;			/* current allocated buffer size */
45

46
#define startlit()	(literalbuf[0] = '\0', literallen = 0)
47
static void addlit(char *ytext, int yleng);
48
static void addlitchar (unsigned char);
49
static void parse_include (void);
50
static void check_escape_warning(void);
51
static bool ecpg_isspace(char ch);
52
static bool isdefine(void);
53

M
Michael Meskes 已提交
54
char *token_start;
M
Michael Meskes 已提交
55
int state_before;
M
Marc G. Fournier 已提交
56

57 58 59 60 61 62 63
struct _yy_buffer 
{ 
	YY_BUFFER_STATE		buffer;
	long				lineno;
	char		  		*filename;
	struct _yy_buffer 	*next;
} *yy_buffer = NULL;
64

M
 
Marc G. Fournier 已提交
65 66
static char *old;

67 68 69
#define MAX_NESTED_IF 128
static short preproc_tos;
static short ifcond;
70 71 72 73
static struct _if_value 
{
	short condition;
	short else_branch;
74 75
} stacked_if_value[MAX_NESTED_IF];

76
%}
M
Michael Meskes 已提交
77

78 79
%option 8bit
%option never-interactive
80
%option nodefault
81 82
%option noyywrap

83
%option yylineno
84

85
%x C SQL incl def def_ident undef 
M
Michael Meskes 已提交
86 87 88

/*
 * OK, here is a short description of lex/flex rules behavior.
M
Marc G. Fournier 已提交
89 90
 * The longest pattern which matches an input string is always chosen.
 * For equal-length patterns, the first occurring in the rules list is chosen.
M
Michael Meskes 已提交
91 92 93
 * INITIAL is the starting state, to which all non-conditional rules apply.
 * Exclusive states change parsing rules while the state is active.  When in
 * an exclusive state, only those rules defined for that state apply.
M
Marc G. Fournier 已提交
94
 *
M
Michael Meskes 已提交
95 96
 * We use exclusive states for quoted strings, extended comments,
 * and to eliminate parsing troubles for numeric strings.
M
Marc G. Fournier 已提交
97
 * Exclusive states:
M
Michael Meskes 已提交
98
 *	<xb> bit string literal
99 100 101
 *	<xc> extended C-style comments - thomas 1997-07-12
 *	<xd> delimited identifiers (double-quoted identifiers) - thomas 1997-10-27
 *	<xh> hexadecimal numeric string - thomas 1997-11-16
102 103
 *	<xq> standard quoted strings - thomas 1997-07-30
 *	<xe> extended quoted strings (support backslash escape sequences)
104
 *	<xn> national character quoted strings
B
Bruce Momjian 已提交
105
 *  <xdolq> $foo$ quoted strings
M
Marc G. Fournier 已提交
106 107
 */

M
Michael Meskes 已提交
108
%x xb
M
Marc G. Fournier 已提交
109 110
%x xc
%x xd
111
%x xdc
M
Marc G. Fournier 已提交
112
%x xh
113
%x xe
114
%x xn
M
Marc G. Fournier 已提交
115
%x xq
116
%x xdolq
117 118
%x xcond
%x xskip
M
Marc G. Fournier 已提交
119

M
Michael Meskes 已提交
120
/* Bit string
M
Marc G. Fournier 已提交
121
 */
M
Michael Meskes 已提交
122 123
xbstart			[bB]{quote}
xbinside		[^']*
M
Marc G. Fournier 已提交
124

125
/* Hexadecimal number */
M
Marc G. Fournier 已提交
126
xhstart			[xX]{quote}
127
xhinside		[^']*
M
Marc G. Fournier 已提交
128

129
/* National character */
130
xnstart			[nN]{quote}
M
Michael Meskes 已提交
131

132
/* Quoted string that allows backslash escapes */
B
Bruce Momjian 已提交
133
xestart 		[eE]{quote}
134 135 136 137
xeinside		[^\\']+
xeescape		[\\][^0-7]
xeoctesc		[\\][0-7]{1,3}
xehexesc		[\\]x[0-9A-Fa-f]{1,2}
138 139

/* C version of hex number */
140
xch				0[xX][0-9A-Fa-f]*
M
Michael Meskes 已提交
141

M
Marc G. Fournier 已提交
142
/* Extended quote
143
 * xqdouble implements embedded quote, ''''
M
Marc G. Fournier 已提交
144 145 146
 */
xqstart			{quote}
xqdouble		{quote}{quote}
147
xqinside		[^']+
M
Marc G. Fournier 已提交
148

149 150 151 152 153
/* $foo$ style quotes ("dollar quoting")
 * The quoted string starts with $foo$ where "foo" is an optional string
 * in the form of an identifier, except that it may not contain "$",
 * and extends to the first occurrence of an identical string.
 * There is *no* processing of the quoted text.
154 155 156
 *
 * {dolqfailed} is an error rule to avoid scanner backup when {dolqdelim}
 * fails to match its trailing "$".
157
 */
158 159 160 161
dolq_start		[A-Za-z\200-\377_]
dolq_cont		[A-Za-z\200-\377_0-9]
dolqdelim		\$({dolq_start}{dolq_cont}*)?\$
dolqfailed		\${dolq_start}{dolq_cont}*
M
Michael Meskes 已提交
162
dolqinside		[^$]+
163

164
/* Double quote
M
Marc G. Fournier 已提交
165 166 167 168 169
 * Allows embedded spaces and other special characters into identifiers.
 */
dquote			\"
xdstart			{dquote}
xdstop			{dquote}
B
Bruce Momjian 已提交
170
xddouble		{dquote}{dquote}
M
Michael Meskes 已提交
171
xdinside		[^"]+
M
Michael Meskes 已提交
172 173 174 175 176 177

/* special stuff for C strings */
xdcqq			\\\\
xdcqdq			\\\"
xdcother		[^"]
xdcinside		({xdcqq}|{xdcqdq}|{xdcother})
M
Marc G. Fournier 已提交
178

179 180
/* C-style comments
 *
M
Michael Meskes 已提交
181 182 183
 * The "extended comment" syntax closely resembles allowable operator syntax.
 * The tricky part here is to get lex to recognize a string starting with
 * slash-star as a comment, when interpreting it as an operator would produce
184
 * a longer match --- remember lex will prefer a longer match!	Also, if we
185 186
 * have something like plus-slash-star, lex will think this is a 3-character
 * operator whereas we want to see it as a + operator and a comment start.
M
Michael Meskes 已提交
187
 * The solution is two-fold:
188
 * 1. append {op_chars}* to xcstart so that it matches as much text as
189 190 191
 *	  {operator} would. Then the tie-breaker (first matching rule of same
 *	  length) ensures xcstart wins.  We put back the extra stuff with yyless()
 *	  in case it contains a star-slash that should terminate the comment.
M
Michael Meskes 已提交
192
 * 2. In the operator rule, check for slash-star within the operator, and
193 194
 *	  if found throw it back with yyless().  This handles the plus-slash-star
 *	  problem.
195
 * Dash-dash comments have similar interactions with the operator rule.
M
Marc G. Fournier 已提交
196
 */
197
xcstart			\/\*{op_chars}*
M
Michael Meskes 已提交
198
xcstop			\*+\/
199
xcinside		[^*/]+
M
Marc G. Fournier 已提交
200 201

digit			[0-9]
M
Michael Meskes 已提交
202 203
ident_start		[A-Za-z\200-\377_]
ident_cont		[A-Za-z\200-\377_0-9\$]
M
Marc G. Fournier 已提交
204

M
Michael Meskes 已提交
205
identifier		{ident_start}{ident_cont}*
M
Marc G. Fournier 已提交
206

207
array			({ident_cont}|{whitespace}|[\[\]\+\-\*\%\/\(\)\>\.])*
M
Marc G. Fournier 已提交
208 209
typecast		"::"

210 211
/*
 * "self" is the set of chars that should be returned as single-character
212
 * tokens.	"op_chars" is the set of chars that can make up "Op" tokens,
213 214 215 216 217 218 219
 * which can be one or more characters long (but if a single-char token
 * appears in the "self" set, it is not to be returned as an Op).  Note
 * that the sets overlap, but each has some chars that are not in the other.
 *
 * If you change either set, adjust the character lists appearing in the
 * rule for "operator"!
 */
M
Michael Meskes 已提交
220 221
self			[,()\[\].;\:\+\-\*\/\%\^\<\>\=]
op_chars		[\~\!\@\#\^\&\|\`\?\+\-\*\/\%\<\>\=]
222
operator		{op_chars}+
M
Marc G. Fournier 已提交
223

224
/* we no longer allow unary minus in numbers.
225
 * instead we pass it separately to parser. there it gets
226
 * coerced via doNegate() -- Leon aug 20 1999
227 228 229
 *
 * {realfail1} and {realfail2} are added to prevent the need for scanner
 * backup when the {real} rule fails to match completely.
M
Michael Meskes 已提交
230
 */
231

M
Michael Meskes 已提交
232 233
integer			{digit}+
decimal			(({digit}*\.{digit}+)|({digit}+\.{digit}*))
234 235 236
real			({integer}|{decimal})[Ee][-+]?{digit}+
realfail1		({integer}|{decimal})[Ee]
realfail2		({integer}|{decimal})[Ee][-+]
M
Marc G. Fournier 已提交
237 238 239

param			\${integer}

M
Michael Meskes 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252
/*
 * In order to make the world safe for Windows and Mac clients as well as
 * Unix ones, we accept either \n or \r as a newline.  A DOS-style \r\n
 * sequence will be seen as two successive newlines, but that doesn't cause
 * any problems.  SQL92-style comments, which start with -- and extend to the
 * next newline, are treated as equivalent to a single whitespace character.
 *
 * NOTE a fine point: if there is no newline following --, we will absorb
 * everything to the end of the input as a comment.  This is correct.  Older
 * versions of Postgres failed to recognize -- as a comment if the input
 * did not end with a newline.
 *
 * XXX perhaps \f (formfeed) should be treated as a newline as well?
253 254 255
 *
 * XXX if you change the set of whitespace characters, fix ecpg_isspace()
 * to agree.
M
Michael Meskes 已提交
256 257
 */

M
Michael Meskes 已提交
258
ccomment		"//".*\n
M
Marc G. Fournier 已提交
259

260
space			[ \t\n\r\f]
M
Michael Meskes 已提交
261
horiz_space		[ \t\f]
262
newline			[\n\r]
M
Michael Meskes 已提交
263 264
non_newline		[^\n\r]

265
comment			("--"{non_newline}*)
M
Michael Meskes 已提交
266

267
whitespace		({space}+|{comment})
M
Michael Meskes 已提交
268 269 270 271 272 273 274 275 276

/*
 * SQL92 requires at least one newline in the whitespace separating
 * string literals that are to be concatenated.  Silly, but who are we
 * to argue?  Note that {whitespace_with_newline} should not have * after
 * it, whereas {whitespace} should generally have a * after it...
 */

horiz_whitespace	({horiz_space}|{comment})
277
whitespace_with_newline ({horiz_whitespace}*{newline}{whitespace}*)
M
Michael Meskes 已提交
278

279 280
quote			'
quotestop		{quote}{whitespace}*
281
quotecontinue	{quote}{whitespace_with_newline}{quote}
282 283
quotefail		{quote}{whitespace}*"-"

284 285 286 287
/* special characters for other dbms */
/* we have to react differently in compat mode */
informix_special	[\$]

M
Marc G. Fournier 已提交
288 289 290
other			.

/* some stuff needed for ecpg */
B
Bruce Momjian 已提交
291 292 293 294
exec			[eE][xX][eE][cC]
sql				[sS][qQ][lL]
define			[dD][eE][fF][iI][nN][eE]
include 		[iI][nN][cC][lL][uU][dD][eE]
M
Michael Meskes 已提交
295
undef			[uU][nN][dD][eE][fF]
M
Marc G. Fournier 已提交
296

B
Bruce Momjian 已提交
297 298 299 300 301
ifdef			[iI][fF][dD][eE][fF]
ifndef			[iI][fF][nN][dD][eE][fF]
else			[eE][lL][sS][eE]
elif			[eE][lL][iI][fF]
endif			[eE][nN][dD][iI][fF]
302

B
Bruce Momjian 已提交
303
struct			[sS][tT][rR][uU][cC][tT]
304

305
exec_sql		{exec}{space}*{sql}{space}*
M
Michael Meskes 已提交
306
ipdigit			({digit}|{digit}{digit}|{digit}{digit}{digit})
307
ip				{ipdigit}\.{ipdigit}\.{ipdigit}\.{ipdigit}
308

309 310 311
/* we might want to parse all cpp include files */
cppinclude 		{space}*#{include}{space}*

312 313
/* Take care of cpp lines, they may also be continuated */
cppline			{space}*#(.*\\{space})*.*{newline}
M
 
Marc G. Fournier 已提交
314

315
/*
316 317
 * Dollar quoted strings are totally opaque, and no escaping is done on them.
 * Other quoted strings must allow some special characters such as single-quote
318
 *	and newline.
319
 * Embedded single-quotes are implemented both in the SQL standard
320 321
 *	style of two adjacent single quotes "''" and in the Postgres/Java style
 *	of escaped-quote "\'".
M
Marc G. Fournier 已提交
322
 * Other embedded escaped characters are matched explicitly and the leading
323
 *	backslash is dropped from the string. - thomas 1997-09-24
M
Michael Meskes 已提交
324
 * Note that xcstart must appear before operator, as explained above!
325
 *	Also whitespace (comment) must appear before operator.
M
Marc G. Fournier 已提交
326 327
 */

328
%%
329 330

%{
331 332
		/* code to execute during start of each call of yylex() */
		token_start = NULL;
333 334
%}

M
Michael Meskes 已提交
335
<SQL>{whitespace}	{ /* ignore */ }
M
Marc G. Fournier 已提交
336

337
<C,SQL>{xcstart}		{
B
Bruce Momjian 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351
					token_start = yytext;
					state_before = YYSTATE;
					xcdepth = 0;
					BEGIN(xc);
					/* Put back any characters past slash-star; see above */
					yyless(2);
					fputs("/*", yyout);
				}
<xc>{xcstart}	{
					xcdepth++;
			 		/* Put back any characters past slash-star; see above */
			 		yyless(2);
					fputs("/*", yyout);
				}
352

B
Bruce Momjian 已提交
353 354 355 356 357 358
<xc>{xcstop}	{
					ECHO;
					if (xcdepth <= 0)
					{
						BEGIN(state_before);
						token_start = NULL;
359
					}
B
Bruce Momjian 已提交
360 361 362
					else
						xcdepth--;
				}
363

364 365
<xc>{xcinside}	{ ECHO; }
<xc>{op_chars}	{ ECHO; }
366
<xc>\*+ 		{ ECHO; }
367

368
<xc><<EOF>>		{ mmerror(PARSE_ERROR, ET_FATAL, "Unterminated /* comment"); }
369

M
Michael Meskes 已提交
370
<SQL>{xbstart}	{
371 372 373 374 375
					token_start = yytext;
					BEGIN(xb);
					startlit();
					addlitchar('b');
				}
376 377
<xb>{quotestop} |
<xb>{quotefail}	{
378 379 380 381 382 383 384
					yyless(1);
					BEGIN(SQL);
					if (literalbuf[strspn(literalbuf, "01") + 1] != '\0')
						mmerror(PARSE_ERROR, ET_ERROR, "invalid bit string input.");
					yylval.str = mm_strdup(literalbuf);
					return BCONST;
				}
M
Marc G. Fournier 已提交
385 386

<xh>{xhinside}	|
M
Michael Meskes 已提交
387
<xb>{xbinside}	{ addlit(yytext, yyleng); }
388 389
<xh>{quotecontinue}	|
<xb>{quotecontinue}	{ /* ignore */ }
390
<xb><<EOF>>		{ mmerror(PARSE_ERROR, ET_FATAL, "Unterminated bit string"); }
M
Marc G. Fournier 已提交
391

392 393 394 395 396 397
<SQL>{xhstart}	{
					token_start = yytext;
					BEGIN(xh);
					startlit();
					addlitchar('x');
				}
398 399 400 401 402 403 404
<xh>{quotestop}	|
<xh>{quotefail} 	{
				yyless(1);
				BEGIN(SQL);
				yylval.str = mm_strdup(literalbuf);
				return XCONST;
			}
405

406
<xh><<EOF>>		{ mmerror(PARSE_ERROR, ET_FATAL, "Unterminated hexadecimal integer"); }
M
Michael Meskes 已提交
407 408
<SQL>{xnstart}              {
				/* National character.
409 410
				 * Transfer it as-is to the backend.
				 */
411
				warn_on_first_escape = true;
412
		        token_start = yytext;
413
				state_before = YYSTATE;
414
				BEGIN(xn);
415 416 417 418 419 420
				startlit();
			}
<C>{xqstart}	{
				warn_on_first_escape = false;
				token_start = yytext;
				state_before = YYSTATE;
421
				BEGIN(xq);
M
Michael Meskes 已提交
422 423
				startlit();
			}
424
<SQL>{xqstart}	{
425 426 427
				warn_on_first_escape = true;
				token_start = yytext;
				state_before = YYSTATE;
428
				BEGIN(xq);
429 430
				startlit();
			}
431
<SQL>{xestart}	{
432 433 434
				warn_on_first_escape = false;
				token_start = yytext;
				state_before = YYSTATE;
435
				BEGIN(xe);
436 437
				startlit();
			}
438 439
<xq>{quotestop} |
<xq>{quotefail} {
440 441 442 443 444
				yyless(1);
				BEGIN(state_before);
				yylval.str = mm_strdup(literalbuf);
				return SCONST;
			}
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
<xe>{quotestop} |
<xe>{quotefail} {
				yyless(1);
				BEGIN(state_before);
				yylval.str = mm_strdup(literalbuf);
				return ECONST;
			}
<xn>{quotestop} |
<xn>{quotefail} {
				yyless(1);
				BEGIN(state_before);
				yylval.str = mm_strdup(literalbuf);
				return NCONST;
			}
<xq,xe,xn>{xqdouble}		{ addlitchar('\''); }
<xq,xn>{xqinside}		{ addlit(yytext, yyleng); }
461 462
<xe>{xeinside}		{ addlit(yytext, yyleng); }
<xe>{xeescape}  	{ 
463 464 465
				check_escape_warning();
				addlit(yytext, yyleng);
			}
466
<xe>{xeoctesc}		{ 
467 468 469
				check_escape_warning();
				addlit(yytext, yyleng);
			}
470
<xe>{xehexesc}		{ 
471 472 473
				check_escape_warning();
				addlit(yytext, yyleng);
			}
474
<xq,xe,xn>{quotecontinue}	{ /* ignore */ }
475
<xe>.                   {
476 477 478
			   /* This is only needed for \ just before EOF */
			   addlitchar(yytext[0]);
			}
479
<xq,xe,xn><<EOF>>		{ mmerror(PARSE_ERROR, ET_FATAL, "Unterminated quoted string"); }
480 481 482 483 484 485
<SQL>{dolqfailed}	{
				/* throw back all but the initial "$" */
				yyless(1);
				/* and treat it as {other} */
				return yytext[0];	
			}
486 487 488 489 490
<SQL>{dolqdelim}        {
				token_start = yytext;
				dolqstart = mm_strdup(yytext);
				BEGIN(xdolq);
				startlit();
M
Michael Meskes 已提交
491
				addlit(yytext, yyleng);
492 493 494 495
			}
<xdolq>{dolqdelim}      {
				if (strcmp(yytext, dolqstart) == 0)
				{
M
Michael Meskes 已提交
496
					addlit(yytext, yyleng);
497 498 499
					free(dolqstart);
					BEGIN(SQL);
					yylval.str = mm_strdup(literalbuf);
M
Michael Meskes 已提交
500
					return DOLCONST;
501 502 503
				}
				else
				{
504 505 506 507 508 509 510
			        /*
			         * When we fail to match $...$ to dolqstart, transfer
			         * the $... part to the output, but put back the final
			         * $ for rescanning.  Consider $delim$...$junk$delim$
			         */
			        addlit(yytext, yyleng-1);
			        yyless(yyleng-1);
511 512
				}
			}
513 514
<xdolq>{dolqinside} 	{ addlit(yytext, yyleng); }
<xdolq>{dolqfailed}	{ addlit(yytext, yyleng); }
M
Michael Meskes 已提交
515
<xdolq>{other}		{
516
				/* single quote or dollar sign */
517 518
				addlitchar(yytext[0]);
			}
519
<xdolq><<EOF>> 		{ base_yyerror("unterminated dollar-quoted string"); }
520 521 522 523 524
<SQL>{xdstart}		{
						state_before = YYSTATE;
						BEGIN(xd);
						startlit();
					}
M
Michael Meskes 已提交
525
<xd>{xdstop}		{
526
						BEGIN(state_before);
527 528
						if (literallen == 0)
							mmerror(PARSE_ERROR, ET_ERROR, "zero-length delimited identifier");
529
						/* The backend will truncate the idnetifier here. We do not as it does not change the result. */
530 531 532 533 534 535 536 537
						yylval.str = mm_strdup(literalbuf);
						return CSTRING;
					}
<xdc>{xdstop}		{
						BEGIN(state_before);
						yylval.str = mm_strdup(literalbuf);
						return CSTRING;
					}
538
<xd>{xddouble}		{ addlitchar('"'); }
539
<xd>{xdinside}		{ addlit(yytext, yyleng); }
540
<xd,xdc><<EOF>>		{ mmerror(PARSE_ERROR, ET_FATAL, "Unterminated quoted identifier"); }
541 542 543 544
<C,SQL>{xdstart}	{
						state_before = YYSTATE;
						BEGIN(xdc);
						startlit();
M
Michael Meskes 已提交
545
					}
546 547
<xdc>{xdcinside}	{ addlit(yytext, yyleng); }
<SQL>{typecast}		{ return TYPECAST; }
548 549
<SQL>{informix_special}	{
			  /* are we simulating Informix? */
550 551 552 553 554 555 556
				if (INFORMIX_MODE)
				{
					unput(':');
				}
				else
					return yytext[0];
				}
557 558 559 560 561 562
<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)
563
						 BEGIN(C);
564 565 566 567 568 569 570 571 572 573
					  return yytext[0];
					}
<SQL>{operator}		{
						/*
						 * Check for embedded slash-star or dash-dash; those
						 * are comment starts, so operator must stop there.
						 * Note that slash-star or dash-dash at the first
						 * character will match a prior rule, not this one.
						 */
						int		nchars = yyleng;
574 575
						char   *slashstar = strstr(yytext, "/*");
						char   *dashdash = strstr(yytext, "--");
576 577

						if (slashstar && dashdash)
578
						{
579 580 581
							/* if both appear, take the first one */
							if (slashstar > dashdash)
								slashstar = dashdash;
582
						}
583 584 585
						else if (!slashstar)
							slashstar = dashdash;
						if (slashstar)
586
							nchars = slashstar - yytext;
587 588

						/*
589
						 * For SQL compatibility, '+' and '-' cannot be the
590
						 * last char of a multi-char operator unless the operator
591
						 * contains chars that are not in SQL operators.
592 593
						 * The idea is to lex '=-' as two operators, but not
						 * to forbid operator names like '?-' that could not be
594
						 * sequences of SQL operators.
M
Michael Meskes 已提交
595
						 */
596 597 598 599 600 601 602 603
						while (nchars > 1 &&
							   (yytext[nchars-1] == '+' ||
								yytext[nchars-1] == '-'))
						{
							int		ic;

							for (ic = nchars-2; ic >= 0; ic--)
							{
M
Michael Meskes 已提交
604
								if (strchr("~!@#^&|`?%", yytext[ic]))
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
									break;
							}
							if (ic >= 0)
								break; /* found a char that makes it OK */
							nchars--; /* else remove the +/-, and check again */
						}

						if (nchars < yyleng)
						{
							/* Strip the unwanted chars from the token */
							yyless(nchars);
							/*
							 * If what we have left is only one char, and it's
							 * one of the characters matching "self", then
							 * return it as a character token the same way
							 * that the "self" rule would have.
							 */
							if (nchars == 1 &&
M
Michael Meskes 已提交
623
								strchr(",()[].;:+-*/%^<>=", yytext[0]))
624 625 626 627
								return yytext[0];
						}

						/* Convert "!=" operator to "<>" for compatibility */
628
						if (strcmp(yytext, "!=") == 0)
629 630
							yylval.str = mm_strdup("<>");
						else
631
							yylval.str = mm_strdup(yytext);
632
						return Op;
M
Michael Meskes 已提交
633
					}
634
<SQL>{param}		{
635
						yylval.ival = atol(yytext+1);
636 637 638 639 640
						return PARAM;
					}
<C,SQL>{integer}	{
						long val;
						char* endptr;
M
Michael Meskes 已提交
641

M
Marc G. Fournier 已提交
642
						errno = 0;
643 644 645 646 647 648 649 650 651
						val = strtol((char *)yytext, &endptr,10);
						if (*endptr != '\0' || errno == ERANGE
#ifdef HAVE_LONG_INT_64
							/* if long > 32 bits, check for overflow of int4 */
							|| val != (long) ((int32) val)
#endif
							)
						{
							errno = 0;
652
							yylval.str = mm_strdup(yytext);
653 654 655 656 657 658
							return FCONST;
						}
						yylval.ival = val;
						return ICONST;
					}
<SQL>{ip}			{
659
						yylval.str = mm_strdup(yytext);
660
						return IP;
M
Michael Meskes 已提交
661 662
				}
<C,SQL>{decimal}	{
663
						yylval.str = mm_strdup(yytext);
664
						return FCONST;
665
			}
666
<C,SQL>{real}		{
667
						yylval.str = mm_strdup(yytext);
M
Michael Meskes 已提交
668
						return FCONST;
669 670
			}
<SQL>{realfail1}	{
671 672 673 674
						yyless(yyleng-1);
						yylval.str = mm_strdup(yytext);
						return FCONST;
					}
675
<SQL>{realfail2}	{
676 677 678 679
						yyless(yyleng-2);
						yylval.str = mm_strdup(yytext);
						return FCONST;
					}
680
<SQL>:{identifier}((("->"|\.){identifier})|(\[{array}\]))*	{
681
						yylval.str = mm_strdup(yytext+1);
682 683
						return(CVARIABLE);
					}
M
Marc G. Fournier 已提交
684
<SQL>{identifier}	{
685
						const ScanKeyword    *keyword;
M
Michael Meskes 已提交
686
						
687
						if (!isdefine())
688
						{
689
							/* Is it an SQL/ECPG keyword? */
690 691 692
							keyword = ScanKeywordLookup(yytext);
							if (keyword != NULL)
								return keyword->value;
693

694 695 696 697
							/* Is it a C keyword? */
							keyword = ScanCKeywordLookup(yytext);
							if (keyword != NULL)
								return keyword->value;
698

699 700 701
							/*
							 * None of the above.  Return it as an identifier.
							 *
M
Michael Meskes 已提交
702
							 * The backend will attempt to truncate and case-fold
703 704 705 706
							 * the identifier, but I see no good reason for ecpg
							 * to do so; that's just another way that ecpg could get
							 * out of step with the backend.
							 */
M
Michael Meskes 已提交
707 708
							yylval.str = mm_strdup(yytext);
							return IDENT;
M
Marc G. Fournier 已提交
709 710
						}
					}
711
<SQL>{other}		{ return yytext[0]; }
712
<C>{exec_sql}		{ BEGIN(SQL); return SQL_START; }
713
<C>{informix_special}	{ 
714 715 716
						/* are we simulating Informix? */
						if (INFORMIX_MODE)
						{
717
							BEGIN(SQL);
718 719 720 721 722 723
							return SQL_START;
						}
						else
							return S_ANYTHING;
					 }
<C>{ccomment}		{ ECHO; }
M
Michael Meskes 已提交
724
<C>{xch}			{
725
						char* endptr;
M
Michael Meskes 已提交
726 727

						errno = 0;
M
Michael Meskes 已提交
728
						yylval.ival = strtoul((char *)yytext,&endptr,16);
729 730 731
						if (*endptr != '\0' || errno == ERANGE)
						{
							errno = 0;
732
							yylval.str = mm_strdup(yytext);
733 734 735 736
							return SCONST;
						}
						return ICONST;
					}
737
<C>{cppinclude}		{
738 739 740 741 742 743 744 745 746 747 748
						if (system_includes)
						{
							BEGIN(incl);
						}
						else
					 	{
							yylval.str = mm_strdup(yytext);
							return(CPP_LINE);
					  	}
					}
<C,SQL>{cppline}	{
749
						yylval.str = mm_strdup(yytext);
750
						return(CPP_LINE);
M
Michael Meskes 已提交
751
					}
752
<C>{identifier} 	{
753
						const ScanKeyword		*keyword;
M
 
Marc G. Fournier 已提交
754

755
						if (INFORMIX_MODE || !isdefine())
756 757 758 759
						{
							keyword = ScanCKeywordLookup(yytext);
							if (keyword != NULL)
								return keyword->value;
760 761
							else
							{
762
								yylval.str = mm_strdup(yytext);
763
								return IDENT;
M
 
Marc G. Fournier 已提交
764 765
							}
						}
M
Marc G. Fournier 已提交
766
					}
767
<C>":"				{ return(':'); }
768 769 770 771 772 773 774 775 776
<C>";"				{ return(';'); }
<C>","				{ return(','); }
<C>"*"				{ return('*'); }
<C>"%"				{ return('%'); }
<C>"/"				{ return('/'); }
<C>"+"				{ return('+'); }
<C>"-"				{ return('-'); }
<C>"("				{ return('('); }
<C>")"				{ return(')'); }
M
Michael Meskes 已提交
777
<C,xskip>{space}		{ ECHO; }
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
<C>\{				{ return('{'); }
<C>\}				{ return('}'); }
<C>\[				{ return('['); }
<C>\]				{ return(']'); }
<C>\=				{ return('='); }
<C>"->"				{ return(S_MEMBER); }
<C>">>"				{ return(S_RSHIFT); }
<C>"<<"				{ return(S_LSHIFT); }
<C>"||"				{ return(S_OR); }
<C>"&&"				{ return(S_AND); }
<C>"++"				{ return(S_INC); }
<C>"--"				{ return(S_DEC); }
<C>"=="				{ return(S_EQUAL); }
<C>"!="				{ return(S_NEQUAL); }
<C>"+="				{ return(S_ADD); }
<C>"-="				{ return(S_SUB); }
<C>"*="				{ return(S_MUL); }
<C>"/="				{ return(S_DIV); }
<C>"%="				{ return(S_MOD); }
<C>"->*"			{ return(S_MEMPOINT); }
<C>".*"				{ return(S_DOTPOINT); }
<C>{other}			{ return S_ANYTHING; }
800
<C>{exec_sql}{define}{space}*	{ BEGIN(def_ident); }
801 802
<C>{informix_special}{define}{space}*	{
						/* are we simulating Informix? */
803
						if (INFORMIX_MODE)
804 805 806 807 808
						{
							BEGIN(def_ident);
						}
						else
						{
809
							yyless(1);
810 811 812
							return (S_ANYTHING);
						}
					}
M
Michael Meskes 已提交
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
<C>{exec_sql}{undef}{space}*		{ BEGIN(undef); }
<C>{informix_special}{undef}{space}* 	{
						/* are we simulating Informix? */
						if (INFORMIX_MODE)
						{
							BEGIN(undef);
						}
						else
						{
							yyless(1);
							return (S_ANYTHING);
						}
					}
<undef>{identifier}{space}*";" {
					struct _defines *ptr, *ptr2 = NULL;
					int i;

					/*
					 *	Skip the ";" and trailing whitespace. Note that yytext
					 *	contains at least one non-space character plus the ";"
					 */
					for (i = strlen(yytext)-2;
835
						 i > 0 && ecpg_isspace(yytext[i]);
M
Michael Meskes 已提交
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
						 i-- )
						;
					yytext[i+1] = '\0';


					for (ptr = defines; ptr != NULL; ptr2 = ptr, ptr = ptr->next)
					{
						if (strcmp(yytext, ptr->old) == 0)
						{
							if (ptr2 == NULL)
								defines = ptr->next;
							else
								ptr2->next = ptr->next;
							free(ptr->new);
							free(ptr->old);
							free(ptr);
							break;
						}
					}

					BEGIN(C);
				}
858
<undef>{other}|\n  		{
M
Michael Meskes 已提交
859 860 861
		                	mmerror(PARSE_ERROR, ET_FATAL, "Missing identifier in 'EXEC SQL UNDEF' command");
			                yyterminate();
				}
862
<C>{exec_sql}{include}{space}*	{ BEGIN(incl); }
863 864
<C>{informix_special}{include}{space}* { 
					  /* are we simulating Informix? */
865
					  if (INFORMIX_MODE)
866
					  {
867
						  BEGIN(incl);
868 869 870
					  }
					  else
					  {
871 872
						  yyless(1);
						  return (S_ANYTHING);
873 874
					  }
					}
875
<C,xskip>{exec_sql}{ifdef}{space}*	{ ifcond = TRUE; BEGIN(xcond); }
876 877
<C,xskip>{informix_special}{ifdef}{space}* { 
					  /* are we simulating Informix? */
878
					  if (INFORMIX_MODE)
879
					  {
880 881
						  ifcond = TRUE;
						  BEGIN(xcond);
882 883 884
					  }
					  else
					  {
885 886
						  yyless(1);
						  return (S_ANYTHING);
887 888
					  }
					}
889
<C,xskip>{exec_sql}{ifndef}{space}* { ifcond = FALSE; BEGIN(xcond); }
890 891
<C,xskip>{informix_special}{ifndef}{space}* { 
					  /* are we simulating Informix? */
892
					  if (INFORMIX_MODE)
893
					  {
894 895
						  ifcond = FALSE;
						  BEGIN(xcond);
896 897 898
					  }
					  else
					  {
899 900
						  yyless(1);
						  return (S_ANYTHING);
901 902
					  }
					}
903
<C,xskip>{exec_sql}{elif}{space}*	{	/* pop stack */
904
						if ( preproc_tos == 0 ) {
905
							mmerror(PARSE_ERROR, ET_FATAL, "Missing matching 'EXEC SQL IFDEF / EXEC SQL IFNDEF'");
906
						}
907 908 909 910
						else if ( stacked_if_value[preproc_tos].else_branch )
							mmerror(PARSE_ERROR, ET_FATAL, "Missing 'EXEC SQL ENDIF;'");
						else
							preproc_tos--;
911 912 913

						ifcond = TRUE; BEGIN(xcond);
					}
914
<C,xskip>{informix_special}{elif}{space}* { 
915 916 917 918
					/* are we simulating Informix? */
					if (INFORMIX_MODE)
					{
						if (preproc_tos == 0)
919
							mmerror(PARSE_ERROR, ET_FATAL, "Missing matching 'EXEC SQL IFDEF / EXEC SQL IFNDEF'");
920
						else if (stacked_if_value[preproc_tos].else_branch)
921 922 923 924
							mmerror(PARSE_ERROR, ET_FATAL, "Missing 'EXEC SQL ENDIF;'");
						else
							preproc_tos--;

925 926
						ifcond = TRUE;
						BEGIN(xcond);
927
					}
928 929 930 931 932 933
					else
					{
						yyless(1);
						return (S_ANYTHING);
					}
				}
934

935
<C,xskip>{exec_sql}{else}{space}*";" {	/* only exec sql endif pops the stack, so take care of duplicated 'else' */
936 937 938 939 940 941
					if (stacked_if_value[preproc_tos].else_branch)
						mmerror(PARSE_ERROR, ET_FATAL, "Duplicated 'EXEC SQL ELSE;'");
					else
					{
						stacked_if_value[preproc_tos].else_branch = TRUE;
						stacked_if_value[preproc_tos].condition =
942
							(stacked_if_value[preproc_tos-1].condition &&
943
							 !stacked_if_value[preproc_tos].condition);
944

945 946 947 948
						if (stacked_if_value[preproc_tos].condition)
							BEGIN(C);
						else
							BEGIN(xskip);
949
					}
950
				}
951
<C,xskip>{informix_special}{else}{space}*	{
952 953 954 955
					/* are we simulating Informix? */
					if (INFORMIX_MODE)
					{
						if (stacked_if_value[preproc_tos].else_branch)
956
							mmerror(PARSE_ERROR, ET_FATAL, "Duplicated 'EXEC SQL ELSE;'");
957 958
						else
						{
959 960 961
							stacked_if_value[preproc_tos].else_branch = TRUE;
							stacked_if_value[preproc_tos].condition =
							(stacked_if_value[preproc_tos-1].condition &&
962
							 !stacked_if_value[preproc_tos].condition);
963

964
							if (stacked_if_value[preproc_tos].condition)
965 966 967 968 969
								BEGIN(C);
							else
								BEGIN(xskip);
						}
					}
970 971 972 973
					else
					{
						yyless(1);
						return (S_ANYTHING);
974
					}
975 976 977 978 979 980 981 982 983 984 985 986
				}
<C,xskip>{exec_sql}{endif}{space}*";" {
					if (preproc_tos == 0)
						mmerror(PARSE_ERROR, ET_FATAL, "Unmatched 'EXEC SQL ENDIF;'");
					else
						preproc_tos--;

					if (stacked_if_value[preproc_tos].condition)
					   BEGIN(C);
					else
					   BEGIN(xskip);
				}
M
Michael Meskes 已提交
987
<C,xskip>{informix_special}{endif}{space}*";"	{
988 989 990 991
					/* are we simulating Informix? */
					if (INFORMIX_MODE)
					{
						if (preproc_tos == 0)
992 993 994 995
							mmerror(PARSE_ERROR, ET_FATAL, "Unmatched 'EXEC SQL ENDIF;'");
						else
							preproc_tos--;

996 997
						if (stacked_if_value[preproc_tos].condition)
							BEGIN(C);
998
						else
999 1000 1001 1002 1003 1004
							BEGIN(xskip);
					}
					else
					{
						yyless(1);
						return (S_ANYTHING);
1005
					}
1006
				}
1007

1008
<xskip>{other}		{ /* ignore */ }
1009

1010
<xcond>{identifier}{space}*";" {
1011 1012 1013 1014 1015 1016
					if (preproc_tos >= MAX_NESTED_IF-1)
						mmerror(PARSE_ERROR, ET_FATAL, "Too many nested 'EXEC SQL IFDEF' conditions");
					else 
					{
						struct _defines *defptr;
						unsigned int i;
1017

1018 1019 1020 1021 1022
						/*
						 *	Skip the ";" and trailing whitespace. Note that yytext
						 *	contains at least one non-space character plus the ";"
						 */
						for (i = strlen(yytext)-2;
1023
							 i > 0 && ecpg_isspace(yytext[i]);
1024 1025 1026 1027 1028 1029
							 i-- )
							;
						yytext[i+1] = '\0';

						for (defptr = defines;
							 defptr != NULL && strcmp(yytext, defptr->old) != 0;
1030
							 defptr = defptr->next);
1031 1032 1033 1034 1035

						preproc_tos++;
						stacked_if_value[preproc_tos].else_branch = FALSE;
						stacked_if_value[preproc_tos].condition =
						(defptr ? ifcond : !ifcond) && stacked_if_value[preproc_tos-1].condition;
1036 1037
					}

1038
					if (stacked_if_value[preproc_tos].condition)
1039
						BEGIN(C);
1040 1041 1042 1043
					else
						BEGIN(xskip);
				}

1044 1045 1046 1047
<xcond>{other}|\n	{
				mmerror(PARSE_ERROR, ET_FATAL, "Missing identifier in 'EXEC SQL IFDEF' command");
				yyterminate();
			}
1048
<def_ident>{identifier} {
M
 
Marc G. Fournier 已提交
1049
				old = mm_strdup(yytext);
M
 
Marc G. Fournier 已提交
1050
				BEGIN(def);
1051
				startlit();
M
 
Marc G. Fournier 已提交
1052
			}
1053 1054 1055 1056
<def_ident>{other}|\n	{
				mmerror(PARSE_ERROR, ET_FATAL, "Missing identifier in 'EXEC SQL DEFINE' command");
				yyterminate();
			}	
1057
<def>{space}*";"	{
1058
						struct _defines *ptr, *this;
1059

1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
						for (ptr = defines; ptr != NULL; ptr = ptr->next)
						{
							 if (strcmp(old, ptr->old) == 0)
							 {
								free(ptr->new);
								ptr->new = mm_strdup(literalbuf);
							 }
						}
						if (ptr == NULL)
						{
M
Michael Meskes 已提交
1070
							this = (struct _defines *) mm_alloc(sizeof(struct _defines));
1071

M
Michael Meskes 已提交
1072 1073 1074
							/* initial definition */
							this->old = old;
							this->new = mm_strdup(literalbuf);
1075
							this->next = defines;
M
Michael Meskes 已提交
1076
							this->used = NULL;
1077 1078
							defines = this;
						}
1079

1080 1081 1082
						BEGIN(C);
					}
<def>[^;]			{ addlit(yytext, yyleng); }
1083 1084
<incl>\<[^\>]+\>{space}*";"?		{	parse_include(); }
<incl>{dquote}{xdinside}{dquote}{space}*";"?	{	parse_include(); }
1085
<incl>[^;\<\>\"]+";"		{ parse_include(); }
1086 1087 1088 1089
<incl>{other}|\n		{
					mmerror(PARSE_ERROR, ET_FATAL, "Incorrect 'EXEC SQL INCLUDE' command");
					yyterminate();
				}
1090 1091

<<EOF>>				{
1092 1093
			  		if (yy_buffer == NULL)
					{
1094 1095 1096 1097 1098
				  		if ( preproc_tos > 0 ) 
						{
					  		preproc_tos = 0;
							mmerror(PARSE_ERROR, ET_FATAL, "Missing 'EXEC SQL ENDIF;'");
				  		}
1099 1100 1101 1102 1103 1104 1105
						yyterminate();
					}
			  		else
			  		{
						struct _yy_buffer *yb = yy_buffer;
						int i;
						struct _defines *ptr;
1106

1107 1108 1109 1110 1111 1112 1113 1114 1115
						for (ptr = defines; ptr; ptr = ptr->next)
							if (ptr->used == yy_buffer)
							{
								ptr->used = NULL;
								break;
							}
								
						if (yyin != NULL)
							fclose(yyin);
1116

1117 1118
						yy_delete_buffer( YY_CURRENT_BUFFER );
						yy_switch_to_buffer(yy_buffer->buffer);
1119

1120
						yylineno = yy_buffer->lineno;
1121

1122 1123
						/* We have to output the filename only if we change files here */
						i = strcmp(input_filename, yy_buffer->filename);
1124

1125 1126
						free(input_filename);
						input_filename = yy_buffer->filename;
1127

1128 1129
						yy_buffer = yy_buffer->next;
						free(yb);
1130

1131 1132 1133 1134 1135
						if (i != 0)
							output_line_number();
						
			  		}
				}
1136
<INITIAL>{other}|\n     	{ mmerror(PARSE_ERROR, ET_FATAL, "Internal error: unreachable state, please inform pgsql-bugs@postgresql.org"); }
1137 1138
%%
void
1139
lex_init(void)
1140
{
1141 1142 1143
	braces_open = 0;

	preproc_tos = 0;
M
Michael Meskes 已提交
1144
	yylineno = 1;
1145 1146 1147
	ifcond = TRUE;
	stacked_if_value[preproc_tos].condition = ifcond;
	stacked_if_value[preproc_tos].else_branch = FALSE;
1148 1149 1150 1151 1152 1153 1154 1155 1156

	/* initialize literal buffer to a reasonable but expansible size */
	if (literalbuf == NULL)
	{
		literalalloc = 128;
		literalbuf = (char *) malloc(literalalloc);
	}
	startlit();

1157
	BEGIN(C);
1158 1159
}

1160 1161 1162 1163 1164 1165
static void
addlit(char *ytext, int yleng)
{
	/* enlarge buffer if needed */
	if ((literallen+yleng) >= literalalloc)
	{
1166
		do 
1167
			literalalloc *= 2;
1168
		while ((literallen+yleng) >= literalalloc);
1169 1170
		literalbuf = (char *) realloc(literalbuf, literalalloc);
	}
1171 1172
	/* append new data, add trailing null */
	memcpy(literalbuf+literallen, ytext, yleng);
1173
	literallen += yleng;
1174
	literalbuf[literallen] = '\0';
1175 1176
}

1177 1178
static void
addlitchar(unsigned char ychar)
1179
{
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
	/* enlarge buffer if needed */
        if ((literallen+1) >= literalalloc)
        {
                literalalloc *= 2;
                literalbuf = (char *) realloc(literalbuf, literalalloc);
        }
	/* append new data, add trailing null */
	literalbuf[literallen] = ychar;
	literallen += 1;
	literalbuf[literallen] = '\0';
}
1191

1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
static void
parse_include(void)
{
	/* got the include file name */
	struct _yy_buffer *yb;
  	struct _include_path *ip;
  	char inc_file[MAXPGPATH];
  	unsigned int i;

  	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;

  	/*
	 * skip the ";" if there is one and trailing whitespace. Note that
	 * yytext contains at least one non-space character plus the ";" 
	 */
1214
  	for (i = strlen(yytext)-2;
1215
		 i > 0 && ecpg_isspace(yytext[i]);
1216 1217
		 i--)
		;
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227

	if (yytext[i] == ';')
		i--;

	yytext[i+1] = '\0';
	
	yyin = NULL;

	/* If file name is enclosed in '"' remove these and look only in '.' */
	/* Informix does look into all include paths though, except filename starts with '/' */
1228
	if (yytext[0] == '"' && yytext[i] == '"' &&
1229
	    ((compat != ECPG_COMPAT_INFORMIX && compat != ECPG_COMPAT_INFORMIX_SE) || yytext[1] == '/'))
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
	{
		yytext[i] = '\0';
		memmove(yytext, yytext+1, strlen(yytext));
	
		strncpy(inc_file, yytext, sizeof(inc_file));
		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");
			}
		}
		
	}
	else
	{
		if ((yytext[0] == '"' && yytext[i] == '"') || (yytext[0] == '<' && yytext[i] == '>'))
		{
			yytext[i] = '\0';
			memmove(yytext, yytext+1, strlen(yytext));
		}
		
	  	for (ip = include_paths; yyin == NULL && ip != NULL; ip = ip->next)
	  	{
			if (strlen(ip->path) + strlen(yytext) + 3 > MAXPGPATH)
			{
				fprintf(stderr, "Error: Path %s/%s is too long in line %d, skipping.\n", ip->path, yytext, yylineno);
				continue;
			}
			snprintf (inc_file, sizeof(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
Michael Meskes 已提交
1274
		mmerror(NO_INCLUDE_FILE, ET_FATAL, "Cannot open include file %s in line %d\n", yytext, yylineno);
1275 1276 1277 1278 1279 1280

	input_filename = mm_strdup(inc_file);
	yy_switch_to_buffer(yy_create_buffer(yyin,YY_BUF_SIZE ));
	yylineno = 1;
	output_line_number();

1281
  	BEGIN(C);
1282
}
1283 1284 1285 1286 1287 1288

static void
check_escape_warning(void)
{
	if (warn_on_first_escape && escape_string_warning)
	       	mmerror (PARSE_ERROR, ET_WARNING, "nonstandard use of escape in a string literal");
1289
	warn_on_first_escape = false;   /* warn only once per string */
1290
}
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305

/*
 * ecpg_isspace() --- return TRUE if flex scanner considers char whitespace
 */
static bool
ecpg_isspace(char ch)
{
	if (ch == ' ' ||
		ch == '\t' ||
		ch == '\n' ||
		ch == '\r' ||
		ch == '\f')
		return true;
	return false;
}
1306

1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
static bool isdefine(void)
{
	struct _defines *ptr;

	/* is it a define? */
	for (ptr = defines; ptr; ptr = ptr->next)
	{
		if (strcmp(yytext, ptr->old) == 0 && ptr->used == NULL)
		{
			struct _yy_buffer *yb;

			yb = mm_alloc(sizeof(struct _yy_buffer));

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

			ptr->used = yy_buffer = yb;

			yy_scan_string(ptr->new);
			return true;
		}
	}

	return false;
}
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374

/*
 * Called before any actual parsing is done
 */
void
scanner_init(const char *str)
{
	Size	slen = strlen(str);

	/*
	 * Might be left over after ereport()
	 */
	if (YY_CURRENT_BUFFER)
		yy_delete_buffer(YY_CURRENT_BUFFER);

	/*
	 * Make a scan buffer with special termination needed by flex.
	 */
	scanbuf = mm_alloc(slen + 2);
	memcpy(scanbuf, str, slen);
	scanbuf[slen] = scanbuf[slen + 1] = YY_END_OF_BUFFER_CHAR;
	scanbufhandle = yy_scan_buffer(scanbuf, slen + 2);

	/* initialize literal buffer to a reasonable but expansible size */
	literalalloc = 128;
	literalbuf = (char *) mm_alloc(literalalloc);
	startlit();

	BEGIN(INITIAL);
}


/*
 * Called after parsing is done to clean up after scanner_init()
 */
void
scanner_finish(void)
{
	yy_delete_buffer(scanbufhandle);
	free(scanbuf);
}