xref: /sqlite-3.40.0/src/tokenize.c (revision fb046e76)
175897234Sdrh /*
2b19a2bc6Sdrh ** 2001 September 15
375897234Sdrh **
4b19a2bc6Sdrh ** The author disclaims copyright to this source code.  In place of
5b19a2bc6Sdrh ** a legal notice, here is a blessing:
675897234Sdrh **
7b19a2bc6Sdrh **    May you do good and not evil.
8b19a2bc6Sdrh **    May you find forgiveness for yourself and forgive others.
9b19a2bc6Sdrh **    May you share freely, never taking more than you give.
1075897234Sdrh **
1175897234Sdrh *************************************************************************
1275897234Sdrh ** An tokenizer for SQL
1375897234Sdrh **
1475897234Sdrh ** This file contains C code that splits an SQL input string up into
1575897234Sdrh ** individual tokens and sends those tokens one-by-one over to the
1675897234Sdrh ** parser for analysis.
1775897234Sdrh */
1875897234Sdrh #include "sqliteInt.h"
19dcc581ccSdrh #include <stdlib.h>
2075897234Sdrh 
2175897234Sdrh /*
229b8f447bSdrh ** The charMap() macro maps alphabetic characters into their
239b8f447bSdrh ** lower-case ASCII equivalent.  On ASCII machines, this is just
249b8f447bSdrh ** an upper-to-lower case map.  On EBCDIC machines we also need
259b8f447bSdrh ** to adjust the encoding.  Only alphabetic characters and underscores
269b8f447bSdrh ** need to be translated.
279b8f447bSdrh */
289b8f447bSdrh #ifdef SQLITE_ASCII
299b8f447bSdrh # define charMap(X) sqlite3UpperToLower[(unsigned char)X]
309b8f447bSdrh #endif
319b8f447bSdrh #ifdef SQLITE_EBCDIC
329b8f447bSdrh # define charMap(X) ebcdicToAscii[(unsigned char)X]
339b8f447bSdrh const unsigned char ebcdicToAscii[] = {
349b8f447bSdrh /* 0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F */
359b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 0x */
369b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 1x */
379b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 2x */
389b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 3x */
399b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 4x */
409b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 5x */
419b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 95,  0,  0,  /* 6x */
429b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 7x */
439b8f447bSdrh    0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* 8x */
449b8f447bSdrh    0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* 9x */
459b8f447bSdrh    0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ax */
469b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Bx */
479b8f447bSdrh    0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* Cx */
489b8f447bSdrh    0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* Dx */
499b8f447bSdrh    0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ex */
509b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Fx */
519b8f447bSdrh };
529b8f447bSdrh #endif
539b8f447bSdrh 
549b8f447bSdrh /*
5552fb6d71Sdrh ** The sqlite3KeywordCode function looks up an identifier to determine if
5652fb6d71Sdrh ** it is a keyword.  If it is a keyword, the token code of that keyword is
5775897234Sdrh ** returned.  If the input is not a keyword, TK_ID is returned.
582090a0edSdrh **
592090a0edSdrh ** The implementation of this routine was generated by a program,
6073b211abSdrh ** mkkeywordhash.h, located in the tool subdirectory of the distribution.
6152fb6d71Sdrh ** The output of the mkkeywordhash.c program is written into a file
6273b211abSdrh ** named keywordhash.h and then included into this source file by
6352fb6d71Sdrh ** the #include below.
6475897234Sdrh */
6573b211abSdrh #include "keywordhash.h"
6675897234Sdrh 
6740f20f7dSdrh 
6898808babSdrh /*
699b8f447bSdrh ** If X is a character that can be used in an identifier then
709b8f447bSdrh ** IdChar(X) will be true.  Otherwise it is false.
7198808babSdrh **
729b8f447bSdrh ** For ASCII, any character with the high-order bit set is
739b8f447bSdrh ** allowed in an identifier.  For 7-bit characters,
749b8f447bSdrh ** sqlite3IsIdChar[X] must be 1.
759b8f447bSdrh **
769b8f447bSdrh ** For EBCDIC, the rules are more complex but have the same
779b8f447bSdrh ** end result.
78a0d1f663Sdrh **
79a0d1f663Sdrh ** Ticket #1066.  the SQL standard does not allow '$' in the
8060ec914cSpeter.d.reid ** middle of identifiers.  But many SQL implementations do.
81a0d1f663Sdrh ** SQLite will allow '$' in identifiers for compatibility.
82a0d1f663Sdrh ** But the feature is undocumented.
8398808babSdrh */
849b8f447bSdrh #ifdef SQLITE_ASCII
85af2b5720Sdrh #define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
869b8f447bSdrh #endif
879b8f447bSdrh #ifdef SQLITE_EBCDIC
8846c99e0fSdrh const char sqlite3IsEbcdicIdChar[] = {
899b8f447bSdrh /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
909b8f447bSdrh     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 4x */
919b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0,  /* 5x */
929b8f447bSdrh     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,  /* 6x */
939b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,  /* 7x */
949b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0,  /* 8x */
959b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0,  /* 9x */
969b8f447bSdrh     1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0,  /* Ax */
979b8f447bSdrh     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* Bx */
989b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Cx */
999b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Dx */
1009b8f447bSdrh     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Ex */
1019b8f447bSdrh     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0,  /* Fx */
1029b8f447bSdrh };
10346c99e0fSdrh #define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
1049b8f447bSdrh #endif
1059b8f447bSdrh 
10698808babSdrh 
10775897234Sdrh /*
10861b487d0Sdrh ** Return the length of the token that begins at z[0].
10961b487d0Sdrh ** Store the token type in *tokenType before returning.
11075897234Sdrh */
11135b5a33eSdrh int sqlite3GetToken(const unsigned char *z, int *tokenType){
112aa756b09Sdrh   int i, c;
11375897234Sdrh   switch( *z ){
11430cab809Sdrh     case ' ': case '\t': case '\n': case '\f': case '\r': {
11519f81f6cSdrh       testcase( z[0]==' ' );
11619f81f6cSdrh       testcase( z[0]=='\t' );
11719f81f6cSdrh       testcase( z[0]=='\n' );
11819f81f6cSdrh       testcase( z[0]=='\f' );
11919f81f6cSdrh       testcase( z[0]=='\r' );
12078ca0e7eSdanielk1977       for(i=1; sqlite3Isspace(z[i]); i++){}
12175897234Sdrh       *tokenType = TK_SPACE;
12275897234Sdrh       return i;
12375897234Sdrh     }
12475897234Sdrh     case '-': {
12575897234Sdrh       if( z[1]=='-' ){
126aa756b09Sdrh         for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
127d3479b94Sdrh         *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
12875897234Sdrh         return i;
12975897234Sdrh       }
13075897234Sdrh       *tokenType = TK_MINUS;
13175897234Sdrh       return 1;
13275897234Sdrh     }
13375897234Sdrh     case '(': {
13475897234Sdrh       *tokenType = TK_LP;
13575897234Sdrh       return 1;
13675897234Sdrh     }
13775897234Sdrh     case ')': {
13875897234Sdrh       *tokenType = TK_RP;
13975897234Sdrh       return 1;
14075897234Sdrh     }
14175897234Sdrh     case ';': {
14275897234Sdrh       *tokenType = TK_SEMI;
14375897234Sdrh       return 1;
14475897234Sdrh     }
14575897234Sdrh     case '+': {
14675897234Sdrh       *tokenType = TK_PLUS;
14775897234Sdrh       return 1;
14875897234Sdrh     }
14975897234Sdrh     case '*': {
15075897234Sdrh       *tokenType = TK_STAR;
15175897234Sdrh       return 1;
15275897234Sdrh     }
15375897234Sdrh     case '/': {
15466105a8eSdrh       if( z[1]!='*' || z[2]==0 ){
15575897234Sdrh         *tokenType = TK_SLASH;
15675897234Sdrh         return 1;
15775897234Sdrh       }
158aa756b09Sdrh       for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
159aa756b09Sdrh       if( c ) i++;
160d3479b94Sdrh       *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
16166105a8eSdrh       return i;
16266105a8eSdrh     }
163bf4133cbSdrh     case '%': {
164bf4133cbSdrh       *tokenType = TK_REM;
165bf4133cbSdrh       return 1;
166bf4133cbSdrh     }
16775897234Sdrh     case '=': {
16875897234Sdrh       *tokenType = TK_EQ;
16975897234Sdrh       return 1 + (z[1]=='=');
17075897234Sdrh     }
17175897234Sdrh     case '<': {
172aa756b09Sdrh       if( (c=z[1])=='=' ){
17375897234Sdrh         *tokenType = TK_LE;
17475897234Sdrh         return 2;
175aa756b09Sdrh       }else if( c=='>' ){
17675897234Sdrh         *tokenType = TK_NE;
17775897234Sdrh         return 2;
178aa756b09Sdrh       }else if( c=='<' ){
179bf4133cbSdrh         *tokenType = TK_LSHIFT;
180bf4133cbSdrh         return 2;
18175897234Sdrh       }else{
18275897234Sdrh         *tokenType = TK_LT;
18375897234Sdrh         return 1;
18475897234Sdrh       }
18575897234Sdrh     }
18675897234Sdrh     case '>': {
187aa756b09Sdrh       if( (c=z[1])=='=' ){
18875897234Sdrh         *tokenType = TK_GE;
18975897234Sdrh         return 2;
190aa756b09Sdrh       }else if( c=='>' ){
191bf4133cbSdrh         *tokenType = TK_RSHIFT;
192bf4133cbSdrh         return 2;
19375897234Sdrh       }else{
19475897234Sdrh         *tokenType = TK_GT;
19575897234Sdrh         return 1;
19675897234Sdrh       }
19775897234Sdrh     }
19875897234Sdrh     case '!': {
19975897234Sdrh       if( z[1]!='=' ){
20075897234Sdrh         *tokenType = TK_ILLEGAL;
201c837e709Sdrh         return 2;
20275897234Sdrh       }else{
20375897234Sdrh         *tokenType = TK_NE;
20475897234Sdrh         return 2;
20575897234Sdrh       }
20675897234Sdrh     }
2070040077dSdrh     case '|': {
2080040077dSdrh       if( z[1]!='|' ){
209bf4133cbSdrh         *tokenType = TK_BITOR;
2100040077dSdrh         return 1;
2110040077dSdrh       }else{
2120040077dSdrh         *tokenType = TK_CONCAT;
2130040077dSdrh         return 2;
2140040077dSdrh       }
2150040077dSdrh     }
21675897234Sdrh     case ',': {
21775897234Sdrh       *tokenType = TK_COMMA;
21875897234Sdrh       return 1;
21975897234Sdrh     }
220bf4133cbSdrh     case '&': {
221bf4133cbSdrh       *tokenType = TK_BITAND;
222bf4133cbSdrh       return 1;
223bf4133cbSdrh     }
224bf4133cbSdrh     case '~': {
225bf4133cbSdrh       *tokenType = TK_BITNOT;
226bf4133cbSdrh       return 1;
227bf4133cbSdrh     }
2283d94662aSdrh     case '`':
2293d94662aSdrh     case '\'':
2303d94662aSdrh     case '"': {
23175897234Sdrh       int delim = z[0];
23219f81f6cSdrh       testcase( delim=='`' );
23319f81f6cSdrh       testcase( delim=='\'' );
23419f81f6cSdrh       testcase( delim=='"' );
235aa756b09Sdrh       for(i=1; (c=z[i])!=0; i++){
236aa756b09Sdrh         if( c==delim ){
23775897234Sdrh           if( z[i+1]==delim ){
23875897234Sdrh             i++;
23975897234Sdrh           }else{
24075897234Sdrh             break;
24175897234Sdrh           }
24275897234Sdrh         }
24375897234Sdrh       }
244a33cb5f7Sdrh       if( c=='\'' ){
24575897234Sdrh         *tokenType = TK_STRING;
246eef8b558Sdrh         return i+1;
247a33cb5f7Sdrh       }else if( c!=0 ){
248a33cb5f7Sdrh         *tokenType = TK_ID;
249a33cb5f7Sdrh         return i+1;
250eef8b558Sdrh       }else{
251eef8b558Sdrh         *tokenType = TK_ILLEGAL;
25275897234Sdrh         return i;
25375897234Sdrh       }
254eef8b558Sdrh     }
25575897234Sdrh     case '.': {
2567681618cSdrh #ifndef SQLITE_OMIT_FLOATING_POINT
25778ca0e7eSdanielk1977       if( !sqlite3Isdigit(z[1]) )
2587681618cSdrh #endif
2597681618cSdrh       {
26075897234Sdrh         *tokenType = TK_DOT;
26175897234Sdrh         return 1;
26275897234Sdrh       }
2637681618cSdrh       /* If the next character is a digit, this is a floating point
2647681618cSdrh       ** number that begins with ".".  Fall thru into the next case */
2657681618cSdrh     }
26675897234Sdrh     case '0': case '1': case '2': case '3': case '4':
26775897234Sdrh     case '5': case '6': case '7': case '8': case '9': {
26819f81f6cSdrh       testcase( z[0]=='0' );  testcase( z[0]=='1' );  testcase( z[0]=='2' );
26919f81f6cSdrh       testcase( z[0]=='3' );  testcase( z[0]=='4' );  testcase( z[0]=='5' );
27019f81f6cSdrh       testcase( z[0]=='6' );  testcase( z[0]=='7' );  testcase( z[0]=='8' );
27119f81f6cSdrh       testcase( z[0]=='9' );
272c837e709Sdrh       *tokenType = TK_INTEGER;
27328e048c6Sdrh #ifndef SQLITE_OMIT_HEX_INTEGER
27428e048c6Sdrh       if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){
27528e048c6Sdrh         for(i=3; sqlite3Isxdigit(z[i]); i++){}
27628e048c6Sdrh         return i;
27728e048c6Sdrh       }
27828e048c6Sdrh #endif
27978ca0e7eSdanielk1977       for(i=0; sqlite3Isdigit(z[i]); i++){}
280b7f9164eSdrh #ifndef SQLITE_OMIT_FLOATING_POINT
2817681618cSdrh       if( z[i]=='.' ){
2827681618cSdrh         i++;
28378ca0e7eSdanielk1977         while( sqlite3Isdigit(z[i]) ){ i++; }
284c837e709Sdrh         *tokenType = TK_FLOAT;
285c837e709Sdrh       }
28675897234Sdrh       if( (z[i]=='e' || z[i]=='E') &&
28778ca0e7eSdanielk1977            ( sqlite3Isdigit(z[i+1])
28878ca0e7eSdanielk1977             || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
28975897234Sdrh            )
29075897234Sdrh       ){
29175897234Sdrh         i += 2;
29278ca0e7eSdanielk1977         while( sqlite3Isdigit(z[i]) ){ i++; }
29375897234Sdrh         *tokenType = TK_FLOAT;
29475897234Sdrh       }
295b7f9164eSdrh #endif
29667dd9011Sdrh       while( IdChar(z[i]) ){
29767dd9011Sdrh         *tokenType = TK_ILLEGAL;
29867dd9011Sdrh         i++;
29967dd9011Sdrh       }
30075897234Sdrh       return i;
30175897234Sdrh     }
3022f4392ffSdrh     case '[': {
303aa756b09Sdrh       for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
3044b2f9368Sdrh       *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
3052f4392ffSdrh       return i;
3062f4392ffSdrh     }
3077c972decSdrh     case '?': {
30850457896Sdrh       *tokenType = TK_VARIABLE;
30978ca0e7eSdanielk1977       for(i=1; sqlite3Isdigit(z[i]); i++){}
310fa6bc000Sdrh       return i;
31150457896Sdrh     }
312b7f9164eSdrh #ifndef SQLITE_OMIT_TCL_VARIABLE
313288d37f1Sdrh     case '$':
314288d37f1Sdrh #endif
3150b2a5eeaSdrh     case '@':  /* For compatibility with MS SQL Server */
316f59b12fbSdrh     case '#':
317288d37f1Sdrh     case ':': {
318895d7472Sdrh       int n = 0;
319f59b12fbSdrh       testcase( z[0]=='$' );  testcase( z[0]=='@' );
320f59b12fbSdrh       testcase( z[0]==':' );  testcase( z[0]=='#' );
321288d37f1Sdrh       *tokenType = TK_VARIABLE;
322895d7472Sdrh       for(i=1; (c=z[i])!=0; i++){
323288d37f1Sdrh         if( IdChar(c) ){
324895d7472Sdrh           n++;
325288d37f1Sdrh #ifndef SQLITE_OMIT_TCL_VARIABLE
326895d7472Sdrh         }else if( c=='(' && n>0 ){
327895d7472Sdrh           do{
328895d7472Sdrh             i++;
32978ca0e7eSdanielk1977           }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
330895d7472Sdrh           if( c==')' ){
331895d7472Sdrh             i++;
332895d7472Sdrh           }else{
333895d7472Sdrh             *tokenType = TK_ILLEGAL;
334895d7472Sdrh           }
335895d7472Sdrh           break;
336895d7472Sdrh         }else if( c==':' && z[i+1]==':' ){
337895d7472Sdrh           i++;
338288d37f1Sdrh #endif
339895d7472Sdrh         }else{
340895d7472Sdrh           break;
341895d7472Sdrh         }
342895d7472Sdrh       }
3439d74b4c5Sdrh       if( n==0 ) *tokenType = TK_ILLEGAL;
344895d7472Sdrh       return i;
345895d7472Sdrh     }
346b7f9164eSdrh #ifndef SQLITE_OMIT_BLOB_LITERAL
347c572ef7fSdanielk1977     case 'x': case 'X': {
34819f81f6cSdrh       testcase( z[0]=='x' ); testcase( z[0]=='X' );
3494b2f9368Sdrh       if( z[1]=='\'' ){
3503fd0a736Sdanielk1977         *tokenType = TK_BLOB;
3513a61a5a2Sdrh         for(i=2; sqlite3Isxdigit(z[i]); i++){}
3523a61a5a2Sdrh         if( z[i]!='\'' || i%2 ){
3533fd0a736Sdanielk1977           *tokenType = TK_ILLEGAL;
3543a61a5a2Sdrh           while( z[i] && z[i]!='\'' ){ i++; }
3553fd0a736Sdanielk1977         }
3563a61a5a2Sdrh         if( z[i] ) i++;
357c572ef7fSdanielk1977         return i;
358c572ef7fSdanielk1977       }
359c572ef7fSdanielk1977       /* Otherwise fall through to the next case */
360c572ef7fSdanielk1977     }
361b7f9164eSdrh #endif
36298808babSdrh     default: {
3639a087a99Sdrh       if( !IdChar(*z) ){
36498808babSdrh         break;
36598808babSdrh       }
366aa756b09Sdrh       for(i=1; IdChar(z[i]); i++){}
367c60e9b82Sdanielk1977       *tokenType = keywordCode((char*)z, i);
36875897234Sdrh       return i;
36975897234Sdrh     }
37075897234Sdrh   }
37175897234Sdrh   *tokenType = TK_ILLEGAL;
37275897234Sdrh   return 1;
37375897234Sdrh }
37475897234Sdrh 
37575897234Sdrh /*
37675897234Sdrh ** Run the parser on the given SQL string.  The parser structure is
377b19a2bc6Sdrh ** passed in.  An SQLITE_ status code is returned.  If an error occurs
378fb45d8c5Sdrh ** then an and attempt is made to write an error message into
379fb45d8c5Sdrh ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
380fb45d8c5Sdrh ** error message.
38175897234Sdrh */
3824adee20fSdanielk1977 int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
383d9da78a2Sdrh   int nErr = 0;                   /* Number of errors encountered */
384d9da78a2Sdrh   int i;                          /* Loop counter */
385d9da78a2Sdrh   void *pEngine;                  /* The LEMON-generated LALR(1) parser */
386d9da78a2Sdrh   int tokenType;                  /* type of the next token */
387d9da78a2Sdrh   int lastTokenParsed = -1;       /* type of the previous token */
388b08a67a7Sshane   u8 enableLookaside;             /* Saved value of db->lookaside.bEnabled */
389d9da78a2Sdrh   sqlite3 *db = pParse->db;       /* The database connection */
390d9da78a2Sdrh   int mxSqlLen;                   /* Max length of an SQL string */
39175897234Sdrh 
392d9da78a2Sdrh 
393d9da78a2Sdrh   mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
3944f7d3a5fSdrh   if( db->nVdbeActive==0 ){
395881feaa0Sdrh     db->u1.isInterrupted = 0;
39615ca1df1Sdrh   }
3974c504391Sdrh   pParse->rc = SQLITE_OK;
398e7f3f3eeSdrh   pParse->zTail = zSql;
39975897234Sdrh   i = 0;
400fb45d8c5Sdrh   assert( pzErrMsg!=0 );
401*fb046e76Sdrh   pEngine = sqlite3ParserAlloc(sqlite3Malloc);
40275897234Sdrh   if( pEngine==0 ){
403f3a65f7eSdrh     db->mallocFailed = 1;
404defc9972Sdrh     return SQLITE_NOMEM;
40575897234Sdrh   }
406fa6bc000Sdrh   assert( pParse->pNewTable==0 );
407fa6bc000Sdrh   assert( pParse->pNewTrigger==0 );
408fa6bc000Sdrh   assert( pParse->nVar==0 );
409124c0b49Sdrh   assert( pParse->nzVar==0 );
410124c0b49Sdrh   assert( pParse->azVar==0 );
411d9da78a2Sdrh   enableLookaside = db->lookaside.bEnabled;
412d9da78a2Sdrh   if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
41317435752Sdrh   while( !db->mallocFailed && zSql[i]!=0 ){
41432eb7b47Sdrh     assert( i>=0 );
415b7916a78Sdrh     pParse->sLastToken.z = &zSql[i];
41635b5a33eSdrh     pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
41775897234Sdrh     i += pParse->sLastToken.n;
418bb4957f8Sdrh     if( i>mxSqlLen ){
419e5c941b8Sdrh       pParse->rc = SQLITE_TOOBIG;
420e5c941b8Sdrh       break;
421e5c941b8Sdrh     }
42275897234Sdrh     switch( tokenType ){
423200a81dcSdrh       case TK_SPACE: {
424881feaa0Sdrh         if( db->u1.isInterrupted ){
42519f81f6cSdrh           sqlite3ErrorMsg(pParse, "interrupt");
42632eb7b47Sdrh           pParse->rc = SQLITE_INTERRUPT;
42732eb7b47Sdrh           goto abort_parse;
42832eb7b47Sdrh         }
42975897234Sdrh         break;
43075897234Sdrh       }
43132eb7b47Sdrh       case TK_ILLEGAL: {
432633e6d57Sdrh         sqlite3DbFree(db, *pzErrMsg);
4331e536953Sdanielk1977         *pzErrMsg = sqlite3MPrintf(db, "unrecognized token: \"%T\"",
434ae29ffbeSdrh                         &pParse->sLastToken);
43575897234Sdrh         nErr++;
436caec2f12Sdrh         goto abort_parse;
43732eb7b47Sdrh       }
438326dce74Sdrh       case TK_SEMI: {
439326dce74Sdrh         pParse->zTail = &zSql[i];
440326dce74Sdrh         /* Fall thru into the default case */
441326dce74Sdrh       }
44232eb7b47Sdrh       default: {
4434adee20fSdanielk1977         sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
444b86ccfb2Sdrh         lastTokenParsed = tokenType;
445b86ccfb2Sdrh         if( pParse->rc!=SQLITE_OK ){
44632eb7b47Sdrh           goto abort_parse;
44775897234Sdrh         }
44875897234Sdrh         break;
44975897234Sdrh       }
45075897234Sdrh     }
45132eb7b47Sdrh   }
45232eb7b47Sdrh abort_parse:
453b86ccfb2Sdrh   if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
454b86ccfb2Sdrh     if( lastTokenParsed!=TK_SEMI ){
4554adee20fSdanielk1977       sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
456326dce74Sdrh       pParse->zTail = &zSql[i];
45775897234Sdrh     }
4584adee20fSdanielk1977     sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
45975897234Sdrh   }
460ec424a5bSdrh #ifdef YYTRACKMAXSTACKDEPTH
461ec424a5bSdrh   sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK,
462ec424a5bSdrh       sqlite3ParserStackPeak(pEngine)
463ec424a5bSdrh   );
464ec424a5bSdrh #endif /* YYDEBUG */
46517435752Sdrh   sqlite3ParserFree(pEngine, sqlite3_free);
466d9da78a2Sdrh   db->lookaside.bEnabled = enableLookaside;
46717435752Sdrh   if( db->mallocFailed ){
46871c697efSdrh     pParse->rc = SQLITE_NOMEM;
46971c697efSdrh   }
470b86ccfb2Sdrh   if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
471f089aa45Sdrh     sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc));
472b86ccfb2Sdrh   }
47319f81f6cSdrh   assert( pzErrMsg!=0 );
47475897234Sdrh   if( pParse->zErrMsg ){
47575897234Sdrh     *pzErrMsg = pParse->zErrMsg;
476413c3d36Sdrh     sqlite3_log(pParse->rc, "%s", *pzErrMsg);
477b86ccfb2Sdrh     pParse->zErrMsg = 0;
4784b2f9368Sdrh     nErr++;
47975897234Sdrh   }
4802958a4e6Sdrh   if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
4814adee20fSdanielk1977     sqlite3VdbeDelete(pParse->pVdbe);
48275897234Sdrh     pParse->pVdbe = 0;
48375897234Sdrh   }
484c00da105Sdanielk1977 #ifndef SQLITE_OMIT_SHARED_CACHE
485c00da105Sdanielk1977   if( pParse->nested==0 ){
486633e6d57Sdrh     sqlite3DbFree(db, pParse->aTableLock);
487c00da105Sdanielk1977     pParse->aTableLock = 0;
488c00da105Sdanielk1977     pParse->nTableLock = 0;
489c00da105Sdanielk1977   }
490c00da105Sdanielk1977 #endif
4914f3dd150Sdrh #ifndef SQLITE_OMIT_VIRTUALTABLE
492b975598eSdrh   sqlite3_free(pParse->apVtabLock);
4934f3dd150Sdrh #endif
4947e6ebfb2Sdanielk1977 
4957e6ebfb2Sdanielk1977   if( !IN_DECLARE_VTAB ){
4967e6ebfb2Sdanielk1977     /* If the pParse->declareVtab flag is set, do not delete any table
4977e6ebfb2Sdanielk1977     ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
4987e6ebfb2Sdanielk1977     ** will take responsibility for freeing the Table structure.
4997e6ebfb2Sdanielk1977     */
5001feeaed2Sdan     sqlite3DeleteTable(db, pParse->pNewTable);
5017e6ebfb2Sdanielk1977   }
5027e6ebfb2Sdanielk1977 
503b290f117Sdan   if( pParse->bFreeWith ) sqlite3WithDelete(db, pParse->pWith);
504633e6d57Sdrh   sqlite3DeleteTrigger(db, pParse->pNewTrigger);
505124c0b49Sdrh   for(i=pParse->nzVar-1; i>=0; i--) sqlite3DbFree(db, pParse->azVar[i]);
506124c0b49Sdrh   sqlite3DbFree(db, pParse->azVar);
5070b9f50d8Sdrh   while( pParse->pAinc ){
5080b9f50d8Sdrh     AutoincInfo *p = pParse->pAinc;
5090b9f50d8Sdrh     pParse->pAinc = p->pNext;
5100b9f50d8Sdrh     sqlite3DbFree(db, p);
5110b9f50d8Sdrh   }
512588a9a1aSdrh   while( pParse->pZombieTab ){
513588a9a1aSdrh     Table *p = pParse->pZombieTab;
514588a9a1aSdrh     pParse->pZombieTab = p->pNextZombie;
5151feeaed2Sdan     sqlite3DeleteTable(db, p);
516588a9a1aSdrh   }
517b5092d18Sdrh   if( nErr>0 && pParse->rc==SQLITE_OK ){
5184c504391Sdrh     pParse->rc = SQLITE_ERROR;
5194c504391Sdrh   }
52075897234Sdrh   return nErr;
52175897234Sdrh }
522