xref: /sqlite-3.40.0/src/tokenize.c (revision 413c3d36)
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
80a0d1f663Sdrh ** middle of identfiers.  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]=='-' ){
126d3479b94Sdrh         /* IMP: R-15891-05542 -- syntax diagram for comments */
127aa756b09Sdrh         for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
128d3479b94Sdrh         *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
12975897234Sdrh         return i;
13075897234Sdrh       }
13175897234Sdrh       *tokenType = TK_MINUS;
13275897234Sdrh       return 1;
13375897234Sdrh     }
13475897234Sdrh     case '(': {
13575897234Sdrh       *tokenType = TK_LP;
13675897234Sdrh       return 1;
13775897234Sdrh     }
13875897234Sdrh     case ')': {
13975897234Sdrh       *tokenType = TK_RP;
14075897234Sdrh       return 1;
14175897234Sdrh     }
14275897234Sdrh     case ';': {
14375897234Sdrh       *tokenType = TK_SEMI;
14475897234Sdrh       return 1;
14575897234Sdrh     }
14675897234Sdrh     case '+': {
14775897234Sdrh       *tokenType = TK_PLUS;
14875897234Sdrh       return 1;
14975897234Sdrh     }
15075897234Sdrh     case '*': {
15175897234Sdrh       *tokenType = TK_STAR;
15275897234Sdrh       return 1;
15375897234Sdrh     }
15475897234Sdrh     case '/': {
15566105a8eSdrh       if( z[1]!='*' || z[2]==0 ){
15675897234Sdrh         *tokenType = TK_SLASH;
15775897234Sdrh         return 1;
15875897234Sdrh       }
159d3479b94Sdrh       /* IMP: R-15891-05542 -- syntax diagram for comments */
160aa756b09Sdrh       for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
161aa756b09Sdrh       if( c ) i++;
162d3479b94Sdrh       *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
16366105a8eSdrh       return i;
16466105a8eSdrh     }
165bf4133cbSdrh     case '%': {
166bf4133cbSdrh       *tokenType = TK_REM;
167bf4133cbSdrh       return 1;
168bf4133cbSdrh     }
16975897234Sdrh     case '=': {
17075897234Sdrh       *tokenType = TK_EQ;
17175897234Sdrh       return 1 + (z[1]=='=');
17275897234Sdrh     }
17375897234Sdrh     case '<': {
174aa756b09Sdrh       if( (c=z[1])=='=' ){
17575897234Sdrh         *tokenType = TK_LE;
17675897234Sdrh         return 2;
177aa756b09Sdrh       }else if( c=='>' ){
17875897234Sdrh         *tokenType = TK_NE;
17975897234Sdrh         return 2;
180aa756b09Sdrh       }else if( c=='<' ){
181bf4133cbSdrh         *tokenType = TK_LSHIFT;
182bf4133cbSdrh         return 2;
18375897234Sdrh       }else{
18475897234Sdrh         *tokenType = TK_LT;
18575897234Sdrh         return 1;
18675897234Sdrh       }
18775897234Sdrh     }
18875897234Sdrh     case '>': {
189aa756b09Sdrh       if( (c=z[1])=='=' ){
19075897234Sdrh         *tokenType = TK_GE;
19175897234Sdrh         return 2;
192aa756b09Sdrh       }else if( c=='>' ){
193bf4133cbSdrh         *tokenType = TK_RSHIFT;
194bf4133cbSdrh         return 2;
19575897234Sdrh       }else{
19675897234Sdrh         *tokenType = TK_GT;
19775897234Sdrh         return 1;
19875897234Sdrh       }
19975897234Sdrh     }
20075897234Sdrh     case '!': {
20175897234Sdrh       if( z[1]!='=' ){
20275897234Sdrh         *tokenType = TK_ILLEGAL;
203c837e709Sdrh         return 2;
20475897234Sdrh       }else{
20575897234Sdrh         *tokenType = TK_NE;
20675897234Sdrh         return 2;
20775897234Sdrh       }
20875897234Sdrh     }
2090040077dSdrh     case '|': {
2100040077dSdrh       if( z[1]!='|' ){
211bf4133cbSdrh         *tokenType = TK_BITOR;
2120040077dSdrh         return 1;
2130040077dSdrh       }else{
2140040077dSdrh         *tokenType = TK_CONCAT;
2150040077dSdrh         return 2;
2160040077dSdrh       }
2170040077dSdrh     }
21875897234Sdrh     case ',': {
21975897234Sdrh       *tokenType = TK_COMMA;
22075897234Sdrh       return 1;
22175897234Sdrh     }
222bf4133cbSdrh     case '&': {
223bf4133cbSdrh       *tokenType = TK_BITAND;
224bf4133cbSdrh       return 1;
225bf4133cbSdrh     }
226bf4133cbSdrh     case '~': {
227bf4133cbSdrh       *tokenType = TK_BITNOT;
228bf4133cbSdrh       return 1;
229bf4133cbSdrh     }
2303d94662aSdrh     case '`':
2313d94662aSdrh     case '\'':
2323d94662aSdrh     case '"': {
23375897234Sdrh       int delim = z[0];
23419f81f6cSdrh       testcase( delim=='`' );
23519f81f6cSdrh       testcase( delim=='\'' );
23619f81f6cSdrh       testcase( delim=='"' );
237aa756b09Sdrh       for(i=1; (c=z[i])!=0; i++){
238aa756b09Sdrh         if( c==delim ){
23975897234Sdrh           if( z[i+1]==delim ){
24075897234Sdrh             i++;
24175897234Sdrh           }else{
24275897234Sdrh             break;
24375897234Sdrh           }
24475897234Sdrh         }
24575897234Sdrh       }
246a33cb5f7Sdrh       if( c=='\'' ){
24775897234Sdrh         *tokenType = TK_STRING;
248eef8b558Sdrh         return i+1;
249a33cb5f7Sdrh       }else if( c!=0 ){
250a33cb5f7Sdrh         *tokenType = TK_ID;
251a33cb5f7Sdrh         return i+1;
252eef8b558Sdrh       }else{
253eef8b558Sdrh         *tokenType = TK_ILLEGAL;
25475897234Sdrh         return i;
25575897234Sdrh       }
256eef8b558Sdrh     }
25775897234Sdrh     case '.': {
2587681618cSdrh #ifndef SQLITE_OMIT_FLOATING_POINT
25978ca0e7eSdanielk1977       if( !sqlite3Isdigit(z[1]) )
2607681618cSdrh #endif
2617681618cSdrh       {
26275897234Sdrh         *tokenType = TK_DOT;
26375897234Sdrh         return 1;
26475897234Sdrh       }
2657681618cSdrh       /* If the next character is a digit, this is a floating point
2667681618cSdrh       ** number that begins with ".".  Fall thru into the next case */
2677681618cSdrh     }
26875897234Sdrh     case '0': case '1': case '2': case '3': case '4':
26975897234Sdrh     case '5': case '6': case '7': case '8': case '9': {
27019f81f6cSdrh       testcase( z[0]=='0' );  testcase( z[0]=='1' );  testcase( z[0]=='2' );
27119f81f6cSdrh       testcase( z[0]=='3' );  testcase( z[0]=='4' );  testcase( z[0]=='5' );
27219f81f6cSdrh       testcase( z[0]=='6' );  testcase( z[0]=='7' );  testcase( z[0]=='8' );
27319f81f6cSdrh       testcase( z[0]=='9' );
274c837e709Sdrh       *tokenType = TK_INTEGER;
27578ca0e7eSdanielk1977       for(i=0; sqlite3Isdigit(z[i]); i++){}
276b7f9164eSdrh #ifndef SQLITE_OMIT_FLOATING_POINT
2777681618cSdrh       if( z[i]=='.' ){
2787681618cSdrh         i++;
27978ca0e7eSdanielk1977         while( sqlite3Isdigit(z[i]) ){ i++; }
280c837e709Sdrh         *tokenType = TK_FLOAT;
281c837e709Sdrh       }
28275897234Sdrh       if( (z[i]=='e' || z[i]=='E') &&
28378ca0e7eSdanielk1977            ( sqlite3Isdigit(z[i+1])
28478ca0e7eSdanielk1977             || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
28575897234Sdrh            )
28675897234Sdrh       ){
28775897234Sdrh         i += 2;
28878ca0e7eSdanielk1977         while( sqlite3Isdigit(z[i]) ){ i++; }
28975897234Sdrh         *tokenType = TK_FLOAT;
29075897234Sdrh       }
291b7f9164eSdrh #endif
29267dd9011Sdrh       while( IdChar(z[i]) ){
29367dd9011Sdrh         *tokenType = TK_ILLEGAL;
29467dd9011Sdrh         i++;
29567dd9011Sdrh       }
29675897234Sdrh       return i;
29775897234Sdrh     }
2982f4392ffSdrh     case '[': {
299aa756b09Sdrh       for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
3004b2f9368Sdrh       *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
3012f4392ffSdrh       return i;
3022f4392ffSdrh     }
3037c972decSdrh     case '?': {
30450457896Sdrh       *tokenType = TK_VARIABLE;
30578ca0e7eSdanielk1977       for(i=1; sqlite3Isdigit(z[i]); i++){}
306fa6bc000Sdrh       return i;
30750457896Sdrh     }
308288d37f1Sdrh     case '#': {
30978ca0e7eSdanielk1977       for(i=1; sqlite3Isdigit(z[i]); i++){}
310288d37f1Sdrh       if( i>1 ){
311288d37f1Sdrh         /* Parameters of the form #NNN (where NNN is a number) are used
312288d37f1Sdrh         ** internally by sqlite3NestedParse.  */
313288d37f1Sdrh         *tokenType = TK_REGISTER;
314895d7472Sdrh         return i;
315895d7472Sdrh       }
316288d37f1Sdrh       /* Fall through into the next case if the '#' is not followed by
317288d37f1Sdrh       ** a digit. Try to match #AAAA where AAAA is a parameter name. */
318288d37f1Sdrh     }
319b7f9164eSdrh #ifndef SQLITE_OMIT_TCL_VARIABLE
320288d37f1Sdrh     case '$':
321288d37f1Sdrh #endif
3220b2a5eeaSdrh     case '@':  /* For compatibility with MS SQL Server */
323288d37f1Sdrh     case ':': {
324895d7472Sdrh       int n = 0;
3258be4f639Sdrh       testcase( z[0]=='$' );  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;
3564b2f9368Sdrh         for(i=2; (c=z[i])!=0 && c!='\''; i++){
35778ca0e7eSdanielk1977           if( !sqlite3Isxdigit(c) ){
3583fd0a736Sdanielk1977             *tokenType = TK_ILLEGAL;
3593fd0a736Sdanielk1977           }
360c572ef7fSdanielk1977         }
3614b2f9368Sdrh         if( i%2 || !c ) *tokenType = TK_ILLEGAL;
362aa756b09Sdrh         if( c ) i++;
363c572ef7fSdanielk1977         return i;
364c572ef7fSdanielk1977       }
365c572ef7fSdanielk1977       /* Otherwise fall through to the next case */
366c572ef7fSdanielk1977     }
367b7f9164eSdrh #endif
36898808babSdrh     default: {
3699a087a99Sdrh       if( !IdChar(*z) ){
37098808babSdrh         break;
37198808babSdrh       }
372aa756b09Sdrh       for(i=1; IdChar(z[i]); i++){}
373c60e9b82Sdanielk1977       *tokenType = keywordCode((char*)z, i);
37475897234Sdrh       return i;
37575897234Sdrh     }
37675897234Sdrh   }
37775897234Sdrh   *tokenType = TK_ILLEGAL;
37875897234Sdrh   return 1;
37975897234Sdrh }
38075897234Sdrh 
38175897234Sdrh /*
38275897234Sdrh ** Run the parser on the given SQL string.  The parser structure is
383b19a2bc6Sdrh ** passed in.  An SQLITE_ status code is returned.  If an error occurs
384fb45d8c5Sdrh ** then an and attempt is made to write an error message into
385fb45d8c5Sdrh ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
386fb45d8c5Sdrh ** error message.
38775897234Sdrh */
3884adee20fSdanielk1977 int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
389d9da78a2Sdrh   int nErr = 0;                   /* Number of errors encountered */
390d9da78a2Sdrh   int i;                          /* Loop counter */
391d9da78a2Sdrh   void *pEngine;                  /* The LEMON-generated LALR(1) parser */
392d9da78a2Sdrh   int tokenType;                  /* type of the next token */
393d9da78a2Sdrh   int lastTokenParsed = -1;       /* type of the previous token */
394b08a67a7Sshane   u8 enableLookaside;             /* Saved value of db->lookaside.bEnabled */
395d9da78a2Sdrh   sqlite3 *db = pParse->db;       /* The database connection */
396d9da78a2Sdrh   int mxSqlLen;                   /* Max length of an SQL string */
39775897234Sdrh 
398d9da78a2Sdrh 
399d9da78a2Sdrh   mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
40015ca1df1Sdrh   if( db->activeVdbeCnt==0 ){
401881feaa0Sdrh     db->u1.isInterrupted = 0;
40215ca1df1Sdrh   }
4034c504391Sdrh   pParse->rc = SQLITE_OK;
404e7f3f3eeSdrh   pParse->zTail = zSql;
40575897234Sdrh   i = 0;
406fb45d8c5Sdrh   assert( pzErrMsg!=0 );
407e5ae5735Sdrh   pEngine = sqlite3ParserAlloc((void*(*)(size_t))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 );
415fa6bc000Sdrh   assert( pParse->nVarExpr==0 );
416fa6bc000Sdrh   assert( pParse->nVarExprAlloc==0 );
417fa6bc000Sdrh   assert( pParse->apVarExpr==0 );
418d9da78a2Sdrh   enableLookaside = db->lookaside.bEnabled;
419d9da78a2Sdrh   if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
42017435752Sdrh   while( !db->mallocFailed && zSql[i]!=0 ){
42132eb7b47Sdrh     assert( i>=0 );
422b7916a78Sdrh     pParse->sLastToken.z = &zSql[i];
42335b5a33eSdrh     pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
42475897234Sdrh     i += pParse->sLastToken.n;
425bb4957f8Sdrh     if( i>mxSqlLen ){
426e5c941b8Sdrh       pParse->rc = SQLITE_TOOBIG;
427e5c941b8Sdrh       break;
428e5c941b8Sdrh     }
42975897234Sdrh     switch( tokenType ){
430200a81dcSdrh       case TK_SPACE: {
431881feaa0Sdrh         if( db->u1.isInterrupted ){
43219f81f6cSdrh           sqlite3ErrorMsg(pParse, "interrupt");
43332eb7b47Sdrh           pParse->rc = SQLITE_INTERRUPT;
43432eb7b47Sdrh           goto abort_parse;
43532eb7b47Sdrh         }
43675897234Sdrh         break;
43775897234Sdrh       }
43832eb7b47Sdrh       case TK_ILLEGAL: {
439633e6d57Sdrh         sqlite3DbFree(db, *pzErrMsg);
4401e536953Sdanielk1977         *pzErrMsg = sqlite3MPrintf(db, "unrecognized token: \"%T\"",
441ae29ffbeSdrh                         &pParse->sLastToken);
44275897234Sdrh         nErr++;
443caec2f12Sdrh         goto abort_parse;
44432eb7b47Sdrh       }
445326dce74Sdrh       case TK_SEMI: {
446326dce74Sdrh         pParse->zTail = &zSql[i];
447326dce74Sdrh         /* Fall thru into the default case */
448326dce74Sdrh       }
44932eb7b47Sdrh       default: {
4504adee20fSdanielk1977         sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
451b86ccfb2Sdrh         lastTokenParsed = tokenType;
452b86ccfb2Sdrh         if( pParse->rc!=SQLITE_OK ){
45332eb7b47Sdrh           goto abort_parse;
45475897234Sdrh         }
45575897234Sdrh         break;
45675897234Sdrh       }
45775897234Sdrh     }
45832eb7b47Sdrh   }
45932eb7b47Sdrh abort_parse:
460b86ccfb2Sdrh   if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
461b86ccfb2Sdrh     if( lastTokenParsed!=TK_SEMI ){
4624adee20fSdanielk1977       sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
463326dce74Sdrh       pParse->zTail = &zSql[i];
46475897234Sdrh     }
4654adee20fSdanielk1977     sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
46675897234Sdrh   }
467ec424a5bSdrh #ifdef YYTRACKMAXSTACKDEPTH
468ec424a5bSdrh   sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK,
469ec424a5bSdrh       sqlite3ParserStackPeak(pEngine)
470ec424a5bSdrh   );
471ec424a5bSdrh #endif /* YYDEBUG */
47217435752Sdrh   sqlite3ParserFree(pEngine, sqlite3_free);
473d9da78a2Sdrh   db->lookaside.bEnabled = enableLookaside;
47417435752Sdrh   if( db->mallocFailed ){
47571c697efSdrh     pParse->rc = SQLITE_NOMEM;
47671c697efSdrh   }
477b86ccfb2Sdrh   if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
478f089aa45Sdrh     sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc));
479b86ccfb2Sdrh   }
48019f81f6cSdrh   assert( pzErrMsg!=0 );
48175897234Sdrh   if( pParse->zErrMsg ){
48275897234Sdrh     *pzErrMsg = pParse->zErrMsg;
483*413c3d36Sdrh     sqlite3_log(pParse->rc, "%s", *pzErrMsg);
484b86ccfb2Sdrh     pParse->zErrMsg = 0;
4854b2f9368Sdrh     nErr++;
48675897234Sdrh   }
4872958a4e6Sdrh   if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
4884adee20fSdanielk1977     sqlite3VdbeDelete(pParse->pVdbe);
48975897234Sdrh     pParse->pVdbe = 0;
49075897234Sdrh   }
491c00da105Sdanielk1977 #ifndef SQLITE_OMIT_SHARED_CACHE
492c00da105Sdanielk1977   if( pParse->nested==0 ){
493633e6d57Sdrh     sqlite3DbFree(db, pParse->aTableLock);
494c00da105Sdanielk1977     pParse->aTableLock = 0;
495c00da105Sdanielk1977     pParse->nTableLock = 0;
496c00da105Sdanielk1977   }
497c00da105Sdanielk1977 #endif
4984f3dd150Sdrh #ifndef SQLITE_OMIT_VIRTUALTABLE
499633e6d57Sdrh   sqlite3DbFree(db, pParse->apVtabLock);
5004f3dd150Sdrh #endif
5017e6ebfb2Sdanielk1977 
5027e6ebfb2Sdanielk1977   if( !IN_DECLARE_VTAB ){
5037e6ebfb2Sdanielk1977     /* If the pParse->declareVtab flag is set, do not delete any table
5047e6ebfb2Sdanielk1977     ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
5057e6ebfb2Sdanielk1977     ** will take responsibility for freeing the Table structure.
5067e6ebfb2Sdanielk1977     */
507a04a34ffSdanielk1977     sqlite3DeleteTable(pParse->pNewTable);
5087e6ebfb2Sdanielk1977   }
5097e6ebfb2Sdanielk1977 
510633e6d57Sdrh   sqlite3DeleteTrigger(db, pParse->pNewTrigger);
511633e6d57Sdrh   sqlite3DbFree(db, pParse->apVarExpr);
5128b213899Sdrh   sqlite3DbFree(db, pParse->aAlias);
5130b9f50d8Sdrh   while( pParse->pAinc ){
5140b9f50d8Sdrh     AutoincInfo *p = pParse->pAinc;
5150b9f50d8Sdrh     pParse->pAinc = p->pNext;
5160b9f50d8Sdrh     sqlite3DbFree(db, p);
5170b9f50d8Sdrh   }
518588a9a1aSdrh   while( pParse->pZombieTab ){
519588a9a1aSdrh     Table *p = pParse->pZombieTab;
520588a9a1aSdrh     pParse->pZombieTab = p->pNextZombie;
521588a9a1aSdrh     sqlite3DeleteTable(p);
522588a9a1aSdrh   }
523b5092d18Sdrh   if( nErr>0 && pParse->rc==SQLITE_OK ){
5244c504391Sdrh     pParse->rc = SQLITE_ERROR;
5254c504391Sdrh   }
52675897234Sdrh   return nErr;
52775897234Sdrh }
528