blob: 24433a07faa82e1e0b7364e1622c40914d92d711 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
12** An tokenizer for SQL
13**
14** This file contains C code that splits an SQL input string up into
15** individual tokens and sends those tokens one-by-one over to the
16** parser for analysis.
17**
drh7c972de2003-09-06 22:18:07 +000018** $Id: tokenize.c,v 1.62 2003/09/06 22:18:08 drh Exp $
drh75897232000-05-29 14:26:00 +000019*/
20#include "sqliteInt.h"
drhad75e982001-10-09 04:19:46 +000021#include "os.h"
drh75897232000-05-29 14:26:00 +000022#include <ctype.h>
drhdcc581c2000-05-30 13:44:19 +000023#include <stdlib.h>
drh75897232000-05-29 14:26:00 +000024
25/*
26** All the keywords of the SQL language are stored as in a hash
27** table composed of instances of the following structure.
28*/
29typedef struct Keyword Keyword;
30struct Keyword {
31 char *zName; /* The keyword name */
32 int len; /* Number of characters in the keyword */
33 int tokenType; /* The token value for this keyword */
34 Keyword *pNext; /* Next keyword with the same hash */
35};
36
37/*
38** These are the keywords
39*/
40static Keyword aKeywordTable[] = {
drh9cfcf5d2002-01-29 18:41:24 +000041 { "ABORT", 0, TK_ABORT, 0 },
danielk1977c3f9bad2002-05-15 08:30:12 +000042 { "AFTER", 0, TK_AFTER, 0 },
drhfef52082000-06-06 01:50:43 +000043 { "ALL", 0, TK_ALL, 0 },
drh75897232000-05-29 14:26:00 +000044 { "AND", 0, TK_AND, 0 },
45 { "AS", 0, TK_AS, 0 },
46 { "ASC", 0, TK_ASC, 0 },
drh113088e2003-03-20 01:16:58 +000047 { "ATTACH", 0, TK_ATTACH, 0 },
danielk1977c3f9bad2002-05-15 08:30:12 +000048 { "BEFORE", 0, TK_BEFORE, 0 },
drhc4a3c772001-04-04 11:48:57 +000049 { "BEGIN", 0, TK_BEGIN, 0 },
drhfef52082000-06-06 01:50:43 +000050 { "BETWEEN", 0, TK_BETWEEN, 0 },
drh75897232000-05-29 14:26:00 +000051 { "BY", 0, TK_BY, 0 },
drh04738cb2002-06-02 18:19:00 +000052 { "CASCADE", 0, TK_CASCADE, 0 },
drh17a7f8d2002-03-24 13:13:27 +000053 { "CASE", 0, TK_CASE, 0 },
drh75897232000-05-29 14:26:00 +000054 { "CHECK", 0, TK_CHECK, 0 },
drhf57b14a2001-09-14 18:54:08 +000055 { "CLUSTER", 0, TK_CLUSTER, 0 },
drh8e2ca022002-06-17 17:07:19 +000056 { "COLLATE", 0, TK_COLLATE, 0 },
drhc4a3c772001-04-04 11:48:57 +000057 { "COMMIT", 0, TK_COMMIT, 0 },
drh9cfcf5d2002-01-29 18:41:24 +000058 { "CONFLICT", 0, TK_CONFLICT, 0 },
drh75897232000-05-29 14:26:00 +000059 { "CONSTRAINT", 0, TK_CONSTRAINT, 0 },
drh982cef72000-05-30 16:27:03 +000060 { "COPY", 0, TK_COPY, 0 },
drh75897232000-05-29 14:26:00 +000061 { "CREATE", 0, TK_CREATE, 0 },
drh5ad1a6c2002-07-01 12:27:09 +000062 { "CROSS", 0, TK_JOIN_KW, 0 },
drh113088e2003-03-20 01:16:58 +000063 { "DATABASE", 0, TK_DATABASE, 0 },
drh75897232000-05-29 14:26:00 +000064 { "DEFAULT", 0, TK_DEFAULT, 0 },
drh04738cb2002-06-02 18:19:00 +000065 { "DEFERRED", 0, TK_DEFERRED, 0 },
66 { "DEFERRABLE", 0, TK_DEFERRABLE, 0 },
drh75897232000-05-29 14:26:00 +000067 { "DELETE", 0, TK_DELETE, 0 },
drh982cef72000-05-30 16:27:03 +000068 { "DELIMITERS", 0, TK_DELIMITERS, 0 },
drh75897232000-05-29 14:26:00 +000069 { "DESC", 0, TK_DESC, 0 },
drh113088e2003-03-20 01:16:58 +000070 { "DETACH", 0, TK_DETACH, 0 },
drhefb72512000-05-31 20:00:52 +000071 { "DISTINCT", 0, TK_DISTINCT, 0 },
drh75897232000-05-29 14:26:00 +000072 { "DROP", 0, TK_DROP, 0 },
drhc4a3c772001-04-04 11:48:57 +000073 { "END", 0, TK_END, 0 },
danielk1977c3f9bad2002-05-15 08:30:12 +000074 { "EACH", 0, TK_EACH, 0 },
drh17a7f8d2002-03-24 13:13:27 +000075 { "ELSE", 0, TK_ELSE, 0 },
drh82c3d632000-06-06 21:56:07 +000076 { "EXCEPT", 0, TK_EXCEPT, 0 },
drh75897232000-05-29 14:26:00 +000077 { "EXPLAIN", 0, TK_EXPLAIN, 0 },
drh1c928532002-01-31 15:54:21 +000078 { "FAIL", 0, TK_FAIL, 0 },
danielk1977c3f9bad2002-05-15 08:30:12 +000079 { "FOR", 0, TK_FOR, 0 },
drh04738cb2002-06-02 18:19:00 +000080 { "FOREIGN", 0, TK_FOREIGN, 0 },
drh75897232000-05-29 14:26:00 +000081 { "FROM", 0, TK_FROM, 0 },
drh5ad1a6c2002-07-01 12:27:09 +000082 { "FULL", 0, TK_JOIN_KW, 0 },
drhdce2cbe2000-05-31 02:27:49 +000083 { "GLOB", 0, TK_GLOB, 0 },
drh22827922000-06-06 17:27:05 +000084 { "GROUP", 0, TK_GROUP, 0 },
85 { "HAVING", 0, TK_HAVING, 0 },
drh9cfcf5d2002-01-29 18:41:24 +000086 { "IGNORE", 0, TK_IGNORE, 0 },
drh04738cb2002-06-02 18:19:00 +000087 { "IMMEDIATE", 0, TK_IMMEDIATE, 0 },
drhfef52082000-06-06 01:50:43 +000088 { "IN", 0, TK_IN, 0 },
drh75897232000-05-29 14:26:00 +000089 { "INDEX", 0, TK_INDEX, 0 },
drh04738cb2002-06-02 18:19:00 +000090 { "INITIALLY", 0, TK_INITIALLY, 0 },
drh5ad1a6c2002-07-01 12:27:09 +000091 { "INNER", 0, TK_JOIN_KW, 0 },
drh75897232000-05-29 14:26:00 +000092 { "INSERT", 0, TK_INSERT, 0 },
danielk1977c3f9bad2002-05-15 08:30:12 +000093 { "INSTEAD", 0, TK_INSTEAD, 0 },
drh82c3d632000-06-06 21:56:07 +000094 { "INTERSECT", 0, TK_INTERSECT, 0 },
drh75897232000-05-29 14:26:00 +000095 { "INTO", 0, TK_INTO, 0 },
96 { "IS", 0, TK_IS, 0 },
97 { "ISNULL", 0, TK_ISNULL, 0 },
drh01f3f252002-05-24 16:14:15 +000098 { "JOIN", 0, TK_JOIN, 0 },
drh75897232000-05-29 14:26:00 +000099 { "KEY", 0, TK_KEY, 0 },
drh5ad1a6c2002-07-01 12:27:09 +0000100 { "LEFT", 0, TK_JOIN_KW, 0 },
drhdce2cbe2000-05-31 02:27:49 +0000101 { "LIKE", 0, TK_LIKE, 0 },
drh9bbca4c2001-11-06 04:00:18 +0000102 { "LIMIT", 0, TK_LIMIT, 0 },
drh04738cb2002-06-02 18:19:00 +0000103 { "MATCH", 0, TK_MATCH, 0 },
drh5ad1a6c2002-07-01 12:27:09 +0000104 { "NATURAL", 0, TK_JOIN_KW, 0 },
drh75897232000-05-29 14:26:00 +0000105 { "NOT", 0, TK_NOT, 0 },
106 { "NOTNULL", 0, TK_NOTNULL, 0 },
107 { "NULL", 0, TK_NULL, 0 },
danielk1977c3f9bad2002-05-15 08:30:12 +0000108 { "OF", 0, TK_OF, 0 },
drh9bbca4c2001-11-06 04:00:18 +0000109 { "OFFSET", 0, TK_OFFSET, 0 },
drh75897232000-05-29 14:26:00 +0000110 { "ON", 0, TK_ON, 0 },
111 { "OR", 0, TK_OR, 0 },
112 { "ORDER", 0, TK_ORDER, 0 },
drh5ad1a6c2002-07-01 12:27:09 +0000113 { "OUTER", 0, TK_JOIN_KW, 0 },
drhf57b14a2001-09-14 18:54:08 +0000114 { "PRAGMA", 0, TK_PRAGMA, 0 },
drh75897232000-05-29 14:26:00 +0000115 { "PRIMARY", 0, TK_PRIMARY, 0 },
danielk19776f349032002-06-11 02:25:40 +0000116 { "RAISE", 0, TK_RAISE, 0 },
drh04738cb2002-06-02 18:19:00 +0000117 { "REFERENCES", 0, TK_REFERENCES, 0 },
drh9cfcf5d2002-01-29 18:41:24 +0000118 { "REPLACE", 0, TK_REPLACE, 0 },
drh04738cb2002-06-02 18:19:00 +0000119 { "RESTRICT", 0, TK_RESTRICT, 0 },
drh5ad1a6c2002-07-01 12:27:09 +0000120 { "RIGHT", 0, TK_JOIN_KW, 0 },
drhc4a3c772001-04-04 11:48:57 +0000121 { "ROLLBACK", 0, TK_ROLLBACK, 0 },
danielk1977c3f9bad2002-05-15 08:30:12 +0000122 { "ROW", 0, TK_ROW, 0 },
drh75897232000-05-29 14:26:00 +0000123 { "SELECT", 0, TK_SELECT, 0 },
124 { "SET", 0, TK_SET, 0 },
drh1873cd52002-05-23 00:30:31 +0000125 { "STATEMENT", 0, TK_STATEMENT, 0 },
drh75897232000-05-29 14:26:00 +0000126 { "TABLE", 0, TK_TABLE, 0 },
drhf57b3392001-10-08 13:22:32 +0000127 { "TEMP", 0, TK_TEMP, 0 },
128 { "TEMPORARY", 0, TK_TEMP, 0 },
drh17a7f8d2002-03-24 13:13:27 +0000129 { "THEN", 0, TK_THEN, 0 },
drhc4a3c772001-04-04 11:48:57 +0000130 { "TRANSACTION", 0, TK_TRANSACTION, 0 },
danielk1977c3f9bad2002-05-15 08:30:12 +0000131 { "TRIGGER", 0, TK_TRIGGER, 0 },
drh82c3d632000-06-06 21:56:07 +0000132 { "UNION", 0, TK_UNION, 0 },
drh75897232000-05-29 14:26:00 +0000133 { "UNIQUE", 0, TK_UNIQUE, 0 },
134 { "UPDATE", 0, TK_UPDATE, 0 },
drh982cef72000-05-30 16:27:03 +0000135 { "USING", 0, TK_USING, 0 },
drhdce2cbe2000-05-31 02:27:49 +0000136 { "VACUUM", 0, TK_VACUUM, 0 },
drh75897232000-05-29 14:26:00 +0000137 { "VALUES", 0, TK_VALUES, 0 },
drha76b5df2002-02-23 02:32:10 +0000138 { "VIEW", 0, TK_VIEW, 0 },
drh17a7f8d2002-03-24 13:13:27 +0000139 { "WHEN", 0, TK_WHEN, 0 },
drh75897232000-05-29 14:26:00 +0000140 { "WHERE", 0, TK_WHERE, 0 },
141};
142
143/*
144** This is the hash table
145*/
drhdaffd0e2001-04-11 14:28:42 +0000146#define KEY_HASH_SIZE 71
drh75897232000-05-29 14:26:00 +0000147static Keyword *apHashTable[KEY_HASH_SIZE];
148
149
150/*
151** This function looks up an identifier to determine if it is a
152** keyword. If it is a keyword, the token code of that keyword is
153** returned. If the input is not a keyword, TK_ID is returned.
154*/
drh17f71932002-02-21 12:01:27 +0000155int sqliteKeywordCode(const char *z, int n){
drh75897232000-05-29 14:26:00 +0000156 int h;
157 Keyword *p;
158 if( aKeywordTable[0].len==0 ){
159 /* Initialize the keyword hash table */
drhad75e982001-10-09 04:19:46 +0000160 sqliteOsEnterMutex();
161 if( aKeywordTable[0].len==0 ){
162 int i;
163 int n;
164 n = sizeof(aKeywordTable)/sizeof(aKeywordTable[0]);
165 for(i=0; i<n; i++){
166 aKeywordTable[i].len = strlen(aKeywordTable[i].zName);
167 h = sqliteHashNoCase(aKeywordTable[i].zName, aKeywordTable[i].len);
168 h %= KEY_HASH_SIZE;
169 aKeywordTable[i].pNext = apHashTable[h];
170 apHashTable[h] = &aKeywordTable[i];
171 }
drh75897232000-05-29 14:26:00 +0000172 }
drhad75e982001-10-09 04:19:46 +0000173 sqliteOsLeaveMutex();
drh75897232000-05-29 14:26:00 +0000174 }
175 h = sqliteHashNoCase(z, n) % KEY_HASH_SIZE;
176 for(p=apHashTable[h]; p; p=p->pNext){
177 if( p->len==n && sqliteStrNICmp(p->zName, z, n)==0 ){
178 return p->tokenType;
179 }
180 }
181 return TK_ID;
182}
183
drh98808ba2001-10-18 12:34:46 +0000184
185/*
186** If X is a character that can be used in an identifier then
187** isIdChar[X] will be 1. Otherwise isIdChar[X] will be 0.
188**
189** In this implementation, an identifier can be a string of
190** alphabetic characters, digits, and "_" plus any character
191** with the high-order bit set. The latter rule means that
192** any sequence of UTF-8 characters or characters taken from
193** an extended ISO8859 character set can form an identifier.
194*/
195static const char isIdChar[] = {
196/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
197 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
198 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
199 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
200 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
201 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
202 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
203 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
204 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
205 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 8x */
206 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 9x */
207 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Ax */
208 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Bx */
209 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Cx */
210 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Dx */
211 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Ex */
212 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Fx */
213};
214
215
drh75897232000-05-29 14:26:00 +0000216/*
217** Return the length of the token that begins at z[0]. Return
218** -1 if the token is (or might be) incomplete. Store the token
219** type in *tokenType before returning.
220*/
drh98808ba2001-10-18 12:34:46 +0000221static int sqliteGetToken(const unsigned char *z, int *tokenType){
drh75897232000-05-29 14:26:00 +0000222 int i;
223 switch( *z ){
drh30cab802000-08-09 17:17:25 +0000224 case ' ': case '\t': case '\n': case '\f': case '\r': {
drh32eb7b42003-01-07 01:44:37 +0000225 for(i=1; isspace(z[i]); i++){}
drh75897232000-05-29 14:26:00 +0000226 *tokenType = TK_SPACE;
227 return i;
228 }
229 case '-': {
230 if( z[1]==0 ) return -1;
231 if( z[1]=='-' ){
232 for(i=2; z[i] && z[i]!='\n'; i++){}
233 *tokenType = TK_COMMENT;
234 return i;
235 }
236 *tokenType = TK_MINUS;
237 return 1;
238 }
239 case '(': {
drh1f162302002-10-27 19:35:33 +0000240 if( z[1]=='+' && z[2]==')' ){
241 *tokenType = TK_ORACLE_OUTER_JOIN;
242 return 3;
243 }else{
244 *tokenType = TK_LP;
245 return 1;
246 }
drh75897232000-05-29 14:26:00 +0000247 }
248 case ')': {
249 *tokenType = TK_RP;
250 return 1;
251 }
252 case ';': {
253 *tokenType = TK_SEMI;
254 return 1;
255 }
256 case '+': {
257 *tokenType = TK_PLUS;
258 return 1;
259 }
260 case '*': {
261 *tokenType = TK_STAR;
262 return 1;
263 }
264 case '/': {
drh66105a82002-08-27 14:28:29 +0000265 if( z[1]!='*' || z[2]==0 ){
266 *tokenType = TK_SLASH;
267 return 1;
268 }
269 for(i=3; z[i] && (z[i]!='/' || z[i-1]!='*'); i++){}
270 if( z[i] ) i++;
271 *tokenType = TK_COMMENT;
272 return i;
drh75897232000-05-29 14:26:00 +0000273 }
drhbf4133c2001-10-13 02:59:08 +0000274 case '%': {
275 *tokenType = TK_REM;
276 return 1;
277 }
drh75897232000-05-29 14:26:00 +0000278 case '=': {
279 *tokenType = TK_EQ;
280 return 1 + (z[1]=='=');
281 }
282 case '<': {
283 if( z[1]=='=' ){
284 *tokenType = TK_LE;
285 return 2;
286 }else if( z[1]=='>' ){
287 *tokenType = TK_NE;
288 return 2;
drhbf4133c2001-10-13 02:59:08 +0000289 }else if( z[1]=='<' ){
290 *tokenType = TK_LSHIFT;
291 return 2;
drh75897232000-05-29 14:26:00 +0000292 }else{
293 *tokenType = TK_LT;
294 return 1;
295 }
296 }
297 case '>': {
298 if( z[1]=='=' ){
299 *tokenType = TK_GE;
300 return 2;
drhbf4133c2001-10-13 02:59:08 +0000301 }else if( z[1]=='>' ){
302 *tokenType = TK_RSHIFT;
303 return 2;
drh75897232000-05-29 14:26:00 +0000304 }else{
305 *tokenType = TK_GT;
306 return 1;
307 }
308 }
309 case '!': {
310 if( z[1]!='=' ){
311 *tokenType = TK_ILLEGAL;
drhc837e702000-06-08 16:26:24 +0000312 return 2;
drh75897232000-05-29 14:26:00 +0000313 }else{
314 *tokenType = TK_NE;
315 return 2;
316 }
317 }
drh00400772000-06-16 20:51:26 +0000318 case '|': {
319 if( z[1]!='|' ){
drhbf4133c2001-10-13 02:59:08 +0000320 *tokenType = TK_BITOR;
drh00400772000-06-16 20:51:26 +0000321 return 1;
322 }else{
323 *tokenType = TK_CONCAT;
324 return 2;
325 }
326 }
drh75897232000-05-29 14:26:00 +0000327 case ',': {
328 *tokenType = TK_COMMA;
329 return 1;
330 }
drhbf4133c2001-10-13 02:59:08 +0000331 case '&': {
332 *tokenType = TK_BITAND;
333 return 1;
334 }
335 case '~': {
336 *tokenType = TK_BITNOT;
337 return 1;
338 }
drh75897232000-05-29 14:26:00 +0000339 case '\'': case '"': {
340 int delim = z[0];
341 for(i=1; z[i]; i++){
342 if( z[i]==delim ){
343 if( z[i+1]==delim ){
344 i++;
345 }else{
346 break;
347 }
348 }
349 }
350 if( z[i] ) i++;
351 *tokenType = TK_STRING;
352 return i;
353 }
354 case '.': {
drhbb07e9a2003-04-16 02:17:35 +0000355 *tokenType = TK_DOT;
356 return 1;
drh75897232000-05-29 14:26:00 +0000357 }
358 case '0': case '1': case '2': case '3': case '4':
359 case '5': case '6': case '7': case '8': case '9': {
drhc837e702000-06-08 16:26:24 +0000360 *tokenType = TK_INTEGER;
drh32eb7b42003-01-07 01:44:37 +0000361 for(i=1; isdigit(z[i]); i++){}
drhbb07e9a2003-04-16 02:17:35 +0000362 if( z[i]=='.' && isdigit(z[i+1]) ){
363 i += 2;
drh32eb7b42003-01-07 01:44:37 +0000364 while( isdigit(z[i]) ){ i++; }
drhc837e702000-06-08 16:26:24 +0000365 *tokenType = TK_FLOAT;
366 }
367 if( (z[i]=='e' || z[i]=='E') &&
drh75897232000-05-29 14:26:00 +0000368 ( isdigit(z[i+1])
369 || ((z[i+1]=='+' || z[i+1]=='-') && isdigit(z[i+2]))
370 )
drhc837e702000-06-08 16:26:24 +0000371 ){
372 i += 2;
drh32eb7b42003-01-07 01:44:37 +0000373 while( isdigit(z[i]) ){ i++; }
drh75897232000-05-29 14:26:00 +0000374 *tokenType = TK_FLOAT;
drh75897232000-05-29 14:26:00 +0000375 }
376 return i;
377 }
drh2f4392f2002-02-14 21:42:51 +0000378 case '[': {
379 for(i=1; z[i] && z[i-1]!=']'; i++){}
380 *tokenType = TK_ID;
381 return i;
382 }
drh7c972de2003-09-06 22:18:07 +0000383 case '?': {
drh50457892003-09-06 01:10:47 +0000384 *tokenType = TK_VARIABLE;
drh7c972de2003-09-06 22:18:07 +0000385 return 1;
drh50457892003-09-06 01:10:47 +0000386 }
drh98808ba2001-10-18 12:34:46 +0000387 default: {
388 if( !isIdChar[*z] ){
389 break;
390 }
391 for(i=1; isIdChar[z[i]]; i++){}
drh6a535342001-10-19 16:44:56 +0000392 *tokenType = sqliteKeywordCode((char*)z, i);
drh75897232000-05-29 14:26:00 +0000393 return i;
394 }
drh75897232000-05-29 14:26:00 +0000395 }
396 *tokenType = TK_ILLEGAL;
397 return 1;
398}
399
400/*
401** Run the parser on the given SQL string. The parser structure is
drhb19a2bc2001-09-16 00:13:26 +0000402** passed in. An SQLITE_ status code is returned. If an error occurs
403** and pzErrMsg!=NULL then an error message might be written into
404** memory obtained from malloc() and *pzErrMsg made to point to that
405** error message. Or maybe not.
drh75897232000-05-29 14:26:00 +0000406*/
drh80ff32f2001-11-04 18:32:46 +0000407int sqliteRunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
drh75897232000-05-29 14:26:00 +0000408 int nErr = 0;
409 int i;
410 void *pEngine;
drhb86ccfb2003-01-28 23:13:10 +0000411 int tokenType;
412 int lastTokenParsed = -1;
drh6d4abfb2001-10-22 02:58:08 +0000413 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000414 extern void *sqliteParserAlloc(void*(*)(int));
415 extern void sqliteParserFree(void*, void(*)(void*));
drh338ea132001-02-11 16:56:24 +0000416 extern int sqliteParser(void*, int, Token, Parse*);
drh75897232000-05-29 14:26:00 +0000417
drh6d4abfb2001-10-22 02:58:08 +0000418 db->flags &= ~SQLITE_Interrupt;
drh4c504392000-10-16 22:06:40 +0000419 pParse->rc = SQLITE_OK;
drh75897232000-05-29 14:26:00 +0000420 i = 0;
drh982cef72000-05-30 16:27:03 +0000421 pEngine = sqliteParserAlloc((void*(*)(int))malloc);
drh75897232000-05-29 14:26:00 +0000422 if( pEngine==0 ){
423 sqliteSetString(pzErrMsg, "out of memory", 0);
424 return 1;
425 }
drh32eb7b42003-01-07 01:44:37 +0000426 pParse->sLastToken.dyn = 0;
drh326dce72003-01-29 14:06:07 +0000427 pParse->zTail = zSql;
drh32eb7b42003-01-07 01:44:37 +0000428 while( sqlite_malloc_failed==0 && zSql[i]!=0 ){
drh75897232000-05-29 14:26:00 +0000429
drh32eb7b42003-01-07 01:44:37 +0000430 assert( i>=0 );
drh75897232000-05-29 14:26:00 +0000431 pParse->sLastToken.z = &zSql[i];
drh32eb7b42003-01-07 01:44:37 +0000432 assert( pParse->sLastToken.dyn==0 );
drh98808ba2001-10-18 12:34:46 +0000433 pParse->sLastToken.n = sqliteGetToken((unsigned char*)&zSql[i], &tokenType);
drh75897232000-05-29 14:26:00 +0000434 i += pParse->sLastToken.n;
drh75897232000-05-29 14:26:00 +0000435 switch( tokenType ){
436 case TK_SPACE:
drh75897232000-05-29 14:26:00 +0000437 case TK_COMMENT: {
drh32eb7b42003-01-07 01:44:37 +0000438 if( (db->flags & SQLITE_Interrupt)!=0 ){
439 pParse->rc = SQLITE_INTERRUPT;
440 sqliteSetString(pzErrMsg, "interrupt", 0);
441 goto abort_parse;
442 }
drh75897232000-05-29 14:26:00 +0000443 break;
444 }
drh32eb7b42003-01-07 01:44:37 +0000445 case TK_ILLEGAL: {
drhc837e702000-06-08 16:26:24 +0000446 sqliteSetNString(pzErrMsg, "unrecognized token: \"", -1,
447 pParse->sLastToken.z, pParse->sLastToken.n, "\"", 1, 0);
drh75897232000-05-29 14:26:00 +0000448 nErr++;
drhcaec2f12003-01-07 02:47:47 +0000449 goto abort_parse;
drh32eb7b42003-01-07 01:44:37 +0000450 }
drh326dce72003-01-29 14:06:07 +0000451 case TK_SEMI: {
452 pParse->zTail = &zSql[i];
453 /* Fall thru into the default case */
454 }
drh32eb7b42003-01-07 01:44:37 +0000455 default: {
drh75897232000-05-29 14:26:00 +0000456 sqliteParser(pEngine, tokenType, pParse->sLastToken, pParse);
drhb86ccfb2003-01-28 23:13:10 +0000457 lastTokenParsed = tokenType;
458 if( pParse->rc!=SQLITE_OK ){
drh32eb7b42003-01-07 01:44:37 +0000459 goto abort_parse;
drh75897232000-05-29 14:26:00 +0000460 }
461 break;
drh32eb7b42003-01-07 01:44:37 +0000462 }
drh75897232000-05-29 14:26:00 +0000463 }
464 }
drh32eb7b42003-01-07 01:44:37 +0000465abort_parse:
drhb86ccfb2003-01-28 23:13:10 +0000466 if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
467 if( lastTokenParsed!=TK_SEMI ){
468 sqliteParser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
drh326dce72003-01-29 14:06:07 +0000469 pParse->zTail = &zSql[i];
drh75897232000-05-29 14:26:00 +0000470 }
drhb86ccfb2003-01-28 23:13:10 +0000471 sqliteParser(pEngine, 0, pParse->sLastToken, pParse);
drh75897232000-05-29 14:26:00 +0000472 }
drhdcc581c2000-05-30 13:44:19 +0000473 sqliteParserFree(pEngine, free);
drhb86ccfb2003-01-28 23:13:10 +0000474 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
475 sqliteSetString(&pParse->zErrMsg, sqlite_error_string(pParse->rc), 0);
476 }
drh75897232000-05-29 14:26:00 +0000477 if( pParse->zErrMsg ){
drhb86ccfb2003-01-28 23:13:10 +0000478 if( pzErrMsg && *pzErrMsg==0 ){
drh75897232000-05-29 14:26:00 +0000479 *pzErrMsg = pParse->zErrMsg;
480 }else{
481 sqliteFree(pParse->zErrMsg);
482 }
drhb86ccfb2003-01-28 23:13:10 +0000483 pParse->zErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000484 if( !nErr ) nErr++;
485 }
drhb86ccfb2003-01-28 23:13:10 +0000486 if( pParse->pVdbe && (pParse->useCallback || pParse->nErr>0) ){
drh75897232000-05-29 14:26:00 +0000487 sqliteVdbeDelete(pParse->pVdbe);
488 pParse->pVdbe = 0;
489 }
490 if( pParse->pNewTable ){
491 sqliteDeleteTable(pParse->db, pParse->pNewTable);
492 pParse->pNewTable = 0;
493 }
drhf0f258b2003-04-21 18:48:45 +0000494 if( pParse->pNewTrigger ){
495 sqliteDeleteTrigger(pParse->pNewTrigger);
496 pParse->pNewTrigger = 0;
497 }
drhb86ccfb2003-01-28 23:13:10 +0000498 if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){
drh4c504392000-10-16 22:06:40 +0000499 pParse->rc = SQLITE_ERROR;
500 }
drh75897232000-05-29 14:26:00 +0000501 return nErr;
502}
drh7ad43342003-05-04 17:58:25 +0000503
504/*
505** Token types used by the sqlite_complete() routine. See the header
506** comments on that procedure for additional information.
507*/
508#define tkEXPLAIN 0
509#define tkCREATE 1
510#define tkTEMP 2
511#define tkTRIGGER 3
512#define tkEND 4
513#define tkSEMI 5
514#define tkWS 6
515#define tkOTHER 7
516
517/*
518** Return TRUE if the given SQL string ends in a semicolon.
519**
520** Special handling is require for CREATE TRIGGER statements.
521** Whenever the CREATE TRIGGER keywords are seen, the statement
522** must end with ";END;".
523**
524** This implementation uses a state machine with 7 states:
525**
526** (0) START At the beginning or end of an SQL statement. This routine
527** returns 1 if it ends in the START state and 0 if it ends
528** in any other state.
529**
530** (1) EXPLAIN The keyword EXPLAIN has been seen at the beginning of
531** a statement.
532**
533** (2) CREATE The keyword CREATE has been seen at the beginning of a
534** statement, possibly preceeded by EXPLAIN and/or followed by
535** TEMP or TEMPORARY
536**
537** (3) NORMAL We are in the middle of statement which ends with a single
538** semicolon.
539**
540** (4) TRIGGER We are in the middle of a trigger definition that must be
541** ended by a semicolon, the keyword END, and another semicolon.
542**
543** (5) SEMI We've seen the first semicolon in the ";END;" that occurs at
544** the end of a trigger definition.
545**
546** (6) END We've seen the ";END" of the ";END;" that occurs at the end
547** of a trigger difinition.
548**
549** Transitions between states above are determined by tokens extracted
550** from the input. The following tokens are significant:
551**
552** (0) tkEXPLAIN The "explain" keyword.
553** (1) tkCREATE The "create" keyword.
554** (2) tkTEMP The "temp" or "temporary" keyword.
555** (3) tkTRIGGER The "trigger" keyword.
556** (4) tkEND The "end" keyword.
557** (5) tkSEMI A semicolon.
558** (6) tkWS Whitespace
559** (7) tkOTHER Any other SQL token.
560**
561** Whitespace never causes a state transition and is always ignored.
562*/
563int sqlite_complete(const char *zSql){
564 u8 state = 0; /* Current state, using numbers defined in header comment */
565 u8 token; /* Value of the next token */
566
567 /* The following matrix defines the transition from one state to another
568 ** according to what token is seen. trans[state][token] returns the
569 ** next state.
570 */
571 static const u8 trans[7][8] = {
drhe1e38c42003-05-04 18:30:59 +0000572 /* Token: */
drh7ad43342003-05-04 17:58:25 +0000573 /* State: ** EXPLAIN CREATE TEMP TRIGGER END SEMI WS OTHER */
574 /* 0 START: */ { 1, 2, 3, 3, 3, 0, 0, 3, },
575 /* 1 EXPLAIN: */ { 3, 2, 3, 3, 3, 0, 1, 3, },
576 /* 2 CREATE: */ { 3, 3, 2, 4, 3, 0, 2, 3, },
577 /* 3 NORMAL: */ { 3, 3, 3, 3, 3, 0, 3, 3, },
578 /* 4 TRIGGER: */ { 4, 4, 4, 4, 4, 5, 4, 4, },
579 /* 5 SEMI: */ { 4, 4, 4, 4, 6, 5, 5, 4, },
580 /* 6 END: */ { 4, 4, 4, 4, 4, 0, 6, 4, },
581 };
582
583 while( *zSql ){
584 switch( *zSql ){
585 case ';': { /* A semicolon */
586 token = tkSEMI;
587 break;
588 }
589 case ' ':
590 case '\r':
591 case '\t':
592 case '\n':
593 case '\f': { /* White space is ignored */
594 token = tkWS;
595 break;
596 }
597 case '/': { /* C-style comments */
598 if( zSql[1]!='*' ){
599 token = tkOTHER;
600 break;
601 }
602 zSql += 2;
603 while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
604 if( zSql[0]==0 ) return 0;
605 zSql++;
606 token = tkWS;
607 break;
608 }
609 case '-': { /* SQL-style comments from "--" to end of line */
610 if( zSql[1]!='-' ){
611 token = tkOTHER;
612 break;
613 }
614 while( *zSql && *zSql!='\n' ){ zSql++; }
615 if( *zSql==0 ) return state==0;
616 token = tkWS;
617 break;
618 }
619 case '[': { /* Microsoft-style identifiers in [...] */
620 zSql++;
621 while( *zSql && *zSql!=']' ){ zSql++; }
622 if( *zSql==0 ) return 0;
623 token = tkOTHER;
624 break;
625 }
626 case '"': /* single- and double-quoted strings */
627 case '\'': {
628 int c = *zSql;
629 zSql++;
630 while( *zSql && *zSql!=c ){ zSql++; }
631 if( *zSql==0 ) return 0;
632 token = tkOTHER;
633 break;
634 }
635 default: {
drhe1e38c42003-05-04 18:30:59 +0000636 if( isIdChar[(u8)*zSql] ){
drh7ad43342003-05-04 17:58:25 +0000637 /* Keywords and unquoted identifiers */
638 int nId;
drhe1e38c42003-05-04 18:30:59 +0000639 for(nId=1; isIdChar[(u8)zSql[nId]]; nId++){}
drh7ad43342003-05-04 17:58:25 +0000640 switch( *zSql ){
641 case 'c': case 'C': {
642 if( nId==6 && sqliteStrNICmp(zSql, "create", 6)==0 ){
643 token = tkCREATE;
644 }else{
645 token = tkOTHER;
646 }
647 break;
648 }
649 case 't': case 'T': {
650 if( nId==7 && sqliteStrNICmp(zSql, "trigger", 7)==0 ){
651 token = tkTRIGGER;
652 }else if( nId==4 && sqliteStrNICmp(zSql, "temp", 4)==0 ){
653 token = tkTEMP;
654 }else if( nId==9 && sqliteStrNICmp(zSql, "temporary", 9)==0 ){
655 token = tkTEMP;
656 }else{
657 token = tkOTHER;
658 }
659 break;
660 }
661 case 'e': case 'E': {
662 if( nId==3 && sqliteStrNICmp(zSql, "end", 3)==0 ){
663 token = tkEND;
664 }else if( nId==7 && sqliteStrNICmp(zSql, "explain", 7)==0 ){
665 token = tkEXPLAIN;
666 }else{
667 token = tkOTHER;
668 }
669 break;
670 }
671 default: {
672 token = tkOTHER;
673 break;
674 }
675 }
676 zSql += nId-1;
677 }else{
678 /* Operators and special symbols */
679 token = tkOTHER;
680 }
681 break;
682 }
683 }
684 state = trans[state][token];
685 zSql++;
686 }
687 return state==0;
688}