xref: /sqlite-3.40.0/src/tokenize.c (revision 3bd48ab2)
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
105f5ed7ad6Sdrh 
106f5ed7ad6Sdrh /* Make the IdChar function accessible from ctime.c */
107f5ed7ad6Sdrh #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
10897348b37Sdrh int sqlite3IsIdChar(u8 c){ return IdChar(c); }
109f5ed7ad6Sdrh #endif
1109b8f447bSdrh 
11198808babSdrh 
11275897234Sdrh /*
11361b487d0Sdrh ** Return the length of the token that begins at z[0].
11461b487d0Sdrh ** Store the token type in *tokenType before returning.
11575897234Sdrh */
11635b5a33eSdrh int sqlite3GetToken(const unsigned char *z, int *tokenType){
117aa756b09Sdrh   int i, c;
11875897234Sdrh   switch( *z ){
11930cab809Sdrh     case ' ': case '\t': case '\n': case '\f': case '\r': {
12019f81f6cSdrh       testcase( z[0]==' ' );
12119f81f6cSdrh       testcase( z[0]=='\t' );
12219f81f6cSdrh       testcase( z[0]=='\n' );
12319f81f6cSdrh       testcase( z[0]=='\f' );
12419f81f6cSdrh       testcase( z[0]=='\r' );
12578ca0e7eSdanielk1977       for(i=1; sqlite3Isspace(z[i]); i++){}
12675897234Sdrh       *tokenType = TK_SPACE;
12775897234Sdrh       return i;
12875897234Sdrh     }
12975897234Sdrh     case '-': {
13075897234Sdrh       if( z[1]=='-' ){
131aa756b09Sdrh         for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
132d3479b94Sdrh         *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
13375897234Sdrh         return i;
13475897234Sdrh       }
13575897234Sdrh       *tokenType = TK_MINUS;
13675897234Sdrh       return 1;
13775897234Sdrh     }
13875897234Sdrh     case '(': {
13975897234Sdrh       *tokenType = TK_LP;
14075897234Sdrh       return 1;
14175897234Sdrh     }
14275897234Sdrh     case ')': {
14375897234Sdrh       *tokenType = TK_RP;
14475897234Sdrh       return 1;
14575897234Sdrh     }
14675897234Sdrh     case ';': {
14775897234Sdrh       *tokenType = TK_SEMI;
14875897234Sdrh       return 1;
14975897234Sdrh     }
15075897234Sdrh     case '+': {
15175897234Sdrh       *tokenType = TK_PLUS;
15275897234Sdrh       return 1;
15375897234Sdrh     }
15475897234Sdrh     case '*': {
15575897234Sdrh       *tokenType = TK_STAR;
15675897234Sdrh       return 1;
15775897234Sdrh     }
15875897234Sdrh     case '/': {
15966105a8eSdrh       if( z[1]!='*' || z[2]==0 ){
16075897234Sdrh         *tokenType = TK_SLASH;
16175897234Sdrh         return 1;
16275897234Sdrh       }
163aa756b09Sdrh       for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
164aa756b09Sdrh       if( c ) i++;
165d3479b94Sdrh       *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
16666105a8eSdrh       return i;
16766105a8eSdrh     }
168bf4133cbSdrh     case '%': {
169bf4133cbSdrh       *tokenType = TK_REM;
170bf4133cbSdrh       return 1;
171bf4133cbSdrh     }
17275897234Sdrh     case '=': {
17375897234Sdrh       *tokenType = TK_EQ;
17475897234Sdrh       return 1 + (z[1]=='=');
17575897234Sdrh     }
17675897234Sdrh     case '<': {
177aa756b09Sdrh       if( (c=z[1])=='=' ){
17875897234Sdrh         *tokenType = TK_LE;
17975897234Sdrh         return 2;
180aa756b09Sdrh       }else if( c=='>' ){
18175897234Sdrh         *tokenType = TK_NE;
18275897234Sdrh         return 2;
183aa756b09Sdrh       }else if( c=='<' ){
184bf4133cbSdrh         *tokenType = TK_LSHIFT;
185bf4133cbSdrh         return 2;
18675897234Sdrh       }else{
18775897234Sdrh         *tokenType = TK_LT;
18875897234Sdrh         return 1;
18975897234Sdrh       }
19075897234Sdrh     }
19175897234Sdrh     case '>': {
192aa756b09Sdrh       if( (c=z[1])=='=' ){
19375897234Sdrh         *tokenType = TK_GE;
19475897234Sdrh         return 2;
195aa756b09Sdrh       }else if( c=='>' ){
196bf4133cbSdrh         *tokenType = TK_RSHIFT;
197bf4133cbSdrh         return 2;
19875897234Sdrh       }else{
19975897234Sdrh         *tokenType = TK_GT;
20075897234Sdrh         return 1;
20175897234Sdrh       }
20275897234Sdrh     }
20375897234Sdrh     case '!': {
20475897234Sdrh       if( z[1]!='=' ){
20575897234Sdrh         *tokenType = TK_ILLEGAL;
206c837e709Sdrh         return 2;
20775897234Sdrh       }else{
20875897234Sdrh         *tokenType = TK_NE;
20975897234Sdrh         return 2;
21075897234Sdrh       }
21175897234Sdrh     }
2120040077dSdrh     case '|': {
2130040077dSdrh       if( z[1]!='|' ){
214bf4133cbSdrh         *tokenType = TK_BITOR;
2150040077dSdrh         return 1;
2160040077dSdrh       }else{
2170040077dSdrh         *tokenType = TK_CONCAT;
2180040077dSdrh         return 2;
2190040077dSdrh       }
2200040077dSdrh     }
22175897234Sdrh     case ',': {
22275897234Sdrh       *tokenType = TK_COMMA;
22375897234Sdrh       return 1;
22475897234Sdrh     }
225bf4133cbSdrh     case '&': {
226bf4133cbSdrh       *tokenType = TK_BITAND;
227bf4133cbSdrh       return 1;
228bf4133cbSdrh     }
229bf4133cbSdrh     case '~': {
230bf4133cbSdrh       *tokenType = TK_BITNOT;
231bf4133cbSdrh       return 1;
232bf4133cbSdrh     }
2333d94662aSdrh     case '`':
2343d94662aSdrh     case '\'':
2353d94662aSdrh     case '"': {
23675897234Sdrh       int delim = z[0];
23719f81f6cSdrh       testcase( delim=='`' );
23819f81f6cSdrh       testcase( delim=='\'' );
23919f81f6cSdrh       testcase( delim=='"' );
240aa756b09Sdrh       for(i=1; (c=z[i])!=0; i++){
241aa756b09Sdrh         if( c==delim ){
24275897234Sdrh           if( z[i+1]==delim ){
24375897234Sdrh             i++;
24475897234Sdrh           }else{
24575897234Sdrh             break;
24675897234Sdrh           }
24775897234Sdrh         }
24875897234Sdrh       }
249a33cb5f7Sdrh       if( c=='\'' ){
25075897234Sdrh         *tokenType = TK_STRING;
251eef8b558Sdrh         return i+1;
252a33cb5f7Sdrh       }else if( c!=0 ){
253a33cb5f7Sdrh         *tokenType = TK_ID;
254a33cb5f7Sdrh         return i+1;
255eef8b558Sdrh       }else{
256eef8b558Sdrh         *tokenType = TK_ILLEGAL;
25775897234Sdrh         return i;
25875897234Sdrh       }
259eef8b558Sdrh     }
26075897234Sdrh     case '.': {
2617681618cSdrh #ifndef SQLITE_OMIT_FLOATING_POINT
26278ca0e7eSdanielk1977       if( !sqlite3Isdigit(z[1]) )
2637681618cSdrh #endif
2647681618cSdrh       {
26575897234Sdrh         *tokenType = TK_DOT;
26675897234Sdrh         return 1;
26775897234Sdrh       }
2687681618cSdrh       /* If the next character is a digit, this is a floating point
2697681618cSdrh       ** number that begins with ".".  Fall thru into the next case */
2707681618cSdrh     }
27175897234Sdrh     case '0': case '1': case '2': case '3': case '4':
27275897234Sdrh     case '5': case '6': case '7': case '8': case '9': {
27319f81f6cSdrh       testcase( z[0]=='0' );  testcase( z[0]=='1' );  testcase( z[0]=='2' );
27419f81f6cSdrh       testcase( z[0]=='3' );  testcase( z[0]=='4' );  testcase( z[0]=='5' );
27519f81f6cSdrh       testcase( z[0]=='6' );  testcase( z[0]=='7' );  testcase( z[0]=='8' );
27619f81f6cSdrh       testcase( z[0]=='9' );
277c837e709Sdrh       *tokenType = TK_INTEGER;
27828e048c6Sdrh #ifndef SQLITE_OMIT_HEX_INTEGER
27928e048c6Sdrh       if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){
28028e048c6Sdrh         for(i=3; sqlite3Isxdigit(z[i]); i++){}
28128e048c6Sdrh         return i;
28228e048c6Sdrh       }
28328e048c6Sdrh #endif
28478ca0e7eSdanielk1977       for(i=0; sqlite3Isdigit(z[i]); i++){}
285b7f9164eSdrh #ifndef SQLITE_OMIT_FLOATING_POINT
2867681618cSdrh       if( z[i]=='.' ){
2877681618cSdrh         i++;
28878ca0e7eSdanielk1977         while( sqlite3Isdigit(z[i]) ){ i++; }
289c837e709Sdrh         *tokenType = TK_FLOAT;
290c837e709Sdrh       }
29175897234Sdrh       if( (z[i]=='e' || z[i]=='E') &&
29278ca0e7eSdanielk1977            ( sqlite3Isdigit(z[i+1])
29378ca0e7eSdanielk1977             || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
29475897234Sdrh            )
29575897234Sdrh       ){
29675897234Sdrh         i += 2;
29778ca0e7eSdanielk1977         while( sqlite3Isdigit(z[i]) ){ i++; }
29875897234Sdrh         *tokenType = TK_FLOAT;
29975897234Sdrh       }
300b7f9164eSdrh #endif
30167dd9011Sdrh       while( IdChar(z[i]) ){
30267dd9011Sdrh         *tokenType = TK_ILLEGAL;
30367dd9011Sdrh         i++;
30467dd9011Sdrh       }
30575897234Sdrh       return i;
30675897234Sdrh     }
3072f4392ffSdrh     case '[': {
308aa756b09Sdrh       for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
3094b2f9368Sdrh       *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
3102f4392ffSdrh       return i;
3112f4392ffSdrh     }
3127c972decSdrh     case '?': {
31350457896Sdrh       *tokenType = TK_VARIABLE;
31478ca0e7eSdanielk1977       for(i=1; sqlite3Isdigit(z[i]); i++){}
315fa6bc000Sdrh       return i;
31650457896Sdrh     }
317b7f9164eSdrh #ifndef SQLITE_OMIT_TCL_VARIABLE
318288d37f1Sdrh     case '$':
319288d37f1Sdrh #endif
3200b2a5eeaSdrh     case '@':  /* For compatibility with MS SQL Server */
321f59b12fbSdrh     case '#':
322288d37f1Sdrh     case ':': {
323895d7472Sdrh       int n = 0;
324f59b12fbSdrh       testcase( z[0]=='$' );  testcase( z[0]=='@' );
325f59b12fbSdrh       testcase( z[0]==':' );  testcase( z[0]=='#' );
326288d37f1Sdrh       *tokenType = TK_VARIABLE;
327895d7472Sdrh       for(i=1; (c=z[i])!=0; i++){
328288d37f1Sdrh         if( IdChar(c) ){
329895d7472Sdrh           n++;
330288d37f1Sdrh #ifndef SQLITE_OMIT_TCL_VARIABLE
331895d7472Sdrh         }else if( c=='(' && n>0 ){
332895d7472Sdrh           do{
333895d7472Sdrh             i++;
33478ca0e7eSdanielk1977           }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
335895d7472Sdrh           if( c==')' ){
336895d7472Sdrh             i++;
337895d7472Sdrh           }else{
338895d7472Sdrh             *tokenType = TK_ILLEGAL;
339895d7472Sdrh           }
340895d7472Sdrh           break;
341895d7472Sdrh         }else if( c==':' && z[i+1]==':' ){
342895d7472Sdrh           i++;
343288d37f1Sdrh #endif
344895d7472Sdrh         }else{
345895d7472Sdrh           break;
346895d7472Sdrh         }
347895d7472Sdrh       }
3489d74b4c5Sdrh       if( n==0 ) *tokenType = TK_ILLEGAL;
349895d7472Sdrh       return i;
350895d7472Sdrh     }
351b7f9164eSdrh #ifndef SQLITE_OMIT_BLOB_LITERAL
352c572ef7fSdanielk1977     case 'x': case 'X': {
35319f81f6cSdrh       testcase( z[0]=='x' ); testcase( z[0]=='X' );
3544b2f9368Sdrh       if( z[1]=='\'' ){
3553fd0a736Sdanielk1977         *tokenType = TK_BLOB;
3563a61a5a2Sdrh         for(i=2; sqlite3Isxdigit(z[i]); i++){}
3573a61a5a2Sdrh         if( z[i]!='\'' || i%2 ){
3583fd0a736Sdanielk1977           *tokenType = TK_ILLEGAL;
3593a61a5a2Sdrh           while( z[i] && z[i]!='\'' ){ i++; }
3603fd0a736Sdanielk1977         }
3613a61a5a2Sdrh         if( z[i] ) i++;
362c572ef7fSdanielk1977         return i;
363c572ef7fSdanielk1977       }
364c572ef7fSdanielk1977       /* Otherwise fall through to the next case */
365c572ef7fSdanielk1977     }
366b7f9164eSdrh #endif
36798808babSdrh     default: {
3689a087a99Sdrh       if( !IdChar(*z) ){
36998808babSdrh         break;
37098808babSdrh       }
371aa756b09Sdrh       for(i=1; IdChar(z[i]); i++){}
372c60e9b82Sdanielk1977       *tokenType = keywordCode((char*)z, i);
37375897234Sdrh       return i;
37475897234Sdrh     }
37575897234Sdrh   }
37675897234Sdrh   *tokenType = TK_ILLEGAL;
37775897234Sdrh   return 1;
37875897234Sdrh }
37975897234Sdrh 
38075897234Sdrh /*
38175897234Sdrh ** Run the parser on the given SQL string.  The parser structure is
382b19a2bc6Sdrh ** passed in.  An SQLITE_ status code is returned.  If an error occurs
383fb45d8c5Sdrh ** then an and attempt is made to write an error message into
384fb45d8c5Sdrh ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
385fb45d8c5Sdrh ** error message.
38675897234Sdrh */
3874adee20fSdanielk1977 int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
388d9da78a2Sdrh   int nErr = 0;                   /* Number of errors encountered */
389d9da78a2Sdrh   int i;                          /* Loop counter */
390d9da78a2Sdrh   void *pEngine;                  /* The LEMON-generated LALR(1) parser */
391d9da78a2Sdrh   int tokenType;                  /* type of the next token */
392d9da78a2Sdrh   int lastTokenParsed = -1;       /* type of the previous token */
393b08a67a7Sshane   u8 enableLookaside;             /* Saved value of db->lookaside.bEnabled */
394d9da78a2Sdrh   sqlite3 *db = pParse->db;       /* The database connection */
395d9da78a2Sdrh   int mxSqlLen;                   /* Max length of an SQL string */
39675897234Sdrh 
39796c707a3Sdrh   assert( zSql!=0 );
398d9da78a2Sdrh   mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
3994f7d3a5fSdrh   if( db->nVdbeActive==0 ){
400881feaa0Sdrh     db->u1.isInterrupted = 0;
40115ca1df1Sdrh   }
4024c504391Sdrh   pParse->rc = SQLITE_OK;
403e7f3f3eeSdrh   pParse->zTail = zSql;
40475897234Sdrh   i = 0;
405fb45d8c5Sdrh   assert( pzErrMsg!=0 );
406*3bd48ab2Sdrh   /* sqlite3ParserTrace(stdout, "parser: "); */
407fb046e76Sdrh   pEngine = sqlite3ParserAlloc(sqlite3Malloc);
40875897234Sdrh   if( pEngine==0 ){
409f3a65f7eSdrh     db->mallocFailed = 1;
410defc9972Sdrh     return SQLITE_NOMEM;
41175897234Sdrh   }
412fa6bc000Sdrh   assert( pParse->pNewTable==0 );
413fa6bc000Sdrh   assert( pParse->pNewTrigger==0 );
414fa6bc000Sdrh   assert( pParse->nVar==0 );
415124c0b49Sdrh   assert( pParse->nzVar==0 );
416124c0b49Sdrh   assert( pParse->azVar==0 );
417d9da78a2Sdrh   enableLookaside = db->lookaside.bEnabled;
418d9da78a2Sdrh   if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
41917435752Sdrh   while( !db->mallocFailed && zSql[i]!=0 ){
42032eb7b47Sdrh     assert( i>=0 );
421b7916a78Sdrh     pParse->sLastToken.z = &zSql[i];
42235b5a33eSdrh     pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
42375897234Sdrh     i += pParse->sLastToken.n;
424bb4957f8Sdrh     if( i>mxSqlLen ){
425e5c941b8Sdrh       pParse->rc = SQLITE_TOOBIG;
426e5c941b8Sdrh       break;
427e5c941b8Sdrh     }
42875897234Sdrh     switch( tokenType ){
429200a81dcSdrh       case TK_SPACE: {
430881feaa0Sdrh         if( db->u1.isInterrupted ){
43119f81f6cSdrh           sqlite3ErrorMsg(pParse, "interrupt");
43232eb7b47Sdrh           pParse->rc = SQLITE_INTERRUPT;
43332eb7b47Sdrh           goto abort_parse;
43432eb7b47Sdrh         }
43575897234Sdrh         break;
43675897234Sdrh       }
43732eb7b47Sdrh       case TK_ILLEGAL: {
438347bdc39Sdrh         sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"",
439ae29ffbeSdrh                         &pParse->sLastToken);
440caec2f12Sdrh         goto abort_parse;
44132eb7b47Sdrh       }
442326dce74Sdrh       case TK_SEMI: {
443326dce74Sdrh         pParse->zTail = &zSql[i];
444326dce74Sdrh         /* Fall thru into the default case */
445326dce74Sdrh       }
44632eb7b47Sdrh       default: {
4474adee20fSdanielk1977         sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
448b86ccfb2Sdrh         lastTokenParsed = tokenType;
449b86ccfb2Sdrh         if( pParse->rc!=SQLITE_OK ){
45032eb7b47Sdrh           goto abort_parse;
45175897234Sdrh         }
45275897234Sdrh         break;
45375897234Sdrh       }
45475897234Sdrh     }
45532eb7b47Sdrh   }
45632eb7b47Sdrh abort_parse:
4570be0cf67Sdrh   assert( nErr==0 );
458e7266225Sdrh   if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){
459e7266225Sdrh     assert( zSql[i]==0 );
460b86ccfb2Sdrh     if( lastTokenParsed!=TK_SEMI ){
4614adee20fSdanielk1977       sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
46275897234Sdrh     }
4639a9705deSdrh     if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){
4644adee20fSdanielk1977       sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
46575897234Sdrh     }
4669a9705deSdrh   }
467*3bd48ab2Sdrh   pParse->zTail = &zSql[i];
468ec424a5bSdrh #ifdef YYTRACKMAXSTACKDEPTH
469af89fe66Sdrh   sqlite3_mutex_enter(sqlite3MallocMutex());
470ec424a5bSdrh   sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK,
471ec424a5bSdrh       sqlite3ParserStackPeak(pEngine)
472ec424a5bSdrh   );
473af89fe66Sdrh   sqlite3_mutex_leave(sqlite3MallocMutex());
474ec424a5bSdrh #endif /* YYDEBUG */
47517435752Sdrh   sqlite3ParserFree(pEngine, sqlite3_free);
476d9da78a2Sdrh   db->lookaside.bEnabled = enableLookaside;
47717435752Sdrh   if( db->mallocFailed ){
47871c697efSdrh     pParse->rc = SQLITE_NOMEM;
47971c697efSdrh   }
480b86ccfb2Sdrh   if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
48122c17b8bSdrh     pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
482b86ccfb2Sdrh   }
48319f81f6cSdrh   assert( pzErrMsg!=0 );
48475897234Sdrh   if( pParse->zErrMsg ){
48575897234Sdrh     *pzErrMsg = pParse->zErrMsg;
486413c3d36Sdrh     sqlite3_log(pParse->rc, "%s", *pzErrMsg);
487b86ccfb2Sdrh     pParse->zErrMsg = 0;
4884b2f9368Sdrh     nErr++;
48975897234Sdrh   }
4902958a4e6Sdrh   if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
4914adee20fSdanielk1977     sqlite3VdbeDelete(pParse->pVdbe);
49275897234Sdrh     pParse->pVdbe = 0;
49375897234Sdrh   }
494c00da105Sdanielk1977 #ifndef SQLITE_OMIT_SHARED_CACHE
495c00da105Sdanielk1977   if( pParse->nested==0 ){
496633e6d57Sdrh     sqlite3DbFree(db, pParse->aTableLock);
497c00da105Sdanielk1977     pParse->aTableLock = 0;
498c00da105Sdanielk1977     pParse->nTableLock = 0;
499c00da105Sdanielk1977   }
500c00da105Sdanielk1977 #endif
5014f3dd150Sdrh #ifndef SQLITE_OMIT_VIRTUALTABLE
502b975598eSdrh   sqlite3_free(pParse->apVtabLock);
5034f3dd150Sdrh #endif
5047e6ebfb2Sdanielk1977 
5057e6ebfb2Sdanielk1977   if( !IN_DECLARE_VTAB ){
5067e6ebfb2Sdanielk1977     /* If the pParse->declareVtab flag is set, do not delete any table
5077e6ebfb2Sdanielk1977     ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
5087e6ebfb2Sdanielk1977     ** will take responsibility for freeing the Table structure.
5097e6ebfb2Sdanielk1977     */
5101feeaed2Sdan     sqlite3DeleteTable(db, pParse->pNewTable);
5117e6ebfb2Sdanielk1977   }
5127e6ebfb2Sdanielk1977 
513b290f117Sdan   if( pParse->bFreeWith ) sqlite3WithDelete(db, pParse->pWith);
514633e6d57Sdrh   sqlite3DeleteTrigger(db, pParse->pNewTrigger);
515124c0b49Sdrh   for(i=pParse->nzVar-1; i>=0; i--) sqlite3DbFree(db, pParse->azVar[i]);
516124c0b49Sdrh   sqlite3DbFree(db, pParse->azVar);
5170b9f50d8Sdrh   while( pParse->pAinc ){
5180b9f50d8Sdrh     AutoincInfo *p = pParse->pAinc;
5190b9f50d8Sdrh     pParse->pAinc = p->pNext;
5200b9f50d8Sdrh     sqlite3DbFree(db, p);
5210b9f50d8Sdrh   }
522588a9a1aSdrh   while( pParse->pZombieTab ){
523588a9a1aSdrh     Table *p = pParse->pZombieTab;
524588a9a1aSdrh     pParse->pZombieTab = p->pNextZombie;
5251feeaed2Sdan     sqlite3DeleteTable(db, p);
526588a9a1aSdrh   }
527f3151f0aSdrh   assert( nErr==0 || pParse->rc!=SQLITE_OK );
52875897234Sdrh   return nErr;
52975897234Sdrh }
530