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.132 2002/12/12 20:35:13 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[] = {
33 {"abort", ABORT_TRANS},
34 {"absolute", ABSOLUTE},
39 {"aggregate", AGGREGATE},
42 {"analyse", ANALYSE}, /* British spelling */
48 {"assertion", ASSERTION},
49 {"assignment", ASSIGNMENT},
51 {"authorization", AUTHORIZATION},
52 {"backward", BACKWARD},
54 {"begin", BEGIN_TRANS},
69 {"character", CHARACTER},
70 {"characteristics", CHARACTERISTICS},
72 {"checkpoint", CHECKPOINT},
76 {"coalesce", COALESCE},
81 {"committed", COMMITTED},
82 {"constraint", CONSTRAINT},
83 {"constraints", CONSTRAINTS},
84 {"conversion", CONVERSION_P},
88 {"createdb", CREATEDB},
89 {"createuser", CREATEUSER},
91 {"current_date", CURRENT_DATE},
92 {"current_time", CURRENT_TIME},
93 {"current_timestamp", CURRENT_TIMESTAMP},
94 {"current_user", CURRENT_USER},
97 {"database", DATABASE},
99 {"deallocate", DEALLOCATE},
101 {"decimal", DECIMAL},
102 {"declare", DECLARE},
103 {"default", DEFAULT},
104 {"deferrable", DEFERRABLE},
105 {"deferred", DEFERRED},
106 {"definer", DEFINER},
107 {"delete", DELETE_P},
108 {"delimiter", DELIMITER},
109 {"delimiters", DELIMITERS},
111 {"distinct", DISTINCT},
113 {"domain", DOMAIN_P},
118 {"encoding", ENCODING},
119 {"encrypted", ENCRYPTED},
123 {"exclusive", EXCLUSIVE},
124 {"execute", EXECUTE},
126 {"explain", EXPLAIN},
127 {"external", EXTERNAL},
128 {"extract", EXTRACT},
134 {"foreign", FOREIGN},
135 {"forward", FORWARD},
139 {"function", FUNCTION},
144 {"handler", HANDLER},
148 {"immediate", IMMEDIATE},
149 {"immutable", IMMUTABLE},
150 {"implicit", IMPLICIT_P},
152 {"increment", INCREMENT},
154 {"inherits", INHERITS},
155 {"initially", INITIALLY},
159 {"insensitive", INSENSITIVE},
161 {"instead", INSTEAD},
163 {"integer", INTEGER},
164 {"intersect", INTERSECT},
165 {"interval", INTERVAL},
167 {"invoker", INVOKER},
170 {"isolation", ISOLATION},
173 {"lancompiler", LANCOMPILER},
174 {"language", LANGUAGE},
176 {"leading", LEADING},
184 {"localtime", LOCALTIME},
185 {"localtimestamp", LOCALTIMESTAMP},
186 {"location", LOCATION},
189 {"maxvalue", MAXVALUE},
190 {"minute", MINUTE_P},
191 {"minvalue", MINVALUE},
196 {"national", NATIONAL},
197 {"natural", NATURAL},
202 {"nocreatedb", NOCREATEDB},
203 {"nocreateuser", NOCREATEUSER},
206 {"nothing", NOTHING},
208 {"notnull", NOTNULL},
211 {"numeric", NUMERIC},
219 {"operator", OPERATOR},
225 {"overlaps", OVERLAPS},
226 {"overlay", OVERLAY},
228 {"partial", PARTIAL},
229 {"password", PASSWORD},
231 {"pendant", PENDANT},
232 {"placing", PLACING},
233 {"position", POSITION},
234 {"precision", PRECISION},
235 {"prepare", PREPARE},
236 {"preserve", PRESERVE},
237 {"primary", PRIMARY},
239 {"privileges", PRIVILEGES},
240 {"procedural", PROCEDURAL},
241 {"procedure", PROCEDURE},
244 {"recheck", RECHECK},
245 {"references", REFERENCES},
246 {"reindex", REINDEX},
247 {"relative", RELATIVE},
249 {"replace", REPLACE},
251 {"restrict", RESTRICT},
252 {"returns", RETURNS},
255 {"rollback", ROLLBACK},
261 {"second", SECOND_P},
262 {"security", SECURITY},
264 {"sequence", SEQUENCE},
265 {"serializable", SERIALIZABLE},
266 {"session", SESSION},
267 {"session_user", SESSION_USER},
272 {"similar", SIMILAR},
274 {"smallint", SMALLINT},
278 {"statement", STATEMENT},
279 {"statistics", STATISTICS},
282 {"storage", STORAGE},
284 {"substring", SUBSTRING},
288 {"template", TEMPLATE},
289 {"temporary", TEMPORARY},
292 {"timestamp", TIMESTAMP},
295 {"trailing", TRAILING},
296 {"transaction", TRANSACTION},
298 {"trigger", TRIGGER},
301 {"truncate", TRUNCATE},
302 {"trusted", TRUSTED},
304 {"unencrypted", UNENCRYPTED},
307 {"unknown", UNKNOWN},
308 {"unlisten", UNLISTEN},
316 {"validator", VALIDATOR},
318 {"varchar", VARCHAR},
319 {"varying", VARYING},
320 {"verbose", VERBOSE},
321 {"version", VERSION},
323 {"volatile", VOLATILE},
327 {"without", WITHOUT},
335 * ScanKeywordLookup - see if a given word is a keyword
337 * Returns a pointer to the ScanKeyword table entry, or NULL if no match.
339 * The match is done case-insensitively. Note that we deliberately use a
340 * dumbed-down case conversion that will only translate 'A'-'Z' into 'a'-'z',
341 * even if we are in a locale where tolower() would produce more or different
342 * translations. This is to conform to the SQL99 spec, which says that
343 * keywords are to be matched in this way even though non-keyword identifiers
344 * receive a different case-normalization mapping.
347 ScanKeywordLookup(const char *text)
351 char word[NAMEDATALEN];
352 const ScanKeyword *low;
353 const ScanKeyword *high;
356 /* We assume all keywords are shorter than NAMEDATALEN. */
357 if (len >= NAMEDATALEN)
361 * Apply an ASCII-only downcasing. We must not use tolower() since it
362 * may produce the wrong translation in some locales (eg, Turkish),
363 * and we don't trust isupper() very much either. In an ASCII-based
364 * encoding the tests against A and Z are sufficient, but we also
365 * check isupper() so that we will work correctly under EBCDIC. The
366 * actual case conversion step should work for either ASCII or EBCDIC.
368 for (i = 0; i < len; i++)
372 if (ch >= 'A' && ch <= 'Z' && isupper((unsigned char) ch))
379 * Now do a binary search using plain strcmp() comparison.
381 low = &ScanKeywords[0];
382 high = endof(ScanKeywords) - 1;
385 const ScanKeyword *middle;
388 middle = low + (high - low) / 2;
389 difference = strcmp(middle->name, word);
392 else if (difference < 0)