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 */ 1875897234Sdrh #include "sqliteInt.h" 19dcc581ccSdrh #include <stdlib.h> 2075897234Sdrh 2134dcee65Sdrh /* Character classes for tokenizing 2234dcee65Sdrh ** 2334dcee65Sdrh ** In the sqlite3GetToken() function, a switch() on aiClass[c] is implemented 2434dcee65Sdrh ** using a lookup table, whereas a switch() directly on c uses a binary search. 2534dcee65Sdrh ** The lookup table is much faster. To maximize speed, and to ensure that 2634dcee65Sdrh ** a lookup table is used, all of the classes need to be small integers and 2734dcee65Sdrh ** all of them need to be used within the switch. 2834dcee65Sdrh */ 29e96f3619Sdrh #define CC_X 0 /* The letter 'x', or start of BLOB literal */ 308974331fSdrh #define CC_KYWD 1 /* Alphabetics or '_'. Usable in a keyword */ 318974331fSdrh #define CC_ID 2 /* unicode characters usable in IDs */ 328974331fSdrh #define CC_DIGIT 3 /* Digits */ 338974331fSdrh #define CC_DOLLAR 4 /* '$' */ 348974331fSdrh #define CC_VARALPHA 5 /* '@', '#', ':'. Alphabetic SQL variables */ 358974331fSdrh #define CC_VARNUM 6 /* '?'. Numeric SQL variables */ 368974331fSdrh #define CC_SPACE 7 /* Space characters */ 378974331fSdrh #define CC_QUOTE 8 /* '"', '\'', or '`'. String literals, quoted ids */ 388974331fSdrh #define CC_QUOTE2 9 /* '['. [...] style quoted ids */ 398974331fSdrh #define CC_PIPE 10 /* '|'. Bitwise OR or concatenate */ 408974331fSdrh #define CC_MINUS 11 /* '-'. Minus or SQL-style comment */ 418974331fSdrh #define CC_LT 12 /* '<'. Part of < or <= or <> */ 428974331fSdrh #define CC_GT 13 /* '>'. Part of > or >= */ 438974331fSdrh #define CC_EQ 14 /* '='. Part of = or == */ 448974331fSdrh #define CC_BANG 15 /* '!'. Part of != */ 458974331fSdrh #define CC_SLASH 16 /* '/'. / or c-style comment */ 468974331fSdrh #define CC_LP 17 /* '(' */ 478974331fSdrh #define CC_RP 18 /* ')' */ 488974331fSdrh #define CC_SEMI 19 /* ';' */ 498974331fSdrh #define CC_PLUS 20 /* '+' */ 508974331fSdrh #define CC_STAR 21 /* '*' */ 518974331fSdrh #define CC_PERCENT 22 /* '%' */ 528974331fSdrh #define CC_COMMA 23 /* ',' */ 538974331fSdrh #define CC_AND 24 /* '&' */ 548974331fSdrh #define CC_TILDA 25 /* '~' */ 558974331fSdrh #define CC_DOT 26 /* '.' */ 568974331fSdrh #define CC_ILLEGAL 27 /* Illegal character */ 578974331fSdrh 588974331fSdrh static const unsigned char aiClass[] = { 5934dcee65Sdrh #ifdef SQLITE_ASCII 608974331fSdrh /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */ 618974331fSdrh /* 0x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 7, 7, 27, 7, 7, 27, 27, 628974331fSdrh /* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 638974331fSdrh /* 2x */ 7, 15, 8, 5, 4, 22, 24, 8, 17, 18, 21, 20, 23, 11, 26, 16, 648974331fSdrh /* 3x */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 19, 12, 14, 13, 6, 658974331fSdrh /* 4x */ 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 668974331fSdrh /* 5x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 9, 27, 27, 27, 1, 678974331fSdrh /* 6x */ 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 688974331fSdrh /* 7x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 27, 10, 27, 25, 27, 698974331fSdrh /* 8x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 708974331fSdrh /* 9x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 718974331fSdrh /* Ax */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 728974331fSdrh /* Bx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 738974331fSdrh /* Cx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 748974331fSdrh /* Dx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 758974331fSdrh /* Ex */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 768974331fSdrh /* Fx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 7734dcee65Sdrh #endif 7834dcee65Sdrh #ifdef SQLITE_EBCDIC 7934dcee65Sdrh /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */ 8034dcee65Sdrh /* 0x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 7, 7, 27, 27, 8134dcee65Sdrh /* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 8234dcee65Sdrh /* 2x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 8334dcee65Sdrh /* 3x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 8434dcee65Sdrh /* 4x */ 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 12, 17, 20, 10, 8534dcee65Sdrh /* 5x */ 24, 27, 27, 27, 27, 27, 27, 27, 27, 27, 15, 4, 21, 18, 19, 27, 8634dcee65Sdrh /* 6x */ 11, 16, 27, 27, 27, 27, 27, 27, 27, 27, 27, 23, 22, 1, 13, 7, 8734dcee65Sdrh /* 7x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 5, 5, 5, 8, 14, 8, 8834dcee65Sdrh /* 8x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27, 8934dcee65Sdrh /* 9x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27, 9034dcee65Sdrh /* 9x */ 25, 1, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27, 9134dcee65Sdrh /* Bx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 9, 27, 27, 27, 27, 27, 9234dcee65Sdrh /* Cx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27, 9334dcee65Sdrh /* Dx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27, 9434dcee65Sdrh /* Ex */ 27, 27, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27, 9534dcee65Sdrh /* Fx */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 27, 27, 27, 27, 27, 27, 9634dcee65Sdrh #endif 978974331fSdrh }; 988974331fSdrh 9975897234Sdrh /* 10034dcee65Sdrh ** The charMap() macro maps alphabetic characters (only) into their 1019b8f447bSdrh ** lower-case ASCII equivalent. On ASCII machines, this is just 1029b8f447bSdrh ** an upper-to-lower case map. On EBCDIC machines we also need 10334dcee65Sdrh ** to adjust the encoding. The mapping is only valid for alphabetics 10434dcee65Sdrh ** which are the only characters for which this feature is used. 10534dcee65Sdrh ** 10634dcee65Sdrh ** Used by keywordhash.h 1079b8f447bSdrh */ 1089b8f447bSdrh #ifdef SQLITE_ASCII 1099b8f447bSdrh # define charMap(X) sqlite3UpperToLower[(unsigned char)X] 1109b8f447bSdrh #endif 1119b8f447bSdrh #ifdef SQLITE_EBCDIC 1129b8f447bSdrh # define charMap(X) ebcdicToAscii[(unsigned char)X] 1139b8f447bSdrh const unsigned char ebcdicToAscii[] = { 1149b8f447bSdrh /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ 1159b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */ 1169b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */ 1179b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */ 1189b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */ 1199b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */ 1209b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */ 1219b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */ 1229b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */ 1239b8f447bSdrh 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */ 1249b8f447bSdrh 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */ 1259b8f447bSdrh 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */ 1269b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */ 1279b8f447bSdrh 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */ 1289b8f447bSdrh 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */ 1299b8f447bSdrh 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */ 1309b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */ 1319b8f447bSdrh }; 1329b8f447bSdrh #endif 1339b8f447bSdrh 1349b8f447bSdrh /* 13552fb6d71Sdrh ** The sqlite3KeywordCode function looks up an identifier to determine if 13652fb6d71Sdrh ** it is a keyword. If it is a keyword, the token code of that keyword is 13775897234Sdrh ** returned. If the input is not a keyword, TK_ID is returned. 1382090a0edSdrh ** 1392090a0edSdrh ** The implementation of this routine was generated by a program, 140e96f3619Sdrh ** mkkeywordhash.c, located in the tool subdirectory of the distribution. 14152fb6d71Sdrh ** The output of the mkkeywordhash.c program is written into a file 14273b211abSdrh ** named keywordhash.h and then included into this source file by 14352fb6d71Sdrh ** the #include below. 14475897234Sdrh */ 14573b211abSdrh #include "keywordhash.h" 14675897234Sdrh 14740f20f7dSdrh 14898808babSdrh /* 1499b8f447bSdrh ** If X is a character that can be used in an identifier then 1509b8f447bSdrh ** IdChar(X) will be true. Otherwise it is false. 15198808babSdrh ** 1529b8f447bSdrh ** For ASCII, any character with the high-order bit set is 1539b8f447bSdrh ** allowed in an identifier. For 7-bit characters, 1549b8f447bSdrh ** sqlite3IsIdChar[X] must be 1. 1559b8f447bSdrh ** 1569b8f447bSdrh ** For EBCDIC, the rules are more complex but have the same 1579b8f447bSdrh ** end result. 158a0d1f663Sdrh ** 159a0d1f663Sdrh ** Ticket #1066. the SQL standard does not allow '$' in the 16060ec914cSpeter.d.reid ** middle of identifiers. But many SQL implementations do. 161a0d1f663Sdrh ** SQLite will allow '$' in identifiers for compatibility. 162a0d1f663Sdrh ** But the feature is undocumented. 16398808babSdrh */ 1649b8f447bSdrh #ifdef SQLITE_ASCII 165af2b5720Sdrh #define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0) 1669b8f447bSdrh #endif 1679b8f447bSdrh #ifdef SQLITE_EBCDIC 16846c99e0fSdrh const char sqlite3IsEbcdicIdChar[] = { 1699b8f447bSdrh /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ 1709b8f447bSdrh 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */ 1719b8f447bSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */ 1729b8f447bSdrh 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */ 1739b8f447bSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */ 1749b8f447bSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */ 1759b8f447bSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */ 1769b8f447bSdrh 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */ 1779b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */ 1789b8f447bSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */ 1799b8f447bSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */ 1809b8f447bSdrh 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */ 1819b8f447bSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */ 1829b8f447bSdrh }; 18346c99e0fSdrh #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40])) 1849b8f447bSdrh #endif 185f5ed7ad6Sdrh 186f5ed7ad6Sdrh /* Make the IdChar function accessible from ctime.c */ 187f5ed7ad6Sdrh #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS 18897348b37Sdrh int sqlite3IsIdChar(u8 c){ return IdChar(c); } 189f5ed7ad6Sdrh #endif 1909b8f447bSdrh 19198808babSdrh 19275897234Sdrh /* 193e96f3619Sdrh ** Return the length (in bytes) of the token that begins at z[0]. 19461b487d0Sdrh ** Store the token type in *tokenType before returning. 19575897234Sdrh */ 19635b5a33eSdrh int sqlite3GetToken(const unsigned char *z, int *tokenType){ 197aa756b09Sdrh int i, c; 198e96f3619Sdrh switch( aiClass[*z] ){ /* Switch on the character-class of the first byte 199e96f3619Sdrh ** of the token. See the comment on the CC_ defines 200e96f3619Sdrh ** above. */ 2018974331fSdrh case CC_SPACE: { 20219f81f6cSdrh testcase( z[0]==' ' ); 20319f81f6cSdrh testcase( z[0]=='\t' ); 20419f81f6cSdrh testcase( z[0]=='\n' ); 20519f81f6cSdrh testcase( z[0]=='\f' ); 20619f81f6cSdrh testcase( z[0]=='\r' ); 20778ca0e7eSdanielk1977 for(i=1; sqlite3Isspace(z[i]); i++){} 20875897234Sdrh *tokenType = TK_SPACE; 20975897234Sdrh return i; 21075897234Sdrh } 2118974331fSdrh case CC_MINUS: { 21275897234Sdrh if( z[1]=='-' ){ 213aa756b09Sdrh for(i=2; (c=z[i])!=0 && c!='\n'; i++){} 214d3479b94Sdrh *tokenType = TK_SPACE; /* IMP: R-22934-25134 */ 21575897234Sdrh return i; 21675897234Sdrh } 21775897234Sdrh *tokenType = TK_MINUS; 21875897234Sdrh return 1; 21975897234Sdrh } 2208974331fSdrh case CC_LP: { 22175897234Sdrh *tokenType = TK_LP; 22275897234Sdrh return 1; 22375897234Sdrh } 2248974331fSdrh case CC_RP: { 22575897234Sdrh *tokenType = TK_RP; 22675897234Sdrh return 1; 22775897234Sdrh } 2288974331fSdrh case CC_SEMI: { 22975897234Sdrh *tokenType = TK_SEMI; 23075897234Sdrh return 1; 23175897234Sdrh } 2328974331fSdrh case CC_PLUS: { 23375897234Sdrh *tokenType = TK_PLUS; 23475897234Sdrh return 1; 23575897234Sdrh } 2368974331fSdrh case CC_STAR: { 23775897234Sdrh *tokenType = TK_STAR; 23875897234Sdrh return 1; 23975897234Sdrh } 2408974331fSdrh case CC_SLASH: { 24166105a8eSdrh if( z[1]!='*' || z[2]==0 ){ 24275897234Sdrh *tokenType = TK_SLASH; 24375897234Sdrh return 1; 24475897234Sdrh } 245aa756b09Sdrh for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){} 246aa756b09Sdrh if( c ) i++; 247d3479b94Sdrh *tokenType = TK_SPACE; /* IMP: R-22934-25134 */ 24866105a8eSdrh return i; 24966105a8eSdrh } 2508974331fSdrh case CC_PERCENT: { 251bf4133cbSdrh *tokenType = TK_REM; 252bf4133cbSdrh return 1; 253bf4133cbSdrh } 2548974331fSdrh case CC_EQ: { 25575897234Sdrh *tokenType = TK_EQ; 25675897234Sdrh return 1 + (z[1]=='='); 25775897234Sdrh } 2588974331fSdrh case CC_LT: { 259aa756b09Sdrh if( (c=z[1])=='=' ){ 26075897234Sdrh *tokenType = TK_LE; 26175897234Sdrh return 2; 262aa756b09Sdrh }else if( c=='>' ){ 26375897234Sdrh *tokenType = TK_NE; 26475897234Sdrh return 2; 265aa756b09Sdrh }else if( c=='<' ){ 266bf4133cbSdrh *tokenType = TK_LSHIFT; 267bf4133cbSdrh return 2; 26875897234Sdrh }else{ 26975897234Sdrh *tokenType = TK_LT; 27075897234Sdrh return 1; 27175897234Sdrh } 27275897234Sdrh } 2738974331fSdrh case CC_GT: { 274aa756b09Sdrh if( (c=z[1])=='=' ){ 27575897234Sdrh *tokenType = TK_GE; 27675897234Sdrh return 2; 277aa756b09Sdrh }else if( c=='>' ){ 278bf4133cbSdrh *tokenType = TK_RSHIFT; 279bf4133cbSdrh return 2; 28075897234Sdrh }else{ 28175897234Sdrh *tokenType = TK_GT; 28275897234Sdrh return 1; 28375897234Sdrh } 28475897234Sdrh } 2858974331fSdrh case CC_BANG: { 28675897234Sdrh if( z[1]!='=' ){ 28775897234Sdrh *tokenType = TK_ILLEGAL; 288*b2bddbbcSdrh return 1; 28975897234Sdrh }else{ 29075897234Sdrh *tokenType = TK_NE; 29175897234Sdrh return 2; 29275897234Sdrh } 29375897234Sdrh } 2948974331fSdrh case CC_PIPE: { 2950040077dSdrh if( z[1]!='|' ){ 296bf4133cbSdrh *tokenType = TK_BITOR; 2970040077dSdrh return 1; 2980040077dSdrh }else{ 2990040077dSdrh *tokenType = TK_CONCAT; 3000040077dSdrh return 2; 3010040077dSdrh } 3020040077dSdrh } 3038974331fSdrh case CC_COMMA: { 30475897234Sdrh *tokenType = TK_COMMA; 30575897234Sdrh return 1; 30675897234Sdrh } 3078974331fSdrh case CC_AND: { 308bf4133cbSdrh *tokenType = TK_BITAND; 309bf4133cbSdrh return 1; 310bf4133cbSdrh } 3118974331fSdrh case CC_TILDA: { 312bf4133cbSdrh *tokenType = TK_BITNOT; 313bf4133cbSdrh return 1; 314bf4133cbSdrh } 3158974331fSdrh case CC_QUOTE: { 31675897234Sdrh int delim = z[0]; 31719f81f6cSdrh testcase( delim=='`' ); 31819f81f6cSdrh testcase( delim=='\'' ); 31919f81f6cSdrh testcase( delim=='"' ); 320aa756b09Sdrh for(i=1; (c=z[i])!=0; i++){ 321aa756b09Sdrh if( c==delim ){ 32275897234Sdrh if( z[i+1]==delim ){ 32375897234Sdrh i++; 32475897234Sdrh }else{ 32575897234Sdrh break; 32675897234Sdrh } 32775897234Sdrh } 32875897234Sdrh } 329a33cb5f7Sdrh if( c=='\'' ){ 33075897234Sdrh *tokenType = TK_STRING; 331eef8b558Sdrh return i+1; 332a33cb5f7Sdrh }else if( c!=0 ){ 333a33cb5f7Sdrh *tokenType = TK_ID; 334a33cb5f7Sdrh return i+1; 335eef8b558Sdrh }else{ 336eef8b558Sdrh *tokenType = TK_ILLEGAL; 33775897234Sdrh return i; 33875897234Sdrh } 339eef8b558Sdrh } 3408974331fSdrh case CC_DOT: { 3417681618cSdrh #ifndef SQLITE_OMIT_FLOATING_POINT 34278ca0e7eSdanielk1977 if( !sqlite3Isdigit(z[1]) ) 3437681618cSdrh #endif 3447681618cSdrh { 34575897234Sdrh *tokenType = TK_DOT; 34675897234Sdrh return 1; 34775897234Sdrh } 3487681618cSdrh /* If the next character is a digit, this is a floating point 3497681618cSdrh ** number that begins with ".". Fall thru into the next case */ 3507681618cSdrh } 3518974331fSdrh case CC_DIGIT: { 35219f81f6cSdrh testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' ); 35319f81f6cSdrh testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' ); 35419f81f6cSdrh testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' ); 35519f81f6cSdrh testcase( z[0]=='9' ); 356c837e709Sdrh *tokenType = TK_INTEGER; 35728e048c6Sdrh #ifndef SQLITE_OMIT_HEX_INTEGER 35828e048c6Sdrh if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){ 35928e048c6Sdrh for(i=3; sqlite3Isxdigit(z[i]); i++){} 36028e048c6Sdrh return i; 36128e048c6Sdrh } 36228e048c6Sdrh #endif 36378ca0e7eSdanielk1977 for(i=0; sqlite3Isdigit(z[i]); i++){} 364b7f9164eSdrh #ifndef SQLITE_OMIT_FLOATING_POINT 3657681618cSdrh if( z[i]=='.' ){ 3667681618cSdrh i++; 36778ca0e7eSdanielk1977 while( sqlite3Isdigit(z[i]) ){ i++; } 368c837e709Sdrh *tokenType = TK_FLOAT; 369c837e709Sdrh } 37075897234Sdrh if( (z[i]=='e' || z[i]=='E') && 37178ca0e7eSdanielk1977 ( sqlite3Isdigit(z[i+1]) 37278ca0e7eSdanielk1977 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2])) 37375897234Sdrh ) 37475897234Sdrh ){ 37575897234Sdrh i += 2; 37678ca0e7eSdanielk1977 while( sqlite3Isdigit(z[i]) ){ i++; } 37775897234Sdrh *tokenType = TK_FLOAT; 37875897234Sdrh } 379b7f9164eSdrh #endif 38067dd9011Sdrh while( IdChar(z[i]) ){ 38167dd9011Sdrh *tokenType = TK_ILLEGAL; 38267dd9011Sdrh i++; 38367dd9011Sdrh } 38475897234Sdrh return i; 38575897234Sdrh } 3868974331fSdrh case CC_QUOTE2: { 387aa756b09Sdrh for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){} 3884b2f9368Sdrh *tokenType = c==']' ? TK_ID : TK_ILLEGAL; 3892f4392ffSdrh return i; 3902f4392ffSdrh } 3918974331fSdrh case CC_VARNUM: { 39250457896Sdrh *tokenType = TK_VARIABLE; 39378ca0e7eSdanielk1977 for(i=1; sqlite3Isdigit(z[i]); i++){} 394fa6bc000Sdrh return i; 39550457896Sdrh } 3968974331fSdrh case CC_DOLLAR: 3978974331fSdrh case CC_VARALPHA: { 398895d7472Sdrh int n = 0; 399f59b12fbSdrh testcase( z[0]=='$' ); testcase( z[0]=='@' ); 400f59b12fbSdrh testcase( z[0]==':' ); testcase( z[0]=='#' ); 401288d37f1Sdrh *tokenType = TK_VARIABLE; 402895d7472Sdrh for(i=1; (c=z[i])!=0; i++){ 403288d37f1Sdrh if( IdChar(c) ){ 404895d7472Sdrh n++; 405288d37f1Sdrh #ifndef SQLITE_OMIT_TCL_VARIABLE 406895d7472Sdrh }else if( c=='(' && n>0 ){ 407895d7472Sdrh do{ 408895d7472Sdrh i++; 40978ca0e7eSdanielk1977 }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' ); 410895d7472Sdrh if( c==')' ){ 411895d7472Sdrh i++; 412895d7472Sdrh }else{ 413895d7472Sdrh *tokenType = TK_ILLEGAL; 414895d7472Sdrh } 415895d7472Sdrh break; 416895d7472Sdrh }else if( c==':' && z[i+1]==':' ){ 417895d7472Sdrh i++; 418288d37f1Sdrh #endif 419895d7472Sdrh }else{ 420895d7472Sdrh break; 421895d7472Sdrh } 422895d7472Sdrh } 4239d74b4c5Sdrh if( n==0 ) *tokenType = TK_ILLEGAL; 424895d7472Sdrh return i; 425895d7472Sdrh } 426e96f3619Sdrh case CC_KYWD: { 427e96f3619Sdrh for(i=1; aiClass[z[i]]<=CC_KYWD; i++){} 428e96f3619Sdrh if( IdChar(z[i]) ){ 429e96f3619Sdrh /* This token started out using characters that can appear in keywords, 430e96f3619Sdrh ** but z[i] is a character not allowed within keywords, so this must 431e96f3619Sdrh ** be an identifier instead */ 432e96f3619Sdrh i++; 433e96f3619Sdrh break; 434e96f3619Sdrh } 435e96f3619Sdrh *tokenType = TK_ID; 436e96f3619Sdrh return keywordCode((char*)z, i, tokenType); 437e96f3619Sdrh } 438b7f9164eSdrh #ifndef SQLITE_OMIT_BLOB_LITERAL 4398974331fSdrh case CC_X: { 44019f81f6cSdrh testcase( z[0]=='x' ); testcase( z[0]=='X' ); 4414b2f9368Sdrh if( z[1]=='\'' ){ 4423fd0a736Sdanielk1977 *tokenType = TK_BLOB; 4433a61a5a2Sdrh for(i=2; sqlite3Isxdigit(z[i]); i++){} 4443a61a5a2Sdrh if( z[i]!='\'' || i%2 ){ 4453fd0a736Sdanielk1977 *tokenType = TK_ILLEGAL; 4463a61a5a2Sdrh while( z[i] && z[i]!='\'' ){ i++; } 4473fd0a736Sdanielk1977 } 4483a61a5a2Sdrh if( z[i] ) i++; 449c572ef7fSdanielk1977 return i; 450c572ef7fSdanielk1977 } 451e96f3619Sdrh /* If it is not a BLOB literal, then it must be an ID, since no 452e96f3619Sdrh ** SQL keywords start with the letter 'x'. Fall through */ 453c572ef7fSdanielk1977 } 454b7f9164eSdrh #endif 4558974331fSdrh case CC_ID: { 4568974331fSdrh i = 1; 45798808babSdrh break; 45898808babSdrh } 4598974331fSdrh default: { 46075897234Sdrh *tokenType = TK_ILLEGAL; 46175897234Sdrh return 1; 46275897234Sdrh } 4638974331fSdrh } 46434dcee65Sdrh while( IdChar(z[i]) ){ i++; } 4658974331fSdrh *tokenType = TK_ID; 4668974331fSdrh return i; 4678974331fSdrh } 46875897234Sdrh 46975897234Sdrh /* 47075897234Sdrh ** Run the parser on the given SQL string. The parser structure is 471b19a2bc6Sdrh ** passed in. An SQLITE_ status code is returned. If an error occurs 472fb45d8c5Sdrh ** then an and attempt is made to write an error message into 473fb45d8c5Sdrh ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that 474fb45d8c5Sdrh ** error message. 47575897234Sdrh */ 4764adee20fSdanielk1977 int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){ 477d9da78a2Sdrh int nErr = 0; /* Number of errors encountered */ 478d9da78a2Sdrh int i; /* Loop counter */ 479d9da78a2Sdrh void *pEngine; /* The LEMON-generated LALR(1) parser */ 480d9da78a2Sdrh int tokenType; /* type of the next token */ 481d9da78a2Sdrh int lastTokenParsed = -1; /* type of the previous token */ 482d9da78a2Sdrh sqlite3 *db = pParse->db; /* The database connection */ 483d9da78a2Sdrh int mxSqlLen; /* Max length of an SQL string */ 48475897234Sdrh 48596c707a3Sdrh assert( zSql!=0 ); 486d9da78a2Sdrh mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; 4874f7d3a5fSdrh if( db->nVdbeActive==0 ){ 488881feaa0Sdrh db->u1.isInterrupted = 0; 48915ca1df1Sdrh } 4904c504391Sdrh pParse->rc = SQLITE_OK; 491e7f3f3eeSdrh pParse->zTail = zSql; 49275897234Sdrh i = 0; 493fb45d8c5Sdrh assert( pzErrMsg!=0 ); 4943bd48ab2Sdrh /* sqlite3ParserTrace(stdout, "parser: "); */ 495fb046e76Sdrh pEngine = sqlite3ParserAlloc(sqlite3Malloc); 49675897234Sdrh if( pEngine==0 ){ 4974a642b60Sdrh sqlite3OomFault(db); 498fad3039cSmistachkin return SQLITE_NOMEM_BKPT; 49975897234Sdrh } 500fa6bc000Sdrh assert( pParse->pNewTable==0 ); 501fa6bc000Sdrh assert( pParse->pNewTrigger==0 ); 502fa6bc000Sdrh assert( pParse->nVar==0 ); 503124c0b49Sdrh assert( pParse->nzVar==0 ); 504124c0b49Sdrh assert( pParse->azVar==0 ); 5050739e45bSdrh while( zSql[i]!=0 ){ 50632eb7b47Sdrh assert( i>=0 ); 507b7916a78Sdrh pParse->sLastToken.z = &zSql[i]; 50835b5a33eSdrh pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType); 50975897234Sdrh i += pParse->sLastToken.n; 510bb4957f8Sdrh if( i>mxSqlLen ){ 511e5c941b8Sdrh pParse->rc = SQLITE_TOOBIG; 512e5c941b8Sdrh break; 513e5c941b8Sdrh } 5140739e45bSdrh if( tokenType>=TK_SPACE ){ 5150739e45bSdrh assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL ); 516881feaa0Sdrh if( db->u1.isInterrupted ){ 51732eb7b47Sdrh pParse->rc = SQLITE_INTERRUPT; 51875897234Sdrh break; 51975897234Sdrh } 5200739e45bSdrh if( tokenType==TK_ILLEGAL ){ 521347bdc39Sdrh sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"", 522ae29ffbeSdrh &pParse->sLastToken); 52375897234Sdrh break; 52475897234Sdrh } 5250739e45bSdrh }else{ 5260739e45bSdrh sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse); 5270739e45bSdrh lastTokenParsed = tokenType; 5280739e45bSdrh if( pParse->rc!=SQLITE_OK || db->mallocFailed ) break; 52975897234Sdrh } 53032eb7b47Sdrh } 5310be0cf67Sdrh assert( nErr==0 ); 53230c14728Sdrh pParse->zTail = &zSql[i]; 533e7266225Sdrh if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){ 534e7266225Sdrh assert( zSql[i]==0 ); 535b86ccfb2Sdrh if( lastTokenParsed!=TK_SEMI ){ 5364adee20fSdanielk1977 sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse); 53775897234Sdrh } 5389a9705deSdrh if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){ 5394adee20fSdanielk1977 sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse); 54075897234Sdrh } 5419a9705deSdrh } 542ec424a5bSdrh #ifdef YYTRACKMAXSTACKDEPTH 543af89fe66Sdrh sqlite3_mutex_enter(sqlite3MallocMutex()); 544b02392e6Sdrh sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK, 545ec424a5bSdrh sqlite3ParserStackPeak(pEngine) 546ec424a5bSdrh ); 547af89fe66Sdrh sqlite3_mutex_leave(sqlite3MallocMutex()); 548ec424a5bSdrh #endif /* YYDEBUG */ 54917435752Sdrh sqlite3ParserFree(pEngine, sqlite3_free); 55017435752Sdrh if( db->mallocFailed ){ 551fad3039cSmistachkin pParse->rc = SQLITE_NOMEM_BKPT; 55271c697efSdrh } 553b86ccfb2Sdrh if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){ 55422c17b8bSdrh pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc)); 555b86ccfb2Sdrh } 55619f81f6cSdrh assert( pzErrMsg!=0 ); 55775897234Sdrh if( pParse->zErrMsg ){ 55875897234Sdrh *pzErrMsg = pParse->zErrMsg; 559413c3d36Sdrh sqlite3_log(pParse->rc, "%s", *pzErrMsg); 560b86ccfb2Sdrh pParse->zErrMsg = 0; 5614b2f9368Sdrh nErr++; 56275897234Sdrh } 5632958a4e6Sdrh if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){ 5644adee20fSdanielk1977 sqlite3VdbeDelete(pParse->pVdbe); 56575897234Sdrh pParse->pVdbe = 0; 56675897234Sdrh } 567c00da105Sdanielk1977 #ifndef SQLITE_OMIT_SHARED_CACHE 568c00da105Sdanielk1977 if( pParse->nested==0 ){ 569633e6d57Sdrh sqlite3DbFree(db, pParse->aTableLock); 570c00da105Sdanielk1977 pParse->aTableLock = 0; 571c00da105Sdanielk1977 pParse->nTableLock = 0; 572c00da105Sdanielk1977 } 573c00da105Sdanielk1977 #endif 5744f3dd150Sdrh #ifndef SQLITE_OMIT_VIRTUALTABLE 575b975598eSdrh sqlite3_free(pParse->apVtabLock); 5764f3dd150Sdrh #endif 5777e6ebfb2Sdanielk1977 5787e6ebfb2Sdanielk1977 if( !IN_DECLARE_VTAB ){ 5797e6ebfb2Sdanielk1977 /* If the pParse->declareVtab flag is set, do not delete any table 5807e6ebfb2Sdanielk1977 ** structure built up in pParse->pNewTable. The calling code (see vtab.c) 5817e6ebfb2Sdanielk1977 ** will take responsibility for freeing the Table structure. 5827e6ebfb2Sdanielk1977 */ 5831feeaed2Sdan sqlite3DeleteTable(db, pParse->pNewTable); 5847e6ebfb2Sdanielk1977 } 5857e6ebfb2Sdanielk1977 5866e772266Sdrh sqlite3WithDelete(db, pParse->pWithToFree); 587633e6d57Sdrh sqlite3DeleteTrigger(db, pParse->pNewTrigger); 588124c0b49Sdrh for(i=pParse->nzVar-1; i>=0; i--) sqlite3DbFree(db, pParse->azVar[i]); 589124c0b49Sdrh sqlite3DbFree(db, pParse->azVar); 5900b9f50d8Sdrh while( pParse->pAinc ){ 5910b9f50d8Sdrh AutoincInfo *p = pParse->pAinc; 5920b9f50d8Sdrh pParse->pAinc = p->pNext; 5930b9f50d8Sdrh sqlite3DbFree(db, p); 5940b9f50d8Sdrh } 595588a9a1aSdrh while( pParse->pZombieTab ){ 596588a9a1aSdrh Table *p = pParse->pZombieTab; 597588a9a1aSdrh pParse->pZombieTab = p->pNextZombie; 5981feeaed2Sdan sqlite3DeleteTable(db, p); 599588a9a1aSdrh } 600f3151f0aSdrh assert( nErr==0 || pParse->rc!=SQLITE_OK ); 60175897234Sdrh return nErr; 60275897234Sdrh } 603