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*8e2ca029Sdrh ** $Id: tokenize.c,v 1.46 2002/06/17 17:07:20 drh Exp $ 1975897234Sdrh */ 2075897234Sdrh #include "sqliteInt.h" 21ad75e987Sdrh #include "os.h" 2275897234Sdrh #include <ctype.h> 23dcc581ccSdrh #include <stdlib.h> 2475897234Sdrh 2575897234Sdrh /* 2675897234Sdrh ** All the keywords of the SQL language are stored as in a hash 2775897234Sdrh ** table composed of instances of the following structure. 2875897234Sdrh */ 2975897234Sdrh typedef struct Keyword Keyword; 3075897234Sdrh struct Keyword { 3175897234Sdrh char *zName; /* The keyword name */ 3275897234Sdrh int len; /* Number of characters in the keyword */ 3375897234Sdrh int tokenType; /* The token value for this keyword */ 3475897234Sdrh Keyword *pNext; /* Next keyword with the same hash */ 3575897234Sdrh }; 3675897234Sdrh 3775897234Sdrh /* 3875897234Sdrh ** These are the keywords 3975897234Sdrh */ 4075897234Sdrh static Keyword aKeywordTable[] = { 419cfcf5d4Sdrh { "ABORT", 0, TK_ABORT, 0 }, 42c3f9bad2Sdanielk1977 { "AFTER", 0, TK_AFTER, 0 }, 43fef5208cSdrh { "ALL", 0, TK_ALL, 0 }, 4475897234Sdrh { "AND", 0, TK_AND, 0 }, 4575897234Sdrh { "AS", 0, TK_AS, 0 }, 4675897234Sdrh { "ASC", 0, TK_ASC, 0 }, 47c3f9bad2Sdanielk1977 { "BEFORE", 0, TK_BEFORE, 0 }, 48c4a3c779Sdrh { "BEGIN", 0, TK_BEGIN, 0 }, 49fef5208cSdrh { "BETWEEN", 0, TK_BETWEEN, 0 }, 5075897234Sdrh { "BY", 0, TK_BY, 0 }, 5104738cb9Sdrh { "CASCADE", 0, TK_CASCADE, 0 }, 5217a7f8ddSdrh { "CASE", 0, TK_CASE, 0 }, 5375897234Sdrh { "CHECK", 0, TK_CHECK, 0 }, 54f57b14a6Sdrh { "CLUSTER", 0, TK_CLUSTER, 0 }, 55*8e2ca029Sdrh { "COLLATE", 0, TK_COLLATE, 0 }, 56c4a3c779Sdrh { "COMMIT", 0, TK_COMMIT, 0 }, 579cfcf5d4Sdrh { "CONFLICT", 0, TK_CONFLICT, 0 }, 5875897234Sdrh { "CONSTRAINT", 0, TK_CONSTRAINT, 0 }, 59982cef7eSdrh { "COPY", 0, TK_COPY, 0 }, 6075897234Sdrh { "CREATE", 0, TK_CREATE, 0 }, 6175897234Sdrh { "DEFAULT", 0, TK_DEFAULT, 0 }, 6204738cb9Sdrh { "DEFERRED", 0, TK_DEFERRED, 0 }, 6304738cb9Sdrh { "DEFERRABLE", 0, TK_DEFERRABLE, 0 }, 6475897234Sdrh { "DELETE", 0, TK_DELETE, 0 }, 65982cef7eSdrh { "DELIMITERS", 0, TK_DELIMITERS, 0 }, 6675897234Sdrh { "DESC", 0, TK_DESC, 0 }, 67efb7251dSdrh { "DISTINCT", 0, TK_DISTINCT, 0 }, 6875897234Sdrh { "DROP", 0, TK_DROP, 0 }, 69c4a3c779Sdrh { "END", 0, TK_END, 0 }, 70c3f9bad2Sdanielk1977 { "EACH", 0, TK_EACH, 0 }, 7117a7f8ddSdrh { "ELSE", 0, TK_ELSE, 0 }, 7282c3d636Sdrh { "EXCEPT", 0, TK_EXCEPT, 0 }, 7375897234Sdrh { "EXPLAIN", 0, TK_EXPLAIN, 0 }, 741c92853dSdrh { "FAIL", 0, TK_FAIL, 0 }, 75c3f9bad2Sdanielk1977 { "FOR", 0, TK_FOR, 0 }, 7604738cb9Sdrh { "FOREIGN", 0, TK_FOREIGN, 0 }, 7775897234Sdrh { "FROM", 0, TK_FROM, 0 }, 7804738cb9Sdrh { "FULL", 0, TK_FULL, 0 }, 79dce2cbe6Sdrh { "GLOB", 0, TK_GLOB, 0 }, 802282792aSdrh { "GROUP", 0, TK_GROUP, 0 }, 812282792aSdrh { "HAVING", 0, TK_HAVING, 0 }, 829cfcf5d4Sdrh { "IGNORE", 0, TK_IGNORE, 0 }, 8304738cb9Sdrh { "IMMEDIATE", 0, TK_IMMEDIATE, 0 }, 84fef5208cSdrh { "IN", 0, TK_IN, 0 }, 8575897234Sdrh { "INDEX", 0, TK_INDEX, 0 }, 8604738cb9Sdrh { "INITIALLY", 0, TK_INITIALLY, 0 }, 8775897234Sdrh { "INSERT", 0, TK_INSERT, 0 }, 88c3f9bad2Sdanielk1977 { "INSTEAD", 0, TK_INSTEAD, 0 }, 8982c3d636Sdrh { "INTERSECT", 0, TK_INTERSECT, 0 }, 9075897234Sdrh { "INTO", 0, TK_INTO, 0 }, 9175897234Sdrh { "IS", 0, TK_IS, 0 }, 9275897234Sdrh { "ISNULL", 0, TK_ISNULL, 0 }, 9301f3f253Sdrh { "JOIN", 0, TK_JOIN, 0 }, 9475897234Sdrh { "KEY", 0, TK_KEY, 0 }, 95dce2cbe6Sdrh { "LIKE", 0, TK_LIKE, 0 }, 969bbca4c1Sdrh { "LIMIT", 0, TK_LIMIT, 0 }, 9704738cb9Sdrh { "MATCH", 0, TK_MATCH, 0 }, 9875897234Sdrh { "NOT", 0, TK_NOT, 0 }, 9975897234Sdrh { "NOTNULL", 0, TK_NOTNULL, 0 }, 10075897234Sdrh { "NULL", 0, TK_NULL, 0 }, 101c3f9bad2Sdanielk1977 { "OF", 0, TK_OF, 0 }, 1029bbca4c1Sdrh { "OFFSET", 0, TK_OFFSET, 0 }, 10375897234Sdrh { "ON", 0, TK_ON, 0 }, 10475897234Sdrh { "OR", 0, TK_OR, 0 }, 10575897234Sdrh { "ORDER", 0, TK_ORDER, 0 }, 10604738cb9Sdrh { "PARTIAL", 0, TK_PARTIAL, 0 }, 107f57b14a6Sdrh { "PRAGMA", 0, TK_PRAGMA, 0 }, 10875897234Sdrh { "PRIMARY", 0, TK_PRIMARY, 0 }, 1096f34903eSdanielk1977 { "RAISE", 0, TK_RAISE, 0 }, 11004738cb9Sdrh { "REFERENCES", 0, TK_REFERENCES, 0 }, 1119cfcf5d4Sdrh { "REPLACE", 0, TK_REPLACE, 0 }, 11204738cb9Sdrh { "RESTRICT", 0, TK_RESTRICT, 0 }, 113c4a3c779Sdrh { "ROLLBACK", 0, TK_ROLLBACK, 0 }, 114c3f9bad2Sdanielk1977 { "ROW", 0, TK_ROW, 0 }, 11575897234Sdrh { "SELECT", 0, TK_SELECT, 0 }, 11675897234Sdrh { "SET", 0, TK_SET, 0 }, 1171873cd50Sdrh { "STATEMENT", 0, TK_STATEMENT, 0 }, 11875897234Sdrh { "TABLE", 0, TK_TABLE, 0 }, 119f57b3399Sdrh { "TEMP", 0, TK_TEMP, 0 }, 120f57b3399Sdrh { "TEMPORARY", 0, TK_TEMP, 0 }, 12117a7f8ddSdrh { "THEN", 0, TK_THEN, 0 }, 122c4a3c779Sdrh { "TRANSACTION", 0, TK_TRANSACTION, 0 }, 123c3f9bad2Sdanielk1977 { "TRIGGER", 0, TK_TRIGGER, 0 }, 12482c3d636Sdrh { "UNION", 0, TK_UNION, 0 }, 12575897234Sdrh { "UNIQUE", 0, TK_UNIQUE, 0 }, 12675897234Sdrh { "UPDATE", 0, TK_UPDATE, 0 }, 127982cef7eSdrh { "USING", 0, TK_USING, 0 }, 128dce2cbe6Sdrh { "VACUUM", 0, TK_VACUUM, 0 }, 12975897234Sdrh { "VALUES", 0, TK_VALUES, 0 }, 130a76b5dfcSdrh { "VIEW", 0, TK_VIEW, 0 }, 13117a7f8ddSdrh { "WHEN", 0, TK_WHEN, 0 }, 13275897234Sdrh { "WHERE", 0, TK_WHERE, 0 }, 13375897234Sdrh }; 13475897234Sdrh 13575897234Sdrh /* 13675897234Sdrh ** This is the hash table 13775897234Sdrh */ 138daffd0e5Sdrh #define KEY_HASH_SIZE 71 13975897234Sdrh static Keyword *apHashTable[KEY_HASH_SIZE]; 14075897234Sdrh 14175897234Sdrh 14275897234Sdrh /* 14375897234Sdrh ** This function looks up an identifier to determine if it is a 14475897234Sdrh ** keyword. If it is a keyword, the token code of that keyword is 14575897234Sdrh ** returned. If the input is not a keyword, TK_ID is returned. 14675897234Sdrh */ 14717f71934Sdrh int sqliteKeywordCode(const char *z, int n){ 14875897234Sdrh int h; 14975897234Sdrh Keyword *p; 15075897234Sdrh if( aKeywordTable[0].len==0 ){ 15175897234Sdrh /* Initialize the keyword hash table */ 152ad75e987Sdrh sqliteOsEnterMutex(); 153ad75e987Sdrh if( aKeywordTable[0].len==0 ){ 15475897234Sdrh int i; 15575897234Sdrh int n; 15675897234Sdrh n = sizeof(aKeywordTable)/sizeof(aKeywordTable[0]); 15775897234Sdrh for(i=0; i<n; i++){ 15875897234Sdrh aKeywordTable[i].len = strlen(aKeywordTable[i].zName); 15975897234Sdrh h = sqliteHashNoCase(aKeywordTable[i].zName, aKeywordTable[i].len); 16075897234Sdrh h %= KEY_HASH_SIZE; 16175897234Sdrh aKeywordTable[i].pNext = apHashTable[h]; 16275897234Sdrh apHashTable[h] = &aKeywordTable[i]; 16375897234Sdrh } 16475897234Sdrh } 165ad75e987Sdrh sqliteOsLeaveMutex(); 166ad75e987Sdrh } 16775897234Sdrh h = sqliteHashNoCase(z, n) % KEY_HASH_SIZE; 16875897234Sdrh for(p=apHashTable[h]; p; p=p->pNext){ 16975897234Sdrh if( p->len==n && sqliteStrNICmp(p->zName, z, n)==0 ){ 17075897234Sdrh return p->tokenType; 17175897234Sdrh } 17275897234Sdrh } 17375897234Sdrh return TK_ID; 17475897234Sdrh } 17575897234Sdrh 17698808babSdrh 17798808babSdrh /* 17898808babSdrh ** If X is a character that can be used in an identifier then 17998808babSdrh ** isIdChar[X] will be 1. Otherwise isIdChar[X] will be 0. 18098808babSdrh ** 18198808babSdrh ** In this implementation, an identifier can be a string of 18298808babSdrh ** alphabetic characters, digits, and "_" plus any character 18398808babSdrh ** with the high-order bit set. The latter rule means that 18498808babSdrh ** any sequence of UTF-8 characters or characters taken from 18598808babSdrh ** an extended ISO8859 character set can form an identifier. 18698808babSdrh */ 18798808babSdrh static const char isIdChar[] = { 18898808babSdrh /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ 18998808babSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */ 19098808babSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */ 19198808babSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */ 19298808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */ 19398808babSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */ 19498808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */ 19598808babSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */ 19698808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */ 19798808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 8x */ 19898808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 9x */ 19998808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Ax */ 20098808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Bx */ 20198808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Cx */ 20298808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Dx */ 20398808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Ex */ 20498808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Fx */ 20598808babSdrh }; 20698808babSdrh 20798808babSdrh 20875897234Sdrh /* 20975897234Sdrh ** Return the length of the token that begins at z[0]. Return 21075897234Sdrh ** -1 if the token is (or might be) incomplete. Store the token 21175897234Sdrh ** type in *tokenType before returning. 21275897234Sdrh */ 21398808babSdrh static int sqliteGetToken(const unsigned char *z, int *tokenType){ 21475897234Sdrh int i; 21575897234Sdrh switch( *z ){ 21630cab809Sdrh case ' ': case '\t': case '\n': case '\f': case '\r': { 21775897234Sdrh for(i=1; z[i] && isspace(z[i]); i++){} 21875897234Sdrh *tokenType = TK_SPACE; 21975897234Sdrh return i; 22075897234Sdrh } 22175897234Sdrh case '-': { 22275897234Sdrh if( z[1]==0 ) return -1; 22375897234Sdrh if( z[1]=='-' ){ 22475897234Sdrh for(i=2; z[i] && z[i]!='\n'; i++){} 22575897234Sdrh *tokenType = TK_COMMENT; 22675897234Sdrh return i; 22775897234Sdrh } 22875897234Sdrh *tokenType = TK_MINUS; 22975897234Sdrh return 1; 23075897234Sdrh } 23175897234Sdrh case '(': { 23275897234Sdrh *tokenType = TK_LP; 23375897234Sdrh return 1; 23475897234Sdrh } 23575897234Sdrh case ')': { 23675897234Sdrh *tokenType = TK_RP; 23775897234Sdrh return 1; 23875897234Sdrh } 23975897234Sdrh case ';': { 24075897234Sdrh *tokenType = TK_SEMI; 24175897234Sdrh return 1; 24275897234Sdrh } 24375897234Sdrh case '+': { 24475897234Sdrh *tokenType = TK_PLUS; 24575897234Sdrh return 1; 24675897234Sdrh } 24775897234Sdrh case '*': { 24875897234Sdrh *tokenType = TK_STAR; 24975897234Sdrh return 1; 25075897234Sdrh } 25175897234Sdrh case '/': { 25275897234Sdrh *tokenType = TK_SLASH; 25375897234Sdrh return 1; 25475897234Sdrh } 255bf4133cbSdrh case '%': { 256bf4133cbSdrh *tokenType = TK_REM; 257bf4133cbSdrh return 1; 258bf4133cbSdrh } 25975897234Sdrh case '=': { 26075897234Sdrh *tokenType = TK_EQ; 26175897234Sdrh return 1 + (z[1]=='='); 26275897234Sdrh } 26375897234Sdrh case '<': { 26475897234Sdrh if( z[1]=='=' ){ 26575897234Sdrh *tokenType = TK_LE; 26675897234Sdrh return 2; 26775897234Sdrh }else if( z[1]=='>' ){ 26875897234Sdrh *tokenType = TK_NE; 26975897234Sdrh return 2; 270bf4133cbSdrh }else if( z[1]=='<' ){ 271bf4133cbSdrh *tokenType = TK_LSHIFT; 272bf4133cbSdrh return 2; 27375897234Sdrh }else{ 27475897234Sdrh *tokenType = TK_LT; 27575897234Sdrh return 1; 27675897234Sdrh } 27775897234Sdrh } 27875897234Sdrh case '>': { 27975897234Sdrh if( z[1]=='=' ){ 28075897234Sdrh *tokenType = TK_GE; 28175897234Sdrh return 2; 282bf4133cbSdrh }else if( z[1]=='>' ){ 283bf4133cbSdrh *tokenType = TK_RSHIFT; 284bf4133cbSdrh return 2; 28575897234Sdrh }else{ 28675897234Sdrh *tokenType = TK_GT; 28775897234Sdrh return 1; 28875897234Sdrh } 28975897234Sdrh } 29075897234Sdrh case '!': { 29175897234Sdrh if( z[1]!='=' ){ 29275897234Sdrh *tokenType = TK_ILLEGAL; 293c837e709Sdrh return 2; 29475897234Sdrh }else{ 29575897234Sdrh *tokenType = TK_NE; 29675897234Sdrh return 2; 29775897234Sdrh } 29875897234Sdrh } 2990040077dSdrh case '|': { 3000040077dSdrh if( z[1]!='|' ){ 301bf4133cbSdrh *tokenType = TK_BITOR; 3020040077dSdrh return 1; 3030040077dSdrh }else{ 3040040077dSdrh *tokenType = TK_CONCAT; 3050040077dSdrh return 2; 3060040077dSdrh } 3070040077dSdrh } 30875897234Sdrh case ',': { 30975897234Sdrh *tokenType = TK_COMMA; 31075897234Sdrh return 1; 31175897234Sdrh } 312bf4133cbSdrh case '&': { 313bf4133cbSdrh *tokenType = TK_BITAND; 314bf4133cbSdrh return 1; 315bf4133cbSdrh } 316bf4133cbSdrh case '~': { 317bf4133cbSdrh *tokenType = TK_BITNOT; 318bf4133cbSdrh return 1; 319bf4133cbSdrh } 32075897234Sdrh case '\'': case '"': { 32175897234Sdrh int delim = z[0]; 32275897234Sdrh for(i=1; z[i]; i++){ 32375897234Sdrh if( z[i]==delim ){ 32475897234Sdrh if( z[i+1]==delim ){ 32575897234Sdrh i++; 32675897234Sdrh }else{ 32775897234Sdrh break; 32875897234Sdrh } 32975897234Sdrh } 33075897234Sdrh } 33175897234Sdrh if( z[i] ) i++; 33275897234Sdrh *tokenType = TK_STRING; 33375897234Sdrh return i; 33475897234Sdrh } 33575897234Sdrh case '.': { 33675897234Sdrh if( !isdigit(z[1]) ){ 33775897234Sdrh *tokenType = TK_DOT; 33875897234Sdrh return 1; 33975897234Sdrh } 34075897234Sdrh /* Fall thru into the next case */ 34175897234Sdrh } 34275897234Sdrh case '0': case '1': case '2': case '3': case '4': 34375897234Sdrh case '5': case '6': case '7': case '8': case '9': { 344c837e709Sdrh *tokenType = TK_INTEGER; 34575897234Sdrh for(i=1; z[i] && isdigit(z[i]); i++){} 34675897234Sdrh if( z[i]=='.' ){ 34775897234Sdrh i++; 34875897234Sdrh while( z[i] && isdigit(z[i]) ){ i++; } 349c837e709Sdrh *tokenType = TK_FLOAT; 350c837e709Sdrh } 35175897234Sdrh if( (z[i]=='e' || z[i]=='E') && 35275897234Sdrh ( isdigit(z[i+1]) 35375897234Sdrh || ((z[i+1]=='+' || z[i+1]=='-') && isdigit(z[i+2])) 35475897234Sdrh ) 35575897234Sdrh ){ 35675897234Sdrh i += 2; 35775897234Sdrh while( z[i] && isdigit(z[i]) ){ i++; } 35875897234Sdrh *tokenType = TK_FLOAT; 35975897234Sdrh }else if( z[0]=='.' ){ 36075897234Sdrh *tokenType = TK_FLOAT; 36175897234Sdrh } 36275897234Sdrh return i; 36375897234Sdrh } 3642f4392ffSdrh case '[': { 3652f4392ffSdrh for(i=1; z[i] && z[i-1]!=']'; i++){} 3662f4392ffSdrh *tokenType = TK_ID; 3672f4392ffSdrh return i; 3682f4392ffSdrh } 36998808babSdrh default: { 37098808babSdrh if( !isIdChar[*z] ){ 37198808babSdrh break; 37298808babSdrh } 37398808babSdrh for(i=1; isIdChar[z[i]]; i++){} 3746a535340Sdrh *tokenType = sqliteKeywordCode((char*)z, i); 37575897234Sdrh return i; 37675897234Sdrh } 37775897234Sdrh } 37875897234Sdrh *tokenType = TK_ILLEGAL; 37975897234Sdrh return 1; 38075897234Sdrh } 38175897234Sdrh 38275897234Sdrh /* 38375897234Sdrh ** Run the parser on the given SQL string. The parser structure is 384b19a2bc6Sdrh ** passed in. An SQLITE_ status code is returned. If an error occurs 385b19a2bc6Sdrh ** and pzErrMsg!=NULL then an error message might be written into 386b19a2bc6Sdrh ** memory obtained from malloc() and *pzErrMsg made to point to that 387b19a2bc6Sdrh ** error message. Or maybe not. 38875897234Sdrh */ 38980ff32f5Sdrh int sqliteRunParser(Parse *pParse, const char *zSql, char **pzErrMsg){ 39075897234Sdrh int nErr = 0; 39175897234Sdrh int i; 39275897234Sdrh void *pEngine; 39375897234Sdrh int once = 1; 3946d4abfbeSdrh sqlite *db = pParse->db; 39575897234Sdrh extern void *sqliteParserAlloc(void*(*)(int)); 39675897234Sdrh extern void sqliteParserFree(void*, void(*)(void*)); 397338ea13cSdrh extern int sqliteParser(void*, int, Token, Parse*); 39875897234Sdrh 3996d4abfbeSdrh db->flags &= ~SQLITE_Interrupt; 4004c504391Sdrh pParse->rc = SQLITE_OK; 40175897234Sdrh i = 0; 402982cef7eSdrh pEngine = sqliteParserAlloc((void*(*)(int))malloc); 40375897234Sdrh if( pEngine==0 ){ 40475897234Sdrh sqliteSetString(pzErrMsg, "out of memory", 0); 40575897234Sdrh return 1; 40675897234Sdrh } 407daffd0e5Sdrh while( sqlite_malloc_failed==0 && nErr==0 && i>=0 && zSql[i]!=0 ){ 40875897234Sdrh int tokenType; 40975897234Sdrh 4106d4abfbeSdrh if( (db->flags & SQLITE_Interrupt)!=0 ){ 4114c504391Sdrh pParse->rc = SQLITE_INTERRUPT; 4124c504391Sdrh sqliteSetString(pzErrMsg, "interrupt", 0); 4134c504391Sdrh break; 4144c504391Sdrh } 41575897234Sdrh pParse->sLastToken.z = &zSql[i]; 41698808babSdrh pParse->sLastToken.n = sqliteGetToken((unsigned char*)&zSql[i], &tokenType); 41775897234Sdrh i += pParse->sLastToken.n; 41875897234Sdrh if( once ){ 41975897234Sdrh pParse->sFirstToken = pParse->sLastToken; 42075897234Sdrh once = 0; 42175897234Sdrh } 42275897234Sdrh switch( tokenType ){ 42375897234Sdrh case TK_SPACE: 42475897234Sdrh case TK_COMMENT: { 42575897234Sdrh break; 42675897234Sdrh } 42775897234Sdrh case TK_ILLEGAL: 428c837e709Sdrh sqliteSetNString(pzErrMsg, "unrecognized token: \"", -1, 429c837e709Sdrh pParse->sLastToken.z, pParse->sLastToken.n, "\"", 1, 0); 43075897234Sdrh nErr++; 43175897234Sdrh break; 43275897234Sdrh default: 43375897234Sdrh sqliteParser(pEngine, tokenType, pParse->sLastToken, pParse); 4346e142f54Sdrh if( pParse->zErrMsg && pParse->sErrToken.z ){ 43575897234Sdrh sqliteSetNString(pzErrMsg, "near \"", -1, 43675897234Sdrh pParse->sErrToken.z, pParse->sErrToken.n, 43775897234Sdrh "\": ", -1, 43875897234Sdrh pParse->zErrMsg, -1, 43975897234Sdrh 0); 44075897234Sdrh nErr++; 4416e142f54Sdrh sqliteFree(pParse->zErrMsg); 4426e142f54Sdrh pParse->zErrMsg = 0; 443ecdc7530Sdrh }else if( pParse->rc!=SQLITE_OK ){ 4446d4abfbeSdrh sqliteSetString(pzErrMsg, sqlite_error_string(pParse->rc), 0); 445ecdc7530Sdrh nErr++; 44675897234Sdrh } 44775897234Sdrh break; 44875897234Sdrh } 44975897234Sdrh } 450a76b5dfcSdrh if( zSql[i]==0 ){ 451094b2bbfSdrh sqliteParser(pEngine, TK_SEMI, pParse->sLastToken, pParse); 45275897234Sdrh sqliteParser(pEngine, 0, pParse->sLastToken, pParse); 4536e142f54Sdrh if( pParse->zErrMsg && pParse->sErrToken.z ){ 45475897234Sdrh sqliteSetNString(pzErrMsg, "near \"", -1, 45575897234Sdrh pParse->sErrToken.z, pParse->sErrToken.n, 45675897234Sdrh "\": ", -1, 45775897234Sdrh pParse->zErrMsg, -1, 45875897234Sdrh 0); 45975897234Sdrh nErr++; 4606e142f54Sdrh sqliteFree(pParse->zErrMsg); 4616e142f54Sdrh pParse->zErrMsg = 0; 46275897234Sdrh } 46375897234Sdrh } 464dcc581ccSdrh sqliteParserFree(pEngine, free); 46575897234Sdrh if( pParse->zErrMsg ){ 46675897234Sdrh if( pzErrMsg ){ 4676e142f54Sdrh sqliteFree(*pzErrMsg); 46875897234Sdrh *pzErrMsg = pParse->zErrMsg; 46975897234Sdrh }else{ 47075897234Sdrh sqliteFree(pParse->zErrMsg); 47175897234Sdrh } 47275897234Sdrh if( !nErr ) nErr++; 47375897234Sdrh } 47475897234Sdrh if( pParse->pVdbe ){ 47575897234Sdrh sqliteVdbeDelete(pParse->pVdbe); 47675897234Sdrh pParse->pVdbe = 0; 47775897234Sdrh } 47875897234Sdrh if( pParse->pNewTable ){ 47975897234Sdrh sqliteDeleteTable(pParse->db, pParse->pNewTable); 48075897234Sdrh pParse->pNewTable = 0; 48175897234Sdrh } 4824c504391Sdrh if( nErr>0 && pParse->rc==SQLITE_OK ){ 4834c504391Sdrh pParse->rc = SQLITE_ERROR; 4844c504391Sdrh } 48575897234Sdrh return nErr; 48675897234Sdrh } 487