xref: /sqlite-3.40.0/src/tokenize.c (revision b5092d18)
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*b5092d18Sdrh ** $Id: tokenize.c,v 1.161 2009/06/17 01:17:13 drh Exp $
1975897234Sdrh */
2075897234Sdrh #include "sqliteInt.h"
21dcc581ccSdrh #include <stdlib.h>
2275897234Sdrh 
2375897234Sdrh /*
249b8f447bSdrh ** The charMap() macro maps alphabetic characters into their
259b8f447bSdrh ** lower-case ASCII equivalent.  On ASCII machines, this is just
269b8f447bSdrh ** an upper-to-lower case map.  On EBCDIC machines we also need
279b8f447bSdrh ** to adjust the encoding.  Only alphabetic characters and underscores
289b8f447bSdrh ** need to be translated.
299b8f447bSdrh */
309b8f447bSdrh #ifdef SQLITE_ASCII
319b8f447bSdrh # define charMap(X) sqlite3UpperToLower[(unsigned char)X]
329b8f447bSdrh #endif
339b8f447bSdrh #ifdef SQLITE_EBCDIC
349b8f447bSdrh # define charMap(X) ebcdicToAscii[(unsigned char)X]
359b8f447bSdrh const unsigned char ebcdicToAscii[] = {
369b8f447bSdrh /* 0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F */
379b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 0x */
389b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 1x */
399b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 2x */
409b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 3x */
419b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 4x */
429b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 5x */
439b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 95,  0,  0,  /* 6x */
449b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 7x */
459b8f447bSdrh    0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* 8x */
469b8f447bSdrh    0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* 9x */
479b8f447bSdrh    0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ax */
489b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Bx */
499b8f447bSdrh    0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* Cx */
509b8f447bSdrh    0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* Dx */
519b8f447bSdrh    0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ex */
529b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Fx */
539b8f447bSdrh };
549b8f447bSdrh #endif
559b8f447bSdrh 
569b8f447bSdrh /*
5752fb6d71Sdrh ** The sqlite3KeywordCode function looks up an identifier to determine if
5852fb6d71Sdrh ** it is a keyword.  If it is a keyword, the token code of that keyword is
5975897234Sdrh ** returned.  If the input is not a keyword, TK_ID is returned.
602090a0edSdrh **
612090a0edSdrh ** The implementation of this routine was generated by a program,
6273b211abSdrh ** mkkeywordhash.h, located in the tool subdirectory of the distribution.
6352fb6d71Sdrh ** The output of the mkkeywordhash.c program is written into a file
6473b211abSdrh ** named keywordhash.h and then included into this source file by
6552fb6d71Sdrh ** the #include below.
6675897234Sdrh */
6773b211abSdrh #include "keywordhash.h"
6875897234Sdrh 
6940f20f7dSdrh 
7098808babSdrh /*
719b8f447bSdrh ** If X is a character that can be used in an identifier then
729b8f447bSdrh ** IdChar(X) will be true.  Otherwise it is false.
7398808babSdrh **
749b8f447bSdrh ** For ASCII, any character with the high-order bit set is
759b8f447bSdrh ** allowed in an identifier.  For 7-bit characters,
769b8f447bSdrh ** sqlite3IsIdChar[X] must be 1.
779b8f447bSdrh **
789b8f447bSdrh ** For EBCDIC, the rules are more complex but have the same
799b8f447bSdrh ** end result.
80a0d1f663Sdrh **
81a0d1f663Sdrh ** Ticket #1066.  the SQL standard does not allow '$' in the
82a0d1f663Sdrh ** middle of identfiers.  But many SQL implementations do.
83a0d1f663Sdrh ** SQLite will allow '$' in identifiers for compatibility.
84a0d1f663Sdrh ** But the feature is undocumented.
8598808babSdrh */
869b8f447bSdrh #ifdef SQLITE_ASCII
8746c99e0fSdrh const char sqlite3IsAsciiIdChar[] = {
8898808babSdrh /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
89a0d1f663Sdrh     0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 2x */
9098808babSdrh     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
9198808babSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
9298808babSdrh     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
9398808babSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
9498808babSdrh     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
9598808babSdrh };
9646c99e0fSdrh #define IdChar(C)  (((c=C)&0x80)!=0 || (c>0x1f && sqlite3IsAsciiIdChar[c-0x20]))
979b8f447bSdrh #endif
989b8f447bSdrh #ifdef SQLITE_EBCDIC
9946c99e0fSdrh const char sqlite3IsEbcdicIdChar[] = {
1009b8f447bSdrh /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
1019b8f447bSdrh     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 4x */
1029b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0,  /* 5x */
1039b8f447bSdrh     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,  /* 6x */
1049b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,  /* 7x */
1059b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0,  /* 8x */
1069b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0,  /* 9x */
1079b8f447bSdrh     1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0,  /* Ax */
1089b8f447bSdrh     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* Bx */
1099b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Cx */
1109b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Dx */
1119b8f447bSdrh     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Ex */
1129b8f447bSdrh     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0,  /* Fx */
1139b8f447bSdrh };
11446c99e0fSdrh #define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
1159b8f447bSdrh #endif
1169b8f447bSdrh 
11798808babSdrh 
11875897234Sdrh /*
11961b487d0Sdrh ** Return the length of the token that begins at z[0].
12061b487d0Sdrh ** Store the token type in *tokenType before returning.
12175897234Sdrh */
12235b5a33eSdrh int sqlite3GetToken(const unsigned char *z, int *tokenType){
123aa756b09Sdrh   int i, c;
12475897234Sdrh   switch( *z ){
12530cab809Sdrh     case ' ': case '\t': case '\n': case '\f': case '\r': {
12619f81f6cSdrh       testcase( z[0]==' ' );
12719f81f6cSdrh       testcase( z[0]=='\t' );
12819f81f6cSdrh       testcase( z[0]=='\n' );
12919f81f6cSdrh       testcase( z[0]=='\f' );
13019f81f6cSdrh       testcase( z[0]=='\r' );
13178ca0e7eSdanielk1977       for(i=1; sqlite3Isspace(z[i]); i++){}
13275897234Sdrh       *tokenType = TK_SPACE;
13375897234Sdrh       return i;
13475897234Sdrh     }
13575897234Sdrh     case '-': {
13675897234Sdrh       if( z[1]=='-' ){
137aa756b09Sdrh         for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
138200a81dcSdrh         *tokenType = TK_SPACE;
13975897234Sdrh         return i;
14075897234Sdrh       }
14175897234Sdrh       *tokenType = TK_MINUS;
14275897234Sdrh       return 1;
14375897234Sdrh     }
14475897234Sdrh     case '(': {
14575897234Sdrh       *tokenType = TK_LP;
14675897234Sdrh       return 1;
14775897234Sdrh     }
14875897234Sdrh     case ')': {
14975897234Sdrh       *tokenType = TK_RP;
15075897234Sdrh       return 1;
15175897234Sdrh     }
15275897234Sdrh     case ';': {
15375897234Sdrh       *tokenType = TK_SEMI;
15475897234Sdrh       return 1;
15575897234Sdrh     }
15675897234Sdrh     case '+': {
15775897234Sdrh       *tokenType = TK_PLUS;
15875897234Sdrh       return 1;
15975897234Sdrh     }
16075897234Sdrh     case '*': {
16175897234Sdrh       *tokenType = TK_STAR;
16275897234Sdrh       return 1;
16375897234Sdrh     }
16475897234Sdrh     case '/': {
16566105a8eSdrh       if( z[1]!='*' || z[2]==0 ){
16675897234Sdrh         *tokenType = TK_SLASH;
16775897234Sdrh         return 1;
16875897234Sdrh       }
169aa756b09Sdrh       for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
170aa756b09Sdrh       if( c ) i++;
171200a81dcSdrh       *tokenType = TK_SPACE;
17266105a8eSdrh       return i;
17366105a8eSdrh     }
174bf4133cbSdrh     case '%': {
175bf4133cbSdrh       *tokenType = TK_REM;
176bf4133cbSdrh       return 1;
177bf4133cbSdrh     }
17875897234Sdrh     case '=': {
17975897234Sdrh       *tokenType = TK_EQ;
18075897234Sdrh       return 1 + (z[1]=='=');
18175897234Sdrh     }
18275897234Sdrh     case '<': {
183aa756b09Sdrh       if( (c=z[1])=='=' ){
18475897234Sdrh         *tokenType = TK_LE;
18575897234Sdrh         return 2;
186aa756b09Sdrh       }else if( c=='>' ){
18775897234Sdrh         *tokenType = TK_NE;
18875897234Sdrh         return 2;
189aa756b09Sdrh       }else if( c=='<' ){
190bf4133cbSdrh         *tokenType = TK_LSHIFT;
191bf4133cbSdrh         return 2;
19275897234Sdrh       }else{
19375897234Sdrh         *tokenType = TK_LT;
19475897234Sdrh         return 1;
19575897234Sdrh       }
19675897234Sdrh     }
19775897234Sdrh     case '>': {
198aa756b09Sdrh       if( (c=z[1])=='=' ){
19975897234Sdrh         *tokenType = TK_GE;
20075897234Sdrh         return 2;
201aa756b09Sdrh       }else if( c=='>' ){
202bf4133cbSdrh         *tokenType = TK_RSHIFT;
203bf4133cbSdrh         return 2;
20475897234Sdrh       }else{
20575897234Sdrh         *tokenType = TK_GT;
20675897234Sdrh         return 1;
20775897234Sdrh       }
20875897234Sdrh     }
20975897234Sdrh     case '!': {
21075897234Sdrh       if( z[1]!='=' ){
21175897234Sdrh         *tokenType = TK_ILLEGAL;
212c837e709Sdrh         return 2;
21375897234Sdrh       }else{
21475897234Sdrh         *tokenType = TK_NE;
21575897234Sdrh         return 2;
21675897234Sdrh       }
21775897234Sdrh     }
2180040077dSdrh     case '|': {
2190040077dSdrh       if( z[1]!='|' ){
220bf4133cbSdrh         *tokenType = TK_BITOR;
2210040077dSdrh         return 1;
2220040077dSdrh       }else{
2230040077dSdrh         *tokenType = TK_CONCAT;
2240040077dSdrh         return 2;
2250040077dSdrh       }
2260040077dSdrh     }
22775897234Sdrh     case ',': {
22875897234Sdrh       *tokenType = TK_COMMA;
22975897234Sdrh       return 1;
23075897234Sdrh     }
231bf4133cbSdrh     case '&': {
232bf4133cbSdrh       *tokenType = TK_BITAND;
233bf4133cbSdrh       return 1;
234bf4133cbSdrh     }
235bf4133cbSdrh     case '~': {
236bf4133cbSdrh       *tokenType = TK_BITNOT;
237bf4133cbSdrh       return 1;
238bf4133cbSdrh     }
2393d94662aSdrh     case '`':
2403d94662aSdrh     case '\'':
2413d94662aSdrh     case '"': {
24275897234Sdrh       int delim = z[0];
24319f81f6cSdrh       testcase( delim=='`' );
24419f81f6cSdrh       testcase( delim=='\'' );
24519f81f6cSdrh       testcase( delim=='"' );
246aa756b09Sdrh       for(i=1; (c=z[i])!=0; i++){
247aa756b09Sdrh         if( c==delim ){
24875897234Sdrh           if( z[i+1]==delim ){
24975897234Sdrh             i++;
25075897234Sdrh           }else{
25175897234Sdrh             break;
25275897234Sdrh           }
25375897234Sdrh         }
25475897234Sdrh       }
255a33cb5f7Sdrh       if( c=='\'' ){
25675897234Sdrh         *tokenType = TK_STRING;
257eef8b558Sdrh         return i+1;
258a33cb5f7Sdrh       }else if( c!=0 ){
259a33cb5f7Sdrh         *tokenType = TK_ID;
260a33cb5f7Sdrh         return i+1;
261eef8b558Sdrh       }else{
262eef8b558Sdrh         *tokenType = TK_ILLEGAL;
26375897234Sdrh         return i;
26475897234Sdrh       }
265eef8b558Sdrh     }
26675897234Sdrh     case '.': {
2677681618cSdrh #ifndef SQLITE_OMIT_FLOATING_POINT
26878ca0e7eSdanielk1977       if( !sqlite3Isdigit(z[1]) )
2697681618cSdrh #endif
2707681618cSdrh       {
27175897234Sdrh         *tokenType = TK_DOT;
27275897234Sdrh         return 1;
27375897234Sdrh       }
2747681618cSdrh       /* If the next character is a digit, this is a floating point
2757681618cSdrh       ** number that begins with ".".  Fall thru into the next case */
2767681618cSdrh     }
27775897234Sdrh     case '0': case '1': case '2': case '3': case '4':
27875897234Sdrh     case '5': case '6': case '7': case '8': case '9': {
27919f81f6cSdrh       testcase( z[0]=='0' );  testcase( z[0]=='1' );  testcase( z[0]=='2' );
28019f81f6cSdrh       testcase( z[0]=='3' );  testcase( z[0]=='4' );  testcase( z[0]=='5' );
28119f81f6cSdrh       testcase( z[0]=='6' );  testcase( z[0]=='7' );  testcase( z[0]=='8' );
28219f81f6cSdrh       testcase( z[0]=='9' );
283c837e709Sdrh       *tokenType = TK_INTEGER;
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     }
317288d37f1Sdrh     case '#': {
31878ca0e7eSdanielk1977       for(i=1; sqlite3Isdigit(z[i]); i++){}
319288d37f1Sdrh       if( i>1 ){
320288d37f1Sdrh         /* Parameters of the form #NNN (where NNN is a number) are used
321288d37f1Sdrh         ** internally by sqlite3NestedParse.  */
322288d37f1Sdrh         *tokenType = TK_REGISTER;
323895d7472Sdrh         return i;
324895d7472Sdrh       }
325288d37f1Sdrh       /* Fall through into the next case if the '#' is not followed by
326288d37f1Sdrh       ** a digit. Try to match #AAAA where AAAA is a parameter name. */
327288d37f1Sdrh     }
328b7f9164eSdrh #ifndef SQLITE_OMIT_TCL_VARIABLE
329288d37f1Sdrh     case '$':
330288d37f1Sdrh #endif
3310b2a5eeaSdrh     case '@':  /* For compatibility with MS SQL Server */
332288d37f1Sdrh     case ':': {
333895d7472Sdrh       int n = 0;
3348be4f639Sdrh       testcase( z[0]=='$' );  testcase( z[0]=='@' );  testcase( z[0]==':' );
335288d37f1Sdrh       *tokenType = TK_VARIABLE;
336895d7472Sdrh       for(i=1; (c=z[i])!=0; i++){
337288d37f1Sdrh         if( IdChar(c) ){
338895d7472Sdrh           n++;
339288d37f1Sdrh #ifndef SQLITE_OMIT_TCL_VARIABLE
340895d7472Sdrh         }else if( c=='(' && n>0 ){
341895d7472Sdrh           do{
342895d7472Sdrh             i++;
34378ca0e7eSdanielk1977           }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
344895d7472Sdrh           if( c==')' ){
345895d7472Sdrh             i++;
346895d7472Sdrh           }else{
347895d7472Sdrh             *tokenType = TK_ILLEGAL;
348895d7472Sdrh           }
349895d7472Sdrh           break;
350895d7472Sdrh         }else if( c==':' && z[i+1]==':' ){
351895d7472Sdrh           i++;
352288d37f1Sdrh #endif
353895d7472Sdrh         }else{
354895d7472Sdrh           break;
355895d7472Sdrh         }
356895d7472Sdrh       }
3579d74b4c5Sdrh       if( n==0 ) *tokenType = TK_ILLEGAL;
358895d7472Sdrh       return i;
359895d7472Sdrh     }
360b7f9164eSdrh #ifndef SQLITE_OMIT_BLOB_LITERAL
361c572ef7fSdanielk1977     case 'x': case 'X': {
36219f81f6cSdrh       testcase( z[0]=='x' ); testcase( z[0]=='X' );
3634b2f9368Sdrh       if( z[1]=='\'' ){
3643fd0a736Sdanielk1977         *tokenType = TK_BLOB;
3654b2f9368Sdrh         for(i=2; (c=z[i])!=0 && c!='\''; i++){
36678ca0e7eSdanielk1977           if( !sqlite3Isxdigit(c) ){
3673fd0a736Sdanielk1977             *tokenType = TK_ILLEGAL;
3683fd0a736Sdanielk1977           }
369c572ef7fSdanielk1977         }
3704b2f9368Sdrh         if( i%2 || !c ) *tokenType = TK_ILLEGAL;
371aa756b09Sdrh         if( c ) i++;
372c572ef7fSdanielk1977         return i;
373c572ef7fSdanielk1977       }
374c572ef7fSdanielk1977       /* Otherwise fall through to the next case */
375c572ef7fSdanielk1977     }
376b7f9164eSdrh #endif
37798808babSdrh     default: {
3789a087a99Sdrh       if( !IdChar(*z) ){
37998808babSdrh         break;
38098808babSdrh       }
381aa756b09Sdrh       for(i=1; IdChar(z[i]); i++){}
382c60e9b82Sdanielk1977       *tokenType = keywordCode((char*)z, i);
38375897234Sdrh       return i;
38475897234Sdrh     }
38575897234Sdrh   }
38675897234Sdrh   *tokenType = TK_ILLEGAL;
38775897234Sdrh   return 1;
38875897234Sdrh }
38975897234Sdrh 
39075897234Sdrh /*
39175897234Sdrh ** Run the parser on the given SQL string.  The parser structure is
392b19a2bc6Sdrh ** passed in.  An SQLITE_ status code is returned.  If an error occurs
393fb45d8c5Sdrh ** then an and attempt is made to write an error message into
394fb45d8c5Sdrh ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
395fb45d8c5Sdrh ** error message.
39675897234Sdrh */
3974adee20fSdanielk1977 int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
398d9da78a2Sdrh   int nErr = 0;                   /* Number of errors encountered */
399d9da78a2Sdrh   int i;                          /* Loop counter */
400d9da78a2Sdrh   void *pEngine;                  /* The LEMON-generated LALR(1) parser */
401d9da78a2Sdrh   int tokenType;                  /* type of the next token */
402d9da78a2Sdrh   int lastTokenParsed = -1;       /* type of the previous token */
403b08a67a7Sshane   u8 enableLookaside;             /* Saved value of db->lookaside.bEnabled */
404d9da78a2Sdrh   sqlite3 *db = pParse->db;       /* The database connection */
405d9da78a2Sdrh   int mxSqlLen;                   /* Max length of an SQL string */
40675897234Sdrh 
407d9da78a2Sdrh 
408d9da78a2Sdrh   mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
40915ca1df1Sdrh   if( db->activeVdbeCnt==0 ){
410881feaa0Sdrh     db->u1.isInterrupted = 0;
41115ca1df1Sdrh   }
4124c504391Sdrh   pParse->rc = SQLITE_OK;
4134b1d66c1Sdrh   pParse->zTail = pParse->zSql = zSql;
41475897234Sdrh   i = 0;
415fb45d8c5Sdrh   assert( pzErrMsg!=0 );
416e5ae5735Sdrh   pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3Malloc);
41775897234Sdrh   if( pEngine==0 ){
418f3a65f7eSdrh     db->mallocFailed = 1;
419defc9972Sdrh     return SQLITE_NOMEM;
42075897234Sdrh   }
421fa6bc000Sdrh   assert( pParse->pNewTable==0 );
422fa6bc000Sdrh   assert( pParse->pNewTrigger==0 );
423fa6bc000Sdrh   assert( pParse->nVar==0 );
424fa6bc000Sdrh   assert( pParse->nVarExpr==0 );
425fa6bc000Sdrh   assert( pParse->nVarExprAlloc==0 );
426fa6bc000Sdrh   assert( pParse->apVarExpr==0 );
427d9da78a2Sdrh   enableLookaside = db->lookaside.bEnabled;
428d9da78a2Sdrh   if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
42917435752Sdrh   while( !db->mallocFailed && zSql[i]!=0 ){
43032eb7b47Sdrh     assert( i>=0 );
431b7916a78Sdrh     pParse->sLastToken.z = &zSql[i];
43235b5a33eSdrh     pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
43375897234Sdrh     i += pParse->sLastToken.n;
434bb4957f8Sdrh     if( i>mxSqlLen ){
435e5c941b8Sdrh       pParse->rc = SQLITE_TOOBIG;
436e5c941b8Sdrh       break;
437e5c941b8Sdrh     }
43875897234Sdrh     switch( tokenType ){
439200a81dcSdrh       case TK_SPACE: {
440881feaa0Sdrh         if( db->u1.isInterrupted ){
44119f81f6cSdrh           sqlite3ErrorMsg(pParse, "interrupt");
44232eb7b47Sdrh           pParse->rc = SQLITE_INTERRUPT;
44332eb7b47Sdrh           goto abort_parse;
44432eb7b47Sdrh         }
44575897234Sdrh         break;
44675897234Sdrh       }
44732eb7b47Sdrh       case TK_ILLEGAL: {
448633e6d57Sdrh         sqlite3DbFree(db, *pzErrMsg);
4491e536953Sdanielk1977         *pzErrMsg = sqlite3MPrintf(db, "unrecognized token: \"%T\"",
450ae29ffbeSdrh                         &pParse->sLastToken);
45175897234Sdrh         nErr++;
452caec2f12Sdrh         goto abort_parse;
45332eb7b47Sdrh       }
454326dce74Sdrh       case TK_SEMI: {
455326dce74Sdrh         pParse->zTail = &zSql[i];
456326dce74Sdrh         /* Fall thru into the default case */
457326dce74Sdrh       }
45832eb7b47Sdrh       default: {
4594adee20fSdanielk1977         sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
460b86ccfb2Sdrh         lastTokenParsed = tokenType;
461b86ccfb2Sdrh         if( pParse->rc!=SQLITE_OK ){
46232eb7b47Sdrh           goto abort_parse;
46375897234Sdrh         }
46475897234Sdrh         break;
46575897234Sdrh       }
46675897234Sdrh     }
46732eb7b47Sdrh   }
46832eb7b47Sdrh abort_parse:
469b86ccfb2Sdrh   if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
470b86ccfb2Sdrh     if( lastTokenParsed!=TK_SEMI ){
4714adee20fSdanielk1977       sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
472326dce74Sdrh       pParse->zTail = &zSql[i];
47375897234Sdrh     }
4744adee20fSdanielk1977     sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
47575897234Sdrh   }
476ec424a5bSdrh #ifdef YYTRACKMAXSTACKDEPTH
477ec424a5bSdrh   sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK,
478ec424a5bSdrh       sqlite3ParserStackPeak(pEngine)
479ec424a5bSdrh   );
480ec424a5bSdrh #endif /* YYDEBUG */
48117435752Sdrh   sqlite3ParserFree(pEngine, sqlite3_free);
482d9da78a2Sdrh   db->lookaside.bEnabled = enableLookaside;
48317435752Sdrh   if( db->mallocFailed ){
48471c697efSdrh     pParse->rc = SQLITE_NOMEM;
48571c697efSdrh   }
486b86ccfb2Sdrh   if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
487f089aa45Sdrh     sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc));
488b86ccfb2Sdrh   }
48919f81f6cSdrh   assert( pzErrMsg!=0 );
49075897234Sdrh   if( pParse->zErrMsg ){
49175897234Sdrh     *pzErrMsg = pParse->zErrMsg;
492b86ccfb2Sdrh     pParse->zErrMsg = 0;
4934b2f9368Sdrh     nErr++;
49475897234Sdrh   }
4952958a4e6Sdrh   if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
4964adee20fSdanielk1977     sqlite3VdbeDelete(pParse->pVdbe);
49775897234Sdrh     pParse->pVdbe = 0;
49875897234Sdrh   }
499c00da105Sdanielk1977 #ifndef SQLITE_OMIT_SHARED_CACHE
500c00da105Sdanielk1977   if( pParse->nested==0 ){
501633e6d57Sdrh     sqlite3DbFree(db, pParse->aTableLock);
502c00da105Sdanielk1977     pParse->aTableLock = 0;
503c00da105Sdanielk1977     pParse->nTableLock = 0;
504c00da105Sdanielk1977   }
505c00da105Sdanielk1977 #endif
5064f3dd150Sdrh #ifndef SQLITE_OMIT_VIRTUALTABLE
507633e6d57Sdrh   sqlite3DbFree(db, pParse->apVtabLock);
5084f3dd150Sdrh #endif
5097e6ebfb2Sdanielk1977 
5107e6ebfb2Sdanielk1977   if( !IN_DECLARE_VTAB ){
5117e6ebfb2Sdanielk1977     /* If the pParse->declareVtab flag is set, do not delete any table
5127e6ebfb2Sdanielk1977     ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
5137e6ebfb2Sdanielk1977     ** will take responsibility for freeing the Table structure.
5147e6ebfb2Sdanielk1977     */
515a04a34ffSdanielk1977     sqlite3DeleteTable(pParse->pNewTable);
5167e6ebfb2Sdanielk1977   }
5177e6ebfb2Sdanielk1977 
518633e6d57Sdrh   sqlite3DeleteTrigger(db, pParse->pNewTrigger);
519633e6d57Sdrh   sqlite3DbFree(db, pParse->apVarExpr);
5208b213899Sdrh   sqlite3DbFree(db, pParse->aAlias);
521588a9a1aSdrh   while( pParse->pZombieTab ){
522588a9a1aSdrh     Table *p = pParse->pZombieTab;
523588a9a1aSdrh     pParse->pZombieTab = p->pNextZombie;
524588a9a1aSdrh     sqlite3DeleteTable(p);
525588a9a1aSdrh   }
526*b5092d18Sdrh   if( nErr>0 && pParse->rc==SQLITE_OK ){
5274c504391Sdrh     pParse->rc = SQLITE_ERROR;
5284c504391Sdrh   }
52975897234Sdrh   return nErr;
53075897234Sdrh }
531