|
1 | 1 | /*-------------------------------------------------------------------------
|
2 | 2 | *
|
3 | 3 | * keywords.c
|
4 |
| - * lexical token lookup for key words in PostgreSQL |
| 4 | + * PostgreSQL's list of SQL keywords |
5 | 5 | *
|
6 | 6 | *
|
7 | 7 | * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
|
|
13 | 13 | *
|
14 | 14 | *-------------------------------------------------------------------------
|
15 | 15 | */
|
16 |
| -#ifndef FRONTEND |
17 |
| -#include "postgres.h" |
18 |
| -#else |
19 |
| -#include "postgres_fe.h" |
20 |
| -#endif |
| 16 | +#include "c.h" |
21 | 17 |
|
22 |
| -#ifndef FRONTEND |
23 |
| - |
24 |
| -#include "parser/gramparse.h" |
| 18 | +#include "common/keywords.h" |
25 | 19 |
|
26 |
| -#define PG_KEYWORD(a,b,c) {a,b,c}, |
27 | 20 |
|
28 |
| -#else |
| 21 | +/* ScanKeywordList lookup data for SQL keywords */ |
29 | 22 |
|
30 |
| -#include "common/keywords.h" |
31 |
| - |
32 |
| -/* |
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. |
35 |
| - */ |
36 |
| -#define PG_KEYWORD(a,b,c) {a,0,c}, |
| 23 | +#include "kwlist_d.h" |
37 | 24 |
|
38 |
| -#endif /* FRONTEND */ |
| 25 | +/* Keyword categories for SQL keywords */ |
39 | 26 |
|
| 27 | +#define PG_KEYWORD(kwname, value, category) category, |
40 | 28 |
|
41 |
| -const ScanKeyword ScanKeywords[] = { |
| 29 | +const uint8 ScanKeywordCategories[SCANKEYWORDS_NUM_KEYWORDS] = { |
42 | 30 | #include "parser/kwlist.h"
|
43 | 31 | };
|
44 | 32 |
|
45 |
| -const int NumScanKeywords = lengthof(ScanKeywords); |
46 |
| - |
47 |
| - |
48 |
| -/* |
49 |
| - * ScanKeywordLookup - see if a given word is a keyword |
50 |
| - * |
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. |
53 |
| - * |
54 |
| - * Returns a pointer to the ScanKeyword table entry, or NULL if no match. |
55 |
| - * |
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. |
62 |
| - */ |
63 |
| -const ScanKeyword * |
64 |
| -ScanKeywordLookup(const char *text, |
65 |
| - const ScanKeyword *keywords, |
66 |
| - int num_keywords) |
67 |
| -{ |
68 |
| - int len, |
69 |
| - i; |
70 |
| - char word[NAMEDATALEN]; |
71 |
| - const ScanKeyword *low; |
72 |
| - const ScanKeyword *high; |
73 |
| - |
74 |
| - len = strlen(text); |
75 |
| - /* We assume all keywords are shorter than NAMEDATALEN. */ |
76 |
| - if (len >= NAMEDATALEN) |
77 |
| - return NULL; |
78 |
| - |
79 |
| - /* |
80 |
| - * Apply an ASCII-only downcasing. We must not use tolower() since it may |
81 |
| - * produce the wrong translation in some locales (eg, Turkish). |
82 |
| - */ |
83 |
| - for (i = 0; i < len; i++) |
84 |
| - { |
85 |
| - char ch = text[i]; |
86 |
| - |
87 |
| - if (ch >= 'A' && ch <= 'Z') |
88 |
| - ch += 'a' - 'A'; |
89 |
| - word[i] = ch; |
90 |
| - } |
91 |
| - word[len] = '\0'; |
92 |
| - |
93 |
| - /* |
94 |
| - * Now do a binary search using plain strcmp() comparison. |
95 |
| - */ |
96 |
| - low = keywords; |
97 |
| - high = keywords + (num_keywords - 1); |
98 |
| - while (low <= high) |
99 |
| - { |
100 |
| - const ScanKeyword *middle; |
101 |
| - int difference; |
102 |
| - |
103 |
| - middle = low + (high - low) / 2; |
104 |
| - difference = strcmp(middle->name, word); |
105 |
| - if (difference == 0) |
106 |
| - return middle; |
107 |
| - else if (difference < 0) |
108 |
| - low = middle + 1; |
109 |
| - else |
110 |
| - high = middle - 1; |
111 |
| - } |
112 |
| - |
113 |
| - return NULL; |
114 |
| -} |
| 33 | +#undef PG_KEYWORD |
0 commit comments