1 /*-------------------------------------------------------------------------
4 * lexical token lookup for reserved words in PostgreSQL
6 * Portions Copyright (c) 1996-2001, 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.98 2001/10/02 21:39:35 tgl Exp $
13 *-------------------------------------------------------------------------
19 #include "nodes/parsenodes.h"
20 #include "parser/keywords.h"
21 #include "parser/parse.h"
24 * List of (keyword-name, keyword-token-value) pairs.
26 * !!WARNING!!: This list must be sorted, because binary
27 * search is used to locate entries.
29 static ScanKeyword ScanKeywords[] = {
31 {"abort", ABORT_TRANS},
32 {"absolute", ABSOLUTE},
37 {"aggregate", AGGREGATE},
40 {"analyse", ANALYSE}, /* British spelling */
47 {"authorization", AUTHORIZATION},
48 {"backward", BACKWARD},
50 {"begin", BEGIN_TRANS},
62 {"character", CHARACTER},
63 {"characteristics", CHARACTERISTICS},
65 {"checkpoint", CHECKPOINT},
68 {"coalesce", COALESCE},
73 {"committed", COMMITTED},
74 {"constraint", CONSTRAINT},
75 {"constraints", CONSTRAINTS},
78 {"createdb", CREATEDB},
79 {"createuser", CREATEUSER},
81 {"current_date", CURRENT_DATE},
82 {"current_time", CURRENT_TIME},
83 {"current_timestamp", CURRENT_TIMESTAMP},
84 {"current_user", CURRENT_USER},
87 {"database", DATABASE},
93 {"deferrable", DEFERRABLE},
94 {"deferred", DEFERRED},
96 {"delimiters", DELIMITERS},
98 {"distinct", DISTINCT},
104 {"encoding", ENCODING},
105 {"encrypted", ENCRYPTED},
109 {"exclusive", EXCLUSIVE},
110 {"execute", EXECUTE},
112 {"explain", EXPLAIN},
113 {"extract", EXTRACT},
119 {"foreign", FOREIGN},
120 {"forward", FORWARD},
124 {"function", FUNCTION},
128 {"handler", HANDLER},
132 {"immediate", IMMEDIATE},
134 {"increment", INCREMENT},
136 {"inherits", INHERITS},
137 {"initially", INITIALLY},
140 {"insensitive", INSENSITIVE},
142 {"instead", INSTEAD},
143 {"intersect", INTERSECT},
144 {"interval", INTERVAL},
148 {"isolation", ISOLATION},
151 {"lancompiler", LANCOMPILER},
152 {"language", LANGUAGE},
153 {"leading", LEADING},
161 {"location", LOCATION},
164 {"maxvalue", MAXVALUE},
165 {"minute", MINUTE_P},
166 {"minvalue", MINVALUE},
171 {"national", NATIONAL},
172 {"natural", NATURAL},
177 {"nocreatedb", NOCREATEDB},
178 {"nocreateuser", NOCREATEUSER},
181 {"nothing", NOTHING},
183 {"notnull", NOTNULL},
186 {"numeric", NUMERIC},
194 {"operator", OPERATOR},
200 {"overlaps", OVERLAPS},
202 {"partial", PARTIAL},
203 {"password", PASSWORD},
205 {"pendant", PENDANT},
206 {"position", POSITION},
207 {"precision", PRECISION},
208 {"primary", PRIMARY},
210 {"privileges", PRIVILEGES},
211 {"procedural", PROCEDURAL},
212 {"procedure", PROCEDURE},
215 {"references", REFERENCES},
216 {"reindex", REINDEX},
217 {"relative", RELATIVE},
219 {"replace", REPLACE},
221 {"restrict", RESTRICT},
222 {"returns", RETURNS},
225 {"rollback", ROLLBACK},
230 {"second", SECOND_P},
232 {"sequence", SEQUENCE},
233 {"serializable", SERIALIZABLE},
234 {"session", SESSION},
235 {"session_user", SESSION_USER},
242 {"statement", STATEMENT},
243 {"statistics", STATISTICS},
246 {"substring", SUBSTRING},
250 {"template", TEMPLATE},
251 {"temporary", TEMPORARY},
254 {"timestamp", TIMESTAMP},
255 {"timezone_hour", TIMEZONE_HOUR},
256 {"timezone_minute", TIMEZONE_MINUTE},
259 {"trailing", TRAILING},
260 {"transaction", TRANSACTION},
261 {"trigger", TRIGGER},
264 {"truncate", TRUNCATE},
265 {"trusted", TRUSTED},
267 {"unencrypted", UNENCRYPTED},
270 {"unknown", UNKNOWN},
271 {"unlisten", UNLISTEN},
279 {"varchar", VARCHAR},
280 {"varying", VARYING},
281 {"verbose", VERBOSE},
282 {"version", VERSION},
287 {"without", WITHOUT},
294 * ScanKeywordLookup - see if a given word is a keyword
296 * Returns a pointer to the ScanKeyword table entry, or NULL if no match.
298 * The match is done case-insensitively. Note that we deliberately use a
299 * dumbed-down case conversion that will only translate 'A'-'Z' into 'a'-'z',
300 * even if we are in a locale where tolower() would produce more or different
301 * translations. This is to conform to the SQL99 spec, which says that
302 * keywords are to be matched in this way even though non-keyword identifiers
303 * receive a different case-normalization mapping.
306 ScanKeywordLookup(char *text)
310 char word[NAMEDATALEN];
315 /* We assume all keywords are shorter than NAMEDATALEN. */
316 if (len >= NAMEDATALEN)
320 * Apply an ASCII-only downcasing. We must not use tolower() since it
321 * may produce the wrong translation in some locales (eg, Turkish),
322 * and we don't trust isupper() very much either. In an ASCII-based
323 * encoding the tests against A and Z are sufficient, but we also
324 * check isupper() so that we will work correctly under EBCDIC. The
325 * actual case conversion step should work for either ASCII or EBCDIC.
327 for (i = 0; i < len; i++)
331 if (ch >= 'A' && ch <= 'Z' && isupper((unsigned char) ch))
338 * Now do a binary search using plain strcmp() comparison.
340 low = &ScanKeywords[0];
341 high = endof(ScanKeywords) - 1;
347 middle = low + (high - low) / 2;
348 difference = strcmp(middle->name, word);
351 else if (difference < 0)