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*4e0cff60Sdrh ** $Id: tokenize.c,v 1.95 2004/11/05 05:10:29 drh Exp $ 1975897234Sdrh */ 2075897234Sdrh #include "sqliteInt.h" 21ad75e987Sdrh #include "os.h" 2275897234Sdrh #include <ctype.h> 23dcc581ccSdrh #include <stdlib.h> 2475897234Sdrh 2575897234Sdrh /* 2652fb6d71Sdrh ** The sqlite3KeywordCode function looks up an identifier to determine if 2752fb6d71Sdrh ** it is a keyword. If it is a keyword, the token code of that keyword is 2875897234Sdrh ** returned. If the input is not a keyword, TK_ID is returned. 292090a0edSdrh ** 302090a0edSdrh ** The implementation of this routine was generated by a program, 312090a0edSdrh ** mkkeywordhash.c, located in the tool subdirectory of the distribution. 3252fb6d71Sdrh ** The output of the mkkeywordhash.c program is written into a file 3352fb6d71Sdrh ** named keywordhash.c and then included into this source file by 3452fb6d71Sdrh ** the #include below. 3575897234Sdrh */ 3652fb6d71Sdrh #include "keywordhash.c" 3775897234Sdrh 3840f20f7dSdrh 3998808babSdrh /* 40ba21256fSdrh ** If X is a character that can be used in an identifier and 41ba21256fSdrh ** X&0x80==0 then isIdChar[X] will be 1. If X&0x80==0x80 then 42ba21256fSdrh ** X is always an identifier character. (Hence all UTF-8 43ba21256fSdrh ** characters can be part of an identifier). isIdChar[X] will 44ba21256fSdrh ** be 0 for every character in the lower 128 ASCII characters 45ba21256fSdrh ** that cannot be used as part of an identifier. 4698808babSdrh ** 4798808babSdrh ** In this implementation, an identifier can be a string of 4898808babSdrh ** alphabetic characters, digits, and "_" plus any character 4998808babSdrh ** with the high-order bit set. The latter rule means that 5098808babSdrh ** any sequence of UTF-8 characters or characters taken from 5198808babSdrh ** an extended ISO8859 character set can form an identifier. 5298808babSdrh */ 5398808babSdrh static const char isIdChar[] = { 5498808babSdrh /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ 5598808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */ 5698808babSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */ 5798808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */ 5898808babSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */ 5998808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */ 6098808babSdrh }; 6198808babSdrh 62aa756b09Sdrh #define IdChar(C) (((c=C)&0x80)!=0 || (c>0x2f && isIdChar[c-0x30])) 6398808babSdrh 6475897234Sdrh /* 6561b487d0Sdrh ** Return the length of the token that begins at z[0]. 6661b487d0Sdrh ** Store the token type in *tokenType before returning. 6775897234Sdrh */ 6898808babSdrh static int sqliteGetToken(const unsigned char *z, int *tokenType){ 69aa756b09Sdrh int i, c; 7075897234Sdrh switch( *z ){ 7130cab809Sdrh case ' ': case '\t': case '\n': case '\f': case '\r': { 7232eb7b47Sdrh for(i=1; isspace(z[i]); i++){} 7375897234Sdrh *tokenType = TK_SPACE; 7475897234Sdrh return i; 7575897234Sdrh } 7675897234Sdrh case '-': { 7775897234Sdrh if( z[1]=='-' ){ 78aa756b09Sdrh for(i=2; (c=z[i])!=0 && c!='\n'; i++){} 7975897234Sdrh *tokenType = TK_COMMENT; 8075897234Sdrh return i; 8175897234Sdrh } 8275897234Sdrh *tokenType = TK_MINUS; 8375897234Sdrh return 1; 8475897234Sdrh } 8575897234Sdrh case '(': { 8675897234Sdrh *tokenType = TK_LP; 8775897234Sdrh return 1; 8875897234Sdrh } 8975897234Sdrh case ')': { 9075897234Sdrh *tokenType = TK_RP; 9175897234Sdrh return 1; 9275897234Sdrh } 9375897234Sdrh case ';': { 9475897234Sdrh *tokenType = TK_SEMI; 9575897234Sdrh return 1; 9675897234Sdrh } 9775897234Sdrh case '+': { 9875897234Sdrh *tokenType = TK_PLUS; 9975897234Sdrh return 1; 10075897234Sdrh } 10175897234Sdrh case '*': { 10275897234Sdrh *tokenType = TK_STAR; 10375897234Sdrh return 1; 10475897234Sdrh } 10575897234Sdrh case '/': { 10666105a8eSdrh if( z[1]!='*' || z[2]==0 ){ 10775897234Sdrh *tokenType = TK_SLASH; 10875897234Sdrh return 1; 10975897234Sdrh } 110aa756b09Sdrh for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){} 111aa756b09Sdrh if( c ) i++; 11266105a8eSdrh *tokenType = TK_COMMENT; 11366105a8eSdrh return i; 11466105a8eSdrh } 115bf4133cbSdrh case '%': { 116bf4133cbSdrh *tokenType = TK_REM; 117bf4133cbSdrh return 1; 118bf4133cbSdrh } 11975897234Sdrh case '=': { 12075897234Sdrh *tokenType = TK_EQ; 12175897234Sdrh return 1 + (z[1]=='='); 12275897234Sdrh } 12375897234Sdrh case '<': { 124aa756b09Sdrh if( (c=z[1])=='=' ){ 12575897234Sdrh *tokenType = TK_LE; 12675897234Sdrh return 2; 127aa756b09Sdrh }else if( c=='>' ){ 12875897234Sdrh *tokenType = TK_NE; 12975897234Sdrh return 2; 130aa756b09Sdrh }else if( c=='<' ){ 131bf4133cbSdrh *tokenType = TK_LSHIFT; 132bf4133cbSdrh return 2; 13375897234Sdrh }else{ 13475897234Sdrh *tokenType = TK_LT; 13575897234Sdrh return 1; 13675897234Sdrh } 13775897234Sdrh } 13875897234Sdrh case '>': { 139aa756b09Sdrh if( (c=z[1])=='=' ){ 14075897234Sdrh *tokenType = TK_GE; 14175897234Sdrh return 2; 142aa756b09Sdrh }else if( c=='>' ){ 143bf4133cbSdrh *tokenType = TK_RSHIFT; 144bf4133cbSdrh return 2; 14575897234Sdrh }else{ 14675897234Sdrh *tokenType = TK_GT; 14775897234Sdrh return 1; 14875897234Sdrh } 14975897234Sdrh } 15075897234Sdrh case '!': { 15175897234Sdrh if( z[1]!='=' ){ 15275897234Sdrh *tokenType = TK_ILLEGAL; 153c837e709Sdrh return 2; 15475897234Sdrh }else{ 15575897234Sdrh *tokenType = TK_NE; 15675897234Sdrh return 2; 15775897234Sdrh } 15875897234Sdrh } 1590040077dSdrh case '|': { 1600040077dSdrh if( z[1]!='|' ){ 161bf4133cbSdrh *tokenType = TK_BITOR; 1620040077dSdrh return 1; 1630040077dSdrh }else{ 1640040077dSdrh *tokenType = TK_CONCAT; 1650040077dSdrh return 2; 1660040077dSdrh } 1670040077dSdrh } 16875897234Sdrh case ',': { 16975897234Sdrh *tokenType = TK_COMMA; 17075897234Sdrh return 1; 17175897234Sdrh } 172bf4133cbSdrh case '&': { 173bf4133cbSdrh *tokenType = TK_BITAND; 174bf4133cbSdrh return 1; 175bf4133cbSdrh } 176bf4133cbSdrh case '~': { 177bf4133cbSdrh *tokenType = TK_BITNOT; 178bf4133cbSdrh return 1; 179bf4133cbSdrh } 180*4e0cff60Sdrh case '#': { 181*4e0cff60Sdrh for(i=1; isdigit(z[i]); i++){} 182*4e0cff60Sdrh *tokenType = TK_REGISTER; 183*4e0cff60Sdrh return i; 184*4e0cff60Sdrh } 18575897234Sdrh case '\'': case '"': { 18675897234Sdrh int delim = z[0]; 187aa756b09Sdrh for(i=1; (c=z[i])!=0; i++){ 188aa756b09Sdrh if( c==delim ){ 18975897234Sdrh if( z[i+1]==delim ){ 19075897234Sdrh i++; 19175897234Sdrh }else{ 19275897234Sdrh break; 19375897234Sdrh } 19475897234Sdrh } 19575897234Sdrh } 196aa756b09Sdrh if( c ) i++; 19775897234Sdrh *tokenType = TK_STRING; 19875897234Sdrh return i; 19975897234Sdrh } 20075897234Sdrh case '.': { 20175897234Sdrh *tokenType = TK_DOT; 20275897234Sdrh return 1; 20375897234Sdrh } 20475897234Sdrh case '0': case '1': case '2': case '3': case '4': 20575897234Sdrh case '5': case '6': case '7': case '8': case '9': { 206c837e709Sdrh *tokenType = TK_INTEGER; 20732eb7b47Sdrh for(i=1; isdigit(z[i]); i++){} 208b7f9164eSdrh #ifndef SQLITE_OMIT_FLOATING_POINT 209bb07e9a3Sdrh if( z[i]=='.' && isdigit(z[i+1]) ){ 210bb07e9a3Sdrh i += 2; 21132eb7b47Sdrh while( isdigit(z[i]) ){ i++; } 212c837e709Sdrh *tokenType = TK_FLOAT; 213c837e709Sdrh } 21475897234Sdrh if( (z[i]=='e' || z[i]=='E') && 21575897234Sdrh ( isdigit(z[i+1]) 21675897234Sdrh || ((z[i+1]=='+' || z[i+1]=='-') && isdigit(z[i+2])) 21775897234Sdrh ) 21875897234Sdrh ){ 21975897234Sdrh i += 2; 22032eb7b47Sdrh while( isdigit(z[i]) ){ i++; } 22175897234Sdrh *tokenType = TK_FLOAT; 22275897234Sdrh } 223b7f9164eSdrh #endif 22475897234Sdrh return i; 22575897234Sdrh } 2262f4392ffSdrh case '[': { 227aa756b09Sdrh for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){} 2282f4392ffSdrh *tokenType = TK_ID; 2292f4392ffSdrh return i; 2302f4392ffSdrh } 2317c972decSdrh case '?': { 23250457896Sdrh *tokenType = TK_VARIABLE; 233fa6bc000Sdrh for(i=1; isdigit(z[i]); i++){} 234fa6bc000Sdrh return i; 23550457896Sdrh } 236895d7472Sdrh case ':': { 237aa756b09Sdrh for(i=1; IdChar(z[i]); i++){} 2382c6674cfSdrh *tokenType = i>1 ? TK_VARIABLE : TK_ILLEGAL; 239895d7472Sdrh return i; 240895d7472Sdrh } 241b7f9164eSdrh #ifndef SQLITE_OMIT_TCL_VARIABLE 242895d7472Sdrh case '$': { 2439d74b4c5Sdrh *tokenType = TK_VARIABLE; 244895d7472Sdrh if( z[1]=='{' ){ 245895d7472Sdrh int nBrace = 1; 246895d7472Sdrh for(i=2; (c=z[i])!=0 && nBrace; i++){ 247895d7472Sdrh if( c=='{' ){ 248895d7472Sdrh nBrace++; 249895d7472Sdrh }else if( c=='}' ){ 250895d7472Sdrh nBrace--; 251895d7472Sdrh } 252895d7472Sdrh } 2539d74b4c5Sdrh if( c==0 ) *tokenType = TK_ILLEGAL; 254895d7472Sdrh }else{ 255895d7472Sdrh int n = 0; 256895d7472Sdrh for(i=1; (c=z[i])!=0; i++){ 257895d7472Sdrh if( isalnum(c) || c=='_' ){ 258895d7472Sdrh n++; 259895d7472Sdrh }else if( c=='(' && n>0 ){ 260895d7472Sdrh do{ 261895d7472Sdrh i++; 262895d7472Sdrh }while( (c=z[i])!=0 && !isspace(c) && c!=')' ); 263895d7472Sdrh if( c==')' ){ 264895d7472Sdrh i++; 265895d7472Sdrh }else{ 266895d7472Sdrh *tokenType = TK_ILLEGAL; 267895d7472Sdrh } 268895d7472Sdrh break; 269895d7472Sdrh }else if( c==':' && z[i+1]==':' ){ 270895d7472Sdrh i++; 271895d7472Sdrh }else{ 272895d7472Sdrh break; 273895d7472Sdrh } 274895d7472Sdrh } 2759d74b4c5Sdrh if( n==0 ) *tokenType = TK_ILLEGAL; 276895d7472Sdrh } 277895d7472Sdrh return i; 278895d7472Sdrh } 279b7f9164eSdrh #endif 280b7f9164eSdrh #ifndef SQLITE_OMIT_BLOB_LITERAL 281c572ef7fSdanielk1977 case 'x': case 'X': { 282aa756b09Sdrh if( (c=z[1])=='\'' || c=='"' ){ 283aa756b09Sdrh int delim = c; 2843fd0a736Sdanielk1977 *tokenType = TK_BLOB; 285aa756b09Sdrh for(i=2; (c=z[i])!=0; i++){ 286aa756b09Sdrh if( c==delim ){ 2873fd0a736Sdanielk1977 if( i%2 ) *tokenType = TK_ILLEGAL; 288c572ef7fSdanielk1977 break; 289c572ef7fSdanielk1977 } 290aa756b09Sdrh if( !isxdigit(c) ){ 2913fd0a736Sdanielk1977 *tokenType = TK_ILLEGAL; 2923fd0a736Sdanielk1977 return i; 2933fd0a736Sdanielk1977 } 294c572ef7fSdanielk1977 } 295aa756b09Sdrh if( c ) i++; 296c572ef7fSdanielk1977 return i; 297c572ef7fSdanielk1977 } 298c572ef7fSdanielk1977 /* Otherwise fall through to the next case */ 299c572ef7fSdanielk1977 } 300b7f9164eSdrh #endif 30198808babSdrh default: { 302aa756b09Sdrh if( !IdChar(*z) ){ 30398808babSdrh break; 30498808babSdrh } 305aa756b09Sdrh for(i=1; IdChar(z[i]); i++){} 3064adee20fSdanielk1977 *tokenType = sqlite3KeywordCode((char*)z, i); 30775897234Sdrh return i; 30875897234Sdrh } 30975897234Sdrh } 31075897234Sdrh *tokenType = TK_ILLEGAL; 31175897234Sdrh return 1; 31275897234Sdrh } 31375897234Sdrh 31475897234Sdrh /* 31575897234Sdrh ** Run the parser on the given SQL string. The parser structure is 316b19a2bc6Sdrh ** passed in. An SQLITE_ status code is returned. If an error occurs 317b19a2bc6Sdrh ** and pzErrMsg!=NULL then an error message might be written into 318b19a2bc6Sdrh ** memory obtained from malloc() and *pzErrMsg made to point to that 319b19a2bc6Sdrh ** error message. Or maybe not. 32075897234Sdrh */ 3214adee20fSdanielk1977 int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){ 32275897234Sdrh int nErr = 0; 32375897234Sdrh int i; 32475897234Sdrh void *pEngine; 325b86ccfb2Sdrh int tokenType; 326b86ccfb2Sdrh int lastTokenParsed = -1; 3279bb575fdSdrh sqlite3 *db = pParse->db; 3284adee20fSdanielk1977 extern void *sqlite3ParserAlloc(void*(*)(int)); 3294adee20fSdanielk1977 extern void sqlite3ParserFree(void*, void(*)(void*)); 3304adee20fSdanielk1977 extern int sqlite3Parser(void*, int, Token, Parse*); 33175897234Sdrh 3326d4abfbeSdrh db->flags &= ~SQLITE_Interrupt; 3334c504391Sdrh pParse->rc = SQLITE_OK; 33475897234Sdrh i = 0; 3354adee20fSdanielk1977 pEngine = sqlite3ParserAlloc((void*(*)(int))malloc); 33675897234Sdrh if( pEngine==0 ){ 3374adee20fSdanielk1977 sqlite3SetString(pzErrMsg, "out of memory", (char*)0); 33875897234Sdrh return 1; 33975897234Sdrh } 340fa6bc000Sdrh assert( pParse->sLastToken.dyn==0 ); 341fa6bc000Sdrh assert( pParse->pNewTable==0 ); 342fa6bc000Sdrh assert( pParse->pNewTrigger==0 ); 343fa6bc000Sdrh assert( pParse->nVar==0 ); 344fa6bc000Sdrh assert( pParse->nVarExpr==0 ); 345fa6bc000Sdrh assert( pParse->nVarExprAlloc==0 ); 346fa6bc000Sdrh assert( pParse->apVarExpr==0 ); 3473f7d4e49Sdrh pParse->zTail = pParse->zSql = zSql; 3486f8a503dSdanielk1977 while( sqlite3_malloc_failed==0 && zSql[i]!=0 ){ 34932eb7b47Sdrh assert( i>=0 ); 35075897234Sdrh pParse->sLastToken.z = &zSql[i]; 35132eb7b47Sdrh assert( pParse->sLastToken.dyn==0 ); 35298808babSdrh pParse->sLastToken.n = sqliteGetToken((unsigned char*)&zSql[i], &tokenType); 35375897234Sdrh i += pParse->sLastToken.n; 35475897234Sdrh switch( tokenType ){ 35575897234Sdrh case TK_SPACE: 35675897234Sdrh case TK_COMMENT: { 35732eb7b47Sdrh if( (db->flags & SQLITE_Interrupt)!=0 ){ 35832eb7b47Sdrh pParse->rc = SQLITE_INTERRUPT; 3594adee20fSdanielk1977 sqlite3SetString(pzErrMsg, "interrupt", (char*)0); 36032eb7b47Sdrh goto abort_parse; 36132eb7b47Sdrh } 36275897234Sdrh break; 36375897234Sdrh } 36432eb7b47Sdrh case TK_ILLEGAL: { 365ae29ffbeSdrh if( pzErrMsg ){ 366ae29ffbeSdrh sqliteFree(*pzErrMsg); 367ae29ffbeSdrh *pzErrMsg = sqlite3MPrintf("unrecognized token: \"%T\"", 368ae29ffbeSdrh &pParse->sLastToken); 369ae29ffbeSdrh } 37075897234Sdrh nErr++; 371caec2f12Sdrh goto abort_parse; 37232eb7b47Sdrh } 373326dce74Sdrh case TK_SEMI: { 374326dce74Sdrh pParse->zTail = &zSql[i]; 375326dce74Sdrh /* Fall thru into the default case */ 376326dce74Sdrh } 37732eb7b47Sdrh default: { 3784adee20fSdanielk1977 sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse); 379b86ccfb2Sdrh lastTokenParsed = tokenType; 380b86ccfb2Sdrh if( pParse->rc!=SQLITE_OK ){ 38132eb7b47Sdrh goto abort_parse; 38275897234Sdrh } 38375897234Sdrh break; 38475897234Sdrh } 38575897234Sdrh } 38632eb7b47Sdrh } 38732eb7b47Sdrh abort_parse: 388b86ccfb2Sdrh if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){ 389b86ccfb2Sdrh if( lastTokenParsed!=TK_SEMI ){ 3904adee20fSdanielk1977 sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse); 391326dce74Sdrh pParse->zTail = &zSql[i]; 39275897234Sdrh } 3934adee20fSdanielk1977 sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse); 39475897234Sdrh } 3954adee20fSdanielk1977 sqlite3ParserFree(pEngine, free); 39671c697efSdrh if( sqlite3_malloc_failed ){ 39771c697efSdrh pParse->rc = SQLITE_NOMEM; 39871c697efSdrh } 399b86ccfb2Sdrh if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){ 400f20b21c8Sdanielk1977 sqlite3SetString(&pParse->zErrMsg, sqlite3ErrStr(pParse->rc), 40141743984Sdrh (char*)0); 402b86ccfb2Sdrh } 40375897234Sdrh if( pParse->zErrMsg ){ 404b86ccfb2Sdrh if( pzErrMsg && *pzErrMsg==0 ){ 40575897234Sdrh *pzErrMsg = pParse->zErrMsg; 40675897234Sdrh }else{ 40775897234Sdrh sqliteFree(pParse->zErrMsg); 40875897234Sdrh } 409b86ccfb2Sdrh pParse->zErrMsg = 0; 41075897234Sdrh if( !nErr ) nErr++; 41175897234Sdrh } 412826fb5a3Sdrh if( pParse->pVdbe && pParse->nErr>0 ){ 4134adee20fSdanielk1977 sqlite3VdbeDelete(pParse->pVdbe); 41475897234Sdrh pParse->pVdbe = 0; 41575897234Sdrh } 4164adee20fSdanielk1977 sqlite3DeleteTable(pParse->db, pParse->pNewTable); 4174adee20fSdanielk1977 sqlite3DeleteTrigger(pParse->pNewTrigger); 418fa6bc000Sdrh sqliteFree(pParse->apVarExpr); 419b86ccfb2Sdrh if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){ 4204c504391Sdrh pParse->rc = SQLITE_ERROR; 4214c504391Sdrh } 42275897234Sdrh return nErr; 42375897234Sdrh } 4247ad4334aSdrh 4257ad4334aSdrh /* 4266f8a503dSdanielk1977 ** Token types used by the sqlite3_complete() routine. See the header 4277ad4334aSdrh ** comments on that procedure for additional information. 4287ad4334aSdrh */ 429b7f9164eSdrh #define tkSEMI 0 430b7f9164eSdrh #define tkWS 1 431b7f9164eSdrh #define tkOTHER 2 432b7f9164eSdrh #define tkEXPLAIN 3 433b7f9164eSdrh #define tkCREATE 4 434b7f9164eSdrh #define tkTEMP 5 435b7f9164eSdrh #define tkTRIGGER 6 436b7f9164eSdrh #define tkEND 7 4377ad4334aSdrh 4387ad4334aSdrh /* 4397ad4334aSdrh ** Return TRUE if the given SQL string ends in a semicolon. 4407ad4334aSdrh ** 4417ad4334aSdrh ** Special handling is require for CREATE TRIGGER statements. 4427ad4334aSdrh ** Whenever the CREATE TRIGGER keywords are seen, the statement 4437ad4334aSdrh ** must end with ";END;". 4447ad4334aSdrh ** 4457ad4334aSdrh ** This implementation uses a state machine with 7 states: 4467ad4334aSdrh ** 4477ad4334aSdrh ** (0) START At the beginning or end of an SQL statement. This routine 4487ad4334aSdrh ** returns 1 if it ends in the START state and 0 if it ends 4497ad4334aSdrh ** in any other state. 4507ad4334aSdrh ** 451b7f9164eSdrh ** (1) NORMAL We are in the middle of statement which ends with a single 452b7f9164eSdrh ** semicolon. 453b7f9164eSdrh ** 454b7f9164eSdrh ** (2) EXPLAIN The keyword EXPLAIN has been seen at the beginning of 4557ad4334aSdrh ** a statement. 4567ad4334aSdrh ** 457b7f9164eSdrh ** (3) CREATE The keyword CREATE has been seen at the beginning of a 4587ad4334aSdrh ** statement, possibly preceeded by EXPLAIN and/or followed by 4597ad4334aSdrh ** TEMP or TEMPORARY 4607ad4334aSdrh ** 4617ad4334aSdrh ** (4) TRIGGER We are in the middle of a trigger definition that must be 4627ad4334aSdrh ** ended by a semicolon, the keyword END, and another semicolon. 4637ad4334aSdrh ** 4647ad4334aSdrh ** (5) SEMI We've seen the first semicolon in the ";END;" that occurs at 4657ad4334aSdrh ** the end of a trigger definition. 4667ad4334aSdrh ** 4677ad4334aSdrh ** (6) END We've seen the ";END" of the ";END;" that occurs at the end 4687ad4334aSdrh ** of a trigger difinition. 4697ad4334aSdrh ** 4707ad4334aSdrh ** Transitions between states above are determined by tokens extracted 4717ad4334aSdrh ** from the input. The following tokens are significant: 4727ad4334aSdrh ** 473b7f9164eSdrh ** (0) tkSEMI A semicolon. 474b7f9164eSdrh ** (1) tkWS Whitespace 475b7f9164eSdrh ** (2) tkOTHER Any other SQL token. 476b7f9164eSdrh ** (3) tkEXPLAIN The "explain" keyword. 477b7f9164eSdrh ** (4) tkCREATE The "create" keyword. 478b7f9164eSdrh ** (5) tkTEMP The "temp" or "temporary" keyword. 479b7f9164eSdrh ** (6) tkTRIGGER The "trigger" keyword. 480b7f9164eSdrh ** (7) tkEND The "end" keyword. 4817ad4334aSdrh ** 4827ad4334aSdrh ** Whitespace never causes a state transition and is always ignored. 483b7f9164eSdrh ** 484b7f9164eSdrh ** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed 485b7f9164eSdrh ** to recognize the end of a trigger can be omitted. All we have to do 486b7f9164eSdrh ** is look for a semicolon that is not part of an string or comment. 4877ad4334aSdrh */ 4886f8a503dSdanielk1977 int sqlite3_complete(const char *zSql){ 4897ad4334aSdrh u8 state = 0; /* Current state, using numbers defined in header comment */ 4907ad4334aSdrh u8 token; /* Value of the next token */ 4917ad4334aSdrh 492b7f9164eSdrh #ifndef SQLITE_OMIT_TRIGGER 493b7f9164eSdrh /* A complex statement machine used to detect the end of a CREATE TRIGGER 494b7f9164eSdrh ** statement. This is the normal case. 4957ad4334aSdrh */ 4967ad4334aSdrh static const u8 trans[7][8] = { 497e1e38c4dSdrh /* Token: */ 498b7f9164eSdrh /* State: ** SEMI WS OTHER EXPLAIN CREATE TEMP TRIGGER END */ 499b7f9164eSdrh /* 0 START: */ { 0, 0, 1, 2, 3, 1, 1, 1, }, 500b7f9164eSdrh /* 1 NORMAL: */ { 0, 1, 1, 1, 1, 1, 1, 1, }, 501b7f9164eSdrh /* 2 EXPLAIN: */ { 0, 2, 1, 1, 3, 1, 1, 1, }, 502b7f9164eSdrh /* 3 CREATE: */ { 0, 3, 1, 1, 1, 3, 4, 1, }, 503b7f9164eSdrh /* 4 TRIGGER: */ { 5, 4, 4, 4, 4, 4, 4, 4, }, 504b7f9164eSdrh /* 5 SEMI: */ { 5, 5, 4, 4, 4, 4, 4, 6, }, 505b7f9164eSdrh /* 6 END: */ { 0, 6, 4, 4, 4, 4, 4, 4, }, 5067ad4334aSdrh }; 507b7f9164eSdrh #else 508b7f9164eSdrh /* If triggers are not suppored by this compile then the statement machine 509b7f9164eSdrh ** used to detect the end of a statement is much simplier 510b7f9164eSdrh */ 511b7f9164eSdrh static const u8 trans[2][3] = { 512b7f9164eSdrh /* Token: */ 513b7f9164eSdrh /* State: ** SEMI WS OTHER */ 514b7f9164eSdrh /* 0 START: */ { 0, 0, 1, }, 515b7f9164eSdrh /* 1 NORMAL: */ { 0, 1, 1, }, 516b7f9164eSdrh }; 517b7f9164eSdrh #endif /* SQLITE_OMIT_TRIGGER */ 5187ad4334aSdrh 5197ad4334aSdrh while( *zSql ){ 5207ad4334aSdrh switch( *zSql ){ 5217ad4334aSdrh case ';': { /* A semicolon */ 5227ad4334aSdrh token = tkSEMI; 5237ad4334aSdrh break; 5247ad4334aSdrh } 5257ad4334aSdrh case ' ': 5267ad4334aSdrh case '\r': 5277ad4334aSdrh case '\t': 5287ad4334aSdrh case '\n': 5297ad4334aSdrh case '\f': { /* White space is ignored */ 5307ad4334aSdrh token = tkWS; 5317ad4334aSdrh break; 5327ad4334aSdrh } 5337ad4334aSdrh case '/': { /* C-style comments */ 5347ad4334aSdrh if( zSql[1]!='*' ){ 5357ad4334aSdrh token = tkOTHER; 5367ad4334aSdrh break; 5377ad4334aSdrh } 5387ad4334aSdrh zSql += 2; 5397ad4334aSdrh while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; } 5407ad4334aSdrh if( zSql[0]==0 ) return 0; 5417ad4334aSdrh zSql++; 5427ad4334aSdrh token = tkWS; 5437ad4334aSdrh break; 5447ad4334aSdrh } 5457ad4334aSdrh case '-': { /* SQL-style comments from "--" to end of line */ 5467ad4334aSdrh if( zSql[1]!='-' ){ 5477ad4334aSdrh token = tkOTHER; 5487ad4334aSdrh break; 5497ad4334aSdrh } 5507ad4334aSdrh while( *zSql && *zSql!='\n' ){ zSql++; } 5517ad4334aSdrh if( *zSql==0 ) return state==0; 5527ad4334aSdrh token = tkWS; 5537ad4334aSdrh break; 5547ad4334aSdrh } 5557ad4334aSdrh case '[': { /* Microsoft-style identifiers in [...] */ 5567ad4334aSdrh zSql++; 5577ad4334aSdrh while( *zSql && *zSql!=']' ){ zSql++; } 5587ad4334aSdrh if( *zSql==0 ) return 0; 5597ad4334aSdrh token = tkOTHER; 5607ad4334aSdrh break; 5617ad4334aSdrh } 5627ad4334aSdrh case '"': /* single- and double-quoted strings */ 5637ad4334aSdrh case '\'': { 5647ad4334aSdrh int c = *zSql; 5657ad4334aSdrh zSql++; 5667ad4334aSdrh while( *zSql && *zSql!=c ){ zSql++; } 5677ad4334aSdrh if( *zSql==0 ) return 0; 5687ad4334aSdrh token = tkOTHER; 5697ad4334aSdrh break; 5707ad4334aSdrh } 5717ad4334aSdrh default: { 572aa756b09Sdrh int c; 573aa756b09Sdrh if( IdChar((u8)*zSql) ){ 5747ad4334aSdrh /* Keywords and unquoted identifiers */ 5757ad4334aSdrh int nId; 576aa756b09Sdrh for(nId=1; IdChar(zSql[nId]); nId++){} 577b7f9164eSdrh #ifdef SQLITE_OMIT_TRIGGER 578b7f9164eSdrh token = tkOTHER; 579b7f9164eSdrh #else 5807ad4334aSdrh switch( *zSql ){ 5817ad4334aSdrh case 'c': case 'C': { 5824adee20fSdanielk1977 if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){ 5837ad4334aSdrh token = tkCREATE; 5847ad4334aSdrh }else{ 5857ad4334aSdrh token = tkOTHER; 5867ad4334aSdrh } 5877ad4334aSdrh break; 5887ad4334aSdrh } 5897ad4334aSdrh case 't': case 'T': { 5904adee20fSdanielk1977 if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){ 5917ad4334aSdrh token = tkTRIGGER; 5924adee20fSdanielk1977 }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){ 5937ad4334aSdrh token = tkTEMP; 5944adee20fSdanielk1977 }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){ 5957ad4334aSdrh token = tkTEMP; 5967ad4334aSdrh }else{ 5977ad4334aSdrh token = tkOTHER; 5987ad4334aSdrh } 5997ad4334aSdrh break; 6007ad4334aSdrh } 6017ad4334aSdrh case 'e': case 'E': { 6024adee20fSdanielk1977 if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){ 6037ad4334aSdrh token = tkEND; 604b7f9164eSdrh }else 605b7f9164eSdrh #ifndef SQLITE_OMIT_EXPLAIN 606b7f9164eSdrh if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){ 6077ad4334aSdrh token = tkEXPLAIN; 608b7f9164eSdrh }else 609b7f9164eSdrh #endif 610b7f9164eSdrh { 6117ad4334aSdrh token = tkOTHER; 6127ad4334aSdrh } 6137ad4334aSdrh break; 6147ad4334aSdrh } 6157ad4334aSdrh default: { 6167ad4334aSdrh token = tkOTHER; 6177ad4334aSdrh break; 6187ad4334aSdrh } 6197ad4334aSdrh } 620b7f9164eSdrh #endif /* SQLITE_OMIT_TRIGGER */ 6217ad4334aSdrh zSql += nId-1; 6227ad4334aSdrh }else{ 6237ad4334aSdrh /* Operators and special symbols */ 6247ad4334aSdrh token = tkOTHER; 6257ad4334aSdrh } 6267ad4334aSdrh break; 6277ad4334aSdrh } 6287ad4334aSdrh } 6297ad4334aSdrh state = trans[state][token]; 6307ad4334aSdrh zSql++; 6317ad4334aSdrh } 6327ad4334aSdrh return state==0; 6337ad4334aSdrh } 63461de0d1bSdanielk1977 635b7f9164eSdrh #ifndef SQLITE_OMIT_UTF16 63661de0d1bSdanielk1977 /* 63761de0d1bSdanielk1977 ** This routine is the same as the sqlite3_complete() routine described 63861de0d1bSdanielk1977 ** above, except that the parameter is required to be UTF-16 encoded, not 63961de0d1bSdanielk1977 ** UTF-8. 64061de0d1bSdanielk1977 */ 64161de0d1bSdanielk1977 int sqlite3_complete16(const void *zSql){ 642bfd6cce5Sdanielk1977 sqlite3_value *pVal; 6435314c4d7Sdanielk1977 char const *zSql8; 644bfd6cce5Sdanielk1977 int rc = 0; 645bfd6cce5Sdanielk1977 646bfd6cce5Sdanielk1977 pVal = sqlite3ValueNew(); 647bfd6cce5Sdanielk1977 sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC); 648bfd6cce5Sdanielk1977 zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8); 649bfd6cce5Sdanielk1977 if( zSql8 ){ 65061de0d1bSdanielk1977 rc = sqlite3_complete(zSql8); 651bfd6cce5Sdanielk1977 } 652bfd6cce5Sdanielk1977 sqlite3ValueFree(pVal); 65361de0d1bSdanielk1977 return rc; 65461de0d1bSdanielk1977 } 655b7f9164eSdrh #endif /* SQLITE_OMIT_UTF16 */ 656