000001  /*
000002  ** 2001 September 15
000003  **
000004  ** The author disclaims copyright to this source code.  In place of
000005  ** a legal notice, here is a blessing:
000006  **
000007  **    May you do good and not evil.
000008  **    May you find forgiveness for yourself and forgive others.
000009  **    May you share freely, never taking more than you give.
000010  **
000011  *************************************************************************
000012  ** An tokenizer for SQL
000013  **
000014  ** This file contains C code that splits an SQL input string up into
000015  ** individual tokens and sends those tokens one-by-one over to the
000016  ** parser for analysis.
000017  */
000018  #include "sqliteInt.h"
000019  #include <stdlib.h>
000020  
000021  /* Character classes for tokenizing
000022  **
000023  ** In the sqlite3GetToken() function, a switch() on aiClass[c] is implemented
000024  ** using a lookup table, whereas a switch() directly on c uses a binary search.
000025  ** The lookup table is much faster.  To maximize speed, and to ensure that
000026  ** a lookup table is used, all of the classes need to be small integers and
000027  ** all of them need to be used within the switch.
000028  */
000029  #define CC_X          0    /* The letter 'x', or start of BLOB literal */
000030  #define CC_KYWD       1    /* Alphabetics or '_'.  Usable in a keyword */
000031  #define CC_ID         2    /* unicode characters usable in IDs */
000032  #define CC_DIGIT      3    /* Digits */
000033  #define CC_DOLLAR     4    /* '$' */
000034  #define CC_VARALPHA   5    /* '@', '#', ':'.  Alphabetic SQL variables */
000035  #define CC_VARNUM     6    /* '?'.  Numeric SQL variables */
000036  #define CC_SPACE      7    /* Space characters */
000037  #define CC_QUOTE      8    /* '"', '\'', or '`'.  String literals, quoted ids */
000038  #define CC_QUOTE2     9    /* '['.   [...] style quoted ids */
000039  #define CC_PIPE      10    /* '|'.   Bitwise OR or concatenate */
000040  #define CC_MINUS     11    /* '-'.  Minus or SQL-style comment */
000041  #define CC_LT        12    /* '<'.  Part of < or <= or <> */
000042  #define CC_GT        13    /* '>'.  Part of > or >= */
000043  #define CC_EQ        14    /* '='.  Part of = or == */
000044  #define CC_BANG      15    /* '!'.  Part of != */
000045  #define CC_SLASH     16    /* '/'.  / or c-style comment */
000046  #define CC_LP        17    /* '(' */
000047  #define CC_RP        18    /* ')' */
000048  #define CC_SEMI      19    /* ';' */
000049  #define CC_PLUS      20    /* '+' */
000050  #define CC_STAR      21    /* '*' */
000051  #define CC_PERCENT   22    /* '%' */
000052  #define CC_COMMA     23    /* ',' */
000053  #define CC_AND       24    /* '&' */
000054  #define CC_TILDA     25    /* '~' */
000055  #define CC_DOT       26    /* '.' */
000056  #define CC_ILLEGAL   27    /* Illegal character */
000057  
000058  static const unsigned char aiClass[] = {
000059  #ifdef SQLITE_ASCII
000060  /*         x0  x1  x2  x3  x4  x5  x6  x7  x8  x9  xa  xb  xc  xd  xe  xf */
000061  /* 0x */   27, 27, 27, 27, 27, 27, 27, 27, 27,  7,  7, 27,  7,  7, 27, 27,
000062  /* 1x */   27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
000063  /* 2x */    7, 15,  8,  5,  4, 22, 24,  8, 17, 18, 21, 20, 23, 11, 26, 16,
000064  /* 3x */    3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  5, 19, 12, 14, 13,  6,
000065  /* 4x */    5,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
000066  /* 5x */    1,  1,  1,  1,  1,  1,  1,  1,  0,  1,  1,  9, 27, 27, 27,  1,
000067  /* 6x */    8,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
000068  /* 7x */    1,  1,  1,  1,  1,  1,  1,  1,  0,  1,  1, 27, 10, 27, 25, 27,
000069  /* 8x */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
000070  /* 9x */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
000071  /* Ax */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
000072  /* Bx */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
000073  /* Cx */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
000074  /* Dx */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
000075  /* Ex */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
000076  /* Fx */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2
000077  #endif
000078  #ifdef SQLITE_EBCDIC
000079  /*         x0  x1  x2  x3  x4  x5  x6  x7  x8  x9  xa  xb  xc  xd  xe  xf */
000080  /* 0x */   27, 27, 27, 27, 27,  7, 27, 27, 27, 27, 27, 27,  7,  7, 27, 27,
000081  /* 1x */   27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
000082  /* 2x */   27, 27, 27, 27, 27,  7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
000083  /* 3x */   27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
000084  /* 4x */    7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 26, 12, 17, 20, 10,
000085  /* 5x */   24, 27, 27, 27, 27, 27, 27, 27, 27, 27, 15,  4, 21, 18, 19, 27,
000086  /* 6x */   11, 16, 27, 27, 27, 27, 27, 27, 27, 27, 27, 23, 22,  1, 13,  6,
000087  /* 7x */   27, 27, 27, 27, 27, 27, 27, 27, 27,  8,  5,  5,  5,  8, 14,  8,
000088  /* 8x */   27,  1,  1,  1,  1,  1,  1,  1,  1,  1, 27, 27, 27, 27, 27, 27,
000089  /* 9x */   27,  1,  1,  1,  1,  1,  1,  1,  1,  1, 27, 27, 27, 27, 27, 27,
000090  /* Ax */   27, 25,  1,  1,  1,  1,  1,  0,  1,  1, 27, 27, 27, 27, 27, 27,
000091  /* Bx */   27, 27, 27, 27, 27, 27, 27, 27, 27, 27,  9, 27, 27, 27, 27, 27,
000092  /* Cx */   27,  1,  1,  1,  1,  1,  1,  1,  1,  1, 27, 27, 27, 27, 27, 27,
000093  /* Dx */   27,  1,  1,  1,  1,  1,  1,  1,  1,  1, 27, 27, 27, 27, 27, 27,
000094  /* Ex */   27, 27,  1,  1,  1,  1,  1,  0,  1,  1, 27, 27, 27, 27, 27, 27,
000095  /* Fx */    3,  3,  3,  3,  3,  3,  3,  3,  3,  3, 27, 27, 27, 27, 27, 27,
000096  #endif
000097  };
000098  
000099  /*
000100  ** The charMap() macro maps alphabetic characters (only) into their
000101  ** lower-case ASCII equivalent.  On ASCII machines, this is just
000102  ** an upper-to-lower case map.  On EBCDIC machines we also need
000103  ** to adjust the encoding.  The mapping is only valid for alphabetics
000104  ** which are the only characters for which this feature is used. 
000105  **
000106  ** Used by keywordhash.h
000107  */
000108  #ifdef SQLITE_ASCII
000109  # define charMap(X) sqlite3UpperToLower[(unsigned char)X]
000110  #endif
000111  #ifdef SQLITE_EBCDIC
000112  # define charMap(X) ebcdicToAscii[(unsigned char)X]
000113  const unsigned char ebcdicToAscii[] = {
000114  /* 0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F */
000115     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 0x */
000116     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 1x */
000117     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 2x */
000118     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 3x */
000119     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 4x */
000120     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 5x */
000121     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 95,  0,  0,  /* 6x */
000122     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 7x */
000123     0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* 8x */
000124     0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* 9x */
000125     0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ax */
000126     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Bx */
000127     0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* Cx */
000128     0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* Dx */
000129     0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ex */
000130     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Fx */
000131  };
000132  #endif
000133  
000134  /*
000135  ** The sqlite3KeywordCode function looks up an identifier to determine if
000136  ** it is a keyword.  If it is a keyword, the token code of that keyword is 
000137  ** returned.  If the input is not a keyword, TK_ID is returned.
000138  **
000139  ** The implementation of this routine was generated by a program,
000140  ** mkkeywordhash.c, located in the tool subdirectory of the distribution.
000141  ** The output of the mkkeywordhash.c program is written into a file
000142  ** named keywordhash.h and then included into this source file by
000143  ** the #include below.
000144  */
000145  #include "keywordhash.h"
000146  
000147  
000148  /*
000149  ** If X is a character that can be used in an identifier then
000150  ** IdChar(X) will be true.  Otherwise it is false.
000151  **
000152  ** For ASCII, any character with the high-order bit set is
000153  ** allowed in an identifier.  For 7-bit characters, 
000154  ** sqlite3IsIdChar[X] must be 1.
000155  **
000156  ** For EBCDIC, the rules are more complex but have the same
000157  ** end result.
000158  **
000159  ** Ticket #1066.  the SQL standard does not allow '$' in the
000160  ** middle of identifiers.  But many SQL implementations do. 
000161  ** SQLite will allow '$' in identifiers for compatibility.
000162  ** But the feature is undocumented.
000163  */
000164  #ifdef SQLITE_ASCII
000165  #define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
000166  #endif
000167  #ifdef SQLITE_EBCDIC
000168  const char sqlite3IsEbcdicIdChar[] = {
000169  /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
000170      0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 4x */
000171      0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0,  /* 5x */
000172      0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,  /* 6x */
000173      0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,  /* 7x */
000174      0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0,  /* 8x */
000175      0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0,  /* 9x */
000176      1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0,  /* Ax */
000177      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* Bx */
000178      0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Cx */
000179      0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Dx */
000180      0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Ex */
000181      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0,  /* Fx */
000182  };
000183  #define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
000184  #endif
000185  
000186  /* Make the IdChar function accessible from ctime.c */
000187  #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
000188  int sqlite3IsIdChar(u8 c){ return IdChar(c); }
000189  #endif
000190  
000191  
000192  /*
000193  ** Return the length (in bytes) of the token that begins at z[0]. 
000194  ** Store the token type in *tokenType before returning.
000195  */
000196  int sqlite3GetToken(const unsigned char *z, int *tokenType){
000197    int i, c;
000198    switch( aiClass[*z] ){  /* Switch on the character-class of the first byte
000199                            ** of the token. See the comment on the CC_ defines
000200                            ** above. */
000201      case CC_SPACE: {
000202        testcase( z[0]==' ' );
000203        testcase( z[0]=='\t' );
000204        testcase( z[0]=='\n' );
000205        testcase( z[0]=='\f' );
000206        testcase( z[0]=='\r' );
000207        for(i=1; sqlite3Isspace(z[i]); i++){}
000208        *tokenType = TK_SPACE;
000209        return i;
000210      }
000211      case CC_MINUS: {
000212        if( z[1]=='-' ){
000213          for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
000214          *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
000215          return i;
000216        }
000217        *tokenType = TK_MINUS;
000218        return 1;
000219      }
000220      case CC_LP: {
000221        *tokenType = TK_LP;
000222        return 1;
000223      }
000224      case CC_RP: {
000225        *tokenType = TK_RP;
000226        return 1;
000227      }
000228      case CC_SEMI: {
000229        *tokenType = TK_SEMI;
000230        return 1;
000231      }
000232      case CC_PLUS: {
000233        *tokenType = TK_PLUS;
000234        return 1;
000235      }
000236      case CC_STAR: {
000237        *tokenType = TK_STAR;
000238        return 1;
000239      }
000240      case CC_SLASH: {
000241        if( z[1]!='*' || z[2]==0 ){
000242          *tokenType = TK_SLASH;
000243          return 1;
000244        }
000245        for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
000246        if( c ) i++;
000247        *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
000248        return i;
000249      }
000250      case CC_PERCENT: {
000251        *tokenType = TK_REM;
000252        return 1;
000253      }
000254      case CC_EQ: {
000255        *tokenType = TK_EQ;
000256        return 1 + (z[1]=='=');
000257      }
000258      case CC_LT: {
000259        if( (c=z[1])=='=' ){
000260          *tokenType = TK_LE;
000261          return 2;
000262        }else if( c=='>' ){
000263          *tokenType = TK_NE;
000264          return 2;
000265        }else if( c=='<' ){
000266          *tokenType = TK_LSHIFT;
000267          return 2;
000268        }else{
000269          *tokenType = TK_LT;
000270          return 1;
000271        }
000272      }
000273      case CC_GT: {
000274        if( (c=z[1])=='=' ){
000275          *tokenType = TK_GE;
000276          return 2;
000277        }else if( c=='>' ){
000278          *tokenType = TK_RSHIFT;
000279          return 2;
000280        }else{
000281          *tokenType = TK_GT;
000282          return 1;
000283        }
000284      }
000285      case CC_BANG: {
000286        if( z[1]!='=' ){
000287          *tokenType = TK_ILLEGAL;
000288          return 1;
000289        }else{
000290          *tokenType = TK_NE;
000291          return 2;
000292        }
000293      }
000294      case CC_PIPE: {
000295        if( z[1]!='|' ){
000296          *tokenType = TK_BITOR;
000297          return 1;
000298        }else{
000299          *tokenType = TK_CONCAT;
000300          return 2;
000301        }
000302      }
000303      case CC_COMMA: {
000304        *tokenType = TK_COMMA;
000305        return 1;
000306      }
000307      case CC_AND: {
000308        *tokenType = TK_BITAND;
000309        return 1;
000310      }
000311      case CC_TILDA: {
000312        *tokenType = TK_BITNOT;
000313        return 1;
000314      }
000315      case CC_QUOTE: {
000316        int delim = z[0];
000317        testcase( delim=='`' );
000318        testcase( delim=='\'' );
000319        testcase( delim=='"' );
000320        for(i=1; (c=z[i])!=0; i++){
000321          if( c==delim ){
000322            if( z[i+1]==delim ){
000323              i++;
000324            }else{
000325              break;
000326            }
000327          }
000328        }
000329        if( c=='\'' ){
000330          *tokenType = TK_STRING;
000331          return i+1;
000332        }else if( c!=0 ){
000333          *tokenType = TK_ID;
000334          return i+1;
000335        }else{
000336          *tokenType = TK_ILLEGAL;
000337          return i;
000338        }
000339      }
000340      case CC_DOT: {
000341  #ifndef SQLITE_OMIT_FLOATING_POINT
000342        if( !sqlite3Isdigit(z[1]) )
000343  #endif
000344        {
000345          *tokenType = TK_DOT;
000346          return 1;
000347        }
000348        /* If the next character is a digit, this is a floating point
000349        ** number that begins with ".".  Fall thru into the next case */
000350      }
000351      case CC_DIGIT: {
000352        testcase( z[0]=='0' );  testcase( z[0]=='1' );  testcase( z[0]=='2' );
000353        testcase( z[0]=='3' );  testcase( z[0]=='4' );  testcase( z[0]=='5' );
000354        testcase( z[0]=='6' );  testcase( z[0]=='7' );  testcase( z[0]=='8' );
000355        testcase( z[0]=='9' );
000356        *tokenType = TK_INTEGER;
000357  #ifndef SQLITE_OMIT_HEX_INTEGER
000358        if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){
000359          for(i=3; sqlite3Isxdigit(z[i]); i++){}
000360          return i;
000361        }
000362  #endif
000363        for(i=0; sqlite3Isdigit(z[i]); i++){}
000364  #ifndef SQLITE_OMIT_FLOATING_POINT
000365        if( z[i]=='.' ){
000366          i++;
000367          while( sqlite3Isdigit(z[i]) ){ i++; }
000368          *tokenType = TK_FLOAT;
000369        }
000370        if( (z[i]=='e' || z[i]=='E') &&
000371             ( sqlite3Isdigit(z[i+1]) 
000372              || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
000373             )
000374        ){
000375          i += 2;
000376          while( sqlite3Isdigit(z[i]) ){ i++; }
000377          *tokenType = TK_FLOAT;
000378        }
000379  #endif
000380        while( IdChar(z[i]) ){
000381          *tokenType = TK_ILLEGAL;
000382          i++;
000383        }
000384        return i;
000385      }
000386      case CC_QUOTE2: {
000387        for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
000388        *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
000389        return i;
000390      }
000391      case CC_VARNUM: {
000392        *tokenType = TK_VARIABLE;
000393        for(i=1; sqlite3Isdigit(z[i]); i++){}
000394        return i;
000395      }
000396      case CC_DOLLAR:
000397      case CC_VARALPHA: {
000398        int n = 0;
000399        testcase( z[0]=='$' );  testcase( z[0]=='@' );
000400        testcase( z[0]==':' );  testcase( z[0]=='#' );
000401        *tokenType = TK_VARIABLE;
000402        for(i=1; (c=z[i])!=0; i++){
000403          if( IdChar(c) ){
000404            n++;
000405  #ifndef SQLITE_OMIT_TCL_VARIABLE
000406          }else if( c=='(' && n>0 ){
000407            do{
000408              i++;
000409            }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
000410            if( c==')' ){
000411              i++;
000412            }else{
000413              *tokenType = TK_ILLEGAL;
000414            }
000415            break;
000416          }else if( c==':' && z[i+1]==':' ){
000417            i++;
000418  #endif
000419          }else{
000420            break;
000421          }
000422        }
000423        if( n==0 ) *tokenType = TK_ILLEGAL;
000424        return i;
000425      }
000426      case CC_KYWD: {
000427        for(i=1; aiClass[z[i]]<=CC_KYWD; i++){}
000428        if( IdChar(z[i]) ){
000429          /* This token started out using characters that can appear in keywords,
000430          ** but z[i] is a character not allowed within keywords, so this must
000431          ** be an identifier instead */
000432          i++;
000433          break;
000434        }
000435        *tokenType = TK_ID;
000436        return keywordCode((char*)z, i, tokenType);
000437      }
000438      case CC_X: {
000439  #ifndef SQLITE_OMIT_BLOB_LITERAL
000440        testcase( z[0]=='x' ); testcase( z[0]=='X' );
000441        if( z[1]=='\'' ){
000442          *tokenType = TK_BLOB;
000443          for(i=2; sqlite3Isxdigit(z[i]); i++){}
000444          if( z[i]!='\'' || i%2 ){
000445            *tokenType = TK_ILLEGAL;
000446            while( z[i] && z[i]!='\'' ){ i++; }
000447          }
000448          if( z[i] ) i++;
000449          return i;
000450        }
000451  #endif
000452        /* If it is not a BLOB literal, then it must be an ID, since no
000453        ** SQL keywords start with the letter 'x'.  Fall through */
000454      }
000455      case CC_ID: {
000456        i = 1;
000457        break;
000458      }
000459      default: {
000460        *tokenType = TK_ILLEGAL;
000461        return 1;
000462      }
000463    }
000464    while( IdChar(z[i]) ){ i++; }
000465    *tokenType = TK_ID;
000466    return i;
000467  }
000468  
000469  /*
000470  ** Run the parser on the given SQL string.  The parser structure is
000471  ** passed in.  An SQLITE_ status code is returned.  If an error occurs
000472  ** then an and attempt is made to write an error message into 
000473  ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
000474  ** error message.
000475  */
000476  int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
000477    int nErr = 0;                   /* Number of errors encountered */
000478    int i;                          /* Loop counter */
000479    void *pEngine;                  /* The LEMON-generated LALR(1) parser */
000480    int tokenType;                  /* type of the next token */
000481    int lastTokenParsed = -1;       /* type of the previous token */
000482    sqlite3 *db = pParse->db;       /* The database connection */
000483    int mxSqlLen;                   /* Max length of an SQL string */
000484  
000485    assert( zSql!=0 );
000486    mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
000487    if( db->nVdbeActive==0 ){
000488      db->u1.isInterrupted = 0;
000489    }
000490    pParse->rc = SQLITE_OK;
000491    pParse->zTail = zSql;
000492    i = 0;
000493    assert( pzErrMsg!=0 );
000494    /* sqlite3ParserTrace(stdout, "parser: "); */
000495    pEngine = sqlite3ParserAlloc(sqlite3Malloc);
000496    if( pEngine==0 ){
000497      sqlite3OomFault(db);
000498      return SQLITE_NOMEM_BKPT;
000499    }
000500    assert( pParse->pNewTable==0 );
000501    assert( pParse->pNewTrigger==0 );
000502    assert( pParse->nVar==0 );
000503    assert( pParse->pVList==0 );
000504    while( 1 ){
000505      assert( i>=0 );
000506      if( zSql[i]!=0 ){
000507        pParse->sLastToken.z = &zSql[i];
000508        pParse->sLastToken.n = sqlite3GetToken((u8*)&zSql[i],&tokenType);
000509        i += pParse->sLastToken.n;
000510        if( i>mxSqlLen ){
000511          pParse->rc = SQLITE_TOOBIG;
000512          break;
000513        }
000514      }else{
000515        /* Upon reaching the end of input, call the parser two more times
000516        ** with tokens TK_SEMI and 0, in that order. */
000517        if( lastTokenParsed==TK_SEMI ){
000518          tokenType = 0;
000519        }else if( lastTokenParsed==0 ){
000520          break;
000521        }else{
000522          tokenType = TK_SEMI;
000523        }
000524      }
000525      if( tokenType>=TK_SPACE ){
000526        assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL );
000527        if( db->u1.isInterrupted ){
000528          pParse->rc = SQLITE_INTERRUPT;
000529          break;
000530        }
000531        if( tokenType==TK_ILLEGAL ){
000532          sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"",
000533                          &pParse->sLastToken);
000534          break;
000535        }
000536      }else{
000537        sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
000538        lastTokenParsed = tokenType;
000539        if( pParse->rc!=SQLITE_OK || db->mallocFailed ) break;
000540      }
000541    }
000542    assert( nErr==0 );
000543    pParse->zTail = &zSql[i];
000544  #ifdef YYTRACKMAXSTACKDEPTH
000545    sqlite3_mutex_enter(sqlite3MallocMutex());
000546    sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK,
000547        sqlite3ParserStackPeak(pEngine)
000548    );
000549    sqlite3_mutex_leave(sqlite3MallocMutex());
000550  #endif /* YYDEBUG */
000551    sqlite3ParserFree(pEngine, sqlite3_free);
000552    if( db->mallocFailed ){
000553      pParse->rc = SQLITE_NOMEM_BKPT;
000554    }
000555    if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
000556      pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
000557    }
000558    assert( pzErrMsg!=0 );
000559    if( pParse->zErrMsg ){
000560      *pzErrMsg = pParse->zErrMsg;
000561      sqlite3_log(pParse->rc, "%s", *pzErrMsg);
000562      pParse->zErrMsg = 0;
000563      nErr++;
000564    }
000565    if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
000566      sqlite3VdbeDelete(pParse->pVdbe);
000567      pParse->pVdbe = 0;
000568    }
000569  #ifndef SQLITE_OMIT_SHARED_CACHE
000570    if( pParse->nested==0 ){
000571      sqlite3DbFree(db, pParse->aTableLock);
000572      pParse->aTableLock = 0;
000573      pParse->nTableLock = 0;
000574    }
000575  #endif
000576  #ifndef SQLITE_OMIT_VIRTUALTABLE
000577    sqlite3_free(pParse->apVtabLock);
000578  #endif
000579  
000580    if( !IN_DECLARE_VTAB ){
000581      /* If the pParse->declareVtab flag is set, do not delete any table 
000582      ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
000583      ** will take responsibility for freeing the Table structure.
000584      */
000585      sqlite3DeleteTable(db, pParse->pNewTable);
000586    }
000587  
000588    if( pParse->pWithToFree ) sqlite3WithDelete(db, pParse->pWithToFree);
000589    sqlite3DeleteTrigger(db, pParse->pNewTrigger);
000590    sqlite3DbFree(db, pParse->pVList);
000591    while( pParse->pAinc ){
000592      AutoincInfo *p = pParse->pAinc;
000593      pParse->pAinc = p->pNext;
000594      sqlite3DbFree(db, p);
000595    }
000596    while( pParse->pZombieTab ){
000597      Table *p = pParse->pZombieTab;
000598      pParse->pZombieTab = p->pNextZombie;
000599      sqlite3DeleteTable(db, p);
000600    }
000601    assert( nErr==0 || pParse->rc!=SQLITE_OK );
000602    return nErr;
000603  }