1 /*-------------------------------------------------------------------------
4 * lexical token lookup for reserved words in PostgreSQL
6 * Portions Copyright (c) 1996-2003, 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.141 2003/08/04 02:40:01 momjian 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_P},
39 {"aggregate", AGGREGATE},
42 {"analyse", ANALYSE}, /* British spelling */
49 {"assertion", ASSERTION},
50 {"assignment", ASSIGNMENT},
52 {"authorization", AUTHORIZATION},
53 {"backward", BACKWARD},
60 {"boolean", BOOLEAN_P},
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_P},
103 {"declare", DECLARE},
104 {"default", DEFAULT},
105 {"defaults", DEFAULTS},
106 {"deferrable", DEFERRABLE},
107 {"deferred", DEFERRED},
108 {"definer", DEFINER},
109 {"delete", DELETE_P},
110 {"delimiter", DELIMITER},
111 {"delimiters", DELIMITERS},
113 {"distinct", DISTINCT},
115 {"domain", DOMAIN_P},
116 {"double", DOUBLE_P},
120 {"encoding", ENCODING},
121 {"encrypted", ENCRYPTED},
125 {"excluding", EXCLUDING},
126 {"exclusive", EXCLUSIVE},
127 {"execute", EXECUTE},
129 {"explain", EXPLAIN},
130 {"external", EXTERNAL},
131 {"extract", EXTRACT},
138 {"foreign", FOREIGN},
139 {"forward", FORWARD},
143 {"function", FUNCTION},
147 {"handler", HANDLER},
152 {"immediate", IMMEDIATE},
153 {"immutable", IMMUTABLE},
154 {"implicit", IMPLICIT_P},
156 {"including", INCLUDING},
157 {"increment", INCREMENT},
159 {"inherits", INHERITS},
160 {"initially", INITIALLY},
164 {"insensitive", INSENSITIVE},
166 {"instead", INSTEAD},
168 {"integer", INTEGER},
169 {"intersect", INTERSECT},
170 {"interval", INTERVAL},
172 {"invoker", INVOKER},
175 {"isolation", ISOLATION},
178 {"lancompiler", LANCOMPILER},
179 {"language", LANGUAGE},
181 {"leading", LEADING},
189 {"localtime", LOCALTIME},
190 {"localtimestamp", LOCALTIMESTAMP},
191 {"location", LOCATION},
194 {"maxvalue", MAXVALUE},
195 {"minute", MINUTE_P},
196 {"minvalue", MINVALUE},
201 {"national", NATIONAL},
202 {"natural", NATURAL},
207 {"nocreatedb", NOCREATEDB},
208 {"nocreateuser", NOCREATEUSER},
211 {"nothing", NOTHING},
213 {"notnull", NOTNULL},
216 {"numeric", NUMERIC},
224 {"operator", OPERATOR},
230 {"overlaps", OVERLAPS},
231 {"overlay", OVERLAY},
233 {"partial", PARTIAL},
234 {"password", PASSWORD},
236 {"pendant", PENDANT},
237 {"placing", PLACING},
238 {"position", POSITION},
239 {"precision", PRECISION},
240 {"prepare", PREPARE},
241 {"preserve", PRESERVE},
242 {"primary", PRIMARY},
244 {"privileges", PRIVILEGES},
245 {"procedural", PROCEDURAL},
246 {"procedure", PROCEDURE},
249 {"recheck", RECHECK},
250 {"references", REFERENCES},
251 {"reindex", REINDEX},
252 {"relative", RELATIVE_P},
254 {"replace", REPLACE},
256 {"restart", RESTART},
257 {"restrict", RESTRICT},
258 {"returns", RETURNS},
261 {"rollback", ROLLBACK},
267 {"second", SECOND_P},
268 {"security", SECURITY},
270 {"sequence", SEQUENCE},
271 {"serializable", SERIALIZABLE},
272 {"session", SESSION},
273 {"session_user", SESSION_USER},
278 {"similar", SIMILAR},
280 {"smallint", SMALLINT},
284 {"statement", STATEMENT},
285 {"statistics", STATISTICS},
288 {"storage", STORAGE},
289 {"strict", STRICT_P},
290 {"substring", SUBSTRING},
294 {"template", TEMPLATE},
295 {"temporary", TEMPORARY},
298 {"timestamp", TIMESTAMP},
301 {"trailing", TRAILING},
302 {"transaction", TRANSACTION},
304 {"trigger", TRIGGER},
307 {"truncate", TRUNCATE},
308 {"trusted", TRUSTED},
310 {"unencrypted", UNENCRYPTED},
313 {"unknown", UNKNOWN},
314 {"unlisten", UNLISTEN},
322 {"validator", VALIDATOR},
324 {"varchar", VARCHAR},
325 {"varying", VARYING},
326 {"verbose", VERBOSE},
327 {"version", VERSION},
329 {"volatile", VOLATILE},
333 {"without", WITHOUT},
341 * ScanKeywordLookup - see if a given word is a keyword
343 * Returns a pointer to the ScanKeyword table entry, or NULL if no match.
345 * The match is done case-insensitively. Note that we deliberately use a
346 * dumbed-down case conversion that will only translate 'A'-'Z' into 'a'-'z',
347 * even if we are in a locale where tolower() would produce more or different
348 * translations. This is to conform to the SQL99 spec, which says that
349 * keywords are to be matched in this way even though non-keyword identifiers
350 * receive a different case-normalization mapping.
353 ScanKeywordLookup(const char *text)
357 char word[NAMEDATALEN];
358 const ScanKeyword *low;
359 const ScanKeyword *high;
362 /* We assume all keywords are shorter than NAMEDATALEN. */
363 if (len >= NAMEDATALEN)
367 * Apply an ASCII-only downcasing. We must not use tolower() since it
368 * may produce the wrong translation in some locales (eg, Turkish),
369 * and we don't trust isupper() very much either. In an ASCII-based
370 * encoding the tests against A and Z are sufficient, but we also
371 * check isupper() so that we will work correctly under EBCDIC. The
372 * actual case conversion step should work for either ASCII or EBCDIC.
374 for (i = 0; i < len; i++)
378 if (ch >= 'A' && ch <= 'Z' && isupper((unsigned char) ch))
385 * Now do a binary search using plain strcmp() comparison.
387 low = &ScanKeywords[0];
388 high = endof(ScanKeywords) - 1;
391 const ScanKeyword *middle;
394 middle = low + (high - low) / 2;
395 difference = strcmp(middle->name, word);
398 else if (difference < 0)