extensions/sqlite/sqlite-source/tokenize.c
author Scott Ehlert <ds@alliedmods.net>
Sun Mar 30 07:00:22 2008 +0000 (2008-03-30)
changeset 1952 b7aa2b626807
permissions -rw-r--r--
Spring Cleaning, Part Ichi (1)
Various minor things done to project files
Updated sample extension project file and updated makefile to the new unified version (more changes likely on the way)
Updated regex project file and makefile
ds@1952
     1
/*
ds@1952
     2
** 2001 September 15
ds@1952
     3
**
ds@1952
     4
** The author disclaims copyright to this source code.  In place of
ds@1952
     5
** a legal notice, here is a blessing:
ds@1952
     6
**
ds@1952
     7
**    May you do good and not evil.
ds@1952
     8
**    May you find forgiveness for yourself and forgive others.
ds@1952
     9
**    May you share freely, never taking more than you give.
ds@1952
    10
**
ds@1952
    11
*************************************************************************
ds@1952
    12
** An tokenizer for SQL
ds@1952
    13
**
ds@1952
    14
** This file contains C code that splits an SQL input string up into
ds@1952
    15
** individual tokens and sends those tokens one-by-one over to the
ds@1952
    16
** parser for analysis.
ds@1952
    17
**
ds@1952
    18
** $Id$
ds@1952
    19
*/
ds@1952
    20
#include "sqliteInt.h"
ds@1952
    21
#include <ctype.h>
ds@1952
    22
#include <stdlib.h>
ds@1952
    23
ds@1952
    24
/*
ds@1952
    25
** The charMap() macro maps alphabetic characters into their
ds@1952
    26
** lower-case ASCII equivalent.  On ASCII machines, this is just
ds@1952
    27
** an upper-to-lower case map.  On EBCDIC machines we also need
ds@1952
    28
** to adjust the encoding.  Only alphabetic characters and underscores
ds@1952
    29
** need to be translated.
ds@1952
    30
*/
ds@1952
    31
#ifdef SQLITE_ASCII
ds@1952
    32
# define charMap(X) sqlite3UpperToLower[(unsigned char)X]
ds@1952
    33
#endif
ds@1952
    34
#ifdef SQLITE_EBCDIC
ds@1952
    35
# define charMap(X) ebcdicToAscii[(unsigned char)X]
ds@1952
    36
const unsigned char ebcdicToAscii[] = {
ds@1952
    37
/* 0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F */
ds@1952
    38
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 0x */
ds@1952
    39
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 1x */
ds@1952
    40
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 2x */
ds@1952
    41
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 3x */
ds@1952
    42
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 4x */
ds@1952
    43
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 5x */
ds@1952
    44
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 95,  0,  0,  /* 6x */
ds@1952
    45
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 7x */
ds@1952
    46
   0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* 8x */
ds@1952
    47
   0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* 9x */
ds@1952
    48
   0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ax */
ds@1952
    49
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Bx */
ds@1952
    50
   0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* Cx */
ds@1952
    51
   0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* Dx */
ds@1952
    52
   0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ex */
ds@1952
    53
   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Fx */
ds@1952
    54
};
ds@1952
    55
#endif
ds@1952
    56
ds@1952
    57
/*
ds@1952
    58
** The sqlite3KeywordCode function looks up an identifier to determine if
ds@1952
    59
** it is a keyword.  If it is a keyword, the token code of that keyword is 
ds@1952
    60
** returned.  If the input is not a keyword, TK_ID is returned.
ds@1952
    61
**
ds@1952
    62
** The implementation of this routine was generated by a program,
ds@1952
    63
** mkkeywordhash.h, located in the tool subdirectory of the distribution.
ds@1952
    64
** The output of the mkkeywordhash.c program is written into a file
ds@1952
    65
** named keywordhash.h and then included into this source file by
ds@1952
    66
** the #include below.
ds@1952
    67
*/
ds@1952
    68
#include "keywordhash.h"
ds@1952
    69
ds@1952
    70
ds@1952
    71
