blob: 7dd49b21a4a3c5d370d0c3b606e64fa9abfe4797 [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**
drh326dce72003-01-29 14:06:07 +000018** $Id: tokenize.c,v 1.55 2003/01/29 14:06:09 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 },
danielk1977c3f9bad2002-05-15 08:30:12 +000047 { "BEFORE", 0, TK_BEFORE, 0 },
drhc4a3c772001-04-04 11:48:57 +000048 { "BEGIN", 0, TK_BEGIN, 0 },
drhfef52082000-06-06 01:50:43 +000049 { "BETWEEN", 0, TK_BETWEEN, 0 },
drh75897232000-05-29 14:26:00 +000050 { "BY", 0, TK_BY, 0 },
drh04738cb2002-06-02 18:19:00 +000051 { "CASCADE", 0, TK_CASCADE, 0 },
drh17a7f8d2002-03-24 13:13:27 +000052 { "CASE", 0, TK_CASE, 0 },
drh75897232000-05-29 14:26:00 +000053 { "CHECK", 0, TK_CHECK, 0 },
drhf57b14a2001-09-14 18:54:08 +000054 { "CLUSTER", 0, TK_CLUSTER, 0 },
drh8e2ca022002-06-17 17:07:19 +000055 { "COLLATE", 0, TK_COLLATE, 0 },
drhc4a3c772001-04-04 11:48:57 +000056 { "COMMIT", 0, TK_COMMIT, 0 },
drh9cfcf5d2002-01-29 18:41:24 +000057 { "CONFLICT", 0, TK_CONFLICT, 0 },
drh75897232000-05-29 14:26:00 +000058 { "CONSTRAINT", 0, TK_CONSTRAINT, 0 },
drh982cef72000-05-30 16:27:03 +000059 { "COPY", 0, TK_COPY, 0 },
drh75897232000-05-29 14:26:00 +000060 { "CREATE", 0, TK_CREATE, 0 },
drh5ad1a6c2002-07-01 12:27:09 +000061 { "CROSS", 0, TK_JOIN_KW, 0 },
drh75897232000-05-29 14:26:00 +000062 { "DEFAULT", 0, TK_DEFAULT, 0 },
drh04738cb2002-06-02 18:19:00 +000063 { "DEFERRED", 0, TK_DEFERRED, 0 },
64 { "DEFERRABLE", 0, TK_DEFERRABLE, 0 },
drh75897232000-05-29 14:26:00 +000065 { "DELETE", 0, TK_DELETE, 0 },
drh982cef72000-05-30 16:27:03 +000066 { "DELIMITERS", 0, TK_DELIMITERS, 0 },
drh75897232000-05-29 14:26:00 +000067 { "DESC", 0, TK_DESC, 0 },
drhefb72512000-05-31 20:00:52 +000068 { "DISTINCT", 0, TK_DISTINCT, 0 },
drh75897232000-05-29 14:26:00 +000069 { "DROP", 0, TK_DROP, 0 },
drhc4a3c772001-04-04 11:48:57 +000070 { "END", 0, TK_END, 0 },
danielk1977c3f9bad2002-05-15 08:30:12 +000071 { "EACH", 0, TK_EACH, 0 },
drh17a7f8d2002-03-24 13:13:27 +000072 { "ELSE", 0, TK_ELSE, 0 },
drh82c3d632000-06-06 21:56:07 +000073 { "EXCEPT", 0, TK_EXCEPT, 0 },
drh75897232000-05-29 14:26:00 +000074 { "EXPLAIN", 0, TK_EXPLAIN, 0 },
drh1c928532002-01-31 15:54:21 +000075 { "FAIL", 0, TK_FAIL, 0 },
danielk1977c3f9bad2002-05-15 08:30:12 +000076 { "FOR", 0, TK_FOR, 0 },
drh04738cb2002-06-02 18:19:00 +000077 { "FOREIGN", 0, TK_FOREIGN, 0 },
drh75897232000-05-29 14:26:00 +000078 { "FROM", 0, TK_FROM, 0 },
drh5ad1a6c2002-07-01 12:27:09 +000079 { "FULL", 0, TK_JOIN_KW, 0 },
drhdce2cbe2000-05-31 02:27:49 +000080 { "GLOB", 0, TK_GLOB, 0 },
drh22827922000-06-06 17:27:05 +000081 { "GROUP", 0, TK_GROUP, 0 },
82 { "HAVING", 0, TK_HAVING, 0 },
drh9cfcf5d2002-01-29 18:41:24 +000083 { "IGNORE", 0, TK_IGNORE, 0 },
drh04738cb2002-06-02 18:19:00 +000084 { "IMMEDIATE", 0, TK_IMMEDIATE, 0 },
drhfef52082000-06-06 01:50:43 +000085 { "IN", 0, TK_IN, 0 },
drh75897232000-05-29 14:26:00 +000086 { "INDEX", 0, TK_INDEX, 0 },
drh04738cb2002-06-02 18:19:00 +000087 { "INITIALLY", 0, TK_INITIALLY, 0 },
drh5ad1a6c2002-07-01 12:27:09 +000088 { "INNER", 0, TK_JOIN_KW, 0 },
drh75897232000-05-29 14:26:00 +000089 { "INSERT", 0, TK_INSERT, 0 },
danielk1977c3f9bad2002-05-15 08:30:12 +000090 { "INSTEAD", 0, TK_INSTEAD, 0 },
drh82c3d632000-06-06 21:56:07 +000091 { "INTERSECT", 0, TK_INTERSECT, 0 },
drh75897232000-05-29 14:26:00 +000092 { "INTO", 0, TK_INTO, 0 },
93 { "IS", 0, TK_IS, 0 },
94 { "ISNULL", 0, TK_ISNULL, 0 },
drh01f3f252002-05-24 16:14:15 +000095 { "JOIN", 0, TK_JOIN, 0 },
drh75897232000-05-29 14:26:00 +000096 { "KEY", 0, TK_KEY, 0 },
drh5ad1a6c2002-07-01 12:27:09 +000097 { "LEFT", 0, TK_JOIN_KW, 0 },
drhdce2cbe2000-05-31 02:27:49 +000098 { "LIKE", 0, TK_LIKE, 0 },
drh9bbca4c2001-11-06 04:00:18 +000099 { "LIMIT", 0, TK_LIMIT, 0 },
drh04738cb2002-06-02 18:19:00 +0000100 { "MATCH", 0, TK_MATCH, 0 },
drh5ad1a6c2002-07-01 12:27:09 +0000101 { "NATURAL", 0, TK_JOIN_KW, 0 },
drh75897232000-05-29 14:26:00 +0000102 { "NOT", 0, TK_NOT, 0 },
103 { "NOTNULL", 0, TK_NOTNULL, 0 },
104 { "NULL", 0, TK_NULL, 0 },
danielk1977c3f9bad2002-05-15 08:30:12 +0000105 { "OF", 0, TK_OF, 0 },
drh9bbca4c2001-11-06 04:00:18 +0000106 { "OFFSET", 0, TK_OFFSET, 0 },
drh75897232000-05-29 14:26:00 +0000107 { "ON", 0, TK_ON, 0 },
108 { "OR", 0, TK_OR, 0 },
109 { "ORDER", 0, TK_ORDER, 0 },
drh5ad1a6c2002-07-01 12:27:09 +0000110 { "OUTER", 0, TK_JOIN_KW, 0 },
drhf57b14a2001-09-14 18:54:08 +0000111 { "PRAGMA", 0, TK_PRAGMA, 0 },
drh75897232000-05-29 14:26:00 +0000112 { "PRIMARY", 0, TK_PRIMARY, 0 },
danielk19776f349032002-06-11 02:25:40 +0000113 { "RAISE", 0, TK_RAISE, 0 },
drh04738cb2002-06-02 18:19:00 +0000114 { "REFERENCES", 0, TK_REFERENCES, 0 },
drh9cfcf5d2002-01-29 18:41:24 +0000115 { "REPLACE", 0, TK_REPLACE, 0 },
drh04738cb2002-06-02 18:19:00 +0000116 { "RESTRICT", 0, TK_RESTRICT, 0 },
drh5ad1a6c2002-07-01 12:27:09 +0000117 { "RIGHT", 0, TK_JOIN_KW, 0 },
drhc4a3c772001-04-04 11:48:57 +0000118 { "ROLLBACK", 0, TK_ROLLBACK, 0 },
danielk1977c3f9bad2002-05-15 08:30:12 +0000119 { "ROW", 0, TK_ROW, 0 },
drh75897232000-05-29 14:26:00 +0000120 { "SELECT", 0, TK_SELECT, 0 },
121 { "SET", 0, TK_SET, 0 },
drh1873cd52002-05-23 00:30:31 +0000122 { "STATEMENT", 0, TK_STATEMENT, 0 },
drh75897232000-05-29 14:26:00 +0000123 { "TABLE", 0, TK_TABLE, 0 },
drhf57b3392001-10-08 13:22:32 +0000124 { "TEMP", 0, TK_TEMP, 0 },
125 { "TEMPORARY", 0, TK_TEMP, 0 },
drh17a7f8d2002-03-24 13:13:27 +0000126 { "THEN", 0, TK_THEN, 0 },
drhc4a3c772001-04-04 11:48:57 +0000127 { "TRANSACTION", 0, TK_TRANSACTION, 0 },
danielk1977c3f9bad2002-05-15 08:30:12 +0000128 { "TRIGGER", 0, TK_TRIGGER, 0 },
drh82c3d632000-06-06 21:56:07 +0000129 { "UNION", 0, TK_UNION, 0 },
drh75897232000-05-29 14:26:00 +0000130 { "UNIQUE", 0, TK_UNIQUE, 0 },
131 { "UPDATE", 0, TK_UPDATE, 0 },
drh982cef72000-05-30 16:27:03 +0000132 { "USING", 0, TK_USING, 0 },
drhdce2cbe2000-05-31 02:27:49 +0000133 { "VACUUM", 0, TK_VACUUM, 0 },
drh75897232000-05-29 14:26:00 +0000134 { "VALUES", 0, TK_VALUES, 0 },
drha76b5df2002-02-23 02:32:10 +0000135 { "VIEW", 0, TK_VIEW, 0 },
drh17a7f8d2002-03-24 13:13:27 +0000136 { "WHEN", 0, TK_WHEN, 0 },
drh75897232000-05-29 14:26:00 +0000137 { "WHERE", 0, TK_WHERE, 0 },
138};
139
140/*
141** This is the hash table
142*/
drhdaffd0e2001-04-11 14:28:42 +0000143#define KEY_HASH_SIZE 71
drh75897232000-05-29 14:26:00 +0000144static Keyword *apHashTable[KEY_HASH_SIZE];
145
146
147/*
148** This function looks up an identifier to determine if it is a
149** keyword. If it is a keyword, the token code of that keyword is
150** returned. If the input is not a keyword, TK_ID is returned.
151*/
drh17f71932002-02-21 12:01:27 +0000152int sqliteKeywordCode(const char *z, int n){
drh75897232000-05-29 14:26:00 +0000153 int h;
154 Keyword *p;
155 if( aKeywordTable[0].len==0 ){
156 /* Initialize the keyword hash table */
drhad75e982001-10-09 04:19:46 +0000157 sqliteOsEnterMutex();
158 if( aKeywordTable[0].len==0 ){
159 int i;
160 int n;
161 n = sizeof(aKeywordTable)/sizeof(aKeywordTable[0]);
162 for(i=0; i<n; i++){
163 aKeywordTable[i].len = strlen(aKeywordTable[i].zName);
164 h = sqliteHashNoCase(aKeywordTable[i].zName, aKeywordTable[i].len);
165 h %= KEY_HASH_SIZE;
166 aKeywordTable[i].pNext = apHashTable[h];
167 apHashTable[h] = &aKeywordTable[i];
168 }
drh75897232000-05-29 14:26:00 +0000169 }
drhad75e982001-10-09 04:19:46 +0000170 sqliteOsLeaveMutex();
drh75897232000-05-29 14:26:00 +0000171 }
172 h = sqliteHashNoCase(z, n) % KEY_HASH_SIZE;
173 for(p=apHashTable[h]; p; p=p->pNext){
174 if( p->len==n && sqliteStrNICmp(p->zName, z, n)==0 ){
175 return p->tokenType;
176 }
177 }
178 return TK_ID;
179}
180
drh98808ba2001-10-18 12:34:46 +0000181
182/*
183** If X is a character that can be used in an identifier then
184** isIdChar[X] will be 1. Otherwise isIdChar[X] will be 0.
185**
186** In this implementation, an identifier can be a string of
187** alphabetic characters, digits, and "_" plus any character
188** with the high-order bit set. The latter rule means that
189** any sequence of UTF-8 characters or characters taken from
190** an extended ISO8859 character set can form an identifier.
191*/
192static const char isIdChar[] = {
193/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
194 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
195 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
196 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
197 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
198 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
199 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
200 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
201 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
202 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 8x */
203 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 9x */
204 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Ax */
205 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Bx */
206 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Cx */
207 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Dx */
208 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Ex */
209 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Fx */
210};
211
212
drh75897232000-05-29 14:26:00 +0000213/*
214** Return the length of the token that begins at z[0]. Return
215** -1 if the token is (or might be) incomplete. Store the token
216** type in *tokenType before returning.
217*/
drh98808ba2001-10-18 12:34:46 +0000218static int sqliteGetToken(const unsigned char *z, int *tokenType){
drh75897232000-05-29 14:26:00 +0000219 int i;
220 switch( *z ){
drh30cab802000-08-09 17:17:25 +0000221 case ' ': case '\t': case '\n': case '\f': case '\r': {
drh32eb7b42003-01-07 01:44:37 +0000222 for(i=1; isspace(z[i]); i++){}
drh75897232000-05-29 14:26:00 +0000223 *tokenType = TK_SPACE;
224 return i;
225 }
226 case '-': {
227 if( z[1]==0 ) return -1;
228 if( z[1]=='-' ){
229 for(i=2; z[i] && z[i]!='\n'; i++){}
230 *tokenType = TK_COMMENT;
231 return i;
232 }
233 *tokenType = TK_MINUS;
234 return 1;
235 }
236 case '(': {
drh1f162302002-10-27 19:35:33 +0000237 if( z[1]=='+' && z[2]==')' ){
238 *tokenType = TK_ORACLE_OUTER_JOIN;
239 return 3;
240 }else{
241 *tokenType = TK_LP;
242 return 1;
243 }
drh75897232000-05-29 14:26:00 +0000244 }
245 case ')': {
246 *tokenType = TK_RP;
247 return 1;
248 }
249 case ';': {
250 *tokenType = TK_SEMI;
251 return 1;
252 }
253 case '+': {
254 *tokenType = TK_PLUS;
255 return 1;
256 }
257 case '*': {
258 *tokenType = TK_STAR;
259 return 1;
260 }
261 case '/': {
drh66105a82002-08-27 14:28:29 +0000262 if( z[1]!='*' || z[2]==0 ){
263 *tokenType = TK_SLASH;
264 return 1;
265 }
266 for(i=3; z[i] && (z[i]!='/' || z[i-1]!='*'); i++){}
267 if( z[i] ) i++;
268 *tokenType = TK_COMMENT;
269 return i;
drh75897232000-05-29 14:26:00 +0000270 }
drhbf4133c2001-10-13 02:59:08 +0000271 case '%': {
272 *tokenType = TK_REM;
273 return 1;
274 }
drh75897232000-05-29 14:26:00 +0000275 case '=': {
276 *tokenType = TK_EQ;
277 return 1 + (z[1]=='=');
278 }
279 case '<': {
280 if( z[1]=='=' ){
281 *tokenType = TK_LE;
282 return 2;
283 }else if( z[1]=='>' ){
284 *tokenType = TK_NE;
285 return 2;
drhbf4133c2001-10-13 02:59:08 +0000286 }else if( z[1]=='<' ){
287 *tokenType = TK_LSHIFT;
288 return 2;
drh75897232000-05-29 14:26:00 +0000289 }else{
290 *tokenType = TK_LT;
291 return 1;
292 }
293 }
294 case '>': {
295 if( z[1]=='=' ){
296 *tokenType = TK_GE;
297 return 2;
drhbf4133c2001-10-13 02:59:08 +0000298 }else if( z[1]=='>' ){
299 *tokenType = TK_RSHIFT;
300 return 2;
drh75897232000-05-29 14:26:00 +0000301 }else{
302 *tokenType = TK_GT;
303 return 1;
304 }
305 }
306 case '!': {
307 if( z[1]!='=' ){
308 *tokenType = TK_ILLEGAL;
drhc837e702000-06-08 16:26:24 +0000309 return 2;
drh75897232000-05-29 14:26:00 +0000310 }else{
311 *tokenType = TK_NE;
312 return 2;
313 }
314 }
drh00400772000-06-16 20:51:26 +0000315 case '|': {
316 if( z[1]!='|' ){
drhbf4133c2001-10-13 02:59:08 +0000317 *tokenType = TK_BITOR;
drh00400772000-06-16 20:51:26 +0000318 return 1;
319 }else{
320 *tokenType = TK_CONCAT;
321 return 2;
322 }
323 }
drh75897232000-05-29 14:26:00 +0000324 case ',': {
325 *tokenType = TK_COMMA;
326 return 1;
327 }
drhbf4133c2001-10-13 02:59:08 +0000328 case '&': {
329 *tokenType = TK_BITAND;
330 return 1;
331 }
332 case '~': {
333 *tokenType = TK_BITNOT;
334 return 1;
335 }
drh75897232000-05-29 14:26:00 +0000336 case '\'': case '"': {
337 int delim = z[0];
338 for(i=1; z[i]; i++){
339 if( z[i]==delim ){
340 if( z[i+1]==delim ){
341 i++;
342 }else{
343 break;
344 }
345 }
346 }
347 if( z[i] ) i++;
348 *tokenType = TK_STRING;
349 return i;
350 }
351 case '.': {
352 if( !isdigit(z[1]) ){
353 *tokenType = TK_DOT;
354 return 1;
355 }
356 /* Fall thru into the next case */
357 }
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++){}
drh75897232000-05-29 14:26:00 +0000362 if( z[i]=='.' ){
363 i++;
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;
375 }else if( z[0]=='.' ){
376 *tokenType = TK_FLOAT;
drh75897232000-05-29 14:26:00 +0000377 }
378 return i;
379 }
drh2f4392f2002-02-14 21:42:51 +0000380 case '[': {
381 for(i=1; z[i] && z[i-1]!=']'; i++){}
382 *tokenType = TK_ID;
383 return i;
384 }
drh98808ba2001-10-18 12:34:46 +0000385 default: {
386 if( !isIdChar[*z] ){
387 break;
388 }
389 for(i=1; isIdChar[z[i]]; i++){}
drh6a535342001-10-19 16:44:56 +0000390 *tokenType = sqliteKeywordCode((char*)z, i);
drh75897232000-05-29 14:26:00 +0000391 return i;
392 }
drh75897232000-05-29 14:26:00 +0000393 }
394 *tokenType = TK_ILLEGAL;
395 return 1;
396}
397
398/*
399** Run the parser on the given SQL string. The parser structure is
drhb19a2bc2001-09-16 00:13:26 +0000400** passed in. An SQLITE_ status code is returned. If an error occurs
401** and pzErrMsg!=NULL then an error message might be written into
402** memory obtained from malloc() and *pzErrMsg made to point to that
403** error message. Or maybe not.
drh75897232000-05-29 14:26:00 +0000404*/
drh80ff32f2001-11-04 18:32:46 +0000405int sqliteRunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
drh75897232000-05-29 14:26:00 +0000406 int nErr = 0;
407 int i;
408 void *pEngine;
drhb86ccfb2003-01-28 23:13:10 +0000409 int tokenType;
410 int lastTokenParsed = -1;
drh6d4abfb2001-10-22 02:58:08 +0000411 sqlite *db = pParse->db;
drh75897232000-05-29 14:26:00 +0000412 extern void *sqliteParserAlloc(void*(*)(int));
413 extern void sqliteParserFree(void*, void(*)(void*));
drh338ea132001-02-11 16:56:24 +0000414 extern int sqliteParser(void*, int, Token, Parse*);
drh75897232000-05-29 14:26:00 +0000415
drh6d4abfb2001-10-22 02:58:08 +0000416 db->flags &= ~SQLITE_Interrupt;
drh4c504392000-10-16 22:06:40 +0000417 pParse->rc = SQLITE_OK;
drh75897232000-05-29 14:26:00 +0000418 i = 0;
drh982cef72000-05-30 16:27:03 +0000419 pEngine = sqliteParserAlloc((void*(*)(int))malloc);
drh75897232000-05-29 14:26:00 +0000420 if( pEngine==0 ){
421 sqliteSetString(pzErrMsg, "out of memory", 0);
422 return 1;
423 }
drh32eb7b42003-01-07 01:44:37 +0000424 pParse->sLastToken.dyn = 0;
drh326dce72003-01-29 14:06:07 +0000425 pParse->zTail = zSql;
drh32eb7b42003-01-07 01:44:37 +0000426 while( sqlite_malloc_failed==0 && zSql[i]!=0 ){
drh75897232000-05-29 14:26:00 +0000427
drh32eb7b42003-01-07 01:44:37 +0000428 assert( i>=0 );
drh75897232000-05-29 14:26:00 +0000429 pParse->sLastToken.z = &zSql[i];
drh32eb7b42003-01-07 01:44:37 +0000430 assert( pParse->sLastToken.dyn==0 );
drh98808ba2001-10-18 12:34:46 +0000431 pParse->sLastToken.n = sqliteGetToken((unsigned char*)&zSql[i], &tokenType);
drh75897232000-05-29 14:26:00 +0000432 i += pParse->sLastToken.n;
drh75897232000-05-29 14:26:00 +0000433 switch( tokenType ){
434 case TK_SPACE:
drh75897232000-05-29 14:26:00 +0000435 case TK_COMMENT: {
drh32eb7b42003-01-07 01:44:37 +0000436 if( (db->flags & SQLITE_Interrupt)!=0 ){
437 pParse->rc = SQLITE_INTERRUPT;
438 sqliteSetString(pzErrMsg, "interrupt", 0);
439 goto abort_parse;
440 }
drh75897232000-05-29 14:26:00 +0000441 break;
442 }
drh32eb7b42003-01-07 01:44:37 +0000443 case TK_ILLEGAL: {
drhc837e702000-06-08 16:26:24 +0000444 sqliteSetNString(pzErrMsg, "unrecognized token: \"", -1,
445 pParse->sLastToken.z, pParse->sLastToken.n, "\"", 1, 0);
drh75897232000-05-29 14:26:00 +0000446 nErr++;
drhcaec2f12003-01-07 02:47:47 +0000447 goto abort_parse;
drh32eb7b42003-01-07 01:44:37 +0000448 }
drh326dce72003-01-29 14:06:07 +0000449 case TK_SEMI: {
450 pParse->zTail = &zSql[i];
451 /* Fall thru into the default case */
452 }
drh32eb7b42003-01-07 01:44:37 +0000453 default: {
drh75897232000-05-29 14:26:00 +0000454 sqliteParser(pEngine, tokenType, pParse->sLastToken, pParse);
drhb86ccfb2003-01-28 23:13:10 +0000455 lastTokenParsed = tokenType;
456 if( pParse->rc!=SQLITE_OK ){
drh32eb7b42003-01-07 01:44:37 +0000457 goto abort_parse;
drh75897232000-05-29 14:26:00 +0000458 }
459 break;
drh32eb7b42003-01-07 01:44:37 +0000460 }
drh75897232000-05-29 14:26:00 +0000461 }
462 }
drh32eb7b42003-01-07 01:44:37 +0000463abort_parse:
drhb86ccfb2003-01-28 23:13:10 +0000464 if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
465 if( lastTokenParsed!=TK_SEMI ){
466 sqliteParser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
drh326dce72003-01-29 14:06:07 +0000467 pParse->zTail = &zSql[i];
drh75897232000-05-29 14:26:00 +0000468 }
drhb86ccfb2003-01-28 23:13:10 +0000469 sqliteParser(pEngine, 0, pParse->sLastToken, pParse);
drh75897232000-05-29 14:26:00 +0000470 }
drhdcc581c2000-05-30 13:44:19 +0000471 sqliteParserFree(pEngine, free);
drhb86ccfb2003-01-28 23:13:10 +0000472 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
473 sqliteSetString(&pParse->zErrMsg, sqlite_error_string(pParse->rc), 0);
474 }
drh75897232000-05-29 14:26:00 +0000475 if( pParse->zErrMsg ){
drhb86ccfb2003-01-28 23:13:10 +0000476 if( pzErrMsg && *pzErrMsg==0 ){
drh75897232000-05-29 14:26:00 +0000477 *pzErrMsg = pParse->zErrMsg;
478 }else{
479 sqliteFree(pParse->zErrMsg);
480 }
drhb86ccfb2003-01-28 23:13:10 +0000481 pParse->zErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000482 if( !nErr ) nErr++;
483 }
drhb86ccfb2003-01-28 23:13:10 +0000484 if( pParse->pVdbe && (pParse->useCallback || pParse->nErr>0) ){
drh75897232000-05-29 14:26:00 +0000485 sqliteVdbeDelete(pParse->pVdbe);
486 pParse->pVdbe = 0;
487 }
488 if( pParse->pNewTable ){
489 sqliteDeleteTable(pParse->db, pParse->pNewTable);
490 pParse->pNewTable = 0;
491 }
drhb86ccfb2003-01-28 23:13:10 +0000492 if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){
drh4c504392000-10-16 22:06:40 +0000493 pParse->rc = SQLITE_ERROR;
494 }
drh75897232000-05-29 14:26:00 +0000495 return nErr;
496}