Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1aa58d3

Browse files
committed
Tweak the core scanner so that it can be used by plpgsql too.
Changes:Pass in the keyword lookup array instead of having it be hardwired.(This incidentally allows elimination of some duplicate coding in ecpg.)Re-order the token declarations in gram.y so that non-keyword tokens havenumbers that won't change when keywords are added or removed.Add ".." and ":=" to the set of tokens recognized by scan.l. (Since thesecombinations are nowhere legal in core SQL, this does not change anythingexcept the precise wording of the error you get when you write this.)
1 parent0d4899e commit1aa58d3

File tree

15 files changed

+139
-116
lines changed

15 files changed

+139
-116
lines changed

‎src/backend/parser/gram.y

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.668 2009/07/13 02:02:20 tgl Exp $
14+
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.669 2009/07/14 20:24:10 tgl Exp $
1515
*
1616
* HISTORY
1717
* AUTHORDATEMAJOR EVENT
@@ -421,10 +421,23 @@ static TypeName *TableFuncTypeName(List *columns);
421421

422422

423423
/*
424-
* If you make any token changes, update the keyword table in
425-
* src/include/parser/kwlist.h and add new keywords to the appropriate one of
426-
* the reserved-or-not-so-reserved keyword lists, below; search
427-
* this file for "Name classification hierarchy".
424+
* Non-keyword token types. These are hard-wired into the "flex" lexer.
425+
* They must be listed first so that their numeric codes do not depend on
426+
* the set of keywords. PL/pgsql depends on this so that it can share the
427+
* same lexer. If you add/change tokens here, fix PL/pgsql to match!
428+
*
429+
* DOT_DOT and COLON_EQUALS are unused in the core SQL grammar, and so will
430+
* always provoke parse errors. They are needed by PL/pgsql.
431+
*/
432+
%token<str>IDENTFCONSTSCONSTBCONSTXCONSTOp
433+
%token<ival>ICONSTPARAM
434+
%tokenTYPECASTDOT_DOTCOLON_EQUALS
435+
436+
/*
437+
* If you want to make any keyword changes, update the keyword table in
438+
* src/include/parser/kwlist.h and add new keywords to the appropriate one
439+
* of the reserved-or-not-so-reserved keyword lists, below; search
440+
* this file for "Keyword category lists".
428441
*/
429442

430443
/* ordinary key words in alphabetical order*/
@@ -515,17 +528,15 @@ static TypeName *TableFuncTypeName(List *columns);
515528

516529
ZONE
517530

518-
/* The grammar thinks these are keywords, but they are not in the kwlist.h
531+
/*
532+
* The grammar thinks these are keywords, but they are not in the kwlist.h
519533
* list and so can never be entered directly. The filter in parser.c
520534
* creates these tokens when required.
521535
*/
522536
%tokenNULLS_FIRSTNULLS_LASTWITH_TIME
523537

524-
/* Special token types, not actually keywords - see the "lex" file*/
525-
%token<str>IDENTFCONSTSCONSTBCONSTXCONSTOp
526-
%token<ival>ICONSTPARAM
527538

528-
/*precedence: lowest to highest*/
539+
/*Precedence: lowest to highest*/
529540
%nonassocSET/* see relation_expr_opt_alias*/
530541
%leftUNIONEXCEPT
531542
%leftINTERSECT

‎src/backend/parser/keywords.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/parser/keywords.c,v 1.213 2009/07/12 17:12:33 tgl Exp $
12+
* $PostgreSQL: pgsql/src/backend/parser/keywords.c,v 1.214 2009/07/14 20:24:10 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
1616
#include"postgres.h"
1717

1818
#include"parser/gramparse.h"
19-
#include"parser/keywords.h"
2019

2120
#definePG_KEYWORD(a,b,c) {a,b,c},
2221

@@ -25,5 +24,4 @@ const ScanKeyword ScanKeywords[] = {
2524
#include"parser/kwlist.h"
2625
};
2726

28-
/* End of ScanKeywords, for use in kwlookup.c and elsewhere */
29-
constScanKeyword*LastScanKeyword=endof(ScanKeywords);
27+
constintNumScanKeywords=lengthof(ScanKeywords);

