xref: /sqlite-3.40.0/src/tokenize.c (revision 23a4d14e)
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 **
18*23a4d14eSdrh ** $Id: tokenize.c,v 1.125 2007/01/26 19:31:01 drh Exp $
1975897234Sdrh */
2075897234Sdrh #include "sqliteInt.h"
21ad75e987Sdrh #include "os.h"
2275897234Sdrh #include <ctype.h>
23dcc581ccSdrh #include <stdlib.h>
2475897234Sdrh 
2575897234Sdrh /*
269b8f447bSdrh ** The charMap() macro maps alphabetic characters into their
279b8f447bSdrh ** lower-case ASCII equivalent.  On ASCII machines, this is just
289b8f447bSdrh ** an upper-to-lower case map.  On EBCDIC machines we also need
299b8f447bSdrh ** to adjust the encoding.  Only alphabetic characters and underscores
309b8f447bSdrh ** need to be translated.
319b8f447bSdrh */
329b8f447bSdrh #ifdef SQLITE_ASCII
339b8f447bSdrh # define charMap(X) sqlite3UpperToLower[(unsigned char)X]
349b8f447bSdrh #endif
359b8f447bSdrh #ifdef SQLITE_EBCDIC
369b8f447bSdrh # define charMap(X) ebcdicToAscii[(unsigned char)X]
379b8f447bSdrh const unsigned char ebcdicToAscii[] = {
389b8f447bSdrh /* 0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F */
399b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 0x */
409b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 1x */
419b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 2x */
429b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 3x */
439b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 4x */
449b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 5x */
459b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 95,  0,  0,  /* 6x */
469b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 7x */
479b8f447bSdrh    0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* 8x */
489b8f447bSdrh    0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* 9x */
499b8f447bSdrh    0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ax */
509b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Bx */
519b8f447bSdrh    0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* Cx */
529b8f447bSdrh    0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* Dx */
539b8f447bSdrh    0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ex */
549b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Fx */
559b8f447bSdrh };
569b8f447bSdrh #endif
579b8f447bSdrh 
589b8f447bSdrh /*
5952fb6d71Sdrh ** The sqlite3KeywordCode function looks up an identifier to determine if
6052fb6d71Sdrh ** it is a keyword.  If it is a keyword, the token code of that keyword is
6175897234Sdrh ** returned.  If the input is not a keyword, TK_ID is returned.
622090a0edSdrh **
632090a0edSdrh ** The implementation of this routine was generated by a program,
6473b211abSdrh ** mkkeywordhash.h, located in the tool subdirectory of the distribution.
6552fb6d71Sdrh ** The output of the mkkeywordhash.c program is written into a file
6673b211abSdrh ** named keywordhash.h and then included into this source file by
6752fb6d71Sdrh ** the #include below.
6875897234Sdrh */
6973b211abSdrh #include "keywordhash.h"
7075897234Sdrh 
7140f20f7dSdrh 
7298808babSdrh /*
739b8f447bSdrh ** If X is a character that can be used in an identifier then
749b8f447bSdrh ** IdChar(X) will be true.  Otherwise it is false.
7598808babSdrh **
769b8f447bSdrh ** For ASCII, any character with the high-order bit set is
779b8f447bSdrh ** allowed in an identifier.  For 7-bit characters,
789b8f447bSdrh ** sqlite3IsIdChar[X] must be 1.
799b8f447bSdrh **
809b8f447bSdrh ** For EBCDIC, the rules are more complex but have the same
819b8f447bSdrh ** end result.
82a0d1f663Sdrh **
83a0d1f663Sdrh ** Ticket #1066.  the SQL standard does not allow '$' in the
84a0d1f663Sdrh ** middle of identfiers.  But many SQL implementations do.
85a0d1f663Sdrh ** SQLite will allow '$' in identifiers for compatibility.
86a0d1f663Sdrh ** But the feature is undocumented.
8798808babSdrh */
889b8f447bSdrh #ifdef SQLITE_ASCII
89a2b902d6Sdrh const char sqlite3IsIdChar[] = {
9098808babSdrh /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
91a0d1f663Sdrh     0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 2x */
9298808babSdrh     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
9398808babSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
9498808babSdrh     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
9598808babSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
9698808babSdrh     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
9798808babSdrh };
98a2b902d6Sdrh #define IdChar(C)  (((c=C)&0x80)!=0 || (c>0x1f && sqlite3IsIdChar[c-0x20]))
999b8f447bSdrh #endif
1009b8f447bSdrh #ifdef SQLITE_EBCDIC
1019b8f447bSdrh const char sqlite3IsIdChar[] = {
1029b8f447bSdrh /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
1039b8f447bSdrh     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 4x */
1049b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0,  /* 5x */
1059b8f447bSdrh     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,  /* 6x */
1069b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,  /* 7x */
1079b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0,  /* 8x */
1089b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0,  /* 9x */
1099b8f447bSdrh     1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0,  /* Ax */
1109b8f447bSdrh     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* Bx */
1119b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Cx */
1129b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Dx */
1139b8f447bSdrh     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Ex */
1149b8f447bSdrh     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0,  /* Fx */
1159b8f447bSdrh };
1169b8f447bSdrh #define IdChar(C)  (((c=C)>=0x42 && sqlite3IsIdChar[c-0x40]))
1179b8f447bSdrh #endif
1189b8f447bSdrh 
11998808babSdrh 
12075897234Sdrh /*
12161b487d0Sdrh ** Return the length of the token that begins at z[0].
12261b487d0Sdrh ** Store the token type in *tokenType before returning.
12375897234Sdrh */
124c60e9b82Sdanielk1977 static int getToken(const unsigned char *z, int *tokenType){
125aa756b09Sdrh   int i, c;
12675897234Sdrh   switch( *z ){
12730cab809Sdrh     case ' ': case '\t': case '\n': case '\f': case '\r': {
12832eb7b47Sdrh       for(i=1; isspace(z[i]); i++){}
12975897234Sdrh       *tokenType = TK_SPACE;
13075897234Sdrh       return i;
13175897234Sdrh     }
13275897234Sdrh     case '-': {
13375897234Sdrh       if( z[1]=='-' ){
134aa756b09Sdrh         for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
13575897234Sdrh         *tokenType = TK_COMMENT;
13675897234Sdrh         return i;
13775897234Sdrh       }
13875897234Sdrh       *tokenType = TK_MINUS;
13975897234Sdrh       return 1;
14075897234Sdrh     }
14175897234Sdrh     case '(': {
14275897234Sdrh       *tokenType = TK_LP;
14375897234Sdrh       return 1;
14475897234Sdrh     }
14575897234Sdrh     case ')': {
14675897234Sdrh       *tokenType = TK_RP;
14775897234Sdrh       return 1;
14875897234Sdrh     }
14975897234Sdrh     case ';': {
15075897234Sdrh       *tokenType = TK_SEMI;
15175897234Sdrh       return 1;
15275897234Sdrh     }
15375897234Sdrh     case '+': {
15475897234Sdrh       *tokenType = TK_PLUS;
15575897234Sdrh       return 1;
15675897234Sdrh     }
15775897234Sdrh     case '*': {
15875897234Sdrh       *tokenType = TK_STAR;
15975897234Sdrh       return 1;
16075897234Sdrh     }
16175897234Sdrh     case '/': {
16266105a8eSdrh       if( z[1]!='*' || z[2]==0 ){
16375897234Sdrh         *tokenType = TK_SLASH;
16475897234Sdrh         return 1;
16575897234Sdrh       }
166aa756b09Sdrh       for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
167aa756b09Sdrh       if( c ) i++;
16866105a8eSdrh       *tokenType = TK_COMMENT;
16966105a8eSdrh       return i;
17066105a8eSdrh     }
171bf4133cbSdrh     case '%': {
172bf4133cbSdrh       *tokenType = TK_REM;
173bf4133cbSdrh       return 1;
174bf4133cbSdrh     }
17575897234Sdrh     case '=': {
17675897234Sdrh       *tokenType = TK_EQ;
17775897234Sdrh       return 1 + (z[1]=='=');
17875897234Sdrh     }
17975897234Sdrh     case '<': {
180aa756b09Sdrh       if( (c=z[1])=='=' ){
18175897234Sdrh         *tokenType = TK_LE;
18275897234Sdrh         return 2;
183aa756b09Sdrh       }else if( c=='>' ){
18475897234Sdrh         *tokenType = TK_NE;
18575897234Sdrh         return 2;
186aa756b09Sdrh       }else if( c=='<' ){
187bf4133cbSdrh         *tokenType = TK_LSHIFT;
188bf4133cbSdrh         return 2;
18975897234Sdrh       }else{
19075897234Sdrh         *tokenType = TK_LT;
19175897234Sdrh         return 1;
19275897234Sdrh       }
19375897234Sdrh     }
19475897234Sdrh     case '>': {
195aa756b09Sdrh       if( (c=z[1])=='=' ){
19675897234Sdrh         *tokenType = TK_GE;
19775897234Sdrh         return 2;
198aa756b09Sdrh       }else if( c=='>' ){
199bf4133cbSdrh         *tokenType = TK_RSHIFT;
200bf4133cbSdrh         return 2;
20175897234Sdrh       }else{
20275897234Sdrh         *tokenType = TK_GT;
20375897234Sdrh         return 1;
20475897234Sdrh       }
20575897234Sdrh     }
20675897234Sdrh     case '!': {
20775897234Sdrh       if( z[1]!='=' ){
20875897234Sdrh         *tokenType = TK_ILLEGAL;
209c837e709Sdrh         return 2;
21075897234Sdrh       }else{
21175897234Sdrh         *tokenType = TK_NE;
21275897234Sdrh         return 2;
21375897234Sdrh       }
21475897234Sdrh     }
2150040077dSdrh     case '|': {
2160040077dSdrh       if( z[1]!='|' ){
217bf4133cbSdrh         *tokenType = TK_BITOR;
2180040077dSdrh         return 1;
2190040077dSdrh       }else{
2200040077dSdrh         *tokenType = TK_CONCAT;
2210040077dSdrh         return 2;
2220040077dSdrh       }
2230040077dSdrh     }
22475897234Sdrh     case ',': {
22575897234Sdrh       *tokenType = TK_COMMA;
22675897234Sdrh       return 1;
22775897234Sdrh     }
228bf4133cbSdrh     case '&': {
229bf4133cbSdrh       *tokenType = TK_BITAND;
230bf4133cbSdrh       return 1;
231bf4133cbSdrh     }
232bf4133cbSdrh     case '~': {
233bf4133cbSdrh       *tokenType = TK_BITNOT;
234bf4133cbSdrh       return 1;
235bf4133cbSdrh     }
2363d94662aSdrh     case '`':
2373d94662aSdrh     case '\'':
2383d94662aSdrh     case '"': {
23975897234Sdrh       int delim = z[0];
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       }
249eef8b558Sdrh       if( c ){
25075897234Sdrh         *tokenType = TK_STRING;
251eef8b558Sdrh         return i+1;
252eef8b558Sdrh       }else{
253eef8b558Sdrh         *tokenType = TK_ILLEGAL;
25475897234Sdrh         return i;
25575897234Sdrh       }
256eef8b558Sdrh     }
25775897234Sdrh     case '.': {
2587681618cSdrh #ifndef SQLITE_OMIT_FLOATING_POINT
2597681618cSdrh       if( !isdigit(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': {
270c837e709Sdrh       *tokenType = TK_INTEGER;
2717681618cSdrh       for(i=0; isdigit(z[i]); i++){}
272b7f9164eSdrh #ifndef SQLITE_OMIT_FLOATING_POINT
2737681618cSdrh       if( z[i]=='.' ){
2747681618cSdrh         i++;
27532eb7b47Sdrh         while( isdigit(z[i]) ){ i++; }
276c837e709Sdrh         *tokenType = TK_FLOAT;
277c837e709Sdrh       }
27875897234Sdrh       if( (z[i]=='e' || z[i]=='E') &&
27975897234Sdrh            ( isdigit(z[i+1])
28075897234Sdrh             || ((z[i+1]=='+' || z[i+1]=='-') && isdigit(z[i+2]))
28175897234Sdrh            )
28275897234Sdrh       ){
28375897234Sdrh         i += 2;
28432eb7b47Sdrh         while( isdigit(z[i]) ){ i++; }
28575897234Sdrh         *tokenType = TK_FLOAT;
28675897234Sdrh       }
287b7f9164eSdrh #endif
28867dd9011Sdrh       while( IdChar(z[i]) ){
28967dd9011Sdrh         *tokenType = TK_ILLEGAL;
29067dd9011Sdrh         i++;
29167dd9011Sdrh       }
29275897234Sdrh       return i;
29375897234Sdrh     }
2942f4392ffSdrh     case '[': {
295aa756b09Sdrh       for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
2962f4392ffSdrh       *tokenType = TK_ID;
2972f4392ffSdrh       return i;
2982f4392ffSdrh     }
2997c972decSdrh     case '?': {
30050457896Sdrh       *tokenType = TK_VARIABLE;
301fa6bc000Sdrh       for(i=1; isdigit(z[i]); i++){}
302fa6bc000Sdrh       return i;
30350457896Sdrh     }
304288d37f1Sdrh     case '#': {
305288d37f1Sdrh       for(i=1; isdigit(z[i]); i++){}
306288d37f1Sdrh       if( i>1 ){
307288d37f1Sdrh         /* Parameters of the form #NNN (where NNN is a number) are used
308288d37f1Sdrh         ** internally by sqlite3NestedParse.  */
309288d37f1Sdrh         *tokenType = TK_REGISTER;
310895d7472Sdrh         return i;
311895d7472Sdrh       }
312288d37f1Sdrh       /* Fall through into the next case if the '#' is not followed by
313288d37f1Sdrh       ** a digit. Try to match #AAAA where AAAA is a parameter name. */
314288d37f1Sdrh     }
315b7f9164eSdrh #ifndef SQLITE_OMIT_TCL_VARIABLE
316288d37f1Sdrh     case '$':
317288d37f1Sdrh #endif
3180b2a5eeaSdrh     case '@':  /* For compatibility with MS SQL Server */
319288d37f1Sdrh     case ':': {
320895d7472Sdrh       int n = 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++;
329895d7472Sdrh           }while( (c=z[i])!=0 && !isspace(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': {
348aa756b09Sdrh       if( (c=z[1])=='\'' || c=='"' ){
349aa756b09Sdrh         int delim = c;
3503fd0a736Sdanielk1977         *tokenType = TK_BLOB;
351aa756b09Sdrh         for(i=2; (c=z[i])!=0; i++){
352aa756b09Sdrh           if( c==delim ){
3533fd0a736Sdanielk1977             if( i%2 ) *tokenType = TK_ILLEGAL;
354c572ef7fSdanielk1977             break;
355c572ef7fSdanielk1977           }
356aa756b09Sdrh           if( !isxdigit(c) ){
3573fd0a736Sdanielk1977             *tokenType = TK_ILLEGAL;
3583fd0a736Sdanielk1977             return i;
3593fd0a736Sdanielk1977           }
360c572ef7fSdanielk1977         }
361aa756b09Sdrh         if( c ) i++;
362c572ef7fSdanielk1977         return i;
363c572ef7fSdanielk1977       }
364c572ef7fSdanielk1977       /* Otherwise fall through to the next case */
365c572ef7fSdanielk1977     }
366b7f9164eSdrh #endif
36798808babSdrh     default: {
368aa756b09Sdrh       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 }
379c60e9b82Sdanielk1977 int sqlite3GetToken(const unsigned char *z, int *tokenType){
380c60e9b82Sdanielk1977   return getToken(z, tokenType);
381c60e9b82Sdanielk1977 }
38275897234Sdrh 
38375897234Sdrh /*
38475897234Sdrh ** Run the parser on the given SQL string.  The parser structure is
385b19a2bc6Sdrh ** passed in.  An SQLITE_ status code is returned.  If an error occurs
386b19a2bc6Sdrh ** and pzErrMsg!=NULL then an error message might be written into
387b19a2bc6Sdrh ** memory obtained from malloc() and *pzErrMsg made to point to that
388b19a2bc6Sdrh ** error message.  Or maybe not.
38975897234Sdrh */
3904adee20fSdanielk1977 int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
39175897234Sdrh   int nErr = 0;
39275897234Sdrh   int i;
39375897234Sdrh   void *pEngine;
394b86ccfb2Sdrh   int tokenType;
395b86ccfb2Sdrh   int lastTokenParsed = -1;
3969bb575fdSdrh   sqlite3 *db = pParse->db;
397*23a4d14eSdrh   extern void *sqlite3ParserAlloc(void*(*)(size_t));
3984adee20fSdanielk1977   extern void sqlite3ParserFree(void*, void(*)(void*));
399*23a4d14eSdrh   extern void sqlite3Parser(void*, int, Token, Parse*);
40075897234Sdrh 
40115ca1df1Sdrh   if( db->activeVdbeCnt==0 ){
402881feaa0Sdrh     db->u1.isInterrupted = 0;
40315ca1df1Sdrh   }
4044c504391Sdrh   pParse->rc = SQLITE_OK;
40575897234Sdrh   i = 0;
406*23a4d14eSdrh   pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3MallocX);
40775897234Sdrh   if( pEngine==0 ){
408defc9972Sdrh     return SQLITE_NOMEM;
40975897234Sdrh   }
410fa6bc000Sdrh   assert( pParse->sLastToken.dyn==0 );
411fa6bc000Sdrh   assert( pParse->pNewTable==0 );
412fa6bc000Sdrh   assert( pParse->pNewTrigger==0 );
413fa6bc000Sdrh   assert( pParse->nVar==0 );
414fa6bc000Sdrh   assert( pParse->nVarExpr==0 );
415fa6bc000Sdrh   assert( pParse->nVarExprAlloc==0 );
416fa6bc000Sdrh   assert( pParse->apVarExpr==0 );
4173f7d4e49Sdrh   pParse->zTail = pParse->zSql = zSql;
4189e12800dSdanielk1977   while( !sqlite3MallocFailed() && zSql[i]!=0 ){
41932eb7b47Sdrh     assert( i>=0 );
4202646da7eSdrh     pParse->sLastToken.z = (u8*)&zSql[i];
42132eb7b47Sdrh     assert( pParse->sLastToken.dyn==0 );
422c60e9b82Sdanielk1977     pParse->sLastToken.n = getToken((unsigned char*)&zSql[i],&tokenType);
42375897234Sdrh     i += pParse->sLastToken.n;
42475897234Sdrh     switch( tokenType ){
42575897234Sdrh       case TK_SPACE:
42675897234Sdrh       case TK_COMMENT: {
427881feaa0Sdrh         if( db->u1.isInterrupted ){
42832eb7b47Sdrh           pParse->rc = SQLITE_INTERRUPT;
4294adee20fSdanielk1977           sqlite3SetString(pzErrMsg, "interrupt", (char*)0);
43032eb7b47Sdrh           goto abort_parse;
43132eb7b47Sdrh         }
43275897234Sdrh         break;
43375897234Sdrh       }
43432eb7b47Sdrh       case TK_ILLEGAL: {
435ae29ffbeSdrh         if( pzErrMsg ){
436ae29ffbeSdrh           sqliteFree(*pzErrMsg);
437ae29ffbeSdrh           *pzErrMsg = sqlite3MPrintf("unrecognized token: \"%T\"",
438ae29ffbeSdrh                           &pParse->sLastToken);
439ae29ffbeSdrh         }
44075897234Sdrh         nErr++;
441caec2f12Sdrh         goto abort_parse;
44232eb7b47Sdrh       }
443326dce74Sdrh       case TK_SEMI: {
444326dce74Sdrh         pParse->zTail = &zSql[i];
445326dce74Sdrh         /* Fall thru into the default case */
446326dce74Sdrh       }
44732eb7b47Sdrh       default: {
4484adee20fSdanielk1977         sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
449b86ccfb2Sdrh         lastTokenParsed = tokenType;
450b86ccfb2Sdrh         if( pParse->rc!=SQLITE_OK ){
45132eb7b47Sdrh           goto abort_parse;
45275897234Sdrh         }
45375897234Sdrh         break;
45475897234Sdrh       }
45575897234Sdrh     }
45632eb7b47Sdrh   }
45732eb7b47Sdrh abort_parse:
458b86ccfb2Sdrh   if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
459b86ccfb2Sdrh     if( lastTokenParsed!=TK_SEMI ){
4604adee20fSdanielk1977       sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
461326dce74Sdrh       pParse->zTail = &zSql[i];
46275897234Sdrh     }
4634adee20fSdanielk1977     sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
46475897234Sdrh   }
465132d8d6aSdrh   sqlite3ParserFree(pEngine, sqlite3FreeX);
4669e12800dSdanielk1977   if( sqlite3MallocFailed() ){
46771c697efSdrh     pParse->rc = SQLITE_NOMEM;
46871c697efSdrh   }
469b86ccfb2Sdrh   if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
470261919ccSdanielk1977     sqlite3SetString(&pParse->zErrMsg, sqlite3ErrStr(pParse->rc), (char*)0);
471b86ccfb2Sdrh   }
47275897234Sdrh   if( pParse->zErrMsg ){
473b86ccfb2Sdrh     if( pzErrMsg && *pzErrMsg==0 ){
47475897234Sdrh       *pzErrMsg = pParse->zErrMsg;
47575897234Sdrh     }else{
47675897234Sdrh       sqliteFree(pParse->zErrMsg);
47775897234Sdrh     }
478b86ccfb2Sdrh     pParse->zErrMsg = 0;
47975897234Sdrh     if( !nErr ) nErr++;
48075897234Sdrh   }
4812958a4e6Sdrh   if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
4824adee20fSdanielk1977     sqlite3VdbeDelete(pParse->pVdbe);
48375897234Sdrh     pParse->pVdbe = 0;
48475897234Sdrh   }
485c00da105Sdanielk1977 #ifndef SQLITE_OMIT_SHARED_CACHE
486c00da105Sdanielk1977   if( pParse->nested==0 ){
487c00da105Sdanielk1977     sqliteFree(pParse->aTableLock);
488c00da105Sdanielk1977     pParse->aTableLock = 0;
489c00da105Sdanielk1977     pParse->nTableLock = 0;
490c00da105Sdanielk1977   }
491c00da105Sdanielk1977 #endif
4927e6ebfb2Sdanielk1977 
4937e6ebfb2Sdanielk1977   if( !IN_DECLARE_VTAB ){
4947e6ebfb2Sdanielk1977     /* If the pParse->declareVtab flag is set, do not delete any table
4957e6ebfb2Sdanielk1977     ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
4967e6ebfb2Sdanielk1977     ** will take responsibility for freeing the Table structure.
4977e6ebfb2Sdanielk1977     */
4984adee20fSdanielk1977     sqlite3DeleteTable(pParse->db, pParse->pNewTable);
4997e6ebfb2Sdanielk1977   }
5007e6ebfb2Sdanielk1977 
5014adee20fSdanielk1977   sqlite3DeleteTrigger(pParse->pNewTrigger);
502fa6bc000Sdrh   sqliteFree(pParse->apVarExpr);
503b86ccfb2Sdrh   if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){
5044c504391Sdrh     pParse->rc = SQLITE_ERROR;
5054c504391Sdrh   }
50675897234Sdrh   return nErr;
50775897234Sdrh }
508