/*
ds@1952
    72
** If X is a character that can be used in an identifier then
ds@1952
    73
** IdChar(X) will be true.  Otherwise it is false.
ds@1952
    74
**
ds@1952
    75
** For ASCII, any character with the high-order bit set is
ds@1952
    76
** allowed in an identifier.  For 7-bit characters, 
ds@1952
    77
** sqlite3IsIdChar[X] must be 1.
ds@1952
    78
**
ds@1952
    79
** For EBCDIC, the rules are more complex but have the same
ds@1952
    80
** end result.
ds@1952
    81
**
ds@1952
    82
** Ticket #1066.  the SQL standard does not allow '$' in the
ds@1952
    83
** middle of identfiers.  But many SQL implementations do. 
ds@1952
    84
** SQLite will allow '$' in identifiers for compatibility.
ds@1952
    85
** But the feature is undocumented.
ds@1952
    86
*/
ds@1952
    87
#ifdef SQLITE_ASCII
ds@1952
    88
const char sqlite3IsAsciiIdChar[] = {
ds@1952
    89
/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
ds@1952
    90
    0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 2x */
ds@1952
    91
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
ds@1952
    92
    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
ds@1952
    93
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
ds@1952
    94
    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
ds@1952
    95
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
ds@1952
    96
};
ds@1952
    97
#define IdChar(C)  (((c=C)&0x80)!=0 || (c>0x1f && sqlite3IsAsciiIdChar[c-0x20]))
ds@1952
    98
#endif
ds@1952
    99
#ifdef SQLITE_EBCDIC
ds@1952
   100
const char sqlite3IsEbcdicIdChar[] = {
ds@1952
   101
/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
ds@1952
   102
    0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 4x */
ds@1952
   103
    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0,  /* 5x */
ds@1952
   104
    0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,  /* 6x */
ds@1952
   105
    0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,  /* 7x */
ds@1952
   106
    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0,  /* 8x */
ds@1952
   107
    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0,  /* 9x */
ds@1952
   108
    1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0,  /* Ax */
ds@1952
   109
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* Bx */
ds@1952
   110
    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Cx */
ds@1952
   111
    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Dx */
ds@1952
   112
    0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Ex */
ds@1952
   113
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0,  /* Fx */
ds@1952
   114
};
ds@1952
   115
#define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
ds@1952
   116
#endif
ds@1952
   117
ds@1952
   118
ds@1952
   119
/*
ds@1952
   120
** Return the length of the token that begins at z[0]. 
ds@1952
   121
** Store the token type in *tokenType before returning.
ds@1952
   122
*/
ds@1952
   123
