blob: 2f8fd98ede4271c75116bbee860d05d810745e93 [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.
drh75897232000-05-29 14:26:00 +000017*/
18#include "sqliteInt.h"
drhdcc581c2000-05-30 13:44:19 +000019#include <stdlib.h>
drh75897232000-05-29 14:26:00 +000020
21/*
drh9b8f4472006-04-04 01:54:55 +000022** The charMap() macro maps alphabetic characters into their
23** lower-case ASCII equivalent. On ASCII machines, this is just
24** an upper-to-lower case map. On EBCDIC machines we also need
25** to adjust the encoding. Only alphabetic characters and underscores
26** need to be translated.
27*/
28#ifdef SQLITE_ASCII
29# define charMap(X) sqlite3UpperToLower[(unsigned char)X]
30#endif
31#ifdef SQLITE_EBCDIC
32# define charMap(X) ebcdicToAscii[(unsigned char)X]
33const unsigned char ebcdicToAscii[] = {
34/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
35 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
36 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
37 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
38 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */
39 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */
40 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */
41 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */
42 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */
43 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */
44 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */
45 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */
46 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
47 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */
48 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */
49 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */
50 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */
51};
52#endif
53
54/*
drh52fb6d72004-11-03 03:59:57 +000055** The sqlite3KeywordCode function looks up an identifier to determine if
56** it is a keyword. If it is a keyword, the token code of that keyword is
drh75897232000-05-29 14:26:00 +000057** returned. If the input is not a keyword, TK_ID is returned.
drh2090a0e2004-10-07 19:03:01 +000058**
59** The implementation of this routine was generated by a program,
drh73b211a2005-01-18 04:00:42 +000060** mkkeywordhash.h, located in the tool subdirectory of the distribution.
drh52fb6d72004-11-03 03:59:57 +000061** The output of the mkkeywordhash.c program is written into a file
drh73b211a2005-01-18 04:00:42 +000062** named keywordhash.h and then included into this source file by
drh52fb6d72004-11-03 03:59:57 +000063** the #include below.
drh75897232000-05-29 14:26:00 +000064*/
drh73b211a2005-01-18 04:00:42 +000065#include "keywordhash.h"
drh75897232000-05-29 14:26:00 +000066
drh40f20f72004-10-23 05:10:18 +000067
drh98808ba2001-10-18 12:34:46 +000068/*
drh9b8f4472006-04-04 01:54:55 +000069** If X is a character that can be used in an identifier then
70** IdChar(X) will be true. Otherwise it is false.
drh98808ba2001-10-18 12:34:46 +000071**
drh9b8f4472006-04-04 01:54:55 +000072** For ASCII, any character with the high-order bit set is
73** allowed in an identifier. For 7-bit characters,
74** sqlite3IsIdChar[X] must be 1.
75**
76** For EBCDIC, the rules are more complex but have the same
77** end result.
drha0d1f662005-01-11 17:59:47 +000078**
79** Ticket #1066. the SQL standard does not allow '$' in the
peter.d.reid60ec9142014-09-06 16:39:46 +000080** middle of identifiers. But many SQL implementations do.
drha0d1f662005-01-11 17:59:47 +000081** SQLite will allow '$' in identifiers for compatibility.
82** But the feature is undocumented.
drh98808ba2001-10-18 12:34:46 +000083*/
drh9b8f4472006-04-04 01:54:55 +000084#ifdef SQLITE_ASCII
drhaf2b5722009-11-16 15:11:51 +000085#define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
drh9b8f4472006-04-04 01:54:55 +000086#endif
87#ifdef SQLITE_EBCDIC
drh46c99e02007-08-27 23:26:59 +000088const char sqlite3IsEbcdicIdChar[] = {
drh9b8f4472006-04-04 01:54:55 +000089/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
90 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */
91 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */
92 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */
93 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */
94 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */
95 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */
96 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */
97 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */
98 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */
99 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */
100 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */
101 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */
102};
drh46c99e02007-08-27 23:26:59 +0000103#define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
drh9b8f4472006-04-04 01:54:55 +0000104#endif
drh97348b32014-09-25 02:44:29 +0000105int sqlite3IsIdChar(u8 c){ return IdChar(c); }
drh9b8f4472006-04-04 01:54:55 +0000106
drh98808ba2001-10-18 12:34:46 +0000107
drh75897232000-05-29 14:26:00 +0000108/*
drh61b487d2003-09-12 02:08:14 +0000109** Return the length of the token that begins at z[0].
110** Store the token type in *tokenType before returning.
drh75897232000-05-29 14:26:00 +0000111*/
drh35b5a332008-04-05 18:41:42 +0000112int sqlite3GetToken(const unsigned char *z, int *tokenType){
drhaa756b02004-09-25 15:25:26 +0000113 int i, c;
drh75897232000-05-29 14:26:00 +0000114 switch( *z ){
drh30cab802000-08-09 17:17:25 +0000115 case ' ': case '\t': case '\n': case '\f': case '\r': {
drh19f81f62009-06-09 18:01:37 +0000116 testcase( z[0]==' ' );
117 testcase( z[0]=='\t' );
118 testcase( z[0]=='\n' );
119 testcase( z[0]=='\f' );
120 testcase( z[0]=='\r' );
danielk197778ca0e72009-01-20 16:53:39 +0000121 for(i=1; sqlite3Isspace(z[i]); i++){}
drh75897232000-05-29 14:26:00 +0000122 *tokenType = TK_SPACE;
123 return i;
124 }
125 case '-': {
drh75897232000-05-29 14:26:00 +0000126 if( z[1]=='-' ){
drhaa756b02004-09-25 15:25:26 +0000127 for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
drhd3479b92009-12-14 17:42:12 +0000128 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
drh75897232000-05-29 14:26:00 +0000129 return i;
130 }
131 *tokenType = TK_MINUS;
132 return 1;
133 }
134 case '(': {
drhdab35182003-09-27 13:39:38 +0000135 *tokenType = TK_LP;
136 return 1;
drh75897232000-05-29 14:26:00 +0000137 }
138 case ')': {
139 *tokenType = TK_RP;
140 return 1;
141 }
142 case ';': {
143 *tokenType = TK_SEMI;
144 return 1;
145 }
146 case '+': {
147 *tokenType = TK_PLUS;
148 return 1;
149 }
150 case '*': {
151 *tokenType = TK_STAR;
152 return 1;
153 }
154 case '/': {
drh66105a82002-08-27 14:28:29 +0000155 if( z[1]!='*' || z[2]==0 ){
156 *tokenType = TK_SLASH;
157 return 1;
158 }
drhaa756b02004-09-25 15:25:26 +0000159 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
160 if( c ) i++;
drhd3479b92009-12-14 17:42:12 +0000161 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
drh66105a82002-08-27 14:28:29 +0000162 return i;
drh75897232000-05-29 14:26:00 +0000163 }
drhbf4133c2001-10-13 02:59:08 +0000164 case '%': {
165 *tokenType = TK_REM;
166 return 1;
167 }
drh75897232000-05-29 14:26:00 +0000168 case '=': {
169 *tokenType = TK_EQ;
170 return 1 + (z[1]=='=');
171 }
172 case '<': {
drhaa756b02004-09-25 15:25:26 +0000173 if( (c=z[1])=='=' ){
drh75897232000-05-29 14:26:00 +0000174 *tokenType = TK_LE;
175 return 2;
drhaa756b02004-09-25 15:25:26 +0000176 }else if( c=='>' ){
drh75897232000-05-29 14:26:00 +0000177 *tokenType = TK_NE;
178 return 2;
drhaa756b02004-09-25 15:25:26 +0000179 }else if( c=='<' ){
drhbf4133c2001-10-13 02:59:08 +0000180 *tokenType = TK_LSHIFT;
181 return 2;
drh75897232000-05-29 14:26:00 +0000182 }else{
183 *tokenType = TK_LT;
184 return 1;
185 }
186 }
187 case '>': {
drhaa756b02004-09-25 15:25:26 +0000188 if( (c=z[1])=='=' ){
drh75897232000-05-29 14:26:00 +0000189 *tokenType = TK_GE;
190 return 2;
drhaa756b02004-09-25 15:25:26 +0000191 }else if( c=='>' ){
drhbf4133c2001-10-13 02:59:08 +0000192 *tokenType = TK_RSHIFT;
193 return 2;
drh75897232000-05-29 14:26:00 +0000194 }else{
195 *tokenType = TK_GT;
196 return 1;
197 }
198 }
199 case '!': {
200 if( z[1]!='=' ){
201 *tokenType = TK_ILLEGAL;
drhc837e702000-06-08 16:26:24 +0000202 return 2;
drh75897232000-05-29 14:26:00 +0000203 }else{
204 *tokenType = TK_NE;
205 return 2;
206 }
207 }
drh00400772000-06-16 20:51:26 +0000208 case '|': {
209 if( z[1]!='|' ){
drhbf4133c2001-10-13 02:59:08 +0000210 *tokenType = TK_BITOR;
drh00400772000-06-16 20:51:26 +0000211 return 1;
212 }else{
213 *tokenType = TK_CONCAT;
214 return 2;
215 }
216 }
drh75897232000-05-29 14:26:00 +0000217 case ',': {
218 *tokenType = TK_COMMA;
219 return 1;
220 }
drhbf4133c2001-10-13 02:59:08 +0000221 case '&': {
222 *tokenType = TK_BITAND;
223 return 1;
224 }
225 case '~': {
226 *tokenType = TK_BITNOT;
227 return 1;
228 }
drh3d946622005-08-13 18:15:42 +0000229 case '`':
230 case '\'':
231 case '"': {
drh75897232000-05-29 14:26:00 +0000232 int delim = z[0];
drh19f81f62009-06-09 18:01:37 +0000233 testcase( delim=='`' );
234 testcase( delim=='\'' );
235 testcase( delim=='"' );
drhaa756b02004-09-25 15:25:26 +0000236 for(i=1; (c=z[i])!=0; i++){
237 if( c==delim ){
drh75897232000-05-29 14:26:00 +0000238 if( z[i+1]==delim ){
239 i++;
240 }else{
241 break;
242 }
243 }
244 }
drha33cb5f2008-08-07 13:05:34 +0000245 if( c=='\'' ){
drheef8b552005-10-23 11:29:40 +0000246 *tokenType = TK_STRING;
247 return i+1;
drha33cb5f2008-08-07 13:05:34 +0000248 }else if( c!=0 ){
249 *tokenType = TK_ID;
250 return i+1;
drheef8b552005-10-23 11:29:40 +0000251 }else{
252 *tokenType = TK_ILLEGAL;
253 return i;
254 }
drh75897232000-05-29 14:26:00 +0000255 }
256 case '.': {
drh76816182005-08-23 11:31:26 +0000257#ifndef SQLITE_OMIT_FLOATING_POINT
danielk197778ca0e72009-01-20 16:53:39 +0000258 if( !sqlite3Isdigit(z[1]) )
drh76816182005-08-23 11:31:26 +0000259#endif
260 {
261 *tokenType = TK_DOT;
262 return 1;
263 }
264 /* If the next character is a digit, this is a floating point
265 ** number that begins with ".". Fall thru into the next case */
drh75897232000-05-29 14:26:00 +0000266 }
267 case '0': case '1': case '2': case '3': case '4':
268 case '5': case '6': case '7': case '8': case '9': {
drh19f81f62009-06-09 18:01:37 +0000269 testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' );
270 testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' );
271 testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' );
272 testcase( z[0]=='9' );
drhc837e702000-06-08 16:26:24 +0000273 *tokenType = TK_INTEGER;
drh28e048c2014-07-23 01:26:51 +0000274#ifndef SQLITE_OMIT_HEX_INTEGER
275 if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){
276 for(i=3; sqlite3Isxdigit(z[i]); i++){}
277 return i;
278 }
279#endif
danielk197778ca0e72009-01-20 16:53:39 +0000280 for(i=0; sqlite3Isdigit(z[i]); i++){}
drhb7f91642004-10-31 02:22:47 +0000281#ifndef SQLITE_OMIT_FLOATING_POINT
drh76816182005-08-23 11:31:26 +0000282 if( z[i]=='.' ){
283 i++;
danielk197778ca0e72009-01-20 16:53:39 +0000284 while( sqlite3Isdigit(z[i]) ){ i++; }
drhc837e702000-06-08 16:26:24 +0000285 *tokenType = TK_FLOAT;
286 }
287 if( (z[i]=='e' || z[i]=='E') &&
danielk197778ca0e72009-01-20 16:53:39 +0000288 ( sqlite3Isdigit(z[i+1])
289 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
drh75897232000-05-29 14:26:00 +0000290 )
drhc837e702000-06-08 16:26:24 +0000291 ){
292 i += 2;
danielk197778ca0e72009-01-20 16:53:39 +0000293 while( sqlite3Isdigit(z[i]) ){ i++; }
drh75897232000-05-29 14:26:00 +0000294 *tokenType = TK_FLOAT;
drh75897232000-05-29 14:26:00 +0000295 }
drhb7f91642004-10-31 02:22:47 +0000296#endif
drh67dd9012006-08-12 12:33:14 +0000297 while( IdChar(z[i]) ){
298 *tokenType = TK_ILLEGAL;
299 i++;
300 }
drh75897232000-05-29 14:26:00 +0000301 return i;
302 }
drh2f4392f2002-02-14 21:42:51 +0000303 case '[': {
drhaa756b02004-09-25 15:25:26 +0000304 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
drh4b2f9362008-01-22 23:37:09 +0000305 *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
drh2f4392f2002-02-14 21:42:51 +0000306 return i;
307 }
drh7c972de2003-09-06 22:18:07 +0000308 case '?': {
drh50457892003-09-06 01:10:47 +0000309 *tokenType = TK_VARIABLE;
danielk197778ca0e72009-01-20 16:53:39 +0000310 for(i=1; sqlite3Isdigit(z[i]); i++){}
drhfa6bc002004-09-07 16:19:52 +0000311 return i;
drh50457892003-09-06 01:10:47 +0000312 }
drhb7f91642004-10-31 02:22:47 +0000313#ifndef SQLITE_OMIT_TCL_VARIABLE
drh288d37f2005-06-22 08:48:06 +0000314 case '$':
315#endif
drh0b2a5ee2006-02-09 22:24:41 +0000316 case '@': /* For compatibility with MS SQL Server */
drhf59b12f2014-01-11 03:54:05 +0000317 case '#':
drh288d37f2005-06-22 08:48:06 +0000318 case ':': {
319 int n = 0;
drhf59b12f2014-01-11 03:54:05 +0000320 testcase( z[0]=='$' ); testcase( z[0]=='@' );
321 testcase( z[0]==':' ); testcase( z[0]=='#' );
drh9d74b4c2004-08-24 15:23:34 +0000322 *tokenType = TK_VARIABLE;
drh288d37f2005-06-22 08:48:06 +0000323 for(i=1; (c=z[i])!=0; i++){
324 if( IdChar(c) ){
325 n++;
326#ifndef SQLITE_OMIT_TCL_VARIABLE
327 }else if( c=='(' && n>0 ){
328 do{
329 i++;
danielk197778ca0e72009-01-20 16:53:39 +0000330 }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
drh288d37f2005-06-22 08:48:06 +0000331 if( c==')' ){
drh895d7472004-08-20 16:02:39 +0000332 i++;
333 }else{
drh288d37f2005-06-22 08:48:06 +0000334 *tokenType = TK_ILLEGAL;
drh895d7472004-08-20 16:02:39 +0000335 }
drh288d37f2005-06-22 08:48:06 +0000336 break;
337 }else if( c==':' && z[i+1]==':' ){
338 i++;
339#endif
340 }else{
341 break;
drh895d7472004-08-20 16:02:39 +0000342 }
343 }
drh288d37f2005-06-22 08:48:06 +0000344 if( n==0 ) *tokenType = TK_ILLEGAL;
drh895d7472004-08-20 16:02:39 +0000345 return i;
drhb7f91642004-10-31 02:22:47 +0000346 }
drhb7f91642004-10-31 02:22:47 +0000347#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +0000348 case 'x': case 'X': {
drh19f81f62009-06-09 18:01:37 +0000349 testcase( z[0]=='x' ); testcase( z[0]=='X' );
drh4b2f9362008-01-22 23:37:09 +0000350 if( z[1]=='\'' ){
danielk19773fd0a732004-05-27 13:35:19 +0000351 *tokenType = TK_BLOB;
drh3a61a5a2011-06-03 21:34:45 +0000352 for(i=2; sqlite3Isxdigit(z[i]); i++){}
353 if( z[i]!='\'' || i%2 ){
354 *tokenType = TK_ILLEGAL;
355 while( z[i] && z[i]!='\'' ){ i++; }
danielk1977c572ef72004-05-27 09:28:41 +0000356 }
drh3a61a5a2011-06-03 21:34:45 +0000357 if( z[i] ) i++;
danielk1977c572ef72004-05-27 09:28:41 +0000358 return i;
359 }
360 /* Otherwise fall through to the next case */
361 }
drhb7f91642004-10-31 02:22:47 +0000362#endif
drh98808ba2001-10-18 12:34:46 +0000363 default: {
drh9a087a92007-05-15 14:34:32 +0000364 if( !IdChar(*z) ){
drh98808ba2001-10-18 12:34:46 +0000365 break;
366 }
drhaa756b02004-09-25 15:25:26 +0000367 for(i=1; IdChar(z[i]); i++){}
danielk1977c60e9b82005-01-31 12:42:29 +0000368 *tokenType = keywordCode((char*)z, i);
drh75897232000-05-29 14:26:00 +0000369 return i;
370 }
drh75897232000-05-29 14:26:00 +0000371 }
372 *tokenType = TK_ILLEGAL;
373 return 1;
374}
375
376/*
377** Run the parser on the given SQL string. The parser structure is
drhb19a2bc2001-09-16 00:13:26 +0000378** passed in. An SQLITE_ status code is returned. If an error occurs
drhfb45d8c2008-07-08 00:06:49 +0000379** then an and attempt is made to write an error message into
380** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
381** error message.
drh75897232000-05-29 14:26:00 +0000382*/
danielk19774adee202004-05-08 08:23:19 +0000383int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
drhd9da78a2009-03-24 15:08:09 +0000384 int nErr = 0; /* Number of errors encountered */
385 int i; /* Loop counter */
386 void *pEngine; /* The LEMON-generated LALR(1) parser */
387 int tokenType; /* type of the next token */
388 int lastTokenParsed = -1; /* type of the previous token */
shaneb08a67a2009-03-31 03:41:56 +0000389 u8 enableLookaside; /* Saved value of db->lookaside.bEnabled */
drhd9da78a2009-03-24 15:08:09 +0000390 sqlite3 *db = pParse->db; /* The database connection */
391 int mxSqlLen; /* Max length of an SQL string */
drh75897232000-05-29 14:26:00 +0000392
drh96c707a2015-02-13 16:36:14 +0000393 assert( zSql!=0 );
drhd9da78a2009-03-24 15:08:09 +0000394 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
drh4f7d3a52013-06-27 23:54:02 +0000395 if( db->nVdbeActive==0 ){
drh15ca1df2006-07-26 13:43:30 +0000396 db->u1.isInterrupted = 0;
397 }
drh4c504392000-10-16 22:06:40 +0000398 pParse->rc = SQLITE_OK;
drhe7f3f3e2009-07-03 22:54:36 +0000399 pParse->zTail = zSql;
drh75897232000-05-29 14:26:00 +0000400 i = 0;
drhfb45d8c2008-07-08 00:06:49 +0000401 assert( pzErrMsg!=0 );
drhfb046e72014-09-12 04:28:33 +0000402 pEngine = sqlite3ParserAlloc(sqlite3Malloc);
drh75897232000-05-29 14:26:00 +0000403 if( pEngine==0 ){
drhf3a65f72007-08-22 20:18:21 +0000404 db->mallocFailed = 1;
drhdefc9972005-06-06 14:45:42 +0000405 return SQLITE_NOMEM;
drh75897232000-05-29 14:26:00 +0000406 }
drhfa6bc002004-09-07 16:19:52 +0000407 assert( pParse->pNewTable==0 );
408 assert( pParse->pNewTrigger==0 );
409 assert( pParse->nVar==0 );
drh124c0b42011-06-01 18:15:55 +0000410 assert( pParse->nzVar==0 );
411 assert( pParse->azVar==0 );
drhd9da78a2009-03-24 15:08:09 +0000412 enableLookaside = db->lookaside.bEnabled;
413 if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
drh17435752007-08-16 04:30:38 +0000414 while( !db->mallocFailed && zSql[i]!=0 ){
drh32eb7b42003-01-07 01:44:37 +0000415 assert( i>=0 );
drhb7916a72009-05-27 10:31:29 +0000416 pParse->sLastToken.z = &zSql[i];
drh35b5a332008-04-05 18:41:42 +0000417 pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
drh75897232000-05-29 14:26:00 +0000418 i += pParse->sLastToken.n;
drhbb4957f2008-03-20 14:03:29 +0000419 if( i>mxSqlLen ){
drhe5c941b2007-05-08 13:58:26 +0000420 pParse->rc = SQLITE_TOOBIG;
421 break;
422 }
drh75897232000-05-29 14:26:00 +0000423 switch( tokenType ){
drh200a81d2008-08-08 14:19:41 +0000424 case TK_SPACE: {
drh881feaa2006-07-26 01:39:30 +0000425 if( db->u1.isInterrupted ){
drh19f81f62009-06-09 18:01:37 +0000426 sqlite3ErrorMsg(pParse, "interrupt");
drh32eb7b42003-01-07 01:44:37 +0000427 pParse->rc = SQLITE_INTERRUPT;
drh32eb7b42003-01-07 01:44:37 +0000428 goto abort_parse;
429 }
drh75897232000-05-29 14:26:00 +0000430 break;
431 }
drh32eb7b42003-01-07 01:44:37 +0000432 case TK_ILLEGAL: {
drh347bdc32015-04-15 07:57:27 +0000433 sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"",
drhfb45d8c2008-07-08 00:06:49 +0000434 &pParse->sLastToken);
drhcaec2f12003-01-07 02:47:47 +0000435 goto abort_parse;
drh32eb7b42003-01-07 01:44:37 +0000436 }
drh326dce72003-01-29 14:06:07 +0000437 case TK_SEMI: {
438 pParse->zTail = &zSql[i];
439 /* Fall thru into the default case */
440 }
drh32eb7b42003-01-07 01:44:37 +0000441 default: {
danielk19774adee202004-05-08 08:23:19 +0000442 sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
drhb86ccfb2003-01-28 23:13:10 +0000443 lastTokenParsed = tokenType;
444 if( pParse->rc!=SQLITE_OK ){
drh32eb7b42003-01-07 01:44:37 +0000445 goto abort_parse;
drh75897232000-05-29 14:26:00 +0000446 }
447 break;
drh32eb7b42003-01-07 01:44:37 +0000448 }
drh75897232000-05-29 14:26:00 +0000449 }
450 }
drh32eb7b42003-01-07 01:44:37 +0000451abort_parse:
drh0be0cf62015-04-15 08:37:42 +0000452 assert( nErr==0 );
453 if( zSql[i]==0 && pParse->rc==SQLITE_OK ){
drhb86ccfb2003-01-28 23:13:10 +0000454 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
drhaf89fe62015-03-23 17:25:18 +0000461 sqlite3_mutex_enter(sqlite3MallocMutex());
drhec424a52008-07-25 15:39:03 +0000462 sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK,
463 sqlite3ParserStackPeak(pEngine)
464 );
drhaf89fe62015-03-23 17:25:18 +0000465 sqlite3_mutex_leave(sqlite3MallocMutex());
drhec424a52008-07-25 15:39:03 +0000466#endif /* YYDEBUG */
drh17435752007-08-16 04:30:38 +0000467 sqlite3ParserFree(pEngine, sqlite3_free);
drhd9da78a2009-03-24 15:08:09 +0000468 db->lookaside.bEnabled = enableLookaside;
drh17435752007-08-16 04:30:38 +0000469 if( db->mallocFailed ){
drh71c697e2004-08-08 23:39:19 +0000470 pParse->rc = SQLITE_NOMEM;
471 }
drhb86ccfb2003-01-28 23:13:10 +0000472 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
drhf089aa42008-07-08 19:34:06 +0000473 sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc));
drhb86ccfb2003-01-28 23:13:10 +0000474 }
drh19f81f62009-06-09 18:01:37 +0000475 assert( pzErrMsg!=0 );
drh75897232000-05-29 14:26:00 +0000476 if( pParse->zErrMsg ){
drh19f81f62009-06-09 18:01:37 +0000477 *pzErrMsg = pParse->zErrMsg;
drh413c3d32010-02-23 20:11:56 +0000478 sqlite3_log(pParse->rc, "%s", *pzErrMsg);
drhb86ccfb2003-01-28 23:13:10 +0000479 pParse->zErrMsg = 0;
drh4b2f9362008-01-22 23:37:09 +0000480 nErr++;
drh75897232000-05-29 14:26:00 +0000481 }
drh2958a4e2004-11-12 03:56:15 +0000482 if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
danielk19774adee202004-05-08 08:23:19 +0000483 sqlite3VdbeDelete(pParse->pVdbe);
drh75897232000-05-29 14:26:00 +0000484 pParse->pVdbe = 0;
485 }
danielk1977c00da102006-01-07 13:21:04 +0000486#ifndef SQLITE_OMIT_SHARED_CACHE
487 if( pParse->nested==0 ){
drh633e6d52008-07-28 19:34:53 +0000488 sqlite3DbFree(db, pParse->aTableLock);
danielk1977c00da102006-01-07 13:21:04 +0000489 pParse->aTableLock = 0;
490 pParse->nTableLock = 0;
491 }
492#endif
drh4f3dd152008-04-28 18:46:43 +0000493#ifndef SQLITE_OMIT_VIRTUALTABLE
drhb9755982010-07-24 16:34:37 +0000494 sqlite3_free(pParse->apVtabLock);
drh4f3dd152008-04-28 18:46:43 +0000495#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 */
dan1feeaed2010-07-23 15:41:47 +0000502 sqlite3DeleteTable(db, pParse->pNewTable);
danielk19777e6ebfb2006-06-12 11:24:37 +0000503 }
504
danb290f112014-01-17 14:59:27 +0000505 if( pParse->bFreeWith ) sqlite3WithDelete(db, pParse->pWith);
drh633e6d52008-07-28 19:34:53 +0000506 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
drh124c0b42011-06-01 18:15:55 +0000507 for(i=pParse->nzVar-1; i>=0; i--) sqlite3DbFree(db, pParse->azVar[i]);
508 sqlite3DbFree(db, pParse->azVar);
drh0b9f50d2009-06-23 20:28:53 +0000509 while( pParse->pAinc ){
510 AutoincInfo *p = pParse->pAinc;
511 pParse->pAinc = p->pNext;
512 sqlite3DbFree(db, p);
513 }
drh588a9a12008-09-01 15:52:10 +0000514 while( pParse->pZombieTab ){
515 Table *p = pParse->pZombieTab;
516 pParse->pZombieTab = p->pNextZombie;
dan1feeaed2010-07-23 15:41:47 +0000517 sqlite3DeleteTable(db, p);
drh588a9a12008-09-01 15:52:10 +0000518 }
drhb5092d12009-06-17 01:17:13 +0000519 if( nErr>0 && pParse->rc==SQLITE_OK ){
drh4c504392000-10-16 22:06:40 +0000520 pParse->rc = SQLITE_ERROR;
521 }
drh75897232000-05-29 14:26:00 +0000522 return nErr;
523}