keywords.c 5.37 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
10
 *	  $Header: /cvsroot/pgsql/src/backend/parser/keywords.c,v 1.28 1997/12/16 05:04:00 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 32 33
	/* name					value			*/
	{"abort", ABORT_TRANS},
	{"acl", ACL},
34
	{"action", ACTION},
35 36 37 38 39 40 41 42
	{"add", ADD},
	{"after", AFTER},
	{"aggregate", AGGREGATE},
	{"all", ALL},
	{"alter", ALTER},
	{"analyze", ANALYZE},
	{"and", AND},
	{"append", APPEND},
Bruce Momjian's avatar
Bruce Momjian committed
43
	{"archive", ARCHIVE},
44 45 46 47 48 49 50 51 52
	{"as", AS},
	{"asc", ASC},
	{"backward", BACKWARD},
	{"before", BEFORE},
	{"begin", BEGIN_TRANS},
	{"between", BETWEEN},
	{"binary", BINARY},
	{"both", BOTH},
	{"by", BY},
53
	{"cascade", CASCADE},
54 55
	{"cast", CAST},
	{"change", CHANGE},
56
	{"char", CHAR},
57
	{"character", CHARACTER},
58 59 60
	{"check", CHECK},
	{"close", CLOSE},
	{"cluster", CLUSTER},
61
	{"collate", COLLATE},
62 63 64 65 66
	{"column", COLUMN},
	{"commit", COMMIT},
	{"constraint", CONSTRAINT},
	{"copy", COPY},
	{"create", CREATE},
67 68
	{"createdb", CREATEDB},
	{"createuser", CREATEUSER},
69 70
	{"cross", CROSS},
	{"current", CURRENT},
71 72 73
	{"current_date", CURRENT_DATE},
	{"current_time", CURRENT_TIME},
	{"current_timestamp", CURRENT_TIMESTAMP},
74
	{"current_user", CURRENT_USER},
75 76
	{"cursor", CURSOR},
	{"database", DATABASE},
77
	{"day", DAY_P},
78
	{"decimal", DECIMAL},
79 80 81 82 83 84 85
	{"declare", DECLARE},
	{"default", DEFAULT},
	{"delete", DELETE},
	{"delimiters", DELIMITERS},
	{"desc", DESC},
	{"distinct", DISTINCT},
	{"do", DO},
86
	{"double", DOUBLE},
87 88 89 90 91 92 93
	{"drop", DROP},
	{"end", END_TRANS},
	{"execute", EXECUTE},
	{"exists", EXISTS},
	{"explain", EXPLAIN},
	{"extend", EXTEND},
	{"extract", EXTRACT},
94
	{"false", FALSE_P},
95
	{"fetch", FETCH},
96
	{"float", FLOAT},
97
	{"for", FOR},
98
	{"foreign", FOREIGN},
99 100 101 102 103 104
	{"forward", FORWARD},
	{"from", FROM},
	{"full", FULL},
	{"function", FUNCTION},
	{"grant", GRANT},
	{"group", GROUP},
105
	{"handler", HANDLER},
106
	{"having", HAVING},
107
	{"hour", HOUR_P},
108 109 110
	{"in", IN},
	{"index", INDEX},
	{"inherits", INHERITS},
111
	{"inner", INNER_P},
112 113 114 115 116 117 118
	{"insert", INSERT},
	{"instead", INSTEAD},
	{"interval", INTERVAL},
	{"into", INTO},
	{"is", IS},
	{"isnull", ISNULL},
	{"join", JOIN},
119
	{"key", KEY},
120
	{"lancompiler", LANCOMPILER},
121 122 123 124 125 126 127
	{"language", LANGUAGE},
	{"leading", LEADING},
	{"left", LEFT},
	{"like", LIKE},
	{"listen", LISTEN},
	{"load", LOAD},
	{"local", LOCAL},
128
	{"location", LOCATION},
129
	{"match", MATCH},
130
	{"merge", MERGE},
131 132
	{"minute", MINUTE_P},
	{"month", MONTH_P},
133
	{"move", MOVE},
134
	{"national", NATIONAL},
135
	{"natural", NATURAL},