‎src/backend/parser/kwlookup.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@
66
* NB - this file is also used by ECPG and several frontend programs in
77
* src/bin/ including pg_dump and psql
88
*
9-
* Note that this file expects that the ScanKeywords array is defined
10-
* and that LastScanKeyword points to its element one past the last.
11-
*
129
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
1310
* Portions Copyright (c) 1994, Regents of the University of California
1411
*
1512
*
1613
* IDENTIFICATION
17-
* $PostgreSQL: pgsql/src/backend/parser/kwlookup.c,v 2.2 2009/03/08 16:53:30 alvherre Exp $
14+
* $PostgreSQL: pgsql/src/backend/parser/kwlookup.c,v 2.3 2009/07/14 20:24:10 tgl Exp $
1815
*
1916
*-------------------------------------------------------------------------
2017
*/
@@ -39,7 +36,9 @@
3936
* receive a different case-normalization mapping.
4037
*/
4138
constScanKeyword*
42-
ScanKeywordLookup(constchar*text)
39+
ScanKeywordLookup(constchar*text,
40+
constScanKeyword*keywords,
41+
intnum_keywords)
4342
{
4443
intlen,
4544
i;
@@ -69,8 +68,8 @@ ScanKeywordLookup(const char *text)
6968
/*
7069
* Now do a binary search using plain strcmp() comparison.
7170
*/
72-
low=&ScanKeywords[0];
73-
high=LastScanKeyword-1;
71+
low=keywords;
72+
high=keywords+ (num_keywords-1);
7473
while (low <=high)
7574
{
7675
constScanKeyword*middle;

‎src/backend/parser/parser.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* Portions Copyright (c) 1994, Regents of the University of California
1515
*
1616
* IDENTIFICATION
17-
* $PostgreSQL: pgsql/src/backend/parser/parser.c,v 1.80 2009/07/13 02:02:20 tgl Exp $
17+
* $PostgreSQL: pgsql/src/backend/parser/parser.c,v 1.81 2009/07/14 20:24:10 tgl Exp $
1818
*
1919
*-------------------------------------------------------------------------
2020
*/
@@ -39,7 +39,7 @@ raw_parser(const char *str)
3939
intyyresult;
4040

4141
/* initialize the flex scanner */
42-
yyscanner=scanner_init(str,&yyextra);
42+
yyscanner=scanner_init(str,&yyextra,ScanKeywords,NumScanKeywords);
4343

4444
/* filtered_base_yylex() only needs this much initialization */
4545
yyextra.have_lookahead= false;
@@ -79,7 +79,7 @@ pg_parse_string_token(const char *token)
7979
YYSTYPEyylval;
8080
YYLTYPEyylloc;
8181

82-
yyscanner=scanner_init(token,&yyextra);
82+
yyscanner=scanner_init(token,&yyextra,ScanKeywords,NumScanKeywords);
8383

8484
ctoken=base_yylex(&yylval,&yylloc,yyscanner);
8585

‎src/backend/parser/scan.l

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* Portions Copyright (c) 1994, Regents of the University of California
2525
*
2626
* IDENTIFICATION
27-
* $PostgreSQL: pgsql/src/backend/parser/scan.l,v 1.156 2009/07/13 03:11:12 tgl Exp $
27+
* $PostgreSQL: pgsql/src/backend/parser/scan.l,v 1.157 2009/07/14 20:24:10 tgl Exp $
2828
*
2929
*-------------------------------------------------------------------------
3030
*/
@@ -304,6 +304,10 @@ identifier{ident_start}{ident_cont}*
304304

305305
typecast"::"
306306

307+
/* these two token types are used by PL/pgsql, though not in core SQL */
308+
dot_dot\.\.
309+
colon_equals":="
310+
307311
/*
308312
* "self" is the set of chars that should be returned as single-character
309313
* tokens. "op_chars" is the set of chars that can make up "Op" tokens,
@@ -450,11 +454,21 @@ other.
450454

451455
SET_YYLLOC();
452456
yyless(1);/* eat only 'n' this time */
453-
/* nchar had better be a keyword! */
454-
keyword =ScanKeywordLookup("nchar");
455-
Assert(keyword !=NULL);
456-
yylval->keyword = keyword->name;
457-
return keyword->value;
457+
458+
keyword =ScanKeywordLookup("nchar",
459+
yyextra->keywords,
460+
yyextra->num_keywords);
461+
if (keyword !=NULL)
462+
{
463+
yylval->keyword = keyword->name;
464+
return keyword->value;
465+
}
466+
else
467+
{
468+
/* If NCHAR isn't a keyword, just return "n" */
469+
yylval->str =pstrdup("n");
470+
return IDENT;
471+
}
458472
}
459473

460474
{xqstart}{
@@ -680,6 +694,16 @@ other.
680694
return TYPECAST;
681695
}
682696

697+
{dot_dot}{
698+
SET_YYLLOC();
699+
return DOT_DOT;
700+
}
701+
702+
{colon_equals}{
703+
SET_YYLLOC();
704+
return COLON_EQUALS;
705+
}
706+
683707
{self}{
684708
SET_YYLLOC();
685709
return yytext[0];
@@ -830,7 +854,9 @@ other.
830854
SET_YYLLOC();
831855

832856
/* Is it a keyword? */
833-
keyword =ScanKeywordLookup(yytext);
857+
keyword =ScanKeywordLookup(yytext,
858+
yyextra->keywords,
859+
yyextra->num_keywords);
834860
if (keyword !=NULL)
835861
{
836862
yylval->keyword = keyword->name;
@@ -939,7 +965,10 @@ scanner_yyerror(const char *message, base_yyscan_t yyscanner)
939965
* Called before any actual parsing is done
940966
*/
941967
base_yyscan_t
942-
scanner_init(constchar *str, base_yy_extra_type *yyext)
968+
scanner_init(constchar *str,
969+
base_yy_extra_type *yyext,
970+
const ScanKeyword *keywords,
971+
int num_keywords)
943972
{
944973
Sizeslen =strlen(str);
945974
yyscan_tscanner;
@@ -949,6 +978,9 @@ scanner_init(const char *str, base_yy_extra_type *yyext)
949978

950979
base_yyset_extra(yyext, scanner);
951980

981+
yyext->keywords = keywords;
982+
yyext->num_keywords = num_keywords;
983+
952984
/*
953985
* Make a scan buffer with special termination needed by flex.
954986
*/

‎src/backend/utils/adt/misc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/misc.c,v 1.71 2009/06/11 14:49:03 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/misc.c,v 1.72 2009/07/14 20:24:10 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -334,7 +334,7 @@ pg_get_keywords(PG_FUNCTION_ARGS)
334334

335335
funcctx=SRF_PERCALL_SETUP();
336336

337-
if (&ScanKeywords[funcctx->call_cntr]<LastScanKeyword)
337+
if (funcctx->call_cntr<NumScanKeywords)
338338
{
339339
char*values[3];
340340
HeapTupletuple;

‎src/backend/utils/adt/ruleutils.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.301 2009/07/12 17:12:34 tgl Exp $
12+
* $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.302 2009/07/14 20:24:10 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -6219,7 +6219,9 @@ quote_identifier(const char *ident)
62196219
* Note: ScanKeywordLookup() does case-insensitive comparison, but
62206220
* that's fine, since we already know we have all-lower-case.
62216221
*/
6222-
constScanKeyword*keyword=ScanKeywordLookup(ident);
6222+
constScanKeyword*keyword=ScanKeywordLookup(ident,
6223+
ScanKeywords,
6224+
NumScanKeywords);
62236225

62246226
if (keyword!=NULL&&keyword->category!=UNRESERVED_KEYWORD)
62256227
safe= false;

‎src/bin/pg_dump/dumputils.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
99
* Portions Copyright (c) 1994, Regents of the University of California
1010
*
11-
* $PostgreSQL: pgsql/src/bin/pg_dump/dumputils.c,v 1.46 2009/06/11 14:49:07 momjian Exp $
11+
* $PostgreSQL: pgsql/src/bin/pg_dump/dumputils.c,v 1.47 2009/07/14 20:24:10 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -130,7 +130,9 @@ fmtId(const char *rawid)
130130
* Note: ScanKeywordLookup() does case-insensitive comparison, but
131131
* that's fine, since we already know we have all-lower-case.
132132
*/
133-
constScanKeyword*keyword=ScanKeywordLookup(rawid);
133+
constScanKeyword*keyword=ScanKeywordLookup(rawid,
134+
ScanKeywords,
135+
NumScanKeywords);
134136

135137
if (keyword!=NULL&&keyword->category!=UNRESERVED_KEYWORD)
136138
need_quotes= true;

‎src/bin/pg_dump/keywords.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/bin/pg_dump/keywords.c,v 1.3 2009/06/11 14:49:07 momjian Exp $
12+
* $PostgreSQL: pgsql/src/bin/pg_dump/keywords.c,v 1.4 2009/07/14 20:24:10 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -27,5 +27,4 @@ const ScanKeyword ScanKeywords[] = {
2727
#include"parser/kwlist.h"
2828
};
2929

30-
/* End of ScanKeywords, for use in kwlookup.c */
31-
constScanKeyword*LastScanKeyword=endof(ScanKeywords);
30+
constintNumScanKeywords=lengthof(ScanKeywords);

‎src/include/parser/gramparse.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
1212
* Portions Copyright (c) 1994, Regents of the University of California
1313
*
14-
* $PostgreSQL: pgsql/src/include/parser/gramparse.h,v 1.46 2009/07/13 02:02:20 tgl Exp $
14+
* $PostgreSQL: pgsql/src/include/parser/gramparse.h,v 1.47 2009/07/14 20:24:10 tgl Exp $
1515
*
1616
*-------------------------------------------------------------------------
1717
*/
@@ -20,6 +20,7 @@
2020
#defineGRAMPARSE_H
2121

2222
#include"nodes/parsenodes.h"
23+
#include"parser/keywords.h"
2324

2425
/*
2526
* We track token locations in terms of byte offsets from the start of the
@@ -49,6 +50,12 @@ typedef struct base_yy_extra_type
4950
char*scanbuf;
5051
Sizescanbuflen;
5152

53+
/*
54+
* The keyword list to use.
55+
*/
56+
constScanKeyword*keywords;
57+
intnum_keywords;
58+
5259
/*
5360
* literalbuf is used to accumulate literal values when multiple rules
5461
* are needed to parse a single literal. Call startlit() to reset buffer
@@ -106,7 +113,10 @@ extern intfiltered_base_yylex(YYSTYPE *lvalp, YYLTYPE *llocp,
106113
base_yyscan_tyyscanner);
107114

108115
/* from scan.l */
109-
externbase_yyscan_tscanner_init(constchar*str,base_yy_extra_type*yyext);
116+
externbase_yyscan_tscanner_init(constchar*str,
117+
base_yy_extra_type*yyext,
118+
constScanKeyword*keywords,
119+
intnum_keywords);
110120
externvoidscanner_finish(base_yyscan_tyyscanner);
111121
externintbase_yylex(YYSTYPE*lvalp,YYLTYPE*llocp,
112122
base_yyscan_tyyscanner);

‎src/include/parser/keywords.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/parser/keywords.h,v 1.26 2009/01/01 17:24:00 momjian Exp $
10+
* $PostgreSQL: pgsql/src/include/parser/keywords.h,v 1.27 2009/07/14 20:24:10 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -29,8 +29,10 @@ typedef struct ScanKeyword
2929
}ScanKeyword;
3030

3131
externconstScanKeywordScanKeywords[];
32-
externconstScanKeyword*LastScanKeyword;
32+
externconstintNumScanKeywords;
3333

34-
externconstScanKeyword*ScanKeywordLookup(constchar*text);
34+
externconstScanKeyword*ScanKeywordLookup(constchar*text,
35+
constScanKeyword*keywords,
36+
intnum_keywords);
3537

3638
#endif/* KEYWORDS_H */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp