175897234Sdrh /* 275897234Sdrh ** Copyright (c) 1999, 2000 D. Richard Hipp 375897234Sdrh ** 475897234Sdrh ** This program is free software; you can redistribute it and/or 575897234Sdrh ** modify it under the terms of the GNU General Public 675897234Sdrh ** License as published by the Free Software Foundation; either 775897234Sdrh ** version 2 of the License, or (at your option) any later version. 875897234Sdrh ** 975897234Sdrh ** This program is distributed in the hope that it will be useful, 1075897234Sdrh ** but WITHOUT ANY WARRANTY; without even the implied warranty of 1175897234Sdrh ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1275897234Sdrh ** General Public License for more details. 1375897234Sdrh ** 1475897234Sdrh ** You should have received a copy of the GNU General Public 1575897234Sdrh ** License along with this library; if not, write to the 1675897234Sdrh ** Free Software Foundation, Inc., 59 Temple Place - Suite 330, 1775897234Sdrh ** Boston, MA 02111-1307, USA. 1875897234Sdrh ** 1975897234Sdrh ** Author contact information: 2075897234Sdrh ** [email protected] 2175897234Sdrh ** http://www.hwaci.com/drh/ 2275897234Sdrh ** 2375897234Sdrh ************************************************************************* 2475897234Sdrh ** An tokenizer for SQL 2575897234Sdrh ** 2675897234Sdrh ** This file contains C code that splits an SQL input string up into 2775897234Sdrh ** individual tokens and sends those tokens one-by-one over to the 2875897234Sdrh ** parser for analysis. 2975897234Sdrh ** 30*30cab809Sdrh ** $Id: tokenize.c,v 1.13 2000/08/09 17:17:25 drh Exp $ 3175897234Sdrh */ 3275897234Sdrh #include "sqliteInt.h" 3375897234Sdrh #include <ctype.h> 34dcc581ccSdrh #include <stdlib.h> 3575897234Sdrh 3675897234Sdrh /* 3775897234Sdrh ** All the keywords of the SQL language are stored as in a hash 3875897234Sdrh ** table composed of instances of the following structure. 3975897234Sdrh */ 4075897234Sdrh typedef struct Keyword Keyword; 4175897234Sdrh struct Keyword { 4275897234Sdrh char *zName; /* The keyword name */ 4375897234Sdrh int len; /* Number of characters in the keyword */ 4475897234Sdrh int tokenType; /* The token value for this keyword */ 4575897234Sdrh Keyword *pNext; /* Next keyword with the same hash */ 4675897234Sdrh }; 4775897234Sdrh 4875897234Sdrh /* 4975897234Sdrh ** These are the keywords 5075897234Sdrh */ 5175897234Sdrh static Keyword aKeywordTable[] = { 52fef5208cSdrh { "ALL", 0, TK_ALL, 0 }, 5375897234Sdrh { "AND", 0, TK_AND, 0 }, 5475897234Sdrh { "AS", 0, TK_AS, 0 }, 5575897234Sdrh { "ASC", 0, TK_ASC, 0 }, 56fef5208cSdrh { "BETWEEN", 0, TK_BETWEEN, 0 }, 5775897234Sdrh { "BY", 0, TK_BY, 0 }, 5875897234Sdrh { "CHECK", 0, TK_CHECK, 0 }, 5975897234Sdrh { "CONSTRAINT", 0, TK_CONSTRAINT, 0 }, 60982cef7eSdrh { "COPY", 0, TK_COPY, 0 }, 6175897234Sdrh { "CREATE", 0, TK_CREATE, 0 }, 6275897234Sdrh { "DEFAULT", 0, TK_DEFAULT, 0 }, 6375897234Sdrh { "DELETE", 0, TK_DELETE, 0 }, 64982cef7eSdrh { "DELIMITERS", 0, TK_DELIMITERS, 0 }, 6575897234Sdrh { "DESC", 0, TK_DESC, 0 }, 66efb7251dSdrh { "DISTINCT", 0, TK_DISTINCT, 0 }, 6775897234Sdrh { "DROP", 0, TK_DROP, 0 }, 6882c3d636Sdrh { "EXCEPT", 0, TK_EXCEPT, 0 }, 6975897234Sdrh { "EXPLAIN", 0, TK_EXPLAIN, 0 }, 7075897234Sdrh { "FROM", 0, TK_FROM, 0 }, 71dce2cbe6Sdrh { "GLOB", 0, TK_GLOB, 0 }, 722282792aSdrh { "GROUP", 0, TK_GROUP, 0 }, 732282792aSdrh { "HAVING", 0, TK_HAVING, 0 }, 74fef5208cSdrh { "IN", 0, TK_IN, 0 }, 7575897234Sdrh { "INDEX", 0, TK_INDEX, 0 }, 7675897234Sdrh { "INSERT", 0, TK_INSERT, 0 }, 7782c3d636Sdrh { "INTERSECT", 0, TK_INTERSECT, 0 }, 7875897234Sdrh { "INTO", 0, TK_INTO, 0 }, 7975897234Sdrh { "IS", 0, TK_IS, 0 }, 8075897234Sdrh { "ISNULL", 0, TK_ISNULL, 0 }, 8175897234Sdrh { "KEY", 0, TK_KEY, 0 }, 82dce2cbe6Sdrh { "LIKE", 0, TK_LIKE, 0 }, 8375897234Sdrh { "NOT", 0, TK_NOT, 0 }, 8475897234Sdrh { "NOTNULL", 0, TK_NOTNULL, 0 }, 8575897234Sdrh { "NULL", 0, TK_NULL, 0 }, 8675897234Sdrh { "ON", 0, TK_ON, 0 }, 8775897234Sdrh { "OR", 0, TK_OR, 0 }, 8875897234Sdrh { "ORDER", 0, TK_ORDER, 0 }, 8975897234Sdrh { "PRIMARY", 0, TK_PRIMARY, 0 }, 9075897234Sdrh { "SELECT", 0, TK_SELECT, 0 }, 9175897234Sdrh { "SET", 0, TK_SET, 0 }, 9275897234Sdrh { "TABLE", 0, TK_TABLE, 0 }, 9382c3d636Sdrh { "UNION", 0, TK_UNION, 0 }, 9475897234Sdrh { "UNIQUE", 0, TK_UNIQUE, 0 }, 9575897234Sdrh { "UPDATE", 0, TK_UPDATE, 0 }, 96982cef7eSdrh { "USING", 0, TK_USING, 0 }, 97dce2cbe6Sdrh { "VACUUM", 0, TK_VACUUM, 0 }, 9875897234Sdrh { "VALUES", 0, TK_VALUES, 0 }, 9975897234Sdrh { "WHERE", 0, TK_WHERE, 0 }, 10075897234Sdrh }; 10175897234Sdrh 10275897234Sdrh /* 10375897234Sdrh ** This is the hash table 10475897234Sdrh */ 10575897234Sdrh #define KEY_HASH_SIZE 37 10675897234Sdrh static Keyword *apHashTable[KEY_HASH_SIZE]; 10775897234Sdrh 10875897234Sdrh 10975897234Sdrh /* 11075897234Sdrh ** This function looks up an identifier to determine if it is a 11175897234Sdrh ** keyword. If it is a keyword, the token code of that keyword is 11275897234Sdrh ** returned. If the input is not a keyword, TK_ID is returned. 11375897234Sdrh */ 11475897234Sdrh static int sqliteKeywordCode(const char *z, int n){ 11575897234Sdrh int h; 11675897234Sdrh Keyword *p; 11775897234Sdrh if( aKeywordTable[0].len==0 ){ 11875897234Sdrh /* Initialize the keyword hash table */ 11975897234Sdrh int i; 12075897234Sdrh int n; 12175897234Sdrh n = sizeof(aKeywordTable)/sizeof(aKeywordTable[0]); 12275897234Sdrh for(i=0; i<n; i++){ 12375897234Sdrh aKeywordTable[i].len = strlen(aKeywordTable[i].zName); 12475897234Sdrh h = sqliteHashNoCase(aKeywordTable[i].zName, aKeywordTable[i].len); 12575897234Sdrh h %= KEY_HASH_SIZE; 12675897234Sdrh aKeywordTable[i].pNext = apHashTable[h]; 12775897234Sdrh apHashTable[h] = &aKeywordTable[i]; 12875897234Sdrh } 12975897234Sdrh } 13075897234Sdrh h = sqliteHashNoCase(z, n) % KEY_HASH_SIZE; 13175897234Sdrh for(p=apHashTable[h]; p; p=p->pNext){ 13275897234Sdrh if( p->len==n && sqliteStrNICmp(p->zName, z, n)==0 ){ 13375897234Sdrh return p->tokenType; 13475897234Sdrh } 13575897234Sdrh } 13675897234Sdrh return TK_ID; 13775897234Sdrh } 13875897234Sdrh 13975897234Sdrh /* 14075897234Sdrh ** Return the length of the token that begins at z[0]. Return 14175897234Sdrh ** -1 if the token is (or might be) incomplete. Store the token 14275897234Sdrh ** type in *tokenType before returning. 14375897234Sdrh */ 14475897234Sdrh int sqliteGetToken(const char *z, int *tokenType){ 14575897234Sdrh int i; 14675897234Sdrh switch( *z ){ 147*30cab809Sdrh case ' ': case '\t': case '\n': case '\f': case '\r': { 14875897234Sdrh for(i=1; z[i] && isspace(z[i]); i++){} 14975897234Sdrh *tokenType = TK_SPACE; 15075897234Sdrh return i; 15175897234Sdrh } 15275897234Sdrh case '-': { 15375897234Sdrh if( z[1]==0 ) return -1; 15475897234Sdrh if( z[1]=='-' ){ 15575897234Sdrh for(i=2; z[i] && z[i]!='\n'; i++){} 15675897234Sdrh *tokenType = TK_COMMENT; 15775897234Sdrh return i; 15875897234Sdrh } 15975897234Sdrh *tokenType = TK_MINUS; 16075897234Sdrh return 1; 16175897234Sdrh } 16275897234Sdrh case '(': { 16375897234Sdrh *tokenType = TK_LP; 16475897234Sdrh return 1; 16575897234Sdrh } 16675897234Sdrh case ')': { 16775897234Sdrh *tokenType = TK_RP; 16875897234Sdrh return 1; 16975897234Sdrh } 17075897234Sdrh case ';': { 17175897234Sdrh *tokenType = TK_SEMI; 17275897234Sdrh return 1; 17375897234Sdrh } 17475897234Sdrh case '+': { 17575897234Sdrh *tokenType = TK_PLUS; 17675897234Sdrh return 1; 17775897234Sdrh } 17875897234Sdrh case '*': { 17975897234Sdrh *tokenType = TK_STAR; 18075897234Sdrh return 1; 18175897234Sdrh } 18275897234Sdrh case '/': { 18375897234Sdrh *tokenType = TK_SLASH; 18475897234Sdrh return 1; 18575897234Sdrh } 18675897234Sdrh case '=': { 18775897234Sdrh *tokenType = TK_EQ; 18875897234Sdrh return 1 + (z[1]=='='); 18975897234Sdrh } 19075897234Sdrh case '<': { 19175897234Sdrh if( z[1]=='=' ){ 19275897234Sdrh *tokenType = TK_LE; 19375897234Sdrh return 2; 19475897234Sdrh }else if( z[1]=='>' ){ 19575897234Sdrh *tokenType = TK_NE; 19675897234Sdrh return 2; 19775897234Sdrh }else{ 19875897234Sdrh *tokenType = TK_LT; 19975897234Sdrh return 1; 20075897234Sdrh } 20175897234Sdrh } 20275897234Sdrh case '>': { 20375897234Sdrh if( z[1]=='=' ){ 20475897234Sdrh *tokenType = TK_GE; 20575897234Sdrh return 2; 20675897234Sdrh }else{ 20775897234Sdrh *tokenType = TK_GT; 20875897234Sdrh return 1; 20975897234Sdrh } 21075897234Sdrh } 21175897234Sdrh case '!': { 21275897234Sdrh if( z[1]!='=' ){ 21375897234Sdrh *tokenType = TK_ILLEGAL; 214c837e709Sdrh return 2; 21575897234Sdrh }else{ 21675897234Sdrh *tokenType = TK_NE; 21775897234Sdrh return 2; 21875897234Sdrh } 21975897234Sdrh } 2200040077dSdrh case '|': { 2210040077dSdrh if( z[1]!='|' ){ 2220040077dSdrh *tokenType = TK_ILLEGAL; 2230040077dSdrh return 1; 2240040077dSdrh }else{ 2250040077dSdrh *tokenType = TK_CONCAT; 2260040077dSdrh return 2; 2270040077dSdrh } 2280040077dSdrh } 22975897234Sdrh case ',': { 23075897234Sdrh *tokenType = TK_COMMA; 23175897234Sdrh return 1; 23275897234Sdrh } 23375897234Sdrh case '\'': case '"': { 23475897234Sdrh int delim = z[0]; 23575897234Sdrh for(i=1; z[i]; i++){ 23675897234Sdrh if( z[i]==delim ){ 23775897234Sdrh if( z[i+1]==delim ){ 23875897234Sdrh i++; 23975897234Sdrh }else{ 24075897234Sdrh break; 24175897234Sdrh } 24275897234Sdrh } 24375897234Sdrh } 24475897234Sdrh if( z[i] ) i++; 24575897234Sdrh *tokenType = TK_STRING; 24675897234Sdrh return i; 24775897234Sdrh } 24875897234Sdrh case '.': { 24975897234Sdrh if( !isdigit(z[1]) ){ 25075897234Sdrh *tokenType = TK_DOT; 25175897234Sdrh return 1; 25275897234Sdrh } 25375897234Sdrh /* Fall thru into the next case */ 25475897234Sdrh } 25575897234Sdrh case '0': case '1': case '2': case '3': case '4': 25675897234Sdrh case '5': case '6': case '7': case '8': case '9': { 257c837e709Sdrh *tokenType = TK_INTEGER; 25875897234Sdrh for(i=1; z[i] && isdigit(z[i]); i++){} 25975897234Sdrh if( z[i]=='.' ){ 26075897234Sdrh i++; 26175897234Sdrh while( z[i] && isdigit(z[i]) ){ i++; } 262c837e709Sdrh *tokenType = TK_FLOAT; 263c837e709Sdrh } 26475897234Sdrh if( (z[i]=='e' || z[i]=='E') && 26575897234Sdrh ( isdigit(z[i+1]) 26675897234Sdrh || ((z[i+1]=='+' || z[i+1]=='-') && isdigit(z[i+2])) 26775897234Sdrh ) 26875897234Sdrh ){ 26975897234Sdrh i += 2; 27075897234Sdrh while( z[i] && isdigit(z[i]) ){ i++; } 27175897234Sdrh *tokenType = TK_FLOAT; 27275897234Sdrh }else if( z[0]=='.' ){ 27375897234Sdrh *tokenType = TK_FLOAT; 27475897234Sdrh } 27575897234Sdrh return i; 27675897234Sdrh } 27775897234Sdrh case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': 27875897234Sdrh case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': 27975897234Sdrh case 'm': case 'n': case 'o': case 'p': case 'q': case 'r': 28075897234Sdrh case 's': case 't': case 'u': case 'v': case 'w': case 'x': 28175897234Sdrh case 'y': case 'z': case '_': 28275897234Sdrh case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': 28375897234Sdrh case 'G': case 'H': case 'I': case 'J': case 'K': case 'L': 28475897234Sdrh case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': 28575897234Sdrh case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': 28675897234Sdrh case 'Y': case 'Z': { 28775897234Sdrh for(i=1; z[i] && (isalnum(z[i]) || z[i]=='_'); i++){} 28875897234Sdrh *tokenType = sqliteKeywordCode(z, i); 28975897234Sdrh return i; 29075897234Sdrh } 29175897234Sdrh default: { 29275897234Sdrh break; 29375897234Sdrh } 29475897234Sdrh } 29575897234Sdrh *tokenType = TK_ILLEGAL; 29675897234Sdrh return 1; 29775897234Sdrh } 29875897234Sdrh 29975897234Sdrh /* 30075897234Sdrh ** Run the parser on the given SQL string. The parser structure is 30175897234Sdrh ** passed in. Return the number of errors. 30275897234Sdrh */ 30375897234Sdrh int sqliteRunParser(Parse *pParse, char *zSql, char **pzErrMsg){ 30475897234Sdrh int nErr = 0; 30575897234Sdrh int i; 30675897234Sdrh void *pEngine; 30775897234Sdrh int once = 1; 30875897234Sdrh static FILE *trace = 0; 30975897234Sdrh extern void *sqliteParserAlloc(void*(*)(int)); 31075897234Sdrh extern void sqliteParserFree(void*, void(*)(void*)); 31175897234Sdrh extern int sqliteParser(void*, int, ...); 31275897234Sdrh extern void sqliteParserTrace(FILE*, char *); 31375897234Sdrh 31475897234Sdrh i = 0; 3152282792aSdrh sqliteParseInfoReset(pParse); 316982cef7eSdrh pEngine = sqliteParserAlloc((void*(*)(int))malloc); 31775897234Sdrh if( pEngine==0 ){ 31875897234Sdrh sqliteSetString(pzErrMsg, "out of memory", 0); 31975897234Sdrh return 1; 32075897234Sdrh } 32175897234Sdrh sqliteParserTrace(trace, "parser: "); 32275897234Sdrh while( nErr==0 && i>=0 && zSql[i]!=0 ){ 32375897234Sdrh int tokenType; 32475897234Sdrh 32575897234Sdrh pParse->sLastToken.z = &zSql[i]; 32675897234Sdrh pParse->sLastToken.n = sqliteGetToken(&zSql[i], &tokenType); 32775897234Sdrh i += pParse->sLastToken.n; 32875897234Sdrh if( once ){ 32975897234Sdrh pParse->sFirstToken = pParse->sLastToken; 33075897234Sdrh once = 0; 33175897234Sdrh } 33275897234Sdrh switch( tokenType ){ 33375897234Sdrh case TK_SPACE: 33475897234Sdrh break; 33575897234Sdrh case TK_COMMENT: { 33675897234Sdrh /* Various debugging modes can be turned on and off using 33775897234Sdrh ** special SQL comments. Check for the special comments 33875897234Sdrh ** here and take approriate action if found. 33975897234Sdrh */ 3406e142f54Sdrh #ifndef NDEBUG 34175897234Sdrh char *z = pParse->sLastToken.z; 34275897234Sdrh if( sqliteStrNICmp(z,"--parser-trace-on--",19)==0 ){ 34375897234Sdrh trace = stderr; 34475897234Sdrh sqliteParserTrace(trace, "parser: "); 34575897234Sdrh }else if( sqliteStrNICmp(z,"--parser-trace-off--", 20)==0 ){ 34675897234Sdrh trace = 0; 34775897234Sdrh sqliteParserTrace(trace, "parser: "); 34875897234Sdrh }else if( sqliteStrNICmp(z,"--vdbe-trace-on--",17)==0 ){ 34975897234Sdrh pParse->db->flags |= SQLITE_VdbeTrace; 350670f74f5Sdrh }else if( sqliteStrNICmp(z,"--vdbe-trace-off--", 18)==0 ){ 35175897234Sdrh pParse->db->flags &= ~SQLITE_VdbeTrace; 3526e142f54Sdrh #ifdef MEMORY_DEBUG 353*30cab809Sdrh }else if( sqliteStrNICmp(z,"--malloc-fail=",14)==0 ){ 3546e142f54Sdrh sqlite_iMallocFail = atoi(&z[14]); 3556e142f54Sdrh }else if( sqliteStrNICmp(z,"--malloc-stats--", 16)==0 ){ 3566e142f54Sdrh if( pParse->xCallback ){ 3576e142f54Sdrh static char *azName[4] = {"malloc", "free", "to_fail", 0 }; 3586e142f54Sdrh char *azArg[4]; 3596e142f54Sdrh char zVal[3][30]; 3606e142f54Sdrh sprintf(zVal[0],"%d", sqlite_nMalloc); 3616e142f54Sdrh sprintf(zVal[1],"%d", sqlite_nFree); 3626e142f54Sdrh sprintf(zVal[2],"%d", sqlite_iMallocFail); 3636e142f54Sdrh azArg[0] = zVal[0]; 3646e142f54Sdrh azArg[1] = zVal[1]; 3656e142f54Sdrh azArg[2] = zVal[2]; 3666e142f54Sdrh azArg[3] = 0; 3676e142f54Sdrh pParse->xCallback(pParse->pArg, 3, azArg, azName); 36875897234Sdrh } 3696e142f54Sdrh #endif 3706e142f54Sdrh } 3716e142f54Sdrh #endif 37275897234Sdrh break; 37375897234Sdrh } 37475897234Sdrh case TK_ILLEGAL: 375c837e709Sdrh sqliteSetNString(pzErrMsg, "unrecognized token: \"", -1, 376c837e709Sdrh pParse->sLastToken.z, pParse->sLastToken.n, "\"", 1, 0); 37775897234Sdrh nErr++; 37875897234Sdrh break; 37975897234Sdrh default: 38075897234Sdrh sqliteParser(pEngine, tokenType, pParse->sLastToken, pParse); 3816e142f54Sdrh if( pParse->zErrMsg && pParse->sErrToken.z ){ 38275897234Sdrh sqliteSetNString(pzErrMsg, "near \"", -1, 38375897234Sdrh pParse->sErrToken.z, pParse->sErrToken.n, 38475897234Sdrh "\": ", -1, 38575897234Sdrh pParse->zErrMsg, -1, 38675897234Sdrh 0); 38775897234Sdrh nErr++; 3886e142f54Sdrh sqliteFree(pParse->zErrMsg); 3896e142f54Sdrh pParse->zErrMsg = 0; 39075897234Sdrh } 39175897234Sdrh break; 39275897234Sdrh } 39375897234Sdrh } 39475897234Sdrh if( nErr==0 ){ 39575897234Sdrh sqliteParser(pEngine, 0, pParse->sLastToken, pParse); 3966e142f54Sdrh if( pParse->zErrMsg && pParse->sErrToken.z ){ 39775897234Sdrh sqliteSetNString(pzErrMsg, "near \"", -1, 39875897234Sdrh pParse->sErrToken.z, pParse->sErrToken.n, 39975897234Sdrh "\": ", -1, 40075897234Sdrh pParse->zErrMsg, -1, 40175897234Sdrh 0); 40275897234Sdrh nErr++; 4036e142f54Sdrh sqliteFree(pParse->zErrMsg); 4046e142f54Sdrh pParse->zErrMsg = 0; 40575897234Sdrh } 40675897234Sdrh } 407dcc581ccSdrh sqliteParserFree(pEngine, free); 40875897234Sdrh if( pParse->zErrMsg ){ 40975897234Sdrh if( pzErrMsg ){ 4106e142f54Sdrh sqliteFree(*pzErrMsg); 41175897234Sdrh *pzErrMsg = pParse->zErrMsg; 41275897234Sdrh }else{ 41375897234Sdrh sqliteFree(pParse->zErrMsg); 41475897234Sdrh } 41575897234Sdrh if( !nErr ) nErr++; 41675897234Sdrh } 41775897234Sdrh if( pParse->pVdbe ){ 41875897234Sdrh sqliteVdbeDelete(pParse->pVdbe); 41975897234Sdrh pParse->pVdbe = 0; 42075897234Sdrh } 42175897234Sdrh if( pParse->pNewTable ){ 42275897234Sdrh sqliteDeleteTable(pParse->db, pParse->pNewTable); 42375897234Sdrh pParse->pNewTable = 0; 42475897234Sdrh } 4252282792aSdrh sqliteParseInfoReset(pParse); 42675897234Sdrh return nErr; 42775897234Sdrh } 428