136
	{"nchar", NCHAR},
137
	{"new", NEW},
138
	{"no", NO},
139 140
	{"nocreatedb", NOCREATEDB},
	{"nocreateuser", NOCREATEUSER},
141 142 143 144 145
	{"none", NONE},
	{"not", NOT},
	{"nothing", NOTHING},
	{"notify", NOTIFY},
	{"notnull", NOTNULL},
146
	{"null", NULL_P},
147
	{"numeric", NUMERIC},
148 149 150 151 152 153
	{"oids", OIDS},
	{"on", ON},
	{"operator", OPERATOR},
	{"option", OPTION},
	{"or", OR},
	{"order", ORDER},
154 155
	{"outer", OUTER_P},
	{"partial", PARTIAL},
156
	{"password", PASSWORD},
157
	{"position", POSITION},
158
	{"precision", PRECISION},
159
	{"primary", PRIMARY},
160
	{"privileges", PRIVILEGES},
161
	{"procedural", PROCEDURAL},
162 163 164
	{"procedure", PROCEDURE},
	{"public", PUBLIC},
	{"recipe", RECIPE},
165
	{"references", REFERENCES},
166 167 168 169 170 171 172 173 174
	{"rename", RENAME},
	{"replace", REPLACE},
	{"reset", RESET},
	{"retrieve", RETRIEVE},
	{"returns", RETURNS},
	{"revoke", REVOKE},
	{"right", RIGHT},
	{"rollback", ROLLBACK},
	{"rule", RULE},
175
	{"second", SECOND_P},
176 177 178 179 180 181 182 183 184 185 186 187
	{"select", SELECT},
	{"sequence", SEQUENCE},
	{"set", SET},
	{"setof", SETOF},
	{"show", SHOW},
	{"stdin", STDIN},
	{"stdout", STDOUT},
	{"substring", SUBSTRING},
	{"table", TABLE},
	{"time", TIME},
	{"to", TO},
	{"trailing", TRAILING},
188
	{"transaction", TRANSACTION},
189 190
	{"trigger", TRIGGER},
	{"trim", TRIM},
191
	{"true", TRUE_P},
192
	{"trusted", TRUSTED},
193
	{"type", TYPE_P},
194 195
	{"union", UNION},
	{"unique", UNIQUE},
196
	{"until", UNTIL},
197
	{"update", UPDATE},
198
	{"user", USER},
199 200
	{"using", USING},
	{"vacuum", VACUUM},
201
	{"valid", VALID},
202
	{"values", VALUES},
203
	{"varchar", VARCHAR},
204
	{"varying", VARYING},
205 206 207 208 209 210
	{"verbose", VERBOSE},
	{"version", VERSION},
	{"view", VIEW},
	{"where", WHERE},
	{"with", WITH},
	{"work", WORK},
211
	{"year", YEAR_P},
212
	{"zone", ZONE},
213 214
};

215
ScanKeyword *
216 217
ScanKeywordLookup(char *text)
{
218 219 220 221
	ScanKeyword *low = &ScanKeywords[0];
	ScanKeyword *high = endof(ScanKeywords) - 1;
	ScanKeyword *middle;
	int			difference;
222 223 224 225 226 227 228 229 230 231 232 233 234 235

	while (low <= high)
	{
		middle = low + (high - low) / 2;
		difference = strcmp(middle->name, text);
		if (difference == 0)
			return (middle);
		else if (difference < 0)
			low = middle + 1;
		else
			high = middle - 1;
	}

	return (NULL);
236 237
}

238
#ifdef NOT_USED
239
char	   *
240 241
AtomValueGetString(int atomval)
{
242 243 244 245
	ScanKeyword *low = &ScanKeywords[0];
	ScanKeyword *high = endof(ScanKeywords) - 1;
	int			keyword_list_length = (high - low);
	int			i;
246 247 248 249 250 251 252

	for (i = 0; i < keyword_list_length; i++)
		if (ScanKeywords[i].value == atomval)
			return (ScanKeywords[i].name);

	elog(WARN, "AtomGetString called with bogus atom # : %d", atomval);
	return (NULL);
253
}
254

255
#endif