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*ccae6026Sdrh ** $Id: tokenize.c,v 1.101 2005/02/26 17:31:27 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, 3173b211abSdrh ** mkkeywordhash.h, located in the tool subdirectory of the distribution. 3252fb6d71Sdrh ** The output of the mkkeywordhash.c program is written into a file 3373b211abSdrh ** named keywordhash.h and then included into this source file by 3452fb6d71Sdrh ** the #include below. 3575897234Sdrh */ 3673b211abSdrh #include "keywordhash.h" 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. 52a0d1f663Sdrh ** 53a0d1f663Sdrh ** Ticket #1066. the SQL standard does not allow '$' in the 54a0d1f663Sdrh ** middle of identfiers. But many SQL implementations do. 55a0d1f663Sdrh ** SQLite will allow '$' in identifiers for compatibility. 56a0d1f663Sdrh ** But the feature is undocumented. 5798808babSdrh */ 5898808babSdrh static const char isIdChar[] = { 5998808babSdrh /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ 60a0d1f663Sdrh 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */ 6198808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */ 6298808babSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */ 6398808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */ 6498808babSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */ 6598808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */ 6698808babSdrh }; 6798808babSdrh 68a0d1f663Sdrh #define IdChar(C) (((c=C)&0x80)!=0 || (c>0x1f && isIdChar[c-0x20])) 6998808babSdrh 7075897234Sdrh /* 7161b487d0Sdrh ** Return the length of the token that begins at z[0]. 7261b487d0Sdrh ** Store the token type in *tokenType before returning. 7375897234Sdrh */ 74c60e9b82Sdanielk1977 static int getToken(const unsigned char *z, int *tokenType){ 75aa756b09Sdrh int i, c; 7675897234Sdrh switch( *z ){ 7730cab809Sdrh case ' ': case '\t': case '\n': case '\f': case '\r': { 7832eb7b47Sdrh for(i=1; isspace(z[i]); i++){} 7975897234Sdrh *tokenType = TK_SPACE; 8075897234Sdrh return i; 8175897234Sdrh } 8275897234Sdrh case '-': { 8375897234Sdrh if( z[1]=='-' ){ 84aa756b09Sdrh for(i=2; (c=z[i])!=0 && c!='\n'; i++){} 8575897234Sdrh *tokenType = TK_COMMENT; 8675897234Sdrh return i; 8775897234Sdrh } 8875897234Sdrh *tokenType = TK_MINUS; 8975897234Sdrh return 1; 9075897234Sdrh } 9175897234Sdrh case '(': { 9275897234Sdrh *tokenType = TK_LP; 9375897234Sdrh return 1; 9475897234Sdrh } 9575897234Sdrh case ')': { 9675897234Sdrh *tokenType = TK_RP; 9775897234Sdrh return 1; 9875897234Sdrh } 9975897234Sdrh case ';': { 10075897234Sdrh *tokenType = TK_SEMI; 10175897234Sdrh return 1; 10275897234Sdrh } 10375897234Sdrh case '+': { 10475897234Sdrh *tokenType = TK_PLUS; 10575897234Sdrh return 1; 10675897234Sdrh } 10775897234Sdrh case '*': { 10875897234Sdrh *tokenType = TK_STAR; 10975897234Sdrh return 1; 11075897234Sdrh } 11175897234Sdrh case '/': { 11266105a8eSdrh if( z[1]!='*' || z[2]==0 ){ 11375897234Sdrh *tokenType = TK_SLASH; 11475897234Sdrh return 1; 11575897234Sdrh } 116aa756b09Sdrh for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){} 117aa756b09Sdrh if( c ) i++; 11866105a8eSdrh *tokenType = TK_COMMENT; 11966105a8eSdrh return i; 12066105a8eSdrh } 121bf4133cbSdrh case '%': { 122bf4133cbSdrh *tokenType = TK_REM; 123bf4133cbSdrh return 1; 124bf4133cbSdrh } 12575897234Sdrh case '=': { 12675897234Sdrh *tokenType = TK_EQ; 12775897234Sdrh return 1 + (z[1]=='='); 12875897234Sdrh } 12975897234Sdrh case '<': { 130aa756b09Sdrh if( (c=z[1])=='=' ){ 13175897234Sdrh *tokenType = TK_LE; 13275897234Sdrh return 2; 133aa756b09Sdrh }else if( c=='>' ){ 13475897234Sdrh *tokenType = TK_NE; 13575897234Sdrh return 2; 136aa756b09Sdrh }else if( c=='<' ){ 137bf4133cbSdrh *tokenType = TK_LSHIFT; 138bf4133cbSdrh return 2; 13975897234Sdrh }else{ 14075897234Sdrh *tokenType = TK_LT; 14175897234Sdrh return 1; 14275897234Sdrh } 14375897234Sdrh } 14475897234Sdrh case '>': { 145aa756b09Sdrh if( (c=z[1])=='=' ){ 14675897234Sdrh *tokenType = TK_GE; 14775897234Sdrh return 2; 148aa756b09Sdrh }else if( c=='>' ){ 149bf4133cbSdrh *tokenType = TK_RSHIFT; 150bf4133cbSdrh return 2; 15175897234Sdrh }else{ 15275897234Sdrh *tokenType = TK_GT; 15375897234Sdrh return 1; 15475897234Sdrh } 15575897234Sdrh } 15675897234Sdrh case '!': { 15775897234Sdrh if( z[1]!='=' ){ 15875897234Sdrh *tokenType = TK_ILLEGAL; 159c837e709Sdrh return 2; 16075897234Sdrh }else{ 16175897234Sdrh *tokenType = TK_NE; 16275897234Sdrh return 2; 16375897234Sdrh } 16475897234Sdrh } 1650040077dSdrh case '|': { 1660040077dSdrh if( z[1]!='|' ){ 167bf4133cbSdrh *tokenType = TK_BITOR; 1680040077dSdrh return 1; 1690040077dSdrh }else{ 1700040077dSdrh *tokenType = TK_CONCAT; 1710040077dSdrh return 2; 1720040077dSdrh } 1730040077dSdrh } 17475897234Sdrh case ',': { 17575897234Sdrh *tokenType = TK_COMMA; 17675897234Sdrh return 1; 17775897234Sdrh } 178bf4133cbSdrh case '&': { 179bf4133cbSdrh *tokenType = TK_BITAND; 180bf4133cbSdrh return 1; 181bf4133cbSdrh } 182bf4133cbSdrh case '~': { 183bf4133cbSdrh *tokenType = TK_BITNOT; 184bf4133cbSdrh return 1; 185bf4133cbSdrh } 1864e0cff60Sdrh case '#': { 1872958a4e6Sdrh for(i=1; isdigit(z[i]) || (i==1 && z[1]=='-'); i++){} 1884e0cff60Sdrh *tokenType = TK_REGISTER; 1894e0cff60Sdrh return i; 1904e0cff60Sdrh } 19175897234Sdrh case '\'': case '"': { 19275897234Sdrh int delim = z[0]; 193aa756b09Sdrh for(i=1; (c=z[i])!=0; i++){ 194aa756b09Sdrh if( c==delim ){ 19575897234Sdrh if( z[i+1]==delim ){ 19675897234Sdrh i++; 19775897234Sdrh }else{ 19875897234Sdrh break; 19975897234Sdrh } 20075897234Sdrh } 20175897234Sdrh } 202aa756b09Sdrh if( c ) i++; 20375897234Sdrh *tokenType = TK_STRING; 20475897234Sdrh return i; 20575897234Sdrh } 20675897234Sdrh case '.': { 20775897234Sdrh *tokenType = TK_DOT; 20875897234Sdrh return 1; 20975897234Sdrh } 21075897234Sdrh case '0': case '1': case '2': case '3': case '4': 21175897234Sdrh case '5': case '6': case '7': case '8': case '9': { 212c837e709Sdrh *tokenType = TK_INTEGER; 21332eb7b47Sdrh for(i=1; isdigit(z[i]); i++){} 214b7f9164eSdrh #ifndef SQLITE_OMIT_FLOATING_POINT 215bb07e9a3Sdrh if( z[i]=='.' && isdigit(z[i+1]) ){ 216bb07e9a3Sdrh i += 2; 21732eb7b47Sdrh while( isdigit(z[i]) ){ i++; } 218c837e709Sdrh *tokenType = TK_FLOAT; 219c837e709Sdrh } 22075897234Sdrh if( (z[i]=='e' || z[i]=='E') && 22175897234Sdrh ( isdigit(z[i+1]) 22275897234Sdrh || ((z[i+1]=='+' || z[i+1]=='-') && isdigit(z[i+2])) 22375897234Sdrh ) 22475897234Sdrh ){ 22575897234Sdrh i += 2; 22632eb7b47Sdrh while( isdigit(z[i]) ){ i++; } 22775897234Sdrh *tokenType = TK_FLOAT; 22875897234Sdrh } 229b7f9164eSdrh #endif 23075897234Sdrh return i; 23175897234Sdrh } 2322f4392ffSdrh case '[': { 233aa756b09Sdrh for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){} 2342f4392ffSdrh *tokenType = TK_ID; 2352f4392ffSdrh return i; 2362f4392ffSdrh } 2377c972decSdrh case '?': { 23850457896Sdrh *tokenType = TK_VARIABLE; 239fa6bc000Sdrh for(i=1; isdigit(z[i]); i++){} 240fa6bc000Sdrh return i; 24150457896Sdrh } 242895d7472Sdrh case ':': { 243aa756b09Sdrh for(i=1; IdChar(z[i]); i++){} 2442c6674cfSdrh *tokenType = i>1 ? TK_VARIABLE : TK_ILLEGAL; 245895d7472Sdrh return i; 246895d7472Sdrh } 247b7f9164eSdrh #ifndef SQLITE_OMIT_TCL_VARIABLE 248895d7472Sdrh case '$': { 2499d74b4c5Sdrh *tokenType = TK_VARIABLE; 250895d7472Sdrh if( z[1]=='{' ){ 251895d7472Sdrh int nBrace = 1; 252895d7472Sdrh for(i=2; (c=z[i])!=0 && nBrace; i++){ 253895d7472Sdrh if( c=='{' ){ 254895d7472Sdrh nBrace++; 255895d7472Sdrh }else if( c=='}' ){ 256895d7472Sdrh nBrace--; 257895d7472Sdrh } 258895d7472Sdrh } 2599d74b4c5Sdrh if( c==0 ) *tokenType = TK_ILLEGAL; 260895d7472Sdrh }else{ 261895d7472Sdrh int n = 0; 262895d7472Sdrh for(i=1; (c=z[i])!=0; i++){ 263895d7472Sdrh if( isalnum(c) || c=='_' ){ 264895d7472Sdrh n++; 265895d7472Sdrh }else if( c=='(' && n>0 ){ 266895d7472Sdrh do{ 267895d7472Sdrh i++; 268895d7472Sdrh }while( (c=z[i])!=0 && !isspace(c) && c!=')' ); 269895d7472Sdrh if( c==')' ){ 270895d7472Sdrh i++; 271895d7472Sdrh }else{ 272895d7472Sdrh *tokenType = TK_ILLEGAL; 273895d7472Sdrh } 274895d7472Sdrh break; 275895d7472Sdrh }else if( c==':' && z[i+1]==':' ){ 276895d7472Sdrh i++; 277895d7472Sdrh }else{ 278895d7472Sdrh break; 279895d7472Sdrh } 280895d7472Sdrh } 2819d74b4c5Sdrh if( n==0 ) *tokenType = TK_ILLEGAL; 282895d7472Sdrh } 283895d7472Sdrh return i; 284895d7472Sdrh } 285b7f9164eSdrh #endif 286b7f9164eSdrh #ifndef SQLITE_OMIT_BLOB_LITERAL 287c572ef7fSdanielk1977 case 'x': case 'X': { 288aa756b09Sdrh if( (c=z[1])=='\'' || c=='"' ){ 289aa756b09Sdrh int delim = c; 2903fd0a736Sdanielk1977 *tokenType = TK_BLOB; 291aa756b09Sdrh for(i=2; (c=z[i])!=0; i++){ 292aa756b09Sdrh if( c==delim ){ 2933fd0a736Sdanielk1977 if( i%2 ) *tokenType = TK_ILLEGAL; 294c572ef7fSdanielk1977 break; 295c572ef7fSdanielk1977 } 296aa756b09Sdrh if( !isxdigit(c) ){ 2973fd0a736Sdanielk1977 *tokenType = TK_ILLEGAL; 2983fd0a736Sdanielk1977 return i; 2993fd0a736Sdanielk1977 } 300c572ef7fSdanielk1977 } 301aa756b09Sdrh if( c ) i++; 302c572ef7fSdanielk1977 return i; 303c572ef7fSdanielk1977 } 304c572ef7fSdanielk1977 /* Otherwise fall through to the next case */ 305c572ef7fSdanielk1977 } 306b7f9164eSdrh #endif 30798808babSdrh default: { 308aa756b09Sdrh if( !IdChar(*z) ){ 30998808babSdrh break; 31098808babSdrh } 311aa756b09Sdrh for(i=1; IdChar(z[i]); i++){} 312c60e9b82Sdanielk1977 *tokenType = keywordCode((char*)z, i); 31375897234Sdrh return i; 31475897234Sdrh } 31575897234Sdrh } 31675897234Sdrh *tokenType = TK_ILLEGAL; 31775897234Sdrh return 1; 31875897234Sdrh } 319c60e9b82Sdanielk1977 int sqlite3GetToken(const unsigned char *z, int *tokenType){ 320c60e9b82Sdanielk1977 return getToken(z, tokenType); 321c60e9b82Sdanielk1977 } 32275897234Sdrh 32375897234Sdrh /* 32475897234Sdrh ** Run the parser on the given SQL string. The parser structure is 325b19a2bc6Sdrh ** passed in. An SQLITE_ status code is returned. If an error occurs 326b19a2bc6Sdrh ** and pzErrMsg!=NULL then an error message might be written into 327b19a2bc6Sdrh ** memory obtained from malloc() and *pzErrMsg made to point to that 328b19a2bc6Sdrh ** error message. Or maybe not. 32975897234Sdrh */ 3304adee20fSdanielk1977 int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){ 33175897234Sdrh int nErr = 0; 33275897234Sdrh int i; 33375897234Sdrh void *pEngine; 334b86ccfb2Sdrh int tokenType; 335b86ccfb2Sdrh int lastTokenParsed = -1; 3369bb575fdSdrh sqlite3 *db = pParse->db; 3374adee20fSdanielk1977 extern void *sqlite3ParserAlloc(void*(*)(int)); 3384adee20fSdanielk1977 extern void sqlite3ParserFree(void*, void(*)(void*)); 3394adee20fSdanielk1977 extern int sqlite3Parser(void*, int, Token, Parse*); 34075897234Sdrh 3416d4abfbeSdrh db->flags &= ~SQLITE_Interrupt; 3424c504391Sdrh pParse->rc = SQLITE_OK; 34375897234Sdrh i = 0; 3444adee20fSdanielk1977 pEngine = sqlite3ParserAlloc((void*(*)(int))malloc); 34575897234Sdrh if( pEngine==0 ){ 3464adee20fSdanielk1977 sqlite3SetString(pzErrMsg, "out of memory", (char*)0); 34775897234Sdrh return 1; 34875897234Sdrh } 349fa6bc000Sdrh assert( pParse->sLastToken.dyn==0 ); 350fa6bc000Sdrh assert( pParse->pNewTable==0 ); 351fa6bc000Sdrh assert( pParse->pNewTrigger==0 ); 352fa6bc000Sdrh assert( pParse->nVar==0 ); 353fa6bc000Sdrh assert( pParse->nVarExpr==0 ); 354fa6bc000Sdrh assert( pParse->nVarExprAlloc==0 ); 355fa6bc000Sdrh assert( pParse->apVarExpr==0 ); 3563f7d4e49Sdrh pParse->zTail = pParse->zSql = zSql; 3576f8a503dSdanielk1977 while( sqlite3_malloc_failed==0 && zSql[i]!=0 ){ 35832eb7b47Sdrh assert( i>=0 ); 35975897234Sdrh pParse->sLastToken.z = &zSql[i]; 36032eb7b47Sdrh assert( pParse->sLastToken.dyn==0 ); 361c60e9b82Sdanielk1977 pParse->sLastToken.n = getToken((unsigned char*)&zSql[i],&tokenType); 36275897234Sdrh i += pParse->sLastToken.n; 36375897234Sdrh switch( tokenType ){ 36475897234Sdrh case TK_SPACE: 36575897234Sdrh case TK_COMMENT: { 36632eb7b47Sdrh if( (db->flags & SQLITE_Interrupt)!=0 ){ 36732eb7b47Sdrh pParse->rc = SQLITE_INTERRUPT; 3684adee20fSdanielk1977 sqlite3SetString(pzErrMsg, "interrupt", (char*)0); 36932eb7b47Sdrh goto abort_parse; 37032eb7b47Sdrh } 37175897234Sdrh break; 37275897234Sdrh } 37332eb7b47Sdrh case TK_ILLEGAL: { 374ae29ffbeSdrh if( pzErrMsg ){ 375ae29ffbeSdrh sqliteFree(*pzErrMsg); 376ae29ffbeSdrh *pzErrMsg = sqlite3MPrintf("unrecognized token: \"%T\"", 377ae29ffbeSdrh &pParse->sLastToken); 378ae29ffbeSdrh } 37975897234Sdrh nErr++; 380caec2f12Sdrh goto abort_parse; 38132eb7b47Sdrh } 382326dce74Sdrh case TK_SEMI: { 383326dce74Sdrh pParse->zTail = &zSql[i]; 384326dce74Sdrh /* Fall thru into the default case */ 385326dce74Sdrh } 38632eb7b47Sdrh default: { 3874adee20fSdanielk1977 sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse); 388b86ccfb2Sdrh lastTokenParsed = tokenType; 389b86ccfb2Sdrh if( pParse->rc!=SQLITE_OK ){ 39032eb7b47Sdrh goto abort_parse; 39175897234Sdrh } 39275897234Sdrh break; 39375897234Sdrh } 39475897234Sdrh } 39532eb7b47Sdrh } 39632eb7b47Sdrh abort_parse: 397b86ccfb2Sdrh if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){ 398b86ccfb2Sdrh if( lastTokenParsed!=TK_SEMI ){ 3994adee20fSdanielk1977 sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse); 400326dce74Sdrh pParse->zTail = &zSql[i]; 40175897234Sdrh } 4024adee20fSdanielk1977 sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse); 40375897234Sdrh } 4044adee20fSdanielk1977 sqlite3ParserFree(pEngine, free); 40571c697efSdrh if( sqlite3_malloc_failed ){ 40671c697efSdrh pParse->rc = SQLITE_NOMEM; 40771c697efSdrh } 408b86ccfb2Sdrh if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){ 409f20b21c8Sdanielk1977 sqlite3SetString(&pParse->zErrMsg, sqlite3ErrStr(pParse->rc), 41041743984Sdrh (char*)0); 411b86ccfb2Sdrh } 41275897234Sdrh if( pParse->zErrMsg ){ 413b86ccfb2Sdrh if( pzErrMsg && *pzErrMsg==0 ){ 41475897234Sdrh *pzErrMsg = pParse->zErrMsg; 41575897234Sdrh }else{ 41675897234Sdrh sqliteFree(pParse->zErrMsg); 41775897234Sdrh } 418b86ccfb2Sdrh pParse->zErrMsg = 0; 41975897234Sdrh if( !nErr ) nErr++; 42075897234Sdrh } 4212958a4e6Sdrh if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){ 4224adee20fSdanielk1977 sqlite3VdbeDelete(pParse->pVdbe); 42375897234Sdrh pParse->pVdbe = 0; 42475897234Sdrh } 4254adee20fSdanielk1977 sqlite3DeleteTable(pParse->db, pParse->pNewTable); 4264adee20fSdanielk1977 sqlite3DeleteTrigger(pParse->pNewTrigger); 427fa6bc000Sdrh sqliteFree(pParse->apVarExpr); 428b86ccfb2Sdrh if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){ 4294c504391Sdrh pParse->rc = SQLITE_ERROR; 4304c504391Sdrh } 43175897234Sdrh return nErr; 43275897234Sdrh } 4337ad4334aSdrh 434*ccae6026Sdrh /* The sqlite3_complete() API may be omitted (to save code space) by 435*ccae6026Sdrh ** defining the following symbol. 436*ccae6026Sdrh */ 437*ccae6026Sdrh #ifndef SQLITE_OMIT_COMPLETE 438*ccae6026Sdrh 4397ad4334aSdrh /* 4406f8a503dSdanielk1977 ** Token types used by the sqlite3_complete() routine. See the header 4417ad4334aSdrh ** comments on that procedure for additional information. 4427ad4334aSdrh */ 443b7f9164eSdrh #define tkSEMI 0 444b7f9164eSdrh #define tkWS 1 445b7f9164eSdrh #define tkOTHER 2 446b7f9164eSdrh #define tkEXPLAIN 3 447b7f9164eSdrh #define tkCREATE 4 448b7f9164eSdrh #define tkTEMP 5 449b7f9164eSdrh #define tkTRIGGER 6 450b7f9164eSdrh #define tkEND 7 4517ad4334aSdrh 4527ad4334aSdrh /* 4537ad4334aSdrh ** Return TRUE if the given SQL string ends in a semicolon. 4547ad4334aSdrh ** 4557ad4334aSdrh ** Special handling is require for CREATE TRIGGER statements. 4567ad4334aSdrh ** Whenever the CREATE TRIGGER keywords are seen, the statement 4577ad4334aSdrh ** must end with ";END;". 4587ad4334aSdrh ** 4597ad4334aSdrh ** This implementation uses a state machine with 7 states: 4607ad4334aSdrh ** 4617ad4334aSdrh ** (0) START At the beginning or end of an SQL statement. This routine 4627ad4334aSdrh ** returns 1 if it ends in the START state and 0 if it ends 4637ad4334aSdrh ** in any other state. 4647ad4334aSdrh ** 465b7f9164eSdrh ** (1) NORMAL We are in the middle of statement which ends with a single 466b7f9164eSdrh ** semicolon. 467b7f9164eSdrh ** 468b7f9164eSdrh ** (2) EXPLAIN The keyword EXPLAIN has been seen at the beginning of 4697ad4334aSdrh ** a statement. 4707ad4334aSdrh ** 471b7f9164eSdrh ** (3) CREATE The keyword CREATE has been seen at the beginning of a 4727ad4334aSdrh ** statement, possibly preceeded by EXPLAIN and/or followed by 4737ad4334aSdrh ** TEMP or TEMPORARY 4747ad4334aSdrh ** 4757ad4334aSdrh ** (4) TRIGGER We are in the middle of a trigger definition that must be 4767ad4334aSdrh ** ended by a semicolon, the keyword END, and another semicolon. 4777ad4334aSdrh ** 4787ad4334aSdrh ** (5) SEMI We've seen the first semicolon in the ";END;" that occurs at 4797ad4334aSdrh ** the end of a trigger definition. 4807ad4334aSdrh ** 4817ad4334aSdrh ** (6) END We've seen the ";END" of the ";END;" that occurs at the end 4827ad4334aSdrh ** of a trigger difinition. 4837ad4334aSdrh ** 4847ad4334aSdrh ** Transitions between states above are determined by tokens extracted 4857ad4334aSdrh ** from the input. The following tokens are significant: 4867ad4334aSdrh ** 487b7f9164eSdrh ** (0) tkSEMI A semicolon. 488b7f9164eSdrh ** (1) tkWS Whitespace 489b7f9164eSdrh ** (2) tkOTHER Any other SQL token. 490b7f9164eSdrh ** (3) tkEXPLAIN The "explain" keyword. 491b7f9164eSdrh ** (4) tkCREATE The "create" keyword. 492b7f9164eSdrh ** (5) tkTEMP The "temp" or "temporary" keyword. 493b7f9164eSdrh ** (6) tkTRIGGER The "trigger" keyword. 494b7f9164eSdrh ** (7) tkEND The "end" keyword. 4957ad4334aSdrh ** 4967ad4334aSdrh ** Whitespace never causes a state transition and is always ignored. 497b7f9164eSdrh ** 498b7f9164eSdrh ** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed 499b7f9164eSdrh ** to recognize the end of a trigger can be omitted. All we have to do 500b7f9164eSdrh ** is look for a semicolon that is not part of an string or comment. 5017ad4334aSdrh */ 5026f8a503dSdanielk1977 int sqlite3_complete(const char *zSql){ 5037ad4334aSdrh u8 state = 0; /* Current state, using numbers defined in header comment */ 5047ad4334aSdrh u8 token; /* Value of the next token */ 5057ad4334aSdrh 506b7f9164eSdrh #ifndef SQLITE_OMIT_TRIGGER 507b7f9164eSdrh /* A complex statement machine used to detect the end of a CREATE TRIGGER 508b7f9164eSdrh ** statement. This is the normal case. 5097ad4334aSdrh */ 5107ad4334aSdrh static const u8 trans[7][8] = { 511e1e38c4dSdrh /* Token: */ 512b7f9164eSdrh /* State: ** SEMI WS OTHER EXPLAIN CREATE TEMP TRIGGER END */ 513b7f9164eSdrh /* 0 START: */ { 0, 0, 1, 2, 3, 1, 1, 1, }, 514b7f9164eSdrh /* 1 NORMAL: */ { 0, 1, 1, 1, 1, 1, 1, 1, }, 515b7f9164eSdrh /* 2 EXPLAIN: */ { 0, 2, 1, 1, 3, 1, 1, 1, }, 516b7f9164eSdrh /* 3 CREATE: */ { 0, 3, 1, 1, 1, 3, 4, 1, }, 517b7f9164eSdrh /* 4 TRIGGER: */ { 5, 4, 4, 4, 4, 4, 4, 4, }, 518b7f9164eSdrh /* 5 SEMI: */ { 5, 5, 4, 4, 4, 4, 4, 6, }, 519b7f9164eSdrh /* 6 END: */ { 0, 6, 4, 4, 4, 4, 4, 4, }, 5207ad4334aSdrh }; 521b7f9164eSdrh #else 522b7f9164eSdrh /* If triggers are not suppored by this compile then the statement machine 523b7f9164eSdrh ** used to detect the end of a statement is much simplier 524b7f9164eSdrh */ 525b7f9164eSdrh static const u8 trans[2][3] = { 526b7f9164eSdrh /* Token: */ 527b7f9164eSdrh /* State: ** SEMI WS OTHER */ 528b7f9164eSdrh /* 0 START: */ { 0, 0, 1, }, 529b7f9164eSdrh /* 1 NORMAL: */ { 0, 1, 1, }, 530b7f9164eSdrh }; 531b7f9164eSdrh #endif /* SQLITE_OMIT_TRIGGER */ 5327ad4334aSdrh 5337ad4334aSdrh while( *zSql ){ 5347ad4334aSdrh switch( *zSql ){ 5357ad4334aSdrh case ';': { /* A semicolon */ 5367ad4334aSdrh token = tkSEMI; 5377ad4334aSdrh break; 5387ad4334aSdrh } 5397ad4334aSdrh case ' ': 5407ad4334aSdrh case '\r': 5417ad4334aSdrh case '\t': 5427ad4334aSdrh case '\n': 5437ad4334aSdrh case '\f': { /* White space is ignored */ 5447ad4334aSdrh token = tkWS; 5457ad4334aSdrh break; 5467ad4334aSdrh } 5477ad4334aSdrh case '/': { /* C-style comments */ 5487ad4334aSdrh if( zSql[1]!='*' ){ 5497ad4334aSdrh token = tkOTHER; 5507ad4334aSdrh break; 5517ad4334aSdrh } 5527ad4334aSdrh zSql += 2; 5537ad4334aSdrh while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; } 5547ad4334aSdrh if( zSql[0]==0 ) return 0; 5557ad4334aSdrh zSql++; 5567ad4334aSdrh token = tkWS; 5577ad4334aSdrh break; 5587ad4334aSdrh } 5597ad4334aSdrh case '-': { /* SQL-style comments from "--" to end of line */ 5607ad4334aSdrh if( zSql[1]!='-' ){ 5617ad4334aSdrh token = tkOTHER; 5627ad4334aSdrh break; 5637ad4334aSdrh } 5647ad4334aSdrh while( *zSql && *zSql!='\n' ){ zSql++; } 5657ad4334aSdrh if( *zSql==0 ) return state==0; 5667ad4334aSdrh token = tkWS; 5677ad4334aSdrh break; 5687ad4334aSdrh } 5697ad4334aSdrh case '[': { /* Microsoft-style identifiers in [...] */ 5707ad4334aSdrh zSql++; 5717ad4334aSdrh while( *zSql && *zSql!=']' ){ zSql++; } 5727ad4334aSdrh if( *zSql==0 ) return 0; 5737ad4334aSdrh token = tkOTHER; 5747ad4334aSdrh break; 5757ad4334aSdrh } 5767ad4334aSdrh case '"': /* single- and double-quoted strings */ 5777ad4334aSdrh case '\'': { 5787ad4334aSdrh int c = *zSql; 5797ad4334aSdrh zSql++; 5807ad4334aSdrh while( *zSql && *zSql!=c ){ zSql++; } 5817ad4334aSdrh if( *zSql==0 ) return 0; 5827ad4334aSdrh token = tkOTHER; 5837ad4334aSdrh break; 5847ad4334aSdrh } 5857ad4334aSdrh default: { 586aa756b09Sdrh int c; 587aa756b09Sdrh if( IdChar((u8)*zSql) ){ 5887ad4334aSdrh /* Keywords and unquoted identifiers */ 5897ad4334aSdrh int nId; 590aa756b09Sdrh for(nId=1; IdChar(zSql[nId]); nId++){} 591b7f9164eSdrh #ifdef SQLITE_OMIT_TRIGGER 592b7f9164eSdrh token = tkOTHER; 593b7f9164eSdrh #else 5947ad4334aSdrh switch( *zSql ){ 5957ad4334aSdrh case 'c': case 'C': { 5964adee20fSdanielk1977 if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){ 5977ad4334aSdrh token = tkCREATE; 5987ad4334aSdrh }else{ 5997ad4334aSdrh token = tkOTHER; 6007ad4334aSdrh } 6017ad4334aSdrh break; 6027ad4334aSdrh } 6037ad4334aSdrh case 't': case 'T': { 6044adee20fSdanielk1977 if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){ 6057ad4334aSdrh token = tkTRIGGER; 6064adee20fSdanielk1977 }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){ 6077ad4334aSdrh token = tkTEMP; 6084adee20fSdanielk1977 }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){ 6097ad4334aSdrh token = tkTEMP; 6107ad4334aSdrh }else{ 6117ad4334aSdrh token = tkOTHER; 6127ad4334aSdrh } 6137ad4334aSdrh break; 6147ad4334aSdrh } 6157ad4334aSdrh case 'e': case 'E': { 6164adee20fSdanielk1977 if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){ 6177ad4334aSdrh token = tkEND; 618b7f9164eSdrh }else 619b7f9164eSdrh #ifndef SQLITE_OMIT_EXPLAIN 620b7f9164eSdrh if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){ 6217ad4334aSdrh token = tkEXPLAIN; 622b7f9164eSdrh }else 623b7f9164eSdrh #endif 624b7f9164eSdrh { 6257ad4334aSdrh token = tkOTHER; 6267ad4334aSdrh } 6277ad4334aSdrh break; 6287ad4334aSdrh } 6297ad4334aSdrh default: { 6307ad4334aSdrh token = tkOTHER; 6317ad4334aSdrh break; 6327ad4334aSdrh } 6337ad4334aSdrh } 634b7f9164eSdrh #endif /* SQLITE_OMIT_TRIGGER */ 6357ad4334aSdrh zSql += nId-1; 6367ad4334aSdrh }else{ 6377ad4334aSdrh /* Operators and special symbols */ 6387ad4334aSdrh token = tkOTHER; 6397ad4334aSdrh } 6407ad4334aSdrh break; 6417ad4334aSdrh } 6427ad4334aSdrh } 6437ad4334aSdrh state = trans[state][token]; 6447ad4334aSdrh zSql++; 6457ad4334aSdrh } 6467ad4334aSdrh return state==0; 6477ad4334aSdrh } 64861de0d1bSdanielk1977 649b7f9164eSdrh #ifndef SQLITE_OMIT_UTF16 65061de0d1bSdanielk1977 /* 65161de0d1bSdanielk1977 ** This routine is the same as the sqlite3_complete() routine described 65261de0d1bSdanielk1977 ** above, except that the parameter is required to be UTF-16 encoded, not 65361de0d1bSdanielk1977 ** UTF-8. 65461de0d1bSdanielk1977 */ 65561de0d1bSdanielk1977 int sqlite3_complete16(const void *zSql){ 656bfd6cce5Sdanielk1977 sqlite3_value *pVal; 6575314c4d7Sdanielk1977 char const *zSql8; 658bfd6cce5Sdanielk1977 int rc = 0; 659bfd6cce5Sdanielk1977 660bfd6cce5Sdanielk1977 pVal = sqlite3ValueNew(); 661bfd6cce5Sdanielk1977 sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC); 662bfd6cce5Sdanielk1977 zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8); 663bfd6cce5Sdanielk1977 if( zSql8 ){ 66461de0d1bSdanielk1977 rc = sqlite3_complete(zSql8); 665bfd6cce5Sdanielk1977 } 666bfd6cce5Sdanielk1977 sqlite3ValueFree(pVal); 66761de0d1bSdanielk1977 return rc; 66861de0d1bSdanielk1977 } 669b7f9164eSdrh #endif /* SQLITE_OMIT_UTF16 */ 670*ccae6026Sdrh #endif /* SQLITE_OMIT_COMPLETE */ 671