1 /*-------------------------------------------------------------------------
4 * lexical token lookup for key words in PostgreSQL
7 * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
12 * src/common/keywords.c
14 *-------------------------------------------------------------------------
19 #include "postgres_fe.h"
24 #include "parser/gramparse.h"
26 #define PG_KEYWORD(a,b,c) {a,b,c},
30 #include "common/keywords.h"
33 * We don't need the token number for frontend uses, so leave it out to avoid
34 * requiring backend headers that won't compile cleanly here.
36 #define PG_KEYWORD(a,b,c) {a,0,c},
41 const ScanKeyword ScanKeywords[] = {
42 #include "parser/kwlist.h"
45 const int NumScanKeywords = lengthof(ScanKeywords);
49 * ScanKeywordLookup - see if a given word is a keyword
51 * The table to be searched is passed explicitly, so that this can be used
52 * to search keyword lists other than the standard list appearing above.
54 * Returns a pointer to the ScanKeyword table entry, or NULL if no match.
56 * The match is done case-insensitively. Note that we deliberately use a
57 * dumbed-down case conversion that will only translate 'A'-'Z' into 'a'-'z',
58 * even if we are in a locale where tolower() would produce more or different
59 * translations. This is to conform to the SQL99 spec, which says that
60 * keywords are to be matched in this way even though non-keyword identifiers
61 * receive a different case-normalization mapping.
64 ScanKeywordLookup(const char *text,
65 const ScanKeyword *keywords,
70 char word[NAMEDATALEN];
71 const ScanKeyword *low;
72 const ScanKeyword *high;
75 /* We assume all keywords are shorter than NAMEDATALEN. */
76 if (len >= NAMEDATALEN)
80 * Apply an ASCII-only downcasing. We must not use tolower() since it may
81 * produce the wrong translation in some locales (eg, Turkish).
83 for (i = 0; i < len; i++)
87 if (ch >= 'A' && ch <= 'Z')
94 * Now do a binary search using plain strcmp() comparison.
97 high = keywords + (num_keywords - 1);
100 const ScanKeyword *middle;
103 middle = low + (high - low) / 2;
104 difference = strcmp(middle->name, word);
107 else if (difference < 0)