]> granicus.if.org Git - postgresql/blob - src/backend/parser/keywords.c
1365d2d66bbab00fb43f4a3a2a25f7846003631d
[postgresql] / src / backend / parser / keywords.c
1 /*-------------------------------------------------------------------------
2  *
3  * keywords.c--
4  *        lexical token lookup for reserved words in postgres SQL
5  *
6  * Copyright (c) 1994, Regents of the University of California
7  *
8  *
9  * IDENTIFICATION
10  *        $Header: /cvsroot/pgsql/src/backend/parser/keywords.c,v 1.47 1998/10/18 23:29:27 momjian Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14 #include <ctype.h>
15 #include <string.h>
16
17 #include "postgres.h"
18 #include "nodes/parsenodes.h"
19 #include "nodes/pg_list.h"
20 #include "parse.h"
21 #include "parser/keywords.h"
22 #include "utils/elog.h"
23
24 /*
25  * List of (keyword-name, keyword-token-value) pairs.
26  *
27  * !!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         {"add", ADD},
36         {"after", AFTER},
37         {"aggregate", AGGREGATE},
38         {"all", ALL},
39         {"alter", ALTER},
40         {"analyze", ANALYZE},
41         {"and", AND},
42         {"any", ANY},
43         {"as", AS},
44         {"asc", ASC},
45         {"backward", BACKWARD},
46         {"before", BEFORE},
47         {"begin", BEGIN_TRANS},
48         {"between", BETWEEN},
49         {"binary", BINARY},
50         {"both", BOTH},
51         {"by", BY},
52         {"cache", CACHE},
53         {"cascade", CASCADE},
54         {"cast", CAST},
55         {"char", CHAR},
56         {"character", CHARACTER},
57         {"check", CHECK},
58         {"close", CLOSE},
59         {"cluster", CLUSTER},
60         {"collate", COLLATE},
61         {"column", COLUMN},
62         {"commit", COMMIT},
63         {"constraint", CONSTRAINT},
64         {"copy", COPY},
65         {"create", CREATE},
66         {"createdb", CREATEDB},
67         {"createuser", CREATEUSER},
68         {"cross", CROSS},
69         {"current", CURRENT},           /* 6.4 to 6.5 is migration time! CURRENT will be removed in 6.5! Use OLD keyword in rules. Jan */
70         {"current_date", CURRENT_DATE},
71         {"current_time", CURRENT_TIME},
72         {"current_timestamp", CURRENT_TIMESTAMP},
73         {"current_user", CURRENT_USER},
74         {"cursor", CURSOR},
75         {"cycle", CYCLE},
76         {"database", DATABASE},
77         {"day", DAY_P},
78         {"decimal", DECIMAL},
79         {"declare", DECLARE},
80         {"default", DEFAULT},
81         {"delete", DELETE},
82         {"delimiters", DELIMITERS},
83         {"desc", DESC},
84         {"distinct", DISTINCT},
85         {"do", DO},
86         {"double", DOUBLE},
87         {"drop", DROP},
88         {"each", EACH},
89         {"encoding", ENCODING},
90         {"end", END_TRANS},
91         {"execute", EXECUTE},
92         {"exists", EXISTS},
93         {"explain", EXPLAIN},
94         {"extend", EXTEND},
95         {"extract", EXTRACT},
96         {"false", FALSE_P},
97         {"fetch", FETCH},
98         {"float", FLOAT},
99         {"for", FOR},
100         {"foreign", FOREIGN},
101         {"forward", FORWARD},
102         {"from", FROM},
103         {"full", FULL},
104         {"function", FUNCTION},
105         {"grant", GRANT},
106         {"group", GROUP},
107         {"handler", HANDLER},
108         {"having", HAVING},
109         {"hour", HOUR_P},
110         {"in", IN},
111         {"increment", INCREMENT},
112         {"index", INDEX},
113         {"inherits", INHERITS},
114         {"inner", INNER_P},
115         {"insensitive", INSENSITIVE},
116         {"insert", INSERT},
117         {"instead", INSTEAD},
118         {"interval", INTERVAL},
119         {"into", INTO},
120         {"is", IS},
121         {"isnull", ISNULL},
122         {"join", JOIN},
123         {"key", KEY},
124         {"lancompiler", LANCOMPILER},
125         {"language", LANGUAGE},
126         {"leading", LEADING},
127         {"left", LEFT},
128         {"like", LIKE},
129         {"listen", LISTEN},
130         {"load", LOAD},
131         {"local", LOCAL},
132         {"location", LOCATION},
133         {"lock", LOCK_P},
134         {"match", MATCH},
135         {"maxvalue", MAXVALUE},
136         {"minute", MINUTE_P},
137         {"minvalue", MINVALUE},
138         {"month", MONTH_P},
139         {"move", MOVE},
140         {"names", NAMES},
141         {"national", NATIONAL},
142         {"natural", NATURAL},
143         {"nchar", NCHAR},
144         {"new", NEW},
145         {"next", NEXT},
146         {"no", NO},
147         {"nocreatedb", NOCREATEDB},
148         {"nocreateuser", NOCREATEUSER},
149         {"none", NONE},
150         {"not", NOT},
151         {"nothing", NOTHING},
152         {"notify", NOTIFY},
153         {"notnull", NOTNULL},
154         {"null", NULL_P},
155         {"numeric", NUMERIC},
156         {"of", OF},
157         {"oids", OIDS},
158         {"old", CURRENT},
159         {"on", ON},
160         {"only", ONLY},
161         {"operator", OPERATOR},
162         {"option", OPTION},
163         {"or", OR},
164         {"order", ORDER},
165         {"outer", OUTER_P},
166         {"partial", PARTIAL},
167         {"password", PASSWORD},
168         {"position", POSITION},
169         {"precision", PRECISION},
170         {"primary", PRIMARY},
171         {"prior", PRIOR},
172         {"privileges", PRIVILEGES},
173         {"procedural", PROCEDURAL},
174         {"procedure", PROCEDURE},
175         {"public", PUBLIC},
176         {"read", READ},
177         {"recipe", RECIPE},
178         {"references", REFERENCES},
179         {"relative", RELATIVE},
180         {"rename", RENAME},
181         {"reset", RESET},
182         {"returns", RETURNS},
183         {"revoke", REVOKE},
184         {"right", RIGHT},
185         {"rollback", ROLLBACK},
186         {"row", ROW},
187         {"rule", RULE},
188         {"scroll", SCROLL},
189         {"second", SECOND_P},
190         {"select", SELECT},
191         {"sequence", SEQUENCE},
192         {"serial", SERIAL},
193         {"set", SET},
194         {"setof", SETOF},
195         {"show", SHOW},
196         {"start", START},
197         {"statement", STATEMENT},
198         {"stdin", STDIN},
199         {"stdout", STDOUT},
200         {"substring", SUBSTRING},
201         {"table", TABLE},
202         {"time", TIME},
203         {"timestamp", TIMESTAMP},
204         {"timezone_hour", TIMEZONE_HOUR},
205         {"timezone_minute", TIMEZONE_MINUTE},
206         {"to", TO},
207         {"trailing", TRAILING},
208         {"transaction", TRANSACTION},
209         {"trigger", TRIGGER},
210         {"trim", TRIM},
211         {"true", TRUE_P},
212         {"trusted", TRUSTED},
213         {"type", TYPE_P},
214         {"union", UNION},
215         {"unique", UNIQUE},
216         {"unlisten", UNLISTEN},
217         {"until", UNTIL},
218         {"update", UPDATE},
219         {"user", USER},
220         {"using", USING},
221         {"vacuum", VACUUM},
222         {"valid", VALID},
223         {"values", VALUES},
224         {"varchar", VARCHAR},
225         {"varying", VARYING},
226         {"verbose", VERBOSE},
227         {"version", VERSION},
228         {"view", VIEW},
229         {"where", WHERE},
230         {"with", WITH},
231         {"work", WORK},
232         {"year", YEAR_P},
233         {"zone", ZONE},
234 };
235
236 ScanKeyword *
237 ScanKeywordLookup(char *text)
238 {
239         ScanKeyword *low = &ScanKeywords[0];
240         ScanKeyword *high = endof(ScanKeywords) - 1;
241         ScanKeyword *middle;
242         int                     difference;
243
244         while (low <= high)
245         {
246                 middle = low + (high - low) / 2;
247                 difference = strcmp(middle->name, text);
248                 if (difference == 0)
249                         return middle;
250                 else if (difference < 0)
251                         low = middle + 1;
252                 else
253                         high = middle - 1;
254         }
255
256         return NULL;
257 }