From 2c6af4f44228d76d3351fe26f68b00b55cdd239a Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Wed, 23 Mar 2016 20:22:08 -0400 Subject: Move keywords.c/kwlookup.c into src/common/. Now that we have src/common/ for code shared between frontend and backend, we can get rid of (most of) the klugy ways that the keyword table and keyword lookup code were formerly shared between different uses. This is a first step towards a more general plan of getting rid of special-purpose kluges for sharing code in src/bin/. I chose to merge kwlookup.c back into keywords.c, as it once was, and always has been so far as keywords.h is concerned. We could have kept them separate, but there is noplace that uses ScanKeywordLookup without also wanting access to the backend's keyword list, so there seems little point. ecpg is still a bit weird, but at least now the trickiness is documented. I think that the MSVC build script should require no adjustments beyond what's done here ... but we'll soon find out. --- src/backend/parser/Makefile | 4 +- src/backend/parser/README | 6 ++- src/backend/parser/keywords.c | 27 ------------ src/backend/parser/kwlookup.c | 89 --------------------------------------- src/backend/utils/adt/misc.c | 2 +- src/backend/utils/adt/ruleutils.c | 2 +- 6 files changed, 8 insertions(+), 122 deletions(-) delete mode 100644 src/backend/parser/keywords.c delete mode 100644 src/backend/parser/kwlookup.c (limited to 'src/backend') diff --git a/src/backend/parser/Makefile b/src/backend/parser/Makefile index 9cc8946fa1b..fdd8485cec5 100644 --- a/src/backend/parser/Makefile +++ b/src/backend/parser/Makefile @@ -12,7 +12,7 @@ include $(top_builddir)/src/Makefile.global override CPPFLAGS := -I. -I$(srcdir) $(CPPFLAGS) -OBJS= analyze.o gram.o scan.o keywords.o kwlookup.o parser.o \ +OBJS= analyze.o gram.o scan.o parser.o \ parse_agg.o parse_clause.o parse_coerce.o parse_collate.o parse_cte.o \ parse_expr.o parse_func.o parse_node.o parse_oper.o parse_param.o \ parse_relation.o parse_target.o parse_type.o parse_utilcmd.o scansup.o @@ -44,7 +44,7 @@ scan.c: FLEX_NO_BACKUP=yes # Force these dependencies to be known even without dependency info built: -gram.o scan.o keywords.o parser.o: gram.h +gram.o scan.o parser.o: gram.h # gram.c, gram.h, and scan.c are in the distribution tarball, so they diff --git a/src/backend/parser/README b/src/backend/parser/README index 08625e427d2..6d8f19b5ca0 100644 --- a/src/backend/parser/README +++ b/src/backend/parser/README @@ -10,8 +10,6 @@ to the optimizer and then executor. parser.c things start here scan.l break query into tokens scansup.c handle escapes in input strings -kwlookup.c turn keywords into specific tokens -keywords.c table of standard keywords (passed to kwlookup.c) gram.y parse the tokens and produce a "raw" parse tree analyze.c top level of parse analysis for optimizable queries parse_agg.c handle aggregates, like SUM(col1), AVG(col2), ... @@ -28,3 +26,7 @@ parse_relation.c support routines for tables and column handling parse_target.c handle the result list of the query parse_type.c support routines for data type handling parse_utilcmd.c parse analysis for utility commands (done at execution time) + +See also src/common/keywords.c, which contains the table of standard +keywords and the keyword lookup function. We separated that out because +various frontend code wants to use it too. diff --git a/src/backend/parser/keywords.c b/src/backend/parser/keywords.c deleted file mode 100644 index 7a4f1286905..00000000000 --- a/src/backend/parser/keywords.c +++ /dev/null @@ -1,27 +0,0 @@ -/*------------------------------------------------------------------------- - * - * keywords.c - * lexical token lookup for key words in PostgreSQL - * - * - * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group - * Portions Copyright (c) 1994, Regents of the University of California - * - * - * IDENTIFICATION - * src/backend/parser/keywords.c - * - *------------------------------------------------------------------------- - */ -#include "postgres.h" - -#include "parser/gramparse.h" - -#define PG_KEYWORD(a,b,c) {a,b,c}, - - -const ScanKeyword ScanKeywords[] = { -#include "parser/kwlist.h" -}; - -const int NumScanKeywords = lengthof(ScanKeywords); diff --git a/src/backend/parser/kwlookup.c b/src/backend/parser/kwlookup.c deleted file mode 100644 index 4406fef37f4..00000000000 --- a/src/backend/parser/kwlookup.c +++ /dev/null @@ -1,89 +0,0 @@ -/*------------------------------------------------------------------------- - * - * kwlookup.c - * lexical token lookup for key words in PostgreSQL - * - * NB - this file is also used by ECPG and several frontend programs in - * src/bin/ including pg_dump and psql - * - * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group - * Portions Copyright (c) 1994, Regents of the University of California - * - * - * IDENTIFICATION - * src/backend/parser/kwlookup.c - * - *------------------------------------------------------------------------- - */ - -/* use c.h so this can be built as either frontend or backend */ -#include "c.h" - -#include - -#include "parser/keywords.h" - -/* - * ScanKeywordLookup - see if a given word is a keyword - * - * Returns a pointer to the ScanKeyword table entry, or NULL if no match. - * - * The match is done case-insensitively. Note that we deliberately use a - * dumbed-down case conversion that will only translate 'A'-'Z' into 'a'-'z', - * even if we are in a locale where tolower() would produce more or different - * translations. This is to conform to the SQL99 spec, which says that - * keywords are to be matched in this way even though non-keyword identifiers - * receive a different case-normalization mapping. - */ -const ScanKeyword * -ScanKeywordLookup(const char *text, - const ScanKeyword *keywords, - int num_keywords) -{ - int len, - i; - char word[NAMEDATALEN]; - const ScanKeyword *low; - const ScanKeyword *high; - - len = strlen(text); - /* We assume all keywords are shorter than NAMEDATALEN. */ - if (len >= NAMEDATALEN) - return NULL; - - /* - * Apply an ASCII-only downcasing. We must not use tolower() since it may - * produce the wrong translation in some locales (eg, Turkish). - */ - for (i = 0; i < len; i++) - { - char ch = text[i]; - - if (ch >= 'A' && ch <= 'Z') - ch += 'a' - 'A'; - word[i] = ch; - } - word[len] = '\0'; - - /* - * Now do a binary search using plain strcmp() comparison. - */ - low = keywords; - high = keywords + (num_keywords - 1); - while (low <= high) - { - const ScanKeyword *middle; - int difference; - - middle = low + (high - low) / 2; - difference = strcmp(middle->name, word); - if (difference == 0) - return middle; - else if (difference < 0) - low = middle + 1; - else - high = middle - 1; - } - - return NULL; -} diff --git a/src/backend/utils/adt/misc.c b/src/backend/utils/adt/misc.c index 4dcc5a63be7..faa8ef3c913 100644 --- a/src/backend/utils/adt/misc.c +++ b/src/backend/utils/adt/misc.c @@ -25,10 +25,10 @@ #include "catalog/pg_tablespace.h" #include "catalog/pg_type.h" #include "commands/dbcommands.h" +#include "common/keywords.h" #include "funcapi.h" #include "miscadmin.h" #include "parser/scansup.h" -#include "parser/keywords.h" #include "postmaster/syslogger.h" #include "rewrite/rewriteHandler.h" #include "storage/fd.h" diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index 490a0906e8e..2b47e95a687 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -38,6 +38,7 @@ #include "catalog/pg_type.h" #include "commands/defrem.h" #include "commands/tablespace.h" +#include "common/keywords.h" #include "executor/spi.h" #include "funcapi.h" #include "mb/pg_wchar.h" @@ -45,7 +46,6 @@ #include "nodes/makefuncs.h" #include "nodes/nodeFuncs.h" #include "optimizer/tlist.h" -#include "parser/keywords.h" #include "parser/parse_node.h" #include "parser/parse_agg.h" #include "parser/parse_func.h" -- cgit v1.2.3