pgc.l 12.9 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"
27 28
#include "type.h"
#include "y.tab.h"
M
Marc G. Fournier 已提交
29
#include "utils/builtins.h"
30

31
#include "extern.h"
32

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

int debugging = 0;
extern YYSTYPE yylval;
int llen;
char literal[MAX_PARSE_BUFFER];
42
int before_comment;
M
Marc G. Fournier 已提交
43

44 45 46 47 48 49
struct _yy_buffer { YY_BUFFER_STATE 	buffer;
		    long		lineno;
		    char	      * filename;
		    struct _yy_buffer * next;
		  } *yy_buffer = NULL;

50
%}
51
%option yylineno
52
%s C SQL incl
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
%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		[^\\']*
xqembedded		"\\'"
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		"::"

self			[,()\[\].$\:\+\-\*\/\<\>\=\|]
op_and_self		[\~\!\@\#\%\^\&\|\`\?\$\:\+\-\*\/\<\>\=]
operator		{op_and_self}+

xminteger              {integer}/-
xmreal                 {real}/{space}*-{digit}
xmstop			-

integer			-?{digit}+
real			-?{digit}+\.{digit}+([Ee][-+]?{digit}+)?

param			\${integer}

comment			("--"|"//").*\n

space			[ \t\n\f]
other			.

/* some stuff needed for ecpg */
157
ccomment	"//".*\n
M
Marc G. Fournier 已提交
158
exec    [eE][xX][eE][cC]
159
include [iI][nN][cC][lL][uU][dD][eE]
M
Marc G. Fournier 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
sql     [sS][qQ][lL]

/* 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
 */

175
%%
M
Marc G. Fournier 已提交
176 177
<SQL>{comment}		{ /* ignore */ }

178
{xcline}		{ /* ignore */ }
M
Marc G. Fournier 已提交
179

180 181 182 183 184 185 186 187
<xc>{xcstar}		{ /* ignore */ }
{xcstart}		{
				fprintf(stderr,"ys = %d %d\n", YYSTATE, before_comment);
				before_comment = YYSTATE;
	 			BEGIN(xc);
				fprintf(stderr,"ys = %d %d\n", YYSTATE,
before_comment);
			}
M
Marc G. Fournier 已提交
188

189
<xc>{xcstop}	{ fprintf(stderr,"ys = %d %d\n", YYSTATE, before_comment);BEGIN(before_comment); }
M
Marc G. Fournier 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

<xc>{xcinside}	{ /* ignore */ }

<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!");
					return (ICONST);
				}
<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");
					return (ICONST);
				}

<SQL>{xqstart}		{
					BEGIN(xq);
					llen = 0;
					*literal = '\0';
				}
<xq>{xqstop}	{
					BEGIN(SQL);
					yylval.str = strdup(scanstr(literal));
					return (SCONST);
				}
<xq>{xqdouble}	|
<xq>{xqinside}	{
					if ((llen+yyleng) > (MAX_PARSE_BUFFER - 1))
						yyerror("ERROR: quoted string parse buffer exceeded");
					memcpy(literal+llen, yytext, yyleng+1);
					llen += yyleng;
				}
<xq>{xqembedded} {
					if ((llen+yyleng-1) > (MAX_PARSE_BUFFER - 1))
						yyerror("ERROR: quoted string parse buffer exceeded");
					memcpy(literal+llen, yytext, yyleng+1);
					*(literal+llen) = '\'';
					llen += yyleng;
				}

<xq>{xqliteral} {
					if ((llen+yyleng-1) > (MAX_PARSE_BUFFER - 1))
						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);
					yylval.str = strdup(literal);
278
					return (CSTRING);
M
Marc G. Fournier 已提交
279 280 281 282 283 284 285
				}
<xd>{xdinside}	{
					if ((llen+yyleng) > (MAX_PARSE_BUFFER - 1))
						yyerror("ERROR: quoted string parse buffer exceeded");
					memcpy(literal+llen, yytext, yyleng+1);
					llen += yyleng;
				}
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
<C>{xdstart}		{
					BEGIN(xdc);
					llen = 0;
					*literal = '\0';
				}
<xdc>{xdstop}	{
					BEGIN(C);
					yylval.str = strdup(literal);
					return (CSTRING);
				}
<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 已提交
302 303 304 305 306 307 308 309 310 311 312 313 314

<xm>{space}*	{ /* ignore */ }
<xm>{xmstop}	{
					BEGIN(SQL);
					return (yytext[0]);
				}


<SQL>{typecast}			{ 	return TYPECAST; }

<SQL>{self}/-[\.0-9]		{
					return (yytext[0]);
				}
315
<SQL>{self}				{ 	return (yytext[0]); }
M
Marc G. Fournier 已提交
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
<SQL>{operator}/-[\.0-9]	{
					yylval.str = strdup((char*)yytext);
					return (Op);
				}
<SQL>{operator}		{
					if (strcmp((char*)yytext,"!=") == 0)
						yylval.str = strdup("<>"); /* compatability */
					else
						yylval.str = strdup((char*)yytext);
					return (Op);
				}
<SQL>{param}			{
					yylval.ival = atoi((char*)&yytext[1]);
					return (PARAM);
				}

<SQL>{identifier}/{space}*-{number}	{
					int i;
					ScanKeyword		*keyword;

					BEGIN(xm);
					for(i = 0; yytext[i]; i++)
						if (isupper(yytext[i]))
							yytext[i] = tolower(yytext[i]);

					keyword = ScanKeywordLookup((char*)yytext);
					if (keyword != NULL) {
						return (keyword->value);
					}
					else
					{
						keyword = ScanECPGKeywordLookup((char*)yytext);
						if (keyword != NULL) {
							return (keyword->value);
						}
						else
						{
							yylval.str = strdup((char*)yytext);
							return (IDENT);
						}
					}
				}
{integer}/{space}*-{number}	{
					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");
						return (FCONST);
					}
					return (ICONST);
				}
{real}/{space}*-{number} {
					char* endptr;

					BEGIN(xm);
					errno = 0;
					yylval.dval = strtod(((char *)yytext),&endptr);
					if (*endptr != '\0' || errno == ERANGE)
						yyerror("ERROR: Bad float8 input");
					return (FCONST);
				}
{integer}		{
					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");
						return (FCONST);
					}
					return (ICONST);
				}
{real}			{
					char* endptr;

					errno = 0;
					yylval.dval = strtod((char *)yytext,&endptr);
					if (*endptr != '\0' || errno == ERANGE)
						yyerror("ERROR: Bad float input");
					return (FCONST);
				}

411
<SQL>:{identifier}(("->"|\.){identifier})*	{
412 413 414
					yylval.str = strdup((char*)yytext+1);
					return(CVARIABLE);
			}
M
Marc G. Fournier 已提交
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
<SQL>{identifier}	{
					int i;
					ScanKeyword		*keyword;

					for(i = 0; yytext[i]; i++)
						if (isupper(yytext[i]))
							yytext[i] = tolower(yytext[i]);

					keyword = ScanKeywordLookup((char*)yytext);
					if (keyword != NULL) {
						return (keyword->value);
					}
					else
					{
						keyword = ScanECPGKeywordLookup((char*)yytext);
						if (keyword != NULL) {
							return (keyword->value);
						}
						else
						{
							yylval.str = strdup((char*)yytext);
							return (IDENT);
						}
					}
				}
<SQL>{space}			{ /* ignore */ }
<SQL>";"	                { BEGIN C; return SQL_SEMI; }
<SQL>{other}			{ return (yytext[0]); }

<C>{exec}{space}{sql}		{ BEGIN SQL; return SQL_START; }
445
<C>{ccomment}			{ /* ignore */ } 
M
Marc G. Fournier 已提交
446 447 448 449 450 451 452 453 454 455 456 457 458 459
<C>{identifier}	{
					ScanKeyword		*keyword;

					keyword = ScanCKeywordLookup((char*)yytext);
					if (keyword != NULL) {
						return (keyword->value);
					}
					else
					{
						yylval.str = strdup((char*)yytext);
						return (IDENT);
					}
				}
<C>";"	      	        { return(';'); }
460 461
<C>","	      	        { return(','); }
<C>"*"	      	        { return('*'); }
M
Marc G. Fournier 已提交
462
<C>{space}		{ ECHO; }
463 464 465 466 467
<C>\{			{ return('{'); }
<C>\}			{ return('}'); }
<C>\[			{ return('['); }
<C>\]			{ return(']'); }
<C>\=			{ return('='); }
M
Marc G. Fournier 已提交
468 469 470
<C>{other}			{ return (S_ANYTHING); }
<C>{exec}{space}{sql}{space}{include}	{ BEGIN(incl); }
<incl>{space}		/* eat the whitespace */
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
<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 已提交
493
					fprintf(stderr, "Error: Path %s/%s is too long in line %d, skipping.\n", ip->path, yytext, yylineno);
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
					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 已提交
510
				fprintf(stderr, "Error: Cannot open include file %s in line %d\n", yytext, yylineno);
511
				exit(NO_INCLUDE_FILE); 
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
			  }

			  input_filename = strdup(inc_file);
			  yy_switch_to_buffer(yy_create_buffer(yyin,YY_BUF_SIZE ));
			  yylineno = 0;

			  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);
			  }
			}

543 544
%%
void
545
lex_init(void)
546
{
547
    braces_open = 0;
548 549 550
    BEGIN C;
}

551
int yywrap(void) 
552 553 554
{ 
    return 1;
}