drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** An tokenizer for SQL |
| 13 | ** |
| 14 | ** This file contains C code that splits an SQL input string up into |
| 15 | ** individual tokens and sends those tokens one-by-one over to the |
| 16 | ** parser for analysis. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 17 | */ |
| 18 | #include "sqliteInt.h" |
drh | dcc581c | 2000-05-30 13:44:19 +0000 | [diff] [blame] | 19 | #include <stdlib.h> |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 20 | |
drh | 34dcee6 | 2016-02-08 19:15:48 +0000 | [diff] [blame] | 21 | /* Character classes for tokenizing |
| 22 | ** |
| 23 | ** In the sqlite3GetToken() function, a switch() on aiClass[c] is implemented |
| 24 | ** using a lookup table, whereas a switch() directly on c uses a binary search. |
| 25 | ** The lookup table is much faster. To maximize speed, and to ensure that |
| 26 | ** a lookup table is used, all of the classes need to be small integers and |
| 27 | ** all of them need to be used within the switch. |
| 28 | */ |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 29 | #define CC_X 0 /* The letter 'x' or 'X'. Start of x'01234fed' */ |
| 30 | #define CC_KYWD 1 /* Alphabetics or '_'. Usable in a keyword */ |
| 31 | #define CC_ID 2 /* unicode characters usable in IDs */ |
| 32 | #define CC_DIGIT 3 /* Digits */ |
| 33 | #define CC_DOLLAR 4 /* '$' */ |
| 34 | #define CC_VARALPHA 5 /* '@', '#', ':'. Alphabetic SQL variables */ |
| 35 | #define CC_VARNUM 6 /* '?'. Numeric SQL variables */ |
| 36 | #define CC_SPACE 7 /* Space characters */ |
| 37 | #define CC_QUOTE 8 /* '"', '\'', or '`'. String literals, quoted ids */ |
| 38 | #define CC_QUOTE2 9 /* '['. [...] style quoted ids */ |
| 39 | #define CC_PIPE 10 /* '|'. Bitwise OR or concatenate */ |
| 40 | #define CC_MINUS 11 /* '-'. Minus or SQL-style comment */ |
| 41 | #define CC_LT 12 /* '<'. Part of < or <= or <> */ |
| 42 | #define CC_GT 13 /* '>'. Part of > or >= */ |
| 43 | #define CC_EQ 14 /* '='. Part of = or == */ |
| 44 | #define CC_BANG 15 /* '!'. Part of != */ |
| 45 | #define CC_SLASH 16 /* '/'. / or c-style comment */ |
| 46 | #define CC_LP 17 /* '(' */ |
| 47 | #define CC_RP 18 /* ')' */ |
| 48 | #define CC_SEMI 19 /* ';' */ |
| 49 | #define CC_PLUS 20 /* '+' */ |
| 50 | #define CC_STAR 21 /* '*' */ |
| 51 | #define CC_PERCENT 22 /* '%' */ |
| 52 | #define CC_COMMA 23 /* ',' */ |
| 53 | #define CC_AND 24 /* '&' */ |
| 54 | #define CC_TILDA 25 /* '~' */ |
| 55 | #define CC_DOT 26 /* '.' */ |
| 56 | #define CC_ILLEGAL 27 /* Illegal character */ |
| 57 | |
| 58 | static const unsigned char aiClass[] = { |
drh | 34dcee6 | 2016-02-08 19:15:48 +0000 | [diff] [blame] | 59 | #ifdef SQLITE_ASCII |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 60 | /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */ |
| 61 | /* 0x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 7, 7, 27, 7, 7, 27, 27, |
| 62 | /* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, |
| 63 | /* 2x */ 7, 15, 8, 5, 4, 22, 24, 8, 17, 18, 21, 20, 23, 11, 26, 16, |
| 64 | /* 3x */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 19, 12, 14, 13, 6, |
| 65 | /* 4x */ 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 66 | /* 5x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 9, 27, 27, 27, 1, |
| 67 | /* 6x */ 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 68 | /* 7x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 27, 10, 27, 25, 27, |
| 69 | /* 8x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 70 | /* 9x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 71 | /* Ax */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 72 | /* Bx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 73 | /* Cx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 74 | /* Dx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 75 | /* Ex */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 76 | /* Fx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 |
drh | 34dcee6 | 2016-02-08 19:15:48 +0000 | [diff] [blame] | 77 | #endif |
| 78 | #ifdef SQLITE_EBCDIC |
| 79 | /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */ |
| 80 | /* 0x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 7, 7, 27, 27, |
| 81 | /* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, |
| 82 | /* 2x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, |
| 83 | /* 3x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, |
| 84 | /* 4x */ 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 12, 17, 20, 10, |
| 85 | /* 5x */ 24, 27, 27, 27, 27, 27, 27, 27, 27, 27, 15, 4, 21, 18, 19, 27, |
| 86 | /* 6x */ 11, 16, 27, 27, 27, 27, 27, 27, 27, 27, 27, 23, 22, 1, 13, 7, |
| 87 | /* 7x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 5, 5, 5, 8, 14, 8, |
| 88 | /* 8x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27, |
| 89 | /* 9x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27, |
| 90 | /* 9x */ 25, 1, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27, |
| 91 | /* Bx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 9, 27, 27, 27, 27, 27, |
| 92 | /* Cx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27, |
| 93 | /* Dx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27, |
| 94 | /* Ex */ 27, 27, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27, |
| 95 | /* Fx */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 27, 27, 27, 27, 27, 27, |
| 96 | #endif |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 99 | /* |
drh | 34dcee6 | 2016-02-08 19:15:48 +0000 | [diff] [blame] | 100 | ** The charMap() macro maps alphabetic characters (only) into their |
drh | 9b8f447 | 2006-04-04 01:54:55 +0000 | [diff] [blame] | 101 | ** lower-case ASCII equivalent. On ASCII machines, this is just |
| 102 | ** an upper-to-lower case map. On EBCDIC machines we also need |
drh | 34dcee6 | 2016-02-08 19:15:48 +0000 | [diff] [blame] | 103 | ** to adjust the encoding. The mapping is only valid for alphabetics |
| 104 | ** which are the only characters for which this feature is used. |
| 105 | ** |
| 106 | ** Used by keywordhash.h |
drh | 9b8f447 | 2006-04-04 01:54:55 +0000 | [diff] [blame] | 107 | */ |
| 108 | #ifdef SQLITE_ASCII |
| 109 | # define charMap(X) sqlite3UpperToLower[(unsigned char)X] |
| 110 | #endif |
| 111 | #ifdef SQLITE_EBCDIC |
| 112 | # define charMap(X) ebcdicToAscii[(unsigned char)X] |
| 113 | const unsigned char ebcdicToAscii[] = { |
| 114 | /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ |
| 115 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */ |
| 116 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */ |
| 117 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */ |
| 118 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */ |
| 119 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */ |
| 120 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */ |
| 121 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */ |
| 122 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */ |
| 123 | 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */ |
| 124 | 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */ |
| 125 | 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */ |
| 126 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */ |
| 127 | 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */ |
| 128 | 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */ |
| 129 | 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */ |
| 130 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */ |
| 131 | }; |
| 132 | #endif |
| 133 | |
| 134 | /* |
drh | 52fb6d7 | 2004-11-03 03:59:57 +0000 | [diff] [blame] | 135 | ** The sqlite3KeywordCode function looks up an identifier to determine if |
| 136 | ** it is a keyword. If it is a keyword, the token code of that keyword is |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 137 | ** returned. If the input is not a keyword, TK_ID is returned. |
drh | 2090a0e | 2004-10-07 19:03:01 +0000 | [diff] [blame] | 138 | ** |
| 139 | ** The implementation of this routine was generated by a program, |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 140 | ** mkkeywordhash.h, located in the tool subdirectory of the distribution. |
drh | 52fb6d7 | 2004-11-03 03:59:57 +0000 | [diff] [blame] | 141 | ** The output of the mkkeywordhash.c program is written into a file |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 142 | ** named keywordhash.h and then included into this source file by |
drh | 52fb6d7 | 2004-11-03 03:59:57 +0000 | [diff] [blame] | 143 | ** the #include below. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 144 | */ |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 145 | #include "keywordhash.h" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 146 | |
drh | 40f20f7 | 2004-10-23 05:10:18 +0000 | [diff] [blame] | 147 | |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 148 | /* |
drh | 9b8f447 | 2006-04-04 01:54:55 +0000 | [diff] [blame] | 149 | ** If X is a character that can be used in an identifier then |
| 150 | ** IdChar(X) will be true. Otherwise it is false. |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 151 | ** |
drh | 9b8f447 | 2006-04-04 01:54:55 +0000 | [diff] [blame] | 152 | ** For ASCII, any character with the high-order bit set is |
| 153 | ** allowed in an identifier. For 7-bit characters, |
| 154 | ** sqlite3IsIdChar[X] must be 1. |
| 155 | ** |
| 156 | ** For EBCDIC, the rules are more complex but have the same |
| 157 | ** end result. |
drh | a0d1f66 | 2005-01-11 17:59:47 +0000 | [diff] [blame] | 158 | ** |
| 159 | ** Ticket #1066. the SQL standard does not allow '$' in the |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 160 | ** middle of identifiers. But many SQL implementations do. |
drh | a0d1f66 | 2005-01-11 17:59:47 +0000 | [diff] [blame] | 161 | ** SQLite will allow '$' in identifiers for compatibility. |
| 162 | ** But the feature is undocumented. |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 163 | */ |
drh | 9b8f447 | 2006-04-04 01:54:55 +0000 | [diff] [blame] | 164 | #ifdef SQLITE_ASCII |
drh | af2b572 | 2009-11-16 15:11:51 +0000 | [diff] [blame] | 165 | #define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0) |
drh | 9b8f447 | 2006-04-04 01:54:55 +0000 | [diff] [blame] | 166 | #endif |
| 167 | #ifdef SQLITE_EBCDIC |
drh | 46c99e0 | 2007-08-27 23:26:59 +0000 | [diff] [blame] | 168 | const char sqlite3IsEbcdicIdChar[] = { |
drh | 9b8f447 | 2006-04-04 01:54:55 +0000 | [diff] [blame] | 169 | /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ |
| 170 | 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */ |
| 171 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */ |
| 172 | 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */ |
| 173 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */ |
| 174 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */ |
| 175 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */ |
| 176 | 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */ |
| 177 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */ |
| 178 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */ |
| 179 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */ |
| 180 | 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */ |
| 181 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */ |
| 182 | }; |
drh | 46c99e0 | 2007-08-27 23:26:59 +0000 | [diff] [blame] | 183 | #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40])) |
drh | 9b8f447 | 2006-04-04 01:54:55 +0000 | [diff] [blame] | 184 | #endif |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 185 | |
| 186 | /* Make the IdChar function accessible from ctime.c */ |
| 187 | #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS |
drh | 97348b3 | 2014-09-25 02:44:29 +0000 | [diff] [blame] | 188 | int sqlite3IsIdChar(u8 c){ return IdChar(c); } |
drh | f5ed7ad | 2015-06-15 14:43:25 +0000 | [diff] [blame] | 189 | #endif |
drh | 9b8f447 | 2006-04-04 01:54:55 +0000 | [diff] [blame] | 190 | |
drh | 98808ba | 2001-10-18 12:34:46 +0000 | [diff] [blame] | 191 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 192 | /* |
drh | 61b487d | 2003-09-12 02:08:14 +0000 | [diff] [blame] | 193 | ** Return the length of the token that begins at z[0]. |
| 194 | ** Store the token type in *tokenType before returning. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 195 | */ |
drh | 35b5a33 | 2008-04-05 18:41:42 +0000 | [diff] [blame] | 196 | int sqlite3GetToken(const unsigned char *z, int *tokenType){ |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 197 | int i, c; |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 198 | switch( aiClass[*z] ){ |
| 199 | case CC_SPACE: { |
drh | 19f81f6 | 2009-06-09 18:01:37 +0000 | [diff] [blame] | 200 | testcase( z[0]==' ' ); |
| 201 | testcase( z[0]=='\t' ); |
| 202 | testcase( z[0]=='\n' ); |
| 203 | testcase( z[0]=='\f' ); |
| 204 | testcase( z[0]=='\r' ); |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 205 | for(i=1; sqlite3Isspace(z[i]); i++){} |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 206 | *tokenType = TK_SPACE; |
| 207 | return i; |
| 208 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 209 | case CC_MINUS: { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 210 | if( z[1]=='-' ){ |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 211 | for(i=2; (c=z[i])!=0 && c!='\n'; i++){} |
drh | d3479b9 | 2009-12-14 17:42:12 +0000 | [diff] [blame] | 212 | *tokenType = TK_SPACE; /* IMP: R-22934-25134 */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 213 | return i; |
| 214 | } |
| 215 | *tokenType = TK_MINUS; |
| 216 | return 1; |
| 217 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 218 | case CC_LP: { |
drh | dab3518 | 2003-09-27 13:39:38 +0000 | [diff] [blame] | 219 | *tokenType = TK_LP; |
| 220 | return 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 221 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 222 | case CC_RP: { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 223 | *tokenType = TK_RP; |
| 224 | return 1; |
| 225 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 226 | case CC_SEMI: { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 227 | *tokenType = TK_SEMI; |
| 228 | return 1; |
| 229 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 230 | case CC_PLUS: { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 231 | *tokenType = TK_PLUS; |
| 232 | return 1; |
| 233 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 234 | case CC_STAR: { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 235 | *tokenType = TK_STAR; |
| 236 | return 1; |
| 237 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 238 | case CC_SLASH: { |
drh | 66105a8 | 2002-08-27 14:28:29 +0000 | [diff] [blame] | 239 | if( z[1]!='*' || z[2]==0 ){ |
| 240 | *tokenType = TK_SLASH; |
| 241 | return 1; |
| 242 | } |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 243 | for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){} |
| 244 | if( c ) i++; |
drh | d3479b9 | 2009-12-14 17:42:12 +0000 | [diff] [blame] | 245 | *tokenType = TK_SPACE; /* IMP: R-22934-25134 */ |
drh | 66105a8 | 2002-08-27 14:28:29 +0000 | [diff] [blame] | 246 | return i; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 247 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 248 | case CC_PERCENT: { |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 249 | *tokenType = TK_REM; |
| 250 | return 1; |
| 251 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 252 | case CC_EQ: { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 253 | *tokenType = TK_EQ; |
| 254 | return 1 + (z[1]=='='); |
| 255 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 256 | case CC_LT: { |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 257 | if( (c=z[1])=='=' ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 258 | *tokenType = TK_LE; |
| 259 | return 2; |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 260 | }else if( c=='>' ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 261 | *tokenType = TK_NE; |
| 262 | return 2; |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 263 | }else if( c=='<' ){ |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 264 | *tokenType = TK_LSHIFT; |
| 265 | return 2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 266 | }else{ |
| 267 | *tokenType = TK_LT; |
| 268 | return 1; |
| 269 | } |
| 270 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 271 | case CC_GT: { |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 272 | if( (c=z[1])=='=' ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 273 | *tokenType = TK_GE; |
| 274 | return 2; |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 275 | }else if( c=='>' ){ |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 276 | *tokenType = TK_RSHIFT; |
| 277 | return 2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 278 | }else{ |
| 279 | *tokenType = TK_GT; |
| 280 | return 1; |
| 281 | } |
| 282 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 283 | case CC_BANG: { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 284 | if( z[1]!='=' ){ |
| 285 | *tokenType = TK_ILLEGAL; |
drh | c837e70 | 2000-06-08 16:26:24 +0000 | [diff] [blame] | 286 | return 2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 287 | }else{ |
| 288 | *tokenType = TK_NE; |
| 289 | return 2; |
| 290 | } |
| 291 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 292 | case CC_PIPE: { |
drh | 0040077 | 2000-06-16 20:51:26 +0000 | [diff] [blame] | 293 | if( z[1]!='|' ){ |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 294 | *tokenType = TK_BITOR; |
drh | 0040077 | 2000-06-16 20:51:26 +0000 | [diff] [blame] | 295 | return 1; |
| 296 | }else{ |
| 297 | *tokenType = TK_CONCAT; |
| 298 | return 2; |
| 299 | } |
| 300 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 301 | case CC_COMMA: { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 302 | *tokenType = TK_COMMA; |
| 303 | return 1; |
| 304 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 305 | case CC_AND: { |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 306 | *tokenType = TK_BITAND; |
| 307 | return 1; |
| 308 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 309 | case CC_TILDA: { |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 310 | *tokenType = TK_BITNOT; |
| 311 | return 1; |
| 312 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 313 | case CC_QUOTE: { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 314 | int delim = z[0]; |
drh | 19f81f6 | 2009-06-09 18:01:37 +0000 | [diff] [blame] | 315 | testcase( delim=='`' ); |
| 316 | testcase( delim=='\'' ); |
| 317 | testcase( delim=='"' ); |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 318 | for(i=1; (c=z[i])!=0; i++){ |
| 319 | if( c==delim ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 320 | if( z[i+1]==delim ){ |
| 321 | i++; |
| 322 | }else{ |
| 323 | break; |
| 324 | } |
| 325 | } |
| 326 | } |
drh | a33cb5f | 2008-08-07 13:05:34 +0000 | [diff] [blame] | 327 | if( c=='\'' ){ |
drh | eef8b55 | 2005-10-23 11:29:40 +0000 | [diff] [blame] | 328 | *tokenType = TK_STRING; |
| 329 | return i+1; |
drh | a33cb5f | 2008-08-07 13:05:34 +0000 | [diff] [blame] | 330 | }else if( c!=0 ){ |
| 331 | *tokenType = TK_ID; |
| 332 | return i+1; |
drh | eef8b55 | 2005-10-23 11:29:40 +0000 | [diff] [blame] | 333 | }else{ |
| 334 | *tokenType = TK_ILLEGAL; |
| 335 | return i; |
| 336 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 337 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 338 | case CC_DOT: { |
drh | 7681618 | 2005-08-23 11:31:26 +0000 | [diff] [blame] | 339 | #ifndef SQLITE_OMIT_FLOATING_POINT |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 340 | if( !sqlite3Isdigit(z[1]) ) |
drh | 7681618 | 2005-08-23 11:31:26 +0000 | [diff] [blame] | 341 | #endif |
| 342 | { |
| 343 | *tokenType = TK_DOT; |
| 344 | return 1; |
| 345 | } |
| 346 | /* If the next character is a digit, this is a floating point |
| 347 | ** number that begins with ".". Fall thru into the next case */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 348 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 349 | case CC_DIGIT: { |
drh | 19f81f6 | 2009-06-09 18:01:37 +0000 | [diff] [blame] | 350 | testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' ); |
| 351 | testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' ); |
| 352 | testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' ); |
| 353 | testcase( z[0]=='9' ); |
drh | c837e70 | 2000-06-08 16:26:24 +0000 | [diff] [blame] | 354 | *tokenType = TK_INTEGER; |
drh | 28e048c | 2014-07-23 01:26:51 +0000 | [diff] [blame] | 355 | #ifndef SQLITE_OMIT_HEX_INTEGER |
| 356 | if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){ |
| 357 | for(i=3; sqlite3Isxdigit(z[i]); i++){} |
| 358 | return i; |
| 359 | } |
| 360 | #endif |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 361 | for(i=0; sqlite3Isdigit(z[i]); i++){} |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 362 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 7681618 | 2005-08-23 11:31:26 +0000 | [diff] [blame] | 363 | if( z[i]=='.' ){ |
| 364 | i++; |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 365 | while( sqlite3Isdigit(z[i]) ){ i++; } |
drh | c837e70 | 2000-06-08 16:26:24 +0000 | [diff] [blame] | 366 | *tokenType = TK_FLOAT; |
| 367 | } |
| 368 | if( (z[i]=='e' || z[i]=='E') && |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 369 | ( sqlite3Isdigit(z[i+1]) |
| 370 | || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2])) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 371 | ) |
drh | c837e70 | 2000-06-08 16:26:24 +0000 | [diff] [blame] | 372 | ){ |
| 373 | i += 2; |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 374 | while( sqlite3Isdigit(z[i]) ){ i++; } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 375 | *tokenType = TK_FLOAT; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 376 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 377 | #endif |
drh | 67dd901 | 2006-08-12 12:33:14 +0000 | [diff] [blame] | 378 | while( IdChar(z[i]) ){ |
| 379 | *tokenType = TK_ILLEGAL; |
| 380 | i++; |
| 381 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 382 | return i; |
| 383 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 384 | case CC_QUOTE2: { |
drh | aa756b0 | 2004-09-25 15:25:26 +0000 | [diff] [blame] | 385 | for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){} |
drh | 4b2f936 | 2008-01-22 23:37:09 +0000 | [diff] [blame] | 386 | *tokenType = c==']' ? TK_ID : TK_ILLEGAL; |
drh | 2f4392f | 2002-02-14 21:42:51 +0000 | [diff] [blame] | 387 | return i; |
| 388 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 389 | case CC_VARNUM: { |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 390 | *tokenType = TK_VARIABLE; |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 391 | for(i=1; sqlite3Isdigit(z[i]); i++){} |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 392 | return i; |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 393 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 394 | case CC_DOLLAR: |
| 395 | case CC_VARALPHA: { |
drh | 288d37f | 2005-06-22 08:48:06 +0000 | [diff] [blame] | 396 | int n = 0; |
drh | f59b12f | 2014-01-11 03:54:05 +0000 | [diff] [blame] | 397 | testcase( z[0]=='$' ); testcase( z[0]=='@' ); |
| 398 | testcase( z[0]==':' ); testcase( z[0]=='#' ); |
drh | 9d74b4c | 2004-08-24 15:23:34 +0000 | [diff] [blame] | 399 | *tokenType = TK_VARIABLE; |
drh | 288d37f | 2005-06-22 08:48:06 +0000 | [diff] [blame] | 400 | for(i=1; (c=z[i])!=0; i++){ |
| 401 | if( IdChar(c) ){ |
| 402 | n++; |
| 403 | #ifndef SQLITE_OMIT_TCL_VARIABLE |
| 404 | }else if( c=='(' && n>0 ){ |
| 405 | do{ |
| 406 | i++; |
danielk1977 | 78ca0e7 | 2009-01-20 16:53:39 +0000 | [diff] [blame] | 407 | }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' ); |
drh | 288d37f | 2005-06-22 08:48:06 +0000 | [diff] [blame] | 408 | if( c==')' ){ |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 409 | i++; |
| 410 | }else{ |
drh | 288d37f | 2005-06-22 08:48:06 +0000 | [diff] [blame] | 411 | *tokenType = TK_ILLEGAL; |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 412 | } |
drh | 288d37f | 2005-06-22 08:48:06 +0000 | [diff] [blame] | 413 | break; |
| 414 | }else if( c==':' && z[i+1]==':' ){ |
| 415 | i++; |
| 416 | #endif |
| 417 | }else{ |
| 418 | break; |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 419 | } |
| 420 | } |
drh | 288d37f | 2005-06-22 08:48:06 +0000 | [diff] [blame] | 421 | if( n==0 ) *tokenType = TK_ILLEGAL; |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 422 | return i; |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 423 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 424 | #ifndef SQLITE_OMIT_BLOB_LITERAL |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 425 | case CC_X: { |
drh | 19f81f6 | 2009-06-09 18:01:37 +0000 | [diff] [blame] | 426 | testcase( z[0]=='x' ); testcase( z[0]=='X' ); |
drh | 4b2f936 | 2008-01-22 23:37:09 +0000 | [diff] [blame] | 427 | if( z[1]=='\'' ){ |
danielk1977 | 3fd0a73 | 2004-05-27 13:35:19 +0000 | [diff] [blame] | 428 | *tokenType = TK_BLOB; |
drh | 3a61a5a | 2011-06-03 21:34:45 +0000 | [diff] [blame] | 429 | for(i=2; sqlite3Isxdigit(z[i]); i++){} |
| 430 | if( z[i]!='\'' || i%2 ){ |
| 431 | *tokenType = TK_ILLEGAL; |
| 432 | while( z[i] && z[i]!='\'' ){ i++; } |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 433 | } |
drh | 3a61a5a | 2011-06-03 21:34:45 +0000 | [diff] [blame] | 434 | if( z[i] ) i++; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 435 | return i; |
| 436 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 437 | i = 1; |
| 438 | break; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 439 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 440 | #endif |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 441 | case CC_KYWD: { |
| 442 | for(i=1; aiClass[z[i]]<=CC_KYWD; i++){} |
drh | 34dcee6 | 2016-02-08 19:15:48 +0000 | [diff] [blame] | 443 | if( IdChar(z[i]) ){ i++; break; } |
drh | 54bb56d | 2015-11-10 03:30:51 +0000 | [diff] [blame] | 444 | *tokenType = TK_ID; |
| 445 | return keywordCode((char*)z, i, tokenType); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 446 | } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 447 | case CC_ID: { |
| 448 | i = 1; |
| 449 | break; |
| 450 | } |
| 451 | default: { |
| 452 | *tokenType = TK_ILLEGAL; |
| 453 | return 1; |
| 454 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 455 | } |
drh | 34dcee6 | 2016-02-08 19:15:48 +0000 | [diff] [blame] | 456 | while( IdChar(z[i]) ){ i++; } |
drh | 8974331 | 2016-02-08 02:30:50 +0000 | [diff] [blame] | 457 | *tokenType = TK_ID; |
| 458 | return i; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /* |
| 462 | ** Run the parser on the given SQL string. The parser structure is |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 463 | ** passed in. An SQLITE_ status code is returned. If an error occurs |
drh | fb45d8c | 2008-07-08 00:06:49 +0000 | [diff] [blame] | 464 | ** then an and attempt is made to write an error message into |
| 465 | ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that |
| 466 | ** error message. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 467 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 468 | int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){ |
drh | d9da78a | 2009-03-24 15:08:09 +0000 | [diff] [blame] | 469 | int nErr = 0; /* Number of errors encountered */ |
| 470 | int i; /* Loop counter */ |
| 471 | void *pEngine; /* The LEMON-generated LALR(1) parser */ |
| 472 | int tokenType; /* type of the next token */ |
| 473 | int lastTokenParsed = -1; /* type of the previous token */ |
drh | d9da78a | 2009-03-24 15:08:09 +0000 | [diff] [blame] | 474 | sqlite3 *db = pParse->db; /* The database connection */ |
| 475 | int mxSqlLen; /* Max length of an SQL string */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 476 | |
drh | 96c707a | 2015-02-13 16:36:14 +0000 | [diff] [blame] | 477 | assert( zSql!=0 ); |
drh | d9da78a | 2009-03-24 15:08:09 +0000 | [diff] [blame] | 478 | mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; |
drh | 4f7d3a5 | 2013-06-27 23:54:02 +0000 | [diff] [blame] | 479 | if( db->nVdbeActive==0 ){ |
drh | 15ca1df | 2006-07-26 13:43:30 +0000 | [diff] [blame] | 480 | db->u1.isInterrupted = 0; |
| 481 | } |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 482 | pParse->rc = SQLITE_OK; |
drh | e7f3f3e | 2009-07-03 22:54:36 +0000 | [diff] [blame] | 483 | pParse->zTail = zSql; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 484 | i = 0; |
drh | fb45d8c | 2008-07-08 00:06:49 +0000 | [diff] [blame] | 485 | assert( pzErrMsg!=0 ); |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 486 | /* sqlite3ParserTrace(stdout, "parser: "); */ |
drh | fb046e7 | 2014-09-12 04:28:33 +0000 | [diff] [blame] | 487 | pEngine = sqlite3ParserAlloc(sqlite3Malloc); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 488 | if( pEngine==0 ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 489 | sqlite3OomFault(db); |
drh | defc997 | 2005-06-06 14:45:42 +0000 | [diff] [blame] | 490 | return SQLITE_NOMEM; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 491 | } |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 492 | assert( pParse->pNewTable==0 ); |
| 493 | assert( pParse->pNewTrigger==0 ); |
| 494 | assert( pParse->nVar==0 ); |
drh | 124c0b4 | 2011-06-01 18:15:55 +0000 | [diff] [blame] | 495 | assert( pParse->nzVar==0 ); |
| 496 | assert( pParse->azVar==0 ); |
drh | 0739e45 | 2015-11-09 02:08:09 +0000 | [diff] [blame] | 497 | while( zSql[i]!=0 ){ |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 498 | assert( i>=0 ); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 499 | pParse->sLastToken.z = &zSql[i]; |
drh | 35b5a33 | 2008-04-05 18:41:42 +0000 | [diff] [blame] | 500 | pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 501 | i += pParse->sLastToken.n; |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 502 | if( i>mxSqlLen ){ |
drh | e5c941b | 2007-05-08 13:58:26 +0000 | [diff] [blame] | 503 | pParse->rc = SQLITE_TOOBIG; |
| 504 | break; |
| 505 | } |
drh | 0739e45 | 2015-11-09 02:08:09 +0000 | [diff] [blame] | 506 | if( tokenType>=TK_SPACE ){ |
| 507 | assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL ); |
| 508 | if( db->u1.isInterrupted ){ |
drh | 0739e45 | 2015-11-09 02:08:09 +0000 | [diff] [blame] | 509 | pParse->rc = SQLITE_INTERRUPT; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 510 | break; |
| 511 | } |
drh | 0739e45 | 2015-11-09 02:08:09 +0000 | [diff] [blame] | 512 | if( tokenType==TK_ILLEGAL ){ |
drh | 347bdc3 | 2015-04-15 07:57:27 +0000 | [diff] [blame] | 513 | sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"", |
drh | fb45d8c | 2008-07-08 00:06:49 +0000 | [diff] [blame] | 514 | &pParse->sLastToken); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 515 | break; |
drh | 32eb7b4 | 2003-01-07 01:44:37 +0000 | [diff] [blame] | 516 | } |
drh | 0739e45 | 2015-11-09 02:08:09 +0000 | [diff] [blame] | 517 | }else{ |
| 518 | if( tokenType==TK_SEMI ) pParse->zTail = &zSql[i]; |
| 519 | sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse); |
| 520 | lastTokenParsed = tokenType; |
| 521 | if( pParse->rc!=SQLITE_OK || db->mallocFailed ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 522 | } |
| 523 | } |
drh | 0be0cf6 | 2015-04-15 08:37:42 +0000 | [diff] [blame] | 524 | assert( nErr==0 ); |
drh | e726622 | 2015-05-29 17:51:16 +0000 | [diff] [blame] | 525 | if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){ |
| 526 | assert( zSql[i]==0 ); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 527 | if( lastTokenParsed!=TK_SEMI ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 528 | sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse); |
drh | a248a72 | 2015-09-07 19:52:55 +0000 | [diff] [blame] | 529 | pParse->zTail = &zSql[i]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 530 | } |
drh | 9a9705d | 2015-04-25 00:32:30 +0000 | [diff] [blame] | 531 | if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){ |
| 532 | sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse); |
| 533 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 534 | } |
drh | ec424a5 | 2008-07-25 15:39:03 +0000 | [diff] [blame] | 535 | #ifdef YYTRACKMAXSTACKDEPTH |
drh | af89fe6 | 2015-03-23 17:25:18 +0000 | [diff] [blame] | 536 | sqlite3_mutex_enter(sqlite3MallocMutex()); |
drh | b02392e | 2015-10-15 15:28:56 +0000 | [diff] [blame] | 537 | sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK, |
drh | ec424a5 | 2008-07-25 15:39:03 +0000 | [diff] [blame] | 538 | sqlite3ParserStackPeak(pEngine) |
| 539 | ); |
drh | af89fe6 | 2015-03-23 17:25:18 +0000 | [diff] [blame] | 540 | sqlite3_mutex_leave(sqlite3MallocMutex()); |
drh | ec424a5 | 2008-07-25 15:39:03 +0000 | [diff] [blame] | 541 | #endif /* YYDEBUG */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 542 | sqlite3ParserFree(pEngine, sqlite3_free); |
| 543 | if( db->mallocFailed ){ |
drh | 71c697e | 2004-08-08 23:39:19 +0000 | [diff] [blame] | 544 | pParse->rc = SQLITE_NOMEM; |
| 545 | } |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 546 | if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){ |
drh | 22c17b8 | 2015-05-15 04:13:15 +0000 | [diff] [blame] | 547 | pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc)); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 548 | } |
drh | 19f81f6 | 2009-06-09 18:01:37 +0000 | [diff] [blame] | 549 | assert( pzErrMsg!=0 ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 550 | if( pParse->zErrMsg ){ |
drh | 19f81f6 | 2009-06-09 18:01:37 +0000 | [diff] [blame] | 551 | *pzErrMsg = pParse->zErrMsg; |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 552 | sqlite3_log(pParse->rc, "%s", *pzErrMsg); |
drh | b86ccfb | 2003-01-28 23:13:10 +0000 | [diff] [blame] | 553 | pParse->zErrMsg = 0; |
drh | 4b2f936 | 2008-01-22 23:37:09 +0000 | [diff] [blame] | 554 | nErr++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 555 | } |
drh | 2958a4e | 2004-11-12 03:56:15 +0000 | [diff] [blame] | 556 | if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 557 | sqlite3VdbeDelete(pParse->pVdbe); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 558 | pParse->pVdbe = 0; |
| 559 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 560 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 561 | if( pParse->nested==0 ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 562 | sqlite3DbFree(db, pParse->aTableLock); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 563 | pParse->aTableLock = 0; |
| 564 | pParse->nTableLock = 0; |
| 565 | } |
| 566 | #endif |
drh | 4f3dd15 | 2008-04-28 18:46:43 +0000 | [diff] [blame] | 567 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 568 | sqlite3_free(pParse->apVtabLock); |
drh | 4f3dd15 | 2008-04-28 18:46:43 +0000 | [diff] [blame] | 569 | #endif |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 570 | |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 571 | if( !IN_DECLARE_VTAB ){ |
| 572 | /* If the pParse->declareVtab flag is set, do not delete any table |
| 573 | ** structure built up in pParse->pNewTable. The calling code (see vtab.c) |
| 574 | ** will take responsibility for freeing the Table structure. |
| 575 | */ |
dan | 1feeaed | 2010-07-23 15:41:47 +0000 | [diff] [blame] | 576 | sqlite3DeleteTable(db, pParse->pNewTable); |
danielk1977 | 7e6ebfb | 2006-06-12 11:24:37 +0000 | [diff] [blame] | 577 | } |
| 578 | |
drh | 6e77226 | 2015-11-07 17:48:21 +0000 | [diff] [blame] | 579 | sqlite3WithDelete(db, pParse->pWithToFree); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 580 | sqlite3DeleteTrigger(db, pParse->pNewTrigger); |
drh | 124c0b4 | 2011-06-01 18:15:55 +0000 | [diff] [blame] | 581 | for(i=pParse->nzVar-1; i>=0; i--) sqlite3DbFree(db, pParse->azVar[i]); |
| 582 | sqlite3DbFree(db, pParse->azVar); |
drh | 0b9f50d | 2009-06-23 20:28:53 +0000 | [diff] [blame] | 583 | while( pParse->pAinc ){ |
| 584 | AutoincInfo *p = pParse->pAinc; |
| 585 | pParse->pAinc = p->pNext; |
| 586 | sqlite3DbFree(db, p); |
| 587 | } |
drh | 588a9a1 | 2008-09-01 15:52:10 +0000 | [diff] [blame] | 588 | while( pParse->pZombieTab ){ |
| 589 | Table *p = pParse->pZombieTab; |
| 590 | pParse->pZombieTab = p->pNextZombie; |
dan | 1feeaed | 2010-07-23 15:41:47 +0000 | [diff] [blame] | 591 | sqlite3DeleteTable(db, p); |
drh | 588a9a1 | 2008-09-01 15:52:10 +0000 | [diff] [blame] | 592 | } |
drh | f3151f0 | 2015-04-16 20:27:09 +0000 | [diff] [blame] | 593 | assert( nErr==0 || pParse->rc!=SQLITE_OK ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 594 | return nErr; |
| 595 | } |