File: pgsScanner.ll

package info (click to toggle)
pgadmin3 1.14.2-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 69,368 kB
  • sloc: cpp: 163,662; sh: 4,339; ansic: 1,636; pascal: 1,120; yacc: 927; makefile: 733; lex: 421; perl: 40
file content (421 lines) | stat: -rw-r--r-- 13,483 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
80
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
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
411
412
413
414
415
416
417
418
419
420
421
L	[a-zA-Z_@#]
D	[0-9]
E	[e-eE-E][+-]?{D}+

%{ /*** C/C++ Declarations ***/

//////////////////////////////////////////////////////////////////////////
//
// pgScript - PostgreSQL Tools
// 
// Copyright (C) 2002 - 2012, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////////////////

#include "pgscript/pgScript.h"
#include "pgscript/parser.tab.hh"
#include "pgscript/utilities/pgsScanner.h"

/* Import the parser's token type into a local typedef */
typedef pgscript::pgsParser::token token;
typedef pgscript::pgsParser::token_type token_type;

/* Work around an incompatibility in flex (at least versions 2.5.31 through
 * 2.5.33): it generates code that does not conform to C89.  See Debian bug
 * 333231 <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>.  */
#undef	yywrap
#define	yywrap()	1

/* By default yylex returns int, we use token_type. Unfortunately yyterminate
 * by default returns 0, which is not of token_type. */
#define yyterminate() return token::PGS_END

/* This disables inclusion of unistd.h, which is not available under Visual C++
 * on Win32. The C++ scanner uses STL streams instead. */
#define YY_NO_UNISTD_H

%}

/*** Flex Declarations and Options ***/

/* Enable C++ scanner class generation */
%option c++

/* Change the name of the scanner class. Results in "pgsFlexLexer" */
%option prefix="pgs"
	
/* Case insensitive */
%option case-insensitive

/* The manual says "somewhat more optimized" */
%option batch

/* Prevent isatty warning in VC++ */
%option never-interactive

/* For using start conditions */
%option stack

/* No support for include files is planned */
%option noyywrap

/* The following paragraph suffices to track locations accurately. Each time
 * yylex is invoked, the begin position is moved onto the end position. */
%{
#define YY_USER_ACTION  yylloc->columns(yyleng);
%}

%x SC_COMMENT
%x SC_QUERY
%x SC_DOLLAR
%x SC_STRING

%% /*** Regular Expressions Part ***/

 /* Code to place at the beginning of yylex() */
%{
	// Reset location
	yylloc->step();
%}

<INITIAL,SC_QUERY>"--".*$	{ /* Ignore SQL comment */ }
<INITIAL>"/*"		{ comment_caller = INITIAL; BEGIN(SC_COMMENT); }
<SC_QUERY>"/*"		{ comment_caller = SC_QUERY; BEGIN(SC_COMMENT); }

<INITIAL>{
"WHILE"				{ return token::PGS_WHILE; }
"BREAK"				{ return token::PGS_BREAK; }
"RETURN"			{ return token::PGS_RETURN; }
"CONTINUE"			{ return token::PGS_CONTINUE; }
"IF"				{ return token::PGS_IF; }
"ELSE"				{ return token::PGS_ELSE; }
"WAITFOR"			{ return token::PGS_WAITFOR; }
"AS"				{ return token::PGS_AS; }

"ASSERT"			{ return token::PGS_ASSERT; }
"GO"				{ /* Ignore it */ }
"PRINT"				{ return token::PGS_PRINT; }

"COLUMNS"			{ return token::PGS_CNT_COLUMNS; }
"LINES"				{ return token::PGS_CNT_LINES; }
"TRIM"				{ return token::PGS_TRIM; }
"RMLINE"			{ return token::PGS_RM_LINE; }
"CAST"				{ return token::PGS_CAST; }

"RECORD"			{ return token::PGS_RECORD; }

"INTEGER"			{ return token::PGS_INTEGER; }
"REAL"				{ return token::PGS_REAL; }
"STRING"			{ return token::PGS_STRING; }
"REGEX"				{ return token::PGS_REGEX; }
"FILE"				{ return token::PGS_FILE; }
"DATE"				{ return token::PGS_DATE; }
"TIME"				{ return token::PGS_TIME; }
"DATETIME"			{ return token::PGS_DATE_TIME; }
"REFERENCE"			{ return token::PGS_REFERENCE; }


"SET"[ \t]+"@"		{ unput('@'); yylloc->end.columns(-1);
					  return token::PGS_SET_ASSIGN; }
"DECLARE"[ \t]+"@"	{ unput('@'); yylloc->end.columns(-1);
					  return token::PGS_DECLARE_ASSGN; }

"BEGIN"				{ /* Block opening */ return token::PGS_OPEN; }
"END"				{ /* Block closing */ return token::PGS_CLOSE; }

"@"({L}|{D})*		{ yylval->str = pnew wxString(yytext, m_conv);
					  return token::PGS_IDENTIFIER; }

{D}+				{ yylval->str = pnew wxString(yytext, m_conv);
					  return token::PGS_VAL_INT; }

{D}*"."{D}+({E})?	{ yylval->str = pnew wxString(yytext, m_conv);
					  return token::PGS_VAL_REAL; }
{D}+{E}				{ yylval->str = pnew wxString(yytext, m_conv);
					  return token::PGS_VAL_REAL; }
{D}+"."{D}*({E})?	{ yylval->str = pnew wxString(yytext, m_conv);
					  return token::PGS_VAL_REAL; }

"AND"				{ return token::PGS_AND_OP; }
"OR"				{ return token::PGS_OR_OP; }
"<="				{ return token::PGS_LE_OP; }
">="				{ return token::PGS_GE_OP; }
"="					{ return token::PGS_EQ_OP; }
"~="				{ return token::PGS_AE_OP; }
"<>"				{ return token::PGS_NE_OP; }
";"					{ return wx_static_cast(token_type, ';'); }
("{"|"<%")			{ return wx_static_cast(token_type, '{'); }
("}"|"%>")			{ return wx_static_cast(token_type, '}'); }
":"					{ return wx_static_cast(token_type, ':'); }
"("					{ return wx_static_cast(token_type, '('); }
")"					{ return wx_static_cast(token_type, ')'); }
("["|"<:")			{ return wx_static_cast(token_type, '['); }
("]"|":>")			{ return wx_static_cast(token_type, ']'); }
"."					{ return wx_static_cast(token_type, '.'); }
","					{ return wx_static_cast(token_type, ','); }
"NOT"				{ return token::PGS_NOT_OP; }
"-"					{ return wx_static_cast(token_type, '-'); }
"+"					{ return wx_static_cast(token_type, '+'); }
"*"					{ return wx_static_cast(token_type, '*'); }
"/"					{ return wx_static_cast(token_type, '/'); }
"%"					{ return wx_static_cast(token_type, '%'); }
"<"					{ return wx_static_cast(token_type, '<'); }
">"					{ return wx_static_cast(token_type, '>'); }

"'"					{ string_caller = INITIAL; BEGIN(SC_STRING); }

"ABORT"				{ query += yytext; query_token = token::PGS_ABORT;
					  BEGIN(SC_QUERY); }
"ALTER"				{ query += yytext; query_token = token::PGS_ALTER;
					  BEGIN(SC_QUERY); }
"ANALYZE"			{ query += yytext; query_token = token::PGS_ANALYZE;
					  BEGIN(SC_QUERY); }
"BEGIN"[ \t]+"TRAN"	{ query += yytext; query_token = token::PGS_BEGIN;
					  BEGIN(SC_QUERY); }
"BEGIN"[ \t]+"WORK"	{ query += yytext; query_token = token::PGS_BEGIN;
					  BEGIN(SC_QUERY); }
"CHECKPOINT"		{ query += yytext; query_token = token::PGS_CHECKPOINT;
					  BEGIN(SC_QUERY); }
"CLOSE"				{ query += yytext; query_token = token::PGS_CLOSE_ST;
					  BEGIN(SC_QUERY); }
"CLUSTER"			{ query += yytext; query_token = token::PGS_CLUSTER;
					  BEGIN(SC_QUERY); }
"COMMENT"			{ query += yytext; query_token = token::PGS_COMMENT;
					  BEGIN(SC_QUERY); }
"COMMIT"			{ query += yytext; query_token = token::PGS_COMMIT;
					  BEGIN(SC_QUERY); }
"COPY"				{ query += yytext; query_token = token::PGS_COPY;
					  BEGIN(SC_QUERY); }
"CREATE"			{ query += yytext; query_token = token::PGS_CREATE;
					  BEGIN(SC_QUERY); }
"DEALLOCATE"		{ query += yytext; query_token = token::PGS_DEALLOCATE;
					  BEGIN(SC_QUERY); }
"DECLARE"			{ query += yytext; query_token = token::PGS_DECLARE;
					  BEGIN(SC_QUERY); }
"DELETE"			{ query += yytext; query_token = token::PGS_DELETE;
					  BEGIN(SC_QUERY); }
"DISCARD"			{ query += yytext; query_token = token::PGS_DISCARD;
					  BEGIN(SC_QUERY); }
"DROP"				{ query += yytext; query_token = token::PGS_DROP;
					  BEGIN(SC_QUERY); }
"END"[ \t]+"TRANS"	{ query += yytext; query_token = token::PGS_END_ST;
					  BEGIN(SC_QUERY); }
"END"[ \t]+"WORK"	{ query += yytext; query_token = token::PGS_END_ST;
					  BEGIN(SC_QUERY); }
"EXECUTE"			{ query += yytext; query_token = token::PGS_EXECUTE;
					  BEGIN(SC_QUERY); }
"EXPLAIN"			{ query += yytext; query_token = token::PGS_EXPLAIN;
					  BEGIN(SC_QUERY); }
"FETCH"				{ query += yytext; query_token = token::PGS_FETCH;
					  BEGIN(SC_QUERY); }
"GRANT"				{ query += yytext; query_token = token::PGS_GRANT;
					  BEGIN(SC_QUERY); }
"INSERT"			{ query += yytext; query_token = token::PGS_INSERT;
					  BEGIN(SC_QUERY); }
"LISTEN"			{ query += yytext; query_token = token::PGS_LISTEN;
					  BEGIN(SC_QUERY); }
"LOAD"				{ query += yytext; query_token = token::PGS_LOAD;
					  BEGIN(SC_QUERY); }
"LOCK"				{ query += yytext; query_token = token::PGS_LOCK;
					  BEGIN(SC_QUERY); }
"MOVE"				{ query += yytext; query_token = token::PGS_MOVE;
					  BEGIN(SC_QUERY); }
"NOTIFY"			{ query += yytext; query_token = token::PGS_NOTIFY;
					  BEGIN(SC_QUERY); }
"PREPARE"			{ query += yytext; query_token = token::PGS_PREPARE;
					  BEGIN(SC_QUERY); }
"REASSIGN"			{ query += yytext; query_token = token::PGS_REASSIGN;
					  BEGIN(SC_QUERY); }
"REINDEX"			{ query += yytext; query_token = token::PGS_REINDEX;
					  BEGIN(SC_QUERY); }
"RELEASE"			{ query += yytext; query_token = token::PGS_RELEASE;
					  BEGIN(SC_QUERY); }
"RESET"				{ query += yytext; query_token = token::PGS_RESET;
					  BEGIN(SC_QUERY); }
"REVOKE"			{ query += yytext; query_token = token::PGS_REVOKE;
					  BEGIN(SC_QUERY); }
"ROLLBACK"			{ query += yytext; query_token = token::PGS_ROLLBACK;
					  BEGIN(SC_QUERY); }
"SAVEPOINT"			{ query += yytext; query_token = token::PGS_SAVEPOINT;
					  BEGIN(SC_QUERY); }
"SELECT"			{ query += yytext; query_token = token::PGS_SELECT;
					  BEGIN(SC_QUERY); }
"SET"				{ query += yytext; query_token = token::PGS_SET;
					  BEGIN(SC_QUERY); }
"SHOW"				{ query += yytext; query_token = token::PGS_SHOW;
					  BEGIN(SC_QUERY); }
"START"				{ query += yytext; query_token = token::PGS_START;
					  BEGIN(SC_QUERY); }
"TRUNCATE"			{ query += yytext; query_token = token::PGS_TRUNCATE;
					  BEGIN(SC_QUERY); }
"UNLISTEN"			{ query += yytext; query_token = token::PGS_UNLISTEN;
					  BEGIN(SC_QUERY); }
"UPDATE"			{ query += yytext; query_token = token::PGS_UPDATE;
					  BEGIN(SC_QUERY); }
"VACUUM"			{ query += yytext; query_token = token::PGS_VACUUM;
					  BEGIN(SC_QUERY); }
"VALUES"			{ query += yytext; query_token = token::PGS_VALUES;
					  BEGIN(SC_QUERY); }

[ \t\v\f]+			{ }
\r					{ yylloc->step(); }
\n					{ yylloc->lines(yyleng); yylloc->step(); }
.					{ return token::PGS_UNKNOWN; }
}

<SC_QUERY>{
"'"					{ query += yytext; string_caller = SC_QUERY; BEGIN(SC_STRING); }
\$({L}|{D})*\$		{ query += yytext; dollar = yytext; BEGIN(SC_DOLLAR); }
";"					{ yylval->str = pnew wxString(query.c_str(), m_conv);
					  query.clear(); unput(';'); yylloc->end.columns(-1);
					  BEGIN(INITIAL); m_parent = 0; return query_token; }
"("					{ ++m_parent; query += yytext; }
")"					{
						--m_parent;
						if (m_parent == -1)
						{
							yylval->str = pnew wxString(query.c_str(), m_conv);
							query.clear(); unput(')'); yylloc->end.columns(-1);
							BEGIN(INITIAL); m_parent = 0; return query_token;
						}
						else
						{
							query += yytext;
						}
					}
\r					{ query += yytext; yylloc->step(); }
\n					{ query += yytext; yylloc->lines(yyleng); yylloc->step(); }
<<EOF>>				{ yylval->str = pnew wxString(query.c_str(), m_conv);
					  query.clear(); m_parent = 0; return query_token; }
.					{ yylloc->columns(columns(*yytext)); query += yytext; }
}

<SC_DOLLAR>{
\$({L}|{D})*\$		{ query += yytext;
					  if (std::string(yytext) == dollar) BEGIN(SC_QUERY); }
\r					{ query += yytext; yylloc->step(); }
\n					{ query += yytext; yylloc->lines(yyleng); yylloc->step(); }
<<EOF>>				{ query += yytext; yylval->str = pnew wxString(query.c_str(), m_conv);
					  query.clear(); m_parent = 0; return query_token; }
.					{ yylloc->columns(columns(*yytext)); query += yytext; }
}

<SC_COMMENT>{
"*/"				{ BEGIN(comment_caller); }
\r					{ yylloc->step(); }
\n					{ yylloc->lines(yyleng); yylloc->step(); }
.					{ yylloc->columns(columns(*yytext)); }
}

<SC_STRING>{
"''"				{
						if (string_caller == SC_QUERY)
							query += yytext;
						else
							str += "'";
					}
\\.					{
						if (string_caller == SC_QUERY)
							query += yytext;
						else
							str += *(yytext + 1);
						yylloc->columns(columns(*(yytext + 1)));
					}
"'"					{
						if (string_caller == SC_QUERY)
						{
							query += yytext;
							BEGIN(string_caller);
						}
						else
						{
							yylval->str = pnew wxString(str.c_str(), m_conv);
							str.clear();
							BEGIN(string_caller);
							return token::PGS_VAL_STR;
						}
					}
\r					{
						if (string_caller == SC_QUERY)
							query += yytext;
						else
							str += yytext;
						yylloc->step();
					}
\n					{
						if (string_caller == SC_QUERY)
							query += yytext;
						else
							str += yytext;
						yylloc->lines(yyleng); yylloc->step();
					}
<<EOF>>				{
						if (string_caller == SC_QUERY)
						{
							query += yytext; yylval->str = pnew wxString(query.c_str(), m_conv);
							query.clear(); m_parent = 0; return query_token;
						}
						else
						{
							yylval->str = pnew wxString(str.c_str(), m_conv);
							str.clear();
							BEGIN(string_caller);
							return token::PGS_VAL_STR;
						}
					}
.					{ 
						if (string_caller == SC_QUERY)
							query += yytext;
						else
							str += yytext;
						yylloc->columns(columns(*yytext));
					}
}

%% /*** Additional Code ***/

namespace pgscript
{

pgsScanner::pgsScanner(wxMBConv & conv, std::istream * in, std::ostream * out) :
	pgsFlexLexer(in, out), m_parent(0), m_conv(conv)
{

}

pgsScanner::~pgsScanner()
{

}

void pgsScanner::set_debug(bool b)
{
	yy_flex_debug = b;
}

int pgsScanner::columns(const char & c)
{
	if ((c & 0xF0) == 0xF0) // 4 bytes
		return -3;
	else if ((c & 0xE0) == 0xE0) // 3 bytes
		return -2;
	else if ((c & 0xC0) == 0xC0) // 2 bytes
		return -1;
	else return 0;
}

}

/* This implementation of pgsFlexLexer::yylex() is required to fill the
 * vtable of the class pgsFlexLexer. We define the scanner's main yylex
 * function via YY_DECL to reside in the pgsScanner class instead. */

#ifdef yylex
#undef yylex
#endif

int pgsFlexLexer::yylex()
{
	return 0;
}