static int getToken(const unsigned char *z, int *tokenType){
ds@1952
   124
  int i, c;
ds@1952
   125
  switch( *z ){
ds@1952
   126
    case ' ': case '\t': case '\n': case '\f': case '\r': {
ds@1952
   127
      for(i=1; isspace(z[i]); i++){}
ds@1952
   128
      *tokenType = TK_SPACE;
ds@1952
   129
      return i;
ds@1952
   130
    }
ds@1952
   131
    case '-': {
ds@1952
   132
      if( z[1]=='-' ){
ds@1952
   133
        for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
ds@1952
   134
        *tokenType = TK_COMMENT;
ds@1952
   135
        return i;
ds@1952
   136
      }
ds@1952
   137
      *tokenType = TK_MINUS;
ds@1952
   138
      return 1;
ds@1952
   139
    }
ds@1952
   140
    case '(': {
ds@1952
   141
      *tokenType = TK_LP;
ds@1952
   142
      return 1;
ds@1952
   143
    }
ds@1952
   144
    case ')': {
ds@1952
   145
      *tokenType = TK_RP;
ds@1952
   146
      return 1;
ds@1952
   147
    }
ds@1952
   148
    case ';': {
ds@1952
   149
      *tokenType = TK_SEMI;
ds@1952
   150
      return 1;
ds@1952
   151
    }
ds@1952
   152
    case '+': {
ds@1952
   153
      *tokenType = TK_PLUS;
ds@1952
   154
      return 1;
ds@1952
   155
    }
ds@1952
   156
    case '*': {
ds@1952
   157
      *tokenType = TK_STAR;
ds@1952
   158
      return 1;
ds@1952
   159
    }
ds@1952
   160
    case '/': {
ds@1952
   161
      if( z[1]!='*' || z[2]==0 ){
ds@1952
   162
        *tokenType = TK_SLASH;
ds@1952
   163
        return 1;
ds@1952
   164
      }
ds@1952
   165
      for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
ds@1952
   166
      if( c ) i++;
ds@1952
   167
      *tokenType = TK_COMMENT;
ds@1952
   168
      return i;
ds@1952
   169
    }
ds@1952
   170
    case '%': {
ds@1952
   171
      *tokenType = TK_REM;
ds@1952
   172
      return 1;
ds@1952
   173
    }
ds@1952
   174
    case '=': {
ds@1952
   175
      *tokenType = TK_EQ;
ds@1952
   176
      return 1 + (z[1]=='=');
ds@1952
   177
    }
ds@1952
   178
    case '<': {
ds@1952
   179
      if( (c=z[1])=='=' ){
ds@1952
   180
        *tokenType = TK_LE;
ds@1952
   181
        return 2;
ds@1952
   182
      }else if( c=='>' ){
ds@1952
   183
        *tokenType = TK_NE;
ds@1952
   184
        return 2;
ds@1952
   185
      }else if( c=='<' ){
ds@1952
   186
        *tokenType = TK_LSHIFT;
ds@1952
   187
        return 2;
ds@1952
   188
      }else{
ds@1952
   189
        *tokenType = TK_LT;
ds@1952
   190
        return 1;
ds@1952
   191
      }
ds@1952
   192
    }
ds@1952
   193
    case '>': {
ds@1952
   194
      if( (c=z[1])=='=' ){
ds@1952
   195
        *tokenType = TK_GE;
ds@1952
   196
        return 2;
ds@1952
   197
      }else if( c=='>' ){
ds@1952
   198
        *tokenType = TK_RSHIFT;
ds@1952
   199
        return 2;
ds@1952
   200
      }else{
ds@1952
   201
        *tokenType = TK_GT;
ds@1952
   202
        return 1;
ds@1952
   203
      }
ds@1952
   204
    }
ds@1952
   205
    case '!': {
ds@1952
   206
      if( z[1]!='=' ){
ds@1952
   207
        *tokenType = TK_ILLEGAL;
ds@1952
   208
        return 2;
ds@1952
   209
      }else{
ds@1952
   210
        *tokenType = TK_NE;
ds@1952
   211
        return 2;
ds@1952
   212
      }
ds@1952
   213
    }
ds@1952
   214
    case '|': {
ds@1952
   215
      if( z[1]!='|' ){
ds@1952
   216
        *tokenType = TK_BITOR;
ds@1952
   217
        return 1;
ds@1952
   218
      }else{
ds@1952
   219
        *tokenType = TK_CONCAT;
ds@1952
   220
        return 2;
ds@1952
   221
      }
ds@1952
   222
    }
ds@1952
   223
    case ',': {
ds@1952
   224
      *tokenType = TK_COMMA;
ds@1952
   225
      return 1;
ds@1952
   226
    }
ds@1952
   227
    case '&': {
ds@1952
   228
      *tokenType = TK_BITAND;
ds@1952
   229
      return 1;
ds@1952
   230
    }
ds@1952
   231
    case '~': {
ds@1952
   232
      *tokenType = TK_BITNOT;
ds@1952
   233
      return 1;
ds@1952
   234
    }
ds@1952
   235
    case '`':
ds@1952
   236
    case '\'':
ds@1952
   237
    case '"': {
ds@1952
   238
      int delim = z[0];
ds@1952
   239
      for(i=1; (c=z[i])!=0; i++){
ds@1952
   240
        if( c==delim ){
ds@1952
   241
          if( z[i+1]==delim ){
ds@1952
   242
            i++;
ds@1952
   243
          }else{
ds@1952
   244
            break;
ds@1952
   245
          }
ds@1952
   246
        }
ds@1952
   247
      }
ds@1952
   248
      if( c ){
ds@1952
   249
        *tokenType = TK_STRING;
ds@1952
   250
        return i+1;
ds@1952
   251
      }else{
ds@1952
   252
        *tokenType = TK_ILLEGAL;
ds@1952
   253
        return i;
ds@1952
   254
      }
ds@1952
   255
    }
ds@1952
   256
    case '.': {
ds@1952
   257
#ifndef SQLITE_OMIT_FLOATING_POINT
ds@1952
   258
      if( !isdigit(z[1]) )
ds@1952
   259
#endif
ds@1952
   260
      {
ds@1952
   261
        *tokenType = TK_DOT;
ds@1952
   262
        return 1;
ds@1952
   263
      }
ds@1952
   264
      /* If the next character is a digit, this is a floating point
ds@1952
   265
      ** number that begins with ".".  Fall thru into the next case */
ds@1952
   266
    }
ds@1952
   267
    case '0': case '1': case '2': case '3': case '4':
ds@1952
   268
    case '5': case '6': case '7': case '8': case '9': {
ds@1952
   269
      *tokenType = TK_INTEGER;
ds@1952
   270
      for(i=0; isdigit(z[i]); i++){}
ds@1952
   271
#ifndef SQLITE_OMIT_FLOATING_POINT
ds@1952
   272
      if( z[i]=='.' ){
ds@1952
   273
        i++;
ds@1952
   274
        while( isdigit(z[i]) ){ i++; }
ds@1952
   275
        *tokenType = TK_FLOAT;
ds@1952
   276
      }
ds@1952
   277
      if( (z[i]=='e' || z[i]=='E') &&
ds@1952
   278
           ( isdigit(z[i+1]) 
ds@1952
   279
            || ((z[i+1]=='+' || z[i+1]=='-') && isdigit(z[i+2]))
ds@1952
   280
           )
ds@1952
   281
      ){
ds@1952
   282
        i += 2;
ds@1952
   283
        while( isdigit(z[i]) ){ i++; }
ds@1952
   284
        *tokenType = TK_FLOAT;
ds@1952
   285
      }
ds@1952
   286
#endif
ds@1952
   287
      while( IdChar(z[i]) ){
ds@1952
   288
        *tokenType = TK_ILLEGAL;
ds@1952
   289
        i++;
ds@1952
   290
      }
ds@1952
   291
      return i;
ds@1952
   292
    }
ds@1952
   293
    case '[': {
ds@1952
   294
      for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
ds@1952
   295
      *tokenType = TK_ID;
ds@1952
   296
      return i;
ds@1952
   297
    }
ds@1952
   298
    case '?': {
ds@1952
   299
      *tokenType = TK_VARIABLE;
ds@1952
   300
      for(i=1; isdigit(z[i]); i++){}
ds@1952
   301
      return i;
ds@1952
   302
    }
ds@1952
   303
    case '#': {
ds@1952
   304
      for(i=1; isdigit(z[i]); i++){}
ds@1952
   305
      if( i>1 ){
ds@1952
   306
        /* Parameters of the form #NNN (where NNN is a number) are used
ds@1952
   307
        ** internally by sqlite3NestedParse.  */
ds@1952
   308
        *tokenType = TK_REGISTER;
ds@1952
   309
        return i;
ds@1952
   310
      }
ds@1952
   311
      /* Fall through into the next case if the '#' is not followed by
ds@1952
   312
      ** a digit. Try to match #AAAA where AAAA is a parameter name. */
ds@1952
   313
    }
ds@1952
   314
#ifndef SQLITE_OMIT_TCL_VARIABLE
ds@1952
   315
    case '$':
ds@1952
   316
#endif
ds@1952
   317
    case '@':  /* For compatibility with MS SQL Server */
ds@1952
   318
    case ':': {
ds@1952
   319
      int n = 0;
ds@1952
   320
      *tokenType = TK_VARIABLE;
ds@1952
   321
      for(i=1; (c=z[i])!=0; i++){
ds@1952
   322
        if( IdChar(c) ){
ds@1952
   323
          n++;
ds@1952
   324
#ifndef SQLITE_OMIT_TCL_VARIABLE
ds@1952
   325
        }else if( c=='(' && n>0 ){
ds@1952
   326
          do{
ds@1952
   327
            i++;
ds@1952
   328
          }while( (c=z[i])!=0 && !isspace(c) && c!=')' );
ds@1952
   329
          if( c==')' ){
ds@1952
   330
            i++;
ds@1952
   331
          }else{
ds@1952
   332
            *tokenType = TK_ILLEGAL;
ds@1952
   333
          }
ds@1952
   334
          break;
ds@1952
   335
        }else if( c==':' && z[i+1]==':' ){
ds@1952
   336
          i++;
ds@1952
   337
#endif
ds@1952
   338
        }else{
ds@1952
   339
          break;
ds@1952
   340
        }
ds@1952
   341
      }
ds@1952
   342
      if( n==0 ) *tokenType = TK_ILLEGAL;
ds@1952
   343
      return i;
ds@1952
   344
    }
ds@1952
   345
#ifndef SQLITE_OMIT_BLOB_LITERAL
ds@1952
   346
    case 'x': case 'X': {
ds@1952
   347
      if( (c=z[1])=='\'' || c=='"' ){
ds@1952
   348
        int delim = c;
ds@1952
   349
        *tokenType = TK_BLOB;
ds@1952
   350
        for(i=2; (c=z[i])!=0; i++){
ds@1952
   351
          if( c==delim ){
ds@1952
   352
            if( i%2 ) *tokenType = TK_ILLEGAL;
ds@1952
   353
            break;
ds@1952
   354
          }
ds@1952
   355
          if( !isxdigit(c) ){
ds@1952
   356
            *tokenType = TK_ILLEGAL;
ds@1952
   357
            return i;
ds@1952
   358
          }
ds@1952
   359
        }
ds@1952
   360
        if( c ) i++;
ds@1952
   361
        return i;
ds@1952
   362
      }
ds@1952
   363
      /* Otherwise fall through to the next case */
ds@1952
   364
    }
ds@1952
   365
#endif
ds@1952
   366
    default: {
ds@1952
   367
      if( !IdChar(*z) ){
ds@1952
   368
        break;
ds@1952
   369
      }
ds@1952
   370
      for(i=1; IdChar(z[i]); i++){}
ds@1952
   371
      *tokenType = keywordCode((char*)z, i);
ds@1952
   372
      return i;
ds@1952
   373
    }
ds@1952
   374
  }
ds@1952
   375
  *tokenType = TK_ILLEGAL;
ds@1952
   376
  return 1;
ds@1952
   377
}
ds@1952
   378
