1 /*-------------------------------------------------------------------------
4 * lexical token lookup for reserved words in PostgreSQL
6 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/parser/keywords.c,v 1.138 2003/04/08 23:20:02 tgl Exp $
13 *-------------------------------------------------------------------------
19 #include "nodes/parsenodes.h"
20 #include "parser/keywords.h"
21 #include "parser/parse.h"
23 /* NB: This file is also used by pg_dump. */
26 * List of (keyword-name, keyword-token-value) pairs.
28 * !!WARNING!!: This list must be sorted, because binary
29 * search is used to locate entries.
31 static const ScanKeyword ScanKeywords[] = {
34 {"absolute", ABSOLUTE},
39 {"aggregate", AGGREGATE},
42 {"analyse", ANALYSE}, /* British spelling */
49 {"assertion", ASSERTION},
50 {"assignment", ASSIGNMENT},
52 {"authorization", AUTHORIZATION},
53 {"backward", BACKWARD},
70 {"character", CHARACTER},
71 {"characteristics", CHARACTERISTICS},
73 {"checkpoint", CHECKPOINT},
77 {"coalesce", COALESCE},
82 {"committed", COMMITTED},
83 {"constraint", CONSTRAINT},
84 {"constraints", CONSTRAINTS},
85 {"conversion", CONVERSION_P},
89 {"createdb", CREATEDB},
90 {"createuser", CREATEUSER},
92 {"current_date", CURRENT_DATE},
93 {"current_time", CURRENT_TIME},
94 {"current_timestamp", CURRENT_TIMESTAMP},
95 {"current_user", CURRENT_USER},
98 {"database", DATABASE},
100 {"deallocate", DEALLOCATE},
102 {"decimal", DECIMAL},
103 {"declare", DECLARE},
104 {"default", DEFAULT},
105 {"deferrable", DEFERRABLE},
106 {"deferred", DEFERRED},
107 {"definer", DEFINER},
108 {"delete", DELETE_P},
109 {"delimiter", DELIMITER},
110 {"delimiters", DELIMITERS},
112 {"distinct", DISTINCT},
114 {"domain", DOMAIN_P},
119 {"encoding", ENCODING},
120 {"encrypted", ENCRYPTED},
124 {"exclusive", EXCLUSIVE},
125 {"execute", EXECUTE},
127 {"explain", EXPLAIN},
128 {"external", EXTERNAL},
129 {"extract", EXTRACT},
136 {"foreign", FOREIGN},
137 {"forward", FORWARD},
141 {"function", FUNCTION},
145 {"handler", HANDLER},
150 {"immediate", IMMEDIATE},
151 {"immutable", IMMUTABLE},
152 {"implicit", IMPLICIT_P},
154 {"increment", INCREMENT},
156 {"inherits", INHERITS},
157 {"initially", INITIALLY},
161 {"insensitive", INSENSITIVE},
163 {"instead", INSTEAD},
165 {"integer", INTEGER},
166 {"intersect", INTERSECT},
167 {"interval", INTERVAL},
169 {"invoker", INVOKER},
172 {"isolation", ISOLATION},
175 {"lancompiler", LANCOMPILER},
176 {"language", LANGUAGE},
178 {"leading", LEADING},
186 {"localtime", LOCALTIME},
187 {"localtimestamp", LOCALTIMESTAMP},
188 {"location", LOCATION},
191 {"maxvalue", MAXVALUE},
192 {"minute", MINUTE_P},
193 {"minvalue", MINVALUE},
198 {"national", NATIONAL},
199 {"natural", NATURAL},
204 {"nocreatedb", NOCREATEDB},
205 {"nocreateuser", NOCREATEUSER},
208 {"nothing", NOTHING},
210 {"notnull", NOTNULL},
213 {"numeric", NUMERIC},
221 {"operator", OPERATOR},
227 {"overlaps", OVERLAPS},
228 {"overlay", OVERLAY},
230 {"partial", PARTIAL},
231 {"password", PASSWORD},
233 {"pendant", PENDANT},
234 {"placing", PLACING},
235 {"position", POSITION},
236 {"precision", PRECISION},
237 {"prepare", PREPARE},
238 {"preserve", PRESERVE},
239 {"primary", PRIMARY},
241 {"privileges", PRIVILEGES},
242 {"procedural", PROCEDURAL},
243 {"procedure", PROCEDURE},
246 {"recheck", RECHECK},
247 {"references", REFERENCES},
248 {"reindex", REINDEX},
249 {"relative", RELATIVE},
251 {"replace", REPLACE},
253 {"restart", RESTART},
254 {"restrict", RESTRICT},
255 {"returns", RETURNS},
258 {"rollback", ROLLBACK},
264 {"second", SECOND_P},
265 {"security", SECURITY},
267 {"sequence", SEQUENCE},
268 {"serializable", SERIALIZABLE},
269 {"session", SESSION},
270 {"session_user", SESSION_USER},
275 {"similar", SIMILAR},
277 {"smallint", SMALLINT},
281 {"statement", STATEMENT},
282 {"statistics", STATISTICS},
285 {"storage", STORAGE},
287 {"substring", SUBSTRING},
291 {"template", TEMPLATE},
292 {"temporary", TEMPORARY},
295 {"timestamp", TIMESTAMP},
298 {"trailing", TRAILING},
299 {"transaction", TRANSACTION},
301 {"trigger", TRIGGER},
304 {"truncate", TRUNCATE},
305 {"trusted", TRUSTED},
307 {"unencrypted", UNENCRYPTED},
310 {"unknown", UNKNOWN},
311 {"unlisten", UNLISTEN},
319 {"validator", VALIDATOR},
321 {"varchar", VARCHAR},
322 {"varying", VARYING},
323 {"verbose", VERBOSE},
324 {"version", VERSION},
326 {"volatile", VOLATILE},
330 {"without", WITHOUT},
338 * ScanKeywordLookup - see if a given word is a keyword
340 * Returns a pointer to the ScanKeyword table entry, or NULL if no match.
342 * The match is done case-insensitively. Note that we deliberately use a
343 * dumbed-down case conversion that will only translate 'A'-'Z' into 'a'-'z',
344 * even if we are in a locale where tolower() would produce more or different
345 * translations. This is to conform to the SQL99 spec, which says that
346 * keywords are to be matched in this way even though non-keyword identifiers
347 * receive a different case-normalization mapping.
350 ScanKeywordLookup(const char *text)
354 char word[NAMEDATALEN];
355 const ScanKeyword *low;
356 const ScanKeyword *high;
359 /* We assume all keywords are shorter than NAMEDATALEN. */
360 if (len >= NAMEDATALEN)
364 * Apply an ASCII-only downcasing. We must not use tolower() since it
365 * may produce the wrong translation in some locales (eg, Turkish),
366 * and we don't trust isupper() very much either. In an ASCII-based
367 * encoding the tests against A and Z are sufficient, but we also
368 * check isupper() so that we will work correctly under EBCDIC. The
369 * actual case conversion step should work for either ASCII or EBCDIC.
371 for (i = 0; i < len; i++)
375 if (ch >= 'A' && ch <= 'Z' && isupper((unsigned char) ch))
382 * Now do a binary search using plain strcmp() comparison.
384 low = &ScanKeywords[0];
385 high = endof(ScanKeywords) - 1;
388 const ScanKeyword *middle;
391 middle = low + (high - low) / 2;
392 difference = strcmp(middle->name, word);
395 else if (difference < 0)