1
-
2
- /* This is a modified version of src/backend/parser/scan.l */
3
1
%{
2
+ /* -------------------------------------------------------------------------
3
+ *
4
+ * pgc.l
5
+ * lexical scanner for ecpg
6
+ *
7
+ * This is a modified version of src/backend/parser/scan.l
8
+ *
9
+ *
10
+ * Copyright (c) 1994, Regents of the University of California
11
+ *
12
+ *
13
+ * IDENTIFICATION
14
+ * $Header: /cvsroot/pgsql/src/interfaces/ecpg/preproc/pgc.l,v 1.45 1999/10/22 23:14:50 tgl Exp $
15
+ *
16
+ *-------------------------------------------------------------------------
17
+ */
4
18
#include < ctype.h>
5
19
#include < sys/types.h>
6
20
#include < limits.h>
7
21
#include < errno.h>
8
22
9
23
#include " postgres.h"
24
+
10
25
#ifndef PATH_MAX
11
26
#include < sys/param.h>
12
27
#define PATH_MAX MAXPATHLEN
13
28
#endif
29
+
14
30
#include " miscadmin.h"
15
- #include " nodes/pg_list.h"
16
31
#include " nodes/parsenodes.h"
32
+ #include " nodes/pg_list.h"
17
33
#include " parser/gramparse.h"
18
34
#include " parser/scansup.h"
19
35
#include " extern.h"
20
36
#include " preproc.h"
21
37
#include " utils/builtins.h"
22
38
23
- #ifdef YY_READ_BUF_SIZE
24
- #undef YY_READ_BUF_SIZE
25
- #endif
26
- #define YY_READ_BUF_SIZE MAX_PARSE_BUFFER
27
-
28
39
/* some versions of lex define this as a macro */
29
40
#if defined(yywrap)
30
41
#undef yywrap
31
42
#endif /* yywrap */
32
43
33
44
extern YYSTYPE yylval;
34
- int llen;
35
- char literal[MAX_PARSE_BUFFER];
45
+
46
+ /*
47
+ * literalbuf is used to accumulate literal values when multiple rules
48
+ * are needed to parse a single literal. Call startlit to reset buffer
49
+ * to empty, addlit to add text. Note that the buffer is permanently
50
+ * malloc'd to the largest size needed so far in the current run.
51
+ */
52
+ static char *literalbuf = NULL ; /* expandable buffer */
53
+ static int literallen; /* actual current length */
54
+ static int literalalloc; /* current allocated buffer size */
55
+
56
+ #define startlit () (literalbuf[0 ] = ' \0 ' , literallen = 0 )
57
+ static void addlit (char *ytext, int yleng);
58
+
36
59
int before_comment;
37
60
38
61
struct _yy_buffer { YY_BUFFER_STATE buffer;
@@ -142,16 +165,14 @@ self [,()\[\].;$\:\+\-\*\/\%\^\<\>\=\|]
142
165
op_and_self [\~\!\@\#\^\&\|\`\?\$\:\+\-\*\/\%\<\>\= ]
143
166
operator {op_and_self }+
144
167
145
- /* we do not allow unary minus in numbers.
146
- * instead we pass it verbatim to parser. there it gets
147
- * coerced via doNegate() -- Leon aug 20 1999
168
+ /* we no longer allow unary minus in numbers.
169
+ * instead we pass it separately to parser. there it gets
170
+ * coerced via doNegate() -- Leon aug 20 1999
148
171
*/
172
+
149
173
integer {digit }+
150
174
decimal (({digit }* \. {digit }+ )| ({digit }+ \. {digit }* ))
151
- real ((({digit }* \. {digit }+ )| ({digit }+ \. {digit }* )| ({digit }+ ))([Ee ][-+ ]? {digit }+ ))
152
- /*
153
- real (((({digit}*\.{digit}+)|({digit}+\.{digit}*))([Ee][-+]?{digit}+)?)|({digit}+[Ee][-+]?{digit}+))
154
- */
175
+ real ((({digit }* \. {digit }+ )| ({digit }+ \. {digit }* )| ({digit }+ ))([Ee ][-+ ]? {digit }+ ))
155
176
156
177
param \$ {integer }
157
178
@@ -200,99 +221,82 @@ cppline {space}*#.*(\\{space}*\n)*\n*
200
221
201
222
<SQL >{xbstart } {
202
223
BEGIN (xb);
203
- llen = 0 ;
204
- *literal = ' \0 ' ;
224
+ startlit ();
205
225
}
206
226
<xb >{xbstop } {
207
227
char * endptr;
208
228
209
229
BEGIN (SQL);
210
230
errno = 0 ;
211
- yylval.ival = strtol (( char *)literal, &endptr,2 );
231
+ yylval.ival = strtol (literalbuf, &endptr, 2 );
212
232
if (*endptr != ' \0 ' || errno == ERANGE)
213
233
yyerror (" ERROR: Bad binary integer input!" );
214
234
return ICONST;
215
235
}
216
236
<xh >{xhinside } |
217
237
<xb >{xbinside } {
218
- if ((llen+yyleng) > (MAX_PARSE_BUFFER - 1 ))
219
- yyerror (" ERROR: quoted string parse buffer exceeded" );
220
- memcpy (literal+llen, yytext, yyleng+1 );
221
- llen += yyleng;
238
+ addlit (yytext, yyleng);
222
239
}
223
240
<xh >{xhcat } |
224
241
<xb >{xbcat } {
225
242
}
226
243
227
244
<SQL >{xhstart } {
228
245
BEGIN (xh);
229
- llen = 0 ;
230
- *literal = ' \0 ' ;
246
+ startlit ();
231
247
}
232
248
<xh >{xhstop } {
233
249
char * endptr;
234
250
235
251
BEGIN (SQL);
236
252
errno = 0 ;
237
- yylval.ival = strtol (( char *)literal, &endptr,16 );
253
+ yylval.ival = strtol (literalbuf, &endptr, 16 );
238
254
if (*endptr != ' \0 ' || errno == ERANGE)
239
255
yyerror (" ERROR: Bad hexadecimal integer input" );
240
256
return ICONST;
241
257
}
242
258
243
259
<SQL >{xqstart } {
244
260
BEGIN (xq);
245
- llen = 0 ;
246
- *literal = ' \0 ' ;
261
+ startlit ();
247
262
}
248
263
<xq >{xqstop } {
249
264
BEGIN (SQL);
250
- yylval.str = mm_strdup (literal );
265
+ yylval.str = mm_strdup (literalbuf );
251
266
return SCONST;
252
267
}
253
268
<xq >{xqdouble } |
254
- <xq >{xqinside } |
269
+ <xq >{xqinside } |
255
270
<xq >{xqliteral } {
256
- if ((llen+yyleng) > (MAX_PARSE_BUFFER - 1 ))
257
- yyerror (" ERROR: quoted string parse buffer exceeded" );
258
- memcpy (literal+llen, yytext, yyleng+1 );
259
- llen += yyleng;
271
+ addlit (yytext, yyleng);
260
272
}
261
273
<xq >{xqcat } {
262
274
}
263
275
264
276
265
277
<SQL >{xdstart } {
266
278
BEGIN (xd);
267
- llen = 0 ;
268
- *literal = ' \0 ' ;
279
+ startlit ();
269
280
}
270
281
<xd >{xdstop } {
271
282
BEGIN (SQL);
272
- yylval.str = mm_strdup (literal );
283
+ yylval.str = mm_strdup (literalbuf );
273
284
return CSTRING;
274
285
}
275
286
<xd >{xdinside } {
276
- if ((llen+yyleng) > (MAX_PARSE_BUFFER - 1 ))
277
- yyerror (" ERROR: quoted string parse buffer exceeded" );
278
- memcpy (literal+llen, yytext, yyleng+1 );
279
- llen += yyleng;
287
+ addlit (yytext, yyleng);
280
288
}
281
289
{xdstart } {
282
290
BEGIN (xdc);
283
- llen = 0 ;
284
- *literal = ' \0 ' ;
291
+ startlit ();
285
292
}
286
293
<xdc >{xdstop } {
287
294
BEGIN (C);
288
- yylval.str = mm_strdup (literal );
295
+ yylval.str = mm_strdup (literalbuf );
289
296
return CSTRING;
290
297
}
291
298
<xdc >{xdcinside } {
292
- if ((llen+yyleng) > (MAX_PARSE_BUFFER - 1 ))
293
- yyerror (" ERROR: quoted string parse buffer exceeded" );
294
- memcpy (literal+llen, yytext, yyleng+1 );
295
- llen += yyleng;
299
+ addlit (yytext, yyleng);
296
300
}
297
301
<SQL >{typecast } { return TYPECAST; }
298
302
<SQL >{self } { /*
@@ -324,24 +328,24 @@ cppline {space}*#.*(\\{space}*\n)*\n*
324
328
{
325
329
errno = 0 ;
326
330
yylval.str = mm_strdup ((char *)yytext);
327
- return SCONST;
331
+ return SCONST;
328
332
}
329
333
return ICONST;
330
334
}
331
- {decimal } {
332
- char * endptr;
335
+ {decimal } {
336
+ char * endptr;
333
337
334
- if (strlen ((char *)yytext) <= 17 )
335
- {
336
- errno = 0 ;
337
- yylval.dval = strtod ((char *)yytext,&endptr);
338
+ if (strlen ((char *)yytext) <= 17 )
339
+ {
340
+ errno = 0 ;
341
+ yylval.dval = strtod ((char *)yytext,&endptr);
338
342
if (*endptr != ' \0 ' || errno == ERANGE)
339
343
yyerror (" ERROR: Bad float8 input" );
340
344
return FCONST;
341
- }
342
- yylval.str = mm_strdup ((char *)yytext);
343
- return SCONST;
344
- }
345
+ }
346
+ yylval.str = mm_strdup ((char *)yytext);
347
+ return SCONST;
348
+ }
345
349
<C ,SQL >{real } {
346
350
char * endptr;
347
351
@@ -420,7 +424,7 @@ cppline {space}*#.*(\\{space}*\n)*\n*
420
424
{
421
425
errno = 0 ;
422
426
yylval.str = mm_strdup ((char *)yytext);
423
- return SCONST;
427
+ return SCONST;
424
428
}
425
429
return ICONST;
426
430
}
@@ -486,8 +490,7 @@ cppline {space}*#.*(\\{space}*\n)*\n*
486
490
<def_ident >{identifier } {
487
491
old = mm_strdup (yytext);
488
492
BEGIN (def);
489
- llen = 0 ;
490
- *literal = ' \0 ' ;
493
+ startlit ();
491
494
}
492
495
<def >{space } /* eat the whitespace */
493
496
<def >" ;" {
@@ -498,8 +501,8 @@ cppline {space}*#.*(\\{space}*\n)*\n*
498
501
if (strcmp (old, ptr->old ) == 0 )
499
502
{
500
503
free (ptr->new );
501
- /* ptr->new = mm_strdup(scanstr(literal ));*/
502
- ptr->new = mm_strdup (literal );
504
+ /* ptr->new = mm_strdup(scanstr(literalbuf ));*/
505
+ ptr->new = mm_strdup (literalbuf );
503
506
}
504
507
}
505
508
if (ptr == NULL )
@@ -508,19 +511,16 @@ cppline {space}*#.*(\\{space}*\n)*\n*
508
511
509
512
/* initial definition */
510
513
this ->old = old;
511
- /* this->new = mm_strdup(scanstr(literal ));*/
512
- this ->new = mm_strdup (literal );
514
+ /* this->new = mm_strdup(scanstr(literalbuf ));*/
515
+ this ->new = mm_strdup (literalbuf );
513
516
this ->next = defines;
514
517
defines = this ;
515
518
}
516
519
517
520
BEGIN (C);
518
521
}
519
522
<def >[^ ";" ] {
520
- if ((llen+yyleng) > (MAX_PARSE_BUFFER - 1 ))
521
- yyerror (" ERROR: define statement parse buffer exceeded" );
522
- memcpy (literal+llen, yytext, yyleng+1 );
523
- llen += yyleng;
523
+ addlit (yytext, yyleng);
524
524
}
525
525
<C >{exec }{space }{sql }{space }{include } { BEGIN (incl); }
526
526
<incl >{space } /* eat the whitespace */
@@ -602,9 +602,34 @@ void
602
602
lex_init (void )
603
603
{
604
604
braces_open = 0 ;
605
+
606
+ /* initialize literal buffer to a reasonable but expansible size */
607
+ if (literalbuf == NULL )
608
+ {
609
+ literalalloc = 128 ;
610
+ literalbuf = (char *) malloc (literalalloc);
611
+ }
612
+ startlit ();
613
+
605
614
BEGIN C;
606
615
}
607
616
617
+ static void
618
+ addlit (char *ytext, int yleng)
619
+ {
620
+ /* enlarge buffer if needed */
621
+ if ((literallen+yleng) >= literalalloc)
622
+ {
623
+ do {
624
+ literalalloc *= 2 ;
625
+ } while ((literallen+yleng) >= literalalloc);
626
+ literalbuf = (char *) realloc (literalbuf, literalalloc);
627
+ }
628
+ /* append data --- note we assume ytext is null-terminated */
629
+ memcpy (literalbuf+literallen, ytext, yleng+1 );
630
+ literallen += yleng;
631
+ }
632
+
608
633
int yywrap (void )
609
634
{
610
635
return 1 ;
0 commit comments