int sqlite3GetToken(const unsigned char *z, int *tokenType){
ds@1952
   379
  return getToken(z, tokenType);
ds@1952
   380
}
ds@1952
   381
ds@1952
   382
/*
ds@1952
   383
** Run the parser on the given SQL string.  The parser structure is
ds@1952
   384
** passed in.  An SQLITE_ status code is returned.  If an error occurs
ds@1952
   385
** and pzErrMsg!=NULL then an error message might be written into 
ds@1952
   386
** memory obtained from sqlite3_malloc() and *pzErrMsg made to point to that
ds@1952
   387
** error message.  Or maybe not.
ds@1952
   388
*/
ds@1952
   389
int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
ds@1952
   390
  int nErr = 0;
ds@1952
   391
  int i;
ds@1952
   392
  void *pEngine;
ds@1952
   393
  int tokenType;
ds@1952
   394
  int lastTokenParsed = -1;
ds@1952
   395
  sqlite3 *db = pParse->db;
ds@1952
   396
ds@1952
   397
  if( db->activeVdbeCnt==0 ){
ds@1952
   398
    db->u1.isInterrupted = 0;
ds@1952
   399
  }
ds@1952
   400
  pParse->rc = SQLITE_OK;
ds@1952
   401
  i = 0;
ds@1952
   402
  pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3_malloc);
