~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/interfaces/ecpg/preproc/keywords.c

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
 *
 
3
 * keywords.c
 
4
 *        lexical token lookup for reserved words in PostgreSQL
 
5
 *
 
6
 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
 
7
 * Portions Copyright (c) 1994, Regents of the University of California
 
8
 *
 
9
 *
 
10
 * IDENTIFICATION
 
11
 *        $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/keywords.c,v 1.68 2004-12-31 22:03:48 pgsql Exp $
 
12
 *
 
13
 *-------------------------------------------------------------------------
 
14
 */
 
15
#include "postgres_fe.h"
 
16
 
 
17
#include <ctype.h>
 
18
 
 
19
#include "extern.h"
 
20
#include "preproc.h"
 
21
 
 
22
 
 
23
/*
 
24
 * List of (keyword-name, keyword-token-value) pairs.
 
25
 *
 
26
 * !!WARNING!!: This list must be sorted, because binary
 
27
 *               search is used to locate entries.
 
28
 */
 
29
static ScanKeyword ScanKeywords[] = {
 
30
        /* name, value */
 
31
        {"abort", ABORT_P},
 
32
        {"absolute", ABSOLUTE_P},
 
33
        {"access", ACCESS},
 
34
        {"action", ACTION},
 
35
        {"add", ADD},
 
36
        {"after", AFTER},
 
37
        {"aggregate", AGGREGATE},
 
38
        {"all", ALL},
 
39
        {"also", ALSO},
 
40
        {"alter", ALTER},
 
41
        {"analyse", ANALYSE},           /* British spelling */
 
42
        {"analyze", ANALYZE},
 
43
        {"and", AND},
 
44
        {"any", ANY},
 
45
                {"array", ARRAY},
 
46
                {"as", AS},
 
47
                {"asc", ASC},
 
48
                {"assertion", ASSERTION},
 
49
                {"assignment", ASSIGNMENT},
 
50
                {"at", AT},
 
51
                {"authorization", AUTHORIZATION},
 
52
                {"backward", BACKWARD},
 
53
                {"before", BEFORE},
 
54
                {"begin", BEGIN_P},
 
55
                {"between", BETWEEN},
 
56
                {"bigint", BIGINT},
 
57
                {"binary", BINARY},
 
58
                {"bit", BIT},
 
59
                {"boolean", BOOLEAN_P},
 
60
                {"both", BOTH},
 
61
                {"by", BY},
 
62
                {"cache", CACHE},
 
63
                {"called", CALLED},
 
64
                {"cascade", CASCADE},
 
65
                {"case", CASE},
 
66
                {"cast", CAST},
 
67
                {"chain", CHAIN},
 
68
                {"char", CHAR_P},
 
69
                {"character", CHARACTER},
 
70
                {"characteristics", CHARACTERISTICS},
 
71
                {"check", CHECK},
 
72
                {"checkpoint", CHECKPOINT},
 
73
                {"class", CLASS},
 
74
                {"close", CLOSE},
 
75
                {"cluster", CLUSTER},
 
76
                {"coalesce", COALESCE},
 
77
                {"collate", COLLATE},
 
78
                {"column", COLUMN},
 
79
                {"comment", COMMENT},
 
80
                {"commit", COMMIT},
 
81
                {"committed", COMMITTED},
 
82
                {"constraint", CONSTRAINT},
 
83
                {"constraints", CONSTRAINTS},
 
84
                {"conversion", CONVERSION_P},
 
85
                {"convert", CONVERT},
 
86
                {"copy", COPY},
 
87
                {"create", CREATE},
 
88
                {"createdb", CREATEDB},
 
89
                {"createuser", CREATEUSER},
 
90
                {"cross", CROSS},
 
91
                {"csv", CSV},
 
92
                {"current_date", CURRENT_DATE},
 
93
                {"current_time", CURRENT_TIME},
 
94
                {"current_timestamp", CURRENT_TIMESTAMP},
 
95
                {"current_user", CURRENT_USER},
 
96
                {"cursor", CURSOR},
 
97
                {"cycle", CYCLE},
 
98
                {"database", DATABASE},
 
99
                {"day", DAY_P},
 
100
                {"deallocate", DEALLOCATE},
 
101
                {"dec", DEC},
 
102
                {"decimal", DECIMAL_P},
 
103
                {"declare", DECLARE},
 
104
                {"default", DEFAULT},
 
105
                {"defaults", DEFAULTS},
 
106
                {"deferrable", DEFERRABLE},
 
107
                {"deferred", DEFERRED},
 
108
                {"definer", DEFINER},
 
109
                {"delete", DELETE_P},
 
110
                {"delimiter", DELIMITER},
 
111
                {"delimiters", DELIMITERS},
 
112
                {"desc", DESC},
 
113
                {"distinct", DISTINCT},
 
114
                {"do", DO},
 
115
                {"domain", DOMAIN_P},
 
116
                {"double", DOUBLE_P},
 
117
                {"drop", DROP},
 
118
                {"each", EACH},
 
119
                {"else", ELSE},
 
120
                {"encoding", ENCODING},
 
121
                {"encrypted", ENCRYPTED},
 
122
                {"end", END_P},
 
123
                {"escape", ESCAPE},
 
124
                {"except", EXCEPT},
 
125
                {"excluding", EXCLUDING},
 
126
                {"exclusive", EXCLUSIVE},
 
127
                {"execute", EXECUTE},
 
128
                {"exists", EXISTS},
 
129
                {"explain", EXPLAIN},
 
130
                {"external", EXTERNAL},
 
131
                {"extract", EXTRACT},
 
132
                {"false", FALSE_P},
 
133
                {"fetch", FETCH},
 
134
                {"first", FIRST_P},
 
135
                {"float", FLOAT_P},
 
136
                {"for", FOR},
 
137
                {"force", FORCE},
 
138
                {"foreign", FOREIGN},
 
139
                {"forward", FORWARD},
 
140
                {"freeze", FREEZE},
 
141
                {"from", FROM},
 
142
                {"full", FULL},
 
143
                {"function", FUNCTION},
 
144
                {"get", GET},
 
145
                {"global", GLOBAL},
 
146
                {"grant", GRANT},
 
147
                {"group", GROUP_P},
 
148
                {"handler", HANDLER},
 
149
                {"having", HAVING},
 
150
                {"hold", HOLD},
 
151
                {"hour", HOUR_P},
 
152
                {"ilike", ILIKE},
 
153
                {"immediate", IMMEDIATE},
 
154
                {"immutable", IMMUTABLE},
 
155
                {"implicit", IMPLICIT_P},
 
156
                {"in", IN_P},
 
157
                {"including", INCLUDING},
 
158
                {"increment", INCREMENT},
 
159
                {"index", INDEX},
 
160
                {"inherits", INHERITS},
 
161
                {"initially", INITIALLY},
 
162
                {"inner", INNER_P},
 
163
                {"inout", INOUT},
 
164
                {"input", INPUT_P},
 
165
                {"insensitive", INSENSITIVE},
 
166
                {"insert", INSERT},
 
167
                {"instead", INSTEAD},
 
168
                {"int", INT_P},
 
169
                {"integer", INTEGER},
 
170
                {"intersect", INTERSECT},
 
171
                {"interval", INTERVAL},
 
172
                {"into", INTO},
 
173
                {"invoker", INVOKER},
 
174
                {"is", IS},
 
175
                {"isnull", ISNULL},
 
176
                {"isolation", ISOLATION},
 
177
                {"join", JOIN},
 
178
                {"key", KEY},
 
179
                {"lancompiler", LANCOMPILER},
 
180
                {"language", LANGUAGE},
 
181
                {"large", LARGE_P},
 
182
                {"last", LAST_P},
 
183
                {"leading", LEADING},
 
184
                {"left", LEFT},
 
185
                {"level", LEVEL},
 
186
                {"like", LIKE},
 
187
                {"limit", LIMIT},
 
188
                {"listen", LISTEN},
 
189
                {"load", LOAD},
 
190
                {"local", LOCAL},
 
191
                {"location", LOCATION},
 
192
                {"lock", LOCK_P},
 
193
                {"match", MATCH},
 
194
                {"maxvalue", MAXVALUE},
 
195
                {"minute", MINUTE_P},
 
196
                {"minvalue", MINVALUE},
 
197
                {"mode", MODE},
 
198
                {"month", MONTH_P},
 
199
                {"move", MOVE},
 
200
                {"names", NAMES},
 
201
                {"national", NATIONAL},
 
202
                {"natural", NATURAL},
 
203
                {"nchar", NCHAR},
 
204
                {"new", NEW},
 
205
                {"next", NEXT},
 
206
                {"no", NO},
 
207
                {"nocreatedb", NOCREATEDB},
 
208
                {"nocreateuser", NOCREATEUSER},
 
209
                {"none", NONE},
 
210
                {"not", NOT},
 
211
                {"nothing", NOTHING},
 
212
                {"notify", NOTIFY},
 
213
                {"notnull", NOTNULL},
 
214
                {"nowait", NOWAIT},
 
215
                {"null", NULL_P},
 
216
                {"nullif", NULLIF},
 
217
                {"numeric", NUMERIC},
 
218
                {"object", OBJECT_P},
 
219
                {"of", OF},
 
220
                {"off", OFF},
 
221
                {"offset", OFFSET},
 
222
                {"oids", OIDS},
 
223
                {"old", OLD},
 
224
                {"on", ON},
 
225
                {"only", ONLY},
 
226
                {"operator", OPERATOR},
 
227
                {"option", OPTION},
 
228
                {"or", OR},
 
229
                {"order", ORDER},
 
230
                {"out", OUT_P},
 
231
                {"outer", OUTER_P},
 
232
                {"overlaps", OVERLAPS},
 
233
                {"owner", OWNER},
 
234
                {"partial", PARTIAL},
 
235
                {"password", PASSWORD},
 
236
                {"position", POSITION},
 
237
                {"precision", PRECISION},
 
238
                {"prepare", PREPARE},
 
239
                {"preserve", PRESERVE},
 
240
                {"primary", PRIMARY},
 
241
                {"prior", PRIOR},
 
242
                {"privileges", PRIVILEGES},
 
243
                {"procedural", PROCEDURAL},
 
244
                {"procedure", PROCEDURE},
 
245
                {"quote", QUOTE},
 
246
                {"read", READ},
 
247
                {"real", REAL},
 
248
                {"recheck", RECHECK},
 
249
                {"references", REFERENCES},
 
250
                {"reindex", REINDEX},
 
251
                {"relative", RELATIVE_P},
 
252
                {"release", RELEASE},
 
253
                {"rename", RENAME},
 
254
                {"repeatable", REPEATABLE},
 
255
                {"replace", REPLACE},
 
256
                {"reset", RESET},
 
257
                {"restart", RESTART},
 
258
                {"restrict", RESTRICT},
 
259
                {"returns", RETURNS},
 
260
                {"revoke", REVOKE},
 
261
                {"right", RIGHT},
 
262
                {"rollback", ROLLBACK},
 
263
                {"row", ROW},
 
264
                {"rows", ROWS},
 
265
                {"rule", RULE},
 
266
                {"savepoint", SAVEPOINT},
 
267
                {"schema", SCHEMA},
 
268
                {"scroll", SCROLL},
 
269
                {"second", SECOND_P},
 
270
                {"security", SECURITY},
 
271
                {"select", SELECT},
 
272
                {"sequence", SEQUENCE},
 
273
                {"serializable", SERIALIZABLE},
 
274
                {"session", SESSION},
 
275
                {"session_user", SESSION_USER},
 
276
                {"set", SET},
 
277
                {"setof", SETOF},
 
278
                {"share", SHARE},
 
279
                {"show", SHOW},
 
280
                {"similar", SIMILAR},
 
281
                {"simple", SIMPLE},
 
282
                {"smallint", SMALLINT},
 
283
                {"some", SOME},
 
284
                {"stable", STABLE},
 
285
                {"start", START},
 
286
                {"statement", STATEMENT},
 
287
                {"statistics", STATISTICS},
 
288
                {"stdin", STDIN},
 
289
                {"stdout", STDOUT},
 
290
                {"storage", STORAGE},
 
291
                {"strict", STRICT_P},
 
292
                {"substring", SUBSTRING},
 
293
                {"sysid", SYSID},
 
294
                {"table", TABLE},
 
295
                {"tablespace", TABLESPACE},
 
296
                {"temp", TEMP},
 
297
                {"template", TEMPLATE},
 
298
                {"temporary", TEMPORARY},
 
299
                {"then", THEN},
 
300
                {"time", TIME},
 
301
                {"timestamp", TIMESTAMP},
 
302
                {"to", TO},
 
303
                {"toast", TOAST},
 
304
                {"trailing", TRAILING},
 
305
                {"transaction", TRANSACTION},
 
306
                {"treat", TREAT},
 
307
                {"trigger", TRIGGER},
 
308
                {"trim", TRIM},
 
309
                {"true", TRUE_P},
 
310
                {"truncate", TRUNCATE},
 
311
                {"trusted", TRUSTED},
 
312
                {"type", TYPE_P},
 
313
                {"uncommitted", UNCOMMITTED},
 
314
                {"unencrypted", UNENCRYPTED},
 
315
                {"union", UNION},
 
316
                {"unique", UNIQUE},
 
317
                {"unknown", UNKNOWN},
 
318
                {"unlisten", UNLISTEN},
 
319
                {"until", UNTIL},
 
320
                {"update", UPDATE},
 
321
                {"usage", USAGE},
 
322
                {"user", USER},
 
323
                {"using", USING},
 
324
                {"vacuum", VACUUM},
 
325
                {"valid", VALID},
 
326
                {"values", VALUES},
 
327
                {"varchar", VARCHAR},
 
328
                {"varying", VARYING},
 
329
                {"verbose", VERBOSE},
 
330
                {"view", VIEW},
 
331
                {"volatile", VOLATILE},
 
332
                {"when", WHEN},
 
333
                {"where", WHERE},
 
334
                {"with", WITH},
 
335
                {"without", WITHOUT},
 
336
                {"work", WORK},
 
337
                {"write", WRITE},
 
338
                {"year", YEAR_P},
 
339
                {"zone", ZONE},
 
340
        };
 
