]> granicus.if.org Git - postgresql/blob - src/backend/parser/keywords.c
fad08c849f1623bea915bdb93b65314a5ab7d119
[postgresql] / src / backend / parser / keywords.c
1 /*-------------------------------------------------------------------------
2  *
3  * keywords.c
4  *        lexical token lookup for reserved words in PostgreSQL
5  *
6  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $Header: /cvsroot/pgsql/src/backend/parser/keywords.c,v 1.99 2001/10/10 00:02:42 petere Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
17 #include <ctype.h>
18
19 #include "nodes/parsenodes.h"
20 #include "parser/keywords.h"
21 #include "parser/parse.h"
22
23 /*
24  * List of (keyword-name, keyword-token-value) pairs.
25  *
26  * !!WARNING!!: This list must be sorted, because binary
27  *               search is used to locate entries.
28  */
29 static ScanKeyword ScanKeywords[] = {
30         /* name, value */
31         {"abort", ABORT_TRANS},
32         {"absolute", ABSOLUTE},
33         {"access", ACCESS},
34         {"action", ACTION},
35         {"add", ADD},
36         {"after", AFTER},
37         {"aggregate", AGGREGATE},
38         {"all", ALL},
39         {"alter", ALTER},
40         {"analyse", ANALYSE},           /* British spelling */
41         {"analyze", ANALYZE},
42         {"and", AND},
43         {"any", ANY},
44         {"as", AS},
45         {"asc", ASC},
46         {"at", AT},
47         {"authorization", AUTHORIZATION},
48         {"backward", BACKWARD},
49         {"before", BEFORE},
50         {"begin", BEGIN_TRANS},
51         {"between", BETWEEN},
52         {"binary", BINARY},
53         {"bit", BIT},
54         {"both", BOTH},
55         {"by", BY},
56         {"cache", CACHE},
57         {"cascade", CASCADE},
58         {"case", CASE},
59         {"cast", CAST},
60         {"chain", CHAIN},
61         {"char", CHAR},
62         {"character", CHARACTER},
63         {"characteristics", CHARACTERISTICS},
64         {"check", CHECK},
65         {"checkpoint", CHECKPOINT},
66         {"close", CLOSE},
67         {"cluster", CLUSTER},
68         {"coalesce", COALESCE},
69         {"collate", COLLATE},
70         {"column", COLUMN},
71         {"comment", COMMENT},
72         {"commit", COMMIT},
73         {"committed", COMMITTED},
74         {"constraint", CONSTRAINT},
75         {"constraints", CONSTRAINTS},
76         {"copy", COPY},
77         {"create", CREATE},
78         {"createdb", CREATEDB},
79         {"createuser", CREATEUSER},
80         {"cross", CROSS},
81         {"current_date", CURRENT_DATE},
82         {"current_time", CURRENT_TIME},
83         {"current_timestamp", CURRENT_TIMESTAMP},
84         {"current_user", CURRENT_USER},
85         {"cursor", CURSOR},
86         {"cycle", CYCLE},
87         {"database", DATABASE},
88         {"day", DAY_P},
89         {"dec", DEC},
90         {"decimal", DECIMAL},
91         {"declare", DECLARE},
92         {"default", DEFAULT},
93         {"deferrable", DEFERRABLE},
94         {"deferred", DEFERRED},
95         {"delete", DELETE},
96         {"delimiters", DELIMITERS},
97         {"desc", DESC},
98         {"distinct", DISTINCT},
99         {"do", DO},
100         {"double", DOUBLE},
101         {"drop", DROP},
102         {"each", EACH},
103         {"else", ELSE},
104         {"encoding", ENCODING},
105         {"encrypted", ENCRYPTED},
106         {"end", END_TRANS},
107         {"escape", ESCAPE},
108         {"except", EXCEPT},
109         {"exclusive", EXCLUSIVE},
110         {"execute", EXECUTE},
111         {"exists", EXISTS},
112         {"explain", EXPLAIN},
113         {"extract", EXTRACT},
114         {"false", FALSE_P},
115         {"fetch", FETCH},
116         {"float", FLOAT},
117         {"for", FOR},
118         {"force", FORCE},
119         {"foreign", FOREIGN},
120         {"forward", FORWARD},
121         {"freeze", FREEZE},
122         {"from", FROM},
123         {"full", FULL},
124         {"function", FUNCTION},
125         {"global", GLOBAL},
126         {"grant", GRANT},
127         {"group", GROUP},
128         {"handler", HANDLER},
129         {"having", HAVING},
130         {"hour", HOUR_P},
131         {"ilike", ILIKE},
132         {"immediate", IMMEDIATE},
133         {"in", IN},
134         {"increment", INCREMENT},
135         {"index", INDEX},
136         {"inherits", INHERITS},
137         {"initially", INITIALLY},
138         {"inner", INNER_P},
139         {"inout", INOUT},
140         {"insensitive", INSENSITIVE},
141         {"insert", INSERT},
142         {"instead", INSTEAD},
143         {"intersect", INTERSECT},
144         {"interval", INTERVAL},
145         {"into", INTO},
146         {"is", IS},
147         {"isnull", ISNULL},
148         {"isolation", ISOLATION},
149         {"join", JOIN},
150         {"key", KEY},
151         {"lancompiler", LANCOMPILER},
152         {"language", LANGUAGE},
153         {"leading", LEADING},
154         {"left", LEFT},
155         {"level", LEVEL},
156         {"like", LIKE},
157         {"limit", LIMIT},
158         {"listen", LISTEN},
159         {"load", LOAD},
160         {"local", LOCAL},
161         {"location", LOCATION},
162         {"lock", LOCK_P},
163         {"match", MATCH},
164         {"maxvalue", MAXVALUE},
165         {"minute", MINUTE_P},
166         {"minvalue", MINVALUE},
167         {"mode", MODE},
168         {"month", MONTH_P},
169         {"move", MOVE},
170         {"names", NAMES},
171         {"national", NATIONAL},
172         {"natural", NATURAL},
173         {"nchar", NCHAR},
174         {"new", NEW},
175         {"next", NEXT},
176         {"no", NO},
177         {"nocreatedb", NOCREATEDB},
178         {"nocreateuser", NOCREATEUSER},
179         {"none", NONE},
180         {"not", NOT},
181         {"nothing", NOTHING},
182         {"notify", NOTIFY},
183         {"notnull", NOTNULL},
184         {"null", NULL_P},
185         {"nullif", NULLIF},
186         {"numeric", NUMERIC},
187         {"of", OF},
188         {"off", OFF},
189         {"offset", OFFSET},
190         {"oids", OIDS},
191         {"old", OLD},
192         {"on", ON},
193         {"only", ONLY},
194         {"operator", OPERATOR},
195         {"option", OPTION},
196         {"or", OR},
197         {"order", ORDER},
198         {"out", OUT},
199         {"outer", OUTER_P},
200         {"overlaps", OVERLAPS},
201         {"owner", OWNER},
202         {"partial", PARTIAL},
203         {"password", PASSWORD},
204         {"path", PATH_P},
205         {"pendant", PENDANT},
206         {"position", POSITION},
207         {"precision", PRECISION},
208         {"primary", PRIMARY},
209         {"prior", PRIOR},
210         {"privileges", PRIVILEGES},
211         {"procedural", PROCEDURAL},
212         {"procedure", PROCEDURE},
213         {"public", PUBLIC},
214         {"read", READ},
215         {"references", REFERENCES},
216         {"reindex", REINDEX},
217         {"relative", RELATIVE},
218         {"rename", RENAME},
219         {"replace", REPLACE},
220         {"reset", RESET},
221         {"restrict", RESTRICT},
222         {"returns", RETURNS},
223         {"revoke", REVOKE},
224         {"right", RIGHT},
225         {"rollback", ROLLBACK},
226         {"row", ROW},
227         {"rule", RULE},
228         {"schema", SCHEMA},
229         {"scroll", SCROLL},
230         {"second", SECOND_P},
231         {"select", SELECT},
232         {"sequence", SEQUENCE},
233         {"serializable", SERIALIZABLE},
234         {"session", SESSION},
235         {"session_user", SESSION_USER},
236         {"set", SET},
237         {"setof", SETOF},
238         {"share", SHARE},
239         {"show", SHOW},
240         {"some", SOME},
241         {"start", START},
242         {"statement", STATEMENT},
243         {"statistics", STATISTICS},
244         {"stdin", STDIN},
245         {"stdout", STDOUT},
246         {"substring", SUBSTRING},
247         {"sysid", SYSID},
248         {"table", TABLE},
249         {"temp", TEMP},
250         {"template", TEMPLATE},
251         {"temporary", TEMPORARY},
252         {"then", THEN},
253         {"time", TIME},
254         {"timestamp", TIMESTAMP},
255         {"to", TO},
256         {"toast", TOAST},
257         {"trailing", TRAILING},
258         {"transaction", TRANSACTION},
259         {"trigger", TRIGGER},
260         {"trim", TRIM},
261         {"true", TRUE_P},
262         {"truncate", TRUNCATE},
263         {"trusted", TRUSTED},
264         {"type", TYPE_P},
265         {"unencrypted", UNENCRYPTED},
266         {"union", UNION},
267         {"unique", UNIQUE},
268         {"unknown", UNKNOWN},
269         {"unlisten", UNLISTEN},
270         {"until", UNTIL},
271         {"update", UPDATE},
272         {"user", USER},
273         {"using", USING},
274         {"vacuum", VACUUM},
275         {"valid", VALID},
276         {"values", VALUES},
277         {"varchar", VARCHAR},
278         {"varying", VARYING},
279         {"verbose", VERBOSE},
280         {"version", VERSION},
281         {"view", VIEW},
282         {"when", WHEN},
283         {"where", WHERE},
284         {"with", WITH},
285         {"without", WITHOUT},
286         {"work", WORK},
287         {"year", YEAR_P},
288         {"zone", ZONE},
289 };
290
291 /*
292  * ScanKeywordLookup - see if a given word is a keyword
293  *
294  * Returns a pointer to the ScanKeyword table entry, or NULL if no match.
295  *
296  * The match is done case-insensitively.  Note that we deliberately use a
297  * dumbed-down case conversion that will only translate 'A'-'Z' into 'a'-'z',
298  * even if we are in a locale where tolower() would produce more or different
299  * translations.  This is to conform to the SQL99 spec, which says that
300  * keywords are to be matched in this way even though non-keyword identifiers
301  * receive a different case-normalization mapping.
302  */
303 ScanKeyword *
304 ScanKeywordLookup(char *text)
305 {
306         int                     len,
307                                 i;
308         char            word[NAMEDATALEN];
309         ScanKeyword *low;
310         ScanKeyword *high;
311
312         len = strlen(text);
313         /* We assume all keywords are shorter than NAMEDATALEN. */
314         if (len >= NAMEDATALEN)
315                 return NULL;
316
317         /*
318          * Apply an ASCII-only downcasing.      We must not use tolower() since it
319          * may produce the wrong translation in some locales (eg, Turkish),
320          * and we don't trust isupper() very much either.  In an ASCII-based
321          * encoding the tests against A and Z are sufficient, but we also
322          * check isupper() so that we will work correctly under EBCDIC.  The
323          * actual case conversion step should work for either ASCII or EBCDIC.
324          */
325         for (i = 0; i < len; i++)
326         {
327                 char            ch = text[i];
328
329                 if (ch >= 'A' && ch <= 'Z' && isupper((unsigned char) ch))
330                         ch += 'a' - 'A';
331                 word[i] = ch;
332         }
333         word[len] = '\0';
334
335         /*
336          * Now do a binary search using plain strcmp() comparison.
337          */
338         low = &ScanKeywords[0];
339         high = endof(ScanKeywords) - 1;
340         while (low <= high)
341         {
342                 ScanKeyword *middle;
343                 int                     difference;
344
345                 middle = low + (high - low) / 2;
346                 difference = strcmp(middle->name, word);
347                 if (difference == 0)
348                         return middle;
349                 else if (difference < 0)
350                         low = middle + 1;
351                 else
352                         high = middle - 1;
353         }
354
355         return NULL;
356 }