ds@1952
   403
  if( pEngine==0 ){
ds@1952
   404
    db->mallocFailed = 1;
ds@1952
   405
    return SQLITE_NOMEM;
ds@1952
   406
  }
ds@1952
   407
  assert( pParse->sLastToken.dyn==0 );
ds@1952
   408
  assert( pParse->pNewTable==0 );
ds@1952
   409
  assert( pParse->pNewTrigger==0 );
ds@1952
   410
  assert( pParse->nVar==0 );
ds@1952
   411
  assert( pParse->nVarExpr==0 );
ds@1952
   412
  assert( pParse->nVarExprAlloc==0 );
ds@1952
   413
  assert( pParse->apVarExpr==0 );
ds@1952
   414
  pParse->zTail = pParse->zSql = zSql;
ds@1952
   415
  while( !db->mallocFailed && zSql[i]!=0 ){
ds@1952
   416
    assert( i>=0 );
ds@1952
   417
    pParse->sLastToken.z = (u8*)&zSql[i];
ds@1952
   418
    assert( pParse->sLastToken.dyn==0 );
ds@1952
   419
    pParse->sLastToken.n = getToken((unsigned char*)&zSql[i],&tokenType);
ds@1952
   420
    i += pParse->sLastToken.n;
ds@1952
   421
    if( i>SQLITE_MAX_SQL_LENGTH ){
ds@1952
   422
      pParse->rc = SQLITE_TOOBIG;
ds@1952
   423
      break;
ds@1952
   424
    }
ds@1952
   425
    switch( tokenType ){
ds@1952
   426
      case TK_SPACE:
ds@1952
   427
      case TK_COMMENT: {
ds@1952
   428
        if( db->u1.isInterrupted ){
ds@1952
   429
          pParse->rc = SQLITE_INTERRUPT;
ds@1952
   430
          sqlite3SetString(pzErrMsg, "interrupt", (char*)0);
ds@1952
   431
          goto abort_parse;
ds@1952
   432
        }
ds@1952
   433
        break;
ds@1952
   434
      }
ds@1952
   435
      case TK_ILLEGAL: {
ds@1952
   436
        if( pzErrMsg ){
ds@1952
   437
          sqlite3_free(*pzErrMsg);
ds@1952
   438
          *pzErrMsg = sqlite3MPrintf(db, "unrecognized token: \"%T\"",
ds@1952
   439
                          &pParse->sLastToken);
ds@1952
   440
        }
ds@1952
   441
        nErr++;
ds@1952
   442
        goto abort_parse;
ds@1952
   443
      }
ds@1952
   444
      case TK_SEMI: {
ds@1952
   445
        pParse->zTail = &zSql[i];
ds@1952
   446
        /* Fall thru into the default case */
ds@1952
   447
      }
ds@1952
   448
      default: {
ds@1952
   449
        sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
ds@1952
   450
        lastTokenParsed = tokenType;
ds@1952
   451
        if( pParse->rc!=SQLITE_OK ){
ds@1952
   452
          goto abort_parse;
ds@1952
   453
        }
ds@1952
   454
        break;
ds@1952
   455
      }
ds@1952
   456
    }
ds@1952
   457
  }
