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*113088ecSdrh ** $Id: tokenize.c,v 1.56 2003/03/20 01:16:59 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 }, 47*113088ecSdrh { "ATTACH", 0, TK_ATTACH, 0 }, 48c3f9bad2Sdanielk1977 { "BEFORE", 0, TK_BEFORE, 0 }, 49c4a3c779Sdrh { "BEGIN", 0, TK_BEGIN, 0 }, 50fef5208cSdrh { "BETWEEN", 0, TK_BETWEEN, 0 }, 5175897234Sdrh { "BY", 0, TK_BY, 0 }, 5204738cb9Sdrh { "CASCADE", 0, TK_CASCADE, 0 }, 5317a7f8ddSdrh { "CASE", 0, TK_CASE, 0 }, 5475897234Sdrh { "CHECK", 0, TK_CHECK, 0 }, 55f57b14a6Sdrh { "CLUSTER", 0, TK_CLUSTER, 0 }, 568e2ca029Sdrh { "COLLATE", 0, TK_COLLATE, 0 }, 57c4a3c779Sdrh { "COMMIT", 0, TK_COMMIT, 0 }, 589cfcf5d4Sdrh { "CONFLICT", 0, TK_CONFLICT, 0 }, 5975897234Sdrh { "CONSTRAINT", 0, TK_CONSTRAINT, 0 }, 60982cef7eSdrh { "COPY", 0, TK_COPY, 0 }, 6175897234Sdrh { "CREATE", 0, TK_CREATE, 0 }, 625ad1a6c8Sdrh { "CROSS", 0, TK_JOIN_KW, 0 }, 63*113088ecSdrh { "DATABASE", 0, TK_DATABASE, 0 }, 6475897234Sdrh { "DEFAULT", 0, TK_DEFAULT, 0 }, 6504738cb9Sdrh { "DEFERRED", 0, TK_DEFERRED, 0 }, 6604738cb9Sdrh { "DEFERRABLE", 0, TK_DEFERRABLE, 0 }, 6775897234Sdrh { "DELETE", 0, TK_DELETE, 0 }, 68982cef7eSdrh { "DELIMITERS", 0, TK_DELIMITERS, 0 }, 6975897234Sdrh { "DESC", 0, TK_DESC, 0 }, 70*113088ecSdrh { "DETACH", 0, TK_DETACH, 0 }, 71efb7251dSdrh { "DISTINCT", 0, TK_DISTINCT, 0 }, 7275897234Sdrh { "DROP", 0, TK_DROP, 0 }, 73c4a3c779Sdrh { "END", 0, TK_END, 0 }, 74c3f9bad2Sdanielk1977 { "EACH", 0, TK_EACH, 0 }, 7517a7f8ddSdrh { "ELSE", 0, TK_ELSE, 0 }, 7682c3d636Sdrh { "EXCEPT", 0, TK_EXCEPT, 0 }, 7775897234Sdrh { "EXPLAIN", 0, TK_EXPLAIN, 0 }, 781c92853dSdrh { "FAIL", 0, TK_FAIL, 0 }, 79c3f9bad2Sdanielk1977 { "FOR", 0, TK_FOR, 0 }, 8004738cb9Sdrh { "FOREIGN", 0, TK_FOREIGN, 0 }, 8175897234Sdrh { "FROM", 0, TK_FROM, 0 }, 825ad1a6c8Sdrh { "FULL", 0, TK_JOIN_KW, 0 }, 83dce2cbe6Sdrh { "GLOB", 0, TK_GLOB, 0 }, 842282792aSdrh { "GROUP", 0, TK_GROUP, 0 }, 852282792aSdrh { "HAVING", 0, TK_HAVING, 0 }, 869cfcf5d4Sdrh { "IGNORE", 0, TK_IGNORE, 0 }, 8704738cb9Sdrh { "IMMEDIATE", 0, TK_IMMEDIATE, 0 }, 88fef5208cSdrh { "IN", 0, TK_IN, 0 }, 8975897234Sdrh { "INDEX", 0, TK_INDEX, 0 }, 9004738cb9Sdrh { "INITIALLY", 0, TK_INITIALLY, 0 }, 915ad1a6c8Sdrh { "INNER", 0, TK_JOIN_KW, 0 }, 9275897234Sdrh { "INSERT", 0, TK_INSERT, 0 }, 93c3f9bad2Sdanielk1977 { "INSTEAD", 0, TK_INSTEAD, 0 }, 9482c3d636Sdrh { "INTERSECT", 0, TK_INTERSECT, 0 }, 9575897234Sdrh { "INTO", 0, TK_INTO, 0 }, 9675897234Sdrh { "IS", 0, TK_IS, 0 }, 9775897234Sdrh { "ISNULL", 0, TK_ISNULL, 0 }, 9801f3f253Sdrh { "JOIN", 0, TK_JOIN, 0 }, 9975897234Sdrh { "KEY", 0, TK_KEY, 0 }, 1005ad1a6c8Sdrh { "LEFT", 0, TK_JOIN_KW, 0 }, 101dce2cbe6Sdrh { "LIKE", 0, TK_LIKE, 0 }, 1029bbca4c1Sdrh { "LIMIT", 0, TK_LIMIT, 0 }, 10304738cb9Sdrh { "MATCH", 0, TK_MATCH, 0 }, 1045ad1a6c8Sdrh { "NATURAL", 0, TK_JOIN_KW, 0 }, 10575897234Sdrh { "NOT", 0, TK_NOT, 0 }, 10675897234Sdrh { "NOTNULL", 0, TK_NOTNULL, 0 }, 10775897234Sdrh { "NULL", 0, TK_NULL, 0 }, 108c3f9bad2Sdanielk1977 { "OF", 0, TK_OF, 0 }, 1099bbca4c1Sdrh { "OFFSET", 0, TK_OFFSET, 0 }, 11075897234Sdrh { "ON", 0, TK_ON, 0 }, 11175897234Sdrh { "OR", 0, TK_OR, 0 }, 11275897234Sdrh { "ORDER", 0, TK_ORDER, 0 }, 1135ad1a6c8Sdrh { "OUTER", 0, TK_JOIN_KW, 0 }, 114f57b14a6Sdrh { "PRAGMA", 0, TK_PRAGMA, 0 }, 11575897234Sdrh { "PRIMARY", 0, TK_PRIMARY, 0 }, 1166f34903eSdanielk1977 { "RAISE", 0, TK_RAISE, 0 }, 11704738cb9Sdrh { "REFERENCES", 0, TK_REFERENCES, 0 }, 1189cfcf5d4Sdrh { "REPLACE", 0, TK_REPLACE, 0 }, 11904738cb9Sdrh { "RESTRICT", 0, TK_RESTRICT, 0 }, 1205ad1a6c8Sdrh { "RIGHT", 0, TK_JOIN_KW, 0 }, 121c4a3c779Sdrh { "ROLLBACK", 0, TK_ROLLBACK, 0 }, 122c3f9bad2Sdanielk1977 { "ROW", 0, TK_ROW, 0 }, 12375897234Sdrh { "SELECT", 0, TK_SELECT, 0 }, 12475897234Sdrh { "SET", 0, TK_SET, 0 }, 1251873cd50Sdrh { "STATEMENT", 0, TK_STATEMENT, 0 }, 12675897234Sdrh { "TABLE", 0, TK_TABLE, 0 }, 127f57b3399Sdrh { "TEMP", 0, TK_TEMP, 0 }, 128f57b3399Sdrh { "TEMPORARY", 0, TK_TEMP, 0 }, 12917a7f8ddSdrh { "THEN", 0, TK_THEN, 0 }, 130c4a3c779Sdrh { "TRANSACTION", 0, TK_TRANSACTION, 0 }, 131c3f9bad2Sdanielk1977 { "TRIGGER", 0, TK_TRIGGER, 0 }, 13282c3d636Sdrh { "UNION", 0, TK_UNION, 0 }, 13375897234Sdrh { "UNIQUE", 0, TK_UNIQUE, 0 }, 13475897234Sdrh { "UPDATE", 0, TK_UPDATE, 0 }, 135982cef7eSdrh { "USING", 0, TK_USING, 0 }, 136dce2cbe6Sdrh { "VACUUM", 0, TK_VACUUM, 0 }, 13775897234Sdrh { "VALUES", 0, TK_VALUES, 0 }, 138a76b5dfcSdrh { "VIEW", 0, TK_VIEW, 0 }, 13917a7f8ddSdrh { "WHEN", 0, TK_WHEN, 0 }, 14075897234Sdrh { "WHERE", 0, TK_WHERE, 0 }, 14175897234Sdrh }; 14275897234Sdrh 14375897234Sdrh /* 14475897234Sdrh ** This is the hash table 14575897234Sdrh */ 146daffd0e5Sdrh #define KEY_HASH_SIZE 71 14775897234Sdrh static Keyword *apHashTable[KEY_HASH_SIZE]; 14875897234Sdrh 14975897234Sdrh 15075897234Sdrh /* 15175897234Sdrh ** This function looks up an identifier to determine if it is a 15275897234Sdrh ** keyword. If it is a keyword, the token code of that keyword is 15375897234Sdrh ** returned. If the input is not a keyword, TK_ID is returned. 15475897234Sdrh */ 15517f71934Sdrh int sqliteKeywordCode(const char *z, int n){ 15675897234Sdrh int h; 15775897234Sdrh Keyword *p; 15875897234Sdrh if( aKeywordTable[0].len==0 ){ 15975897234Sdrh /* Initialize the keyword hash table */ 160ad75e987Sdrh sqliteOsEnterMutex(); 161ad75e987Sdrh if( aKeywordTable[0].len==0 ){ 16275897234Sdrh int i; 16375897234Sdrh int n; 16475897234Sdrh n = sizeof(aKeywordTable)/sizeof(aKeywordTable[0]); 16575897234Sdrh for(i=0; i<n; i++){ 16675897234Sdrh aKeywordTable[i].len = strlen(aKeywordTable[i].zName); 16775897234Sdrh h = sqliteHashNoCase(aKeywordTable[i].zName, aKeywordTable[i].len); 16875897234Sdrh h %= KEY_HASH_SIZE; 16975897234Sdrh aKeywordTable[i].pNext = apHashTable[h]; 17075897234Sdrh apHashTable[h] = &aKeywordTable[i]; 17175897234Sdrh } 17275897234Sdrh } 173ad75e987Sdrh sqliteOsLeaveMutex(); 174ad75e987Sdrh } 17575897234Sdrh h = sqliteHashNoCase(z, n) % KEY_HASH_SIZE; 17675897234Sdrh for(p=apHashTable[h]; p; p=p->pNext){ 17775897234Sdrh if( p->len==n && sqliteStrNICmp(p->zName, z, n)==0 ){ 17875897234Sdrh return p->tokenType; 17975897234Sdrh } 18075897234Sdrh } 18175897234Sdrh return TK_ID; 18275897234Sdrh } 18375897234Sdrh 18498808babSdrh 18598808babSdrh /* 18698808babSdrh ** If X is a character that can be used in an identifier then 18798808babSdrh ** isIdChar[X] will be 1. Otherwise isIdChar[X] will be 0. 18898808babSdrh ** 18998808babSdrh ** In this implementation, an identifier can be a string of 19098808babSdrh ** alphabetic characters, digits, and "_" plus any character 19198808babSdrh ** with the high-order bit set. The latter rule means that 19298808babSdrh ** any sequence of UTF-8 characters or characters taken from 19398808babSdrh ** an extended ISO8859 character set can form an identifier. 19498808babSdrh */ 19598808babSdrh static const char isIdChar[] = { 19698808babSdrh /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ 19798808babSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */ 19898808babSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */ 19998808babSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */ 20098808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */ 20198808babSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */ 20298808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */ 20398808babSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */ 20498808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */ 20598808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 8x */ 20698808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 9x */ 20798808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Ax */ 20898808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Bx */ 20998808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Cx */ 21098808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Dx */ 21198808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Ex */ 21298808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Fx */ 21398808babSdrh }; 21498808babSdrh 21598808babSdrh 21675897234Sdrh /* 21775897234Sdrh ** Return the length of the token that begins at z[0]. Return 21875897234Sdrh ** -1 if the token is (or might be) incomplete. Store the token 21975897234Sdrh ** type in *tokenType before returning. 22075897234Sdrh */ 22198808babSdrh static int sqliteGetToken(const unsigned char *z, int *tokenType){ 22275897234Sdrh int i; 22375897234Sdrh switch( *z ){ 22430cab809Sdrh case ' ': case '\t': case '\n': case '\f': case '\r': { 22532eb7b47Sdrh for(i=1; isspace(z[i]); i++){} 22675897234Sdrh *tokenType = TK_SPACE; 22775897234Sdrh return i; 22875897234Sdrh } 22975897234Sdrh case '-': { 23075897234Sdrh if( z[1]==0 ) return -1; 23175897234Sdrh if( z[1]=='-' ){ 23275897234Sdrh for(i=2; z[i] && z[i]!='\n'; i++){} 23375897234Sdrh *tokenType = TK_COMMENT; 23475897234Sdrh return i; 23575897234Sdrh } 23675897234Sdrh *tokenType = TK_MINUS; 23775897234Sdrh return 1; 23875897234Sdrh } 23975897234Sdrh case '(': { 2401f16230bSdrh if( z[1]=='+' && z[2]==')' ){ 2411f16230bSdrh *tokenType = TK_ORACLE_OUTER_JOIN; 2421f16230bSdrh return 3; 2431f16230bSdrh }else{ 24475897234Sdrh *tokenType = TK_LP; 24575897234Sdrh return 1; 24675897234Sdrh } 2471f16230bSdrh } 24875897234Sdrh case ')': { 24975897234Sdrh *tokenType = TK_RP; 25075897234Sdrh return 1; 25175897234Sdrh } 25275897234Sdrh case ';': { 25375897234Sdrh *tokenType = TK_SEMI; 25475897234Sdrh return 1; 25575897234Sdrh } 25675897234Sdrh case '+': { 25775897234Sdrh *tokenType = TK_PLUS; 25875897234Sdrh return 1; 25975897234Sdrh } 26075897234Sdrh case '*': { 26175897234Sdrh *tokenType = TK_STAR; 26275897234Sdrh return 1; 26375897234Sdrh } 26475897234Sdrh case '/': { 26566105a8eSdrh if( z[1]!='*' || z[2]==0 ){ 26675897234Sdrh *tokenType = TK_SLASH; 26775897234Sdrh return 1; 26875897234Sdrh } 26966105a8eSdrh for(i=3; z[i] && (z[i]!='/' || z[i-1]!='*'); i++){} 27066105a8eSdrh if( z[i] ) i++; 27166105a8eSdrh *tokenType = TK_COMMENT; 27266105a8eSdrh return i; 27366105a8eSdrh } 274bf4133cbSdrh case '%': { 275bf4133cbSdrh *tokenType = TK_REM; 276bf4133cbSdrh return 1; 277bf4133cbSdrh } 27875897234Sdrh case '=': { 27975897234Sdrh *tokenType = TK_EQ; 28075897234Sdrh return 1 + (z[1]=='='); 28175897234Sdrh } 28275897234Sdrh case '<': { 28375897234Sdrh if( z[1]=='=' ){ 28475897234Sdrh *tokenType = TK_LE; 28575897234Sdrh return 2; 28675897234Sdrh }else if( z[1]=='>' ){ 28775897234Sdrh *tokenType = TK_NE; 28875897234Sdrh return 2; 289bf4133cbSdrh }else if( z[1]=='<' ){ 290bf4133cbSdrh *tokenType = TK_LSHIFT; 291bf4133cbSdrh return 2; 29275897234Sdrh }else{ 29375897234Sdrh *tokenType = TK_LT; 29475897234Sdrh return 1; 29575897234Sdrh } 29675897234Sdrh } 29775897234Sdrh case '>': { 29875897234Sdrh if( z[1]=='=' ){ 29975897234Sdrh *tokenType = TK_GE; 30075897234Sdrh return 2; 301bf4133cbSdrh }else if( z[1]=='>' ){ 302bf4133cbSdrh *tokenType = TK_RSHIFT; 303bf4133cbSdrh return 2; 30475897234Sdrh }else{ 30575897234Sdrh *tokenType = TK_GT; 30675897234Sdrh return 1; 30775897234Sdrh } 30875897234Sdrh } 30975897234Sdrh case '!': { 31075897234Sdrh if( z[1]!='=' ){ 31175897234Sdrh *tokenType = TK_ILLEGAL; 312c837e709Sdrh return 2; 31375897234Sdrh }else{ 31475897234Sdrh *tokenType = TK_NE; 31575897234Sdrh return 2; 31675897234Sdrh } 31775897234Sdrh } 3180040077dSdrh case '|': { 3190040077dSdrh if( z[1]!='|' ){ 320bf4133cbSdrh *tokenType = TK_BITOR; 3210040077dSdrh return 1; 3220040077dSdrh }else{ 3230040077dSdrh *tokenType = TK_CONCAT; 3240040077dSdrh return 2; 3250040077dSdrh } 3260040077dSdrh } 32775897234Sdrh case ',': { 32875897234Sdrh *tokenType = TK_COMMA; 32975897234Sdrh return 1; 33075897234Sdrh } 331bf4133cbSdrh case '&': { 332bf4133cbSdrh *tokenType = TK_BITAND; 333bf4133cbSdrh return 1; 334bf4133cbSdrh } 335bf4133cbSdrh case '~': { 336bf4133cbSdrh *tokenType = TK_BITNOT; 337bf4133cbSdrh return 1; 338bf4133cbSdrh } 33975897234Sdrh case '\'': case '"': { 34075897234Sdrh int delim = z[0]; 34175897234Sdrh for(i=1; z[i]; i++){ 34275897234Sdrh if( z[i]==delim ){ 34375897234Sdrh if( z[i+1]==delim ){ 34475897234Sdrh i++; 34575897234Sdrh }else{ 34675897234Sdrh break; 34775897234Sdrh } 34875897234Sdrh } 34975897234Sdrh } 35075897234Sdrh if( z[i] ) i++; 35175897234Sdrh *tokenType = TK_STRING; 35275897234Sdrh return i; 35375897234Sdrh } 35475897234Sdrh case '.': { 35575897234Sdrh if( !isdigit(z[1]) ){ 35675897234Sdrh *tokenType = TK_DOT; 35775897234Sdrh return 1; 35875897234Sdrh } 35975897234Sdrh /* Fall thru into the next case */ 36075897234Sdrh } 36175897234Sdrh case '0': case '1': case '2': case '3': case '4': 36275897234Sdrh case '5': case '6': case '7': case '8': case '9': { 363c837e709Sdrh *tokenType = TK_INTEGER; 36432eb7b47Sdrh for(i=1; isdigit(z[i]); i++){} 36575897234Sdrh if( z[i]=='.' ){ 36675897234Sdrh i++; 36732eb7b47Sdrh while( isdigit(z[i]) ){ i++; } 368c837e709Sdrh *tokenType = TK_FLOAT; 369c837e709Sdrh } 37075897234Sdrh if( (z[i]=='e' || z[i]=='E') && 37175897234Sdrh ( isdigit(z[i+1]) 37275897234Sdrh || ((z[i+1]=='+' || z[i+1]=='-') && isdigit(z[i+2])) 37375897234Sdrh ) 37475897234Sdrh ){ 37575897234Sdrh i += 2; 37632eb7b47Sdrh while( isdigit(z[i]) ){ i++; } 37775897234Sdrh *tokenType = TK_FLOAT; 37875897234Sdrh }else if( z[0]=='.' ){ 37975897234Sdrh *tokenType = TK_FLOAT; 38075897234Sdrh } 38175897234Sdrh return i; 38275897234Sdrh } 3832f4392ffSdrh case '[': { 3842f4392ffSdrh for(i=1; z[i] && z[i-1]!=']'; i++){} 3852f4392ffSdrh *tokenType = TK_ID; 3862f4392ffSdrh return i; 3872f4392ffSdrh } 38898808babSdrh default: { 38998808babSdrh if( !isIdChar[*z] ){ 39098808babSdrh break; 39198808babSdrh } 39298808babSdrh for(i=1; isIdChar[z[i]]; i++){} 3936a535340Sdrh *tokenType = sqliteKeywordCode((char*)z, i); 39475897234Sdrh return i; 39575897234Sdrh } 39675897234Sdrh } 39775897234Sdrh *tokenType = TK_ILLEGAL; 39875897234Sdrh return 1; 39975897234Sdrh } 40075897234Sdrh 40175897234Sdrh /* 40275897234Sdrh ** Run the parser on the given SQL string. The parser structure is 403b19a2bc6Sdrh ** passed in. An SQLITE_ status code is returned. If an error occurs 404b19a2bc6Sdrh ** and pzErrMsg!=NULL then an error message might be written into 405b19a2bc6Sdrh ** memory obtained from malloc() and *pzErrMsg made to point to that 406b19a2bc6Sdrh ** error message. Or maybe not. 40775897234Sdrh */ 40880ff32f5Sdrh int sqliteRunParser(Parse *pParse, const char *zSql, char **pzErrMsg){ 40975897234Sdrh int nErr = 0; 41075897234Sdrh int i; 41175897234Sdrh void *pEngine; 412b86ccfb2Sdrh int tokenType; 413b86ccfb2Sdrh int lastTokenParsed = -1; 4146d4abfbeSdrh sqlite *db = pParse->db; 41575897234Sdrh extern void *sqliteParserAlloc(void*(*)(int)); 41675897234Sdrh extern void sqliteParserFree(void*, void(*)(void*)); 417338ea13cSdrh extern int sqliteParser(void*, int, Token, Parse*); 41875897234Sdrh 4196d4abfbeSdrh db->flags &= ~SQLITE_Interrupt; 4204c504391Sdrh pParse->rc = SQLITE_OK; 42175897234Sdrh i = 0; 422982cef7eSdrh pEngine = sqliteParserAlloc((void*(*)(int))malloc); 42375897234Sdrh if( pEngine==0 ){ 42475897234Sdrh sqliteSetString(pzErrMsg, "out of memory", 0); 42575897234Sdrh return 1; 42675897234Sdrh } 42732eb7b47Sdrh pParse->sLastToken.dyn = 0; 428326dce74Sdrh pParse->zTail = zSql; 42932eb7b47Sdrh while( sqlite_malloc_failed==0 && zSql[i]!=0 ){ 43075897234Sdrh 43132eb7b47Sdrh assert( i>=0 ); 43275897234Sdrh pParse->sLastToken.z = &zSql[i]; 43332eb7b47Sdrh assert( pParse->sLastToken.dyn==0 ); 43498808babSdrh pParse->sLastToken.n = sqliteGetToken((unsigned char*)&zSql[i], &tokenType); 43575897234Sdrh i += pParse->sLastToken.n; 43675897234Sdrh switch( tokenType ){ 43775897234Sdrh case TK_SPACE: 43875897234Sdrh case TK_COMMENT: { 43932eb7b47Sdrh if( (db->flags & SQLITE_Interrupt)!=0 ){ 44032eb7b47Sdrh pParse->rc = SQLITE_INTERRUPT; 44132eb7b47Sdrh sqliteSetString(pzErrMsg, "interrupt", 0); 44232eb7b47Sdrh goto abort_parse; 44332eb7b47Sdrh } 44475897234Sdrh break; 44575897234Sdrh } 44632eb7b47Sdrh case TK_ILLEGAL: { 447c837e709Sdrh sqliteSetNString(pzErrMsg, "unrecognized token: \"", -1, 448c837e709Sdrh pParse->sLastToken.z, pParse->sLastToken.n, "\"", 1, 0); 44975897234Sdrh nErr++; 450caec2f12Sdrh goto abort_parse; 45132eb7b47Sdrh } 452326dce74Sdrh case TK_SEMI: { 453326dce74Sdrh pParse->zTail = &zSql[i]; 454326dce74Sdrh /* Fall thru into the default case */ 455326dce74Sdrh } 45632eb7b47Sdrh default: { 45775897234Sdrh sqliteParser(pEngine, tokenType, pParse->sLastToken, pParse); 458b86ccfb2Sdrh lastTokenParsed = tokenType; 459b86ccfb2Sdrh if( pParse->rc!=SQLITE_OK ){ 46032eb7b47Sdrh goto abort_parse; 46175897234Sdrh } 46275897234Sdrh break; 46375897234Sdrh } 46475897234Sdrh } 46532eb7b47Sdrh } 46632eb7b47Sdrh abort_parse: 467b86ccfb2Sdrh if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){ 468b86ccfb2Sdrh if( lastTokenParsed!=TK_SEMI ){ 469094b2bbfSdrh sqliteParser(pEngine, TK_SEMI, pParse->sLastToken, pParse); 470326dce74Sdrh pParse->zTail = &zSql[i]; 47175897234Sdrh } 472b86ccfb2Sdrh sqliteParser(pEngine, 0, pParse->sLastToken, pParse); 47375897234Sdrh } 474dcc581ccSdrh sqliteParserFree(pEngine, free); 475b86ccfb2Sdrh if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){ 476b86ccfb2Sdrh sqliteSetString(&pParse->zErrMsg, sqlite_error_string(pParse->rc), 0); 477b86ccfb2Sdrh } 47875897234Sdrh if( pParse->zErrMsg ){ 479b86ccfb2Sdrh if( pzErrMsg && *pzErrMsg==0 ){ 48075897234Sdrh *pzErrMsg = pParse->zErrMsg; 48175897234Sdrh }else{ 48275897234Sdrh sqliteFree(pParse->zErrMsg); 48375897234Sdrh } 484b86ccfb2Sdrh pParse->zErrMsg = 0; 48575897234Sdrh if( !nErr ) nErr++; 48675897234Sdrh } 487b86ccfb2Sdrh if( pParse->pVdbe && (pParse->useCallback || pParse->nErr>0) ){ 48875897234Sdrh sqliteVdbeDelete(pParse->pVdbe); 48975897234Sdrh pParse->pVdbe = 0; 49075897234Sdrh } 49175897234Sdrh if( pParse->pNewTable ){ 49275897234Sdrh sqliteDeleteTable(pParse->db, pParse->pNewTable); 49375897234Sdrh pParse->pNewTable = 0; 49475897234Sdrh } 495b86ccfb2Sdrh if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){ 4964c504391Sdrh pParse->rc = SQLITE_ERROR; 4974c504391Sdrh } 49875897234Sdrh return nErr; 49975897234Sdrh } 500