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.100 2002/02/18 23:11:18 petere 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},
257 {"trailing", TRAILING},
258 {"transaction", TRANSACTION},
259 {"trigger", TRIGGER},
262 {"truncate", TRUNCATE},
263 {"trusted", TRUSTED},
265 {"unencrypted", UNENCRYPTED},
268 {"unknown", UNKNOWN},
269 {"unlisten", UNLISTEN},
278 {"varchar", VARCHAR},
279 {"varying", VARYING},
280 {"verbose", VERBOSE},
281 {"version", VERSION},
286 {"without", WITHOUT},
293 * ScanKeywordLookup - see if a given word is a keyword
295 * Returns a pointer to the ScanKeyword table entry, or NULL if no match.
297 * The match is done case-insensitively. Note that we deliberately use a
298 * dumbed-down case conversion that will only translate 'A'-'Z' into 'a'-'z',
299 * even if we are in a locale where tolower() would produce more or different
300 * translations. This is to conform to the SQL99 spec, which says that
301 * keywords are to be matched in this way even though non-keyword identifiers
302 * receive a different case-normalization mapping.
305 ScanKeywordLookup(char *text)
309 char word[NAMEDATALEN];
314 /* We assume all keywords are shorter than NAMEDATALEN. */
315 if (len >= NAMEDATALEN)
319 * Apply an ASCII-only downcasing. We must not use tolower() since it
320 * may produce the wrong translation in some locales (eg, Turkish),
321 * and we don't trust isupper() very much either. In an ASCII-based
322 * encoding the tests against A and Z are sufficient, but we also
323 * check isupper() so that we will work correctly under EBCDIC. The
324 * actual case conversion step should work for either ASCII or EBCDIC.
326 for (i = 0; i < len; i++)
330 if (ch >= 'A' && ch <= 'Z' && isupper((unsigned char) ch))
337 * Now do a binary search using plain strcmp() comparison.
339 low = &ScanKeywords[0];
340 high = endof(ScanKeywords) - 1;
346 middle = low + (high - low) / 2;
347 difference = strcmp(middle->name, word);
350 else if (difference < 0)
361 * This does the reverse mapping from token number to string.
364 TokenString(int token)
367 static char buf[NAMEDATALEN];
369 while (i < sizeof(ScanKeywords))
371 if (ScanKeywords[i].value == token)
376 for (k = 0; k < NAMEDATALEN; k++)
377 if (ScanKeywords[i].name[k] >= 'a'
378 && ScanKeywords[i].name[k] <= 'z')
379 buf[k] = ScanKeywords[i].name[k] + ('A' - 'a');
381 buf[k] = ScanKeywords[i].name[k];