blob: 3e5bd9de9468b6f03d6d942362c6388a409dd73c [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**
drh200a81d2008-08-08 14:19:41 +000018** $Id: tokenize.c,v 1.150 2008/08/08 14:19:41 drh Exp $
drh75897232000-05-29 14:26:00 +000019*/
20#include "sqliteInt.h"
21#include <ctype.h>
drhdcc581c2000-05-30 13:44:19 +000022#include <stdlib.h>
drh75897232000-05-29 14:26:00 +000023
24/*
drh9b8f4472006-04-04 01:54:55 +000025** The charMap() macro maps alphabetic characters into their
26** lower-case ASCII equivalent. On ASCII machines, this is just
27** an upper-to-lower case map. On EBCDIC machines we also need
28** to adjust the encoding. Only alphabetic characters and underscores
29** need to be translated.
30*/
31#ifdef SQLITE_ASCII
32# define charMap(X) sqlite3UpperToLower[(unsigned char)X]
33#endif
34#ifdef SQLITE_EBCDIC
35# define charMap(X) ebcdicToAscii[(unsigned char)X]
36const unsigned char ebcdicToAscii[] = {
37/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
38 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
39 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
40 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
41 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */
42 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */
43 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */
44 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */
45 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */
46 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */
47 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */
48 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */
49 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
50 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */
51 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */
52 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */
53 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */
54};
55#endif
56
57/*
drh52fb6d72004-11-03 03:59:57 +000058** The sqlite3KeywordCode function looks up an identifier to determine if
59** it is a keyword. If it is a keyword, the token code of that keyword is
drh75897232000-05-29 14:26:00 +000060** returned. If the input is not a keyword, TK_ID is returned.
drh2090a0e2004-10-07 19:03:01 +000061**
62** The implementation of this routine was generated by a program,
drh73b211a2005-01-18 04:00:42 +000063** mkkeywordhash.h, located in the tool subdirectory of the distribution.
drh52fb6d72004-11-03 03:59:57 +000064** The output of the mkkeywordhash.c program is written into a file
drh73b211a2005-01-18 04:00:42 +000065** named keywordhash.h and then included into this source file by
drh52fb6d72004-11-03 03:59:57 +000066** the #include below.
drh75897232000-05-29 14:26:00 +000067*/
drh73b211a2005-01-18 04:00:42 +000068#include "keywordhash.h"
drh75897232000-05-29 14:26:00 +000069
drh40f20f72004-10-23 05:10:18 +000070
drh98808ba2001-10-18 12:34:46 +000071/*
drh9b8f4472006-04-04 01:54:55 +000072** If X is a character that can be used in an identifier then
73** IdChar(X) will be true. Otherwise it is false.
drh98808ba2001-10-18 12:34:46 +000074**
drh9b8f4472006-04-04 01:54:55 +000075** For ASCII, any character with the high-order bit set is
76** allowed in an identifier. For 7-bit characters,
77** sqlite3IsIdChar[X] must be 1.
78**
79** For EBCDIC, the rules are more complex but have the same
80** end result.
drha0d1f662005-01-11 17:59:47 +000081**
82** Ticket #1066. the SQL standard does not allow '$' in the
83** middle of identfiers. But many SQL implementations do.
84** SQLite will allow '$' in identifiers for compatibility.
85** But the feature is undocumented.
drh98808ba2001-10-18 12:34:46 +000086*/
drh9b8f4472006-04-04 01:54:55 +000087#ifdef SQLITE_ASCII
drh46c99e02007-08-27 23:26:59 +000088const char sqlite3IsAsciiIdChar[] = {
drh98808ba2001-10-18 12:34:46 +000089/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
drha0d1f662005-01-11 17:59:47 +000090 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
drh98808ba2001-10-18 12:34:46 +000091 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
92 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
93 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
94 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
95 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
drh98808ba2001-10-18 12:34:46 +000096};
drh46c99e02007-08-27 23:26:59 +000097#define IdChar(C) (((c=C)&0x80)!=0 || (c>0x1f && sqlite3IsAsciiIdChar[c-0x20]))
drh9b8f4472006-04-04 01:54:55 +000098#endif
99#ifdef SQLITE_EBCDIC
drh46c99e02007-08-27 23:26:59 +0000100const char sqlite3IsEbcdicIdChar[] = {
drh9b8f4472006-04-04 01:54:55 +0000101/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
102 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */
103 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */
104 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */
105 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */
106 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */
107 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */
108 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */
109 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
110 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */
111 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */
112 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */
113 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */
114};
drh46c99e02007-08-27 23:26:59 +0000115#define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
drh9b8f4472006-04-04 01:54:55 +0000116#endif
117
drh98808ba2001-10-18 12:34:46 +0000118
drh75897232000-05-29 14:26:00 +0000119/*
drh61b487d2003-09-12 02:08:14 +0000120** Return the length of the token that begins at z[0].
121** Store the token type in *tokenType before returning.
drh75897232000-05-29 14:26:00 +0000122*/
drh35b5a332008-04-05 18:41:42 +0000123int sqlite3GetToken(const unsigned char *z, int *tokenType){
drhaa756b02004-09-25 15:25:26 +0000124 int i, c;
drh75897232000-05-29 14:26:00 +0000125 switch( *z ){
drh30cab802000-08-09 17:17:25 +0000126 case ' ': case '\t': case '\n': case '\f': case '\r': {
drh32eb7b42003-01-07 01:44:37 +0000127 for(i=1; isspace(z[i]); i++){}
drh75897232000-05-29 14:26:00 +0000128 *tokenType = TK_SPACE;
129 return i;
130 }
131 case '-': {
drh75897232000-05-29 14:26:00 +0000132 if( z[1]=='-' ){
drhaa756b02004-09-25 15:25:26 +0000133 for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
drh200a81d2008-08-08 14:19:41 +0000134 *tokenType = TK_SPACE;
drh75897232000-05-29 14:26:00 +0000135 return i;
136 }
137 *tokenType = TK_MINUS;
138 return 1;
139 }
140 case '(': {
drhdab35182003-09-27 13:39:38 +0000141 *tokenType = TK_LP;
142 return 1;
drh75897232000-05-29 14:26:00 +0000143 }
144 case ')': {
145 *tokenType = TK_RP;
146 return 1;
147 }
148 case ';': {
149 *tokenType = TK_SEMI;
150 return 1;
151 }
152 case '+': {
153 *tokenType = TK_PLUS;
154 return 1;
155 }
156 case '*': {
157 *tokenType = TK_STAR;
158 return 1;
159 }
160 case '/': {
drh66105a82002-08-27 14:28:29 +0000161 if( z[1]!='*' || z[2]==0 ){
162 *tokenType = TK_SLASH;
163 return 1;
164 }
drhaa756b02004-09-25 15:25:26 +0000165 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
166 if( c ) i++;
drh200a81d2008-08-08 14:19:41 +0000167 *tokenType = TK_SPACE;
drh66105a82002-08-27 14:28:29 +0000168 return i;
drh75897232000-05-29 14:26:00 +0000169 }
drhbf4133c2001-10-13 02:59:08 +0000170 case '%': {
171 *tokenType = TK_REM;
172 return 1;
173 }
drh75897232000-05-29 14:26:00 +0000174 case '=': {
175 *tokenType = TK_EQ;
176 return 1 + (z[1]=='=');
177 }
178 case '<': {
drhaa756b02004-09-25 15:25:26 +0000179 if( (c=z[1])=='=' ){
drh75897232000-05-29 14:26:00 +0000180 *tokenType = TK_LE;
181 return 2;
drhaa756b02004-09-25 15:25:26 +0000182 }else if( c=='>' ){
drh75897232000-05-29 14:26:00 +0000183 *tokenType = TK_NE;
184 return 2;
drhaa756b02004-09-25 15:25:26 +0000185 }else if( c=='<' ){
drhbf4133c2001-10-13 02:59:08 +0000186 *tokenType = TK_LSHIFT;
187 return 2;
drh75897232000-05-29 14:26:00 +0000188 }else{
189 *tokenType = TK_LT;
190 return 1;
191 }
192 }
193 case '>': {
drhaa756b02004-09-25 15:25:26 +0000194 if( (c=z[1])=='=' ){
drh75897232000-05-29 14:26:00 +0000195 *tokenType = TK_GE;
196 return 2;
drhaa756b02004-09-25 15:25:26 +0000197 }else if( c=='>' ){
drhbf4133c2001-10-13 02:59:08 +0000198 *tokenType = TK_RSHIFT;
199 return 2;
drh75897232000-05-29 14:26:00 +0000200 }else{
201 *tokenType = TK_GT;
202 return 1;
203 }
204 }
205 case '!': {
206 if( z[1]!='=' ){
207 *tokenType = TK_ILLEGAL;
drhc837e702000-06-08 16:26:24 +0000208 return 2;
drh75897232000-05-29 14:26:00 +0000209 }else{
210 *tokenType = TK_NE;
211 return 2;
212 }
213 }
drh00400772000-06-16 20:51:26 +0000214 case '|': {
215 if( z[1]!='|' ){
drhbf4133c2001-10-13 02:59:08 +0000216 *tokenType = TK_BITOR;
drh00400772000-06-16 20:51:26 +0000217 return 1;
218 }else{
219 *tokenType = TK_CONCAT;
220 return 2;
221 }
222 }
drh75897232000-05-29 14:26:00 +0000223 case ',': {
224 *tokenType = TK_COMMA;
225 return 1;
226 }
drhbf4133c2001-10-13 02:59:08 +0000227 case '&': {
228 *tokenType = TK_BITAND;
229 return 1;
230 }
231 case '~': {
232 *tokenType = TK_BITNOT;
233 return 1;
234 }
drh3d946622005-08-13 18:15:42 +0000235 case '`':
236 case '\'':
237 case '"': {
drh75897232000-05-29 14:26:00 +0000238 int delim = z[0];
drhaa756b02004-09-25 15:25:26 +0000239 for(i=1; (c=z[i])!=0; i++){
240 if( c==delim ){
drh75897232000-05-29 14:26:00 +0000241 if( z[i+1]==delim ){
242 i++;
243 }else{
244 break;
245 }
246 }
247 }
drha33cb5f2008-08-07 13:05:34 +0000248 if( c=='\'' ){
drheef8b552005-10-23 11:29:40 +0000249 *tokenType = TK_STRING;
250 return i+1;
drha33cb5f2008-08-07 13:05:34 +0000251 }else if( c!=0 ){
252 *tokenType = TK_ID;
253 return i+1;
drheef8b552005-10-23 11:29:40 +0000254 }else{
255 *tokenType = TK_ILLEGAL;
256 return i;
257 }
drh75897232000-05-29 14:26:00 +0000258 }
259 case '.': {
drh76816182005-08-23 11:31:26 +0000260#ifndef SQLITE_OMIT_FLOATING_POINT
261 if( !isdigit(z[1]) )
262#endif
263 {
264 *tokenType = TK_DOT;
265 return 1;
266 }
267 /* If the next character is a digit, this is a floating point
268 ** number that begins with ".". Fall thru into the next case */
drh75897232000-05-29 14:26:00 +0000269 }
270 case '0': case '1': case '2': case '3': case '4':
271 case '5': case '6': case '7': case '8': case '9': {
drhc837e702000-06-08 16:26:24 +0000272 *tokenType = TK_INTEGER;
drh76816182005-08-23 11:31:26 +0000273 for(i=0; isdigit(z[i]); i++){}
drhb7f91642004-10-31 02:22:47 +0000274#ifndef SQLITE_OMIT_FLOATING_POINT
drh76816182005-08-23 11:31:26 +0000275 if( z[i]=='.' ){
276 i++;
drh32eb7b42003-01-07 01:44:37 +0000277 while( isdigit(z[i]) ){ i++; }
drhc837e702000-06-08 16:26:24 +0000278 *tokenType = TK_FLOAT;
279 }
280 if( (z[i]=='e' || z[i]=='E') &&
drh75897232000-05-29 14:26:00 +0000281 ( isdigit(z[i+1])
282 || ((z[i+1]=='+' || z[i+1]=='-') && isdigit(z[i+2]))
283 )
drhc837e702000-06-08 16:26:24 +0000284 ){
285 i += 2;
drh32eb7b42003-01-07 01:44:37 +0000286 while( isdigit(z[i]) ){ i++; }
drh75897232000-05-29 14:26:00 +0000287 *tokenType = TK_FLOAT;
drh75897232000-05-29 14:26:00 +0000288 }
drhb7f91642004-10-31 02:22:47 +0000289#endif
drh67dd9012006-08-12 12:33:14 +0000290 while( IdChar(z[i]) ){
291 *tokenType = TK_ILLEGAL;
292 i++;
293 }
drh75897232000-05-29 14:26:00 +0000294 return i;
295 }
drh2f4392f2002-02-14 21:42:51 +0000296 case '[': {
drhaa756b02004-09-25 15:25:26 +0000297 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
drh4b2f9362008-01-22 23:37:09 +0000298 *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
drh2f4392f2002-02-14 21:42:51 +0000299 return i;
300 }
drh7c972de2003-09-06 22:18:07 +0000301 case '?': {
drh50457892003-09-06 01:10:47 +0000302 *tokenType = TK_VARIABLE;
drhfa6bc002004-09-07 16:19:52 +0000303 for(i=1; isdigit(z[i]); i++){}
304 return i;
drh50457892003-09-06 01:10:47 +0000305 }
drh288d37f2005-06-22 08:48:06 +0000306 case '#': {
307 for(i=1; isdigit(z[i]); i++){}
308 if( i>1 ){
309 /* Parameters of the form #NNN (where NNN is a number) are used
310 ** internally by sqlite3NestedParse. */
311 *tokenType = TK_REGISTER;
312 return i;
313 }
314 /* Fall through into the next case if the '#' is not followed by
315 ** a digit. Try to match #AAAA where AAAA is a parameter name. */
drh895d7472004-08-20 16:02:39 +0000316 }
drhb7f91642004-10-31 02:22:47 +0000317#ifndef SQLITE_OMIT_TCL_VARIABLE
drh288d37f2005-06-22 08:48:06 +0000318 case '$':
319#endif
drh0b2a5ee2006-02-09 22:24:41 +0000320 case '@': /* For compatibility with MS SQL Server */
drh288d37f2005-06-22 08:48:06 +0000321 case ':': {
322 int n = 0;
drh9d74b4c2004-08-24 15:23:34 +0000323 *tokenType = TK_VARIABLE;
drh288d37f2005-06-22 08:48:06 +0000324 for(i=1; (c=z[i])!=0; i++){
325 if( IdChar(c) ){
326 n++;
327#ifndef SQLITE_OMIT_TCL_VARIABLE
328 }else if( c=='(' && n>0 ){
329 do{
330 i++;
331 }while( (c=z[i])!=0 && !isspace(c) && c!=')' );
332 if( c==')' ){
drh895d7472004-08-20 16:02:39 +0000333 i++;
334 }else{
drh288d37f2005-06-22 08:48:06 +0000335 *tokenType = TK_ILLEGAL;
drh895d7472004-08-20 16:02:39 +0000336 }
drh288d37f2005-06-22 08:48:06 +0000337 break;
338 }else if( c==':' && z[i+1]==':' ){
339 i++;
340#endif
341 }else{
342 break;
drh895d7472004-08-20 16:02:39 +0000343 }
344 }
drh288d37f2005-06-22 08:48:06 +0000345 if( n==0 ) *tokenType = TK_ILLEGAL;
drh895d7472004-08-20 16:02:39 +0000346 return i;
drhb7f91642004-10-31 02:22:47 +0000347 }
drhb7f91642004-10-31 02:22:47 +0000348#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +0000349 case 'x': case 'X': {
drh4b2f9362008-01-22 23:37:09 +0000350 if( z[1]=='\'' ){
danielk19773fd0a732004-05-27 13:35:19 +0000351 *tokenType = TK_BLOB;
drh4b2f9362008-01-22 23:37:09 +0000352 for(i=2; (c=z[i])!=0 && c!='\''; i++){
drhaa756b02004-09-25 15:25:26 +0000353 if( !isxdigit(c) ){
danielk19773fd0a732004-05-27 13:35:19 +0000354 *tokenType = TK_ILLEGAL;
danielk19773fd0a732004-05-27 13:35:19 +0000355 }
danielk1977c572ef72004-05-27 09:28:41 +0000356 }
drh4b2f9362008-01-22 23:37:09 +0000357 if( i%2 || !c ) *tokenType = TK_ILLEGAL;
drhaa756b02004-09-25 15:25:26 +0000358 if( c ) i++;
danielk1977c572ef72004-05-27 09:28:41 +0000359 return i;
360 }
361 /* Otherwise fall through to the next case */
362 }
drhb7f91642004-10-31 02:22:47 +0000363#endif
drh98808ba2001-10-18 12:34:46 +0000364 default: {
drh9a087a92007-05-15 14:34:32 +0000365 if( !IdChar(*z) ){
drh98808ba2001-10-18 12:34:46 +0000366 break;
367 }
drhaa756b02004-09-25 15:25:26 +0000368 for(i=1; IdChar(z[i]); i++){}
danielk1977c60e9b82005-01-31 12:42:29 +0000369 *tokenType = keywordCode((char*)z, i);
drh75897232000-05-29 14:26:00 +0000370 return i;
371 }
drh75897232000-05-29 14:26:00 +0000372 }
373 *tokenType = TK_ILLEGAL;
374 return 1;
375}
376
377/*
378** Run the parser on the given SQL string. The parser structure is
drhb19a2bc2001-09-16 00:13:26 +0000379** passed in. An SQLITE_ status code is returned. If an error occurs
drhfb45d8c2008-07-08 00:06:49 +0000380** then an and attempt is made to write an error message into
381** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
382** error message.
drh75897232000-05-29 14:26:00 +0000383*/
danielk19774adee202004-05-08 08:23:19 +0000384int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
drh75897232000-05-29 14:26:00 +0000385 int nErr = 0;
386 int i;
387 void *pEngine;
drhb86ccfb2003-01-28 23:13:10 +0000388 int tokenType;
389 int lastTokenParsed = -1;
drh9bb575f2004-09-06 17:24:11 +0000390 sqlite3 *db = pParse->db;
drhbb4957f2008-03-20 14:03:29 +0000391 int mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
drh75897232000-05-29 14:26:00 +0000392
drh15ca1df2006-07-26 13:43:30 +0000393 if( db->activeVdbeCnt==0 ){
394 db->u1.isInterrupted = 0;
395 }
drh4c504392000-10-16 22:06:40 +0000396 pParse->rc = SQLITE_OK;
drh4b1d66c2008-04-03 21:42:21 +0000397 pParse->zTail = pParse->zSql = zSql;
drh75897232000-05-29 14:26:00 +0000398 i = 0;
drhfb45d8c2008-07-08 00:06:49 +0000399 assert( pzErrMsg!=0 );
drhe5ae5732008-06-15 02:51:47 +0000400 pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3Malloc);
drh75897232000-05-29 14:26:00 +0000401 if( pEngine==0 ){
drhf3a65f72007-08-22 20:18:21 +0000402 db->mallocFailed = 1;
drhdefc9972005-06-06 14:45:42 +0000403 return SQLITE_NOMEM;
drh75897232000-05-29 14:26:00 +0000404 }
drhfa6bc002004-09-07 16:19:52 +0000405 assert( pParse->sLastToken.dyn==0 );
406 assert( pParse->pNewTable==0 );
407 assert( pParse->pNewTrigger==0 );
408 assert( pParse->nVar==0 );
409 assert( pParse->nVarExpr==0 );
410 assert( pParse->nVarExprAlloc==0 );
411 assert( pParse->apVarExpr==0 );
drh17435752007-08-16 04:30:38 +0000412 while( !db->mallocFailed && zSql[i]!=0 ){
drh32eb7b42003-01-07 01:44:37 +0000413 assert( i>=0 );
drh2646da72005-12-09 20:02:05 +0000414 pParse->sLastToken.z = (u8*)&zSql[i];
drh32eb7b42003-01-07 01:44:37 +0000415 assert( pParse->sLastToken.dyn==0 );
drh35b5a332008-04-05 18:41:42 +0000416 pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
drh75897232000-05-29 14:26:00 +0000417 i += pParse->sLastToken.n;
drhbb4957f2008-03-20 14:03:29 +0000418 if( i>mxSqlLen ){
drhe5c941b2007-05-08 13:58:26 +0000419 pParse->rc = SQLITE_TOOBIG;
420 break;
421 }
drh75897232000-05-29 14:26:00 +0000422 switch( tokenType ){
drh200a81d2008-08-08 14:19:41 +0000423 case TK_SPACE: {
drh881feaa2006-07-26 01:39:30 +0000424 if( db->u1.isInterrupted ){
drh32eb7b42003-01-07 01:44:37 +0000425 pParse->rc = SQLITE_INTERRUPT;
drhf089aa42008-07-08 19:34:06 +0000426 sqlite3SetString(pzErrMsg, db, "interrupt");
drh32eb7b42003-01-07 01:44:37 +0000427 goto abort_parse;
428 }
drh75897232000-05-29 14:26:00 +0000429 break;
430 }
drh32eb7b42003-01-07 01:44:37 +0000431 case TK_ILLEGAL: {
drh633e6d52008-07-28 19:34:53 +0000432 sqlite3DbFree(db, *pzErrMsg);
drhfb45d8c2008-07-08 00:06:49 +0000433 *pzErrMsg = sqlite3MPrintf(db, "unrecognized token: \"%T\"",
434 &pParse->sLastToken);
drh75897232000-05-29 14:26:00 +0000435 nErr++;
drhcaec2f12003-01-07 02:47:47 +0000436 goto abort_parse;
drh32eb7b42003-01-07 01:44:37 +0000437 }
drh326dce72003-01-29 14:06:07 +0000438 case TK_SEMI: {
439 pParse->zTail = &zSql[i];
440 /* Fall thru into the default case */
441 }
drh32eb7b42003-01-07 01:44:37 +0000442 default: {
danielk19774adee202004-05-08 08:23:19 +0000443 sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
drhb86ccfb2003-01-28 23:13:10 +0000444 lastTokenParsed = tokenType;
445 if( pParse->rc!=SQLITE_OK ){
drh32eb7b42003-01-07 01:44:37 +0000446 goto abort_parse;
drh75897232000-05-29 14:26:00 +0000447 }
448 break;
drh32eb7b42003-01-07 01:44:37 +0000449 }
drh75897232000-05-29 14:26:00 +0000450 }
451 }
drh32eb7b42003-01-07 01:44:37 +0000452abort_parse:
drhb86ccfb2003-01-28 23:13:10 +0000453 if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
454 if( lastTokenParsed!=TK_SEMI ){
danielk19774adee202004-05-08 08:23:19 +0000455 sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
drh326dce72003-01-29 14:06:07 +0000456 pParse->zTail = &zSql[i];
drh75897232000-05-29 14:26:00 +0000457 }
danielk19774adee202004-05-08 08:23:19 +0000458 sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
drh75897232000-05-29 14:26:00 +0000459 }
drhec424a52008-07-25 15:39:03 +0000460#ifdef YYTRACKMAXSTACKDEPTH
461 sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK,
462 sqlite3ParserStackPeak(pEngine)
463 );
464#endif /* YYDEBUG */
drh17435752007-08-16 04:30:38 +0000465 sqlite3ParserFree(pEngine, sqlite3_free);
466 if( db->mallocFailed ){
drh71c697e2004-08-08 23:39:19 +0000467 pParse->rc = SQLITE_NOMEM;
468 }
drhb86ccfb2003-01-28 23:13:10 +0000469 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
drhf089aa42008-07-08 19:34:06 +0000470 sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc));
drhb86ccfb2003-01-28 23:13:10 +0000471 }
drh75897232000-05-29 14:26:00 +0000472 if( pParse->zErrMsg ){
drhfb45d8c2008-07-08 00:06:49 +0000473 if( *pzErrMsg==0 ){
drh75897232000-05-29 14:26:00 +0000474 *pzErrMsg = pParse->zErrMsg;
475 }else{
drh633e6d52008-07-28 19:34:53 +0000476 sqlite3DbFree(db, pParse->zErrMsg);
drh75897232000-05-29 14:26:00 +0000477 }
drhb86ccfb2003-01-28 23:13:10 +0000478 pParse->zErrMsg = 0;
drh4b2f9362008-01-22 23:37:09 +0000479 nErr++;
drh75897232000-05-29 14:26:00 +0000480 }
drh2958a4e2004-11-12 03:56:15 +0000481 if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
danielk19774adee202004-05-08 08:23:19 +0000482 sqlite3VdbeDelete(pParse->pVdbe);
drh75897232000-05-29 14:26:00 +0000483 pParse->pVdbe = 0;
484 }
danielk1977c00da102006-01-07 13:21:04 +0000485#ifndef SQLITE_OMIT_SHARED_CACHE
486 if( pParse->nested==0 ){
drh633e6d52008-07-28 19:34:53 +0000487 sqlite3DbFree(db, pParse->aTableLock);
danielk1977c00da102006-01-07 13:21:04 +0000488 pParse->aTableLock = 0;
489 pParse->nTableLock = 0;
490 }
491#endif
drh4f3dd152008-04-28 18:46:43 +0000492#ifndef SQLITE_OMIT_VIRTUALTABLE
drh633e6d52008-07-28 19:34:53 +0000493 sqlite3DbFree(db, pParse->apVtabLock);
drh4f3dd152008-04-28 18:46:43 +0000494#endif
danielk19777e6ebfb2006-06-12 11:24:37 +0000495
danielk19777e6ebfb2006-06-12 11:24:37 +0000496 if( !IN_DECLARE_VTAB ){
497 /* If the pParse->declareVtab flag is set, do not delete any table
498 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
499 ** will take responsibility for freeing the Table structure.
500 */
danielk1977a04a34f2007-04-16 15:06:25 +0000501 sqlite3DeleteTable(pParse->pNewTable);
danielk19777e6ebfb2006-06-12 11:24:37 +0000502 }
503
drh633e6d52008-07-28 19:34:53 +0000504 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
505 sqlite3DbFree(db, pParse->apVarExpr);
drhb86ccfb2003-01-28 23:13:10 +0000506 if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){
drh4c504392000-10-16 22:06:40 +0000507 pParse->rc = SQLITE_ERROR;
508 }
drh75897232000-05-29 14:26:00 +0000509 return nErr;
510}