keywords.c 5.77 KB
Newer Older
1 2 3
/*-------------------------------------------------------------------------
 *
 * keywords.c--
4
 *	  lexical token lookup for reserved words in postgres SQL
5 6 7 8 9
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
Bruce Momjian's avatar
Hi!  
Bruce Momjian committed
10
 *	  $Header: /cvsroot/pgsql/src/backend/parser/keywords.c,v 1.51 1999/01/18 00:09:53 momjian Exp $
11 12 13 14 15 16
 *
 *-------------------------------------------------------------------------
 */
#include <ctype.h>
#include <string.h>

Bruce Momjian's avatar
Bruce Momjian committed
17
#include "postgres.h"
18
#include "nodes/parsenodes.h"
Bruce Momjian's avatar
Bruce Momjian committed
19
#include "nodes/pg_list.h"
20 21
#include "parse.h"
#include "parser/keywords.h"
22
#include "utils/elog.h"
23 24 25 26 27

/*
 * List of (keyword-name, keyword-token-value) pairs.
 *
 * !!WARNING!!: This list must be sorted, because binary
28
 *		 search is used to locate entries.
29 30
 */
static ScanKeyword ScanKeywords[] = {
31
	/* name, value */
32
	{"abort", ABORT_TRANS},
33
	{"absolute", ABSOLUTE},
34
	{"action", ACTION},
35 36 37 38 39 40 41
	{"add", ADD},
	{"after", AFTER},
	{"aggregate", AGGREGATE},
	{"all", ALL},
	{"alter", ALTER},
	{"analyze", ANALYZE},
	{"and", AND},
42
	{"any", ANY},
43 44 45 46 47 48 49 50 51
	{"as", AS},
	{"asc", ASC},
	{"backward", BACKWARD},
	{"before", BEFORE},
	{"begin", BEGIN_TRANS},
	{"between", BETWEEN},
	{"binary", BINARY},
	{"both", BOTH},
	{"by", BY},
52
	{"cache", CACHE},
53
	{"cascade", CASCADE},
54
	{"case", CASE},
55
	{"cast", CAST},
56
	{"char", CHAR},
57
	{"character", CHARACTER},
58 59 60
	{"check", CHECK},
	{"close", CLOSE},
	{"cluster", CLUSTER},
61
	{"coalesce", COALESCE},
62
	{"collate", COLLATE},
63 64 65 66 67
	{"column", COLUMN},
	{"commit", COMMIT},
	{"constraint", CONSTRAINT},
	{"copy", COPY},
	{"create", CREATE},
68 69
	{"createdb", CREATEDB},
	{"createuser", CREATEUSER},
70
	{"cross", CROSS},
Bruce Momjian's avatar
Bruce Momjian committed
71 72 73
	{"current", CURRENT},		/* 6.4 to 6.5 is migration time! CURRENT
								 * will be removed in 6.5! Use OLD keyword
								 * in rules. Jan */
74 75 76
	{"current_date", CURRENT_DATE},
	{"current_time", CURRENT_TIME},
	{"current_timestamp", CURRENT_TIMESTAMP},
77
	{"current_user", CURRENT_USER},
78
	{"cursor", CURSOR},
79
	{"cycle", CYCLE},
80
	{"database", DATABASE},
81
	{"day", DAY_P},
82
	{"decimal", DECIMAL},
83 84 85 86 87 88 89
	{"declare", DECLARE},
	{"default", DEFAULT},
	{"delete", DELETE},
	{"delimiters", DELIMITERS},
	{"desc", DESC},
	{"distinct", DISTINCT},
	{"do", DO},
90
	{"double", DOUBLE},
91
	{"drop", DROP},
92
	{"each", EACH},
93
	{"else", ELSE},
94
	{"encoding", ENCODING},
95
	{"end", END_TRANS},
Bruce Momjian's avatar
Hi!  
Bruce Momjian committed
96 97 98
	/***S*I***/
	{"except", EXCEPT},

99 100 101 102 103
	{"execute", EXECUTE},
	{"exists", EXISTS},
	{"explain", EXPLAIN},
	{"extend", EXTEND},
	{"extract", EXTRACT},
104
	{"false", FALSE_P},
105
	{"fetch", FETCH},
106
	{"float", FLOAT},
107
	{"for", FOR},
108
	{"foreign", FOREIGN},
109 110 111 112 113 114
	{"forward", FORWARD},
	{"from", FROM},
	{"full", FULL},
	{"function", FUNCTION},
	{"grant", GRANT},
	{"group", GROUP},
115
	{"handler", HANDLER},
116
	{"having", HAVING},
117
	{"hour", HOUR_P},
118
	{"in", IN},
119
	{"increment", INCREMENT},
120 121
	{"index", INDEX},
	{"inherits", INHERITS},
122
	{"inner", INNER_P},
123
	{"insensitive", INSENSITIVE},
Bruce Momjian's avatar
Bruce Momjian committed
124
	{"insert", INSERT},
125
	{"instead", INSTEAD},
Bruce Momjian's avatar
Hi!  
Bruce Momjian committed
126 127 128
	/***S*I***/
	{"intersect", INTERSECT},

129 130 131 132
	{"interval", INTERVAL},
	{"into", INTO},
	{"is", IS},
	{"isnull", ISNULL},
133
	{"isolation", ISOLATION},
134
	{"join", JOIN},
135
	{"key", KEY},
136
	{"lancompiler", LANCOMPILER},
137 138 139
	{"language", LANGUAGE},
	{"leading", LEADING},
	{"left", LEFT},
140
	{"level", LEVEL},
141 142 143 144
	{"like", LIKE},
	{"listen", LISTEN},
	{"load", LOAD},
	{"local", LOCAL},
145
	{"location", LOCATION},
146
	{"lock", LOCK_P},
147
	{"match", MATCH},
148
	{"maxvalue", MAXVALUE},
149
	{"minute", MINUTE_P},
150
	{"minvalue", MINVALUE},
151
	{"month", MONTH_P},
152
	{"move", MOVE},
153
	{"names", NAMES},
154
	{"national", NATIONAL},
155
	{"natural", NATURAL},
156
	{"nchar", NCHAR},
157
	{"new", NEW},
158
	{"next", NEXT},
159
	{"no", NO},
160 161
	{"nocreatedb", NOCREATEDB},
	{"nocreateuser", NOCREATEUSER},
162 163 164 165 166
	{"none", NONE},
	{"not", NOT},
	{"nothing", NOTHING},
	{"notify", NOTIFY},
	{"notnull", NOTNULL},
167
	{"null", NULL_P},
168
	{"nullif", NULLIF},
169
	{"numeric", NUMERIC},
170
	{"of", OF},
171
	{"oids", OIDS},
172
	{"old", CURRENT},
173
	{"on", ON},
174
	{"only", ONLY},
175 176 177 178
	{"operator", OPERATOR},
	{"option", OPTION},
	{"or", OR},
	{"order", ORDER},
179 180
	{"outer", OUTER_P},
	{"partial", PARTIAL},
181
	{"password", PASSWORD},
182
	{"position", POSITION},
183
	{"precision", PRECISION},
184
	{"primary", PRIMARY},
185
	{"prior", PRIOR},
186
	{"privileges", PRIVILEGES},
187
	{"procedural", PROCEDURAL},
188 189
	{"procedure", PROCEDURE},
	{"public", PUBLIC},
190
	{"read", READ},
191
	{"recipe", RECIPE},
192
	{"references", REFERENCES},
193
	{"relative", RELATIVE},
194 195 196 197 198 199
	{"rename", RENAME},
	{"reset", RESET},
	{"returns", RETURNS},
	{"revoke", REVOKE},
	{"right", RIGHT},
	{"rollback", ROLLBACK},
200
	{"row", ROW},
201
	{"rule", RULE},
202
	{"scroll", SCROLL},
203
	{"second", SECOND_P},
204 205
	{"select", SELECT},
	{"sequence", SEQUENCE},
206
	{"serial", SERIAL},
207 208 209
	{"set", SET},
	{"setof", SETOF},
	{"show", SHOW},
210
	{"start", START},
211
	{"statement", STATEMENT},
212 213 214 215
	{"stdin", STDIN},
	{"stdout", STDOUT},
	{"substring", SUBSTRING},
	{"table", TABLE},
216
	{"then", THEN},
217
	{"time", TIME},
218
	{"timestamp", TIMESTAMP},
219 220
	{"timezone_hour", TIMEZONE_HOUR},
	{"timezone_minute", TIMEZONE_MINUTE},
221 222
	{"to", TO},
	{"trailing", TRAILING},
223
	{"transaction", TRANSACTION},
224 225
	{"trigger", TRIGGER},
	{"trim", TRIM},
226
	{"true", TRUE_P},
227
	{"trusted", TRUSTED},
228
	{"type", TYPE_P},
229 230
	{"union", UNION},
	{"unique", UNIQUE},
231
	{"unlisten", UNLISTEN},
232
	{"until", UNTIL},
233
	{"update", UPDATE},
234
	{"user", USER},
235 236
	{"using", USING},
	{"vacuum", VACUUM},
237
	{"valid", VALID},
238
	{"values", VALUES},
239
	{"varchar", VARCHAR},
240
	{"varying", VARYING},
241 242 243
	{"verbose", VERBOSE},
	{"version", VERSION},
	{"view", VIEW},
244
	{"when", WHEN},
245 246 247
	{"where", WHERE},
	{"with", WITH},
	{"work", WORK},
248
	{"year", YEAR_P},
249
	{"zone", ZONE},
250 251
};

252
ScanKeyword *
253 254
ScanKeywordLookup(char *text)
{
255 256 257 258
	ScanKeyword *low = &ScanKeywords[0];
	ScanKeyword *high = endof(ScanKeywords) - 1;
	ScanKeyword *middle;
	int			difference;
259 260 261 262 263 264

	while (low <= high)
	{
		middle = low + (high - low) / 2;
		difference = strcmp(middle->name, text);
		if (difference == 0)
265
			return middle;
266 267 268 269 270 271
		else if (difference < 0)
			low = middle + 1;
		else
			high = middle - 1;
	}

272
	return NULL;
273
}