341
 
 
342
/*
 
343
 * ScanKeywordLookup - see if a given word is a keyword
 
344
 *
 
345
 * Returns a pointer to the ScanKeyword table entry, or NULL if no match.
 
346
 *
 
347
 * The match is done case-insensitively.  Note that we deliberately use a
 
348
 * dumbed-down case conversion that will only translate 'A'-'Z' into 'a'-'z',
 
349
 * even if we are in a locale where tolower() would produce more or different
 
350
 * translations.  This is to conform to the SQL99 spec, which says that
 
351
 * keywords are to be matched in this way even though non-keyword identifiers
 
352
 * receive a different case-normalization mapping.
 
353
 */
 
354
        ScanKeyword *
 
355
                                ScanKeywordLookup(char *text)
 
356
        {
 
357
                int                     len,
 
358
                                        i;
 
359
                char            word[NAMEDATALEN];
 
360
                ScanKeyword *low;
 
361
                ScanKeyword *high;
 
362
 
 
363
                len = strlen(text);
 
364
                /* We assume all keywords are shorter than NAMEDATALEN. */
 
365
                if (len >= NAMEDATALEN)
 
366
                        return NULL;
 
367
 
 
368
                /*
 
369
                 * Apply an ASCII-only downcasing.      We must not use tolower()
 
370
                 * since it may produce the wrong translation in some locales (eg,
 
371
                 * Turkish).
 
372
                 */
 
373
                for (i = 0; i < len; i++)
 
374
                {
 
375
                        char            ch = text[i];
 
376
 
 
377
                        if (ch >= 'A' && ch <= 'Z')
 
378
                                ch += 'a' - 'A';
 
379
                        word[i] = ch;
 
380
                }
 
381
                word[len] = '\0';
 
382
 
 
383
                /*
 
384
                 * Now do a binary search using plain strcmp() comparison.
 
385
                 */
 
386
                low = &ScanKeywords[0];
 
387
                high = endof(ScanKeywords) - 1;
 
388
                while (low <= high)
 
389
                {
 
390
                        ScanKeyword *middle;
 
391
                        int                     difference;
 
392
 
 
393
                        middle = low + (high - low) / 2;
 
394
                        difference = strcmp(middle->name, word);
 
395
                        if (difference == 0)
 
396
                                return middle;
 
397
                        else if (difference < 0)
 
398
                                low = middle + 1;
 
399
                        else
 
400
                                high = middle - 1;
 
401
                }
 
402
 
 
403
                return NULL;
 
404
        }