blob: a9e0167b56dab9b4a8dd16df53292c1acbede4ab [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**
drheab7f3f2007-05-15 09:00:14 +000018** $Id: tokenize.c,v 1.128 2007/05/15 09:00:15 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/*
drh9b8f4472006-04-04 01:54:55 +000026** The charMap() macro maps alphabetic characters into their
27** lower-case ASCII equivalent. On ASCII machines, this is just
28** an upper-to-lower case map. On EBCDIC machines we also need
29** to adjust the encoding. Only alphabetic characters and underscores
30** need to be translated.
31*/
32#ifdef SQLITE_ASCII
33# define charMap(X) sqlite3UpperToLower[(unsigned char)X]
34#endif
35#ifdef SQLITE_EBCDIC
36# define charMap(X) ebcdicToAscii[(unsigned char)X]
37const unsigned char ebcdicToAscii[] = {
38/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
39 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
40 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
41 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
42 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */
43 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */
44 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */
45 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */
46 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */
47 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */
48 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */
49 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */
50 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
51 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */
52 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */
53 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */
54 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */
55};
56#endif
57
58/*
drh52fb6d72004-11-03 03:59:57 +000059** The sqlite3KeywordCode function looks up an identifier to determine if
60** it is a keyword. If it is a keyword, the token code of that keyword is
drh75897232000-05-29 14:26:00 +000061** returned. If the input is not a keyword, TK_ID is returned.
drh2090a0e2004-10-07 19:03:01 +000062**
63** The implementation of this routine was generated by a program,
drh73b211a2005-01-18 04:00:42 +000064** mkkeywordhash.h, located in the tool subdirectory of the distribution.
drh52fb6d72004-11-03 03:59:57 +000065** The output of the mkkeywordhash.c program is written into a file
drh73b211a2005-01-18 04:00:42 +000066** named keywordhash.h and then included into this source file by
drh52fb6d72004-11-03 03:59:57 +000067** the #include below.
drh75897232000-05-29 14:26:00 +000068*/
drh73b211a2005-01-18 04:00:42 +000069#include "keywordhash.h"
drh75897232000-05-29 14:26:00 +000070
drh40f20f72004-10-23 05:10:18 +000071
drh98808ba2001-10-18 12:34:46 +000072/*
drh9b8f4472006-04-04 01:54:55 +000073** If X is a character that can be used in an identifier then
74** IdChar(X) will be true. Otherwise it is false.
drh98808ba2001-10-18 12:34:46 +000075**
drh9b8f4472006-04-04 01:54:55 +000076** For ASCII, any character with the high-order bit set is
77** allowed in an identifier. For 7-bit characters,
78** sqlite3IsIdChar[X] must be 1.
79**
80** For EBCDIC, the rules are more complex but have the same
81** end result.
drha0d1f662005-01-11 17:59:47 +000082**
83** Ticket #1066. the SQL standard does not allow '$' in the
84** middle of identfiers. But many SQL implementations do.
85** SQLite will allow '$' in identifiers for compatibility.
86** But the feature is undocumented.
drh98808ba2001-10-18 12:34:46 +000087*/
drh9b8f4472006-04-04 01:54:55 +000088#ifdef SQLITE_ASCII
drha2b902d2005-08-14 17:53:20 +000089const char sqlite3IsIdChar[] = {
drh98808ba2001-10-18 12:34:46 +000090/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
drha0d1f662005-01-11 17:59:47 +000091 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
drh98808ba2001-10-18 12:34:46 +000092 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
93 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
94 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
95 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
96 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
drh98808ba2001-10-18 12:34:46 +000097};
drha2b902d2005-08-14 17:53:20 +000098#define IdChar(C) (((c=C)&0x80)!=0 || (c>0x1f && sqlite3IsIdChar[c-0x20]))
drh9b8f4472006-04-04 01:54:55 +000099#endif
100#ifdef SQLITE_EBCDIC
101const char sqlite3IsIdChar[] = {
102/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
103 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */
104 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */
105 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */
106 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */
107 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */
108 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */
109 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */
110 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
111 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */
112 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */
113 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */
114 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */
115};
116#define IdChar(C) (((c=C)>=0x42 && sqlite3IsIdChar[c-0x40]))
117#endif
118
drh98808ba2001-10-18 12:34:46 +0000119
drh75897232000-05-29 14:26:00 +0000120/*
drh61b487d2003-09-12 02:08:14 +0000121** Return the length of the token that begins at z[0].
122** Store the token type in *tokenType before returning.
drh75897232000-05-29 14:26:00 +0000123*/
danielk1977c60e9b82005-01-31 12:42:29 +0000124static int getToken(const unsigned char *z, int *tokenType){
drhaa756b02004-09-25 15:25:26 +0000125 int i, c;
drh75897232000-05-29 14:26:00 +0000126 switch( *z ){
drh30cab802000-08-09 17:17:25 +0000127 case ' ': case '\t': case '\n': case '\f': case '\r': {
drh32eb7b42003-01-07 01:44:37 +0000128 for(i=1; isspace(z[i]); i++){}
drh75897232000-05-29 14:26:00 +0000129 *tokenType = TK_SPACE;
130 return i;
131 }
132 case '-': {
drh75897232000-05-29 14:26:00 +0000133 if( z[1]=='-' ){
drhaa756b02004-09-25 15:25:26 +0000134 for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
drh75897232000-05-29 14:26:00 +0000135 *tokenType = TK_COMMENT;
136 return i;
137 }
138 *tokenType = TK_MINUS;
139 return 1;
140 }
141 case '(': {
drhdab35182003-09-27 13:39:38 +0000142 *tokenType = TK_LP;
143 return 1;
drh75897232000-05-29 14:26:00 +0000144 }
145 case ')': {
146 *tokenType = TK_RP;
147 return 1;
148 }
149 case ';': {
150 *tokenType = TK_SEMI;
151 return 1;
152 }
153 case '+': {
154 *tokenType = TK_PLUS;
155 return 1;
156 }
157 case '*': {
158 *tokenType = TK_STAR;
159 return 1;
160 }
161 case '/': {
drh66105a82002-08-27 14:28:29 +0000162 if( z[1]!='*' || z[2]==0 ){
163 *tokenType = TK_SLASH;
164 return 1;
165 }
drhaa756b02004-09-25 15:25:26 +0000166 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
167 if( c ) i++;
drh66105a82002-08-27 14:28:29 +0000168 *tokenType = TK_COMMENT;
169 return i;
drh75897232000-05-29 14:26:00 +0000170 }
drhbf4133c2001-10-13 02:59:08 +0000171 case '%': {
172 *tokenType = TK_REM;
173 return 1;
174 }
drh75897232000-05-29 14:26:00 +0000175 case '=': {
176 *tokenType = TK_EQ;
177 return 1 + (z[1]=='=');
178 }
179 case '<': {
drhaa756b02004-09-25 15:25:26 +0000180 if( (c=z[1])=='=' ){
drh75897232000-05-29 14:26:00 +0000181 *tokenType = TK_LE;
182 return 2;
drhaa756b02004-09-25 15:25:26 +0000183 }else if( c=='>' ){
drh75897232000-05-29 14:26:00 +0000184 *tokenType = TK_NE;
185 return 2;
drhaa756b02004-09-25 15:25:26 +0000186 }else if( c=='<' ){
drhbf4133c2001-10-13 02:59:08 +0000187 *tokenType = TK_LSHIFT;
188 return 2;
drh75897232000-05-29 14:26:00 +0000189 }else{
190 *tokenType = TK_LT;
191 return 1;
192 }
193 }
194 case '>': {
drhaa756b02004-09-25 15:25:26 +0000195 if( (c=z[1])=='=' ){
drh75897232000-05-29 14:26:00 +0000196 *tokenType = TK_GE;
197 return 2;
drhaa756b02004-09-25 15:25:26 +0000198 }else if( c=='>' ){
drhbf4133c2001-10-13 02:59:08 +0000199 *tokenType = TK_RSHIFT;
200 return 2;
drh75897232000-05-29 14:26:00 +0000201 }else{
202 *tokenType = TK_GT;
203 return 1;
204 }
205 }
206 case '!': {
207 if( z[1]!='=' ){
208 *tokenType = TK_ILLEGAL;
drhc837e702000-06-08 16:26:24 +0000209 return 2;
drh75897232000-05-29 14:26:00 +0000210 }else{
211 *tokenType = TK_NE;
212 return 2;
213 }
214 }
drh00400772000-06-16 20:51:26 +0000215 case '|': {
216 if( z[1]!='|' ){
drhbf4133c2001-10-13 02:59:08 +0000217 *tokenType = TK_BITOR;
drh00400772000-06-16 20:51:26 +0000218 return 1;
219 }else{
220 *tokenType = TK_CONCAT;
221 return 2;
222 }
223 }
drh75897232000-05-29 14:26:00 +0000224 case ',': {
225 *tokenType = TK_COMMA;
226 return 1;
227 }
drhbf4133c2001-10-13 02:59:08 +0000228 case '&': {
229 *tokenType = TK_BITAND;
230 return 1;
231 }
232 case '~': {
233 *tokenType = TK_BITNOT;
234 return 1;
235 }
drh3d946622005-08-13 18:15:42 +0000236 case '`':
237 case '\'':
238 case '"': {
drh75897232000-05-29 14:26:00 +0000239 int delim = z[0];
drhaa756b02004-09-25 15:25:26 +0000240 for(i=1; (c=z[i])!=0; i++){
241 if( c==delim ){
drh75897232000-05-29 14:26:00 +0000242 if( z[i+1]==delim ){
243 i++;
244 }else{
245 break;
246 }
247 }
248 }
drheef8b552005-10-23 11:29:40 +0000249 if( c ){
250 *tokenType = TK_STRING;
251 return i+1;
252 }else{
253 *tokenType = TK_ILLEGAL;
254 return i;
255 }
drh75897232000-05-29 14:26:00 +0000256 }
257 case '.': {
drh76816182005-08-23 11:31:26 +0000258#ifndef SQLITE_OMIT_FLOATING_POINT
259 if( !isdigit(z[1]) )
260#endif
261 {
262 *tokenType = TK_DOT;
263 return 1;
264 }
265 /* If the next character is a digit, this is a floating point
266 ** number that begins with ".". Fall thru into the next case */
drh75897232000-05-29 14:26:00 +0000267 }
268 case '0': case '1': case '2': case '3': case '4':
269 case '5': case '6': case '7': case '8': case '9': {
drhc837e702000-06-08 16:26:24 +0000270 *tokenType = TK_INTEGER;
drh76816182005-08-23 11:31:26 +0000271 for(i=0; isdigit(z[i]); i++){}
drhb7f91642004-10-31 02:22:47 +0000272#ifndef SQLITE_OMIT_FLOATING_POINT
drh76816182005-08-23 11:31:26 +0000273 if( z[i]=='.' ){
274 i++;
drh32eb7b42003-01-07 01:44:37 +0000275 while( isdigit(z[i]) ){ i++; }
drhc837e702000-06-08 16:26:24 +0000276 *tokenType = TK_FLOAT;
277 }
278 if( (z[i]=='e' || z[i]=='E') &&
drh75897232000-05-29 14:26:00 +0000279 ( isdigit(z[i+1])
280 || ((z[i+1]=='+' || z[i+1]=='-') && isdigit(z[i+2]))
281 )
drhc837e702000-06-08 16:26:24 +0000282 ){
283 i += 2;
drh32eb7b42003-01-07 01:44:37 +0000284 while( isdigit(z[i]) ){ i++; }
drh75897232000-05-29 14:26:00 +0000285 *tokenType = TK_FLOAT;
drh75897232000-05-29 14:26:00 +0000286 }
drhb7f91642004-10-31 02:22:47 +0000287#endif
drh67dd9012006-08-12 12:33:14 +0000288 while( IdChar(z[i]) ){
289 *tokenType = TK_ILLEGAL;
290 i++;
291 }
drh75897232000-05-29 14:26:00 +0000292 return i;
293 }
drh2f4392f2002-02-14 21:42:51 +0000294 case '[': {
drhaa756b02004-09-25 15:25:26 +0000295 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
drh2f4392f2002-02-14 21:42:51 +0000296 *tokenType = TK_ID;
297 return i;
298 }
drh7c972de2003-09-06 22:18:07 +0000299 case '?': {
drh50457892003-09-06 01:10:47 +0000300 *tokenType = TK_VARIABLE;
drhfa6bc002004-09-07 16:19:52 +0000301 for(i=1; isdigit(z[i]); i++){}
302 return i;
drh50457892003-09-06 01:10:47 +0000303 }
drh288d37f2005-06-22 08:48:06 +0000304 case '#': {
305 for(i=1; isdigit(z[i]); i++){}
306 if( i>1 ){
307 /* Parameters of the form #NNN (where NNN is a number) are used
308 ** internally by sqlite3NestedParse. */
309 *tokenType = TK_REGISTER;
310 return i;
311 }
312 /* Fall through into the next case if the '#' is not followed by
313 ** a digit. Try to match #AAAA where AAAA is a parameter name. */
drh895d7472004-08-20 16:02:39 +0000314 }
drhb7f91642004-10-31 02:22:47 +0000315#ifndef SQLITE_OMIT_TCL_VARIABLE
drh288d37f2005-06-22 08:48:06 +0000316 case '$':
317#endif
drh0b2a5ee2006-02-09 22:24:41 +0000318 case '@': /* For compatibility with MS SQL Server */
drh288d37f2005-06-22 08:48:06 +0000319 case ':': {
320 int n = 0;
drh9d74b4c2004-08-24 15:23:34 +0000321 *tokenType = TK_VARIABLE;
drh288d37f2005-06-22 08:48:06 +0000322 for(i=1; (c=z[i])!=0; i++){
323 if( IdChar(c) ){
324 n++;
325#ifndef SQLITE_OMIT_TCL_VARIABLE
326 }else if( c=='(' && n>0 ){
327 do{
328 i++;
329 }while( (c=z[i])!=0 && !isspace(c) && c!=')' );
330 if( c==')' ){
drh895d7472004-08-20 16:02:39 +0000331 i++;
332 }else{
drh288d37f2005-06-22 08:48:06 +0000333 *tokenType = TK_ILLEGAL;
drh895d7472004-08-20 16:02:39 +0000334 }
drh288d37f2005-06-22 08:48:06 +0000335 break;
336 }else if( c==':' && z[i+1]==':' ){
337 i++;
338#endif
339 }else{
340 break;
drh895d7472004-08-20 16:02:39 +0000341 }
342 }
drh288d37f2005-06-22 08:48:06 +0000343 if( n==0 ) *tokenType = TK_ILLEGAL;
drh895d7472004-08-20 16:02:39 +0000344 return i;
drhb7f91642004-10-31 02:22:47 +0000345 }
drhb7f91642004-10-31 02:22:47 +0000346#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +0000347 case 'x': case 'X': {
drhaa756b02004-09-25 15:25:26 +0000348 if( (c=z[1])=='\'' || c=='"' ){
349 int delim = c;
danielk19773fd0a732004-05-27 13:35:19 +0000350 *tokenType = TK_BLOB;
drhaa756b02004-09-25 15:25:26 +0000351 for(i=2; (c=z[i])!=0; i++){
352 if( c==delim ){
danielk19773fd0a732004-05-27 13:35:19 +0000353 if( i%2 ) *tokenType = TK_ILLEGAL;
danielk1977c572ef72004-05-27 09:28:41 +0000354 break;
355 }
drhaa756b02004-09-25 15:25:26 +0000356 if( !isxdigit(c) ){
danielk19773fd0a732004-05-27 13:35:19 +0000357 *tokenType = TK_ILLEGAL;
358 return i;
359 }
danielk1977c572ef72004-05-27 09:28:41 +0000360 }
drhaa756b02004-09-25 15:25:26 +0000361 if( c ) i++;
danielk1977c572ef72004-05-27 09:28:41 +0000362 return i;
363 }
364 /* Otherwise fall through to the next case */
365 }
drhb7f91642004-10-31 02:22:47 +0000366#endif
drh98808ba2001-10-18 12:34:46 +0000367 default: {
drheab7f3f2007-05-15 09:00:14 +0000368 if( !IdChar(*z) || (*z & 0xc0)==0x80 ){
drh98808ba2001-10-18 12:34:46 +0000369 break;
370 }
drhaa756b02004-09-25 15:25:26 +0000371 for(i=1; IdChar(z[i]); i++){}
danielk1977c60e9b82005-01-31 12:42:29 +0000372 *tokenType = keywordCode((char*)z, i);
drh75897232000-05-29 14:26:00 +0000373 return i;
374 }
drh75897232000-05-29 14:26:00 +0000375 }
376 *tokenType = TK_ILLEGAL;
377 return 1;
378}
danielk1977c60e9b82005-01-31 12:42:29 +0000379int sqlite3GetToken(const unsigned char *z, int *tokenType){
380 return getToken(z, tokenType);
381}
drh75897232000-05-29 14:26:00 +0000382
383/*
384** Run the parser on the given SQL string. The parser structure is
drhb19a2bc2001-09-16 00:13:26 +0000385** passed in. An SQLITE_ status code is returned. If an error occurs
386** and pzErrMsg!=NULL then an error message might be written into
387** memory obtained from malloc() and *pzErrMsg made to point to that
388** error message. Or maybe not.
drh75897232000-05-29 14:26:00 +0000389*/
danielk19774adee202004-05-08 08:23:19 +0000390int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
drh75897232000-05-29 14:26:00 +0000391 int nErr = 0;
392 int i;
393 void *pEngine;
drhb86ccfb2003-01-28 23:13:10 +0000394 int tokenType;
395 int lastTokenParsed = -1;
drh9bb575f2004-09-06 17:24:11 +0000396 sqlite3 *db = pParse->db;
drh23a4d142007-01-26 19:31:00 +0000397 extern void *sqlite3ParserAlloc(void*(*)(size_t));
danielk19774adee202004-05-08 08:23:19 +0000398 extern void sqlite3ParserFree(void*, void(*)(void*));
drh23a4d142007-01-26 19:31:00 +0000399 extern void sqlite3Parser(void*, int, Token, Parse*);
drh75897232000-05-29 14:26:00 +0000400
drh15ca1df2006-07-26 13:43:30 +0000401 if( db->activeVdbeCnt==0 ){
402 db->u1.isInterrupted = 0;
403 }
drh4c504392000-10-16 22:06:40 +0000404 pParse->rc = SQLITE_OK;
drh75897232000-05-29 14:26:00 +0000405 i = 0;
drh23a4d142007-01-26 19:31:00 +0000406 pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3MallocX);
drh75897232000-05-29 14:26:00 +0000407 if( pEngine==0 ){
drhdefc9972005-06-06 14:45:42 +0000408 return SQLITE_NOMEM;
drh75897232000-05-29 14:26:00 +0000409 }
drhfa6bc002004-09-07 16:19:52 +0000410 assert( pParse->sLastToken.dyn==0 );
411 assert( pParse->pNewTable==0 );
412 assert( pParse->pNewTrigger==0 );
413 assert( pParse->nVar==0 );
414 assert( pParse->nVarExpr==0 );
415 assert( pParse->nVarExprAlloc==0 );
416 assert( pParse->apVarExpr==0 );
drh3f7d4e42004-07-24 14:35:58 +0000417 pParse->zTail = pParse->zSql = zSql;
danielk19779e128002006-01-18 16:51:35 +0000418 while( !sqlite3MallocFailed() && zSql[i]!=0 ){
drh32eb7b42003-01-07 01:44:37 +0000419 assert( i>=0 );
drh2646da72005-12-09 20:02:05 +0000420 pParse->sLastToken.z = (u8*)&zSql[i];
drh32eb7b42003-01-07 01:44:37 +0000421 assert( pParse->sLastToken.dyn==0 );
danielk1977c60e9b82005-01-31 12:42:29 +0000422 pParse->sLastToken.n = getToken((unsigned char*)&zSql[i],&tokenType);
drh75897232000-05-29 14:26:00 +0000423 i += pParse->sLastToken.n;
drhe5c941b2007-05-08 13:58:26 +0000424 if( i>SQLITE_MAX_SQL_LENGTH ){
425 pParse->rc = SQLITE_TOOBIG;
426 break;
427 }
drh75897232000-05-29 14:26:00 +0000428 switch( tokenType ){
429 case TK_SPACE:
drh75897232000-05-29 14:26:00 +0000430 case TK_COMMENT: {
drh881feaa2006-07-26 01:39:30 +0000431 if( db->u1.isInterrupted ){
drh32eb7b42003-01-07 01:44:37 +0000432 pParse->rc = SQLITE_INTERRUPT;
danielk19774adee202004-05-08 08:23:19 +0000433 sqlite3SetString(pzErrMsg, "interrupt", (char*)0);
drh32eb7b42003-01-07 01:44:37 +0000434 goto abort_parse;
435 }
drh75897232000-05-29 14:26:00 +0000436 break;
437 }
drh32eb7b42003-01-07 01:44:37 +0000438 case TK_ILLEGAL: {
drhae29ffb2004-09-25 14:39:18 +0000439 if( pzErrMsg ){
440 sqliteFree(*pzErrMsg);
441 *pzErrMsg = sqlite3MPrintf("unrecognized token: \"%T\"",
442 &pParse->sLastToken);
443 }
drh75897232000-05-29 14:26:00 +0000444 nErr++;
drhcaec2f12003-01-07 02:47:47 +0000445 goto abort_parse;
drh32eb7b42003-01-07 01:44:37 +0000446 }
drh326dce72003-01-29 14:06:07 +0000447 case TK_SEMI: {
448 pParse->zTail = &zSql[i];
449 /* Fall thru into the default case */
450 }
drh32eb7b42003-01-07 01:44:37 +0000451 default: {
danielk19774adee202004-05-08 08:23:19 +0000452 sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
drhb86ccfb2003-01-28 23:13:10 +0000453 lastTokenParsed = tokenType;
454 if( pParse->rc!=SQLITE_OK ){
drh32eb7b42003-01-07 01:44:37 +0000455 goto abort_parse;
drh75897232000-05-29 14:26:00 +0000456 }
457 break;
drh32eb7b42003-01-07 01:44:37 +0000458 }
drh75897232000-05-29 14:26:00 +0000459 }
460 }
drh32eb7b42003-01-07 01:44:37 +0000461abort_parse:
drhb86ccfb2003-01-28 23:13:10 +0000462 if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
463 if( lastTokenParsed!=TK_SEMI ){
danielk19774adee202004-05-08 08:23:19 +0000464 sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
drh326dce72003-01-29 14:06:07 +0000465 pParse->zTail = &zSql[i];
drh75897232000-05-29 14:26:00 +0000466 }
danielk19774adee202004-05-08 08:23:19 +0000467 sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
drh75897232000-05-29 14:26:00 +0000468 }
drh132d8d62005-05-22 20:12:37 +0000469 sqlite3ParserFree(pEngine, sqlite3FreeX);
danielk19779e128002006-01-18 16:51:35 +0000470 if( sqlite3MallocFailed() ){
drh71c697e2004-08-08 23:39:19 +0000471 pParse->rc = SQLITE_NOMEM;
472 }
drhb86ccfb2003-01-28 23:13:10 +0000473 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
danielk1977261919c2005-12-06 12:52:59 +0000474 sqlite3SetString(&pParse->zErrMsg, sqlite3ErrStr(pParse->rc), (char*)0);
drhb86ccfb2003-01-28 23:13:10 +0000475 }
drh75897232000-05-29 14:26:00 +0000476 if( pParse->zErrMsg ){
drhb86ccfb2003-01-28 23:13:10 +0000477 if( pzErrMsg && *pzErrMsg==0 ){
drh75897232000-05-29 14:26:00 +0000478 *pzErrMsg = pParse->zErrMsg;
479 }else{
480 sqliteFree(pParse->zErrMsg);
481 }
drhb86ccfb2003-01-28 23:13:10 +0000482 pParse->zErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000483 if( !nErr ) nErr++;
484 }
drh2958a4e2004-11-12 03:56:15 +0000485 if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
danielk19774adee202004-05-08 08:23:19 +0000486 sqlite3VdbeDelete(pParse->pVdbe);
drh75897232000-05-29 14:26:00 +0000487 pParse->pVdbe = 0;
488 }
danielk1977c00da102006-01-07 13:21:04 +0000489#ifndef SQLITE_OMIT_SHARED_CACHE
490 if( pParse->nested==0 ){
491 sqliteFree(pParse->aTableLock);
492 pParse->aTableLock = 0;
493 pParse->nTableLock = 0;
494 }
495#endif
danielk19777e6ebfb2006-06-12 11:24:37 +0000496
danielk19777e6ebfb2006-06-12 11:24:37 +0000497 if( !IN_DECLARE_VTAB ){
498 /* If the pParse->declareVtab flag is set, do not delete any table
499 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
500 ** will take responsibility for freeing the Table structure.
501 */
danielk1977a04a34f2007-04-16 15:06:25 +0000502 sqlite3DeleteTable(pParse->pNewTable);
danielk19777e6ebfb2006-06-12 11:24:37 +0000503 }
504
drhfa6bc002004-09-07 16:19:52 +0000505 sqlite3DeleteTrigger(pParse->pNewTrigger);
506 sqliteFree(pParse->apVarExpr);
drhb86ccfb2003-01-28 23:13:10 +0000507 if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){
drh4c504392000-10-16 22:06:40 +0000508 pParse->rc = SQLITE_ERROR;
509 }
drh75897232000-05-29 14:26:00 +0000510 return nErr;
511}