ds@1952
   458
abort_parse:
ds@1952
   459
  if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
ds@1952
   460
    if( lastTokenParsed!=TK_SEMI ){
ds@1952
   461
      sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
ds@1952
   462
      pParse->zTail = &zSql[i];
ds@1952
   463
    }
ds@1952
   464
    sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
ds@1952
   465
  }
ds@1952
   466
  sqlite3ParserFree(pEngine, sqlite3_free);
ds@1952
   467
  if( db->mallocFailed ){
ds@1952
   468
    pParse->rc = SQLITE_NOMEM;
ds@1952
   469
  }
ds@1952
   470
  if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
ds@1952
   471
    sqlite3SetString(&pParse->zErrMsg, sqlite3ErrStr(pParse->rc), (char*)0);
ds@1952
   472
  }
ds@1952
   473
  if( pParse->zErrMsg ){
ds@1952
   474
    if( pzErrMsg && *pzErrMsg==0 ){
ds@1952
   475
      *pzErrMsg = pParse->zErrMsg;
ds@1952
   476
    }else{
ds@1952
   477
      sqlite3_free(pParse->zErrMsg);
ds@1952
   478
    }
ds@1952
   479
    pParse->zErrMsg = 0;
ds@1952
   480
    if( !nErr ) nErr++;
ds@1952
   481
  }
ds@1952
   482
  if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
ds@1952
   483
    sqlite3VdbeDelete(pParse->pVdbe);
ds@1952
   484
    pParse->pVdbe = 0;
ds@1952
   485
  }
ds@1952
   486
#ifndef SQLITE_OMIT_SHARED_CACHE
ds@1952
   487
  if( pParse->nested==0 ){
ds@1952
   488
    sqlite3_free(pParse->aTableLock);
ds@1952
   489
    pParse->aTableLock = 0;
ds@1952
   490
    pParse->nTableLock = 0;
ds@1952
   491
  }
ds@1952
   492
#endif
ds@1952
   493
ds@1952
   494
  if( !IN_DECLARE_VTAB ){
ds@1952
   495
    /* If the pParse->declareVtab flag is set, do not delete any table 
ds@1952
   496
    ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
ds@1952
   497
    ** will take responsibility for freeing the Table structure.
ds@1952
   498
    */
ds@1952
   499
    sqlite3DeleteTable(pParse->pNewTable);
ds@1952
   500
  }
ds@1952
   501
ds@1952
   502
  sqlite3DeleteTrigger(pParse->pNewTrigger);
ds@1952
   503
  sqlite3_free(pParse->apVarExpr);
ds@1952
   504
  if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){
ds@1952
   505
    pParse->rc = SQLITE_ERROR;
ds@1952
   506
  }
ds@1952
   507
  return nErr;
ds@1952
   508
}