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.89 2001/02/21 18:53:46 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 {"backward", BACKWARD},
49 {"begin", BEGIN_TRANS},
61 {"character", CHARACTER},
62 {"characteristics", CHARACTERISTICS},
64 {"checkpoint", CHECKPOINT},
67 {"coalesce", COALESCE},
72 {"committed", COMMITTED},
73 {"constraint", CONSTRAINT},
74 {"constraints", CONSTRAINTS},
77 {"createdb", CREATEDB},
78 {"createuser", CREATEUSER},
80 {"current_date", CURRENT_DATE},
81 {"current_time", CURRENT_TIME},
82 {"current_timestamp", CURRENT_TIMESTAMP},
83 {"current_user", CURRENT_USER},
86 {"database", DATABASE},
92 {"deferrable", DEFERRABLE},
93 {"deferred", DEFERRED},
95 {"delimiters", DELIMITERS},
97 {"distinct", DISTINCT},
103 {"encoding", ENCODING},
107 {"exclusive", EXCLUSIVE},
108 {"execute", EXECUTE},
110 {"explain", EXPLAIN},
112 {"extract", EXTRACT},
118 {"foreign", FOREIGN},
119 {"forward", FORWARD},
122 {"function", FUNCTION},
126 {"handler", HANDLER},
130 {"immediate", IMMEDIATE},
132 {"increment", INCREMENT},
134 {"inherits", INHERITS},
135 {"initially", INITIALLY},
138 {"insensitive", INSENSITIVE},
140 {"instead", INSTEAD},
141 {"intersect", INTERSECT},
142 {"interval", INTERVAL},
146 {"isolation", ISOLATION},
149 {"lancompiler", LANCOMPILER},
150 {"language", LANGUAGE},
151 {"leading", LEADING},
159 {"location", LOCATION},
162 {"maxvalue", MAXVALUE},
163 {"minute", MINUTE_P},
164 {"minvalue", MINVALUE},
169 {"national", NATIONAL},
170 {"natural", NATURAL},
175 {"nocreatedb", NOCREATEDB},
176 {"nocreateuser", NOCREATEUSER},
179 {"nothing", NOTHING},
181 {"notnull", NOTNULL},
184 {"numeric", NUMERIC},
192 {"operator", OPERATOR},
198 {"overlaps", OVERLAPS},
200 {"partial", PARTIAL},
201 {"password", PASSWORD},
203 {"pendant", PENDANT},
204 {"position", POSITION},
205 {"precision", PRECISION},
206 {"primary", PRIMARY},
208 {"privileges", PRIVILEGES},
209 {"procedural", PROCEDURAL},
210 {"procedure", PROCEDURE},
213 {"references", REFERENCES},
214 {"reindex", REINDEX},
215 {"relative", RELATIVE},
218 {"restrict", RESTRICT},
219 {"returns", RETURNS},
222 {"rollback", ROLLBACK},
227 {"second", SECOND_P},
229 {"sequence", SEQUENCE},
231 {"serializable", SERIALIZABLE},
232 {"session", SESSION},
233 {"session_user", SESSION_USER},
240 {"statement", STATEMENT},
243 {"substring", SUBSTRING},
247 {"template", TEMPLATE},
248 {"temporary", TEMPORARY},
251 {"timestamp", TIMESTAMP},
252 {"timezone_hour", TIMEZONE_HOUR},
253 {"timezone_minute", TIMEZONE_MINUTE},
256 {"trailing", TRAILING},
257 {"transaction", TRANSACTION},
258 {"trigger", TRIGGER},
261 {"truncate", TRUNCATE},
262 {"trusted", TRUSTED},
266 {"unlisten", UNLISTEN},
274 {"varchar", VARCHAR},
275 {"varying", VARYING},
276 {"verbose", VERBOSE},
277 {"version", VERSION},
282 {"without", WITHOUT},
289 * ScanKeywordLookup - see if a given word is a keyword
291 * Returns a pointer to the ScanKeyword table entry, or NULL if no match.
293 * The match is done case-insensitively. Note that we deliberately use a
294 * dumbed-down case conversion that will only translate 'A'-'Z' into 'a'-'z',
295 * even if we are in a locale where tolower() would produce more or different
296 * translations. This is to conform to the SQL99 spec, which says that
297 * keywords are to be matched in this way even though non-keyword identifiers
298 * receive a different case-normalization mapping.
301 ScanKeywordLookup(char *text)
305 char word[NAMEDATALEN];
310 /* We assume all keywords are shorter than NAMEDATALEN. */
311 if (len >= NAMEDATALEN)
315 * Apply an ASCII-only downcasing. We must not use tolower() since
316 * it may produce the wrong translation in some locales (eg, Turkish),
317 * and we don't trust isupper() very much either. In an ASCII-based
318 * encoding the tests against A and Z are sufficient, but we also check
319 * isupper() so that we will work correctly under EBCDIC. The actual
320 * case conversion step should work for either ASCII or EBCDIC.
322 for (i = 0; i < len; i++)
326 if (ch >= 'A' && ch <= 'Z' && isupper((unsigned char) ch))
333 * Now do a binary search using plain strcmp() comparison.
335 low = &ScanKeywords[0];
336 high = endof(ScanKeywords) - 1;
342 middle = low + (high - low) / 2;
343 difference = strcmp(middle->name, word);
346 else if (difference < 0)